這篇文章太重要了,所以必須轉載。感謝作者
一、Kobject
- struct kobject {
- const char *name; //kobject的名稱
- struct list_head entry; //kobject結構鏈表
- struct kobject *parent; //父kobject結構體
- struct kset *kset; //kset集合
- struct kobj_type *ktype; //kobject的類型描述符
- struct sysfs_dirent *sd; //sysfs文件目錄
- struct kref kref; //kobject引用計數
- unsigned int state_initialized:1; //kobject是否初始化
- unsigned int state_in_sysfs:1; //是否已經加入sysfs
- unsigned int state_add_uevent_sent:1;
- unsigned int state_remove_uevent_sent:1;
- unsigned int uevent_suppress:1;
- };
sysfs組織結構,進入sysfs目錄中。有block bus class dev devices firmware fs kernel module power這些目錄。具體代表看名字差不多就可以看出。在層次結構上,假如有一個設備A。將有一個名稱爲A的目錄。A設備是在B總線上。那A設備應該在bus目錄下的B總線下。A設備肯定會有設備的屬性(ktype),例如是音頻設備則應該有音量屬性,則音量屬性將在A設備目錄下有個音量屬性文件。在使用設備時,如果要改變音量大小,則可以寫屬性文件入音量指。得到音量大小時,可以讀取屬性文件中的音量值。
- /**
- * kobject_init - initialize a kobject structure
- * @kobj: pointer to the kobject to initialize
- * @ktype: pointer to the ktype for this kobject.
- *
- * This function will properly initialize a kobject such that it can then
- * be passed to the kobject_add() call.
- *
- * After this function is called, the kobject MUST be cleaned up by a call
- * to kobject_put(), not by a call to kfree directly to ensure that all of
- * the memory is cleaned up properly.
- */
- void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
- {
- char *err_str;
- if (!kobj) { //檢查kobj變量是否爲空
- err_str = "invalid kobject pointer!";
- goto error;
- }
- if (!ktype) { //檢查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 */
- printk(KERN_ERR "kobject (%p): tried to init an initialized "
- "object, something is seriously wrong.\n", kobj);
- dump_stack();
- }
- kobject_init_internal(kobj); //進一步初始化kobj內部成員
- kobj->ktype = ktype; //將參數中傳來的ktype變量賦值給kobj的ktype變量。
- return;
- error:
- printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
- dump_stack();
- }
分析kobject_init_internal函數(lib/kobject.c),此函數主要設置一些kobj中的一些變量
- static void kobject_init_internal(struct kobject *kobj)
- {
- if (!kobj) //kobj是否爲空
- return;
- kref_init(&kobj->kref); //增加kobject的引用計數,kref_set(kref, 1);
- INIT_LIST_HEAD(&kobj->entry); //初始化kobj的鏈表
- kobj->state_in_sysfs = 0; //kobject還沒有註冊到sysfs中
- kobj->state_add_uevent_sent = 0; //
- kobj->state_remove_uevent_sent = 0;
- kobj->state_initialized = 1;
- }
三。kobj_type
- struct kobj_type {
- void (*release)(struct kobject *kobj); //釋放函數(驅動編寫時提供),此函數會被kobject_put函數調用
- struct sysfs_ops *sysfs_ops; //屬性文件的操作函數(只有讀和寫操作)
- struct attribute **default_attrs; //屬性數組
- };
1.討論kobj_type和kobject的關係,就要先說說kobject的引用。引用一個kobject使用函數kobject_get()這個函數會增加kobject的引用並返回kobject的指針。增加其引用是通過其kobject中斷哦kref變量完成的。對kobject的引用管理主要是爲了知道被引用的情況,如引用不爲0就不能銷燬kobject對象,引用爲0時則調用相應的釋放函數等。
- struct kobject *kobject_get(struct kobject *kobj)
- {
- if (kobj)
- kref_get(&kobj->kref);
- return kobj;
- }
- void kref_get(struct kref *kref)
- {
- WARN_ON(!atomic_read(&kref->refcount));
- atomic_inc(&kref->refcount); //將kref中的這個原子變量加1
- smp_mb__after_atomic_inc();
- }
- void kobject_put(struct kobject *kobj)
- {
- if (kobj) {
- if (!kobj->state_initialized) //若kobj沒有初始化就不能減少其引用
- WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
- "initialized, yet kobject_put() is being "
- "called.\n", kobject_name(kobj), kobj);
- kref_put(&kobj->kref, kobject_release); //減少kref計數
- }
- }
- int kref_put(struct kref *kref, void (*release)(struct kref *kref))
- {
- WARN_ON(release == NULL); //爲空警告
- WARN_ON(release == (void (*)(struct kref *))kfree); //如果release函數就是kfree,則警告(即release函數不能是簡單的kfree)
- if (atomic_dec_and_test(&kref->refcount)) { //遞減原子變量並檢查其值
- release(kref); //回調release函數
- return 1;
- }
- return 0;
- }
- /**
- * 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; //這個鏈表存放這個kset關聯的所有kobject
- spinlock_t list_lock; //維護此鏈表的鎖
- struct kobject kobj; //內嵌的kobject。這樣kset本身也是一個kobject也被表現爲一個目錄
- struct kset_uevent_ops *uevent_ops; //支持熱插拔事件的函數集
- };
- struct kset_uevent_ops {
- int (*filter)(struct kset *kset, struct kobject *kobj); //事件過濾函數
- const char *(*name)(struct kset *kset, struct kobject *kobj); //事件名稱函數
- int (*uevent)(struct kset *kset, struct kobject *kobj,struct kobj_uevent_env *env);
- //uevent函數可在熱插拔程序執行前,向環境變量寫值
- };
- /**
- * kset_register - initialize and add a kset.
- * @k: kset.
- */
- int kset_register(struct kset *k)
- {
- int err;
- if (!k)
- return -EINVAL;
- kset_init(k);
- err = kobject_add_internal(&k->kobj); //將kset中的kobject添加進sysfs,函數將在後面講解
- if (err)
- return err;
- kobject_uevent(&k->kobj, KOBJ_ADD);
- return 0;
- }
- /**
- * kset_init - initialize a kset for use
- * @k: kset
- */
- void kset_init(struct kset *k)
- {
- kobject_init_internal(&k->kobj);
- INIT_LIST_HEAD(&k->list);
- spin_lock_init(&k->list_lock);
- }
- /**
- * kset_create_and_add - create a struct kset dynamically and add it to sysfs
- *
- * @name: the name for the kset
- * @uevent_ops: a struct kset_uevent_ops for the kset
- * @parent_kobj: the parent kobject of this kset, if any.
- *
- * This function creates a kset structure dynamically and registers it
- * with sysfs. When you are finished with this structure, call
- * kset_unregister() and the structure will be dynamically freed when it
- * is no longer being used.
- *
- * If the kset was not able to be created, NULL will be returned.
- */
- struct kset *kset_create_and_add(const char *name,
- struct kset_uevent_ops *uevent_ops,
- struct kobject *parent_kobj)
- {
- struct kset *kset;
- int error;
- kset = kset_create(name, uevent_ops, parent_kobj); //根據參數創建一個kset
- if (!kset)
- return NULL;
- error = kset_register(kset); //將kset註冊進sysfs,函數在上面已經分析過
- if (error) {
- kfree(kset);
- return NULL;
- }
- return kset;
- }
- /**
- * kset_create - create a struct kset dynamically
- *
- * @name: the name for the kset
- * @uevent_ops: a struct kset_uevent_ops for the kset
- * @parent_kobj: the parent kobject of this kset, if any.
- *
- * This function creates a kset structure dynamically. This structure can
- * then be registered with the system and show up in sysfs with a call to
- * kset_register(). When you are finished with this structure, if
- * kset_register() has been called, call kset_unregister() and the
- * structure will be dynamically freed when it is no longer being used.
- *
- * If the kset was not able to be created, NULL will be returned.
- */
- static struct kset *kset_create(const char *name,
- struct kset_uevent_ops *uevent_ops,
- struct kobject *parent_kobj)
- {
- struct kset *kset;
- int retval;
- kset = kzalloc(sizeof(*kset), GFP_KERNEL);
- if (!kset)
- return NULL;
- retval = kobject_set_name(&kset->kobj, name); //設置kobject名稱
- if (retval) {
- kfree(kset);
- return NULL;
- }
- kset->uevent_ops = uevent_ops;
- kset->kobj.parent = parent_kobj; //設置kset的kobject的父對象
- /*
- * The kobject of this kset will have a type of kset_ktype and belong to
- * no kset itself. That way we can properly free it when it is
- * finished being used.
- */
- kset->kobj.ktype = &kset_ktype; //設置kset的kobject的默認屬性
- kset->kobj.kset = NULL;
- return kset;
- }
上面這些函數主要包含即調用關係如下,
- static inline struct kset *kset_get(struct kset *k)
- {
- return k ? to_kset(kobject_get(&k->kobj)) : NULL;
- }
減少kset的引用,實際是調用kobject_put減少kset中的kobject的引用
- static inline void kset_put(struct kset *k)
- {
- kobject_put(&k->kobj);
- }
順便提一下,子系統subsystem,在新的內核中已經沒有這個結構了。在原來的內核中它用來表示比kset更高一層的容器,kset應該屬於一個子系統,子系統幫助內核在分層結構中定位kset。內核子系統包括 block_subsys(/sys/block 塊設備)、 devices_subsys(/sys/devices 核心設備層)。現在subsystem已經被kset代替了。
- /**
- * 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, ...)
- {
- va_list args;
- int retval;
- kobject_init(kobj, ktype); //調用初始化函數先初始化kobject變量
- va_start(args, fmt); //解析可變參數列表
- retval = kobject_add_varg(kobj, parent, fmt, args); //給kobject添加參數,並且將其添加到sysfs系統。
- va_end(args); //結束解析參數列表
- return retval;
- }
- static 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) {
- printk(KERN_ERR "kobject: can not set name properly!\n");
- return retval;
- }
- kobj->parent = parent; //設置kobject的父kobject
- return kobject_add_internal(kobj); //添加kobject
- }
- static int kobject_add_internal(struct kobject *kobj)
- {
- int error = 0;
- struct kobject *parent;
- if (!kobj) //檢查是否爲空
- return -ENOENT;
- if (!kobj->name || !kobj->name[0]) { //kobj是否有名稱,如果沒有則不能註冊,生成目錄。
- WARN(1, "kobject: (%p): attempted to be registered with empty "
- "name!\n", kobj);
- return -EINVAL;
- }
- parent = kobject_get(kobj->parent); //獲得父kobject,並增加父kobject的引用計數
- /* join kset if set, use it as parent if we do not already have one */
- if (kobj->kset) { //是否有kset集合
- if (!parent) //如果沒有父kobject則用kset中的kobject對象
- parent = kobject_get(&kobj->kset->kobj);
- kobj_kset_join(kobj); //將kobject添加進它關聯的kset的list鏈表中。
- kobj->parent = parent; //設置父koject
- }
- 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); //創建kobject的相應目錄
- if (error) { //創建時出錯處理
- kobj_kset_leave(kobj);
- kobject_put(parent);
- kobj->parent = NULL;
- /* be noisy on error issues */
- if (error == -EEXIST)
- printk(KERN_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
- printk(KERN_ERR "%s failed for %s (%d)\n",
- __func__, kobject_name(kobj), error);
- dump_stack();
- } else
- kobj->state_in_sysfs = 1; //標記爲已經註冊進sysfs
- return error;
- }
- static int create_dir(struct kobject *kobj)
- {
- int error = 0;
- if (kobject_name(kobj)) {
- error = sysfs_create_dir(kobj); //在sysfs中創建目錄,將來有時間了可以分析下sysfs子系統。
- if (!error) {
- error = populate_dir(kobj);
- if (error)
- sysfs_remove_dir(kobj);
- }
- }
- return error;
- }
以上函數的主要調用關係,如下
- 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) {
- printk(KERN_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;
- }