stable_api_nonsense.txt 9.3 KB

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