由kobject_create_and_add全面了解kobject
一、引言
从v2.6开始linux引入了统一的设备模型,用kobject数据结构来描述设备。有了kobject,设备间的拓扑关系就一目了然了,它为设备管理带来了很大的便利。最近学习到kobject_create_and_add(),通过对这个函数的学习,可以全面细致地了解到kobject。p.s.本文相关代码基于linux 5.4.74。内容有点多,读不下去请跳转至文章末尾。
二、kobject相关数据结构
首先,简单了解下kobject相关的数据结构。
kobject结构体如下所示:
//代码路径kernel5.4/include/linux/kobject.h
struct kobject {
const char *name; //name指针指向kobject的名称
struct list_head entry;
struct kobject *parent;
struct kset *kset;
struct kobj_type *ktype;
struct kernfs_node *sd; /* sysfs directory entry */
struct kref kref; //kobject refcount 引用计数,为1引用,为0释放
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
struct delayed_work release;
#endif
//下面是一些标志变量,用位域的方式声明
unsigned int state_initialized:1;
unsigned int state_in_sysfs:1;
unsigned int state_add_uevent_sent:1;
unsigned int state_remove_uevent_sent:1;
unsigned int uevent_suppress:1;
};
在这些成员变量中,ktype、kset、kref值得注意。它们是对一个kobject的描述、分类和引用标识。
2.1 ktype
ktype指针指向了一个kobj_type的数据结构,该数据结构如下代码段所示:
struct kobj_type {
void (*release)(struct kobject *kobj);//析构函数指针,指向析构函数,该函数在refcount为0时,清理所有”同类“的kobject并释放内存。
const struct sysfs_ops *sysfs_ops;//指针指向”sysfs读写操作函数结构体“
struct attribute **default_attrs;//指针指向attribute结构体数组,描述了属于该ktype的kobject的默认属性
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);
};
kobj_type是对kobject的特性描述。ktype数据结构初始化将会在随后的kobject_init流程中看到。
2.2 kset
kset指针指向一个kset的数据结构,该数据结构如下代码段所示:
struct kset {
struct list_head list;//kset链表
spinlock_t list_lock;
struct kobject kobj;//该kset的基类(就是说该kset中的kobject都长这样)
const struct kset_uevent_ops *uevent_ops;//指针指向kset_uevent_ops结构体,用于处理kset中的kobject对象的热插拔操作。
} __randomize_layout;
kset是很多kobject的集合。一般来说,如果一个kobject属于某个kset,即kset->kobj是存在的,那么这个kset->kobj将作为该kobject的父对象。这一点在随后的kobject_add流程中体现。
2.3 kref
kref指针指向一个kref的数据结构,该数据结构如下代码段所示:
//路径/kernel5.4/linux/kref.h
struct kref {
refcount_t refcount;
};
kref引用计数来标示kobject的生命周期,在对象初始化时,调用kref_init()来使kref->refcount被原子置1(这一步将会在随后的kobject_init流程中看到)。引用了该kobject的代码都要进行引用计数加一。只要这个refcount不为0,该kobject就会继续被保留在内存中,否则对象将被撤销、内存被释放。
refcount的初始化、增加和减少分别用下面函数来实现:
//路径/kernel5.4/linux/kref.h
static inline void kref_init(struct kref *kref)
{
refcount_set(&kref->refcount, 1);
}
static inline void refcount_set(refcount_t *r, unsigned int n)
{
atomic_set(&r->refs, n);
}
static inline void kref_get(struct kref *kref)
{
refcount_inc(&kref->refcount);//这里最终调用也是原子加一
}
static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref))
{
if (refcount_dec_and_test(&kref->refcount)) { //这里最终调用也是原子减一
release(kref);
return 1;
}
return 0;
}
三、kobject_create_and_add函数
kobject_create_and_add,功能:动态注册kobject并生成sysfs
//代码路径kernel5.4/lib/kobject.c
/**
* @name: kobject名称
* @parent: the parent kobject of this kobject, if any.
*
* 返回:一个kobject类型的数据结构
* 不用这个kobject的时候用kobject_put()释放掉这个kobject
* 未创建成功返回NULL
*/
struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
{
struct kobject *kobj;
int retval;
kobj = kobject_create();//获得kobject
if (!kobj)
return NULL;
retval = kobject_add(kobj, parent, "%s", name);//添加sysfs
if (retval) {
pr_warn("%s: kobject_add error: %d\n", __func__, retval);
kobject_put(kobj);//释放kobject
kobj = NULL;
}
return kobj;
}
kobject_create_and_add的整体调用流程图如下图所示:
从整体流程图可以看出,实际上是对kobject数据结构的填充。下面详细分析一下以上流程。
3.1 kobject_create()
kobject_create()–动态创建kobject数据结构
//代码路径kernel5.4/lib/kobject.c
/**
* kobject_create - create a struct kobject dynamically
*/
struct kobject *kobject_create(void)
{
struct kobject *kobj;
kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);//为该kobj分配内核空间
if (!kobj)
return NULL;
kobject_init(kobj, &dynamic_kobj_ktype);//kobject初始化
return kobj;
}
kobject_create()中的关键是kobject_init(kobj, &dynamic_kobj_ktype);
在深入了解kobject_init之前,先来看下它的入口参数dynamic_kobj_ktype:
//代码路径kernel5.4/lib/kobject.c
static struct kobj_type dynamic_kobj_ktype = {
.release = dynamic_kobj_release,
.sysfs_ops = &kobj_sysfs_ops,
};
在kobject.c中已经给出了dynamic_kobj_ktype的定义,它给出了kobj_type的析构函数和sysfs操作函数。
//代码路径kernel5.4/lib/kobject.c
//析构函数,实质就是释放该kobject
static void dynamic_kobj_release(struct kobject *kobj)
{
pr_debug("kobject: (%p): %s\n", kobj, __func__);
kfree(kobj);
}
//代码路径kernel5.4/lib/kobject.c
//sysfs操作函数,包含对sysfs节点属性的读写操作函数设置
const struct sysfs_ops kobj_sysfs_ops = {
.show = kobj_attr_show, //读操作
.store = kobj_attr_store, //写操作
};
3.1.1 kobject_init
下面我们继续深入源码,看看kobject_init做了什么。
上源码:
//代码路径kernel5.4/lib/kobject.c
/**
* kobject_init() - Initialize a kobject structure.
* @kobj: pointer to the kobject to initialize
* @ktype: pointer to the ktype for this kobject.
*
* 这个函数在kobject_add()函数中被调用时,将会初始化一个kobject
*
* 注意用kobject_put()释放kobject
*/
void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
{
char *err_str;
if (!kobj) {
err_str = "invalid kobject pointer!";
goto error;
}
if (!ktype) {
err_str = "must have a ktype to be initialized properly!\n";
goto error;
}
if (kobj->state_initialized) {
/* do not error out as sometimes we can recover */
pr_err("kobject (%p): tried to init an initialized object, something is seriously wrong.\n",
kobj);
dump_stack();
}
kobject_init_internal(kobj);
kobj->ktype = ktype;//这里,直接把入口的ktype即dynamic_kobj_ktype赋值给kobj->ktype
return;
error:
pr_err("kobject (%p): %s\n", kobj, err_str);
dump_stack();
}
可以看到,在kobject_init中做了两部分工作:(1)对一些错误情况的处理;(2)kobject_init_internal。
那么kobject_init_internal做了什么呢?
//代码路径kernel5.4/lib/kobject.c
static void kobject_init_internal(struct kobject *kobj)
{
if (!kobj)
return;
kref_init(&kobj->kref); //引用计数初始化。在使用kref前,必须先通过kref_init()函数来初始化它。
INIT_LIST_HEAD(&kobj->entry);
kobj->state_in_sysfs = 0;
kobj->state_add_uevent_sent = 0;
kobj->state_remove_uevent_sent = 0;
kobj->state_initialized = 1;
}
可以看到,kobject_init_internal是对kobject中的一些成员的初始化。
3.1.2 小结
整体来看kobject_create()就是对kobject数据结构的填充(kobject初始化)。
3.2 kobject_add()
在kobject_create_and_add()中kobject_add调用如下:(是时候回头看下kobject_create_and_add了)
kobject_add(kobj, parent, "%s", name);
kobject_add()–把该kobject添加到sysfs中,源码如下所示:
//代码路径kernel5.4/lib/kobject.c
/**
* kobject_add - the main kobject add function
* @kobj: the kobject to add
* @parent: pointer to the parent of the kobject.
* @fmt: format to name the kobject with.
*
* 翻译参考《linux内核设计与实现(第三版)》17.4 sysfs
* The kobject name is set and added to the kobject hierarchy in this
* function.
* 翻译:设置kobject的名称并把它添加到kobject层次结构中。
*
* If @parent is set, then the parent of the @kobj will be set to it.
* If @parent is NULL, then the parent of the @kobj will be set to the
* kobject associated with the kset assigned to this kobject. If no kset
* is assigned to the kobject, then the kobject will be located in the
* root of the sysfs tree.
* 翻译:如果parent指针被设置,即该kobject的父指针被设置,那么在sysfs中kobject将被映射
* 为其父目录下的子目录;未设置,则将被映射为kset->kobject下的子目录。如果parent和kset
* 都没有被设置,那么我们认为当前kobject没有父对象,因此会被映射成sysfs下的根级目录。
* tips:如果kobject一开始就不打算设置为根级目录,那么最好在add前设置好parent或kset。
*
*/
int kobject_add(struct kobject *kobj, struct kobject *parent,
const char *fmt, ...)
{
va_list args;
int retval;
if (!kobj)
return -EINVAL;
if (!kobj->state_initialized) {
pr_err("kobject '%s' (%p): tried to add an uninitialized object, something is seriously wrong.\n",
kobject_name(kobj), kobj);
dump_stack();
return -EINVAL;
}
va_start(args, fmt);
retval = kobject_add_varg(kobj, parent, fmt, args);
va_end(args);
return retval;
}
kobject_add()中的关键是kobject_add_varg(kobj, parent, fmt, args);
3.2.1 kobject_add_varg
该函数设置了kobject的名称、获得父对象并创建sysfs。
static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
struct kobject *parent,
const char *fmt, va_list vargs)
{
int retval;
retval = kobject_set_name_vargs(kobj, fmt, vargs);//设置kobject名称
if (retval) {
pr_err("kobject: can not set name properly!\n");
return retval;
}
kobj->parent = parent;//获得父对象
return kobject_add_internal(kobj);//创建sysfs
}
在kobject_add_internal()函数中调用**kobject_add_internal()**函数来实现sysfs的创建。
kobject_add_internal()主要工作:(1)判断kobject是否存在父对象,以及对父对象的设置;(2)创建sysfs目录
static int kobject_add_internal(struct kobject *kobj)
{
int error = 0;
struct kobject *parent;
if (!kobj)
return -ENOENT;
if (!kobj->name || !kobj->name[0]) {
WARN(1,
"kobject: (%p): attempted to be registered with empty name!\n",
kobj);
return -EINVAL;
}
parent = kobject_get(kobj->parent);//获取该kobject的父对象指针
/* join kset if set, use it as parent if we do not already have one */
/* 下面这一段判断该kobject的kset是否被设置并为kobj->parent赋值。
如果当前object还没设置父对象, 则引用设置的kset对象为父对象
如果设置了,则将其加入到其设置的kset集合中(将其挂载到kset的链表上)
然后设置父对象,即初始化kobj->parent*/
if (kobj->kset) {
if (!parent)
parent = kobject_get(&kobj->kset->kobj);
kobj_kset_join(kobj);
kobj->parent = parent;
}
pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
kobject_name(kobj), kobj, __func__,
parent ? kobject_name(parent) : "<NULL>",
kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
error = create_dir(kobj);//根据kobj->ktype创建sysfs目录
if (error) {
kobj_kset_leave(kobj);
kobject_put(parent);
kobj->parent = NULL;
/* be noisy on error issues */
if (error == -EEXIST)
pr_err("%s failed for %s with -EEXIST, don't try to register things with the same name in the same directory.\n",
__func__, kobject_name(kobj));
else
pr_err("%s failed for %s (error: %d parent: %s)\n",
__func__, kobject_name(kobj), error,
parent ? kobject_name(parent) : "'none'");
} else
kobj->state_in_sysfs = 1;
return error;
}
3.2.2 小结
整体来看kobject_add()就是创建kobject的sysfs。
四、总结
源码是最好的教材,别偷懒了,回去吧。
tips:错误处理的部分可以选择不读,请重点关注加厚字体描述的内容。
以上,与君共勉。
【声明】本博文为个人学习笔记,仅供参考。转发请注明出处。如发现有误,还请不吝赐教,谢谢!