memblock tests: cover low-address allocations
PATCH v3 2/2 memblock tests: cover allocations below the low address limit
In reply to Tianyi ChenAdd memblock_alloc_low() coverage using the simulator's low address limit. Exercise aligned allocation, an allocation whose last byte is immediately below the limit, an alignment constraint that prevents fitting below it, and fully reserved low memory with high memory still available.
Run each case with bottom-up and top-down allocation. Check zeroing and reserved-region accounting as well as returned addresses. Verify that an unrestricted allocation can use the free high memory after the low allocation fails.
Document the simulated limit and remove the completed TODO.
--- tools/testing/memblock/Makefile | 3 +- tools/testing/memblock/README | 12 +- tools/testing/memblock/TODO | 5 - tools/testing/memblock/main.c | 2 + tools/testing/memblock/tests/alloc_low_api.c | 148 +++++++++++++++++++ tools/testing/memblock/tests/alloc_low_api.h | 9 ++ 6 files changed, 166 insertions(+), 13 deletions(-) delete mode 100644 tools/testing/memblock/TODO create mode 100644 tools/testing/memblock/tests/alloc_low_api.c create mode 100644 tools/testing/memblock/tests/alloc_low_api.h
tools/testing/memblock/Makefile +2 -1
@@ -7,7 +7,8 @@ CFLAGS += -I. -I../../include -Wall -O2 -fsanitize=address \ LDFLAGS += -fsanitize=address -fsanitize=undefined TARGETS = main TEST_OFILES = tests/alloc_nid_api.o tests/alloc_helpers_api.o tests/alloc_api.o \ - tests/basic_api.o tests/common.o tests/alloc_exact_nid_api.o + tests/basic_api.o tests/common.o tests/alloc_exact_nid_api.o \ + tests/alloc_low_api.o DEP_OFILES = memblock.o lib/slab.o mmzone.o slab.o cmdline.o OFILES = main.o $(DEP_OFILES) $(TEST_OFILES) EXTR_SRC = ../../../mm/memblock.c
tools/testing/memblock/README +5 -7
@@ -67,13 +67,13 @@ memblock |-- tests | |-- alloc_api.(c|h) -- memblock_alloc tests | |-- alloc_helpers_api.(c|h) -- memblock_alloc_from tests +| |-- alloc_low_api.(c|h) -- memblock_alloc_low tests | |-- alloc_nid_api.(c|h) -- memblock_alloc_try_nid tests | |-- basic_api.(c|h) -- memblock_add/memblock_reserve/... tests | |-- common.(c|h) -- helper functions for resetting memblock; |-- main.c --------------. dummy physical memory definition |-- Makefile `- test runner |-- README -|-- TODO |-- .gitignore Simulating physical memory
@@ -101,12 +101,10 @@ There's no need to explicitly free the dummy memory from memblock via memblock_free() call. The entry will be erased by reset_memblock_regions(), called at the beginning of each test. -Known issues -============ - -1. Tests for memblock_alloc_low() can't be easily implemented. The function uses - ARCH_LOW_ADDRESS_LIMIT marco, which can't be changed to point at the low - memory of the memory_block. +The simulator defines ARCH_LOW_ADDRESS_LIMIT in asm/dma.h using the midpoint +of the MEM_SIZE range registered by setup_memblock(). This leaves registered +memory both below and above the limit. The limit is the first address an +allocation must not use; an allocation's base plus its size may equal the limit. References ==========
tools/testing/memblock/TODO deleted +0 -5
@@ -1,5 +0,0 @@ -TODO -===== - -1. Add tests for memblock_alloc_low() once the simulator can model - ARCH_LOW_ADDRESS_LIMIT against the low memory in memory_block
tools/testing/memblock/main.c +2 -0
@@ -4,6 +4,7 @@#include "tests/alloc_helpers_api.h" #include "tests/alloc_nid_api.h" #include "tests/alloc_exact_nid_api.h" +#include "tests/alloc_low_api.h" #include "tests/common.h" int main (int argc,char **argv)
@@ -12,6 +13,7 @@ int main(int argc, char **argv)memblock_basic_checks ();memblock_alloc_checks ();memblock_alloc_helpers_checks (); +memblock_alloc_low_checks ();memblock_alloc_nid_checks ();memblock_alloc_exact_nid_checks ();
tools/testing/memblock/tests/alloc_low_api.c new file +148 -0
148 lines not on this page · raw message
tools/testing/memblock/tests/alloc_low_api.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef _MEMBLOCK_ALLOC_LOW_H +#define _MEMBLOCK_ALLOC_LOW_H + +#include "common.h" + +int memblock_alloc_low_checks (void ); + +#endif
Signature · 1 line
2.55.0
PATCH v3 1/2 memblock tests: model the low allocation limit within dummy memory
In reply to Tianyi ChenThe simulator represents physical addresses using the address of its allocated buffer. That buffer can lie above the default ARCH_LOW_ADDRESS_LIMIT, preventing meaningful memblock_alloc_low() tests.
Use the architecture override in asm/dma.h to place the limit halfway through the memory registered by setup_memblock(). Resolve the limit after allocating the buffer, leaving registered memory on both sides.
--- tools/testing/memblock/asm/dma.h | 6 ++++++ tools/testing/memblock/tests/common.c | 6 ++++++ 2 files changed, 12 insertions(+)
tools/testing/memblock/asm/dma.h +6 -0
@@ -2,4 +2,10 @@#ifndef _TOOLS_DMA_H#define _TOOLS_DMA_H +#include <linux/types.h> + +phys_addr_t dummy_physical_memory_low_limit (void ); + +#define ARCH_LOW_ADDRESS_LIMIT dummy_physical_memory_low_limit() +#endif
tools/testing/memblock/tests/common.c +6 -0
@@ -118,6 +118,12 @@ phys_addr_t dummy_physical_memory_base(void)return (phys_addr_t )memory_block.base; } +phys_addr_t dummy_physical_memory_low_limit (void ) +{ +/* The low limit is halfway through memory registered by setup_memblock(). */ +return dummy_physical_memory_base () +MEM_SIZE /2 ; +} +static void usage (const char *prog) {BUILD_BUG_ON (ARRAY_SIZE (help_opts) !=ARRAY_SIZE (long_opts) -1 );
Signature · 1 line
2.55.0
v3 cover memblock tests: cover low-address allocations
Model a low allocation limit within the memory registered by the simulator, then exercise memblock_alloc_low() in both allocation directions.
Changes in v3:
- Describe the low limit and simulated RAM alignment directly in comments and README.
- Use Assisted-by: LLM in both patches.
- Start a separate thread for this revision.
The pointer casts remain unchanged. With BUILD=32, pointers are 32-bit and phys_addr_t is 64-bit: the tested compiler sign-extends a direct cast, while the conversion through uintptr_t zero-extends it. The direct casts also introduce compiler warnings, so this revision preserves the existing conversion behavior. There are no executable code changes from v2.
Validation with ASan and UBSan enabled:
- Default and 32BIT_PHYS_ADDR_T=1: 189 tests pass in each runtime mode.
- NUMA=1, 32BIT_PHYS_ADDR_T=1 with NUMA=1, and MEMBLOCK_DEBUG=1 with NUMA=1:
272 tests pass in each runtime mode.
- Both ./main -v and ./main -v -m were run for those configurations.
- BUILD=32 builds without warnings from alloc_low_api.c. Its runtime still hits the pre-existing basic_api.c memblock_free_near_max_check assertion before reaching the low-allocation tests.
v2: https://lore.kernel.org/r/178874362959.1415955.18132997964995947947.memblock-v2-0@tychen.cc
Tianyi Chen (2):
memblock tests: model the low allocation limit within dummy memory memblock tests: cover allocations below the low address limit
tools/testing/memblock/Makefile | 3 +- tools/testing/memblock/README | 12 +- tools/testing/memblock/TODO | 5 - tools/testing/memblock/asm/dma.h | 6 + tools/testing/memblock/main.c | 2 + tools/testing/memblock/tests/alloc_low_api.c | 148 +++++++++++++++++++ tools/testing/memblock/tests/alloc_low_api.h | 9 ++ tools/testing/memblock/tests/common.c | 6 + 8 files changed, 178 insertions(+), 13 deletions(-) delete mode 100644 tools/testing/memblock/TODO create mode 100644 tools/testing/memblock/tests/alloc_low_api.c create mode 100644 tools/testing/memblock/tests/alloc_low_api.h
Signature · 1 line
2.55.0
Quoted 5 lines · Tianyi Chen
memblock tests: cover low-address allocations
Model a low allocation limit within the memory registered by the simulator, then exercise
memblock_alloc_low()in both allocation directions.
Quoted 7 lines
Changes in v3:
- Describe the low limit and simulated RAM alignment directly in comments and README.
- Use Assisted-by: LLM in both patches.
- Start a separate thread for this revision.[...]
Applied to for-next branch of memblock.git tree, thanks!
[1/2] memblock tests: model the low allocation limit within dummy memory
commit: 5c194192279e0894b898967ff939e310a9bd06b5
[2/2] memblock tests: cover allocations below the low address limit
commit: ba7b611a1fada0bff7f6c3fbc6ff13978bc1af91tree: https://git.kernel.org/pub/scm/linux/kernel/git/rppt/memblock
branch: for-next
--
Sincerely yours,
Mike.