Click on the above“Linux Notes”, select“Pin/Star the Official Account”
Welfare and valuable content delivered promptly
-
Introduction
-
Considerations
-
Introduction to sys Filesystem
-
What is the sys Filesystem
-
Description of sys Filesystem Functions
-
sysfs and Object
-
Using sysfs Interface
-
Examples of sysfs Read/Write Operations
-
Common sysfs Interfaces
-
Common sysfs Structures
-
Code Experiment
-
Conclusion
Introduction
The previous article introduced the creation of the procfs interface in Linux drivers, today we will discuss the creation of the sysfs interface, using kernel version 5.10.
Considerations
Before reading this article, consider two questions:
-
Since there is a proc directory filesystem, why do we need the sys directory filesystem?
-
Try to write a kernel module that creates a directory named “test” under /sys, and creates a node called data within the “test” directory, allowing you to write characters and read back the written data.
Introduction to sys Filesystem
What is the sys Filesystem
The sysfs filesystem is a special filesystem similar to the proc filesystem, presenting information about devices and drivers in the kernel to user space in the form of files, focusing on the details of devices and drivers.
Description of sys Filesystem Functions
Compared to proc, sysfs has many advantages. The design principle of sysfs is that an attribute file does one thing only, and sysfs attribute files generally have only one value, which can be read or written directly.
Subdirectories under /sys
The main directories and their function descriptions under the sys filesystem are shown in the table below.

Therefore, overall system information can be obtained through procfs, while device and driver-related detailed information can be obtained through sysfs.
sysfs and Object
Kobject provides a generic, hierarchical object model for managing various objects in the kernel. Each Kobject has a unique name and a pointer to its parent Kobject, organizing them into a hierarchy. Additionally, Kobject can have attributes (such as device attributes, driver attributes, etc.), which can be exposed to user space through the sysfs filesystem.
Sysfs is a way to represent information about kernel objects such as devices and drivers through Kobject. When objects like devices and drivers are created in the kernel, the corresponding Kobject is also created, and its information is exposed to user space through Sysfs.
Using sysfs Interface
Examples of sysfs Read/Write Operations
Reading sysfs files: You can use the cat command or other file reading commands to read the contents of files in sysfs.
For example, we read the value content of gpio1’s sysfs:
# cat /sys/class/gpio/gpio1/value
Writing to sysfs files: You can use the echo command or other file writing commands to write content to files in sysfs.
For example, you can declare GPIO1:
# echo 1 > /sys/class/gpio/export
Note that some sysfs files are read-only and cannot be written to.
Common sysfs Interfaces
Below are some API functions that will be used in this experiment.

Dynamic generation of kobject data structures, then registering them in the sysfs filesystem.
struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
Parameters:
- name: The name of the file or directory to be created
- parent: Pointer to the parent directory’s kobject data structure; if parent is NULL, it indicates that the /sys directory is the parent directory.
Given a directory kobject, create a set of attribute groups.
int sysfs_create_groups(struct kobject *kobj, const struct attribute_group **groups)
Parameters:
- kobj: Describes the creation of a set of attributes under the directory and displays the files in that attribute set.
- grp: Describes a set of attribute types.
To delete the corresponding sysfs file, you can use:
void sysfs_remove_file (struct kobject *kobj, const struct attribute *attr);
Parameters:
- kobj: Describes the creation of a set of attributes under the directory and displays the files in that attribute set.
- attr: Represents a specific group of node attributes.
To delete the corresponding sysfs directory:
void kobject_put(struct kobject *kobj);
Parameters:
- kobj: Describes the creation of a set of attributes under the directory.
Common sysfs Structures
Definition of the key attribute_group structure:
#include <linux/sysfs.h>
struct attribute_group {
const char *name;
umode_t (*is_visible)(struct kobject *, struct attribute *, int);
umode_t (*is_bin_visible)(struct kobject *, struct bin_attribute *, int);
struct attribute **attrs;
struct bin_attribute **bin_attrs;
};
Definition of the device attribute device_attribute structure:
struct device_attribute {
struct attribute attr; // The device attribute to be created
ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf); // Read operation callback function
ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); // Write operation callback function
};
To facilitate the definition of device attributes, the kernel provides a series of related macros for initializing device attributes.
#define DEVICE_ATTR(_name, _mode, _show, _store) \
struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
#define DEVICE_ATTR_RW(_name) \ // Readable and writable device attribute
struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
#define DEVICE_ATTR_RO(_name) \ // Read-only device attribute
struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
#define DEVICE_ATTR_WO(_name) \ // Write-only device attribute
struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
Code Experiment
#include <linux/syscalls.h>
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/init.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#define KERNEL_ATTR_RO(_name) \
static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
#define KERNEL_ATTR_RW(_name) \
static struct kobj_attribute _name##_attr = \
__ATTR(_name, 0644, _name##_show, _name##_store)
struct kobject *test_kobj;
static char example_sysfs_value[64] = {0};
// Read function handler
static ssize_t data_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
return sprintf(buf, "%s\n", example_sysfs_value);
}
// Write function handler
static ssize_t data_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{
sscanf(buf, "%s", example_sysfs_value);
return count;
}
// Define read/write attribute for data
KERNEL_ATTR_RW(data);
static struct attribute * test_attrs[] = {
&data_attr.attr,
NULL,
};
static const struct attribute_group test_attr_group = {
.attrs = test_attrs,
};
static int __init ktest_init(void)
{
int error;
// Create /sys/test
test_kobj = kobject_create_and_add("test", NULL);
if (!test_kobj) {
error = -ENOMEM;
goto exit;
}
// Create /sys/test attribute group
error = sysfs_create_group(test_kobj, &test_attr_group);
if (error)
goto kset_exit;
return 0;
kset_exit:
// Delete the corresponding sysfs directory
kobject_put(kernel_kobj);
exit:
return error;
}
core_initcall(ktest_init);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("pan");
Makefile
KERNELDIR ?= /home/forlinx/OK3588_Linux_fs/kernel
obj-m += sys_test.o
all: modules
modules:
$(MAKE) ARCH=arm64 CROSS_COMPILE=aarch64-none-linux-gnu- -C $(KERNELDIR) M=$(shell pwd) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules_install
clean:
$(MAKE) -C $(KERNELDIR) M=$(shell pwd) modules clean
Test Results:
# insmod sys_test.ko
# echo hello > /sys/test/data
# cat /sys/test/data
hello
Conclusion
In this article, we learned about common sysfs node information and practical operations with sysfs nodes. Students who learned from this article can support with a quick three-step action, and feel free to follow the official account [Linux Notes] for continuous sharing of valuable content!
end
Previous Recommendations
Essential reading books recommended for learning embedded systems
My Linux driver learning path
1-on-1 paid consultation
What websites do embedded experts usually browse?
Ten projects recommended for fresh graduates to include in their resumes