1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2019 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 /*
7 * We are testing mbind() EIO error.
8 *
9 * We first fault a allocated page, then attempt to mbind it to a different node.
10 *
11 * This is a regression test for:
12 *
13 * a7f40cfe3b7a mm: mempolicy: make mbind() return -EIO when MPOL_MF_STRICT is specified
14 *
15 */
16
17 #include <errno.h>
18 #include "config.h"
19 #ifdef HAVE_NUMA_H
20 # include <numa.h>
21 # include <numaif.h>
22 #endif
23 #include "tst_test.h"
24 #include "tst_numa.h"
25
26 #ifdef HAVE_NUMA_V2
27
28 static size_t page_size;
29 static struct tst_nodemap *nodes;
30
setup(void)31 static void setup(void)
32 {
33 page_size = getpagesize();
34
35 nodes = tst_get_nodemap(TST_NUMA_MEM, 2 * page_size / 1024);
36 if (nodes->cnt <= 1)
37 tst_brk(TCONF, "Test requires at least two NUMA memory nodes");
38 }
39
cleanup(void)40 static void cleanup(void)
41 {
42 tst_nodemap_free(nodes);
43 }
44
verify_policy(int mode)45 static void verify_policy(int mode)
46 {
47 struct bitmask *bm = numa_allocate_nodemask();
48 unsigned int i;
49 void *ptr;
50 unsigned long size = page_size;
51 int node = 0;
52
53 ptr = tst_numa_map(NULL, size);
54 tst_nodemap_reset_counters(nodes);
55 tst_numa_fault(ptr, size);
56 tst_nodemap_count_pages(nodes, ptr, size);
57 tst_nodemap_print_counters(nodes);
58
59 for (i = 0; i < nodes->cnt; i++) {
60 if (!nodes->counters[i]) {
61 node = nodes->map[i];
62 tst_res(TINFO, "Attempting to bind to node %i", node);
63 numa_bitmask_setbit(bm, node);
64 break;
65 }
66 }
67
68 TEST(mbind(ptr, size, mode, bm->maskp, bm->size + 1, MPOL_MF_STRICT));
69
70 tst_numa_unmap(ptr, size);
71 numa_free_nodemask(bm);
72
73 if (TST_RET != -1) {
74 tst_res(TFAIL,
75 "mbind(%s, MPOL_MF_STRICT) node %u returned %li, expected -1",
76 tst_mempolicy_mode_name(mode), node, TST_RET);
77 return;
78 }
79
80 if (TST_ERR == EIO) {
81 tst_res(TPASS | TTERRNO,
82 "mbind(%s, MPOL_MF_STRICT) node %u",
83 tst_mempolicy_mode_name(mode), node);
84 } else {
85 tst_res(TFAIL | TTERRNO,
86 "mbind(%s, MPOL_MF_STRICT) node %u expected EIO",
87 tst_mempolicy_mode_name(mode), node);
88 }
89 }
90
91 static const int modes[] = {
92 MPOL_PREFERRED,
93 MPOL_BIND,
94 MPOL_INTERLEAVE,
95 };
96
verify_mbind(unsigned int n)97 static void verify_mbind(unsigned int n)
98 {
99 verify_policy(modes[n]);
100 }
101
102 static struct tst_test test = {
103 .setup = setup,
104 .cleanup = cleanup,
105 .test = verify_mbind,
106 .tcnt = ARRAY_SIZE(modes),
107 .tags = (const struct tst_tag[]) {
108 {"linux-git", "a7f40cfe3b7a"},
109 {}
110 }
111 };
112
113 #else
114
115 TST_TEST_TCONF(NUMA_ERROR_MSG);
116
117 #endif /* HAVE_NUMA_H */
118