r/linuxquestions • • Jan 14 '21

How does the Linux kernel stay so small while supporting so many different hardware configurations?

Not tech support but I don't know where else to ask, sorry!

My five-year-old level understanding of the kernel is that someone can see that a WiFi card (for example) isn't supported then go in and add support. Doesn't that add up over time? 70MB seems small to me with that level of hardware support.

190 Upvotes

80 comments sorted by

143

u/aioeu Jan 14 '21

There is a remarkable amount of code sharing within the kernel.

Adding support for a new wifi device could be as simple as adding an entry to a table somewhere. Even if the device requires some new code written for it, it's still likely to have most things in common with some other device that already has support. Any "quirks" for the new device are available for future device models too.

47

u/jabjoe Jan 14 '21

Code sharing is the big thing. It's all about having open everything and a unstable internal API. The unstable API is important because all too often it turns out drivers are all doing the same thing, so instead it can be moved to common and the API changed and all the drivers changed to this consolidation. Plus Linux drivers are chip based, not vendor based. So what would be different drivers on Windows are the same on Linux (and without branding and bloatware apps).

5

u/Astra3_reddit Jan 14 '21

Can you "inherit" some code from driver and create new driver with extended or edited features while some code base remains inherited from the original code?

3

u/aioeu Jan 14 '21 edited Jan 14 '21

It's usually easier just to extend the original code with the new features you need.

What you see is a single piece of code typically handling a family of closely related hardware (and as /u/jabjoe points out, this is closely related at the chip level, not necessarily the branding on the box the hardware came in), with a table listing which specific quirks need to be applied to which specific hardware models.

But, yes, you can also get multiple drivers all using some base functionality. You might notice this when you load certain kernel modules. There are often dependencies upon other kernel modules, and these other modules get loaded as well.

1

u/Astra3_reddit Jan 15 '21

Yeah this is probably why, for example, amdgpu works for all new (all made from 2011 I think) AMD graphic cards and no separate drivers are needed.

102

u/Peetz0r Jan 14 '21 edited Jan 14 '21

70 MB is actually pretty big. Lets do some comparisons.

I just downloaded Pride and Prejudice by Jane Austen from Project Gutenberg. On paper it's 435 pages (the PDF version is 144 pages, but those are US Letter). The plain text version is 781 kB. Lets round that to 700. So that book would fit 100x in the same space. Imagine 100 books of technical documentation. That's a lot.

And the kernel is bzip2 compressed, so I bz2'd the book and it ended up being 180 kb, so it would fit over 380 times in 70 MB! According to at least 2 bookstores, the book would be 43mm thich. So our end result is a 16 meter (53 feet) long book shelf. You could fill 3.5 IKEA Billy's with that. Imagine that filled with technical documentation again.

Fun fact, the biggest microSD cards you can find are 1TB these days. On that you can fit around 50.000 filled IKEA Billy's of our bz2 compressed books. That's like 5 1.5 soccer fields of them! On a card that's small enough to loose in your pocket. Insane!

Edit: The actual reason I think 70 MB is pretty big is because I remember entire OS'es and even web browsers fitting on single floppy disks.

21

u/whitedranzer Jan 14 '21

That's like 5 soccer fields of them!

Obligatory "this proves Americans will measure with anything except the metric system."

P.S. the analogy is amazing. Thank you.

3

u/Peetz0r Jan 14 '21

Oops, I just found out that I made a silly mistake. In my defense, I was barely awake when I wrote the original post. I think I might have use the height instead of the width to get the surface area.

So let's be exact while I re-do them here :p

IKEA Billy is 80*28*202 cm. So when using the right two of them, that's 0.224 m² of floor space needed.

Soccer fields are usually 105*68 m, so 7140 m². That's 31875 Billys. So we need only abpout 1.6 fields! I think that, in my earlier calculations, I put Billy on its side and used 0.5656 m² as the area. And even then it's closer to 4 fields, not 5.

