Skip to content

Commit ad5926a

Browse files
Yonghong SongKernel Patches Daemon
authored andcommitted
selftests/bpf: Refactor out some functions in ns_current_pid_tgid test
Refactor some functions in both user space code and bpf program as these functions are used by later cgroup/sk_msg tests. Another change is to mark tp program optional loading as later patches will use optional loading as well since they have quite different attachment and testing logic. There is no functionality change. Signed-off-by: Yonghong Song <[email protected]>
1 parent 6a86137 commit ad5926a

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,46 @@
1616
#define STACK_SIZE (1024 * 1024)
1717
static char child_stack[STACK_SIZE];
1818

19-
static int test_current_pid_tgid(void *args)
19+
static int get_pid_tgid(pid_t *pid, pid_t *tgid,
20+
struct test_ns_current_pid_tgid__bss *bss)
2021
{
21-
struct test_ns_current_pid_tgid__bss *bss;
22-
struct test_ns_current_pid_tgid *skel;
23-
int ret = -1, err;
24-
pid_t tgid, pid;
2522
struct stat st;
23+
int err;
2624

27-
skel = test_ns_current_pid_tgid__open_and_load();
28-
if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open_and_load"))
29-
goto out;
30-
31-
pid = syscall(SYS_gettid);
32-
tgid = getpid();
25+
*pid = syscall(SYS_gettid);
26+
*tgid = getpid();
3327

3428
err = stat("/proc/self/ns/pid", &st);
3529
if (!ASSERT_OK(err, "stat /proc/self/ns/pid"))
36-
goto cleanup;
30+
return err;
3731

38-
bss = skel->bss;
3932
bss->dev = st.st_dev;
4033
bss->ino = st.st_ino;
4134
bss->user_pid = 0;
4235
bss->user_tgid = 0;
36+
return 0;
37+
}
38+
39+
static int test_current_pid_tgid_tp(void *args)
40+
{
41+
struct test_ns_current_pid_tgid__bss *bss;
42+
struct test_ns_current_pid_tgid *skel;
43+
int ret = -1, err;
44+
pid_t tgid, pid;
45+
46+
skel = test_ns_current_pid_tgid__open();
47+
if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open"))
48+
return ret;
49+
50+
bpf_program__set_autoload(skel->progs.tp_handler, true);
51+
52+
err = test_ns_current_pid_tgid__load(skel);
53+
if (!ASSERT_OK(err, "test_ns_current_pid_tgid__load"))
54+
goto cleanup;
55+
56+
bss = skel->bss;
57+
if (get_pid_tgid(&pid, &tgid, bss))
58+
goto cleanup;
4359

4460
err = test_ns_current_pid_tgid__attach(skel);
4561
if (!ASSERT_OK(err, "test_ns_current_pid_tgid__attach"))
@@ -55,20 +71,19 @@ static int test_current_pid_tgid(void *args)
5571

5672
cleanup:
5773
test_ns_current_pid_tgid__destroy(skel);
58-
out:
5974
return ret;
6075
}
6176

62-
static void test_ns_current_pid_tgid_new_ns(void)
77+
static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
6378
{
6479
int wstatus;
6580
pid_t cpid;
6681

6782
/* Create a process in a new namespace, this process
6883
* will be the init process of this new namespace hence will be pid 1.
6984
*/
70-
cpid = clone(test_current_pid_tgid, child_stack + STACK_SIZE,
71-
CLONE_NEWPID | SIGCHLD, NULL);
85+
cpid = clone(fn, child_stack + STACK_SIZE,
86+
CLONE_NEWPID | SIGCHLD, arg);
7287

7388
if (!ASSERT_NEQ(cpid, -1, "clone"))
7489
return;
@@ -84,7 +99,7 @@ static void test_ns_current_pid_tgid_new_ns(void)
8499
void serial_test_ns_current_pid_tgid(void)
85100
{
86101
if (test__start_subtest("root_ns_tp"))
87-
test_current_pid_tgid(NULL);
102+
test_current_pid_tgid_tp(NULL);
88103
if (test__start_subtest("new_ns_tp"))
89-
test_ns_current_pid_tgid_new_ns();
104+
test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_tp, NULL);
90105
}

tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ __u64 user_tgid = 0;
1010
__u64 dev = 0;
1111
__u64 ino = 0;
1212

13-
SEC("tracepoint/syscalls/sys_enter_nanosleep")
14-
int handler(const void *ctx)
13+
static void get_pid_tgid(void)
1514
{
1615
struct bpf_pidns_info nsdata;
1716

1817
if (bpf_get_ns_current_pid_tgid(dev, ino, &nsdata, sizeof(struct bpf_pidns_info)))
19-
return 0;
18+
return;
2019

2120
user_pid = nsdata.pid;
2221
user_tgid = nsdata.tgid;
22+
}
2323

24+
SEC("?tracepoint/syscalls/sys_enter_nanosleep")
25+
int tp_handler(const void *ctx)
26+
{
27+
get_pid_tgid();
2428
return 0;
2529
}
2630

0 commit comments

Comments
 (0)