← Documents Documentation/process/coding-style.rst GitHub 원문 ↗

Linux 6.18.37 · Kernel programming

Linux kernel coding style

들여쓰기와 brace부터 naming, function, macro, logging, allocation, 조건부 컴파일과 crash 방지까지 kernel C code의 공통 규칙을 설명합니다.

Source pathDocumentation/process/coding-style.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

8-character tab과 중첩 깊이

coding-style.rst:3-95

Kernel indentation은 tab 하나당 8 character다. 큰 들여쓰기는 control block 경계를 명확히 하고, 세 단계보다 깊은 nesting이 생겼을 때 function 구조를 다시 나눠야 한다는 신호를 준다. Comment, documentation과 Kconfig를 제외한 code indentation에 space를 사용하지 않는다.

switch (suffix) {
case 'G':
case 'g':
	mem <<= 30;
	break;
case 'K':
case 'k':
	mem <<= 10;
	fallthrough;
default:
	break;
}

Switch와 case label은 같은 column에 둔다. 한 줄에 여러 statement나 assignment를 쓰지 않고 comma operator로 brace를 피하지 않는다. 여러 statement block에는 brace를 사용하며 trailing whitespace를 남기지 않는다.

80 column과 긴 expression

coding-style.rst:98-117

권장 line limit은 80 column이다. 긴 statement는 의미 단위로 나누고 continuation line을 parent보다 오른쪽에 배치한다. Function call과 prototype argument는 opening parenthesis 기준으로 정렬하는 방식이 일반적이다.

User-visible printk string은 grep 가능성을 깨뜨리므로 단순히 80 column을 맞추려고 literal을 여러 줄로 나누지 않는다. 80 column 초과가 오히려 정보를 더 잘 보이고 숨기지 않는다면 예외가 가능하다.

Brace와 space 배치

coding-style.rst:120-303

if, switch, for, while, do 같은 statement block의 opening brace는 같은 줄 끝에 두고 closing brace는 첫 column에 둔다. Function 정의만 opening brace를 다음 줄에 둔다. 같은 statement가 이어지는 else와 do-while의 while은 closing brace 뒤 같은 줄에 둔다.

if (condition) {
	do_this();
} else {
	do_that();
}

int function(int x)
{
	return x + 1;
}

Single simple statement에는 불필요한 brace를 생략할 수 있지만 if와 else 중 한쪽이 여러 statement라면 양쪽 모두 brace를 쓴다. Loop body에 nested control statement가 있으면 outer loop에도 brace를 둔다.

if, switch, for, while 뒤에는 space를 쓰지만 sizeof, typeof, alignof, __attribute__ 뒤에는 쓰지 않는다. Parenthesis 안쪽에 space를 넣지 않는다. Pointer 선언의 *는 variable·function name 쪽에 붙인다. Binary·ternary operator 양쪽에는 space를, unary와 ++·--, .와 -> 주변에는 space를 두지 않는다.

Local과 global 이름

coding-style.rst:306-357

짧은 범위의 local loop counter는 i, 임시값은 tmp처럼 짧고 직접적인 이름을 쓸 수 있다. 반대로 global variable과 function은 count_active_users()처럼 역할을 분명히 드러내야 한다. Type 정보를 이름에 반복하는 Hungarian notation과 mixed-case 이름은 사용하지 않는다.

새 symbol과 문서에는 master/slave, blacklist/whitelist 용어를 도입하지 않고 primary/secondary, initiator/target, controller/device, allowlist/denylist 같은 문맥에 맞는 표현을 쓴다. 기존 UAPI나 hardware·protocol specification을 그대로 유지해야 하는 경우는 예외다.

Typedef를 제한적으로 사용한다

coding-style.rst:359-442

Struct와 pointer를 숨기는 vps_t 같은 typedef는 실제 type과 object 성격을 감추므로 사용하지 않는다. struct virtual_container *처럼 code에서 struct임을 드러내면 type 변경과 review가 쉽다.

  • Opaque object처럼 type 자체를 의도적으로 감춰야 하는 경우
  • u8, u16, u32처럼 architecture와 무관하게 정확한 width를 표현하는 정수형
  • Sparse가 type safety를 검사하도록 만든 명확한 new type
  • C99 표준 type
  • Userspace와 공유하는 type에서 예외적으로 typedef가 더 명확한 경우

새 typedef를 만들기 전에는 이름을 여러 번 쓰는 수고가 interface의 실제 type을 숨기는 비용보다 큰지 검토한다.

Function 길이와 prototype

coding-style.rst:443-525

Function은 하나의 일을 수행하고 화면 한두 개 안에서 이해할 수 있을 만큼 짧게 유지한다. 깊은 nesting과 지나치게 많은 local variable은 helper로 분리할 신호다. 복잡한 function도 line 수 자체보다 개념의 응집성과 각 block의 독립성을 기준으로 나눈다.

Source file의 local function은 definition 전에 static prototype을 불필요하게 모아 두지 말고 call order에 맞춰 정의할 수 있다. Exported function prototype은 적절한 header에 두며 parameter name도 실제 역할을 설명하도록 포함한다. Prototype declaration에서 extern은 생략한다.

중앙집중식 error cleanup

coding-style.rst:526-597

여러 resource를 순서대로 얻는 function은 goto label을 사용해 역순으로 해제하는 단일 exit path를 만들 수 있다. 중복 cleanup code를 줄이고 새 resource가 추가됐을 때 모든 error branch를 따로 고칠 필요가 없어진다.

Label 이름은 err1, err2보다 out_free_buffer, out_unlock처럼 실행하는 cleanup을 표현한다. Goto가 오히려 state를 숨기거나 한 번만 쓰이는 trivial cleanup이면 direct return이 더 명확할 수 있다. Errno와 acquired-state가 각 label에 도달할 때 일관되는지 확인한다.

Code가 아니라 이유를 comment한다

coding-style.rst:598-640

Comment는 code를 다시 영어로 읽는 대신 왜 이 동작과 ordering이 필요한지, 어떤 hardware erratum이나 invariant를 만족하는지 설명한다. Function이 무엇을 하는지 설명해야 한다면 kernel-doc 형식으로 interface와 parameter, return을 문서화한다.

긴 block comment는 각 줄 앞에 *를 정렬하는 kernel 형식을 사용한다. Comment가 code와 어긋나지 않도록 변경 때 함께 검토한다. 명백한 statement마다 comment를 붙이면 중요한 제약이 묻힌다.

Formatting 도구, Kconfig와 data structure

coding-style.rst:641-794

Formatting이 무너졌다면 scripts/Lindent, clang-format과 editor 설정을 참고할 수 있지만 자동 결과를 그대로 믿지 않고 subsystem 주변 code와 비교한다. Kconfig help는 option이 무엇을 enable하고 누가 필요한지 설명하며 indentation과 menu dependency를 기존 규칙에 맞춘다.

자주 함께 접근하는 field를 data structure에서 가까이 두고 cache-line sharing과 alignment를 고려한다. Structure는 선언 순서 자체가 ABI 또는 hardware layout이 아닌 한 의미 있는 group으로 정리하고, concurrent access와 lifetime rule을 code 가까이에 문서화한다.

Macro, enum과 control flow

coding-style.rst:795-898

여러 statement macro는 do { ... } while (0)으로 감싸 if·else context에서도 하나의 statement처럼 동작하게 한다. 모든 parameter 사용을 parenthesis로 감싸고 argument를 여러 번 평가해 side effect가 반복되지 않게 한다.

Macro가 function처럼 동작할 수 있으면 static inline function이 type checking, debug와 evaluation semantics에서 낫다. Flow control을 macro 안에 숨기거나 caller local variable 이름에 의존하는 형태를 피한다. 관련 상수 집합은 의미 있는 enum을 검토한다.

Kernel message, allocation과 inline

coding-style.rst:899-1004

Kernel message는 KERN 계열 또는 pr_*·dev_* level을 상황에 맞게 선택한다. Device 관련 message는 device identity가 자동으로 붙는 dev_err, dev_warn, dev_info를 사용한다. 같은 오류가 반복되는 경로에는 rate limiting을 고려하고 정상 동작을 error level로 출력하지 않는다.

Memory allocation은 sizeof(type)보다 sizeof(*ptr)를 사용해 declaration과 allocation type이 어긋나지 않게 한다. Array와 trailing object는 kmalloc_array, struct_size 같은 overflow-aware helper를 사용하며 cast로 kmalloc return을 가리지 않는다.

inline은 compiler에게 강제 명령이 아니며 code size와 instruction cache에 악영향을 줄 수 있다. 매우 작거나 compile-time constant 최적화가 중요한 function 외에는 compiler 판단에 맡기고 단순히 call overhead를 두려워해 남용하지 않는다.

Return value, bool과 기존 helper

coding-style.rst:1005-1092

Function 이름은 return convention을 예상할 수 있게 한다. Predicate는 true·false를 반환하고 action function은 성공 0과 negative errno를 쓰는 관례를 따른다. Pointer-returning function은 실패에 NULL 또는 ERR_PTR 중 무엇을 쓰는지 interface 문맥에 맞춘다.

실제 boolean state에는 bool과 true·false를 쓰되 bitfield 크기나 hardware register에 bool을 억지로 사용하지 않는다. ARRAY_SIZE, FIELD_GET, min_t와 container_of처럼 검증된 kernel macro가 이미 있으면 동일 기능을 직접 다시 구현하지 않는다.

Inline assembly와 conditional compilation

coding-style.rst:1093-1207

Editor modeline처럼 개인 도구 설정을 source에 넣지 않는다. Inline assembly는 C로 표현할 수 없고 architecture primitive로 격리할 수 있을 때만 사용하며 constraint, clobber와 memory effect를 정확히 선언한다.

C source 안의 #ifdef를 넓게 퍼뜨리기보다 header의 stub helper와 IS_ENABLED(CONFIG_...)를 사용해 type checking을 유지한다. Configuration에 따라 function 전체가 필요 없다면 Makefile에서 object build를 제어한다. Conditional branch마다 독립적으로 build되는지 확인한다.

Kernel을 의도적으로 crash시키지 않는다

coding-style.rst:1208-1269

복구 가능한 error에 panic을 사용하지 않는다. BUG와 BUG_ON은 lock과 state를 정리하지 못한 채 execution을 중단하므로 WARN 계열과 정상 error path로 바꾼다.

반복 가능한 invariant violation에는 WARN_ON_ONCE를 우선 검토해 log flood를 막는다. WARN은 도달해서는 안 되는 kernel bug에만 쓰며 잘못된 userspace input이나 예상 가능한 hardware failure에 사용하지 않는다. panic_on_warn 설정 사용자를 이유로 필요한 WARN을 피하지는 않는다.

