linux对象系统---kobject, ktype, kset, subsys

本文转自:linux中kobject/ktype/kset/subsys之间的关系


随着内核版本的发展,会有一些变化,无论怎样,变化的是形式,不变的是思想! 

那么他们之间具有什么关系?那应该不是‘小3‘也不是'小5‘的关系,总之这种关系超越了人们,构成了Linux,是一种"你中有我,我中有你“的关系,其实关系复杂了,语言是难以描述的,不过还是先从文字开始吧。本文基于内核版本linux2.6.30.4,从分析bus总线来初步了解kobj, ktype,kset,subsys关系.因为bus总线本身是一个容器,包含了它们!

kobject,ktpye,kset可以说是Linux内核最基本,也是最重要的数据结构了。如果把Linux比做一栋大夏,那么它们就是大夏的基石。

1. kobject
kobject第一次出现是在内核版本2.5.45,是Linux 2.6后引入的新的设备管理机制,在内核中由struct kobject表示。通过这个数据结构使所有设备在底层都具有统一的接口,kobject提供基本的对象管理,是构成Linux2.6设备模型的核心结构,它与sysfs文件系统紧密关联,每个在内核中注册的kobject对象都对应于sysfs文件系统中的一个目录。Kobject是组成设备模型的基本结构。类似于C++中的基类,它嵌入于更大的对象的对象中--所谓的容器--用来描述设备模型的组件。如bus,devices, drivers 都是典型的容器。这些容器就是通kobject连接起来了,形成了一个树状结构。这个树状结构就与/sys相对应。
A kobject is an object of type struct kobject. Kobjects have a name and a reference count. A kobject also has a parent pointer (allowing kobjects to be arranged into hierarchies), a specific type, and, perhaps, a representation in the sysfs virtual filesystem.
Kobjects are generally not interesting on their own; instead, they are usually embedded within some other structure which contains the stuff the code is really interested in.
[cpp]  view plain copy
  1. struct kobject {  
  2. const char *name;  
  3. struct list_headentry;  
  4. struct kobject*parent;   
  5. struct kset *kset;    
  6. struct kobj_type*ktype;  
  7. struct sysfs_dirent*sd;  
  8. struct kref kref;  
  9. unsigned int state_initialized:1;  
  10. unsigned int state_in_sysfs:1;  
  11. unsigned int state_add_uevent_sent:1;  
  12. unsigned int state_remove_uevent_sent:1;  
  13. unsigned int uevent_suppress:1;  
  14. };  
2. ktype
Kobj type数据结构包含三个域:一个release方法用于释放kobject占用的资源;一个sysfs ops指针指向sysfs操作表和一个sysfs文件系统缺省属性列表。Sysfs操作表包括两个函数store()和show()。当用户态读取属性时,show()函数被调用,该函数编码指定属性值存入buffer中返回给用户态;而store()函数用于存储用户态传入的属性值。
A ktype is a type associated with a kobject. The ktype controls what happens when a kobject is no longer referenced and the kobject's default representation in sysfs. 
[cpp]  view plain copy
  1. struct kobj_type {  
  2. void (*release)(struct kobject *kobj);  
  3. struct sysfs_ops *sysfs_ops;  
  4. struct attribute **default_attrs;  
  5. };  

其中struct sysfs_ops 定义如下:

[cpp]  view plain copy
  1. struct sysfs_ops {  
  2.     ssize_t (*show)(struct kobject *, struct attribute *,char *);  
  3.     ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);  
  4. };  

例如bus的ktype是这样定义的:

[cpp]  view plain copy
  1. static struct sysfs_ops bus_sysfs_ops = {  
  2.     .show   = bus_attr_show,  
  3.     .store  = bus_attr_store,  
  4. };  
  5. static struct kobj_type bus_ktype = {  
  6.     .sysfs_ops  = &bus_sysfs_ops,  
  7. };  

3.kset

