Chapter 6. Common Routines

Table of Contents
printk() include/linux/kernel.h
copy_[to/from]_user() / get_user() / put_user() include/asm/uaccess.h
kmalloc()/kfree() include/linux/slab.h
current include/asm/current.h
udelay()/mdelay() include/asm/delay.h include/linux/delay.h
local_irq_save()/local_irq_restore() include/asm/system.h
local_bh_disable()/local_bh_enable() include/asm/softirq.h
smp_processor_id()/cpu_[number/logical]_map() include/asm/smp.h
__init/__exit/__initdata include/linux/init.h
__initcall()/module_init() include/linux/init.h
module_exit() include/linux/init.h
MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT include/linux/module.h

printk() include/linux/kernel.h

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:

printk(KERN_INFO "i = %u\n", i);
   

See include/linux/kernel.h; for other KERN_ values; these are interpreted by syslog as the level. Special case: for printing an IP address use

__u32 ipaddress;
printk(KERN_INFO "my ip: %d.%d.%d.%d\n", NIPQUAD(ipaddress));
   

printk() internally uses a 1K buffer and does not catch overruns. Make sure that will be enough.

Note: You will know when you are a real kernel hacker when you start typoing printf as printk in your user programs :)

Note: 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.