1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2019 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker * We are testing mbind() MPOL_MF_MOVE and MPOL_MF_MOVE_ALL.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * If one of these flags is passed along with the policy kernel attempts to
10*49cdfc7eSAndroid Build Coastguard Worker * move already faulted pages to match the requested policy.
11*49cdfc7eSAndroid Build Coastguard Worker */
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
15*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_NUMA_H
16*49cdfc7eSAndroid Build Coastguard Worker # include <numa.h>
17*49cdfc7eSAndroid Build Coastguard Worker # include <numaif.h>
18*49cdfc7eSAndroid Build Coastguard Worker # include "mbind.h"
19*49cdfc7eSAndroid Build Coastguard Worker #endif
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_numa.h"
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #ifdef HAVE_NUMA_V2
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker static size_t page_size;
26*49cdfc7eSAndroid Build Coastguard Worker static struct tst_nodemap *nodes;
27*49cdfc7eSAndroid Build Coastguard Worker
setup(void)28*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker page_size = getpagesize();
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker nodes = tst_get_nodemap(TST_NUMA_MEM, 2 * page_size / 1024);
33*49cdfc7eSAndroid Build Coastguard Worker if (nodes->cnt <= 1)
34*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "Test requires at least two NUMA memory nodes");
35*49cdfc7eSAndroid Build Coastguard Worker }
36*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)37*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker tst_nodemap_free(nodes);
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker
verify_policy(int mode,unsigned flag)42*49cdfc7eSAndroid Build Coastguard Worker static void verify_policy(int mode, unsigned flag)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker struct bitmask *bm = numa_allocate_nodemask();
45*49cdfc7eSAndroid Build Coastguard Worker unsigned int i;
46*49cdfc7eSAndroid Build Coastguard Worker void *ptr;
47*49cdfc7eSAndroid Build Coastguard Worker unsigned long size = page_size;
48*49cdfc7eSAndroid Build Coastguard Worker unsigned int node = 0;
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker ptr = tst_numa_map(NULL, size);
51*49cdfc7eSAndroid Build Coastguard Worker tst_nodemap_reset_counters(nodes);
52*49cdfc7eSAndroid Build Coastguard Worker tst_numa_fault(ptr, size);
53*49cdfc7eSAndroid Build Coastguard Worker tst_nodemap_count_pages(nodes, ptr, size);
54*49cdfc7eSAndroid Build Coastguard Worker tst_nodemap_print_counters(nodes);
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < nodes->cnt; i++) {
57*49cdfc7eSAndroid Build Coastguard Worker if (!nodes->counters[i]) {
58*49cdfc7eSAndroid Build Coastguard Worker node = nodes->map[i];
59*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Attempting to move to node %i", node);
60*49cdfc7eSAndroid Build Coastguard Worker numa_bitmask_setbit(bm, node);
61*49cdfc7eSAndroid Build Coastguard Worker break;
62*49cdfc7eSAndroid Build Coastguard Worker }
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker TEST(mbind(ptr, size, mode, bm->maskp, bm->size + 1, flag));
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET) {
68*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
69*49cdfc7eSAndroid Build Coastguard Worker "mbind(%s, %s) node %u",
70*49cdfc7eSAndroid Build Coastguard Worker tst_mempolicy_mode_name(mode), mbind_flag_name(flag), node);
71*49cdfc7eSAndroid Build Coastguard Worker goto exit;
72*49cdfc7eSAndroid Build Coastguard Worker } else {
73*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "mbind(%s, %s) node %u succeded",
74*49cdfc7eSAndroid Build Coastguard Worker tst_mempolicy_mode_name(mode), mbind_flag_name(flag), node);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker tst_nodemap_reset_counters(nodes);
78*49cdfc7eSAndroid Build Coastguard Worker tst_nodemap_count_pages(nodes, ptr, size);
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < nodes->cnt; i++) {
81*49cdfc7eSAndroid Build Coastguard Worker if (nodes->map[i] == node) {
82*49cdfc7eSAndroid Build Coastguard Worker if (nodes->counters[i] == 1) {
83*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Node %u allocated %u", node, 1);
84*49cdfc7eSAndroid Build Coastguard Worker } else {
85*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Node %u allocated %u, expected %u",
86*49cdfc7eSAndroid Build Coastguard Worker node, nodes->counters[i], 0);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker continue;
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker if (nodes->counters[i]) {
92*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Node %u allocated %u, expected 0",
93*49cdfc7eSAndroid Build Coastguard Worker i, nodes->counters[i]);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker }
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker exit:
98*49cdfc7eSAndroid Build Coastguard Worker tst_numa_unmap(ptr, size);
99*49cdfc7eSAndroid Build Coastguard Worker numa_free_nodemask(bm);
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker static const int modes[] = {
103*49cdfc7eSAndroid Build Coastguard Worker MPOL_PREFERRED,
104*49cdfc7eSAndroid Build Coastguard Worker MPOL_BIND,
105*49cdfc7eSAndroid Build Coastguard Worker MPOL_INTERLEAVE,
106*49cdfc7eSAndroid Build Coastguard Worker };
107*49cdfc7eSAndroid Build Coastguard Worker
verify_mbind(unsigned int n)108*49cdfc7eSAndroid Build Coastguard Worker static void verify_mbind(unsigned int n)
109*49cdfc7eSAndroid Build Coastguard Worker {
110*49cdfc7eSAndroid Build Coastguard Worker verify_policy(modes[n], MPOL_MF_MOVE);
111*49cdfc7eSAndroid Build Coastguard Worker verify_policy(modes[n], MPOL_MF_MOVE_ALL);
112*49cdfc7eSAndroid Build Coastguard Worker }
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
115*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
116*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
117*49cdfc7eSAndroid Build Coastguard Worker .test = verify_mbind,
118*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(modes),
119*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
120*49cdfc7eSAndroid Build Coastguard Worker };
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker #else
123*49cdfc7eSAndroid Build Coastguard Worker
124*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF(NUMA_ERROR_MSG);
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker #endif /* HAVE_NUMA_H */
127