mm/shrinker_debug.c
Source file repositories/reference/linux-study-clean/mm/shrinker_debug.c
File Facts
- System
- Linux kernel
- Corpus path
mm/shrinker_debug.c- Extension
.c- Size
- 5980 bytes
- Lines
- 273
- Domain
- Core OS
- Bucket
- Memory Management
- Inferred role
- Core OS: operation-table or driver-model contract
- Status
- pattern 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 an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- 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
linux/idr.hlinux/slab.hlinux/debugfs.hlinux/seq_file.hlinux/shrinker.hlinux/memcontrol.hinternal.h
Detected Declarations
function shrinker_count_objectsfunction for_each_nodefunction shrinker_debugfs_count_showfunction shrinker_debugfs_scan_openfunction shrinker_debugfs_scan_writefunction shrinker_debugfs_addfunction shrinker_debugfs_renamefunction shrinker_debugfs_removefunction shrinker_debugfs_initexport shrinker_debugfs_rename
Annotated Snippet
static const struct file_operations shrinker_debugfs_scan_fops = {
.owner = THIS_MODULE,
.open = shrinker_debugfs_scan_open,
.write = shrinker_debugfs_scan_write,
};
int shrinker_debugfs_add(struct shrinker *shrinker)
{
struct dentry *entry;
char buf[128];
int id;
lockdep_assert_held(&shrinker_mutex);
/* debugfs isn't initialized yet, add debugfs entries later. */
if (!shrinker_debugfs_root)
return 0;
id = ida_alloc(&shrinker_debugfs_ida, GFP_KERNEL);
if (id < 0)
return id;
shrinker->debugfs_id = id;
snprintf(buf, sizeof(buf), "%s-%d", shrinker->name, id);
/* create debugfs entry */
entry = debugfs_create_dir(buf, shrinker_debugfs_root);
if (IS_ERR(entry)) {
ida_free(&shrinker_debugfs_ida, id);
return PTR_ERR(entry);
}
shrinker->debugfs_entry = entry;
debugfs_create_file("count", 0440, entry, shrinker,
&shrinker_debugfs_count_fops);
debugfs_create_file("scan", 0220, entry, shrinker,
&shrinker_debugfs_scan_fops);
return 0;
}
int shrinker_debugfs_rename(struct shrinker *shrinker, const char *fmt, ...)
{
const char *new, *old;
va_list ap;
int ret = 0;
va_start(ap, fmt);
new = kvasprintf_const(GFP_KERNEL, fmt, ap);
va_end(ap);
if (!new)
return -ENOMEM;
mutex_lock(&shrinker_mutex);
old = shrinker->name;
shrinker->name = new;
ret = debugfs_change_name(shrinker->debugfs_entry, "%s-%d",
shrinker->name, shrinker->debugfs_id);
if (ret) {
shrinker->name = old;
kfree_const(new);
} else {
kfree_const(old);
}
mutex_unlock(&shrinker_mutex);
return ret;
}
EXPORT_SYMBOL(shrinker_debugfs_rename);
struct dentry *shrinker_debugfs_detach(struct shrinker *shrinker,
int *debugfs_id)
{
struct dentry *entry = shrinker->debugfs_entry;
lockdep_assert_held(&shrinker_mutex);
*debugfs_id = entry ? shrinker->debugfs_id : -1;
shrinker->debugfs_entry = NULL;
return entry;
}
void shrinker_debugfs_remove(struct dentry *debugfs_entry, int debugfs_id)
{
debugfs_remove_recursive(debugfs_entry);
ida_free(&shrinker_debugfs_ida, debugfs_id);
Annotation
- Immediate include surface: `linux/idr.h`, `linux/slab.h`, `linux/debugfs.h`, `linux/seq_file.h`, `linux/shrinker.h`, `linux/memcontrol.h`, `internal.h`.
- Detected declarations: `function shrinker_count_objects`, `function for_each_node`, `function shrinker_debugfs_count_show`, `function shrinker_debugfs_scan_open`, `function shrinker_debugfs_scan_write`, `function shrinker_debugfs_add`, `function shrinker_debugfs_rename`, `function shrinker_debugfs_remove`, `function shrinker_debugfs_init`, `export shrinker_debugfs_rename`.
- Atlas domain: Core OS / Memory Management.
- Implementation status: pattern 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.