From nobody Fri Jun 12 22:50:36 2026 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BF01266B72 for ; Tue, 12 May 2026 02:20:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778552421; cv=none; b=tKHgk9Pt932PNoyYgwJvUVR+TEaFy9kuh6vDYrkZP4cKIqSG6VEP70DFt5Z4329IImNQpJWoJCoNoYqLlDk/JhQII2YPMbWtRaRHYBeZAmrpVah6mzgrTY7gV4v2SVpxdh0KHNksFRh/WP2dTZvlCjSeIuGqgnOsQstSm1j3O64= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778552421; c=relaxed/simple; bh=9kCQYvZbkLONuMS842oFCdINLqnsKsoDikNYn/YXN9s=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=JfJiQ2Owjy1rHAi8HQ+JBvkxLJFJ6/1aIKmBdjpXDYa2W+UN9gC9cmE2TVWNA8bUhUWmtErFOAjoaI0KWEoUDyKKCEmRexPxpMN3zpPPkAfPCFBpPUBy9aVAhDI9QSyhxggq4Iziip27CycddWrcZtFmXH2byxC8gaQpZlxtG8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=PrH1+PC7; arc=none smtp.client-ip=209.85.216.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="PrH1+PC7" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-3664df30f53so2202888a91.1 for ; Mon, 11 May 2026 19:20:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1778552419; x=1779157219; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3abEkWy1zSpEx/nYCxvV4AVhIZirosMNZhqnKUXsN/k=; b=PrH1+PC7fVqW0CjLFbmBnIFkoz9NRv1/KrHUbQK9LP5/ab/wnqmKvycADfW6hQ58Z9 uRqTgMX+vo35xSVWHB6A9HPG0SRxBh1VGjAo7HgwGxeL0LUiJpk630q7JZFWY61l7gL3 TMlDhw945vDH0A91ybGz6o93pcu4P2UZ4VAh1g7uPTXQ/giaIbcbNkhQvspTcXIrWay+ 49SuCMOtKfoAc3x4VgTWWxVGKNSJBotXjmsE6CyepDg6wQXZr4VoDuOaFtXrVNc3Smd0 BIhxjUcYl2yJu7phSUYLYUjeMsLqRwq5K/Tg3JNFlyFMfotKZeMck+6dG6oVJHKUI7Ej wtiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778552419; x=1779157219; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=3abEkWy1zSpEx/nYCxvV4AVhIZirosMNZhqnKUXsN/k=; b=QrUSWrXjOS6jPatS8CLOW07B3LZbeEnWmGkqMAIV/PWcx9DBD/iuwK0MJxDsFfxzHy Ti5IArz4O6rXvV+CdRDxSX5ggGl3z9/SJtO5sUtPF0NEcz8P3HgDnjESbzOyi0uHdt+/ 1uWgH3rFiy008om9mPR6t4UXXbTA/rxxt+lAoXlvj7XvyyFZxTacFWud3QauqOzWWfOp 5NR9ZjDDkTi/o8yuV09SUCjJ6u5NUyi6ZFRm/ZKz86vQM4C7Zdsu/iq+RMgN9pDumu7Z mNSEfBdS4M12DeeRAz80Y21+1Sblufg2Fc8fl/ZsNJ2aGXiX3l2c4KLln/WR853DYIff FzRQ== X-Forwarded-Encrypted: i=1; AFNElJ8QGhq/JgwkjU68uFzoJkcgOEXmP8PE8MWBDZlO9QA7Y1dZxY2N8A+36WjA4QmeO60O16uTGbTGJ3IYb9I=@vger.kernel.org X-Gm-Message-State: AOJu0Yzkxzvdkc3GYLZRSgiCq2kuLlVI/nbLvl1yMJ7Rb45OzyMjOD6Y KcZAz+Jq40ygKs7TgYD6h4tBpAK57L6ANg0EZUp5AxN+PZGB0ZRhTa6zAhJj9o788yY= X-Gm-Gg: Acq92OH7fMb2H0pGom2Ib30JZEfdLA47Z5bR2fMJmK41CgpbNhJGLXn4FxESl7lU1+e oRwT1JAahkFpvIjVLazTBp+VFQ2XYdiAYIHKwRTfZ78xkP5yQUji0kKoLCJfO6QfW9sMGxfo/cp 0Sf5EamOfxQndp3D9oWtPELhz+bHI0MfIbAiWTUurYbe+gOhQr2MSEnVBZn1JDH59wyVz/X6neH oKGb44eVq/2hhAZMNq0mPMiiFN+rJHtsdX9rYTYcMbdXWJPEbaTZ2mFOOvxvZbJQ7sUBNTyYlFD rWOiyH9TClJOqrGHxaStijtflPkmqyAiqsJAlc7xEjQyxmhgWE8mQb2wvwbghvJNIuP7Htx19Fr WLGaAig+QVtxbr3T1RvBcpVYKE4h1NRppj6pWtjDx1S0yadNKm4XLY5ltw7xQr/0pPlniUOlEUK ce4juKzMHv/Bso04dBWaKG0rS4xcGzoFJ2isL1spOdT9c= X-Received: by 2002:a17:90b:5787:b0:367:f9f1:af78 with SMTP id 98e67ed59e1d1-368b24ca34emr1232873a91.7.1778552418807; Mon, 11 May 2026 19:20:18 -0700 (PDT) Received: from n232-176-004.byted.org ([36.110.163.96]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-367d687bedesm11806019a91.16.2026.05.11.19.20.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 May 2026 19:20:18 -0700 (PDT) From: Muchun Song To: Andrew Morton , Vlastimil Babka Cc: Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Zi Yan , zihan zhou <15645113830zzh@gmail.com>, yaowenchao , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , muchun.song@linux.dev Subject: [PATCH v2] mm/page_alloc: fix zone reserve update serialization Date: Tue, 12 May 2026 10:19:44 +0800 Message-ID: <20260512021944.3445546-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.54.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 ma= naged pages change") Signed-off-by: Muchun Song --- 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); } =20 +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); =20 - spin_lock(&lock); - __setup_per_zone_wmarks(); - spin_unlock(&lock); + scoped_guard(spinlock_irqsave, &zone_reserve_lock) + __setup_per_zone_wmarks(); =20 /* * The watermark size have changed so update the pcpu batch base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83 --=20 2.54.0