?? kernel-hacking.tmpl
字號:
<programlisting>if (signal_pending()) return -ERESTARTSYS; </programlisting> <para> If you're doing longer computations: first think userspace. If you <emphasis>really</emphasis> 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: </para> <programlisting>if (current->need_resched) schedule(); /* Will sleep */ </programlisting> <para> A short note on interface design: the UNIX system call motto is "Provide mechanism not policy". </para> </chapter> <chapter id="deadlock-recipes"> <title>Recipes for Deadlock</title> <para> You cannot call any routines which may sleep, unless: </para> <itemizedlist> <listitem> <para> You are in user context. </para> </listitem> <listitem> <para> You do not own any spinlocks. </para> </listitem> <listitem> <para> You have interrupts enabled (actually, Andi Kleen says that the scheduling code will enable them for you, but that's probably not what you wanted). </para> </listitem> </itemizedlist> <para> Note that some functions may sleep implicitly: common ones are the user space access functions (*_user) and memory allocation functions without <symbol>GFP_ATOMIC</symbol>. </para> <para> You will eventually lock up your box if you break these rules. </para> <para> Really. </para> </chapter> <chapter id="common-routines"> <title>Common Routines</title> <sect1 id="routines-printk"> <title> <function>printk()</function> <filename class=headerfile>include/linux/kernel.h</filename> </title> <para> <function>printk()</function> 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: </para> <programlisting>printk(KERN_INFO "i = %u\n", i); </programlisting> <para> See <filename class=headerfile>include/linux/kernel.h</filename>; for other KERN_ values; these are interpreted by syslog as the level. Special case: for printing an IP address use </para> <programlisting>__u32 ipaddress;printk(KERN_INFO "my ip: %d.%d.%d.%d\n", NIPQUAD(ipaddress)); </programlisting> <para> <function>printk()</function> internally uses a 1K buffer and does not catch overruns. Make sure that will be enough. </para> <note> <para> You will know when you are a real kernel hacker when you start typoing printf as printk in your user programs :) </para> </note> <!--- From the Lions book reader department --> <note> <para> Another 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. </para> </note> </sect1> <sect1 id="routines-copy"> <title> <function>copy_[to/from]_user()</function> / <function>get_user()</function> / <function>put_user()</function> <filename class=headerfile>include/asm/uaccess.h</filename> </title> <para> <emphasis>[SLEEPS]</emphasis> </para> <para> <function>put_user()</function> and <function>get_user()</function> 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 <constant>-EFAULT</constant> or 0. </para> <para> <function>copy_to_user()</function> and <function>copy_from_user()</function> are more general: they copy an arbitrary amount of data to and from userspace. <caution> <para> Unlike <function>put_user()</function> and <function>get_user()</function>, they return the amount of uncopied data (ie. <returnvalue>0</returnvalue> still means success). </para> </caution> [Yes, this moronic interface makes me cringe. Please submit a patch and become my hero --RR.] </para> <para> The functions may sleep implicitly. This should never be called outside user context (it makes no sense), with interrupts disabled, or a spinlock held. </para> </sect1> <sect1 id="routines-kmalloc"> <title><function>kmalloc()</function>/<function>kfree()</function> <filename class=headerfile>include/linux/slab.h</filename></title> <para> <emphasis>[MAY SLEEP: SEE BELOW]</emphasis> </para> <para> These routines are used to dynamically request pointer-aligned chunks of memory, like malloc and free do in userspace, but <function>kmalloc()</function> takes an extra flag word. Important values: </para> <variablelist> <varlistentry> <term> <constant> GFP_KERNEL </constant> </term> <listitem> <para> May sleep and swap to free memory. Only allowed in user context, but is the most reliable way to allocate memory. </para> </listitem> </varlistentry> <varlistentry> <term> <constant> GFP_ATOMIC </constant> </term> <listitem> <para> Don't sleep. Less reliable than <constant>GFP_KERNEL</constant>, but may be called from interrupt context. You should <emphasis>really</emphasis> have a good out-of-memory error-handling strategy. </para> </listitem> </varlistentry> <varlistentry> <term> <constant> GFP_DMA </constant> </term> <listitem> <para> Allocate ISA DMA lower than 16MB. If you don't know what that is you don't need it. Very unreliable. </para> </listitem> </varlistentry> </variablelist> <para> If you see a <errorname>kmem_grow: Called nonatomically from int </errorname> warning message you called a memory allocation function from interrupt context without <constant>GFP_ATOMIC</constant>. You should really fix that. Run, don't walk. </para> <para> If you are allocating at least <constant>PAGE_SIZE</constant> (<filename class=headerfile>include/asm/page.h</filename>) bytes, consider using <function>__get_free_pages()</function> (<filename class=headerfile>include/linux/mm.h</filename>). 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. </para> <para> If you are allocating more than a page worth of bytes you can use <function>vmalloc()</function>. It'll allocate virtual memory in the kernel map. This block is not contiguous in physical memory, but the <acronym>MMU</acronym> 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 <function>alloc_bootmem()</function> routine. </para> <para> Before inventing your own cache of often-used objects consider using a slab cache in <filename class=headerfile>include/linux/slab.h</filename> </para> </sect1> <sect1 id="routines-current"> <title><function>current</function> <filename class=headerfile>include/asm/current.h</filename></title> <para> This 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 <emphasis>not NULL</emphasis> in interrupt context. </para> </sect1> <sect1 id="routines-udelay"> <title><function>udelay()</function>/<function>mdelay()</function> <filename class=headerfile>include/asm/delay.h</filename> <filename class=headerfile>include/linux/delay.h</filename> </title> <para> The <function>udelay()</function> function can be used for small pauses. Do not use large values with <function>udelay()</function> as you risk overflow - the helper function <function>mdelay()</function> is useful here, or even consider <function>schedule_timeout()</function>. </para> </sect1> <sect1 id="routines-endian"> <title><function>cpu_to_be32()</function>/<function>be32_to_cpu()</function>/<function>cpu_to_le32()</function>/<function>le32_to_cpu()</function> <filename class=headerfile>include/asm/byteorder.h</filename> </title> <para> The <function>cpu_to_be32()</function> 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: <function>be32_to_cpu()</function>, etc. </para> <para> There are two major variations of these functions: the pointer variation, such as <function>cpu_to_be32p()</function>, which take a pointer to the given type, and return the converted value. The other variation is the "in-situ" family, such as <function>cpu_to_be32s()</function>, which convert value referred to by the pointer, and return void. </para> </sect1> <sect1 id="routines-local-irqs"> <title><function>local_irq_save()</function>/<function>local_irq_restore()</function> <filename class=headerfile>include/asm/system.h</filename> </title> <para> These routines disable hard interrupts on the local CPU, and restore them. They are reentrant; saving the previous state in their one <varname>unsigned long flags</varname> argument. If you know that interrupts are enabled, you can simply use <function>local_irq_disable()</function> and <function>local_irq_enable()</function>. </para> </sect1> <sect1 id="routines-softirqs"> <title><function>local_bh_disable()</function>/<function>local_bh_enable()</function> <filename class=headerfile>include/asm/softirq.h</filename></title> <para> These 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, tasklets and bottom halves from running on the current CPU. </para> </sect1> <sect1 id="routines-processorids"> <title><function>smp_processor_id</function>()/<function>cpu_[number/logical]_map()</function> <filename class=headerfile>include/asm/smp.h</filename></title> <para> <function>smp_processor_id()</function> returns the current processor number, between 0 and <symbol>NR_CPUS</symbol> (the maximum number of CPUs supported by Linux, currently 32). These values are not necessarily continuous: to get a number between 0 and <function>smp_num_cpus()</function> (the number of actual processors in this machine), the <function>cpu_number_map()</function> function is used to map the processor id to a logical number. <function>cpu_logical_map()</function> does the reverse.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -