tools/testing/selftests/bpf/progs/rcu_read_lock.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/rcu_read_lock.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/progs/rcu_read_lock.c- Extension
.c- Size
- 12041 bytes
- Lines
- 552
- 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.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- 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
vmlinux.hbpf/bpf_helpers.hbpf/bpf_tracing.hbpf_tracing_net.hbpf_misc.h
Detected Declarations
function get_cgroup_idfunction task_succfunction no_lockfunction two_regionsfunction non_sleepable_1function non_sleepable_2function task_acquirefunction miss_lockfunction miss_unlockfunction non_sleepable_rcu_mismatchfunction inproper_sleepable_helperfunction BPF_PROGfunction nested_rcu_regionfunction nested_rcu_region_unbalanced_1function nested_rcu_region_unbalanced_2function task_trusted_non_rcuptrfunction task_untrusted_rcuptrfunction cross_rcu_regionfunction static_subprogfunction global_subprogfunction static_subprog_lockfunction global_subprog_lockfunction static_subprog_unlockfunction global_subprog_unlockfunction rcu_read_lock_subprogfunction rcu_read_lock_global_subprogfunction rcu_read_lock_subprog_lockfunction rcu_read_lock_global_subprog_lockfunction rcu_read_lock_subprog_unlockfunction rcu_read_lock_global_subprog_unlockfunction global_sleepable_helper_subprogfunction global_sleepable_kfunc_subprogfunction global_subprog_calling_sleepable_globalfunction rcu_read_lock_sleepable_helper_global_subprogfunction rcu_read_lock_sleepable_kfunc_global_subprogfunction rcu_read_lock_sleepable_global_subprog_indirect
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_tracing_net.h"
#include "bpf_misc.h"
/* clang considers 'sum += 1' as usage but 'sum++' as non-usage. GCC
* is more consistent and considers both 'sum += 1' and 'sum++' as
* non-usage. This triggers warnings in the functions below.
*
* Starting with GCC 16 -Wunused-but-set-variable=2 can be used to
* mimic clang's behavior. */
#if !defined(__clang__) && __GNUC__ > 15
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#endif
char _license[] SEC("license") = "GPL";
struct {
__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
__uint(map_flags, BPF_F_NO_PREALLOC);
__type(key, int);
__type(value, long);
} map_a SEC(".maps");
__u32 user_data, target_pid;
__s32 key_serial;
__u64 flags, task_storage_val, cgroup_id;
struct bpf_key *bpf_lookup_user_key(__s32 serial, __u64 flags) __ksym;
void bpf_key_put(struct bpf_key *key) __ksym;
void bpf_rcu_read_lock(void) __ksym;
void bpf_rcu_read_unlock(void) __ksym;
struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
void bpf_task_release(struct task_struct *p) __ksym;
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
int get_cgroup_id(void *ctx)
{
struct task_struct *task;
struct css_set *cgroups;
task = bpf_get_current_task_btf();
if (task->pid != target_pid)
return 0;
/* simulate bpf_get_current_cgroup_id() helper */
bpf_rcu_read_lock();
cgroups = task->cgroups;
if (!cgroups)
goto unlock;
cgroup_id = cgroups->dfl_cgrp->kn->id;
unlock:
bpf_rcu_read_unlock();
return 0;
}
SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
int task_succ(void *ctx)
{
struct task_struct *task, *real_parent;
long init_val = 2;
long *ptr;
task = bpf_get_current_task_btf();
if (task->pid != target_pid)
return 0;
bpf_rcu_read_lock();
/* region including helper using rcu ptr real_parent */
real_parent = task->real_parent;
if (!real_parent)
goto out;
ptr = bpf_task_storage_get(&map_a, real_parent, &init_val,
BPF_LOCAL_STORAGE_GET_F_CREATE);
if (!ptr)
goto out;
ptr = bpf_task_storage_get(&map_a, real_parent, 0, 0);
if (!ptr)
goto out;
task_storage_val = *ptr;
out:
bpf_rcu_read_unlock();
return 0;
}
SEC("?fentry.s/" SYS_PREFIX "sys_nanosleep")
Annotation
- Immediate include surface: `vmlinux.h`, `bpf/bpf_helpers.h`, `bpf/bpf_tracing.h`, `bpf_tracing_net.h`, `bpf_misc.h`.
- Detected declarations: `function get_cgroup_id`, `function task_succ`, `function no_lock`, `function two_regions`, `function non_sleepable_1`, `function non_sleepable_2`, `function task_acquire`, `function miss_lock`, `function miss_unlock`, `function non_sleepable_rcu_mismatch`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.