作者:empty 页数:467 出版社:empty |
De relo perk Library books are designed to prov de pract ng programmers wthunique, high-quality references and tutorials on the program tn ing languages andtechnologies they use in their daily work.All bou ks in the Dere loper'Lil nary are written by expert technology practitionerswho are especially skilled at organizing and presenting information in away tha esuseful for other program n mers
Key titles include some of the best, most widely ae claimed books within theirtopic areas:PHPSMySQLWalDevclopneentPython Esse u tial ReferenceLuke Welling&Laura ThomsonDavid Beazley
ISBN 978-0-672-32946-3ipok.talk.paper 1.Lnux.2.Opera tn gay stems(Computers)lex to egtheLoledStaesonreyaed paper at RRD on rell.Cram frd all, In dans.Many af the designations used by manufacturers and sellers to dist n guish their productsare claimed as trade merks.Where those de eig nations ap peer in this book, and the publisheer was aware of a trademark cia im, the designations have baen prnt ed with initial capitalThe author and publisher have taken care in the preparation of this book, but make noex or essed or impl led warranty of ary kind and assume ne responsibility for errors crom is-sions.No liability is assumed for incidental or consequential damages in connection with ararising out of the use of the information or programs contained herein,The publisher offers axcel i ant discounts on this book when ordered n quant y for bull cpu rchases or sp eol al sales, wh vich may include electronic versions and/or custom covers andsrtmbfemcrffebdeseoohac taming go a, mara tngt us, and bra on gini e ests.Evaluation Warning:The document was created with Spire.PDF for.NET.
Contents at a Glance
1Introducion to the Linux Kernel 1
2 GettingStarted with the Kernel 11
3 Process Management 23
4 Process Scheduling 41
5 System Calls69
6 Kernel DataStructures 85
7 Interrupts and Interrupt Handlers113
8 Bottom Halves and Deferring Work 133
9An Introduction to Kernel Synchronization161
10 Kernel Synchronization Methods175
11 Timers and Time Management 207
12 Memory Management 231
13 The Virtual Filesystem261
14 The Block I/OLayer 289
15 The Process Address Space 305
16 The PageCache and Page Writeback323
17 Devices and Modules 337
18 Debugging 363
19 Portability 379
20 Patches, Hacking, and the Community 395
Bibliography 407
Index 411
TableofContents
1 Introduction to the Linux Kernel 1
History of Unix 1
Along Came Linus:Introduction to Linux 3
Over v lew of Operating Systems and Kem els 4
Linux Versus Classic Unix Kernels 6
Linux Kem el Versions 8
The Linux Kem el Development Community 10
Before We Begin 10
2 GettingStarted with the Kernel 11
Obtaining the Kernel Source 11
Using Git 11
Inline Functions18
Inline Assembly 19
Branch Annotation 19
Installing the Kernel Source 12
Using Patches12
The Kem el SourceTree 12
Bull ding the Kernel13
Configuring the Kem el 14
Minimizing Build Noise 15
Spawning Multiple Build Jobs 16
Installing the New Kernel16
A Beast of a Different Nature 16
No libc or Standard Headers 17
GNU C 18
No Memory Protection 20
No(Easy) Use of Floating Point 20
5m all, Fixed-Size Stack 20
Synchron t zation and Concurrency 21
Importance of Portability 21
Conclusion 21
3 Process Management 23
The Process 23
Process Descriptor and the Task Structure 24
Allocating the Process Descriptor 25
Storing the Process Descriptor 26
Process State 27
Manipulating the Current Process State 29
Process Context 29
The Process Family Tree 29
Process Creation 31
Copy-on-Write 31
For king 32
v tork33
The Linux Implementation of Threads 33
Creating Threads34
Kernel Threads 35
Process Termination 36
Removing the Process Descriptor 37
The Dilemma of the Parentless Task 38
Conclusion 40
4 Process Scheduling 41
Multitasking 41
Linux's Process Scheduler 42
Policy 43
/O-Bound Versus Processor Bound Process 0s 43
Process Priority 44
Timeslice45
The Scheduling Policy in Action 45
The Linux Scheduling Algorithm 46
Scheduler Classes 46
Process Scheduling in Unix System ns 47
Fair Scheduling 48
The Linux Scheduling Implementation 50
Time Accounting 50
The Scheduler Entity Structure 50
The Virtual Runtime 51
Contents
Process Selection 52
Picking the Next Task 53
Adding Processes to the Tree 54
Removing Processes from the Tree 56
The Scheduler EntryPoint 57
Sleeping and Waking Up 58
Wait Queues 58
Waking Up 61
Preemption and Context Switching 62
User Preemption 62
Kernel Preemption 63
Real-Time Scheduling Policies 64
Scheduler-Related System Calls 65
Scheduling Policy and Priority Related
System Calls 66
Processor A finity System Calls 66
Yielding Pra cessor Time 66
Conclusion 67
5 System Calls 69
Communicating with the Kernel 69
APIs, POSIX, and the C Library70
Syscalls 71
System Call Numbers
System Call Performance
System Call Handler
Denoting the Correct System Call 73
Parameter Passing74
System Call Implementation74
Implementing System Calls 74
Verifying the Parameters 75
System Call Context78
Final Steps in Binding a System Call 79
Accessing the System Call from User-Space 81
Why Not to Implement a System Call 82
Conclusion 83
Can tents
6Kemel DataStructures 85
Linked Lists 85
Singly and Doubly Linked Lists 85
Circular Linked Lists 86
Moving Through a LinkedList 87
The Linux Kornel's Implementation 88
The LinkedList Structure 88
Defining a LinkedList 89
List Heads90
Manipulating Linked Lists 90
Adding aNode to a LinkedList 90
Deleting aNode from a LinkedList 91
Moving and Splicing LinkedList Nodes 92
T aver sing Linked Lists 93
The Basic Approach 93
The Usable Approach 93
Itera ing Through a List Backward 94
Iterating While Removing 95
Other LinkedList Methods 96
Queues96
Creating a Queue 97
Enqueuing Data 98
De queuing Data 98
Obtaining the Sizeof a Queue 98
Reso tting and Destroying the Queue 99
Example Quo ue Usage 99
Initializing an idr 101
Allocating a New UID 101
Looking Up a UID 102
Removing a UID 103
Destroying an idr 103
Binary Trees103
Binary Search Trees 104
Self Balancing Binary Sc arch Trees 105
Red-Black Trees 105
rb trees 106
Contents
What DataStructure to Use, When 108
Algorithmic Complexity 109
Algorithms 109
Big-O Notation 109
Big Theta Notation 109
Time Complexity 110
Can clusion111
7 Interrupts and Interrupt Handlers 113
Interrupts 113
Interrupt Handlers114
Top Halves Versus Bottom Halves 115
Registering an Interrupt Handier 116
Interrupt Handler Flags 116
An Interrupt Example 117
Freeing an Interrupt Handler 118
Writing an Interrupt Handler 118
Shared Handlers 119
A Real Lite Interrupt Hand ier. 120
Interrupt Context 122
Implement ng Interrupt Handlers 123
/proc/interrupts126
Interrupt Control 127
Disabling and Enabling Interrupts127
Disabling a Speci tic Interrupt Line 129
Status of the Interrupt System 130
Conclusion 131
8 Bottom Halves and Deferring Work 133
Bottom Halves 134
Why Bottom Halves ? 134
A World of Bottom Halves 135
The Original“Bottom Half”135
Task Queues 135
Soft i rgs and Task lets136
Dispelling the Confusion 137
Softirq s 137
Implementing Softirq s 137
The Softirq Handler138
Executing Softirq s 138
Using Softirq s140
Assigning an Index 140
Registering Your Handler141
Raising Your Softirq 141
Task lets 142
Implementing Task lets 142
The Task let Structure 142
Scheduling Task lets 143
Using Task lets144
Declaring Your Task let 144
Writing Your Task let Handler145
Scheduling Your Task let 145
k softirq d 146
The Old BH Mechanism 148
Work Queues 149
Implementing Work Queues 149
Why Do We Need Protection ? 162
The Single Variable163
DataStructures Representing the Threads 149
DataStructures Representing the Work 150
Work Queue Implementation Summary 152
Using Work Queues 153
Creating Work153
Your Work Queue Handler 153
Scheduling Work153
Flushing Work 154
Creating New Work Queues 154
The Old Task Queue Mechanism 155
Which Bottom Halt Should I Use? 156
Locking Between the Bottom Halves 157
Disabling Bottom Halves 157
Can clus ian 159
9An Introduction to Kernel Synchronization 161
Critical Regions and Race Conditions 162
Contents
x ili
Lacking 165
Causes of Concurrency 167
Knowing What to Protect 168
Deadlocks 169
Contention and Scalability 171
Conclusion 172
10 Kernel Synchronization Methods 175
Atomic Operations 175
Kernel Notion of Time 208
The Ideal HZ Value 210
Atomic Integer Operations 176
64-Bit Atomic D perations180
Atomic Bitwise Operations 181
SpinLocks 183
SpinLock Methods184
Other SpinLock Methods 186
SpinLocks and Bottom Halves 187
Reader Writer SpinLocks 188
Semaphores 190
Counting and Binary Semaphores 191
Creating and Initializing Semaphores 192
Using Semaphores 193
Reader Writer Semaphores 194
Mutexes 195
Semaphores Versus Mutexes197
SpinLocks Versus Mutexes 197
Completion Variables197
BKL:The Big Kernel Lock 198
Sequential Locks 200
Preemption Disabling 201
Ordering and Barriers 203
Conc i usion 206
11 Timers and Time Management 207
The Tick Rate:HZ 208
Advantages with a larger HZ 210
Disadvantages with a Larger HZ 211
Jit fies 212
Internal Representation of Jiffies 213
Jit fies Wraparound214
UserSpace and HZ 216
Hardware Clocks and Timers 216
RealTime Clock 217
System Timer 217
The Timer Interrupt Handler 217
The TimeofDay 220
Timers 222
Using Timers 222
Timer Race Conditions 224
Timer Implementation 224
Delaying Execution 225
Busy Looping 225
Small Delays 226
schedule_timeout() 227
12 Memory Management 231
schedule_time aut() Implementation 228
Sleeping on a Walt Queue, with a Timeout 229
Can clusion 230
Pages 231
Zones233
Getting Pages 235
Getting Zeroed Pages 236
Freeing Pages 237
kmalloc()238
gfp_mask Flags 238
Action Modifiers 239
Zone Modi tiers 240
Type Flags 241
k free() 243
vmalloc()244
Slab Layer 245
Design of the Slab Layer 245
Contents
xv
Slab Allocator Interface 249
Allocating Trom the Cache 250
Statically Allocating on the Stack 252
Common Filesystem Interface 261
Filesystem Abstraction Layer 262
Unix Filesystems 263
VFS Objects and Their DataStructures 255
The Superblock Object 266
Superblock Operations 267
The Inode Object 270
Inode Operations 271
The Dentry Object 275
Dentry State 276
The Dentry Cache 276
Dentry Operations 278
The File Object 279
File Operations 280
DataStructures Associated with Filesystems 285
DataStructures Associated with a Process 286
Can clusion 288
Example of Using the Slab Allocator 251
Single Page Kernel Stacks 252
Playing Fair on the Stack 253
High Memory Mappings 253
Permanent Mappings 254
Temporary Mappings 254
Per-CPU Allocations 255
The New per opu Interface256
Per-CPU Data at Compile-Time 256
Per-CPU Data at Runtime 257
Reasons for Using Per-CPU Data 258
Picking an Allocation Method 259
Conclusion 260
13 The Virtual Filesystem 261
14 The Block I/OLayer 289
Anatomy of a Block Dev ie 290
Buffers and Buffer Heads 291
The bioS trust ure 294
/O vectors 295
The Old Versus the New 296
Request Queues 297
//O Schedulers 297
The Job of an I/O Scheduler 298
The Linus Elevator 299
The Deadline I/O Scheduler 300
The Anticipatory I/O Scheduler 302
15 The Process Address Space 305
The Complete Fair Queuing I/O Scheduler 303
The No opl/O Scheduler 303
// 0 Scheduler Selection 304
Conc i usion 304
Address Spaces 305
The Memory Descriptor 306
Allocating a Memory Descriptor 308
Destroying a Memory Descriptor 309
Them_struct and Kernel Threads 309
Virtual Memory Areas 309
VMA Rags311
VMA Operations 312
Lists and Trees of Memory Areas313
Memory Areas in Real Life 314
Manipula ing Memory Areas 315
f1nd_vma()316
find_vma_prev t)317
tin d_vma_intersection() 317
mmap() and do_mmap() :Creating an
Address Interval 318
munna p() and do_munmap() ; Removing an
Address Into rval 320
Page Tables 320
Conclusion 322
Contents
xvii
16 The PageCache and Page Writeback 323
Approaches to Caching 323
Write Caching 324
Cache Eviction 324
The Linux PageCache326
Least Recently Used 325
The Two-List Strategy 325
The address space Object 326
address_space Operations 328
Radix Tree330
The Old Page HashTable 330
The Buffer Cache330
The Flusher Threads 331
Laptop Mode 333
Hello, World l 338
Building Modules340
History:bdflush.k updated, and pdf lush 333
Avoiding Congestion with Multiple Threads 334
Conclusion 335
17 Devices and Modules 337
Device Types 337
Modules 338
Living in the SourceTree 340
Living Externally 342
Installing Modules 342
Gone rating Module Dependencies 342
Loading Modules 343
Managing Configuration Options 344
Module Parameters 346
Exported Symbols 348
The Device Model 348
kobj oct s 349
K types 350
K sets351
Interrelation of K objects, K types, and K sets 351
Managing and Manipula tng K objects 352
xvii lCan tents
Reference Counts 353
sysfs 355
GettingStarted 363
Bugs in the kernel 364
Debugging by Printing 364
Incrementing and Decrementing
Reference Counts 354
K refs354
Robustness 365
Log levels 365
The Log Buffer 366
syslogd and klogd367
Transposing printf() and printk() 367
k syn oops369
kallsyms369
Kernel Debugging Options 370
Asserting Bugs and Dumping Information 370
The Saga of aKe mel Debugger 372
gdb 372
kgdb373
Poking and Probing the System 373
Using UID as a Conditional 373
Using Condition Variables 374
Using Statistics 374
Rate and Occurrence Limiting Your Debugging 375
Add ng and Rem aving k objects from systs 357
Adding Files to sysfs 358
Default Attributes358
Creating New Attributes 359
Destroying Attributes 360
sysfs Conventions 360
The Kem el Events Layer 361
Can clusion 362
18 Debugging 363
Do ps367
Magic SysRq Key 371
Contents
xix
Binary Sear a hing to Find the Culprit Change 376
Binary Searching with Git 376
When All Else Fails:The Community 377
Conclusion 378
19Portabllity 379
20 Patches, Hacking, and the Community 395
Portable Operating Systems 379
History af Portability in Linux 380
Word Size and DataTypes 381
Dpa que Types 384
Special Types 384
Explicitly Sized Types385
Signedness of Chars 386
Data Alignment 386
Avoiding Alignment Issues 387
Al in ment af Nonstandard Types 387
Structure Padding 387
Byte Dr der 389
Time 391
PageSize 391
Processor Dr dering 392
SMP Kernel Preemption, and High Me mary 393
Conc i usion 393
The Community395
Linux Coding Style396
Indention396
Switch Statements396
Spacing397
Braces 398
Line Length399
Naming 400
Functions 4D 0
Comments 400
Typedefs 401
Use Existing