From nobody Fri Apr 17 06:35:59 2026 Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDD09332EC1 for ; Fri, 9 Jan 2026 10:52:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767955935; cv=none; b=lQrsIibXVocYEQdDFniImCAJnmPZSUgP9rbFy6j2la+Mh9LgKJmLIyLBeyxur42MUe2QUOTQzHzt9sNU/YZ8CRCFxFxwN/7k+WDPCDU0ubYy04wcUUY6Hh5w+Cr5/0mhqqtHbajkTcwlGUF/uOXMwycwBWnlTYWFRQZfAON6bBM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767955935; c=relaxed/simple; bh=PKjk3jV14zS9OlgkF9+U99EgQmBNatZi4SjfTopqExk=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=jgl34JlkFGGgTlg4JCUqdvBK7LB7+Ww1+H9I5acMkn/svhhHxj9DoU7GhM2OLbhiePk1D+6zWX+agkzN/9pWiPWPQBMC1g/FQd565vvq+S786Bt+5oOUR76Ah4r/4GRUMaLRGMeUZT8oG/soDWmO7CaMIScIXASoIj34Z4SBnRA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=eZaCSV4R; arc=none smtp.client-ip=95.215.58.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="eZaCSV4R" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767955931; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=1TQfjl1Oyg8oTFGlG7k+ntvES7Gvx/vdSZ107UMkTkk=; b=eZaCSV4R0Xi5F60N89Kd3yCS91+f00jluNA0tHPTGxReDiNpEL4Hm0eVG95mPaEN4TYduJ ioG4iPz0nUFlMzoGhxoyKORf6+IkyjCi/j9STFNx6Vx2Pg4Hnqi/PLTDqDvfW87c8hAOps e0CtIN2T6e+xF825mbZcFua9+hn/V9E= From: Yajun Deng To: akpm@linux-foundation.org, vbabka@suse.cz, surenb@google.com, mhocko@suse.com, jackmanb@google.com, hannes@cmpxchg.org, ziy@nvidia.com Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Yajun Deng Subject: [PATCH] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list() Date: Fri, 9 Jan 2026 18:51:21 +0800 Message-Id: <20260109105121.328780-1-yajun.deng@linux.dev> 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 X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" In move_to_free_list(), when a page block changes its migration type, we need to update free page counts for both the old and new types. Originally, this was done by two calls to account_freepages(), which updates NR_FREE_PAGES and also type-specific counters. However, this causes NR_FREE_PAGES to be updated twice, while the net change is zero in most cases. This patch introduces a new function account_freepages_both() that updates the statistics for both old and new migration types in one go. It avoids the double update of NR_FREE_PAGES by computing the net change only when the isolation status changes. The optimization avoid duplicate NR_FREE_PAGES updates in move_to_free_list(). Signed-off-by: Yajun Deng --- mm/page_alloc.c | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index ebfa07632995..e51d8bd7ab7d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -812,6 +812,16 @@ compaction_capture(struct capture_control *capc, struc= t page *page, } #endif /* CONFIG_COMPACTION */ =20 +static inline void account_specific_freepages(struct zone *zone, int nr_pa= ges, + int migratetype) +{ + if (is_migrate_cma(migratetype)) + __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages); + else if (migratetype =3D=3D MIGRATE_HIGHATOMIC) + WRITE_ONCE(zone->nr_free_highatomic, + zone->nr_free_highatomic + nr_pages); +} + static inline void account_freepages(struct zone *zone, int nr_pages, int migratetype) { @@ -822,11 +832,25 @@ static inline void account_freepages(struct zone *zon= e, int nr_pages, =20 __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages); =20 - if (is_migrate_cma(migratetype)) - __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages); - else if (migratetype =3D=3D MIGRATE_HIGHATOMIC) - WRITE_ONCE(zone->nr_free_highatomic, - zone->nr_free_highatomic + nr_pages); + account_specific_freepages(zone, nr_pages, migratetype); +} + +static inline void account_freepages_both(struct zone *zone, int nr_pages, + int old_mt, int new_mt) +{ + lockdep_assert_held(&zone->lock); + + bool old_isolated =3D is_migrate_isolate(old_mt); + bool new_isolated =3D is_migrate_isolate(new_mt); + + if (old_isolated !=3D new_isolated) + __mod_zone_page_state(zone, NR_FREE_PAGES, + old_isolated ? nr_pages : -nr_pages); + + if (!old_isolated) + account_specific_freepages(zone, -nr_pages, old_mt); + if (!new_isolated) + account_specific_freepages(zone, nr_pages, new_mt); } =20 /* Used for pages not on another list */ @@ -869,8 +893,7 @@ static inline void move_to_free_list(struct page *page,= struct zone *zone, =20 list_move_tail(&page->buddy_list, &area->free_list[new_mt]); =20 - account_freepages(zone, -nr_pages, old_mt); - account_freepages(zone, nr_pages, new_mt); + account_freepages_both(zone, nr_pages, old_mt, new_mt); =20 if (order >=3D pageblock_order && is_migrate_isolate(old_mt) !=3D is_migrate_isolate(new_mt)) { --=20 2.34.1