Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux kernel: why do 'subclass' structs put base class info at end?

Tags:

I was reading the chapter in Beautiful Code on the Linux kernel and the author discusses how Linux kernel implements inheritance in the C language (amongst other topics). In a nutshell, a 'base' struct is defined and in order to inherit from it the 'subclass' struct places a copy of the base at the end of the subclass struct definition. The author then spends a couple pages explaining a clever and complicated macro to figure out how many bytes to back in order to convert from the base part of the object to the subclass part of the object.

My question: Within the subclass struct, why not declare the base struct as the first thing in the struct, instead of the last thing?

The main advantage of putting the base struct stuff first is when casting from the base to the subclass you wouldn't need to move the pointer at all - essentially, doing the cast just means telling the compiler to let your code use the 'extra' fields that the subclass struct has placed after the stuff that the base defines.

Just to clarify my question a little bit let me throw some code out:

struct device { // this is the 'base class' struct
     int a;
     int b;
     //etc
}
struct usb_device { // this is the 'subclass' struct
    int usb_a;
    int usb_b;
    struct device dev; // This is what confuses me - 
                       // why put this here, rather than before usb_a?
}

If one happens to have a pointer to the "dev" field inside of a usb_device object then in order to cast it back to that usb_device object one needs to subtract 8 from that pointer. But if "dev" was the first thing in a usb_device casting the pointer wouldn't need to move the pointer at all.

Any help on this would be greatly appreciated. Even advice on where to find an answer would be appreciated - I'm not really sure how to Google for the architectural reason behind a decision like this. The closest I could find here on StackOverflow is: why to use these weird nesting structure

And, just to be clear - I understand that a lot of bright people have worked on the Linux kernel for a long time so clearly there's a good reason for doing it this way, I just can't figure out what it is.

like image 437
MikeTheTall Avatar asked Jan 20 '15 07:01

MikeTheTall


People also ask

What is a subclass in Java?

A subclass ( or derived class ) like the name implies extends the base class; you use the parent class as a template, and you add something else creating a new template, let’s add some color to our cubes with a new colorCube that inherits from cube :

What is a Linux-like kernel?

The term Linux-like has also been applied to the Embeddable Linux Kernel Subset, which does not include the full mainline Linux kernel but a small modified subset of the code. There are certain communities that develop kernels based on the official Linux.

Is the Linux kernel stable?

However, the interface between the kernel and loadable kernel modules (LKMs), unlike in many other kernels and operating systems, is not meant to be very stable by design. The Linux kernel, developed by contributors worldwide, is a prominent example of free and open source software.

What is the difference between Linux kernel and LSB?

However, the LSB goes beyond what concerns the Linux kernel, because it also defines the desktop specifications, the X libraries and Qt that have little to do with it. The LSB version 5 is built upon several standards and drafts (POSIX, SUS, X/Open, File System Hierarchy (FHS), and others).


2 Answers

The Amiga OS uses this "common header" trick in a lot of places and it looked like a good idea at the time: Subclassing by simply casting the pointer type. But there are drawbacks.

Pro:

  • You can extend existing data structures
  • You can use the same pointer in all places where the base type is expected, no pointer arithmetic needed, saving precious cycles
  • It feels natural

Con:

  • Different compilers tend to align data structures differently. If the base structure ended with char a;, then you could have 0, 1 or 3 pad bytes afterwards before the next field of the subclass starts. This led to quite nasty bugs, especially when you had to maintain backwards compatibility (i.e. for some reason, you have to have a certain padding because an ancient compiler version had a bug and now, there is lots of code which expects the buggy padding).
  • You don't notice quickly when you pass the wrong structure around. With the code in your question, fields get trashed very quickly if the pointer arithmetic is wrong. That is a good thing since it raises chances that a bug is discovered more early.
  • It leads to an attitude "my compiler will fix it for me" (which it sometimes won't) and all the casts lead to a "I know better than the compiler" attitude. The latter one would make you automatically insert casts before understanding the error message, which would lead to all kinds of odd problems.

The Linux kernel is putting the common structure elsewhere; it can be but doesn't have to be at the end.

Pro:

  • Bugs will show early
  • You will have to do some pointer arithmetic for every structure, so you're used to it
  • You don't need casts

Con:

  • Not obvious
  • Code is more complex
like image 139
Aaron Digulla Avatar answered Oct 04 '22 23:10

Aaron Digulla


I'm new to the Linux kernel code, so take my ramblings here with a grain of salt. As far as I can tell, there is no requirement as to where to put the "subclass" struct. That is exactly what the macros provide: You can cast to the "subclass" structure, regardless of its layout. This provides robustness to your code (the layout of a structure can be changed, without having to change your code. Perhaps there is a convention of placing the "base class" struct at the end, but I'm not aware of it. I've seen lots of code in drivers, where different "base class" structs are used to cast back to the same "subclass" structure (from different fields in the "subclass" of course).

like image 38
S.C. Madsen Avatar answered Oct 04 '22 22:10

S.C. Madsen