1*4882a593Smuzhiyun.. _development_coding: 2*4882a593Smuzhiyun 3*4882a593SmuzhiyunGetting the code right 4*4882a593Smuzhiyun====================== 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunWhile there is much to be said for a solid and community-oriented design 7*4882a593Smuzhiyunprocess, the proof of any kernel development project is in the resulting 8*4882a593Smuzhiyuncode. It is the code which will be examined by other developers and merged 9*4882a593Smuzhiyun(or not) into the mainline tree. So it is the quality of this code which 10*4882a593Smuzhiyunwill determine the ultimate success of the project. 11*4882a593Smuzhiyun 12*4882a593SmuzhiyunThis section will examine the coding process. We'll start with a look at a 13*4882a593Smuzhiyunnumber of ways in which kernel developers can go wrong. Then the focus 14*4882a593Smuzhiyunwill shift toward doing things right and the tools which can help in that 15*4882a593Smuzhiyunquest. 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun 18*4882a593SmuzhiyunPitfalls 19*4882a593Smuzhiyun--------- 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunCoding style 22*4882a593Smuzhiyun************ 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunThe kernel has long had a standard coding style, described in 25*4882a593Smuzhiyun:ref:`Documentation/process/coding-style.rst <codingstyle>`. For much of 26*4882a593Smuzhiyunthat time, the policies described in that file were taken as being, at most, 27*4882a593Smuzhiyunadvisory. As a result, there is a substantial amount of code in the kernel 28*4882a593Smuzhiyunwhich does not meet the coding style guidelines. The presence of that code 29*4882a593Smuzhiyunleads to two independent hazards for kernel developers. 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunThe first of these is to believe that the kernel coding standards do not 32*4882a593Smuzhiyunmatter and are not enforced. The truth of the matter is that adding new 33*4882a593Smuzhiyuncode to the kernel is very difficult if that code is not coded according to 34*4882a593Smuzhiyunthe standard; many developers will request that the code be reformatted 35*4882a593Smuzhiyunbefore they will even review it. A code base as large as the kernel 36*4882a593Smuzhiyunrequires some uniformity of code to make it possible for developers to 37*4882a593Smuzhiyunquickly understand any part of it. So there is no longer room for 38*4882a593Smuzhiyunstrangely-formatted code. 39*4882a593Smuzhiyun 40*4882a593SmuzhiyunOccasionally, the kernel's coding style will run into conflict with an 41*4882a593Smuzhiyunemployer's mandated style. In such cases, the kernel's style will have to 42*4882a593Smuzhiyunwin before the code can be merged. Putting code into the kernel means 43*4882a593Smuzhiyungiving up a degree of control in a number of ways - including control over 44*4882a593Smuzhiyunhow the code is formatted. 45*4882a593Smuzhiyun 46*4882a593SmuzhiyunThe other trap is to assume that code which is already in the kernel is 47*4882a593Smuzhiyunurgently in need of coding style fixes. Developers may start to generate 48*4882a593Smuzhiyunreformatting patches as a way of gaining familiarity with the process, or 49*4882a593Smuzhiyunas a way of getting their name into the kernel changelogs - or both. But 50*4882a593Smuzhiyunpure coding style fixes are seen as noise by the development community; 51*4882a593Smuzhiyunthey tend to get a chilly reception. So this type of patch is best 52*4882a593Smuzhiyunavoided. It is natural to fix the style of a piece of code while working 53*4882a593Smuzhiyunon it for other reasons, but coding style changes should not be made for 54*4882a593Smuzhiyuntheir own sake. 55*4882a593Smuzhiyun 56*4882a593SmuzhiyunThe coding style document also should not be read as an absolute law which 57*4882a593Smuzhiyuncan never be transgressed. If there is a good reason to go against the 58*4882a593Smuzhiyunstyle (a line which becomes far less readable if split to fit within the 59*4882a593Smuzhiyun80-column limit, for example), just do it. 60*4882a593Smuzhiyun 61*4882a593SmuzhiyunNote that you can also use the ``clang-format`` tool to help you with 62*4882a593Smuzhiyunthese rules, to quickly re-format parts of your code automatically, 63*4882a593Smuzhiyunand to review full files in order to spot coding style mistakes, 64*4882a593Smuzhiyuntypos and possible improvements. It is also handy for sorting ``#includes``, 65*4882a593Smuzhiyunfor aligning variables/macros, for reflowing text and other similar tasks. 66*4882a593SmuzhiyunSee the file :ref:`Documentation/process/clang-format.rst <clangformat>` 67*4882a593Smuzhiyunfor more details. 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun 70*4882a593SmuzhiyunAbstraction layers 71*4882a593Smuzhiyun****************** 72*4882a593Smuzhiyun 73*4882a593SmuzhiyunComputer Science professors teach students to make extensive use of 74*4882a593Smuzhiyunabstraction layers in the name of flexibility and information hiding. 75*4882a593SmuzhiyunCertainly the kernel makes extensive use of abstraction; no project 76*4882a593Smuzhiyuninvolving several million lines of code could do otherwise and survive. 77*4882a593SmuzhiyunBut experience has shown that excessive or premature abstraction can be 78*4882a593Smuzhiyunjust as harmful as premature optimization. Abstraction should be used to 79*4882a593Smuzhiyunthe level required and no further. 80*4882a593Smuzhiyun 81*4882a593SmuzhiyunAt a simple level, consider a function which has an argument which is 82*4882a593Smuzhiyunalways passed as zero by all callers. One could retain that argument just 83*4882a593Smuzhiyunin case somebody eventually needs to use the extra flexibility that it 84*4882a593Smuzhiyunprovides. By that time, though, chances are good that the code which 85*4882a593Smuzhiyunimplements this extra argument has been broken in some subtle way which was 86*4882a593Smuzhiyunnever noticed - because it has never been used. Or, when the need for 87*4882a593Smuzhiyunextra flexibility arises, it does not do so in a way which matches the 88*4882a593Smuzhiyunprogrammer's early expectation. Kernel developers will routinely submit 89*4882a593Smuzhiyunpatches to remove unused arguments; they should, in general, not be added 90*4882a593Smuzhiyunin the first place. 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunAbstraction layers which hide access to hardware - often to allow the bulk 93*4882a593Smuzhiyunof a driver to be used with multiple operating systems - are especially 94*4882a593Smuzhiyunfrowned upon. Such layers obscure the code and may impose a performance 95*4882a593Smuzhiyunpenalty; they do not belong in the Linux kernel. 96*4882a593Smuzhiyun 97*4882a593SmuzhiyunOn the other hand, if you find yourself copying significant amounts of code 98*4882a593Smuzhiyunfrom another kernel subsystem, it is time to ask whether it would, in fact, 99*4882a593Smuzhiyunmake sense to pull out some of that code into a separate library or to 100*4882a593Smuzhiyunimplement that functionality at a higher level. There is no value in 101*4882a593Smuzhiyunreplicating the same code throughout the kernel. 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun#ifdef and preprocessor use in general 105*4882a593Smuzhiyun************************************** 106*4882a593Smuzhiyun 107*4882a593SmuzhiyunThe C preprocessor seems to present a powerful temptation to some C 108*4882a593Smuzhiyunprogrammers, who see it as a way to efficiently encode a great deal of 109*4882a593Smuzhiyunflexibility into a source file. But the preprocessor is not C, and heavy 110*4882a593Smuzhiyunuse of it results in code which is much harder for others to read and 111*4882a593Smuzhiyunharder for the compiler to check for correctness. Heavy preprocessor use 112*4882a593Smuzhiyunis almost always a sign of code which needs some cleanup work. 113*4882a593Smuzhiyun 114*4882a593SmuzhiyunConditional compilation with #ifdef is, indeed, a powerful feature, and it 115*4882a593Smuzhiyunis used within the kernel. But there is little desire to see code which is 116*4882a593Smuzhiyunsprinkled liberally with #ifdef blocks. As a general rule, #ifdef use 117*4882a593Smuzhiyunshould be confined to header files whenever possible. 118*4882a593SmuzhiyunConditionally-compiled code can be confined to functions which, if the code 119*4882a593Smuzhiyunis not to be present, simply become empty. The compiler will then quietly 120*4882a593Smuzhiyunoptimize out the call to the empty function. The result is far cleaner 121*4882a593Smuzhiyuncode which is easier to follow. 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunC preprocessor macros present a number of hazards, including possible 124*4882a593Smuzhiyunmultiple evaluation of expressions with side effects and no type safety. 125*4882a593SmuzhiyunIf you are tempted to define a macro, consider creating an inline function 126*4882a593Smuzhiyuninstead. The code which results will be the same, but inline functions are 127*4882a593Smuzhiyuneasier to read, do not evaluate their arguments multiple times, and allow 128*4882a593Smuzhiyunthe compiler to perform type checking on the arguments and return value. 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun 131*4882a593SmuzhiyunInline functions 132*4882a593Smuzhiyun**************** 133*4882a593Smuzhiyun 134*4882a593SmuzhiyunInline functions present a hazard of their own, though. Programmers can 135*4882a593Smuzhiyunbecome enamored of the perceived efficiency inherent in avoiding a function 136*4882a593Smuzhiyuncall and fill a source file with inline functions. Those functions, 137*4882a593Smuzhiyunhowever, can actually reduce performance. Since their code is replicated 138*4882a593Smuzhiyunat each call site, they end up bloating the size of the compiled kernel. 139*4882a593SmuzhiyunThat, in turn, creates pressure on the processor's memory caches, which can 140*4882a593Smuzhiyunslow execution dramatically. Inline functions, as a rule, should be quite 141*4882a593Smuzhiyunsmall and relatively rare. The cost of a function call, after all, is not 142*4882a593Smuzhiyunthat high; the creation of large numbers of inline functions is a classic 143*4882a593Smuzhiyunexample of premature optimization. 144*4882a593Smuzhiyun 145*4882a593SmuzhiyunIn general, kernel programmers ignore cache effects at their peril. The 146*4882a593Smuzhiyunclassic time/space tradeoff taught in beginning data structures classes 147*4882a593Smuzhiyunoften does not apply to contemporary hardware. Space *is* time, in that a 148*4882a593Smuzhiyunlarger program will run slower than one which is more compact. 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunMore recent compilers take an increasingly active role in deciding whether 151*4882a593Smuzhiyuna given function should actually be inlined or not. So the liberal 152*4882a593Smuzhiyunplacement of "inline" keywords may not just be excessive; it could also be 153*4882a593Smuzhiyunirrelevant. 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun 156*4882a593SmuzhiyunLocking 157*4882a593Smuzhiyun******* 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunIn May, 2006, the "Devicescape" networking stack was, with great 160*4882a593Smuzhiyunfanfare, released under the GPL and made available for inclusion in the 161*4882a593Smuzhiyunmainline kernel. This donation was welcome news; support for wireless 162*4882a593Smuzhiyunnetworking in Linux was considered substandard at best, and the Devicescape 163*4882a593Smuzhiyunstack offered the promise of fixing that situation. Yet, this code did not 164*4882a593Smuzhiyunactually make it into the mainline until June, 2007 (2.6.22). What 165*4882a593Smuzhiyunhappened? 166*4882a593Smuzhiyun 167*4882a593SmuzhiyunThis code showed a number of signs of having been developed behind 168*4882a593Smuzhiyuncorporate doors. But one large problem in particular was that it was not 169*4882a593Smuzhiyundesigned to work on multiprocessor systems. Before this networking stack 170*4882a593Smuzhiyun(now called mac80211) could be merged, a locking scheme needed to be 171*4882a593Smuzhiyunretrofitted onto it. 172*4882a593Smuzhiyun 173*4882a593SmuzhiyunOnce upon a time, Linux kernel code could be developed without thinking 174*4882a593Smuzhiyunabout the concurrency issues presented by multiprocessor systems. Now, 175*4882a593Smuzhiyunhowever, this document is being written on a dual-core laptop. Even on 176*4882a593Smuzhiyunsingle-processor systems, work being done to improve responsiveness will 177*4882a593Smuzhiyunraise the level of concurrency within the kernel. The days when kernel 178*4882a593Smuzhiyuncode could be written without thinking about locking are long past. 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunAny resource (data structures, hardware registers, etc.) which could be 181*4882a593Smuzhiyunaccessed concurrently by more than one thread must be protected by a lock. 182*4882a593SmuzhiyunNew code should be written with this requirement in mind; retrofitting 183*4882a593Smuzhiyunlocking after the fact is a rather more difficult task. Kernel developers 184*4882a593Smuzhiyunshould take the time to understand the available locking primitives well 185*4882a593Smuzhiyunenough to pick the right tool for the job. Code which shows a lack of 186*4882a593Smuzhiyunattention to concurrency will have a difficult path into the mainline. 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunRegressions 190*4882a593Smuzhiyun*********** 191*4882a593Smuzhiyun 192*4882a593SmuzhiyunOne final hazard worth mentioning is this: it can be tempting to make a 193*4882a593Smuzhiyunchange (which may bring big improvements) which causes something to break 194*4882a593Smuzhiyunfor existing users. This kind of change is called a "regression," and 195*4882a593Smuzhiyunregressions have become most unwelcome in the mainline kernel. With few 196*4882a593Smuzhiyunexceptions, changes which cause regressions will be backed out if the 197*4882a593Smuzhiyunregression cannot be fixed in a timely manner. Far better to avoid the 198*4882a593Smuzhiyunregression in the first place. 199*4882a593Smuzhiyun 200*4882a593SmuzhiyunIt is often argued that a regression can be justified if it causes things 201*4882a593Smuzhiyunto work for more people than it creates problems for. Why not make a 202*4882a593Smuzhiyunchange if it brings new functionality to ten systems for each one it 203*4882a593Smuzhiyunbreaks? The best answer to this question was expressed by Linus in July, 204*4882a593Smuzhiyun2007: 205*4882a593Smuzhiyun 206*4882a593Smuzhiyun:: 207*4882a593Smuzhiyun 208*4882a593Smuzhiyun So we don't fix bugs by introducing new problems. That way lies 209*4882a593Smuzhiyun madness, and nobody ever knows if you actually make any real 210*4882a593Smuzhiyun progress at all. Is it two steps forwards, one step back, or one 211*4882a593Smuzhiyun step forward and two steps back? 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun(https://lwn.net/Articles/243460/). 214*4882a593Smuzhiyun 215*4882a593SmuzhiyunAn especially unwelcome type of regression is any sort of change to the 216*4882a593Smuzhiyunuser-space ABI. Once an interface has been exported to user space, it must 217*4882a593Smuzhiyunbe supported indefinitely. This fact makes the creation of user-space 218*4882a593Smuzhiyuninterfaces particularly challenging: since they cannot be changed in 219*4882a593Smuzhiyunincompatible ways, they must be done right the first time. For this 220*4882a593Smuzhiyunreason, a great deal of thought, clear documentation, and wide review for 221*4882a593Smuzhiyunuser-space interfaces is always required. 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun 224*4882a593SmuzhiyunCode checking tools 225*4882a593Smuzhiyun------------------- 226*4882a593Smuzhiyun 227*4882a593SmuzhiyunFor now, at least, the writing of error-free code remains an ideal that few 228*4882a593Smuzhiyunof us can reach. What we can hope to do, though, is to catch and fix as 229*4882a593Smuzhiyunmany of those errors as possible before our code goes into the mainline 230*4882a593Smuzhiyunkernel. To that end, the kernel developers have put together an impressive 231*4882a593Smuzhiyunarray of tools which can catch a wide variety of obscure problems in an 232*4882a593Smuzhiyunautomated way. Any problem caught by the computer is a problem which will 233*4882a593Smuzhiyunnot afflict a user later on, so it stands to reason that the automated 234*4882a593Smuzhiyuntools should be used whenever possible. 235*4882a593Smuzhiyun 236*4882a593SmuzhiyunThe first step is simply to heed the warnings produced by the compiler. 237*4882a593SmuzhiyunContemporary versions of gcc can detect (and warn about) a large number of 238*4882a593Smuzhiyunpotential errors. Quite often, these warnings point to real problems. 239*4882a593SmuzhiyunCode submitted for review should, as a rule, not produce any compiler 240*4882a593Smuzhiyunwarnings. When silencing warnings, take care to understand the real cause 241*4882a593Smuzhiyunand try to avoid "fixes" which make the warning go away without addressing 242*4882a593Smuzhiyunits cause. 243*4882a593Smuzhiyun 244*4882a593SmuzhiyunNote that not all compiler warnings are enabled by default. Build the 245*4882a593Smuzhiyunkernel with "make EXTRA_CFLAGS=-W" to get the full set. 246*4882a593Smuzhiyun 247*4882a593SmuzhiyunThe kernel provides several configuration options which turn on debugging 248*4882a593Smuzhiyunfeatures; most of these are found in the "kernel hacking" submenu. Several 249*4882a593Smuzhiyunof these options should be turned on for any kernel used for development or 250*4882a593Smuzhiyuntesting purposes. In particular, you should turn on: 251*4882a593Smuzhiyun 252*4882a593Smuzhiyun - ENABLE_MUST_CHECK and FRAME_WARN to get an 253*4882a593Smuzhiyun extra set of warnings for problems like the use of deprecated interfaces 254*4882a593Smuzhiyun or ignoring an important return value from a function. The output 255*4882a593Smuzhiyun generated by these warnings can be verbose, but one need not worry about 256*4882a593Smuzhiyun warnings from other parts of the kernel. 257*4882a593Smuzhiyun 258*4882a593Smuzhiyun - DEBUG_OBJECTS will add code to track the lifetime of various objects 259*4882a593Smuzhiyun created by the kernel and warn when things are done out of order. If 260*4882a593Smuzhiyun you are adding a subsystem which creates (and exports) complex objects 261*4882a593Smuzhiyun of its own, consider adding support for the object debugging 262*4882a593Smuzhiyun infrastructure. 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun - DEBUG_SLAB can find a variety of memory allocation and use errors; it 265*4882a593Smuzhiyun should be used on most development kernels. 266*4882a593Smuzhiyun 267*4882a593Smuzhiyun - DEBUG_SPINLOCK, DEBUG_ATOMIC_SLEEP, and DEBUG_MUTEXES will find a 268*4882a593Smuzhiyun number of common locking errors. 269*4882a593Smuzhiyun 270*4882a593SmuzhiyunThere are quite a few other debugging options, some of which will be 271*4882a593Smuzhiyundiscussed below. Some of them have a significant performance impact and 272*4882a593Smuzhiyunshould not be used all of the time. But some time spent learning the 273*4882a593Smuzhiyunavailable options will likely be paid back many times over in short order. 274*4882a593Smuzhiyun 275*4882a593SmuzhiyunOne of the heavier debugging tools is the locking checker, or "lockdep." 276*4882a593SmuzhiyunThis tool will track the acquisition and release of every lock (spinlock or 277*4882a593Smuzhiyunmutex) in the system, the order in which locks are acquired relative to 278*4882a593Smuzhiyuneach other, the current interrupt environment, and more. It can then 279*4882a593Smuzhiyunensure that locks are always acquired in the same order, that the same 280*4882a593Smuzhiyuninterrupt assumptions apply in all situations, and so on. In other words, 281*4882a593Smuzhiyunlockdep can find a number of scenarios in which the system could, on rare 282*4882a593Smuzhiyunoccasion, deadlock. This kind of problem can be painful (for both 283*4882a593Smuzhiyundevelopers and users) in a deployed system; lockdep allows them to be found 284*4882a593Smuzhiyunin an automated manner ahead of time. Code with any sort of non-trivial 285*4882a593Smuzhiyunlocking should be run with lockdep enabled before being submitted for 286*4882a593Smuzhiyuninclusion. 287*4882a593Smuzhiyun 288*4882a593SmuzhiyunAs a diligent kernel programmer, you will, beyond doubt, check the return 289*4882a593Smuzhiyunstatus of any operation (such as a memory allocation) which can fail. The 290*4882a593Smuzhiyunfact of the matter, though, is that the resulting failure recovery paths 291*4882a593Smuzhiyunare, probably, completely untested. Untested code tends to be broken code; 292*4882a593Smuzhiyunyou could be much more confident of your code if all those error-handling 293*4882a593Smuzhiyunpaths had been exercised a few times. 294*4882a593Smuzhiyun 295*4882a593SmuzhiyunThe kernel provides a fault injection framework which can do exactly that, 296*4882a593Smuzhiyunespecially where memory allocations are involved. With fault injection 297*4882a593Smuzhiyunenabled, a configurable percentage of memory allocations will be made to 298*4882a593Smuzhiyunfail; these failures can be restricted to a specific range of code. 299*4882a593SmuzhiyunRunning with fault injection enabled allows the programmer to see how the 300*4882a593Smuzhiyuncode responds when things go badly. See 301*4882a593SmuzhiyunDocumentation/fault-injection/fault-injection.rst for more information on 302*4882a593Smuzhiyunhow to use this facility. 303*4882a593Smuzhiyun 304*4882a593SmuzhiyunOther kinds of errors can be found with the "sparse" static analysis tool. 305*4882a593SmuzhiyunWith sparse, the programmer can be warned about confusion between 306*4882a593Smuzhiyunuser-space and kernel-space addresses, mixture of big-endian and 307*4882a593Smuzhiyunsmall-endian quantities, the passing of integer values where a set of bit 308*4882a593Smuzhiyunflags is expected, and so on. Sparse must be installed separately (it can 309*4882a593Smuzhiyunbe found at https://sparse.wiki.kernel.org/index.php/Main_Page if your 310*4882a593Smuzhiyundistributor does not package it); it can then be run on the code by adding 311*4882a593Smuzhiyun"C=1" to your make command. 312*4882a593Smuzhiyun 313*4882a593SmuzhiyunThe "Coccinelle" tool (http://coccinelle.lip6.fr/) is able to find a wide 314*4882a593Smuzhiyunvariety of potential coding problems; it can also propose fixes for those 315*4882a593Smuzhiyunproblems. Quite a few "semantic patches" for the kernel have been packaged 316*4882a593Smuzhiyununder the scripts/coccinelle directory; running "make coccicheck" will run 317*4882a593Smuzhiyunthrough those semantic patches and report on any problems found. See 318*4882a593Smuzhiyun:ref:`Documentation/dev-tools/coccinelle.rst <devtools_coccinelle>` 319*4882a593Smuzhiyunfor more information. 320*4882a593Smuzhiyun 321*4882a593SmuzhiyunOther kinds of portability errors are best found by compiling your code for 322*4882a593Smuzhiyunother architectures. If you do not happen to have an S/390 system or a 323*4882a593SmuzhiyunBlackfin development board handy, you can still perform the compilation 324*4882a593Smuzhiyunstep. A large set of cross compilers for x86 systems can be found at 325*4882a593Smuzhiyun 326*4882a593Smuzhiyun https://www.kernel.org/pub/tools/crosstool/ 327*4882a593Smuzhiyun 328*4882a593SmuzhiyunSome time spent installing and using these compilers will help avoid 329*4882a593Smuzhiyunembarrassment later. 330*4882a593Smuzhiyun 331*4882a593Smuzhiyun 332*4882a593SmuzhiyunDocumentation 333*4882a593Smuzhiyun------------- 334*4882a593Smuzhiyun 335*4882a593SmuzhiyunDocumentation has often been more the exception than the rule with kernel 336*4882a593Smuzhiyundevelopment. Even so, adequate documentation will help to ease the merging 337*4882a593Smuzhiyunof new code into the kernel, make life easier for other developers, and 338*4882a593Smuzhiyunwill be helpful for your users. In many cases, the addition of 339*4882a593Smuzhiyundocumentation has become essentially mandatory. 340*4882a593Smuzhiyun 341*4882a593SmuzhiyunThe first piece of documentation for any patch is its associated 342*4882a593Smuzhiyunchangelog. Log entries should describe the problem being solved, the form 343*4882a593Smuzhiyunof the solution, the people who worked on the patch, any relevant 344*4882a593Smuzhiyuneffects on performance, and anything else that might be needed to 345*4882a593Smuzhiyununderstand the patch. Be sure that the changelog says *why* the patch is 346*4882a593Smuzhiyunworth applying; a surprising number of developers fail to provide that 347*4882a593Smuzhiyuninformation. 348*4882a593Smuzhiyun 349*4882a593SmuzhiyunAny code which adds a new user-space interface - including new sysfs or 350*4882a593Smuzhiyun/proc files - should include documentation of that interface which enables 351*4882a593Smuzhiyunuser-space developers to know what they are working with. See 352*4882a593SmuzhiyunDocumentation/ABI/README for a description of how this documentation should 353*4882a593Smuzhiyunbe formatted and what information needs to be provided. 354*4882a593Smuzhiyun 355*4882a593SmuzhiyunThe file :ref:`Documentation/admin-guide/kernel-parameters.rst 356*4882a593Smuzhiyun<kernelparameters>` describes all of the kernel's boot-time parameters. 357*4882a593SmuzhiyunAny patch which adds new parameters should add the appropriate entries to 358*4882a593Smuzhiyunthis file. 359*4882a593Smuzhiyun 360*4882a593SmuzhiyunAny new configuration options must be accompanied by help text which 361*4882a593Smuzhiyunclearly explains the options and when the user might want to select them. 362*4882a593Smuzhiyun 363*4882a593SmuzhiyunInternal API information for many subsystems is documented by way of 364*4882a593Smuzhiyunspecially-formatted comments; these comments can be extracted and formatted 365*4882a593Smuzhiyunin a number of ways by the "kernel-doc" script. If you are working within 366*4882a593Smuzhiyuna subsystem which has kerneldoc comments, you should maintain them and add 367*4882a593Smuzhiyunthem, as appropriate, for externally-available functions. Even in areas 368*4882a593Smuzhiyunwhich have not been so documented, there is no harm in adding kerneldoc 369*4882a593Smuzhiyuncomments for the future; indeed, this can be a useful activity for 370*4882a593Smuzhiyunbeginning kernel developers. The format of these comments, along with some 371*4882a593Smuzhiyuninformation on how to create kerneldoc templates can be found at 372*4882a593Smuzhiyun:ref:`Documentation/doc-guide/ <doc_guide>`. 373*4882a593Smuzhiyun 374*4882a593SmuzhiyunAnybody who reads through a significant amount of existing kernel code will 375*4882a593Smuzhiyunnote that, often, comments are most notable by their absence. Once again, 376*4882a593Smuzhiyunthe expectations for new code are higher than they were in the past; 377*4882a593Smuzhiyunmerging uncommented code will be harder. That said, there is little desire 378*4882a593Smuzhiyunfor verbosely-commented code. The code should, itself, be readable, with 379*4882a593Smuzhiyuncomments explaining the more subtle aspects. 380*4882a593Smuzhiyun 381*4882a593SmuzhiyunCertain things should always be commented. Uses of memory barriers should 382*4882a593Smuzhiyunbe accompanied by a line explaining why the barrier is necessary. The 383*4882a593Smuzhiyunlocking rules for data structures generally need to be explained somewhere. 384*4882a593SmuzhiyunMajor data structures need comprehensive documentation in general. 385*4882a593SmuzhiyunNon-obvious dependencies between separate bits of code should be pointed 386*4882a593Smuzhiyunout. Anything which might tempt a code janitor to make an incorrect 387*4882a593Smuzhiyun"cleanup" needs a comment saying why it is done the way it is. And so on. 388*4882a593Smuzhiyun 389*4882a593Smuzhiyun 390*4882a593SmuzhiyunInternal API changes 391*4882a593Smuzhiyun-------------------- 392*4882a593Smuzhiyun 393*4882a593SmuzhiyunThe binary interface provided by the kernel to user space cannot be broken 394*4882a593Smuzhiyunexcept under the most severe circumstances. The kernel's internal 395*4882a593Smuzhiyunprogramming interfaces, instead, are highly fluid and can be changed when 396*4882a593Smuzhiyunthe need arises. If you find yourself having to work around a kernel API, 397*4882a593Smuzhiyunor simply not using a specific functionality because it does not meet your 398*4882a593Smuzhiyunneeds, that may be a sign that the API needs to change. As a kernel 399*4882a593Smuzhiyundeveloper, you are empowered to make such changes. 400*4882a593Smuzhiyun 401*4882a593SmuzhiyunThere are, of course, some catches. API changes can be made, but they need 402*4882a593Smuzhiyunto be well justified. So any patch making an internal API change should be 403*4882a593Smuzhiyunaccompanied by a description of what the change is and why it is 404*4882a593Smuzhiyunnecessary. This kind of change should also be broken out into a separate 405*4882a593Smuzhiyunpatch, rather than buried within a larger patch. 406*4882a593Smuzhiyun 407*4882a593SmuzhiyunThe other catch is that a developer who changes an internal API is 408*4882a593Smuzhiyungenerally charged with the task of fixing any code within the kernel tree 409*4882a593Smuzhiyunwhich is broken by the change. For a widely-used function, this duty can 410*4882a593Smuzhiyunlead to literally hundreds or thousands of changes - many of which are 411*4882a593Smuzhiyunlikely to conflict with work being done by other developers. Needless to 412*4882a593Smuzhiyunsay, this can be a large job, so it is best to be sure that the 413*4882a593Smuzhiyunjustification is solid. Note that the Coccinelle tool can help with 414*4882a593Smuzhiyunwide-ranging API changes. 415*4882a593Smuzhiyun 416*4882a593SmuzhiyunWhen making an incompatible API change, one should, whenever possible, 417*4882a593Smuzhiyunensure that code which has not been updated is caught by the compiler. 418*4882a593SmuzhiyunThis will help you to be sure that you have found all in-tree uses of that 419*4882a593Smuzhiyuninterface. It will also alert developers of out-of-tree code that there is 420*4882a593Smuzhiyuna change that they need to respond to. Supporting out-of-tree code is not 421*4882a593Smuzhiyunsomething that kernel developers need to be worried about, but we also do 422*4882a593Smuzhiyunnot have to make life harder for out-of-tree developers than it needs to 423*4882a593Smuzhiyunbe. 424