• kobject 与sysfs属性文件读写


    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 中的顶级目录。

    1. struct kobject {//目录抽象---基类
    2. const char *name;
    3. struct list_head entry;
    4. struct kobject *parent;
    5. struct kset *kset;//subsystem or dir
    6. struct kobj_type *ktype;
    7. struct kernfs_node *sd;//sysfs
    8. struct kref kref;//refcount
    9. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
    10. struct delayed_work release;
    11. #endif
    12. unsigned int state_initialized:1;
    13. unsigned int state_in_sysfs:1;
    14. unsigned int state_add_uevent_sent:1;
    15. unsigned int state_remove_uevent_sent:1;
    16. unsigned int uevent_suppress:1;
    17. };
    18. kobj_type代表Kobject(严格地讲,是包含了Kobject的数据结构)的属性操作集合(由于通用性,多个Kobject可能共用同一个属性操作集,因此把Ktype独立出来了)
    19. struct kobj_type {
    20. void (*release)(struct kobject *kobj);//kobject release func when kobject refcont is 0
    21. const struct sysfs_ops *sysfs_ops;//目录的操作ops----》attribute中的对应的ops,也就是目录---》文件
    22. struct attribute **default_attrs;//设置的默认属性 会被框架自动创建
    23. const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
    24. const void *(*namespace)(struct kobject *kobj);
    25. };
    26. This structure is used to describe a particular type of kobject (or, more correctly, of containing object).
    27. Every kobject needs to have an associated kobj_type structure; a pointer to that structure must be specified when you
    28. call kobject_init() or kobject_init_and_add().
    29. 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
    30. sysfs; they are beyond the scope of this document.
    31. The default_attrs pointer is a list of default attributes that will be automatically created for any kobject that is registered with this ktype
    32. kset 与 kobj 都是目录,既然是目录,那么应该就是一个树状结构,每一个目录都将有一个父节点:
    33. 在kset中使用kset.kobj->parent 指定,在kboject中使用 kobj->parent 指定
    34. kset 主要用途 是发送uevent和 创建同一类kobject。
    35. /**
    36. * struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
    37. *
    38. * A kset defines a group of kobjects. They can be individually different "types" but overall these kobjects all want to be grouped
    39. * together and operated on in the same manner. ksets are used to define the attribute callbacks and other common events that happen to
    40. * a kobject.
    41. *
    42. * @list: the list of all kobjects for this kset
    43. * @list_lock: a lock for iterating over the kobjects
    44. * @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
    45. * @uevent_ops: the set of uevent operations for this kset. These are
    46. * called whenever a kobject has something happen to it so that the kset
    47. * can add new environment variables, or filter out the uevents if so
    48. * desired.
    49. */
    50. struct kset {// a group of kobjects
    51. struct list_head list;//the list of all kobjects for this kset
    52. spinlock_t list_lock;
    53. struct kobject kobj;//基类,kset也是一种kobj
    54. const struct kset_uevent_ops *uevent_ops;//some event to user space
    55. };

     


    kobject 注册函数

    1. /**
    2. * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
    3. * @kobj: pointer to the kobject to initialize
    4. * @ktype: pointer to the ktype for this kobject.
    5. * @parent: pointer to the parent of this kobject.
    6. * @fmt: the name of the kobject.
    7. *
    8. * This function combines the call to kobject_init() and
    9. * kobject_add(). The same type of error handling after a call to
    10. * kobject_add() and kobject lifetime rules are the same here.
    11. */
    12. int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
    13. struct kobject *parent, const char *fmt, ...)//ktype need init and set
    14. {
    15. va_list args;
    16. int retval;
    17. kobject_init(kobj, ktype);//init,fill kobject struct
    18. va_start(args, fmt);
    19. retval = kobject_add_varg(kobj, parent, fmt, args);
    20. va_end(args);
    21. return retval;
    22. }

     kobject_add_varg--》kobject_add_internal

    1. static int kobject_add_internal(struct kobject *kobj)
    2. {
    3. int error = 0;
    4. struct kobject *parent;
    5. if (!kobj)
    6. return -ENOENT;
    7. if (!kobj->name || !kobj->name[0]) {
    8. WARN(1, "kobject: (%p): attempted to be registered with empty "
    9. "name!\n", kobj);
    10. return -EINVAL;
    11. }
    12. parent = kobject_get(kobj->parent);
    13. /* join kset if set, use it as parent if we do not already have one */
    14. /*如果当前object还没设置父对象, 则引用设置的kset对象为父对象, 如果设置了,则将其加入到其设置的kset集合中(将其挂载到kset的链表上)
    15. 然后设置父对象,即初始化kobj->parent*/
    16. if (kobj->kset) {
    17. if (!parent)
    18. parent = kobject_get(&kobj->kset->kobj);
    19. kobj_kset_join(kobj);
    20. kobj->parent = parent;
    21. }
    22. pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
    23. kobject_name(kobj), kobj, __func__,
    24. parent ? kobject_name(parent) : "",
    25. kobj->kset ? kobject_name(&kobj->kset->kobj) : "");
    26. error = create_dir(kobj);//creat and fill
    27. if (error) {
    28. kobj_kset_leave(kobj);
    29. kobject_put(parent);
    30. kobj->parent = NULL;
    31. /* be noisy on error issues */
    32. if (error == -EEXIST)
    33. WARN(1, "%s failed for %s with "
    34. "-EEXIST, don't try to register things with "
    35. "the same name in the same directory.\n",
    36. __func__, kobject_name(kobj));
    37. else
    38. WARN(1, "%s failed for %s (error: %d parent: %s)\n",
    39. __func__, kobject_name(kobj), error,
    40. parent ? kobject_name(parent) : "'none'");
    41. } else
    42. kobj->state_in_sysfs = 1;//normal is 1
    43. return error;
    44. }

     creat_dir 部分注释

    1. static int create_dir(struct kobject *kobj)//creat dir at sysfs
    2. {
    3. const struct kobj_ns_type_operations *ops;
    4. int error;
    5. error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));//kobject-->sysfs
    6. if (error)
    7. return error;
    8. error = populate_dir(kobj);//有attr的话创建 attr file throught sysfs
    9. if (error) {
    10. sysfs_remove_dir(kobj);
    11. return error;
    12. }
    13. /*
    14. * @kobj->sd may be deleted by an ancestor going away. Hold an
    15. * extra reference so that it stays until @kobj is gone.
    16. */
    17. sysfs_get(kobj->sd);
    18. /*
    19. * If @kobj has ns_ops, its children need to be filtered based on
    20. * their namespace tags. Enable namespace support on @kobj->sd.
    21. */
    22. ops = kobj_child_ns_ops(kobj);
    23. if (ops) {
    24. BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE);
    25. BUG_ON(ops->type >= KOBJ_NS_TYPES);
    26. BUG_ON(!kobj_ns_type_registered(ops->type));
    27. sysfs_enable_ns(kobj->sd);
    28. }
    29. return 0;
    30. }

    kset 注册过程

    1. /**
    2. * kset_register - initialize and add a kset.
    3. * @k: kset.
    4. */
    5. int kset_register(struct kset *k)//first init,then add
    6. {
    7. int err;
    8. if (!k)
    9. return -EINVAL;
    10. kset_init(k);
    11. err = kobject_add_internal(&k->kobj);
    12. if (err)
    13. return err;
    14. kobject_uevent(&k->kobj, KOBJ_ADD);//通过uevent机制通知用户空间
    15. return 0;
    16. }

    kobjkset关系总结

    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 文件打开,读,写过程代码总结

  • 相关阅读:
    稳定的排序算法:直接插入排序和冒泡排序 (c++实现)
    强软弱虚引用
    Day798.Java编译 -Java 性能调优实战
    Java程序员进阶全过程
    Python数据分析训练营——Python数据分析之Numpy
    element-ui日期选择器el-date-picker, 案例:填写有效期和选择开始时间后, 自动生成结束时间, datetime时间转换
    jenkins的安装与配置(超详细)
    File、Blob、FileReader、ArrayBuffer、Base64
    Java老人护理上门服务类型系统小程序APP源码
    C++线程创建的方式和使用
  • 原文地址:https://blog.csdn.net/xuelin273/article/details/128035666