On Fri, Sep 12, 2025 at 04:56:47PM +0200, David Hildenbrand wrote: > On 12.09.25 16:35, Kiryl Shutsemau wrote: > > On Fri, Sep 12, 2025 at 04:28:09PM +0200, David Hildenbrand wrote: > > > On 12.09.25 15:47, David Hildenbrand wrote: > > > > On 12.09.25 14:19, Kiryl Shutsemau wrote: > > > > > On Thu, Sep 11, 2025 at 09:27:55PM -0600, Nico Pache wrote: > > > > > > The following series provides khugepaged with the capability to collapse > > > > > > anonymous memory regions to mTHPs. > > > > > > > > > > > > To achieve this we generalize the khugepaged functions to no longer depend > > > > > > on PMD_ORDER. Then during the PMD scan, we use a bitmap to track individual > > > > > > pages that are occupied (!none/zero). After the PMD scan is done, we do > > > > > > binary recursion on the bitmap to find the optimal mTHP sizes for the PMD > > > > > > range. The restriction on max_ptes_none is removed during the scan, to make > > > > > > sure we account for the whole PMD range. When no mTHP size is enabled, the > > > > > > legacy behavior of khugepaged is maintained. max_ptes_none will be scaled > > > > > > by the attempted collapse order to determine how full a mTHP must be to be > > > > > > eligible for the collapse to occur. If a mTHP collapse is attempted, but > > > > > > contains swapped out, or shared pages, we don't perform the collapse. It is > > > > > > now also possible to collapse to mTHPs without requiring the PMD THP size > > > > > > to be enabled. > > > > > > > > > > > > When enabling (m)THP sizes, if max_ptes_none >= HPAGE_PMD_NR/2 (255 on > > > > > > 4K page size), it will be automatically capped to HPAGE_PMD_NR/2 - 1 for > > > > > > mTHP collapses to prevent collapse "creep" behavior. This prevents > > > > > > constantly promoting mTHPs to the next available size, which would occur > > > > > > because a collapse introduces more non-zero pages that would satisfy the > > > > > > promotion condition on subsequent scans. > > > > > > > > > > Hm. Maybe instead of capping at HPAGE_PMD_NR/2 - 1 we can count > > > > > all-zeros 4k as none_or_zero? It mirrors the logic of shrinker. > > > > > > > > BTW, I thought further about this and I agree: if we count zero-filled > > > > pages towards none_or_zero one we can avoid the "creep" problem. > > > > > > > > The scanning-for-zero part is rather nasty, though. > > > > > > Aaand, thinking again from the other direction, this would mean that just > > > because pages became zero after some time that we would no longer collapse > > > because none_or_zero would then be higher. Hm .... > > > > > > How I hate all of this so very very much :) > > > > This is not new. Shrinker has the same problem: it cannot distinguish > > between hot 4k that happened to be zero from the 4k that is there just > > because of we faulted in 2M a time. > > Right. And so far that problem is isolated to the shrinker. > > To me so far "none_or_zero" really meant "will I consume more memory when > collapsing". That's not true for zero-filled pages, obviously. Well, KSM can reclaim these zero-filled memory until we collapse it. -- Kiryl Shutsemau / Kirill A. Shutemov