Re: [RFC PATCH 4/4] mm: adjust free_pages to make __zone_watermark_ok() mTHP-aware

From: Karim Manaouil

Date: Fri Sep 18 2026 - 08:11:36 EST


On Fri, Sep 18, 2026 at 10:42:38AM +0800, Bo Zhang wrote:
> On Thu, Sep 17, 2026 at 06:16:42PM +0100, Karim Manaouil wrote:
> > > + if (READ_ONCE(huge_anon_orders_always) && order == compact_hpage_order()) {
> > > + unsigned long free_pages = 0;
> > > +
> > > + for (int o = order; o < NR_PAGE_ORDERS; o++)
> > > + free_pages += zone->free_area[o].nr_free << o;
> >
> > I understand that you don't need a precise value here, but in every
> > instance in the kernel source tree where nr_free is accessed, it's
> > accessed under the zone lock. Do we need to use READ_ONCE()?
>
> Good point, thanks. The nr_free read here is an intentional lockless
> estimate. I'll annotate it with data_race(), matching how
> fill_contig_page_info() in mm/vmstat.c reads the same
> zone->free_area[].nr_free field without the zone lock:
>
> free_pages += data_race(zone->free_area[o].nr_free) << o;
>
> data_race() documents the intended racy read and keeps KCSAN quiet.
> Happy to use READ_ONCE() instead if you'd prefer.

Yeah, data_race() is probably a good starting point. Thanks!

> Bo

--
~karim