sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget*/translations/zh_CN/kernel-hacking/hackingmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/zh_TW/kernel-hacking/hackingmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/it_IT/kernel-hacking/hackingmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/ja_JP/kernel-hacking/hackingmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/ko_KR/kernel-hacking/hackingmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget*/translations/sp_SP/kernel-hacking/hackingmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhtarget)}(h.. _kernel_hacking_hack:h]h}(h]h ]h"]h$]h&]refidkernel-hacking-hackuh1hhKhhhhhD/var/lib/git/docbuild/linux/Documentation/kernel-hacking/hacking.rstubhsection)}(hhh](htitle)}(h,Unreliable Guide To Hacking The Linux Kernelh]h,Unreliable Guide To Hacking The Linux Kernel}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh field_list)}(hhh]hfield)}(hhh](h field_name)}(hAuthorh]hAuthor}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh field_body)}(hRusty Russell h]h paragraph)}(h Rusty Russellh]h Rusty Russell}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h Introductionh]h Introduction}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK ubh)}(hXuWelcome, gentle reader, to Rusty's Remarkably Unreliable Guide to Linux Kernel Hacking. This document describes the common routines and general requirements for kernel code: its goal is to serve as a primer for Linux kernel development for experienced C programmers. I avoid implementation details: that's what the code is for, and I ignore whole tracts of useful routines.h]hXyWelcome, gentle reader, to Rusty’s Remarkably Unreliable Guide to Linux Kernel Hacking. This document describes the common routines and general requirements for kernel code: its goal is to serve as a primer for Linux kernel development for experienced C programmers. I avoid implementation details: that’s what the code is for, and I ignore whole tracts of useful routines.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hjhhubh)}(hXBefore you read this, please understand that I never wanted to write this document, being grossly under-qualified, but I always wanted to read it, and this was the only way. I hope it will grow into a compendium of best practice, common starting points and random information.h]hXBefore you read this, please understand that I never wanted to write this document, being grossly under-qualified, but I always wanted to read it, and this was the only way. I hope it will grow into a compendium of best practice, common starting points and random information.}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h] introductionah ]h"] introductionah$]h&]uh1hhhhhhhhK ubh)}(hhh](h)}(h The Playersh]h The Players}(hj?hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj<hhhhhKubh)}(h0At any time each of the CPUs in a system can be:h]h0At any time each of the CPUs in a system can be:}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj<hhubh bullet_list)}(hhh](h list_item)}(h?not associated with any process, serving a hardware interrupt; h]h)}(h>not associated with any process, serving a hardware interrupt;h]h>not associated with any process, serving a hardware interrupt;}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjbubah}(h]h ]h"]h$]h&]uh1j`hj]hhhhhNubja)}(h?not associated with any process, serving a softirq or tasklet; h]h)}(h>not associated with any process, serving a softirq or tasklet;h]h>not associated with any process, serving a softirq or tasklet;}(hj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hjzubah}(h]h ]h"]h$]h&]uh1j`hj]hhhhhNubja)}(hCrunning in kernel space, associated with a process (user context); h]h)}(hBrunning in kernel space, associated with a process (user context);h]hBrunning in kernel space, associated with a process (user context);}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK"hjubah}(h]h ]h"]h$]h&]uh1j`hj]hhhhhNubja)}(h!running a process in user space. h]h)}(h running a process in user space.h]h running a process in user space.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK$hjubah}(h]h ]h"]h$]h&]uh1j`hj]hhhhhNubeh}(h]h ]h"]h$]h&]bullet-uh1j[hhhKhj<hhubh)}(hXPThere is an ordering between these. The bottom two can preempt each other, but above that is a strict hierarchy: each can only be preempted by the ones above it. For example, while a softirq is running on a CPU, no other softirq will preempt it, but a hardware interrupt can. However, any other CPUs in the system execute independently.h]hXPThere is an ordering between these. The bottom two can preempt each other, but above that is a strict hierarchy: each can only be preempted by the ones above it. For example, while a softirq is running on a CPU, no other softirq will preempt it, but a hardware interrupt can. However, any other CPUs in the system execute independently.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK&hj<hhubh)}(hgWe'll see a number of ways that the user context can block interrupts, to become truly non-preemptable.h]hiWe’ll see a number of ways that the user context can block interrupts, to become truly non-preemptable.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK,hj<hhubh)}(hhh](h)}(h User Contexth]h User Context}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK0ubh)}(hUser context is when you are coming in from a system call or other trap: like userspace, you can be preempted by more important tasks and by interrupts. You can sleep, by calling :c:func:`schedule()`.h](hUser context is when you are coming in from a system call or other trap: like userspace, you can be preempted by more important tasks and by interrupts. You can sleep, by calling }(hjhhhNhNubh)}(h:c:func:`schedule()`h]hliteral)}(hjh]h schedule()}(hjhhhNhNubah}(h]h ](xrefcc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdockernel-hacking/hacking refdomainjreftypefunc refexplicitrefwarn reftargetscheduleuh1hhhhK2hjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK2hjhhubhnote)}(hfYou are always in user context on module load and unload, and on operations on the block device layer.h]h)}(hfYou are always in user context on module load and unload, and on operations on the block device layer.h]hfYou are always in user context on module load and unload, and on operations on the block device layer.}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK8hj0ubah}(h]h ]h"]h$]h&]uh1j.hjhhhhhNubh)}(hIn user context, the ``current`` pointer (indicating the task we are currently executing) is valid, and :c:func:`in_interrupt()` (``include/linux/preempt.h``) is false.h](hIn user context, the }(hjHhhhNhNubj)}(h ``current``h]hcurrent}(hjPhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjHubhH pointer (indicating the task we are currently executing) is valid, and }(hjHhhhNhNubh)}(h:c:func:`in_interrupt()`h]j)}(hjdh]hin_interrupt()}(hjfhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjbubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" in_interruptuh1hhhhK;hjHubh (}(hjHhhhNhNubj)}(h``include/linux/preempt.h``h]hinclude/linux/preempt.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjHubh ) is false.}(hjHhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK;hjhhubhwarning)}(h{Beware that if you have preemption or softirqs disabled (see below), :c:func:`in_interrupt()` will return a false positive.h]h)}(h{Beware that if you have preemption or softirqs disabled (see below), :c:func:`in_interrupt()` will return a false positive.h](hEBeware that if you have preemption or softirqs disabled (see below), }(hjhhhNhNubh)}(h:c:func:`in_interrupt()`h]j)}(hjh]hin_interrupt()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" in_interruptuh1hhhhKAhjubh will return a false positive.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKAhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h] user-contextah ]h"] user contextah$]h&]uh1hhj<hhhhhK0ubh)}(hhh](h)}(hHardware Interrupts (Hard IRQs)h]hHardware Interrupts (Hard IRQs)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKEubh)}(hXTimer ticks, network cards and keyboard are examples of real hardware which produce interrupts at any time. The kernel runs interrupt handlers, which services the hardware. The kernel guarantees that this handler is never re-entered: if the same interrupt arrives, it is queued (or dropped). Because it disables interrupts, this handler has to be fast: frequently it simply acknowledges the interrupt, marks a 'software interrupt' for execution and exits.h]hXTimer ticks, network cards and keyboard are examples of real hardware which produce interrupts at any time. The kernel runs interrupt handlers, which services the hardware. The kernel guarantees that this handler is never re-entered: if the same interrupt arrives, it is queued (or dropped). Because it disables interrupts, this handler has to be fast: frequently it simply acknowledges the interrupt, marks a ‘software interrupt’ for execution and exits.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKGhjhhubh)}(hPYou can tell you are in a hardware interrupt, because in_hardirq() returns true.h]hPYou can tell you are in a hardware interrupt, because in_hardirq() returns true.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKOhjhhubj)}(hUBeware that this will return a false positive if interrupts are disabled (see below).h]h)}(hUBeware that this will return a false positive if interrupts are disabled (see below).h]hUBeware that this will return a false positive if interrupts are disabled (see below).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKThjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]hardware-interrupts-hard-irqsah ]h"]hardware interrupts (hard irqs)ah$]h&]uh1hhj<hhhhhKEubh)}(hhh](h)}(h1Software Interrupt Context: Softirqs and Taskletsh]h1Software Interrupt Context: Softirqs and Tasklets}(hj2hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj/hhhhhKXubh)}(hWhenever a system call is about to return to userspace, or a hardware interrupt handler exits, any 'software interrupts' which are marked pending (usually by hardware interrupts) are run (``kernel/softirq.c``).h](hWhenever a system call is about to return to userspace, or a hardware interrupt handler exits, any ‘software interrupts’ which are marked pending (usually by hardware interrupts) are run (}(hj@hhhNhNubj)}(h``kernel/softirq.c``h]hkernel/softirq.c}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj@ubh).}(hj@hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKZhj/hhubh)}(hX4Much of the real interrupt handling work is done here. Early in the transition to SMP, there were only 'bottom halves' (BHs), which didn't take advantage of multiple CPUs. Shortly after we switched from wind-up computers made of match-sticks and snot, we abandoned this limitation and switched to 'softirqs'.h]hX>Much of the real interrupt handling work is done here. Early in the transition to SMP, there were only ‘bottom halves’ (BHs), which didn’t take advantage of multiple CPUs. Shortly after we switched from wind-up computers made of match-sticks and snot, we abandoned this limitation and switched to ‘softirqs’.}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK^hj/hhubh)}(h``include/linux/interrupt.h`` lists the different softirqs. A very important softirq is the timer softirq (``include/linux/timer.h``): you can register to have it call functions for you in a given length of time.h](j)}(h``include/linux/interrupt.h``h]hinclude/linux/interrupt.h}(hjrhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjnubhN lists the different softirqs. A very important softirq is the timer softirq (}(hjnhhhNhNubj)}(h``include/linux/timer.h``h]hinclude/linux/timer.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjnubhP): you can register to have it call functions for you in a given length of time.}(hjnhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKdhj/hhubh)}(hXSoftirqs are often a pain to deal with, since the same softirq will run simultaneously on more than one CPU. For this reason, tasklets (``include/linux/interrupt.h``) are more often used: they are dynamically-registrable (meaning you can have as many as you want), and they also guarantee that any tasklet will only run on one CPU at any time, although different tasklets can run simultaneously.h](hSoftirqs are often a pain to deal with, since the same softirq will run simultaneously on more than one CPU. For this reason, tasklets (}(hjhhhNhNubj)}(h``include/linux/interrupt.h``h]hinclude/linux/interrupt.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh) are more often used: they are dynamically-registrable (meaning you can have as many as you want), and they also guarantee that any tasklet will only run on one CPU at any time, although different tasklets can run simultaneously.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKihj/hhubj)}(hGThe name 'tasklet' is misleading: they have nothing to do with 'tasks'.h]h)}(hGThe name 'tasklet' is misleading: they have nothing to do with 'tasks'.h]hOThe name ‘tasklet’ is misleading: they have nothing to do with ‘tasks’.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKrhjubah}(h]h ]h"]h$]h&]uh1jhj/hhhhhNubh)}(htYou can tell you are in a softirq (or tasklet) using the :c:func:`in_softirq()` macro (``include/linux/preempt.h``).h](h9You can tell you are in a softirq (or tasklet) using the }(hjhhhNhNubh)}(h:c:func:`in_softirq()`h]j)}(hjh]h in_softirq()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" in_softirquh1hhhhKuhjubh macro (}(hjhhhNhNubj)}(h``include/linux/preempt.h``h]hinclude/linux/preempt.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh).}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKuhj/hhubj)}(hfBeware that this will return a false positive if a :ref:`bottom half lock ` is held.h]h)}(hfBeware that this will return a false positive if a :ref:`bottom half lock ` is held.h](h3Beware that this will return a false positive if a }(hjhhhNhNubh)}(h*:ref:`bottom half lock `h]hinline)}(hj%h]hbottom half lock}(hj)hhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1j'hj#ubah}(h]h ]h"]h$]h&]refdocj refdomainj3reftyperef refexplicitrefwarnj"local_bh_disableuh1hhhhKzhjubh is held.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKzhjubah}(h]h ]h"]h$]h&]uh1jhj/hhhhhNubeh}(h]0software-interrupt-context-softirqs-and-taskletsah ]h"]1software interrupt context: softirqs and taskletsah$]h&]uh1hhj<hhhhhKXubeh}(h] the-playersah ]h"] the playersah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hSome Basic Rulesh]hSome Basic Rules}(hjhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjehhhhhK~ubhdefinition_list)}(hhh](hdefinition_list_item)}(hNo memory protection If you corrupt memory, whether in user context or interrupt context, the whole machine will crash. Are you sure you can't do what you want in userspace? h](hterm)}(hNo memory protectionh]hNo memory protection}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhj}ubh definition)}(hhh]h)}(hIf you corrupt memory, whether in user context or interrupt context, the whole machine will crash. Are you sure you can't do what you want in userspace?h]hIf you corrupt memory, whether in user context or interrupt context, the whole machine will crash. Are you sure you can’t do what you want in userspace?}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhj}ubeh}(h]h ]h"]h$]h&]uh1j{hhhKhjxubj|)}(hXxNo floating point or MMX The FPU context is not saved; even in user context the FPU state probably won't correspond with the current process: you would mess with some user process' FPU state. If you really want to do this, you would have to explicitly save/restore the full FPU state (and avoid context switches). It is generally a bad idea; use fixed point arithmetic first. h](j)}(hNo floating point or MMXh]hNo floating point or MMX}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(hhh]h)}(hX^The FPU context is not saved; even in user context the FPU state probably won't correspond with the current process: you would mess with some user process' FPU state. If you really want to do this, you would have to explicitly save/restore the full FPU state (and avoid context switches). It is generally a bad idea; use fixed point arithmetic first.h]hXbThe FPU context is not saved; even in user context the FPU state probably won’t correspond with the current process: you would mess with some user process’ FPU state. If you really want to do this, you would have to explicitly save/restore the full FPU state (and avoid context switches). It is generally a bad idea; use fixed point arithmetic first.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1j{hhhKhjxhhubj|)}(hX5A rigid stack limit Depending on configuration options the kernel stack is about 3K to 6K for most 32-bit architectures: it's about 14K on most 64-bit archs, and often shared with interrupts so you can't use it all. Avoid deep recursion and huge local arrays on the stack (allocate them dynamically instead). h](j)}(hA rigid stack limith]hA rigid stack limit}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(hhh]h)}(hX Depending on configuration options the kernel stack is about 3K to 6K for most 32-bit architectures: it's about 14K on most 64-bit archs, and often shared with interrupts so you can't use it all. Avoid deep recursion and huge local arrays on the stack (allocate them dynamically instead).h]hX$Depending on configuration options the kernel stack is about 3K to 6K for most 32-bit architectures: it’s about 14K on most 64-bit archs, and often shared with interrupts so you can’t use it all. Avoid deep recursion and huge local arrays on the stack (allocate them dynamically instead).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1j{hhhKhjxhhubj|)}(hXHThe Linux kernel is portable Let's keep it that way. Your code should be 64-bit clean, and endian-independent. You should also minimize CPU specific stuff, e.g. inline assembly should be cleanly encapsulated and minimized to ease porting. Generally it should be restricted to the architecture-dependent part of the kernel tree. h](j)}(hThe Linux kernel is portableh]hThe Linux kernel is portable}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhhKhjubj)}(hhh]h)}(hX*Let's keep it that way. Your code should be 64-bit clean, and endian-independent. You should also minimize CPU specific stuff, e.g. inline assembly should be cleanly encapsulated and minimized to ease porting. Generally it should be restricted to the architecture-dependent part of the kernel tree.h]hX,Let’s keep it that way. Your code should be 64-bit clean, and endian-independent. You should also minimize CPU specific stuff, e.g. inline assembly should be cleanly encapsulated and minimized to ease porting. Generally it should be restricted to the architecture-dependent part of the kernel tree.}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1j{hhhKhjxhhubeh}(h]h ]h"]h$]h&]uh1jvhjehhhhhNubeh}(h]some-basic-rulesah ]h"]some basic rulesah$]h&]uh1hhhhhhhhK~ubh)}(hhh](h)}(h%ioctls: Not writing a new system callh]h%ioctls: Not writing a new system call}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjKhhhhhKubh)}(h)A system call generally looks like this::h]h(A system call generally looks like this:}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjKhhubh literal_block)}(h9asmlinkage long sys_mycall(int arg) { return 0; }h]h9asmlinkage long sys_mycall(int arg) { return 0; }}hjlsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1jjhhhKhjKhhubh)}(hXVFirst, in most cases you don't want to create a new system call. You create a character device and implement an appropriate ioctl for it. This is much more flexible than system calls, doesn't have to be entered in every architecture's ``include/asm/unistd.h`` and ``arch/kernel/entry.S`` file, and is much more likely to be accepted by Linus.h](hFirst, in most cases you don’t want to create a new system call. You create a character device and implement an appropriate ioctl for it. This is much more flexible than system calls, doesn’t have to be entered in every architecture’s }(hj|hhhNhNubj)}(h``include/asm/unistd.h``h]hinclude/asm/unistd.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj|ubh and }(hj|hhhNhNubj)}(h``arch/kernel/entry.S``h]harch/kernel/entry.S}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj|ubh7 file, and is much more likely to be accepted by Linus.}(hj|hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjKhhubh)}(hvIf all your routine does is read or write some parameter, consider implementing a :c:func:`sysfs()` interface instead.h](hRIf all your routine does is read or write some parameter, consider implementing a }(hjhhhNhNubh)}(h:c:func:`sysfs()`h]j)}(hjh]hsysfs()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"sysfsuh1hhhhKhjubh interface instead.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjKhhubh)}(hInside the ioctl you're in user context to a process. When a error occurs you return a negated errno (see ``include/uapi/asm-generic/errno-base.h``, ``include/uapi/asm-generic/errno.h`` and ``include/linux/errno.h``), otherwise you return 0.h](hlInside the ioctl you’re in user context to a process. When a error occurs you return a negated errno (see }(hjhhhNhNubj)}(h)``include/uapi/asm-generic/errno-base.h``h]h%include/uapi/asm-generic/errno-base.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, }(hjhhhNhNubj)}(h$``include/uapi/asm-generic/errno.h``h]h include/uapi/asm-generic/errno.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh and }(hjhhhNhNubj)}(h``include/linux/errno.h``h]hinclude/linux/errno.h}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh), otherwise you return 0.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjKhhubh)}(hXAfter you slept you should check if a signal occurred: the Unix/Linux way of handling signals is to temporarily exit the system call with the ``-ERESTARTSYS`` error. The system call entry code will switch back to user context, process the signal handler and then your system call will be restarted (unless the user disabled that). So you should be prepared to process the restart, e.g. if you're in the middle of manipulating some data structure.h](hAfter you slept you should check if a signal occurred: the Unix/Linux way of handling signals is to temporarily exit the system call with the }(hj#hhhNhNubj)}(h``-ERESTARTSYS``h]h -ERESTARTSYS}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj#ubhX" error. The system call entry code will switch back to user context, process the signal handler and then your system call will be restarted (unless the user disabled that). So you should be prepared to process the restart, e.g. if you’re in the middle of manipulating some data structure.}(hj#hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjKhhubjk)}(h9if (signal_pending(current)) return -ERESTARTSYS;h]h9if (signal_pending(current)) return -ERESTARTSYS;}hjCsbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhKhjKhhubh)}(hIf you're doing longer computations: first think userspace. If you **really** want to do it in kernel you should regularly check if you need to give up the CPU (remember there is cooperative multitasking per CPU). Idiom::h](hEIf you’re doing longer computations: first think userspace. If you }(hjQhhhNhNubhstrong)}(h **really**h]hreally}(hj[hhhNhNubah}(h]h ]h"]h$]h&]uh1jYhjQubh want to do it in kernel you should regularly check if you need to give up the CPU (remember there is cooperative multitasking per CPU). Idiom:}(hjQhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjKhhubjk)}(h cond_resched(); /* Will sleep */h]h cond_resched(); /* Will sleep */}hjssbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhKhjKhhubh)}(h_A short note on interface design: the UNIX system call motto is "Provide mechanism not policy".h]hcA short note on interface design: the UNIX system call motto is “Provide mechanism not policy”.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjKhhubeh}(h]$ioctls-not-writing-a-new-system-callah ]h"]%ioctls: not writing a new system callah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hRecipes for Deadlockh]hRecipes for Deadlock}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(h5You cannot call any routines which may sleep, unless:h]h5You cannot call any routines which may sleep, unless:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj\)}(hhh](ja)}(hYou are in user context. h]h)}(hYou are in user context.h]hYou are in user context.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(hYou do not own any spinlocks. h]h)}(hYou do not own any spinlocks.h]hYou do not own any spinlocks.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(hYou have interrupts enabled (actually, Andi Kleen says that the scheduling code will enable them for you, but that's probably not what you wanted). h]h)}(hYou have interrupts enabled (actually, Andi Kleen says that the scheduling code will enable them for you, but that's probably not what you wanted).h]hYou have interrupts enabled (actually, Andi Kleen says that the scheduling code will enable them for you, but that’s probably not what you wanted).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1j[hhhKhjhhubh)}(hNote that some functions may sleep implicitly: common ones are the user space access functions (\*_user) and memory allocation functions without ``GFP_ATOMIC``.h](hNote that some functions may sleep implicitly: common ones are the user space access functions (*_user) and memory allocation functions without }(hjhhhNhNubj)}(h``GFP_ATOMIC``h]h GFP_ATOMIC}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hYou should always compile your kernel ``CONFIG_DEBUG_ATOMIC_SLEEP`` on, and it will warn you if you break these rules. If you **do** break the rules, you will eventually lock up your box.h](h&You should always compile your kernel }(hj'hhhNhNubj)}(h``CONFIG_DEBUG_ATOMIC_SLEEP``h]hCONFIG_DEBUG_ATOMIC_SLEEP}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj'ubh; on, and it will warn you if you break these rules. If you }(hj'hhhNhNubjZ)}(h**do**h]hdo}(hjAhhhNhNubah}(h]h ]h"]h$]h&]uh1jYhj'ubh7 break the rules, you will eventually lock up your box.}(hj'hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hReally.h]hReally.}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]recipes-for-deadlockah ]h"]recipes for deadlockah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hCommon Routinesh]hCommon Routines}(hjrhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjohhhhhKubh)}(hhh](h)}(h:c:func:`printk()`h]h)}(hjh]j)}(hjh]hprintk()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"printkuh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(h%Defined in ``include/linux/printk.h``h](h Defined in }(hjhhhNhNubj)}(h``include/linux/printk.h``h]hinclude/linux/printk.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hX:c:func:`printk()` feeds kernel messages to the console, dmesg, and the syslog daemon. It is useful for debugging and reporting errors, and can be used inside interrupt context, but use with caution: a machine which has its console flooded with printk messages is unusable. It uses a format string mostly compatible with ANSI C printf, and C string concatenation to give it a first "priority" argument::h](h)}(h:c:func:`printk()`h]j)}(hjh]hprintk()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"printkuh1hhhhKhjubhX feeds kernel messages to the console, dmesg, and the syslog daemon. It is useful for debugging and reporting errors, and can be used inside interrupt context, but use with caution: a machine which has its console flooded with printk messages is unusable. It uses a format string mostly compatible with ANSI C printf, and C string concatenation to give it a first “priority” argument:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjk)}(h printk(KERN_INFO "i = %u\n", i);h]h printk(KERN_INFO "i = %u\n", i);}hjsbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhKhjhhubh)}(hSee ``include/linux/kern_levels.h``; for other ``KERN_`` values; these are interpreted by syslog as the level. Special case: for printing an IP address use::h](hSee }(hjhhhNhNubj)}(h``include/linux/kern_levels.h``h]hinclude/linux/kern_levels.h}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh ; for other }(hjhhhNhNubj)}(h ``KERN_``h]hKERN_}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhd values; these are interpreted by syslog as the level. Special case: for printing an IP address use:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjk)}(h@__be32 ipaddress; printk(KERN_INFO "my ip: %pI4\n", &ipaddress);h]h@__be32 ipaddress; printk(KERN_INFO "my ip: %pI4\n", &ipaddress);}hj4sbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhKhjhhubh)}(hj:c:func:`printk()` internally uses a 1K buffer and does not catch overruns. Make sure that will be enough.h](h)}(h:c:func:`printk()`h]j)}(hjHh]hprintk()}(hjJhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjFubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"printkuh1hhhhMhjBubhX internally uses a 1K buffer and does not catch overruns. Make sure that will be enough.}(hjBhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj/)}(hpYou will know when you are a real kernel hacker when you start typoing printf as printk in your user programs :)h]h)}(hpYou will know when you are a real kernel hacker when you start typoing printf as printk in your user programs :)h]hpYou will know when you are a real kernel hacker when you start typoing printf as printk in your user programs :)}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjoubah}(h]h ]h"]h$]h&]uh1j.hjhhhhhNubj/)}(hAnother sidenote: the original Unix Version 6 sources had a comment on top of its printf function: "Printf should not be used for chit-chat". You should follow that advice.h]h)}(hAnother sidenote: the original Unix Version 6 sources had a comment on top of its printf function: "Printf should not be used for chit-chat". You should follow that advice.h]hAnother sidenote: the original Unix Version 6 sources had a comment on top of its printf function: “Printf should not be used for chit-chat”. You should follow that advice.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjubah}(h]h ]h"]h$]h&]uh1j.hjhhhhhNubeh}(h]printkah ]h"]printk()ah$]h&]uh1hhjohhhhhKubh)}(hhh](h)}(hc:c:func:`copy_to_user()` / :c:func:`copy_from_user()` / :c:func:`get_user()` / :c:func:`put_user()`h](h)}(h:c:func:`copy_to_user()`h]j)}(hjh]hcopy_to_user()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" copy_to_useruh1hhhhMhjubh / }(hjhhhNhNubh)}(h:c:func:`copy_from_user()`h]j)}(hjh]hcopy_from_user()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"copy_from_useruh1hhhhMhjubh / }hjsbh)}(h:c:func:`get_user()`h]j)}(hjh]h get_user()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"get_useruh1hhhhMhjubh / }hjsbh)}(h:c:func:`put_user()`h]j)}(hj h]h put_user()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"put_useruh1hhhhMhjubeh}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(h:Defined in ``include/linux/uaccess.h`` / ``asm/uaccess.h``h](h Defined in }(hj< hhhNhNubj)}(h``include/linux/uaccess.h``h]hinclude/linux/uaccess.h}(hjD hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj< ubh / }(hj< hhhNhNubj)}(h``asm/uaccess.h``h]h asm/uaccess.h}(hjV hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj< ubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h **[SLEEPS]**h]jZ)}(hjl h]h[SLEEPS]}(hjn hhhNhNubah}(h]h ]h"]h$]h&]uh1jYhjj ubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hX:c:func:`put_user()` and :c:func:`get_user()` are used to get and put single values (such as an int, char, or long) from and to userspace. A pointer into userspace should never be simply dereferenced: data should be copied using these routines. Both return ``-EFAULT`` or 0.h](h)}(h:c:func:`put_user()`h]j)}(hj h]h put_user()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"put_useruh1hhhhMhj ubh and }(hj hhhNhNubh)}(h:c:func:`get_user()`h]j)}(hj h]h get_user()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"get_useruh1hhhhMhj ubh are used to get and put single values (such as an int, char, or long) from and to userspace. A pointer into userspace should never be simply dereferenced: data should be copied using these routines. Both return }(hj hhhNhNubj)}(h ``-EFAULT``h]h-EFAULT}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh or 0.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h:c:func:`copy_to_user()` and :c:func:`copy_from_user()` are more general: they copy an arbitrary amount of data to and from userspace.h](h)}(h:c:func:`copy_to_user()`h]j)}(hj h]hcopy_to_user()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" copy_to_useruh1hhhhMhj ubh and }(hj hhhNhNubh)}(h:c:func:`copy_from_user()`h]j)}(hj h]hcopy_from_user()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"copy_from_useruh1hhhhMhj ubhO are more general: they copy an arbitrary amount of data to and from userspace.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hzUnlike :c:func:`put_user()` and :c:func:`get_user()`, they return the amount of uncopied data (ie. 0 still means success).h]h)}(hzUnlike :c:func:`put_user()` and :c:func:`get_user()`, they return the amount of uncopied data (ie. 0 still means success).h](hUnlike }(hj7 hhhNhNubh)}(h:c:func:`put_user()`h]j)}(hjA h]h put_user()}(hjC hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj? ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"put_useruh1hhhhM!hj7 ubh and }(hj7 hhhNhNubh)}(h:c:func:`get_user()`h]j)}(hjd h]h get_user()}(hjf hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjb ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"get_useruh1hhhhM!hj7 ubhF, they return the amount of uncopied data (ie. 0 still means success).}(hj7 hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM!hj3 ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubh)}(hb[Yes, this objectionable interface makes me cringe. The flamewar comes up every year or so. --RR.]h]hb[Yes, this objectionable interface makes me cringe. The flamewar comes up every year or so. --RR.]}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM$hjhhubh)}(hThe functions may sleep implicitly. This should never be called outside user context (it makes no sense), with interrupts disabled, or a spinlock held.h]hThe functions may sleep implicitly. This should never be called outside user context (it makes no sense), with interrupts disabled, or a spinlock held.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM'hjhhubeh}(h]-copy-to-user-copy-from-user-get-user-put-userah ]h"];copy_to_user() / copy_from_user() / get_user() / put_user()ah$]h&]uh1hhjohhhhhMubh)}(hhh](h)}(h%:c:func:`kmalloc()`/:c:func:`kfree()`h](h)}(h:c:func:`kmalloc()`h]j)}(hj h]h kmalloc()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"kmallocuh1hhhhM+hj ubh/}(hj hhhNhNubh)}(h:c:func:`kfree()`h]j)}(hj h]hkfree()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"kfreeuh1hhhhM+hj ubeh}(h]h ]h"]h$]h&]uh1hhj hhhhhM,ubh)}(h#Defined in ``include/linux/slab.h``h](h Defined in }(hj hhhNhNubj)}(h``include/linux/slab.h``h]hinclude/linux/slab.h}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1hhhhM.hj hhubh)}(h**[MAY SLEEP: SEE BELOW]**h]jZ)}(hj" h]h[MAY SLEEP: SEE BELOW]}(hj$ hhhNhNubah}(h]h ]h"]h$]h&]uh1jYhj ubah}(h]h ]h"]h$]h&]uh1hhhhM0hj hhubh)}(hThese routines are used to dynamically request pointer-aligned chunks of memory, like malloc and free do in userspace, but :c:func:`kmalloc()` takes an extra flag word. Important values:h](h{These routines are used to dynamically request pointer-aligned chunks of memory, like malloc and free do in userspace, but }(hj7 hhhNhNubh)}(h:c:func:`kmalloc()`h]j)}(hjA h]h kmalloc()}(hjC hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj? ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"kmallocuh1hhhhM2hj7 ubh, takes an extra flag word. Important values:}(hj7 hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM2hj hhubjw)}(hhh](j|)}(h``GFP_KERNEL`` May sleep and swap to free memory. Only allowed in user context, but is the most reliable way to allocate memory. h](j)}(h``GFP_KERNEL``h]j)}(hjq h]h GFP_KERNEL}(hjs hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjo ubah}(h]h ]h"]h$]h&]uh1jhhhM8hjk ubj)}(hhh]h)}(hqMay sleep and swap to free memory. Only allowed in user context, but is the most reliable way to allocate memory.h]hqMay sleep and swap to free memory. Only allowed in user context, but is the most reliable way to allocate memory.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM7hj ubah}(h]h ]h"]h$]h&]uh1jhjk ubeh}(h]h ]h"]h$]h&]uh1j{hhhM8hjh ubj|)}(h``GFP_ATOMIC`` Don't sleep. Less reliable than ``GFP_KERNEL``, but may be called from interrupt context. You should **really** have a good out-of-memory error-handling strategy. h](j)}(h``GFP_ATOMIC``h]j)}(hj h]h GFP_ATOMIC}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]uh1jhhhM=hj ubj)}(hhh]h)}(hDon't sleep. Less reliable than ``GFP_KERNEL``, but may be called from interrupt context. You should **really** have a good out-of-memory error-handling strategy.h](h"Don’t sleep. Less reliable than }(hj hhhNhNubj)}(h``GFP_KERNEL``h]h GFP_KERNEL}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh7, but may be called from interrupt context. You should }(hj hhhNhNubjZ)}(h **really**h]hreally}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jYhj ubh3 have a good out-of-memory error-handling strategy.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM;hj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1j{hhhM=hjh hhubj|)}(hq``GFP_DMA`` Allocate ISA DMA lower than 16MB. If you don't know what that is you don't need it. Very unreliable. h](j)}(h ``GFP_DMA``h]j)}(hj h]hGFP_DMA}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]uh1jhhhMAhj ubj)}(hhh]h)}(hdAllocate ISA DMA lower than 16MB. If you don't know what that is you don't need it. Very unreliable.h]hhAllocate ISA DMA lower than 16MB. If you don’t know what that is you don’t need it. Very unreliable.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM@hj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1j{hhhMAhjh hhubeh}(h]h ]h"]h$]h&]uh1jvhj hhhhhNubh)}(hIf you see a sleeping function called from invalid context warning message, then maybe you called a sleeping allocation function from interrupt context without ``GFP_ATOMIC``. You should really fix that. Run, don't walk.h](hIf you see a sleeping function called from invalid context warning message, then maybe you called a sleeping allocation function from interrupt context without }(hj= hhhNhNubj)}(h``GFP_ATOMIC``h]h GFP_ATOMIC}(hjE hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj= ubh0. You should really fix that. Run, don’t walk.}(hj= hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMChj hhubh)}(hX,If you are allocating at least ``PAGE_SIZE`` (``asm/page.h`` or ``asm/page_types.h``) bytes, consider using :c:func:`__get_free_pages()` (``include/linux/gfp.h``). It takes an order argument (0 for page sized, 1 for double page, 2 for four pages etc.) and the same memory priority flag word as above.h](hIf you are allocating at least }(hj] hhhNhNubj)}(h ``PAGE_SIZE``h]h PAGE_SIZE}(hje hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj] ubh (}(hj] hhhNhNubj)}(h``asm/page.h``h]h asm/page.h}(hjw hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj] ubh or }(hj] hhhNhNubj)}(h``asm/page_types.h``h]hasm/page_types.h}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj] ubh) bytes, consider using }(hj] hhhNhNubh)}(h:c:func:`__get_free_pages()`h]j)}(hj h]h__get_free_pages()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"__get_free_pagesuh1hhhhMHhj] ubh (}(hj] hhhNhNubj)}(h``include/linux/gfp.h``h]hinclude/linux/gfp.h}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj] ubh). It takes an order argument (0 for page sized, 1 for double page, 2 for four pages etc.) and the same memory priority flag word as above.}(hj] hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMHhj hhubh)}(hXnIf you are allocating more than a page worth of bytes you can use :c:func:`vmalloc()`. It'll allocate virtual memory in the kernel map. This block is not contiguous in physical memory, but the MMU makes it look like it is for you (so it'll only look contiguous to the CPUs, not to external device drivers). If you really need large physically contiguous memory for some weird device, you have a problem: it is poorly supported in Linux because after some time memory fragmentation in a running kernel makes it hard. The best way is to allocate the block early in the boot process via the :c:func:`alloc_bootmem()` routine.h](hBIf you are allocating more than a page worth of bytes you can use }(hj hhhNhNubh)}(h:c:func:`vmalloc()`h]j)}(hj h]h vmalloc()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"vmallocuh1hhhhMNhj ubhX. It’ll allocate virtual memory in the kernel map. This block is not contiguous in physical memory, but the MMU makes it look like it is for you (so it’ll only look contiguous to the CPUs, not to external device drivers). If you really need large physically contiguous memory for some weird device, you have a problem: it is poorly supported in Linux because after some time memory fragmentation in a running kernel makes it hard. The best way is to allocate the block early in the boot process via the }(hj hhhNhNubh)}(h:c:func:`alloc_bootmem()`h]j)}(hj h]halloc_bootmem()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" alloc_bootmemuh1hhhhMNhj ubh routine.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMNhj hhubh)}(hmBefore inventing your own cache of often-used objects consider using a slab cache in ``include/linux/slab.h``h](hUBefore inventing your own cache of often-used objects consider using a slab cache in }(hj* hhhNhNubj)}(h``include/linux/slab.h``h]hinclude/linux/slab.h}(hj2 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj* ubeh}(h]h ]h"]h$]h&]uh1hhhhMYhj hhubeh}(h] kmalloc-kfreeah ]h"]kmalloc()/kfree()ah$]h&]uh1hhjohhhhhM,ubh)}(hhh](h)}(h:c:macro:`current`h]h)}(hjS h]j)}(hjS h]hcurrent}(hjX hhhNhNubah}(h]h ](jjc-macroeh"]h$]h&]uh1jhjU ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypemacro refexplicitrefwarnj"currentuh1hhhhM\hjQ ubah}(h]h ]h"]h$]h&]uh1hhjN hhhhhM]ubh)}(h$Defined in ``include/asm/current.h``h](h Defined in }(hjy hhhNhNubj)}(h``include/asm/current.h``h]hinclude/asm/current.h}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjy ubeh}(h]h ]h"]h$]h&]uh1hhhhM_hjN hhubh)}(hXThis global variable (really a macro) contains a pointer to the current task structure, so is only valid in user context. For example, when a process makes a system call, this will point to the task structure of the calling process. It is **not NULL** in interrupt context.h](hThis global variable (really a macro) contains a pointer to the current task structure, so is only valid in user context. For example, when a process makes a system call, this will point to the task structure of the calling process. It is }(hj hhhNhNubjZ)}(h **not NULL**h]hnot NULL}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jYhj ubh in interrupt context.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMahjN hhubeh}(h]currentah ]h"]currentah$]h&]uh1hhjohhhhhM]ubh)}(hhh](h)}(h%:c:func:`mdelay()`/:c:func:`udelay()`h](h)}(h:c:func:`mdelay()`h]j)}(hj h]hmdelay()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"mdelayuh1hhhhMfhj ubh/}(hj hhhNhNubh)}(h:c:func:`udelay()`h]j)}(hj h]hudelay()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"udelayuh1hhhhMfhj ubeh}(h]h ]h"]h$]h&]uh1hhj hhhhhMgubh)}(h>Defined in ``include/asm/delay.h`` / ``include/linux/delay.h``h](h Defined in }(hj hhhNhNubj)}(h``include/asm/delay.h``h]hinclude/asm/delay.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh / }(hj hhhNhNubj)}(h``include/linux/delay.h``h]hinclude/linux/delay.h}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1hhhhMihj hhubh)}(hThe :c:func:`udelay()` and :c:func:`ndelay()` functions can be used for small pauses. Do not use large values with them as you risk overflow - the helper function :c:func:`mdelay()` is useful here, or consider :c:func:`msleep()`.h](hThe }(hj:hhhNhNubh)}(h:c:func:`udelay()`h]j)}(hjDh]hudelay()}(hjFhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjBubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"udelayuh1hhhhMkhj:ubh and }(hj:hhhNhNubh)}(h:c:func:`ndelay()`h]j)}(hjgh]hndelay()}(hjihhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjeubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"ndelayuh1hhhhMkhj:ubhv functions can be used for small pauses. Do not use large values with them as you risk overflow - the helper function }(hj:hhhNhNubh)}(h:c:func:`mdelay()`h]j)}(hjh]hmdelay()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"mdelayuh1hhhhMkhj:ubh is useful here, or consider }(hj:hhhNhNubh)}(h:c:func:`msleep()`h]j)}(hjh]hmsleep()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"msleepuh1hhhhMkhj:ubh.}(hj:hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMkhj hhubeh}(h] mdelay-udelayah ]h"]mdelay()/udelay()ah$]h&]uh1hhjohhhhhMgubh)}(hhh](h)}(h_:c:func:`cpu_to_be32()`/:c:func:`be32_to_cpu()`/:c:func:`cpu_to_le32()`/:c:func:`le32_to_cpu()`h](h)}(h:c:func:`cpu_to_be32()`h]j)}(hjh]h cpu_to_be32()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" cpu_to_be32uh1hhhhMphjubh/}(hjhhhNhNubh)}(h:c:func:`be32_to_cpu()`h]j)}(hjh]h be32_to_cpu()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" be32_to_cpuuh1hhhhMphjubh/}hjsbh)}(h:c:func:`cpu_to_le32()`h]j)}(hj+h]h cpu_to_le32()}(hj-hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj)ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" cpu_to_le32uh1hhhhMphjubh/}hjsbh)}(h:c:func:`le32_to_cpu()`h]j)}(hjNh]h le32_to_cpu()}(hjPhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjLubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" le32_to_cpuuh1hhhhMphjubeh}(h]h ]h"]h$]h&]uh1hhjhhhhhMqubh)}(h&Defined in ``include/asm/byteorder.h``h](h Defined in }(hjqhhhNhNubj)}(h``include/asm/byteorder.h``h]hinclude/asm/byteorder.h}(hjyhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjqubeh}(h]h ]h"]h$]h&]uh1hhhhMshjhhubh)}(hXThe :c:func:`cpu_to_be32()` family (where the "32" can be replaced by 64 or 16, and the "be" can be replaced by "le") are the general way to do endian conversions in the kernel: they return the converted value. All variations supply the reverse as well: :c:func:`be32_to_cpu()`, etc.h](hThe }(hjhhhNhNubh)}(h:c:func:`cpu_to_be32()`h]j)}(hjh]h cpu_to_be32()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" cpu_to_be32uh1hhhhMuhjubh family (where the “32” can be replaced by 64 or 16, and the “be” can be replaced by “le”) are the general way to do endian conversions in the kernel: they return the converted value. All variations supply the reverse as well: }(hjhhhNhNubh)}(h:c:func:`be32_to_cpu()`h]j)}(hjh]h be32_to_cpu()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" be32_to_cpuuh1hhhhMuhjubh, etc.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMuhjhhubh)}(hXCThere are two major variations of these functions: the pointer variation, such as :c:func:`cpu_to_be32p()`, which take a pointer to the given type, and return the converted value. The other variation is the "in-situ" family, such as :c:func:`cpu_to_be32s()`, which convert value referred to by the pointer, and return void.h](hRThere are two major variations of these functions: the pointer variation, such as }(hjhhhNhNubh)}(h:c:func:`cpu_to_be32p()`h]j)}(hjh]hcpu_to_be32p()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" cpu_to_be32puh1hhhhM{hjubh, which take a pointer to the given type, and return the converted value. The other variation is the “in-situ” family, such as }(hjhhhNhNubh)}(h:c:func:`cpu_to_be32s()`h]j)}(hjh]hcpu_to_be32s()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" cpu_to_be32suh1hhhhM{hjubhB, which convert value referred to by the pointer, and return void.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM{hjhhubeh}(h]/cpu-to-be32-be32-to-cpu-cpu-to-le32-le32-to-cpuah ]h"]7cpu_to_be32()/be32_to_cpu()/cpu_to_le32()/le32_to_cpu()ah$]h&]uh1hhjohhhhhMqubh)}(hhh](h)}(h8:c:func:`local_irq_save()`/:c:func:`local_irq_restore()`h](h)}(h:c:func:`local_irq_save()`h]j)}(hjFh]hlocal_irq_save()}(hjHhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjDubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"local_irq_saveuh1hhhhMhj@ubh/}(hj@hhhNhNubh)}(h:c:func:`local_irq_restore()`h]j)}(hjih]hlocal_irq_restore()}(hjkhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjgubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"local_irq_restoreuh1hhhhMhj@ubeh}(h]h ]h"]h$]h&]uh1hhj=hhhhhMubh)}(h'Defined in ``include/linux/irqflags.h``h](h Defined in }(hjhhhNhNubj)}(h``include/linux/irqflags.h``h]hinclude/linux/irqflags.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1hhhhMhj=hhubh)}(hX#These routines disable hard interrupts on the local CPU, and restore them. They are reentrant; saving the previous state in their one ``unsigned long flags`` argument. If you know that interrupts are enabled, you can simply use :c:func:`local_irq_disable()` and :c:func:`local_irq_enable()`.h](hThese routines disable hard interrupts on the local CPU, and restore them. They are reentrant; saving the previous state in their one }(hjhhhNhNubj)}(h``unsigned long flags``h]hunsigned long flags}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhG argument. If you know that interrupts are enabled, you can simply use }(hjhhhNhNubh)}(h:c:func:`local_irq_disable()`h]j)}(hjh]hlocal_irq_disable()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"local_irq_disableuh1hhhhMhjubh and }(hjhhhNhNubh)}(h:c:func:`local_irq_enable()`h]j)}(hjh]hlocal_irq_enable()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"local_irq_enableuh1hhhhMhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj=hhubh)}(h.. _local_bh_disable:h]h}(h]h ]h"]h$]h&]hlocal-bh-disableuh1hhMhj=hhhhubeh}(h] local-irq-save-local-irq-restoreah ]h"]$local_irq_save()/local_irq_restore()ah$]h&]uh1hhjohhhhhMubh)}(hhh](h)}(h8:c:func:`local_bh_disable()`/:c:func:`local_bh_enable()`h](h)}(h:c:func:`local_bh_disable()`h]j)}(hj*h]hlocal_bh_disable()}(hj,hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj(ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"local_bh_disableuh1hhhhMhj$ubh/}(hj$hhhNhNubh)}(h:c:func:`local_bh_enable()`h]j)}(hjMh]hlocal_bh_enable()}(hjOhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjKubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"local_bh_enableuh1hhhhMhj$ubeh}(h]h ]h"]h$]h&]uh1hhj!hhhhhMubh)}(h*Defined in ``include/linux/bottom_half.h``h](h Defined in }(hjphhhNhNubj)}(h``include/linux/bottom_half.h``h]hinclude/linux/bottom_half.h}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjpubeh}(h]h ]h"]h$]h&]uh1hhhhMhj!hhubh)}(hXThese routines disable soft interrupts on the local CPU, and restore them. They are reentrant; if soft interrupts were disabled before, they will still be disabled after this pair of functions has been called. They prevent softirqs and tasklets from running on the current CPU.h]hXThese routines disable soft interrupts on the local CPU, and restore them. They are reentrant; if soft interrupts were disabled before, they will still be disabled after this pair of functions has been called. They prevent softirqs and tasklets from running on the current CPU.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj!hhubeh}(h]( local-bh-disable-local-bh-enablejeh ]h"]($local_bh_disable()/local_bh_enable()local_bh_disableeh$]h&]uh1hhjohhhhhMexpect_referenced_by_name}jjsexpect_referenced_by_id}jjsubh)}(hhh](h)}(h:c:func:`smp_processor_id()`h]h)}(hjh]j)}(hjh]hsmp_processor_id()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"smp_processor_iduh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(h"Defined in ``include/linux/smp.h``h](h Defined in }(hjhhhNhNubj)}(h``include/linux/smp.h``h]hinclude/linux/smp.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hX:c:func:`get_cpu()` disables preemption (so you won't suddenly get moved to another CPU) and returns the current processor number, between 0 and ``NR_CPUS``. Note that the CPU numbers are not necessarily continuous. You return it again with :c:func:`put_cpu()` when you are done.h](h)}(h:c:func:`get_cpu()`h]j)}(hjh]h get_cpu()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"get_cpuuh1hhhhMhjubh disables preemption (so you won’t suddenly get moved to another CPU) and returns the current processor number, between 0 and }(hjhhhNhNubj)}(h ``NR_CPUS``h]hNR_CPUS}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhU. Note that the CPU numbers are not necessarily continuous. You return it again with }(hjhhhNhNubh)}(h:c:func:`put_cpu()`h]j)}(hj)h]h put_cpu()}(hj+hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj'ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"put_cpuuh1hhhhMhjubh when you are done.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hIf you know you cannot be preempted by another task (ie. you are in interrupt context, or have preemption disabled) you can use smp_processor_id().h]hIf you know you cannot be preempted by another task (ie. you are in interrupt context, or have preemption disabled) you can use smp_processor_id().}(hjPhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]smp-processor-idah ]h"]smp_processor_id()ah$]h&]uh1hhjohhhhhMubh)}(hhh](h)}(h$``__init``/``__exit``/``__initdata``h](j)}(h ``__init``h]h__init}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjiubh/}(hjihhhNhNubj)}(h ``__exit``h]h__exit}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjiubh/}hjisbj)}(h``__initdata``h]h __initdata}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjiubeh}(h]h ]h"]h$]h&]uh1hhjfhhhhhMubh)}(h$Defined in ``include/linux/init.h``h](h Defined in }(hjhhhNhNubj)}(h``include/linux/init.h``h]hinclude/linux/init.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1hhhhMhjfhhubh)}(hX=After boot, the kernel frees up a special section; functions marked with ``__init`` and data structures marked with ``__initdata`` are dropped after boot is complete: similarly modules discard this memory after initialization. ``__exit`` is used to declare a function which is only required on exit: the function will be dropped if this file is not compiled as a module. See the header file for use. Note that it makes no sense for a function marked with ``__init`` to be exported to modules with :c:func:`EXPORT_SYMBOL()` or :c:func:`EXPORT_SYMBOL_GPL()`- this will break.h](hIAfter boot, the kernel frees up a special section; functions marked with }(hjhhhNhNubj)}(h ``__init``h]h__init}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh! and data structures marked with }(hjhhhNhNubj)}(h``__initdata``h]h __initdata}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubha are dropped after boot is complete: similarly modules discard this memory after initialization. }(hjhhhNhNubj)}(h ``__exit``h]h__exit}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh is used to declare a function which is only required on exit: the function will be dropped if this file is not compiled as a module. See the header file for use. Note that it makes no sense for a function marked with }(hjhhhNhNubj)}(h ``__init``h]h__init}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh to be exported to modules with }(hjhhhNhNubh)}(h:c:func:`EXPORT_SYMBOL()`h]j)}(hjh]hEXPORT_SYMBOL()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" EXPORT_SYMBOLuh1hhhhMhjubh or }(hjhhhNhNubh)}(h:c:func:`EXPORT_SYMBOL_GPL()`h]j)}(hj6h]hEXPORT_SYMBOL_GPL()}(hj8hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj4ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"EXPORT_SYMBOL_GPLuh1hhhhMhjubh- this will break.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjfhhubeh}(h]init-exit-initdataah ]h"]__init/__exit/__initdataah$]h&]uh1hhjohhhhhMubh)}(hhh](h)}(h.:c:func:`__initcall()`/:c:func:`module_init()`h](h)}(h:c:func:`__initcall()`h]j)}(hjnh]h __initcall()}(hjphhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjlubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" __initcalluh1hhhhMhjhubh/}(hjhhhhNhNubh)}(h:c:func:`module_init()`h]j)}(hjh]h module_init()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" module_inituh1hhhhMhjhubeh}(h]h ]h"]h$]h&]uh1hhjehhhhhMubh)}(hADefined in ``include/linux/init.h`` / ``include/linux/module.h``h](h Defined in }(hjhhhNhNubj)}(h``include/linux/init.h``h]hinclude/linux/init.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh / }(hjhhhNhNubj)}(h``include/linux/module.h``h]hinclude/linux/module.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1hhhhMhjehhubh)}(hX Many parts of the kernel are well served as a module (dynamically-loadable parts of the kernel). Using the :c:func:`module_init()` and :c:func:`module_exit()` macros it is easy to write code without #ifdefs which can operate both as a module or built into the kernel.h](hkMany parts of the kernel are well served as a module (dynamically-loadable parts of the kernel). Using the }(hjhhhNhNubh)}(h:c:func:`module_init()`h]j)}(hjh]h module_init()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" module_inituh1hhhhMhjubh and }(hjhhhNhNubh)}(h:c:func:`module_exit()`h]j)}(hjh]h module_exit()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" module_exituh1hhhhMhjubhm macros it is easy to write code without #ifdefs which can operate both as a module or built into the kernel.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjehhubh)}(hXYThe :c:func:`module_init()` macro defines which function is to be called at module insertion time (if the file is compiled as a module), or at boot time: if the file is not compiled as a module the :c:func:`module_init()` macro becomes equivalent to :c:func:`__initcall()`, which through linker magic ensures that the function is called on boot.h](hThe }(hj6hhhNhNubh)}(h:c:func:`module_init()`h]j)}(hj@h]h module_init()}(hjBhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj>ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" module_inituh1hhhhMhj6ubh macro defines which function is to be called at module insertion time (if the file is compiled as a module), or at boot time: if the file is not compiled as a module the }(hj6hhhNhNubh)}(h:c:func:`module_init()`h]j)}(hjch]h module_init()}(hjehhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjaubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" module_inituh1hhhhMhj6ubh macro becomes equivalent to }(hj6hhhNhNubh)}(h:c:func:`__initcall()`h]j)}(hjh]h __initcall()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" __initcalluh1hhhhMhj6ubhI, which through linker magic ensures that the function is called on boot.}(hj6hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjehhubh)}(hThe function can return a negative error number to cause module loading to fail (unfortunately, this has no effect if the module is compiled into the kernel). This function is called in user context with interrupts enabled, so it can sleep.h]hThe function can return a negative error number to cause module loading to fail (unfortunately, this has no effect if the module is compiled into the kernel). This function is called in user context with interrupts enabled, so it can sleep.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjehhubeh}(h]initcall-module-initah ]h"]__initcall()/module_init()ah$]h&]uh1hhjohhhhhMubh)}(hhh](h)}(h:c:func:`module_exit()`h]h)}(hjh]j)}(hjh]h module_exit()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" module_exituh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(h&Defined in ``include/linux/module.h``h](h Defined in }(hjhhhNhNubj)}(h``include/linux/module.h``h]hinclude/linux/module.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hX)This macro defines the function to be called at module removal time (or never, in the case of the file compiled into the kernel). It will only be called if the module usage count has reached zero. This function can also sleep, but cannot fail: everything must be cleaned up by the time it returns.h]hX)This macro defines the function to be called at module removal time (or never, in the case of the file compiled into the kernel). It will only be called if the module usage count has reached zero. This function can also sleep, but cannot fail: everything must be cleaned up by the time it returns.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hrNote that this macro is optional: if it is not present, your module will not be removable (except for 'rmmod -f').h]hvNote that this macro is optional: if it is not present, your module will not be removable (except for ‘rmmod -f’).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h] module-exitah ]h"] module_exit()ah$]h&]uh1hhjohhhhhMubh)}(hhh](h)}(h1:c:func:`try_module_get()`/:c:func:`module_put()`h](h)}(h:c:func:`try_module_get()`h]j)}(hj7h]htry_module_get()}(hj9hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj5ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"try_module_getuh1hhhhMhj1ubh/}(hj1hhhNhNubh)}(h:c:func:`module_put()`h]j)}(hjZh]h module_put()}(hj\hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjXubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" module_putuh1hhhhMhj1ubeh}(h]h ]h"]h$]h&]uh1hhj.hhhhhMubh)}(h%Defined in ``include/linux/module.h``h](h Defined in }(hj}hhhNhNubj)}(h``include/linux/module.h``h]hinclude/linux/module.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj}ubeh}(h]h ]h"]h$]h&]uh1hhhhMhj.hhubh)}(hXThese manipulate the module usage count, to protect against removal (a module also can't be removed if another module uses one of its exported symbols: see below). Before calling into module code, you should call :c:func:`try_module_get()` on that module: if it fails, then the module is being removed and you should act as if it wasn't there. Otherwise, you can safely enter the module, and call :c:func:`module_put()` when you're finished.h](hThese manipulate the module usage count, to protect against removal (a module also can’t be removed if another module uses one of its exported symbols: see below). Before calling into module code, you should call }(hjhhhNhNubh)}(h:c:func:`try_module_get()`h]j)}(hjh]htry_module_get()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"try_module_getuh1hhhhMhjubh on that module: if it fails, then the module is being removed and you should act as if it wasn’t there. Otherwise, you can safely enter the module, and call }(hjhhhNhNubh)}(h:c:func:`module_put()`h]j)}(hjh]h module_put()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" module_putuh1hhhhMhjubh when you’re finished.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj.hhubh)}(hMost registerable structures have an owner field, such as in the :c:type:`struct file_operations ` structure. Set this field to the macro ``THIS_MODULE``.h](hAMost registerable structures have an owner field, such as in the }(hjhhhNhNubh)}(h2:c:type:`struct file_operations `h]j)}(hjh]hstruct file_operations}(hjhhhNhNubah}(h]h ](jjc-typeeh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypetype refexplicitrefwarnj"file_operationsuh1hhhhMhjubh( structure. Set this field to the macro }(hjhhhNhNubj)}(h``THIS_MODULE``h]h THIS_MODULE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj.hhubeh}(h]try-module-get-module-putah ]h"]try_module_get()/module_put()ah$]h&]uh1hhjohhhhhMubeh}(h]common-routinesah ]h"]common routinesah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h$Wait Queues ``include/linux/wait.h``h](h Wait Queues }(hjChhhNhNubj)}(h``include/linux/wait.h``h]hinclude/linux/wait.h}(hjKhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjCubeh}(h]h ]h"]h$]h&]uh1hhj@hhhhhMubh)}(h **[SLEEPS]**h]jZ)}(hjah]h[SLEEPS]}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1jYhj_ubah}(h]h ]h"]h$]h&]uh1hhhhMhj@hhubh)}(hX\A wait queue is used to wait for someone to wake you up when a certain condition is true. They must be used carefully to ensure there is no race condition. You declare a :c:type:`wait_queue_head_t`, and then processes which want to wait for that condition declare a :c:type:`wait_queue_entry_t` referring to themselves, and place that in the queue.h](hA wait queue is used to wait for someone to wake you up when a certain condition is true. They must be used carefully to ensure there is no race condition. You declare a }(hjvhhhNhNubh)}(h:c:type:`wait_queue_head_t`h]j)}(hjh]hwait_queue_head_t}(hjhhhNhNubah}(h]h ](jjc-typeeh"]h$]h&]uh1jhj~ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypetype refexplicitrefwarnj"wait_queue_head_tuh1hhhhMhjvubhE, and then processes which want to wait for that condition declare a }(hjvhhhNhNubh)}(h:c:type:`wait_queue_entry_t`h]j)}(hjh]hwait_queue_entry_t}(hjhhhNhNubah}(h]h ](jjc-typeeh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypetype refexplicitrefwarnj"wait_queue_entry_tuh1hhhhMhjvubh6 referring to themselves, and place that in the queue.}(hjvhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj@hhubh)}(hhh](h)}(h Declaringh]h Declaring}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hYou declare a ``wait_queue_head_t`` using the :c:func:`DECLARE_WAIT_QUEUE_HEAD()` macro, or using the :c:func:`init_waitqueue_head()` routine in your initialization code.h](hYou declare a }(hjhhhNhNubj)}(h``wait_queue_head_t``h]hwait_queue_head_t}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh using the }(hjhhhNhNubh)}(h#:c:func:`DECLARE_WAIT_QUEUE_HEAD()`h]j)}(hjh]hDECLARE_WAIT_QUEUE_HEAD()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"DECLARE_WAIT_QUEUE_HEADuh1hhhhMhjubh macro, or using the }(hjhhhNhNubh)}(h:c:func:`init_waitqueue_head()`h]j)}(hjh]hinit_waitqueue_head()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"init_waitqueue_headuh1hhhhMhjubh% routine in your initialization code.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h] declaringah ]h"] declaringah$]h&]uh1hhj@hhhhhMubh)}(hhh](h)}(hQueuingh]hQueuing}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjIhhhhhMubh)}(hXPlacing yourself in the waitqueue is fairly complex, because you must put yourself in the queue before checking the condition. There is a macro to do this: :c:func:`wait_event_interruptible()` (``include/linux/wait.h``) The first argument is the wait queue head, and the second is an expression which is evaluated; the macro returns 0 when this expression is true, or ``-ERESTARTSYS`` if a signal is received. The :c:func:`wait_event()` version ignores signals.h](hPlacing yourself in the waitqueue is fairly complex, because you must put yourself in the queue before checking the condition. There is a macro to do this: }(hjZhhhNhNubh)}(h$:c:func:`wait_event_interruptible()`h]j)}(hjdh]hwait_event_interruptible()}(hjfhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjbubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"wait_event_interruptibleuh1hhhhMhjZubh (}(hjZhhhNhNubj)}(h``include/linux/wait.h``h]hinclude/linux/wait.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubh) The first argument is the wait queue head, and the second is an expression which is evaluated; the macro returns 0 when this expression is true, or }(hjZhhhNhNubj)}(h``-ERESTARTSYS``h]h -ERESTARTSYS}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubh if a signal is received. The }(hjZhhhNhNubh)}(h:c:func:`wait_event()`h]j)}(hjh]h wait_event()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" wait_eventuh1hhhhMhjZubh version ignores signals.}(hjZhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjIhhubeh}(h]queuingah ]h"]queuingah$]h&]uh1hhj@hhhhhMubh)}(hhh](h)}(hWaking Up Queued Tasksh]hWaking Up Queued Tasks}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhM ubh)}(hX!Call :c:func:`wake_up()` (``include/linux/wait.h``), which will wake up every process in the queue. The exception is if one has ``TASK_EXCLUSIVE`` set, in which case the remainder of the queue will not be woken. There are other variants of this basic function available in the same header.h](hCall }(hjhhhNhNubh)}(h:c:func:`wake_up()`h]j)}(hjh]h wake_up()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"wake_upuh1hhhhMhjubh (}(hjhhhNhNubj)}(h``include/linux/wait.h``h]hinclude/linux/wait.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhN), which will wake up every process in the queue. The exception is if one has }(hjhhhNhNubj)}(h``TASK_EXCLUSIVE``h]hTASK_EXCLUSIVE}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh set, in which case the remainder of the queue will not be woken. There are other variants of this basic function available in the same header.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]waking-up-queued-tasksah ]h"]waking up queued tasksah$]h&]uh1hhj@hhhhhM ubeh}(h] wait-queues-include-linux-wait-hah ]h"] wait queues include/linux/wait.hah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hAtomic Operationsh]hAtomic Operations}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1hhjPhhhhhMubh)}(hXCertain operations are guaranteed atomic on all platforms. The first class of operations work on :c:type:`atomic_t` (``include/asm/atomic.h``); this contains a signed integer (at least 32 bits long), and you must use these functions to manipulate or read :c:type:`atomic_t` variables. :c:func:`atomic_read()` and :c:func:`atomic_set()` get and set the counter, :c:func:`atomic_add()`, :c:func:`atomic_sub()`, :c:func:`atomic_inc()`, :c:func:`atomic_dec()`, and :c:func:`atomic_dec_and_test()` (returns true if it was decremented to zero).h](haCertain operations are guaranteed atomic on all platforms. The first class of operations work on }(hjahhhNhNubh)}(h:c:type:`atomic_t`h]j)}(hjkh]hatomic_t}(hjmhhhNhNubah}(h]h ](jjc-typeeh"]h$]h&]uh1jhjiubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypetype refexplicitrefwarnj"atomic_tuh1hhhhMhjaubh (}(hjahhhNhNubj)}(h``include/asm/atomic.h``h]hinclude/asm/atomic.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjaubhr); this contains a signed integer (at least 32 bits long), and you must use these functions to manipulate or read }(hjahhhNhNubh)}(h:c:type:`atomic_t`h]j)}(hjh]hatomic_t}(hjhhhNhNubah}(h]h ](jjc-typeeh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypetype refexplicitrefwarnj"atomic_tuh1hhhhMhjaubh variables. }(hjahhhNhNubh)}(h:c:func:`atomic_read()`h]j)}(hjh]h atomic_read()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" atomic_readuh1hhhhMhjaubh and }(hjahhhNhNubh)}(h:c:func:`atomic_set()`h]j)}(hjh]h atomic_set()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" atomic_setuh1hhhhMhjaubh get and set the counter, }(hjahhhNhNubh)}(h:c:func:`atomic_add()`h]j)}(hj h]h atomic_add()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" atomic_adduh1hhhhMhjaubh, }(hjahhhNhNubh)}(h:c:func:`atomic_sub()`h]j)}(hj,h]h atomic_sub()}(hj.hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj*ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" atomic_subuh1hhhhMhjaubh, }(hjahhhNhNubh)}(h:c:func:`atomic_inc()`h]j)}(hjOh]h atomic_inc()}(hjQhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjMubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" atomic_incuh1hhhhMhjaubh, }hjasbh)}(h:c:func:`atomic_dec()`h]j)}(hjrh]h atomic_dec()}(hjthhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjpubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" atomic_decuh1hhhhMhjaubh, and }(hjahhhNhNubh)}(h:c:func:`atomic_dec_and_test()`h]j)}(hjh]hatomic_dec_and_test()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"atomic_dec_and_testuh1hhhhMhjaubh. (returns true if it was decremented to zero).}(hjahhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjPhhubh)}(h@Yes. It returns true (i.e. != 0) if the atomic variable is zero.h]h@Yes. It returns true (i.e. != 0) if the atomic variable is zero.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM"hjPhhubh)}(heNote that these functions are slower than normal arithmetic, and so should not be used unnecessarily.h]heNote that these functions are slower than normal arithmetic, and so should not be used unnecessarily.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM$hjPhhubh)}(hX:The second class of atomic operations is atomic bit operations on an ``unsigned long``, defined in ``include/linux/bitops.h``. These operations generally take a pointer to the bit pattern, and a bit number: 0 is the least significant bit. :c:func:`set_bit()`, :c:func:`clear_bit()` and :c:func:`change_bit()` set, clear, and flip the given bit. :c:func:`test_and_set_bit()`, :c:func:`test_and_clear_bit()` and :c:func:`test_and_change_bit()` do the same thing, except return true if the bit was previously set; these are particularly useful for atomically setting flags.h](hEThe second class of atomic operations is atomic bit operations on an }(hjhhhNhNubj)}(h``unsigned long``h]h unsigned long}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh , defined in }(hjhhhNhNubj)}(h``include/linux/bitops.h``h]hinclude/linux/bitops.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhr. These operations generally take a pointer to the bit pattern, and a bit number: 0 is the least significant bit. }(hjhhhNhNubh)}(h:c:func:`set_bit()`h]j)}(hjh]h set_bit()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"set_bituh1hhhhM'hjubh, }(hjhhhNhNubh)}(h:c:func:`clear_bit()`h]j)}(hj)h]h clear_bit()}(hj+hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj'ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" clear_bituh1hhhhM'hjubh and }(hjhhhNhNubh)}(h:c:func:`change_bit()`h]j)}(hjLh]h change_bit()}(hjNhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjJubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" change_bituh1hhhhM'hjubh% set, clear, and flip the given bit. }(hjhhhNhNubh)}(h:c:func:`test_and_set_bit()`h]j)}(hjoh]htest_and_set_bit()}(hjqhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjmubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"test_and_set_bituh1hhhhM'hjubh, }hjsbh)}(h:c:func:`test_and_clear_bit()`h]j)}(hjh]htest_and_clear_bit()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"test_and_clear_bituh1hhhhM'hjubh and }(hjhhhNhNubh)}(h:c:func:`test_and_change_bit()`h]j)}(hjh]htest_and_change_bit()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"test_and_change_bituh1hhhhM'hjubh do the same thing, except return true if the bit was previously set; these are particularly useful for atomically setting flags.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM'hjPhhubh)}(hIt is possible to call these operations with bit indices greater than ``BITS_PER_LONG``. The resulting behavior is strange on big-endian platforms though so it is a good idea not to do this.h](hFIt is possible to call these operations with bit indices greater than }(hjhhhNhNubj)}(h``BITS_PER_LONG``h]h BITS_PER_LONG}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhg. The resulting behavior is strange on big-endian platforms though so it is a good idea not to do this.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM2hjPhhubeh}(h]atomic-operationsah ]h"]atomic operationsah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hSymbolsh]hSymbols}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhM7ubh)}(hXBWithin the kernel proper, the normal linking rules apply (ie. unless a symbol is declared to be file scope with the ``static`` keyword, it can be used anywhere in the kernel). However, for modules, a special exported symbol table is kept which limits the entry points to the kernel proper. Modules can also export symbols.h](htWithin the kernel proper, the normal linking rules apply (ie. unless a symbol is declared to be file scope with the }(hjhhhNhNubj)}(h ``static``h]hstatic}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh keyword, it can be used anywhere in the kernel). However, for modules, a special exported symbol table is kept which limits the entry points to the kernel proper. Modules can also export symbols.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM9hjhhubh)}(hhh](h)}(h:c:func:`EXPORT_SYMBOL()`h]h)}(hj:h]j)}(hj:h]hEXPORT_SYMBOL()}(hj?hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj<ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" EXPORT_SYMBOLuh1hhhhM?hj8ubah}(h]h ]h"]h$]h&]uh1hhj5hhhhhM@ubh)}(h%Defined in ``include/linux/export.h``h](h Defined in }(hj`hhhNhNubj)}(h``include/linux/export.h``h]hinclude/linux/export.h}(hjhhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj`ubeh}(h]h ]h"]h$]h&]uh1hhhhMBhj5hhubh)}(hvThis is the classic method of exporting a symbol: dynamically loaded modules will be able to use the symbol as normal.h]hvThis is the classic method of exporting a symbol: dynamically loaded modules will be able to use the symbol as normal.}(hj|hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMDhj5hhubeh}(h] export-symbolah ]h"]export_symbol()ah$]h&]uh1hhjhhhhhM@ubh)}(hhh](h)}(h:c:func:`EXPORT_SYMBOL_GPL()`h]h)}(hjh]j)}(hjh]hEXPORT_SYMBOL_GPL()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"EXPORT_SYMBOL_GPLuh1hhhhMGhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMHubh)}(h%Defined in ``include/linux/export.h``h](h Defined in }(hjhhhNhNubj)}(h``include/linux/export.h``h]hinclude/linux/export.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1hhhhMJhjhhubh)}(hXSimilar to :c:func:`EXPORT_SYMBOL()` except that the symbols exported by :c:func:`EXPORT_SYMBOL_GPL()` can only be seen by modules with a :c:func:`MODULE_LICENSE()` that specifies a GPLv2 compatible license. It implies that the function is considered an internal implementation issue, and not really an interface. Some maintainers and developers may however require EXPORT_SYMBOL_GPL() when adding any new APIs or functionality.h](h Similar to }(hjhhhNhNubh)}(h:c:func:`EXPORT_SYMBOL()`h]j)}(hjh]hEXPORT_SYMBOL()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" EXPORT_SYMBOLuh1hhhhMLhjubh% except that the symbols exported by }(hjhhhNhNubh)}(h:c:func:`EXPORT_SYMBOL_GPL()`h]j)}(hjh]hEXPORT_SYMBOL_GPL()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"EXPORT_SYMBOL_GPLuh1hhhhMLhjubh$ can only be seen by modules with a }(hjhhhNhNubh)}(h:c:func:`MODULE_LICENSE()`h]j)}(hj)h]hMODULE_LICENSE()}(hj+hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj'ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"MODULE_LICENSEuh1hhhhMLhjubhX that specifies a GPLv2 compatible license. It implies that the function is considered an internal implementation issue, and not really an interface. Some maintainers and developers may however require EXPORT_SYMBOL_GPL() when adding any new APIs or functionality.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMLhjhhubeh}(h]export-symbol-gplah ]h"]export_symbol_gpl()ah$]h&]uh1hhjhhhhhMHubh)}(hhh](h)}(h:c:func:`EXPORT_SYMBOL_NS()`h]h)}(hj]h]j)}(hj]h]hEXPORT_SYMBOL_NS()}(hjbhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj_ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"EXPORT_SYMBOL_NSuh1hhhhMThj[ubah}(h]h ]h"]h$]h&]uh1hhjXhhhhhMUubh)}(h%Defined in ``include/linux/export.h``h](h Defined in }(hjhhhNhNubj)}(h``include/linux/export.h``h]hinclude/linux/export.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1hhhhMWhjXhhubh)}(hThis is the variant of `EXPORT_SYMBOL()` that allows specifying a symbol namespace. Symbol Namespaces are documented in Documentation/core-api/symbol-namespaces.rsth](hThis is the variant of }(hjhhhNhNubhtitle_reference)}(h`EXPORT_SYMBOL()`h]hEXPORT_SYMBOL()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh| that allows specifying a symbol namespace. Symbol Namespaces are documented in Documentation/core-api/symbol-namespaces.rst}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMYhjXhhubeh}(h]export-symbol-nsah ]h"]export_symbol_ns()ah$]h&]uh1hhjhhhhhMUubh)}(hhh](h)}(h :c:func:`EXPORT_SYMBOL_NS_GPL()`h]h)}(hjh]j)}(hjh]hEXPORT_SYMBOL_NS_GPL()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"EXPORT_SYMBOL_NS_GPLuh1hhhhM]hjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhM^ubh)}(h%Defined in ``include/linux/export.h``h](h Defined in }(hjhhhNhNubj)}(h``include/linux/export.h``h]hinclude/linux/export.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1hhhhM`hjhhubh)}(hThis is the variant of `EXPORT_SYMBOL_GPL()` that allows specifying a symbol namespace. Symbol Namespaces are documented in Documentation/core-api/symbol-namespaces.rsth](hThis is the variant of }(hjhhhNhNubj)}(h`EXPORT_SYMBOL_GPL()`h]hEXPORT_SYMBOL_GPL()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh| that allows specifying a symbol namespace. Symbol Namespaces are documented in Documentation/core-api/symbol-namespaces.rst}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMbhjhhubeh}(h]export-symbol-ns-gplah ]h"]export_symbol_ns_gpl()ah$]h&]uh1hhjhhhhhM^ubeh}(h]symbolsah ]h"]symbolsah$]h&]uh1hhhhhhhhM7ubh)}(hhh](h)}(hRoutines and Conventionsh]hRoutines and Conventions}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@hhhhhMgubh)}(hhh](h)}(h,Double-linked lists ``include/linux/list.h``h](hDouble-linked lists }(hjThhhNhNubj)}(h``include/linux/list.h``h]hinclude/linux/list.h}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubeh}(h]h ]h"]h$]h&]uh1hhjQhhhhhMjubh)}(hThere used to be three sets of linked-list routines in the kernel headers, but this one is the winner. If you don't have some particular pressing need for a single list, it's a good choice.h]hThere used to be three sets of linked-list routines in the kernel headers, but this one is the winner. If you don’t have some particular pressing need for a single list, it’s a good choice.}(hjphhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMlhjQhhubh)}(h9In particular, :c:func:`list_for_each_entry()` is useful.h](hIn particular, }(hj~hhhNhNubh)}(h:c:func:`list_for_each_entry()`h]j)}(hjh]hlist_for_each_entry()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"list_for_each_entryuh1hhhhMphj~ubh is useful.}(hj~hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMphjQhhubeh}(h](double-linked-lists-include-linux-list-hah ]h"](double-linked lists include/linux/list.hah$]h&]uh1hhj@hhhhhMjubh)}(hhh](h)}(hReturn Conventionsh]hReturn Conventions}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMsubh)}(hFor code called in user context, it's very common to defy C convention, and return 0 for success, and a negative error number (eg. ``-EFAULT``) for failure. This can be unintuitive at first, but it's fairly widespread in the kernel.h](hFor code called in user context, it’s very common to defy C convention, and return 0 for success, and a negative error number (eg. }(hjhhhNhNubj)}(h ``-EFAULT``h]h-EFAULT}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh\) for failure. This can be unintuitive at first, but it’s fairly widespread in the kernel.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMuhjhhubh)}(hXUsing :c:func:`ERR_PTR()` (``include/linux/err.h``) to encode a negative error number into a pointer, and :c:func:`IS_ERR()` and :c:func:`PTR_ERR()` to get it back out again: avoids a separate pointer parameter for the error number. Icky, but in a good way.h](hUsing }(hjhhhNhNubh)}(h:c:func:`ERR_PTR()`h]j)}(hjh]h ERR_PTR()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"ERR_PTRuh1hhhhMzhjubh (}(hjhhhNhNubj)}(h``include/linux/err.h``h]hinclude/linux/err.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh8) to encode a negative error number into a pointer, and }(hjhhhNhNubh)}(h:c:func:`IS_ERR()`h]j)}(hj'h]hIS_ERR()}(hj)hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj%ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"IS_ERRuh1hhhhMzhjubh and }(hjhhhNhNubh)}(h:c:func:`PTR_ERR()`h]j)}(hjJh]h PTR_ERR()}(hjLhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjHubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj"PTR_ERRuh1hhhhMzhjubhm to get it back out again: avoids a separate pointer parameter for the error number. Icky, but in a good way.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMzhjhhubeh}(h]return-conventionsah ]h"]return conventionsah$]h&]uh1hhj@hhhhhMsubh)}(hhh](h)}(hBreaking Compilationh]hBreaking Compilation}(hj|hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjyhhhhhMubh)}(hXLinus and the other developers sometimes change function or structure names in development kernels; this is not done just to keep everyone on their toes: it reflects a fundamental change (eg. can no longer be called with interrupts on, or does extra checks, or doesn't do checks which were caught before). Usually this is accompanied by a fairly complete note to the appropriate kernel development mailing list; search the archives. Simply doing a global replace on the file usually makes things **worse**.h](hXLinus and the other developers sometimes change function or structure names in development kernels; this is not done just to keep everyone on their toes: it reflects a fundamental change (eg. can no longer be called with interrupts on, or does extra checks, or doesn’t do checks which were caught before). Usually this is accompanied by a fairly complete note to the appropriate kernel development mailing list; search the archives. Simply doing a global replace on the file usually makes things }(hjhhhNhNubjZ)}(h **worse**h]hworse}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jYhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjyhhubeh}(h]breaking-compilationah ]h"]breaking compilationah$]h&]uh1hhj@hhhhhMubh)}(hhh](h)}(hInitializing structure membersh]hInitializing structure members}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hnThe preferred method of initializing structures is to use designated initialisers, as defined by ISO C99, eg::h]hmThe preferred method of initializing structures is to use designated initialisers, as defined by ISO C99, eg:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubjk)}(hstatic struct block_device_operations opt_fops = { .open = opt_open, .release = opt_release, .ioctl = opt_ioctl, .check_media_change = opt_media_change, };h]hstatic struct block_device_operations opt_fops = { .open = opt_open, .release = opt_release, .ioctl = opt_ioctl, .check_media_change = opt_media_change, };}hjsbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhMhjhhubh)}(h|This makes it easy to grep for, and makes it clear which structure fields are set. You should do this because it looks cool.h]h|This makes it easy to grep for, and makes it clear which structure fields are set. You should do this because it looks cool.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]initializing-structure-membersah ]h"]initializing structure membersah$]h&]uh1hhj@hhhhhMubh)}(hhh](h)}(hGNU Extensionsh]hGNU Extensions}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hX]GNU Extensions are explicitly allowed in the Linux kernel. Note that some of the more complex ones are not very well supported, due to lack of general use, but the following are considered standard (see the GCC info page section "C Extensions" for more details - Yes, really the info page, the man page is only a short summary of the stuff in info).h]hXaGNU Extensions are explicitly allowed in the Linux kernel. Note that some of the more complex ones are not very well supported, due to lack of general use, but the following are considered standard (see the GCC info page section “C Extensions” for more details - Yes, really the info page, the man page is only a short summary of the stuff in info).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj\)}(hhh](ja)}(hInline functions h]h)}(hInline functionsh]hInline functions}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(h6Statement expressions (ie. the ({ and }) constructs). h]h)}(h5Statement expressions (ie. the ({ and }) constructs).h]h5Statement expressions (ie. the ({ and }) constructs).}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj/ubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(hEDeclaring attributes of a function / variable / type (__attribute__) h]h)}(hDDeclaring attributes of a function / variable / type (__attribute__)h]hDDeclaring attributes of a function / variable / type (__attribute__)}(hjKhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjGubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(htypeof h]h)}(htypeofh]htypeof}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj_ubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(hZero length arrays h]h)}(hZero length arraysh]hZero length arrays}(hj{hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjwubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(hMacro varargs h]h)}(h Macro varargsh]h Macro varargs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(hArithmetic on void pointers h]h)}(hArithmetic on void pointersh]hArithmetic on void pointers}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(hNon-Constant initializers h]h)}(hNon-Constant initializersh]hNon-Constant initializers}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j`hjhhhhhNubja)}(h hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj; hhhhhMubh)}(hX Using C++ in the kernel is usually a bad idea, because the kernel does not provide the necessary runtime environment and the include files are not tested for it. It is still possible, but not recommended. If you really want to do this, forget about exceptions at least.h]hX Using C++ in the kernel is usually a bad idea, because the kernel does not provide the necessary runtime environment and the include files are not tested for it. It is still possible, but not recommended. If you really want to do this, forget about exceptions at least.}(hjL hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj; hhubeh}(h]cah ]h"]c++ah$]h&]uh1hhj@hhhhhMubh)}(hhh](h)}(h#ifh]h#if}(hje hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjb hhhhhMubh)}(hIt is generally considered cleaner to use macros in header files (or at the top of .c files) to abstract away functions rather than using \`#if' pre-processor statements throughout the source code.h]hIt is generally considered cleaner to use macros in header files (or at the top of .c files) to abstract away functions rather than using `#if’ pre-processor statements throughout the source code.}(hjs hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjb hhubeh}(h]ifah ]h"]#ifah$]h&]uh1hhj@hhhhhMubeh}(h]routines-and-conventionsah ]h"]routines and conventionsah$]h&]uh1hhhhhhhhMgubh)}(hhh](h)}(h Putting Your Stuff in the Kernelh]h Putting Your Stuff in the Kernel}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hIn order to get your stuff into shape for official inclusion, or even to make a neat patch, there's administrative work to be done:h]hIn order to get your stuff into shape for official inclusion, or even to make a neat patch, there’s administrative work to be done:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj\)}(hhh](ja)}(hXFigure out who are the owners of the code you've been modifying. Look at the top of the source files, inside the ``MAINTAINERS`` file, and last of all in the ``CREDITS`` file. You should coordinate with these people to make sure you're not duplicating effort, or trying something that's already been rejected. Make sure you put your name and email address at the top of any files you create or modify significantly. This is the first place people will look when they find a bug, or when **they** want to make a change. h](h)}(hX5Figure out who are the owners of the code you've been modifying. Look at the top of the source files, inside the ``MAINTAINERS`` file, and last of all in the ``CREDITS`` file. You should coordinate with these people to make sure you're not duplicating effort, or trying something that's already been rejected.h](hsFigure out who are the owners of the code you’ve been modifying. Look at the top of the source files, inside the }(hj hhhNhNubj)}(h``MAINTAINERS``h]h MAINTAINERS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh file, and last of all in the }(hj hhhNhNubj)}(h ``CREDITS``h]hCREDITS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh file. You should coordinate with these people to make sure you’re not duplicating effort, or trying something that’s already been rejected.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj ubh)}(hMake sure you put your name and email address at the top of any files you create or modify significantly. This is the first place people will look when they find a bug, or when **they** want to make a change.h](hMake sure you put your name and email address at the top of any files you create or modify significantly. This is the first place people will look when they find a bug, or when }(hj hhhNhNubjZ)}(h**they**h]hthey}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jYhj ubh want to make a change.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj ubeh}(h]h ]h"]h$]h&]uh1j`hj hhhhhNubja)}(hXBUsually you want a configuration option for your kernel hack. Edit ``Kconfig`` in the appropriate directory. The Config language is simple to use by cut and paste, and there's complete documentation in ``Documentation/kbuild/kconfig-language.rst``. In your description of the option, make sure you address both the expert user and the user who knows nothing about your feature. Mention incompatibilities and issues here. **Definitely** end your description with “if in doubt, say N” (or, occasionally, \`Y'); this is for people who have no idea what you are talking about. h](h)}(hUsually you want a configuration option for your kernel hack. Edit ``Kconfig`` in the appropriate directory. The Config language is simple to use by cut and paste, and there's complete documentation in ``Documentation/kbuild/kconfig-language.rst``.h](hCUsually you want a configuration option for your kernel hack. Edit }(hj!hhhNhNubj)}(h ``Kconfig``h]hKconfig}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj!ubh~ in the appropriate directory. The Config language is simple to use by cut and paste, and there’s complete documentation in }(hj!hhhNhNubj)}(h-``Documentation/kbuild/kconfig-language.rst``h]h)Documentation/kbuild/kconfig-language.rst}(hj-!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj!ubh.}(hj!hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj!ubh)}(hXGIn your description of the option, make sure you address both the expert user and the user who knows nothing about your feature. Mention incompatibilities and issues here. **Definitely** end your description with “if in doubt, say N” (or, occasionally, \`Y'); this is for people who have no idea what you are talking about.h](hIn your description of the option, make sure you address both the expert user and the user who knows nothing about your feature. Mention incompatibilities and issues here. }(hjE!hhhNhNubjZ)}(h**Definitely**h]h Definitely}(hjM!hhhNhNubah}(h]h ]h"]h$]h&]uh1jYhjE!ubh end your description with “if in doubt, say N” (or, occasionally, `Y’); this is for people who have no idea what you are talking about.}(hjE!hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj!ubeh}(h]h ]h"]h$]h&]uh1j`hj hhhhhNubja)}(hEdit the ``Makefile``: the CONFIG variables are exported here so you can usually just add a "obj-$(CONFIG_xxx) += xxx.o" line. The syntax is documented in ``Documentation/kbuild/makefiles.rst``. h]h)}(hEdit the ``Makefile``: the CONFIG variables are exported here so you can usually just add a "obj-$(CONFIG_xxx) += xxx.o" line. The syntax is documented in ``Documentation/kbuild/makefiles.rst``.h](h Edit the }(hjo!hhhNhNubj)}(h ``Makefile``h]hMakefile}(hjw!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjo!ubh: the CONFIG variables are exported here so you can usually just add a “obj-$(CONFIG_xxx) += xxx.o” line. The syntax is documented in }(hjo!hhhNhNubj)}(h&``Documentation/kbuild/makefiles.rst``h]h"Documentation/kbuild/makefiles.rst}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjo!ubh.}(hjo!hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjk!ubah}(h]h ]h"]h$]h&]uh1j`hj hhhhhNubja)}(hXQPut yourself in ``CREDITS`` if you consider what you've done noteworthy, usually beyond a single file (your name should be at the top of the source files anyway). ``MAINTAINERS`` means you want to be consulted when changes are made to a subsystem, and hear about bugs; it implies a more-than-passing commitment to some part of the code. h]h)}(hXPPut yourself in ``CREDITS`` if you consider what you've done noteworthy, usually beyond a single file (your name should be at the top of the source files anyway). ``MAINTAINERS`` means you want to be consulted when changes are made to a subsystem, and hear about bugs; it implies a more-than-passing commitment to some part of the code.h](hPut yourself in }(hj!hhhNhNubj)}(h ``CREDITS``h]hCREDITS}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj!ubh if you consider what you’ve done noteworthy, usually beyond a single file (your name should be at the top of the source files anyway). }(hj!hhhNhNubj)}(h``MAINTAINERS``h]h MAINTAINERS}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj!ubh means you want to be consulted when changes are made to a subsystem, and hear about bugs; it implies a more-than-passing commitment to some part of the code.}(hj!hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj!ubah}(h]h ]h"]h$]h&]uh1j`hj hhhhhNubja)}(hOFinally, don't forget to read ``Documentation/process/submitting-patches.rst`` h]h)}(hNFinally, don't forget to read ``Documentation/process/submitting-patches.rst``h](h Finally, don’t forget to read }(hj!hhhNhNubj)}(h0``Documentation/process/submitting-patches.rst``h]h,Documentation/process/submitting-patches.rst}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj!ubeh}(h]h ]h"]h$]h&]uh1hhhhMhj!ubah}(h]h ]h"]h$]h&]uh1j`hj hhhhhNubeh}(h]h ]h"]h$]h&]jjuh1j[hhhMhj hhubeh}(h] putting-your-stuff-in-the-kernelah ]h"] putting your stuff in the kernelah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hKernel Cantripsh]hKernel Cantrips}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj"hhhhhMubh)}(hGSome favorites from browsing the source. Feel free to add to this list.h]hGSome favorites from browsing the source. Feel free to add to this list.(}(hj("hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj"hhubh)}(h"``arch/x86/include/asm/delay.h``::h](j)}(h ``arch/x86/include/asm/delay.h``h]harch/x86/include/asm/delay.h}(hj:"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj6"ubh:}(hj6"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj"hhubjk)}(h#define ndelay(n) (__builtin_constant_p(n) ? \ ((n) > 20000 ? __bad_ndelay() : __const_udelay((n) * 5ul)) : \ __ndelay(n))h]h#define ndelay(n) (__builtin_constant_p(n) ? \ ((n) > 20000 ? __bad_ndelay() : __const_udelay((n) * 5ul)) : \ __ndelay(n))}hjR"sbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhMhj"hhubh)}(h``include/linux/fs.h``::h](j)}(h``include/linux/fs.h``h]hinclude/linux/fs.h}(hjd"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj`"ubh:}(hj`"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj"hhubjk)}(hX/* * Kernel pointers have redundant information, so we can use a * scheme where we can return either an error code or a dentry * pointer with the same return value. * * This should be a per-architecture thing, to allow different * error and pointer decisions. */ #define ERR_PTR(err) ((void *)((long)(err))) #define PTR_ERR(ptr) ((long)(ptr)) #define IS_ERR(ptr) ((unsigned long)(ptr) > (unsigned long)(-1000))h]hX/* * Kernel pointers have redundant information, so we can use a * scheme where we can return either an error code or a dentry * pointer with the same return value. * * This should be a per-architecture thing, to allow different * error and pointer decisions. */ #define ERR_PTR(err) ((void *)((long)(err))) #define PTR_ERR(ptr) ((long)(ptr)) #define IS_ERR(ptr) ((unsigned long)(ptr) > (unsigned long)(-1000))}hj|"sbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhMhj"hhubh)}(h(``arch/x86/include/asm/uaccess_32.h:``::h](j)}(h&``arch/x86/include/asm/uaccess_32.h:``h]h"arch/x86/include/asm/uaccess_32.h:}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubh:}(hj"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj"hhubjk)}(h#define copy_to_user(to,from,n) \ (__builtin_constant_p(n) ? \ __constant_copy_to_user((to),(from),(n)) : \ __generic_copy_to_user((to),(from),(n)))h]h#define copy_to_user(to,from,n) \ (__builtin_constant_p(n) ? \ __constant_copy_to_user((to),(from),(n)) : \ __generic_copy_to_user((to),(from),(n)))}hj"sbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhMhj"hhubh)}(h``arch/sparc/kernel/head.S:``::h](j)}(h``arch/sparc/kernel/head.S:``h]harch/sparc/kernel/head.S:}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubh:}(hj"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj"hhubjk)}(hX/* * Sun people can't spell worth damn. "compatability" indeed. * At least we *know* we can't spell, and use a spell-checker. */ /* Uh, actually Linus it is I who cannot spell. Too much murky * Sparc assembly will do this to ya. */ C_LABEL(cputypvar): .asciz "compatibility" /* Tested on SS-5, SS-10. Probably someone at Sun applied a spell-checker. */ .align 4 C_LABEL(cputypvar_sun4m): .asciz "compatible"h]hX/* * Sun people can't spell worth damn. "compatability" indeed. * At least we *know* we can't spell, and use a spell-checker. */ /* Uh, actually Linus it is I who cannot spell. Too much murky * Sparc assembly will do this to ya. */ C_LABEL(cputypvar): .asciz "compatibility" /* Tested on SS-5, SS-10. Probably someone at Sun applied a spell-checker. */ .align 4 C_LABEL(cputypvar_sun4m): .asciz "compatible"}hj"sbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhMhj"hhubh)}(h ``arch/sparc/lib/checksum.S:``::h](j)}(h``arch/sparc/lib/checksum.S:``h]harch/sparc/lib/checksum.S:}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubh:}(hj"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM-hj"hhubjk)}(h/* Sun, you just can't beat me, you just can't. Stop trying, * give up. I'm serious, I am going to kick the living shit * out of you, game over, lights out. */h]h/* Sun, you just can't beat me, you just can't. Stop trying, * give up. I'm serious, I am going to kick the living shit * out of you, game over, lights out. */}hj"sbah}(h]h ]h"]h$]h&]jzj{uh1jjhhhM/hj"hhubeh}(h]kernel-cantripsah ]h"]kernel cantripsah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hThanksh]hThanks}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj#hhhhhM6ubh)}(hXThanks to Andi Kleen for the idea, answering my questions, fixing my mistakes, filling content, etc. Philipp Rumpf for more spelling and clarity fixes, and some excellent non-obvious points. Werner Almesberger for giving me a great summary of :c:func:`disable_irq()`, and Jes Sorensen and Andrea Arcangeli added caveats. Michael Elizabeth Chastain for checking and adding to the Configure section. Telsa Gwynne for teaching me DocBook.h](hThanks to Andi Kleen for the idea, answering my questions, fixing my mistakes, filling content, etc. Philipp Rumpf for more spelling and clarity fixes, and some excellent non-obvious points. Werner Almesberger for giving me a great summary of }(hj!#hhhNhNubh)}(h:c:func:`disable_irq()`h]j)}(hj+#h]h disable_irq()}(hj-#hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj)#ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj" disable_irquh1hhhhM8hj!#ubh, and Jes Sorensen and Andrea Arcangeli added caveats. Michael Elizabeth Chastain for checking and adding to the Configure section. Telsa Gwynne for teaching me DocBook.}(hj!#hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM8hj#hhubeh}(h]thanksah ]h"]thanksah$]h&]uh1hhhhhhhhM6ubeh}(h](,unreliable-guide-to-hacking-the-linux-kernelheh ]h"](,unreliable guide to hacking the linux kernelkernel_hacking_hackeh$]h&]uh1hhhhhhhhKj}j`#hsj}hhsubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerj#error_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}(h]haj]jaunameids}(j`#hj_#j\#j9j6jbj_jjj,j)jZjWjHjEjjjljij=j:jjj j jK jH j j jjj:j7jjjjjjjcj`jbj_jjj+j(j5j2jMjJjFjCjjjEjBjjj=j:jjjUjRjjj5j2j j jjjvjsjjjjj8 j5 j_ j\ j j j"j"j #j #jW#jT#u nametypes}(j`#j_#j9jbjj,jZjHjjlj=jj jK j jj:jjjjcjbjj+j5jMjFjjEjj=jjUjj5j jjvjjj8 j_ j j"j #jW#uh}(hhj\#hj6jj_j<jjj)jjWj/jEjejjKjijj:jojjj jjH j j jN jj j7jjj=jj!jj!j`jj_jfjjej(jj2j.jJj@jCjjjIjBjjjPj:jjj5jRjjjXj2jj j@jjQjsjjjyjjj5 jj\ j; j jb j"j j #j"jT#j#u footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages](hsystem_message)}(hhh]h)}(hhh]h9Hyperlink target "kernel-hacking-hack" is not referenced.}hj#sbah}(h]h ]h"]h$]h&]uh1hhj#ubah}(h]h ]h"]h$]h&]levelKtypeINFOsourcehlineKuh1j#ubj#)}(hhh]h)}(hhh]h6Hyperlink target "local-bh-disable" is not referenced.}hj$sbah}(h]h ]h"]h$]h&]uh1hhj $ubah}(h]h ]h"]h$]h&]levelKtypej$sourcehlineMuh1j#ube transformerN include_log] decorationNhhub.