10e4f07a6SMauro Carvalho Chehab.. _development_coding: 20e4f07a6SMauro Carvalho Chehab 30e4f07a6SMauro Carvalho ChehabGetting the code right 40e4f07a6SMauro Carvalho Chehab====================== 50e4f07a6SMauro Carvalho Chehab 60e4f07a6SMauro Carvalho ChehabWhile there is much to be said for a solid and community-oriented design 70e4f07a6SMauro Carvalho Chehabprocess, the proof of any kernel development project is in the resulting 80e4f07a6SMauro Carvalho Chehabcode. It is the code which will be examined by other developers and merged 90e4f07a6SMauro Carvalho Chehab(or not) into the mainline tree. So it is the quality of this code which 100e4f07a6SMauro Carvalho Chehabwill determine the ultimate success of the project. 110e4f07a6SMauro Carvalho Chehab 120e4f07a6SMauro Carvalho ChehabThis section will examine the coding process. We'll start with a look at a 130e4f07a6SMauro Carvalho Chehabnumber of ways in which kernel developers can go wrong. Then the focus 140e4f07a6SMauro Carvalho Chehabwill shift toward doing things right and the tools which can help in that 150e4f07a6SMauro Carvalho Chehabquest. 160e4f07a6SMauro Carvalho Chehab 170e4f07a6SMauro Carvalho Chehab 180e4f07a6SMauro Carvalho ChehabPitfalls 190e4f07a6SMauro Carvalho Chehab--------- 200e4f07a6SMauro Carvalho Chehab 210e4f07a6SMauro Carvalho ChehabCoding style 220e4f07a6SMauro Carvalho Chehab************ 230e4f07a6SMauro Carvalho Chehab 240e4f07a6SMauro Carvalho ChehabThe kernel has long had a standard coding style, described in 259b9355a2SAndrew Clayton:ref:`Documentation/process/coding-style.rst <codingstyle>`. For much of 269b9355a2SAndrew Claytonthat time, the policies described in that file were taken as being, at most, 279b9355a2SAndrew Claytonadvisory. As a result, there is a substantial amount of code in the kernel 289b9355a2SAndrew Claytonwhich does not meet the coding style guidelines. The presence of that code 299b9355a2SAndrew Claytonleads to two independent hazards for kernel developers. 300e4f07a6SMauro Carvalho Chehab 310e4f07a6SMauro Carvalho ChehabThe first of these is to believe that the kernel coding standards do not 320e4f07a6SMauro Carvalho Chehabmatter and are not enforced. The truth of the matter is that adding new 330e4f07a6SMauro Carvalho Chehabcode to the kernel is very difficult if that code is not coded according to 340e4f07a6SMauro Carvalho Chehabthe standard; many developers will request that the code be reformatted 350e4f07a6SMauro Carvalho Chehabbefore they will even review it. A code base as large as the kernel 360e4f07a6SMauro Carvalho Chehabrequires some uniformity of code to make it possible for developers to 370e4f07a6SMauro Carvalho Chehabquickly understand any part of it. So there is no longer room for 380e4f07a6SMauro Carvalho Chehabstrangely-formatted code. 390e4f07a6SMauro Carvalho Chehab 400e4f07a6SMauro Carvalho ChehabOccasionally, the kernel's coding style will run into conflict with an 410e4f07a6SMauro Carvalho Chehabemployer's mandated style. In such cases, the kernel's style will have to 420e4f07a6SMauro Carvalho Chehabwin before the code can be merged. Putting code into the kernel means 430e4f07a6SMauro Carvalho Chehabgiving up a degree of control in a number of ways - including control over 440e4f07a6SMauro Carvalho Chehabhow the code is formatted. 450e4f07a6SMauro Carvalho Chehab 460e4f07a6SMauro Carvalho ChehabThe other trap is to assume that code which is already in the kernel is 470e4f07a6SMauro Carvalho Chehaburgently in need of coding style fixes. Developers may start to generate 480e4f07a6SMauro Carvalho Chehabreformatting patches as a way of gaining familiarity with the process, or 490e4f07a6SMauro Carvalho Chehabas a way of getting their name into the kernel changelogs - or both. But 500e4f07a6SMauro Carvalho Chehabpure coding style fixes are seen as noise by the development community; 510e4f07a6SMauro Carvalho Chehabthey tend to get a chilly reception. So this type of patch is best 520e4f07a6SMauro Carvalho Chehabavoided. It is natural to fix the style of a piece of code while working 530e4f07a6SMauro Carvalho Chehabon it for other reasons, but coding style changes should not be made for 540e4f07a6SMauro Carvalho Chehabtheir own sake. 550e4f07a6SMauro Carvalho Chehab 560e4f07a6SMauro Carvalho ChehabThe coding style document also should not be read as an absolute law which 570e4f07a6SMauro Carvalho Chehabcan never be transgressed. If there is a good reason to go against the 580e4f07a6SMauro Carvalho Chehabstyle (a line which becomes far less readable if split to fit within the 590e4f07a6SMauro Carvalho Chehab80-column limit, for example), just do it. 600e4f07a6SMauro Carvalho Chehab 61d4ef8d3fSMiguel OjedaNote that you can also use the ``clang-format`` tool to help you with 62d4ef8d3fSMiguel Ojedathese rules, to quickly re-format parts of your code automatically, 63d4ef8d3fSMiguel Ojedaand to review full files in order to spot coding style mistakes, 64d4ef8d3fSMiguel Ojedatypos and possible improvements. It is also handy for sorting ``#includes``, 65d4ef8d3fSMiguel Ojedafor aligning variables/macros, for reflowing text and other similar tasks. 66d4ef8d3fSMiguel OjedaSee the file :ref:`Documentation/process/clang-format.rst <clangformat>` 67d4ef8d3fSMiguel Ojedafor more details. 68d4ef8d3fSMiguel Ojeda 690e4f07a6SMauro Carvalho Chehab 700e4f07a6SMauro Carvalho ChehabAbstraction layers 710e4f07a6SMauro Carvalho Chehab****************** 720e4f07a6SMauro Carvalho Chehab 730e4f07a6SMauro Carvalho ChehabComputer Science professors teach students to make extensive use of 740e4f07a6SMauro Carvalho Chehababstraction layers in the name of flexibility and information hiding. 750e4f07a6SMauro Carvalho ChehabCertainly the kernel makes extensive use of abstraction; no project 760e4f07a6SMauro Carvalho Chehabinvolving several million lines of code could do otherwise and survive. 770e4f07a6SMauro Carvalho ChehabBut experience has shown that excessive or premature abstraction can be 780e4f07a6SMauro Carvalho Chehabjust as harmful as premature optimization. Abstraction should be used to 790e4f07a6SMauro Carvalho Chehabthe level required and no further. 800e4f07a6SMauro Carvalho Chehab 810e4f07a6SMauro Carvalho ChehabAt a simple level, consider a function which has an argument which is 820e4f07a6SMauro Carvalho Chehabalways passed as zero by all callers. One could retain that argument just 830e4f07a6SMauro Carvalho Chehabin case somebody eventually needs to use the extra flexibility that it 840e4f07a6SMauro Carvalho Chehabprovides. By that time, though, chances are good that the code which 850e4f07a6SMauro Carvalho Chehabimplements this extra argument has been broken in some subtle way which was 860e4f07a6SMauro Carvalho Chehabnever noticed - because it has never been used. Or, when the need for 870e4f07a6SMauro Carvalho Chehabextra flexibility arises, it does not do so in a way which matches the 880e4f07a6SMauro Carvalho Chehabprogrammer's early expectation. Kernel developers will routinely submit 890e4f07a6SMauro Carvalho Chehabpatches to remove unused arguments; they should, in general, not be added 900e4f07a6SMauro Carvalho Chehabin the first place. 910e4f07a6SMauro Carvalho Chehab 920e4f07a6SMauro Carvalho ChehabAbstraction layers which hide access to hardware - often to allow the bulk 930e4f07a6SMauro Carvalho Chehabof a driver to be used with multiple operating systems - are especially 940e4f07a6SMauro Carvalho Chehabfrowned upon. Such layers obscure the code and may impose a performance 950e4f07a6SMauro Carvalho Chehabpenalty; they do not belong in the Linux kernel. 960e4f07a6SMauro Carvalho Chehab 970e4f07a6SMauro Carvalho ChehabOn the other hand, if you find yourself copying significant amounts of code 980e4f07a6SMauro Carvalho Chehabfrom another kernel subsystem, it is time to ask whether it would, in fact, 990e4f07a6SMauro Carvalho Chehabmake sense to pull out some of that code into a separate library or to 1000e4f07a6SMauro Carvalho Chehabimplement that functionality at a higher level. There is no value in 1010e4f07a6SMauro Carvalho Chehabreplicating the same code throughout the kernel. 1020e4f07a6SMauro Carvalho Chehab 1030e4f07a6SMauro Carvalho Chehab 1040e4f07a6SMauro Carvalho Chehab#ifdef and preprocessor use in general 1050e4f07a6SMauro Carvalho Chehab************************************** 1060e4f07a6SMauro Carvalho Chehab 1070e4f07a6SMauro Carvalho ChehabThe C preprocessor seems to present a powerful temptation to some C 1080e4f07a6SMauro Carvalho Chehabprogrammers, who see it as a way to efficiently encode a great deal of 1090e4f07a6SMauro Carvalho Chehabflexibility into a source file. But the preprocessor is not C, and heavy 1100e4f07a6SMauro Carvalho Chehabuse of it results in code which is much harder for others to read and 1110e4f07a6SMauro Carvalho Chehabharder for the compiler to check for correctness. Heavy preprocessor use 1120e4f07a6SMauro Carvalho Chehabis almost always a sign of code which needs some cleanup work. 1130e4f07a6SMauro Carvalho Chehab 1140e4f07a6SMauro Carvalho ChehabConditional compilation with #ifdef is, indeed, a powerful feature, and it 1150e4f07a6SMauro Carvalho Chehabis used within the kernel. But there is little desire to see code which is 1160e4f07a6SMauro Carvalho Chehabsprinkled liberally with #ifdef blocks. As a general rule, #ifdef use 1170e4f07a6SMauro Carvalho Chehabshould be confined to header files whenever possible. 1180e4f07a6SMauro Carvalho ChehabConditionally-compiled code can be confined to functions which, if the code 1190e4f07a6SMauro Carvalho Chehabis not to be present, simply become empty. The compiler will then quietly 1200e4f07a6SMauro Carvalho Chehaboptimize out the call to the empty function. The result is far cleaner 1210e4f07a6SMauro Carvalho Chehabcode which is easier to follow. 1220e4f07a6SMauro Carvalho Chehab 1230e4f07a6SMauro Carvalho ChehabC preprocessor macros present a number of hazards, including possible 1240e4f07a6SMauro Carvalho Chehabmultiple evaluation of expressions with side effects and no type safety. 1250e4f07a6SMauro Carvalho ChehabIf you are tempted to define a macro, consider creating an inline function 1260e4f07a6SMauro Carvalho Chehabinstead. The code which results will be the same, but inline functions are 1270e4f07a6SMauro Carvalho Chehabeasier to read, do not evaluate their arguments multiple times, and allow 1280e4f07a6SMauro Carvalho Chehabthe compiler to perform type checking on the arguments and return value. 1290e4f07a6SMauro Carvalho Chehab 1300e4f07a6SMauro Carvalho Chehab 1310e4f07a6SMauro Carvalho ChehabInline functions 1320e4f07a6SMauro Carvalho Chehab**************** 1330e4f07a6SMauro Carvalho Chehab 1340e4f07a6SMauro Carvalho ChehabInline functions present a hazard of their own, though. Programmers can 1350e4f07a6SMauro Carvalho Chehabbecome enamored of the perceived efficiency inherent in avoiding a function 1360e4f07a6SMauro Carvalho Chehabcall and fill a source file with inline functions. Those functions, 1370e4f07a6SMauro Carvalho Chehabhowever, can actually reduce performance. Since their code is replicated 1380e4f07a6SMauro Carvalho Chehabat each call site, they end up bloating the size of the compiled kernel. 1390e4f07a6SMauro Carvalho ChehabThat, in turn, creates pressure on the processor's memory caches, which can 1400e4f07a6SMauro Carvalho Chehabslow execution dramatically. Inline functions, as a rule, should be quite 1410e4f07a6SMauro Carvalho Chehabsmall and relatively rare. The cost of a function call, after all, is not 1420e4f07a6SMauro Carvalho Chehabthat high; the creation of large numbers of inline functions is a classic 1430e4f07a6SMauro Carvalho Chehabexample of premature optimization. 1440e4f07a6SMauro Carvalho Chehab 1450e4f07a6SMauro Carvalho ChehabIn general, kernel programmers ignore cache effects at their peril. The 1460e4f07a6SMauro Carvalho Chehabclassic time/space tradeoff taught in beginning data structures classes 1470e4f07a6SMauro Carvalho Chehaboften does not apply to contemporary hardware. Space *is* time, in that a 1480e4f07a6SMauro Carvalho Chehablarger program will run slower than one which is more compact. 1490e4f07a6SMauro Carvalho Chehab 1500e4f07a6SMauro Carvalho ChehabMore recent compilers take an increasingly active role in deciding whether 1510e4f07a6SMauro Carvalho Chehaba given function should actually be inlined or not. So the liberal 1520e4f07a6SMauro Carvalho Chehabplacement of "inline" keywords may not just be excessive; it could also be 1530e4f07a6SMauro Carvalho Chehabirrelevant. 1540e4f07a6SMauro Carvalho Chehab 1550e4f07a6SMauro Carvalho Chehab 1560e4f07a6SMauro Carvalho ChehabLocking 1570e4f07a6SMauro Carvalho Chehab******* 1580e4f07a6SMauro Carvalho Chehab 1590e4f07a6SMauro Carvalho ChehabIn May, 2006, the "Devicescape" networking stack was, with great 1600e4f07a6SMauro Carvalho Chehabfanfare, released under the GPL and made available for inclusion in the 1610e4f07a6SMauro Carvalho Chehabmainline kernel. This donation was welcome news; support for wireless 1620e4f07a6SMauro Carvalho Chehabnetworking in Linux was considered substandard at best, and the Devicescape 1630e4f07a6SMauro Carvalho Chehabstack offered the promise of fixing that situation. Yet, this code did not 1640e4f07a6SMauro Carvalho Chehabactually make it into the mainline until June, 2007 (2.6.22). What 1650e4f07a6SMauro Carvalho Chehabhappened? 1660e4f07a6SMauro Carvalho Chehab 1670e4f07a6SMauro Carvalho ChehabThis code showed a number of signs of having been developed behind 1680e4f07a6SMauro Carvalho Chehabcorporate doors. But one large problem in particular was that it was not 1690e4f07a6SMauro Carvalho Chehabdesigned to work on multiprocessor systems. Before this networking stack 1700e4f07a6SMauro Carvalho Chehab(now called mac80211) could be merged, a locking scheme needed to be 1710e4f07a6SMauro Carvalho Chehabretrofitted onto it. 1720e4f07a6SMauro Carvalho Chehab 1730e4f07a6SMauro Carvalho ChehabOnce upon a time, Linux kernel code could be developed without thinking 1740e4f07a6SMauro Carvalho Chehababout the concurrency issues presented by multiprocessor systems. Now, 1750e4f07a6SMauro Carvalho Chehabhowever, this document is being written on a dual-core laptop. Even on 1760e4f07a6SMauro Carvalho Chehabsingle-processor systems, work being done to improve responsiveness will 1770e4f07a6SMauro Carvalho Chehabraise the level of concurrency within the kernel. The days when kernel 1780e4f07a6SMauro Carvalho Chehabcode could be written without thinking about locking are long past. 1790e4f07a6SMauro Carvalho Chehab 1800e4f07a6SMauro Carvalho ChehabAny resource (data structures, hardware registers, etc.) which could be 1810e4f07a6SMauro Carvalho Chehabaccessed concurrently by more than one thread must be protected by a lock. 1820e4f07a6SMauro Carvalho ChehabNew code should be written with this requirement in mind; retrofitting 1830e4f07a6SMauro Carvalho Chehablocking after the fact is a rather more difficult task. Kernel developers 1840e4f07a6SMauro Carvalho Chehabshould take the time to understand the available locking primitives well 1850e4f07a6SMauro Carvalho Chehabenough to pick the right tool for the job. Code which shows a lack of 1860e4f07a6SMauro Carvalho Chehabattention to concurrency will have a difficult path into the mainline. 1870e4f07a6SMauro Carvalho Chehab 1880e4f07a6SMauro Carvalho Chehab 1890e4f07a6SMauro Carvalho ChehabRegressions 1900e4f07a6SMauro Carvalho Chehab*********** 1910e4f07a6SMauro Carvalho Chehab 1920e4f07a6SMauro Carvalho ChehabOne final hazard worth mentioning is this: it can be tempting to make a 1930e4f07a6SMauro Carvalho Chehabchange (which may bring big improvements) which causes something to break 1940e4f07a6SMauro Carvalho Chehabfor existing users. This kind of change is called a "regression," and 1950e4f07a6SMauro Carvalho Chehabregressions have become most unwelcome in the mainline kernel. With few 1960e4f07a6SMauro Carvalho Chehabexceptions, changes which cause regressions will be backed out if the 1970e4f07a6SMauro Carvalho Chehabregression cannot be fixed in a timely manner. Far better to avoid the 1980e4f07a6SMauro Carvalho Chehabregression in the first place. 1990e4f07a6SMauro Carvalho Chehab 2000e4f07a6SMauro Carvalho ChehabIt is often argued that a regression can be justified if it causes things 2010e4f07a6SMauro Carvalho Chehabto work for more people than it creates problems for. Why not make a 2020e4f07a6SMauro Carvalho Chehabchange if it brings new functionality to ten systems for each one it 2030e4f07a6SMauro Carvalho Chehabbreaks? The best answer to this question was expressed by Linus in July, 2040e4f07a6SMauro Carvalho Chehab2007: 2050e4f07a6SMauro Carvalho Chehab 2060e4f07a6SMauro Carvalho Chehab:: 2070e4f07a6SMauro Carvalho Chehab 2080e4f07a6SMauro Carvalho Chehab So we don't fix bugs by introducing new problems. That way lies 2090e4f07a6SMauro Carvalho Chehab madness, and nobody ever knows if you actually make any real 2100e4f07a6SMauro Carvalho Chehab progress at all. Is it two steps forwards, one step back, or one 2110e4f07a6SMauro Carvalho Chehab step forward and two steps back? 2120e4f07a6SMauro Carvalho Chehab 213e7b4311eSAlexander A. Klimov(https://lwn.net/Articles/243460/). 2140e4f07a6SMauro Carvalho Chehab 2150e4f07a6SMauro Carvalho ChehabAn especially unwelcome type of regression is any sort of change to the 2160e4f07a6SMauro Carvalho Chehabuser-space ABI. Once an interface has been exported to user space, it must 2170e4f07a6SMauro Carvalho Chehabbe supported indefinitely. This fact makes the creation of user-space 2180e4f07a6SMauro Carvalho Chehabinterfaces particularly challenging: since they cannot be changed in 2190e4f07a6SMauro Carvalho Chehabincompatible ways, they must be done right the first time. For this 2200e4f07a6SMauro Carvalho Chehabreason, a great deal of thought, clear documentation, and wide review for 2210e4f07a6SMauro Carvalho Chehabuser-space interfaces is always required. 2220e4f07a6SMauro Carvalho Chehab 2230e4f07a6SMauro Carvalho Chehab 2240e4f07a6SMauro Carvalho ChehabCode checking tools 2250e4f07a6SMauro Carvalho Chehab------------------- 2260e4f07a6SMauro Carvalho Chehab 2270e4f07a6SMauro Carvalho ChehabFor now, at least, the writing of error-free code remains an ideal that few 2280e4f07a6SMauro Carvalho Chehabof us can reach. What we can hope to do, though, is to catch and fix as 2290e4f07a6SMauro Carvalho Chehabmany of those errors as possible before our code goes into the mainline 2300e4f07a6SMauro Carvalho Chehabkernel. To that end, the kernel developers have put together an impressive 2310e4f07a6SMauro Carvalho Chehabarray of tools which can catch a wide variety of obscure problems in an 2320e4f07a6SMauro Carvalho Chehabautomated way. Any problem caught by the computer is a problem which will 2330e4f07a6SMauro Carvalho Chehabnot afflict a user later on, so it stands to reason that the automated 2340e4f07a6SMauro Carvalho Chehabtools should be used whenever possible. 2350e4f07a6SMauro Carvalho Chehab 2360e4f07a6SMauro Carvalho ChehabThe first step is simply to heed the warnings produced by the compiler. 2370e4f07a6SMauro Carvalho ChehabContemporary versions of gcc can detect (and warn about) a large number of 2380e4f07a6SMauro Carvalho Chehabpotential errors. Quite often, these warnings point to real problems. 2390e4f07a6SMauro Carvalho ChehabCode submitted for review should, as a rule, not produce any compiler 2400e4f07a6SMauro Carvalho Chehabwarnings. When silencing warnings, take care to understand the real cause 2410e4f07a6SMauro Carvalho Chehaband try to avoid "fixes" which make the warning go away without addressing 2420e4f07a6SMauro Carvalho Chehabits cause. 2430e4f07a6SMauro Carvalho Chehab 2440e4f07a6SMauro Carvalho ChehabNote that not all compiler warnings are enabled by default. Build the 245*163ba35fSMasahiro Yamadakernel with "make KCFLAGS=-W" to get the full set. 2460e4f07a6SMauro Carvalho Chehab 2470e4f07a6SMauro Carvalho ChehabThe kernel provides several configuration options which turn on debugging 2480e4f07a6SMauro Carvalho Chehabfeatures; most of these are found in the "kernel hacking" submenu. Several 2490e4f07a6SMauro Carvalho Chehabof these options should be turned on for any kernel used for development or 2500e4f07a6SMauro Carvalho Chehabtesting purposes. In particular, you should turn on: 2510e4f07a6SMauro Carvalho Chehab 2520ef597c3SMiguel Ojeda - FRAME_WARN to get warnings for stack frames larger than a given amount. 2530ef597c3SMiguel Ojeda The output generated can be verbose, but one need not worry about 2540e4f07a6SMauro Carvalho Chehab warnings from other parts of the kernel. 2550e4f07a6SMauro Carvalho Chehab 2560e4f07a6SMauro Carvalho Chehab - DEBUG_OBJECTS will add code to track the lifetime of various objects 2570e4f07a6SMauro Carvalho Chehab created by the kernel and warn when things are done out of order. If 2580e4f07a6SMauro Carvalho Chehab you are adding a subsystem which creates (and exports) complex objects 2590e4f07a6SMauro Carvalho Chehab of its own, consider adding support for the object debugging 2600e4f07a6SMauro Carvalho Chehab infrastructure. 2610e4f07a6SMauro Carvalho Chehab 2620e4f07a6SMauro Carvalho Chehab - DEBUG_SLAB can find a variety of memory allocation and use errors; it 2630e4f07a6SMauro Carvalho Chehab should be used on most development kernels. 2640e4f07a6SMauro Carvalho Chehab 2650e4f07a6SMauro Carvalho Chehab - DEBUG_SPINLOCK, DEBUG_ATOMIC_SLEEP, and DEBUG_MUTEXES will find a 2660e4f07a6SMauro Carvalho Chehab number of common locking errors. 2670e4f07a6SMauro Carvalho Chehab 2680e4f07a6SMauro Carvalho ChehabThere are quite a few other debugging options, some of which will be 2690e4f07a6SMauro Carvalho Chehabdiscussed below. Some of them have a significant performance impact and 2700e4f07a6SMauro Carvalho Chehabshould not be used all of the time. But some time spent learning the 2710e4f07a6SMauro Carvalho Chehabavailable options will likely be paid back many times over in short order. 2720e4f07a6SMauro Carvalho Chehab 2730e4f07a6SMauro Carvalho ChehabOne of the heavier debugging tools is the locking checker, or "lockdep." 2740e4f07a6SMauro Carvalho ChehabThis tool will track the acquisition and release of every lock (spinlock or 2750e4f07a6SMauro Carvalho Chehabmutex) in the system, the order in which locks are acquired relative to 2760e4f07a6SMauro Carvalho Chehabeach other, the current interrupt environment, and more. It can then 2770e4f07a6SMauro Carvalho Chehabensure that locks are always acquired in the same order, that the same 2780e4f07a6SMauro Carvalho Chehabinterrupt assumptions apply in all situations, and so on. In other words, 2790e4f07a6SMauro Carvalho Chehablockdep can find a number of scenarios in which the system could, on rare 2800e4f07a6SMauro Carvalho Chehaboccasion, deadlock. This kind of problem can be painful (for both 2810e4f07a6SMauro Carvalho Chehabdevelopers and users) in a deployed system; lockdep allows them to be found 2820e4f07a6SMauro Carvalho Chehabin an automated manner ahead of time. Code with any sort of non-trivial 2830e4f07a6SMauro Carvalho Chehablocking should be run with lockdep enabled before being submitted for 2840e4f07a6SMauro Carvalho Chehabinclusion. 2850e4f07a6SMauro Carvalho Chehab 2860e4f07a6SMauro Carvalho ChehabAs a diligent kernel programmer, you will, beyond doubt, check the return 2870e4f07a6SMauro Carvalho Chehabstatus of any operation (such as a memory allocation) which can fail. The 2880e4f07a6SMauro Carvalho Chehabfact of the matter, though, is that the resulting failure recovery paths 2890e4f07a6SMauro Carvalho Chehabare, probably, completely untested. Untested code tends to be broken code; 2900e4f07a6SMauro Carvalho Chehabyou could be much more confident of your code if all those error-handling 2910e4f07a6SMauro Carvalho Chehabpaths had been exercised a few times. 2920e4f07a6SMauro Carvalho Chehab 2930e4f07a6SMauro Carvalho ChehabThe kernel provides a fault injection framework which can do exactly that, 2940e4f07a6SMauro Carvalho Chehabespecially where memory allocations are involved. With fault injection 2950e4f07a6SMauro Carvalho Chehabenabled, a configurable percentage of memory allocations will be made to 2960e4f07a6SMauro Carvalho Chehabfail; these failures can be restricted to a specific range of code. 2970e4f07a6SMauro Carvalho ChehabRunning with fault injection enabled allows the programmer to see how the 2980e4f07a6SMauro Carvalho Chehabcode responds when things go badly. See 29910ffebbeSMauro Carvalho ChehabDocumentation/fault-injection/fault-injection.rst for more information on 3000e4f07a6SMauro Carvalho Chehabhow to use this facility. 3010e4f07a6SMauro Carvalho Chehab 3020e4f07a6SMauro Carvalho ChehabOther kinds of errors can be found with the "sparse" static analysis tool. 3030e4f07a6SMauro Carvalho ChehabWith sparse, the programmer can be warned about confusion between 3040e4f07a6SMauro Carvalho Chehabuser-space and kernel-space addresses, mixture of big-endian and 3050e4f07a6SMauro Carvalho Chehabsmall-endian quantities, the passing of integer values where a set of bit 3060e4f07a6SMauro Carvalho Chehabflags is expected, and so on. Sparse must be installed separately (it can 3070e4f07a6SMauro Carvalho Chehabbe found at https://sparse.wiki.kernel.org/index.php/Main_Page if your 3080e4f07a6SMauro Carvalho Chehabdistributor does not package it); it can then be run on the code by adding 3090e4f07a6SMauro Carvalho Chehab"C=1" to your make command. 3100e4f07a6SMauro Carvalho Chehab 3110e4f07a6SMauro Carvalho ChehabThe "Coccinelle" tool (http://coccinelle.lip6.fr/) is able to find a wide 3120e4f07a6SMauro Carvalho Chehabvariety of potential coding problems; it can also propose fixes for those 3130e4f07a6SMauro Carvalho Chehabproblems. Quite a few "semantic patches" for the kernel have been packaged 3140e4f07a6SMauro Carvalho Chehabunder the scripts/coccinelle directory; running "make coccicheck" will run 3150e4f07a6SMauro Carvalho Chehabthrough those semantic patches and report on any problems found. See 316f77af637SFederico Vaga:ref:`Documentation/dev-tools/coccinelle.rst <devtools_coccinelle>` 317f77af637SFederico Vagafor more information. 3180e4f07a6SMauro Carvalho Chehab 3190e4f07a6SMauro Carvalho ChehabOther kinds of portability errors are best found by compiling your code for 3200e4f07a6SMauro Carvalho Chehabother architectures. If you do not happen to have an S/390 system or a 3210e4f07a6SMauro Carvalho ChehabBlackfin development board handy, you can still perform the compilation 3220e4f07a6SMauro Carvalho Chehabstep. A large set of cross compilers for x86 systems can be found at 3230e4f07a6SMauro Carvalho Chehab 324e7b4311eSAlexander A. Klimov https://www.kernel.org/pub/tools/crosstool/ 3250e4f07a6SMauro Carvalho Chehab 3260e4f07a6SMauro Carvalho ChehabSome time spent installing and using these compilers will help avoid 3270e4f07a6SMauro Carvalho Chehabembarrassment later. 3280e4f07a6SMauro Carvalho Chehab 3290e4f07a6SMauro Carvalho Chehab 3300e4f07a6SMauro Carvalho ChehabDocumentation 3310e4f07a6SMauro Carvalho Chehab------------- 3320e4f07a6SMauro Carvalho Chehab 3330e4f07a6SMauro Carvalho ChehabDocumentation has often been more the exception than the rule with kernel 3340e4f07a6SMauro Carvalho Chehabdevelopment. Even so, adequate documentation will help to ease the merging 3350e4f07a6SMauro Carvalho Chehabof new code into the kernel, make life easier for other developers, and 3360e4f07a6SMauro Carvalho Chehabwill be helpful for your users. In many cases, the addition of 3370e4f07a6SMauro Carvalho Chehabdocumentation has become essentially mandatory. 3380e4f07a6SMauro Carvalho Chehab 3390e4f07a6SMauro Carvalho ChehabThe first piece of documentation for any patch is its associated 3400e4f07a6SMauro Carvalho Chehabchangelog. Log entries should describe the problem being solved, the form 3410e4f07a6SMauro Carvalho Chehabof the solution, the people who worked on the patch, any relevant 3420e4f07a6SMauro Carvalho Chehabeffects on performance, and anything else that might be needed to 3430e4f07a6SMauro Carvalho Chehabunderstand the patch. Be sure that the changelog says *why* the patch is 3440e4f07a6SMauro Carvalho Chehabworth applying; a surprising number of developers fail to provide that 3450e4f07a6SMauro Carvalho Chehabinformation. 3460e4f07a6SMauro Carvalho Chehab 3470e4f07a6SMauro Carvalho ChehabAny code which adds a new user-space interface - including new sysfs or 3480e4f07a6SMauro Carvalho Chehab/proc files - should include documentation of that interface which enables 3490e4f07a6SMauro Carvalho Chehabuser-space developers to know what they are working with. See 3500e4f07a6SMauro Carvalho ChehabDocumentation/ABI/README for a description of how this documentation should 3510e4f07a6SMauro Carvalho Chehabbe formatted and what information needs to be provided. 3520e4f07a6SMauro Carvalho Chehab 3539b9355a2SAndrew ClaytonThe file :ref:`Documentation/admin-guide/kernel-parameters.rst 3549b9355a2SAndrew Clayton<kernelparameters>` describes all of the kernel's boot-time parameters. 3559b9355a2SAndrew ClaytonAny patch which adds new parameters should add the appropriate entries to 3569b9355a2SAndrew Claytonthis file. 3570e4f07a6SMauro Carvalho Chehab 3580e4f07a6SMauro Carvalho ChehabAny new configuration options must be accompanied by help text which 3590e4f07a6SMauro Carvalho Chehabclearly explains the options and when the user might want to select them. 3600e4f07a6SMauro Carvalho Chehab 3610e4f07a6SMauro Carvalho ChehabInternal API information for many subsystems is documented by way of 3620e4f07a6SMauro Carvalho Chehabspecially-formatted comments; these comments can be extracted and formatted 3630e4f07a6SMauro Carvalho Chehabin a number of ways by the "kernel-doc" script. If you are working within 3640e4f07a6SMauro Carvalho Chehaba subsystem which has kerneldoc comments, you should maintain them and add 3650e4f07a6SMauro Carvalho Chehabthem, as appropriate, for externally-available functions. Even in areas 3660e4f07a6SMauro Carvalho Chehabwhich have not been so documented, there is no harm in adding kerneldoc 3670e4f07a6SMauro Carvalho Chehabcomments for the future; indeed, this can be a useful activity for 3680e4f07a6SMauro Carvalho Chehabbeginning kernel developers. The format of these comments, along with some 3691dc4bbf0SMauro Carvalho Chehabinformation on how to create kerneldoc templates can be found at 3701dc4bbf0SMauro Carvalho Chehab:ref:`Documentation/doc-guide/ <doc_guide>`. 3710e4f07a6SMauro Carvalho Chehab 3720e4f07a6SMauro Carvalho ChehabAnybody who reads through a significant amount of existing kernel code will 3730e4f07a6SMauro Carvalho Chehabnote that, often, comments are most notable by their absence. Once again, 3740e4f07a6SMauro Carvalho Chehabthe expectations for new code are higher than they were in the past; 3750e4f07a6SMauro Carvalho Chehabmerging uncommented code will be harder. That said, there is little desire 3760e4f07a6SMauro Carvalho Chehabfor verbosely-commented code. The code should, itself, be readable, with 3770e4f07a6SMauro Carvalho Chehabcomments explaining the more subtle aspects. 3780e4f07a6SMauro Carvalho Chehab 3790e4f07a6SMauro Carvalho ChehabCertain things should always be commented. Uses of memory barriers should 3800e4f07a6SMauro Carvalho Chehabbe accompanied by a line explaining why the barrier is necessary. The 3810e4f07a6SMauro Carvalho Chehablocking rules for data structures generally need to be explained somewhere. 3820e4f07a6SMauro Carvalho ChehabMajor data structures need comprehensive documentation in general. 3830e4f07a6SMauro Carvalho ChehabNon-obvious dependencies between separate bits of code should be pointed 3840e4f07a6SMauro Carvalho Chehabout. Anything which might tempt a code janitor to make an incorrect 3850e4f07a6SMauro Carvalho Chehab"cleanup" needs a comment saying why it is done the way it is. And so on. 3860e4f07a6SMauro Carvalho Chehab 3870e4f07a6SMauro Carvalho Chehab 3880e4f07a6SMauro Carvalho ChehabInternal API changes 3890e4f07a6SMauro Carvalho Chehab-------------------- 3900e4f07a6SMauro Carvalho Chehab 3910e4f07a6SMauro Carvalho ChehabThe binary interface provided by the kernel to user space cannot be broken 3920e4f07a6SMauro Carvalho Chehabexcept under the most severe circumstances. The kernel's internal 3930e4f07a6SMauro Carvalho Chehabprogramming interfaces, instead, are highly fluid and can be changed when 3940e4f07a6SMauro Carvalho Chehabthe need arises. If you find yourself having to work around a kernel API, 3950e4f07a6SMauro Carvalho Chehabor simply not using a specific functionality because it does not meet your 3960e4f07a6SMauro Carvalho Chehabneeds, that may be a sign that the API needs to change. As a kernel 3970e4f07a6SMauro Carvalho Chehabdeveloper, you are empowered to make such changes. 3980e4f07a6SMauro Carvalho Chehab 3990e4f07a6SMauro Carvalho ChehabThere are, of course, some catches. API changes can be made, but they need 4000e4f07a6SMauro Carvalho Chehabto be well justified. So any patch making an internal API change should be 4010e4f07a6SMauro Carvalho Chehabaccompanied by a description of what the change is and why it is 4020e4f07a6SMauro Carvalho Chehabnecessary. This kind of change should also be broken out into a separate 4030e4f07a6SMauro Carvalho Chehabpatch, rather than buried within a larger patch. 4040e4f07a6SMauro Carvalho Chehab 4050e4f07a6SMauro Carvalho ChehabThe other catch is that a developer who changes an internal API is 4060e4f07a6SMauro Carvalho Chehabgenerally charged with the task of fixing any code within the kernel tree 4070e4f07a6SMauro Carvalho Chehabwhich is broken by the change. For a widely-used function, this duty can 4080e4f07a6SMauro Carvalho Chehablead to literally hundreds or thousands of changes - many of which are 4090e4f07a6SMauro Carvalho Chehablikely to conflict with work being done by other developers. Needless to 4100e4f07a6SMauro Carvalho Chehabsay, this can be a large job, so it is best to be sure that the 4110e4f07a6SMauro Carvalho Chehabjustification is solid. Note that the Coccinelle tool can help with 4120e4f07a6SMauro Carvalho Chehabwide-ranging API changes. 4130e4f07a6SMauro Carvalho Chehab 4140e4f07a6SMauro Carvalho ChehabWhen making an incompatible API change, one should, whenever possible, 4150e4f07a6SMauro Carvalho Chehabensure that code which has not been updated is caught by the compiler. 4160e4f07a6SMauro Carvalho ChehabThis will help you to be sure that you have found all in-tree uses of that 4170e4f07a6SMauro Carvalho Chehabinterface. It will also alert developers of out-of-tree code that there is 4180e4f07a6SMauro Carvalho Chehaba change that they need to respond to. Supporting out-of-tree code is not 4190e4f07a6SMauro Carvalho Chehabsomething that kernel developers need to be worried about, but we also do 4200e4f07a6SMauro Carvalho Chehabnot have to make life harder for out-of-tree developers than it needs to 4210e4f07a6SMauro Carvalho Chehabbe. 422