Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 1: Introduction and Overview


Another structure is provided to group common features of kernel objects. It is defined as follows:

<kobject.h>
struct kobj_type {
...
struct sysfs_ops * sysfs_ops;
struct attribute ** default_attrs;
};

Note that akobj_typeisnotused to collect various kernel objects — this is already managed byksets.
Instead, it provides an interface to the sysfs filesystem (discussed in Section 10.3). If multiple objects
export similar information via the filesystem, then this can be simplified by using a single ktype to pro-
vide the required methods.

ReferenceCounting


Reference counting is used to detect from how many places in the kernel an object is used. Whenever one
part of the kernel needs information contained in one object, it increments the reference count, and when
it does not need the information anymore, the count is decremented. Once the count has dropped to 0,
the kernel knows that the object is not required anymore, and that it is safe to release it from memory.
The kernel provides the following data structure to handle reference counting:

<kref.h>
struct kref {
atomic_t refcount;
};

The data structure is really simple in that it only provides a generic, atomic reference count. ‘‘Atomic’’
means in this context that incrementing and decrementing the variable is also safe on multiprocessor
systems, where more than one code path can access an object at the same time. Chapter 5 discusses the
need for this in more detail.

The auxiliary methodskref_init,kref_get,andkref_putare provided to initialize, increment, or
decrement the reference counter. This might seem trivial at a first glance. Nevertheless, it helps to avoid
excessive code duplication because such reference counts together with the aforementioned operations
are used all over the kernel.

Although manipulating the reference counter this way is safe against concurrency
issues, this doesnotimply that the surrounding data structure is safe against
concurrent access! Kernel code needs to employ further means to ensure that access
to data structures does not cause any problems when this can happen from multiple
processors simultaneously, and I discuss these issues in Chapter 5.

Finally, notice that the kernel contains some documentation related to kernel objects inDocumentation/
kobject.txt.

1.3.15 Data Types


Some issues related to data types are handled differently in the kernel in comparison to userland
programs.
Free download pdf