This book describes the implementation of core modules in the Linux kernel based on the source code of the Linux 5.0 kernel. It has a total of 9 chapters, and the main contents include processor architecture, ARM64 implementation in the Linux kernel, preliminary knowledge of memory management, physical memory and virtual memory, good topics of memory management, practical cases of memory management, basic concepts of process management, scheduling and load balancing of process management, debugging and case analysis of process management. The book is suitable for Linux system developers, embedded system developers and Android developers, and can also be read by teachers and students of computer-related majors. The author is an engineer at a first-line chip company, known as "Stupid Uncle" on the Internet . There are also chapter check-in activities to help read, and the author is invited to set the questions~ Interested netizens, welcome to participate~
how to apply
(1) Scroll down to the "Product Information" column to learn more about the book content and chapter settings;
(2) >> Click "I want to apply" and carefully fill in the reason for your application, reading sharing plan, etc. to have a chance to get the book for free.
Event Schedule
Application period: November 2 to November 16
Selection announcement: All shortlisted candidates will be announced before November 20
Reading period: November 27 to January 28
Awarding time: within two weeks after the event ends
Book Catalog
Chapter 1 Processor Architecture 1
1.1 Introduction to Processor Architecture 3
1.1.1 Reduced Instruction Set and Complex Instruction Set 3
1.1.2 Big/Little Endian Byte Order 3 1.1.3
The Whole Process
of Executing a Storage Read/Write Instruction 4
1.1.4 The Cause of Memory Barrier 8
1.1.5 How Cache Works 10
1.1.6 Cache Mapping Method 11
1.1.7 Set Associative Cache 13
1.1.8 The Difference Between PIPT and VIVT 14
1.1.9 Page Table Creation and Query Process 17
1.1.10 TLB 21
1.1.11 MESI Protocol 21
1.1.12 Cache Pseudo-Sharing 26
1.1.13 Cache Application in Linux Kernel
27
1.1.14 ARM Big/Little Core Architecture 28
1.1.15 Cache coherence and consistency
Memory model 30
1.1.16 Cache write-back policy and
Replacement policy 30
1.1.17 NUMA 31
1.1.18 ARM processor design 32
1.1.19 Recent developments 33
1.2 ARM64 architecture 34
1.2.1 ARMv8-A architecture 34
1.2.2 Common processors using ARMv8 architecture
Core 35
1.2.3 Basic concepts in ARMv8 architecture 35
1.2.4 ARMv8 processor execution state 36
1.2.5 Data width supported by ARMv8 36
1.2.6 Unaligned access 37
1.3 ARMv8 registers 37
1.3.1 General registers 37
1.3.2 Processor state 38
1.3.3 Special registers 39
1.3.4 System registers 41
1.4 A64 Instruction Set 42
1.4.1 Common Arithmetic and Move Instructions 42
1.4.2 Multiplication and Division Instructions 43
1.4.3 Shift Operation Instructions 45
1.4.4 Bit Operation Instructions 45
1.4.5 Conditional Operations 47
1.4.6 Memory Load Instructions 48
1.4.7 Multi-Byte Memory Load and Store Instructions 50
1.4.8 Non-Privileged Access Level Load and Store
Instructions 50
1.4.9 Introduction to Memory Barrier Instructions 51
1.4.10 Exclusive Memory Access Instructions 51
1.4.11 Jump and Compare Instructions 52 1.4.12
Exception Handling Instructions 53
1.4.13 System Register Access Instructions 54
1.5 GCC Inline Assembly 55
1.6 Function Call Standards and Stack Layout 57
1.7 ARM64 Exception Handling 59
1.7.1 Exception Types 59
1.7.2 Synchronous Exceptions and Asynchronous Exceptions 60
1.7.3 Processing After an Exception Occurs 60 Chapter 2 Implementation of ARM64 in the Linux Kernel 62
2.1 ARM64 Memory Management 63
2.1.1 Page Table 63
2.1.2 Page Table Mapping 64
2.1.3 Page Table Entry Descriptor 65
2.1.4 Page Table in the Linux Kernel 68
2.1.5 ARM64 Kernel Memory Distribution 74
2.1.6 Case Analysis: ARM64 Page Table Mapping
Process 78
2.2 Cache Management 84
2.3 TLB Management 87
2.4 Memory Attributes 90
2.4.1 Memory Attributes 91
2.4.2 Cache Sharing Attributes 94
2.5 Memory Barrier 95
2.5.1 Memory Barrier Instruction 95
2.5.2 Load-acquire barrier primitives and store-
release barrier primitives 96
2.6 Analysis of Linux kernel assembly code 97
2.6.1 Basics of link files 97
2.6.2 Analysis of vmlinux.lds.S file 99
2.6.3 Startup assembly code 102
2.6.4 Create identity mapping and kernel image
mapping 106
2.6.5 Analysis of __cpu_setup function 114
2.6.6 Analysis of __primary_switch function 117
2.7 Frequently asked questions about page tables 120
2.7.1 About the base address of the next level page table 121
2.7.2 Software traversal of page tables 121 Chapter 3 Preliminary knowledge of memory management 124
3.1 Memory management from the perspective of hardware 126
3.1.1 The "ancient times" of memory management 126
3.1.2 Segmentation mechanism 127
3.1.3 Paging mechanism 128
3.1.4 Conversion from virtual address to physical address
128
3.2 Memory Management from the Perspective of Software 129
3.2.1 Memory Management from the Perspective of Linux System Users
129 3.2.2
Memory Management from the Perspective
of Linux Application Developers 130
3.2.3 Memory Management from the Perspective of Memory Distribution
131
3.2.4 Memory Management from the Perspective of Processes 131
3.2.5 Memory Management from the Perspective of the Linux Kernel
135
3.3 Preliminary Knowledge of Physical Memory Management 136
3.3.1 UMA and NUMA Memory Architectures 136
3.3.2 Data Structures of Memory Management 138
3.3.3 Memory Size 140
3.3.4 Physical Memory Mapping 141
3.3.5 Zone Initialization 143
3.3.6 Space Division 145
3.3.7 Physical Memory Initialization 146 Chapter 4 Physical Memory and Virtual Memory 152
4.1 Fast Path of Page Allocation 153
4.1.1 Interface Function for Allocating Physical Pages 154
4.1.2 Allocation Mask 155
4.1.3 alloc_pages() Function 158
4.1.4 get_page_from_freelist() Function 162 4.1.5
zone_watermark_fast() Function 164
4.1.6 rmqueue() Function 165
4.1.7 Release Page 167
4.1.8 Summary 170
4.2 Slab Allocator 170
4.2.1 Background of Slab Allocator 170
4.2.2 Create Slab Descriptor 173
4.2.3 Memory Layout of Slab Allocator 176
4.2.4 Configure Slab Descriptor 178
4.2.5 Allocate Slab Object 180
4.2.6 Release Slab Cache Object 183
4.2.7 Slab allocator and buddy system interface
Function 185
4.2.8 Management area 185
4.2.9 kmalloc() 188
4.2.10 Summary 189
4.3 vmalloc() 190
4.4 Virtual memory management process address space 194
4.4.1 Process address space 194
4.4.2 mm_struct data structure 195
4.4.3 VMA data structure 197
4.4.4 VMA attributes 199
4.4.5 Find VMA 201
4.4.6 Insert VMA 203
4.4.7 Merge VMA 206
4.4.8 Red-black tree example 207
4.4.9 Summary 209
4.5 malloc() 210
4.5.1 brk system call 210
4.5.2 User-mode address space division 212
4.5.3 __do_sys_brk() function 213
4.5.4 do_brk_flags() function 213
4.5.5 __mm__populate() function 215
4.5.6 get_user_pages() function 216
4.5.7 Summary 220
4.6 mmap 223
4.6.1 Overview of mmap 223
4.6.2 Summary 225
4.7 Page fault exception handling 227
4.7.1 Low-level handling of ARM64 page fault exceptions
Process 228
4.7.2 do_page_fault() function 234
4.7.3 handle_mm_fault() function 237
4.7.4 Anonymous page fault interrupt 240
4.7.5 System zero page 241
4.7.6 File mapping page fault interrupt 242
4.7.7 Copy-on-write 245 4.7.8
Competition issues
caused by ARM64 hardware DBM mechanism 248
4.7.9 Issues about the safe use of pte_offset_map()
251
4.7.10 Competition issues about copy-on-write 253 4.7.11 Why should
we flush the TLB
before switching page table entries 256
4.7.12 Deadlock caused by page fault exception 257
4.7.13 Summary 257 Chapter 5 Good Topics in Memory Management 259
5.1 page 260
5.1.1 page data structure 260
5.1.2 Application of _refcount 265
5.1.3 Application of _mapcount 268
5.1.4 PG_Locked 269
5.1.5 Magical uses of mapping members 270
5.1.6 Several interface functions related to page 271
5.1.7 Summary 272
5.2 RMAP 272
5.2.1 The main data structure of RMAP 273
5.2.2 The parent process generates anonymous pages 274
5.2.3 Create a child process based on the parent process 277
5.2.4 Copy-on-write occurs in the child process 280
5.2.5 Application of RMAP 281
5.2.6 Summary 283
5.3 Page Reclamation 285
5.3.1 LRU linked list 285
5.3.2 Second chance method 288
5.3.3 Triggering page reclamation 293
5.3.4 kswapd kernel thread 293
5.3.5 balance_pgdat() function 296
5.3.6 shrink_node() function 298
5.3.7 shrink_active_list() function 302
5.3.8 shrink_inactive_list() function 304
5.3.9
5.3.10 Page Reclaim Mechanism 309
5.3.11 Refault Distance Algorithm 312
5.3.12 Summary 317
5.4 Anonymous Page Lifecycle 318
5.4.1 Generation of Anonymous Pages 319
5.4.2 Use of Anonymous Pages 319
5.4.3 Swapping Out Anonymous Pages 319
5.4.4 Swapping In Anonymous Pages 321 5.4.5 Destroying Anonymous
Pages 321 5.5 Page Migration
321
5.5.1 Which Pages Can Be Migrated 322
5.5.2 Main Page Migration Function 322
5.5.3 move_to_new_page() Function 324
5.5.4 Migrate Page Table 325
5.5.5 Migrate Non-LRU Pages 326
5.5.6 Summary 328
5.6 Memory Consolidation 329
5.6.1 The Basic Principle of Memory Consolidation 329
5.6.2 Triggering Memory Consolidation 330
5.6.3 Direct Memory Consolidation 330
5.6.4 The compact_zone() Function 333
5.6.5 Which Pages Are Suitable for Memory Consolidation 338
5.7 KSM 339
5.7.1 Enabling KSM 339 5.7.2
Basic Implementation of KSM 343
5.7.3 KSM Data Structure 345
5.7.4 New Features of the New Version of KSM 345
5.7.5 Can Pages Allocated by malloc() Be
Scanned by KSM? 353
5.7.6 Merging Pages 355
5.7.7 An Interesting Calculation Formula 358
5.7.8 The Value of page->index 359
5.7.9 Summary 360
5.8 Slow Path of Page Allocation 362
5.8.1 alloc_pages_slowpath() Function 362
5.8.2 Water Level Management and Allocation Priority 367
5.9 Memory Fragmentation Management 368
5.9.1 How to Reduce
Memory Fragmentation with the Buddy System Algorithm 368
5.9.2 Page Migration Types and Memory Regularization 370 5.9.3 Anti-fragmentation
Optimization
Newly Added in the Linux 5.0 Kernel 371 Chapter 6 Practical Case Analysis of Memory Management 375
6.1 Memory Management Log Information and Debug Information 375
6.1.1 vm_stat Count 375
6.1.2 meminfo Analysis 377
6.1.3 Buddy System Information 383
6.1.4 Viewing Memory Management Area Information 383
6.1.5 Viewing Process-related Memory Information 385
6.1.6 Why S_swap and P_swap
Are Not Equal 387
6.1.7 Interpretation of the log information output by the OOM Killer mechanism
388 6.1.8 Interpretation of the crash
log information
output after the page fault exception 394
6.2 Memory management tuning parameters 400 6.2.1
Parameter min_free_kbytes
that affects the tuning of the memory management area water level 401
6.2.2 Parameter lowmem_reserve_ratio that affects page allocation
402
6.2.3 Parameters that affect page recycling 404
6.2.4 Parameters that affect dirty page writeback 406
6.3 Practical case analysis of memory management 407 6.3.1 Case 1:
Crash caused by
page fault exception and file system 407
6.3.2 Case 2: Virtual machine crash caused by KSM and NUMA
410
6.3.3 Case 3: Why can't a
page be allocated 415
6.3.4 Case 4: Secret Mission - Dynamic Modification
Four crashes caused by the system call table 420 Chapter 7 Basic Concepts of Process Management 430
7.1 Basic concepts about processes 431
7.1.1 Origin of processes 431
7.1.2 Process descriptors 432
7.1.3 Process lifecycle 434
7.1.4 Process identifiers 436
7.1.5 Family relationships between processes 436 7.1.6
Get the current process 438
7.1.7 Process 0 and process 1 440 7.2 Operating system
primitives
related to process creation and termination 441
7.2.1 Copy-on-write technology 442
7.2.2 fork() function 443
7.2.3 vfork() function 444
7.2.4 clone() function 445
7.2.5 Kernel threads 445
7.2.6 Terminating a process 446
7.2.7 Zombie processes and process orphans 446
7.3 Code analysis: process creation and termination 447
7.3.1 Analysis of _do_fork() Function 447
7.3.2 Analysis of copy_process() Function 449
7.3.3 Analysis of dup_task_struct() Function 453
7.3.4 Analysis of sched_fork() Function 453
7.3.5 Analysis of copy_mm() Function 454
7.3.6 Return after Process Creation 458
7.4 Process Scheduling Primitives 461
7.4.1 Process Classification 461
7.4.2 Process Priority and Weight 461
7.4.3 Scheduling Policy 463
7.4.4 Time Slice 465
7.4.5 Classic Scheduling Algorithm 465
7.4.6 O(n) Scheduling Algorithm of Linux Kernel 467
7.4.7 O(1) Scheduling Algorithm of Linux Kernel 467
7.4.8 CFS of Linux Kernel 468 Chapter 8 Process Management Scheduling and Load Balancing 469
8.1 CFS 470
8.1.1 Calculation of vruntime 471
8.1.2 Scheduler Data Structure 474
8.1.3 Analysis of Process Creation Code 479 8.1.4
Analysis
of Process Addition to Scheduler Code 483
8.1.5 Process Scheduling 485
8.1.6 Process Switching 488
8.1.7 Scheduling Beat 497
8.1.8 Group Scheduling Mechanism 498 8.1.9
Summary 502
8.2 Load Calculation 503
8.2.1 How to Measure the Load of a CPU 503
8.2.2 Workload and Quantized Load 504
8.2.3 Calculation of Historical Accumulated Decay 505
8.2.4 Calculation of Quantized Load 506
8.2.5 Calculation of Actual Computing Power 507
8.2.6 sched_avg Data Structure 508
8.2.7 PELT Code Analysis 510
8.2.8 PELT Interface Functions 516
8.3 SMP Load Balancing 517
8.3.1 CPU Management Bitmap 517
8.3.2 CPU Scheduling Domains 519
8.3.3 Establishing CPU Scheduling Domain Topology 522
8.3.4 Load Balancing 529
8.3.5 Waking Up a Process 536
8.3.6 Wake Affine Feature 538
8.3.7 Debugging 541
8.3.8 Summary 542
8.4 Green Energy Saving Scheduler 542
8.4.1 Quantified Computing Power 545
8.4.2 Energy Efficiency Model 548
8.4.3 OPP Subsystem 551
8.4.4 Initialize CPUfreq-dt driver 556
8.4.5 Register energy efficiency model subsystem 557
8.4.6 Which CPU should be selected to execute wake-up
process p 561
8.4.7 Overutilized condition judgment 569
8.4.8 CPU dynamic frequency adjustment 570
8.4.9 Summary 573
8.5 Real-time scheduling 574
8.5.1 Real-time delay analysis 574
8.5.2 Linux kernel real-time performance improvement 576 Chapter 9 Process management debugging and case analysis 580
9.1 Process management debugging 580
9.1.1 View scheduling information related to the process 580
9.1.2 View CFS information 582
9.1.3 View scheduling domain information 585
9.1.4 Debug nodes related to scheduling 586
9.2 Comprehensive case analysis - system scheduling 587
9.3 Process management 589
9.3.1 The Nature of Processes 589
9.3.2 Inescapable Process Priorities 590
9.3.3 Choice of Scheduler 591
9.3.4 Understanding Load in Four-Dimensional Space 591
9.3.5 Case Analysis - Why Can't
Scheduling 592
For more event information, >>click here to go to the event page
|