mm: KUnit tests for the page allocator
Subsystems (9)
cover mm: KUnit tests for the page allocator
The page allocator does a lot of stuff that is not visible to the user in any deterministic way. But this stuff is still important and it would be nice to test that behaviour.
KUnit is a tool for unit-testing kernel-internal APIs. This is an attempt to adopt it the page allocator.
I have been hacking on this as a way to try and test the code I'm writing for my ASI page_alloc integration proposal [0]. It's been extremely useful to be able to "just call it and see what it does". So I wanna gather some feedback on whether this basic idea is of interest before I invest too much more time in it.
You can run these tests like this:
tools/testing/kunit/kunit.py run \ --arch=x86_64 --kernel_args="movablecore=2G" \ --qemu_args="-m 4G" --kunitconfig mm/.kunitconfig
Unit-testing code that has mutable global variables can be a pain. Unit-testing code with mutable global variables _that can change concurrently with the tests_ is basically impossible. So, we need some way to isolate an "instance" of the allocator that doesn't refer to any such concurrently-mutated state.
Luckily, the allocator only has one really important global variable:node_data. So, the approach here is to carve out a subset of that variable which is as isolated as possible from the rest of rthe system, which can be used for deterministic testing. This is achieved by crating a fake "isolated" node at boot, and plugging in memory at test init time.
This is an RFC and not a PATCH because:
1. I have not taken much care to ensure the isolation is complete. There are probably sources of flakiness and nondeterminism in here.
2. I suspect the the basic idea might be over-complicated: do we really need memory hotplug here? Do we even need the instance of the allocator we're testing to actual memory behind the pages it's allocating, or could we just hallucinate a new region of vmemmap without any of that awkwardness?
One significant downside of relying on memory hotplug is that the test won't run if we can't hotplug anything out. That means you have to fiddle with the platform to even run the tests - see the --kernel_args and --qemu_args I had to add to my kunit.py command above.
So yeah, other suggestions welcome.
2b. I'm not very confident I'm using the hotplug API properly.
3. There's no point in merging this without actually having at least a few tests that are actually interesting!
Maybe a "build it and they will come" approach can be justified to some extent, but there's a nonzero cost to the infrastructure so we should probably have some confidence that they will indeed come.
[0] https://lore.kernel.org/linux-mm/[email protected]/
---
Brendan Jackman (4):
kunit: Allocate assertion data with GFP_ATOMIC mm/page_alloc_test: Add empty KUnit boilerplate mm/page_alloc_test: Add logic to isolate a node for testing mm/page_alloc_test: Add smoke-test for page allocation
drivers/base/memory.c | 5 +- include/linux/memory.h | 4 + include/linux/nodemask.h | 13 +++ kernel/kthread.c | 3 + lib/kunit/assert.c | 2 +- lib/kunit/resource.c | 2 +- lib/kunit/test.c | 2 +- mm/.kunitconfig | 10 ++ mm/Kconfig | 8 ++ mm/Makefile | 2 + mm/internal.h | 11 ++ mm/memory_hotplug.c | 26 +++-- mm/numa_memblks.c | 22 ++++ mm/page_alloc.c | 37 +++++- mm/page_alloc_test.c | 296 +++++++++++++++++++++++++++++++++++++++++++++++ 15 files changed, 429 insertions(+), 14 deletions(-)
---
base-commit: d082ecbc71e9e0bf49883ee4afd435a77a5101b6
change-id: 20250219-page-alloc-kunit-df76ef8d8eb9
Best regards,
Signature · 1 line
Brendan Jackman <[email protected]>
RFC PATCH 1/4 kunit: Allocate assertion data with GFP_ATOMIC
At present KUnit doesn't handle assertions happening in atomic contexts. A later commit will add tests that make assertions with spinlocks held.
In preparation, switch to GFP_ATOMIC.
"Just use GFP_ATOMIC" is not generally a solution to this kind of
problem: since it uses up memory reserves, instead it should be only used when truly needed.
However, for test code that should not be expected to run in production systems it seems tolerable, given that it avoids creating more complex APIs.
--- lib/kunit/assert.c | 2 +- lib/kunit/resource.c | 2 +- lib/kunit/test.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
lib/kunit/assert.c +1 -1
@@ -101,7 +101,7 @@ VISIBLE_IF_KUNIT bool is_literal(const char *text, long long value)if (strlen (text) != len)return false; - buffer =kmalloc (len+1 ,GFP_KERNEL ); + buffer =kmalloc (len+1 ,GFP_ATOMIC );if (!buffer)return false;
lib/kunit/resource.c +1 -1
@@ -98,7 +98,7 @@ int kunit_add_action(struct kunit *test, void (*action)(void *), void *ctx)KUNIT_ASSERT_NOT_NULL_MSG (test, action,"Tried to action a NULL function!" ); - action_ctx =kzalloc (sizeof (*action_ctx),GFP_KERNEL ); + action_ctx =kzalloc (sizeof (*action_ctx),GFP_ATOMIC );if (!action_ctx)return -ENOMEM ;
lib/kunit/test.c +1 -1
@@ -279,7 +279,7 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,kunit_set_failure (test); - stream =kunit_alloc_string_stream (test,GFP_KERNEL ); + stream =kunit_alloc_string_stream (test,GFP_ATOMIC );if (IS_ERR (stream)) { WARN(true, "Could not allocate stream to print failed assertion in %s:%d\n",
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
RFC PATCH 2/4 mm/page_alloc_test: Add empty KUnit boilerplate
In reply to Brendan JackmanAdd the Kbuild plumbing to create a new KUnit suite. Create the suite, with no tests inside it.
--- mm/.kunitconfig | 2 ++ mm/Kconfig | 8 ++++++++ mm/Makefile | 2 ++ mm/page_alloc_test.c | 21 +++++++++++++++++++++ 4 files changed, 33 insertions(+)
mm/.kunitconfig new file +2 -0
@@ -0,0 +1,2 @@ +CONFIG_KUNIT=y +CONFIG_PAGE_ALLOC_KUNIT_TEST=y
mm/Kconfig +8 -0
@@ -1358,6 +1358,14 @@ config PT_RECLAIM Note: now only empty user PTE page table pages will be reclaimed. +config PAGE_ALLOC_KUNIT_TEST + tristate "KUnit test for page allocator" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Builds unit tests for page allocator. + + If unsure, say N. source "mm/damon/Kconfig"
mm/Makefile +2 -0
@@ -61,6 +61,8 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ page-alloc-y := page_alloc.o page-alloc-$(CONFIG_SHUFFLE_PAGE_ALLOCATOR) += shuffle.o +obj-$(CONFIG_PAGE_ALLOC_KUNIT_TEST) += page_alloc_test.o + # Give 'memory_hotplug' its own module-parameter namespace memory-hotplug-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o
mm/page_alloc_test.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/errname.h> +#include <linux/list.h> +#include <linux/gfp.h> +#include <linux/memory.h> +#include <linux/nodemask.h> +#include <linux/percpu.h> +#include <linux/smp.h> + +#include <kunit/test.h> + +static struct kunit_case test_cases[] = { {} }; + +static struct kunit_suite test_suite = { + .name ="page_alloc" , + .test_cases = test_cases, +}; +kunit_test_suite (test_suite); + +MODULE_LICENSE ("GPL" ); +MODULE_IMPORT_NS ("EXPORTED_FOR_KUNIT_TESTING" );
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
RFC PATCH 3/4 mm/page_alloc_test: Add logic to isolate a node for testing
In reply to Brendan JackmanIn order to test the page allocator, we need an "instance" of the page allocator that is not subject to unpredictable perturbation by the live system. The closest thing that we have to an "instance" of the allocator is a NUMA node.
So, introduce a new concept of an "isolated" node. This is an extension of the existing concept of a "fake" node, with the addition that nothing else in the system will touch it unless instructed to by the test code.
The node is created during boot but has no memory nor any CPUs attached. It is not on any other node's fallback lists. Any code that pays general attention to NODE_DATA in such a way that might cause the page allocator data structures to be modified asynchronously to the test, is enlightened to ignore it via the node_isolated() helper.
Then, during initialization of the allocator test suite, hotplug out some memory and then plug it back in to the isolated node. The node can then be used for testing.
Because it's easy to miss code that needs enlightenment, which can lead to confusing test behaviour, also add some defensive checks to try and interference with the isolated node before the start of the test.
--- drivers/base/memory.c | 5 +- include/linux/memory.h | 4 ++ include/linux/nodemask.h | 13 +++++ kernel/kthread.c | 3 + mm/.kunitconfig | 10 +++- mm/Kconfig | 2 +- mm/internal.h | 11 ++++ mm/memory_hotplug.c | 26 ++++++--- mm/numa_memblks.c | 22 ++++++++ mm/page_alloc.c | 37 +++++++++++- mm/page_alloc_test.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++- 11 files changed, 260 insertions(+), 15 deletions(-)
drivers/base/memory.c +4 -1
@@ -26,6 +26,8 @@#include <linux/atomic.h> #include <linux/uaccess.h> +#include <kunit/visibility.h> +#define MEMORY_CLASS_NAME "memory"static const char *const online_type_to_str[] = {
@@ -183,7 +185,7 @@ static inline unsigned long memblk_nr_poison(struct memory_block *mem)/* * Must acquire mem_hotplug_lock in write mode. */ -static int memory_block_online (struct memory_block *mem) +VISIBLE_IF_KUNIT int memory_block_online(struct memory_block *mem) {unsigned long start_pfn =section_nr_to_pfn (mem->start_section_nr);unsigned long nr_pages =PAGES_PER_SECTION * sections_per_block;
@@ -250,6 +252,7 @@ static int memory_block_online(struct memory_block *mem)mem_hotplug_done ();return ret; } +EXPORT_SYMBOL_IF_KUNIT (memory_block_online);/* * Must acquire mem_hotplug_lock in write mode.
include/linux/memory.h +4 -0
@@ -177,6 +177,10 @@ int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, register_memory_notifier(&fn##_mem_nb); \ }) +#ifdef CONFIG_KUNIT +int memory_block_online (struct memory_block *mem); +#endif +#ifdef CONFIG_NUMA void memory_block_add_nid (struct memory_block *mem,int nid,enum meminit_context context);
include/linux/nodemask.h +13 -0
@@ -536,6 +536,19 @@ static __always_inline int node_random(const nodemask_t *maskp)#define for_each_node (node) for_each_node_state(node, N_POSSIBLE)#define for_each_online_node (node) for_each_node_state(node, N_ONLINE) + +#ifdef CONFIG_PAGE_ALLOC_KUNIT_TEST +/* +* An isolated node is a fake node for testing, that boots with no memory and no +* attached CPUs, and nothing should touch it except for test code. +*/ +extern bool node_isolated (int node); +/* Only one isolated node is supported at present and it cannot be un-isolated. */ +extern void node_set_isolated (int node); +#else +static inline bool node_isolated (int node) {return false; } +#endif /* CONFIG_PAGE_ALLOC_KUNIT_TEST */ +/* * For nodemask scratch area. * NODEMASK_ALLOC(type, name) allocates an object with a specified type and
kernel/kthread.c +3 -0
@@ -9,6 +9,7 @@*/ #include <uapi/linux/sched/types.h> #include <linux/mm.h> +#include <linux/mmdebug.h> #include <linux/mmu_context.h> #include <linux/sched.h> #include <linux/sched/mm.h>
@@ -511,6 +512,8 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),struct kthread_create_info *create =kmalloc (sizeof (*create),GFP_KERNEL ); +VM_WARN_ON (node !=NUMA_NO_NODE &&node_isolated (node)); +if (!create)return ERR_PTR (-ENOMEM ); create->threadfn = threadfn;
mm/.kunitconfig +9 -1
@@ -1,2 +1,10 @@ CONFIG_KUNIT=y -CONFIG_PAGE_ALLOC_KUNIT_TEST=y +CONFIG_PAGE_ALLOC_KUNIT_TEST=y + +# Required for NUMA +CONFIG_SMP=y +# Used by tests to carve out fake node for isolating page_alloc data. +CONFIG_NUMA=y +CONFIG_NUMA_EMU=y +CONFIG_MEMORY_HOTPLUG=y +CONFIG_MEMORY_HOTREMOVE=y
mm/Kconfig +1 -1
@@ -1360,7 +1360,7 @@ config PT_RECLAIM config PAGE_ALLOC_KUNIT_TEST tristate "KUnit test for page allocator" if !KUNIT_ALL_TESTS - depends on KUNIT + depends on KUNIT && NUMA && MEMORY_HOTREMOVE default KUNIT_ALL_TESTS help Builds unit tests for page allocator.
mm/internal.h +11 -0
@@ -1545,5 +1545,16 @@ static inline bool reclaim_pt_is_enabled(unsigned long start, unsigned long end, }#endif /* CONFIG_PT_RECLAIM */ +#ifdef CONFIG_PAGE_ALLOC_KUNIT_TEST +/* +* Note that node_isolated() is separate, that's a "public API". But only +* test code needs to look up which node is isolated. +*/ +extern int isolated_node; +#endif + +#ifdef CONFIG_KUNIT +void drain_pages (unsigned int cpu); +#endif #endif /* __MM_INTERNAL_H */
mm/memory_hotplug.c +18 -8
@@ -1198,10 +1198,12 @@ int online_pages(unsigned long pfn, unsigned long nr_pages, arg.nr_pages = nr_pages;node_states_check_changes_online (nr_pages, zone, &arg); - ret =memory_notify (MEM_GOING_ONLINE , &arg); - ret =notifier_to_errno (ret); -if (ret) - goto failed_addition; +if (!node_isolated (nid)) { + ret =memory_notify (MEM_GOING_ONLINE , &arg); + ret =notifier_to_errno (ret); +if (ret) + goto failed_addition; + }/* * Fixup the number of isolated pageblocks before marking the sections
@@ -1242,19 +1244,27 @@ int online_pages(unsigned long pfn, unsigned long nr_pages,/* reinitialise watermarks and update pcp limits */ init_per_zone_wmark_min (); -kswapd_run (nid); -kcompactd_run (nid); +/* +* Don't run daemons on the special test node, if that needs to be +* tested the test should run it. +*/ +if (!node_isolated (nid)) { +kswapd_run (nid); +kcompactd_run (nid); + }writeback_set_ratelimit (); -memory_notify (MEM_ONLINE , &arg); +if (!node_isolated (nid)) +memory_notify (MEM_ONLINE , &arg);return 0 ; failed_addition:pr_debug ("online_pages [mem %#010llx-%#010llx] failed\n" , (unsigned long long ) pfn <<PAGE_SHIFT , (((unsigned long long ) pfn + nr_pages) <<PAGE_SHIFT ) -1 ); -memory_notify (MEM_CANCEL_ONLINE , &arg); +if (!node_isolated (nid)) +memory_notify (MEM_CANCEL_ONLINE , &arg);remove_pfn_range_from_zone (zone, pfn, nr_pages);return ret; }
mm/numa_memblks.c +22 -0
@@ -7,6 +7,8 @@#include <linux/numa.h> #include <linux/numa_memblks.h> +#include "internal.h" +int numa_distance_cnt;static u8 *numa_distance;
@@ -371,6 +373,24 @@ static void __init numa_clear_kernel_node_hotplug(void) } } +#ifdef CONFIG_PAGE_ALLOC_KUNIT_TEST +static inline void make_isolated_node (void ) +{ +int node; + + node =num_possible_nodes (); +if (!numa_valid_node (node)) { +pr_err ("All node IDs used, can't fake another.\n" ); + }else { +node_set (num_possible_nodes (), node_possible_map); +node_set_isolated (node); + } +} +#else +static inline void make_isolated_node (void ) { } +#endif + +static int __init numa_register_meminfo (struct numa_meminfo *mi) {int i;
@@ -381,6 +401,8 @@ static int __init numa_register_meminfo(struct numa_meminfo *mi)if (WARN_ON (nodes_empty (node_possible_map)))return -EINVAL ; +make_isolated_node (); +for (i =0 ; i < mi->nr_blks; i++) {struct numa_memblk *mb = &mi->blk[i];
mm/page_alloc.c +35 -2
@@ -56,6 +56,7 @@#include <linux/cacheinfo.h> #include <linux/pgalloc_tag.h> #include <asm/div64.h> +#include <kunit/visibility.h> #include "internal.h" #include "shuffle.h" #include "page_reporting.h"
@@ -291,6 +292,26 @@ static bool __free_unaccepted(struct page *page);int page_group_by_mobility_disabled__read_mostly ; +/* +* Test harness for KUnit - pick a node that we will never allocate from, except +* for in the page allocator tests. +*/ +#ifdef CONFIG_PAGE_ALLOC_KUNIT_TEST +int isolated_node =NUMA_NO_NODE ; +EXPORT_SYMBOL (isolated_node); + +void node_set_isolated (int node) +{ +WARN_ON (isolated_node !=NUMA_NO_NODE ); + isolated_node = node; +} + +bool node_isolated (int node) +{ +return node == isolated_node; +} +#endif /* CONFIG_POAGE_ALLOC_KUNIT_TEST */ +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT /* * During boot we initialize deferred pages on-demand, as needed, but once
@@ -2410,7 +2431,7 @@ static void drain_pages_zone(unsigned int cpu, struct zone *zone)/* * Drain pcplists of all zones on the indicated processor. */ -static void drain_pages (unsigned int cpu) +VISIBLE_IF_KUNIT void drain_pages(unsigned int cpu) {struct zone *zone;
@@ -2418,6 +2439,7 @@ static void drain_pages(unsigned int cpu)drain_pages_zone (cpu, zone); } } +EXPORT_SYMBOL_IF_KUNIT (drain_pages);/* * Spill all of this CPU's per-cpu pages back into the buddy allocator.
@@ -5087,6 +5109,8 @@ int find_next_best_node(int node, nodemask_t *used_node_mask) }for_each_node_state (n,N_MEMORY ) { +if (node_isolated (n)) +continue ;/* Don't want a node to appear more than once */ if (node_isset (n, *used_node_mask))
@@ -5134,8 +5158,17 @@ static void build_zonelists_in_node_order(pg_data_t *pgdat, int *node_order,for (i =0 ; i < nr_nodes; i++) {int nr_zones; +int other_nid = node_order[i]; +pg_data_t *node =NODE_DATA (other_nid); -pg_data_t *node =NODE_DATA (node_order[i]); +/* +* Never fall back to the isolated node. The isolated node has +* to be able to fall back to other nodes because that fallback +* is relied on for allocating data structures that describe the +* node. +*/ +if (node_isolated (other_nid) && other_nid != pgdat->node_id) +continue ; nr_zones =build_zonerefs_node (node, zonerefs); zonerefs += nr_zones;
mm/page_alloc_test.c +140 -2
159 lines not on this page · raw message
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Re: [PATCH RFC 3/4] mm/page_alloc_test: Add logic to isolate a node for testing
Quoted 73 lines · Brendan Jackman
In order to test the page allocator, we need an "instance" of the page allocator that is not subject to unpredictable perturbation by the live system. The closest thing that we have to an "instance" of the allocator is a NUMA node.
So, introduce a new concept of an "isolated" node. This is an extension of the existing concept of a "fake" node, with the addition that nothing else in the system will touch it unless instructed to by the test code.
The node is created during boot but has no memory nor any CPUs attached. It is not on any other node's fallback lists. Any code that pays general attention to
NODE_DATAin such a way that might cause the page allocator data structures to be modified asynchronously to the test, is enlightened to ignore it via thenode_isolated()helper.Then, during initialization of the allocator test suite, hotplug out some memory and then plug it back in to the isolated node. The node can then be used for testing.
Because it's easy to miss code that needs enlightenment, which can lead to confusing test behaviour, also add some defensive checks to try and interference with the isolated node before the start of the test.
Signed-off-by Brendan Jackman <[email protected]>--- drivers/base/memory.c | 5 +- include/linux/memory.h | 4 ++ include/linux/nodemask.h | 13 +++++ kernel/kthread.c | 3 + mm/.kunitconfig | 10 +++- mm/Kconfig | 2 +- mm/internal.h | 11 ++++ mm/memory_hotplug.c | 26 ++++++--- mm/numa_memblks.c | 22 ++++++++ mm/page_alloc.c | 37 +++++++++++- mm/page_alloc_test.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++- 11 files changed, 260 insertions(+), 15 deletions(-)drivers/base/memory.c +4
-1@@ -26,6 +26,8 @@#include <linux/atomic.h> #include <linux/uaccess.h> +#include <kunit/visibility.h> +#define MEMORY_CLASS_NAME "memory"static const char *const online_type_to_str[] = {@@ -183,7 +185,7 @@ static inline unsigned long memblk_nr_poison(struct memory_block *mem)/* * Must acquire mem_hotplug_lock in write mode. */ -static int memory_block_online (struct memory_block *mem) +VISIBLE_IF_KUNIT int memory_block_online(struct memory_block *mem) {unsigned long start_pfn =section_nr_to_pfn (mem->start_section_nr);unsigned long nr_pages =PAGES_PER_SECTION * sections_per_block;@@ -250,6 +252,7 @@ static int memory_block_online(struct memory_block *mem)mem_hotplug_done ();return ret; } +EXPORT_SYMBOL_IF_KUNIT (memory_block_online);/* * Must acquire mem_hotplug_lock in write mode. include/linux/memory.h +0
-0
include/linux/memory.h int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, · line 180
Quoted 4 lines
include/linux/memory.h +1
-0@@ -177,6 +177,10 @@ int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func, register_memory_notifier(&fn##_mem_nb); \ }) +#ifdef CONFIG_KUNIT
Why not CONFIG_PAGE_ALLOC_KUNIT_TEST?
Quoted 89 lines
include/linux/memory.h +3
-0+int memory_block_online (struct memory_block *mem); +#endif +#ifdef CONFIG_NUMA void memory_block_add_nid (struct memory_block *mem,int nid,enum meminit_context context);include/linux/nodemask.h +13
-0@@ -536,6 +536,19 @@ static __always_inline int node_random(const nodemask_t *maskp)#define for_each_node (node) for_each_node_state(node, N_POSSIBLE)#define for_each_online_node (node) for_each_node_state(node, N_ONLINE) + +#ifdef CONFIG_PAGE_ALLOC_KUNIT_TEST +/* +* An isolated node is a fake node for testing, that boots with no memory and no +* attached CPUs, and nothing should touch it except for test code. +*/ +extern bool node_isolated (int node); +/* Only one isolated node is supported at present and it cannot be un-isolated. */ +extern void node_set_isolated (int node); +#else +static inline bool node_isolated (int node) {return false; } +#endif /* CONFIG_PAGE_ALLOC_KUNIT_TEST */ +/* * For nodemask scratch area. * NODEMASK_ALLOC(type, name) allocates an object with a specified type and kernel/kthread.c +3
-0@@ -9,6 +9,7 @@*/ #include <uapi/linux/sched/types.h> #include <linux/mm.h> +#include <linux/mmdebug.h> #include <linux/mmu_context.h> #include <linux/sched.h> #include <linux/sched/mm.h> @@ -511,6 +512,8 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data),struct kthread_create_info *create =kmalloc (sizeof (*create),GFP_KERNEL ); +VM_WARN_ON (node !=NUMA_NO_NODE &&node_isolated (node)); +if (!create)return ERR_PTR (-ENOMEM ); create->threadfn = threadfn;mm/.kunitconfig +9
-1@@ -1,2 +1,10 @@ CONFIG_KUNIT=y -CONFIG_PAGE_ALLOC_KUNIT_TEST=y +CONFIG_PAGE_ALLOC_KUNIT_TEST=y + +# Required for NUMA +CONFIG_SMP=y +# Used by tests to carve out fake node for isolating page_alloc data. +CONFIG_NUMA=y +CONFIG_NUMA_EMU=y +CONFIG_MEMORY_HOTPLUG=y +CONFIG_MEMORY_HOTREMOVE=ymm/Kconfig +1
-1@@ -1360,7 +1360,7 @@ config PT_RECLAIM config PAGE_ALLOC_KUNIT_TEST tristate "KUnit test for page allocator" if !KUNIT_ALL_TESTS - depends on KUNIT + depends on KUNIT && NUMA && MEMORY_HOTREMOVE default KUNIT_ALL_TESTS help Builds unit tests for page allocator.mm/internal.h +0
-0@@ -1545,5 +1545,16 @@ static inline bool reclaim_pt_is_enabled(unsigned long start, unsigned long end, }#endif /* CONFIG_PT_RECLAIM */
mm/internal.h static inline bool reclaim_pt_is_enabled(unsigned long start, unsigned long end, · line 1556
Quoted 10 lines
mm/internal.h +9
-0+#ifdef CONFIG_PAGE_ALLOC_KUNIT_TEST +/* +* Note that node_isolated() is separate, that's a "public API". But only +* test code needs to look up which node is isolated. +*/ +extern int isolated_node; +#endif + +#ifdef CONFIG_KUNIT
Same here, why not just put it in the above #ifdef?
Quoted 35 lines
mm/internal.h +2
-0+void drain_pages (unsigned int cpu); +#endif #endif /* __MM_INTERNAL_H */ mm/memory_hotplug.c +10
-6@@ -1198,10 +1198,12 @@ int online_pages(unsigned long pfn, unsigned long nr_pages, arg.nr_pages = nr_pages;node_states_check_changes_online (nr_pages, zone, &arg); - ret =memory_notify (MEM_GOING_ONLINE , &arg); - ret =notifier_to_errno (ret); -if (ret) - goto failed_addition; +if (!node_isolated (nid)) { + ret =memory_notify (MEM_GOING_ONLINE , &arg); + ret =notifier_to_errno (ret); +if (ret) + goto failed_addition; + }/* * Fixup the number of isolated pageblocks before marking the sections @@ -1242,19 +1244,27 @@ int online_pages(unsigned long pfn, unsigned long nr_pages,/* reinitialise watermarks and update pcp limits */ init_per_zone_wmark_min (); -kswapd_run (nid); -kcompactd_run (nid); +/* +* Don't run daemons on the special test node, if that needs to be +* tested the test should run it. +*/
mm/memory_hotplug.c int online_pages(unsigned long pfn, unsigned long nr_pages, · line 1259
Quoted 10 lines
mm/memory_hotplug.c +6
-1+if (!node_isolated (nid)) { +kswapd_run (nid); +kcompactd_run (nid); + }writeback_set_ratelimit (); -memory_notify (MEM_ONLINE , &arg); +if (!node_isolated (nid)) +memory_notify (MEM_ONLINE , &arg);
I am not familiar with this code, I am wondering if we can move things around to have a single block of things we skip for isolated nodes. It depends on ordering dependencies so we need someone who knows this code to tell us.
Quoted 12 lines
mm/memory_hotplug.c +2
-1return 0 ; failed_addition:pr_debug ("online_pages [mem %#010llx-%#010llx] failed\n" , (unsigned long long ) pfn <<PAGE_SHIFT , (((unsigned long long ) pfn + nr_pages) <<PAGE_SHIFT ) -1 ); -memory_notify (MEM_CANCEL_ONLINE , &arg); +if (!node_isolated (nid)) +memory_notify (MEM_CANCEL_ONLINE , &arg);remove_pfn_range_from_zone (zone, pfn, nr_pages);return ret; }
[..]
mm/memory_hotplug.c
Quoted 2 lines
mm/memory_hotplug.c +1
-1-static struct kunit_suite test_suite = { +struct kunit_suite page_alloc_test_suite = {
We should probably just intrdouce the suite as page_alloc_test_suite to begin with?
Quoted 15 lines
mm/memory_hotplug.c +4
-1.name = "page_alloc", .test_cases = test_cases, + .suite_init = populate_isolated_node, + .suite_exit = depopulate_isolated_node, + .init = test_init, }; -kunit_test_suite (test_suite); +kunit_test_suite (page_alloc_test_suite);MODULE_LICENSE ("GPL" );MODULE_IMPORT_NS ("EXPORTED_FOR_KUNIT_TESTING" );Signature · 2 lines
-- 2.48.1.601.g30ceb7b040-goog
Re: [PATCH RFC 3/4] mm/page_alloc_test: Add logic to isolate a node for testing
Quoted 3 lines · Yosry Ahmed
Quoted 1 line
+#ifdef
CONFIG_KUNITWhy not
CONFIG_PAGE_ALLOC_KUNIT_TEST?
VISIBLE_IF_KUNIT is paired with #ifdef CONFIG_KUNIT elsewhere (I think there might even be docs that do this in an example) so I just followed the pattern.
#ifdef CONFIG_KUNIT -> things are consistent and you just don't have to think about this very much.
#ifdef CONFIG_PAGE_ALLOC_KUNIT_TEST -> better scoping.
So yeah, shrug. Maybe David/Rae/Brendan has an opinion.
Re: [PATCH RFC 3/4] mm/page_alloc_test: Add logic to isolate a node for testing
Quoted 10 lines · Brendan Jackman
Quoted 3 lines · Yosry Ahmed
Quoted 1 line
+#ifdef
CONFIG_KUNITWhy not
CONFIG_PAGE_ALLOC_KUNIT_TEST?
VISIBLE_IF_KUNITis paired with #ifdefCONFIG_KUNITelsewhere (I think there might even be docs that do this in an example) so I just followed the pattern.
Quoted 6 lines
#ifdef
CONFIG_KUNIT-> things are consistent and you just don't have to think about this very much.#ifdef
CONFIG_PAGE_ALLOC_KUNIT_TEST-> better scoping.So yeah, shrug. Maybe David/Rae/Brendan has an opinion.
Oh, actually I rescind my shrug. If we used #ifdef CONFIG_PAGE_ALLOC_KUNIT_TEST, then -Wmissing-prototypes would fire for people running other KUnit tests.
So yeah the function needs to be non-static exactly when there's a prototype in the header.
RFC PATCH 4/4 mm/page_alloc_test: Add smoke-test for page allocation
In reply to Brendan JackmanThis is the bare minimum to illustrate what KUnit code would look like that covers the page allocator.
Even this trivial test illustrates a couple of nice things that are possible when testing via KUnit
1. We can directly assert that the correct zone was used. (Although note due to the simplistic setup, you can have any zone you like as long as it's ZONE_NORMAL).
2. We can assert that a page got freed. It's probably pretty unlikely that we'd have a bug that actually causes a page to get leaked by the allocator, but it serves as a good example of the kind of assertions we can make by judicously peeking at allocator internals.
--- mm/page_alloc_test.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 138 insertions(+), 1 deletion(-)
mm/page_alloc_test.c +138 -1
@@ -26,6 +26,139 @@ } \ }) +#define EXPECT_WITHIN_ZONE (test, page, zone) ({ \ + unsigned long pfn = page_to_pfn(page); \ + unsigned long start_pfn = zone->zone_start_pfn; \ + unsigned long end_pfn = start_pfn + zone->spanned_pages; \ + \ + KUNIT_EXPECT_TRUE_MSG(test, \ + pfn >= start_pfn && pfn < end_pfn, \ + "Wanted PFN 0x%lx - 0x%lx, got 0x%lx", \ + start_pfn, end_pfn, pfn); \ + KUNIT_EXPECT_PTR_EQ_MSG(test, page_zone(page), zone, \ + "Wanted %px (%s), got %px (%s)", \ + zone, zone->name, page_zone(page), page_zone(page)->name); \ +}) + +static void action_nodemask_free (void *ctx) +{ +NODEMASK_FREE (ctx); +} + +/* +* Call __alloc_pages_noprof with a nodemask containing only the nid. +* +* Never returns NULL. +*/ +static inline struct page *alloc_pages_force_nid (struct kunit *test, +gfp_t gfp,int order,int nid) +{ +NODEMASK_ALLOC (nodemask_t, nodemask,GFP_KERNEL ); +struct page *page; + +KUNIT_ASSERT_NOT_NULL (test, nodemask); +kunit_add_action (test, action_nodemask_free, &nodemask); +nodes_clear (*nodemask); +node_set (nid, *nodemask); + + page =__alloc_pages_noprof (GFP_KERNEL ,0 , nid, nodemask); +KUNIT_ASSERT_NOT_NULL (test, page); +return page; +} + +static inline bool page_on_buddy_list (struct page *want_page,struct list_head *head) +{ +struct page *found_page; + +list_for_each_entry (found_page, head, buddy_list) { +if (found_page == want_page) +return true; + } + +return false; +} + +/* Test case parameters that are independent of alloc order. */ +static const struct { +gfp_t gfp_flags; +enum zone_type want_zone; +} alloc_fresh_gfps[] = { +/* +* The way we currently set up the isolated node, everything ends up in +* ZONE_NORMAL. +*/ + { .gfp_flags =GFP_KERNEL , .want_zone =ZONE_NORMAL }, + { .gfp_flags =GFP_ATOMIC , .want_zone =ZONE_NORMAL }, + { .gfp_flags =GFP_USER , .want_zone =ZONE_NORMAL }, + { .gfp_flags =GFP_DMA32 , .want_zone =ZONE_NORMAL }, +}; + +struct alloc_fresh_test_case { +int order; +int gfp_idx; +}; + +/* Generate test cases as the cross product of orders and alloc_fresh_gfps. */ +static const void *alloc_fresh_gen_params (const void *prev,char *desc) +{ +/* Buffer to avoid allocations. */ +static struct alloc_fresh_test_case tc; + +if (!prev) { +/* First call */ + tc.order =0 ; + tc.gfp_idx =0 ; +return &tc; + } + + tc.gfp_idx++; +if (tc.gfp_idx >=ARRAY_SIZE (alloc_fresh_gfps)) { + tc.gfp_idx =0 ; + tc.order++; + } +if (tc.order >MAX_PAGE_ORDER ) +/* Finished. */ +return NULL ; + +snprintf (desc,KUNIT_PARAM_DESC_SIZE ,"order %d %pGg\n" , + tc.order, &alloc_fresh_gfps[tc.gfp_idx].gfp_flags); +return &tc; +} + +/* Smoke test: allocate from a node where everything is in a pristine state. */ +static void test_alloc_fresh (struct kunit *test) +{ +const struct alloc_fresh_test_case *tc = test->param_value; +gfp_t gfp_flags = alloc_fresh_gfps[tc->gfp_idx].gfp_flags; +enum zone_type want_zone_type = alloc_fresh_gfps[tc->gfp_idx].want_zone; +struct zone *want_zone = &NODE_DATA (isolated_node)->node_zones[want_zone_type]; +struct list_head *buddy_list; +struct per_cpu_pages *pcp; +struct page *page, *merged_page; +int cpu; + + page =alloc_pages_force_nid (test, gfp_flags, tc->order, isolated_node); + +EXPECT_WITHIN_ZONE (test, page, want_zone); + + cpu =get_cpu (); +__free_pages (page,0 ); + pcp =per_cpu_ptr (want_zone->per_cpu_pageset, cpu); +put_cpu (); + +/* +* Should end up back in the free area when drained. Because everything +* is free, it should get buddy-merged up to the maximum order. +*/ +drain_zone_pages (want_zone, pcp); +KUNIT_EXPECT_TRUE (test,PageBuddy (page)); +KUNIT_EXPECT_EQ (test,buddy_order (page),MAX_PAGE_ORDER ); +KUNIT_EXPECT_TRUE (test,list_empty (&pcp->lists[MIGRATE_UNMOVABLE ])); + merged_page =pfn_to_page (round_down (page_to_pfn (page),1 <<MAX_PAGE_ORDER )); + buddy_list = &want_zone->free_area[MAX_PAGE_ORDER ].free_list[MIGRATE_UNMOVABLE ]; +KUNIT_EXPECT_TRUE (test,page_on_buddy_list (merged_page, buddy_list)); +} +static void action_drain_pages_all (void *unused) {int cpu;
@@ -144,7 +277,11 @@ static void depopulate_isolated_node(struct kunit_suite *suite)WARN_ON (add_memory (0 , start, size,MMOP_ONLINE ));WARN_ON (walk_memory_blocks (start, size,NULL , memory_block_online_cb)); } -static struct kunit_case test_cases[] = { {} }; + +static struct kunit_case test_cases[] = { +KUNIT_CASE_PARAM (test_alloc_fresh, alloc_fresh_gen_params), + {} +};struct kunit_suite page_alloc_test_suite = { .name ="page_alloc" ,
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
Re: [PATCH RFC 4/4] mm/page_alloc_test: Add smoke-test for page allocation
Quoted 25 lines · Brendan Jackman
This is the bare minimum to illustrate what KUnit code would look like that covers the page allocator.
Even this trivial test illustrates a couple of nice things that are possible when testing via KUnit
1. We can directly assert that the correct zone was used. (Although note due to the simplistic setup, you can have any zone you like as long as it's
ZONE_NORMAL).2. We can assert that a page got freed. It's probably pretty unlikely that we'd have a bug that actually causes a page to get leaked by the allocator, but it serves as a good example of the kind of assertions we can make by judicously peeking at allocator internals.
Signed-off-by Brendan Jackman <[email protected]>--- mm/page_alloc_test.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 138 insertions(+), 1 deletion(-)mm/page_alloc_test.c +0
-0
mm/page_alloc_test.c line 29
Quoted 4 lines
mm/page_alloc_test.c +1
-0@@ -26,6 +26,139 @@ } \ }) +#define EXPECT_WITHIN_ZONE (test, page, zone) ({ \
ultranit: the '\' above is unaligned with the ones below.
Quoted 17 lines
mm/page_alloc_test.c +17
-0+unsigned long pfn =page_to_pfn (page); \ +unsigned long start_pfn = zone->zone_start_pfn; \ +unsigned long end_pfn = start_pfn + zone->spanned_pages; \ + \ +KUNIT_EXPECT_TRUE_MSG (test, \ + pfn >= start_pfn && pfn < end_pfn, \ +"Wanted PFN 0x%lx - 0x%lx, got 0x%lx" , \ + start_pfn, end_pfn, pfn); \ +KUNIT_EXPECT_PTR_EQ_MSG (test,page_zone (page), zone, \ +"Wanted %px (%s), got %px (%s)" , \ + zone, zone->name,page_zone (page),page_zone (page)->name); \ +}) + +static void action_nodemask_free (void *ctx) +{ +NODEMASK_FREE (ctx); +}
mm/page_alloc_test.c line 56
Quoted 10 lines
mm/page_alloc_test.c +10
-0+ +/* +* Call __alloc_pages_noprof with a nodemask containing only the nid. +* +* Never returns NULL. +*/ +static inline struct page *alloc_pages_force_nid (struct kunit *test, +gfp_t gfp,int order,int nid) +{ +NODEMASK_ALLOC (nodemask_t, nodemask,GFP_KERNEL );
For the sake of the test can't we just put the nodemask on the stack?
mm/page_alloc_test.c line 60
Quoted 4 lines
mm/page_alloc_test.c +4
-0+struct page *page; + +KUNIT_ASSERT_NOT_NULL (test, nodemask); +kunit_add_action (test, action_nodemask_free, &nodemask);
Why aren't we just freeing the nodemask after using it, before we make any assertions?
mm/page_alloc_test.c line 71
Quoted 11 lines
mm/page_alloc_test.c +11
-0+nodes_clear (*nodemask); +node_set (nid, *nodemask); + + page =__alloc_pages_noprof (GFP_KERNEL ,0 , nid, nodemask); +KUNIT_ASSERT_NOT_NULL (test, page); +return page; +} + +static inline bool page_on_buddy_list (struct page *want_page,struct list_head *head) +{ +struct page *found_page;
nit: This is just an iterator, we can probably just call it 'page'. 'found' is confusing for me because we didn't really search for it.
Quoted 31 lines
mm/page_alloc_test.c +31
-0+ +list_for_each_entry (found_page, head, buddy_list) { +if (found_page == want_page) +return true; + } + +return false; +} + +/* Test case parameters that are independent of alloc order. */ +static const struct { +gfp_t gfp_flags; +enum zone_type want_zone; +} alloc_fresh_gfps[] = { +/* +* The way we currently set up the isolated node, everything ends up in +* ZONE_NORMAL. +*/ + { .gfp_flags =GFP_KERNEL , .want_zone =ZONE_NORMAL }, + { .gfp_flags =GFP_ATOMIC , .want_zone =ZONE_NORMAL }, + { .gfp_flags =GFP_USER , .want_zone =ZONE_NORMAL }, + { .gfp_flags =GFP_DMA32 , .want_zone =ZONE_NORMAL }, +}; + +struct alloc_fresh_test_case { +int order; +int gfp_idx; +}; + +/* Generate test cases as the cross product of orders and alloc_fresh_gfps. */ +static const void *alloc_fresh_gen_params (const void *prev,char *desc)
mm/page_alloc_test.c line 112
Quoted 10 lines
mm/page_alloc_test.c +10
-0+{ +/* Buffer to avoid allocations. */ +static struct alloc_fresh_test_case tc; + +if (!prev) { +/* First call */ + tc.order =0 ; + tc.gfp_idx =0 ; +return &tc; + }
We need to set 'tc' here to whatever 'prev' is pointing at, right?
Quoted 69 lines
mm/page_alloc_test.c +54
-1+ + tc.gfp_idx++; +if (tc.gfp_idx >=ARRAY_SIZE (alloc_fresh_gfps)) { + tc.gfp_idx =0 ; + tc.order++; + } +if (tc.order >MAX_PAGE_ORDER ) +/* Finished. */ +return NULL ; + +snprintf (desc,KUNIT_PARAM_DESC_SIZE ,"order %d %pGg\n" , + tc.order, &alloc_fresh_gfps[tc.gfp_idx].gfp_flags); +return &tc; +} + +/* Smoke test: allocate from a node where everything is in a pristine state. */ +static void test_alloc_fresh (struct kunit *test) +{ +const struct alloc_fresh_test_case *tc = test->param_value; +gfp_t gfp_flags = alloc_fresh_gfps[tc->gfp_idx].gfp_flags; +enum zone_type want_zone_type = alloc_fresh_gfps[tc->gfp_idx].want_zone; +struct zone *want_zone = &NODE_DATA (isolated_node)->node_zones[want_zone_type]; +struct list_head *buddy_list; +struct per_cpu_pages *pcp; +struct page *page, *merged_page; +int cpu; + + page =alloc_pages_force_nid (test, gfp_flags, tc->order, isolated_node); + +EXPECT_WITHIN_ZONE (test, page, want_zone); + + cpu =get_cpu (); +__free_pages (page,0 ); + pcp =per_cpu_ptr (want_zone->per_cpu_pageset, cpu); +put_cpu (); + +/* +* Should end up back in the free area when drained. Because everything +* is free, it should get buddy-merged up to the maximum order. +*/ +drain_zone_pages (want_zone, pcp); +KUNIT_EXPECT_TRUE (test,PageBuddy (page)); +KUNIT_EXPECT_EQ (test,buddy_order (page),MAX_PAGE_ORDER ); +KUNIT_EXPECT_TRUE (test,list_empty (&pcp->lists[MIGRATE_UNMOVABLE ])); + merged_page =pfn_to_page (round_down (page_to_pfn (page),1 <<MAX_PAGE_ORDER )); + buddy_list = &want_zone->free_area[MAX_PAGE_ORDER ].free_list[MIGRATE_UNMOVABLE ]; +KUNIT_EXPECT_TRUE (test,page_on_buddy_list (merged_page, buddy_list)); +} +static void action_drain_pages_all (void *unused) {int cpu;@@ -144,7 +277,11 @@ static void depopulate_isolated_node(struct kunit_suite *suite)WARN_ON (add_memory (0 , start, size,MMOP_ONLINE ));WARN_ON (walk_memory_blocks (start, size,NULL , memory_block_online_cb)); } -static struct kunit_case test_cases[] = { {} }; + +static struct kunit_case test_cases[] = { +KUNIT_CASE_PARAM (test_alloc_fresh, alloc_fresh_gen_params), + {} +};struct kunit_suite page_alloc_test_suite = { .name ="page_alloc" ,Signature · 2 lines
-- 2.48.1.601.g30ceb7b040-goog
Re: [PATCH RFC 4/4] mm/page_alloc_test: Add smoke-test for page allocation
Quoted 8 lines · Yosry Ahmed
Quoted 8 lines
mm/page_alloc_test.c +8
-0+static void action_nodemask_free (void *ctx) +{ +NODEMASK_FREE (ctx); +} + +/* +* Call __alloc_pages_noprof with a nodemask containing only the nid. +*
Quoted 8 lines
mm/page_alloc_test.c line 56
Quoted 6 lines
+ * Never returns NULL.
+ */mm/page_alloc_test.c +4
-0+static inline struct page *alloc_pages_force_nid (struct kunit *test, +gfp_t gfp,int order,int nid) +{ +NODEMASK_ALLOC (nodemask_t, nodemask,GFP_KERNEL );For the sake of the test can't we just put the nodemask on the stack?
Hm, I think whether or not it's test code is irrelevant to whether we can put it on the stack. Presumably the nodemask code is written as it is because nodemasks can be massive, so we can overflow the stack here just as easily and confusingly as anywhere else.
(I think we're not in a very deep stack here right now but KUnit could easily change that).
FWIW I think when using the mm/.kunitconfig provided in this series it does actually go on the stack.
Quoted 7 lines
mm/page_alloc_test.c line 60
Quoted 4 lines
mm/page_alloc_test.c +4
-0+struct page *page; + +KUNIT_ASSERT_NOT_NULL (test, nodemask); +kunit_add_action (test, action_nodemask_free, &nodemask);Why aren't we just freeing the nodemask after using it, before we make any assertions?
I guess that's just a philosophical question, I usually default to writing KUnit code such that you can throw an assertion in ~anywhere and things just work.
But, I'm not passionate about it, I would also be fine with freeing it directly (it would certainly save quite a few lines of code).
Quoted 6 lines
Quoted 6 lines
mm/page_alloc_test.c +6
-0+/* Generate test cases as the cross product of orders and alloc_fresh_gfps. */ +static const void *alloc_fresh_gen_params (const void *prev,char *desc) +{ +/* Buffer to avoid allocations. */ +static struct alloc_fresh_test_case tc; +
Quoted 8 lines
mm/page_alloc_test.c line 112
Quoted 6 lines
mm/page_alloc_test.c +6
-0+if (!prev) { +/* First call */ + tc.order =0 ; + tc.gfp_idx =0 ; +return &tc; + }We need to set 'tc' here to whatever 'prev' is pointing at, right?
prev always points at tc (or is NULL). Sounds like it needs a comment to that effect!
(Note tc is static).
Ack to everything else, thanks for the review!
Re: [PATCH RFC 4/4] mm/page_alloc_test: Add smoke-test for page allocation
In reply to Brendan Jackmanmm/page_alloc_test.c line 64
Quoted 12 lines · Brendan Jackman
mm/page_alloc_test.c +12
-0+static inline struct page *alloc_pages_force_nid (struct kunit *test, +gfp_t gfp,int order,int nid) +{ +NODEMASK_ALLOC (nodemask_t, nodemask,GFP_KERNEL ); +struct page *page; + +KUNIT_ASSERT_NOT_NULL (test, nodemask); +kunit_add_action (test, action_nodemask_free, &nodemask); +nodes_clear (*nodemask); +node_set (nid, *nodemask); + + page =__alloc_pages_noprof (GFP_KERNEL ,0 , nid, nodemask);
Oops, it's ignoring the gfp argument here.
Quoted 1 line
+ { .gfp_flags =
GFP_DMA32, .want_zone =ZONE_NORMAL},
And with that fixed, it becomes clear DMA32 allocations can't be expected to succeed in this zone setup.
(Anyway, it's a bit of a silly test regardless, just something to illustrate the KUnit idea).
Quoted 49 lines · Brendan Jackman
The page allocator does a lot of stuff that is not visible to the user in any deterministic way. But this stuff is still important and it would be nice to test that behaviour.
KUnit is a tool for unit-testing kernel-internal APIs. This is an attempt to adopt it the page allocator.
I have been hacking on this as a way to try and test the code I'm writing for my ASI
page_allocintegration proposal [0]. It's been extremely useful to be able to "just call it and see what it does". So I wanna gather some feedback on whether this basic idea is of interest before I invest too much more time in it.You can run these tests like this:
tools/testing/kunit/kunit.py run \ --arch=x86_64 --kernel_args="movablecore=2G" \ --qemu_args="-m 4G" --kunitconfig mm/.kunitconfigUnit-testing code that has mutable global variables can be a pain. Unit-testing code with mutable global variables
_thatcan change concurrently with thetests_is basically impossible. So, we need some way to isolate an "instance" of the allocator that doesn't refer to any such concurrently-mutated state.Luckily, the allocator only has one really important global variable:
node_data. So, the approach here is to carve out a subset of that variable which is as isolated as possible from the rest of rthe system, which can be used for deterministic testing. This is achieved by crating a fake "isolated" node at boot, and plugging in memory at test init time.This is an RFC and not a PATCH because:
1. I have not taken much care to ensure the isolation is complete. There are probably sources of flakiness and nondeterminism in here.
2. I suspect the the basic idea might be over-complicated: do we really need memory hotplug here? Do we even need the instance of the allocator we're testing to actual memory behind the pages it's allocating, or could we just hallucinate a new region of vmemmap without any of that awkwardness?
One significant downside of relying on memory hotplug is that the test won't run if we can't hotplug anything out. That means you have to fiddle with the platform to even run the tests - see the --kernel_args and --qemu_args I had to add to my
kunit.pycommand above.
Quoted 3 lines
So yeah, other suggestions welcome.
2b. I'm not very confident I'm using the hotplug API properly.
Me neither ;)
Dynamically adding memory to that "fake" node is certainly interesting, but which guarantees do we have that some other features (page migration, memory offlining, page reporting ...) don't interact in weird ways with this "fake" node? Adding special-casing all over the place for that feels wrong. I assume this is point 1. you note above.
So I don't quite love the idea on first sight ... but I haven't grasped all details of the full picture yet I'm afraid.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 21 lines · David Hildenbrand
Quoted 17 lines
This is an RFC and not a PATCH because:
1. I have not taken much care to ensure the isolation is complete. There are probably sources of flakiness and nondeterminism in here.
2. I suspect the the basic idea might be over-complicated: do we really need memory hotplug here? Do we even need the instance of the allocator we're testing to actual memory behind the pages it's allocating, or could we just hallucinate a new region of vmemmap without any of that awkwardness?
One significant downside of relying on memory hotplug is that the test won't run if we can't hotplug anything out. That means you have to fiddle with the platform to even run the tests - see the --kernel_args and --qemu_args I had to add to my
kunit.pycommand above.Quoted 3 lines
So yeah, other suggestions welcome.
2b. I'm not very confident I'm using the hotplug API properly.
Quoted 7 lines
Me neither ;)
Dynamically adding memory to that "fake" node is certainly interesting, but which guarantees do we have that some other features (page migration, memory offlining, page reporting ...) don't interact in weird ways with this "fake" node? Adding special-casing all over the place for that feels wrong. I assume this is point 1. you note above.
Yeah, basically this is the big downside. Changing the system we're trying to test in order to make it testable can't be avoided entirely, but I am also pretty unhappy with sprinkling "if (node_isolated(node))" everywhere.
I guess the ideal approach is one where, instead of having to modify the meaning of node_data, we just support replacing it completely, e.g:
struct page *__alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order,
int preferred_nid, nodemask_t *nodemask, struct pagelist_data *node_data)
{
struct alloc_context ac = { .node_data = node_data };
// ...
}Ideally this could be done in such a way that it disappears completely outside of KUnit builds, the interface should be private and we'd wanna get rid of any unnecessary pointer chasing with stuff like:
#ifdef CONFIG_PAGE_ALLOC_KUNIT_TESTS
static inline struct ac_node_data(struct alloc_context *ac, int node)
{
return ac->node_data[node];
}#else
#define ac_node_data(ac, node) (NODE_DATA(node))
#endif
I initially rejected this approach because it felt "too intrusive", but now that I've actually written this RFC I think it could be less intrusive than the node_isolated() thing I've proposed here.
The most obvious challenges I can see there are:
- There might be places that page_alloc.c calls out to that care about node_data but where we really don't want to plumb the alloc_context through (maybe vmscan.c is already such a place)?
- I dunno how many more such helpers we'd need beyond ac_node_data(), like do we need ac_nodes_possible_mask() etc etc etc?
But maybe worth a try - can you see any obvious reason this idea is stupid?
Quoted 2 lines
So I don't quite love the idea on first sight ... but I haven't grasped all details of the full picture yet I'm afraid.
Do you have any thoughts about "making up" memory instead of hot-unplugging real memory for test usage? That might simplify things a bit?
It seems possible that very little mm code cares if the memory we're managing actually exists. (For ASI code we did briefly experiment with tracking information about free pages in the page itself, but it's pretty sketchy and the presence of debug_pagealloc makes me think nobody does it today).
There might be arch-specific issues there, but for unit tests it seems OK if they don't work on every ISA.
Quoted 80 lines · Brendan Jackman
Quoted 21 lines · David Hildenbrand
Quoted 17 lines
This is an RFC and not a PATCH because:
1. I have not taken much care to ensure the isolation is complete. There are probably sources of flakiness and nondeterminism in here.
2. I suspect the the basic idea might be over-complicated: do we really need memory hotplug here? Do we even need the instance of the allocator we're testing to actual memory behind the pages it's allocating, or could we just hallucinate a new region of vmemmap without any of that awkwardness?
One significant downside of relying on memory hotplug is that the test won't run if we can't hotplug anything out. That means you have to fiddle with the platform to even run the tests - see the --kernel_args and --qemu_args I had to add to my
kunit.pycommand above.Quoted 3 lines
So yeah, other suggestions welcome.
2b. I'm not very confident I'm using the hotplug API properly.
Quoted 7 lines
Me neither ;)
Dynamically adding memory to that "fake" node is certainly interesting, but which guarantees do we have that some other features (page migration, memory offlining, page reporting ...) don't interact in weird ways with this "fake" node? Adding special-casing all over the place for that feels wrong. I assume this is point 1. you note above.
Yeah, basically this is the big downside. Changing the system we're trying to test in order to make it testable can't be avoided entirely, but I am also pretty unhappy with sprinkling "if (
node_isolated(node))" everywhere.I guess the ideal approach is one where, instead of having to modify the meaning of
node_data, we just support replacing it completely, e.g:struct page *__alloc_frozen_pages_noprof(gfp_t gfp, unsigned int order,
intpreferred_nid,nodemask_t*nodemask, structpagelist_data*node_data){ struct alloc_context ac = { .node_data = node_data }; // ... }Ideally this could be done in such a way that it disappears completely outside of KUnit builds, the interface should be private and we'd wanna get rid of any unnecessary pointer chasing with stuff like:
#ifdef
CONFIG_PAGE_ALLOC_KUNIT_TESTS
static inline structac_node_data(struct alloc_context *ac, int node){ return ac->node_data[node]; }#else
#defineac_node_data(ac, node)(NODE_DATA(node))
#endifI initially rejected this approach because it felt "too intrusive", but now that I've actually written this RFC I think it could be less intrusive than the
node_isolated()thing I've proposed here.The most obvious challenges I can see there are:
- There might be places that
page_alloc.ccalls out to that care aboutnode_databut where we really don't want to plumb thealloc_contextthrough (maybevmscan.cis already such a place)?- I dunno how many more such helpers we'd need beyond
ac_node_data(), like do we needac_nodes_possible_mask()etc etc etc?But maybe worth a try - can you see any obvious reason this idea is stupid?
Quoted 2 lines
So I don't quite love the idea on first sight ... but I haven't grasped all details of the full picture yet I'm afraid.
Quoted 8 lines
Do you have any thoughts about "making up" memory instead of hot-unplugging real memory for test usage? That might simplify things a bit?
Quoted 1 line
It seems possible that very little mm code cares if the memory we're
managing actually exists. (For ASI code we did briefly experiment with tracking information about free pages in the page itself, but it's pretty sketchy and the presence of
debug_pageallocmakes me think nobody does it today).
At least when it comes to the buddy, only page zeroing+poisoning should access actual page content.
So making up memory might actually work in quite some setups, assuming that it will never get allocated.
The "complicated" thing is still that we are trying to test parts of the buddy in a well-controlled way while other kernel infrastructure is using the buddy in rather uncontrolled ways.
Quoted 3 lines
There might be arch-specific issues there, but for unit tests it seems OK if they don't work on every ISA.
Just pointing it out: for memblock tests (tools/testing/memblock/) we actually compile memblock.c to be used in a user space application, stubbing all external function calls etc such that we get the basics running.
It'd probably be quite some work to get page_alloc.c into a similar shape, likely we'd have to move a lot of unrelated-for-the tests stuff, and think about how to handle some nasty details like pcp etc. Just wondering, did you think about that option as well?
The nice thing about such an approach is that we can test the allcator without any possible side effects from the running system.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 9 lines · David Hildenbrand
Quoted 5 lines
Quoted 1 line
It seems possible that very little mm code cares if the memory we're
managing actually exists. (For ASI code we did briefly experiment with tracking information about free pages in the page itself, but it's pretty sketchy and the presence of
debug_pageallocmakes me think nobody does it today).At least when it comes to the buddy, only page zeroing+poisoning should access actual page content.
Quoted 6 lines
So making up memory might actually work in quite some setups, assuming that it will never get allocated.
The "complicated" thing is still that we are trying to test parts of the buddy in a well-controlled way while other kernel infrastructure is using the buddy in rather uncontrolled ways.
Thanks, yeah that makes sense, and I agree that's the hard part. If we can design a way to actually test the interface in an isolated way, where we get the "memory" that we use to do that is kinda secondary and can be changed later.
Quoted 8 lines
Quoted 2 lines
There might be arch-specific issues there, but for unit tests it seems OK if they don't work on every ISA.
Just pointing it out: for memblock tests (
tools/testing/memblock/) we actually compilememblock.cto be used in a user space application, stubbing all external function calls etc such that we get the basics running.
Quoted 7 lines
It'd probably be quite some work to get
page_alloc.cinto a similar shape, likely we'd have to move a lot of unrelated-for-the tests stuff, and think about how to handle some nasty details like pcp etc. Just wondering, did you think about that option as well?The nice thing about such an approach is that we can test the allcator without any possible side effects from the running system.
Yeah Lorenzo also pointed me to tools/testing/vma and I am pretty sold that it's a better approach than KUnit where it's possible. But, I'm doubtful about using it for page_alloc.
I think it could definitely be a good idea for the really core buddy logic (like rmqueue_buddy() and below), where I'm sure we could stub out stuff like percpu_* and locking and have the tests still be meaningful. But I'm not sure that really low-level code is calling out for more testing.
Whereas I suspect if you zoom out even just to the level of __alloc_frozen_pages_noprof(), it starts to get a bit impractical already. And that's where I really wanna get coverage.
Anyway, I'm thinking the next step here is to explore how to get away from the node_isolated() stuff in this RFC, so I'll keep that idea in mind and try to get a feel for whether it looks possible.