On 7/22/24 4:07 PM, Kirill A. Shutemov wrote: > On Wed, Jul 17, 2024 at 02:06:46PM +0200, Michal Hocko wrote: >> Please try to investigate this further. The patch as is looks rather >> questionable to me TBH. Spilling unaccepted memory into the reclaim >> seems like something we should avoid if possible as this is something > > Okay, I believe I have a better understanding of the situation: > > - __alloc_pages_bulk() takes pages from the free list without accepting > more memory. This can cause number of free pages to fall below the > watermark. > > This issue can be resolved by accepting more memory in > __alloc_pages_bulk() if the watermark check fails. > > The problem is not only related to unallocated memory. I think the > deferred page initialization mechanism could result in premature OOM if > __alloc_pages_bulk() allocates pages faster than deferred page > initialization can add them to the free lists. However, this scenario is > unlikely. > > - There is nothing that compels the kernel to accept more memory after the > watermarks have been calculated in __setup_per_zone_wmarks(). This can > put us under the watermark. > > This issue can be resolved by accepting memory up to the watermark after > the watermarks have been initialized. > > - Once kswapd is started, it will begin spinning if we are below the > watermark and there is no memory that can be reclaimed. Once the above > problems are fixed, the issue will be resolved. > > - The kernel needs to accept memory up to the PROMO watermark. This will > prevent unaccepted memory from interfering with NUMA balancing. So do we still assume all memory is eventually accepted and it's just a initialization phase thing? And the only reason we don't do everything in a kthread like the deferred struct page init, is to spread out some potential contention on the host side? If yes, do we need NUMA balancing even to be already active during that phase? > The patch below addresses the issues I listed earlier. It is not yet ready > for application. Please see the issues listed below. > > Andrew, please drop the current patch. > > There are a few more things I am worried about: > > - The current get_page_from_freelist() and patched __alloc_pages_bulk() > only try to accept memory if the requested (alloc_flags & ALLOC_WMARK_MASK) > watermark check fails. For example, if a requested allocation with > ALLOC_WMARK_MIN is called, we will not try to accept more memory, which > could potentially put us under the high/promo watermark and cause the > following kswapd start to get us into an endless loop. > > Do we want to make memory acceptance in these paths independent of > alloc_flags? Hm ALLOC_WMARK_MIN will proceed, but with a watermark below the low watermark will still wake up kswapd, right? Isn't that another scenario where kswapd can start spinning? > - __isolate_free_page() removes a page from the free list without > accepting new memory. The function is called with the zone lock taken. > It is bad idea to accept memory while holding the zone lock, but > the alternative of pushing the accept to the caller is not much better. > > I have not observed any issues caused by __isolate_free_page() in > practice, but there is no reason why it couldn't potentially cause > problems. > > - The function take_pages_off_buddy() also removes pages from the free > list without accepting new memory. Unlike the function > __isolate_free_page(), it is called without the zone lock being held, so > we can accept memory there. I believe we should do so. > > I understand why adding unaccepted memory handling into the reclaim path > is questionable. However, it may be the best way to handle cases like > __isolate_free_page() and possibly others in the future that directly take > memory from free lists. Yes seems it might be not that bad solution, otherwise it could be hopeless whack-a-mole to prevent all corner cases where reclaim can be triggered without accepting memory first. Although just removing the lazy accept mode would be much more appealing solution than this :) > Any thoughts? Wonder if deferred struct page init has many of the same problems, i.e. with __isolate_free_page() and take_pages_off_buddy(), and if not, why? > I am still new to reclaim code and may be overlooking something > significant. Please correct any misconceptions you see. > > diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h > index c11b7cde81ef..5e0bdfbe2f1f 100644 > --- a/include/linux/mmzone.h > +++ b/include/linux/mmzone.h > @@ -667,6 +667,7 @@ enum zone_watermarks { > #define min_wmark_pages(z) (z->_watermark[WMARK_MIN] + z->watermark_boost) > #define low_wmark_pages(z) (z->_watermark[WMARK_LOW] + z->watermark_boost) > #define high_wmark_pages(z) (z->_watermark[WMARK_HIGH] + z->watermark_boost) > +#define promo_wmark_pages(z) (z->_watermark[WMARK_PROMO] + z->watermark_boost) > #define wmark_pages(z, i) (z->_watermark[i] + z->watermark_boost) > > /* > diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c > index c62805dbd608..d537c633c6e9 100644 > --- a/kernel/sched/fair.c > +++ b/kernel/sched/fair.c > @@ -1748,7 +1748,7 @@ static bool pgdat_free_space_enough(struct pglist_data *pgdat) > continue; > > if (zone_watermark_ok(zone, 0, > - wmark_pages(zone, WMARK_PROMO) + enough_wmark, > + promo_wmark_pages(zone) + enough_wmark, > ZONE_MOVABLE, 0)) > return true; > } > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index 14d39f34d336..b744743d14a2 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -4462,6 +4462,22 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, > alloc_flags, gfp)) { > break; > } > + > + if (has_unaccepted_memory()) { > + if (try_to_accept_memory(zone, 0)) > + break; > + } > + > +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT > + /* > + * Watermark failed for this zone, but see if we can > + * grow this zone if it contains deferred pages. > + */ > + if (deferred_pages_enabled()) { > + if (_deferred_grow_zone(zone, 0)) > + break; > + } > +#endif > } > > /* > @@ -5899,6 +5915,9 @@ static void __setup_per_zone_wmarks(void) > zone->_watermark[WMARK_PROMO] = high_wmark_pages(zone) + tmp; > > spin_unlock_irqrestore(&zone->lock, flags); > + > + if (managed_zone(zone)) > + try_to_accept_memory(zone, 0); > } > > /* update totalreserve_pages */ > @@ -6866,8 +6885,8 @@ static bool try_to_accept_memory(struct zone *zone, unsigned int order) > long to_accept; > int ret = false; > > - /* How much to accept to get to high watermark? */ > - to_accept = high_wmark_pages(zone) - > + /* How much to accept to get to promo watermark? */ > + to_accept = wmark_pages(zone, WMARK_PROMO) - > (zone_page_state(zone, NR_FREE_PAGES) - > __zone_watermark_unusable_free(zone, order, 0)); > > diff --git a/mm/vmscan.c b/mm/vmscan.c > index 3ef654addd44..d20242e36904 100644 > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -6607,7 +6607,7 @@ static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx) > continue; > > if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) > - mark = wmark_pages(zone, WMARK_PROMO); > + mark = promo_wmark_pages(zone); > else > mark = high_wmark_pages(zone); > if (zone_watermark_ok_safe(zone, order, mark, highest_zoneidx))