mbox series

[RFC,0/4] mm: KUnit tests for the page allocator

Message ID 20250224-page-alloc-kunit-v1-0-d337bb440889@google.com (mailing list archive)
Headers show
Series mm: KUnit tests for the page allocator | expand

Message

Brendan Jackman Feb. 24, 2025, 2:47 p.m. UTC
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/20250129144320.2675822-1-jackmanb@google.com/ 

Signed-off-by: Brendan Jackman <jackmanb@google.com>
---
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,