Leanpub Header

Skip to main content

Linux GPU Drivers from Scratch

Building Real DRM Drivers for the Modern Linux Kernel

Linux GPU Drivers from Scratch
This book is 100% completeLast updated on 2026-09-17

Go beyond writing kernel modules and learn how real Linux GPU drivers are built. Starting with GPU architecture and the graphics stack, this hands-on guide walks you through creating a complete DRM driver, from a minimal module to memory management, command submission, display support and error recovery.

Minimum price

$25.00

$35.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
209
Pages
About

About

About the Book

This book takes you from the fundamentals of GPU architecture and the Linux graphics stack all the way to designing, implementing, and debugging a complete Direct Rendering Manager driver for the Linux kernel. If you can write C and understand the basics of kernel modules, this book will teach you how to bridge the gap between graphics hardware and userspace applications through production-quality Linux kernel code. Every chapter builds on the previous one, progressing from a minimal loadable module to a fully functional DRM driver with memory management, command submission, synchronization, display support, and error recovery. Real code, real build instructions, no shortcuts.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 420 engineers and researchers. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

Building Real DRM Drivers for the Modern Linux Kernel

Chapter 1: Before We Begin

  1. Understanding What a GPU Driver Is
  2. Prerequisites and Assumptions
  3. Choosing a Kernel Version
  4. Setting Up the Build Environment
  5. Testing in a Virtual Environment
  6. The Tools You Will Use
  7. How to Read This Book

Chapter 2: GPU Architecture Fundamentals

  1. From CPU to GPU: Why Graphics Need Special Hardware
  2. The GPU Command Processor
  3. Ring Buffers and Submission Queues
  4. GPU Memory Hierarchy
  5. Execution Units
  6. Display Engines
  7. The Role of the Driver in the Control Chain
  8. Hardware Register Interfaces
  9. What Comes Next

Chapter 3: The Linux Graphics Stack

  1. A Brief History: From Framebuffer to DRM
  2. The Modern Stack: Layer by Layer
  3. The Complete Path: A Drawing Call End to End
  4. Userspace vs Kernel Responsibilities
  5. Render Nodes vs Card Nodes
  6. Mesa and the Kernel: A Partnership
  7. What Comes Next

Chapter 4: Direct Rendering Manager

  1. What DRM Is and Why It Exists
  2. The drm_device Structure
  3. The drm_driver Structure
  4. Device Registration Flow
  5. Device Management with devm
  6. DRM File Operations
  7. DRM Minor Devices
  8. DRM Capabilities
  9. Debugging Support
  10. What Comes Next

Chapter 5: Kernel Mode Setting

  1. From UMS to KMS
  2. Display Pipeline Objects
  3. How Objects Are Connected
  4. Display Modes
  5. EDID and DDC
  6. Atomic Mode Setting
  7. The ww_mutex Locking Scheme
  8. Vblank and Timing
  9. Standard Properties
  10. What Comes Next

Chapter 6: Graphics Execution Manager

  1. What GEM Is and What It Solves
  2. The drm_gem_object Structure
  3. GEM Object Operations
  4. GEM Initialization
  5. Creating GEM Objects
  6. GEM Handles
  7. GEM mmap
  8. GEM Prime and Buffer Sharing
  9. GEM CMA Helper
  10. What Comes Next

Chapter 7: Translation Table Maps

  1. What TTM Is and Why It Exists
  2. TTM Memory Types
  3. The ttm_bo Object
  4. TTM Driver Interface
  5. TTM Initialization
  6. TTM and GEM Integration
  7. When to Use TTM vs GEM
  8. What Comes Next

Chapter 8: PCI Integration and Device Discovery

  1. PCI Enumeration: How Devices Are Found
  2. The PCI Device Driver Model
  3. PCI Configuration Space
  4. PCI BARs: Memory-Mapped Registers
  5. DMA and DMA Masks
  6. PCI Interrupts: MSI and MSI-X
  7. Device Tree for Embedded GPUs
  8. What Comes Next

Chapter 9: Building the Minimal DRM Driver

  1. Directory Structure and Kernel Module Layout
  2. Building the Driver
  3. Loading and Verifying
  4. Unloading the Module
  5. Troubleshooting
  6. Summary

Chapter 10: Device Registration and Driver Lifecycle

  1. Module Init and Exit
  2. Probe: Discovering and Initializing Hardware
  3. Probe Error Handling
  4. Remove: Cleaning Up
  5. Runtime Power Management
  6. System Sleep: Suspend and Resume
  7. Debugging Registration Failures
  8. Summary

Chapter 11: GPU Memory Management

  1. Allocating GEM Objects from Kernel Space
  2. DMA Allocation: Coherent vs Streaming
  3. Userspace GEM Handles and Ioctls
  4. Mapping GEM Objects
  5. GEM Object Lifecycle and Reference Counting
  6. Mapping GEM Objects into GPU Address Space
  7. Implementing a Minimal GEM Memory Manager
  8. Testing the Memory Manager
  9. Summary

Chapter 12: Command Submission

  1. Command Streams: Structure and Semantics
  2. Ring Buffers and Circular Queues
  3. The DRM Command Submission Ioctl
  4. Parsing and Validating Command Buffers
  5. Hardware Submission: Writing to GPU Registers
  6. Error Paths: Invalid Commands and Buffer Overruns
  7. Summary

Chapter 13: GPU Virtual Memory

  1. Why GPUs Use Virtual Memory
  2. GPU Address Spaces and Contexts
  3. Page Table Structures
  4. The DRM GPUVA Manager
  5. Mapping and Unmapping Buffers
  6. Handling GPU Page Faults
  7. Summary

