Skip to content

Commit

Permalink
memblock tests: Add simulation of physical memory
Browse files Browse the repository at this point in the history
Allocation functions that return virtual addresses (with an exception
of _raw variant) clear the allocated memory after reserving it. This
requires valid memory ranges in memblock.memory.

Introduce memory_block variable to store memory that can be registered
with memblock data structure. Move assert.h and size.h includes to common.h
to share them between the test files.

Signed-off-by: Karolina Drobnik <karolinadrobnik@gmail.com>
Signed-off-by: Mike Rapoport <rppt@linux.ibm.com>
Link: https://lore.kernel.org/r/dce115503c74a6936c44694b00014658a1bb6522.1646055639.git.karolinadrobnik@gmail.com
  • Loading branch information
Karolina Drobnik authored and Mike Rapoport committed Mar 9, 2022
1 parent 2c3dacb commit 284d950
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
1 change: 0 additions & 1 deletion tools/testing/memblock/tests/basic_api.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include <string.h>
#include <linux/memblock.h>
#include <linux/sizes.h>
#include "basic_api.h"

#define EXPECTED_MEMBLOCK_REGIONS 128
Expand Down
1 change: 0 additions & 1 deletion tools/testing/memblock/tests/basic_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#ifndef _MEMBLOCK_BASIC_H
#define _MEMBLOCK_BASIC_H

#include <assert.h>
#include "common.h"

int memblock_basic_checks(void);
Expand Down
19 changes: 19 additions & 0 deletions tools/testing/memblock/tests/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#define INIT_MEMBLOCK_REGIONS 128
#define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS

static struct test_memory memory_block;

void reset_memblock_regions(void)
{
memset(memblock.memory.regions, 0,
Expand All @@ -27,3 +29,20 @@ void reset_memblock_attributes(void)
memblock.bottom_up = false;
memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
}

void setup_memblock(void)
{
reset_memblock_regions();
memblock_add((phys_addr_t)memory_block.base, MEM_SIZE);
}

void dummy_physical_memory_init(void)
{
memory_block.base = malloc(MEM_SIZE);
assert(memory_block.base);
}

void dummy_physical_memory_cleanup(void)
{
free(memory_block.base);
}
18 changes: 18 additions & 0 deletions tools/testing/memblock/tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@
#ifndef _MEMBLOCK_TEST_H
#define _MEMBLOCK_TEST_H

#include <stdlib.h>
#include <assert.h>
#include <linux/types.h>
#include <linux/memblock.h>
#include <linux/sizes.h>

#define MEM_SIZE SZ_16K

/*
* Available memory registered with memblock needs to be valid for allocs
* test to run. This is a convenience wrapper for memory allocated in
* dummy_physical_memory_init() that is later registered with memblock
* in setup_memblock().
*/
struct test_memory {
void *base;
};

struct region {
phys_addr_t base;
Expand All @@ -12,5 +27,8 @@ struct region {

void reset_memblock_regions(void);
void reset_memblock_attributes(void);
void setup_memblock(void);
void dummy_physical_memory_init(void);
void dummy_physical_memory_cleanup(void);

#endif

0 comments on commit 284d950

Please sign in to comment.