tools/sched_ext/scx_qmap.bpf.c
Source file repositories/reference/linux-study-clean/tools/sched_ext/scx_qmap.bpf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/sched_ext/scx_qmap.bpf.c- Extension
.c- Size
- 34910 bytes
- Lines
- 1242
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
scx/common.bpf.hscx_qmap.h
Detected Declarations
struct task_ctxstruct task_ctx_stor_valstruct monitor_timerstruct lowpri_timerenum constsfunction qmap_spin_lockfunction pick_direct_dispatch_cidfunction qmap_fifo_enqueuefunction qmap_fifo_removefunction BPF_STRUCT_OPSfunction weight_to_idxfunction BPF_STRUCT_OPSfunction qmap_select_cidfunction BPF_STRUCT_OPSfunction update_core_sched_head_seqfunction scx_bpf_dsq_movefunction BPF_STRUCT_OPSfunction bpf_repeatfunction BPF_STRUCT_OPSfunction task_qdistfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPS_SLEEPABLEfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction bpf_repeatfunction bpf_repeatfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction monitor_cpuperffunction bpf_forfunction scx_bpf_dsq_nr_queuedfunction monitor_timerfnfunction lowpri_timerfnfunction BPF_STRUCT_OPS_SLEEPABLEfunction spacefunction bpf_forfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPSfunction BPF_STRUCT_OPS
Annotated Snippet
struct task_ctx {
struct task_ctx __arena *next_free; /* only valid on free list */
struct task_ctx __arena *q_next; /* queue link, NULL if tail */
struct task_ctx __arena *q_prev; /* queue link, NULL if head */
struct qmap_fifo __arena *fifo; /* queue we're on, NULL if not queued */
u64 tid;
s32 pid; /* for dump only */
bool force_local; /* Dispatch directly to local_dsq */
bool highpri;
u64 core_sched_seq;
struct scx_cmask cpus_allowed; /* per-task affinity in cid space */
};
/*
* Slab stride for task_ctx. cpus_allowed's flex array bits[] overlaps the
* tail bytes appended per entry; struct_size() gives the actual per-entry
* footprint.
*/
#define TASK_CTX_STRIDE \
struct_size_t(struct task_ctx, cpus_allowed.bits, \
CMASK_NR_WORDS(SCX_QMAP_MAX_CPUS))
/* All task_ctx pointers are arena pointers. */
typedef struct task_ctx __arena task_ctx_t;
/* Holds an arena pointer to the task's slab entry. */
struct task_ctx_stor_val {
task_ctx_t *taskc;
};
struct {
__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
__uint(map_flags, BPF_F_NO_PREALLOC);
__type(key, int);
__type(value, struct task_ctx_stor_val);
} task_ctx_stor SEC(".maps");
/* Protects the task_ctx slab free list. */
__hidden struct bpf_res_spin_lock qa_task_lock SEC(".data.qa_task_lock");
static int qmap_spin_lock(struct bpf_res_spin_lock *lock)
{
if (bpf_res_spin_lock(lock)) {
scx_bpf_error("res_spin_lock failed");
return -EBUSY;
}
return 0;
}
/*
* Try prev_cid, then scan taskc->cpus_allowed AND qa_idle_cids round-robin
* from prev_cid + 1. Atomic claim retries on race; bounded by
* IDLE_PICK_RETRIES to keep the verifier's insn budget in check.
*/
#define IDLE_PICK_RETRIES 16
static s32 pick_direct_dispatch_cid(struct task_struct *p, s32 prev_cid,
task_ctx_t *taskc)
{
u32 nr_cids = scx_bpf_nr_cids();
s32 cid;
u32 i;
if (!always_enq_immed && p->nr_cpus_allowed == 1)
return prev_cid;
if (cmask_test_and_clear(prev_cid, qa_idle_cids))
return prev_cid;
cid = prev_cid;
bpf_for(i, 0, IDLE_PICK_RETRIES) {
cid = cmask_next_and_set_wrap(&taskc->cpus_allowed,
qa_idle_cids, cid + 1);
barrier_var(cid);
if (cid >= nr_cids)
return -1;
if (cmask_test_and_clear(cid, qa_idle_cids))
return cid;
}
return -1;
}
/*
* Force a reference to the arena map. The verifier associates an arena with
* a program by finding an LD_IMM64 instruction that loads the arena's BPF
* map; programs that only use arena pointers returned from task-local
* storage (like qmap_select_cpu) never reference @arena directly. Without
* this, the verifier rejects addr_space_cast with "addr_space_cast insn
* can only be used in a program that has an associated arena".
*/
Annotation
- Immediate include surface: `scx/common.bpf.h`, `scx_qmap.h`.
- Detected declarations: `struct task_ctx`, `struct task_ctx_stor_val`, `struct monitor_timer`, `struct lowpri_timer`, `enum consts`, `function qmap_spin_lock`, `function pick_direct_dispatch_cid`, `function qmap_fifo_enqueue`, `function qmap_fifo_remove`, `function BPF_STRUCT_OPS`.
- Atlas domain: Support Tooling And Documentation / tools.
- 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.