Re: [PATCH v5 3/4] khugepaged: add defer option to mTHP options

From: Nico Pache
Date: Thu May 01 2025 - 18:54:14 EST


On Wed, Apr 30, 2025 at 2:34 PM Zi Yan <ziy@xxxxxxxxxx> wrote:
>
> On 28 Apr 2025, at 14:29, Nico Pache wrote:
>
> > Now that we have defer to globally disable THPs at fault time, lets add
> > a defer setting to the mTHP options. This will allow khugepaged to
> > operate at that order, while avoiding it at PF time.
> >
> > Signed-off-by: Nico Pache <npache@xxxxxxxxxx>
> > ---
> > include/linux/huge_mm.h | 5 +++++
> > mm/huge_memory.c | 38 +++++++++++++++++++++++++++++++++-----
> > mm/khugepaged.c | 8 ++++----
> > 3 files changed, 42 insertions(+), 9 deletions(-)
> >
> > diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
> > index 57e6c962afb1..a877c59bea67 100644
> > --- a/include/linux/huge_mm.h
> > +++ b/include/linux/huge_mm.h
> > @@ -96,6 +96,7 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr;
> > #define TVA_SMAPS (1 << 0) /* Will be used for procfs */
> > #define TVA_IN_PF (1 << 1) /* Page fault handler */
> > #define TVA_ENFORCE_SYSFS (1 << 2) /* Obey sysfs configuration */
> > +#define TVA_IN_KHUGEPAGE ((1 << 2) | (1 << 3)) /* Khugepaged defer support */
>
> Why is TVA_IN_KHUGEPAGE a superset of TVA_ENFORCE_SYSFS? Because khugepaged
> also obeys sysfs configuration?
Correct! The need for a TVA_IN_KHUGEPAGED is to isolate the "deferred"
mTHPs from being "allowed" unless we are in khugepaged.
>
> I wonder if explicitly coding the behavior is better. For example,
> in __thp_vma_allowable_orders(), enforce_sysfs = tva_flags & (TVA_ENFORCE_SYSFS | TVA_IN_KHUGEPAGE).
I'm rather indifferent about either approach. If you (or any others)
have a strong preference for an explicit (none-supersetted) TVA flag I
can make the change.
>
> >
> > #define thp_vma_allowable_order(vma, vm_flags, tva_flags, order) \
> > (!!thp_vma_allowable_orders(vma, vm_flags, tva_flags, BIT(order)))
> > @@ -182,6 +183,7 @@ extern unsigned long transparent_hugepage_flags;
> > extern unsigned long huge_anon_orders_always;
> > extern unsigned long huge_anon_orders_madvise;
> > extern unsigned long huge_anon_orders_inherit;
> > +extern unsigned long huge_anon_orders_defer;
> >
> > static inline bool hugepage_global_enabled(void)
> > {
> > @@ -306,6 +308,9 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma,
> > /* Optimization to check if required orders are enabled early. */
> > if ((tva_flags & TVA_ENFORCE_SYSFS) && vma_is_anonymous(vma)) {
>
> And code here becomes tva_flags & (TVA_ENFORCE_SYSFS | TVA_IN_KHUGEPAGE).
or just (enforce_sysfs & vma_is_anon) like you mentioned. Then we
check for the TVA_IN_KHUGEPAGED before appending the defer bits.
>
> Otherwise, LGTM. Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
Thanks !
>
> --
> Best Regards,
> Yan, Zi
>