Skip to content

Commit 4d4c266

Browse files
laoarKernel Patches Daemon
authored andcommitted
selftests/bpf: Add selftests for mbind(2) with lsm prog
The result as follows, #142/1 mempolicy/MPOL_BIND_with_lsm:OK #142/2 mempolicy/MPOL_DEFAULT_with_lsm:OK #142/3 mempolicy/MPOL_BIND_without_lsm:OK #142/4 mempolicy/MPOL_DEFAULT_without_lsm:OK #142 mempolicy:OK Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
1 parent 54fa082 commit 4d4c266

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 79 additions & 0 deletions
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 <laoar.shao@gmail.com> */
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_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_mempolicy(void)
47+
{
48+
struct test_mempolicy *skel;
49+
int err;
50+
51+
skel = test_mempolicy__open();
52+
if (!ASSERT_OK_PTR(skel, "open"))
53+
return;
54+
55+
skel->bss->target_pid = getpid();
56+
57+
err = test_mempolicy__load(skel);
58+
if (!ASSERT_OK(err, "load"))
59+
goto destroy;
60+
61+
/* Attach LSM prog first */
62+
err = test_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_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+
}
Lines changed: 29 additions & 0 deletions
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 <laoar.shao@gmail.com> */
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/mbind")
24+
int BPF_PROG(mbind_run, u64 start, u64 len, u64 mode, const u64 *nmask, u64 maxnode, u32 flags)
25+
{
26+
return mem_policy_adjustment(mode);
27+
}
28+
29+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)