Kobject, kobj_type, kset of linux device driver model

While learning the platform platform driver, I slowly discovered that the bottom layer is calledlinux device driver models things. The discovery is that it is an important organizational structure for the underlying drive, and now it is aimed atDevice driver modelPerform analysis. So as to deepen the understanding of the entire device driver registration.

The user space program accesses the relevant information of the device through the sysfs virtual file system. This information is organized into a hierarchical structure and represented by the sysfs virtual file system. The user can control the device or read the information of the device by operating the sysfs.

1. Introduction to sysfs file system:

1. Overview of sysfs

The sysfs file system is a manifestation of kernel objects (kobject), attributes (kobj_type), and their relationship.
Very important features of sysfsUsers can read kernel data from sysfs, or write user data into the kernel.

2. Correspondence between kernel structure and sysfs:

kobject --> catalog

kobj_type-->Properties file

3. Features

The sysfs file system only exists in memory and dynamically represents the kernel data structure. When the device is started, the device driver model will register the kobject object and generate a directory file under sys in the sysfs file system.
such as
sys/bus: The buses registered in the system are listed below, such as USB bus and platform bus.
sys/class: Device class registered in the kernel, such as sound and input.


Second, the core data structure

In the driver representation of Linux, there are mainly three basic structures, namely kobject, kset, and ktype. These three structures are the lower structure in the device model.
Each element in the model corresponds to a kobject.
kset and ktype can be seen as extensions of kobject in terms of hierarchical structure and attribute structure. The relationship between the three is described as follows:

Reference: http://blog.csdn.net/lizuobin2/article/details/51511336

As shown in the figure above, each directory in sysfs corresponds to a kobject.
These kobjects have their own parent. If no parent is specified, it will point to the kset->object to which it belongs.
Secondly, kset also embeds kobject. This kobject can refer to its parent at a higher level. that's it. It constitutes a hierarchical relationship above the space.
Actually, every object has attributes. For example, power management, executive plug-in management and so on.
Because most devices of the same type have the same attributes, this attribute is isolated and stored in ktype.
This allows flexible management. Remember when analyzing sysfs.
For ordinary file read and write operations in sysfs, kobject->ktype->sysfs_ops is used to complete.

1, kobject structure

kobject constitutes the basic structure of the device driver model.
In sysfs, the device is represented by a tree structure, and the tree structureEach directory corresponds to a kobject object(Information such as directory organization structure and name)


struct kobject {
	const char *name; The name of the kobject, displayed in the sysfs file system, as the name of a directory.
	 struct list_head entry; link to the next kobject structure
	 struct kobject *parent; points to the parent kobject structure
	 struct kset *kset; points to the kest set it belongs to
	 struct kobj_type *ktype; points to the properties file of kobject, each object has properties
								     Organize the attributes separately into a data structure kobj_type and store them in ktype
								     For ordinary file read and write operations in sysfs, kobject->ktype->sysfs_ops is used to complete
	 struct sysfs_dirent *sd; corresponding to the file directory of sysfs
	 struct kref kref; reference count of kobject
	 unsigned int state_initialized:1; Initialized state
	 unsigned int state_in_sysfs:1; Whether it has been added to sysfs
	unsigned int state_add_uevent_sent:1;
	unsigned int state_remove_uevent_sent:1;
	unsigned int uevent_suppress:1;
};

In the kernel, there is no variable defined directly with kobject, kobject is only used as aAbstract base classAnd exist. Generally, kobject is embedded in another structure, and this structure can be regarded as one of kobjectSubclass. The subclasses of kobject will be more concerned about the properties and methods of kobject.

The devices in the kernel are organized in a tree form. In this organizational structure, the upper-level nodes can be regarded as the parent nodes of the lower-level nodes.
reflected in sysfs is the relationship between the upper-level directory and the lower-level directory. In the kernel,It is kobject that helps us realize this parent-child relationship.

Members of kobject:

name represents the name of kobject in sysfs;

The pointer parent is used to point to the parent of kobject;

Kref should be familiar to everyone, kobject uses it to implement reference counting;

The Kset pointer is used to point to the kset to which this kobject belongs;

For ktype, it is used to describe the type information of kobject.

The role of kobject:

(1)kobject always represents a directory in the sysfs file system, and the name member specifies the name of the directory, not a file.

(2)parent member specifiedThe directory of kobject in sysfs, thus forming a tree structure.

(3)ktype isThe attributes of kobject, which are represented by files, placed inkobject corresponds to the directory.


2. Equipment attribute kobj_type

