Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp871575rwl; Wed, 5 Apr 2023 08:46:08 -0700 (PDT) X-Google-Smtp-Source: AKy350ZBiK2aeQgjh2J5T5dgBoZQXCzMNDvW+6D4K1rU7KN48kGMhhc5tgYgla/e23WmVBFQIEwu X-Received: by 2002:a17:90a:1042:b0:237:3dfb:9095 with SMTP id y2-20020a17090a104200b002373dfb9095mr7313640pjd.6.1680709567832; Wed, 05 Apr 2023 08:46:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680709567; cv=none; d=google.com; s=arc-20160816; b=zJ6eHPiTCYWj+f12Fcuts08Q4uxcYMZVOH0sxUhgAzdmoH5c0qB5KmCy0W/gCxWStv H+CylSEDwdcez3yhBjDuHVL6zLdJM2YGpbjD1/Ysr+JWZLmTrG3fX7yXW4ZfsZNnPh3k UPl0kcLf6iQVSVDOprpeEEkGAFBiRZDXHwcpEHbfTyiNfXc9IhGWh5exACuHYNRbR75e 1tOqTscSCFRTI/JL/a5CkvQfXXyPznHzdhyi1zlLd96CpfWKaOJkS9d7lu0oDXFoBHOq pD6lZ7yhOaRiA6Ytx+B6U9xb2UiR4s08uhFjRtye6Enzi4H5KO+ltd3pIkU3F8DG7rFm 2xXA== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=9+5VtQajWJ6aryPiByZHs9cjsreXsrWr3HePjo1dmYw=; b=jOc8z3u+okqVOQKEr2QIKeOv314GUsKYZKzj6Lyz5Y6AW5z0qvAFYJMM/scLDBspTA +FmoQruUW21brxF1PNJNCwf/RL+0jRdU833y7RoZx6FljAmFT6tWGsH5zcpLu2ndFQ2g wghgsO1pKL4GdauVyRvVtDqmu6grvYb7KUybP2hszYX6hmeiBnb1IWvOxH3UmJrjOMnf UrKkKiCcGiWsdUxOHt0YkwHTZW/tOLF7S90EkBFnNLfrZjcXRy8msCtfeiRKjsiIrMe/ I+hfBEltK8XllDkTV/EJ0RuEBVHciYQVLmt5Mb9p+C6+6QkF7SkAdof2UxT9g+icR980 u3hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QgWGo3uT; 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 mu15-20020a17090b388f00b0023f61c7570bsi1574442pjb.163.2023.04.05.08.45.55; Wed, 05 Apr 2023 08:46:07 -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=QgWGo3uT; 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 S238914AbjDEPpW (ORCPT + 99 others); Wed, 5 Apr 2023 11:45:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238944AbjDEPpU (ORCPT ); Wed, 5 Apr 2023 11:45:20 -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 D65D16EAB for ; Wed, 5 Apr 2023 08:44:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680709403; 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: in-reply-to:in-reply-to:references:references; bh=9+5VtQajWJ6aryPiByZHs9cjsreXsrWr3HePjo1dmYw=; b=QgWGo3uTGrQ0lAOnYiXbcXaxemF29bGFSwn0KLnDFv6yLoj0VxrTT9Vq43rWPI1Y98gyO8 VII8yZSBU2DT9omSEKHq2E0c3E+rfT3iyUd2DQdmyKO6bI6K8z5zhdEODv4IMoCbAJ3ked +p0dIbkgs4KYmtVix+UzmSuo1XbrW5Q= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-508-HpkldPjZP26aNWsVjHhn_g-1; Wed, 05 Apr 2023 11:43:22 -0400 X-MC-Unique: HpkldPjZP26aNWsVjHhn_g-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-3e2daffa0d4so12274021cf.0 for ; Wed, 05 Apr 2023 08:43:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680709402; h=in-reply-to: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=9+5VtQajWJ6aryPiByZHs9cjsreXsrWr3HePjo1dmYw=; b=18BDov9mflJaTlNHD+wxvi8lW0jnOq/jeKEw8lVTLWWIx/uguYIRKr92Nn6qkB4iOU BqhWrHC1dQTzpeJR8ze20nZcmuos+ytOHvhSJcO/KpcOM0gc80iQnFIaNZylE2u2hpan pSV8FUzMJF3dYzTMkQgBm0hzIeI4RzTCaSv+9Zt1XKeg2Fr6WevfO7CdIZKpsg5ez4jF l2qMkM3z+w2mFShy/xh0u25Sqm5+UqyTTAtiNrrKrYaK0jIv5uEtdlMUz0XBfnfvlNGU OPzAtO9FfJYh1vpak+O33cTH6J+fjRCU4cdn2EFOL8n1xxImz8IiDfOm0ClVl+Jx8qm9 TW/A== X-Gm-Message-State: AAQBX9f9gVTXkC8Try/5c89l4sF4SVtVgeUQas8hGckLclV81/jjNKmK 5enrAMy8Q2nk7lpk3gfUwhuh6y8fnjtS/p18yuToDdzMLuBJE5ux3SedBLH6pTie/VWDR95lu8l XY8pc1v2sXX7/MqkVPvaRk27M X-Received: by 2002:a05:6214:508f:b0:5aa:14b8:e935 with SMTP id kk15-20020a056214508f00b005aa14b8e935mr5332692qvb.2.1680709401929; Wed, 05 Apr 2023 08:43:21 -0700 (PDT) X-Received: by 2002:a05:6214:508f:b0:5aa:14b8:e935 with SMTP id kk15-20020a056214508f00b005aa14b8e935mr5332654qvb.2.1680709401524; Wed, 05 Apr 2023 08:43:21 -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 f24-20020ac84658000000b003b9a73cd120sm4059709qto.17.2023.04.05.08.43.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 08:43:21 -0700 (PDT) Date: Wed, 5 Apr 2023 11:43:19 -0400 From: Peter Xu To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Muhammad Usama Anjum , stable@vger.kernel.org Subject: Re: [PATCH v1 1/2] mm/userfaultfd: fix uffd-wp handling for THP migration entries Message-ID: References: <20230405142535.493854-1-david@redhat.com> <20230405142535.493854-2-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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 Wed, Apr 05, 2023 at 05:17:31PM +0200, David Hildenbrand wrote: > On 05.04.23 17:12, Peter Xu wrote: > > On Wed, Apr 05, 2023 at 04:25:34PM +0200, David Hildenbrand wrote: > > > Looks like what we fixed for hugetlb in commit 44f86392bdd1 ("mm/hugetlb: > > > fix uffd-wp handling for migration entries in hugetlb_change_protection()") > > > similarly applies to THP. > > > > > > Setting/clearing uffd-wp on THP migration entries is not implemented > > > properly. Further, while removing migration PMDs considers the uffd-wp > > > bit, inserting migration PMDs does not consider the uffd-wp bit. > > > > > > We have to set/clear independently of the migration entry type in > > > change_huge_pmd() and properly copy the uffd-wp bit in > > > set_pmd_migration_entry(). > > > > > > Verified using a simple reproducer that triggers migration of a THP, that > > > the set_pmd_migration_entry() no longer loses the uffd-wp bit. > > > > > > Fixes: f45ec5ff16a7 ("userfaultfd: wp: support swap and page migration") > > > Cc: stable@vger.kernel.org > > > Signed-off-by: David Hildenbrand > > > > Reviewed-by: Peter Xu > > > > Thanks, one trivial nitpick: > > > > > --- > > > mm/huge_memory.c | 14 ++++++++++++-- > > > 1 file changed, 12 insertions(+), 2 deletions(-) > > > > > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > > > index 032fb0ef9cd1..bdda4f426d58 100644 > > > --- a/mm/huge_memory.c > > > +++ b/mm/huge_memory.c > > > @@ -1838,10 +1838,10 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, > > > if (is_swap_pmd(*pmd)) { > > > swp_entry_t entry = pmd_to_swp_entry(*pmd); > > > struct page *page = pfn_swap_entry_to_page(entry); > > > + pmd_t newpmd; > > > VM_BUG_ON(!is_pmd_migration_entry(*pmd)); > > > if (is_writable_migration_entry(entry)) { > > > - pmd_t newpmd; > > > /* > > > * A protection check is difficult so > > > * just be safe and disable write > > > @@ -1855,8 +1855,16 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, > > > newpmd = pmd_swp_mksoft_dirty(newpmd); > > > if (pmd_swp_uffd_wp(*pmd)) > > > newpmd = pmd_swp_mkuffd_wp(newpmd); > > > - set_pmd_at(mm, addr, pmd, newpmd); > > > + } else { > > > + newpmd = *pmd; > > > } > > > + > > > + if (uffd_wp) > > > + newpmd = pmd_swp_mkuffd_wp(newpmd); > > > + else if (uffd_wp_resolve) > > > + newpmd = pmd_swp_clear_uffd_wp(newpmd); > > > + if (!pmd_same(*pmd, newpmd)) > > > + set_pmd_at(mm, addr, pmd, newpmd); > > > goto unlock; > > > } > > > #endif > > > @@ -3251,6 +3259,8 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw, > > > pmdswp = swp_entry_to_pmd(entry); > > > if (pmd_soft_dirty(pmdval)) > > > pmdswp = pmd_swp_mksoft_dirty(pmdswp); > > > + if (pmd_swp_uffd_wp(*pvmw->pmd)) > > > + pmdswp = pmd_swp_mkuffd_wp(pmdswp); > > > > I think it's fine to use *pmd, but maybe still better to use pmdval? I > > worry pmdp_invalidate()) can be something else in the future that may > > affect the bit. > > Wondering how I ended up with that, I realized that it's actually > wrong and might have worked by chance for my reproducer on x86. > > That should make it work: > > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index f977c965fdad..fffc953fa6ea 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -3257,7 +3257,7 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw, > pmdswp = swp_entry_to_pmd(entry); > if (pmd_soft_dirty(pmdval)) > pmdswp = pmd_swp_mksoft_dirty(pmdswp); > - if (pmd_swp_uffd_wp(*pvmw->pmd)) > + if (pmd_uffd_wp(pmdval)) > pmdswp = pmd_swp_mkuffd_wp(pmdswp); > set_pmd_at(mm, address, pvmw->pmd, pmdswp); > page_remove_rmap(page, vma, true); I guess pmd_swp_uffd_wp() just reads the _USER bit 2 which is also set for a present pte, but then it sets swp uffd-wp always even if it was not set. Yes the change must be squashed in to be correct, with that, my R-b keeps. Thanks, -- Peter Xu