1. Preface
This article mainly introduces the basic data types implemented by Linux kernel, including linked list, kernel object, kernel object reference count, kernel object collection,
2. Linked list
1. Basic structure of linked list
The kernel list can link any type of data structure, and the list structure is as follows:
1 struct List_head {2 struct list_head *next, *prev; 3 };
Figure standard double-linked list
A typical circular doubly linked list is shown.
2. List-related APIs
List_head (list_name) defines a list_head struct, and the next and pre members are initialized to the currently newly created List_headlist_add (new, HEAD) Immediately after the head element is inserted new element List_add_tail (new, head) inserts a new element before the head element, because it is a circular linked list, it is actually inserting the new element into the end List_del ( Entry) removes an item from the list List_empty (head) checks if the linked list is empty list_splice (list, head) merges two linked lists, inserts the list into the back of the other linked list head list_entry (PTR, TPE, Member) PTR is a pointer to a List_head member in a data structure, type is the kind of the data structure, and member is the list_head member variable name in the data structure List_for_each (POS, head) is used to traverse all elements of the linked list. POS represents the current position of the linked list, and head specifies the table header
3. Kernel Object 3.1 kobject
1. Basic structure of Kobject
1 structKobject {2 Const Char*name; /*kobject's name * * 3 structList_head entry; / * For placing kobject in a linked list * /4 structKobject *parent; / * The parent object that points to the current kobject, used to establish the hierarchy * /5 structKset *Kset; /* Used to place objects in a collection with other objects */ 6 structKobj_type *Ktype; / * Provides more properties for Kobject, and most importantly the destructor function that frees the data structure resource * /7 structKernfs_node *SD;/*Sysfs Directory Entry*/8 structKref Kref; / * for simplifying reference counting * /9 #ifdef config_debug_kobject_releaseTen structdelayed_work release; One #endif AUnsignedintState_initialized:1; -UnsignedintSTATE_IN_SYSFS:1; -UnsignedintState_add_uevent_sent:1; theUnsignedintState_remove_uevent_sent:1; -UnsignedintUevent_suppress:1; -};
Note: Kobject is not connected by pointers to other data structures, but must be embedded directly into other data structures, so that management kobject the inclusion of Kobject objects. Since kobject are embedded in many data structures, it is very small to keep the kobject structure.
2. Kobject's API
structKobject *kobject_get (structKobject *kobj) Increase the reference count of KobjectvoidKobject_put (structKobject *kobj) Reduce the reference count of KobjectvoidKobject_init (structKobject *kobj,structKobj_type *ktype) Initializes the kobject struct, sets the reference count to 0, and initializes the list member of the KobjectintKobject_add (structKobject *kobj,structKobject *parent, adding Kobject to SYSFS displayConst Char*fmt, ...)intKobject_init_and_add (structKobject *kobj,structKobj_type *ktype,structKobject *parent,Const Char*FMT, ...) Merging operations of Kobject_init and Kobject_addstructKobject *kobject_create (void) allocates space for a kobject and invokes Kobject_init initializationstructKobject *kobject_create_and_add (Const Char*name,structKobject *parent) Merge operations for Kobject_create and Kobject_addStatic voidKobject_cleanup (structKobject *kobj) Releases Kobject-occupied resources when Kobject (and objects containing kobject) are not required
3.2 Kref
1.kref Structural Body
The reference count is used to detect how many places in the kernel are using an object, and when a part of the kernel contains information about an object, you need to add the object to the Kobject kref plus 1, minus 1 if you don't need it, and if you subtract 0, release the object
1 struct 2/ * Atomic count, which gives the count of an object used in the kernel in the current period, the Kobject can remove */3} from memory when the count is 0 o'clock;
API for 2.kref
Static void kref_init (struct kref *kref) Initializes kref, RefCount is initialized to 1staticvoid Kref_get (struct kref *kref) to use an object must add a reference count of the corresponding kobject to 1staticint kref_ Sub (structint count,void (*release) (struct kref *kref)) if the object is not in use, the corresponding kref minus 1, and if the reference count is 0, call release Kobject
3.3 Kset
Kset is the first example of a kobject application, so it is managed with Kobject and is independent of the kobject contained in Kset, where Kobject is purely for the purpose of managing Kset
1 struct Kset {2 struct list_head list;/* is used to link the list of all the Kobject in the current Kset * / 3 4 struct Kobject kobj; 5 Const struct kset_uevent_ops *uevent_ops;/* Provides a number of function pointers for Kset related information to the application layer*/6 };
3.4 Ktype
1 structKobj_type {2 void(*release) (structKobject *kobj);3 Const structSysfs_ops *Sysfs_ops;4 structAttribute * *Default_attrs;5 Const structKobj_ns_type_operations * (*child_ns_type) (structKobject *kobj);6 Const void*(*namespace)(structKobject *kobj);7};
Note: Ktype does not have a relationship with Kset, Kset has provided a collection function, Ktype provides interfaces related to SYSFS file systems. If multiple Kobject export similar information, you can share a ktype to provide the required method
4. Data type
typedef to avoid dependency on the architecture of related features, such as sector_t, pid_t, etc.
Cpu_to_le64 Converting a 64-bit data type to a small-endian format
LE64_TO_CPU converting 64-bit small-endian format to 64-bit CPU-side format
can effectively avoid concurrency problems caused by multiple processors accessing the same variable concurrently
DEFINE_PER_CPU (name, type) name is the variable name, type is the data type
GET_CPU (name, CPU) Gets the instance of the variable created on the current CPU
SMP_PROCESSOR_ID () returns the ID of the current CPU
__user Use this tag to identify pointers to user space
5. Reference documentation
[1] Deep Linux kernel architecture (Plka)
Linux kernel Infrastructure