mirror of
https://github.com/nxp-imx/linux-imx.git
synced 2025-07-07 09:55:19 +02:00
selftests/bpf: Refactor out some functions in ns_current_pid_tgid test
[ Upstream commit4d4bd29e36
] 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 <yonghong.song@linux.dev> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20240315184904.2976123-1-yonghong.song@linux.dev Stable-dep-of:21f0b0af97
("selftests/bpf: Fix include of <sys/fcntl.h>") Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
parent
d6e16c33e0
commit
17536f3b72
|
@ -16,30 +16,46 @@
|
||||||
#define STACK_SIZE (1024 * 1024)
|
#define STACK_SIZE (1024 * 1024)
|
||||||
static char child_stack[STACK_SIZE];
|
static char child_stack[STACK_SIZE];
|
||||||
|
|
||||||
static int test_current_pid_tgid(void *args)
|
static int get_pid_tgid(pid_t *pid, pid_t *tgid,
|
||||||
|
struct test_ns_current_pid_tgid__bss *bss)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
*pid = syscall(SYS_gettid);
|
||||||
|
*tgid = getpid();
|
||||||
|
|
||||||
|
err = stat("/proc/self/ns/pid", &st);
|
||||||
|
if (!ASSERT_OK(err, "stat /proc/self/ns/pid"))
|
||||||
|
return err;
|
||||||
|
|
||||||
|
bss->dev = st.st_dev;
|
||||||
|
bss->ino = st.st_ino;
|
||||||
|
bss->user_pid = 0;
|
||||||
|
bss->user_tgid = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int test_current_pid_tgid_tp(void *args)
|
||||||
{
|
{
|
||||||
struct test_ns_current_pid_tgid__bss *bss;
|
struct test_ns_current_pid_tgid__bss *bss;
|
||||||
struct test_ns_current_pid_tgid *skel;
|
struct test_ns_current_pid_tgid *skel;
|
||||||
int ret = -1, err;
|
int ret = -1, err;
|
||||||
pid_t tgid, pid;
|
pid_t tgid, pid;
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
skel = test_ns_current_pid_tgid__open_and_load();
|
skel = test_ns_current_pid_tgid__open();
|
||||||
if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open_and_load"))
|
if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open"))
|
||||||
goto out;
|
return ret;
|
||||||
|
|
||||||
pid = syscall(SYS_gettid);
|
bpf_program__set_autoload(skel->progs.tp_handler, true);
|
||||||
tgid = getpid();
|
|
||||||
|
|
||||||
err = stat("/proc/self/ns/pid", &st);
|
err = test_ns_current_pid_tgid__load(skel);
|
||||||
if (!ASSERT_OK(err, "stat /proc/self/ns/pid"))
|
if (!ASSERT_OK(err, "test_ns_current_pid_tgid__load"))
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
bss = skel->bss;
|
bss = skel->bss;
|
||||||
bss->dev = st.st_dev;
|
if (get_pid_tgid(&pid, &tgid, bss))
|
||||||
bss->ino = st.st_ino;
|
goto cleanup;
|
||||||
bss->user_pid = 0;
|
|
||||||
bss->user_tgid = 0;
|
|
||||||
|
|
||||||
err = test_ns_current_pid_tgid__attach(skel);
|
err = test_ns_current_pid_tgid__attach(skel);
|
||||||
if (!ASSERT_OK(err, "test_ns_current_pid_tgid__attach"))
|
if (!ASSERT_OK(err, "test_ns_current_pid_tgid__attach"))
|
||||||
|
@ -55,11 +71,10 @@ static int test_current_pid_tgid(void *args)
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
test_ns_current_pid_tgid__destroy(skel);
|
test_ns_current_pid_tgid__destroy(skel);
|
||||||
out:
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_ns_current_pid_tgid_new_ns(void)
|
static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void *arg)
|
||||||
{
|
{
|
||||||
int wstatus;
|
int wstatus;
|
||||||
pid_t cpid;
|
pid_t cpid;
|
||||||
|
@ -67,8 +82,8 @@ static void test_ns_current_pid_tgid_new_ns(void)
|
||||||
/* Create a process in a new namespace, this process
|
/* Create a process in a new namespace, this process
|
||||||
* will be the init process of this new namespace hence will be pid 1.
|
* will be the init process of this new namespace hence will be pid 1.
|
||||||
*/
|
*/
|
||||||
cpid = clone(test_current_pid_tgid, child_stack + STACK_SIZE,
|
cpid = clone(fn, child_stack + STACK_SIZE,
|
||||||
CLONE_NEWPID | SIGCHLD, NULL);
|
CLONE_NEWPID | SIGCHLD, arg);
|
||||||
|
|
||||||
if (!ASSERT_NEQ(cpid, -1, "clone"))
|
if (!ASSERT_NEQ(cpid, -1, "clone"))
|
||||||
return;
|
return;
|
||||||
|
@ -84,7 +99,7 @@ static void test_ns_current_pid_tgid_new_ns(void)
|
||||||
void serial_test_ns_current_pid_tgid(void)
|
void serial_test_ns_current_pid_tgid(void)
|
||||||
{
|
{
|
||||||
if (test__start_subtest("root_ns_tp"))
|
if (test__start_subtest("root_ns_tp"))
|
||||||
test_current_pid_tgid(NULL);
|
test_current_pid_tgid_tp(NULL);
|
||||||
if (test__start_subtest("new_ns_tp"))
|
if (test__start_subtest("new_ns_tp"))
|
||||||
test_ns_current_pid_tgid_new_ns();
|
test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_tp, NULL);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,21 @@ __u64 user_tgid = 0;
|
||||||
__u64 dev = 0;
|
__u64 dev = 0;
|
||||||
__u64 ino = 0;
|
__u64 ino = 0;
|
||||||
|
|
||||||
SEC("tracepoint/syscalls/sys_enter_nanosleep")
|
static void get_pid_tgid(void)
|
||||||
int handler(const void *ctx)
|
|
||||||
{
|
{
|
||||||
struct bpf_pidns_info nsdata;
|
struct bpf_pidns_info nsdata;
|
||||||
|
|
||||||
if (bpf_get_ns_current_pid_tgid(dev, ino, &nsdata, sizeof(struct bpf_pidns_info)))
|
if (bpf_get_ns_current_pid_tgid(dev, ino, &nsdata, sizeof(struct bpf_pidns_info)))
|
||||||
return 0;
|
return;
|
||||||
|
|
||||||
user_pid = nsdata.pid;
|
user_pid = nsdata.pid;
|
||||||
user_tgid = nsdata.tgid;
|
user_tgid = nsdata.tgid;
|
||||||
|
}
|
||||||
|
|
||||||
|
SEC("?tracepoint/syscalls/sys_enter_nanosleep")
|
||||||
|
int tp_handler(const void *ctx)
|
||||||
|
{
|
||||||
|
get_pid_tgid();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user