Each kobject object has some attributes, which are represented by kobj_type, there is a pointer to kobj_type in kobject, and the attributes are represented by files.
struct kobj_type {
	 void (*release)(struct kobject *kobj); release kobject and its resource-occupied function
	 const struct sysfs_ops *sysfs_ops; The method of operating the next attribute array
	 struct attribute **default_attrs; attribute array
	const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
	const void *(*namespace)(struct kobject *kobj);
};
The default_attrs member of kobj_type saves an array of attributes. Each kobject object can have one or more attributes. The attributes are defined as follows:
struct attribute {
	 const char *name; The name of the attribute, corresponding to the name of a file in the directory
	 umode_t mode; read and write permissions for attributes
#ifdef CONFIG_DEBUG_LOCK_ALLOC
	struct lock_class_key	*key;
	struct lock_class_key	skey;
#endif
};
The default_attrs array of kobj_type indicates which attributes kobject has, but it does not explain how to operate these attributes. This task is determined by kobj_type
sysfs_ops->sysfs_ops to complete

struct sysfs_ops {
	 ssize_t (*show)(struct kobject *, struct attribute *,char *); read attribute operation function
	 ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t); write attribute operation function
	const void *(*namespace)(struct kobject *, const struct attribute *);
};
kobject: the kobject pointer to be read and written, attribute: the attribute to be read and written.

3.kset structure

I said that kobject corresponds to a directory in the file system /sys, and kset contains the kobject structure, so kset also corresponds to a directory in /sys.
To put it simply, kset and kobj are both directories. Since they are directories, they are in a tree structure, and each directory will have a parent node.
Use kset.kobj->parent to specify in kset
Use kobj->parent to specify in kboject
Obviously, the entire tree-like directory structure is built through kobj, but some kobj is embedded in Kset. It is easier to understand kset as an ordinary kobj when analyzing the directory structure .
kobject is organized into a hierarchical structure through kset, which is a collection of kobjects of the same type. Just like the driver is placed in the /sys/drivers directory, the directory drivers is a kset object, containing the directory corresponding to the driver in the system, and the directory of the driver is represented by kboject. The kernel will be similarThe kboject structure is connected in the kset collection.

struct kset {
	struct list_head list;
	spinlock_t list_lock;
	struct kobject kobj;
	const struct kset_uevent_ops *uevent_ops;
};


kobject andKset correspondence:



The current simple understanding:

Linux has a virtual file system /sysfs. After the kernel starts to mount the file system, the sysfs file system will be mounted under /sys. When the kernel is started, some buses and devices will be initialized and registered. These buses and devices will create directories under sys for storage. For example, platform device information is stored under /sys/bus/platform. How to organize and manage equipment and create a catalog?Device driver modelSeveral important structures: Kobject, kobj_type, kset to organize and manage the directory and file structure.

Intelligent Recommendation

Device driver model: kobject, kset, ktype (2)

prior tohttps://mp.csdn.net/mdeditor/84722837#This blog introduces the relationship between the three structures of kobject, set, ktype and its role, which can be used as a reference. The following de...

Device driver model: kobject, kset, ktype (3)

This blog has an analysis of the role of kobject, this blog is a practical application of kobject, the code is as follows: <kobject.c> The actual operation results are as follows:...

Device driver model: kobject, kset, ktype (5)

In the "Device Driver Model: kobject, kset, ktype (four)" blog has detailed the operation of kset and the relationship between kset and kobject, the following is some practical operations fo...

Analysis KOBJECT, KOBJ_TYPE and KSET

First, KOBJECT Each directory (including subdirectory) under the SYS directory (ie, the sysfs virtual file system) corresponds to a KOBJECT structure instance, you need to specify a directory name whe...

Understanding of kobject, kset, kobj_type

Cited literature:Linux sysfs file system analysis_jansert's blog-CSDN blog §1 Kobject  The Kobject structure is defined as: struct kobject { char * kname; pointer to device name char na...

More Recommendation

Linux device model (two) kobject and kset

The functions of kobject are: In the Linux kernel, kobject is the basic data type that composes the Linux device model. Each kobject object registered in the kernel corresponds to a directory in the s...

Device driver model-kset

table of Contents 1. Introduction to kset 1.1 Definition 1.2 Hot swap events 1.2.1 Two ways of message notification 1.2.2 Operation interface 2. The relationship between kobject and kset 2.1 Inheritan...

Device driver model-kobject

table of Contents 1. Introduction to Device Driver Model 2. Introduction to kobject 2.1 kobject kernel data structure 2.2 kobject operation kernel interface Three. kobject instance 3.1 Makefile 3.2 Ex...

KObject of Linux device model

With the development of the Linux kernel, there are more and more devices they support, but there is no good way to manage the increased equipment drive. In order to provide a unified mechanism in the...

linux kobject kobject_type kset

Foreword: The linux device model includes kobject, kobject_type, and kset. For a better understanding, take a chestnut: kset is like a spider web, and there are many intersections of lines on the web....

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top