Commit 3c5864ba authored by Chuyi Zhou's avatar Chuyi Zhou Committed by Martin KaFai Lau
Browse files

selftests/bpf: get trusted cgrp from bpf_iter__cgroup directly



Commit f49843af (selftests/bpf: Add tests for css_task iter combining
with cgroup iter) added a test which demonstrates how css_task iter can be
combined with cgroup iter. That test used bpf_cgroup_from_id() to convert
bpf_iter__cgroup->cgroup to a trusted ptr which is pointless now, since
with the previous fix, we can get a trusted cgroup directly from
bpf_iter__cgroup.

Signed-off-by: default avatarChuyi Zhou <zhouchuyi@bytedance.com>
Acked-by: default avatarYonghong Song <yonghong.song@linux.dev>
Link: https://lore.kernel.org/r/20231107132204.912120-3-zhouchuyi@bytedance.com


Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parent 0de4f50d
Loading
Loading
Loading
Loading
+4 −12
Original line number Diff line number Diff line
@@ -56,12 +56,9 @@ SEC("?iter/cgroup")
int cgroup_id_printer(struct bpf_iter__cgroup *ctx)
{
	struct seq_file *seq = ctx->meta->seq;
	struct cgroup *cgrp, *acquired;
	struct cgroup *cgrp = ctx->cgroup;
	struct cgroup_subsys_state *css;
	struct task_struct *task;
	u64 cgrp_id;

	cgrp = ctx->cgroup;

	/* epilogue */
	if (cgrp == NULL) {
@@ -73,20 +70,15 @@ int cgroup_id_printer(struct bpf_iter__cgroup *ctx)
	if (ctx->meta->seq_num == 0)
		BPF_SEQ_PRINTF(seq, "prologue\n");

	cgrp_id = cgroup_id(cgrp);

	BPF_SEQ_PRINTF(seq, "%8llu\n", cgrp_id);
	BPF_SEQ_PRINTF(seq, "%8llu\n", cgroup_id(cgrp));

	acquired = bpf_cgroup_from_id(cgrp_id);
	if (!acquired)
		return 0;
	css = &acquired->self;
	css = &cgrp->self;
	css_task_cnt = 0;
	bpf_for_each(css_task, task, css, CSS_TASK_ITER_PROCS) {
		if (task->pid == target_pid)
			css_task_cnt++;
	}
	bpf_cgroup_release(acquired);

	return 0;
}