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.

Dependency Surface

Detected Declarations

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 = &sect_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, &sect_attrs->grp);

Annotation

Implementation Notes