blob: 847b342b7b20bd73758c603d833811c5ac738930 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001The Linux Kernel Driver Interface
2(all of your questions answered and then some)
3
4Greg Kroah-Hartman <greg@kroah.com>
5
6This is being written to try to explain why Linux does not have a binary
7kernel interface, nor does it have a stable kernel interface. Please
8realize that this article describes the _in kernel_ interfaces, not the
9kernel to userspace interfaces. The kernel to userspace interface is
10the one that application programs use, the syscall interface. That
11interface is _very_ stable over time, and will not break. I have old
12programs that were built on a pre 0.9something kernel that still work
Juan Langa2765e82007-07-24 13:24:19 -070013just fine on the latest 2.6 kernel release. That interface is the one
Linus Torvalds1da177e2005-04-16 15:20:36 -070014that users and application programmers can count on being stable.
15
16
17Executive Summary
18-----------------
19You think you want a stable kernel interface, but you really do not, and
20you don't even know it. What you want is a stable running driver, and
21you get that only if your driver is in the main kernel tree. You also
22get lots of other good benefits if your driver is in the main kernel
23tree, all of which has made Linux into such a strong, stable, and mature
24operating system which is the reason you are using it in the first
25place.
26
27
28Intro
29-----
30
31It's only the odd person who wants to write a kernel driver that needs
32to worry about the in-kernel interfaces changing. For the majority of
33the world, they neither see this interface, nor do they care about it at
34all.
35
36First off, I'm not going to address _any_ legal issues about closed
37source, hidden source, binary blobs, source wrappers, or any other term
38that describes kernel drivers that do not have their source code
39released under the GPL. Please consult a lawyer if you have any legal
40questions, I'm a programmer and hence, I'm just going to be describing
41the technical issues here (not to make light of the legal issues, they
42are real, and you do need to be aware of them at all times.)
43
44So, there are two main topics here, binary kernel interfaces and stable
45kernel source interfaces. They both depend on each other, but we will
46discuss the binary stuff first to get it out of the way.
47
48
49Binary Kernel Interface
50-----------------------
51Assuming that we had a stable kernel source interface for the kernel, a
52binary interface would naturally happen too, right? Wrong. Please
53consider the following facts about the Linux kernel:
54 - Depending on the version of the C compiler you use, different kernel
55 data structures will contain different alignment of structures, and
56 possibly include different functions in different ways (putting
57 functions inline or not.) The individual function organization
58 isn't that important, but the different data structure padding is
59 very important.
60 - Depending on what kernel build options you select, a wide range of
61 different things can be assumed by the kernel:
62 - different structures can contain different fields
63 - Some functions may not be implemented at all, (i.e. some locks
64 compile away to nothing for non-SMP builds.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 - Memory within the kernel can be aligned in different ways,
66 depending on the build options.
67 - Linux runs on a wide range of different processor architectures.
68 There is no way that binary drivers from one architecture will run
69 on another architecture properly.
70
71Now a number of these issues can be addressed by simply compiling your
72module for the exact specific kernel configuration, using the same exact
73C compiler that the kernel was built with. This is sufficient if you
74want to provide a module for a specific release version of a specific
75Linux distribution. But multiply that single build by the number of
76different Linux distributions and the number of different supported
77releases of the Linux distribution and you quickly have a nightmare of
78different build options on different releases. Also realize that each
79Linux distribution release contains a number of different kernels, all
80tuned to different hardware types (different processor types and
81different options), so for even a single release you will need to create
82multiple versions of your module.
83
84Trust me, you will go insane over time if you try to support this kind
85of release, I learned this the hard way a long time ago...
86
87
88Stable Kernel Source Interfaces
89-------------------------------
90
91This is a much more "volatile" topic if you talk to people who try to
92keep a Linux kernel driver that is not in the main kernel tree up to
93date over time.
94
95Linux kernel development is continuous and at a rapid pace, never
96stopping to slow down. As such, the kernel developers find bugs in
97current interfaces, or figure out a better way to do things. If they do
98that, they then fix the current interfaces to work better. When they do
99so, function names may change, structures may grow or shrink, and
100function parameters may be reworked. If this happens, all of the
101instances of where this interface is used within the kernel are fixed up
102at the same time, ensuring that everything continues to work properly.
103
104As a specific examples of this, the in-kernel USB interfaces have
105undergone at least three different reworks over the lifetime of this
106subsystem. These reworks were done to address a number of different
107issues:
108 - A change from a synchronous model of data streams to an asynchronous
109 one. This reduced the complexity of a number of drivers and
110 increased the throughput of all USB drivers such that we are now
111 running almost all USB devices at their maximum speed possible.
112 - A change was made in the way data packets were allocated from the
113 USB core by USB drivers so that all drivers now needed to provide
114 more information to the USB core to fix a number of documented
115 deadlocks.
116
117This is in stark contrast to a number of closed source operating systems
118which have had to maintain their older USB interfaces over time. This
119provides the ability for new developers to accidentally use the old
120interfaces and do things in improper ways, causing the stability of the
121operating system to suffer.
122
123In both of these instances, all developers agreed that these were
124important changes that needed to be made, and they were made, with
125relatively little pain. If Linux had to ensure that it preserve a
126stable source interface, a new interface would have been created, and
127the older, broken one would have had to be maintained over time, leading
128to extra work for the USB developers. Since all Linux USB developers do
129their work on their own time, asking programmers to do extra work for no
130gain, for free, is not a possibility.
131
Daniel Walker30d07a22005-07-29 12:14:07 -0700132Security issues are also very important for Linux. When a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133security issue is found, it is fixed in a very short amount of time. A
134number of times this has caused internal kernel interfaces to be
135reworked to prevent the security problem from occurring. When this
136happens, all drivers that use the interfaces were also fixed at the
137same time, ensuring that the security problem was fixed and could not
138come back at some future time accidentally. If the internal interfaces
139were not allowed to change, fixing this kind of security problem and
140insuring that it could not happen again would not be possible.
141
142Kernel interfaces are cleaned up over time. If there is no one using a
143current interface, it is deleted. This ensures that the kernel remains
144as small as possible, and that all potential interfaces are tested as
145well as they can be (unused interfaces are pretty much impossible to
146test for validity.)
147
148
149What to do
150----------
151
152So, if you have a Linux kernel driver that is not in the main kernel
153tree, what are you, a developer, supposed to do? Releasing a binary
154driver for every different kernel version for every distribution is a
155nightmare, and trying to keep up with an ever changing kernel interface
156is also a rough job.
157
158Simple, get your kernel driver into the main kernel tree (remember we
159are talking about GPL released drivers here, if your code doesn't fall
160under this category, good luck, you are on your own here, you leech
161<insert link to leech comment from Andrew and Linus here>.) If your
162driver is in the tree, and a kernel interface changes, it will be fixed
163up by the person who did the kernel change in the first place. This
164ensures that your driver is always buildable, and works over time, with
165very little effort on your part.
166
167The very good side effects of having your driver in the main kernel tree
168are:
169 - The quality of the driver will rise as the maintenance costs (to the
170 original developer) will decrease.
171 - Other developers will add features to your driver.
172 - Other people will find and fix bugs in your driver.
173 - Other people will find tuning opportunities in your driver.
174 - Other people will update the driver for you when external interface
175 changes require it.
176 - The driver automatically gets shipped in all Linux distributions
177 without having to ask the distros to add it.
178
179As Linux supports a larger number of different devices "out of the box"
180than any other operating system, and it supports these devices on more
181different processor architectures than any other operating system, this
182proven type of development model must be doing something right :)
183
184
185
186------
187
188Thanks to Randy Dunlap, Andrew Morton, David Brownell, Hanna Linder,
189Robert Love, and Nishanth Aravamudan for their review and comments on
190early drafts of this paper.