On 5 Jun 2026, at 12:14, Nico Pache wrote: > generalize the order of the __collapse_huge_page_* and collapse_max_* > functions to support future mTHP collapse. > > The current mechanism for determining collapse with the > khugepaged_max_ptes_none value is not designed with mTHP in mind. This > raises a key design issue: if we support user defined max_pte_none values > (even those scaled by order), a collapse of a lower order can introduces > an feedback loop, or "creep", when max_ptes_none is set to a value greater > than HPAGE_PMD_NR / 2. [1] > > With this configuration, a successful collapse to order N will populate > enough pages to satisfy the collapse condition on order N+1 on the next > scan. This leads to unnecessary work and memory churn. > > To fix this issue introduce a helper function that will limit mTHP > collapse support to two max_ptes_none values, 0 and HPAGE_PMD_NR - 1. > This effectively supports two modes: [2] > > - max_ptes_none=0: never collapses if it encounters an empty PTE or a PTE > that maps the shared zeropage. Consequently, no memory bloat. > - max_ptes_none=511 (on 4k pagesz): Always collapse to the highest > available mTHP order. > > This removes the possibility of "creep", and a warning will be emitted if > any non-supported max_ptes_none value is configured with mTHP enabled. > Any intermediate value will default mTHP collapse to max_ptes_none=0. > > mTHP collapse will not honor the khugepaged_max_ptes_shared or > khugepaged_max_ptes_swap parameters, and will fail if it encounters a > shared or swapped entry. > > No functional changes in this patch; however it defines future behavior > for mTHP collapse. > > [1] - https://lore.kernel.org/all/[email protected] > [2] - > https://lore.kernel.org/all/[email protected] > > Reviewed-by: Lorenzo Stoakes <[email protected]> > Acked-by: David Hildenbrand (arm) <[email protected]> > Reviewed-by: Lance Yang <[email protected]> > Co-developed-by: Dev Jain <[email protected]> > Signed-off-by: Dev Jain <[email protected]> > Signed-off-by: Nico Pache <[email protected]> > --- > mm/khugepaged.c | 126 +++++++++++++++++++++++++++++++++++------------- > 1 file changed, 93 insertions(+), 33 deletions(-) > LGTM.
Reviewed-by: Zi Yan <[email protected]> Best Regards, Yan, Zi
