活动地址:CSDN21天学习挑战赛
学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您:
想系统/深入学习某技术知识点…
一个人摸索学习很难坚持,想组团高效学习…
想写博客但无从下手,急需写作干货注入能量…
热爱写作,愿意让自己成为更好的人…
内核对象(kernel object)是将一个数据结构struct kobject作为一种公共连接部件定义到更高层的其他数据结构中去,而各个高层数据结构之间的关系通过kobject结构以不同的链表方式表示,从而形成结构紧密的上下层次关系。通过这种kobject结构,可以清晰地描述总线、设备、设备上的接口等相互间的关系,变得更有层次且方便系统管理者了解系统状况。

//kernel/include/linux/kobject.h
struct kobject {
const char *name; //在sysfs下对应的名字
struct list_head entry; //挂接到kset中的链表单元
struct kobject *parent; //所属的父kobject
struct kset *kset; //所属的kset
struct kobj_type *ktype; //所属的类型
struct kernfs_node *sd; /* sysfs directory entry sysfs文件系统中文件节点的入口*/
struct kref kref; //对象引用计数
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
struct delayed_work release;
#endif
unsigned int state_initialized:1; //已初始化标志位
unsigned int state_in_sysfs:1; //已添加到sysfs标志位
unsigned int state_add_uevent_sent:1; // add 事件已发送标志位
unsigned int state_remove_uevent_sent:1; //remove事件已发送标志位
unsigned int uevent_suppress:1;
ANDROID_KABI_RESERVE(1);
ANDROID_KABI_RESERVE(2);
ANDROID_KABI_RESERVE(3);
ANDROID_KABI_RESERVE(4);
};
struct kobj_type {
void (*release)(struct kobject *kobj);
const struct sysfs_ops *sysfs_ops;
struct attribute **default_attrs;
const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
const void *(*namespace)(struct kobject *kobj);
void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
ANDROID_KABI_RESERVE(1);
ANDROID_KABI_RESERVE(2);
ANDROID_KABI_RESERVE(3);
ANDROID_KABI_RESERVE(4);
};
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;
};

kobject通常本身并不关系它们自己,相反它们通常嵌入包含在其他结构中,感兴趣的是该结构体中的其他内容。“kobject”结构首先出现在 2.5.45 开发内核中。它最初的目的是作为一种统一管理引用计数对象的内核代码的简单方法。随着内核的不断更新升级,kobject的地位作用越来越明显,现在主要作用为如下:
了解kobject后,还有一个和kobject相关的重要对象叫做kset。
/**
* struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
*
* A kset defines a group of kobjects. They can be individually
* different "types" but overall these kobjects all want to be grouped
* together and operated on in the same manner. ksets are used to
* define the attribute callbacks and other common events that happen to
* a kobject.
*
* @list: the list of all kobjects for this kset
* @list_lock: a lock for iterating over the kobjects
* @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
* @uevent_ops: the set of uevent operations for this kset. These are
* called whenever a kobject has something happen to it so that the kset
* can add new environment variables, or filter out the uevents if so
* desired.
*/
struct kset {
struct list_head list; //挂载kobject结构的链表
spinlock_t list_lock; //自旋锁
struct kobject kobj; //自身包含一个kobject结构
const struct kset_uevent_ops *uevent_ops; //udev机制的uevent时间操作集
ANDROID_KABI_RESERVE(1);
ANDROID_KABI_RESERVE(2);
ANDROID_KABI_RESERVE(3);
ANDROID_KABI_RESERVE(4);
} __randomize_layout;
在许多方面,kset 看起来像是 kobj_type 结构的扩展; kset 是相同 kobjects 的集合。但是,虽然 struct kobj_type 关注对象的类型,但 struct kset 关注聚合和集合。
一个 kset 服务于这些功能:

接下来将看到这些重要的结构的定义解说明.