ContentsXVSlabAllocator Interface249AllocatingfromtheCache250ExampleofUsingtheSlabAllocator251StaticallyAllocatingontheStack252Single-PageKernelStacks252PlayingFair on the Stack253HighMemoryMappings253PermanentMappings254TemporaryMappings:254Per-CPUAllocations255TheNewpercpu Interface256Per-CPU Dataat Compile-Time256Per-CPUDataatRuntime257ReasonsforUsingPer-CPUData258PickinganAllocation Method259Conclusion260133TheVirtualFilesystem261CommonFilesystemInterface261FilesystemAbstractionLayer262UnixFilesystems263VFSObjectsandTheirDataStructures265TheSuperblockObject266SuperblockOperations267The InodeObject270InodeOperationss271TheDentryObject 275DentryState276TheDentryCache276DentryOperations278The File Object279FileOperations280Data StructuresAssociatedwithFilesystems285DataStructuresAssociatedwithaProcess286Conclusion288www.it-ebooks.info
ptg Contents xv Slab Allocator Interface 249 Allocating from the Cache 250 Example of Using the Slab Allocator 251 Statically Allocating on the Stack 252 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 percpu Interface 256 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 Common Filesystem Interface 261 Filesystem Abstraction Layer 262 Unix Filesystems 263 VFS Objects and Their Data Structures 265 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 Data Structures Associated with Filesystems 285 Data Structures Associated with a Process 286 Conclusion 288 www.it-ebooks.info
Contentsxvi14TheBlockl/oLayer289AnatomyofaBlockDevice290Buffersand BufferHeads291ThebioStructure294/Ovectors295TheOld Versus the New296RequestQueues2971/0Schedulers297TheJobofanI/oScheduler298TheLinus Elevator299300TheDeadlinel/OSchedulerTheAnticipatoryI/OScheduler302TheCompleteFairQueuing/0Scheduler303TheNoopl/0Scheduler:303/OSchedulerSelection304Conclusion 30415TheProcessAddressSpace305AddressSpaces305TheMemoryDescriptor306AllocatingaMemoryDescriptor 308309DestroyingaMemoryDescriptorThemmstructandKernelThreads309VirtualMemoryAreas309VMA Flags311VMAOperations312ListsandTreesof MemoryAreas6313MemoryAreasinRealLife314ManipulatingMemoryAreas315find_vma()316find_vma_prev()317317find_vma_intersection()mmap()anddo_mmap():CreatinganAddress Interval318munmap()anddo_munmap():RemovinganAddressInterval320PageTables320322Conclusionwww.it-ebooks.info
ptg xvi Contents 14 The Block I/O Layer 289 Anatomy of a Block Device 290 Buffers and Buffer Heads 291 The bio Structure 294 I/O vectors 295 The Old Versus the New 296 Request Queues 297 I/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 The Complete Fair Queuing I/O Scheduler 303 The Noop I/O Scheduler 303 I/O Scheduler Selection 304 Conclusion 304 15 The Process Address Space 305 Address Spaces 305 The Memory Descriptor 306 Allocating a Memory Descriptor 308 Destroying a Memory Descriptor 309 The mm_struct and Kernel Threads 309 Virtual Memory Areas 309 VMA Flags 311 VMA Operations 312 Lists and Trees of Memory Areas 313 Memory Areas in Real Life 314 Manipulating Memory Areas 315 find_vma() 316 find_vma_prev() 317 find_vma_intersection() 317 mmap() and do_mmap(): Creating an Address Interval 318 munmap() and do_munmap(): Removing an Address Interval 320 Page Tables 320 Conclusion 322 www.it-ebooks.info
Contentsxvii16ThePageCacheandPageWriteback323ApproachestoCaching323WriteCaching324Cache Eviction 324LeastRecentlyUsed325TheTwo-ListStrategy325TheLinuxPageCache326The address_space Object326328address_spaceOperationsRadixTree330330TheOldPageHashTableTheBufferCache330TheFlusherThreads331LaptopMode333History:bdflush,kupdated,andpdflush333AvoidingCongestionwithMultipleThreads334Conclusion33533717DevicesandModulesDevice Types337Modules338Hello,World!338BuildingModules340Living in the Source Tree340LivingExternally342InstallingModules342GeneratingModuleDependencies6342LoadingModules343344Managing Configuration OptionsModuleParameters6346ExportedSymbols348TheDeviceModel348Kobjects349Ktypes350Ksets351InterrelationofKobjects,Ktypes,andKsets351ManagingandManipulatingKobjects352www.it-ebooks.info
ptg Contents xvii 16 The Page Cache and Page Writeback 323 Approaches to Caching 323 Write Caching 324 Cache Eviction 324 Least Recently Used 325 The Two-List Strategy 325 The Linux Page Cache 326 The address_space Object 326 address_space Operations 328 Radix Tree 330 The Old Page Hash Table 330 The Buffer Cache 330 The Flusher Threads 331 Laptop Mode 333 History: bdflush, kupdated, and pdflush 333 Avoiding Congestion with Multiple Threads 334 Conclusion 335 17 Devices and Modules 337 Device Types 337 Modules 338 Hello, World! 338 Building Modules 340 Living in the Source Tree 340 Living Externally 342 Installing Modules 342 Generating Module Dependencies 342 Loading Modules 343 Managing Configuration Options 344 Module Parameters 346 Exported Symbols 348 The Device Model 348 Kobjects 349 Ktypes 350 Ksets 351 Interrelation of Kobjects, Ktypes, and Ksets 351 Managing and Manipulating Kobjects 352 www.it-ebooks.info
ContentsxviliReferenceCounts353Incrementing and DecrementingReferenceCounts354Krefs354sysfs355AddingandRemovingkobjectsfromsysfs357AddingFilesto sysfs358Default Attributes358CreatingNewAttributes359DestroyingAttributes360sysfsConventions360TheKernel Events Layer361Conclusion362183Debugging363GettingStarted363Bugs in the Kernel 364DebuggingbyPrinting364Robustness365Loglevels365The Log Buffer366syslogdandklogd367367Transposingprintf()andprintk()Oops367ksymoops369kallsyms369KernelDebuggingOptions370370AssertingBugsandDumpingInformationMagic SysRKey371TheSagaofaKernel Debugger3372gdb372kgdb373Poking andProbing theSystem373UsingUIDasaConditional373Using Condition Variables374Using Statistics374RateandOccurrenceLimiting YourDebugging375www.it-ebooks.info
ptg xviii Contents Reference Counts 353 Incrementing and Decrementing Reference Counts 354 Krefs 354 sysfs 355 Adding and Removing kobjects from sysfs 357 Adding Files to sysfs 358 Default Attributes 358 Creating New Attributes 359 Destroying Attributes 360 sysfs Conventions 360 The Kernel Events Layer 361 Conclusion 362 18 Debugging 363 Getting Started 363 Bugs in the Kernel 364 Debugging by Printing 364 Robustness 365 Loglevels 365 The Log Buffer 366 syslogd and klogd 367 Transposing printf() and printk() 367 Oops 367 ksymoops 369 kallsyms 369 Kernel Debugging Options 370 Asserting Bugs and Dumping Information 370 Magic SysRq Key 371 The Saga of a Kernel Debugger 372 gdb 372 kgdb 373 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 www.it-ebooks.info
ContentsxixBinary Searching to Find the Culprit Change376BinarySearchingwithGit376WhenAll ElseFails:TheCommunity377Conclusion:37819Portabillity379PortableOperatingSystems379Historyof Portability in Linux380Word Sizeand Data Typess381OpaqueTypes384Special Types384385ExplicitlySizedTypesSignedness of Chars386Data Alignment386387AvoidingAlignmentIssuesAlignmentofNonstandardTypes387StructurePadding387ByteOrder389Time391PageSize391ProcessorOrdering 392SMPKernelPreemption,andHighMemory393Conclusion39339520Patches,Hacking,andthe CommunityTheCommunity395Linux Coding Style396Indention396SwitchStatements6396Spacing397398BracesLine Length399Naming400Functions400400Comments401TypedefsUse Existing Routines402www.it-ebooks.info
ptg Contents xix Binary Searching to Find the Culprit Change 376 Binary Searching with Git 376 When All Else Fails: The Community 377 Conclusion 378 19 Portability 379 Portable Operating Systems 379 History of Portability in Linux 380 Word Size and Data Types 381 Opaque Types 384 Special Types 384 Explicitly Sized Types 385 Signedness of Chars 386 Data Alignment 386 Avoiding Alignment Issues 387 Alignment of Nonstandard Types 387 Structure Padding 387 Byte Order 389 Time 391 Page Size 391 Processor Ordering 392 SMP, Kernel Preemption, and High Memory 393 Conclusion 393 20 Patches, Hacking, and the Community 395 The Community 395 Linux Coding Style 396 Indention 396 Switch Statements 396 Spacing 397 Braces 398 Line Length 399 Naming 400 Functions 400 Comments 400 Typedefs 401 Use Existing Routines 402 www.it-ebooks.info