From: Ryan Roberts <ryan.roberts@arm.com>
To: Anshuman Khandual <anshuman.khandual@arm.com>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>,
Muchun Song <muchun.song@linux.dev>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
Andrew Morton <akpm@linux-foundation.org>,
Uladzislau Rezki <urezki@gmail.com>,
Christoph Hellwig <hch@infradead.org>,
Mark Rutland <mark.rutland@arm.com>,
Ard Biesheuvel <ardb@kernel.org>, Dev Jain <dev.jain@arm.com>,
Alexandre Ghiti <alexghiti@rivosinc.com>,
Steve Capper <steve.capper@linaro.org>,
Kevin Brodsky <kevin.brodsky@arm.com>
Cc: linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1 06/16] arm64/mm: Refactor __set_ptes() and __ptep_get_and_clear()
Date: Fri, 7 Feb 2025 09:38:20 +0000 [thread overview]
Message-ID: <0492ed88-673a-435b-bc89-5f35637b4be6@arm.com> (raw)
In-Reply-To: <0538d186-470e-4c4d-974c-0d7f2c96e549@arm.com>
On 06/02/2025 13:26, Ryan Roberts wrote:
> On 06/02/2025 11:48, Anshuman Khandual wrote:
>> On 2/5/25 20:39, Ryan Roberts wrote:
>>> Refactor __set_ptes(), set_pmd_at() and set_pud_at() so that they are
>>> all a thin wrapper around a generic ___set_ptes(), which takes pgsize
>>
>> s/generic/common - as generic might be misleading for being generic MM.
>
> Good spot. I'll make this change.
>
>>
>>> parameter. This cleans up the code to remove the confusing
>>> __set_pte_at() (which was only ever used for pmd/pud) and will allow us
>>> to perform future barrier optimizations in a single place. Additionally,
>>> it will permit the huge_pte API to efficiently batch-set pgtable entries
>>> and take advantage of the future barrier optimizations.
>>
>> Makes sense.
>>
>>>
>>> ___set_ptes() calls the correct page_table_check_*_set() function based
>>> on the pgsize. This means that huge_ptes be able to get proper coverage
>>> regardless of their size, once it's plumbed into huge_pte. Currently the
>>> huge_pte API always uses the pte API which assumes an entry only covers
>>> a single page.
>>
>> Right
>>
>>>
>>> While we are at it, refactor __ptep_get_and_clear() and
>>> pmdp_huge_get_and_clear() to use a common ___ptep_get_and_clear() which
>>> also takes a pgsize parameter. This will provide the huge_pte API the
>>> means to clear ptes corresponding with the way they were set.
>>
>> __ptep_get_and_clear() refactoring does not seem to be related to the
>> previous __set_ptes(). Should they be separated out into two different
>> patches instead - for better clarity and review ? Both these clean ups
>> have enough change and can stand own their own.
>
> Yeah I think you're probably right... I was being lazy... I'll separate them.
>
>>
>>>
>>> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
>>> ---
>>> arch/arm64/include/asm/pgtable.h | 108 +++++++++++++++++++------------
>>> 1 file changed, 67 insertions(+), 41 deletions(-)
>>>
>>> diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
>>> index 0b2a2ad1b9e8..3b55d9a15f05 100644
>>> --- a/arch/arm64/include/asm/pgtable.h
>>> +++ b/arch/arm64/include/asm/pgtable.h
>>> @@ -420,23 +420,6 @@ static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr)
>>> return pfn_pte(pte_pfn(pte) + nr, pte_pgprot(pte));
>>> }
>>>
>>> -static inline void __set_ptes(struct mm_struct *mm,
>>> - unsigned long __always_unused addr,
>>> - pte_t *ptep, pte_t pte, unsigned int nr)
>>> -{
>>> - page_table_check_ptes_set(mm, ptep, pte, nr);
>>> - __sync_cache_and_tags(pte, nr);
>>> -
>>> - for (;;) {
>>> - __check_safe_pte_update(mm, ptep, pte);
>>> - __set_pte(ptep, pte);
>>> - if (--nr == 0)
>>> - break;
>>> - ptep++;
>>> - pte = pte_advance_pfn(pte, 1);
>>> - }
>>> -}
>>> -
>>> /*
>>> * Hugetlb definitions.
>>> */
>>> @@ -641,30 +624,59 @@ static inline pgprot_t pud_pgprot(pud_t pud)
>>> return __pgprot(pud_val(pfn_pud(pfn, __pgprot(0))) ^ pud_val(pud));
>>> }
>>>
>>> -static inline void __set_pte_at(struct mm_struct *mm,
>>> - unsigned long __always_unused addr,
>>> - pte_t *ptep, pte_t pte, unsigned int nr)
>>> +static inline void ___set_ptes(struct mm_struct *mm, pte_t *ptep, pte_t pte,
>>> + unsigned int nr, unsigned long pgsize)
>>
>> So address got dropped and page size got added as an argument.
>
> Yeah; addr is never used in our implementations so I figured why haul it around
> everywhere?
>
>> s/___set_ptes/___set_pxds ? to be more generic for all levels.
>
> So now we are into naming... I agree that in some senses pte feels specific to
> the last level. But it's long form "page table entry" seems more generic than
> "pxd" which implies only pmd, pud, p4d and pgd. At least to me...
>
> I think we got stuck trying to figure out a clear and short term for "page table
> entry at any level" in the past. I think ttd was the best we got; Translation
> Table Descriptor, which is the term the Arm ARM uses. But that opens a can of
> worms as now we need tdd_t and all the converters pte_tdd(), tdd_pte(),
> pmd_tdd(), ... and probably a bunch more stuff on top.
>
> So personally I prefer to take the coward's way out and just reuse pte.
How about set_ptes_anylvl() and ptep_get_and_clear_anylvl()? I think this makes
it explicit and has the benefit of removing the leading underscores. It also
means we can reuse pte_t and friends, and we can exetend this nomenclature to
other places in future at the expense of a 7 char suffix ("_anylvl").
What do you think?
>
>>
>>> {
>>> - __sync_cache_and_tags(pte, nr);
>>> - __check_safe_pte_update(mm, ptep, pte);
>>> - __set_pte(ptep, pte);
>>> + unsigned long stride = pgsize >> PAGE_SHIFT;
>>> +
>>> + switch (pgsize) {
>>> + case PAGE_SIZE:
>>> + page_table_check_ptes_set(mm, ptep, pte, nr);
>>> + break;
>>> + case PMD_SIZE:
>>> + page_table_check_pmds_set(mm, (pmd_t *)ptep, pte_pmd(pte), nr);
>>> + break;
>>> + case PUD_SIZE:
>>> + page_table_check_puds_set(mm, (pud_t *)ptep, pte_pud(pte), nr);
>>> + break;
>>
>> This is where the new page table check APIs get used for batch testing.
>
> Yes and I anticipate that the whole switch block should be optimized out when
> page_table_check is disabled.
>
>>
>>> + default:
>>> + VM_WARN_ON(1);
>>> + }
>>> +
>>> + __sync_cache_and_tags(pte, nr * stride);
>>> +
>>> + for (;;) {
>>> + __check_safe_pte_update(mm, ptep, pte);
>>> + __set_pte(ptep, pte);
>>> + if (--nr == 0)
>>> + break;
>>> + ptep++;
>>> + pte = pte_advance_pfn(pte, stride);
>>> + }
>>> }
>>
>> LGTM
>>
>>>
>>> -static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
>>> - pmd_t *pmdp, pmd_t pmd)
>>> +static inline void __set_ptes(struct mm_struct *mm,
>>> + unsigned long __always_unused addr,
>>> + pte_t *ptep, pte_t pte, unsigned int nr)
>>> {
>>> - page_table_check_pmd_set(mm, pmdp, pmd);
>>> - return __set_pte_at(mm, addr, (pte_t *)pmdp, pmd_pte(pmd),
>>> - PMD_SIZE >> PAGE_SHIFT);
>>> + ___set_ptes(mm, ptep, pte, nr, PAGE_SIZE);
>>> }
>>>
>>> -static inline void set_pud_at(struct mm_struct *mm, unsigned long addr,
>>> - pud_t *pudp, pud_t pud)
>>> +static inline void __set_pmds(struct mm_struct *mm,
>>> + unsigned long __always_unused addr,
>>> + pmd_t *pmdp, pmd_t pmd, unsigned int nr)
>>> +{
>>> + ___set_ptes(mm, (pte_t *)pmdp, pmd_pte(pmd), nr, PMD_SIZE);
>>> +}
>>> +#define set_pmd_at(mm, addr, pmdp, pmd) __set_pmds(mm, addr, pmdp, pmd, 1)
>>> +
>>> +static inline void __set_puds(struct mm_struct *mm,
>>> + unsigned long __always_unused addr,
>>> + pud_t *pudp, pud_t pud, unsigned int nr)
>>> {
>>> - page_table_check_pud_set(mm, pudp, pud);
>>> - return __set_pte_at(mm, addr, (pte_t *)pudp, pud_pte(pud),
>>> - PUD_SIZE >> PAGE_SHIFT);
>>> + ___set_ptes(mm, (pte_t *)pudp, pud_pte(pud), nr, PUD_SIZE);
>>> }
>>> +#define set_pud_at(mm, addr, pudp, pud) __set_puds(mm, addr, pudp, pud, 1)
>>
>> LGTM
>>
>>>
>>> #define __p4d_to_phys(p4d) __pte_to_phys(p4d_pte(p4d))
>>> #define __phys_to_p4d_val(phys) __phys_to_pte_val(phys)
>>> @@ -1276,16 +1288,34 @@ static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
>>> }
>>> #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */
>>>
>>> -static inline pte_t __ptep_get_and_clear(struct mm_struct *mm,
>>> - unsigned long address, pte_t *ptep)
>>> +static inline pte_t ___ptep_get_and_clear(struct mm_struct *mm, pte_t *ptep,
>>> + unsigned long pgsize)
>>
>> So address got dropped and page size got added as an argument.
>>
>>> {
>>> pte_t pte = __pte(xchg_relaxed(&pte_val(*ptep), 0));
>>>
>>> - page_table_check_pte_clear(mm, pte);
>>> + switch (pgsize) {
>>> + case PAGE_SIZE:
>>> + page_table_check_pte_clear(mm, pte);
>>> + break;
>>> + case PMD_SIZE:
>>> + page_table_check_pmd_clear(mm, pte_pmd(pte));
>>> + break;
>>> + case PUD_SIZE:
>>> + page_table_check_pud_clear(mm, pte_pud(pte));
>>> + break;
>>> + default:
>>> + VM_WARN_ON(1);
>>> + }
>>>
>>> return pte;
>>> }
>>>
>>> +static inline pte_t __ptep_get_and_clear(struct mm_struct *mm,
>>> + unsigned long address, pte_t *ptep)
>>> +{
>>> + return ___ptep_get_and_clear(mm, ptep, PAGE_SIZE);
>>> +}
>>> +
>>> static inline void __clear_full_ptes(struct mm_struct *mm, unsigned long addr,
>>> pte_t *ptep, unsigned int nr, int full)
>>> {
>>> @@ -1322,11 +1352,7 @@ static inline pte_t __get_and_clear_full_ptes(struct mm_struct *mm,
>>> static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
>>> unsigned long address, pmd_t *pmdp)
>>> {
>>> - pmd_t pmd = __pmd(xchg_relaxed(&pmd_val(*pmdp), 0));
>>> -
>>> - page_table_check_pmd_clear(mm, pmd);
>>> -
>>> - return pmd;
>>> + return pte_pmd(___ptep_get_and_clear(mm, (pte_t *)pmdp, PMD_SIZE));
>>> }
>>> #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
>>>
>>
>> Although currently there is no pudp_huge_get_and_clear() helper on arm64
>> reworked ___ptep_get_and_clear() will be able to support that as well if
>> and when required as it now supports PUD_SIZE page size.
>
> yep.
>
> Thanks for all your review so far!
>
next prev parent reply other threads:[~2025-02-07 9:38 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-05 15:09 [PATCH v1 00/16] hugetlb and vmalloc fixes and perf improvements Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 01/16] mm: hugetlb: Add huge page size param to huge_ptep_get_and_clear() Ryan Roberts
2025-02-06 5:03 ` Anshuman Khandual
2025-02-06 12:15 ` Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 02/16] arm64: hugetlb: Fix huge_ptep_get_and_clear() for non-present ptes Ryan Roberts
2025-02-06 6:15 ` Anshuman Khandual
2025-02-06 12:55 ` Ryan Roberts
2025-02-12 14:44 ` Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 03/16] arm64: hugetlb: Fix flush_hugetlb_tlb_range() invalidation level Ryan Roberts
2025-02-06 6:46 ` Anshuman Khandual
2025-02-06 13:04 ` Ryan Roberts
2025-02-13 4:57 ` Anshuman Khandual
2025-02-05 15:09 ` [PATCH v1 04/16] arm64: hugetlb: Refine tlb maintenance scope Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 05/16] mm/page_table_check: Batch-check pmds/puds just like ptes Ryan Roberts
2025-02-06 10:55 ` Anshuman Khandual
2025-02-06 13:07 ` Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 06/16] arm64/mm: Refactor __set_ptes() and __ptep_get_and_clear() Ryan Roberts
2025-02-06 11:48 ` Anshuman Khandual
2025-02-06 13:26 ` Ryan Roberts
2025-02-07 9:38 ` Ryan Roberts [this message]
2025-02-12 15:29 ` Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 07/16] arm64: hugetlb: Use ___set_ptes() and ___ptep_get_and_clear() Ryan Roberts
2025-02-07 4:09 ` Anshuman Khandual
2025-02-07 10:00 ` Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 08/16] arm64/mm: Hoist barriers out of ___set_ptes() loop Ryan Roberts
2025-02-07 5:35 ` Anshuman Khandual
2025-02-07 10:38 ` Ryan Roberts
2025-02-12 16:00 ` Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 09/16] arm64/mm: Avoid barriers for invalid or userspace mappings Ryan Roberts
2025-02-07 8:11 ` Anshuman Khandual
2025-02-07 10:53 ` Ryan Roberts
2025-02-12 16:48 ` Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 10/16] mm/vmalloc: Warn on improper use of vunmap_range() Ryan Roberts
2025-02-07 8:41 ` Anshuman Khandual
2025-02-07 10:59 ` Ryan Roberts
2025-02-13 6:36 ` Anshuman Khandual
2025-02-05 15:09 ` [PATCH v1 11/16] mm/vmalloc: Gracefully unmap huge ptes Ryan Roberts
2025-02-07 9:19 ` Anshuman Khandual
2025-02-05 15:09 ` [PATCH v1 12/16] arm64/mm: Support huge pte-mapped pages in vmap Ryan Roberts
2025-02-07 10:04 ` Anshuman Khandual
2025-02-07 11:20 ` Ryan Roberts
2025-02-13 6:32 ` Anshuman Khandual
2025-02-13 9:09 ` Ryan Roberts
2025-02-17 4:33 ` Anshuman Khandual
2025-02-05 15:09 ` [PATCH v1 13/16] mm: Don't skip arch_sync_kernel_mappings() in error paths Ryan Roberts
2025-02-07 10:21 ` Anshuman Khandual
2025-02-05 15:09 ` [PATCH v1 14/16] mm/vmalloc: Batch arch_sync_kernel_mappings() more efficiently Ryan Roberts
2025-02-10 7:11 ` Anshuman Khandual
2025-02-05 15:09 ` [PATCH v1 15/16] mm: Generalize arch_sync_kernel_mappings() Ryan Roberts
2025-02-10 7:45 ` Anshuman Khandual
2025-02-10 11:04 ` Ryan Roberts
2025-02-13 5:57 ` Anshuman Khandual
2025-02-13 9:17 ` Ryan Roberts
2025-02-05 15:09 ` [PATCH v1 16/16] arm64/mm: Defer barriers when updating kernel mappings Ryan Roberts
2025-02-10 8:03 ` Anshuman Khandual
2025-02-10 11:12 ` Ryan Roberts
2025-02-13 5:30 ` Anshuman Khandual
2025-02-13 9:38 ` Ryan Roberts
2025-02-17 4:48 ` Anshuman Khandual
2025-02-17 9:40 ` Ryan Roberts
2025-02-06 7:52 ` [PATCH v1 00/16] hugetlb and vmalloc fixes and perf improvements Andrew Morton
2025-02-06 11:59 ` Ryan Roberts
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=0492ed88-673a-435b-bc89-5f35637b4be6@arm.com \
--to=ryan.roberts@arm.com \
--cc=akpm@linux-foundation.org \
--cc=alexghiti@rivosinc.com \
--cc=anshuman.khandual@arm.com \
--cc=ardb@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=dev.jain@arm.com \
--cc=hch@infradead.org \
--cc=kevin.brodsky@arm.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mark.rutland@arm.com \
--cc=muchun.song@linux.dev \
--cc=pasha.tatashin@soleen.com \
--cc=steve.capper@linaro.org \
--cc=urezki@gmail.com \
--cc=will@kernel.org \
/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