kernel/bpf/task_iter.c
Source file repositories/reference/linux-study-clean/kernel/bpf/task_iter.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/bpf/task_iter.c- Extension
.c- Size
- 29254 bytes
- Lines
- 1192
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/init.hlinux/namei.hlinux/pid_namespace.hlinux/fs.hlinux/filter.hlinux/bpf_mem_alloc.hlinux/btf_ids.hlinux/mm_types.hlinux/mmap_lock.hlinux/sched/mm.hmmap_unlock_work.h
Detected Declarations
struct bpf_iter_seq_task_commonstruct bpf_iter_seq_task_infostruct bpf_iter__taskstruct bpf_iter_seq_task_file_infostruct bpf_iter__task_filestruct bpf_iter_seq_task_vma_infostruct bpf_iter__task_vmastruct bpf_iter_task_vma_kern_datastruct bpf_iter_task_vmastruct bpf_iter_task_vma_kernstruct bpf_iter_css_taskstruct bpf_iter_css_task_kernstruct bpf_iter_taskstruct bpf_iter_task_kernenum bpf_task_vma_iter_find_opfunction task_seq_startfunction __task_seq_showfunction task_seq_showfunction task_seq_stopfunction bpf_iter_attach_taskfunction task_file_seq_get_nextfunction __task_file_seq_showfunction task_file_seq_showfunction task_file_seq_stopfunction init_seq_pidnsfunction fini_seq_pidnsfunction task_vma_seq_get_nextfunction __task_vma_seq_showfunction task_vma_seq_showfunction task_vma_seq_stopfunction bpf_iter_fill_link_infofunction bpf_iter_task_show_fdinfofunction bpf_iter_mmput_asyncfunction bpf_iter_task_vma_newfunction _nextfunction get_task_mmfunction lock_vma_under_rcufunction bpf_iter_task_vma_snapshot_resetfunction bpf_iter_task_vma_destroyfunction bpf_iter_css_task_newfunction bpf_iter_css_task_destroyfunction bpf_iter_task_newfunction bpf_iter_task_destroyfunction do_mmap_read_unlockfunction task_iter_initfunction for_each_possible_cpu
Annotated Snippet
struct bpf_iter_seq_task_common {
struct pid_namespace *ns;
enum bpf_iter_task_type type;
u32 pid;
u32 pid_visiting;
};
struct bpf_iter_seq_task_info {
/* The first field must be struct bpf_iter_seq_task_common.
* this is assumed by {init, fini}_seq_pidns() callback functions.
*/
struct bpf_iter_seq_task_common common;
u32 tid;
};
static struct task_struct *task_group_seq_get_next(struct bpf_iter_seq_task_common *common,
u32 *tid,
bool skip_if_dup_files)
{
struct task_struct *task;
struct pid *pid;
u32 next_tid;
if (!*tid) {
/* The first time, the iterator calls this function. */
pid = find_pid_ns(common->pid, common->ns);
task = get_pid_task(pid, PIDTYPE_TGID);
if (!task)
return NULL;
*tid = common->pid;
common->pid_visiting = common->pid;
return task;
}
/* If the control returns to user space and comes back to the
* kernel again, *tid and common->pid_visiting should be the
* same for task_seq_start() to pick up the correct task.
*/
if (*tid == common->pid_visiting) {
pid = find_pid_ns(common->pid_visiting, common->ns);
task = get_pid_task(pid, PIDTYPE_PID);
return task;
}
task = find_task_by_pid_ns(common->pid_visiting, common->ns);
if (!task)
return NULL;
retry:
task = __next_thread(task);
if (!task)
return NULL;
next_tid = __task_pid_nr_ns(task, PIDTYPE_PID, common->ns);
if (!next_tid)
goto retry;
if (skip_if_dup_files && task->files == task->group_leader->files)
goto retry;
*tid = common->pid_visiting = next_tid;
get_task_struct(task);
return task;
}
static struct task_struct *task_seq_get_next(struct bpf_iter_seq_task_common *common,
u32 *tid,
bool skip_if_dup_files)
{
struct task_struct *task = NULL;
struct pid *pid;
if (common->type == BPF_TASK_ITER_TID) {
if (*tid && *tid != common->pid)
return NULL;
rcu_read_lock();
pid = find_pid_ns(common->pid, common->ns);
if (pid) {
task = get_pid_task(pid, PIDTYPE_PID);
*tid = common->pid;
}
rcu_read_unlock();
return task;
}
if (common->type == BPF_TASK_ITER_TGID) {
Annotation
- Immediate include surface: `linux/init.h`, `linux/namei.h`, `linux/pid_namespace.h`, `linux/fs.h`, `linux/filter.h`, `linux/bpf_mem_alloc.h`, `linux/btf_ids.h`, `linux/mm_types.h`.
- Detected declarations: `struct bpf_iter_seq_task_common`, `struct bpf_iter_seq_task_info`, `struct bpf_iter__task`, `struct bpf_iter_seq_task_file_info`, `struct bpf_iter__task_file`, `struct bpf_iter_seq_task_vma_info`, `struct bpf_iter__task_vma`, `struct bpf_iter_task_vma_kern_data`, `struct bpf_iter_task_vma`, `struct bpf_iter_task_vma_kern`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.