So an American (NFL) Football field is 100 yards by 160 feet. For some reason that I am too metric to understand they specified both sides in different units.

"this proves Americans will measure with anything and everything except the metric system."

But I'll convert them to meters anyway. Actually, someone on Wikipedia already did that for us. 91.44 * 48.8 = 4462 m². A fair bit smaller, but it still fits 19920 Billys. So we need around 2.5 fields for that.

I don't know why I did all of the above, I guess I have nothing better to do during lockdown. Ah well.

3

u/igoro00 Jan 14 '21

True Americans measure stuff in real FREEDOM football(the handegg one) fields, not this commie knockoff "soccer"

1

u/pikecat Jan 14 '21

Soccer, or more correctly football, where you move a ball exclusively with the foot, has been played for 2500 years. Which one is the knockoff?

2

u/igoro00 Jan 14 '21

Shut your commie mouth up. The real football is the AMERICAN 🏈🍔👨🏿🔫👮🏻‍♂️🇺🇸🇺🇸🌎🦅🦅 one. You either agree with values of this beautiful country of FREEDOM or we censor you and put you in the guantanamo prison without the right to defend urself because we believe in FREEDOM and you don't

1

u/pikecat Jan 14 '21

You are having a little problem with the meaning of freedom aren't you?

0

u/igoro00 Jan 14 '21 edited Jan 14 '21

-911, what is your emergency
-theres a man on r/linuxquestions who thinks America isn't a country of FREEDOM and probably doesn't pray to our flag
-I understand. Is this person black?
-i dont know, maybe?
-ok, we're sending all of our police force and a swat team to u/pikecat's current location.
-b.. but are you gonna shoot him?
-😂
-😂

64

u/_badwithcomputer Jan 14 '21

781 kB. Lets round that to 700

odd choice rounding 781 DOWN to 700

14

u/Peetz0r Jan 14 '21

I did that because 100 is a nice round number. Technically incorrect, but close enough.

I thought it would make the text as a whole nicer to read if you could follow along with at least some of the calculations without a calculator.

29

u/tomkatt Jan 14 '21

I think they meant at 781 it would have made more sense to round up to 800. Normally with rounding like that you'd round up at 0.5 and above, and down for anything 0.49 and below.

35

u/Peetz0r Jan 14 '21

Yeah I'm pretty sure they did. But I picked 700 KB because it's exactly 1/100th of 70 MB, yet still close enough to handwave away as rounding even though it is incorrect rounding.

3

u/tomkatt Jan 14 '21

Makes sense. 👍

0

u/[deleted] Jan 14 '21

[deleted]

14

u/salYBC Jan 14 '21

It's not a lot when you're just making estimates. Sure, don't do it when building bridges, but when you're just fooling around with numbers +- 10% is nothing.

0

u/[deleted] Jan 14 '21

[deleted]

5

u/salYBC Jan 14 '21

Except we're just spitballing relative magnitudes here. Physical scientists do this all the time. For a real calculation, sure be exact as possible.

5

u/TEKrific Jan 14 '21

I agree completely but I'm guessing they're physics majors and that kind of behaviour is not only expected but enforced in Physics departments. ;)

3

u/Bakoro Jan 14 '21

Normally with rounding like that you'd round up at 0.5 and above, and down for anything 0.49 and below.

What about between 0.49 and .5? There's infinity numbers between those.

2

u/tomkatt Jan 14 '21

Round down.

4

u/Bakoro Jan 14 '21

I know, I'm poking fun.

12

u/victorbrca Jan 14 '21

Great analogy... Just great!

10

u/tomkatt Jan 14 '21

Edit: The actual reason I think 70 MB is pretty big is because I remember entire OS'es and even web browsers fitting on single floppy disks.

Hell yeah. I remember back in the day, with the Commodore Amiga 500 my family was on some mailing list or something where we got sent floppies with new software periodically, and each floppy included the entire OS along with the software provided. It didn't actually have a hard drive.

5

u/Tetmohawk Jan 14 '21