kset最重要的是建立上层(sub-system)和下层的(kobject)的关联性。kobject 也会利用它来分辨自已是属于那一個类型,然後在/sys 下建立正确的目录位置。而kset 的优先权比较高,kobject会利用自已的*kset 找到自已所属的kset,并把*ktype 指定成该kset下的ktype,除非沒有定义kset,才会用ktype來建立关系。Kobject通过kset组织成层次化的结构,kset是具有相同类型的kobject的集合,也可以说kset具有kobject所有的功能.
A kset is a group of kobjects all of which are embedded in structures of the same type. The kset is the basic container type for collections of kobjects. Ksets contain their own kobjects, for what it's worth. Among other things, that means that a kobject's parent is usually the kset that contains it, though things do not normally have to be that way.
When you see a sysfs directory full of entries, generally each of those entries corresponds to a kobject in the same kset.
[cpp]  view plain copy
  1. struct kset {  
  2. struct list_head list; //the list of all kobjects for this kset  
  3. spinlock_t list_lock; //a lock for iterating over the kobjects  
  4. struct kobject kobj; //the embedded kobject for this kset (recursion, isn't it fun...)  
  5. struct kset_uevent_ops *uevent_ops;//the set of uevent operations for this kset.  
  6. };  

总线初始化buses_init时,建立了bus_kset.

[cpp]  view plain copy
  1. static struct kset *bus_kset;  
  2. int __init buses_init(void)  
  3. {  
  4.     bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);  
  5.     if (!bus_kset)  
  6.         return -ENOMEM;  
  7.     return 0;  
  8. }  

4.subsystem

如果说kset是管理kobject 的集合,那么subsystem 就是管理kset 的集合。它描述系统中某一类设备子系统,如block subsys表示所有的块设备,对应于sysfs文件系统中的block目录。类似的,devices subsys对应于sysfs中的devices目录,描述系统中所有的设备。一个具体总线可称之为subsystem.如I2C,SPI子系统等.它们是bus的子系统.
A subsystem is a collection of ksets which, collectively, make up a major sub-part of the kernel. Subsystems normally correspond to the top-level directories in sysfs. 值得说明的是在以前的版本中内核是有一个struct subsystem 来描述subsys,但后来被去掉了,不过subsystem这一思想依旧存在.注意到在linux2.6.30.4版本中bus_register()中的struct bus_type_private *priv的机构作为bus的susbsystem,但在linux3.0版本后就被struct subsys_private *priv; 取代。可见subsystem变化的只是形式,思想一直没变。
[cpp]  view plain copy
  1. struct bus_type_private {  
  2.     struct kset subsys;  
  3.     struct kset *drivers_kset;  
  4.     struct kset *devices_kset;  
  5.     struct klist klist_devices;  
  6.     struct klist klist_drivers;  
  7.     struct blocking_notifier_head bus_notifier;  
  8.     unsigned int drivers_autoprobe:1;  
  9.     struct bus_type *bus;  
  10. };  

在bus下注册一个总线的过程,就是形成一个子系统subsystem的过程:

[cpp]  view plain copy
  1. int bus_register(struct bus_type *bus)  
  2. {  
  3.     int retval;  
  4.     struct bus_type_private *priv;  
  5.       
  6.     priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);  
  7.         ...  
  8.     retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);  
  9.     if (retval)  
  10.         goto out;  
  11.     priv->subsys.kobj.kset = bus_kset;    //  
  12.     priv->subsys.kobj.ktype = &bus_ktype; //  
  13.     priv->drivers_autoprobe = 1;  
  14.              ...  
  15. }  
 

 

下面这个图比较经典,直观的反映了kset 和kobj之间的关系

kset 本身嵌有一个kobj实体作为所以同类kobj的'父母亲',同时还维护一个链表kset child list, 这个链表中所有的kobj的kset分别指向“父母亲"的kset.


最后不得不说,要全部了解其中关系,涉及的东西太多了,要学习的东西也太多了,不过,不积跬步,无以至千里,继续努力!以后会继续结合内核源码分析....

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值