From: Ryan Roberts <ryan.roberts@arm.com>
To: Dev Jain <dev.jain@arm.com>, akpm@linux-foundation.org
Cc: 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,
hughd@google.com, yang@os.amperecomputing.com, ziy@nvidia.com
Subject: Re: [PATCH v3 4/5] arm64: Add batched version of ptep_modify_prot_start
Date: Wed, 21 May 2025 15:14:20 +0100 [thread overview]
Message-ID: <eb57601d-abac-492a-8e62-dc1c406af572@arm.com> (raw)
In-Reply-To: <20250519074824.42909-5-dev.jain@arm.com>
On 19/05/2025 08:48, Dev Jain wrote:
> Override the generic definition to use get_and_clear_full_ptes(). This helper
> does a TLBI only for the starting and ending contpte block of the range, whereas
> the current implementation will call ptep_get_and_clear() for every contpte block,
> thus doing a TLBI on every contpte block. Therefore, we have a performance win.
> The arm64 definition of pte_accessible() allows us to batch around it in clear_flush_ptes():
> #define pte_accessible(mm, pte) \
> (mm_tlb_flush_pending(mm) ? pte_present(pte) : pte_valid(pte))
>
> All ptes are obviously present in the folio batch, and they are also valid.
>
> Signed-off-by: Dev Jain <dev.jain@arm.com>
Please squash this with the
> ---
> arch/arm64/include/asm/pgtable.h | 5 +++++
> arch/arm64/mm/mmu.c | 12 +++++++++---
> include/linux/pgtable.h | 4 ++++
> mm/pgtable-generic.c | 16 +++++++++++-----
> 4 files changed, 29 insertions(+), 8 deletions(-)
>
> diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
> index 2a77f11b78d5..8872ea5f0642 100644
> --- a/arch/arm64/include/asm/pgtable.h
> +++ b/arch/arm64/include/asm/pgtable.h
> @@ -1553,6 +1553,11 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma,
> unsigned long addr, pte_t *ptep,
> pte_t old_pte, pte_t new_pte);
>
> +#define modify_prot_start_ptes modify_prot_start_ptes
> +extern pte_t modify_prot_start_ptes(struct vm_area_struct *vma,
> + unsigned long addr, pte_t *ptep,
> + unsigned int nr);
> +
> #ifdef CONFIG_ARM64_CONTPTE
>
> /*
> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> index 8fcf59ba39db..fe60be8774f4 100644
> --- a/arch/arm64/mm/mmu.c
> +++ b/arch/arm64/mm/mmu.c
> @@ -1523,7 +1523,8 @@ static int __init prevent_bootmem_remove_init(void)
> early_initcall(prevent_bootmem_remove_init);
> #endif
>
> -pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
> +pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
> + pte_t *ptep, unsigned int nr)
> {
> if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
> /*
> @@ -1532,9 +1533,14 @@ pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte
> * in cases where cpu is affected with errata #2645198.
> */
> if (pte_user_exec(ptep_get(ptep)))
> - return ptep_clear_flush(vma, addr, ptep);
> + return clear_flush_ptes(vma, addr, ptep, nr);
> }
> - return ptep_get_and_clear(vma->vm_mm, addr, ptep);
> + return get_and_clear_full_ptes(vma->vm_mm, addr, ptep, nr, 0);
> +}
I think we can do this more precisely with respect to tlbis and also without
needing to create a new clear_flush_ptes() helper:
pte_t modify_prot_start_ptes(struct vm_area_struct *vma, unsigned long addr,
pte_t *ptep, unsigned int nr)
{
pte_t pte = get_and_clear_full_ptes(vma->vm_mm, addr, ptep, nr, 0);
if (alternative_has_cap_unlikely(ARM64_WORKAROUND_2645198)) {
/*
* Break-before-make (BBM) is required for all user space mappings
* when the permission changes from executable to non-executable
* in cases where cpu is affected with errata #2645198.
*/
if (pte_accessible(vma->vm_mm, pte) && pte_user_exec(pte))
__flush_tlb_range(vma, addr, nr * PAGE_SIZE,
PAGE_SIZE, true, 3);
}
return pte;
}
> +
> +pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
> +{
> + return modify_prot_start_ptes(vma, addr, ptep, 1);
> }
>
> void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep,
> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
> index e40ed57e034d..41f4a8de5c28 100644
> --- a/include/linux/pgtable.h
> +++ b/include/linux/pgtable.h
> @@ -828,6 +828,10 @@ extern pte_t ptep_clear_flush(struct vm_area_struct *vma,
> pte_t *ptep);
> #endif
>
> +extern pte_t clear_flush_ptes(struct vm_area_struct *vma,
> + unsigned long address,
> + pte_t *ptep, unsigned int nr);
> +
> #ifndef __HAVE_ARCH_PMDP_HUGE_CLEAR_FLUSH
> extern pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma,
> unsigned long address,
> diff --git a/mm/pgtable-generic.c b/mm/pgtable-generic.c
> index 5a882f2b10f9..e238f88c3cac 100644
> --- a/mm/pgtable-generic.c
> +++ b/mm/pgtable-generic.c
> @@ -90,17 +90,23 @@ int ptep_clear_flush_young(struct vm_area_struct *vma,
> }
> #endif
>
> -#ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
> -pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long address,
> - pte_t *ptep)
> +pte_t clear_flush_ptes(struct vm_area_struct *vma, unsigned long address,
> + pte_t *ptep, unsigned int nr)
> {
> struct mm_struct *mm = (vma)->vm_mm;
> pte_t pte;
> - pte = ptep_get_and_clear(mm, address, ptep);
> + pte = get_and_clear_full_ptes(mm, address, ptep, nr, 0);
> if (pte_accessible(mm, pte))
> - flush_tlb_page(vma, address);
> + flush_tlb_range(vma, address, address + nr * PAGE_SIZE);
> return pte;
> }
Let's not create a new generic helper if only arm64 is using it. We would
also want to add a doc header to describe this helper. My proposal avoids
this.
Thanks,
Ryan
> +
> +#ifndef __HAVE_ARCH_PTEP_CLEAR_FLUSH
> +pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long address,
> + pte_t *ptep)
> +{
> + return clear_flush_ptes(vma, address, ptep, 1);
> +}
> #endif
>
> #ifdef CONFIG_TRANSPARENT_HUGEPAGE
next prev parent reply other threads:[~2025-05-21 14:14 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-19 7:48 [PATCH v3 0/5] Optimize mprotect() for large folios Dev Jain
2025-05-19 7:48 ` [PATCH v3 1/5] mm: Optimize mprotect() by batch-skipping PTEs Dev Jain
2025-05-21 8:43 ` Ryan Roberts
2025-05-21 11:58 ` Ryan Roberts
2025-05-22 5:45 ` Dev Jain
2025-05-21 12:06 ` David Hildenbrand
2025-05-22 5:43 ` Dev Jain
2025-05-22 7:13 ` David Hildenbrand
2025-05-22 7:47 ` Dev Jain
2025-05-22 16:18 ` David Hildenbrand
2025-06-04 10:38 ` Dev Jain
2025-06-04 11:44 ` David Hildenbrand
2025-05-19 7:48 ` [PATCH v3 2/5] mm: Add batched versions of ptep_modify_prot_start/commit Dev Jain
2025-05-21 11:16 ` Ryan Roberts
2025-05-21 11:45 ` Ryan Roberts
2025-05-22 6:33 ` Dev Jain
2025-05-22 7:51 ` Ryan Roberts
2025-05-22 6:39 ` Dev Jain
2025-06-16 6:37 ` Dev Jain
2025-05-19 7:48 ` [PATCH v3 3/5] mm: Optimize mprotect() by PTE batching Dev Jain
2025-05-19 8:18 ` Barry Song
2025-05-20 9:18 ` Dev Jain
2025-05-21 13:26 ` Ryan Roberts
2025-05-22 6:59 ` Dev Jain
2025-05-22 7:11 ` Dev Jain
2025-06-16 11:24 ` Dev Jain
2025-06-26 8:09 ` Ryan Roberts
2025-06-27 4:55 ` Dev Jain
2025-05-19 7:48 ` [PATCH v3 4/5] arm64: Add batched version of ptep_modify_prot_start Dev Jain
2025-05-21 14:14 ` Ryan Roberts [this message]
2025-05-22 7:13 ` Dev Jain
2025-05-19 7:48 ` [PATCH v3 5/5] arm64: Add batched version of ptep_modify_prot_commit Dev Jain
2025-05-21 14:17 ` Ryan Roberts
2025-05-22 7:12 ` Dev Jain
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=eb57601d-abac-492a-8e62-dc1c406af572@arm.com \
--to=ryan.roberts@arm.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=anshuman.khandual@arm.com \
--cc=baohua@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=christophe.leroy@csgroup.eu \
--cc=david@redhat.com \
--cc=dev.jain@arm.com \
--cc=hughd@google.com \
--cc=ioworker0@gmail.com \
--cc=jannh@google.com \
--cc=joey.gouly@arm.com \
--cc=kevin.brodsky@arm.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=peterx@redhat.com \
--cc=quic_zhenhuah@quicinc.com \
--cc=vbabka@suse.cz \
--cc=will@kernel.org \
--cc=willy@infradead.org \
--cc=yang@os.amperecomputing.com \
--cc=yangyicong@hisilicon.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox