Message ID | 20231123180222.1048297-1-peterx@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp620296vqx; Thu, 23 Nov 2023 10:02:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IHtx8Wy3FyLtwaxHTlHN3PaRr/oOIMUqFh5YxO59ZwfUmfgyzwD1eHqexmM+HZaqdTia0ZY X-Received: by 2002:a17:90b:3882:b0:27d:8ad:c4e1 with SMTP id mu2-20020a17090b388200b0027d08adc4e1mr247728pjb.19.1700762565454; Thu, 23 Nov 2023 10:02:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700762565; cv=none; d=google.com; s=arc-20160816; b=VowiGR/DfT/PAWCOBtg98cjR3X75rESCKlTCvH8GCSzK9wdzcBourFNwMVDfN3N6na rjVg21x/yBRCQk7gpI3VO3Fk09+nk8aKj8pRfjVC+Pu945B51y7OYf3p9FAo9nw4463F w9ESYsVivXE6rv0aQQ4r+OnnR6hztwU+JKpy9iXYLlx10QuQIlSjalnxmoaHI9IpT1oQ LG4+Pyw/p6859qpc10UvWz8BmvVFsI9hD8fEkT0IaoYoNNkf3e03jD7F8Dvi01cTj6Xu Z03LgXLhzuCRVVOpIfXGQh2Keo+Z+u1zlSYlB6lfjUXZ4RlpjSHL8J+TyUy1vMZsXWQD xKGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=4EEXViqB805TEJLl95Oc48y0HOh1LGI/lPszglbnn4k=; fh=1h37GgTKWT2dcNXRShUqFifROqUVIkVTBR9DaxLTIOA=; b=0SII2KjFig7bY0+ttPgJBUTqISt2vkekvyOdbTOFgitu6LmyIO13GyA/NkDQ1JtRVf u0JZsdH3+d7X0ZCVSHkkVgn46s/izrl+m86yExCTpwBqFCHrdRPZoEazoCqNmgFe5GH5 cC+WgdA0/2CKTMMZnHdxwVHuXaXq3Hq0Tfs/gJf89iCuZk8M2/vVONNFOd6vtk9dI9OP UkshCFmMBUziFf/LwOoQ1viTNxVSwLBsK4lui/fSs2ID3g4QH+xtGgnMWVEp1SjwmL39 /3Jy5n0GvStN4QPrOW8uMXT6QWfZ429WRjTeUfyay3cKCyrJydxDb932t2R9ZZnUcVOl yyVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iQM2FzLO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id d22-20020a17090ad3d600b00283a1ef6feasi2245503pjw.147.2023.11.23.10.02.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:02:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iQM2FzLO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 2ED9B809B9D0; Thu, 23 Nov 2023 10:02:31 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229981AbjKWSCW (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Thu, 23 Nov 2023 13:02:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229462AbjKWSCV (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 23 Nov 2023 13:02:21 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8FF3D41 for <linux-kernel@vger.kernel.org>; Thu, 23 Nov 2023 10:02:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700762546; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=4EEXViqB805TEJLl95Oc48y0HOh1LGI/lPszglbnn4k=; b=iQM2FzLOiwoYKUGqIMdAHh3rSeG6/y1SK/SWyDhqrOyVm9itztVZn9boGg1BlyZbLVsk/S MfGDcU56elmktZEQ408DvZJ99qt6jo59swYjG7R80WRtxISLG09xmrmoNglrURmJu/Yqe+ b1TUIQx9f4wMiRrVXna+hTEaUaHwPBw= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-126-aewL9pyOOc-jHFR0DY4Hdw-1; Thu, 23 Nov 2023 13:02:25 -0500 X-MC-Unique: aewL9pyOOc-jHFR0DY4Hdw-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-67a05428cceso2146146d6.0 for <linux-kernel@vger.kernel.org>; Thu, 23 Nov 2023 10:02:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700762544; x=1701367344; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4EEXViqB805TEJLl95Oc48y0HOh1LGI/lPszglbnn4k=; b=Pm+zGSrbyvWYMCO2e/AxE1OdkIG5pp94liJKQ3/jNPIlpXLWaGh5OnhPVRGHX76feo YaZ3E3zfdhcoxDwaiI4COYbZwkrybXMVMW/lZCQbuwNGZfBhh7JYtdik+3D5NiuJnIQ6 B/df5fuCXLx2HEmkzPxU3J6PzaGtuT2G1Dtrx85/CSPlUBox8JhdsF+99SSp8zPOmGHP KQWy7Qde7re00dUxq0naXfsQLffOdQk1COwJd17NKvmDysNMdfdVMvT5GDTLCQEWX0ou W7E4Aob14HGt19sUagFjKc4GVNDYuJ1bOdXnobI16+U9P/CaDxBp6JQ+AIaC/+RiRck/ Q2Rg== X-Gm-Message-State: AOJu0YyDllhig5Rk+hCzbL4nu7OKnaDq25uwpfCaAp1Tg7j9Ah9eAoYa QWQEVVuT65p8ZEGeFVD8+p23z97rOHKc90WzaVHGRi4fjiSarBqRUpKutBlSDCGMUvr49JAKDvi MwIJkCLRQ4WYhksKD79yCPljrS2m+yQse X-Received: by 2002:ad4:420f:0:b0:679:d8a9:ef41 with SMTP id k15-20020ad4420f000000b00679d8a9ef41mr273790qvp.0.1700762544394; Thu, 23 Nov 2023 10:02:24 -0800 (PST) X-Received: by 2002:ad4:420f:0:b0:679:d8a9:ef41 with SMTP id k15-20020ad4420f000000b00679d8a9ef41mr273772qvp.0.1700762544131; Thu, 23 Nov 2023 10:02:24 -0800 (PST) Received: from x1n.redhat.com (cpe688f2e2cb7c3-cm688f2e2cb7c0.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id s2-20020a0cdc02000000b0065af9d1203dsm693210qvk.121.2023.11.23.10.02.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 10:02:23 -0800 (PST) From: Peter Xu <peterx@redhat.com> To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: peterx@redhat.com, Andrew Morton <akpm@linux-foundation.org>, Dan Williams <dan.j.williams@intel.com>, Mel Gorman <mgorman@suse.de>, Matthew Wilcox <willy@infradead.org>, "Aneesh Kumar K . V" <aneesh.kumar@linux.vnet.ibm.com>, Christoph Hellwig <hch@lst.de> Subject: [PATCH] mm/gup: Fix follow_devmap_p[mu]d() on page==NULL handling Date: Thu, 23 Nov 2023 13:02:22 -0500 Message-ID: <20231123180222.1048297-1-peterx@redhat.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 23 Nov 2023 10:02:31 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783378807507962092 X-GMAIL-MSGID: 1783378807507962092 |
Series |
mm/gup: Fix follow_devmap_p[mu]d() on page==NULL handling
|
|
Commit Message
Peter Xu
Nov. 23, 2023, 6:02 p.m. UTC
This is a bug found not by any report but only by code observations.
When GUP sees a devpmd/devpud and if page==NULL is returned, it means a
fault is probably required. Here falling through when page==NULL can cause
unexpected behavior.
Fix both cases by catching the page==NULL cases with no_page_table().
Fixes: 3565fce3a659 ("mm, x86: get_user_pages() for dax mappings")
Fixes: 080dbb618b4b ("mm/follow_page_mask: split follow_page_mask to smaller functions.")
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
mm/gup.c | 2 ++
1 file changed, 2 insertions(+)
Comments
On Thu, 23 Nov 2023 13:02:22 -0500 Peter Xu <peterx@redhat.com> wrote: > This is a bug found not by any report but only by code observations. > > When GUP sees a devpmd/devpud and if page==NULL is returned, it means a > fault is probably required. Here falling through when page==NULL can cause > unexpected behavior. > Well this is worrisome. We aren't able to construct a test case to demonstrate this bug? Why is that? Is it perhaps just dead code?
On Fri, Nov 24, 2023 at 11:20:59AM -0800, Andrew Morton wrote: > On Thu, 23 Nov 2023 13:02:22 -0500 Peter Xu <peterx@redhat.com> wrote: > > > This is a bug found not by any report but only by code observations. > > > > When GUP sees a devpmd/devpud and if page==NULL is returned, it means a > > fault is probably required. Here falling through when page==NULL can cause > > unexpected behavior. > > > > Well this is worrisome. We aren't able to construct a test case to > demonstrate this bug? Why is that? Is it perhaps just dead code? IIUC it's not dead code. Take the example of follow_devmap_pmd(), it can return page==NULL at least when seeing write bit missing: if (flags & FOLL_WRITE && !pmd_write(*pmd)) return NULL; AFAICT it can happen if someone does "echo 4 > /proc/$PID/clear_refs" when the mm contains the devmap pmd. Same to pud. It'll be nice if someone that works with dax would like to verify it. In my series (refactor hugetlb gup, part 2) IIUC some hugetlb selftest can start to trigger this path, but I'll need to check. So far it's dax-only. Thanks,
On 26.11.23 22:55, Peter Xu wrote: > On Fri, Nov 24, 2023 at 11:20:59AM -0800, Andrew Morton wrote: >> On Thu, 23 Nov 2023 13:02:22 -0500 Peter Xu <peterx@redhat.com> wrote: >> >>> This is a bug found not by any report but only by code observations. >>> >>> When GUP sees a devpmd/devpud and if page==NULL is returned, it means a >>> fault is probably required. Here falling through when page==NULL can cause >>> unexpected behavior. >>> >> >> Well this is worrisome. We aren't able to construct a test case to >> demonstrate this bug? Why is that? Is it perhaps just dead code? > > IIUC it's not dead code. Take the example of follow_devmap_pmd(), it can > return page==NULL at least when seeing write bit missing: > > if (flags & FOLL_WRITE && !pmd_write(*pmd)) > return NULL; > > AFAICT it can happen if someone does "echo 4 > /proc/$PID/clear_refs" when > the mm contains the devmap pmd. Same to pud. > > It'll be nice if someone that works with dax would like to verify it. In > my series (refactor hugetlb gup, part 2) IIUC some hugetlb selftest can > start to trigger this path, but I'll need to check. So far it's dax-only. It certainly looks weird to continue there. Triggering it by mmaping some devdax device might be possible (e.g., using devdax emulation). We know the PMD is present and the PMD is devmap. We take the pmd lock, and in follow_devmap_pmd() we recheck both. I suspect the original idea was: if it's suddenly no longer present or no longer devmap, it was replaced by a PTE table. So we know a deeper level is there and can simply continue instead of triggering a fault. But that does not seem to be the case, because I suspect the PMD could have been zapped (MADV_DONTNEED?) in the meantime, and the "writability" check is similarly weird. So I assume the patch from Peter is ok: even if the PMD got replaced by a PTE table, we'd trigger a fault and simply retry. Acked-by: David Hildenbrand <david@redhat.com>
diff --git a/mm/gup.c b/mm/gup.c index 231711efa390..0a5f0e91bfec 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -710,6 +710,7 @@ static struct page *follow_pmd_mask(struct vm_area_struct *vma, spin_unlock(ptl); if (page) return page; + return no_page_table(vma, flags); } if (likely(!pmd_trans_huge(pmdval))) return follow_page_pte(vma, address, pmd, flags, &ctx->pgmap); @@ -758,6 +759,7 @@ static struct page *follow_pud_mask(struct vm_area_struct *vma, spin_unlock(ptl); if (page) return page; + return no_page_table(vma, flags); } if (unlikely(pud_bad(*pud))) return no_page_table(vma, flags);