kernel/module/tree_lookup.c
Source file repositories/reference/linux-study-clean/kernel/module/tree_lookup.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/module/tree_lookup.c- Extension
.c- Size
- 2696 bytes
- Lines
- 113
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/module.hlinux/rbtree_latch.hinternal.h
Detected Declarations
function __mod_tree_valfunction __mod_tree_sizefunction mod_tree_lessfunction mod_tree_compfunction __mod_tree_insertfunction __mod_tree_removefunction mod_tree_insertfunction mod_tree_remove_initfunction mod_tree_remove
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Modules tree lookup
*
* Copyright (C) 2015 Peter Zijlstra
* Copyright (C) 2015 Rusty Russell
*/
#include <linux/module.h>
#include <linux/rbtree_latch.h>
#include "internal.h"
/*
* Use a latched RB-tree for __module_address(); this allows us to use
* RCU lookups of the address from any context.
*
* This is conditional on PERF_EVENTS || TRACING || CFI because those can
* really hit __module_address() hard by doing a lot of stack unwinding;
* potentially from NMI context.
*/
static __always_inline unsigned long __mod_tree_val(struct latch_tree_node *n)
{
struct module_memory *mod_mem = container_of(n, struct module_memory, mtn.node);
return (unsigned long)mod_mem->base;
}
static __always_inline unsigned long __mod_tree_size(struct latch_tree_node *n)
{
struct module_memory *mod_mem = container_of(n, struct module_memory, mtn.node);
return (unsigned long)mod_mem->size;
}
static __always_inline bool
mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b)
{
return __mod_tree_val(a) < __mod_tree_val(b);
}
static __always_inline int
mod_tree_comp(void *key, struct latch_tree_node *n)
{
unsigned long val = (unsigned long)key;
unsigned long start, end;
start = __mod_tree_val(n);
if (val < start)
return -1;
end = start + __mod_tree_size(n);
if (val >= end)
return 1;
return 0;
}
static const struct latch_tree_ops mod_tree_ops = {
.less = mod_tree_less,
.comp = mod_tree_comp,
};
static noinline void __mod_tree_insert(struct mod_tree_node *node, struct mod_tree_root *tree)
{
latch_tree_insert(&node->node, &tree->root, &mod_tree_ops);
}
static void __mod_tree_remove(struct mod_tree_node *node, struct mod_tree_root *tree)
{
latch_tree_erase(&node->node, &tree->root, &mod_tree_ops);
}
/*
* These modifications: insert, remove_init and remove; are serialized by the
* module_mutex.
*/
void mod_tree_insert(struct module *mod)
{
for_each_mod_mem_type(type) {
mod->mem[type].mtn.mod = mod;
if (mod->mem[type].size)
__mod_tree_insert(&mod->mem[type].mtn, &mod_tree);
}
}
void mod_tree_remove_init(struct module *mod)
{
for_class_mod_mem_type(type, init) {
if (mod->mem[type].size)
Annotation
- Immediate include surface: `linux/module.h`, `linux/rbtree_latch.h`, `internal.h`.
- Detected declarations: `function __mod_tree_val`, `function __mod_tree_size`, `function mod_tree_less`, `function mod_tree_comp`, `function __mod_tree_insert`, `function __mod_tree_remove`, `function mod_tree_insert`, `function mod_tree_remove_init`, `function mod_tree_remove`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- 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.