You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Kernel Subsystems Read and Understand
This lesson dives into key kernel subsystems (memory, scheduler, filesystems, networking, drivers), explaining their roles, how they interact, and where to read the code. We'll include code references, educational analogies, mermaid diagrams, and hands-on exercises to build intuition.
## Educational Context: Subsystems as Building Blocks
The kernel is modular: subsystems handle specific responsibilities, communicating via well-defined interfaces. Understanding one subsystem helps with others, as patterns repeat (e.g., allocators, locks, callbacks).
Analogy: The kernel is a factory. Subsystems are departments: Memory (warehouse), Scheduler (production line), Filesystems (inventory), Networking (shipping), Drivers (machinery).
## 1. Memory Management (mm/)
**Role**: Manages virtual and physical memory, preventing conflicts and optimizing usage.
**Why it matters**: Memory is finite; efficient management prevents crashes and enables multitasking.
**Key Concepts**:
- Virtual memory: Each process thinks it has all RAM.
- Page tables: Map virtual to physical addresses.
- Allocators: Buddy system for pages, slab for objects.
**Code Reading Guide**:
- Start with mm/page_alloc.c: �lloc_pages() Allocates physical pages using the buddy allocator.
- mm/mmap.c: do_mmap() Handles mmap() syscall for memory mapping.
- mm/slab.c: kmalloc() Object caching for frequent allocations.
**Code reference**: mm/page_alloc.c line ~500: Buddy allocator logic with bitmaps for free blocks.
**Educational note**: The buddy system splits memory like a tree, minimizing fragmentation.
**Interaction**: Called by scheduler (for task stacks), filesystems (for buffers), networking (for sockets).
## 2. Scheduler (kernel/sched/)
**Role**: Decides which process runs when, balancing fairness and performance.
**Why it matters**: Multitasking requires switching contexts efficiently.
**Key Concepts**:
- CFS (Completely Fair Scheduler): Proportional CPU time.
- Run queues: Ready tasks per CPU.
- Priorities: Nice values, real-time classes.
**Code Reading Guide**:
- kernel/sched/core.c: schedule() Main switch function.
- kernel/sched/fair.c: pick_next_task_fair() CFS decision logic.
- kernel/sched/rt.c: Real-time scheduling.
**Code reference**: kernel/sched/fair.c line ~2000: VR runtime calculations for fairness.
**Educational note**: CFS uses virtual runtime; tasks with lower VR run first.
**Interaction**: Uses memory (task structs), interacts with interrupts (preemption).
## 3. Filesystems (fs/)
**Role**: Organizes data on storage, providing file/directory abstractions.
**Why it matters**: Storage is diverse; VFS unifies access.
**Key Concepts**:
- Inodes: File metadata.
- Dentries: Directory entries.
- Superblocks: Filesystem info.
**Code Reading Guide**:
- s/open.c: do_sys_open() Open syscall handler.
- s/read_write.c: �fs_read() Generic read function.
- s/namei.c: path_lookup() Path resolution.
- s/ext4/: ext4-specific code.
**Code reference**: s/namei.c line ~1000: d_lookup() for dentry caching.
**Educational note**: VFS is polymorphic; each filesystem implements ops structs.
**Interaction**: Uses memory (buffers), scheduler (I/O waits), drivers (storage access).
## 4. Networking (net/)
**Role**: Handles data transmission over networks, from packets to sockets.
**Why it matters**: Communication is essential for distributed systems.
**Key Concepts**:
- OSI layers: Link (MAC), Network (IP), Transport (TCP/UDP).
- Sockets: User interface to networking.
- Routing: Path selection.
**Code Reading Guide**:
- et/core/dev.c:
egister_netdev() Device registration.
- et/ipv4/tcp.c: TCP state machine.
- et/ipv4/af_inet.c: IPv4 socket operations.
**Code reference**:
et/ipv4/tcp.c line ~500: TCP handshake states (SYN, SYN-ACK).
**Educational note**: Networking is layered; each layer adds headers.
**Interaction**: Uses memory (buffers), scheduler (softirqs), drivers (NICs).
## 5. Device Drivers (drivers/)
**Role**: Interfaces with hardware, abstracting devices as files or APIs.
**Why it matters**: Hardware varies; drivers make it uniform.
**Key Concepts**:
- Probe/init: Detect and initialize devices.
- Interrupts: Handle hardware events.
- DMA: Direct memory access for efficiency.
**Code Reading Guide**:
- drivers/net/ethernet/intel/e1000/: Intel NIC driver.
- drivers/block/sd.c: SCSI disk driver.
- drivers/char/random.c: Random device.
**Code reference**: drivers/net/ethernet/intel/e1000/e1000_main.c line ~1000: e1000_probe() PCI detection.
**Educational note**: Drivers register with subsystems (e.g., netdev for networking).
**Interaction**: Trigger interrupts, use memory, provide data to filesystems/networking.
## Mermaid Diagram: Subsystem Interactions
```mermaid
graph TD
A[Scheduler] --> B[Memory]
A --> C[Filesystems]
A --> D[Networking]
B --> E[Drivers]
C --> F[Drivers]
D --> G[Drivers]
A --> H[Interrupts]
B --> I[Page Faults]
C --> J[I/O Requests]
D --> K[Packets]
style A fill:#bbf
style B fill:#bfb
style C fill:#ffb
style D fill:#fbb
style E fill:#f9f
```
This graph shows how subsystems depend on each other and external inputs.
## Hands-on Exercises
1. **Memory Deep Dive**: In mm/page_alloc.c, find the �uddy comment and explain how the allocator works. Run grep -n 'buddy' mm/page_alloc.c.
2. **Scheduler Exploration**: Read kernel/sched/fair.c around pick_next_task_fair and describe the fairness algorithm. Compare to RT scheduler in
t.c.
3. **Filesystem Trace**: Follow open() from s/open.c to VFS ops. What struct holds filesystem operations?
4. **Networking Layers**: In
et/ipv4/tcp.c, find the state enum and list TCP states. How does it handle retransmissions?
5. **Driver Registration**: Pick a simple driver (e.g., drivers/char/null.c) and trace how it registers with the kernel.
This subsystem knowledge is key for targeted debugging. Next, try hands-on exercises in 4-hands-on-exercises.md.