Architecture of the Linux Kernel
Total Page:16
File Type:pdf, Size:1020Kb
Architecture of the Linux kernel Gerion Entrup Felix Herrmann Sophie Matter Elias Entrup Matthias Jakob Jan Eberhardt Mathias Casselt March 21, 2018 Contents 1 Introduction 7 1.1 Overview of Linux components ................................... 7 1.2 License ................................................. 9 2 Process Management 11 2.1 Processes ............................................... 11 2.2 Process creation - Forking a process ................................ 13 2.3 Threads ................................................ 16 3 Scheduling in Linux 17 3.1 Scheduling-Classes .......................................... 17 3.2 Process Categories .......................................... 17 3.3 O(1)-Scheduler ............................................ 18 3.4 Priorities and Timeslices ....................................... 18 3.5 Completely Fair Scheduler ...................................... 19 3.5.1 Implementation details .................................... 20 3.6 Kernel Preemption .......................................... 21 4 Interrupts 23 4.1 Interrupt processing ......................................... 23 4.2 Interrupt entry ............................................ 23 4.3 Event handler ............................................. 24 4.3.1 Example: Generic Top Halve ................................ 24 4.3.2 Example: Handler ...................................... 24 4.4 /proc/interrupts ........................................... 25 4.5 Conclusion .............................................. 25 5 Bottom Halves 27 5.1 Selecting the correct Bottom Halve mechanism .......................... 27 5.2 Preemption and Priorities ...................................... 28 5.3 Softirq ................................................. 28 5.4 Tasklet ................................................. 29 5.5 Work queue .............................................. 29 5.6 Conclusion .............................................. 30 6 Kernel Synchronization Methods 31 6.1 Why Is Synchronization Needed? .................................. 31 6.2 Standard Synchronization Methods ................................. 31 6.3 Special Synchronization Methods .................................. 35 6.4 Interaction with Interrupts, Bottom Halves and Kernel Preemption ............... 36 6.5 Which one to choose ......................................... 36 6.6 Problems ............................................... 37 6.7 Conclusion .............................................. 37 7 System calls 39 7.1 The sync system call ......................................... 39 7.1.1 Userland part ......................................... 40 3 Contents 7.1.2 Kernel part .......................................... 41 7.2 Syscalls with arguments ....................................... 43 7.3 Other input paths .......................................... 43 7.4 Conclusion .............................................. 44 8 Timer 45 8.1 Hz ................................................... 45 8.2 Jiffies ................................................. 45 8.2.1 Calculating with Jiffies .................................... 46 8.3 The timer interrupt handling .................................... 47 8.4 Timer ................................................. 48 8.4.1 The timer wheel ....................................... 49 8.5 Waiting ................................................ 49 8.6 Conclusion .............................................. 50 9 Memory Management 51 9.1 Description of Physical Memory ................................... 51 9.1.1 UMA and NUMA Architectures .............................. 51 9.1.2 Nodes ............................................. 51 9.1.3 Zones ............................................. 52 9.1.4 Pages ............................................. 53 9.2 Managing Memory .......................................... 54 9.2.1 Allocating and Freeing Pages ................................ 54 9.2.2 High Memory Mappings ................................... 54 9.2.3 Per-CPU Allocations ..................................... 55 9.2.4 Allocation Interfaces ..................................... 55 9.2.5 The Buddy System ...................................... 55 9.3 The Slab Allocator .......................................... 56 9.3.1 Slab Caches .......................................... 56 9.3.2 Slob and Slub Allocator ................................... 57 9.3.3 Allocating a Slab ....................................... 57 9.3.4 kmalloc, kfree ......................................... 58 9.3.5 Allocating Non-Contiguous Memory ............................ 59 9.3.6 Choosing an Allocation Method ............................... 59 9.4 Conclusion .............................................. 60 10 VFS 61 10.1 Filesystem ............................................... 61 10.1.1 file system type ........................................ 61 10.1.2 superblock .......................................... 61 10.1.3 vfsmount ........................................... 62 10.2 Resolving a file path ......................................... 62 10.2.1 dentry ............................................. 62 10.2.2 inode ............................................. 63 10.2.3 file ............................................... 64 10.3 Process-specific information ..................................... 64 10.4 Summary ............................................... 64 11 Virtual Adress Space 65 11.1 mm struct ............................................... 65 11.2 Virtual Memory Areas ........................................ 66 11.3 Page tables .............................................. 67 11.3.1 Recent developments ..................................... 67 11.4 Summary ............................................... 68 4 Contents 12 Block I/O (BIO) 69 12.1 Devices and Units .......................................... 69 12.2 Data structures ............................................ 69 12.3 I/O Scheduler ............................................. 70 12.3.1 Linus Elevator ........................................ 70 12.3.2 Deadline Scheduler ...................................... 71 12.3.3 Complete Fair Queuing Scheduler .............................. 71 12.3.4 Noop Scheduler ........................................ 72 12.3.5 Budget Fair Queuing Scheduler ............................... 72 12.3.6 Kyber Scheduler ....................................... 72 13 Page Cache and Page Writeback 73 13.1 Basic concepts and terms ...................................... 73 13.2 The Linux Page Cache ........................................ 73 13.3 Dirty Page Writeback ........................................ 74 14 sysfs 75 14.1 Data structures ............................................ 75 14.1.1 Kobjects ............................................ 75 14.1.2 Ksets ............................................. 78 14.1.3 Subsystems .......................................... 79 14.2 Checkpoint .............................................. 79 14.3 Mapping Kobjects .......................................... 80 14.3.1 Directories .......................................... 80 14.3.2 Files .............................................. 81 14.3.3 Interaction .......................................... 81 14.4 Summary ............................................... 82 15 Modules 83 15.1 Source code .............................................. 83 15.1.1 Kernel Exported Interfaces ................................. 83 15.1.2 Format ............................................ 83 15.1.3 Providing parameters .................................... 84 15.1.4 Example ............................................ 85 15.1.5 In-tree compilation ...................................... 87 15.1.6 External compilation ..................................... 87 15.2 Loading modules ........................................... 88 15.2.1 Dependencies ......................................... 88 15.2.2 Loading from inside the kernel ............................... 89 15.3 Summary ............................................... 89 Bibliography 91 5 1 Introduction On 26. August 1991 Linus Torvalds wrote in the Usenet-Newsgroup comp.os.minix the following message: Hello everybody out there using minix - I’m doing a (free) operating system (just a hobby, won’t be big and professional like gnu) for 386(486) AT clones. This has been brewing since april, and is starting to get ready. I’d like any feedback on things people like/dislike in minix, as my OS resembles it somewhat (same physical layout of the file-system (due to practical reasons) among other things). I’ve currently ported bash(1.08) and gcc(1.40), and things seem to work. This implies that I’ll get something practical within a few months, and I’d like to know what features most people would want. Any suggestions are welcome, but I won’t promise I’ll implement them :-) Linus ([email protected].fi) PS. Yes - it’s free of any minix code, and it has a multi-threaded fs. It is NOT protable (uses 386 task switching etc), and it probably never will support anything other than AT-harddisks, as that’s all I have :-(. This was the first public announcement of the Linux operating system, that is nowadays one of the biggest operating systems in the world. The most exiting part of Linux is that it is Free Software. It is licensed under the GNU GPL and developed in such a way that everybody can study the internals of Linux, dive into the code, improve it and contribute it back. This has formed a huge community around Linux and leads to a very