From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3849FC369D5 for ; Mon, 28 Apr 2025 15:54:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 365E66B000D; Mon, 28 Apr 2025 11:54:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F0676B000E; Mon, 28 Apr 2025 11:54:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 190B06B0010; Mon, 28 Apr 2025 11:54:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EA0E96B000D for ; Mon, 28 Apr 2025 11:54:51 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D7349120ADA for ; Mon, 28 Apr 2025 15:54:52 +0000 (UTC) X-FDA: 83383900824.02.9941CD7 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) by imf15.hostedemail.com (Postfix) with ESMTP id A4C8DA000B for ; Mon, 28 Apr 2025 15:54:50 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=FaA0Twe5; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf15.hostedemail.com: domain of lance.yang@linux.dev designates 91.218.175.188 as permitted sender) smtp.mailfrom=lance.yang@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745855691; a=rsa-sha256; cv=none; b=tEFQzjFjDm0i47tfMufi/qV+GyuOHebOIoccLgt9leZnhXOgN1w7rRALbcvfvhA8UrRZgk EE9keXdtPsMuUZnM+aXiXvOyl9UZut1bRxb2krPf0hRWLOmcDoMQ7ZBZt90IXGUvD5rJOt oBhKedM+SxlkFrx49V6dr4yxJmO6XtM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=FaA0Twe5; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf15.hostedemail.com: domain of lance.yang@linux.dev designates 91.218.175.188 as permitted sender) smtp.mailfrom=lance.yang@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745855691; h=from:from:sender: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:dkim-signature; bh=Dawl8i88r8gHM8GMLkwVV5hNLqE1msotfeeEvyM0UGg=; b=EJWCdD0w1EHNJAIYKTyhDO/GATon/3XmFMdLe5Xb+qm491yGNDsu587xJ8+w3hfQ794w5C sPukEGdOtRQnXEgtjHIKbUZy4FFhFP2AYU+ju30hnFA5x72UB/jyTFOPj0WGaKfG/qFMKf zu84Qth1F5WUW3HpQWIc2djLNGlWUzA= Message-ID: <6a0b4d81-a1b8-4533-8b4e-de270e39c5aa@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1745855688; 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=Dawl8i88r8gHM8GMLkwVV5hNLqE1msotfeeEvyM0UGg=; b=FaA0Twe5lUPFZNu4Y8Ll6qulwKqptD919xn9eQA4GemadbkvSp/kBtsBN6cXgzPo6c5Bys 0vMyUWz47EYj2SSEgpHpVWO2bEjAKyuDU58SILPTrWX76GLpW6uVnLbhjNNtDoNYSOtu3f obX5tmczNYhctTAqE69zHyFgFSiNFjo= Date: Mon, 28 Apr 2025 23:54:34 +0800 MIME-Version: 1.0 Subject: Re: [PATCH 6/7] mm: Batch around can_change_pte_writable() Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Lance Yang To: Dev Jain , akpm@linux-foundation.org Cc: ryan.roberts@arm.com, david@redhat.com, willy@infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, catalin.marinas@arm.com, will@kernel.org, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, vbabka@suse.cz, jannh@google.com, anshuman.khandual@arm.com, peterx@redhat.com, joey.gouly@arm.com, ioworker0@gmail.com, baohua@kernel.org, kevin.brodsky@arm.com, quic_zhenhuah@quicinc.com, christophe.leroy@csgroup.eu, yangyicong@hisilicon.com, linux-arm-kernel@lists.infradead.org, namit@vmware.com, hughd@google.com, yang@os.amperecomputing.com, ziy@nvidia.com References: <20250428120414.12101-1-dev.jain@arm.com> <20250428120414.12101-7-dev.jain@arm.com> <29c70c06-42c2-4bc0-a56e-443a1200fde0@linux.dev> In-Reply-To: <29c70c06-42c2-4bc0-a56e-443a1200fde0@linux.dev> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: A4C8DA000B X-Stat-Signature: zerkcbisyi559qc75iqj7jsn1fz3s7zk X-Rspam-User: X-HE-Tag: 1745855690-668084 X-HE-Meta: U2FsdGVkX1+2qyTSyOOazYT5fk5osnFtG0IUfqdFBt62PlWVCRcFlUYgCZovRnX/jQnw5ac433+n0JDU0kBM9jcs+DsBM1f6OpEwXDhtnF1BhpUnbjFrWmwEsIqiTsSc37XbQ7/gvqzsmTuOzjOon3WqQhZlkEffUjZI3If6lxouOtlfGkHiqlY5SEy0JlKJ2uMkGu9DjWytai3U5wkghWhEVuPRGYU4Mkb0kEBBWKwMq4cKZ39Q/rbFvHp5CUMPXtArECgJuDrFDu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000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 2025/4/28 21:16, Lance Yang wrote: > > > On 2025/4/28 20:50, Lance Yang wrote: >> Hey Dev, >> >> On 2025/4/28 20:04, Dev Jain wrote: >>> In preparation for patch 7, we need to properly batch around >>> can_change_pte_writable(). We batch around pte_needs_soft_dirty_wp() by >>> the corresponding fpb flag, we batch around the page-anon exclusive >>> check >>> using folio_maybe_mapped_shared(); modify_prot_start_ptes() collects the >>> dirty and access bits across the batch, therefore batching across >>> pte_dirty(): this is correct since the dirty bit on the PTE really >>> is just an indication that the folio got written to, so even if >>> the PTE is not actually dirty (but one of the PTEs in the batch is), >>> the wp-fault optimization can be made. >>> >>> Signed-off-by: Dev Jain >>> --- >>>   include/linux/mm.h | 4 ++-- >>>   mm/gup.c           | 2 +- >>>   mm/huge_memory.c   | 4 ++-- >>>   mm/memory.c        | 6 +++--- >>>   mm/mprotect.c      | 9 ++++++--- >>>   5 files changed, 14 insertions(+), 11 deletions(-) >>> >>> diff --git a/include/linux/mm.h b/include/linux/mm.h >>> index 5eb0d77c4438..ffa02e15863f 100644 >>> --- a/include/linux/mm.h >>> +++ b/include/linux/mm.h >>> @@ -2710,8 +2710,8 @@ int get_cmdline(struct task_struct *task, char >>> *buffer, int buflen); >>>   #define  MM_CP_UFFD_WP_ALL                 (MM_CP_UFFD_WP | \ >>>                           MM_CP_UFFD_WP_RESOLVE) >>> -bool can_change_pte_writable(struct vm_area_struct *vma, unsigned >>> long addr, >>> -                 pte_t pte); >>> +bool can_change_ptes_writable(struct vm_area_struct *vma, unsigned >>> long addr, >>> +                 pte_t pte, struct folio *folio, unsigned int nr); >>>   extern long change_protection(struct mmu_gather *tlb, >>>                     struct vm_area_struct *vma, unsigned long start, >>>                     unsigned long end, unsigned long cp_flags); >>> diff --git a/mm/gup.c b/mm/gup.c >>> index 84461d384ae2..6a605fc5f2cb 100644 >>> --- a/mm/gup.c >>> +++ b/mm/gup.c >>> @@ -614,7 +614,7 @@ static inline bool can_follow_write_common(struct >>> page *page, >>>           return false; >>>       /* >>> -     * See can_change_pte_writable(): we broke COW and could map the >>> page >>> +     * See can_change_ptes_writable(): we broke COW and could map >>> the page >>>        * writable if we have an exclusive anonymous page ... >>>        */ >>>       return page && PageAnon(page) && PageAnonExclusive(page); >>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c >>> index 28c87e0e036f..e5496c0d9e7e 100644 >>> --- a/mm/huge_memory.c >>> +++ b/mm/huge_memory.c >>> @@ -2032,12 +2032,12 @@ static inline bool >>> can_change_pmd_writable(struct vm_area_struct *vma, >>>           return false; >>>       if (!(vma->vm_flags & VM_SHARED)) { >>> -        /* See can_change_pte_writable(). */ >>> +        /* See can_change_ptes_writable(). */ >>>           page = vm_normal_page_pmd(vma, addr, pmd); >>>           return page && PageAnon(page) && PageAnonExclusive(page); >>>       } >>> -    /* See can_change_pte_writable(). */ >>> +    /* See can_change_ptes_writable(). */ >>>       return pmd_dirty(pmd); >>>   } >>> diff --git a/mm/memory.c b/mm/memory.c >>> index b9e8443aaa86..b1fda3de8d27 100644 >>> --- a/mm/memory.c >>> +++ b/mm/memory.c >>> @@ -750,7 +750,7 @@ static void restore_exclusive_pte(struct >>> vm_area_struct *vma, >>>           pte = pte_mkuffd_wp(pte); >>>       if ((vma->vm_flags & VM_WRITE) && >>> -        can_change_pte_writable(vma, address, pte)) { >>> +        can_change_ptes_writable(vma, address, pte, NULL, 1)) { >>>           if (folio_test_dirty(folio)) >>>               pte = pte_mkdirty(pte); >>>           pte = pte_mkwrite(pte, vma); >>> @@ -5767,7 +5767,7 @@ static void numa_rebuild_large_mapping(struct >>> vm_fault *vmf, struct vm_area_stru >>>               ptent = pte_modify(ptent, vma->vm_page_prot); >>>               writable = pte_write(ptent); >>>               if (!writable && pte_write_upgrade && >>> -                can_change_pte_writable(vma, addr, ptent)) >>> +                can_change_ptes_writable(vma, addr, ptent, NULL, 1)) >>>                   writable = true; >>>           } >>> @@ -5808,7 +5808,7 @@ static vm_fault_t do_numa_page(struct vm_fault >>> *vmf) >>>        */ >>>       writable = pte_write(pte); >>>       if (!writable && pte_write_upgrade && >>> -        can_change_pte_writable(vma, vmf->address, pte)) >>> +        can_change_ptes_writable(vma, vmf->address, pte, NULL, 1)) >>>           writable = true; >>>       folio = vm_normal_folio(vma, vmf->address, pte); >>> diff --git a/mm/mprotect.c b/mm/mprotect.c >>> index 33eabc995584..362fd7e5457d 100644 >>> --- a/mm/mprotect.c >>> +++ b/mm/mprotect.c >>> @@ -40,8 +40,8 @@ >>>   #include "internal.h" >>> -bool can_change_pte_writable(struct vm_area_struct *vma, unsigned >>> long addr, >>> -                 pte_t pte) >>> +bool can_change_ptes_writable(struct vm_area_struct *vma, unsigned >>> long addr, >>> +                  pte_t pte, struct folio *folio, unsigned int nr) >>>   { >>>       struct page *page; >>> @@ -67,6 +67,9 @@ bool can_change_pte_writable(struct vm_area_struct >>> *vma, unsigned long addr, >>>            * write-fault handler similarly would map them writable >>> without >>>            * any additional checks while holding the PT lock. >>>            */ >>> +        if (unlikely(nr != 1)) >>> +            return !folio_maybe_mapped_shared(folio); >>> + >>>           page = vm_normal_page(vma, addr, pte); >>>           return page && PageAnon(page) && PageAnonExclusive(page); >>>       } >> >> IIUC, As mentioned in the comment above, we should do the same >> anonymous check >> to large folios. And folio_maybe_mapped_shared() already handles both >> order-0 >> and large folios nicely, so we could simplify the logic as follows: > > Forget to add: > > Note that the exclusive flag is set only for non-large folios or the head > page of large folios during mapping, so PageAnonExclusive() will always > return false for tail pages of large folios, IIUC. Correction: the exclusive flag would be set for all sub pages of large folios during mapping. Thanks, Lance > > Thanks, > Lance > >> >> diff --git a/mm/mprotect.c b/mm/mprotect.c >> index 1605e89349d2..df56a30bb241 100644 >> --- a/mm/mprotect.c >> +++ b/mm/mprotect.c >> @@ -43,8 +43,6 @@ >>   bool can_change_ptes_writable(struct vm_area_struct *vma, unsigned >> long addr, >>                                pte_t pte, struct folio *folio, >> unsigned int nr) >>   { >> -       struct page *page; >> - >>          if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE))) >>                  return false; >> >> @@ -67,11 +65,7 @@ bool can_change_ptes_writable(struct vm_area_struct >> *vma, unsigned long addr, >>                   * write-fault handler similarly would map them >> writable without >>                   * any additional checks while holding the PT lock. >>                   */ >> -               if (unlikely(nr != 1)) >> -                       return !folio_maybe_mapped_shared(folio); >> - >> -               page = vm_normal_page(vma, addr, pte); >> -               return page && PageAnon(page) && PageAnonExclusive(page); >> +               return folio_test_anon(folio) && ! >> folio_maybe_mapped_shared(folio); >>          } >> >>          VM_WARN_ON_ONCE(is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte)); >> -- >> >> Thanks, >> Lance >> >>> @@ -222,7 +225,7 @@ static long change_pte_range(struct mmu_gather *tlb, >>>                */ >>>               if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && >>>                   !pte_write(ptent) && >>> -                can_change_pte_writable(vma, addr, ptent)) >>> +                can_change_ptes_writable(vma, addr, ptent, folio, 1)) >>>                   ptent = pte_mkwrite(ptent, vma); >>>               ptep_modify_prot_commit(vma, addr, pte, oldpte, ptent); >> >