From nobody Sun Feb 8 02:21:56 2026 Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) (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 5FC972D063E for ; Mon, 12 Jan 2026 12:16:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.185 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768220197; cv=none; b=Cm2xHRP+Xvrsd5v6pyyp3WaWg4S1y7+MsqiAHU+MgVbihL14XWt6YQBfCYEMLUPTYBwhczEyRTi3Dl2aqj2est+at/m/MLBn4Qa+b+piDFDUgdRgYptZuvUAf6Q3MoorbyDLppZRy3emGPIZwfe/00AgY58yuqvFlCgug3p/zJw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768220197; c=relaxed/simple; bh=2WZet1unRNDs8W7gpVZ9hjAomEFKstjf6Jl4HEpwNvA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=txKiSCybEbQ9qxYcRn2RssEIYBS47sf8jLLsgPDBcxAtiv/VOQZwAGL07+ZVsqXLNldwi+g+RFRy1lCSfRmICv3jleMf4F4RmczDbR7ALmxERnE7jbSKjhAjKTPDNlv7rkB6rZLOV5msiJX2nIAi7oOtsDGtdSgPk59+lbS89NU= 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=imbyE6ku; arc=none smtp.client-ip=91.218.175.185 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="imbyE6ku" 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=1768220194; 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=OVWY/dnICKjphyKsb50bvyo0Pt8KlZMZFnV041JZSGk=; b=imbyE6ku6JxtT9B0A+vaToGO5vlqkt7Ohwuao9YbvaEPl/EPO9MnlXX/RjHp3KvezjLKO5 +s28Gix19dhPj49Vq35vDTkuERrNZpUSgn606YK5ppAMVqjPqqjAF2LZEzLuuqjtAfVPj2 kmM4kArMsouyjodp7X0v2YVxSDCOUUE= 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 , Joshua Hahn Subject: [PATCH v2] mm/page_alloc: Avoid duplicate NR_FREE_PAGES updates in move_to_free_list() Date: Mon, 12 Jan 2026 20:16:14 +0800 Message-Id: <20260112121614.1840607-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 adds a condition that updates the NR_FREE_PAGES only if one of the two types is the isolate type. This avoids NR_FREE_PAGES being updates twice. The optimization avoid duplicate NR_FREE_PAGES updates in move_to_free_list(). Signed-off-by: Yajun Deng Suggested-by: Joshua Hahn Reviewed-by: Joshua Hahn --- v2: remove account_freepages_both(). v1: https://lore.kernel.org/all/20260109105121.328780-1-yajun.deng@linux.= dev/ --- mm/page_alloc.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index ebfa07632995..d56e94eb4914 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,7 @@ static inline void account_freepages(struct zone *zone= , 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); } =20 /* Used for pages not on another list */ @@ -861,6 +867,8 @@ static inline void move_to_free_list(struct page *page,= struct zone *zone, { struct free_area *area =3D &zone->free_area[order]; int nr_pages =3D 1 << order; + bool old_isolated =3D is_migrate_isolate(old_mt); + bool new_isolated =3D is_migrate_isolate(new_mt); =20 /* Free page moving can fail, so it happens before the type update */ VM_WARN_ONCE(get_pageblock_migratetype(page) !=3D old_mt, @@ -869,14 +877,18 @@ static inline void move_to_free_list(struct page *pag= e, 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); - - if (order >=3D pageblock_order && - is_migrate_isolate(old_mt) !=3D is_migrate_isolate(new_mt)) { - if (!is_migrate_isolate(old_mt)) - nr_pages =3D -nr_pages; - __mod_zone_page_state(zone, NR_FREE_PAGES_BLOCKS, nr_pages); + if (!old_isolated) + account_specific_freepages(zone, -nr_pages, old_mt); + if (!new_isolated) + account_specific_freepages(zone, nr_pages, new_mt); + + /* Only update NR_FREE_PAGES if one of them is isolate type */ + if (old_isolated !=3D new_isolated) { + nr_pages =3D old_isolated ? nr_pages : -nr_pages; + __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages); + if (order >=3D pageblock_order) + __mod_zone_page_state(zone, NR_FREE_PAGES_BLOCKS, + nr_pages); } } =20 --=20 2.34.1