Compile-time에 확인할 수 있는 invariant는 runtime BUG가 아니라 BUILD_BUG_ON이나 static assertion으로 검증한다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. _codingstyle:
2
3 Linux kernel coding style
4 =========================
5
6 This is a short document describing the preferred coding style for the
7 linux kernel. Coding style is very personal, and I won't **force** my
8 views on anybody, but this is what goes for anything that I have to be
9 able to maintain, and I'd prefer it for most other things too. Please
10 at least consider the points made here.
11
12 First off, I'd suggest printing out a copy of the GNU coding standards,
13 and NOT read it. Burn them, it's a great symbolic gesture.
14
15 Anyway, here goes:
16
17
18 1) Indentation
19 --------------
20
21 Tabs are 8 characters, and thus indentations are also 8 characters.
22 There are heretic movements that try to make indentations 4 (or even 2!)
23 characters deep, and that is akin to trying to define the value of PI to
24 be 3.
25
26 Rationale: The whole idea behind indentation is to clearly define where
27 a block of control starts and ends. Especially when you've been looking
28 at your screen for 20 straight hours, you'll find it a lot easier to see
29 how the indentation works if you have large indentations.
30
31 Now, some people will claim that having 8-character indentations makes
32 the code move too far to the right, and makes it hard to read on a
33 80-character terminal screen. The answer to that is that if you need
34 more than 3 levels of indentation, you're screwed anyway, and should fix
35 your program.
36
37 In short, 8-char indents make things easier to read, and have the added
38 benefit of warning you when you're nesting your functions too deep.
39 Heed that warning.
40
41 The preferred way to ease multiple indentation levels in a switch statement is
42 to align the ``switch`` and its subordinate ``case`` labels in the same column
43 instead of ``double-indenting`` the ``case`` labels. E.g.:
44
45 .. code-block:: c
46
47 switch (suffix) {
48 case 'G':
49 case 'g':
50 mem <<= 30;
51 break;
52 case 'M':
53 case 'm':
54 mem <<= 20;
55 break;
56 case 'K':
57 case 'k':
58 mem <<= 10;
59 fallthrough;
60 default:
61 break;
62 }
63
64 Don't put multiple statements on a single line unless you have
65 something to hide:
66
67 .. code-block:: c
68
69 if (condition) do_this;
70 do_something_everytime;
71
72 Don't use commas to avoid using braces:
73
74 .. code-block:: c
75
76 if (condition)
77 do_this(), do_that();
78
79 Always uses braces for multiple statements:
80
81 .. code-block:: c
82
83 if (condition) {
84 do_this();
85 do_that();
86 }
87
88 Don't put multiple assignments on a single line either. Kernel coding style
89 is super simple. Avoid tricky expressions.
90
91
92 Outside of comments, documentation and except in Kconfig, spaces are never
93 used for indentation, and the above example is deliberately broken.
94
95 Get a decent editor and don't leave whitespace at the end of lines.
96
97
98 2) Breaking long lines and strings
99 ----------------------------------
100
101 Coding style is all about readability and maintainability using commonly
102 available tools.
103
104 The preferred limit on the length of a single line is 80 columns.
105
106 Statements longer than 80 columns should be broken into sensible chunks,
107 unless exceeding 80 columns significantly increases readability and does
108 not hide information.
109
110 Descendants are always substantially shorter than the parent and
111 are placed substantially to the right. A very commonly used style
112 is to align descendants to a function open parenthesis.
113
114 These same rules are applied to function headers with a long argument list.
115
116 However, never break user-visible strings such as printk messages because
117 that breaks the ability to grep for them.
118
119
120 3) Placing Braces and Spaces
121 ----------------------------
122
123 The other issue that always comes up in C styling is the placement of
124 braces. Unlike the indent size, there are few technical reasons to
125 choose one placement strategy over the other, but the preferred way, as
126 shown to us by the prophets Kernighan and Ritchie, is to put the opening
127 brace last on the line, and put the closing brace first, thusly:
128
129 .. code-block:: c
130
131 if (x is true) {
132 we do y
133 }
134
135 This applies to all non-function statement blocks (if, switch, for,
136 while, do). E.g.:
137
138 .. code-block:: c
139
140 switch (action) {
141 case KOBJ_ADD:
142 return "add";
143 case KOBJ_REMOVE:
144 return "remove";
145 case KOBJ_CHANGE:
146 return "change";
147 default:
148 return NULL;
149 }
150
151 However, there is one special case, namely functions: they have the
152 opening brace at the beginning of the next line, thus:
153
154 .. code-block:: c
155
156 int function(int x)
157 {
158 body of function
159 }
160
161 Heretic people all over the world have claimed that this inconsistency
162 is ... well ... inconsistent, but all right-thinking people know that
163 (a) K&R are **right** and (b) K&R are right. Besides, functions are
164 special anyway (you can't nest them in C).
165
166 Note that the closing brace is empty on a line of its own, **except** in
167 the cases where it is followed by a continuation of the same statement,
168 ie a ``while`` in a do-statement or an ``else`` in an if-statement, like
169 this:
170
171 .. code-block:: c
172
173 do {
174 body of do-loop
175 } while (condition);
176
177 and
178
179 .. code-block:: c
180
181 if (x == y) {
182 ..
183 } else if (x > y) {
184 ...
185 } else {
186 ....
187 }
188
189 Rationale: K&R.
190
191 Also, note that this brace-placement also minimizes the number of empty
192 (or almost empty) lines, without any loss of readability. Thus, as the
193 supply of new-lines on your screen is not a renewable resource (think
194 25-line terminal screens here), you have more empty lines to put
195 comments on.
196
197 Do not unnecessarily use braces where a single statement will do.
198
199 .. code-block:: c
200
201 if (condition)
202 action();
203
204 and
205
206 .. code-block:: c
207
208 if (condition)
209 do_this();
210 else
211 do_that();
212
213 This does not apply if only one branch of a conditional statement is a single
214 statement; in the latter case use braces in both branches:
215
216 .. code-block:: c
217
218 if (condition) {
219 do_this();
220 do_that();
221 } else {
222 otherwise();
223 }
224
225 Also, use braces when a loop contains more than a single simple statement:
226
227 .. code-block:: c
228
229 while (condition) {
230 if (test)
231 do_something();
232 }
233
234 3.1) Spaces
235 ***********
236
237 Linux kernel style for use of spaces depends (mostly) on
238 function-versus-keyword usage. Use a space after (most) keywords. The
239 notable exceptions are sizeof, typeof, alignof, and __attribute__, which look
240 somewhat like functions (and are usually used with parentheses in Linux,
241 although they are not required in the language, as in: ``sizeof info`` after
242 ``struct fileinfo info;`` is declared).
243
244 So use a space after these keywords::
245
246 if, switch, case, for, do, while
247
248 but not with sizeof, typeof, alignof, or __attribute__. E.g.,
249
250 .. code-block:: c
251
252
253 s = sizeof(struct file);
254
255 Do not add spaces around (inside) parenthesized expressions. This example is
256 **bad**:
257
258 .. code-block:: c
259
260
261 s = sizeof( struct file );
262
263 When declaring pointer data or a function that returns a pointer type, the
264 preferred use of ``*`` is adjacent to the data name or function name and not
265 adjacent to the type name. Examples:
266
267 .. code-block:: c
268
269
270 char *linux_banner;
271 unsigned long long memparse(char *ptr, char **retptr);
272 char *match_strdup(substring_t *s);
273
274 Use one space around (on each side of) most binary and ternary operators,
275 such as any of these::
276
277 = + - < > * / % | & ^ <= >= == != ? :
278
279 but no space after unary operators::
280
281 & * + - ~ ! sizeof typeof alignof __attribute__ defined
282
283 no space before the postfix increment & decrement unary operators::
284
285 ++ --
286
287 no space after the prefix increment & decrement unary operators::
288
289 ++ --
290
291 and no space around the ``.`` and ``->`` structure member operators.
292
293 Do not leave trailing whitespace at the ends of lines. Some editors with
294 ``smart`` indentation will insert whitespace at the beginning of new lines as
295 appropriate, so you can start typing the next line of code right away.
296 However, some such editors do not remove the whitespace if you end up not
297 putting a line of code there, such as if you leave a blank line. As a result,
298 you end up with lines containing trailing whitespace.
299
300 Git will warn you about patches that introduce trailing whitespace, and can
301 optionally strip the trailing whitespace for you; however, if applying a series
302 of patches, this may make later patches in the series fail by changing their
303 context lines.
304
305
306 4) Naming
307 ---------
308
309 C is a Spartan language, and your naming conventions should follow suit.
310 Unlike Modula-2 and Pascal programmers, C programmers do not use cute
311 names like ThisVariableIsATemporaryCounter. A C programmer would call that
312 variable ``tmp``, which is much easier to write, and not the least more
313 difficult to understand.
314
315 HOWEVER, while mixed-case names are frowned upon, descriptive names for
316 global variables are a must. To call a global function ``foo`` is a
317 shooting offense.
318
319 GLOBAL variables (to be used only if you **really** need them) need to
320 have descriptive names, as do global functions. If you have a function
321 that counts the number of active users, you should call that
322 ``count_active_users()`` or similar, you should **not** call it ``cntusr()``.
323
324 Encoding the type of a function into the name (so-called Hungarian
325 notation) is asinine - the compiler knows the types anyway and can check
326 those, and it only confuses the programmer.
327
328 LOCAL variable names should be short, and to the point. If you have
329 some random integer loop counter, it should probably be called ``i``.
330 Calling it ``loop_counter`` is non-productive, if there is no chance of it
331 being mis-understood. Similarly, ``tmp`` can be just about any type of
332 variable that is used to hold a temporary value.
333
334 If you are afraid to mix up your local variable names, you have another
335 problem, which is called the function-growth-hormone-imbalance syndrome.
336 See chapter 6 (Functions).
337
338 For symbol names and documentation, avoid introducing new usage of
339 'master / slave' (or 'slave' independent of 'master') and 'blacklist /
340 whitelist'.
341
342 Recommended replacements for 'master / slave' are:
343 '{primary,main} / {secondary,replica,subordinate}'
344 '{initiator,requester} / {target,responder}'
345 '{controller,host} / {device,worker,proxy}'
346 'leader / follower'
347 'director / performer'
348
349 Recommended replacements for 'blacklist/whitelist' are:
350 'denylist / allowlist'
351 'blocklist / passlist'
352
353 Exceptions for introducing new usage is to maintain a userspace ABI/API,
354 or when updating code for an existing (as of 2020) hardware or protocol
355 specification that mandates those terms. For new specifications
356 translate specification usage of the terminology to the kernel coding
357 standard where possible.
358
359 5) Typedefs
360 -----------
361
362 Please don't use things like ``vps_t``.
363 It's a **mistake** to use typedef for structures and pointers. When you see a
364
365 .. code-block:: c
366
367
368 vps_t a;
369
370 in the source, what does it mean?
371 In contrast, if it says
372
373 .. code-block:: c
374
375 struct virtual_container *a;
376
377 you can actually tell what ``a`` is.
378
379 Lots of people think that typedefs ``help readability``. Not so. They are
380 useful only for:
381
382 (a) totally opaque objects (where the typedef is actively used to **hide**
383 what the object is).
384
385 Example: ``pte_t`` etc. opaque objects that you can only access using
386 the proper accessor functions.
387
388 .. note::
389
390 Opaqueness and ``accessor functions`` are not good in themselves.
391 The reason we have them for things like pte_t etc. is that there
392 really is absolutely **zero** portably accessible information there.
393
394 (b) Clear integer types, where the abstraction **helps** avoid confusion
395 whether it is ``int`` or ``long``.
396
397 u8/u16/u32 are perfectly fine typedefs, although they fit into
398 category (d) better than here.
399
400 .. note::
401
402 Again - there needs to be a **reason** for this. If something is
403 ``unsigned long``, then there's no reason to do
404
405 typedef unsigned long myflags_t;
406
407 but if there is a clear reason for why it under certain circumstances
408 might be an ``unsigned int`` and under other configurations might be
409 ``unsigned long``, then by all means go ahead and use a typedef.
410
411 (c) when you use sparse to literally create a **new** type for
412 type-checking.
413
414 (d) New types which are identical to standard C99 types, in certain
415 exceptional circumstances.
416
417 Although it would only take a short amount of time for the eyes and
418 brain to become accustomed to the standard types like ``uint32_t``,
419 some people object to their use anyway.
420
421 Therefore, the Linux-specific ``u8/u16/u32/u64`` types and their
422 signed equivalents which are identical to standard types are
423 permitted -- although they are not mandatory in new code of your
424 own.
425
426 When editing existing code which already uses one or the other set
427 of types, you should conform to the existing choices in that code.
428
429 (e) Types safe for use in userspace.
430
431 In certain structures which are visible to userspace, we cannot
432 require C99 types and cannot use the ``u32`` form above. Thus, we
433 use __u32 and similar types in all structures which are shared
434 with userspace.
435
436 Maybe there are other cases too, but the rule should basically be to NEVER
437 EVER use a typedef unless you can clearly match one of those rules.
438
439 In general, a pointer, or a struct that has elements that can reasonably
440 be directly accessed should **never** be a typedef.
441
442
443 6) Functions
444 ------------
445
446 Functions should be short and sweet, and do just one thing. They should
447 fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
448 as we all know), and do one thing and do that well.
449
450 The maximum length of a function is inversely proportional to the
451 complexity and indentation level of that function. So, if you have a
452 conceptually simple function that is just one long (but simple)
453 case-statement, where you have to do lots of small things for a lot of
454 different cases, it's OK to have a longer function.
455
456 However, if you have a complex function, and you suspect that a
457 less-than-gifted first-year high-school student might not even
458 understand what the function is all about, you should adhere to the
459 maximum limits all the more closely. Use helper functions with
460 descriptive names (you can ask the compiler to in-line them if you think
461 it's performance-critical, and it will probably do a better job of it
462 than you would have done).
463
464 Another measure of the function is the number of local variables. They
465 shouldn't exceed 5-10, or you're doing something wrong. Re-think the
466 function, and split it into smaller pieces. A human brain can
467 generally easily keep track of about 7 different things, anything more
468 and it gets confused. You know you're brilliant, but maybe you'd like
469 to understand what you did 2 weeks from now.
470
471 In source files, separate functions with one blank line. If the function is
472 exported, the **EXPORT** macro for it should follow immediately after the
473 closing function brace line. E.g.:
474
475 .. code-block:: c
476
477 int system_is_up(void)
478 {
479 return system_state == SYSTEM_RUNNING;
480 }
481 EXPORT_SYMBOL(system_is_up);
482
483 6.1) Function prototypes
484 ************************
485
486 In function prototypes, include parameter names with their data types.
487 Although this is not required by the C language, it is preferred in Linux
488 because it is a simple way to add valuable information for the reader.
489
490 Do not use the ``extern`` keyword with function declarations as this makes
491 lines longer and isn't strictly necessary.
492
493 When writing function prototypes, please keep the `order of elements regular
494 <https://lore.kernel.org/mm-commits/CAHk-=wiOCLRny5aifWNhr621kYrJwhfURsa0vFPeUEm8mF0ufg@mail.gmail.com/>`_.
495 For example, using this function declaration example::
496
497 __init void * __must_check action(enum magic value, size_t size, u8 count,
498 char *fmt, ...) __printf(4, 5) __malloc;
499
500 The preferred order of elements for a function prototype is:
501
502 - storage class (below, ``static __always_inline``, noting that ``__always_inline``
503 is technically an attribute but is treated like ``inline``)
504 - storage class attributes (here, ``__init`` -- i.e. section declarations, but also
505 things like ``__cold``)
506 - return type (here, ``void *``)
507 - return type attributes (here, ``__must_check``)
508 - function name (here, ``action``)
509 - function parameters (here, ``(enum magic value, size_t size, u8 count, char *fmt, ...)``,
510 noting that parameter names should always be included)
511 - function parameter attributes (here, ``__printf(4, 5)``)
512 - function behavior attributes (here, ``__malloc``)
513
514 Note that for a function **definition** (i.e. the actual function body),
515 the compiler does not allow function parameter attributes after the
516 function parameters. In these cases, they should go after the storage
517 class attributes (e.g. note the changed position of ``__printf(4, 5)``
518 below, compared to the **declaration** example above)::
519
520 static __always_inline __init __printf(4, 5) void * __must_check action(enum magic value,
521 size_t size, u8 count, char *fmt, ...) __malloc
522 {
523 ...
524 }
525
526 7) Centralized exiting of functions
527 -----------------------------------
528
529 Albeit deprecated by some people, the equivalent of the goto statement is
530 used frequently by compilers in form of the unconditional jump instruction.
531
532 The goto statement comes in handy when a function exits from multiple
533 locations and some common work such as cleanup has to be done. If there is no
534 cleanup needed then just return directly.
535
536 Choose label names which say what the goto does or why the goto exists. An
537 example of a good name could be ``out_free_buffer:`` if the goto frees ``buffer``.
538 Avoid using GW-BASIC names like ``err1:`` and ``err2:``, as you would have to
539 renumber them if you ever add or remove exit paths, and they make correctness
540 difficult to verify anyway.
541
542 The rationale for using gotos is:
543
544 - unconditional statements are easier to understand and follow
545 - nesting is reduced
546 - errors by not updating individual exit points when making
547 modifications are prevented
548 - saves the compiler work to optimize redundant code away ;)
549
550 .. code-block:: c
551
552 int fun(int a)
553 {
554 int result = 0;
555 char *buffer;
556
557 buffer = kmalloc(SIZE, GFP_KERNEL);
558 if (!buffer)
559 return -ENOMEM;
560
561 if (condition1) {
562 while (loop1) {
563 ...
564 }
565 result = 1;
566 goto out_free_buffer;
567 }
568 ...
569 out_free_buffer:
570 kfree(buffer);
571 return result;
572 }
573
574 A common type of bug to be aware of is ``one err bugs`` which look like this:
575
576 .. code-block:: c
577
578 err:
579 kfree(foo->bar);
580 kfree(foo);
581 return ret;
582
583 The bug in this code is that on some exit paths ``foo`` is NULL. Normally the
584 fix for this is to split it up into two error labels ``err_free_bar:`` and
585 ``err_free_foo:``:
586
587 .. code-block:: c
588
589 err_free_bar:
590 kfree(foo->bar);
591 err_free_foo:
592 kfree(foo);
593 return ret;
594
595 Ideally you should simulate errors to test all exit paths.
596
597
598 8) Commenting
599 -------------
600
601 Comments are good, but there is also a danger of over-commenting. NEVER
602 try to explain HOW your code works in a comment: it's much better to
603 write the code so that the **working** is obvious, and it's a waste of
604 time to explain badly written code.
605
606 Generally, you want your comments to tell WHAT your code does, not HOW.
607 Also, try to avoid putting comments inside a function body: if the
608 function is so complex that you need to separately comment parts of it,
609 you should probably go back to chapter 6 for a while. You can make
610 small comments to note or warn about something particularly clever (or
611 ugly), but try to avoid excess. Instead, put the comments at the head
612 of the function, telling people what it does, and possibly WHY it does
613 it.
614
615 When commenting the kernel API functions, please use the kernel-doc format.
616 See the files at :ref:`Documentation/doc-guide/ <doc_guide>` and
617 ``scripts/kernel-doc`` for details. Note that the danger of over-commenting
618 applies to kernel-doc comments all the same. Do not add boilerplate
619 kernel-doc which simply reiterates what's obvious from the signature
620 of the function.
621
622 The preferred style for long (multi-line) comments is:
623
624 .. code-block:: c
625
626 /*
627 * This is the preferred style for multi-line
628 * comments in the Linux kernel source code.
629 * Please use it consistently.
630 *
631 * Description: A column of asterisks on the left side,
632 * with beginning and ending almost-blank lines.
633 */
634
635 It's also important to comment data, whether they are basic types or derived
636 types. To this end, use just one data declaration per line (no commas for
637 multiple data declarations). This leaves you room for a small comment on each
638 item, explaining its use.
639
640
641 9) You've made a mess of it
642 ---------------------------
643
644 That's OK, we all do. You've probably been told by your long-time Unix
645 user helper that ``GNU emacs`` automatically formats the C sources for
646 you, and you've noticed that yes, it does do that, but the defaults it
647 uses are less than desirable (in fact, they are worse than random
648 typing - an infinite number of monkeys typing into GNU emacs would never
649 make a good program).
650
651 So, you can either get rid of GNU emacs, or change it to use saner
652 values. To do the latter, you can stick the following in your .emacs file:
653
654 .. code-block:: elisp
655
656 (defun c-lineup-arglist-tabs-only (ignored)
657 "Line up argument lists by tabs, not spaces"
658 (let* ((anchor (c-langelem-pos c-syntactic-element))
659 (column (c-langelem-2nd-pos c-syntactic-element))
660 (offset (- (1+ column) anchor))
661 (steps (floor offset c-basic-offset)))
662 (* (max steps 1)
663 c-basic-offset)))
664
665 (dir-locals-set-class-variables
666 'linux-kernel
667 '((c-mode . (
668 (c-basic-offset . 8)
669 (c-label-minimum-indentation . 0)
670 (c-offsets-alist . (
671 (arglist-close . c-lineup-arglist-tabs-only)
672 (arglist-cont-nonempty .
673 (c-lineup-gcc-asm-reg c-lineup-arglist-tabs-only))
674 (arglist-intro . +)
675 (brace-list-intro . +)
676 (c . c-lineup-C-comments)
677 (case-label . 0)
678 (comment-intro . c-lineup-comment)
679 (cpp-define-intro . +)
680 (cpp-macro . -1000)
681 (cpp-macro-cont . +)
682 (defun-block-intro . +)
683 (else-clause . 0)
684 (func-decl-cont . +)
685 (inclass . +)
686 (inher-cont . c-lineup-multi-inher)
687 (knr-argdecl-intro . 0)
688 (label . -1000)
689 (statement . 0)
690 (statement-block-intro . +)
691 (statement-case-intro . +)
692 (statement-cont . +)
693 (substatement . +)
694 ))
695 (indent-tabs-mode . t)
696 (show-trailing-whitespace . t)
697 ))))
698
699 (dir-locals-set-directory-class
700 (expand-file-name "~/src/linux-trees")
701 'linux-kernel)
702
703 This will make emacs go better with the kernel coding style for C
704 files below ``~/src/linux-trees``.
705
706 But even if you fail in getting emacs to do sane formatting, not
707 everything is lost: use ``indent``.
708
709 Now, again, GNU indent has the same brain-dead settings that GNU emacs
710 has, which is why you need to give it a few command line options.
711 However, that's not too bad, because even the makers of GNU indent
712 recognize the authority of K&R (the GNU people aren't evil, they are
713 just severely misguided in this matter), so you just give indent the
714 options ``-kr -i8`` (stands for ``K&R, 8 character indents``), or use
715 ``scripts/Lindent``, which indents in the latest style.
716
717 ``indent`` has a lot of options, and especially when it comes to comment
718 re-formatting you may want to take a look at the man page. But
719 remember: ``indent`` is not a fix for bad programming.
720
721 Note that you can also use the ``clang-format`` tool to help you with
722 these rules, to quickly re-format parts of your code automatically,
723 and to review full files in order to spot coding style mistakes,
724 typos and possible improvements. It is also handy for sorting ``#includes``,
725 for aligning variables/macros, for reflowing text and other similar tasks.
726 See the file :ref:`Documentation/dev-tools/clang-format.rst <clangformat>`
727 for more details.
728
729 Some basic editor settings, such as indentation and line endings, will be
730 set automatically if you are using an editor that is compatible with
731 EditorConfig. See the official EditorConfig website for more information:
732 https://editorconfig.org/
733
734 10) Kconfig configuration files
735 -------------------------------
736
737 For all of the Kconfig* configuration files throughout the source tree,
738 the indentation is somewhat different. Lines under a ``config`` definition
739 are indented with one tab, while help text is indented an additional two
740 spaces. Example::
741
742 config AUDIT
743 bool "Auditing support"
744 depends on NET
745 help
746 Enable auditing infrastructure that can be used with another
747 kernel subsystem, such as SELinux (which requires this for
748 logging of avc messages output). Does not do system-call
749 auditing without CONFIG_AUDITSYSCALL.
750
751 Seriously dangerous features (such as write support for certain
752 filesystems) should advertise this prominently in their prompt string::
753
754 config ADFS_FS_RW
755 bool "ADFS write support (DANGEROUS)"
756 depends on ADFS_FS
757 ...
758
759 For full documentation on the configuration files, see the file
760 Documentation/kbuild/kconfig-language.rst.
761
762
763 11) Data structures
764 -------------------
765
766 Data structures that have visibility outside the single-threaded
767 environment they are created and destroyed in should always have
768 reference counts. In the kernel, garbage collection doesn't exist (and
769 outside the kernel garbage collection is slow and inefficient), which
770 means that you absolutely **have** to reference count all your uses.
771
772 Reference counting means that you can avoid locking, and allows multiple
773 users to have access to the data structure in parallel - and not having
774 to worry about the structure suddenly going away from under them just
775 because they slept or did something else for a while.
776
777 Note that locking is **not** a replacement for reference counting.
778 Locking is used to keep data structures coherent, while reference
779 counting is a memory management technique. Usually both are needed, and
780 they are not to be confused with each other.
781
782 Many data structures can indeed have two levels of reference counting,
783 when there are users of different ``classes``. The subclass count counts
784 the number of subclass users, and decrements the global count just once
785 when the subclass count goes to zero.
786
787 Examples of this kind of ``multi-level-reference-counting`` can be found in
788 memory management (``struct mm_struct``: mm_users and mm_count), and in
789 filesystem code (``struct super_block``: s_count and s_active).
790
791 Remember: if another thread can find your data structure, and you don't
792 have a reference count on it, you almost certainly have a bug.
793
794
795 12) Macros, Enums and RTL
796 -------------------------
797
798 Names of macros defining constants and labels in enums are capitalized.
799
800 .. code-block:: c
801
802 #define CONSTANT 0x12345
803
804 Enums are preferred when defining several related constants.
805
806 CAPITALIZED macro names are appreciated but macros resembling functions
807 may be named in lower case.
808
809 Generally, inline functions are preferable to macros resembling functions.
810
811 Macros with multiple statements should be enclosed in a do - while block:
812
813 .. code-block:: c
814
815 #define macrofun(a, b, c) \
816 do { \
817 if (a == 5) \
818 do_this(b, c); \
819 } while (0)
820
821 Function-like macros with unused parameters should be replaced by static
822 inline functions to avoid the issue of unused variables:
823
824 .. code-block:: c
825
826 static inline void fun(struct foo *foo)
827 {
828 }
829
830 Due to historical practices, many files still employ the "cast to (void)"
831 approach to evaluate parameters. However, this method is not advisable.
832 Inline functions address the issue of "expression with side effects
833 evaluated more than once", circumvent unused-variable problems, and
834 are generally better documented than macros for some reason.
835
836 .. code-block:: c
837
838 /*
839 * Avoid doing this whenever possible and instead opt for static
840 * inline functions
841 */
842 #define macrofun(foo) do { (void) (foo); } while (0)
843
844 Things to avoid when using macros:
845
846 1) macros that affect control flow:
847
848 .. code-block:: c
849
850 #define FOO(x) \
851 do { \
852 if (blah(x) < 0) \
853 return -EBUGGERED; \
854 } while (0)
855
856 is a **very** bad idea. It looks like a function call but exits the ``calling``
857 function; don't break the internal parsers of those who will read the code.
858
859 2) macros that depend on having a local variable with a magic name:
860
861 .. code-block:: c
862
863 #define FOO(val) bar(index, val)
864
865 might look like a good thing, but it's confusing as hell when one reads the
866 code and it's prone to breakage from seemingly innocent changes.
867
868 3) macros with arguments that are used as l-values: FOO(x) = y; will
869 bite you if somebody e.g. turns FOO into an inline function.
870
871 4) forgetting about precedence: macros defining constants using expressions
872 must enclose the expression in parentheses. Beware of similar issues with
873 macros using parameters.
874
875 .. code-block:: c
876
877 #define CONSTANT 0x4000
878 #define CONSTEXP (CONSTANT | 3)
879
880 5) namespace collisions when defining local variables in macros resembling
881 functions:
882
883 .. code-block:: c
884
885 #define FOO(x) \
886 ({ \
887 typeof(x) ret; \
888 ret = calc_ret(x); \
889 (ret); \
890 })
891
892 ret is a common name for a local variable - __foo_ret is less likely
893 to collide with an existing variable.
894
895 The cpp manual deals with macros exhaustively. The gcc internals manual also
896 covers RTL which is used frequently with assembly language in the kernel.
897
898
899 13) Printing kernel messages
900 ----------------------------
901
902 Kernel developers like to be seen as literate. Do mind the spelling
903 of kernel messages to make a good impression. Do not use incorrect
904 contractions like ``dont``; use ``do not`` or ``don't`` instead. Make the
905 messages concise, clear, and unambiguous.
906
907 Kernel messages do not have to be terminated with a period.
908
909 Printing numbers in parentheses (%d) adds no value and should be avoided.
910
911 There are a number of driver model diagnostic macros in <linux/dev_printk.h>
912 which you should use to make sure messages are matched to the right device
913 and driver, and are tagged with the right level: dev_err(), dev_warn(),
914 dev_info(), and so forth. For messages that aren't associated with a
915 particular device, <linux/printk.h> defines pr_notice(), pr_info(),
916 pr_warn(), pr_err(), etc. When drivers are working properly they are quiet,
917 so prefer to use dev_dbg/pr_debug unless something is wrong.
918
919 Coming up with good debugging messages can be quite a challenge; and once
920 you have them, they can be a huge help for remote troubleshooting. However
921 debug message printing is handled differently than printing other non-debug
922 messages. While the other pr_XXX() functions print unconditionally,
923 pr_debug() does not; it is compiled out by default, unless either DEBUG is
924 defined or CONFIG_DYNAMIC_DEBUG is set. That is true for dev_dbg() also,
925 and a related convention uses VERBOSE_DEBUG to add dev_vdbg() messages to
926 the ones already enabled by DEBUG.
927
928 Many subsystems have Kconfig debug options to turn on -DDEBUG in the
929 corresponding Makefile; in other cases specific files #define DEBUG. And
930 when a debug message should be unconditionally printed, such as if it is
931 already inside a debug-related #ifdef section, printk(KERN_DEBUG ...) can be
932 used.
933
934
935 14) Allocating memory
936 ---------------------
937
938 The kernel provides the following general purpose memory allocators:
939 kmalloc(), kzalloc(), kmalloc_array(), kcalloc(), vmalloc(), and
940 vzalloc(). Please refer to the API documentation for further information
941 about them. :ref:`Documentation/core-api/memory-allocation.rst
942 <memory_allocation>`
943
944 The preferred form for passing a size of a struct is the following:
945
946 .. code-block:: c
947
948 p = kmalloc(sizeof(*p), ...);
949
950 The alternative form where struct name is spelled out hurts readability and
951 introduces an opportunity for a bug when the pointer variable type is changed
952 but the corresponding sizeof that is passed to a memory allocator is not.
953
954 Casting the return value which is a void pointer is redundant. The conversion
955 from void pointer to any other pointer type is guaranteed by the C programming
956 language.
957
958 The preferred form for allocating an array is the following:
959
960 .. code-block:: c
961
962 p = kmalloc_array(n, sizeof(...), ...);
963
964 The preferred form for allocating a zeroed array is the following:
965
966 .. code-block:: c
967
968 p = kcalloc(n, sizeof(...), ...);
969
970 Both forms check for overflow on the allocation size n * sizeof(...),
971 and return NULL if that occurred.
972
973 These generic allocation functions all emit a stack dump on failure when used
974 without __GFP_NOWARN so there is no use in emitting an additional failure
975 message when NULL is returned.
976
977 15) The inline disease
978 ----------------------
979
980 There appears to be a common misperception that gcc has a magic "make me
981 faster" speedup option called ``inline``. While the use of inlines can be
982 appropriate (for example as a means of replacing macros, see Chapter 12), it
983 very often is not. Abundant use of the inline keyword leads to a much bigger
984 kernel, which in turn slows the system as a whole down, due to a bigger
985 icache footprint for the CPU and simply because there is less memory
986 available for the pagecache. Just think about it; a pagecache miss causes a
987 disk seek, which easily takes 5 milliseconds. There are a LOT of cpu cycles
988 that can go into these 5 milliseconds.
989
990 A reasonable rule of thumb is to not put inline at functions that have more
991 than 3 lines of code in them. An exception to this rule are the cases where
992 a parameter is known to be a compile time constant, and as a result of this
993 constantness you *know* the compiler will be able to optimize most of your
994 function away at compile time. For a good example of this later case, see
995 the kmalloc() inline function.
996
997 Often people argue that adding inline to functions that are static and used
998 only once is always a win since there is no space tradeoff. While this is
999 technically correct, gcc is capable of inlining these automatically without
1000 help, and the maintenance issue of removing the inline when a second user
1001 appears outweighs the potential value of the hint that tells gcc to do
1002 something it would have done anyway.
1005 16) Function return values and names
1006 ------------------------------------
1008 Functions can return values of many different kinds, and one of the
1009 most common is a value indicating whether the function succeeded or
1010 failed. Such a value can be represented as an error-code integer
1011 (-Exxx = failure, 0 = success) or a ``succeeded`` boolean (0 = failure,
1012 non-zero = success).
1014 Mixing up these two sorts of representations is a fertile source of
1015 difficult-to-find bugs. If the C language included a strong distinction
1016 between integers and booleans then the compiler would find these mistakes
1017 for us... but it doesn't. To help prevent such bugs, always follow this
1018 convention::
1020 If the name of a function is an action or an imperative command,
1021 the function should return an error-code integer. If the name
1022 is a predicate, the function should return a "succeeded" boolean.
1024 For example, ``add work`` is a command, and the add_work() function returns 0
1025 for success or -EBUSY for failure. In the same way, ``PCI device present`` is
1026 a predicate, and the pci_dev_present() function returns 1 if it succeeds in
1027 finding a matching device or 0 if it doesn't.
1029 All EXPORTed functions must respect this convention, and so should all
1030 public functions. Private (static) functions need not, but it is
1031 recommended that they do.
1033 Functions whose return value is the actual result of a computation, rather
1034 than an indication of whether the computation succeeded, are not subject to
1035 this rule. Generally they indicate failure by returning some out-of-range
1036 result. Typical examples would be functions that return pointers; they use
1037 NULL or the ERR_PTR mechanism to report failure.
1040 17) Using bool
1041 --------------
1043 The Linux kernel bool type is an alias for the C99 _Bool type. bool values can
1044 only evaluate to 0 or 1, and implicit or explicit conversion to bool
1045 automatically converts the value to true or false. When using bool types the
1046 !! construction is not needed, which eliminates a class of bugs.
1048 When working with bool values the true and false definitions should be used
1049 instead of 1 and 0.
1051 bool function return types and stack variables are always fine to use whenever
1052 appropriate. Use of bool is encouraged to improve readability and is often a
1053 better option than 'int' for storing boolean values.
1055 Do not use bool if cache line layout or size of the value matters, as its size
1056 and alignment varies based on the compiled architecture. Structures that are
1057 optimized for alignment and size should not use bool.
1059 If a structure has many true/false values, consider consolidating them into a
1060 bitfield with 1 bit members, or using an appropriate fixed width type, such as
1061 u8.
1063 Similarly for function arguments, many true/false values can be consolidated
1064 into a single bitwise 'flags' argument and 'flags' can often be a more
1065 readable alternative if the call-sites have naked true/false constants.
1067 Otherwise limited use of bool in structures and arguments can improve
1068 readability.
1070 18) Don't re-invent the kernel macros
1071 -------------------------------------
1073 The header file include/linux/kernel.h contains a number of macros that
1074 you should use, rather than explicitly coding some variant of them yourself.
1075 For example, if you need to calculate the length of an array, take advantage
1076 of the macro
1078 .. code-block:: c
1080 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
1082 Similarly, if you need to calculate the size of some structure member, use
1084 .. code-block:: c
1086 #define sizeof_field(t, f) (sizeof(((t*)0)->f))
1088 There are also min() and max() macros that do strict type checking if you
1089 need them. Feel free to peruse that header file to see what else is already
1090 defined that you shouldn't reproduce in your code.
1093 19) Editor modelines and other cruft
1094 ------------------------------------
1096 Some editors can interpret configuration information embedded in source files,
1097 indicated with special markers. For example, emacs interprets lines marked
1098 like this:
1100 .. code-block:: c
1102 -*- mode: c -*-
1104 Or like this:
1106 .. code-block:: c
1108 /*
1109 Local Variables:
1110 compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c"
1111 End:
1112 */
1114 Vim interprets markers that look like this:
1116 .. code-block:: c
1118 /* vim:set sw=8 noet */
1120 Do not include any of these in source files. People have their own personal
1121 editor configurations, and your source files should not override them. This
1122 includes markers for indentation and mode configuration. People may use their
1123 own custom mode, or may have some other magic method for making indentation
1124 work correctly.
1127 20) Inline assembly
1128 -------------------
1130 In architecture-specific code, you may need to use inline assembly to interface
1131 with CPU or platform functionality. Don't hesitate to do so when necessary.
1132 However, don't use inline assembly gratuitously when C can do the job. You can
1133 and should poke hardware from C when possible.
1135 Consider writing simple helper functions that wrap common bits of inline
1136 assembly, rather than repeatedly writing them with slight variations. Remember
1137 that inline assembly can use C parameters.
1139 Large, non-trivial assembly functions should go in .S files, with corresponding
1140 C prototypes defined in C header files. The C prototypes for assembly
1141 functions should use ``asmlinkage``.
1143 You may need to mark your asm statement as volatile, to prevent GCC from
1144 removing it if GCC doesn't notice any side effects. You don't always need to
1145 do so, though, and doing so unnecessarily can limit optimization.
1147 When writing a single inline assembly statement containing multiple
1148 instructions, put each instruction on a separate line in a separate quoted
1149 string, and end each string except the last with ``\n\t`` to properly indent
1150 the next instruction in the assembly output:
1152 .. code-block:: c
1154 asm ("magic %reg1, #42\n\t"
1155 "more_magic %reg2, %reg3"
1156 : /* outputs */ : /* inputs */ : /* clobbers */);
1159 21) Conditional Compilation
1160 ---------------------------
1162 Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c
1163 files; doing so makes code harder to read and logic harder to follow. Instead,
1164 use such conditionals in a header file defining functions for use in those .c
1165 files, providing no-op stub versions in the #else case, and then call those
1166 functions unconditionally from .c files. The compiler will avoid generating
1167 any code for the stub calls, producing identical results, but the logic will
1168 remain easy to follow.
1170 Prefer to compile out entire functions, rather than portions of functions or
1171 portions of expressions. Rather than putting an ifdef in an expression, factor
1172 out part or all of the expression into a separate helper function and apply the
1173 conditional to that function.
1175 If you have a function or variable which may potentially go unused in a
1176 particular configuration, and the compiler would warn about its definition
1177 going unused, mark the definition as __maybe_unused rather than wrapping it in
1178 a preprocessor conditional. (However, if a function or variable *always* goes
1179 unused, delete it.)
1181 Within code, where possible, use the IS_ENABLED macro to convert a Kconfig
1182 symbol into a C boolean expression, and use it in a normal C conditional:
1184 .. code-block:: c
1186 if (IS_ENABLED(CONFIG_SOMETHING)) {
1187 ...
1188 }
1190 The compiler will constant-fold the conditional away, and include or exclude
1191 the block of code just as with an #ifdef, so this will not add any runtime
1192 overhead. However, this approach still allows the C compiler to see the code
1193 inside the block, and check it for correctness (syntax, types, symbol
1194 references, etc). Thus, you still have to use an #ifdef if the code inside the
1195 block references symbols that will not exist if the condition is not met.
1197 At the end of any non-trivial #if or #ifdef block (more than a few lines),
1198 place a comment after the #endif on the same line, noting the conditional
1199 expression used. For instance:
1201 .. code-block:: c
1203 #ifdef CONFIG_SOMETHING
1204 ...
1205 #endif /* CONFIG_SOMETHING */
1208 22) Do not crash the kernel
1209 ---------------------------
1211 In general, the decision to crash the kernel belongs to the user, rather
1212 than to the kernel developer.
1214 Avoid panic()
1215 *************
1217 panic() should be used with care and primarily only during system boot.
1218 panic() is, for example, acceptable when running out of memory during boot and
1219 not being able to continue.
1221 Use WARN() rather than BUG()
1222 ****************************
1224 Do not add new code that uses any of the BUG() variants, such as BUG(),
1225 BUG_ON(), or VM_BUG_ON(). Instead, use a WARN*() variant, preferably
1226 WARN_ON_ONCE(), and possibly with recovery code. Recovery code is not
1227 required if there is no reasonable way to at least partially recover.
1229 "I'm too lazy to do error handling" is not an excuse for using BUG(). Major
1230 internal corruptions with no way of continuing may still use BUG(), but need
1231 good justification.
1233 Use WARN_ON_ONCE() rather than WARN() or WARN_ON()
1234 **************************************************
1236 WARN_ON_ONCE() is generally preferred over WARN() or WARN_ON(), because it
1237 is common for a given warning condition, if it occurs at all, to occur
1238 multiple times. This can fill up and wrap the kernel log, and can even slow
1239 the system enough that the excessive logging turns into its own, additional
1240 problem.
1242 Do not WARN lightly
1243 *******************
1245 WARN*() is intended for unexpected, this-should-never-happen situations.
1246 WARN*() macros are not to be used for anything that is expected to happen
1247 during normal operation. These are not pre- or post-condition asserts, for
1248 example. Again: WARN*() must not be used for a condition that is expected
1249 to trigger easily, for example, by user space actions. pr_warn_once() is a
1250 possible alternative, if you need to notify the user of a problem.
1252 Do not worry about panic_on_warn users
1253 **************************************
1255 A few more words about panic_on_warn: Remember that ``panic_on_warn`` is an
1256 available kernel option, and that many users set this option. This is why
1257 there is a "Do not WARN lightly" writeup, above. However, the existence of
1258 panic_on_warn users is not a valid reason to avoid the judicious use
1259 WARN*(). That is because, whoever enables panic_on_warn has explicitly
1260 asked the kernel to crash if a WARN*() fires, and such users must be
1261 prepared to deal with the consequences of a system that is somewhat more
1262 likely to crash.
1264 Use BUILD_BUG_ON() for compile-time assertions
1265 **********************************************
1267 The use of BUILD_BUG_ON() is acceptable and encouraged, because it is a
1268 compile-time assertion that has no effect at runtime.
1270 Appendix I) References
1271 ----------------------
1273 The C Programming Language, Second Edition
1274 by Brian W. Kernighan and Dennis M. Ritchie.
1275 Prentice Hall, Inc., 1988.
1276 ISBN 0-13-110362-8 (paperback), 0-13-110370-9 (hardback).
1278 The Practice of Programming
1279 by Brian W. Kernighan and Rob Pike.
1280 Addison-Wesley, Inc., 1999.
1281 ISBN 0-201-61586-X.
1283 GNU manuals - where in compliance with K&R and this text - for cpp, gcc,
1284 gcc internals and indent, all available from https://www.gnu.org/manual/
1286 WG14 is the international standardization working group for the programming
1287 language C, URL: http://www.open-std.org/JTC1/SC22/WG14/
1289 Kernel CodingStyle, by greg@kroah.com at OLS 2002:
1290 http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

