tools/testing/selftests/bpf/progs/iters_css_task.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/iters_css_task.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/iters_css_task.c- Extension
.c- Size
- 2182 bytes
- Lines
- 103
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
vmlinux.herrno.hbpf/bpf_helpers.hbpf/bpf_tracing.hbpf_misc.hbpf_experimental.h
Detected Declarations
function BPF_PROGfunction cgroup_idfunction cgroup_id_printerfunction BPF_PROGfunction bpf_for_each
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2023 Chuyi Zhou <zhouchuyi@bytedance.com> */
#include "vmlinux.h"
#include <errno.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
#include "bpf_experimental.h"
char _license[] SEC("license") = "GPL";
struct cgroup *bpf_cgroup_acquire(struct cgroup *p) __ksym;
struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
void bpf_cgroup_release(struct cgroup *p) __ksym;
pid_t target_pid;
int css_task_cnt;
u64 cg_id;
SEC("lsm/file_mprotect")
int BPF_PROG(iter_css_task_for_each, struct vm_area_struct *vma,
unsigned long reqprot, unsigned long prot, int ret)
{
struct task_struct *cur_task = bpf_get_current_task_btf();
struct cgroup_subsys_state *css;
struct task_struct *task;
struct cgroup *cgrp;
if (cur_task->pid != target_pid)
return ret;
cgrp = bpf_cgroup_from_id(cg_id);
if (!cgrp)
return -EPERM;
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(cgrp);
return -EPERM;
}
static inline u64 cgroup_id(struct cgroup *cgrp)
{
return cgrp->kn->id;
}
SEC("?iter/cgroup")
int cgroup_id_printer(struct bpf_iter__cgroup *ctx)
{
struct seq_file *seq = ctx->meta->seq;
struct cgroup *cgrp = ctx->cgroup;
struct cgroup_subsys_state *css;
struct task_struct *task;
/* epilogue */
if (cgrp == NULL) {
BPF_SEQ_PRINTF(seq, "epilogue\n");
return 0;
}
/* prologue */
if (ctx->meta->seq_num == 0)
BPF_SEQ_PRINTF(seq, "prologue\n");
BPF_SEQ_PRINTF(seq, "%8llu\n", cgroup_id(cgrp));
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++;
}
return 0;
}
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
int BPF_PROG(iter_css_task_for_each_sleep)
{
u64 cgrp_id = bpf_get_current_cgroup_id();
struct cgroup *cgrp = bpf_cgroup_from_id(cgrp_id);
struct cgroup_subsys_state *css;
Annotation
- Immediate include surface: `vmlinux.h`, `errno.h`, `bpf/bpf_helpers.h`, `bpf/bpf_tracing.h`, `bpf_misc.h`, `bpf_experimental.h`.
- Detected declarations: `function BPF_PROG`, `function cgroup_id`, `function cgroup_id_printer`, `function BPF_PROG`, `function bpf_for_each`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.