world leader in high performance signal processing
Trace:

Differences

This shows you the differences between two versions of the page.

kernel_objects [2006/12/08 01:02]
rgetz
kernel_objects [2010/09/09 22:02] (current)
Line 1: Line 1:
 ==== Kernel Objects and Sysfs ==== ==== Kernel Objects and Sysfs ====
-This section contains  
  
-   * History +The ''/proc'' filesystem emerged as a superb tool during the development of the 2.4 kernel series.
-   * Sysfs Brief Overview +
-   * Kernel Objects Overview +
-   * Sysfs Basic Example+
  
 +It provided "instant" access to and control of kernel parameters in a user friendly manner such that by simply using "cat" and "echo" kernel features could be examined and manipulated.
  
-==== Proc FS History ====+Lack of documentation and a non unified structure caused a some what haphazard application of this excellent feature.
  
-The **/proc** filesystem emerged as a superb tool during the +An attempt was made in the 2.4 kernel to provide a more uniform approach to the concept using the SYSCTL interface.
-development of the 2.4 kernel series.+
  
-It provided "instant" access to and control of kernel parameters in +The 2.6 kernel permitted major change to the whole infrastructure and produced what is called **[[wp>sysfs]]**.
-user friendly manner such that by simply using "cat" and "echo" +
-kernel features could be examined and manipulated.+
  
- +Tightly coupled with the sysfs are things called kernel objects (more about those soon)
-Lack of documentation and a non unified structure caused a somewhat +
-haphazard  application of this excellent feature. +
- +
-An attempt was made in the 2.4 kernel to provide a more uniform +
-approach to the concept using the SYSCTL interface. +
- +
-The 2.6 kernel permitted a major change to the whole infrastructure and +
-produced what is called **sysfs**. +
- +
-Tightly coupled with the sysfs are things called kernel objects +
-     (more about those soon)+
  
 ===== A brief Overview of Sysfs ===== ===== A brief Overview of Sysfs =====
  
 +Sysfs is a ram based file system like procfs. It allows kernel structures to be exported and their attributes and relationships to be visible from userspace.
  
-Sysfs is a ram based file system like procfs. It allows kernel +In fact it has all of the good features of the proc interface but with formal structure and documentation.
-structures to be exported and their attributes and relationships to be +
-visible from userspace.+
  
-In fact it has all of the good features of the proc interface but with +The only possible drawback to the system is the large amount of memory it takes to hold the in memory filesystem. This problem is being addressed in recent kernel patches.
-formal structure and documentation. +
- +
-The only possible drawback to the system is the large amount of memory it +
-takes to hold the in memory filesystem. +
-This problem is being addressed in recent kernel patches.+
  
 Here is an overview of sysfs features Here is an overview of sysfs features
  
- +  * Sysfs is a dynamic system allowing run time events to modify the system.
-  * Sysfs is a dynamic system allowing run time events to modify +
-     the system. +
   * Every Kobject is a directory in sysfs   * Every Kobject is a directory in sysfs
   * Every Kobject is a child of a subsystem   * Every Kobject is a child of a subsystem
-  * An object's attributes are exposed as regular files in the +  * An object's attributes are exposed as regular files in the object's directory.
-     object's directory.+
   * Attributes an be read from or written to using sysfs.   * Attributes an be read from or written to using sysfs.
   * The process of registering a kobject will also create an entry in sysfs.   * The process of registering a kobject will also create an entry in sysfs.
-  * Sysfs will implement a hierarchy for a object providing parents, +  * Sysfs will implement a hierarchy for a object providing parents, subsystem, and type definitions.
-     subsystem, and type definitions.+
   * The subsystem can include links to show how devices are related.   * The subsystem can include links to show how devices are related.
  
 ==== Mounting Sysfs ==== ==== Mounting Sysfs ====
- 
  
 The following command will mount the sysfs on a 2.6 system The following command will mount the sysfs on a 2.6 system
  
 <code> <code>
 +mount -t sysfs sysfs /sys
 +</code>
  