문서의 목적과 들여쓰기

1-97

이 문서는 Linux 커널에서 선호하는 코딩 형식을 간결하게 설명한다. 코딩 스타일은 개인 취향의 문제가 될 수 있지만, 커널은 여러 사람이 오랫동안 함께 수정하는 코드이므로 하나의 일관된 형식을 사용해야 한다. GNU 코딩 표준을 읽고 혼란스러웠다면 그 내용을 잊고 이 문서를 따르라는 도입부의 농담도 원문의 어조 그대로 이해하면 된다.

들여쓰기는 탭 문자이며 탭 너비는 8자다. 8자는 우연히 정한 값이 아니다. 조건문과 반복문이 여러 단계 겹치면 코드가 화면 오른쪽으로 빠르게 밀려나므로, 함수가 지나치게 복잡해졌다는 사실이 눈에 띈다. 들여쓰기가 세 단계보다 깊어졌다면 하위 동작을 별도 함수로 나누는 편이 낫다.

switch 문의 case 레이블은 switch와 같은 열에 둔다. 논리적으로는 case가 switch 내부에 있지만, 8자 탭을 사용하는 환경에서는 이 배치가 각 분기와 분기 본문을 가장 분명하게 구별한다.

switch (suffix) {
case 'G':
case 'g':
	mem <<= 30;
	break;
case 'M':
case 'm':
	mem <<= 20;
	break;
case 'K':
case 'k':
	mem <<= 10;
	fallthrough;
default:
	break;
}

