205 lines · plain
1.. _stable_api_nonsense:2 3The Linux Kernel Driver Interface4==================================5 6(all of your questions answered and then some)7 8Greg Kroah-Hartman <greg@kroah.com>9 10This is being written to try to explain why Linux **does not have a binary11kernel interface, nor does it have a stable kernel interface**.12 13.. note::14 15 Please realize that this article describes the **in kernel** interfaces, not16 the kernel to userspace interfaces.17 18 The kernel to userspace interface is the one that application programs use,19 the syscall interface. That interface is **very** stable over time, and20 will not break. I have old programs that were built on a pre 0.9something21 kernel that still work just fine on the latest 2.6 kernel release.22 That interface is the one that users and application programmers can count23 on being stable.24 25 26Executive Summary27-----------------28You think you want a stable kernel interface, but you really do not, and29you don't even know it. What you want is a stable running driver, and30you get that only if your driver is in the main kernel tree. You also31get lots of other good benefits if your driver is in the main kernel32tree, all of which has made Linux into such a strong, stable, and mature33operating system which is the reason you are using it in the first34place.35 36 37Intro38-----39 40It's only the odd person who wants to write a kernel driver that needs41to worry about the in-kernel interfaces changing. For the majority of42the world, they neither see this interface, nor do they care about it at43all.44 45First off, I'm not going to address **any** legal issues about closed46source, hidden source, binary blobs, source wrappers, or any other term47that describes kernel drivers that do not have their source code48released under the GPL. Please consult a lawyer if you have any legal49questions, I'm a programmer and hence, I'm just going to be describing50the technical issues here (not to make light of the legal issues, they51are real, and you do need to be aware of them at all times.)52 53So, there are two main topics here, binary kernel interfaces and stable54kernel source interfaces. They both depend on each other, but we will55discuss the binary stuff first to get it out of the way.56 57 58Binary Kernel Interface59-----------------------60Assuming that we had a stable kernel source interface for the kernel, a61binary interface would naturally happen too, right? Wrong. Please62consider the following facts about the Linux kernel:63 64 - Depending on the version of the C compiler you use, different kernel65 data structures will contain different alignment of structures, and66 possibly include different functions in different ways (putting67 functions inline or not.) The individual function organization68 isn't that important, but the different data structure padding is69 very important.70 71 - Depending on what kernel build options you select, a wide range of72 different things can be assumed by the kernel:73 74 - different structures can contain different fields75 - Some functions may not be implemented at all, (i.e. some locks76 compile away to nothing for non-SMP builds.)77 - Memory within the kernel can be aligned in different ways,78 depending on the build options.79 80 - Linux runs on a wide range of different processor architectures.81 There is no way that binary drivers from one architecture will run82 on another architecture properly.83 84Now a number of these issues can be addressed by simply compiling your85module for the exact specific kernel configuration, using the same exact86C compiler that the kernel was built with. This is sufficient if you87want to provide a module for a specific release version of a specific88Linux distribution. But multiply that single build by the number of89different Linux distributions and the number of different supported90releases of the Linux distribution and you quickly have a nightmare of91different build options on different releases. Also realize that each92Linux distribution release contains a number of different kernels, all93tuned to different hardware types (different processor types and94different options), so for even a single release you will need to create95multiple versions of your module.96 97Trust me, you will go insane over time if you try to support this kind98of release, I learned this the hard way a long time ago...99 100 101Stable Kernel Source Interfaces102-------------------------------103 104This is a much more "volatile" topic if you talk to people who try to105keep a Linux kernel driver that is not in the main kernel tree up to106date over time.107 108Linux kernel development is continuous and at a rapid pace, never109stopping to slow down. As such, the kernel developers find bugs in110current interfaces, or figure out a better way to do things. If they do111that, they then fix the current interfaces to work better. When they do112so, function names may change, structures may grow or shrink, and113function parameters may be reworked. If this happens, all of the114instances of where this interface is used within the kernel are fixed up115at the same time, ensuring that everything continues to work properly.116 117As a specific examples of this, the in-kernel USB interfaces have118undergone at least three different reworks over the lifetime of this119subsystem. These reworks were done to address a number of different120issues:121 122 - A change from a synchronous model of data streams to an asynchronous123 one. This reduced the complexity of a number of drivers and124 increased the throughput of all USB drivers such that we are now125 running almost all USB devices at their maximum speed possible.126 - A change was made in the way data packets were allocated from the127 USB core by USB drivers so that all drivers now needed to provide128 more information to the USB core to fix a number of documented129 deadlocks.130 131This is in stark contrast to a number of closed source operating systems132which have had to maintain their older USB interfaces over time. This133provides the ability for new developers to accidentally use the old134interfaces and do things in improper ways, causing the stability of the135operating system to suffer.136 137In both of these instances, all developers agreed that these were138important changes that needed to be made, and they were made, with139relatively little pain. If Linux had to ensure that it will preserve a140stable source interface, a new interface would have been created, and141the older, broken one would have had to be maintained over time, leading142to extra work for the USB developers. Since all Linux USB developers do143their work on their own time, asking programmers to do extra work for no144gain, for free, is not a possibility.145 146Security issues are also very important for Linux. When a147security issue is found, it is fixed in a very short amount of time. A148number of times this has caused internal kernel interfaces to be149reworked to prevent the security problem from occurring. When this150happens, all drivers that use the interfaces were also fixed at the151same time, ensuring that the security problem was fixed and could not152come back at some future time accidentally. If the internal interfaces153were not allowed to change, fixing this kind of security problem and154insuring that it could not happen again would not be possible.155 156Kernel interfaces are cleaned up over time. If there is no one using a157current interface, it is deleted. This ensures that the kernel remains158as small as possible, and that all potential interfaces are tested as159well as they can be (unused interfaces are pretty much impossible to160test for validity.)161 162 163What to do164----------165 166So, if you have a Linux kernel driver that is not in the main kernel167tree, what are you, a developer, supposed to do? Releasing a binary168driver for every different kernel version for every distribution is a169nightmare, and trying to keep up with an ever changing kernel interface170is also a rough job.171 172Simple, get your kernel driver into the main kernel tree (remember we are173talking about drivers released under a GPL-compatible license here, if your174code doesn't fall under this category, good luck, you are on your own here,175you leech). If your driver is in the tree, and a kernel interface changes,176it will be fixed up by the person who did the kernel change in the first177place. This ensures that your driver is always buildable, and works over178time, with very little effort on your part.179 180The very good side effects of having your driver in the main kernel tree181are:182 183 - The quality of the driver will rise as the maintenance costs (to the184 original developer) will decrease.185 - Other developers will add features to your driver.186 - Other people will find and fix bugs in your driver.187 - Other people will find tuning opportunities in your driver.188 - Other people will update the driver for you when external interface189 changes require it.190 - The driver automatically gets shipped in all Linux distributions191 without having to ask the distros to add it.192 193As Linux supports a larger number of different devices "out of the box"194than any other operating system, and it supports these devices on more195different processor architectures than any other operating system, this196proven type of development model must be doing something right :)197 198 199 200------201 202Thanks to Randy Dunlap, Andrew Morton, David Brownell, Hanna Linder,203Robert Love, and Nishanth Aravamudan for their review and comments on204early drafts of this paper.205