Linux device underlying model

  

On the linux device model online, there are some discussions, some things I used to use, to modify and organize.

§1 KobjectKobject is a new device management mechanism introduced in Linux 2.6, represented by struct kobject in the kernel. Through this data structure, all devices have a unified interface at the bottom, kobject provides basic object management, is the core structure of the Linux2.6 device model, it is closely related to the sysfs file system, each kobject object registered in the kernel Both correspond to a directory in the sysfs file system. Kobject is the basic structure that makes up the device model. Similar to the base class in C++, it is embedded in objects of larger objects -- so-called containers -- that are used to describe the components of the device model. Such as bus, devices, drivers are typical containers. These containers are connected by kobject, forming a tree structure. This tree structure corresponds to the /sys. The kobject structure provides basic object management for some large data structures and subsystems, avoiding the repetitive implementation of similar functions. These functions include - object reference count. - Maintain object linked list (collection). - Object lock. - Representation in user space.

Kobject structure is defined as:
struct kobject {char * k name; Pointer to the device name char name[KOBJ NAME LEN]; device name struct kref kref; object reference count struct list head entry; unit attached to the kset struct kobject * parent; pointer to the parent object struct kset * kset The pointer to the kset struct kobj type * ktype; the pointer to its object type descriptor struct dentry * dentry; the file node path pointer corresponding to the object in the sysfs file system};

where the kref field indicates the object reference Count, the kernel implements object reference count management through kref. The kernel provides two functions kobject_get() and kobject_put() to increase and decrease the reference count respectively. When the reference count is 0, all the resources used by the object are released. The Ktype field is a pointer to the kobj type structure, indicating the type of the object.

Related Functions
void kobject_init(struct kobject * kobj);//kobject initialization function. Int kobject_set_name(struct kobject *kobj, const char *format, ...);//Set the name of the specified kobject. Struct kobject *kobject_get(struct kobject *kobj);//Adds a reference count of the kobj object to 1 and returns a pointer to the object. Void kobject_put(struct kobject * kobj);//Decrements the reference count of the kobj object by 1. If the reference count drops to 0, then kobject release() is called to release the kobject object. Int kobject_add(struct kobject * kobj);//Add the kobj object to the Linux device hierarchy. Attach the kobject object to the list chain of kset, increase the reference count of kobject //in the parent directory, create a file node in the directory pointed to by the parent, and start the hotplug function of the kernel object of the type. Int kobject_register(struct kobject * kobj); //kobject registration function. The kobj is initialized by calling kobject init(), and kobject_add() is called to complete the registration of the kernel object //. Void kobject_del(struct kobject * kobj);//Remove the kobj object from the Linux device hierarchy (hierarchy). Void kobject_unregister(struct kobject * kobj); //kobject logout function. Contrary to kobject register(), it first calls kobject del to remove the object from the device hierarchy, and //calls kobject put() to reduce the reference count of the object. If the reference count drops to 0, the kobject object is released.

§2 Kobj type
struct kobj_type {void (*release)(struct kobject *);struct sysfs_ops * sysfs_ops;struct attribute ** default_attrs;};

The Kobj type data structure contains three fields: A release method is used to release the resources occupied by the kobject; a sysfs ops pointer points to the sysfs operation table and a list of default attributes of the sysfs file system. The Sysfs action table includes two functions, store() and show(). When the user state reads the property, the show() function is called, and the function encodes the specified property value into the buffer and returns it to the user state; and the store() function stores the property value passed in the user state. Attribute
struct attribute {char * name;struct module * owner;mode_t mode;};

attribute, attribute. It is output as a file to the sysfs directory. Below the directory corresponding to kobject. The file name is name. The method of reading and writing files corresponds to sysfs ops in kobj type. §3. The most important thing about ksetkset is to establish the association between the sub-system and the underlying (kobject). Kobject will also use it to distinguish which type it belongs to, and then create the correct directory location under /sys. The priority of kset is relatively high. Kobject will use its own *kset to find its own kset, and specify *ktype as the ktype under the kset. Unless kset is defined, ktype will be used to establish the relationship. Kobject is organized into a hierarchical structure by kset. kset is a collection of kobjects of the same type. It is represented by the kset data structure in the kernel and is defined as:
struct kset {struct subsystem * subsys; pointer struct kobj of the subsystem Type * ktype; pointer to the kset object type descriptor struct list head list; chain table header struct kobject kobj for connecting all kobjects in the kset; embedded kobjectstruct kset hotplug ops * hotplug ops; pointing to the hot plug operation table Pointer};

All kobjects contained in kset are organized into a bidirectional circular list, and the list field is the head of the list. The Ktype field points to a kobj type structure that is shared by all kobjects in the kset to indicate the type of these objects. The Kset data structure also embeds a kobject object (represented by the kobj field), and all the parent fields of the kobject object belonging to this kset point to this embedded object. In addition, kset also relies on kobj to maintain reference counts: the reference count for kset is actually the reference count of the embedded kobject object. See Figure 1, kset vs. kobject


This picture is very classic, she reflects the connection of the entire kobject.

The correlation function is similar to kobject, kset_init() completes the initialization of the specified kset, and kset_get() and kset_put() increment and decrement the reference count of the kset object, respectively. The Kset_add() and kset_del() functions respectively implement the addition and deletion of the specified keset object to the device hierarchy; the kset_register() function completes the registration of the kset and the kset_unregister() function completes the logout of the kset.

§4 subsystem If kset is a collection of kobjects, the subsystem is a collection of ksets. It describes a class of device subsystems in the system, such as block subsys, which represents all block devices, corresponding to the block directory in the sysfs file system. Similarly, devices subsys corresponds to the devices directory in sysfs and describes all devices in the system. Subsystem is described by the struct subsystem data structure, defined as:
struct subsystem {struct kset kset; embedded kset object struct rw semaphore rwsem; mutual access semaphore};

can be seen, the difference between subsystem and kset There is an extra semaphore, so in the later code, the subsystem has been completely banned by kset.

Each kset belongs to a subsystem. You can add a kset to the subsystem by setting the subsys field in the kset structure to point to the specified subsystem. All ksets attached to the same subsystem share the same rwsem semaphore for synchronous access to the linked list in kset. Related Functions
subsystem has a similar set of functions: void subsystem_init(struct subsystem *subsys);int subsystem_register(struct subsystem *subsys);void subsystem_unregister(struct subsystem *subsys);struct subsystem *subsys_get(struct subsystem * Subsys)void subsys_put(struct subsystem *subsys);

About the usage of those functions, it will be discussed in detail in the examples that follow. Here is just an introduction.

Copyright © Windows knowledge All Rights Reserved