18c2ecf20Sopenharmony_ci.. _codingstyle: 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ciLinux kernel coding style 48c2ecf20Sopenharmony_ci========================= 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ciThis is a short document describing the preferred coding style for the 78c2ecf20Sopenharmony_cilinux kernel. Coding style is very personal, and I won't **force** my 88c2ecf20Sopenharmony_civiews on anybody, but this is what goes for anything that I have to be 98c2ecf20Sopenharmony_ciable to maintain, and I'd prefer it for most other things too. Please 108c2ecf20Sopenharmony_ciat least consider the points made here. 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ciFirst off, I'd suggest printing out a copy of the GNU coding standards, 138c2ecf20Sopenharmony_ciand NOT read it. Burn them, it's a great symbolic gesture. 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ciAnyway, here goes: 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci1) Indentation 198c2ecf20Sopenharmony_ci-------------- 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ciTabs are 8 characters, and thus indentations are also 8 characters. 228c2ecf20Sopenharmony_ciThere are heretic movements that try to make indentations 4 (or even 2!) 238c2ecf20Sopenharmony_cicharacters deep, and that is akin to trying to define the value of PI to 248c2ecf20Sopenharmony_cibe 3. 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciRationale: The whole idea behind indentation is to clearly define where 278c2ecf20Sopenharmony_cia block of control starts and ends. Especially when you've been looking 288c2ecf20Sopenharmony_ciat your screen for 20 straight hours, you'll find it a lot easier to see 298c2ecf20Sopenharmony_cihow the indentation works if you have large indentations. 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ciNow, some people will claim that having 8-character indentations makes 328c2ecf20Sopenharmony_cithe code move too far to the right, and makes it hard to read on a 338c2ecf20Sopenharmony_ci80-character terminal screen. The answer to that is that if you need 348c2ecf20Sopenharmony_cimore than 3 levels of indentation, you're screwed anyway, and should fix 358c2ecf20Sopenharmony_ciyour program. 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ciIn short, 8-char indents make things easier to read, and have the added 388c2ecf20Sopenharmony_cibenefit of warning you when you're nesting your functions too deep. 398c2ecf20Sopenharmony_ciHeed that warning. 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ciThe preferred way to ease multiple indentation levels in a switch statement is 428c2ecf20Sopenharmony_cito align the ``switch`` and its subordinate ``case`` labels in the same column 438c2ecf20Sopenharmony_ciinstead of ``double-indenting`` the ``case`` labels. E.g.: 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci.. code-block:: c 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci switch (suffix) { 488c2ecf20Sopenharmony_ci case 'G': 498c2ecf20Sopenharmony_ci case 'g': 508c2ecf20Sopenharmony_ci mem <<= 30; 518c2ecf20Sopenharmony_ci break; 528c2ecf20Sopenharmony_ci case 'M': 538c2ecf20Sopenharmony_ci case 'm': 548c2ecf20Sopenharmony_ci mem <<= 20; 558c2ecf20Sopenharmony_ci break; 568c2ecf20Sopenharmony_ci case 'K': 578c2ecf20Sopenharmony_ci case 'k': 588c2ecf20Sopenharmony_ci mem <<= 10; 598c2ecf20Sopenharmony_ci fallthrough; 608c2ecf20Sopenharmony_ci default: 618c2ecf20Sopenharmony_ci break; 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ciDon't put multiple statements on a single line unless you have 658c2ecf20Sopenharmony_cisomething to hide: 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci.. code-block:: c 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci if (condition) do_this; 708c2ecf20Sopenharmony_ci do_something_everytime; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ciDon't put multiple assignments on a single line either. Kernel coding style 738c2ecf20Sopenharmony_ciis super simple. Avoid tricky expressions. 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciOutside of comments, documentation and except in Kconfig, spaces are never 768c2ecf20Sopenharmony_ciused for indentation, and the above example is deliberately broken. 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ciGet a decent editor and don't leave whitespace at the end of lines. 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci2) Breaking long lines and strings 828c2ecf20Sopenharmony_ci---------------------------------- 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ciCoding style is all about readability and maintainability using commonly 858c2ecf20Sopenharmony_ciavailable tools. 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ciThe preferred limit on the length of a single line is 80 columns. 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ciStatements longer than 80 columns should be broken into sensible chunks, 908c2ecf20Sopenharmony_ciunless exceeding 80 columns significantly increases readability and does 918c2ecf20Sopenharmony_cinot hide information. 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ciDescendants are always substantially shorter than the parent and 948c2ecf20Sopenharmony_ciare placed substantially to the right. A very commonly used style 958c2ecf20Sopenharmony_ciis to align descendants to a function open parenthesis. 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ciThese same rules are applied to function headers with a long argument list. 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ciHowever, never break user-visible strings such as printk messages because 1008c2ecf20Sopenharmony_cithat breaks the ability to grep for them. 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci3) Placing Braces and Spaces 1048c2ecf20Sopenharmony_ci---------------------------- 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ciThe other issue that always comes up in C styling is the placement of 1078c2ecf20Sopenharmony_cibraces. Unlike the indent size, there are few technical reasons to 1088c2ecf20Sopenharmony_cichoose one placement strategy over the other, but the preferred way, as 1098c2ecf20Sopenharmony_cishown to us by the prophets Kernighan and Ritchie, is to put the opening 1108c2ecf20Sopenharmony_cibrace last on the line, and put the closing brace first, thusly: 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci.. code-block:: c 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci if (x is true) { 1158c2ecf20Sopenharmony_ci we do y 1168c2ecf20Sopenharmony_ci } 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ciThis applies to all non-function statement blocks (if, switch, for, 1198c2ecf20Sopenharmony_ciwhile, do). E.g.: 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci.. code-block:: c 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci switch (action) { 1248c2ecf20Sopenharmony_ci case KOBJ_ADD: 1258c2ecf20Sopenharmony_ci return "add"; 1268c2ecf20Sopenharmony_ci case KOBJ_REMOVE: 1278c2ecf20Sopenharmony_ci return "remove"; 1288c2ecf20Sopenharmony_ci case KOBJ_CHANGE: 1298c2ecf20Sopenharmony_ci return "change"; 1308c2ecf20Sopenharmony_ci default: 1318c2ecf20Sopenharmony_ci return NULL; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ciHowever, there is one special case, namely functions: they have the 1358c2ecf20Sopenharmony_ciopening brace at the beginning of the next line, thus: 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci.. code-block:: c 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci int function(int x) 1408c2ecf20Sopenharmony_ci { 1418c2ecf20Sopenharmony_ci body of function 1428c2ecf20Sopenharmony_ci } 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ciHeretic people all over the world have claimed that this inconsistency 1458c2ecf20Sopenharmony_ciis ... well ... inconsistent, but all right-thinking people know that 1468c2ecf20Sopenharmony_ci(a) K&R are **right** and (b) K&R are right. Besides, functions are 1478c2ecf20Sopenharmony_cispecial anyway (you can't nest them in C). 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ciNote that the closing brace is empty on a line of its own, **except** in 1508c2ecf20Sopenharmony_cithe cases where it is followed by a continuation of the same statement, 1518c2ecf20Sopenharmony_ciie a ``while`` in a do-statement or an ``else`` in an if-statement, like 1528c2ecf20Sopenharmony_cithis: 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci.. code-block:: c 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci do { 1578c2ecf20Sopenharmony_ci body of do-loop 1588c2ecf20Sopenharmony_ci } while (condition); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ciand 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci.. code-block:: c 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci if (x == y) { 1658c2ecf20Sopenharmony_ci .. 1668c2ecf20Sopenharmony_ci } else if (x > y) { 1678c2ecf20Sopenharmony_ci ... 1688c2ecf20Sopenharmony_ci } else { 1698c2ecf20Sopenharmony_ci .... 1708c2ecf20Sopenharmony_ci } 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ciRationale: K&R. 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ciAlso, note that this brace-placement also minimizes the number of empty 1758c2ecf20Sopenharmony_ci(or almost empty) lines, without any loss of readability. Thus, as the 1768c2ecf20Sopenharmony_cisupply of new-lines on your screen is not a renewable resource (think 1778c2ecf20Sopenharmony_ci25-line terminal screens here), you have more empty lines to put 1788c2ecf20Sopenharmony_cicomments on. 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ciDo not unnecessarily use braces where a single statement will do. 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci.. code-block:: c 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci if (condition) 1858c2ecf20Sopenharmony_ci action(); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ciand 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci.. code-block:: none 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci if (condition) 1928c2ecf20Sopenharmony_ci do_this(); 1938c2ecf20Sopenharmony_ci else 1948c2ecf20Sopenharmony_ci do_that(); 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ciThis does not apply if only one branch of a conditional statement is a single 1978c2ecf20Sopenharmony_cistatement; in the latter case use braces in both branches: 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci.. code-block:: c 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci if (condition) { 2028c2ecf20Sopenharmony_ci do_this(); 2038c2ecf20Sopenharmony_ci do_that(); 2048c2ecf20Sopenharmony_ci } else { 2058c2ecf20Sopenharmony_ci otherwise(); 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ciAlso, use braces when a loop contains more than a single simple statement: 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci.. code-block:: c 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci while (condition) { 2138c2ecf20Sopenharmony_ci if (test) 2148c2ecf20Sopenharmony_ci do_something(); 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci3.1) Spaces 2188c2ecf20Sopenharmony_ci*********** 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ciLinux kernel style for use of spaces depends (mostly) on 2218c2ecf20Sopenharmony_cifunction-versus-keyword usage. Use a space after (most) keywords. The 2228c2ecf20Sopenharmony_cinotable exceptions are sizeof, typeof, alignof, and __attribute__, which look 2238c2ecf20Sopenharmony_cisomewhat like functions (and are usually used with parentheses in Linux, 2248c2ecf20Sopenharmony_cialthough they are not required in the language, as in: ``sizeof info`` after 2258c2ecf20Sopenharmony_ci``struct fileinfo info;`` is declared). 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ciSo use a space after these keywords:: 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci if, switch, case, for, do, while 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_cibut not with sizeof, typeof, alignof, or __attribute__. E.g., 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci.. code-block:: c 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci s = sizeof(struct file); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ciDo not add spaces around (inside) parenthesized expressions. This example is 2398c2ecf20Sopenharmony_ci**bad**: 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci.. code-block:: c 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci s = sizeof( struct file ); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ciWhen declaring pointer data or a function that returns a pointer type, the 2478c2ecf20Sopenharmony_cipreferred use of ``*`` is adjacent to the data name or function name and not 2488c2ecf20Sopenharmony_ciadjacent to the type name. Examples: 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci.. code-block:: c 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci char *linux_banner; 2548c2ecf20Sopenharmony_ci unsigned long long memparse(char *ptr, char **retptr); 2558c2ecf20Sopenharmony_ci char *match_strdup(substring_t *s); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ciUse one space around (on each side of) most binary and ternary operators, 2588c2ecf20Sopenharmony_cisuch as any of these:: 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci = + - < > * / % | & ^ <= >= == != ? : 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_cibut no space after unary operators:: 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci & * + - ~ ! sizeof typeof alignof __attribute__ defined 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_cino space before the postfix increment & decrement unary operators:: 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci ++ -- 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_cino space after the prefix increment & decrement unary operators:: 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci ++ -- 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ciand no space around the ``.`` and ``->`` structure member operators. 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ciDo not leave trailing whitespace at the ends of lines. Some editors with 2778c2ecf20Sopenharmony_ci``smart`` indentation will insert whitespace at the beginning of new lines as 2788c2ecf20Sopenharmony_ciappropriate, so you can start typing the next line of code right away. 2798c2ecf20Sopenharmony_ciHowever, some such editors do not remove the whitespace if you end up not 2808c2ecf20Sopenharmony_ciputting a line of code there, such as if you leave a blank line. As a result, 2818c2ecf20Sopenharmony_ciyou end up with lines containing trailing whitespace. 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ciGit will warn you about patches that introduce trailing whitespace, and can 2848c2ecf20Sopenharmony_cioptionally strip the trailing whitespace for you; however, if applying a series 2858c2ecf20Sopenharmony_ciof patches, this may make later patches in the series fail by changing their 2868c2ecf20Sopenharmony_cicontext lines. 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci4) Naming 2908c2ecf20Sopenharmony_ci--------- 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ciC is a Spartan language, and your naming conventions should follow suit. 2938c2ecf20Sopenharmony_ciUnlike Modula-2 and Pascal programmers, C programmers do not use cute 2948c2ecf20Sopenharmony_cinames like ThisVariableIsATemporaryCounter. A C programmer would call that 2958c2ecf20Sopenharmony_civariable ``tmp``, which is much easier to write, and not the least more 2968c2ecf20Sopenharmony_cidifficult to understand. 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ciHOWEVER, while mixed-case names are frowned upon, descriptive names for 2998c2ecf20Sopenharmony_ciglobal variables are a must. To call a global function ``foo`` is a 3008c2ecf20Sopenharmony_cishooting offense. 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ciGLOBAL variables (to be used only if you **really** need them) need to 3038c2ecf20Sopenharmony_cihave descriptive names, as do global functions. If you have a function 3048c2ecf20Sopenharmony_cithat counts the number of active users, you should call that 3058c2ecf20Sopenharmony_ci``count_active_users()`` or similar, you should **not** call it ``cntusr()``. 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ciEncoding the type of a function into the name (so-called Hungarian 3088c2ecf20Sopenharmony_cinotation) is asinine - the compiler knows the types anyway and can check 3098c2ecf20Sopenharmony_cithose, and it only confuses the programmer. No wonder Microsoft makes buggy 3108c2ecf20Sopenharmony_ciprograms. 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ciLOCAL variable names should be short, and to the point. If you have 3138c2ecf20Sopenharmony_cisome random integer loop counter, it should probably be called ``i``. 3148c2ecf20Sopenharmony_ciCalling it ``loop_counter`` is non-productive, if there is no chance of it 3158c2ecf20Sopenharmony_cibeing mis-understood. Similarly, ``tmp`` can be just about any type of 3168c2ecf20Sopenharmony_civariable that is used to hold a temporary value. 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ciIf you are afraid to mix up your local variable names, you have another 3198c2ecf20Sopenharmony_ciproblem, which is called the function-growth-hormone-imbalance syndrome. 3208c2ecf20Sopenharmony_ciSee chapter 6 (Functions). 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ciFor symbol names and documentation, avoid introducing new usage of 3238c2ecf20Sopenharmony_ci'master / slave' (or 'slave' independent of 'master') and 'blacklist / 3248c2ecf20Sopenharmony_ciwhitelist'. 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ciRecommended replacements for 'master / slave' are: 3278c2ecf20Sopenharmony_ci '{primary,main} / {secondary,replica,subordinate}' 3288c2ecf20Sopenharmony_ci '{initiator,requester} / {target,responder}' 3298c2ecf20Sopenharmony_ci '{controller,host} / {device,worker,proxy}' 3308c2ecf20Sopenharmony_ci 'leader / follower' 3318c2ecf20Sopenharmony_ci 'director / performer' 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ciRecommended replacements for 'blacklist/whitelist' are: 3348c2ecf20Sopenharmony_ci 'denylist / allowlist' 3358c2ecf20Sopenharmony_ci 'blocklist / passlist' 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ciExceptions for introducing new usage is to maintain a userspace ABI/API, 3388c2ecf20Sopenharmony_cior when updating code for an existing (as of 2020) hardware or protocol 3398c2ecf20Sopenharmony_cispecification that mandates those terms. For new specifications 3408c2ecf20Sopenharmony_citranslate specification usage of the terminology to the kernel coding 3418c2ecf20Sopenharmony_cistandard where possible. 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci5) Typedefs 3448c2ecf20Sopenharmony_ci----------- 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ciPlease don't use things like ``vps_t``. 3478c2ecf20Sopenharmony_ciIt's a **mistake** to use typedef for structures and pointers. When you see a 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci.. code-block:: c 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci vps_t a; 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ciin the source, what does it mean? 3558c2ecf20Sopenharmony_ciIn contrast, if it says 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci.. code-block:: c 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci struct virtual_container *a; 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ciyou can actually tell what ``a`` is. 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ciLots of people think that typedefs ``help readability``. Not so. They are 3648c2ecf20Sopenharmony_ciuseful only for: 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci (a) totally opaque objects (where the typedef is actively used to **hide** 3678c2ecf20Sopenharmony_ci what the object is). 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci Example: ``pte_t`` etc. opaque objects that you can only access using 3708c2ecf20Sopenharmony_ci the proper accessor functions. 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci .. note:: 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci Opaqueness and ``accessor functions`` are not good in themselves. 3758c2ecf20Sopenharmony_ci The reason we have them for things like pte_t etc. is that there 3768c2ecf20Sopenharmony_ci really is absolutely **zero** portably accessible information there. 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci (b) Clear integer types, where the abstraction **helps** avoid confusion 3798c2ecf20Sopenharmony_ci whether it is ``int`` or ``long``. 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci u8/u16/u32 are perfectly fine typedefs, although they fit into 3828c2ecf20Sopenharmony_ci category (d) better than here. 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci .. note:: 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci Again - there needs to be a **reason** for this. If something is 3878c2ecf20Sopenharmony_ci ``unsigned long``, then there's no reason to do 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci typedef unsigned long myflags_t; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci but if there is a clear reason for why it under certain circumstances 3928c2ecf20Sopenharmony_ci might be an ``unsigned int`` and under other configurations might be 3938c2ecf20Sopenharmony_ci ``unsigned long``, then by all means go ahead and use a typedef. 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci (c) when you use sparse to literally create a **new** type for 3968c2ecf20Sopenharmony_ci type-checking. 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci (d) New types which are identical to standard C99 types, in certain 3998c2ecf20Sopenharmony_ci exceptional circumstances. 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci Although it would only take a short amount of time for the eyes and 4028c2ecf20Sopenharmony_ci brain to become accustomed to the standard types like ``uint32_t``, 4038c2ecf20Sopenharmony_ci some people object to their use anyway. 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci Therefore, the Linux-specific ``u8/u16/u32/u64`` types and their 4068c2ecf20Sopenharmony_ci signed equivalents which are identical to standard types are 4078c2ecf20Sopenharmony_ci permitted -- although they are not mandatory in new code of your 4088c2ecf20Sopenharmony_ci own. 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci When editing existing code which already uses one or the other set 4118c2ecf20Sopenharmony_ci of types, you should conform to the existing choices in that code. 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci (e) Types safe for use in userspace. 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci In certain structures which are visible to userspace, we cannot 4168c2ecf20Sopenharmony_ci require C99 types and cannot use the ``u32`` form above. Thus, we 4178c2ecf20Sopenharmony_ci use __u32 and similar types in all structures which are shared 4188c2ecf20Sopenharmony_ci with userspace. 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ciMaybe there are other cases too, but the rule should basically be to NEVER 4218c2ecf20Sopenharmony_ciEVER use a typedef unless you can clearly match one of those rules. 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ciIn general, a pointer, or a struct that has elements that can reasonably 4248c2ecf20Sopenharmony_cibe directly accessed should **never** be a typedef. 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci6) Functions 4288c2ecf20Sopenharmony_ci------------ 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ciFunctions should be short and sweet, and do just one thing. They should 4318c2ecf20Sopenharmony_cifit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24, 4328c2ecf20Sopenharmony_cias we all know), and do one thing and do that well. 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ciThe maximum length of a function is inversely proportional to the 4358c2ecf20Sopenharmony_cicomplexity and indentation level of that function. So, if you have a 4368c2ecf20Sopenharmony_ciconceptually simple function that is just one long (but simple) 4378c2ecf20Sopenharmony_cicase-statement, where you have to do lots of small things for a lot of 4388c2ecf20Sopenharmony_cidifferent cases, it's OK to have a longer function. 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_ciHowever, if you have a complex function, and you suspect that a 4418c2ecf20Sopenharmony_ciless-than-gifted first-year high-school student might not even 4428c2ecf20Sopenharmony_ciunderstand what the function is all about, you should adhere to the 4438c2ecf20Sopenharmony_cimaximum limits all the more closely. Use helper functions with 4448c2ecf20Sopenharmony_cidescriptive names (you can ask the compiler to in-line them if you think 4458c2ecf20Sopenharmony_ciit's performance-critical, and it will probably do a better job of it 4468c2ecf20Sopenharmony_cithan you would have done). 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ciAnother measure of the function is the number of local variables. They 4498c2ecf20Sopenharmony_cishouldn't exceed 5-10, or you're doing something wrong. Re-think the 4508c2ecf20Sopenharmony_cifunction, and split it into smaller pieces. A human brain can 4518c2ecf20Sopenharmony_cigenerally easily keep track of about 7 different things, anything more 4528c2ecf20Sopenharmony_ciand it gets confused. You know you're brilliant, but maybe you'd like 4538c2ecf20Sopenharmony_cito understand what you did 2 weeks from now. 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ciIn source files, separate functions with one blank line. If the function is 4568c2ecf20Sopenharmony_ciexported, the **EXPORT** macro for it should follow immediately after the 4578c2ecf20Sopenharmony_ciclosing function brace line. E.g.: 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci.. code-block:: c 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci int system_is_up(void) 4628c2ecf20Sopenharmony_ci { 4638c2ecf20Sopenharmony_ci return system_state == SYSTEM_RUNNING; 4648c2ecf20Sopenharmony_ci } 4658c2ecf20Sopenharmony_ci EXPORT_SYMBOL(system_is_up); 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ciIn function prototypes, include parameter names with their data types. 4688c2ecf20Sopenharmony_ciAlthough this is not required by the C language, it is preferred in Linux 4698c2ecf20Sopenharmony_cibecause it is a simple way to add valuable information for the reader. 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ciDo not use the ``extern`` keyword with function prototypes as this makes 4728c2ecf20Sopenharmony_cilines longer and isn't strictly necessary. 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci7) Centralized exiting of functions 4768c2ecf20Sopenharmony_ci----------------------------------- 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ciAlbeit deprecated by some people, the equivalent of the goto statement is 4798c2ecf20Sopenharmony_ciused frequently by compilers in form of the unconditional jump instruction. 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ciThe goto statement comes in handy when a function exits from multiple 4828c2ecf20Sopenharmony_cilocations and some common work such as cleanup has to be done. If there is no 4838c2ecf20Sopenharmony_cicleanup needed then just return directly. 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ciChoose label names which say what the goto does or why the goto exists. An 4868c2ecf20Sopenharmony_ciexample of a good name could be ``out_free_buffer:`` if the goto frees ``buffer``. 4878c2ecf20Sopenharmony_ciAvoid using GW-BASIC names like ``err1:`` and ``err2:``, as you would have to 4888c2ecf20Sopenharmony_cirenumber them if you ever add or remove exit paths, and they make correctness 4898c2ecf20Sopenharmony_cidifficult to verify anyway. 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ciThe rationale for using gotos is: 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci- unconditional statements are easier to understand and follow 4948c2ecf20Sopenharmony_ci- nesting is reduced 4958c2ecf20Sopenharmony_ci- errors by not updating individual exit points when making 4968c2ecf20Sopenharmony_ci modifications are prevented 4978c2ecf20Sopenharmony_ci- saves the compiler work to optimize redundant code away ;) 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci.. code-block:: c 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci int fun(int a) 5028c2ecf20Sopenharmony_ci { 5038c2ecf20Sopenharmony_ci int result = 0; 5048c2ecf20Sopenharmony_ci char *buffer; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci buffer = kmalloc(SIZE, GFP_KERNEL); 5078c2ecf20Sopenharmony_ci if (!buffer) 5088c2ecf20Sopenharmony_ci return -ENOMEM; 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci if (condition1) { 5118c2ecf20Sopenharmony_ci while (loop1) { 5128c2ecf20Sopenharmony_ci ... 5138c2ecf20Sopenharmony_ci } 5148c2ecf20Sopenharmony_ci result = 1; 5158c2ecf20Sopenharmony_ci goto out_free_buffer; 5168c2ecf20Sopenharmony_ci } 5178c2ecf20Sopenharmony_ci ... 5188c2ecf20Sopenharmony_ci out_free_buffer: 5198c2ecf20Sopenharmony_ci kfree(buffer); 5208c2ecf20Sopenharmony_ci return result; 5218c2ecf20Sopenharmony_ci } 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ciA common type of bug to be aware of is ``one err bugs`` which look like this: 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci.. code-block:: c 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci err: 5288c2ecf20Sopenharmony_ci kfree(foo->bar); 5298c2ecf20Sopenharmony_ci kfree(foo); 5308c2ecf20Sopenharmony_ci return ret; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ciThe bug in this code is that on some exit paths ``foo`` is NULL. Normally the 5338c2ecf20Sopenharmony_cifix for this is to split it up into two error labels ``err_free_bar:`` and 5348c2ecf20Sopenharmony_ci``err_free_foo:``: 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci.. code-block:: c 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci err_free_bar: 5398c2ecf20Sopenharmony_ci kfree(foo->bar); 5408c2ecf20Sopenharmony_ci err_free_foo: 5418c2ecf20Sopenharmony_ci kfree(foo); 5428c2ecf20Sopenharmony_ci return ret; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ciIdeally you should simulate errors to test all exit paths. 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci8) Commenting 5488c2ecf20Sopenharmony_ci------------- 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ciComments are good, but there is also a danger of over-commenting. NEVER 5518c2ecf20Sopenharmony_citry to explain HOW your code works in a comment: it's much better to 5528c2ecf20Sopenharmony_ciwrite the code so that the **working** is obvious, and it's a waste of 5538c2ecf20Sopenharmony_citime to explain badly written code. 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ciGenerally, you want your comments to tell WHAT your code does, not HOW. 5568c2ecf20Sopenharmony_ciAlso, try to avoid putting comments inside a function body: if the 5578c2ecf20Sopenharmony_cifunction is so complex that you need to separately comment parts of it, 5588c2ecf20Sopenharmony_ciyou should probably go back to chapter 6 for a while. You can make 5598c2ecf20Sopenharmony_cismall comments to note or warn about something particularly clever (or 5608c2ecf20Sopenharmony_ciugly), but try to avoid excess. Instead, put the comments at the head 5618c2ecf20Sopenharmony_ciof the function, telling people what it does, and possibly WHY it does 5628c2ecf20Sopenharmony_ciit. 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ciWhen commenting the kernel API functions, please use the kernel-doc format. 5658c2ecf20Sopenharmony_ciSee the files at :ref:`Documentation/doc-guide/ <doc_guide>` and 5668c2ecf20Sopenharmony_ci``scripts/kernel-doc`` for details. 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ciThe preferred style for long (multi-line) comments is: 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_ci.. code-block:: c 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci /* 5738c2ecf20Sopenharmony_ci * This is the preferred style for multi-line 5748c2ecf20Sopenharmony_ci * comments in the Linux kernel source code. 5758c2ecf20Sopenharmony_ci * Please use it consistently. 5768c2ecf20Sopenharmony_ci * 5778c2ecf20Sopenharmony_ci * Description: A column of asterisks on the left side, 5788c2ecf20Sopenharmony_ci * with beginning and ending almost-blank lines. 5798c2ecf20Sopenharmony_ci */ 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ciFor files in net/ and drivers/net/ the preferred style for long (multi-line) 5828c2ecf20Sopenharmony_cicomments is a little different. 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci.. code-block:: c 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci /* The preferred comment style for files in net/ and drivers/net 5878c2ecf20Sopenharmony_ci * looks like this. 5888c2ecf20Sopenharmony_ci * 5898c2ecf20Sopenharmony_ci * It is nearly the same as the generally preferred comment style, 5908c2ecf20Sopenharmony_ci * but there is no initial almost-blank line. 5918c2ecf20Sopenharmony_ci */ 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_ciIt's also important to comment data, whether they are basic types or derived 5948c2ecf20Sopenharmony_citypes. To this end, use just one data declaration per line (no commas for 5958c2ecf20Sopenharmony_cimultiple data declarations). This leaves you room for a small comment on each 5968c2ecf20Sopenharmony_ciitem, explaining its use. 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci 5998c2ecf20Sopenharmony_ci9) You've made a mess of it 6008c2ecf20Sopenharmony_ci--------------------------- 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ciThat's OK, we all do. You've probably been told by your long-time Unix 6038c2ecf20Sopenharmony_ciuser helper that ``GNU emacs`` automatically formats the C sources for 6048c2ecf20Sopenharmony_ciyou, and you've noticed that yes, it does do that, but the defaults it 6058c2ecf20Sopenharmony_ciuses are less than desirable (in fact, they are worse than random 6068c2ecf20Sopenharmony_cityping - an infinite number of monkeys typing into GNU emacs would never 6078c2ecf20Sopenharmony_cimake a good program). 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ciSo, you can either get rid of GNU emacs, or change it to use saner 6108c2ecf20Sopenharmony_civalues. To do the latter, you can stick the following in your .emacs file: 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci.. code-block:: none 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci (defun c-lineup-arglist-tabs-only (ignored) 6158c2ecf20Sopenharmony_ci "Line up argument lists by tabs, not spaces" 6168c2ecf20Sopenharmony_ci (let* ((anchor (c-langelem-pos c-syntactic-element)) 6178c2ecf20Sopenharmony_ci (column (c-langelem-2nd-pos c-syntactic-element)) 6188c2ecf20Sopenharmony_ci (offset (- (1+ column) anchor)) 6198c2ecf20Sopenharmony_ci (steps (floor offset c-basic-offset))) 6208c2ecf20Sopenharmony_ci (* (max steps 1) 6218c2ecf20Sopenharmony_ci c-basic-offset))) 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci (dir-locals-set-class-variables 6248c2ecf20Sopenharmony_ci 'linux-kernel 6258c2ecf20Sopenharmony_ci '((c-mode . ( 6268c2ecf20Sopenharmony_ci (c-basic-offset . 8) 6278c2ecf20Sopenharmony_ci (c-label-minimum-indentation . 0) 6288c2ecf20Sopenharmony_ci (c-offsets-alist . ( 6298c2ecf20Sopenharmony_ci (arglist-close . c-lineup-arglist-tabs-only) 6308c2ecf20Sopenharmony_ci (arglist-cont-nonempty . 6318c2ecf20Sopenharmony_ci (c-lineup-gcc-asm-reg c-lineup-arglist-tabs-only)) 6328c2ecf20Sopenharmony_ci (arglist-intro . +) 6338c2ecf20Sopenharmony_ci (brace-list-intro . +) 6348c2ecf20Sopenharmony_ci (c . c-lineup-C-comments) 6358c2ecf20Sopenharmony_ci (case-label . 0) 6368c2ecf20Sopenharmony_ci (comment-intro . c-lineup-comment) 6378c2ecf20Sopenharmony_ci (cpp-define-intro . +) 6388c2ecf20Sopenharmony_ci (cpp-macro . -1000) 6398c2ecf20Sopenharmony_ci (cpp-macro-cont . +) 6408c2ecf20Sopenharmony_ci (defun-block-intro . +) 6418c2ecf20Sopenharmony_ci (else-clause . 0) 6428c2ecf20Sopenharmony_ci (func-decl-cont . +) 6438c2ecf20Sopenharmony_ci (inclass . +) 6448c2ecf20Sopenharmony_ci (inher-cont . c-lineup-multi-inher) 6458c2ecf20Sopenharmony_ci (knr-argdecl-intro . 0) 6468c2ecf20Sopenharmony_ci (label . -1000) 6478c2ecf20Sopenharmony_ci (statement . 0) 6488c2ecf20Sopenharmony_ci (statement-block-intro . +) 6498c2ecf20Sopenharmony_ci (statement-case-intro . +) 6508c2ecf20Sopenharmony_ci (statement-cont . +) 6518c2ecf20Sopenharmony_ci (substatement . +) 6528c2ecf20Sopenharmony_ci )) 6538c2ecf20Sopenharmony_ci (indent-tabs-mode . t) 6548c2ecf20Sopenharmony_ci (show-trailing-whitespace . t) 6558c2ecf20Sopenharmony_ci )))) 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci (dir-locals-set-directory-class 6588c2ecf20Sopenharmony_ci (expand-file-name "~/src/linux-trees") 6598c2ecf20Sopenharmony_ci 'linux-kernel) 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ciThis will make emacs go better with the kernel coding style for C 6628c2ecf20Sopenharmony_cifiles below ``~/src/linux-trees``. 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ciBut even if you fail in getting emacs to do sane formatting, not 6658c2ecf20Sopenharmony_cieverything is lost: use ``indent``. 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_ciNow, again, GNU indent has the same brain-dead settings that GNU emacs 6688c2ecf20Sopenharmony_cihas, which is why you need to give it a few command line options. 6698c2ecf20Sopenharmony_ciHowever, that's not too bad, because even the makers of GNU indent 6708c2ecf20Sopenharmony_cirecognize the authority of K&R (the GNU people aren't evil, they are 6718c2ecf20Sopenharmony_cijust severely misguided in this matter), so you just give indent the 6728c2ecf20Sopenharmony_cioptions ``-kr -i8`` (stands for ``K&R, 8 character indents``), or use 6738c2ecf20Sopenharmony_ci``scripts/Lindent``, which indents in the latest style. 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci``indent`` has a lot of options, and especially when it comes to comment 6768c2ecf20Sopenharmony_cire-formatting you may want to take a look at the man page. But 6778c2ecf20Sopenharmony_ciremember: ``indent`` is not a fix for bad programming. 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ciNote that you can also use the ``clang-format`` tool to help you with 6808c2ecf20Sopenharmony_cithese rules, to quickly re-format parts of your code automatically, 6818c2ecf20Sopenharmony_ciand to review full files in order to spot coding style mistakes, 6828c2ecf20Sopenharmony_citypos and possible improvements. It is also handy for sorting ``#includes``, 6838c2ecf20Sopenharmony_cifor aligning variables/macros, for reflowing text and other similar tasks. 6848c2ecf20Sopenharmony_ciSee the file :ref:`Documentation/process/clang-format.rst <clangformat>` 6858c2ecf20Sopenharmony_cifor more details. 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci10) Kconfig configuration files 6898c2ecf20Sopenharmony_ci------------------------------- 6908c2ecf20Sopenharmony_ci 6918c2ecf20Sopenharmony_ciFor all of the Kconfig* configuration files throughout the source tree, 6928c2ecf20Sopenharmony_cithe indentation is somewhat different. Lines under a ``config`` definition 6938c2ecf20Sopenharmony_ciare indented with one tab, while help text is indented an additional two 6948c2ecf20Sopenharmony_cispaces. Example:: 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_ci config AUDIT 6978c2ecf20Sopenharmony_ci bool "Auditing support" 6988c2ecf20Sopenharmony_ci depends on NET 6998c2ecf20Sopenharmony_ci help 7008c2ecf20Sopenharmony_ci Enable auditing infrastructure that can be used with another 7018c2ecf20Sopenharmony_ci kernel subsystem, such as SELinux (which requires this for 7028c2ecf20Sopenharmony_ci logging of avc messages output). Does not do system-call 7038c2ecf20Sopenharmony_ci auditing without CONFIG_AUDITSYSCALL. 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ciSeriously dangerous features (such as write support for certain 7068c2ecf20Sopenharmony_cifilesystems) should advertise this prominently in their prompt string:: 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci config ADFS_FS_RW 7098c2ecf20Sopenharmony_ci bool "ADFS write support (DANGEROUS)" 7108c2ecf20Sopenharmony_ci depends on ADFS_FS 7118c2ecf20Sopenharmony_ci ... 7128c2ecf20Sopenharmony_ci 7138c2ecf20Sopenharmony_ciFor full documentation on the configuration files, see the file 7148c2ecf20Sopenharmony_ciDocumentation/kbuild/kconfig-language.rst. 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci11) Data structures 7188c2ecf20Sopenharmony_ci------------------- 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ciData structures that have visibility outside the single-threaded 7218c2ecf20Sopenharmony_cienvironment they are created and destroyed in should always have 7228c2ecf20Sopenharmony_cireference counts. In the kernel, garbage collection doesn't exist (and 7238c2ecf20Sopenharmony_cioutside the kernel garbage collection is slow and inefficient), which 7248c2ecf20Sopenharmony_cimeans that you absolutely **have** to reference count all your uses. 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_ciReference counting means that you can avoid locking, and allows multiple 7278c2ecf20Sopenharmony_ciusers to have access to the data structure in parallel - and not having 7288c2ecf20Sopenharmony_cito worry about the structure suddenly going away from under them just 7298c2ecf20Sopenharmony_cibecause they slept or did something else for a while. 7308c2ecf20Sopenharmony_ci 7318c2ecf20Sopenharmony_ciNote that locking is **not** a replacement for reference counting. 7328c2ecf20Sopenharmony_ciLocking is used to keep data structures coherent, while reference 7338c2ecf20Sopenharmony_cicounting is a memory management technique. Usually both are needed, and 7348c2ecf20Sopenharmony_cithey are not to be confused with each other. 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_ciMany data structures can indeed have two levels of reference counting, 7378c2ecf20Sopenharmony_ciwhen there are users of different ``classes``. The subclass count counts 7388c2ecf20Sopenharmony_cithe number of subclass users, and decrements the global count just once 7398c2ecf20Sopenharmony_ciwhen the subclass count goes to zero. 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ciExamples of this kind of ``multi-level-reference-counting`` can be found in 7428c2ecf20Sopenharmony_cimemory management (``struct mm_struct``: mm_users and mm_count), and in 7438c2ecf20Sopenharmony_cifilesystem code (``struct super_block``: s_count and s_active). 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ciRemember: if another thread can find your data structure, and you don't 7468c2ecf20Sopenharmony_cihave a reference count on it, you almost certainly have a bug. 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci12) Macros, Enums and RTL 7508c2ecf20Sopenharmony_ci------------------------- 7518c2ecf20Sopenharmony_ci 7528c2ecf20Sopenharmony_ciNames of macros defining constants and labels in enums are capitalized. 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci.. code-block:: c 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci #define CONSTANT 0x12345 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_ciEnums are preferred when defining several related constants. 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ciCAPITALIZED macro names are appreciated but macros resembling functions 7618c2ecf20Sopenharmony_cimay be named in lower case. 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ciGenerally, inline functions are preferable to macros resembling functions. 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_ciMacros with multiple statements should be enclosed in a do - while block: 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_ci.. code-block:: c 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci #define macrofun(a, b, c) \ 7708c2ecf20Sopenharmony_ci do { \ 7718c2ecf20Sopenharmony_ci if (a == 5) \ 7728c2ecf20Sopenharmony_ci do_this(b, c); \ 7738c2ecf20Sopenharmony_ci } while (0) 7748c2ecf20Sopenharmony_ci 7758c2ecf20Sopenharmony_ciThings to avoid when using macros: 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_ci1) macros that affect control flow: 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci.. code-block:: c 7808c2ecf20Sopenharmony_ci 7818c2ecf20Sopenharmony_ci #define FOO(x) \ 7828c2ecf20Sopenharmony_ci do { \ 7838c2ecf20Sopenharmony_ci if (blah(x) < 0) \ 7848c2ecf20Sopenharmony_ci return -EBUGGERED; \ 7858c2ecf20Sopenharmony_ci } while (0) 7868c2ecf20Sopenharmony_ci 7878c2ecf20Sopenharmony_ciis a **very** bad idea. It looks like a function call but exits the ``calling`` 7888c2ecf20Sopenharmony_cifunction; don't break the internal parsers of those who will read the code. 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci2) macros that depend on having a local variable with a magic name: 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci.. code-block:: c 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ci #define FOO(val) bar(index, val) 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_cimight look like a good thing, but it's confusing as hell when one reads the 7978c2ecf20Sopenharmony_cicode and it's prone to breakage from seemingly innocent changes. 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_ci3) macros with arguments that are used as l-values: FOO(x) = y; will 8008c2ecf20Sopenharmony_cibite you if somebody e.g. turns FOO into an inline function. 8018c2ecf20Sopenharmony_ci 8028c2ecf20Sopenharmony_ci4) forgetting about precedence: macros defining constants using expressions 8038c2ecf20Sopenharmony_cimust enclose the expression in parentheses. Beware of similar issues with 8048c2ecf20Sopenharmony_cimacros using parameters. 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_ci.. code-block:: c 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci #define CONSTANT 0x4000 8098c2ecf20Sopenharmony_ci #define CONSTEXP (CONSTANT | 3) 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci5) namespace collisions when defining local variables in macros resembling 8128c2ecf20Sopenharmony_cifunctions: 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci.. code-block:: c 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_ci #define FOO(x) \ 8178c2ecf20Sopenharmony_ci ({ \ 8188c2ecf20Sopenharmony_ci typeof(x) ret; \ 8198c2ecf20Sopenharmony_ci ret = calc_ret(x); \ 8208c2ecf20Sopenharmony_ci (ret); \ 8218c2ecf20Sopenharmony_ci }) 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ciret is a common name for a local variable - __foo_ret is less likely 8248c2ecf20Sopenharmony_cito collide with an existing variable. 8258c2ecf20Sopenharmony_ci 8268c2ecf20Sopenharmony_ciThe cpp manual deals with macros exhaustively. The gcc internals manual also 8278c2ecf20Sopenharmony_cicovers RTL which is used frequently with assembly language in the kernel. 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci13) Printing kernel messages 8318c2ecf20Sopenharmony_ci---------------------------- 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_ciKernel developers like to be seen as literate. Do mind the spelling 8348c2ecf20Sopenharmony_ciof kernel messages to make a good impression. Do not use incorrect 8358c2ecf20Sopenharmony_cicontractions like ``dont``; use ``do not`` or ``don't`` instead. Make the 8368c2ecf20Sopenharmony_cimessages concise, clear, and unambiguous. 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ciKernel messages do not have to be terminated with a period. 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ciPrinting numbers in parentheses (%d) adds no value and should be avoided. 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ciThere are a number of driver model diagnostic macros in <linux/device.h> 8438c2ecf20Sopenharmony_ciwhich you should use to make sure messages are matched to the right device 8448c2ecf20Sopenharmony_ciand driver, and are tagged with the right level: dev_err(), dev_warn(), 8458c2ecf20Sopenharmony_cidev_info(), and so forth. For messages that aren't associated with a 8468c2ecf20Sopenharmony_ciparticular device, <linux/printk.h> defines pr_notice(), pr_info(), 8478c2ecf20Sopenharmony_cipr_warn(), pr_err(), etc. 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ciComing up with good debugging messages can be quite a challenge; and once 8508c2ecf20Sopenharmony_ciyou have them, they can be a huge help for remote troubleshooting. However 8518c2ecf20Sopenharmony_cidebug message printing is handled differently than printing other non-debug 8528c2ecf20Sopenharmony_cimessages. While the other pr_XXX() functions print unconditionally, 8538c2ecf20Sopenharmony_cipr_debug() does not; it is compiled out by default, unless either DEBUG is 8548c2ecf20Sopenharmony_cidefined or CONFIG_DYNAMIC_DEBUG is set. That is true for dev_dbg() also, 8558c2ecf20Sopenharmony_ciand a related convention uses VERBOSE_DEBUG to add dev_vdbg() messages to 8568c2ecf20Sopenharmony_cithe ones already enabled by DEBUG. 8578c2ecf20Sopenharmony_ci 8588c2ecf20Sopenharmony_ciMany subsystems have Kconfig debug options to turn on -DDEBUG in the 8598c2ecf20Sopenharmony_cicorresponding Makefile; in other cases specific files #define DEBUG. And 8608c2ecf20Sopenharmony_ciwhen a debug message should be unconditionally printed, such as if it is 8618c2ecf20Sopenharmony_cialready inside a debug-related #ifdef section, printk(KERN_DEBUG ...) can be 8628c2ecf20Sopenharmony_ciused. 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci 8658c2ecf20Sopenharmony_ci14) Allocating memory 8668c2ecf20Sopenharmony_ci--------------------- 8678c2ecf20Sopenharmony_ci 8688c2ecf20Sopenharmony_ciThe kernel provides the following general purpose memory allocators: 8698c2ecf20Sopenharmony_cikmalloc(), kzalloc(), kmalloc_array(), kcalloc(), vmalloc(), and 8708c2ecf20Sopenharmony_civzalloc(). Please refer to the API documentation for further information 8718c2ecf20Sopenharmony_ciabout them. :ref:`Documentation/core-api/memory-allocation.rst 8728c2ecf20Sopenharmony_ci<memory_allocation>` 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ciThe preferred form for passing a size of a struct is the following: 8758c2ecf20Sopenharmony_ci 8768c2ecf20Sopenharmony_ci.. code-block:: c 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_ci p = kmalloc(sizeof(*p), ...); 8798c2ecf20Sopenharmony_ci 8808c2ecf20Sopenharmony_ciThe alternative form where struct name is spelled out hurts readability and 8818c2ecf20Sopenharmony_ciintroduces an opportunity for a bug when the pointer variable type is changed 8828c2ecf20Sopenharmony_cibut the corresponding sizeof that is passed to a memory allocator is not. 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_ciCasting the return value which is a void pointer is redundant. The conversion 8858c2ecf20Sopenharmony_cifrom void pointer to any other pointer type is guaranteed by the C programming 8868c2ecf20Sopenharmony_cilanguage. 8878c2ecf20Sopenharmony_ci 8888c2ecf20Sopenharmony_ciThe preferred form for allocating an array is the following: 8898c2ecf20Sopenharmony_ci 8908c2ecf20Sopenharmony_ci.. code-block:: c 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci p = kmalloc_array(n, sizeof(...), ...); 8938c2ecf20Sopenharmony_ci 8948c2ecf20Sopenharmony_ciThe preferred form for allocating a zeroed array is the following: 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_ci.. code-block:: c 8978c2ecf20Sopenharmony_ci 8988c2ecf20Sopenharmony_ci p = kcalloc(n, sizeof(...), ...); 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_ciBoth forms check for overflow on the allocation size n * sizeof(...), 9018c2ecf20Sopenharmony_ciand return NULL if that occurred. 9028c2ecf20Sopenharmony_ci 9038c2ecf20Sopenharmony_ciThese generic allocation functions all emit a stack dump on failure when used 9048c2ecf20Sopenharmony_ciwithout __GFP_NOWARN so there is no use in emitting an additional failure 9058c2ecf20Sopenharmony_cimessage when NULL is returned. 9068c2ecf20Sopenharmony_ci 9078c2ecf20Sopenharmony_ci15) The inline disease 9088c2ecf20Sopenharmony_ci---------------------- 9098c2ecf20Sopenharmony_ci 9108c2ecf20Sopenharmony_ciThere appears to be a common misperception that gcc has a magic "make me 9118c2ecf20Sopenharmony_cifaster" speedup option called ``inline``. While the use of inlines can be 9128c2ecf20Sopenharmony_ciappropriate (for example as a means of replacing macros, see Chapter 12), it 9138c2ecf20Sopenharmony_civery often is not. Abundant use of the inline keyword leads to a much bigger 9148c2ecf20Sopenharmony_cikernel, which in turn slows the system as a whole down, due to a bigger 9158c2ecf20Sopenharmony_ciicache footprint for the CPU and simply because there is less memory 9168c2ecf20Sopenharmony_ciavailable for the pagecache. Just think about it; a pagecache miss causes a 9178c2ecf20Sopenharmony_cidisk seek, which easily takes 5 milliseconds. There are a LOT of cpu cycles 9188c2ecf20Sopenharmony_cithat can go into these 5 milliseconds. 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ciA reasonable rule of thumb is to not put inline at functions that have more 9218c2ecf20Sopenharmony_cithan 3 lines of code in them. An exception to this rule are the cases where 9228c2ecf20Sopenharmony_cia parameter is known to be a compiletime constant, and as a result of this 9238c2ecf20Sopenharmony_ciconstantness you *know* the compiler will be able to optimize most of your 9248c2ecf20Sopenharmony_cifunction away at compile time. For a good example of this later case, see 9258c2ecf20Sopenharmony_cithe kmalloc() inline function. 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_ciOften people argue that adding inline to functions that are static and used 9288c2ecf20Sopenharmony_cionly once is always a win since there is no space tradeoff. While this is 9298c2ecf20Sopenharmony_citechnically correct, gcc is capable of inlining these automatically without 9308c2ecf20Sopenharmony_cihelp, and the maintenance issue of removing the inline when a second user 9318c2ecf20Sopenharmony_ciappears outweighs the potential value of the hint that tells gcc to do 9328c2ecf20Sopenharmony_cisomething it would have done anyway. 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_ci 9358c2ecf20Sopenharmony_ci16) Function return values and names 9368c2ecf20Sopenharmony_ci------------------------------------ 9378c2ecf20Sopenharmony_ci 9388c2ecf20Sopenharmony_ciFunctions can return values of many different kinds, and one of the 9398c2ecf20Sopenharmony_cimost common is a value indicating whether the function succeeded or 9408c2ecf20Sopenharmony_cifailed. Such a value can be represented as an error-code integer 9418c2ecf20Sopenharmony_ci(-Exxx = failure, 0 = success) or a ``succeeded`` boolean (0 = failure, 9428c2ecf20Sopenharmony_cinon-zero = success). 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ciMixing up these two sorts of representations is a fertile source of 9458c2ecf20Sopenharmony_cidifficult-to-find bugs. If the C language included a strong distinction 9468c2ecf20Sopenharmony_cibetween integers and booleans then the compiler would find these mistakes 9478c2ecf20Sopenharmony_cifor us... but it doesn't. To help prevent such bugs, always follow this 9488c2ecf20Sopenharmony_ciconvention:: 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_ci If the name of a function is an action or an imperative command, 9518c2ecf20Sopenharmony_ci the function should return an error-code integer. If the name 9528c2ecf20Sopenharmony_ci is a predicate, the function should return a "succeeded" boolean. 9538c2ecf20Sopenharmony_ci 9548c2ecf20Sopenharmony_ciFor example, ``add work`` is a command, and the add_work() function returns 0 9558c2ecf20Sopenharmony_cifor success or -EBUSY for failure. In the same way, ``PCI device present`` is 9568c2ecf20Sopenharmony_cia predicate, and the pci_dev_present() function returns 1 if it succeeds in 9578c2ecf20Sopenharmony_cifinding a matching device or 0 if it doesn't. 9588c2ecf20Sopenharmony_ci 9598c2ecf20Sopenharmony_ciAll EXPORTed functions must respect this convention, and so should all 9608c2ecf20Sopenharmony_cipublic functions. Private (static) functions need not, but it is 9618c2ecf20Sopenharmony_cirecommended that they do. 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ciFunctions whose return value is the actual result of a computation, rather 9648c2ecf20Sopenharmony_cithan an indication of whether the computation succeeded, are not subject to 9658c2ecf20Sopenharmony_cithis rule. Generally they indicate failure by returning some out-of-range 9668c2ecf20Sopenharmony_ciresult. Typical examples would be functions that return pointers; they use 9678c2ecf20Sopenharmony_ciNULL or the ERR_PTR mechanism to report failure. 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_ci17) Using bool 9718c2ecf20Sopenharmony_ci-------------- 9728c2ecf20Sopenharmony_ci 9738c2ecf20Sopenharmony_ciThe Linux kernel bool type is an alias for the C99 _Bool type. bool values can 9748c2ecf20Sopenharmony_cionly evaluate to 0 or 1, and implicit or explicit conversion to bool 9758c2ecf20Sopenharmony_ciautomatically converts the value to true or false. When using bool types the 9768c2ecf20Sopenharmony_ci!! construction is not needed, which eliminates a class of bugs. 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ciWhen working with bool values the true and false definitions should be used 9798c2ecf20Sopenharmony_ciinstead of 1 and 0. 9808c2ecf20Sopenharmony_ci 9818c2ecf20Sopenharmony_cibool function return types and stack variables are always fine to use whenever 9828c2ecf20Sopenharmony_ciappropriate. Use of bool is encouraged to improve readability and is often a 9838c2ecf20Sopenharmony_cibetter option than 'int' for storing boolean values. 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ciDo not use bool if cache line layout or size of the value matters, as its size 9868c2ecf20Sopenharmony_ciand alignment varies based on the compiled architecture. Structures that are 9878c2ecf20Sopenharmony_cioptimized for alignment and size should not use bool. 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ciIf a structure has many true/false values, consider consolidating them into a 9908c2ecf20Sopenharmony_cibitfield with 1 bit members, or using an appropriate fixed width type, such as 9918c2ecf20Sopenharmony_ciu8. 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_ciSimilarly for function arguments, many true/false values can be consolidated 9948c2ecf20Sopenharmony_ciinto a single bitwise 'flags' argument and 'flags' can often be a more 9958c2ecf20Sopenharmony_cireadable alternative if the call-sites have naked true/false constants. 9968c2ecf20Sopenharmony_ci 9978c2ecf20Sopenharmony_ciOtherwise limited use of bool in structures and arguments can improve 9988c2ecf20Sopenharmony_cireadability. 9998c2ecf20Sopenharmony_ci 10008c2ecf20Sopenharmony_ci18) Don't re-invent the kernel macros 10018c2ecf20Sopenharmony_ci------------------------------------- 10028c2ecf20Sopenharmony_ci 10038c2ecf20Sopenharmony_ciThe header file include/linux/kernel.h contains a number of macros that 10048c2ecf20Sopenharmony_ciyou should use, rather than explicitly coding some variant of them yourself. 10058c2ecf20Sopenharmony_ciFor example, if you need to calculate the length of an array, take advantage 10068c2ecf20Sopenharmony_ciof the macro 10078c2ecf20Sopenharmony_ci 10088c2ecf20Sopenharmony_ci.. code-block:: c 10098c2ecf20Sopenharmony_ci 10108c2ecf20Sopenharmony_ci #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 10118c2ecf20Sopenharmony_ci 10128c2ecf20Sopenharmony_ciSimilarly, if you need to calculate the size of some structure member, use 10138c2ecf20Sopenharmony_ci 10148c2ecf20Sopenharmony_ci.. code-block:: c 10158c2ecf20Sopenharmony_ci 10168c2ecf20Sopenharmony_ci #define sizeof_field(t, f) (sizeof(((t*)0)->f)) 10178c2ecf20Sopenharmony_ci 10188c2ecf20Sopenharmony_ciThere are also min() and max() macros that do strict type checking if you 10198c2ecf20Sopenharmony_cineed them. Feel free to peruse that header file to see what else is already 10208c2ecf20Sopenharmony_cidefined that you shouldn't reproduce in your code. 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_ci 10238c2ecf20Sopenharmony_ci19) Editor modelines and other cruft 10248c2ecf20Sopenharmony_ci------------------------------------ 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ciSome editors can interpret configuration information embedded in source files, 10278c2ecf20Sopenharmony_ciindicated with special markers. For example, emacs interprets lines marked 10288c2ecf20Sopenharmony_cilike this: 10298c2ecf20Sopenharmony_ci 10308c2ecf20Sopenharmony_ci.. code-block:: c 10318c2ecf20Sopenharmony_ci 10328c2ecf20Sopenharmony_ci -*- mode: c -*- 10338c2ecf20Sopenharmony_ci 10348c2ecf20Sopenharmony_ciOr like this: 10358c2ecf20Sopenharmony_ci 10368c2ecf20Sopenharmony_ci.. code-block:: c 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci /* 10398c2ecf20Sopenharmony_ci Local Variables: 10408c2ecf20Sopenharmony_ci compile-command: "gcc -DMAGIC_DEBUG_FLAG foo.c" 10418c2ecf20Sopenharmony_ci End: 10428c2ecf20Sopenharmony_ci */ 10438c2ecf20Sopenharmony_ci 10448c2ecf20Sopenharmony_ciVim interprets markers that look like this: 10458c2ecf20Sopenharmony_ci 10468c2ecf20Sopenharmony_ci.. code-block:: c 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_ci /* vim:set sw=8 noet */ 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_ciDo not include any of these in source files. People have their own personal 10518c2ecf20Sopenharmony_cieditor configurations, and your source files should not override them. This 10528c2ecf20Sopenharmony_ciincludes markers for indentation and mode configuration. People may use their 10538c2ecf20Sopenharmony_ciown custom mode, or may have some other magic method for making indentation 10548c2ecf20Sopenharmony_ciwork correctly. 10558c2ecf20Sopenharmony_ci 10568c2ecf20Sopenharmony_ci 10578c2ecf20Sopenharmony_ci20) Inline assembly 10588c2ecf20Sopenharmony_ci------------------- 10598c2ecf20Sopenharmony_ci 10608c2ecf20Sopenharmony_ciIn architecture-specific code, you may need to use inline assembly to interface 10618c2ecf20Sopenharmony_ciwith CPU or platform functionality. Don't hesitate to do so when necessary. 10628c2ecf20Sopenharmony_ciHowever, don't use inline assembly gratuitously when C can do the job. You can 10638c2ecf20Sopenharmony_ciand should poke hardware from C when possible. 10648c2ecf20Sopenharmony_ci 10658c2ecf20Sopenharmony_ciConsider writing simple helper functions that wrap common bits of inline 10668c2ecf20Sopenharmony_ciassembly, rather than repeatedly writing them with slight variations. Remember 10678c2ecf20Sopenharmony_cithat inline assembly can use C parameters. 10688c2ecf20Sopenharmony_ci 10698c2ecf20Sopenharmony_ciLarge, non-trivial assembly functions should go in .S files, with corresponding 10708c2ecf20Sopenharmony_ciC prototypes defined in C header files. The C prototypes for assembly 10718c2ecf20Sopenharmony_cifunctions should use ``asmlinkage``. 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ciYou may need to mark your asm statement as volatile, to prevent GCC from 10748c2ecf20Sopenharmony_ciremoving it if GCC doesn't notice any side effects. You don't always need to 10758c2ecf20Sopenharmony_cido so, though, and doing so unnecessarily can limit optimization. 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ciWhen writing a single inline assembly statement containing multiple 10788c2ecf20Sopenharmony_ciinstructions, put each instruction on a separate line in a separate quoted 10798c2ecf20Sopenharmony_cistring, and end each string except the last with ``\n\t`` to properly indent 10808c2ecf20Sopenharmony_cithe next instruction in the assembly output: 10818c2ecf20Sopenharmony_ci 10828c2ecf20Sopenharmony_ci.. code-block:: c 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_ci asm ("magic %reg1, #42\n\t" 10858c2ecf20Sopenharmony_ci "more_magic %reg2, %reg3" 10868c2ecf20Sopenharmony_ci : /* outputs */ : /* inputs */ : /* clobbers */); 10878c2ecf20Sopenharmony_ci 10888c2ecf20Sopenharmony_ci 10898c2ecf20Sopenharmony_ci21) Conditional Compilation 10908c2ecf20Sopenharmony_ci--------------------------- 10918c2ecf20Sopenharmony_ci 10928c2ecf20Sopenharmony_ciWherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c 10938c2ecf20Sopenharmony_cifiles; doing so makes code harder to read and logic harder to follow. Instead, 10948c2ecf20Sopenharmony_ciuse such conditionals in a header file defining functions for use in those .c 10958c2ecf20Sopenharmony_cifiles, providing no-op stub versions in the #else case, and then call those 10968c2ecf20Sopenharmony_cifunctions unconditionally from .c files. The compiler will avoid generating 10978c2ecf20Sopenharmony_ciany code for the stub calls, producing identical results, but the logic will 10988c2ecf20Sopenharmony_ciremain easy to follow. 10998c2ecf20Sopenharmony_ci 11008c2ecf20Sopenharmony_ciPrefer to compile out entire functions, rather than portions of functions or 11018c2ecf20Sopenharmony_ciportions of expressions. Rather than putting an ifdef in an expression, factor 11028c2ecf20Sopenharmony_ciout part or all of the expression into a separate helper function and apply the 11038c2ecf20Sopenharmony_ciconditional to that function. 11048c2ecf20Sopenharmony_ci 11058c2ecf20Sopenharmony_ciIf you have a function or variable which may potentially go unused in a 11068c2ecf20Sopenharmony_ciparticular configuration, and the compiler would warn about its definition 11078c2ecf20Sopenharmony_cigoing unused, mark the definition as __maybe_unused rather than wrapping it in 11088c2ecf20Sopenharmony_cia preprocessor conditional. (However, if a function or variable *always* goes 11098c2ecf20Sopenharmony_ciunused, delete it.) 11108c2ecf20Sopenharmony_ci 11118c2ecf20Sopenharmony_ciWithin code, where possible, use the IS_ENABLED macro to convert a Kconfig 11128c2ecf20Sopenharmony_cisymbol into a C boolean expression, and use it in a normal C conditional: 11138c2ecf20Sopenharmony_ci 11148c2ecf20Sopenharmony_ci.. code-block:: c 11158c2ecf20Sopenharmony_ci 11168c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_SOMETHING)) { 11178c2ecf20Sopenharmony_ci ... 11188c2ecf20Sopenharmony_ci } 11198c2ecf20Sopenharmony_ci 11208c2ecf20Sopenharmony_ciThe compiler will constant-fold the conditional away, and include or exclude 11218c2ecf20Sopenharmony_cithe block of code just as with an #ifdef, so this will not add any runtime 11228c2ecf20Sopenharmony_cioverhead. However, this approach still allows the C compiler to see the code 11238c2ecf20Sopenharmony_ciinside the block, and check it for correctness (syntax, types, symbol 11248c2ecf20Sopenharmony_cireferences, etc). Thus, you still have to use an #ifdef if the code inside the 11258c2ecf20Sopenharmony_ciblock references symbols that will not exist if the condition is not met. 11268c2ecf20Sopenharmony_ci 11278c2ecf20Sopenharmony_ciAt the end of any non-trivial #if or #ifdef block (more than a few lines), 11288c2ecf20Sopenharmony_ciplace a comment after the #endif on the same line, noting the conditional 11298c2ecf20Sopenharmony_ciexpression used. For instance: 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci.. code-block:: c 11328c2ecf20Sopenharmony_ci 11338c2ecf20Sopenharmony_ci #ifdef CONFIG_SOMETHING 11348c2ecf20Sopenharmony_ci ... 11358c2ecf20Sopenharmony_ci #endif /* CONFIG_SOMETHING */ 11368c2ecf20Sopenharmony_ci 11378c2ecf20Sopenharmony_ci 11388c2ecf20Sopenharmony_ciAppendix I) References 11398c2ecf20Sopenharmony_ci---------------------- 11408c2ecf20Sopenharmony_ci 11418c2ecf20Sopenharmony_ciThe C Programming Language, Second Edition 11428c2ecf20Sopenharmony_ciby Brian W. Kernighan and Dennis M. Ritchie. 11438c2ecf20Sopenharmony_ciPrentice Hall, Inc., 1988. 11448c2ecf20Sopenharmony_ciISBN 0-13-110362-8 (paperback), 0-13-110370-9 (hardback). 11458c2ecf20Sopenharmony_ci 11468c2ecf20Sopenharmony_ciThe Practice of Programming 11478c2ecf20Sopenharmony_ciby Brian W. Kernighan and Rob Pike. 11488c2ecf20Sopenharmony_ciAddison-Wesley, Inc., 1999. 11498c2ecf20Sopenharmony_ciISBN 0-201-61586-X. 11508c2ecf20Sopenharmony_ci 11518c2ecf20Sopenharmony_ciGNU manuals - where in compliance with K&R and this text - for cpp, gcc, 11528c2ecf20Sopenharmony_cigcc internals and indent, all available from https://www.gnu.org/manual/ 11538c2ecf20Sopenharmony_ci 11548c2ecf20Sopenharmony_ciWG14 is the international standardization working group for the programming 11558c2ecf20Sopenharmony_cilanguage C, URL: http://www.open-std.org/JTC1/SC22/WG14/ 11568c2ecf20Sopenharmony_ci 11578c2ecf20Sopenharmony_ciKernel :ref:`process/coding-style.rst <codingstyle>`, by greg@kroah.com at OLS 2002: 11588c2ecf20Sopenharmony_cihttp://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/ 1159