한 줄에 문장을 두 개 이상 쓰지 않는다. 아래 첫 예제는 do_this()만 조건에 포함되고 다음 문장은 항상 실행되지만, 화면에서는 둘 다 조건에 속한 것처럼 보인다. 두 번째 예제처럼 콤마 연산자로 중괄호를 피하는 방식도 읽는 사람에게 실행 경계를 숨긴다.

/* 잘못된 예 */
if (condition) do_this;
  do_something_everytime;

/* 이 방법으로 중괄호를 피하지 않는다. */
if (condition)
	do_this(), do_that();

/* 여러 문장은 명시적인 블록으로 묶는다. */
if (condition) {
	do_this();
	do_that();
}

복잡한 표현식과 여러 대입을 한 줄에 압축하지 않는다. 컴파일러가 이해할 수 있다는 사실보다 사람이 코드를 즉시 검토할 수 있는지가 중요하다. 주석, 문서, Kconfig처럼 탭이 적절하지 않은 영역을 제외하면 들여쓰기에 공백을 사용하지 않으며, 줄 끝 공백도 남기지 않는다.

긴 줄과 문자열 나누기

98-119

코딩 스타일의 목적은 읽기 쉽고 유지보수하기 쉬운 코드를 만드는 것이다. 한 줄은 가급적 80열을 넘기지 않는다. 다만 줄을 억지로 나누어 의미 단위가 깨지거나 검색과 이해가 오히려 어려워진다면, 약간 긴 줄을 허용하는 편이 낫다.

