test_progs: Tests new kfunc bpf_task_under_cgroup(). The bpf program saves the new task's pid within a given cgroup to the remote_pid, which is convenient for the user-mode program to verify the test correctness. The user-mode program creates its own mount namespace, and mounts the cgroupsv2 hierarchy in there, call the fork syscall, then check if remote_pid and local_pid are unequal. Signed-off-by: Feng Zhou <zhoufeng.zf@bytedance.com> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/r/20230506031545.35991-3-zhoufeng.zf@bytedance.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
51 lines
1.1 KiB
C
51 lines
1.1 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Copyright (c) 2023 Bytedance */
|
|
|
|
#include <vmlinux.h>
|
|
#include <bpf/bpf_tracing.h>
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
#include "bpf_misc.h"
|
|
|
|
struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
|
|
long bpf_task_under_cgroup(struct task_struct *task, struct cgroup *ancestor) __ksym;
|
|
void bpf_cgroup_release(struct cgroup *p) __ksym;
|
|
struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
|
|
void bpf_task_release(struct task_struct *p) __ksym;
|
|
|
|
const volatile int local_pid;
|
|
const volatile __u64 cgid;
|
|
int remote_pid;
|
|
|
|
SEC("tp_btf/task_newtask")
|
|
int BPF_PROG(handle__task_newtask, struct task_struct *task, u64 clone_flags)
|
|
{
|
|
struct cgroup *cgrp = NULL;
|
|
struct task_struct *acquired;
|
|
|
|
if (local_pid != (bpf_get_current_pid_tgid() >> 32))
|
|
return 0;
|
|
|
|
acquired = bpf_task_acquire(task);
|
|
if (!acquired)
|
|
return 0;
|
|
|
|
if (local_pid == acquired->tgid)
|
|
goto out;
|
|
|
|
cgrp = bpf_cgroup_from_id(cgid);
|
|
if (!cgrp)
|
|
goto out;
|
|
|
|
if (bpf_task_under_cgroup(acquired, cgrp))
|
|
remote_pid = acquired->tgid;
|
|
|
|
out:
|
|
if (cgrp)
|
|
bpf_cgroup_release(cgrp);
|
|
bpf_task_release(acquired);
|
|
|
|
return 0;
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|