Skip to content

Commit 87a07e1

Browse files
laoarintel-lab-lkp
authored andcommitted
selftests/bpf: Add selftests for set_mempolicy with a lsm prog
The result as follows, torvalds#261/1 set_mempolicy/MPOL_BIND_with_lsm:OK torvalds#261/2 set_mempolicy/MPOL_DEFAULT_with_lsm:OK torvalds#261/3 set_mempolicy/MPOL_BIND_without_lsm:OK torvalds#261/4 set_mempolicy/MPOL_DEFAULT_without_lsm:OK torvalds#261 set_mempolicy:OK Summary: 1/4 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yafang Shao <[email protected]>
1 parent 8f27068 commit 87a07e1

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

tools/testing/selftests/bpf/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ CFLAGS += -g $(OPT_FLAGS) -rdynamic \
3535
-I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \
3636
-I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
3737
LDFLAGS += $(SAN_LDFLAGS)
38-
LDLIBS += -lelf -lz -lrt -lpthread
38+
LDLIBS += -lelf -lz -lrt -lpthread -lnuma
3939

4040
ifneq ($(LLVM),)
4141
# Silence some warnings when compiled with clang
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023 Yafang Shao <[email protected]> */
3+
4+
#include <sys/types.h>
5+
#include <unistd.h>
6+
#include <sys/mman.h>
7+
#include <numaif.h>
8+
#include <test_progs.h>
9+
#include "test_set_mempolicy.skel.h"
10+
11+
#define SIZE 4096
12+
13+
static void mempolicy_bind(bool success)
14+
{
15+
unsigned long mask = 1;
16+
char *addr;
17+
int err;
18+
19+
addr = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
20+
if (!ASSERT_OK_PTR(addr, "mmap"))
21+
return;
22+
23+
err = mbind(addr, SIZE, MPOL_BIND, &mask, sizeof(mask), 0);
24+
if (success)
25+
ASSERT_OK(err, "mbind_success");
26+
else
27+
ASSERT_ERR(err, "mbind_fail");
28+
29+
munmap(addr, SIZE);
30+
}
31+
32+
static void mempolicy_default(void)
33+
{
34+
char *addr;
35+
int err;
36+
37+
addr = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
38+
if (!ASSERT_OK_PTR(addr, "mmap"))
39+
return;
40+
41+
err = mbind(addr, SIZE, MPOL_DEFAULT, NULL, 0, 0);
42+
ASSERT_OK(err, "mbind_success");
43+
44+
munmap(addr, SIZE);
45+
}
46+
void test_set_mempolicy(void)
47+
{
48+
struct test_set_mempolicy *skel;
49+
int err;
50+
51+
skel = test_set_mempolicy__open();
52+
if (!ASSERT_OK_PTR(skel, "open"))
53+
return;
54+
55+
skel->bss->target_pid = getpid();
56+
57+
err = test_set_mempolicy__load(skel);
58+
if (!ASSERT_OK(err, "load"))
59+
goto destroy;
60+
61+
/* Attach LSM prog first */
62+
err = test_set_mempolicy__attach(skel);
63+
if (!ASSERT_OK(err, "attach"))
64+
goto destroy;
65+
66+
/* syscall to adjust memory policy */
67+
if (test__start_subtest("MPOL_BIND_with_lsm"))
68+
mempolicy_bind(false);
69+
if (test__start_subtest("MPOL_DEFAULT_with_lsm"))
70+
mempolicy_default();
71+
72+
destroy:
73+
test_set_mempolicy__destroy(skel);
74+
75+
if (test__start_subtest("MPOL_BIND_without_lsm"))
76+
mempolicy_bind(true);
77+
if (test__start_subtest("MPOL_DEFAULT_without_lsm"))
78+
mempolicy_default();
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023 Yafang Shao <[email protected]> */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
#include <bpf/bpf_core_read.h>
8+
9+
int target_pid;
10+
11+
static int mem_policy_adjustment(u64 mode)
12+
{
13+
struct task_struct *task = bpf_get_current_task_btf();
14+
15+
if (task->pid != target_pid)
16+
return 0;
17+
18+
if (mode != MPOL_BIND)
19+
return 0;
20+
return -1;
21+
}
22+
23+
SEC("lsm/set_mempolicy")
24+
int BPF_PROG(setmempolicy, u64 mode, u16 mode_flags, nodemask_t *nmask, u32 flags)
25+
{
26+
return mem_policy_adjustment(mode);
27+
}
28+
29+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)