Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Muchun Song <songmuchun@bytedance.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Vlastimil Babka <vbabka@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>,
	Michal Hocko <mhocko@suse.com>,
	Brendan Jackman <jackmanb@google.com>,
	Johannes Weiner <hannes@cmpxchg.org>, Zi Yan <ziy@nvidia.com>,
	zihan zhou <15645113830zzh@gmail.com>,
	yaowenchao <yaowenchao@jd.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Muchun Song <songmuchun@bytedance.com>,
	muchun.song@linux.dev
Subject: [PATCH v2] mm/page_alloc: fix zone reserve update serialization
Date: Tue, 12 May 2026 10:19:44 +0800	[thread overview]
Message-ID: <20260512021944.3445546-1-songmuchun@bytedance.com> (raw)

Commit 9726891fe753 ("mm/page_alloc: recalculate zone reserve pages
when managed pages change") moved setup_per_zone_lowmem_reserve() into
adjust_managed_page_count(), so zone reserve recalculation can now be
triggered from paths that run concurrently on different CPUs.

setup_per_zone_lowmem_reserve() updates zone->lowmem_reserve[],
pgdat->totalreserve_pages and the global totalreserve_pages as one
logical operation, but adjust_managed_page_count() does not serialize
those updates. Concurrent callers can therefore interleave the reserve
recalculation and leave the reserve accounting temporarily inconsistent.

This race was identified by code inspection rather than by a reported
runtime failure. However, these reserve counters are used by the page
allocator and reclaim paths to make allocation and watermark decisions,
so it is preferable to avoid publishing inconsistent values.

Serialize adjust_managed_page_count() to make each reserve recalculation
observe and publish a consistent state.

Fixes: 9726891fe753 ("mm/page_alloc: recalculate zone reserve pages when managed pages change")
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
v1->v2:
- expand the changelog to explain why the theoretical race matters
---
 mm/page_alloc.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 3a56825a7fc5..0989067da588 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -6384,6 +6384,8 @@ static void calculate_totalreserve_pages(void)
 	trace_mm_calculate_totalreserve_pages(totalreserve_pages);
 }
 
+static DEFINE_SPINLOCK(zone_reserve_lock);
+
 /*
  * setup_per_zone_lowmem_reserve - called whenever
  *	sysctl_lowmem_reserve_ratio changes.  Ensures that each zone
@@ -6394,6 +6396,8 @@ static void setup_per_zone_lowmem_reserve(void)
 {
 	struct pglist_data *pgdat;
 	enum zone_type i, j;
+
+	guard(spinlock_irqsave)(&zone_reserve_lock);
 	/*
 	 * For a given zone node_zones[i], lowmem_reserve[j] (j > i)
 	 * represents how many pages in zone i must effectively be kept
@@ -6509,11 +6513,9 @@ static void __setup_per_zone_wmarks(void)
 void setup_per_zone_wmarks(void)
 {
 	struct zone *zone;
-	static DEFINE_SPINLOCK(lock);
 
-	spin_lock(&lock);
-	__setup_per_zone_wmarks();
-	spin_unlock(&lock);
+	scoped_guard(spinlock_irqsave, &zone_reserve_lock)
+		__setup_per_zone_wmarks();
 
 	/*
 	 * The watermark size have changed so update the pcpu batch

base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
-- 
2.54.0



                 reply	other threads:[~2026-05-12  2:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260512021944.3445546-1-songmuchun@bytedance.com \
    --to=songmuchun@bytedance.com \
    --cc=15645113830zzh@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=hannes@cmpxchg.org \
    --cc=jackmanb@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=surenb@google.com \
    --cc=vbabka@kernel.org \
    --cc=yaowenchao@jd.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox