Writing an OS Kernel from Scratch – Part 19: User and Kernel Address Space — Building Secure Virtual Memory Boundaries

dean
Writing an OS Kernel from Scratch – Part 19: User and Kernel Address Space — Building Secure Virtual Memory Boundaries “All processes share kernel code, but must never peek at each other; user programs roam freely but cannot cross the boundary. Today, we design the higher-half kernel mapping, achieving perfect isolation and efficient sharing of... » read more

Long Article 103: Memory Management – Kernel: In-Depth Slab Allocator Design — Compared with Linux

dean
Long Article 103: Memory Management – Kernel: In-Depth Slab Allocator Design — Compared with Linux “The Buddy system excels at allocating large chunks, but what about the kernel’s frequent 32-byte task_struct allocations? This article dives into the Slab allocator’s design philosophy, compares Linux’s three implementations (SLAB/SLUB/SLOB), and builds a high-performance, low-fragmentation industrial-grade Slab system.” The... » read more

Long Article 102: Memory Management – Physical: In-Depth Buddy System Analysis — From Theory to Industrial Implementation

dean
Long Article 102: Memory Management – Physical: In-Depth Buddy System Analysis — From Theory to Industrial Implementation “The Buddy allocator is far more than just ‘split and merge.’ This article dives into Linux kernel source-level implementation, systematically analyzing watermark management, migration types, per-CPU page frame caches, compound pages, and other core mechanisms, providing a runnable... » read more

Writing an OS Kernel from Scratch – Part 24: Display Server and Client — Building a Shared Memory Graphics Architecture

dean
Writing an OS Kernel from Scratch – Part 24: Display Server and Client — Building a Shared Memory Graphics Architecture “Framebuffer lets you draw pixels, but how do multiple applications share the screen? Today, we implement a Display Server + Client architecture, achieving efficient graphics compositing via shared memory!” In the previous post, we implemented... » read more

A05 — Use-After-Free: The Problem of Accessing Freed Memory

dean
A05 — Use-After-Free: The Problem of Accessing Freed Memory Key insight: Continuing to use a freed memory block is like continuing to build a house on land you’ve already sold — sooner or later someone will come to tear it down. What is Use-After-Free Use-After-Free (UAF) refers to accessing memory that has already been freed... » read more

Long Article 104: Memory Management – Virtual Part I: In-Depth Page Table Operations and Virtual Memory Areas (VMA)

dean
Long Article 104: Memory Management – Virtual Part I: In-Depth Page Table Operations and Virtual Memory Areas (VMA) Analysis “Virtual memory is the cornerstone of modern operating systems, and VMA is the ‘map’ of the process address space. This article dives deep into x86 page table structures and VMA design principles, comparing Linux implementation details... » read more

A01 — The Internals of malloc: How brk and mmap Work Together

dean
A01 — The Internals of malloc: How brk and mmap Work Together Key Insight: Every block of memory you allocate with malloc is backed by the system making allocation decisions on your behalf. malloc is Not a System Call malloc is a glibc (ptmalloc) library function, not a system call. Two paths for malloc: 1.... » read more

Long Article 106: Memory Management — Advanced: Deep Dive into Copy-on-Write, Swapping, and NUMA Optimization

dean
# Long Article 106: Memory Management — Advanced: Deep Dive into Copy-on-Write, Swapping, and NUMA Optimization “When physical memory is insufficient, how does the OS handle it gracefully? This article covers CoW, Swapping, and NUMA optimization.” Introduction Three advanced features for memory pressure: CoW: Share pages on fork, copy only on write Swapping: Move inactive... » read more

A10 — Memory Analysis Tools: malloc_stats, Valgrind Massif, pmap

dean
A10 — Memory Analysis Tools: malloc_stats, Valgrind Massif, pmap Key Insight: The first step to optimizing memory is knowing where it is being used. malloc_stats: glibc Allocation Statistics malloc_stats() 输出所有 arena 的统计信息: #include <malloc.h> malloc_stats(); 输出示例: Arena 0: system bytes = 1048576 memory bytes = 1048576 96 malloc-chunks in use (12345 bytes) Arena 1: system... » read more

A09 — Memory Barriers and Cache Coherence: Out-of-Order Access on Multi-Core

