Commit e04ddf17 authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

Merge branch 'Introduce a new kfunc of bpf_task_under_cgroup'



Feng zhou says:

====================
Trace sched related functions, such as enqueue_task_fair, it is necessary to
specify a task instead of the current task which within a given cgroup.
====================

Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents f4dea968 49e0263a
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -2149,6 +2149,22 @@ __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid)
		return NULL;
	return cgrp;
}

/**
 * bpf_task_under_cgroup - wrap task_under_cgroup_hierarchy() as a kfunc, test
 * task's membership of cgroup ancestry.
 * @task: the task to be tested
 * @ancestor: possible ancestor of @task's cgroup
 *
 * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor.
 * It follows all the same rules as cgroup_is_descendant, and only applies
 * to the default hierarchy.
 */
__bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task,
				       struct cgroup *ancestor)
{
	return task_under_cgroup_hierarchy(task, ancestor);
}
#endif /* CONFIG_CGROUPS */

/**
@@ -2400,6 +2416,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE)
BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU)
#endif
BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL)
BTF_SET8_END(generic_btf_ids)
+1 −0
Original line number Diff line number Diff line
@@ -26,3 +26,4 @@ user_ringbuf # failed to find kernel BTF type ID of
verif_stats                              # trace_vprintk__open_and_load unexpected error: -9                           (?)
xdp_bonding                              # failed to auto-attach program 'trace_on_entry': -524                        (trampoline)
xdp_metadata                             # JIT does not support calling kernel function                                (kfunc)
test_task_under_cgroup                   # JIT does not support calling kernel function                                (kfunc)
+53 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Bytedance */

#include <sys/syscall.h>
#include <test_progs.h>
#include <cgroup_helpers.h>
#include "test_task_under_cgroup.skel.h"

#define FOO	"/foo"

void test_task_under_cgroup(void)
{
	struct test_task_under_cgroup *skel;
	int ret, foo;
	pid_t pid;

	foo = test__join_cgroup(FOO);
	if (!ASSERT_OK(foo < 0, "cgroup_join_foo"))
		return;

	skel = test_task_under_cgroup__open();
	if (!ASSERT_OK_PTR(skel, "test_task_under_cgroup__open"))
		goto cleanup;

	skel->rodata->local_pid = getpid();
	skel->bss->remote_pid = getpid();
	skel->rodata->cgid = get_cgroup_id(FOO);

	ret = test_task_under_cgroup__load(skel);
	if (!ASSERT_OK(ret, "test_task_under_cgroup__load"))
		goto cleanup;

	ret = test_task_under_cgroup__attach(skel);
	if (!ASSERT_OK(ret, "test_task_under_cgroup__attach"))
		goto cleanup;

	pid = fork();
	if (pid == 0)
		exit(0);

	ret = (pid == -1);
	if (ASSERT_OK(ret, "fork process"))
		wait(NULL);

	test_task_under_cgroup__detach(skel);

	ASSERT_NEQ(skel->bss->remote_pid, skel->rodata->local_pid,
		   "test task_under_cgroup");

cleanup:
	test_task_under_cgroup__destroy(skel);
	close(foo);
}
+51 −0
Original line number Diff line number Diff line
// 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";