sysfs in linux kernel


The operating system divides virtual memory into kernel space and user space. Kernel space is strictly reserved for running the kernel, kernel extensions, and most device drivers. In contrast, user space is the memory area where all user mode applications work and can be swapped out if necessary. There are many ways to communicate between user space and kernel space, and sysfs is one of them.

sysfs in Linux

Sysfs, like proc, is a virtual file system. Files in sysfs contain information about devices and drivers. Some files in sysfs are even writable for configuring and controlling devices connected to the system. Sysfs is always mounted on / sys.

Directories in Sysfs contain a hierarchy of devices because they are connected to computers.

Sysfs is a common method to export system information from kernel space to user space of a specific device. Sysfs is associated with the device driver model of the kernel. procfs is used to export the information of a specific process, and debugfs is used to export the debugging information of developers.

Before understanding sysfs, you should understand Kernel Objects.

Kernel Objects

The core content of sysfs is kobject.Kobject is a bridge connecting sysfs and kernel, which is mainly implemented by struct kobject defined in < Linux / kobejct. H >. Struct kobject represents a kernel object, possibly a device, etc. for example, it is displayed as the contents of a directory in the sysfs file system.

Kobjects are usually embedded in other data structures.

Definition of kobject

#define KOBJ_NAME_LEN 20 

struct kobject {
 char *k_name;
 char name[KOBJ_NAME_LEN];              //kobject's name
 struct kref kref;                      //Provide incoming count
 struct list_head entry;
 struct kobject *parent;                //The parent node of kobject is similar to the upper level directory
 struct kset *kset;                     //A set of kobjects, all embedded in the same type of structure
 struct kobj_type *ktype;               //Type associated with kobject
 struct dentry *dentry;

Kobject is the glue that binds most device models and their sysfs interfaces together. The directory used to create kobject in / sys directory.

sysfs in device driver

There are several steps to create and use sysfs.

  1. Create directory under / sys directory
  2. Create sysfs file

Create directory under / sys directory

You can use * * kobject_create_and_add() * * function to create directory

struct kobject * kobject_create_and_add ( const char * name, struct kobject * parent);

name:To create kobject Name of
parent:You need to create the directory where you are and add it to the incoming directory NULL Then in/sys/Create directory under directory

This function will dynamically create a kobject Object and then register to sysfs. If kobject Cannot be created, function will return NULL. 

Create sysfs file

Using the above interface, you can create directories in the / sys directory. Next, you need to create sysfs files to realize the interaction between user space and kernel space. We can create a sysfs file using the sysfs attribute.

Property is represented as a regular file in sysfs, with one value for each file. There are a number of helper functions available to create the kobject attribute. They can all be found in the header file sysfs.h.

Create attribute

kobj_ Definition of attribute

struct kobj_attribute {
 struct attribute attr;
 ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, char *buf);
 ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count);

attr:Represents the properties of the file to be created
show:Point at sysfs Pointer to the function that will be called when reading the file in
store:Point to the file to be written to sysfs Pointer to the function called when

You can use macro definitions__ATTR Create attribute
__ATTR(name, permission, show_ptr, store_ptr);

Display and write functions

Then you need to write the show and store functions

ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, char *buf);

ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count);

The Store function is called whenever we write to the sysfs property.

Whenever we read the sysfs property, we call the Show function.

Create sysfs file

Interface function to create a single sysfs file attribute

int sysfs_create_file ( struct kobject *  kobj, const struct attribute * attr);

kobj:Create a pointer to the object
attr:property descriptor 

Create an interface for a set of properties

int sysfs_create_group ( struct kobject *kobj, const struct attribute_group *grp);

Delete the interface of sysfs

void sysfs_remove_file ( struct kobject *  kobj, const struct attribute * attr);

Welcome to visit My blog

Tags: C Linux Embedded system

Posted on Thu, 21 Oct 2021 01:19:07 -0400 by el_kab0ng