Path: blob/master/tools/testing/memblock/tests/common.c
26299 views
// SPDX-License-Identifier: GPL-2.0-or-later1#include "tests/common.h"2#include <string.h>3#include <getopt.h>4#include <linux/memory_hotplug.h>5#include <linux/build_bug.h>67#define PREFIXES_MAX 158#define DELIM ": "9#define BASIS 100001011static struct test_memory memory_block;12static const char __maybe_unused *prefixes[PREFIXES_MAX];13static int __maybe_unused nr_prefixes;1415static const char *short_opts = "hmv";16static const struct option long_opts[] = {17{"help", 0, NULL, 'h'},18{"movable-node", 0, NULL, 'm'},19{"verbose", 0, NULL, 'v'},20{NULL, 0, NULL, 0}21};2223static const char * const help_opts[] = {24"display this help message and exit",25"disallow allocations from regions marked as hotplugged\n\t\t\t"26"by simulating enabling the \"movable_node\" kernel\n\t\t\t"27"parameter",28"enable verbose output, which includes the name of the\n\t\t\t"29"memblock function being tested, the name of the test,\n\t\t\t"30"and whether the test passed or failed."31};3233static int verbose;3435/* sets global variable returned by movable_node_is_enabled() stub */36bool movable_node_enabled;3738void reset_memblock_regions(void)39{40memset(memblock.memory.regions, 0,41memblock.memory.cnt * sizeof(struct memblock_region));42memblock.memory.cnt = 0;43memblock.memory.max = INIT_MEMBLOCK_REGIONS;44memblock.memory.total_size = 0;4546memset(memblock.reserved.regions, 0,47memblock.reserved.cnt * sizeof(struct memblock_region));48memblock.reserved.cnt = 0;49memblock.reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS;50memblock.reserved.total_size = 0;51}5253void reset_memblock_attributes(void)54{55memblock.memory.name = "memory";56memblock.reserved.name = "reserved";57memblock.bottom_up = false;58memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;59}6061static inline void fill_memblock(void)62{63memset(memory_block.base, 1, PHYS_MEM_SIZE);64}6566void setup_memblock(void)67{68reset_memblock_regions();69memblock_add((phys_addr_t)memory_block.base, MEM_SIZE);70fill_memblock();71}7273/**74* setup_numa_memblock:75* Set up a memory layout with multiple NUMA nodes in a previously allocated76* dummy physical memory.77* @node_fracs: an array representing the fraction of MEM_SIZE contained in78* each node in basis point units (one hundredth of 1% or 1/10000).79* For example, if node 0 should contain 1/8 of MEM_SIZE,80* node_fracs[0] = 1250.81*82* The nids will be set to 0 through NUMA_NODES - 1.83*/84void setup_numa_memblock(const unsigned int node_fracs[])85{86phys_addr_t base;87int flags;8889reset_memblock_regions();90base = (phys_addr_t)memory_block.base;91flags = (movable_node_is_enabled()) ? MEMBLOCK_NONE : MEMBLOCK_HOTPLUG;9293for (int i = 0; i < NUMA_NODES; i++) {94assert(node_fracs[i] <= BASIS);95phys_addr_t size = MEM_SIZE * node_fracs[i] / BASIS;9697memblock_add_node(base, size, i, flags);98base += size;99}100fill_memblock();101}102103void dummy_physical_memory_init(void)104{105memory_block.base = malloc(PHYS_MEM_SIZE);106assert(memory_block.base);107fill_memblock();108}109110void dummy_physical_memory_cleanup(void)111{112free(memory_block.base);113}114115phys_addr_t dummy_physical_memory_base(void)116{117return (phys_addr_t)memory_block.base;118}119120static void usage(const char *prog)121{122BUILD_BUG_ON(ARRAY_SIZE(help_opts) != ARRAY_SIZE(long_opts) - 1);123124printf("Usage: %s [-%s]\n", prog, short_opts);125126for (int i = 0; long_opts[i].name; i++) {127printf(" -%c, --%-12s\t%s\n", long_opts[i].val,128long_opts[i].name, help_opts[i]);129}130131exit(1);132}133134void parse_args(int argc, char **argv)135{136int c;137138while ((c = getopt_long_only(argc, argv, short_opts, long_opts,139NULL)) != -1) {140switch (c) {141case 'm':142movable_node_enabled = true;143break;144case 'v':145verbose = 1;146break;147default:148usage(argv[0]);149}150}151}152153void print_prefixes(const char *postfix)154{155for (int i = 0; i < nr_prefixes; i++)156test_print("%s%s", prefixes[i], DELIM);157test_print(postfix);158}159160void test_fail(void)161{162if (verbose) {163ksft_test_result_fail(": ");164print_prefixes("failed\n");165}166}167168void test_pass(void)169{170if (verbose) {171ksft_test_result_pass(": ");172print_prefixes("passed\n");173}174}175176void test_print(const char *fmt, ...)177{178if (verbose) {179int saved_errno = errno;180va_list args;181182va_start(args, fmt);183errno = saved_errno;184vprintf(fmt, args);185va_end(args);186}187}188189void prefix_reset(void)190{191memset(prefixes, 0, PREFIXES_MAX * sizeof(char *));192nr_prefixes = 0;193}194195void prefix_push(const char *prefix)196{197assert(nr_prefixes < PREFIXES_MAX);198prefixes[nr_prefixes] = prefix;199nr_prefixes++;200}201202void prefix_pop(void)203{204if (nr_prefixes > 0) {205prefixes[nr_prefixes - 1] = 0;206nr_prefixes--;207}208}209210211