What is a Kobject in Linux?

Embedded linux






In this article, we explain what a kobject is in linux.

First, a kobject stands for kernel object and it is represented by struct kobject.

Kobjects are a fundamental building block of the linux device and driver hierarchy.

Kobjects are used to represent the containers in the sys virtual file system.

Usually the kobject's container structure is the struct device that the kobject is embedded in. So what this means is that kobjects represent devices in a linux system.

So, for example, if you create a device that represents a NAND flash drive, the kobject is a representation of this device in the sys virtual file system.

If you create a device that represents a temperature sensor, the kobject is a representaiton of this device.

To show you this within the linux file system itself, let's go to the sys directory, which is located in the root directory of linux.

Let's check out all of the platform devices (devices connected to the platform bus) by going to the /sys/bus/platform/devices directory.

The result of this is shown below.


Devices of the platform bus in linux


Thus, you can see all of the platform devices that are currently loaded in the kernel module in linux.

This is only possible due to kobjects, which represent their container structures, which again is struct device.

We are then able to see these devices in the sys virtual file system.

Kobjects are also used majorly for reference counting of the containers. This tracks the usage of the container (i.e. device).

Kobjects have their own name, type, and parent pointer to navigate the linux device and driver hierarchy.

Using kobjects, you can add attributes to the container, which can be viewed/altered by the user space.

The sysfs filesystem gets populated because of kobjects; sysfs is a user space representation of the kobject's hierarchy inside of the kernel.

So what are containers?

Kobjects are rarely or never used as stand-alone kernel objects. Most of the time, they are embedded in some other structure that we call a container structure, which describes the device driver's model components.

Some examples of container structures could be struct bus, struct device, or struct device_driver.

The container structure's embedded kobject enables the container to become part of an object hierarchy.

The image below helps to explain the kobject hierarchy with it being embedded in a container structure.


Kobject embedded in container structure in linux


You can see based on the image that each kobject has a kref characteristic associated with it; this helps to track the usage of the container structure of that the kobject is embedded in.

Then above this, we have the kobject itself.

Then above this, we have struct device, which is the container of the kobject. The device is, essentially, the container structure of the kobject and realize that all devices are represented in the sys directory. This is why kobjects are the building blocks of the linux device and driver hierarchy.

Let's now look at the kobject structure itself.


struct kobject in linux


Within this kobject structure, you can see various fields, including the kset, kobj_type, kref, etc.

This fields help to define the attributes and behaviors of the kobject, so that we can define exactly how it behaves and its composition.


So this is what an kobject is in linux and how it is the fundamental building block of the linux device and driver hierarchy.



Related Resources





HTML Comment Box is loading comments...