Monday, February 27, 2017

Linux Device Model (LDM)


Explain about the Linux Device Model (LDM)? 

In 2.6 Linux Kernel there was a addition of unified device model.
The device model provides a single mechanism for representing devices and describing their topology in the system.

Such system provide several benefits:-
1. Minimization of code duplication
2. A mechanism for providing common facility such as reference counting. 
3. The capability to enumerate all the devices in the system, view their status and see to what bus they attach.
4. The capability to generate a complete and valid tree of the entire device structure of the system, including all buses and interconnections.
5. The capability to link devices to their drivers and vice-versa.
6. The capability to categorise devices by their class, such as input device, without the need to understand the physical device topology.
7. The capability to walk the tree of devices from the leaves up to the root, powering down devices in the correct order. 


Explain about about ksets, kobjects and ktypes. How are they related?

Kobjects:- 
At the heart of the device model is the kobjects, short for kernel objects, which is represented by struct object and defined  /include/linux/kobject.h
http://lxr.free-electrons.com/source/include/linux/kobject.h













Kobject is similar to object class in object-oriented languages such as C# or Java.
It provides basic facilities such as reference counting, a name, a parent pointer, enabling creation of the hierarchy. 
->Name pointer points to the name of this kobject.
-> The parent pointer points to this kobject's parent. In this way it developed hierarachy.
(Sysfs is a user-space filesystem representation of the kobject hierarchy inside the kernel) 
-> The sd pointer points to a sysfs_dirent structure that represents this kobjects in sysfs.
-> kref - reference count


Kobjects are usually embedded in other structure and are generally not interesting on their own.
Instead a more important structure such as struct device, defined in /include/linux/device.h
http://lxr.free-electrons.com/source/include/linux/device.h#L32



















Ktypes:- 
Kobjects are associated with a specific type, called a ktype, short for kernel object type.
Ktypes are represented by struct kobj_type and defined in /linux/kobject.h
Ktypes have the simple job of describing default behaviour for a family of kobjects. 
Instead of each kobject defining its own behaviour is stored in a ktype, and kobjects of the same type points at the same ktype structure, this sharing the same behaviour.








->release pointer points to the decontructor called when a kobject's reference count reaches zero. 
This function is responsible for freeing any memory associated with this kobject and otherwise cleanup. 
->sysfsops variable points to a sysfs_ops structure. This structure describes the behaviour of sysfs files on read and write.
->default_attrs points to an array of attribute structures. these structurees define the default attributes assocaited with this kobjects. 

Ksets:- 

Ksets short for kernel objects sets, are aggregate collections of kobjects. Ksets work as the base container class for a set of kernel objects, collecting related kobjects, such as "all block devices". 

Ksets might sound similar to Ktypes, but they are different. 
Ksets group related kernel objects together, whereas ktypes enable kernel objects (functionally related or not) to share common operations. 























Relation of kobjects, ktypes and ksets

The important key object is the kobject, represented by struct kobject. The kobject introduces basic object properties—such as reference counting, parent-child relationship, and naming—to kernel data structures.The kobject structure provides these features in a standard unified way. Kobjects, in and of themselves, are not particularly useful. Instead, kobjects are typically embedded in other data structures, giving those containing structures the features of the kobject.

Kobjects are associated with a specific ktype, which is represented by struct kobj_type and pointed at by the ktype variable inside of the kobject. ktypes define some default properties of related kobjects: destruction behavior, sysfs behavior, and default attributes.The ktype structure is not well named; think of ktypes not as a grouping but as a set of shared operations.

Kobjects are then grouped into sets, called ksets, which are represented by struct kset. Ksets provide two functions. First, their embedded kobject acts as a base class for a group of kobjects. Second, ksets aggregate together related kobjects. In sysfs, kobjects are the individual directories in the filesystem. Related directories—say, perhaps all subdirectories of a given directory—might be in the same kset.




No comments:

Post a Comment