kobject和kset的简单总结
• kobject是struct kobject类型的对象。Kobject 有一个名字和一个引用计数。kobject 也有一个父指针(允许 kobjects 被安排到层次结构中),一个特定的类型,也许还有一个在 sysfs 虚拟文件系统中的表示。Kobject 本身通常并不有趣;相反,它们通常嵌入在一些其他结构中,其中包含代码真正感兴趣的内容。
• ktype是与 kobject 关联的类型。ktype 控制当一个 kobject 不再被引用时会发生什么,以及 kobject 在 sysfs 中的默认表示。
• kset是一组 kobjects,所有这些 kobjects 都嵌入在相同类型的结构中。kset 是 kobject 集合的基本容器类型。Ksets 包含它们自己的 kobjects,这是值得的。除其他外,这意味着一个 kobject 的父对象通常是包含它的 kset,尽管事情通常不必那样。当您看到充满条目的 sysfs 目录时,通常这些条目中的每一个都对应于同一 kset 中的一个 kobject。
• A subsystem是 kset的集合,它们共同构成内核的主要子部分。子系统通常对应于 sysfs 中的顶级目录。
struct kobject {//目录抽象---基类
const char *name;
struct list_head entry;
struct kobject *parent;
struct kset *kset;//subsystem or dir
struct kobj_type *ktype;
struct kernfs_node *sd;//sysfs
struct kref kref;//refcount
#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;
};
kobj_type代表Kobject(严格地讲,是包含了Kobject的数据结构)的属性操作集合(由于通用性,多个Kobject可能共用同一个属性操作集,因此把Ktype独立出来了)
struct kobj_type {
void (*release)(struct kobject *kobj);//kobject release func when kobject refcont is 0
const struct sysfs_ops *sysfs_ops;//目录的操作ops----》attribute中的对应的ops,也就是目录---》文件
struct attribute **default_attrs;//设置的默认属性 会被框架自动创建
const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
const void *(*namespace)(struct kobject *kobj);
};
This structure is used to describe a particular type of kobject (or, more correctly, of containing object).
Every kobject needs to have an associated kobj_type structure; a pointer to that structure must be specified when you
call kobject_init() or kobject_init_and_add().
The release field in struct kobj_type is, of course, a pointer to the release() method for this type of kobject. The other two fields (sysfs_ops and default_attrs) control how objects of this type are represented in
sysfs; they are beyond the scope of this document.
The default_attrs pointer is a list of default attributes that will be automatically created for any kobject that is registered with this ktype
kset 与 kobj 都是目录,既然是目录,那么应该就是一个树状结构,每一个目录都将有一个父节点:
在kset中使用kset.kobj->parent 指定,在kboject中使用 kobj->parent 指定
kset 主要用途 是发送uevent和 创建同一类kobject。
/**
* 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 {// a group of kobjects
struct list_head list;//the list of all kobjects for this kset
spinlock_t list_lock;
struct kobject kobj;//基类,kset也是一种kobj
const struct kset_uevent_ops *uevent_ops;//some event to user space
};
kobject 注册函数
/**
* kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
* @kobj: pointer to the kobject to initialize
* @ktype: pointer to the ktype for this kobject.
* @parent: pointer to the parent of this kobject.
* @fmt: the name of the kobject.
*
* This function combines the call to kobject_init() and
* kobject_add(). The same type of error handling after a call to
* kobject_add() and kobject lifetime rules are the same here.
*/
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
struct kobject *parent, const char *fmt, ...)//ktype need init and set
{
va_list args;
int retval;
kobject_init(kobj, ktype);//init,fill kobject struct
va_start(args, fmt);
retval = kobject_add_varg(kobj, parent, fmt, args);
va_end(args);
return retval;
}
kobject_add_varg--》kobject_add_internal
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);
/* join kset if set, use it as parent if we do not already have one */
/*如果当前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);//creat and fill
if (error) {
kobj_kset_leave(kobj);
kobject_put(parent);
kobj->parent = NULL;
/* be noisy on error issues */
if (error == -EEXIST)
WARN(1, "%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
WARN(1, "%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;//normal is 1
return error;
}
creat_dir 部分注释
static int create_dir(struct kobject *kobj)//creat dir at sysfs
{
const struct kobj_ns_type_operations *ops;
int error;
error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));//kobject-->sysfs
if (error)
return error;
error = populate_dir(kobj);//有attr的话创建 attr file throught sysfs
if (error) {
sysfs_remove_dir(kobj);
return error;
}
/*
* @kobj->sd may be deleted by an ancestor going away. Hold an
* extra reference so that it stays until @kobj is gone.
*/
sysfs_get(kobj->sd);
/*
* If @kobj has ns_ops, its children need to be filtered based on
* their namespace tags. Enable namespace support on @kobj->sd.
*/
ops = kobj_child_ns_ops(kobj);
if (ops) {
BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE);
BUG_ON(ops->type >= KOBJ_NS_TYPES);
BUG_ON(!kobj_ns_type_registered(ops->type));
sysfs_enable_ns(kobj->sd);
}
return 0;
}
kset 注册过程
/**
* kset_register - initialize and add a kset.
* @k: kset.
*/
int kset_register(struct kset *k)//first init,then add
{
int err;
if (!k)
return -EINVAL;
kset_init(k);
err = kobject_add_internal(&k->kobj);
if (err)
return err;
kobject_uevent(&k->kobj, KOBJ_ADD);//通过uevent机制通知用户空间
return 0;
}
kobj、kset关系总结
kobj和kset并不是完全的父子关系。kset算是kobj的“接盘侠”,当kobj没有所属的parent时,才让kset来接盘当parent;如果连kset也没有,那该kobj属于顶层对象,其sysfs目录将位于/sys/下。正因为kobj和kset并不是完全的父子关系,因此在注册kobj时,将同时对parent及其所属的kset增加引用计数。若parent和kset为同一对象,则会对kset增加两次引用计数。
kset内部本身也包含一个kobj对象,在sysfs中也表现为目录;所不同的是,kset要承担kobj状态变动消息的发送任务。因此,首先kset会将所属的kobj组织在kset.list下,同时,通过uevent_ops在合适时候发送消息。
对于kobject_add()来说,它的输入信息是:kobj-parent、kobj-name,kobject_add()优先使用传入的parent作为kobj->parent;其次,使用kset作为kobj->parent。
kobj状态变动后,必须依靠所关联的kset来向用户空间发送消息;若无关联kset(该kobj向上组成的树中,任何成员都无所属的kset),则kobj无法发送用户消息。
sysfs 文件打开,读,写过程代码总结