blob: eb4b185d168c0524326793ea7152c02ee3793c82 [file] [log] [blame]
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -03001.. _development_coding:
2
3Getting the code right
4======================
Jonathan Corbet75b02142008-09-30 15:15:56 -06005
6While there is much to be said for a solid and community-oriented design
7process, the proof of any kernel development project is in the resulting
8code. It is the code which will be examined by other developers and merged
9(or not) into the mainline tree. So it is the quality of this code which
10will determine the ultimate success of the project.
11
12This section will examine the coding process. We'll start with a look at a
13number of ways in which kernel developers can go wrong. Then the focus
14will shift toward doing things right and the tools which can help in that
15quest.
16
17
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -030018Pitfalls
19---------
Jonathan Corbet75b02142008-09-30 15:15:56 -060020
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -030021Coding style
22************
Jonathan Corbet75b02142008-09-30 15:15:56 -060023
24The kernel has long had a standard coding style, described in
Andrew Clayton9b9355a2017-04-20 14:04:15 +010025:ref:`Documentation/process/coding-style.rst <codingstyle>`. For much of
26that time, the policies described in that file were taken as being, at most,
27advisory. As a result, there is a substantial amount of code in the kernel
28which does not meet the coding style guidelines. The presence of that code
29leads to two independent hazards for kernel developers.
Jonathan Corbet75b02142008-09-30 15:15:56 -060030
31The first of these is to believe that the kernel coding standards do not
32matter and are not enforced. The truth of the matter is that adding new
33code to the kernel is very difficult if that code is not coded according to
34the standard; many developers will request that the code be reformatted
35before they will even review it. A code base as large as the kernel
36requires some uniformity of code to make it possible for developers to
37quickly understand any part of it. So there is no longer room for
38strangely-formatted code.
39
40Occasionally, the kernel's coding style will run into conflict with an
41employer's mandated style. In such cases, the kernel's style will have to
42win before the code can be merged. Putting code into the kernel means
43giving up a degree of control in a number of ways - including control over
44how the code is formatted.
45
46The other trap is to assume that code which is already in the kernel is
47urgently in need of coding style fixes. Developers may start to generate
48reformatting patches as a way of gaining familiarity with the process, or
49as a way of getting their name into the kernel changelogs - or both. But
50pure coding style fixes are seen as noise by the development community;
51they tend to get a chilly reception. So this type of patch is best
52avoided. It is natural to fix the style of a piece of code while working
53on it for other reasons, but coding style changes should not be made for
54their own sake.
55
56The coding style document also should not be read as an absolute law which
57can never be transgressed. If there is a good reason to go against the
58style (a line which becomes far less readable if split to fit within the
5980-column limit, for example), just do it.
60
Miguel Ojedad4ef8d32018-04-10 16:32:40 -070061Note that you can also use the ``clang-format`` tool to help you with
62these rules, to quickly re-format parts of your code automatically,
63and to review full files in order to spot coding style mistakes,
64typos and possible improvements. It is also handy for sorting ``#includes``,
65for aligning variables/macros, for reflowing text and other similar tasks.
66See the file :ref:`Documentation/process/clang-format.rst <clangformat>`
67for more details.
68
Jonathan Corbet75b02142008-09-30 15:15:56 -060069
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -030070Abstraction layers
71******************
Jonathan Corbet75b02142008-09-30 15:15:56 -060072
73Computer Science professors teach students to make extensive use of
74abstraction layers in the name of flexibility and information hiding.
75Certainly the kernel makes extensive use of abstraction; no project
76involving several million lines of code could do otherwise and survive.
77But experience has shown that excessive or premature abstraction can be
78just as harmful as premature optimization. Abstraction should be used to
79the level required and no further.
80
81At a simple level, consider a function which has an argument which is
82always passed as zero by all callers. One could retain that argument just
83in case somebody eventually needs to use the extra flexibility that it
84provides. By that time, though, chances are good that the code which
85implements this extra argument has been broken in some subtle way which was
86never noticed - because it has never been used. Or, when the need for
87extra flexibility arises, it does not do so in a way which matches the
88programmer's early expectation. Kernel developers will routinely submit
89patches to remove unused arguments; they should, in general, not be added
90in the first place.
91
92Abstraction layers which hide access to hardware - often to allow the bulk
93of a driver to be used with multiple operating systems - are especially
94frowned upon. Such layers obscure the code and may impose a performance
95penalty; they do not belong in the Linux kernel.
96
97On the other hand, if you find yourself copying significant amounts of code
98from another kernel subsystem, it is time to ask whether it would, in fact,
99make sense to pull out some of that code into a separate library or to
100implement that functionality at a higher level. There is no value in
101replicating the same code throughout the kernel.
102
103
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300104#ifdef and preprocessor use in general
105**************************************
Jonathan Corbet75b02142008-09-30 15:15:56 -0600106
107The C preprocessor seems to present a powerful temptation to some C
108programmers, who see it as a way to efficiently encode a great deal of
109flexibility into a source file. But the preprocessor is not C, and heavy
110use of it results in code which is much harder for others to read and
111harder for the compiler to check for correctness. Heavy preprocessor use
112is almost always a sign of code which needs some cleanup work.
113
114Conditional compilation with #ifdef is, indeed, a powerful feature, and it
115is used within the kernel. But there is little desire to see code which is
116sprinkled liberally with #ifdef blocks. As a general rule, #ifdef use
117should be confined to header files whenever possible.
118Conditionally-compiled code can be confined to functions which, if the code
119is not to be present, simply become empty. The compiler will then quietly
120optimize out the call to the empty function. The result is far cleaner
121code which is easier to follow.
122
123C preprocessor macros present a number of hazards, including possible
124multiple evaluation of expressions with side effects and no type safety.
125If you are tempted to define a macro, consider creating an inline function
126instead. The code which results will be the same, but inline functions are
127easier to read, do not evaluate their arguments multiple times, and allow
128the compiler to perform type checking on the arguments and return value.
129
130
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300131Inline functions
132****************
Jonathan Corbet75b02142008-09-30 15:15:56 -0600133
134Inline functions present a hazard of their own, though. Programmers can
135become enamored of the perceived efficiency inherent in avoiding a function
136call and fill a source file with inline functions. Those functions,
137however, can actually reduce performance. Since their code is replicated
138at each call site, they end up bloating the size of the compiled kernel.
139That, in turn, creates pressure on the processor's memory caches, which can
140slow execution dramatically. Inline functions, as a rule, should be quite
141small and relatively rare. The cost of a function call, after all, is not
142that high; the creation of large numbers of inline functions is a classic
143example of premature optimization.
144
145In general, kernel programmers ignore cache effects at their peril. The
146classic time/space tradeoff taught in beginning data structures classes
147often does not apply to contemporary hardware. Space *is* time, in that a
148larger program will run slower than one which is more compact.
149
Jonathan Corbet5c050fb2011-03-25 12:17:53 -0600150More recent compilers take an increasingly active role in deciding whether
151a given function should actually be inlined or not. So the liberal
152placement of "inline" keywords may not just be excessive; it could also be
153irrelevant.
154
Jonathan Corbet75b02142008-09-30 15:15:56 -0600155
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300156Locking
157*******
Jonathan Corbet75b02142008-09-30 15:15:56 -0600158
159In May, 2006, the "Devicescape" networking stack was, with great
160fanfare, released under the GPL and made available for inclusion in the
161mainline kernel. This donation was welcome news; support for wireless
162networking in Linux was considered substandard at best, and the Devicescape
163stack offered the promise of fixing that situation. Yet, this code did not
164actually make it into the mainline until June, 2007 (2.6.22). What
165happened?
166
167This code showed a number of signs of having been developed behind
168corporate doors. But one large problem in particular was that it was not
169designed to work on multiprocessor systems. Before this networking stack
170(now called mac80211) could be merged, a locking scheme needed to be
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300171retrofitted onto it.
Jonathan Corbet75b02142008-09-30 15:15:56 -0600172
173Once upon a time, Linux kernel code could be developed without thinking
174about the concurrency issues presented by multiprocessor systems. Now,
175however, this document is being written on a dual-core laptop. Even on
176single-processor systems, work being done to improve responsiveness will
177raise the level of concurrency within the kernel. The days when kernel
178code could be written without thinking about locking are long past.
179
180Any resource (data structures, hardware registers, etc.) which could be
181accessed concurrently by more than one thread must be protected by a lock.
182New code should be written with this requirement in mind; retrofitting
183locking after the fact is a rather more difficult task. Kernel developers
184should take the time to understand the available locking primitives well
185enough to pick the right tool for the job. Code which shows a lack of
186attention to concurrency will have a difficult path into the mainline.
187
188
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300189Regressions
190***********
Jonathan Corbet75b02142008-09-30 15:15:56 -0600191
192One final hazard worth mentioning is this: it can be tempting to make a
193change (which may bring big improvements) which causes something to break
194for existing users. This kind of change is called a "regression," and
195regressions have become most unwelcome in the mainline kernel. With few
196exceptions, changes which cause regressions will be backed out if the
197regression cannot be fixed in a timely manner. Far better to avoid the
198regression in the first place.
199
200It is often argued that a regression can be justified if it causes things
201to work for more people than it creates problems for. Why not make a
202change if it brings new functionality to ten systems for each one it
203breaks? The best answer to this question was expressed by Linus in July,
2042007:
205
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300206::
207
Jonathan Corbet75b02142008-09-30 15:15:56 -0600208 So we don't fix bugs by introducing new problems. That way lies
209 madness, and nobody ever knows if you actually make any real
210 progress at all. Is it two steps forwards, one step back, or one
211 step forward and two steps back?
212
213(http://lwn.net/Articles/243460/).
214
215An especially unwelcome type of regression is any sort of change to the
216user-space ABI. Once an interface has been exported to user space, it must
217be supported indefinitely. This fact makes the creation of user-space
218interfaces particularly challenging: since they cannot be changed in
219incompatible ways, they must be done right the first time. For this
220reason, a great deal of thought, clear documentation, and wide review for
221user-space interfaces is always required.
222
223
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300224Code checking tools
225-------------------
Jonathan Corbet75b02142008-09-30 15:15:56 -0600226
227For now, at least, the writing of error-free code remains an ideal that few
228of us can reach. What we can hope to do, though, is to catch and fix as
229many of those errors as possible before our code goes into the mainline
230kernel. To that end, the kernel developers have put together an impressive
231array of tools which can catch a wide variety of obscure problems in an
232automated way. Any problem caught by the computer is a problem which will
233not afflict a user later on, so it stands to reason that the automated
234tools should be used whenever possible.
235
236The first step is simply to heed the warnings produced by the compiler.
237Contemporary versions of gcc can detect (and warn about) a large number of
238potential errors. Quite often, these warnings point to real problems.
239Code submitted for review should, as a rule, not produce any compiler
240warnings. When silencing warnings, take care to understand the real cause
241and try to avoid "fixes" which make the warning go away without addressing
242its cause.
243
244Note that not all compiler warnings are enabled by default. Build the
245kernel with "make EXTRA_CFLAGS=-W" to get the full set.
246
247The kernel provides several configuration options which turn on debugging
248features; most of these are found in the "kernel hacking" submenu. Several
249of these options should be turned on for any kernel used for development or
250testing purposes. In particular, you should turn on:
251
252 - ENABLE_WARN_DEPRECATED, ENABLE_MUST_CHECK, and FRAME_WARN to get an
253 extra set of warnings for problems like the use of deprecated interfaces
254 or ignoring an important return value from a function. The output
255 generated by these warnings can be verbose, but one need not worry about
256 warnings from other parts of the kernel.
257
258 - DEBUG_OBJECTS will add code to track the lifetime of various objects
259 created by the kernel and warn when things are done out of order. If
260 you are adding a subsystem which creates (and exports) complex objects
261 of its own, consider adding support for the object debugging
262 infrastructure.
263
264 - DEBUG_SLAB can find a variety of memory allocation and use errors; it
265 should be used on most development kernels.
266
Frederic Weisbeckerd902db12011-06-08 19:31:56 +0200267 - DEBUG_SPINLOCK, DEBUG_ATOMIC_SLEEP, and DEBUG_MUTEXES will find a
Jonathan Corbet75b02142008-09-30 15:15:56 -0600268 number of common locking errors.
269
270There are quite a few other debugging options, some of which will be
271discussed below. Some of them have a significant performance impact and
272should not be used all of the time. But some time spent learning the
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300273available options will likely be paid back many times over in short order.
Jonathan Corbet75b02142008-09-30 15:15:56 -0600274
275One of the heavier debugging tools is the locking checker, or "lockdep."
276This tool will track the acquisition and release of every lock (spinlock or
277mutex) in the system, the order in which locks are acquired relative to
278each other, the current interrupt environment, and more. It can then
279ensure that locks are always acquired in the same order, that the same
280interrupt assumptions apply in all situations, and so on. In other words,
281lockdep can find a number of scenarios in which the system could, on rare
282occasion, deadlock. This kind of problem can be painful (for both
283developers and users) in a deployed system; lockdep allows them to be found
284in an automated manner ahead of time. Code with any sort of non-trivial
285locking should be run with lockdep enabled before being submitted for
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300286inclusion.
Jonathan Corbet75b02142008-09-30 15:15:56 -0600287
288As a diligent kernel programmer, you will, beyond doubt, check the return
289status of any operation (such as a memory allocation) which can fail. The
290fact of the matter, though, is that the resulting failure recovery paths
291are, probably, completely untested. Untested code tends to be broken code;
292you could be much more confident of your code if all those error-handling
293paths had been exercised a few times.
294
295The kernel provides a fault injection framework which can do exactly that,
296especially where memory allocations are involved. With fault injection
297enabled, a configurable percentage of memory allocations will be made to
298fail; these failures can be restricted to a specific range of code.
299Running with fault injection enabled allows the programmer to see how the
300code responds when things go badly. See
Paul Bolle395cf962011-08-15 02:02:26 +0200301Documentation/fault-injection/fault-injection.txt for more information on
Jonathan Corbet75b02142008-09-30 15:15:56 -0600302how to use this facility.
303
304Other kinds of errors can be found with the "sparse" static analysis tool.
305With sparse, the programmer can be warned about confusion between
306user-space and kernel-space addresses, mixture of big-endian and
307small-endian quantities, the passing of integer values where a set of bit
308flags is expected, and so on. Sparse must be installed separately (it can
Justin P. Mattock0ea6e612010-07-23 20:51:24 -0700309be found at https://sparse.wiki.kernel.org/index.php/Main_Page if your
Jonathan Corbet75b02142008-09-30 15:15:56 -0600310distributor does not package it); it can then be run on the code by adding
311"C=1" to your make command.
312
Jonathan Corbet5c050fb2011-03-25 12:17:53 -0600313The "Coccinelle" tool (http://coccinelle.lip6.fr/) is able to find a wide
314variety of potential coding problems; it can also propose fixes for those
315problems. Quite a few "semantic patches" for the kernel have been packaged
316under the scripts/coccinelle directory; running "make coccicheck" will run
317through those semantic patches and report on any problems found. See
Tom Saeger2c0c5c22017-10-12 15:23:53 -0500318Documentation/dev-tools/coccinelle.rst for more information.
Jonathan Corbet5c050fb2011-03-25 12:17:53 -0600319
Jonathan Corbet75b02142008-09-30 15:15:56 -0600320Other kinds of portability errors are best found by compiling your code for
321other architectures. If you do not happen to have an S/390 system or a
322Blackfin development board handy, you can still perform the compilation
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300323step. A large set of cross compilers for x86 systems can be found at
Jonathan Corbet75b02142008-09-30 15:15:56 -0600324
325 http://www.kernel.org/pub/tools/crosstool/
326
327Some time spent installing and using these compilers will help avoid
328embarrassment later.
329
330
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300331Documentation
332-------------
Jonathan Corbet75b02142008-09-30 15:15:56 -0600333
334Documentation has often been more the exception than the rule with kernel
335development. Even so, adequate documentation will help to ease the merging
336of new code into the kernel, make life easier for other developers, and
337will be helpful for your users. In many cases, the addition of
338documentation has become essentially mandatory.
339
340The first piece of documentation for any patch is its associated
341changelog. Log entries should describe the problem being solved, the form
342of the solution, the people who worked on the patch, any relevant
343effects on performance, and anything else that might be needed to
Jonathan Corbet5c050fb2011-03-25 12:17:53 -0600344understand the patch. Be sure that the changelog says *why* the patch is
345worth applying; a surprising number of developers fail to provide that
346information.
Jonathan Corbet75b02142008-09-30 15:15:56 -0600347
348Any code which adds a new user-space interface - including new sysfs or
349/proc files - should include documentation of that interface which enables
350user-space developers to know what they are working with. See
351Documentation/ABI/README for a description of how this documentation should
352be formatted and what information needs to be provided.
353
Andrew Clayton9b9355a2017-04-20 14:04:15 +0100354The file :ref:`Documentation/admin-guide/kernel-parameters.rst
355<kernelparameters>` describes all of the kernel's boot-time parameters.
356Any patch which adds new parameters should add the appropriate entries to
357this file.
Jonathan Corbet75b02142008-09-30 15:15:56 -0600358
359Any new configuration options must be accompanied by help text which
Jonathan Corbet5c050fb2011-03-25 12:17:53 -0600360clearly explains the options and when the user might want to select them.
Jonathan Corbet75b02142008-09-30 15:15:56 -0600361
362Internal API information for many subsystems is documented by way of
363specially-formatted comments; these comments can be extracted and formatted
364in a number of ways by the "kernel-doc" script. If you are working within
365a subsystem which has kerneldoc comments, you should maintain them and add
366them, as appropriate, for externally-available functions. Even in areas
367which have not been so documented, there is no harm in adding kerneldoc
368comments for the future; indeed, this can be a useful activity for
369beginning kernel developers. The format of these comments, along with some
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200370information on how to create kerneldoc templates can be found at
371:ref:`Documentation/doc-guide/ <doc_guide>`.
Jonathan Corbet75b02142008-09-30 15:15:56 -0600372
373Anybody who reads through a significant amount of existing kernel code will
374note that, often, comments are most notable by their absence. Once again,
375the expectations for new code are higher than they were in the past;
376merging uncommented code will be harder. That said, there is little desire
377for verbosely-commented code. The code should, itself, be readable, with
378comments explaining the more subtle aspects.
379
380Certain things should always be commented. Uses of memory barriers should
381be accompanied by a line explaining why the barrier is necessary. The
382locking rules for data structures generally need to be explained somewhere.
383Major data structures need comprehensive documentation in general.
384Non-obvious dependencies between separate bits of code should be pointed
385out. Anything which might tempt a code janitor to make an incorrect
386"cleanup" needs a comment saying why it is done the way it is. And so on.
387
388
Mauro Carvalho Chehabf7c9fe42016-09-19 08:07:36 -0300389Internal API changes
390--------------------
Jonathan Corbet75b02142008-09-30 15:15:56 -0600391
392The binary interface provided by the kernel to user space cannot be broken
393except under the most severe circumstances. The kernel's internal
394programming interfaces, instead, are highly fluid and can be changed when
395the need arises. If you find yourself having to work around a kernel API,
396or simply not using a specific functionality because it does not meet your
397needs, that may be a sign that the API needs to change. As a kernel
398developer, you are empowered to make such changes.
399
400There are, of course, some catches. API changes can be made, but they need
401to be well justified. So any patch making an internal API change should be
402accompanied by a description of what the change is and why it is
403necessary. This kind of change should also be broken out into a separate
404patch, rather than buried within a larger patch.
405
406The other catch is that a developer who changes an internal API is
407generally charged with the task of fixing any code within the kernel tree
408which is broken by the change. For a widely-used function, this duty can
409lead to literally hundreds or thousands of changes - many of which are
410likely to conflict with work being done by other developers. Needless to
411say, this can be a large job, so it is best to be sure that the
Jonathan Corbet5c050fb2011-03-25 12:17:53 -0600412justification is solid. Note that the Coccinelle tool can help with
413wide-ranging API changes.
Jonathan Corbet75b02142008-09-30 15:15:56 -0600414
415When making an incompatible API change, one should, whenever possible,
Jonathan Corbetd5b52432009-01-08 16:32:13 -0700416ensure that code which has not been updated is caught by the compiler.
Jonathan Corbet75b02142008-09-30 15:15:56 -0600417This will help you to be sure that you have found all in-tree uses of that
418interface. It will also alert developers of out-of-tree code that there is
419a change that they need to respond to. Supporting out-of-tree code is not
420something that kernel developers need to be worried about, but we also do
Jonathan Corbetd5b52432009-01-08 16:32:13 -0700421not have to make life harder for out-of-tree developers than it needs to
422be.