Chapter 1: Introduction and Overview
It is essential thatkobjects are not linked with other data structures by means of
pointers but are directly embedded. Managing the kernel object itself amounts to
managing the whole containing object this way. Sincestructkobjectis embedded
into many data structures of the kernel,the developers take care to keep it small.
Adding a single new element to this data structure results in a size increase of many
other data structures. Embedded kernel objects look as follows:
struct sample {
...
struct kobject kobj;
...
};
The meanings of the individual elements ofstruct kobjectare as follows:
❑ k_nameis a text name exported to userspace usingsysfs. Sysfs is a virtual filesystem that allows
for exporting various properties of the system into userspace. Likewisesdsupports this connec-
tion, and I will come back to this in Chapter 10.
❑ krefholds the general typestruct krefdesigned to simplify reference management. I discuss
this below.
❑ entryis a standard list element used to group severalkobjects in a list (known as a set in this
case).
❑ ksetis required when an object is grouped with other objects in a set.
❑ parentis a pointer to the parent element and enables a hierarchical structure to be established
betweenkobjects.
❑ ktypeprovides more detailed information on the data structure in which akobjectis
embedded. Of greatest importance is the destructor function that returns the resources of the
embedding data structure.
The similarity between the namekobjectand theobjectconcept of, well, object-oriented languages
like C++ or Java is by no means coincidental: Thekobjectabstraction indeed allows for using object-
oriented techniques in the kernel, but without requiring all the extra mechanics (and bloat, and overhead)
of C++.
Table 1-1 lists the standard operations provided by the kernel to manipulatekobjectinstances, and
therefore effectively act on the embedding structure.
The layout of thekrefstructure used to manage references is as follows:
<kref.h>
struct kref {
atomic_t refcount;
};
refcountis an atomic data type to specify the number of positions in the kernel at which an object is
currently being used. When the counter reaches 0, the object is no longer needed and can therefore be
removed from memory.