logo资料库

Linux Kernel Development(Third Edition).pdf

第1页 / 共466页
第2页 / 共466页
第3页 / 共466页
第4页 / 共466页
第5页 / 共466页
第6页 / 共466页
第7页 / 共466页
第8页 / 共466页
资料共466页,剩余部分请下载后查看
Table of Contents
1 Introduction to the Linux Kernel
History of Unix
Along Came Linus: Introduction to Linux
Overview of Operating Systems and Kernels
Linux Versus Classic Unix Kernels
Linux Kernel Versions
The Linux Kernel Development Community
Before We Begin
2 Getting Started with the Kernel
Obtaining the Kernel Source
Using Git
Installing the Kernel Source
Using Patches
The Kernel Source Tree
Building the Kernel
Configuring the Kernel
Minimizing Build Noise
Spawning Multiple Build Jobs
Installing the New Kernel
A Beast of a Different Nature
No libc or Standard Headers
GNU C
No Memory Protection
No (Easy) Use of Floating Point
Small, Fixed-Size Stack
Synchronization and Concurrency
Importance of Portability
Conclusion
3 Process Management
The Process
Process Descriptor and the Task Structure
Allocating the Process Descriptor
Storing the Process Descriptor
Process State
Manipulating the Current Process State
Process Context
The Process Family Tree
Process Creation
Copy-on-Write
Forking
vfork()
The Linux Implementation of Threads
Creating Threads
Kernel Threads
Process Termination
Removing the Process Descriptor
The Dilemma of the Parentless Task
Conclusion
4 Process Scheduling
Multitasking
Linux’s Process Scheduler
Policy
I/O-Bound Versus Processor-Bound Processes
Process Priority
Timeslice
The Scheduling Policy in Action
The Linux Scheduling Algorithm
Scheduler Classes
Process Scheduling in Unix Systems
Fair Scheduling
The Linux Scheduling Implementation
Time Accounting
Process Selection
The Scheduler Entry Point
Sleeping and Waking Up
Preemption and Context Switching
User Preemption
Kernel Preemption
Real-Time Scheduling Policies
Scheduler-Related System Calls
Scheduling Policy and Priority-Related System Calls
Processor Affinity System Calls
Yielding Processor Time
Conclusion
5 System Calls
Communicating with the Kernel
APIs, POSIX, and the C Library
Syscalls
System Call Numbers
System Call Performance
System Call Handler
Denoting the Correct System Call
Parameter Passing
System Call Implementation
Implementing System Calls
Verifying the Parameters
System Call Context
Final Steps in Binding a System Call
Accessing the System Call from User-Space
Why Not to Implement a System Call
Conclusion
6 Kernel Data Structures
Linked Lists
Singly and Doubly Linked Lists
Circular Linked Lists
Moving Through a Linked List
The Linux Kernel’s Implementation
Manipulating Linked Lists
Traversing Linked Lists
Queues
kfifo
Creating a Queue
Enqueuing Data
Dequeuing Data
Obtaining the Size of a Queue
Resetting and Destroying the Queue
Example Queue Usage
Maps
Initializing an idr
Allocating a New UID
Looking Up a UID
Removing a UID
Destroying an idr
Binary Trees
Binary Search Trees
Self-Balancing Binary Search Trees
What Data Structure to Use, When
Algorithmic Complexity
Algorithms
Big-O Notation
Big Theta Notation
Time Complexity
Conclusion
7 Interrupts and Interrupt Handlers
Interrupts
Interrupt Handlers
Top Halves Versus Bottom Halves
Registering an Interrupt Handler
Interrupt Handler Flags
An Interrupt Example
Freeing an Interrupt Handler
Writing an Interrupt Handler
Shared Handlers
A Real-Life Interrupt Handler
Interrupt Context
Implementing Interrupt Handlers
/proc/interrupts
Interrupt Control
Disabling and Enabling Interrupts
Disabling a Specific Interrupt Line
Status of the Interrupt System
Conclusion
8 Bottom Halves and Deferring Work
Bottom Halves
Why Bottom Halves?
A World of Bottom Halves
Softirqs
Implementing Softirqs
Using Softirqs
Tasklets
Implementing Tasklets
Using Tasklets
ksoftirqd
The Old BH Mechanism
Work Queues
Implementing Work Queues
Using Work Queues
The Old Task Queue Mechanism
Which Bottom Half Should I Use?
Locking Between the Bottom Halves
Disabling Bottom Halves
Conclusion
9 An Introduction to Kernel Synchronization
Critical Regions and Race Conditions
Why Do We Need Protection?
The Single Variable
Locking
Causes of Concurrency
Knowing What to Protect
Deadlocks
Contention and Scalability
Conclusion
10 Kernel Synchronization Methods
Atomic Operations
Atomic Integer Operations
64-Bit Atomic Operations
Atomic Bitwise Operations
Spin Locks
Spin Lock Methods
Other Spin Lock Methods
Spin Locks and Bottom Halves
Reader-Writer Spin Locks
Semaphores
Counting and Binary Semaphores
Creating and Initializing Semaphores
Using Semaphores
Reader-Writer Semaphores
Mutexes
Semaphores Versus Mutexes
Spin Locks Versus Mutexes
Completion Variables
BKL: The Big Kernel Lock
Sequential Locks
Preemption Disabling
Ordering and Barriers
Conclusion
11 Timers and Time Management
Kernel Notion of Time
The Tick Rate: HZ
The Ideal HZ Value
Advantages with a Larger HZ
Disadvantages with a Larger HZ
Jiffies
Internal Representation of Jiffies
Jiffies Wraparound
User-Space and HZ
Hardware Clocks and Timers
Real-Time Clock
System Timer
The Timer Interrupt Handler
The Time of Day
Timers
Using Timers
Timer Race Conditions
Timer Implementation
Delaying Execution
Busy Looping
Small Delays
schedule_timeout()
Conclusion
12 Memory Management
Pages
Zones
Getting Pages
Getting Zeroed Pages
Freeing Pages
kmalloc()
gfp_mask Flags
kfree()
vmalloc()
Slab Layer
Design of the Slab Layer
Slab Allocator Interface
Statically Allocating on the Stack
Single-Page Kernel Stacks
Playing Fair on the Stack
High Memory Mappings
Permanent Mappings
Temporary Mappings
Per-CPU Allocations
The New percpu Interface
Per-CPU Data at Compile-Time
Per-CPU Data at Runtime
Reasons for Using Per-CPU Data
Picking an Allocation Method
Conclusion
13 The Virtual Filesystem
Common Filesystem Interface
Filesystem Abstraction Layer
Unix Filesystems
VFS Objects and Their Data Structures
The Superblock Object
Superblock Operations
The Inode Object
Inode Operations
The Dentry Object
Dentry State
The Dentry Cache
Dentry Operations
The File Object
File Operations
Data Structures Associated with Filesystems
Data Structures Associated with a Process
Conclusion
14 The Block I/O Layer
Anatomy of a Block Device
Buffers and Buffer Heads
The bio Structure
I/O vectors
The Old Versus the New
Request Queues
I/O Schedulers
The Job of an I/O Scheduler
The Linus Elevator
The Deadline I/O Scheduler
The Anticipatory I/O Scheduler
The Complete Fair Queuing I/O Scheduler
The Noop I/O Scheduler
I/O Scheduler Selection
Conclusion
15 The Process Address Space
Address Spaces
The Memory Descriptor
Allocating a Memory Descriptor
Destroying a Memory Descriptor
The mm_struct and Kernel Threads
Virtual Memory Areas
VMA Flags
VMA Operations
Lists and Trees of Memory Areas
Memory Areas in Real Life
Manipulating Memory Areas
find_vma()
find_vma_prev()
find_vma_intersection()
mmap() and do_mmap(): Creating an Address Interval
munmap() and do_munmap(): Removing an Address Interval
Page Tables
Conclusion
16 The Page Cache and Page Writeback
Approaches to Caching
Write Caching
Cache Eviction
The Linux Page Cache
The address_space Object
address_space Operations
Radix Tree
The Old Page Hash Table
The Buffer Cache
The Flusher Threads
Laptop Mode
History: bdflush, kupdated, and pdflush
Avoiding Congestion with Multiple Threads
Conclusion
17 Devices and Modules
Device Types
Modules
Hello, World!
Building Modules
Installing Modules
Generating Module Dependencies
Loading Modules
Managing Configuration Options
Module Parameters
Exported Symbols
The Device Model
Kobjects
Ktypes
Ksets
Interrelation of Kobjects, Ktypes, and Ksets
Managing and Manipulating Kobjects
Reference Counts
sysfs
Adding and Removing kobjects from sysfs
Adding Files to sysfs
The Kernel Events Layer
Conclusion
18 Debugging
Getting Started
Bugs in the Kernel
Debugging by Printing
Robustness
Loglevels
The Log Buffer
syslogd and klogd
Transposing printf() and printk()
Oops
ksymoops
kallsyms
Kernel Debugging Options
Asserting Bugs and Dumping Information
Magic SysRq Key
The Saga of a Kernel Debugger
gdb
kgdb
Poking and Probing the System
Using UID as a Conditional
Using Condition Variables
Using Statistics
Rate and Occurrence Limiting Your Debugging
Binary Searching to Find the Culprit Change
Binary Searching with Git
When All Else Fails: The Community
Conclusion
19 Portability
Portable Operating Systems
History of Portability in Linux
Word Size and Data Types
Opaque Types
Special Types
Explicitly Sized Types
Signedness of Chars
Data Alignment
Avoiding Alignment Issues
Alignment of Nonstandard Types
Structure Padding
Byte Order
Time
Page Size
Processor Ordering
SMP, Kernel Preemption, and High Memory
Conclusion
20 Patches, Hacking, and the Community
The Community
Linux Coding Style
Indention
Switch Statements
Spacing
Braces
Line Length
Naming
Functions
Comments
Typedefs
Use Existing Routines
Minimize ifdefs in the Source
Structure Initializers
Fixing Up Code Ex Post Facto
Chain of Command
Submitting Bug Reports
Patches
Generating Patches
Generating Patches with Git
Submitting Patches
Conclusion
Bibliography
Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W-X-Y
Z
Linux Kernel Development Third Edition From the Library of Wow! eBook
Developer’s Library ESSENTIAL REFERENCES FOR PROGRAMMING PROFESSIONALS Developer’s Library books are designed to provide practicing programmers with unique, high-quality references and tutorials on the programming languages and technologies they use in their daily work. All books in the Developer’s Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way that’s useful for other programmers. Key titles include some of the best, most widely acclaimed books within their topic areas: PHP & MySQL Web Development Luke Welling & Laura Thomson ISBN 978-0-672-32916-6 MySQL Paul DuBois ISBN-13: 978-0-672-32938-8 Linux Kernel Development Robert Love ISBN-13: 978-0-672-32946-3 Python Essential Reference David Beazley ISBN-13: 978-0-672-32978-6 Programming in Objective-C 2.0 Stephen G. Kochan ISBN-13: 978-0-321-56615-7 PostgreSQL Korry Douglas ISBN-13: 978-0-672-33015-5 Developer’s Library books are available at most retail and online bookstores, as well as by subscription from Safari Books Online at safari.informit.com Developer’s Library informit.com/devlibrary From the Library of Wow! eBook
Linux Kernel Development Third Edition Robert Love Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City From the Library of Wow! eBook
Acquisitions Editor Mark Taber Development Editor Michael Thurston Technical Editor Robert P. J. Day Managing Editor Sandra Schroeder Senior Project Editor Tonya Simpson Copy Editor Apostrophe Editing Services Indexer Brad Herriman Proofreader Debbie Williams Publishing Coordinator Vanessa Evans Book Designer Gary Adair Compositor Mark Shirar Linux Kernel Development Third Edition Copyright © 2010 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited repro- duction, storage in a retrieval system, or transmission in any form or by any means, elec- tronic, mechanical, photocopying, recording, or likewise. ISBN-13: 978-0-672-32946-3 ISBN-10: 0-672-32946-8 Library of Congress Cataloging-in-Publication Data: Love, Robert. Linux kernel development / Robert Love. — 3rd ed. p. cm. Includes bibliographical references and index. ISBN 978-0-672-32946-3 (pbk. : alk. paper) 1. Linux. 2. Operating systems (Computers) I. Title. QA76.76.O63L674 2010 005.4’32—dc22 2010018961 Text printed in the United States on recycled paper at RR Donnelley, Crawfordsville, Indiana. First printing June 2010 Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publish- er was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omis- sions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk pur- chases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States please contact: International Sales international@pearson.com Visit us on the Web: informit.com/aw From the Library of Wow! eBook
❖ For Doris and Helen. ❖ From the Library of Wow! eBook ptg
Contents at a Glance 1 Introduction to the Linux Kernel 1 2 Getting Started with the Kernel 11 3 Process Management 23 4 Process Scheduling 41 5 System Calls 69 6 Kernel Data Structures 85 7 Interrupts and Interrupt Handlers 113 8 Bottom Halves and Deferring Work 133 9 An Introduction to Kernel Synchronization 161 10 Kernel Synchronization Methods 175 11 Timers and Time Management 207 12 Memory Management 231 13 The Virtual Filesystem 261 14 The Block I/O Layer 289 15 The Process Address Space 305 16 The Page Cache and Page Writeback 323 17 Devices and Modules 337 18 Debugging 363 19 Portability 379 20 Patches, Hacking, and the Community 395 Bibliography 407 Index 411 From the Library of Wow! eBook ptg
Table of Contents 1 Introduction to the Linux Kernel 1 History of Unix 1 Along Came Linus: Introduction to Linux 3 Overview of Operating Systems and Kernels 4 Linux Versus Classic Unix Kernels 6 Linux Kernel Versions 8 The Linux Kernel Development Community 10 Before We Begin 10 2 Getting Started with the Kernel 11 Obtaining the Kernel Source 11 Using Git 11 Installing the Kernel Source 12 Using Patches 12 The Kernel Source Tree 12 Building the Kernel 13 Configuring the Kernel 14 Minimizing Build Noise 15 Spawning Multiple Build Jobs 16 Installing the New Kernel 16 A Beast of a Different Nature 16 No libc or Standard Headers 17 GNU C 18 Inline Functions 18 Inline Assembly 19 Branch Annotation 19 No Memory Protection 20 No (Easy) Use of Floating Point 20 Small, Fixed-Size Stack 20 Synchronization and Concurrency 21 Importance of Portability 21 Conclusion 21 From the Library of Wow! eBook ptg
viii Contents 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 Forking 32 vfork() 33 The Linux Implementation of Threads 33 Creating Threads 34 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 I/O-Bound Versus Processor-Bound Processes 43 Process Priority 44 Timeslice 45 The Scheduling Policy in Action 45 The Linux Scheduling Algorithm 46 Scheduler Classes 46 Process Scheduling in Unix Systems 47 Fair Scheduling 48 The Linux Scheduling Implementation 50 Time Accounting 50 The Scheduler Entity Structure 50 The Virtual Runtime 51 From the Library of Wow! eBook ptg
分享到:
收藏