0001 .. _kernel_hacking_hack:
0002
0003 ============================================
0004 Unreliable Guide To Hacking The Linux Kernel
0005 ============================================
0006
0007 :Author: Rusty Russell
0008
0009 Introduction
0010 ============
0011
0012 Welcome, gentle reader, to Rusty's Remarkably Unreliable Guide to Linux
0013 Kernel Hacking. This document describes the common routines and general
0014 requirements for kernel code: its goal is to serve as a primer for Linux
0015 kernel development for experienced C programmers. I avoid implementation
0016 details: that's what the code is for, and I ignore whole tracts of
0017 useful routines.
0018
0019 Before you read this, please understand that I never wanted to write
0020 this document, being grossly under-qualified, but I always wanted to
0021 read it, and this was the only way. I hope it will grow into a
0022 compendium of best practice, common starting points and random
0023 information.
0024
0025 The Players
0026 ===========
0027
0028 At any time each of the CPUs in a system can be:
0029
0030 - not associated with any process, serving a hardware interrupt;
0031
0032 - not associated with any process, serving a softirq or tasklet;
0033
0034 - running in kernel space, associated with a process (user context);
0035
0036 - running a process in user space.
0037
0038 There is an ordering between these. The bottom two can preempt each
0039 other, but above that is a strict hierarchy: each can only be preempted
0040 by the ones above it. For example, while a softirq is running on a CPU,
0041 no other softirq will preempt it, but a hardware interrupt can. However,
0042 any other CPUs in the system execute independently.
0043
0044 We'll see a number of ways that the user context can block interrupts,
0045 to become truly non-preemptable.
0046
0047 User Context
0048 ------------
0049
0050 User context is when you are coming in from a system call or other trap:
0051 like userspace, you can be preempted by more important tasks and by
0052 interrupts. You can sleep, by calling :c:func:`schedule()`.
0053
0054 .. note::
0055
0056 You are always in user context on module load and unload, and on
0057 operations on the block device layer.
0058
0059 In user context, the ``current`` pointer (indicating the task we are
0060 currently executing) is valid, and :c:func:`in_interrupt()`
0061 (``include/linux/preempt.h``) is false.
0062
0063 .. warning::
0064
0065 Beware that if you have preemption or softirqs disabled (see below),
0066 :c:func:`in_interrupt()` will return a false positive.
0067
0068 Hardware Interrupts (Hard IRQs)
0069 -------------------------------
0070
0071 Timer ticks, network cards and keyboard are examples of real hardware
0072 which produce interrupts at any time. The kernel runs interrupt
0073 handlers, which services the hardware. The kernel guarantees that this
0074 handler is never re-entered: if the same interrupt arrives, it is queued
0075 (or dropped). Because it disables interrupts, this handler has to be
0076 fast: frequently it simply acknowledges the interrupt, marks a 'software
0077 interrupt' for execution and exits.
0078
0079 You can tell you are in a hardware interrupt, because in_hardirq() returns
0080 true.
0081
0082 .. warning::
0083
0084 Beware that this will return a false positive if interrupts are
0085 disabled (see below).
0086
0087 Software Interrupt Context: Softirqs and Tasklets
0088 -------------------------------------------------
0089
0090 Whenever a system call is about to return to userspace, or a hardware
0091 interrupt handler exits, any 'software interrupts' which are marked
0092 pending (usually by hardware interrupts) are run (``kernel/softirq.c``).
0093
0094 Much of the real interrupt handling work is done here. Early in the
0095 transition to SMP, there were only 'bottom halves' (BHs), which didn't
0096 take advantage of multiple CPUs. Shortly after we switched from wind-up
0097 computers made of match-sticks and snot, we abandoned this limitation
0098 and switched to 'softirqs'.
0099
0100 ``include/linux/interrupt.h`` lists the different softirqs. A very
0101 important softirq is the timer softirq (``include/linux/timer.h``): you
0102 can register to have it call functions for you in a given length of
0103 time.
0104
0105 Softirqs are often a pain to deal with, since the same softirq will run
0106 simultaneously on more than one CPU. For this reason, tasklets
0107 (``include/linux/interrupt.h``) are more often used: they are
0108 dynamically-registrable (meaning you can have as many as you want), and
0109 they also guarantee that any tasklet will only run on one CPU at any
0110 time, although different tasklets can run simultaneously.
0111
0112 .. warning::
0113
0114 The name 'tasklet' is misleading: they have nothing to do with
0115 'tasks'.
0116
0117 You can tell you are in a softirq (or tasklet) using the
0118 :c:func:`in_softirq()` macro (``include/linux/preempt.h``).
0119
0120 .. warning::
0121
0122 Beware that this will return a false positive if a
0123 :ref:`botton half lock <local_bh_disable>` is held.
0124
0125 Some Basic Rules
0126 ================
0127
0128 No memory protection
0129 If you corrupt memory, whether in user context or interrupt context,
0130 the whole machine will crash. Are you sure you can't do what you
0131 want in userspace?
0132
0133 No floating point or MMX
0134 The FPU context is not saved; even in user context the FPU state
0135 probably won't correspond with the current process: you would mess
0136 with some user process' FPU state. If you really want to do this,
0137 you would have to explicitly save/restore the full FPU state (and
0138 avoid context switches). It is generally a bad idea; use fixed point
0139 arithmetic first.
0140
0141 A rigid stack limit
0142 Depending on configuration options the kernel stack is about 3K to
0143 6K for most 32-bit architectures: it's about 14K on most 64-bit
0144 archs, and often shared with interrupts so you can't use it all.
0145 Avoid deep recursion and huge local arrays on the stack (allocate
0146 them dynamically instead).
0147
0148 The Linux kernel is portable
0149 Let's keep it that way. Your code should be 64-bit clean, and
0150 endian-independent. You should also minimize CPU specific stuff,
0151 e.g. inline assembly should be cleanly encapsulated and minimized to
0152 ease porting. Generally it should be restricted to the
0153 architecture-dependent part of the kernel tree.
0154
0155 ioctls: Not writing a new system call
0156 =====================================
0157
0158 A system call generally looks like this::
0159
0160 asmlinkage long sys_mycall(int arg)
0161 {
0162 return 0;
0163 }
0164
0165
0166 First, in most cases you don't want to create a new system call. You
0167 create a character device and implement an appropriate ioctl for it.
0168 This is much more flexible than system calls, doesn't have to be entered
0169 in every architecture's ``include/asm/unistd.h`` and
0170 ``arch/kernel/entry.S`` file, and is much more likely to be accepted by
0171 Linus.
0172
0173 If all your routine does is read or write some parameter, consider
0174 implementing a :c:func:`sysfs()` interface instead.
0175
0176 Inside the ioctl you're in user context to a process. When a error
0177 occurs you return a negated errno (see
0178 ``include/uapi/asm-generic/errno-base.h``,
0179 ``include/uapi/asm-generic/errno.h`` and ``include/linux/errno.h``),
0180 otherwise you return 0.
0181
0182 After you slept you should check if a signal occurred: the Unix/Linux
0183 way of handling signals is to temporarily exit the system call with the
0184 ``-ERESTARTSYS`` error. The system call entry code will switch back to
0185 user context, process the signal handler and then your system call will
0186 be restarted (unless the user disabled that). So you should be prepared
0187 to process the restart, e.g. if you're in the middle of manipulating
0188 some data structure.
0189
0190 ::
0191
0192 if (signal_pending(current))
0193 return -ERESTARTSYS;
0194
0195
0196 If you're doing longer computations: first think userspace. If you
0197 **really** want to do it in kernel you should regularly check if you need
0198 to give up the CPU (remember there is cooperative multitasking per CPU).
0199 Idiom::
0200
0201 cond_resched(); /* Will sleep */
0202
0203
0204 A short note on interface design: the UNIX system call motto is "Provide
0205 mechanism not policy".
0206
0207 Recipes for Deadlock
0208 ====================
0209
0210 You cannot call any routines which may sleep, unless:
0211
0212 - You are in user context.
0213
0214 - You do not own any spinlocks.
0215
0216 - You have interrupts enabled (actually, Andi Kleen says that the
0217 scheduling code will enable them for you, but that's probably not
0218 what you wanted).
0219
0220 Note that some functions may sleep implicitly: common ones are the user
0221 space access functions (\*_user) and memory allocation functions
0222 without ``GFP_ATOMIC``.
0223
0224 You should always compile your kernel ``CONFIG_DEBUG_ATOMIC_SLEEP`` on,
0225 and it will warn you if you break these rules. If you **do** break the
0226 rules, you will eventually lock up your box.
0227
0228 Really.
0229
0230 Common Routines
0231 ===============
0232
0233 :c:func:`printk()`
0234 ------------------
0235
0236 Defined in ``include/linux/printk.h``
0237
0238 :c:func:`printk()` feeds kernel messages to the console, dmesg, and
0239 the syslog daemon. It is useful for debugging and reporting errors, and
0240 can be used inside interrupt context, but use with caution: a machine
0241 which has its console flooded with printk messages is unusable. It uses
0242 a format string mostly compatible with ANSI C printf, and C string
0243 concatenation to give it a first "priority" argument::
0244
0245 printk(KERN_INFO "i = %u\n", i);
0246
0247
0248 See ``include/linux/kern_levels.h``; for other ``KERN_`` values; these are
0249 interpreted by syslog as the level. Special case: for printing an IP
0250 address use::
0251
0252 __be32 ipaddress;
0253 printk(KERN_INFO "my ip: %pI4\n", &ipaddress);
0254
0255
0256 :c:func:`printk()` internally uses a 1K buffer and does not catch
0257 overruns. Make sure that will be enough.
0258
0259 .. note::
0260
0261 You will know when you are a real kernel hacker when you start
0262 typoing printf as printk in your user programs :)
0263
0264 .. note::
0265
0266 Another sidenote: the original Unix Version 6 sources had a comment
0267 on top of its printf function: "Printf should not be used for
0268 chit-chat". You should follow that advice.
0269
0270 :c:func:`copy_to_user()` / :c:func:`copy_from_user()` / :c:func:`get_user()` / :c:func:`put_user()`
0271 ---------------------------------------------------------------------------------------------------
0272
0273 Defined in ``include/linux/uaccess.h`` / ``asm/uaccess.h``
0274
0275 **[SLEEPS]**
0276
0277 :c:func:`put_user()` and :c:func:`get_user()` are used to get
0278 and put single values (such as an int, char, or long) from and to
0279 userspace. A pointer into userspace should never be simply dereferenced:
0280 data should be copied using these routines. Both return ``-EFAULT`` or
0281 0.
0282
0283 :c:func:`copy_to_user()` and :c:func:`copy_from_user()` are
0284 more general: they copy an arbitrary amount of data to and from
0285 userspace.
0286
0287 .. warning::
0288
0289 Unlike :c:func:`put_user()` and :c:func:`get_user()`, they
0290 return the amount of uncopied data (ie. 0 still means success).
0291
0292 [Yes, this objectionable interface makes me cringe. The flamewar comes
0293 up every year or so. --RR.]
0294
0295 The functions may sleep implicitly. This should never be called outside
0296 user context (it makes no sense), with interrupts disabled, or a
0297 spinlock held.
0298
0299 :c:func:`kmalloc()`/:c:func:`kfree()`
0300 -------------------------------------
0301
0302 Defined in ``include/linux/slab.h``
0303
0304 **[MAY SLEEP: SEE BELOW]**
0305
0306 These routines are used to dynamically request pointer-aligned chunks of
0307 memory, like malloc and free do in userspace, but
0308 :c:func:`kmalloc()` takes an extra flag word. Important values:
0309
0310 ``GFP_KERNEL``
0311 May sleep and swap to free memory. Only allowed in user context, but
0312 is the most reliable way to allocate memory.
0313
0314 ``GFP_ATOMIC``
0315 Don't sleep. Less reliable than ``GFP_KERNEL``, but may be called
0316 from interrupt context. You should **really** have a good
0317 out-of-memory error-handling strategy.
0318
0319 ``GFP_DMA``
0320 Allocate ISA DMA lower than 16MB. If you don't know what that is you
0321 don't need it. Very unreliable.
0322
0323 If you see a sleeping function called from invalid context warning
0324 message, then maybe you called a sleeping allocation function from
0325 interrupt context without ``GFP_ATOMIC``. You should really fix that.
0326 Run, don't walk.
0327
0328 If you are allocating at least ``PAGE_SIZE`` (``asm/page.h`` or
0329 ``asm/page_types.h``) bytes, consider using :c:func:`__get_free_pages()`
0330 (``include/linux/gfp.h``). It takes an order argument (0 for page sized,
0331 1 for double page, 2 for four pages etc.) and the same memory priority
0332 flag word as above.
0333
0334 If you are allocating more than a page worth of bytes you can use
0335 :c:func:`vmalloc()`. It'll allocate virtual memory in the kernel
0336 map. This block is not contiguous in physical memory, but the MMU makes
0337 it look like it is for you (so it'll only look contiguous to the CPUs,
0338 not to external device drivers). If you really need large physically
0339 contiguous memory for some weird device, you have a problem: it is
0340 poorly supported in Linux because after some time memory fragmentation
0341 in a running kernel makes it hard. The best way is to allocate the block
0342 early in the boot process via the :c:func:`alloc_bootmem()`
0343 routine.
0344
0345 Before inventing your own cache of often-used objects consider using a
0346 slab cache in ``include/linux/slab.h``
0347
0348 :c:macro:`current`
0349 ------------------
0350
0351 Defined in ``include/asm/current.h``
0352
0353 This global variable (really a macro) contains a pointer to the current
0354 task structure, so is only valid in user context. For example, when a
0355 process makes a system call, this will point to the task structure of
0356 the calling process. It is **not NULL** in interrupt context.
0357
0358 :c:func:`mdelay()`/:c:func:`udelay()`
0359 -------------------------------------
0360
0361 Defined in ``include/asm/delay.h`` / ``include/linux/delay.h``
0362
0363 The :c:func:`udelay()` and :c:func:`ndelay()` functions can be
0364 used for small pauses. Do not use large values with them as you risk
0365 overflow - the helper function :c:func:`mdelay()` is useful here, or
0366 consider :c:func:`msleep()`.
0367
0368 :c:func:`cpu_to_be32()`/:c:func:`be32_to_cpu()`/:c:func:`cpu_to_le32()`/:c:func:`le32_to_cpu()`
0369 -----------------------------------------------------------------------------------------------
0370
0371 Defined in ``include/asm/byteorder.h``
0372
0373 The :c:func:`cpu_to_be32()` family (where the "32" can be replaced
0374 by 64 or 16, and the "be" can be replaced by "le") are the general way
0375 to do endian conversions in the kernel: they return the converted value.
0376 All variations supply the reverse as well:
0377 :c:func:`be32_to_cpu()`, etc.
0378
0379 There are two major variations of these functions: the pointer
0380 variation, such as :c:func:`cpu_to_be32p()`, which take a pointer
0381 to the given type, and return the converted value. The other variation
0382 is the "in-situ" family, such as :c:func:`cpu_to_be32s()`, which
0383 convert value referred to by the pointer, and return void.
0384
0385 :c:func:`local_irq_save()`/:c:func:`local_irq_restore()`
0386 --------------------------------------------------------
0387
0388 Defined in ``include/linux/irqflags.h``
0389
0390 These routines disable hard interrupts on the local CPU, and restore
0391 them. They are reentrant; saving the previous state in their one
0392 ``unsigned long flags`` argument. If you know that interrupts are
0393 enabled, you can simply use :c:func:`local_irq_disable()` and
0394 :c:func:`local_irq_enable()`.
0395
0396 .. _local_bh_disable:
0397
0398 :c:func:`local_bh_disable()`/:c:func:`local_bh_enable()`
0399 --------------------------------------------------------
0400
0401 Defined in ``include/linux/bottom_half.h``
0402
0403
0404 These routines disable soft interrupts on the local CPU, and restore
0405 them. They are reentrant; if soft interrupts were disabled before, they
0406 will still be disabled after this pair of functions has been called.
0407 They prevent softirqs and tasklets from running on the current CPU.
0408
0409 :c:func:`smp_processor_id()`
0410 ----------------------------
0411
0412 Defined in ``include/linux/smp.h``
0413
0414 :c:func:`get_cpu()` disables preemption (so you won't suddenly get
0415 moved to another CPU) and returns the current processor number, between
0416 0 and ``NR_CPUS``. Note that the CPU numbers are not necessarily
0417 continuous. You return it again with :c:func:`put_cpu()` when you
0418 are done.
0419
0420 If you know you cannot be preempted by another task (ie. you are in
0421 interrupt context, or have preemption disabled) you can use
0422 smp_processor_id().
0423
0424 ``__init``/``__exit``/``__initdata``
0425 ------------------------------------
0426
0427 Defined in ``include/linux/init.h``
0428
0429 After boot, the kernel frees up a special section; functions marked with
0430 ``__init`` and data structures marked with ``__initdata`` are dropped
0431 after boot is complete: similarly modules discard this memory after
0432 initialization. ``__exit`` is used to declare a function which is only
0433 required on exit: the function will be dropped if this file is not
0434 compiled as a module. See the header file for use. Note that it makes no
0435 sense for a function marked with ``__init`` to be exported to modules
0436 with :c:func:`EXPORT_SYMBOL()` or :c:func:`EXPORT_SYMBOL_GPL()`- this
0437 will break.
0438
0439 :c:func:`__initcall()`/:c:func:`module_init()`
0440 ----------------------------------------------
0441
0442 Defined in ``include/linux/init.h`` / ``include/linux/module.h``
0443
0444 Many parts of the kernel are well served as a module
0445 (dynamically-loadable parts of the kernel). Using the
0446 :c:func:`module_init()` and :c:func:`module_exit()` macros it
0447 is easy to write code without #ifdefs which can operate both as a module
0448 or built into the kernel.
0449
0450 The :c:func:`module_init()` macro defines which function is to be
0451 called at module insertion time (if the file is compiled as a module),
0452 or at boot time: if the file is not compiled as a module the
0453 :c:func:`module_init()` macro becomes equivalent to
0454 :c:func:`__initcall()`, which through linker magic ensures that
0455 the function is called on boot.
0456
0457 The function can return a negative error number to cause module loading
0458 to fail (unfortunately, this has no effect if the module is compiled
0459 into the kernel). This function is called in user context with
0460 interrupts enabled, so it can sleep.
0461
0462 :c:func:`module_exit()`
0463 -----------------------
0464
0465
0466 Defined in ``include/linux/module.h``
0467
0468 This macro defines the function to be called at module removal time (or
0469 never, in the case of the file compiled into the kernel). It will only
0470 be called if the module usage count has reached zero. This function can
0471 also sleep, but cannot fail: everything must be cleaned up by the time
0472 it returns.
0473
0474 Note that this macro is optional: if it is not present, your module will
0475 not be removable (except for 'rmmod -f').
0476
0477 :c:func:`try_module_get()`/:c:func:`module_put()`
0478 -------------------------------------------------
0479
0480 Defined in ``include/linux/module.h``
0481
0482 These manipulate the module usage count, to protect against removal (a
0483 module also can't be removed if another module uses one of its exported
0484 symbols: see below). Before calling into module code, you should call
0485 :c:func:`try_module_get()` on that module: if it fails, then the
0486 module is being removed and you should act as if it wasn't there.
0487 Otherwise, you can safely enter the module, and call
0488 :c:func:`module_put()` when you're finished.
0489
0490 Most registerable structures have an owner field, such as in the
0491 :c:type:`struct file_operations <file_operations>` structure.
0492 Set this field to the macro ``THIS_MODULE``.
0493
0494 Wait Queues ``include/linux/wait.h``
0495 ====================================
0496
0497 **[SLEEPS]**
0498
0499 A wait queue is used to wait for someone to wake you up when a certain
0500 condition is true. They must be used carefully to ensure there is no
0501 race condition. You declare a :c:type:`wait_queue_head_t`, and then processes
0502 which want to wait for that condition declare a :c:type:`wait_queue_entry_t`
0503 referring to themselves, and place that in the queue.
0504
0505 Declaring
0506 ---------
0507
0508 You declare a ``wait_queue_head_t`` using the
0509 :c:func:`DECLARE_WAIT_QUEUE_HEAD()` macro, or using the
0510 :c:func:`init_waitqueue_head()` routine in your initialization
0511 code.
0512
0513 Queuing
0514 -------
0515
0516 Placing yourself in the waitqueue is fairly complex, because you must
0517 put yourself in the queue before checking the condition. There is a
0518 macro to do this: :c:func:`wait_event_interruptible()`
0519 (``include/linux/wait.h``) The first argument is the wait queue head, and
0520 the second is an expression which is evaluated; the macro returns 0 when
0521 this expression is true, or ``-ERESTARTSYS`` if a signal is received. The
0522 :c:func:`wait_event()` version ignores signals.
0523
0524 Waking Up Queued Tasks
0525 ----------------------
0526
0527 Call :c:func:`wake_up()` (``include/linux/wait.h``), which will wake
0528 up every process in the queue. The exception is if one has
0529 ``TASK_EXCLUSIVE`` set, in which case the remainder of the queue will
0530 not be woken. There are other variants of this basic function available
0531 in the same header.
0532
0533 Atomic Operations
0534 =================
0535
0536 Certain operations are guaranteed atomic on all platforms. The first
0537 class of operations work on :c:type:`atomic_t` (``include/asm/atomic.h``);
0538 this contains a signed integer (at least 32 bits long), and you must use
0539 these functions to manipulate or read :c:type:`atomic_t` variables.
0540 :c:func:`atomic_read()` and :c:func:`atomic_set()` get and set
0541 the counter, :c:func:`atomic_add()`, :c:func:`atomic_sub()`,
0542 :c:func:`atomic_inc()`, :c:func:`atomic_dec()`, and
0543 :c:func:`atomic_dec_and_test()` (returns true if it was
0544 decremented to zero).
0545
0546 Yes. It returns true (i.e. != 0) if the atomic variable is zero.
0547
0548 Note that these functions are slower than normal arithmetic, and so
0549 should not be used unnecessarily.
0550
0551 The second class of atomic operations is atomic bit operations on an
0552 ``unsigned long``, defined in ``include/linux/bitops.h``. These
0553 operations generally take a pointer to the bit pattern, and a bit
0554 number: 0 is the least significant bit. :c:func:`set_bit()`,
0555 :c:func:`clear_bit()` and :c:func:`change_bit()` set, clear,
0556 and flip the given bit. :c:func:`test_and_set_bit()`,
0557 :c:func:`test_and_clear_bit()` and
0558 :c:func:`test_and_change_bit()` do the same thing, except return
0559 true if the bit was previously set; these are particularly useful for
0560 atomically setting flags.
0561
0562 It is possible to call these operations with bit indices greater than
0563 ``BITS_PER_LONG``. The resulting behavior is strange on big-endian
0564 platforms though so it is a good idea not to do this.
0565
0566 Symbols
0567 =======
0568
0569 Within the kernel proper, the normal linking rules apply (ie. unless a
0570 symbol is declared to be file scope with the ``static`` keyword, it can
0571 be used anywhere in the kernel). However, for modules, a special
0572 exported symbol table is kept which limits the entry points to the
0573 kernel proper. Modules can also export symbols.
0574
0575 :c:func:`EXPORT_SYMBOL()`
0576 -------------------------
0577
0578 Defined in ``include/linux/export.h``
0579
0580 This is the classic method of exporting a symbol: dynamically loaded
0581 modules will be able to use the symbol as normal.
0582
0583 :c:func:`EXPORT_SYMBOL_GPL()`
0584 -----------------------------
0585
0586 Defined in ``include/linux/export.h``
0587
0588 Similar to :c:func:`EXPORT_SYMBOL()` except that the symbols
0589 exported by :c:func:`EXPORT_SYMBOL_GPL()` can only be seen by
0590 modules with a :c:func:`MODULE_LICENSE()` that specifies a GPL
0591 compatible license. It implies that the function is considered an
0592 internal implementation issue, and not really an interface. Some
0593 maintainers and developers may however require EXPORT_SYMBOL_GPL()
0594 when adding any new APIs or functionality.
0595
0596 :c:func:`EXPORT_SYMBOL_NS()`
0597 ----------------------------
0598
0599 Defined in ``include/linux/export.h``
0600
0601 This is the variant of `EXPORT_SYMBOL()` that allows specifying a symbol
0602 namespace. Symbol Namespaces are documented in
0603 Documentation/core-api/symbol-namespaces.rst
0604
0605 :c:func:`EXPORT_SYMBOL_NS_GPL()`
0606 --------------------------------
0607
0608 Defined in ``include/linux/export.h``
0609
0610 This is the variant of `EXPORT_SYMBOL_GPL()` that allows specifying a symbol
0611 namespace. Symbol Namespaces are documented in
0612 Documentation/core-api/symbol-namespaces.rst
0613
0614 Routines and Conventions
0615 ========================
0616
0617 Double-linked lists ``include/linux/list.h``
0618 --------------------------------------------
0619
0620 There used to be three sets of linked-list routines in the kernel
0621 headers, but this one is the winner. If you don't have some particular
0622 pressing need for a single list, it's a good choice.
0623
0624 In particular, :c:func:`list_for_each_entry()` is useful.
0625
0626 Return Conventions
0627 ------------------
0628
0629 For code called in user context, it's very common to defy C convention,
0630 and return 0 for success, and a negative error number (eg. ``-EFAULT``) for
0631 failure. This can be unintuitive at first, but it's fairly widespread in
0632 the kernel.
0633
0634 Using :c:func:`ERR_PTR()` (``include/linux/err.h``) to encode a
0635 negative error number into a pointer, and :c:func:`IS_ERR()` and
0636 :c:func:`PTR_ERR()` to get it back out again: avoids a separate
0637 pointer parameter for the error number. Icky, but in a good way.
0638
0639 Breaking Compilation
0640 --------------------
0641
0642 Linus and the other developers sometimes change function or structure
0643 names in development kernels; this is not done just to keep everyone on
0644 their toes: it reflects a fundamental change (eg. can no longer be
0645 called with interrupts on, or does extra checks, or doesn't do checks
0646 which were caught before). Usually this is accompanied by a fairly
0647 complete note to the appropriate kernel development mailing list; search
0648 the archives. Simply doing a global replace on the file usually makes
0649 things **worse**.
0650
0651 Initializing structure members
0652 ------------------------------
0653
0654 The preferred method of initializing structures is to use designated
0655 initialisers, as defined by ISO C99, eg::
0656
0657 static struct block_device_operations opt_fops = {
0658 .open = opt_open,
0659 .release = opt_release,
0660 .ioctl = opt_ioctl,
0661 .check_media_change = opt_media_change,
0662 };
0663
0664
0665 This makes it easy to grep for, and makes it clear which structure
0666 fields are set. You should do this because it looks cool.
0667
0668 GNU Extensions
0669 --------------
0670
0671 GNU Extensions are explicitly allowed in the Linux kernel. Note that
0672 some of the more complex ones are not very well supported, due to lack
0673 of general use, but the following are considered standard (see the GCC
0674 info page section "C Extensions" for more details - Yes, really the info
0675 page, the man page is only a short summary of the stuff in info).
0676
0677 - Inline functions
0678
0679 - Statement expressions (ie. the ({ and }) constructs).
0680
0681 - Declaring attributes of a function / variable / type
0682 (__attribute__)
0683
0684 - typeof
0685
0686 - Zero length arrays
0687
0688 - Macro varargs
0689
0690 - Arithmetic on void pointers
0691
0692 - Non-Constant initializers
0693
0694 - Assembler Instructions (not outside arch/ and include/asm/)
0695
0696 - Function names as strings (__func__).
0697
0698 - __builtin_constant_p()
0699
0700 Be wary when using long long in the kernel, the code gcc generates for
0701 it is horrible and worse: division and multiplication does not work on
0702 i386 because the GCC runtime functions for it are missing from the
0703 kernel environment.
0704
0705 C++
0706 ---
0707
0708 Using C++ in the kernel is usually a bad idea, because the kernel does
0709 not provide the necessary runtime environment and the include files are
0710 not tested for it. It is still possible, but not recommended. If you
0711 really want to do this, forget about exceptions at least.
0712
0713 #if
0714 ---
0715
0716 It is generally considered cleaner to use macros in header files (or at
0717 the top of .c files) to abstract away functions rather than using \`#if'
0718 pre-processor statements throughout the source code.
0719
0720 Putting Your Stuff in the Kernel
0721 ================================
0722
0723 In order to get your stuff into shape for official inclusion, or even to
0724 make a neat patch, there's administrative work to be done:
0725
0726 - Figure out who are the owners of the code you've been modifying. Look
0727 at the top of the source files, inside the ``MAINTAINERS`` file, and
0728 last of all in the ``CREDITS`` file. You should coordinate with these
0729 people to make sure you're not duplicating effort, or trying something
0730 that's already been rejected.
0731
0732 Make sure you put your name and email address at the top of any files
0733 you create or modify significantly. This is the first place people
0734 will look when they find a bug, or when **they** want to make a change.
0735
0736 - Usually you want a configuration option for your kernel hack. Edit
0737 ``Kconfig`` in the appropriate directory. The Config language is
0738 simple to use by cut and paste, and there's complete documentation in
0739 ``Documentation/kbuild/kconfig-language.rst``.
0740
0741 In your description of the option, make sure you address both the
0742 expert user and the user who knows nothing about your feature.
0743 Mention incompatibilities and issues here. **Definitely** end your
0744 description with “if in doubt, say N” (or, occasionally, \`Y'); this
0745 is for people who have no idea what you are talking about.
0746
0747 - Edit the ``Makefile``: the CONFIG variables are exported here so you
0748 can usually just add a "obj-$(CONFIG_xxx) += xxx.o" line. The syntax
0749 is documented in ``Documentation/kbuild/makefiles.rst``.
0750
0751 - Put yourself in ``CREDITS`` if you consider what you've done
0752 noteworthy, usually beyond a single file (your name should be at the
0753 top of the source files anyway). ``MAINTAINERS`` means you want to be
0754 consulted when changes are made to a subsystem, and hear about bugs;
0755 it implies a more-than-passing commitment to some part of the code.
0756
0757 - Finally, don't forget to read
0758 ``Documentation/process/submitting-patches.rst``
0759
0760 Kernel Cantrips
0761 ===============
0762
0763 Some favorites from browsing the source. Feel free to add to this list.
0764
0765 ``arch/x86/include/asm/delay.h``::
0766
0767 #define ndelay(n) (__builtin_constant_p(n) ? \
0768 ((n) > 20000 ? __bad_ndelay() : __const_udelay((n) * 5ul)) : \
0769 __ndelay(n))
0770
0771
0772 ``include/linux/fs.h``::
0773
0774 /*
0775 * Kernel pointers have redundant information, so we can use a
0776 * scheme where we can return either an error code or a dentry
0777 * pointer with the same return value.
0778 *
0779 * This should be a per-architecture thing, to allow different
0780 * error and pointer decisions.
0781 */
0782 #define ERR_PTR(err) ((void *)((long)(err)))
0783 #define PTR_ERR(ptr) ((long)(ptr))
0784 #define IS_ERR(ptr) ((unsigned long)(ptr) > (unsigned long)(-1000))
0785
0786 ``arch/x86/include/asm/uaccess_32.h:``::
0787
0788 #define copy_to_user(to,from,n) \
0789 (__builtin_constant_p(n) ? \
0790 __constant_copy_to_user((to),(from),(n)) : \
0791 __generic_copy_to_user((to),(from),(n)))
0792
0793
0794 ``arch/sparc/kernel/head.S:``::
0795
0796 /*
0797 * Sun people can't spell worth damn. "compatability" indeed.
0798 * At least we *know* we can't spell, and use a spell-checker.
0799 */
0800
0801 /* Uh, actually Linus it is I who cannot spell. Too much murky
0802 * Sparc assembly will do this to ya.
0803 */
0804 C_LABEL(cputypvar):
0805 .asciz "compatibility"
0806
0807 /* Tested on SS-5, SS-10. Probably someone at Sun applied a spell-checker. */
0808 .align 4
0809 C_LABEL(cputypvar_sun4m):
0810 .asciz "compatible"
0811
0812
0813 ``arch/sparc/lib/checksum.S:``::
0814
0815 /* Sun, you just can't beat me, you just can't. Stop trying,
0816 * give up. I'm serious, I am going to kick the living shit
0817 * out of you, game over, lights out.
0818 */
0819
0820
0821 Thanks
0822 ======
0823
0824 Thanks to Andi Kleen for the idea, answering my questions, fixing my
0825 mistakes, filling content, etc. Philipp Rumpf for more spelling and
0826 clarity fixes, and some excellent non-obvious points. Werner Almesberger
0827 for giving me a great summary of :c:func:`disable_irq()`, and Jes
0828 Sorensen and Andrea Arcangeli added caveats. Michael Elizabeth Chastain
0829 for checking and adding to the Configure section. Telsa Gwynne for
0830 teaching me DocBook.