줄을 나눌 때 이어지는 부분은 원래 줄보다 짧고 오른쪽에 놓여야 한다. 함수 인수는 여는 괄호 다음 위치에 맞추어 정렬한다. 이 원칙은 C 소스뿐 아니라 헤더 파일에도 적용한다.

printk 계열이 출력하는 사용자 가시 문자열은 여러 줄의 문자열 리터럴로 쪼개지 않는다. 커널 로그의 정확한 문구를 grep으로 찾는 일이 많기 때문에, 소스에서 검색 가능한 하나의 문자열로 유지해야 한다.

중괄호와 공백

120-305

커널은 K&R 형식을 따른다. 함수가 아닌 블록의 여는 중괄호는 제어문의 마지막에 두고, 닫는 중괄호는 그 블록을 시작한 문장과 같은 들여쓰기 열의 첫 위치에 둔다.

if (x is true) {
	we do y
}

switch (action) {
case KOBJ_ADD:
	return "add";
case KOBJ_REMOVE:
	return "remove";
case KOBJ_CHANGE:
	return "change";
default:
	return NULL;
}

함수 정의는 예외다. 함수의 여는 중괄호는 함수 이름 다음 줄의 첫 열에 둔다. 이 규칙은 K&R의 오랜 관례다.

int function(int x)
{
	body of function
}

