stable_api_nonsense.txt 9.2 KB

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