From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (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 038243F44E4 for ; Wed, 20 May 2026 15:00:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=96.67.55.147 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779289255; cv=none; b=DaHjmD+86G6iHyOUfA5yVbZ7ukqtayD/RQ6urVgrDT9wn1LoUk6FWEmpcU3SfC34wcPhenanvcfzW0RuleZupAgwmmVSs2QSWbGw2RxRflYWtdPgH1Trq8sGiQ5wt0RlLzpK+QKLY3BWV45BHdlirFS3QD/N/ojugJJS26op8Ag= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779289255; c=relaxed/simple; bh=CYJ+cYBNmtonKiSOG59OLHc5C51h4xmSg9F/jy8nbC8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=K0PCZ5MYWqj6sb1I/81cndICsULRtx/K0WYIeN4d7XDd2R7n2L7El/4gtXtqEXbBytnoFi3LH/el/QwwpiBKUfo/BCDWg+xONPXoKhMyAfe2+4lMNOMbaOnqLaN11zRKuPL/82woUIR6G5XwZzITh0ipNrOejUQSejiEQwwYrr8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.b=Ik8r2kWH; arc=none smtp.client-ip=96.67.55.147 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.b="Ik8r2kWH" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=NwVqkzhkrdkV2AHWI0B8Z//Qg+XaJB1kiB31HcDYG68=; b=Ik8r2kWHczdta/uHFI6arjJ/fs Nuxve6+zYFQbMfUKTwu96aIJ4bsaeE+6DUs44cnubs5qsxKw0aaAWtMR0RHAAFTSPt+KdvWwU5s6B gnf+YgRFfElKUeUymHTUeVN8Vynvnt13z0GRYwsqZbu0FaP3v5XKzANik0nY4Gu6oJW7Fo04gHXCd DN5h4lZkinXbZtGOmbw0HCC0Vw3FPiGZ6SDDwlOGRQu7H4L0OGaA82V8zYj66iPDt03r1pfQPiRrU Lary9QMc44z5C8lN3kzxld/zd1Ypydi8z59mKk300kVFrVi+gd906D4rfRDAU3KMMjgfyX5Siv24Q IDUUEwWw==; Received: from fangorn.home.surriel.com ([10.0.13.7]) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1wPiPM-0000000024Q-2iBW; Wed, 20 May 2026 11:00:28 -0400 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, linux-mm@kvack.org, david@kernel.org, willy@infradead.org, surenb@google.com, hannes@cmpxchg.org, ljs@kernel.org, ziy@nvidia.com, usama.arif@linux.dev, fvdl@google.com, Rik van Riel Subject: [RFC PATCH 22/40] mm: page_alloc: add CONFIG_DEBUG_VM sanity checks for SPB counters Date: Wed, 20 May 2026 10:59:28 -0400 Message-ID: <20260520150018.2491267-23-riel@surriel.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260520150018.2491267-1-riel@surriel.com> References: <20260520150018.2491267-1-riel@surriel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add spb_debug_check() and call it after every site that mutates the per-superpageblock type counters (nr_free / nr_unmovable / nr_reclaimable / nr_movable). Each counter must be <= total_pageblocks; a violation indicates that a PB_has_ bit transition was missed by one of the allocation, free, claim, or evacuation paths and the counter has drifted out of sync with the bits. VM_WARN_ONCE keeps the production cost zero (CONFIG_DEBUG_VM only) while giving us a single place to catch counter drift early during stress testing instead of debugging it from a much later misaccounting symptom. Relax three pre-existing VM_WARN_ONCE checks in __add_to_free_list, move_to_free_list, and __del_page_from_free_list so they no longer warn for MIGRATE_ISOLATE / MIGRATE_CMA pageblocks. Those legitimately carry stale per-type counters from the isolation/un-isolation flow, and the warnings would fire spuriously once spb_debug_check() exposes that path under load. Signed-off-by: Rik van Riel Assisted-by: Claude:claude-opus-4.7 syzkaller --- mm/page_alloc.c | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 8027412da866..e267390a5948 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -477,6 +477,32 @@ static inline int migratetype_to_has_bit(int migratetype) } } +#ifdef CONFIG_DEBUG_VM +static void spb_debug_check(struct superpageblock *sb, const char *caller) +{ + u16 total = sb->total_pageblocks; + + VM_WARN_ONCE(sb->nr_free > total, + "%s: nr_free %u > total %u (zone=%s sb=%lu)\n", + caller, sb->nr_free, total, sb->zone->name, + (unsigned long)(sb - sb->zone->superpageblocks)); + VM_WARN_ONCE(sb->nr_unmovable > total, + "%s: nr_unmovable %u > total %u (zone=%s sb=%lu)\n", + caller, sb->nr_unmovable, total, sb->zone->name, + (unsigned long)(sb - sb->zone->superpageblocks)); + VM_WARN_ONCE(sb->nr_reclaimable > total, + "%s: nr_reclaimable %u > total %u (zone=%s sb=%lu)\n", + caller, sb->nr_reclaimable, total, sb->zone->name, + (unsigned long)(sb - sb->zone->superpageblocks)); + VM_WARN_ONCE(sb->nr_movable > total, + "%s: nr_movable %u > total %u (zone=%s sb=%lu)\n", + caller, sb->nr_movable, total, sb->zone->name, + (unsigned long)(sb - sb->zone->superpageblocks)); +} +#else +static inline void spb_debug_check(struct superpageblock *sb, const char *caller) {} +#endif + /* * __spb_set_has_type - set PB_has_* and increment type counter * @@ -508,6 +534,7 @@ static void __spb_set_has_type(struct page *page, int migratetype) sb->nr_movable++; break; } + spb_debug_check(sb, "__spb_set_has_type"); } } @@ -545,6 +572,7 @@ static void __spb_clear_has_type(struct page *page, int migratetype) sb->nr_movable--; break; } + spb_debug_check(sb, "__spb_clear_has_type"); } } @@ -778,6 +806,7 @@ static void superpageblock_pb_now_free(struct page *page) return; sb->nr_free++; + spb_debug_check(sb, "pb_now_free"); spb_update_list(sb); } @@ -800,6 +829,7 @@ static void superpageblock_pb_now_used(struct page *page) if (sb->nr_free) sb->nr_free--; + spb_debug_check(sb, "pb_now_used"); spb_update_list(sb); } @@ -1265,7 +1295,9 @@ static inline void __add_to_free_list(struct page *page, struct zone *zone, struct free_area *area = pfn_sb_free_area(zone, pfn, order, &sb); int nr_pages = 1 << order; - VM_WARN_ONCE(get_pageblock_migratetype(page) != migratetype, + VM_WARN_ONCE(get_pageblock_migratetype(page) != migratetype && + !is_migrate_isolate(get_pageblock_migratetype(page)) && + !is_migrate_cma(get_pageblock_migratetype(page)), "page type is %d, passed migratetype is %d (nr=%d)\n", get_pageblock_migratetype(page), migratetype, nr_pages); @@ -1299,7 +1331,8 @@ static inline void move_to_free_list(struct page *page, struct zone *zone, int nr_pages = 1 << order; /* Free page moving can fail, so it happens before the type update */ - VM_WARN_ONCE(get_pageblock_migratetype(page) != old_mt, + VM_WARN_ONCE(get_pageblock_migratetype(page) != old_mt && + !is_migrate_cma(get_pageblock_migratetype(page)), "page type is %d, passed migratetype is %d (nr=%d)\n", get_pageblock_migratetype(page), old_mt, nr_pages); @@ -1324,7 +1357,9 @@ static inline void __del_page_from_free_list(struct page *page, struct zone *zon struct free_area *area = pfn_sb_free_area(zone, pfn, order, &sb); int nr_pages = 1 << order; - VM_WARN_ONCE(get_pageblock_migratetype(page) != migratetype, + VM_WARN_ONCE(get_pageblock_migratetype(page) != migratetype && + !is_migrate_isolate(get_pageblock_migratetype(page)) && + !is_migrate_cma(get_pageblock_migratetype(page)), "page type is %d, passed migratetype is %d (nr=%d)\n", get_pageblock_migratetype(page), migratetype, nr_pages); -- 2.54.0