-  mount -t sysfs sysfs /sys +Normally this is done in the ''/etc/rc'' file.
- +
-</code>+
  
 ==== Some sysfs examples ==== ==== Some sysfs examples ====
- 
  
 A tree view of a hard disk in sysfs. A tree view of a hard disk in sysfs.
  
 <code> <code>
- 
 /sys /sys
 |-- block |-- block
Line 104: Line 73:
 Looking at some of the elements Looking at some of the elements
  
-<code >+<code>
 >cat /sys/block/hda/hda2/start >cat /sys/block/hda/hda2/start
 208845 208845
Line 119: Line 88:
 1056767 sectors new seek distance 1056767 sectors new seek distance
 </code> </code>
 +
  
 ==== Sysfs Documentation ==== ==== Sysfs Documentation ====
  
-<p> Unlike the older proc file system the sysfs and associated kernel objects +Unlike the older ''proc'' file system the ''sysfs'' and associated kernel objects do have some documentation and a reference design to look at.
-do have some documentation and a reference design to look at.+
  
-&lt;code&gt;+Documentation: 
 +  * [[http://blackfin.uclinux.org/gf/project/linux-kernel/scmsvn/?action=browse&amp;path=%2Ftrunk%2FDocumentation%2Ffilesystems%2Fsysfs.txt&amp;revision=&view=markup|./Documentation/filesystems/sysfs.txt]] 
 + * [[http://blackfin.uclinux.org/gf/project/linux-kernel/scmsvn/?action=browse&path=%2Ftrunk%2FDocumentation%2Fkobject.txt&revision=&view=markup|./Documentation/kobject.txt]]
  
-Documentation +Reference Example: 
-   linux/Documentation/filesystems/sysfs.txt +  *  [[http://blackfin.uclinux.org/gf/project/linux-kernel/scmsvn/?action=browse&path=%2Ftrunk%2Fblock%2Fgenhd.c&revision=&view=markup|linux/drivers/block/genhd.c]]
- +
-Reference Design +
-   linux/drivers/block/genhd.c +
- +
-</code>+
  
 ===== Kernel Objects Introduction ===== ===== Kernel Objects Introduction =====
  
- +The Kernel Object subsystem is complex. This is an attempt to understand the basics.
-The Kernel Object subsystem is complex. +
-This is an attempt to understand the basics.+
  
 The key feature of a kernel object is the concept of a reference count. The key feature of a kernel object is the concept of a reference count.
  
-The creator of an object can delete that object at any time but any users +The creator of an object can delete that object at any time but any users of that object will cause the object to remain in the system until its reference count falls to zero. At that time the object will be deleted.
-of that object will cause the object to remain in the system until its +
-reference count falls to zero. At that time the object will be deleted. +
  
 === The Kernel Object Players === === The Kernel Object Players ===
- 
  
 The Kernel object basic structure is shown The Kernel object basic structure is shown
  
-<code c> +<source trunk/include/linux/kobject.h:kobject{c linux-kernel>
- +
-struct kobject +
-        char                    * k_name; +
-        char                    name[KOBJ_NAME_LEN]; +
-        atomic_t                refcount; +
-        struct list_head        entry; +
-        struct kobject          * parent; +
-        struct kset             * kset; +
-        struct kobj_type        * ktype; +
-        struct dentry           * dentry; +
-}+
- +
-</code>+
  
 ==== The Kernel Object Elements ==== ==== The Kernel Object Elements ====
  
 In general a Kernel Object needs the following elements In general a Kernel Object needs the following elements
- 
  
   * k_name - The kernel name   * k_name - The kernel name
Line 195: Line 141:
 ==== Use Overview ==== ==== Use Overview ====
  
-<p> In general, when using an object the flow is..+In general, when using an object the flow is...
- +
- +
- +
-  - Define a type that tells the system the object attributes and the release +
-function to delete the object.+
  
 +  - Define a type that tells the system the object attributes and the release function to delete the object.
   - Create a set of objects of the same type belonging to the same subsystem.   - Create a set of objects of the same type belonging to the same subsystem.
-   The set object will be the object's parent. +    * The set object will be the object's parent. Each set has a object type that defines the subsystem and a type that defines the type of objects contained in the set.
-Each set has a object type that defines the subsystem and a type that defines +
-the type of objects contained in the set. +
- +
   - Create a subsystem to act as the root directory ( or use an existing one )   - Create a subsystem to act as the root directory ( or use an existing one )
   - Create the object and fill in the name, parent, set and type fields   - Create the object and fill in the name, parent, set and type fields
   - Register the object (which adds it to the sysfs).   - Register the object (which adds it to the sysfs).
- 
  
 ==== Kset Overview ==== ==== Kset Overview ====
  
-A Kset belongs to a subsystem, it has a type, a list of members and +A Kset belongs to a subsystem, it has a type, a list of members and also contains a kobject of its own.
-   also contains a kobject of its own.+
  
-Ksets also participate in helping with hotplug operations by +Ksets also participate in helping with hotplug operations by specifying a hotplug name and adding items to the hotplug helper's environment.
-   specifying a hotplug name and adding items to the hotplug helper's +
-    environment.+
  
-<code c> +<source trunk/include/linux/kobject.h:kset{} c linux-kernel>
- +
-   struct kset { +
-        struct subsystem        * subsys; +
-        struct kobj_type        * ktype; +
-        struct list_head        list; +
-        struct kobject          kobj; +
-        struct kset_hotplug_ops * hotplug_ops; +
-   }+
- +
-</code>+
  
 ==== Ktype Overview ==== ==== Ktype Overview ====
  
-A Ktype defines the show and store operations that can be performed on the +A Ktype defines the show and store operations that can be performed on the objectit also sets up the default attributes for the object.
-objectit also sets up the default attributes for the object.+
  
-<code c> +<source trunk/include/linux/kobject.h:kobj_type{} c linux-kernel>
- +
-   struct kobj_type { +
-        void (*release)(struct kobject *); +
-        struct sysfs_ops        * sysfs_ops; // just show and store +
-        struct attribute        ** default_attrs; +
-   }; +
- +
-</code>+
  
 ==== Subsystem Overview ==== ==== Subsystem Overview ====
  
-This is a base level system it has a kset whose kobject allows the +This is a base level system it has a kset whose kobject allows the subsystem to be placed in the system hierarchy properly.
-subsystem to be placed in the system hierarchy properly.+
  
- +<source trunk/include/linux/kobject.h:subsystem{} c linux-kernel>
-<code> +
- +
-   struct subsystem { +
-        struct kset             kset; +
-        struct rw_semaphore     rwsem; +
-   }+
- +
-</code>+
  
 ==== Confused ?? ==== ==== Confused ?? ====
  
- +An example will show the basic mechanics of setting up a small subsystem.
-An example will show the basic mechanics of setting up +
-a small subsystem.+
  
    * Remember Kobjects determine the position in the file system    * Remember Kobjects determine the position in the file system
    * The KSET glues a kset to a subsystem    * The KSET glues a kset to a subsystem
- 
- 
- 
  
  
 ===== Kernel Object / Sysfs Example ===== ===== Kernel Object / Sysfs Example =====
  
- +The example discussed will show the basics of setting up a subsystem of a single type of object with attributes.
-The example discussed will show the basics of setting up +
-a subsystem of a single type of object with attributes.+
  
 === The Computer Object === === The Computer Object ===
  
- +The important kernel entity here is the kobject, the attributes are defined by the use of the object.
-The important kernel entity here is the kobject, the attributes are +
-   defined by the use of the object.+
  
 <code c> <code c>
Line 293: Line 190:
 // Step 1 define an object // Step 1 define an object
 // //
-   struct computer { +struct computer { 
-       struct kobject kobj;  /contains the name is also the directory entry +     struct kobject kobj;  /contains the name is also the directory entry */ 
- +                           /user defined attributes follow                */ 
-       // user defined attributes follow +     char make[16]; 
-       char make[16]; +     char ip[16]; 
-       char ip[16]; +     int speed; 
-       int speed; +     int capacity; 
-       int capacity; +     int used; 
-       int used; +};
-   }; +
 </code> </code>
- 
  
 === Define the attributes === === Define the attributes ===
  
- +The attributes of the object will appear as regular files permissions and names need to be set at this stage. The only other element in the attribute structure is the module owner.
-The attributes of the object will appear as regular files +
-    permissions and names need to be set at this stage. +
-    The only other element in the attribute structure is the +
-    module owner. +
  
 Note that no show / store access functions are defined in basic attributes. Note that no show / store access functions are defined in basic attributes.
  
 <code c> <code c>
-// +/* 
-// Step 2 define its attributes + Step 2 define its attributes 
-//+ */ 
 #define COM_MODE ( S_IRUGO | S_IWUGO ) #define COM_MODE ( S_IRUGO | S_IWUGO )
 static struct attribute computer_attr_make ={.name="make",.mode=COM_MODE }; static struct attribute computer_attr_make ={.name="make",.mode=COM_MODE };
Line 328: Line 218:
 static struct attribute computer_attr_capacity={.name="capacity",.mode=COM_MODE}; static struct attribute computer_attr_capacity={.name="capacity",.mode=COM_MODE};
 static struct attribute computer_attr_used = {.name="used",.mode=COM__MODE }; static struct attribute computer_attr_used = {.name="used",.mode=COM__MODE };
-// + 
-// +/* 
-// Now build an attribute structure as a null terminated list + Now build an attribute structure as a null terminated list 
-//+ */
 static struct attribute* computer_default_attrs[] = { static struct attribute* computer_default_attrs[] = {
     &computer_attr_make,     &computer_attr_make,
Line 340: Line 230:
     NULL     NULL
 }; };
- 
 </code> </code>
  
Line 347: Line 236:
 The sysfs_ops table will contain the show and store routines The sysfs_ops table will contain the show and store routines
  
-The release routine will tell the system how to remove the +The release routine will tell the system how to remove the object.
-object.+
  
 The type also defines the default attributes for the object. The type also defines the default attributes for the object.
  
 <code c> <code c>
- +/* 
-/+ Now we can define the type 
-// Now we can define the type + */
-//+
 static struct kobj_type computer_ktype = { static struct kobj_type computer_ktype = {
     .release = computer_release,     .release = computer_release,
Line 362: Line 249:
     .default_attrs = computer_default_attrs     .default_attrs = computer_default_attrs
 }; };
- 
 </code> </code>
  
 === Define the subsystem  type === === Define the subsystem  type ===
  
-The subsystem type simply contains the notifier and clean up routine +The subsystem type simply contains the notifier and clean up routine used when the system is deleted.
-used when the system is deleted.+
  
 Again the sysfs_ops table will contain the show and store routines Again the sysfs_ops table will contain the show and store routines
  
-The release routine will tell the system how to remove the +The release routine will tell the system how to remove the object.
-object.+
  
 The type also defines the default attributes for th object. The type also defines the default attributes for th object.
  
 <code c> <code c>
- 
 static struct kobj_type sysfs_computer_ktype = { static struct kobj_type sysfs_computer_ktype = {
     .release = sysfs_computer_release,     .release = sysfs_computer_release,
Line 384: Line 267:
     .default_attrs = NULL     .default_attrs = NULL
 }; };
- 
 </code> </code>
  
Line 395: Line 277:
  
 <code c> <code c>
- 
    static struct kset computer_set;    static struct kset computer_set;
- 
 </code> </code>
  
Line 405: Line 285:
  
 <code c> <code c>
- +/The following macro completes the definition of the subsystem */
-// The following macro completes the definition of the subsystem+
 decl_subsys(sysfs_computer, &sysfs_computer_ktype, 0 ); decl_subsys(sysfs_computer, &sysfs_computer_ktype, 0 );
 +</code>
  
-//or by hand .. +or by hand .. 
 +<code>
 struct subsystem sysfs_computer_subsys = { struct subsystem sysfs_computer_subsys = {
         .kset = {         .kset = {
Line 419: Line 299:
 }; };
 </code> </code>
- 
- 
- 
  
 ==== Define the KSET ==== ==== Define the KSET ====