dean
A09 — Memory Barriers and Cache Coherence: Out-of-Order Access on Multi-Core Key Insight: In a multi-core CPU, the memory order you observe is not necessarily the true order. Out-of-Order Memory Access on Multi-Core CPUs Modern CPUs reorder memory access instructions for performance optimization. In multi-core programs, this reordering can cause serious problems. 内存访问乱序的例子: 线程 A:... » read more

A02 — Internal Structure of the Heap: Arena, Bin, and Chunk

dean
A02 — Internal Structure of the Heap: Arena, Bin, and Chunk Key Insight: The heap managed by malloc is a precision system of bins and chunks. Structure of malloc_chunk malloc 把内存分成 chunk(块)来管理: struct malloc_chunk { INTERNAL_SIZE_T prev_size; // 前一个 chunk 的大小(如果前一个空闲) INTERNAL_SIZE_T size; // 本 chunk 大小 + 标志位 malloc_chunk *fd; // 同 bin 的下一个... » read more

A06 — new/delete vs malloc/free: Differences in C++ Memory Management

dean
# A06 — new/delete 和 malloc/free: C++ memory management的Difference **金句**: new 不是 malloc, delete 不是 free — 它们不只是allocate, 还有构造和析构. — ## new 和 malloc 的Essential Difference “` new 和 malloc 的区别: malloc: – C 库函数 – 只分配内存,不初始化 – 返回 void* – 失败返回 NULL new: – C++ 关键字 – 分配内存 + 调用构造函数 – 返回具体类型的指针(不需要类型转换) – 失败抛出... » read more

Writing an OS Kernel from Scratch – Part 4: Entering Protected Mode (Detailed Segmented Memory Management)

dean
# Writing an OS Kernel from Scratch – Part 4: Entering Protected Mode (Detailed Segmented Memory Management) “Protected mode ≠ Paging! Its first line of defense is the segmentation mechanism that modern OSes ‘quietly bypass.'” In the previous few articles, we used GRUB to easily boot the kernel, output “Hello World,” and understood the Multiboot... » read more

Writing an OS Kernel from Scratch – Part 5: Enabling Paging — Building the Foundation of Virtual Memory

dean
# Writing an OS Kernel from Scratch – Part 5: Enabling Paging — Building the Foundation of Virtual Memory “Segmentation is just a transition; paging is the true moat of modern operating systems. Today, we manually enable x86 paging, giving the kernel virtual memory!” In the previous article, we deeply understood protected mode and segmented... » read more

A04 — Memory Leaks: How malloc Without free Causes Leaks

dean
A04 — Memory Leaks: How malloc Without free Causes Leaks Key Insight: After the program ends, the system reclaims the memory. But during execution, leaked memory is truly leaked. What is a Memory Leak A memory leak occurs when a program dynamically allocates memory and fails to free it, making that memory permanently unavailable for... » read more

Writing an OS Kernel from Scratch — Part 16: VMA and Process Exit — Safely Reclaiming Every Byte of Memory

dean
从零写 OS kernel-Part 16: VMA 与process退出 — 安全reclaim每一片内存 “Allocating memory is just the beginning — safe release is the true endpoint. Today, we implement Virtual Memory Area (VMA) management, ensuring every resource is reclaimed without leaks when a process exits!” 在前几篇中, 我们Implementation了 Buddy 系统, Slab allocate器, 用户态 malloc, 但process的virtual address空间仍是一团乱麻: 无法跟踪哪些Region已allocate process退出时, 物理页, file descriptor,... » read more

A07 — Memory Pool: Pre-allocate, On-demand Allocation, Avoiding Fragmentation

dean
A07 — Memory Pool: Pre-allocate, On-demand Allocation, Avoiding Fragmentation Key Insight: In scenarios with frequent allocation/deallocation, pre-allocating a pool is 100x faster than asking the system each time. What is a Memory Pool A Memory Pool pre-allocates a large block of memory and allocates from it on demand. Compared to calling malloc each time, memory... » read more

05: Memory Management – User Mode: From brk to malloc — Deep Dive into User-Space Memory Allocators

dean
# 05: Memory Management – User Mode: From brk to malloc — Deep Dive into User-Space Memory Allocators “When a user program calls malloc, how do the kernel and libc work together? This article delves into the brk/mmap system calls, the dlmalloc algorithm, and compares glibc’s implementation details, building an efficient, low-fragmentation user-space memory allocator.”... » read more