닫는 중괄호는 보통 한 줄을 단독으로 차지한다. 단, 같은 제어 구문이 계속되는 do-while의 while이나 if문의 else는 닫는 중괄호와 같은 줄에 둔다.

do {
	body of do-loop
} while (condition);

if (x == y) {
	..
} else if (x > y) {
	...
} else {
	....
}

조건문이나 반복문 본문이 정말로 단순한 한 문장이면 중괄호를 생략할 수 있다. if와 else 가운데 어느 한쪽이 여러 문장이라 중괄호가 필요하다면 양쪽 모두에 중괄호를 사용한다. 반복문 안에 조건문처럼 하위 제어 흐름이 들어가면 바깥 반복문에도 중괄호를 사용해 범위를 분명히 한다.

if (condition)
	action();

if (condition)
	do_this();
else
	do_that();

if (condition) {
	do_this();
	do_that();
} else {
	otherwise();
}

while (condition) {
	if (test)
		do_something();
}

키워드 if, switch, case, for, do, while 뒤에는 공백을 둔다. 함수처럼 보이는 sizeof, typeof, alignof, __attribute__ 뒤에는 공백을 두지 않는다. 괄호 안쪽에는 공백을 넣지 않는다. 따라서 sizeof(struct file)은 맞고 sizeof( struct file )은 틀리다.

포인터 선언의 별표는 자료형이 아니라 변수명이나 함수명 쪽에 붙인다. char *linux_banner, char *match_strdup(...) 형식을 사용한다. =, +, -, <, >, *, /, %, |, &, ^, <=, >=, ==, !=, ?와 : 같은 이항·삼항 연산자 양쪽에는 공백을 둔다.

단항 &, *, +, -, ~, !와 sizeof, typeof, alignof, __attribute__, defined 뒤에는 공백을 두지 않는다. 전위·후위 ++와 -- 주위에도 공백을 두지 않으며, 구조체 멤버 연산자 .와 -> 주위에도 공백을 두지 않는다.

줄 끝 공백은 대부분의 편집기에서 보이지 않지만 패치를 불필요하게 오염시키고 이후 변경의 문맥 일치를 깨뜨린다. 일부 Git 설정은 자동으로 제거할 수 있으나, 이미 존재하는 줄 끝 공백을 기계적으로 모두 고치면 관련 없는 변경이 커질 수 있으므로 주의한다.

이름 짓기

306-358

C는 이름을 짓는 언어다. 지역 변수는 짧고 용도가 즉시 드러나는 이름을 쓴다. 반복 횟수에는 i, 임시 값에는 tmp처럼 관례가 분명한 이름이 긴 CamelCase 이름보다 낫다. 혼합 대소문자 이름은 커널에서 권장하지 않는다.

반대로 전역 함수와 전역 변수는 무엇을 하는지 설명할 수 있는 이름이 필요하다. 전역 심볼 자체도 꼭 필요한 경우에만 만든다. 자료형을 이름에 반복하는 헝가리안 표기법은 컴파일러가 이미 알고 있는 정보를 중복하므로 사용하지 않는다.

새 코드에서는 master/slave와 blacklist/whitelist 용어를 피한다. 문맥에 따라 아래와 같이 역할을 실제로 설명하는 조합을 선택한다.

피할 표현권장 대안
master / slaveprimary 또는 main / secondary, replica 또는 subordinate
master / slaveinitiator 또는 requester / target 또는 responder
master / slavecontroller 또는 host / device, worker 또는 proxy
master / slaveleader / follower, director / performer
blacklist / whitelistdenylist / allowlist, blocklist / passlist

사용자 공간 ABI/API를 유지해야 하거나, 2020년 기준으로 이미 공개된 하드웨어·프로토콜 규격이 특정 용어를 의무화한 경우에는 예외가 될 수 있다. 이때도 새 내부 이름까지 무비판적으로 같은 표현으로 확장하지 않는다.

typedef 사용 기준

359-442

구조체나 포인터를 감추기 위해 typedef를 습관적으로 사용하지 않는다. vps_t a보다 struct virtual_container *a가 객체의 종류와 포인터 여부를 코드에 직접 드러낸다. 구조체 태그를 숨기면 선언만 보고 실제 자료 구조를 알기 어렵고, 포인터 typedef는 값과 참조의 차이까지 감춘다.

/* 피해야 할 형태 */
vps_t a;

/* 구조를 명시하는 형태 */
struct virtual_container *a;

typedef가 정당한 첫 번째 경우는 pte_t처럼 내용을 이식 가능한 방법으로 직접 해석할 수 없고 반드시 접근자 함수를 거쳐야 하는 완전히 불투명한 객체다. 그러나 불투명화와 접근자 함수 자체가 좋은 설계라는 뜻은 아니다. 페이지 테이블 엔트리는 아키텍처마다 표현이 달라 직접 접근할 공통 정보가 전혀 없기 때문에 예외가 성립한다.

두 번째는 int인지 long인지 혼동하기 쉬운 정수 표현을 추상화하는 경우다. 단순히 unsigned long에 myflags_t라는 새 이름을 붙이는 것은 이유가 없다. 설정에 따라 unsigned int와 unsigned long 사이에서 실제 표현이 달라져야 한다면 typedef가 그 차이를 격리할 수 있다.

typedef unsigned long myflags_t;

세 번째는 sparse가 정적 형 검사를 수행할 수 있도록 실제로 새로운 타입을 만드는 경우다. 네 번째는 u8, u16, u32, u64 및 부호 있는 대응형처럼 표준 C99 정수형과 동일하지만 Linux 코드에서 널리 쓰이는 타입이다. 새 코드에서 반드시 Linux 형을 써야 하는 것은 아니며, 기존 파일을 수정할 때는 그 코드가 이미 선택한 표기를 따른다.

다섯 번째는 사용자 공간과 공유되는 구조체다. 사용자 공간에 C99 타입 사용을 강제할 수 없고 커널 내부 u32를 그대로 노출할 수도 있으므로, UAPI 구조체에는 __u32 같은 타입을 사용한다. 이 예외들에 해당하지 않는다면 포인터나 직접 접근 가능한 구조체에 typedef를 만들지 않는다.

함수의 크기, 선언과 속성 순서

443-525

함수는 짧고 한 가지 작업만 수행해야 한다. 이상적인 함수는 80열 24행 화면 한두 개 안에서 전체를 볼 수 있다. 허용 가능한 길이는 복잡도와 들여쓰기 깊이에 반비례한다. 단순한 switch가 많은 case를 갖는 함수는 길어도 이해할 수 있지만, 중첩된 복잡한 함수는 더 짧아야 한다.

별도 이름으로 설명할 수 있는 복잡한 부분은 보조 함수로 분리한다. 컴파일러는 필요하면 이를 인라인으로 최적화할 수 있고, 사람이 읽고 검토하기도 쉬워진다. 지역 변수도 보통 5개에서 10개를 넘기지 않는 것이 좋다. 변수가 많다면 함수가 너무 많은 상태를 한꺼번에 다루고 있는지 살펴본다.

함수 사이에는 빈 줄 하나를 둔다. EXPORT_SYMBOL 계열 매크로는 함수의 닫는 중괄호 바로 다음 줄에 둔다. 함수 원형에는 인수 이름을 포함해야 하며, 함수 선언에 extern을 붙이지 않는다.

int system_is_up(void)
{
	return system_state == SYSTEM_RUNNING;
}
EXPORT_SYMBOL(system_is_up);

함수 선언 요소는 저장 클래스, 저장 클래스 속성, 반환형, 반환값 속성, 함수 이름, 이름을 포함한 매개변수, 매개변수 속성, 동작 속성 순으로 배치한다. 선언 예는 다음과 같다.

__init void * __must_check action(enum magic value, size_t size, u8 count,
				   char *fmt, ...) __printf(4, 5) __malloc;

저장 클래스에는 static과 extern이, 저장 클래스 속성에는 __init과 __cold가, 반환값 속성에는 __must_check가 해당한다. __printf 같은 매개변수 속성과 __malloc 같은 함수 동작 속성은 뒤쪽에 놓인다. 함수 정의에서는 컴파일러 제약 때문에 매개변수 속성을 저장 클래스 속성 다음으로 이동한다.

static __always_inline __init __printf(4, 5) void * __must_check action(enum magic value,
		size_t size, u8 count, char *fmt, ...) __malloc
{
	...
}

공통 종료 경로와 goto

526-597

goto는 무조건 피해야 하는 문법이 아니다. 함수 중간 여러 지점에서 동일한 자원을 정리해야 한다면 하나의 종료 경로로 모으는 것이 중복과 누락을 줄인다. 정리할 것이 없는 단순 오류라면 바로 return하는 편이 낫다.

레이블은 err1, err2처럼 위치만 나타내지 말고 out_free_buffer처럼 수행할 동작을 설명해야 한다. 공통 종료 경로는 본문 중첩을 줄이고, 정리 코드를 한곳에 모아 수정 시 한 경로만 고쳐도 되게 한다.

int fun(int a)
{
	int result = 0;
	char *buffer;

	buffer = kmalloc(SIZE, GFP_KERNEL);
	if (!buffer)
		return -ENOMEM;

	if (condition1) {
		while (loop1) {
			...
		}
		result = 1;
		goto out_free_buffer;
	}
	...
out_free_buffer:
	kfree(buffer);
	return result;
}

단일 err 레이블에서 foo->bar와 foo를 모두 해제하면 bar 할당 전에 실패한 경로도 초기화되지 않은 bar를 해제할 수 있다. 각 자원이 실제로 확보된 시점에 맞춰 레이블을 나누고, 아래쪽 정리 단계로 자연스럽게 떨어지게 해야 한다.

/* 잘못된 정리 경로 */
err:
	kfree(foo->bar);
	kfree(foo);
	return ret;

/* 할당 단계와 대응하는 정리 경로 */
err_free_bar:
	kfree(foo->bar);
err_free_foo:
	kfree(foo);
	return ret;

종료 경로가 맞는지는 각 할당과 초기화 단계에서 실패를 강제로 주입해 검증한다. 정상 경로만 실행해서는 이중 해제, 미할당 객체 해제, 참조 누수 같은 오류를 찾기 어렵다.

주석

598-640

주석은 코드가 무엇을 하는지 그대로 읽어 주기보다 왜 그렇게 해야 하는지를 설명해야 한다. 복잡한 함수의 각 줄에 주석을 붙이는 대신 함수를 단순화하고, 함수 머리말에서 전체 목적과 제약을 설명한다.

