162306a36Sopenharmony_ci.. _development_coding: 262306a36Sopenharmony_ci 362306a36Sopenharmony_ciGetting the code right 462306a36Sopenharmony_ci====================== 562306a36Sopenharmony_ci 662306a36Sopenharmony_ciWhile there is much to be said for a solid and community-oriented design 762306a36Sopenharmony_ciprocess, the proof of any kernel development project is in the resulting 862306a36Sopenharmony_cicode. It is the code which will be examined by other developers and merged 962306a36Sopenharmony_ci(or not) into the mainline tree. So it is the quality of this code which 1062306a36Sopenharmony_ciwill determine the ultimate success of the project. 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ciThis section will examine the coding process. We'll start with a look at a 1362306a36Sopenharmony_cinumber of ways in which kernel developers can go wrong. Then the focus 1462306a36Sopenharmony_ciwill shift toward doing things right and the tools which can help in that 1562306a36Sopenharmony_ciquest. 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ciPitfalls 1962306a36Sopenharmony_ci--------- 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciCoding style 2262306a36Sopenharmony_ci************ 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ciThe kernel has long had a standard coding style, described in 2562306a36Sopenharmony_ci:ref:`Documentation/process/coding-style.rst <codingstyle>`. For much of 2662306a36Sopenharmony_cithat time, the policies described in that file were taken as being, at most, 2762306a36Sopenharmony_ciadvisory. As a result, there is a substantial amount of code in the kernel 2862306a36Sopenharmony_ciwhich does not meet the coding style guidelines. The presence of that code 2962306a36Sopenharmony_cileads to two independent hazards for kernel developers. 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ciThe first of these is to believe that the kernel coding standards do not 3262306a36Sopenharmony_cimatter and are not enforced. The truth of the matter is that adding new 3362306a36Sopenharmony_cicode to the kernel is very difficult if that code is not coded according to 3462306a36Sopenharmony_cithe standard; many developers will request that the code be reformatted 3562306a36Sopenharmony_cibefore they will even review it. A code base as large as the kernel 3662306a36Sopenharmony_cirequires some uniformity of code to make it possible for developers to 3762306a36Sopenharmony_ciquickly understand any part of it. So there is no longer room for 3862306a36Sopenharmony_cistrangely-formatted code. 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ciOccasionally, the kernel's coding style will run into conflict with an 4162306a36Sopenharmony_ciemployer's mandated style. In such cases, the kernel's style will have to 4262306a36Sopenharmony_ciwin before the code can be merged. Putting code into the kernel means 4362306a36Sopenharmony_cigiving up a degree of control in a number of ways - including control over 4462306a36Sopenharmony_cihow the code is formatted. 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ciThe other trap is to assume that code which is already in the kernel is 4762306a36Sopenharmony_ciurgently in need of coding style fixes. Developers may start to generate 4862306a36Sopenharmony_cireformatting patches as a way of gaining familiarity with the process, or 4962306a36Sopenharmony_cias a way of getting their name into the kernel changelogs - or both. But 5062306a36Sopenharmony_cipure coding style fixes are seen as noise by the development community; 5162306a36Sopenharmony_cithey tend to get a chilly reception. So this type of patch is best 5262306a36Sopenharmony_ciavoided. It is natural to fix the style of a piece of code while working 5362306a36Sopenharmony_cion it for other reasons, but coding style changes should not be made for 5462306a36Sopenharmony_citheir own sake. 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ciThe coding style document also should not be read as an absolute law which 5762306a36Sopenharmony_cican never be transgressed. If there is a good reason to go against the 5862306a36Sopenharmony_cistyle (a line which becomes far less readable if split to fit within the 5962306a36Sopenharmony_ci80-column limit, for example), just do it. 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ciNote that you can also use the ``clang-format`` tool to help you with 6262306a36Sopenharmony_cithese rules, to quickly re-format parts of your code automatically, 6362306a36Sopenharmony_ciand to review full files in order to spot coding style mistakes, 6462306a36Sopenharmony_citypos and possible improvements. It is also handy for sorting ``#includes``, 6562306a36Sopenharmony_cifor aligning variables/macros, for reflowing text and other similar tasks. 6662306a36Sopenharmony_ciSee the file :ref:`Documentation/process/clang-format.rst <clangformat>` 6762306a36Sopenharmony_cifor more details. 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ciAbstraction layers 7162306a36Sopenharmony_ci****************** 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ciComputer Science professors teach students to make extensive use of 7462306a36Sopenharmony_ciabstraction layers in the name of flexibility and information hiding. 7562306a36Sopenharmony_ciCertainly the kernel makes extensive use of abstraction; no project 7662306a36Sopenharmony_ciinvolving several million lines of code could do otherwise and survive. 7762306a36Sopenharmony_ciBut experience has shown that excessive or premature abstraction can be 7862306a36Sopenharmony_cijust as harmful as premature optimization. Abstraction should be used to 7962306a36Sopenharmony_cithe level required and no further. 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ciAt a simple level, consider a function which has an argument which is 8262306a36Sopenharmony_cialways passed as zero by all callers. One could retain that argument just 8362306a36Sopenharmony_ciin case somebody eventually needs to use the extra flexibility that it 8462306a36Sopenharmony_ciprovides. By that time, though, chances are good that the code which 8562306a36Sopenharmony_ciimplements this extra argument has been broken in some subtle way which was 8662306a36Sopenharmony_cinever noticed - because it has never been used. Or, when the need for 8762306a36Sopenharmony_ciextra flexibility arises, it does not do so in a way which matches the 8862306a36Sopenharmony_ciprogrammer's early expectation. Kernel developers will routinely submit 8962306a36Sopenharmony_cipatches to remove unused arguments; they should, in general, not be added 9062306a36Sopenharmony_ciin the first place. 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ciAbstraction layers which hide access to hardware - often to allow the bulk 9362306a36Sopenharmony_ciof a driver to be used with multiple operating systems - are especially 9462306a36Sopenharmony_cifrowned upon. Such layers obscure the code and may impose a performance 9562306a36Sopenharmony_cipenalty; they do not belong in the Linux kernel. 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ciOn the other hand, if you find yourself copying significant amounts of code 9862306a36Sopenharmony_cifrom another kernel subsystem, it is time to ask whether it would, in fact, 9962306a36Sopenharmony_cimake sense to pull out some of that code into a separate library or to 10062306a36Sopenharmony_ciimplement that functionality at a higher level. There is no value in 10162306a36Sopenharmony_cireplicating the same code throughout the kernel. 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci#ifdef and preprocessor use in general 10562306a36Sopenharmony_ci************************************** 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ciThe C preprocessor seems to present a powerful temptation to some C 10862306a36Sopenharmony_ciprogrammers, who see it as a way to efficiently encode a great deal of 10962306a36Sopenharmony_ciflexibility into a source file. But the preprocessor is not C, and heavy 11062306a36Sopenharmony_ciuse of it results in code which is much harder for others to read and 11162306a36Sopenharmony_ciharder for the compiler to check for correctness. Heavy preprocessor use 11262306a36Sopenharmony_ciis almost always a sign of code which needs some cleanup work. 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ciConditional compilation with #ifdef is, indeed, a powerful feature, and it 11562306a36Sopenharmony_ciis used within the kernel. But there is little desire to see code which is 11662306a36Sopenharmony_cisprinkled liberally with #ifdef blocks. As a general rule, #ifdef use 11762306a36Sopenharmony_cishould be confined to header files whenever possible. 11862306a36Sopenharmony_ciConditionally-compiled code can be confined to functions which, if the code 11962306a36Sopenharmony_ciis not to be present, simply become empty. The compiler will then quietly 12062306a36Sopenharmony_cioptimize out the call to the empty function. The result is far cleaner 12162306a36Sopenharmony_cicode which is easier to follow. 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ciC preprocessor macros present a number of hazards, including possible 12462306a36Sopenharmony_cimultiple evaluation of expressions with side effects and no type safety. 12562306a36Sopenharmony_ciIf you are tempted to define a macro, consider creating an inline function 12662306a36Sopenharmony_ciinstead. The code which results will be the same, but inline functions are 12762306a36Sopenharmony_cieasier to read, do not evaluate their arguments multiple times, and allow 12862306a36Sopenharmony_cithe compiler to perform type checking on the arguments and return value. 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ciInline functions 13262306a36Sopenharmony_ci**************** 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ciInline functions present a hazard of their own, though. Programmers can 13562306a36Sopenharmony_cibecome enamored of the perceived efficiency inherent in avoiding a function 13662306a36Sopenharmony_cicall and fill a source file with inline functions. Those functions, 13762306a36Sopenharmony_cihowever, can actually reduce performance. Since their code is replicated 13862306a36Sopenharmony_ciat each call site, they end up bloating the size of the compiled kernel. 13962306a36Sopenharmony_ciThat, in turn, creates pressure on the processor's memory caches, which can 14062306a36Sopenharmony_cislow execution dramatically. Inline functions, as a rule, should be quite 14162306a36Sopenharmony_cismall and relatively rare. The cost of a function call, after all, is not 14262306a36Sopenharmony_cithat high; the creation of large numbers of inline functions is a classic 14362306a36Sopenharmony_ciexample of premature optimization. 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciIn general, kernel programmers ignore cache effects at their peril. The 14662306a36Sopenharmony_ciclassic time/space tradeoff taught in beginning data structures classes 14762306a36Sopenharmony_cioften does not apply to contemporary hardware. Space *is* time, in that a 14862306a36Sopenharmony_cilarger program will run slower than one which is more compact. 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ciMore recent compilers take an increasingly active role in deciding whether 15162306a36Sopenharmony_cia given function should actually be inlined or not. So the liberal 15262306a36Sopenharmony_ciplacement of "inline" keywords may not just be excessive; it could also be 15362306a36Sopenharmony_ciirrelevant. 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ciLocking 15762306a36Sopenharmony_ci******* 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ciIn May, 2006, the "Devicescape" networking stack was, with great 16062306a36Sopenharmony_cifanfare, released under the GPL and made available for inclusion in the 16162306a36Sopenharmony_cimainline kernel. This donation was welcome news; support for wireless 16262306a36Sopenharmony_cinetworking in Linux was considered substandard at best, and the Devicescape 16362306a36Sopenharmony_cistack offered the promise of fixing that situation. Yet, this code did not 16462306a36Sopenharmony_ciactually make it into the mainline until June, 2007 (2.6.22). What 16562306a36Sopenharmony_cihappened? 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ciThis code showed a number of signs of having been developed behind 16862306a36Sopenharmony_cicorporate doors. But one large problem in particular was that it was not 16962306a36Sopenharmony_cidesigned to work on multiprocessor systems. Before this networking stack 17062306a36Sopenharmony_ci(now called mac80211) could be merged, a locking scheme needed to be 17162306a36Sopenharmony_ciretrofitted onto it. 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ciOnce upon a time, Linux kernel code could be developed without thinking 17462306a36Sopenharmony_ciabout the concurrency issues presented by multiprocessor systems. Now, 17562306a36Sopenharmony_cihowever, this document is being written on a dual-core laptop. Even on 17662306a36Sopenharmony_cisingle-processor systems, work being done to improve responsiveness will 17762306a36Sopenharmony_ciraise the level of concurrency within the kernel. The days when kernel 17862306a36Sopenharmony_cicode could be written without thinking about locking are long past. 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ciAny resource (data structures, hardware registers, etc.) which could be 18162306a36Sopenharmony_ciaccessed concurrently by more than one thread must be protected by a lock. 18262306a36Sopenharmony_ciNew code should be written with this requirement in mind; retrofitting 18362306a36Sopenharmony_cilocking after the fact is a rather more difficult task. Kernel developers 18462306a36Sopenharmony_cishould take the time to understand the available locking primitives well 18562306a36Sopenharmony_cienough to pick the right tool for the job. Code which shows a lack of 18662306a36Sopenharmony_ciattention to concurrency will have a difficult path into the mainline. 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ciRegressions 19062306a36Sopenharmony_ci*********** 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ciOne final hazard worth mentioning is this: it can be tempting to make a 19362306a36Sopenharmony_cichange (which may bring big improvements) which causes something to break 19462306a36Sopenharmony_cifor existing users. This kind of change is called a "regression," and 19562306a36Sopenharmony_ciregressions have become most unwelcome in the mainline kernel. With few 19662306a36Sopenharmony_ciexceptions, changes which cause regressions will be backed out if the 19762306a36Sopenharmony_ciregression cannot be fixed in a timely manner. Far better to avoid the 19862306a36Sopenharmony_ciregression in the first place. 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ciIt is often argued that a regression can be justified if it causes things 20162306a36Sopenharmony_cito work for more people than it creates problems for. Why not make a 20262306a36Sopenharmony_cichange if it brings new functionality to ten systems for each one it 20362306a36Sopenharmony_cibreaks? The best answer to this question was expressed by Linus in July, 20462306a36Sopenharmony_ci2007: 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci:: 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci So we don't fix bugs by introducing new problems. That way lies 20962306a36Sopenharmony_ci madness, and nobody ever knows if you actually make any real 21062306a36Sopenharmony_ci progress at all. Is it two steps forwards, one step back, or one 21162306a36Sopenharmony_ci step forward and two steps back? 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci(https://lwn.net/Articles/243460/). 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ciAn especially unwelcome type of regression is any sort of change to the 21662306a36Sopenharmony_ciuser-space ABI. Once an interface has been exported to user space, it must 21762306a36Sopenharmony_cibe supported indefinitely. This fact makes the creation of user-space 21862306a36Sopenharmony_ciinterfaces particularly challenging: since they cannot be changed in 21962306a36Sopenharmony_ciincompatible ways, they must be done right the first time. For this 22062306a36Sopenharmony_cireason, a great deal of thought, clear documentation, and wide review for 22162306a36Sopenharmony_ciuser-space interfaces is always required. 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ciCode checking tools 22562306a36Sopenharmony_ci------------------- 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ciFor now, at least, the writing of error-free code remains an ideal that few 22862306a36Sopenharmony_ciof us can reach. What we can hope to do, though, is to catch and fix as 22962306a36Sopenharmony_cimany of those errors as possible before our code goes into the mainline 23062306a36Sopenharmony_cikernel. To that end, the kernel developers have put together an impressive 23162306a36Sopenharmony_ciarray of tools which can catch a wide variety of obscure problems in an 23262306a36Sopenharmony_ciautomated way. Any problem caught by the computer is a problem which will 23362306a36Sopenharmony_cinot afflict a user later on, so it stands to reason that the automated 23462306a36Sopenharmony_citools should be used whenever possible. 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ciThe first step is simply to heed the warnings produced by the compiler. 23762306a36Sopenharmony_ciContemporary versions of gcc can detect (and warn about) a large number of 23862306a36Sopenharmony_cipotential errors. Quite often, these warnings point to real problems. 23962306a36Sopenharmony_ciCode submitted for review should, as a rule, not produce any compiler 24062306a36Sopenharmony_ciwarnings. When silencing warnings, take care to understand the real cause 24162306a36Sopenharmony_ciand try to avoid "fixes" which make the warning go away without addressing 24262306a36Sopenharmony_ciits cause. 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ciNote that not all compiler warnings are enabled by default. Build the 24562306a36Sopenharmony_cikernel with "make KCFLAGS=-W" to get the full set. 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ciThe kernel provides several configuration options which turn on debugging 24862306a36Sopenharmony_cifeatures; most of these are found in the "kernel hacking" submenu. Several 24962306a36Sopenharmony_ciof these options should be turned on for any kernel used for development or 25062306a36Sopenharmony_citesting purposes. In particular, you should turn on: 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci - FRAME_WARN to get warnings for stack frames larger than a given amount. 25362306a36Sopenharmony_ci The output generated can be verbose, but one need not worry about 25462306a36Sopenharmony_ci warnings from other parts of the kernel. 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci - DEBUG_OBJECTS will add code to track the lifetime of various objects 25762306a36Sopenharmony_ci created by the kernel and warn when things are done out of order. If 25862306a36Sopenharmony_ci you are adding a subsystem which creates (and exports) complex objects 25962306a36Sopenharmony_ci of its own, consider adding support for the object debugging 26062306a36Sopenharmony_ci infrastructure. 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci - DEBUG_SLAB can find a variety of memory allocation and use errors; it 26362306a36Sopenharmony_ci should be used on most development kernels. 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci - DEBUG_SPINLOCK, DEBUG_ATOMIC_SLEEP, and DEBUG_MUTEXES will find a 26662306a36Sopenharmony_ci number of common locking errors. 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ciThere are quite a few other debugging options, some of which will be 26962306a36Sopenharmony_cidiscussed below. Some of them have a significant performance impact and 27062306a36Sopenharmony_cishould not be used all of the time. But some time spent learning the 27162306a36Sopenharmony_ciavailable options will likely be paid back many times over in short order. 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ciOne of the heavier debugging tools is the locking checker, or "lockdep." 27462306a36Sopenharmony_ciThis tool will track the acquisition and release of every lock (spinlock or 27562306a36Sopenharmony_cimutex) in the system, the order in which locks are acquired relative to 27662306a36Sopenharmony_cieach other, the current interrupt environment, and more. It can then 27762306a36Sopenharmony_ciensure that locks are always acquired in the same order, that the same 27862306a36Sopenharmony_ciinterrupt assumptions apply in all situations, and so on. In other words, 27962306a36Sopenharmony_cilockdep can find a number of scenarios in which the system could, on rare 28062306a36Sopenharmony_cioccasion, deadlock. This kind of problem can be painful (for both 28162306a36Sopenharmony_cidevelopers and users) in a deployed system; lockdep allows them to be found 28262306a36Sopenharmony_ciin an automated manner ahead of time. Code with any sort of non-trivial 28362306a36Sopenharmony_cilocking should be run with lockdep enabled before being submitted for 28462306a36Sopenharmony_ciinclusion. 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ciAs a diligent kernel programmer, you will, beyond doubt, check the return 28762306a36Sopenharmony_cistatus of any operation (such as a memory allocation) which can fail. The 28862306a36Sopenharmony_cifact of the matter, though, is that the resulting failure recovery paths 28962306a36Sopenharmony_ciare, probably, completely untested. Untested code tends to be broken code; 29062306a36Sopenharmony_ciyou could be much more confident of your code if all those error-handling 29162306a36Sopenharmony_cipaths had been exercised a few times. 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ciThe kernel provides a fault injection framework which can do exactly that, 29462306a36Sopenharmony_ciespecially where memory allocations are involved. With fault injection 29562306a36Sopenharmony_cienabled, a configurable percentage of memory allocations will be made to 29662306a36Sopenharmony_cifail; these failures can be restricted to a specific range of code. 29762306a36Sopenharmony_ciRunning with fault injection enabled allows the programmer to see how the 29862306a36Sopenharmony_cicode responds when things go badly. See 29962306a36Sopenharmony_ciDocumentation/fault-injection/fault-injection.rst for more information on 30062306a36Sopenharmony_cihow to use this facility. 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ciOther kinds of errors can be found with the "sparse" static analysis tool. 30362306a36Sopenharmony_ciWith sparse, the programmer can be warned about confusion between 30462306a36Sopenharmony_ciuser-space and kernel-space addresses, mixture of big-endian and 30562306a36Sopenharmony_cismall-endian quantities, the passing of integer values where a set of bit 30662306a36Sopenharmony_ciflags is expected, and so on. Sparse must be installed separately (it can 30762306a36Sopenharmony_cibe found at https://sparse.wiki.kernel.org/index.php/Main_Page if your 30862306a36Sopenharmony_cidistributor does not package it); it can then be run on the code by adding 30962306a36Sopenharmony_ci"C=1" to your make command. 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ciThe "Coccinelle" tool (http://coccinelle.lip6.fr/) is able to find a wide 31262306a36Sopenharmony_civariety of potential coding problems; it can also propose fixes for those 31362306a36Sopenharmony_ciproblems. Quite a few "semantic patches" for the kernel have been packaged 31462306a36Sopenharmony_ciunder the scripts/coccinelle directory; running "make coccicheck" will run 31562306a36Sopenharmony_cithrough those semantic patches and report on any problems found. See 31662306a36Sopenharmony_ci:ref:`Documentation/dev-tools/coccinelle.rst <devtools_coccinelle>` 31762306a36Sopenharmony_cifor more information. 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ciOther kinds of portability errors are best found by compiling your code for 32062306a36Sopenharmony_ciother architectures. If you do not happen to have an S/390 system or a 32162306a36Sopenharmony_ciBlackfin development board handy, you can still perform the compilation 32262306a36Sopenharmony_cistep. A large set of cross compilers for x86 systems can be found at 32362306a36Sopenharmony_ci 32462306a36Sopenharmony_ci https://www.kernel.org/pub/tools/crosstool/ 32562306a36Sopenharmony_ci 32662306a36Sopenharmony_ciSome time spent installing and using these compilers will help avoid 32762306a36Sopenharmony_ciembarrassment later. 32862306a36Sopenharmony_ci 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ciDocumentation 33162306a36Sopenharmony_ci------------- 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ciDocumentation has often been more the exception than the rule with kernel 33462306a36Sopenharmony_cidevelopment. Even so, adequate documentation will help to ease the merging 33562306a36Sopenharmony_ciof new code into the kernel, make life easier for other developers, and 33662306a36Sopenharmony_ciwill be helpful for your users. In many cases, the addition of 33762306a36Sopenharmony_cidocumentation has become essentially mandatory. 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ciThe first piece of documentation for any patch is its associated 34062306a36Sopenharmony_cichangelog. Log entries should describe the problem being solved, the form 34162306a36Sopenharmony_ciof the solution, the people who worked on the patch, any relevant 34262306a36Sopenharmony_cieffects on performance, and anything else that might be needed to 34362306a36Sopenharmony_ciunderstand the patch. Be sure that the changelog says *why* the patch is 34462306a36Sopenharmony_ciworth applying; a surprising number of developers fail to provide that 34562306a36Sopenharmony_ciinformation. 34662306a36Sopenharmony_ci 34762306a36Sopenharmony_ciAny code which adds a new user-space interface - including new sysfs or 34862306a36Sopenharmony_ci/proc files - should include documentation of that interface which enables 34962306a36Sopenharmony_ciuser-space developers to know what they are working with. See 35062306a36Sopenharmony_ciDocumentation/ABI/README for a description of how this documentation should 35162306a36Sopenharmony_cibe formatted and what information needs to be provided. 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ciThe file :ref:`Documentation/admin-guide/kernel-parameters.rst 35462306a36Sopenharmony_ci<kernelparameters>` describes all of the kernel's boot-time parameters. 35562306a36Sopenharmony_ciAny patch which adds new parameters should add the appropriate entries to 35662306a36Sopenharmony_cithis file. 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ciAny new configuration options must be accompanied by help text which 35962306a36Sopenharmony_ciclearly explains the options and when the user might want to select them. 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_ciInternal API information for many subsystems is documented by way of 36262306a36Sopenharmony_cispecially-formatted comments; these comments can be extracted and formatted 36362306a36Sopenharmony_ciin a number of ways by the "kernel-doc" script. If you are working within 36462306a36Sopenharmony_cia subsystem which has kerneldoc comments, you should maintain them and add 36562306a36Sopenharmony_cithem, as appropriate, for externally-available functions. Even in areas 36662306a36Sopenharmony_ciwhich have not been so documented, there is no harm in adding kerneldoc 36762306a36Sopenharmony_cicomments for the future; indeed, this can be a useful activity for 36862306a36Sopenharmony_cibeginning kernel developers. The format of these comments, along with some 36962306a36Sopenharmony_ciinformation on how to create kerneldoc templates can be found at 37062306a36Sopenharmony_ci:ref:`Documentation/doc-guide/ <doc_guide>`. 37162306a36Sopenharmony_ci 37262306a36Sopenharmony_ciAnybody who reads through a significant amount of existing kernel code will 37362306a36Sopenharmony_cinote that, often, comments are most notable by their absence. Once again, 37462306a36Sopenharmony_cithe expectations for new code are higher than they were in the past; 37562306a36Sopenharmony_cimerging uncommented code will be harder. That said, there is little desire 37662306a36Sopenharmony_cifor verbosely-commented code. The code should, itself, be readable, with 37762306a36Sopenharmony_cicomments explaining the more subtle aspects. 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ciCertain things should always be commented. Uses of memory barriers should 38062306a36Sopenharmony_cibe accompanied by a line explaining why the barrier is necessary. The 38162306a36Sopenharmony_cilocking rules for data structures generally need to be explained somewhere. 38262306a36Sopenharmony_ciMajor data structures need comprehensive documentation in general. 38362306a36Sopenharmony_ciNon-obvious dependencies between separate bits of code should be pointed 38462306a36Sopenharmony_ciout. Anything which might tempt a code janitor to make an incorrect 38562306a36Sopenharmony_ci"cleanup" needs a comment saying why it is done the way it is. And so on. 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci 38862306a36Sopenharmony_ciInternal API changes 38962306a36Sopenharmony_ci-------------------- 39062306a36Sopenharmony_ci 39162306a36Sopenharmony_ciThe binary interface provided by the kernel to user space cannot be broken 39262306a36Sopenharmony_ciexcept under the most severe circumstances. The kernel's internal 39362306a36Sopenharmony_ciprogramming interfaces, instead, are highly fluid and can be changed when 39462306a36Sopenharmony_cithe need arises. If you find yourself having to work around a kernel API, 39562306a36Sopenharmony_cior simply not using a specific functionality because it does not meet your 39662306a36Sopenharmony_cineeds, that may be a sign that the API needs to change. As a kernel 39762306a36Sopenharmony_cideveloper, you are empowered to make such changes. 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ciThere are, of course, some catches. API changes can be made, but they need 40062306a36Sopenharmony_cito be well justified. So any patch making an internal API change should be 40162306a36Sopenharmony_ciaccompanied by a description of what the change is and why it is 40262306a36Sopenharmony_cinecessary. This kind of change should also be broken out into a separate 40362306a36Sopenharmony_cipatch, rather than buried within a larger patch. 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ciThe other catch is that a developer who changes an internal API is 40662306a36Sopenharmony_cigenerally charged with the task of fixing any code within the kernel tree 40762306a36Sopenharmony_ciwhich is broken by the change. For a widely-used function, this duty can 40862306a36Sopenharmony_cilead to literally hundreds or thousands of changes - many of which are 40962306a36Sopenharmony_cilikely to conflict with work being done by other developers. Needless to 41062306a36Sopenharmony_cisay, this can be a large job, so it is best to be sure that the 41162306a36Sopenharmony_cijustification is solid. Note that the Coccinelle tool can help with 41262306a36Sopenharmony_ciwide-ranging API changes. 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_ciWhen making an incompatible API change, one should, whenever possible, 41562306a36Sopenharmony_ciensure that code which has not been updated is caught by the compiler. 41662306a36Sopenharmony_ciThis will help you to be sure that you have found all in-tree uses of that 41762306a36Sopenharmony_ciinterface. It will also alert developers of out-of-tree code that there is 41862306a36Sopenharmony_cia change that they need to respond to. Supporting out-of-tree code is not 41962306a36Sopenharmony_cisomething that kernel developers need to be worried about, but we also do 42062306a36Sopenharmony_cinot have to make life harder for out-of-tree developers than it needs to 42162306a36Sopenharmony_cibe. 422