My first Linux distro was Caldera Linux 1.2 which took up less than 60MB of space. That was everything - kernel, graphical desktop environment, Mozilla, personal files, etc. My how Linux has grown!

3

u/leematthewjackson Jan 14 '21

I love the examples you give here especially the Ikea Billy's. Brightened my morning!

ng book shelf. You could fill 3.5 IKEA Billy's with that. Imagine that filled with technical documentation again. Fun fact, the biggest microSD cards you can find are 1TB these days. On that you can fit around 50.000 filled IKEA Billy's of our bz2 compressed books. That's like 5 soccer fields of them! On a card that's small enough to loose in your pocket. Insane! Edit: The actual reason I think 70 MB is pretty big is because I remember entire OS'es and even web browsers fitting on single floppy disks.

3

u/Atralb Jan 14 '21

Fun fact, the biggest microSD cards you can find are 1TB these days

It's been 2TB for many years.

2

u/jabjoe Jan 14 '21

You can shrink it right down, if you ditch a load of drivers.... I don't think any OS in history has had so many drivers in so little relative space.

4

u/Peetz0r Jan 14 '21

Yeah, compared to other general purpose OS'es Linux is tiny. I guess the modularity helps a lot. Also the fact that the drivers are one large coordinated project, and not just a bunch of blobs written by programmers in different companies who have never seen each others code. Also, drivers are just drivers, and do not contain UI or bloatware or such.

Compare that to windows drivers. Those for my graphics card are a download of 652 MB. So, that would be over 32 Billys!

Yes, as of today a Billy is a new unit of data storage capacity, equivalent to 20 megabytes.

51

u/brothersand Jan 14 '21

Loadable modules. They're not really in the kernel.

man modprobe

13

u/CantPickDamnUsername Jan 14 '21

Isn't most firmware located in different package? For example in Arch I have separate linux (kernel) and linux-firmware package. Don't know about other distros

3

u/andersostling56 Jan 14 '21

And the firmware itself amounts to well over 100MB compressed...

3

u/[deleted] Jan 14 '21

The same for void

8

u/gordonmessmer Fedora Maintainer Jan 14 '21

At "70 MB", I'm certain that OP is talking about the kernel and all of its modules, not just the kernel image. The fact that modules are loadable doesn't explain why the whole set is relatively small.

5

u/insanemal Jan 14 '21

That's not accurate.

Kernel modules are loadable. But they load into kernel space and can be built-in. Any module can be.

Because Linux uses a monolithic model. Yes it's a modified monolithic kernel because it has dynamically loadable modules but they are actually all part of the kernel.

1

u/andersostling56 Jan 14 '21

Isn’t monolithic in this context the opposite to micro kernels, ie has nothing to do with how modules are compiled in or left loadable?

1

u/insanemal Jan 14 '21

Traditionally monolithic kernels did not have loadable modules.

Linux is more of a hybrid. In that it has loadable modules but they aren't running in seperate memory space like microkernels.

