Linux Kernel Development notes
These are some really rough notes about some of the content that I found interesting in “Linux Kernel Development” (Second Edition) by Robert Love:
- Chapter 3: Process Management
- Process descriptor structures:
task_structandthread_info - Process states:
TASK_RUNNING,TASK_INTERRUPTIBLE,TASK_UNINTERRUPTIBLE,TASK_ZOMBIE,TASK_STOPPED(#defined in include/linux/sched.h) - Process creation and Copy-on-write,
fork(),vfork() - The Linux implementation of threads,
clone(), kernel threads (processes that execute only in kernel-space and have no address space; created withkernel\_thread()) - Process termination (
sys\_exit()->do\_exit()), waiting (sys\_wait()->do\_wait()), zombies, reparenting (forget\_original\_parent())
- Process descriptor structures:
- Chapter 4: Process Scheduling
- Basically about the 2.6 kernel’s O(1) scheduler
struct runqueue(has since been renamed tostruct rq) andstruct prio_array- The main scheduler function is
schedule(). - Recalculating time slices - swapping the active and expired priority arrays - happens here.
- Finding the highest priority runnable task is done quickly and cleverly by searching a 140-bit priority array for the first set bit - this happes here.
- Dynamic priority = static priority + bonus for interactivity or a penalty for processor hog -
effective_prio() - Wait queues -
wait_queue_head_t(the book has a typo here, listingwake_queue_head_t); create statically withDECLARE_WAITQUEUE()or dynamically withinit_waitqueue_head(). Wake up processes waiting on a given wait queue withwake_up(). - Load balancing among CPUs on multiprocessing systems happens via
load_balance(), which is called:- by
schedule()when the current runqueue is empty - by timer; every 1 ms when system idle and every 200 ms otherwise
- by
- Preemption (including kernel preemption) and context switching -
context\_switch(), which makes use of the architecture-dependent routinesswitch\_mm()andswitch_to()(super-fun inline assembler!) - Real-time scheduling policies -
SCHED\_FIFOandSCHED\_RR- defined ininclude/linux/sched.h. The difference isSCHED\_RRgets a limited timeslice butSCHED\_FIFOcan run indefinitely. Both are always scheduled overSCHED\_NORMALtasks. Linux has soft real-time behavior. - Schedule-related system calls - priority and policy-related calls, including processor affinity calls
- Chapter 5: System Calls
- x86:
int $0x80->system_call()(arch/i386/kernel/entry.S) ->sys_call_table(the book says that this resides in the same file assystem_call()but in my 2.6.20 tree it’s in another file calledsyscall_table.S). This file mentions 319 system calls for i386. copy_to_user(),copy_from_user()
- x86:
- Chapter 6: Interrupts and Interrupt Handlers
- Top halves and bottom halves
- Registering and freeing an interrupt handler -
request_irq(),free_irq(). Since the publication of the book, the kernel has added a typedefirq_handler_twhich specifies the required function signature for a handler (patch). The book mentions aregsparameter that might not be long for this world, and indeed, it was removed in 2.6.19 (according to LWN here and here). - fast interrupt handler - run with all interrupts disabled on the current processor instead of just the current interrupt. The book describes the
SA_INTERRUPTflag, but according to LWN’s 2.6 kernel API page, in 2.6.18 this (along with otherSA_*flags) was deprecated in favor of the more aptly namedIRQF_DISABLED(see include/linux/interrupt.h) - Code for a real-life interrupt handler -
rtc_interrupt() - Implementation of interrupt handling -
do_IRQ(),handle_irq_event(),ret_from_intr() /proc/interrupts- Disabling and enabling interrupts -
local_irq_disable(),local_irq_enable(),local_irq_save(),local_irq_restore() (include/linux/irqflags.h and include/asm-i386/irqflags.h)
- Disabling a specific interrupt line -
disable_irq(),disable_irq_nosync(),enable_irq(),synchronize_irq() (kernel/irq/manage.c) - Getting status -
in_interrupt(),in_irq() (include/linux/hardirq.h)
- Chapter 7: Bottom Halves and Deferring Work
- Chapter 8: Kernel Synchronization Introduction
- Chapter 9: Kernel Synchronization Methods
- Chapter 10: Timers and Time Management
- Chapter 11: Memory Management
- Chapter 16: Modules
- Chapter 17: kobjects and sysfs
Popularity: 13% [?]



