kernel/module/sysfs.c
Source file repositories/reference/linux-study-clean/kernel/module/sysfs.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/module/sysfs.c- Extension
.c- Size
- 9940 bytes
- Lines
- 440
- 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.
- 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
linux/module.hlinux/kernel.hlinux/fs.hlinux/sysfs.hlinux/slab.hlinux/kallsyms.hlinux/mutex.hinternal.h
Detected Declarations
struct module_sect_attrsstruct module_notes_attrsfunction module_sect_readfunction free_sect_attrsfunction add_sect_attrsfunction remove_sect_attrsfunction free_notes_attrsfunction add_notes_attrsfunction remove_notes_attrsfunction add_sect_attrsfunction remove_sect_attrsfunction remove_notes_attrsfunction add_usage_linksfunction module_remove_modinfo_attrsfunction module_add_modinfo_attrsfunction mod_kobject_putfunction mod_sysfs_initfunction mod_sysfs_setupfunction mod_sysfs_finifunction mod_sysfs_teardownfunction init_param_lock
Annotated Snippet
struct module_sect_attrs {
struct attribute_group grp;
struct bin_attribute attrs[];
};
#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4))
static ssize_t module_sect_read(struct file *file, struct kobject *kobj,
const struct bin_attribute *battr,
char *buf, loff_t pos, size_t count)
{
char bounce[MODULE_SECT_READ_SIZE + 1];
size_t wrote;
if (pos != 0)
return -EINVAL;
/*
* Since we're a binary read handler, we must account for the
* trailing NUL byte that sprintf will write: if "buf" is
* too small to hold the NUL, or the NUL is exactly the last
* byte, the read will look like it got truncated by one byte.
* Since there is no way to ask sprintf nicely to not write
* the NUL, we have to use a bounce buffer.
*/
wrote = scnprintf(bounce, sizeof(bounce), "0x%px\n",
kallsyms_show_value(file->f_cred)
? battr->private : NULL);
count = min(count, wrote);
memcpy(buf, bounce, count);
return count;
}
static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
{
const struct bin_attribute *const *bin_attr;
for (bin_attr = sect_attrs->grp.bin_attrs; *bin_attr; bin_attr++)
kfree((*bin_attr)->attr.name);
kfree(sect_attrs->grp.bin_attrs);
kfree(sect_attrs);
}
static int add_sect_attrs(struct module *mod, const struct load_info *info)
{
struct module_sect_attrs *sect_attrs;
const struct bin_attribute **gattr;
struct bin_attribute *sattr;
unsigned int nloaded = 0, i;
int ret;
/* Count loaded sections and allocate structures */
for (i = 0; i < info->hdr->e_shnum; i++)
if (!sect_empty(&info->sechdrs[i]))
nloaded++;
sect_attrs = kzalloc_flex(*sect_attrs, attrs, nloaded);
if (!sect_attrs)
return -ENOMEM;
gattr = kzalloc_objs(*gattr, nloaded + 1);
if (!gattr) {
kfree(sect_attrs);
return -ENOMEM;
}
/* Setup section attributes. */
sect_attrs->grp.name = "sections";
sect_attrs->grp.bin_attrs = gattr;
sattr = §_attrs->attrs[0];
for (i = 0; i < info->hdr->e_shnum; i++) {
Elf_Shdr *sec = &info->sechdrs[i];
if (sect_empty(sec))
continue;
sysfs_bin_attr_init(sattr);
sattr->attr.name =
kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL);
if (!sattr->attr.name) {
ret = -ENOMEM;
goto out;
}
sattr->read = module_sect_read;
sattr->private = (void *)sec->sh_addr;
sattr->size = MODULE_SECT_READ_SIZE;
sattr->attr.mode = 0400;
*(gattr++) = sattr++;
}
ret = sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp);
Annotation
- Immediate include surface: `linux/module.h`, `linux/kernel.h`, `linux/fs.h`, `linux/sysfs.h`, `linux/slab.h`, `linux/kallsyms.h`, `linux/mutex.h`, `internal.h`.
- Detected declarations: `struct module_sect_attrs`, `struct module_notes_attrs`, `function module_sect_read`, `function free_sect_attrs`, `function add_sect_attrs`, `function remove_sect_attrs`, `function free_notes_attrs`, `function add_notes_attrs`, `function remove_notes_attrs`, `function add_sect_attrs`.
- 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.