But all development of drivers, with some exceptions (I'm looking at you NVIDIA) are done in tree.

That is, all the drivers are part of the kernles code base. Not separate.

The usual packaging of the kernel and the modules as seperate packages is a long story. But both packages are built at the same time from the same code because the modules are part of the kernel.

14

u/gordonmessmer Fedora Maintainer Jan 14 '21

One of the reasons that 70MB looks very small is that drivers for some other operating systems are several megabytes for a single device. You might download a 60MB driver for an Intel Ethernet card.

What's misleading is that Intel's "driver" download isn't just a driver. They ship a large set of Ethernet diagnostic tools with their drivers, and that significantly increases the size of that bundle. Many of those diagnostics exist for Linux systems as well, but they're not part of the kernel.

13

u/GuestStarr Jan 14 '21

Yes, 70MB is actually quite a lot. And think about some Realtek Windows sound driver packages! 180MB full of ..what?

5

u/jess-sch Jan 14 '21

180MB full of ...

... the ugliest GUIs of all time constantly running in the background just to annoy you every time you plug in some headphones.

1

u/GuestStarr Jan 14 '21

No, there must be something else. The stuff you suggested won't take more than a few kB, if even that.

4

u/jess-sch Jan 14 '21

Probably electron. It's always electron.

58

u/Fearless_Process Jan 14 '21

Text does not take up very much space, when compiled to machine code it may even take less, especially after optimization passes by the compiler. The kernel is actually massive from a programming standpoint.

Remember that you can fit a dictionary worth of text in like a few mb, and all of wikipedia's text is a only a few dozen GB.

Also like others have said most drivers can be compiled as modules that are loaded as needed rather than baked directly into the kernel itself. On gentoo the total size of my kernel + modules was like 300-400mb iirc, which is really quite large considering that it's all just binary executable data and not pictures or videos or anything like that.

4

u/smog_alado Jan 14 '21 edited Jan 14 '21

I always remember this song when I stop to think about how much fits in a megabyte:

https://www.youtube.com/watch?v=mSHUIEDBbl4

-12

u/nahnah2017 Jan 14 '21

Text does not take up very much space, when compiled to machine code it may even take less

You don't compile text to machine code.

0

u/Fearless_Process Jan 14 '21

You're telling me source code doesn't get compiled and assembled into binary files that the OS can run? What does it get turned in to?

0

u/nahnah2017 Jan 14 '21

No, I said no such thing at all.

0

u/[deleted] Jan 14 '21

[deleted]

-4

u/nahnah2017 Jan 14 '21

He said "it may even take less" when it's compiled which implies, at least, that he's not talking about code. Programmers do not refer to code as text when compiling such things and especially don't have to guess if it will take less space. We especially don't say we compile text.

1

u/igoro00 Jan 14 '21

Ok "programmer"

-1

u/nahnah2017 Jan 14 '21

I have 40 years of it and you don't seem to want to be educated but that's typical of reddit.

1

u/igoro00 Jan 14 '21

Ugah bugah i am smart u r stupid ugah buga

0

u/nahnah2017 Jan 14 '21

And there it is. The reddit intelligentsia.

0

u/igoro00 Jan 15 '21

I like how you're just confirming what I've just said lmao

1

u/igoro00 Jan 14 '21

"guys don't downvote me. I am very smart i've been programming for 40 years. I am smart you are stupid!!! DO YOU HEAR ME! I'M SMART AND YOU ARE NOT!! DONT DOWNVOTE ME YOU STUPID REDDITORS!!!!!!!!!"

0

u/nahnah2017 Jan 14 '21

And there it is. The reddit intelligentsia.

32

u/looks_like_a_potato Jan 14 '21

What's more impressive is Tiny Core Linux. 16MB, not only kernel but the whole operating system with GUI.

7

u/[deleted] Jan 14 '21

Based on what I know, I have two things to say

  • 70 Megs IS Big.It's not small by any means.

  • Most of the time, new Software/Drivers have code/binaries that are already in the aforementioned 70 MB. It's just a matter of a few configurations. So upgrades do not take up as much space, i.e Do not increase the amount of space used.

P.S My knowledge of linux is a bit lacklustre at the moment (a few days) so please correct me if I am wrong somewhere.

5

u/CalcProgrammer1 Jan 14 '21

Along with what everyone else has said, the kernel is highly configurable. In most desktop oriented distros, the packaged kernel is built with loadable module support and usually all or a great majority of the available modules are provided. These modules consist of parts of the kernel that are not always required, and so are built as separate modules that can be loaded and unloaded as needed. Modules provide things like device drivers, subsystems, file systems, protocols, cryptography algorithms, and more.

The kernel build is highly configurable though, and things that are provided as modules in most major distros can often also be built into the main kernel binary itself, which means the functionality is always resident in memory. This is more common in embedded Linux devices, where the kernel is targeting one specific hardware device. If you're making a Linux powered toaster, for example, and every toaster has the same Broadcom WiFi module, ARM CPU, SD card controller, toast controller, and display module, it would be wise to build a customized kernel that includes drivers for all of those things built in and no other drivers at all, as they would go unused in such a device. This allows you to build a very small kernel binary if you only need a small subset of its features, allowing it to fit in small and light platforms with limited storage memory and RAM. A more practical example would be a consumer WiFi router. A router with 64MB RAM would not be too happy with a 70MB kernel image, but by paring down the kernel to fit the specific hardware needs, it can be under 10MB, leaving plenty of space for a userspace such as OpenWRT.

4

u/[deleted] Jan 14 '21

Magik :)