외부에 공개되는 커널 API는 kernel-doc 형식을 사용한다. 코드만 반복하는 상투적인 주석은 피한다. 여러 줄 주석은 왼쪽에 별표 열을 두는 커널 형식을 일관되게 사용한다.

/*
 * This is the preferred style for multi-line
 * comments in the Linux kernel source code.
 * Please use it consistently.
 *
 * Description:  A column of asterisks on the left side,
 * with beginning and ending almost-blank lines.
 */

함수 내부의 동작뿐 아니라 중요한 자료 구조와 필드의 의미도 설명한다. 여러 변수를 한 선언에 묶으면 각 변수 옆에 의미를 적기 어려우므로, 주석이 필요한 데이터는 한 줄에 하나씩 선언한다.

편집기와 자동 정렬 도구

641-733

일부 편집기의 기본 C 들여쓰기는 커널 형식과 맞지 않는다. 아래 Emacs 설정은 탭 기반 인수 정렬, 8자 기본 들여쓰기, case 레이블과 전처리기 매크로 정렬, 줄 끝 공백 표시 등을 Linux 소스 트리용 디렉터리 클래스에 적용하는 원문의 예다.

(defun c-lineup-arglist-tabs-only (ignored)
  "Line up argument lists by tabs, not spaces"
  (let* ((anchor (c-langelem-pos c-syntactic-element))
         (column (c-langelem-2nd-pos c-syntactic-element))
         (offset (- (1+ column) anchor))
         (steps (floor offset c-basic-offset)))
    (* (max steps 1)
       c-basic-offset)))

(dir-locals-set-class-variables
 'linux-kernel
 '((c-mode . (
        (c-basic-offset . 8)
        (c-label-minimum-indentation . 0)
        (c-offsets-alist . (
                (arglist-close         . c-lineup-arglist-tabs-only)
                (arglist-cont-nonempty .
                    (c-lineup-gcc-asm-reg c-lineup-arglist-tabs-only))
                (arglist-intro         . +)
                (brace-list-intro      . +)
                (c                     . c-lineup-C-comments)
                (case-label            . 0)
                (comment-intro         . c-lineup-comment)
                (cpp-define-intro      . +)
                (cpp-macro             . -1000)
                (cpp-macro-cont        . +)
                (defun-block-intro     . +)
                (else-clause           . 0)
                (func-decl-cont        . +)
                (inclass               . +)
                (inher-cont            . c-lineup-multi-inher)
                (knr-argdecl-intro     . 0)
                (label                 . -1000)
                (statement             . 0)
                (statement-block-intro . +)
                (statement-case-intro  . +)
                (statement-cont        . +)
                (substatement          . +)
                ))
        (indent-tabs-mode . t)
        (show-trailing-whitespace . t)
        ))))