Chapter 14: Synchronization and Fences

  1. Why Synchronization Is Harder with GPUs
  2. DRM Fences: The Fundamental Synchronization Object
  3. Software Fences vs Hardware Fences
  4. Fence Contexts and Timelines
  5. DMA Fences and Cross-Device Synchronization
  6. Implementing Fence Signaling from Interrupts
  7. Userspace Fence Waiting
  8. Summary

Chapter 15: Interrupts and GPU Completion

  1. GPU Interrupts: Types and Sources
  2. MSI vs Legacy INTx Interrupts
  3. Requesting and Sharing IRQs
  4. The Interrupt Handler Structure
  5. Signaling Fences and Wakeups from Interrupt Context
  6. Debugging Interrupt Issues
  7. Summary

Chapter 16: Display Pipeline and Framebuffers

  1. Framebuffer Registration and Pixel Formats
  2. Implementing CRTC Enable/Disable and Mode Set
  3. Plane Composition and Layering
  4. Connector Detection and EDID Reading
  5. Atomic Commit for the Display Pipeline
  6. Scanout and Vblank Timing
  7. Summary

Chapter 17: The Userspace Interface

  1. DRM Ioctls: Standard vs Driver-Private
  2. Designing a Clean Ioctl Interface
  3. Versioning and Backward Compatibility
  4. The DRM File Private Data Structure
  5. Userspace Workflow: Open, Get Capabilities, Allocate, Submit, Wait
  6. libdrm Integration and uAPI Stability
  7. Mesa Driver Hooks: Gallium, VK, and Driver Layers
  8. Summary

Chapter 18: Power Management

  1. Runtime Power Management: Suspend and Resume
  2. System Sleep: Freeze, Thaw, Poweroff, Restore
  3. GPU Power States and DVFS
  4. Clock Gating and Power Gating
  5. Idle Detection and Autosuspend
  6. Resuming GPU State After Sleep
  7. Power Debugging and Measurement
  8. Summary

Chapter 19: Debugging GPU Drivers

  1. Kernel Log Analysis: dmesg, printk Levels
  2. Dynamic Debug: Enabling Per-File Logging
  3. ftrace and Function Graph Tracing
  4. DRM-Specific Debugfs Entries
  5. DRM Tracepoints for GPU Operations
  6. Using QEMU and Virtual Hardware for Testing
  7. GDB with Kernel Modules
  8. Crash Dumps and Stack Traces
  9. Summary

Chapter 20: Concurrency and Safety

  1. Locking Primitives: Mutex, Spinlock, Rwlock
  2. DRM’s Reservation Locking for Shared Objects
  3. Lock Ordering and Deadlocks
  4. Reference Counting: Kref, Drm_Ref, Custom
  5. Memory Ordering: Barriers and Ordering Guarantees
  6. Race Conditions Specific to GPU Drivers
  7. RCU for Read-Mostly Data Structures
  8. Summary

Chapter 21: GPU Hangs and Error Recovery

  1. What a GPU Hang Is and What Causes It
  2. Detecting Hangs: Watchdogs and Timeouts
  3. The DRM Reset Infrastructure
  4. Reset Sequence: Stopping Submission, Resetting, Resuming
  5. Recovering GPU State: Contexts, Page Tables, Buffers
  6. Handling Stale Fences and Waiting Processes
  7. GPU Reset and Userspace Notification
  8. Fault Injection for Testing
  9. Summary

Chapter 22: Performance Optimization

  1. Profiling GPU Drivers: Where Time Is Spent
  2. Reducing CPU Overhead in the Fast Path
  3. Batching and Coalescing Command Submissions
  4. Prefetching and Cache-Friendly Data Structures
  5. Avoiding Page Faults in GPU Memory
  6. Interrupt Coalescing and Rate Limiting
  7. Measuring and Comparing Performance
  8. Summary

Chapter 23: Security Considerations

  1. The GPU Driver as an Attack Surface
  2. IOMMU and DMA Protection
  3. Command Stream Validation
  4. GPU Sandboxing and VM Isolation
  5. Privilege Escalation via the Driver
  6. Information Disclosure through GPU Memory
  7. Secure Boot and Signed Drivers
  8. Best Practices for Security-Hardened Drivers
  9. Summary

Chapter 24: Reading Upstream DRM Drivers

  1. Choosing Drivers to Study: Simple vs Complex Examples
  2. Anatomy of a Modern DRM Driver: File Layout
  3. Understanding DRM Core Abstractions in Practice
  4. How to Read and Understand Complex Initialization Sequences
  5. Identifying Patterns: Common Idioms Across Drivers
  6. Using Coccinelle and Other Tools to Explore Code
  7. Summary

Chapter 25: Upstream Development

  1. The Linux Graphics Mailing Lists and Maintainers
  2. Finding a Maintainer for Your Driver or Subsystem
  3. Patch Series Structure and Cover Letters
  4. Writing Commit Messages for Graphics Drivers
  5. Code Review Expectations and Common Feedback
  6. The Review Cycle: What to Expect
  7. Handling Objections and Iteration
  8. Tools: B4, Git, Patchwork
  9. Summary

Chapter 26: Conclusion

  1. The Complete Driver: From Nothing to Functional
  2. What We Learned and Why It Matters
  3. Emerging Trends: Vulkan Drivers, GPU Compute, Async Scheduling
  4. Continuing to Learn: Where to Go Next
  5. Final Encouragement

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub