From: Lance Yang <ioworker0@gmail.com>
To: Ryan Roberts <ryan.roberts@arm.com>
Cc: akpm@linux-foundation.org, zokeefe@google.com, 21cnbao@gmail.com,
shy828301@gmail.com, david@redhat.com, mhocko@suse.com,
fengwei.yin@intel.com, xiehuan09@gmail.com,
wangkefeng.wang@huawei.com, songmuchun@bytedance.com,
peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v6 2/2] mm/arm64: override clear_young_dirty_ptes() batch helper
Date: Mon, 15 Apr 2024 17:41:16 +0800 [thread overview]
Message-ID: <CAK1f24=oanyccKUYz1E7H2euyhnExjqRShQ8KKqDXAV6qkohhg@mail.gmail.com> (raw)
In-Reply-To: <209b9341-8bd0-47fe-b8fd-9a0f02beeab0@arm.com>
On Mon, Apr 15, 2024 at 4:59 PM Ryan Roberts <ryan.roberts@arm.com> wrote:
>
> On 13/04/2024 01:22, Lance Yang wrote:
> > The per-pte get_and_clear/modify/set approach would result in
> > unfolding/refolding for contpte mappings on arm64. So we need
> > to override clear_young_dirty_ptes() for arm64 to avoid it.
> >
> > Suggested-by: David Hildenbrand <david@redhat.com>
> > Suggested-by: Barry Song <21cnbao@gmail.com>
> > Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
>
> No, afraid I haven't signed off yet!
Actually, you've done most of this change, and I just do the legwork :)
But I'll remove this s-o-b.
>
> > Signed-off-by: Lance Yang <ioworker0@gmail.com>
> > ---
> > arch/arm64/include/asm/pgtable.h | 37 ++++++++++++++++++++++++++++++++
> > arch/arm64/mm/contpte.c | 28 ++++++++++++++++++++++++
> > 2 files changed, 65 insertions(+)
> >
> > diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
> > index 9fd8613b2db2..f951774dd2d6 100644
> > --- a/arch/arm64/include/asm/pgtable.h
> > +++ b/arch/arm64/include/asm/pgtable.h
> > @@ -1223,6 +1223,28 @@ static inline void __wrprotect_ptes(struct mm_struct *mm, unsigned long address,
> > __ptep_set_wrprotect(mm, address, ptep);
> > }
> >
> > +static inline void __clear_young_dirty_ptes(struct mm_struct *mm,
> > + unsigned long addr, pte_t *ptep,
> > + unsigned int nr, cydp_t flags)
> > +{
> > + pte_t pte;
> > +
> > + for (;;) {
> > + pte = __ptep_get(ptep);
> > +
> > + if (flags | CYDP_CLEAR_YOUNG)
>
> bug: should be bitwise AND (&).
Good spot! Thanks!
>
> > + pte = pte_mkold(pte);
> > + if (flags | CYDP_CLEAR_DIRTY)
> > + pte = pte_mkclean(pte);
> > +
> > + __set_pte(ptep, pte);
>
> The __ptep_get() and __set_pte() are not atomic. This is only safe when you are
> clearing BOTH access and dirty (as I explained in the previous version). If you
> are only clearing one of the flags, you will need a similar cmpxchg loop as for
> __ptep_test_and_clear_young(). Otherwise you can race with the HW and lose
> information.
Thanks again for your patience and explanation!
I still got it wrong :(
>
> > + if (--nr == 0)
> > + break;
> > + ptep++;
> > + addr += PAGE_SIZE;
> > + }
> > +}
> > +
> > #ifdef CONFIG_TRANSPARENT_HUGEPAGE
> > #define __HAVE_ARCH_PMDP_SET_WRPROTECT
> > static inline void pmdp_set_wrprotect(struct mm_struct *mm,
> > @@ -1379,6 +1401,9 @@ extern void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
> > extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
> > unsigned long addr, pte_t *ptep,
> > pte_t entry, int dirty);
> > +extern void contpte_clear_young_dirty_ptes(struct mm_struct *mm,
> > + unsigned long addr, pte_t *ptep,
> > + unsigned int nr, cydp_t flags);
> >
> > static __always_inline void contpte_try_fold(struct mm_struct *mm,
> > unsigned long addr, pte_t *ptep, pte_t pte)
> > @@ -1603,6 +1628,17 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma,
> > return contpte_ptep_set_access_flags(vma, addr, ptep, entry, dirty);
> > }
> >
> > +#define clear_young_dirty_ptes clear_young_dirty_ptes
> > +static inline void clear_young_dirty_ptes(struct mm_struct *mm,
> > + unsigned long addr, pte_t *ptep,
> > + unsigned int nr, cydp_t flags)
> > +{
> > + if (likely(nr == 1 && !pte_cont(__ptep_get(ptep))))
> > + __clear_young_dirty_ptes(mm, addr, ptep, nr, flags);
> > + else
> > + contpte_clear_young_dirty_ptes(mm, addr, ptep, nr, flags);
> > +}
> > +
> > #else /* CONFIG_ARM64_CONTPTE */
> >
> > #define ptep_get __ptep_get
> > @@ -1622,6 +1658,7 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma,
> > #define wrprotect_ptes __wrprotect_ptes
> > #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
> > #define ptep_set_access_flags __ptep_set_access_flags
> > +#define clear_young_dirty_ptes __clear_young_dirty_ptes
> >
> > #endif /* CONFIG_ARM64_CONTPTE */
> >
> > diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c
> > index 1b64b4c3f8bf..bf3b089d9641 100644
> > --- a/arch/arm64/mm/contpte.c
> > +++ b/arch/arm64/mm/contpte.c
> > @@ -361,6 +361,34 @@ void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr,
> > }
> > EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes);
> >
> > +void contpte_clear_young_dirty_ptes(struct mm_struct *mm, unsigned long addr,
> > + pte_t *ptep, unsigned int nr, cydp_t flags)
> > +{
> > + /*
> > + * We can safely clear access/dirty without needing to unfold from
> > + * the architectures perspective, even when contpte is set. If the
> > + * range starts or ends midway through a contpte block, we can just
> > + * expand to include the full contpte block. While this is not
> > + * exactly what the core-mm asked for, it tracks access/dirty per
> > + * folio, not per page. And since we only create a contpte block
> > + * when it is covered by a single folio, we can get away with
> > + * clearing access/dirty for the whole block.
> > + */
> > + unsigned int start = addr;
> > + unsigned int end = start + nr;
>
> There are addresses; they should be unsigned long. May have been my error
> originally when I sent you the example snippet.
Got it. I'll sort it.
Thanks again for your time!
Thanks,
Lance
>
> Thanks,
> Ryan
>
> > +
> > + if (pte_cont(__ptep_get(ptep + nr - 1)))
> > + end = ALIGN(end, CONT_PTE_SIZE);
> > +
> > + if (pte_cont(__ptep_get(ptep))) {
> > + start = ALIGN_DOWN(start, CONT_PTE_SIZE);
> > + ptep = contpte_align_down(ptep);
> > + }
> > +
> > + __clear_young_dirty_ptes(mm, start, ptep, end - start, flags);
> > +}
> > +EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes);
> > +
> > int contpte_ptep_set_access_flags(struct vm_area_struct *vma,
> > unsigned long addr, pte_t *ptep,
> > pte_t entry, int dirty)
>
prev parent reply other threads:[~2024-04-15 9:41 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-13 0:22 [PATCH v6 0/2] mm/madvise: enhance lazyfreeing with mTHP in madvise_free Lance Yang
2024-04-13 0:22 ` [PATCH v6 1/2] mm/madvise: optimize " Lance Yang
2024-04-15 8:47 ` Ryan Roberts
2024-04-15 9:23 ` Lance Yang
2024-04-13 0:22 ` [PATCH v6 2/2] mm/arm64: override clear_young_dirty_ptes() batch helper Lance Yang
2024-04-15 8:59 ` Ryan Roberts
2024-04-15 9:41 ` Lance Yang [this message]
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='CAK1f24=oanyccKUYz1E7H2euyhnExjqRShQ8KKqDXAV6qkohhg@mail.gmail.com' \
--to=ioworker0@gmail.com \
--cc=21cnbao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=fengwei.yin@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=minchan@kernel.org \
--cc=peterx@redhat.com \
--cc=ryan.roberts@arm.com \
--cc=shy828301@gmail.com \
--cc=songmuchun@bytedance.com \
--cc=wangkefeng.wang@huawei.com \
--cc=xiehuan09@gmail.com \
--cc=zokeefe@google.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