(dir-locals-set-directory-class
 (expand-file-name "~/src/linux-trees")
 'linux-kernel)

indent 도구를 쓴다면 -kr -i8 옵션 또는 scripts/Lindent를 사용할 수 있다. 다만 자동 정렬은 나쁜 프로그램 구조를 좋은 코드로 바꾸지 못한다. clang-format은 코드 재정렬, include 순서 정리, 변수 정렬, 텍스트 재배치와 같은 작업을 지원하며 관련 사용법은 Documentation/dev-tools/clang-format.rst에 있다. 여러 편집기에서 공통 설정을 읽게 하려면 EditorConfig도 사용할 수 있다.

Kconfig 파일

734-762

Kconfig의 config 아래 항목은 탭 한 단계로 들여쓰고, help 본문은 탭 다음에 공백 두 칸을 더 둔다. 다음 AUDIT 예제가 기준 형식을 보여 준다.

config AUDIT
	bool "Auditing support"
	depends on NET
	help
	  Enable auditing infrastructure that can be used with another
	  kernel subsystem, such as SELinux (which requires this for
	  logging of avc messages output).  Does not do system-call
	  auditing without CONFIG_AUDITSYSCALL.

데이터 손상처럼 위험한 동작을 허용하는 옵션의 프롬프트에는 DANGEROUS를 명시한다. 사용자는 메뉴만 보고도 위험을 알아야 한다.

config ADFS_FS_RW
	bool "ADFS write support (DANGEROUS)"
	depends on ADFS_FS
	...

자료 구조의 참조 수명

763-794

객체를 만들고 없애는 단일 스레드 문맥 밖에서도 찾을 수 있는 자료 구조라면 참조 계수가 필요하다. 다른 실행 주체가 객체를 발견할 수 있는데 수명 확보 절차가 없다면, 발견 직후 다른 CPU가 객체를 해제하는 use-after-free 경쟁이 생긴다.

잠금과 참조 계수는 서로 대체할 수 없다. 잠금은 객체 내용의 일관성을 보호하고, 참조 계수는 객체 메모리가 계속 존재하도록 보장한다. 객체를 잠그려면 먼저 그 객체를 안전하게 참조할 수 있어야 한다.

복잡한 객체는 두 단계 참조 계수를 사용할 수 있다. mm_struct의 mm_users는 주소 공간을 사용하는 사용자 수를, mm_count는 구조체 자체를 붙잡는 내부 참조를 센다. super_block의 s_count와 s_active도 외부 발견 가능성과 활성 사용을 서로 다른 계층에서 관리한다.

핵심 질문은 '다른 스레드가 이 객체를 어떻게 찾고, 찾은 순간부터 어떤 참조가 해제를 막는가'다. 이 질문에 답할 수 없다면 잠금이 있더라도 객체 수명 설계에는 결함이 있다.

매크로, enum과 RTL

795-898

상수 매크로와 enum 레이블은 대문자로 쓴다. 서로 관련된 상수 집합은 enum으로 묶으면 컴파일러와 디버거가 의미를 더 잘 보존한다. 함수처럼 호출되는 매크로는 소문자 이름을 사용할 수 있지만, 형 검사와 디버깅이 가능한 static inline 함수를 우선한다.

#define CONSTANT 0x12345

여러 문장을 포함하는 매크로는 do { ... } while (0)로 감싼다. 이렇게 해야 호출자가 일반 함수처럼 뒤에 세미콜론을 붙일 수 있고, if-else 안에서도 하나의 문장으로 동작한다.

#define macrofun(a, b, c)			\
	do {					\
		if (a == 5)			\
			do_this(b, c);		\
	} while (0)

사용하지 않는 매개변수를 처리하려고 아무 일도 하지 않는 매크로를 만들기보다 빈 static inline 함수를 사용한다. 컴파일러는 인수의 타입을 검사하고 호출 코드를 제거한다. 아래처럼 인수를 void로 캐스팅하는 매크로는 가급적 피한다.

static inline void fun(struct foo *foo)
{
}

/* 가능하면 피하고 static inline 함수를 사용한다. */
#define macrofun(foo) do { (void) (foo); } while (0)

호출 함수에서 return이나 break를 실행하는 제어 흐름 매크로는 호출부만 읽어서는 흐름을 알 수 없으므로 피한다. 아래 FOO는 평범한 함수 호출처럼 보이지만 호출한 함수 전체에서 반환한다.

#define FOO(x)					\
	do {					\
		if (blah(x) < 0)		\
			return -EBUGGERED;	\
	} while (0)

매크로가 호출자 지역 변수에 몰래 의존해서도 안 된다. #define FOO(val) bar(index, val)처럼 index를 인수로 받지 않으면 호출부 문맥에 숨은 결합이 생긴다. 매크로 인수는 여러 번 평가될 수 있으므로 l-value로 사용하지 말고, 표현식과 각 인수는 연산자 우선순위가 바뀌지 않게 괄호로 감싼다.

#define FOO(val) bar(index, val)

#define CONSTANT 0x4000
#define CONSTEXP (CONSTANT | 3)

문장 표현식 매크로 내부의 임시 이름은 호출자 이름과 충돌할 수 있다. ret 대신 __foo_ret처럼 매크로에 고유한 접두사를 붙인다. 아래 원문 예제의 ret는 충돌 가능성을 보여 주기 위한 피해야 할 형태다.

#define FOO(x)			\
({					\
	typeof(x) ret;			\
	ret = calc_ret(x);		\
	(ret);				\
})

전처리기 세부 규칙은 GNU cpp 매뉴얼의 매크로 절을 참고한다. GCC 내부 표현인 RTL은 컴파일러 내부를 다루는 경우의 별도 주제이며, 커널 C 코드의 가독성을 위해 매크로로 복잡한 컴파일러 동작을 흉내 내서는 안 된다.

커널 메시지 출력

899-934

커널 메시지는 맞춤법을 지키고 짧고 분명하며 중의적이지 않게 쓴다. 문장 끝 마침표는 반드시 필요하지 않다. 여러 메시지를 구별하려고 괄호 안에 임의의 번호를 붙이는 방식은 사용자에게 의미가 없으므로 피한다.

장치와 연결된 메시지는 dev_err, dev_warn, dev_info처럼 장치 문맥을 자동으로 포함하는 함수를 사용한다. 장치가 없는 전역 문맥에서는 pr_notice, pr_info, pr_warn, pr_err 등을 사용한다.

드라이버는 정상 동작 중 조용해야 한다. 시스템에 잘못된 일이 생긴 것이 아니라 진단용 정보라면 dev_dbg 또는 pr_debug를 사용한다. 이 호출은 DEBUG가 정의되거나 CONFIG_DYNAMIC_DEBUG가 활성화된 경우가 아니면 컴파일 결과에서 제거된다. 더 상세한 로그에는 VERBOSE_DEBUG와 dev_vdbg를 사용할 수 있다.

Kconfig 디버그 옵션은 필요하면 -DDEBUG를 컴파일 옵션에 추가할 수 있고, 특정 파일에서만 #define DEBUG를 둘 수도 있다. 디버그 조건문 안에서 항상 출력해야 하는 메시지라면 printk(KERN_DEBUG ...)를 직접 사용할 수 있다.

메모리 할당

935-975

일반적인 커널 할당 함수에는 kmalloc, kzalloc, kmalloc_array, kcalloc, vmalloc, vzalloc이 있다. 각 함수의 주소 연속성, 초기화 여부와 수면 가능 문맥은 메모리 할당 문서를 함께 확인해야 한다.

구조체를 할당할 때는 자료형 이름을 반복하지 않고 포인터가 가리키는 객체의 크기를 사용한다. p = kmalloc(sizeof(*p), ...)는 선언 형식이 바뀌어도 할당 크기가 자동으로 따라가며, 긴 구조체 이름을 반복하지 않아 읽기 쉽다. kmalloc 반환값은 void 포인터이므로 캐스팅하지 않는다.

p = kmalloc(sizeof(*p), ...);

배열은 n * sizeof(...)를 직접 계산해 kmalloc에 넘기지 말고 kmalloc_array를 사용한다. 0으로 초기화된 배열은 kcalloc을 사용한다. 두 함수는 곱셈 오버플로를 검사하고 크기를 표현할 수 없으면 NULL을 반환한다.

p = kmalloc_array(n, sizeof(...), ...);

p = kcalloc(n, sizeof(...), ...);

__GFP_NOWARN 없이 일반 할당 함수를 호출하면 실패 시 할당기가 이미 스택 덤프를 출력한다. NULL을 받았다는 이유로 호출부에서 같은 실패 메시지를 추가하면 로그만 중복되므로 별도 오류 메시지를 출력할 필요가 없다.

inline의 과도한 사용

977-1002

inline은 함수를 자동으로 빠르게 만드는 마법의 옵션이 아니다. 매크로를 타입 안전한 함수로 바꾸는 경우처럼 적절한 용도가 있지만, 남용하면 같은 함수 본문이 여러 호출 지점에 복제되어 커널 이미지가 커진다.

커진 코드는 CPU 명령 캐시 점유를 늘리고 page cache에 쓸 수 있는 메모리를 줄여 시스템 전체를 느리게 할 수 있다. 원문은 page cache miss로 디스크 탐색이 발생하면 약 5ms가 걸릴 수 있고, 그 시간에는 매우 많은 CPU 사이클이 들어간다는 예로 코드 크기 비용을 설명한다.

경험칙으로 코드가 세 줄보다 긴 함수에는 inline을 붙이지 않는다. 단, 매개변수가 컴파일 시간 상수이고 그 값 때문에 함수 대부분이 제거된다는 것을 확실히 아는 경우는 예외다. kmalloc() 인라인 함수가 대표적인 예다.

static 함수가 한 번만 호출되므로 inline이 항상 이득이라는 주장도 유지보수 관점에서는 충분하지 않다. GCC는 이런 함수를 스스로 인라인할 수 있다. 나중에 두 번째 호출자가 생겼을 때 inline을 제거해야 하는 부담이, 컴파일러가 이미 할 최적화를 강제하는 작은 이득보다 크다.

함수 이름과 반환값

1005-1037

성공과 실패를 나타내는 반환값은 주로 두 형식이다. 오류 코드 정수는 음수 -Exxx가 실패이고 0이 성공이다. 성공 여부를 나타내는 불리언은 0이 실패이고 0이 아닌 값이 성공이다. 두 규약을 뒤섞으면 조건식을 반대로 해석하는 찾기 어려운 버그가 생긴다.

함수 이름이 동작이나 명령형이면 오류 코드 정수를 반환한다. 함수 이름이 어떤 조건을 묻는 술어라면 성공 여부를 나타내는 bool을 반환한다.

add_work()는 '작업을 추가하라'는 명령이므로 성공 시 0, 실패 시 -EBUSY를 반환한다. pci_dev_present()는 '일치하는 PCI 장치가 존재하는가'라는 술어이므로 찾으면 1, 찾지 못하면 0을 반환한다.

EXPORT된 함수와 모든 공개 함수는 이 규약을 따라야 한다. private static 함수에도 같은 규칙을 적용하는 것을 권장한다. 계산 자체의 결과를 반환하는 함수는 이 규칙의 대상이 아니며, 포인터 함수가 NULL 또는 ERR_PTR로 실패를 표현하듯 정상 범위를 벗어난 값으로 오류를 나타낼 수 있다.

bool 사용

1040-1068

Linux 커널의 bool은 C99 _Bool의 별칭이다. bool 값은 0 또는 1로만 평가되며, bool로 변환하면 자동으로 false 또는 true가 된다. 따라서 bool 값에 !!를 다시 적용할 필요가 없고, 값에는 숫자 1과 0 대신 true와 false를 사용한다.

함수 반환형과 스택 지역 변수에는 의미가 맞으면 bool을 자유롭게 사용할 수 있다. 참과 거짓을 저장하는 int보다 의도를 분명히 하므로 권장된다.

그러나 bool의 크기와 정렬은 대상 아키텍처에 따라 달라질 수 있다. 캐시 라인 배치나 구조체 크기가 중요한 자료 구조에는 bool을 사용하지 않는다. 참·거짓 필드가 많다면 1비트 bitfield로 모으거나 u8 같은 고정 폭 타입을 고려한다.

함수 인수에 true/false가 여러 개 나열되면 호출부만 보고 각 값의 뜻을 알기 어렵다. 이 경우 하나의 비트 flags 인수로 묶으면 더 읽기 쉬울 수 있다. 그 밖의 제한적인 구조체 필드와 인수에서는 bool이 가독성을 높인다.

기존 커널 매크로 재사용

1070-1090

include/linux/kernel.h에는 흔한 계산을 안전하고 일관되게 수행하는 매크로가 이미 있다. 배열 원소 수를 직접 sizeof 식으로 다시 만들지 말고 ARRAY_SIZE를 사용한다.

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

구조체 특정 멤버의 크기는 sizeof_field를 사용한다. 엄격한 타입 검사를 수행하는 min과 max도 제공된다. 비슷한 매크로를 새로 쓰기 전에 기존 헤더에 같은 기능이 있는지 확인한다.

#define sizeof_field(t, f) (sizeof(((t*)0)->f))

소스 파일의 editor modeline 금지

1093-1124

Emacs와 Vim을 비롯한 편집기는 소스 파일 안의 특별한 표식을 읽어 모드, 들여쓰기와 빌드 명령을 바꿀 수 있다. 원문은 다음 세 형태를 예로 든다.

-*- mode: c -*-

/*
Local Variables:
compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c"
End:
*/

/* vim:set sw=8 noet */

이런 modeline을 커널 소스에 넣지 않는다. 개발자는 각자 편집기 설정과 사용자 정의 모드를 가지고 있으며, 저장소의 소스 파일이 개인 환경을 덮어써서는 안 된다. 들여쓰기와 모드 설정을 위한 표식도 모두 이 금지 대상이다.

인라인 어셈블리

1127-1156

아키텍처 전용 코드에서 CPU 또는 플랫폼 기능과 연결하려면 인라인 어셈블리가 필요할 수 있다. 필요한 경우에는 사용하되, C로 같은 작업을 할 수 있다면 불필요한 어셈블리를 쓰지 않는다. 가능한 하드웨어 접근도 C 접근자와 헬퍼로 표현한다.

반복되는 짧은 어셈블리 조각은 C 매개변수를 받는 단순한 헬퍼 함수로 감싼다. 크고 복잡한 어셈블리 함수는 .S 파일에 두고 C 헤더에 대응하는 원형을 선언한다. 어셈블리로 구현한 함수의 C 원형에는 asmlinkage를 사용한다.

GCC가 부수 효과를 알아채지 못해 asm 문을 제거할 가능성이 있을 때는 volatile이 필요할 수 있다. 그러나 모든 asm에 기계적으로 volatile을 붙이면 컴파일러 최적화를 제한하므로 실제 필요성을 판단해야 한다.

하나의 asm 문에 여러 명령을 쓸 때는 명령마다 별도 문자열 줄을 사용하고, 마지막을 제외한 각 문자열을 \n\t로 끝낸다. 그러면 생성된 어셈블리에서도 다음 명령이 올바르게 줄 바꿈되고 들여쓰기된다.

asm ("magic %reg1, #42\n\t"
     "more_magic %reg2, %reg3"
     : /* outputs */ : /* inputs */ : /* clobbers */);

조건부 컴파일

1159-1205

가능하면 .c 파일 안에 #if와 #ifdef를 두지 않는다. 전처리 조건은 코드를 읽기 어렵게 하고 C의 제어 흐름을 끊는다. 대신 헤더에서 설정이 켜졌을 때의 실제 함수와 꺼졌을 때의 no-op stub을 각각 정의하고, .c 파일은 함수를 무조건 호출하게 한다. 컴파일러는 빈 stub 호출을 제거하므로 실행 결과와 비용은 동일하다.

함수 일부나 표현식 일부를 조건부로 지우기보다 함수 전체를 컴파일 대상에서 제외한다. 표현식 중간에 ifdef가 필요하다면 해당 부분을 보조 함수로 분리하고 그 함수 정의에 조건을 적용한다.

특정 설정에서만 사용되지 않을 수 있는 함수나 변수는 전처리 조건으로 선언 자체를 감싸기보다 __maybe_unused로 표시할 수 있다. 모든 설정에서 항상 사용되지 않는 코드라면 표시로 숨기지 말고 삭제한다.

코드 안에서는 가능한 경우 IS_ENABLED로 Kconfig 심볼을 C 불리언 식으로 바꾸어 일반 if문에 사용한다.

if (IS_ENABLED(CONFIG_SOMETHING)) {
	...
}

컴파일러는 상수 조건을 접어 설정에 맞는 블록만 남기므로 런타임 오버헤드가 없다. 동시에 C 컴파일러가 블록 내부의 문법, 타입과 심볼 참조를 검사할 수 있다. 단, 설정이 꺼지면 존재하지 않는 심볼을 블록 안에서 참조하는 경우에는 여전히 #ifdef가 필요하다.

몇 줄을 넘는 #if 또는 #ifdef 블록의 끝에는 #endif와 같은 줄에 원래 조건을 주석으로 적는다.

#ifdef CONFIG_SOMETHING
...
#endif /* CONFIG_SOMETHING */

커널을 함부로 중단하지 않기

1208-1268

커널을 중단할지 결정할 권한은 일반적으로 커널 개발자가 아니라 사용자에게 있다. panic()은 매우 신중하게 사용하며 주로 부팅 중 더 진행할 방법이 없을 때만 허용한다. 예를 들어 부팅 과정에서 메모리가 고갈되어 시스템을 계속 초기화할 수 없다면 panic이 타당할 수 있다.

새 코드에는 BUG(), BUG_ON(), VM_BUG_ON() 같은 BUG 변형을 추가하지 않는다. 대신 WARN 계열, 가능하면 WARN_ON_ONCE()를 사용하고 합리적인 복구가 가능하면 복구 경로를 제공한다. 적어도 부분 복구조차 불가능한 경우에는 복구 코드가 필수는 아니지만, 오류 처리를 작성하기 귀찮다는 이유로 BUG를 선택할 수는 없다. 계속 실행할 방법이 없는 중대한 내부 손상에만 충분한 근거와 함께 BUG가 남을 수 있다.

WARN()이나 WARN_ON()보다 WARN_ON_ONCE()를 일반적으로 선호한다. 한 번 발생한 경고 조건은 반복해서 발생하기 쉬우며, 동일한 경고가 로그를 가득 채우고 이전 로그를 밀어내거나 출력 자체가 시스템을 느리게 만드는 추가 문제를 일으킬 수 있다.

WARN 계열은 정상 동작에서는 절대로 일어나지 않아야 하는 예상 밖 내부 상태를 위한 것이다. 일반적인 사전·사후 조건 검사용 assert가 아니며, 사용자 공간 입력만으로 쉽게 발생할 수 있는 조건에 사용해서는 안 된다. 사용자에게 문제를 한 번 알릴 필요가 있다면 pr_warn_once()가 대안이 될 수 있다.

panic_on_warn을 켠 사용자가 있다는 이유로 필요한 WARN까지 피하지 않는다. 이 옵션을 활성화한 사용자는 WARN 발생 시 커널을 중단하라고 명시적으로 요청한 것이며, 그에 따라 시스템 중단 가능성이 커진다는 결과를 감수한다. 개발자는 WARN의 의미가 맞는지만 신중하게 판단한다.

컴파일 시간 단언에는 BUILD_BUG_ON()을 사용할 수 있고 사용을 권장한다. 이 검사는 빌드 단계에서 실패하며 런타임에는 아무 영향도 주지 않는다.

참고 문헌

1270-1290

원문은 C 언어와 프로그래밍 관례를 위한 기본 참고 문헌으로 Brian W. Kernighan과 Dennis M. Ritchie의 The C Programming Language 2판, Brian W. Kernighan과 Rob Pike의 The Practice of Programming을 제시한다.

K&R 및 이 문서와 충돌하지 않는 범위에서 cpp, gcc, GCC internals와 indent의 GNU 매뉴얼도 참고한다. C 언어 국제 표준화 작업 그룹 WG14와 Greg Kroah-Hartman의 OLS 2002 Kernel CodingStyle 발표 자료가 함께 열거되어 있다.