1*4882a593Smuzhiyun.. _stable_api_nonsense: 2*4882a593Smuzhiyun 3*4882a593SmuzhiyunThe Linux Kernel Driver Interface 4*4882a593Smuzhiyun================================== 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun(all of your questions answered and then some) 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunGreg Kroah-Hartman <greg@kroah.com> 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunThis is being written to try to explain why Linux **does not have a binary 11*4882a593Smuzhiyunkernel interface, nor does it have a stable kernel interface**. 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun.. note:: 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun Please realize that this article describes the **in kernel** interfaces, not 16*4882a593Smuzhiyun the kernel to userspace interfaces. 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun The kernel to userspace interface is the one that application programs use, 19*4882a593Smuzhiyun the syscall interface. That interface is **very** stable over time, and 20*4882a593Smuzhiyun will not break. I have old programs that were built on a pre 0.9something 21*4882a593Smuzhiyun kernel that still work just fine on the latest 2.6 kernel release. 22*4882a593Smuzhiyun That interface is the one that users and application programmers can count 23*4882a593Smuzhiyun on being stable. 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunExecutive Summary 27*4882a593Smuzhiyun----------------- 28*4882a593SmuzhiyunYou think you want a stable kernel interface, but you really do not, and 29*4882a593Smuzhiyunyou don't even know it. What you want is a stable running driver, and 30*4882a593Smuzhiyunyou get that only if your driver is in the main kernel tree. You also 31*4882a593Smuzhiyunget lots of other good benefits if your driver is in the main kernel 32*4882a593Smuzhiyuntree, all of which has made Linux into such a strong, stable, and mature 33*4882a593Smuzhiyunoperating system which is the reason you are using it in the first 34*4882a593Smuzhiyunplace. 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunIntro 38*4882a593Smuzhiyun----- 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunIt's only the odd person who wants to write a kernel driver that needs 41*4882a593Smuzhiyunto worry about the in-kernel interfaces changing. For the majority of 42*4882a593Smuzhiyunthe world, they neither see this interface, nor do they care about it at 43*4882a593Smuzhiyunall. 44*4882a593Smuzhiyun 45*4882a593SmuzhiyunFirst off, I'm not going to address **any** legal issues about closed 46*4882a593Smuzhiyunsource, hidden source, binary blobs, source wrappers, or any other term 47*4882a593Smuzhiyunthat describes kernel drivers that do not have their source code 48*4882a593Smuzhiyunreleased under the GPL. Please consult a lawyer if you have any legal 49*4882a593Smuzhiyunquestions, I'm a programmer and hence, I'm just going to be describing 50*4882a593Smuzhiyunthe technical issues here (not to make light of the legal issues, they 51*4882a593Smuzhiyunare real, and you do need to be aware of them at all times.) 52*4882a593Smuzhiyun 53*4882a593SmuzhiyunSo, there are two main topics here, binary kernel interfaces and stable 54*4882a593Smuzhiyunkernel source interfaces. They both depend on each other, but we will 55*4882a593Smuzhiyundiscuss the binary stuff first to get it out of the way. 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun 58*4882a593SmuzhiyunBinary Kernel Interface 59*4882a593Smuzhiyun----------------------- 60*4882a593SmuzhiyunAssuming that we had a stable kernel source interface for the kernel, a 61*4882a593Smuzhiyunbinary interface would naturally happen too, right? Wrong. Please 62*4882a593Smuzhiyunconsider the following facts about the Linux kernel: 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun - Depending on the version of the C compiler you use, different kernel 65*4882a593Smuzhiyun data structures will contain different alignment of structures, and 66*4882a593Smuzhiyun possibly include different functions in different ways (putting 67*4882a593Smuzhiyun functions inline or not.) The individual function organization 68*4882a593Smuzhiyun isn't that important, but the different data structure padding is 69*4882a593Smuzhiyun very important. 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun - Depending on what kernel build options you select, a wide range of 72*4882a593Smuzhiyun different things can be assumed by the kernel: 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun - different structures can contain different fields 75*4882a593Smuzhiyun - Some functions may not be implemented at all, (i.e. some locks 76*4882a593Smuzhiyun compile away to nothing for non-SMP builds.) 77*4882a593Smuzhiyun - Memory within the kernel can be aligned in different ways, 78*4882a593Smuzhiyun depending on the build options. 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun - Linux runs on a wide range of different processor architectures. 81*4882a593Smuzhiyun There is no way that binary drivers from one architecture will run 82*4882a593Smuzhiyun on another architecture properly. 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunNow a number of these issues can be addressed by simply compiling your 85*4882a593Smuzhiyunmodule for the exact specific kernel configuration, using the same exact 86*4882a593SmuzhiyunC compiler that the kernel was built with. This is sufficient if you 87*4882a593Smuzhiyunwant to provide a module for a specific release version of a specific 88*4882a593SmuzhiyunLinux distribution. But multiply that single build by the number of 89*4882a593Smuzhiyundifferent Linux distributions and the number of different supported 90*4882a593Smuzhiyunreleases of the Linux distribution and you quickly have a nightmare of 91*4882a593Smuzhiyundifferent build options on different releases. Also realize that each 92*4882a593SmuzhiyunLinux distribution release contains a number of different kernels, all 93*4882a593Smuzhiyuntuned to different hardware types (different processor types and 94*4882a593Smuzhiyundifferent options), so for even a single release you will need to create 95*4882a593Smuzhiyunmultiple versions of your module. 96*4882a593Smuzhiyun 97*4882a593SmuzhiyunTrust me, you will go insane over time if you try to support this kind 98*4882a593Smuzhiyunof release, I learned this the hard way a long time ago... 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunStable Kernel Source Interfaces 102*4882a593Smuzhiyun------------------------------- 103*4882a593Smuzhiyun 104*4882a593SmuzhiyunThis is a much more "volatile" topic if you talk to people who try to 105*4882a593Smuzhiyunkeep a Linux kernel driver that is not in the main kernel tree up to 106*4882a593Smuzhiyundate over time. 107*4882a593Smuzhiyun 108*4882a593SmuzhiyunLinux kernel development is continuous and at a rapid pace, never 109*4882a593Smuzhiyunstopping to slow down. As such, the kernel developers find bugs in 110*4882a593Smuzhiyuncurrent interfaces, or figure out a better way to do things. If they do 111*4882a593Smuzhiyunthat, they then fix the current interfaces to work better. When they do 112*4882a593Smuzhiyunso, function names may change, structures may grow or shrink, and 113*4882a593Smuzhiyunfunction parameters may be reworked. If this happens, all of the 114*4882a593Smuzhiyuninstances of where this interface is used within the kernel are fixed up 115*4882a593Smuzhiyunat the same time, ensuring that everything continues to work properly. 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunAs a specific examples of this, the in-kernel USB interfaces have 118*4882a593Smuzhiyunundergone at least three different reworks over the lifetime of this 119*4882a593Smuzhiyunsubsystem. These reworks were done to address a number of different 120*4882a593Smuzhiyunissues: 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun - A change from a synchronous model of data streams to an asynchronous 123*4882a593Smuzhiyun one. This reduced the complexity of a number of drivers and 124*4882a593Smuzhiyun increased the throughput of all USB drivers such that we are now 125*4882a593Smuzhiyun running almost all USB devices at their maximum speed possible. 126*4882a593Smuzhiyun - A change was made in the way data packets were allocated from the 127*4882a593Smuzhiyun USB core by USB drivers so that all drivers now needed to provide 128*4882a593Smuzhiyun more information to the USB core to fix a number of documented 129*4882a593Smuzhiyun deadlocks. 130*4882a593Smuzhiyun 131*4882a593SmuzhiyunThis is in stark contrast to a number of closed source operating systems 132*4882a593Smuzhiyunwhich have had to maintain their older USB interfaces over time. This 133*4882a593Smuzhiyunprovides the ability for new developers to accidentally use the old 134*4882a593Smuzhiyuninterfaces and do things in improper ways, causing the stability of the 135*4882a593Smuzhiyunoperating system to suffer. 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunIn both of these instances, all developers agreed that these were 138*4882a593Smuzhiyunimportant changes that needed to be made, and they were made, with 139*4882a593Smuzhiyunrelatively little pain. If Linux had to ensure that it will preserve a 140*4882a593Smuzhiyunstable source interface, a new interface would have been created, and 141*4882a593Smuzhiyunthe older, broken one would have had to be maintained over time, leading 142*4882a593Smuzhiyunto extra work for the USB developers. Since all Linux USB developers do 143*4882a593Smuzhiyuntheir work on their own time, asking programmers to do extra work for no 144*4882a593Smuzhiyungain, for free, is not a possibility. 145*4882a593Smuzhiyun 146*4882a593SmuzhiyunSecurity issues are also very important for Linux. When a 147*4882a593Smuzhiyunsecurity issue is found, it is fixed in a very short amount of time. A 148*4882a593Smuzhiyunnumber of times this has caused internal kernel interfaces to be 149*4882a593Smuzhiyunreworked to prevent the security problem from occurring. When this 150*4882a593Smuzhiyunhappens, all drivers that use the interfaces were also fixed at the 151*4882a593Smuzhiyunsame time, ensuring that the security problem was fixed and could not 152*4882a593Smuzhiyuncome back at some future time accidentally. If the internal interfaces 153*4882a593Smuzhiyunwere not allowed to change, fixing this kind of security problem and 154*4882a593Smuzhiyuninsuring that it could not happen again would not be possible. 155*4882a593Smuzhiyun 156*4882a593SmuzhiyunKernel interfaces are cleaned up over time. If there is no one using a 157*4882a593Smuzhiyuncurrent interface, it is deleted. This ensures that the kernel remains 158*4882a593Smuzhiyunas small as possible, and that all potential interfaces are tested as 159*4882a593Smuzhiyunwell as they can be (unused interfaces are pretty much impossible to 160*4882a593Smuzhiyuntest for validity.) 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun 163*4882a593SmuzhiyunWhat to do 164*4882a593Smuzhiyun---------- 165*4882a593Smuzhiyun 166*4882a593SmuzhiyunSo, if you have a Linux kernel driver that is not in the main kernel 167*4882a593Smuzhiyuntree, what are you, a developer, supposed to do? Releasing a binary 168*4882a593Smuzhiyundriver for every different kernel version for every distribution is a 169*4882a593Smuzhiyunnightmare, and trying to keep up with an ever changing kernel interface 170*4882a593Smuzhiyunis also a rough job. 171*4882a593Smuzhiyun 172*4882a593SmuzhiyunSimple, get your kernel driver into the main kernel tree (remember we are 173*4882a593Smuzhiyuntalking about drivers released under a GPL-compatible license here, if your 174*4882a593Smuzhiyuncode doesn't fall under this category, good luck, you are on your own here, 175*4882a593Smuzhiyunyou leech). If your driver is in the tree, and a kernel interface changes, 176*4882a593Smuzhiyunit will be fixed up by the person who did the kernel change in the first 177*4882a593Smuzhiyunplace. This ensures that your driver is always buildable, and works over 178*4882a593Smuzhiyuntime, with very little effort on your part. 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunThe very good side effects of having your driver in the main kernel tree 181*4882a593Smuzhiyunare: 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun - The quality of the driver will rise as the maintenance costs (to the 184*4882a593Smuzhiyun original developer) will decrease. 185*4882a593Smuzhiyun - Other developers will add features to your driver. 186*4882a593Smuzhiyun - Other people will find and fix bugs in your driver. 187*4882a593Smuzhiyun - Other people will find tuning opportunities in your driver. 188*4882a593Smuzhiyun - Other people will update the driver for you when external interface 189*4882a593Smuzhiyun changes require it. 190*4882a593Smuzhiyun - The driver automatically gets shipped in all Linux distributions 191*4882a593Smuzhiyun without having to ask the distros to add it. 192*4882a593Smuzhiyun 193*4882a593SmuzhiyunAs Linux supports a larger number of different devices "out of the box" 194*4882a593Smuzhiyunthan any other operating system, and it supports these devices on more 195*4882a593Smuzhiyundifferent processor architectures than any other operating system, this 196*4882a593Smuzhiyunproven type of development model must be doing something right :) 197*4882a593Smuzhiyun 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun------ 201*4882a593Smuzhiyun 202*4882a593SmuzhiyunThanks to Randy Dunlap, Andrew Morton, David Brownell, Hanna Linder, 203*4882a593SmuzhiyunRobert Love, and Nishanth Aravamudan for their review and comments on 204*4882a593Smuzhiyunearly drafts of this paper. 205