18c2ecf20Sopenharmony_ci.. _cn_stable_api_nonsense:
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci.. include:: ../disclaimer-zh_CN.rst
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci:Original: :ref:`Documentation/process/stable-api-nonsense.rst
68c2ecf20Sopenharmony_ci           <stable_api_nonsense>`
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci译者::
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci        中文版维护者: 钟宇  TripleX Chung <xxx.phy@gmail.com>
118c2ecf20Sopenharmony_ci        中文版翻译者: 钟宇  TripleX Chung <xxx.phy@gmail.com>
128c2ecf20Sopenharmony_ci        中文版校译者: 李阳  Li Yang <leoyang.li@nxp.com>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciLinux 内核驱动接口
158c2ecf20Sopenharmony_ci==================
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci写作本文档的目的,是为了解释为什么Linux既没有二进制内核接口,也没有稳定
188c2ecf20Sopenharmony_ci的内核接口。这里所说的内核接口,是指内核里的接口,而不是内核和用户空间
198c2ecf20Sopenharmony_ci的接口。内核到用户空间的接口,是提供给应用程序使用的系统调用,系统调用
208c2ecf20Sopenharmony_ci在历史上几乎没有过变化,将来也不会有变化。我有一些老应用程序是在0.9版本
218c2ecf20Sopenharmony_ci或者更早版本的内核上编译的,在使用2.6版本内核的Linux发布上依然用得很好
228c2ecf20Sopenharmony_ci。用户和应用程序作者可以将这个接口看成是稳定的。
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci执行纲要
268c2ecf20Sopenharmony_ci--------
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci你也许以为自己想要稳定的内核接口,但是你不清楚你要的实际上不是它。你需
298c2ecf20Sopenharmony_ci要的其实是稳定的驱动程序,而你只有将驱动程序放到公版内核的源代码树里,
308c2ecf20Sopenharmony_ci才有可能达到这个目的。而且这样做还有很多其它好处,正是因为这些好处使得
318c2ecf20Sopenharmony_ciLinux能成为强壮,稳定,成熟的操作系统,这也是你最开始选择Linux的原因。
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci入门
358c2ecf20Sopenharmony_ci-----
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci只有那些写驱动程序的“怪人”才会担心内核接口的改变,对广大用户来说,既
388c2ecf20Sopenharmony_ci看不到内核接口,也不需要去关心它。
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci首先,我不打算讨论关于任何非GPL许可的内核驱动的法律问题,这些非GPL许可
418c2ecf20Sopenharmony_ci的驱动程序包括不公开源代码,隐藏源代码,二进制或者是用源代码包装,或者
428c2ecf20Sopenharmony_ci是其它任何形式的不能以GPL许可公开源代码的驱动程序。如果有法律问题,请咨
438c2ecf20Sopenharmony_ci询律师,我只是一个程序员,所以我只打算探讨技术问题(不是小看法律问题,
448c2ecf20Sopenharmony_ci法律问题很实际,并且需要一直关注)。
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci既然只谈技术问题,我们就有了下面两个主题:二进制内核接口和稳定的内核源
478c2ecf20Sopenharmony_ci代码接口。这两个问题是互相关联的,让我们先解决掉二进制接口的问题。
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci二进制内核接口
518c2ecf20Sopenharmony_ci--------------
528c2ecf20Sopenharmony_ci假如我们有一个稳定的内核源代码接口,那么自然而然的,我们就拥有了稳定的
538c2ecf20Sopenharmony_ci二进制接口,是这样的吗?错。让我们看看关于Linux内核的几点事实:
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci    - 取决于所用的C编译器的版本,不同的内核数据结构里的结构体的对齐方
568c2ecf20Sopenharmony_ci      式会有差别,代码中不同函数的表现形式也不一样(函数是不是被inline
578c2ecf20Sopenharmony_ci      编译取决于编译器行为)。不同的函数的表现形式并不重要,但是数据
588c2ecf20Sopenharmony_ci      结构内部的对齐方式很关键。
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci    - 取决于内核的配置选项,不同的选项会让内核的很多东西发生改变:
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci      - 同一个结构体可能包含不同的成员变量
638c2ecf20Sopenharmony_ci      - 有的函数可能根本不会被实现(比如编译的时候没有选择SMP支持
648c2ecf20Sopenharmony_ci        一些锁函数就会被定义成空函数)。
658c2ecf20Sopenharmony_ci      - 内核使用的内存会以不同的方式对齐,这取决于不同的内核配置选
668c2ecf20Sopenharmony_ci        项。
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci    - Linux可以在很多的不同体系结构的处理器上运行。在某个体系结构上编
698c2ecf20Sopenharmony_ci      译好的二进制驱动程序,不可能在另外一个体系结构上正确的运行。
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci对于一个特定的内核,满足这些条件并不难,使用同一个C编译器和同样的内核配
728c2ecf20Sopenharmony_ci置选项来编译驱动程序模块就可以了。这对于给一个特定Linux发布的特定版本提
738c2ecf20Sopenharmony_ci供驱动程序,是完全可以满足需求的。但是如果你要给不同发布的不同版本都发
748c2ecf20Sopenharmony_ci布一个驱动程序,就需要在每个发布上用不同的内核设置参数都编译一次内核,
758c2ecf20Sopenharmony_ci这简直跟噩梦一样。而且还要注意到,每个Linux发布还提供不同的Linux内核,
768c2ecf20Sopenharmony_ci这些内核都针对不同的硬件类型进行了优化(有很多种不同的处理器,还有不同
778c2ecf20Sopenharmony_ci的内核设置选项)。所以每发布一次驱动程序,都需要提供很多不同版本的内核
788c2ecf20Sopenharmony_ci模块。
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci相信我,如果你真的要采取这种发布方式,一定会慢慢疯掉,我很久以前就有过
818c2ecf20Sopenharmony_ci深刻的教训...
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci稳定的内核源代码接口
858c2ecf20Sopenharmony_ci--------------------
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci如果有人不将他的内核驱动程序,放入公版内核的源代码树,而又想让驱动程序
888c2ecf20Sopenharmony_ci一直保持在最新的内核中可用,那么这个话题将会变得没完没了。
898c2ecf20Sopenharmony_ci内核开发是持续而且快节奏的,从来都不会慢下来。内核开发人员在当前接口中
908c2ecf20Sopenharmony_ci找到bug,或者找到更好的实现方式。一旦发现这些,他们就很快会去修改当前的
918c2ecf20Sopenharmony_ci接口。修改接口意味着,函数名可能会改变,结构体可能被扩充或者删减,函数
928c2ecf20Sopenharmony_ci的参数也可能发生改变。一旦接口被修改,内核中使用这些接口的地方需要同时
938c2ecf20Sopenharmony_ci修正,这样才能保证所有的东西继续工作。
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci举一个例子,内核的USB驱动程序接口在USB子系统的整个生命周期中,至少经历
968c2ecf20Sopenharmony_ci了三次重写。这些重写解决以下问题:
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci    - 把数据流从同步模式改成非同步模式,这个改动减少了一些驱动程序的
998c2ecf20Sopenharmony_ci      复杂度,提高了所有USB驱动程序的吞吐率,这样几乎所有的USB设备都
1008c2ecf20Sopenharmony_ci      能以最大速率工作了。
1018c2ecf20Sopenharmony_ci    - 修改了USB核心代码中为USB驱动分配数据包内存的方式,所有的驱动都
1028c2ecf20Sopenharmony_ci      需要提供更多的参数给USB核心,以修正了很多已经被记录在案的死锁。
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci这和一些封闭源代码的操作系统形成鲜明的对比,在那些操作系统上,不得不额
1058c2ecf20Sopenharmony_ci外的维护旧的USB接口。这导致了一个可能性,新的开发者依然会不小心使用旧的
1068c2ecf20Sopenharmony_ci接口,以不恰当的方式编写代码,进而影响到操作系统的稳定性。
1078c2ecf20Sopenharmony_ci在上面的例子中,所有的开发者都同意这些重要的改动,在这样的情况下修改代
1088c2ecf20Sopenharmony_ci价很低。如果Linux保持一个稳定的内核源代码接口,那么就得创建一个新的接口
1098c2ecf20Sopenharmony_ci;旧的,有问题的接口必须一直维护,给Linux USB开发者带来额外的工作。既然
1108c2ecf20Sopenharmony_ci所有的Linux USB驱动的作者都是利用自己的时间工作,那么要求他们去做毫无意
1118c2ecf20Sopenharmony_ci义的免费额外工作,是不可能的。
1128c2ecf20Sopenharmony_ci安全问题对Linux来说十分重要。一个安全问题被发现,就会在短时间内得到修
1138c2ecf20Sopenharmony_ci正。在很多情况下,这将导致Linux内核中的一些接口被重写,以从根本上避免安
1148c2ecf20Sopenharmony_ci全问题。一旦接口被重写,所有使用这些接口的驱动程序,必须同时得到修正,
1158c2ecf20Sopenharmony_ci以确定安全问题已经得到修复并且不可能在未来还有同样的安全问题。如果内核
1168c2ecf20Sopenharmony_ci内部接口不允许改变,那么就不可能修复这样的安全问题,也不可能确认这样的
1178c2ecf20Sopenharmony_ci安全问题以后不会发生。
1188c2ecf20Sopenharmony_ci开发者一直在清理内核接口。如果一个接口没有人在使用了,它就会被删除。这
1198c2ecf20Sopenharmony_ci样可以确保内核尽可能的小,而且所有潜在的接口都会得到尽可能完整的测试
1208c2ecf20Sopenharmony_ci(没有人使用的接口是不可能得到良好的测试的)。
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci要做什么
1248c2ecf20Sopenharmony_ci--------
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci如果你写了一个Linux内核驱动,但是它还不在Linux源代码树里,作为一个开发
1278c2ecf20Sopenharmony_ci者,你应该怎么做?为每个发布的每个版本提供一个二进制驱动,那简直是一个
1288c2ecf20Sopenharmony_ci噩梦,要跟上永远处于变化之中的内核接口,也是一件辛苦活。
1298c2ecf20Sopenharmony_ci很简单,让你的驱动进入内核源代码树(要记得我们在谈论的是以GPL许可发行
1308c2ecf20Sopenharmony_ci的驱动,如果你的代码不符合GPL,那么祝你好运,你只能自己解决这个问题了,
1318c2ecf20Sopenharmony_ci你这个吸血鬼<把Andrew和Linus对吸血鬼的定义链接到这里>)。当你的代码加入
1328c2ecf20Sopenharmony_ci公版内核源代码树之后,如果一个内核接口改变,你的驱动会直接被修改接口的
1338c2ecf20Sopenharmony_ci那个人修改。保证你的驱动永远都可以编译通过,并且一直工作,你几乎不需要
1348c2ecf20Sopenharmony_ci做什么事情。
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci把驱动放到内核源代码树里会有很多的好处:
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci    - 驱动的质量会提升,而维护成本(对原始作者来说)会下降。
1398c2ecf20Sopenharmony_ci    - 其他人会给驱动添加新特性。
1408c2ecf20Sopenharmony_ci    - 其他人会找到驱动中的bug并修复。
1418c2ecf20Sopenharmony_ci    - 其他人会在驱动中找到性能优化的机会。
1428c2ecf20Sopenharmony_ci    - 当外部的接口的改变需要修改驱动程序的时候,其他人会修改驱动程序
1438c2ecf20Sopenharmony_ci    - 不需要联系任何发行商,这个驱动会自动的随着所有的Linux发布一起发
1448c2ecf20Sopenharmony_ci      布。
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci和别的操作系统相比,Linux为更多不同的设备提供现成的驱动,而且能在更多不
1478c2ecf20Sopenharmony_ci同体系结构的处理器上支持这些设备。这个经过考验的开发模式,必然是错不了
1488c2ecf20Sopenharmony_ci的 :)
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci感谢
1518c2ecf20Sopenharmony_ci----
1528c2ecf20Sopenharmony_ci感谢 Randy Dunlap, Andrew Morton, David Brownell, Hanna Linder,
1538c2ecf20Sopenharmony_ciRobert Love, and Nishanth Aravamudan 对于本文档早期版本的评审和建议。
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci英文版维护者: Greg Kroah-Hartman <greg@kroah.com>
156