From nobody Mon Apr 6 09:12:37 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 775FA3EF655 for ; Fri, 20 Mar 2026 18:24:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774031054; cv=none; b=QThIrlOwI0Ha4spd2CzZNzY94LGHrm9ZWZ5JqooaUTSQQHCWcI1rWgg19jqRTAQjGeXwa2vK+R/x5eNrjyVUsx1Gj7RclE97Zv12jO9y0YDT+N/OFeT/TkFrjcizIA2O4iBNeot30Avv6wHdObDCzo1P4WU19TWu4tdsYf5MOGU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774031054; c=relaxed/simple; bh=ier67wHk0TKfjGKnTvFub1nmVzN5c2Hwuz+EgFkKWVo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=P5QZQuPHTHjySQAw/xsI6ust4ck4HGz1B7e2exRk9rk/unwJpiscobI3wrlhMuZpjfgoJKcmKjr7UxAkp15ANZYCm6sHWAWPdBcf2IOip76Yeq10OvKzB5sfVZR2JOfAlqZktlrk7t/mWvfFszQO5mxnafvm8VQhp2d2eGtqSG4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=P0gT0+pC; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="P0gT0+pC" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4852d27f473so10430175e9.1 for ; Fri, 20 Mar 2026 11:24:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774031051; x=1774635851; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kLbn0EHSGtWY5aefUcemJOfT2K5sa2B4hdTOFu3TqVI=; b=P0gT0+pClB/8UWhzbSNu5I/cPRvyBATRPTIdn1+eFkdNgyVHakpKB/EoCEUcZabMeg WQpLXZUrzons0a2TxoPoav/pLc2+7ETBPA7Km9U7qdQC1ocGDg/yyJsq2+gv8q0P1Jo3 jDfPtFwnJsy9rvJshBiB2Q9/lDZ+/8j6zfU9tS5RZ8nDc/wsUxoGUmEl5yyNfcJ6uvzE kpDFHYBIsnKTX/9HNOQ7UwfX8XalRF1GncHMwYK4hZLECTHGp4kgBumdQ/b39bMYw21A bu9e1Fh7LxGJPAMJXWKn9UjriY49pkl6KrJOXUo7xnp/3o2hK2zFylyQ57/q4Ua3SHWg v9XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774031051; x=1774635851; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kLbn0EHSGtWY5aefUcemJOfT2K5sa2B4hdTOFu3TqVI=; b=q23f0s/b0AA0Pb6H4eYTXTY8uUIWOGEJGDix8j9eyYZV02QZjFHXWT4kXYNsdsDirN aNlt22J+MkzpfK8ID6Nv/gIpVXOEJXjfAE3VgqcZ7XQIj0IY237gi/Eg8NMGIg0Xqp7V 6vtMyXzZZGxLHse094Kyj8vQ9hm9OVN4wIU/2sFZ/0RRZgpBX+HR8D4nvfCfBIRseRZ6 6bzYQHun8NWj3O7FiNqc1fKkNMEXpkSRBbhaRJIDkdSVzgFWtxxlyh0TRFRYiAeakZIu RscJtF9Bq5yqe/wjkWM2oIKPAdbIdphD1Knes4k+FvNLXUPRl6eJn283g05e6DMKeP7Z VYxQ== X-Forwarded-Encrypted: i=1; AJvYcCW91x7SlXBwuPopWDa0Y7WwPsldNJdc8szUYaLD75wMjjG35dgchXZHZtcuE/2/61yxTk+RC2caHkSM9iY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4mrwRjNROjmJiiKYRcD00J2O/5ImwlGIlMu2eoiYX29wG1bI/ SLh/BLec5rFBXELydGqyfXWwFWRgiJEDx3ywuwJ+87HtMOTeZjGIM7tBfDMseXPVvpttYklf/Ei ETYj8hjxLpffAig== X-Received: from wmbhi3.prod.google.com ([2002:a05:600c:5343:b0:47f:941a:613d]) (user=jackmanb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8710:b0:485:49c5:8eb7 with SMTP id 5b1f17b1804b1-486fee049b8mr57421845e9.22.1774031050575; Fri, 20 Mar 2026 11:24:10 -0700 (PDT) Date: Fri, 20 Mar 2026 18:23:45 +0000 In-Reply-To: <20260320-page_alloc-unmapped-v2-0-28bf1bd54f41@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260320-page_alloc-unmapped-v2-0-28bf1bd54f41@google.com> X-Mailer: b4 0.14.3 Message-ID: <20260320-page_alloc-unmapped-v2-21-28bf1bd54f41@google.com> Subject: [PATCH v2 21/22] mm: Minimal KUnit tests for some new page_alloc logic From: Brendan Jackman To: Borislav Petkov , Dave Hansen , Peter Zijlstra , Andrew Morton , David Hildenbrand , Vlastimil Babka , Wei Xu , Johannes Weiner , Zi Yan , Lorenzo Stoakes Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, rppt@kernel.org, Sumit Garg , derkling@google.com, reijiw@google.com, Will Deacon , rientjes@google.com, "Kalyazin, Nikita" , patrick.roy@linux.dev, "Itazuri, Takahiro" , Andy Lutomirski , David Kaplan , Thomas Gleixner , Brendan Jackman , Yosry Ahmed Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add a simple smoke test for __GFP_UNMAPPED that tries to exercise flipping pageblocks between mapped/unmapped state. Also add some basic tests for some freelist-indexing helpers. Simplest way to run these on x86: tools/testing/kunit/kunit.py run --arch=3Dx86_64 "page_alloc.*" \ --kconfig_add CONFIG_MERMAP=3Dy --kconfig_add CONFIG_PAGE_ALLOC_UNMAPPED= =3Dy Signed-off-by: Brendan Jackman --- kernel/panic.c | 2 + mm/Kconfig | 2 +- mm/Makefile | 1 + mm/init-mm.c | 3 + mm/internal.h | 6 ++ mm/page_alloc.c | 11 +- mm/tests/page_alloc_kunit.c | 250 ++++++++++++++++++++++++++++++++++++++++= ++++ 7 files changed, 271 insertions(+), 4 deletions(-) diff --git a/kernel/panic.c b/kernel/panic.c index 20feada5319d4..1301444a0447a 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -39,6 +39,7 @@ #include #include #include +#include =20 #define PANIC_TIMER_STEP 100 #define PANIC_BLINK_SPD 18 @@ -941,6 +942,7 @@ unsigned long get_taint(void) { return tainted_mask; } +EXPORT_SYMBOL_IF_KUNIT(get_taint); =20 /** * add_taint: add a taint flag if not already set. diff --git a/mm/Kconfig b/mm/Kconfig index 05b2bb841d0e0..2021f52a0c422 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1509,7 +1509,7 @@ config PAGE_ALLOC_UNMAPPED bool "Support allocating pages that aren't in the direct map" depends on MERMAP =20 -config PAGE_ALLOC_KUNIT_TESTS +config PAGE_ALLOC_KUNIT_TEST tristate "KUnit tests for the page allocator" if !KUNIT_ALL_TESTS depends on KUNIT default KUNIT_ALL_TESTS diff --git a/mm/Makefile b/mm/Makefile index 93a1756303cf9..11849162b6f5a 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -152,3 +152,4 @@ obj-$(CONFIG_TMPFS_QUOTA) +=3D shmem_quota.o obj-$(CONFIG_LAZY_MMU_MODE_KUNIT_TEST) +=3D tests/lazy_mmu_mode_kunit.o obj-$(CONFIG_MERMAP) +=3D mermap.o obj-$(CONFIG_MERMAP_KUNIT_TEST) +=3D tests/mermap_kunit.o +obj-$(CONFIG_PAGE_ALLOC_KUNIT_TEST) +=3D tests/page_alloc_kunit.o diff --git a/mm/init-mm.c b/mm/init-mm.c index c5556bb9d5f01..31103356da654 100644 --- a/mm/init-mm.c +++ b/mm/init-mm.c @@ -13,6 +13,8 @@ #include #include =20 +#include + #ifndef INIT_MM_CONTEXT #define INIT_MM_CONTEXT(name) #endif @@ -50,6 +52,7 @@ struct mm_struct init_mm =3D { .flexible_array =3D MM_STRUCT_FLEXIBLE_ARRAY_INIT, INIT_MM_CONTEXT(init_mm) }; +EXPORT_SYMBOL_IF_KUNIT(init_mm); =20 void setup_initial_init_mm(void *start_code, void *end_code, void *end_data, void *brk) diff --git a/mm/internal.h b/mm/internal.h index 865991aca06ea..6c652148bc906 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1908,4 +1908,10 @@ int __apply_to_page_range(struct mm_struct *mm, unsi= gned long addr, unsigned long size, pte_fn_t fn, void *data, unsigned int flags); =20 +#if IS_ENABLED(CONFIG_KUNIT) +unsigned int order_to_pindex(freetype_t freetype, int order); +int pindex_to_order(unsigned int pindex); +bool pcp_allowed_order(unsigned int order); +#endif /* IS_ENABLED(CONFIG_KUNIT) */ + #endif /* __MM_INTERNAL_H */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 7c91dcbe32576..291ba32f1f1ad 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -58,6 +58,7 @@ #include #include #include +#include #include "internal.h" #include "shuffle.h" #include "page_reporting.h" @@ -461,6 +462,7 @@ get_pfnblock_freetype(const struct page *page, unsigned= long pfn) { return __get_pfnblock_freetype(page, pfn, 0); } +EXPORT_SYMBOL_IF_KUNIT(get_pfnblock_freetype); =20 =20 /** @@ -696,7 +698,7 @@ static void bad_page(struct page *page, const char *rea= son) add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); } =20 -static inline unsigned int order_to_pindex(freetype_t freetype, int order) +VISIBLE_IF_KUNIT inline unsigned int order_to_pindex(freetype_t freetype, = int order) { int migratetype =3D free_to_migratetype(freetype); =20 @@ -724,8 +726,9 @@ static inline unsigned int order_to_pindex(freetype_t f= reetype, int order) =20 return (MIGRATE_PCPTYPES * order) + migratetype; } +EXPORT_SYMBOL_IF_KUNIT(order_to_pindex); =20 -static inline int pindex_to_order(unsigned int pindex) +VISIBLE_IF_KUNIT int pindex_to_order(unsigned int pindex) { unsigned int unmapped_base =3D NR_LOWORDER_PCP_LISTS + NR_PCP_THP; int order; @@ -748,8 +751,9 @@ static inline int pindex_to_order(unsigned int pindex) =20 return order; } +EXPORT_SYMBOL_IF_KUNIT(pindex_to_order); =20 -static inline bool pcp_allowed_order(unsigned int order) +VISIBLE_IF_KUNIT inline bool pcp_allowed_order(unsigned int order) { if (order <=3D PAGE_ALLOC_COSTLY_ORDER) return true; @@ -759,6 +763,7 @@ static inline bool pcp_allowed_order(unsigned int order) #endif return false; } +EXPORT_SYMBOL_IF_KUNIT(pcp_allowed_order); =20 /* * Higher-order pages are called "compound pages". They are structured th= usly: diff --git a/mm/tests/page_alloc_kunit.c b/mm/tests/page_alloc_kunit.c new file mode 100644 index 0000000000000..bd55d0bc35ac9 --- /dev/null +++ b/mm/tests/page_alloc_kunit.c @@ -0,0 +1,250 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "internal.h" + +struct free_pages_ctx { + unsigned int order; + struct list_head pages; +}; + +static inline void action_many__free_pages(void *context) +{ + struct free_pages_ctx *ctx =3D context; + struct page *page, *tmp; + + list_for_each_entry_safe(page, tmp, &ctx->pages, lru) + __free_pages(page, ctx->order); +} + +/* + * Allocate a bunch of pages with the same order and GFP flags, transparen= tly + * take care of error handling and cleanup. Does this all via a single KUn= it + * resource, i.e. has a fixed memory overhead. + */ +static inline struct free_pages_ctx * +do_many_alloc_pages(struct kunit *test, gfp_t gfp, + unsigned int order, unsigned int count) +{ + struct free_pages_ctx *ctx =3D kunit_kzalloc( + test, sizeof(struct free_pages_ctx), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, ctx); + INIT_LIST_HEAD(&ctx->pages); + ctx->order =3D order; + + for (int i =3D 0; i < count; i++) { + struct page *page =3D alloc_pages(gfp, order); + + if (!page) { + struct page *page, *tmp; + + list_for_each_entry_safe(page, tmp, &ctx->pages, lru) + __free_pages(page, order); + + KUNIT_FAIL_AND_ABORT(test, + "Failed to alloc order %d page (GFP *%pG) iter %d", + order, &gfp, i); + } + list_add(&page->lru, &ctx->pages); + } + + KUNIT_ASSERT_EQ(test, + kunit_add_action_or_reset(test, action_many__free_pages, ctx), 0); + return ctx; +} + +#ifdef CONFIG_PAGE_ALLOC_UNMAPPED + +static const gfp_t gfp_params_array[] =3D { + 0, + __GFP_ZERO, +}; + +static void gfp_param_get_desc(const gfp_t *gfp, char *desc) +{ + snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%pGg", gfp); +} + +KUNIT_ARRAY_PARAM(gfp, gfp_params_array, gfp_param_get_desc); + +/* Do some allocations that force the allocator to map/unmap some blocks. = */ +static void test_alloc_map_unmap(struct kunit *test) +{ + unsigned long page_majority; + struct free_pages_ctx *ctx; + const gfp_t *gfp_extra =3D test->param_value; + gfp_t gfp =3D GFP_KERNEL | __GFP_THISNODE | __GFP_UNMAPPED | *gfp_extra; + struct page *page; + + kunit_attach_mm(); + mermap_mm_prepare(current->mm); + + /* No cleanup here - assuming kthread "belongs" to this test. */ + set_cpus_allowed_ptr(current, cpumask_of_node(numa_node_id())); + + /* + * First allocate more than half of the memory in the node as + * unmapped. Assuming the memory starts out mapped, this should + * exercise the unmap. + */ + page_majority =3D (node_present_pages(numa_node_id()) / 2) + 1; + ctx =3D do_many_alloc_pages(test, gfp, 0, page_majority); + + /* Check pages are unmapped */ + list_for_each_entry(page, &ctx->pages, lru) { + freetype_t ft =3D get_pfnblock_freetype(page, page_to_pfn(page)); + + /* + * Logically it should be an EXPECT, but that would + * cause heavy log spam on failure so use ASSERT for + * concision. + */ + KUNIT_ASSERT_FALSE(test, kernel_page_present(page)); + KUNIT_ASSERT_TRUE(test, freetype_flags(ft) & FREETYPE_UNMAPPED); + } + + /* + * Now free them again and allocate the same amount without + * __GFP_UNMAPPED. This will exercise the mapping logic. + */ + kunit_release_action(test, action_many__free_pages, ctx); + gfp &=3D ~__GFP_UNMAPPED; + ctx =3D do_many_alloc_pages(test, gfp, 0, page_majority); + + /* Check pages are mapped. */ + list_for_each_entry(page, &ctx->pages, lru) + KUNIT_ASSERT_TRUE(test, kernel_page_present(page)); +} + +#endif /* CONFIG_PAGE_ALLOC_UNMAPPED */ + +static void __test_pindex_helpers(struct kunit *test, unsigned long *bitma= p, + int mt, unsigned int ftflags, unsigned int order) +{ + freetype_t ft =3D migrate_to_freetype(mt, ftflags); + unsigned int pindex; + int got_order; + + if (!pcp_allowed_order(order)) + return; + + if (mt >=3D MIGRATE_PCPTYPES) + return; + + if (freetype_idx(ft) < 0) + return; + + pindex =3D order_to_pindex(ft, order); + + KUNIT_ASSERT_LT_MSG(test, pindex, NR_PCP_LISTS, + "invalid pindex %d (order %d mt %d flags %#x)", + pindex, order, mt, ftflags); + KUNIT_EXPECT_TRUE_MSG(test, test_bit(pindex, bitmap), + "pindex %d reused (order %d mt %d flags %#x)", + pindex, order, mt, ftflags); + + /* + * For THP, two migratetypes map to the same pindex, + * just manually exclude one of those cases. + */ + if (!(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && + order =3D=3D HPAGE_PMD_ORDER && + mt =3D=3D min(MIGRATE_UNMOVABLE, MIGRATE_RECLAIMABLE))) + clear_bit(pindex, bitmap); + + got_order =3D pindex_to_order(pindex); + KUNIT_EXPECT_EQ_MSG(test, order, got_order, + "roundtrip failed, got %d want %d (pindex %d mt %d flags %#x)", + got_order, order, pindex, mt, ftflags); +} + +/* This just checks for basic arithmetic errors. */ +static void test_pindex_helpers(struct kunit *test) +{ + unsigned long bitmap[bitmap_size(NR_PCP_LISTS)]; + + /* Bit means "pindex not yet used". */ + bitmap_fill(bitmap, NR_PCP_LISTS); + + for (unsigned int order =3D 0; order < NR_PAGE_ORDERS; order++) { + for (int mt =3D 0; mt < MIGRATE_TYPES; mt++) { + __test_pindex_helpers(test, bitmap, mt, 0, order); + if (FREETYPE_UNMAPPED) + __test_pindex_helpers(test, bitmap, mt, + FREETYPE_UNMAPPED, order); + } + } + + KUNIT_EXPECT_TRUE_MSG(test, bitmap_empty(bitmap, NR_PCP_LISTS), + "unused pindices: %*pbl", NR_PCP_LISTS, bitmap); +} + +static void __test_freetype_idx(struct kunit *test, unsigned int order, + int migratetype, unsigned int ftflags, + unsigned long *bitmap) +{ + freetype_t ft =3D migrate_to_freetype(migratetype, ftflags); + int idx =3D freetype_idx(ft); + + if (idx =3D=3D -1) + return; + KUNIT_ASSERT_GE(test, idx, 0); + KUNIT_ASSERT_LT(test, idx, NR_FREETYPE_IDXS); + + KUNIT_EXPECT_LT_MSG(test, idx, NR_PCP_LISTS, + "invalid idx %d (order %d mt %d flags %#x)", + idx, order, migratetype, ftflags); + clear_bit(idx, bitmap); +} + +static void test_freetype_idx(struct kunit *test) +{ + unsigned long bitmap[bitmap_size(NR_FREETYPE_IDXS)]; + + /* Bit means "pindex not yet used". */ + bitmap_fill(bitmap, NR_FREETYPE_IDXS); + + for (unsigned int order =3D 0; order < NR_PAGE_ORDERS; order++) { + for (int mt =3D 0; mt < MIGRATE_TYPES; mt++) { + __test_freetype_idx(test, order, mt, 0, bitmap); + if (FREETYPE_UNMAPPED) + __test_freetype_idx(test, order, mt, + FREETYPE_UNMAPPED, bitmap); + } + } + + KUNIT_EXPECT_TRUE_MSG(test, bitmap_empty(bitmap, NR_FREETYPE_IDXS), + "unused idxs: %*pbl", NR_PCP_LISTS, bitmap); +} + +static struct kunit_case test_cases[] =3D { +#ifdef CONFIG_PAGE_ALLOC_UNMAPPED + KUNIT_CASE_PARAM(test_alloc_map_unmap, gfp_gen_params), +#endif + KUNIT_CASE(test_pindex_helpers), + KUNIT_CASE(test_freetype_idx), + {} +}; + +static struct kunit_suite test_suite =3D { + .name =3D "page_alloc", + .test_cases =3D test_cases, +}; + +kunit_test_suite(test_suite); + +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); --=20 2.51.2