3

u/Zugzub Jan 14 '21

Voodoo

4

u/ropid Jan 14 '21

The kernel is much larger if you take away the compression and if you add all the module files to the number.

I tried to find out what's going on and how much larger everything is when added together and without compression. On my distro here the kernel's files are installed in /usr/lib/modules/. I copied the vmlinuz kernel image and all the .ko.* module files into a temp directory. I decompressed the module files but I don't know how to decompress the vmlinuz file. I get this result here:

$ du --total -h *.ko vmlinuz
...
6.2M    vmlinuz
323M    total

The compressed "vmlinuz" file is just 6MB of this number so hopefully it doesn't matter too much that I didn't manage to decompress it.

The kernel I used for this was the Linux 5.4.89 LTS package of my distro.

3

u/[deleted] Jan 14 '21

Actually it's big.

Slackware back in the 90's was less than 500kb if I remember correctly.

A lot has already been added over the years and you get the current size.

It is primarily C and some asm, but mostly C. It does not have GUI.

3

u/lorhof1 Jan 14 '21

<Not Answer> This subreddit isn't generally about tech support, even if there are a lot of tech support questions. You picked the right subreddit, don't worry.

2

u/Spicy_Poo Jan 14 '21

Modules.

1

u/SugondeseAmbassador Jan 14 '21

Because all that is in kernel modules

1

u/shawn99452 Jan 14 '21

If you're comparing to a Windows driver, a lot of this also comes down to Windows drivers (particularly graphics drivers, or in the past sound card drivers) having to include dependencies, and additional programs for branding, configuration, and updating, along with their various assets (tutorial videos, graphics, etc.). The actual "driver" component of most Windows drivers is fairly small, similar to a driver for a Linux system. Some of the difference also comes from code and dependency sharing among all the different drivers built into the kernel.

1

u/istarian Jan 14 '21

loadable kernel modules

The only things that need to be compiled into the kernel proper are the bare minimum needed to boot a system, the rest can be loaded on demand.

1

u/Cyber_Faustao Jan 14 '21

The short answer is that most 'hardware support' is on the linux-firmware package, which is like 300-500MB in size. The linux kernel is mostly just the plumbing which loads said firmware on a as-needed basis

0

u/ThiefClashRoyale Jan 14 '21

Its a good question. Im also not too sure but I would say that I have been aware of sometimes older stuff being removed but even so it does seem like it should grow over time. I would like to know also the definitive answer.

-11

u/[deleted] Jan 14 '21

My understanding of it is that it is not the kernel's job to support hardware. Since it is open source people write drivers for different hardware configurations and others can contribute to improve and fix the drivers. By installing these drivers your hardware can interact with the kernel.

17

u/insanemal Jan 14 '21

Linux is technically monolithic kernel.

The kernel has the drivers compiled into it.

Yeah they have loadable modules but they are considered part of the kernel

11

u/daemonpenguin Jan 14 '21

The kernel's primary job is to support hardware. Drivers are almost always part of the kernel.

2

u/hmoff Jan 14 '21

Far from the primary job. It also provides files and network services, multi-tasking, multi-user ...

2

u/ThiefClashRoyale Jan 14 '21

I guess its moved that way. I googled and found this. Some critical drivers to boot are still included:

https://askubuntu.com/questions/863521/how-does-the-linux-kernel-deal-with-drivers

-10

u/[deleted] Jan 14 '21

Same way semen does.