What are Kobject Attributes in Linux?

Embedded linux






In this article, we explain what kobject attributes are.

To understand it simply, kobject attributes are software code that define the default behavior for a kobject (or a group of kobjects of the same type).

Kobject attributes define certain parameters regarding a kobject, such as the device number, device name, device size, etc.

A kobject attribute can define any element specific to that device.

For example, with a SSD (Solid State Drive) drive in a linux system, it can define the partition number of that drive. For example, sda1 would be partition 1. sda2 would be partition 2. This wouldn't apply, though, to a temperature sensor, however, because it's not a hard drive. A temperature sensor may have a custom attribute for unit measurement, for example, whether the sensor measures the temperature in celsius, fahrenheit, or kelvin.

The point is later these kobject attributes can be read by a user to gain information about a device. In certain cases, the user may have writing privileges and can even update values to the device (the kobject).

So that's the amazing thing about kobject attributes.

The diagram below illustrates this concept.


Kobject type explanation


So you can see that we have several kobjects. All these kobjects are of the same container type, which in this case is, struct device

Because they all belong to the same type, they all will have the same kobject_type, which is data which defines the behavior of the objects.

Within each kobject in linux, there is a ktype structure, named struct kobj_type, which defines the default behavior (attributes) for a group of kobjects of the same container type.

This kobj_type structure is found at /include/linux/kobject.h.

This kobj_type structure is shown below.


struct kobj_type in linux


So you can see based off of this structure that there are file operation methods that are defined for the given kobject.

This makes sense because similar devices have similar (if not the exact same) file operation methods. These are defined within this structure.

There is also a field of type struct attribute_group. These define the default attributes of the kobject.

Behaviors are manifested in terms of attributes and file operation methods that handle those attributes.

The ktype also controls what happens to the kobject when it is created and destroyed.

Every structure that embeds a kobject needs a corresponding ktype.

So instead of each kobject defining its own behavior, the behavior is stored in a ktype, and kobjects of the same type point at the same ktype structure, thus sharing the same behavior.

So basically we create an instance of the kobj_type structure, defining its file operation methods, attributes, etc.

We then initialize each kobject's kobj_type field to this structure.

Each kobject has a kobj_type field, which can be seen below.


struct kobject in linux


You can see the field above, const struct kobj_type *ktype;

This defines the file operation methods, attributes, etc. of the kobject.

Any kobject initialized to the created instance of the kobj_type structure will inherit these file operation methods and attributes.

So this is how it works for kobject attributes (i.e., its behavior).

Now let's go into an kobject in the linux terminal and see some of these attributes.

Many attributes in linux for kobjects are created by default, called default attributes, although you can create your own custom attributes as well for kobjects.

So, as an example, let's go into a kobject (device) within the platform subsystem. This is a device connected to the platform bus.

In our example, we will go into the pcspkr device in the platform bus, and we see the following shown below.


Kobject attributes shown in the linux terminal


You can see the diferent coloring conventions that linux gives us.

The white lettering signifies the kobject attributes.

In this case, there are 3: driver_override, modalias, and uevent.

These are all default kobject attributes, which means they come with each kobject created.

As stated before, you can create your own kobject attributes, which we will show in a future article.

Let's see what happens if we create our own linux device driver, without modifyingi the kobj_type field.

Let's see what we will get.

Previously, we built a platform device driver in linux, which created 4 platform devices named pseudo-char-device.0, pseudo-char-device.1, pseudo-char-device.2, and pseudo-char-device.3.

This is seen in the linux terminal below.


Pseudo platform devices shown in the linux terminal


Now the interesting and revealing part. Let's go inside of one these devices.

So we cd into the first of the devices, pseudo-char-device.0

Its contents are shown below.


Kobject attributes of a pseudo platform device unmodified shown in linux terminal


We can see that there are the same default kobject attributes, as was in the other device.

Later, again, we will show how to create custom attributes with custom names.

Kobject attributes make it easy for a linux user to find out information about a given kobject.

Using the cat command, you can read any value of any given kobject attribute for any kobject. To see more about this, see how to read kobject attributes in linux

But this serves as an introduction to kobject attributes in linux.



Related Resources





HTML Comment Box is loading comments...