Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1180889rwl; Fri, 24 Mar 2023 07:17:35 -0700 (PDT) X-Google-Smtp-Source: AK7set+RPvM6hM2JRSjQomBn1Gbts1ENUzXtCGynNvm9Fm9McffoUpbrSPYXAhu9fwPhanr4PVUu X-Received: by 2002:a05:6a20:4a13:b0:db:e054:826a with SMTP id fr19-20020a056a204a1300b000dbe054826amr2589113pzb.5.1679667455040; Fri, 24 Mar 2023 07:17:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679667455; cv=none; d=google.com; s=arc-20160816; b=e3Jaeg2dSPInwmG5LkSDBCFN4jHozHxhfpSue54dbHmy1SC3q9prj1aDedZMqvJixH BWcoxFcL4ZfM6zXkd8AwDxjlAuv2ntPJe9kvsiaZ6+ym+OozrMiEpBnH49fsSuv1ERm6 TekkuXlNr78R+48VOAU2rHaiuPOTERIZ2kbNt/Qw+MVcolc6dR2R8Qhi14BTi1gus1Yr FEFv/KVtoS/H5YsVGqbn8wCXLywJ8/Fuby0iyzfkX9e1GgEtcEVPxLJDqeTMdE/50iA2 ADE4KWiZv0hEI5UHU92lh4qptsjVOOiyAW5+3qSedmxbVP0vkE62P5CIOEhp5iW4YKfq z1KQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=67+EwHu+QToRo/sF9RBeGijI61E5QqD34PaUertJaVA=; b=WUTC0/a91AJ5BgGn5qXCBcLrDzAtl+iBQV0Dg20Q1tm4l5vEaxMyq7WzZCte79Iav3 nQo4oHCPvOdJQD5qnJqt9rEIqePcSY+0o0cu6j6FEG8vEmdlM0ghnak3ACJcDIWTKrFE LeFIbijV9b1zF0mFq/8OeSq/Liy1j9yPDZz5917AokfAPhs7erTsis2ld86dolYRdPrV 8u/SDdTJIBDFbh5m+KuKa+ekI9lqomdSu15ftwrUgNYtZ9SBz871VmGEsIcxnZrX8JK6 QDZ+OG+5QNx2IHeJ2K8pJ9OUOvrOM2rZehJE1tiC1kKkslVacMrrPffTxcHT9AfSMDeZ XvqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hOGVtVu3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y37-20020a634965000000b0050b1994efafsi21000765pgk.877.2023.03.24.07.17.11; Fri, 24 Mar 2023 07:17:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hOGVtVu3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231801AbjCXOMY (ORCPT + 99 others); Fri, 24 Mar 2023 10:12:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231974AbjCXOMW (ORCPT ); Fri, 24 Mar 2023 10:12:22 -0400 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 F348DE3AB for ; Fri, 24 Mar 2023 07:11:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679667098; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=67+EwHu+QToRo/sF9RBeGijI61E5QqD34PaUertJaVA=; b=hOGVtVu3gpMfSvCQ1rmQWHPOK4CgDQGLWzVyVFSy7Km/9RKVO397lr6onpSiSsyF9tHFgA Ut3RYXITORdjOnLoNhNXmwlFnoWT9bSFlu4zemJoXbh3XEBpk5W+4fWvC3+Ocr9cngXF8r P0MoTRO0MdwT47KfbsDgjw77VWApY74= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-284-i16Q4nQ2NxGn4M_bOHTxPA-1; Fri, 24 Mar 2023 10:11:34 -0400 X-MC-Unique: i16Q4nQ2NxGn4M_bOHTxPA-1 Received: by mail-qk1-f199.google.com with SMTP id b34-20020a05620a272200b007460c05a463so837622qkp.1 for ; Fri, 24 Mar 2023 07:11:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679667094; x=1682259094; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=67+EwHu+QToRo/sF9RBeGijI61E5QqD34PaUertJaVA=; b=KffXR272eDHGI3NEUi44QySi3OBVCLi4OytGY/vv00Dot+nCYZeGOl8tpEoY2Dy4Rb /pAJAy/08tYEyxYt9oSnb+zxCsWPQE576gc7m4ZvWj7+161fHaDEdq3Vje89C+L9WB9n TEzvG1iKpKqiTi6OLH+Ovbkbc9NbB0xtT36l6dqO7EqZNg7mhxqmnGhZdpmrlvAeOWTq XeWPhIIgVtYpPHwNq3B6gspr2c8a2LJH2Og7pM9j24no7ycIm0m2ZabX0vLFaNRw2+A1 BY0fALn+FPD8PCVUNk24W8u9lzv6OZdgW7P+Rjx9BuW6mhJc/pTTBxcDZP74xyJwDXNa sZeA== X-Gm-Message-State: AO0yUKVXTOpZY9xyUY7rWV/WfOmJG9rEHI3ppc3KndfZPg+lRT0zbiiq yStfQRVWIcrH6NtM7mlHJYSfcZWJh6dqU7YNNKLZlYWpLdp6qOTx3/5XkwEshxoRjJ+V/nIiMQx sOWppfCg7kK57OR/WaAJnJiod X-Received: by 2002:a05:622a:1829:b0:3e3:8f8c:b92f with SMTP id t41-20020a05622a182900b003e38f8cb92fmr5523898qtc.5.1679667094001; Fri, 24 Mar 2023 07:11:34 -0700 (PDT) X-Received: by 2002:a05:622a:1829:b0:3e3:8f8c:b92f with SMTP id t41-20020a05622a182900b003e38f8cb92fmr5523859qtc.5.1679667093676; Fri, 24 Mar 2023 07:11:33 -0700 (PDT) Received: from x1n (bras-base-aurron9127w-grc-40-70-52-229-124.dsl.bell.ca. [70.52.229.124]) by smtp.gmail.com with ESMTPSA id c9-20020ac80549000000b003d8d1ec2672sm12584782qth.89.2023.03.24.07.11.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 07:11:33 -0700 (PDT) Date: Fri, 24 Mar 2023 10:11:31 -0400 From: Peter Xu To: David Hildenbrand Cc: Muhammad Usama Anjum , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrea Arcangeli , Andrew Morton , Axel Rasmussen , Mike Rapoport , Nadav Amit , linux-stable Subject: Re: [PATCH] mm/hugetlb: Fix uffd wr-protection for CoW optimization path Message-ID: References: <20230321191840.1897940-1-peterx@redhat.com> <44aae7fc-fb1f-b38e-bc17-504abf054e3f@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 24, 2023 at 09:51:27AM +0100, David Hildenbrand wrote: > On 23.03.23 23:11, Peter Xu wrote: > > On Thu, Mar 23, 2023 at 08:33:07PM +0500, Muhammad Usama Anjum wrote: > > > Hi Peter, > > > > > > Sorry for late reply. > > > > > > On 3/22/23 12:50 AM, Peter Xu wrote: > > > > On Tue, Mar 21, 2023 at 08:36:35PM +0100, David Hildenbrand wrote: > > > > > On 21.03.23 20:18, Peter Xu wrote: > > > > > > This patch fixes an issue that a hugetlb uffd-wr-protected mapping can be > > > > > > writable even with uffd-wp bit set. It only happens with all these > > > > > > conditions met: (1) hugetlb memory (2) private mapping (3) original mapping > > > > > > was missing, then (4) being wr-protected (IOW, pte marker installed). Then > > > > > > write to the page to trigger. > > > > > > > > > > > > Userfaultfd-wp trap for hugetlb was implemented in hugetlb_fault() before > > > > > > even reaching hugetlb_wp() to avoid taking more locks that userfault won't > > > > > > need. However there's one CoW optimization path for missing hugetlb page > > > > > > that can trigger hugetlb_wp() inside hugetlb_no_page(), that can bypass the > > > > > > userfaultfd-wp traps. > > > > > > > > > > > > A few ways to resolve this: > > > > > > > > > > > > (1) Skip the CoW optimization for hugetlb private mapping, considering > > > > > > that private mappings for hugetlb should be very rare, so it may not > > > > > > really be helpful to major workloads. The worst case is we only skip the > > > > > > optimization if userfaultfd_wp(vma)==true, because uffd-wp needs another > > > > > > fault anyway. > > > > > > > > > > > > (2) Move the userfaultfd-wp handling for hugetlb from hugetlb_fault() > > > > > > into hugetlb_wp(). The major cons is there're a bunch of locks taken > > > > > > when calling hugetlb_wp(), and that will make the changeset unnecessarily > > > > > > complicated due to the lock operations. > > > > > > > > > > > > (3) Carry over uffd-wp bit in hugetlb_wp(), so it'll need to fault again > > > > > > for uffd-wp privately mapped pages. > > > > > > > > > > > > This patch chose option (3) which contains the minimum changeset (simplest > > > > > > for backport) and also make sure hugetlb_wp() itself will start to be > > > > > > always safe with uffd-wp ptes even if called elsewhere in the future. > > > > > > > > > > > > This patch will be needed for v5.19+ hence copy stable. > > > > > > > > > > > > Reported-by: Muhammad Usama Anjum > > > > > > Cc: linux-stable > > > > > > Fixes: 166f3ecc0daf ("mm/hugetlb: hook page faults for uffd write protection") > > > > > > Signed-off-by: Peter Xu > > > > > > --- > > > > > > mm/hugetlb.c | 8 +++++--- > > > > > > 1 file changed, 5 insertions(+), 3 deletions(-) > > > > > > > > > > > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > > > > > > index 8bfd07f4c143..22337b191eae 100644 > > > > > > --- a/mm/hugetlb.c > > > > > > +++ b/mm/hugetlb.c > > > > > > @@ -5478,7 +5478,7 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, > > > > > > struct folio *pagecache_folio, spinlock_t *ptl) > > > > > > { > > > > > > const bool unshare = flags & FAULT_FLAG_UNSHARE; > > > > > > - pte_t pte; > > > > > > + pte_t pte, newpte; > > > > > > struct hstate *h = hstate_vma(vma); > > > > > > struct page *old_page; > > > > > > struct folio *new_folio; > > > > > > @@ -5622,8 +5622,10 @@ static vm_fault_t hugetlb_wp(struct mm_struct *mm, struct vm_area_struct *vma, > > > > > > mmu_notifier_invalidate_range(mm, range.start, range.end); > > > > > > page_remove_rmap(old_page, vma, true); > > > > > > hugepage_add_new_anon_rmap(new_folio, vma, haddr); > > > > > > - set_huge_pte_at(mm, haddr, ptep, > > > > > > - make_huge_pte(vma, &new_folio->page, !unshare)); > > > > > > + newpte = make_huge_pte(vma, &new_folio->page, !unshare); > > > > > > + if (huge_pte_uffd_wp(pte)) > > > > > > + newpte = huge_pte_mkuffd_wp(newpte); > > > > > > + set_huge_pte_at(mm, haddr, ptep, newpte); > > > > > > folio_set_hugetlb_migratable(new_folio); > > > > > > /* Make the old page be freed below */ > > > > > > new_folio = page_folio(old_page); > > > > > > > > > > Looks correct to me. Do we have a reproducer? > > > > > > > > I used a reproducer for the async mode I wrote (patch 2 attached, need to > > > > change to VM_PRIVATE): > > > > > > > > https://lore.kernel.org/all/ZBNr4nohj%2FTw4Zhw@x1n/ > > > > > > > > I don't think kernel kselftest can trigger it because we don't do strict > > > > checks yet with uffd-wp bits. I've already started looking into cleanup > > > > the test cases and I do plan to add new tests to cover this. > > > > > > > > Meanwhile, let's also wait for an ack from Muhammad. Even though the async > > > > mode is not part of the code base, it'll be a good test for verifying every > > > > single uffd-wp bit being set or cleared as expected. > > > I've tested by applying this patch. But the bug is still there. Just like > > > Peter has mentioned, we are using our in progress patches related to > > > pagemap_scan ioctl and userfaultd wp async patches to reproduce it. > > > > > > To reproduce please build kernel and run pagemap_ioctl test in mm in > > > hugetlb_mem_reproducer branch: > > > https://gitlab.collabora.com/usama.anjum/linux-mainline/-/tree/hugetlb_mem_reproducer > > > > > > In case you have any question on how to reproduce, please let me know. I'll > > > try to provide a cleaner alternative. > > > > Hmm, I think my current fix is incomplete if not wrong. The root cause > > should still be valid, however I overlooked another path: > > > > if (page_mapcount(old_page) == 1 && PageAnon(old_page)) { > > if (!PageAnonExclusive(old_page)) > > page_move_anon_rmap(old_page, vma); > > if (likely(!unshare)) > > set_huge_ptep_writable(vma, haddr, ptep); > > > > delayacct_wpcopy_end(); > > return 0; > > } > > > > We should bail out early in this path, and it'll be even easier we always > > bail out hugetlb_wp() as long as uffd-wp is detected because userfault > > should always be handled before any decision to CoW. > > > > v2 attached.. Please give it another shot. > > Hmmm, I think you must only do that for !unshare (FAULT_FLAG_WRITE). > Otherwise you'll never be able to resolve an unsharing request on a r/o > mapped hugetlb page that has the uffd-wp set? > > Or am I missing something? No, I think you're right. I'll fix that up when posting a v3. Thanks, -- Peter Xu