📝 Slab allocation in Kernel Memory (15 MCQs)
Object-Based Allocation for Frequently Used Kernel Structures
Slab allocation is a kernel-memory management technique that allocates memory for frequently used kernel objects through caches of preallocated object slots. Each cache is associated with a particular object type, such as process descriptors or file-system structures. Slabs contain multiple objects and obtain their underlying pages from a lower-level allocator. This approach reduces repeated allocation overhead and improves reuse of common kernel structures.
Why Repeated Small Kernel Allocations Need Special Handling
Kernel subsystems repeatedly create and destroy objects of predictable sizes. General-purpose allocation can waste memory, increase fragmentation, and require repeated initialisation. Slab allocation addresses these costs by keeping object caches ready for reuse. It can also preserve initialised object state where appropriate, reducing repeated setup operations when objects of the same type are allocated again.
Creating Caches and Reusing Object Slots
The kernel creates an object cache with a defined object size and alignment. The allocator obtains memory pages, divides them into object slots, and groups the resulting slabs according to availability. Allocation returns a free object slot, while deallocation marks it reusable. Some implementations use constructors, per-CPU caches, and separate lists for full, partial, and empty slabs.
Allocating Process Descriptor Objects
Suppose a kernel cache manages process descriptor objects of 256 bytes each. A 4 KB slab page can contain 16 such objects before metadata and alignment overhead. When a new process requires a descriptor, the allocator returns an available slot rather than requesting a separate page. After process termination, the slot becomes reusable for another descriptor.
Reduced Fragmentation and Faster Object Reuse
Slab allocation improves performance for repeated fixed-size kernel objects and reduces fragmentation between objects of different types. Per-CPU caches can also lower locking and contention costs. However, partially occupied slabs may retain unused memory, and many object caches require metadata and management. Modern implementations vary in their allocation and reclamation strategies, including variants such as SLUB.
Common Errors About Slab-Based Allocation
'A slab cache stores only complete process images' is incorrect because caches contain fixed-size kernel objects. 'Every object receives a separate physical page' is incorrect because many objects share a slab. 'Slab allocation replaces every other kernel allocator' is incorrect because it commonly obtains pages from lower-level physical-memory allocation mechanisms.
Exam Focus: Slab Structure and Object Reuse
Explain the relationship between caches, slabs, pages, and individual object slots. Calculate how many objects fit in a slab before accounting for metadata and alignment. Distinguish empty, partial, and full slabs. Compare slab allocation with the buddy system and explain why the former is suitable for repeated small kernel-object allocations.
Key Terms of Slab Allocation
- Slab allocator - Reuses fixed-size kernel objects.
- Object cache - Holds objects of one type.
- Slab - Group of allocated object slots.
- Constructor - Performs initial object setup.
- Per-CPU cache - Reduces processor allocation contention.
- SLUB - A slab allocator implementation.
📝 All Slab allocation in Kernel Memory MCQs
Q1. A process requests a new stack page and the OS employs zero-fill-on-demand. Which description best matches the resulting physical memory state?
📖 Explanation: Zero-fill-on-demand means the operating system reserves a free physical page and writes zeros to every byte before handing it to the process. This guarantees that no leftover data is exposed, protecting security and ensuring a clean starting state. Option B accurately reflects this behavior. Options A and C describe copy-on-write, which is different, and D suggests no initialization, which would be unsafe.
Q2. When a child process modifies a copy-on-write page, what happens to the parent’s version of that page?
📖 Explanation: Copy-on-write ensures that only the process that writes to a page receives a private copy. The parent’s page stays read‑only and unchanged, allowing it to be shared safely. Therefore option B is correct. Option A is wrong because the parent does not become dirty; option C is incorrect because the page is still needed; option D is false because merging does not occur until a later copy‑on‑write fault.
Q3. A system has a pool of five free pages. Two pages are allocated for expanding a heap, and one page is used for a copy‑on‑write fault. How many free pages remain?
📖 Explanation: The pool started with five pages. After allocating two for heap growth, three remain. Allocating one more for a copy‑on‑write fault reduces the count to two, leaving three free pages. Option B correctly reflects the remaining pages. Options A, C, and D miscount the allocations, either forgetting the copy‑on‑write page or subtracting the wrong number.
Q4. Which OS mechanism allows a child process to share executable code pages with its parent without copying them?
📖 Explanation: Executable code pages are typically marked read‑only, allowing both parent and child to map the same physical pages safely. This sharing avoids unnecessary copies and conserves memory. Option C captures this mechanism. Copy‑on‑write (A) copies pages only when they are written, not for read‑only code. Zero-fill-on-demand (B) creates new zeroed pages, not shared ones. vfork (D) is a fork variant but does not itself define the sharing method.
Q5. A developer notices that after forking a process, the child’s memory usage spikes unexpectedly. Which scenario most likely explains this behavior?
📖 Explanation: When a child writes to stack pages that were initially marked copy‑on‑write, each write causes the OS to allocate a private copy, increasing the child’s memory footprint. This matches option A. Executing a new program (B) would replace the address space, not increase usage. Modifying shared library code (C) is uncommon and would still be copy‑on‑write. Lazy allocation (D) affects page allocation, not copying after writes.
Q6. In a system that uses slab allocation for kernel objects, what advantage does allocating pages from a pre‑initialized slab provide when handling copy‑on‑write faults?
📖 Explanation: Slab allocation keeps caches of pre‑initialized objects, allowing the kernel to quickly satisfy allocation requests without costly initialization each time. When a copy‑on‑write fault occurs, the OS can pull a ready‑to‑use page from the slab, speeding up the copy process. Option C reflects this speed benefit. Reducing fragmentation (A) is a general benefit but not specific to copy‑on‑write. Zeroing pages (B) is handled by zero‑fill‑on‑demand, not slab caches. Cache locality (D) is a secondary effect, not the primary advantage in this context.
Q7. Consider two processes sharing a read‑only code page. If the OS mistakenly marks the page as writable, what risk does this introduce?
📖 Explanation: Marking a shared code page as writable allows either process to modify the page, which can cause one process’s changes to affect the other, leading to unexpected behavior or crashes—i.e., data corruption. Option B captures this risk. Increased latency (A) might occur but is not the primary danger. Higher CPU usage (C) and reduced disk I/O (D) are unrelated to the correctness issue.
Q8. A system administrator observes that many copy‑on‑write pages are being allocated from the free page pool during a burst of process creation. Which tuning action is most likely to reduce this overhead?
📖 Explanation: Enabling aggressive page reclamation frees up pages before a fork, so the OS can satisfy copy‑on‑write demands without waiting for new allocations, thus lowering overhead. Option B is correct. Increasing slab size (A) helps kernel objects but not copy‑on‑write pages. Disabling zero‑fill‑on‑demand (C) would keep stale data and is unsafe. Reducing process limits (D) merely limits workload, not the allocation efficiency.
Q9. When a copy‑on‑write page is duplicated, which of the following metadata must be updated in the page tables of both parent and child?
📖 Explanation: Duplicating a copy‑on‑write page requires the OS to adjust the reference count (to reflect two owners) and change protection bits from read‑only to read‑write for the writer. Therefore both the count and protection bits are updated, making option C correct. Updating only the frame number (A) ignores reference tracking. Changing only the reference count (B) would leave protection incorrect. Virtual address mapping (D) stays the same for shared pages.
Q10. A program allocates a large buffer using malloc, then forks. The child immediately writes to the buffer. Which memory‑management behavior explains the observed increase in the child’s RSS?
📖 Explanation: When the child writes to the newly allocated buffer, the pages that were shared copy‑on‑write are duplicated, causing the child’s resident set size (RSS) to grow. Option B captures this. Demand paging (A) concerns bringing pages into memory, not duplication. Swapping (C) moves pages to disk, not increase RSS. Lazy allocation (D) delays allocation but does not cause immediate RSS growth after a write.
Q11. Which statement best describes why zero‑fill‑on‑demand pages improve security in an OS that uses copy‑on‑write?
📖 Explanation: Zero‑fill‑on‑demand clears any leftover data from previous uses before a page is handed out, guaranteeing that no confidential information leaks to a new process. This security benefit is captured by option B. Preventing accidental overwrites (A) is a function of copy‑on‑write, not zero‑fill. Sharing without copying (C) is also a copy‑on‑write property. Speeding up creation (D) may be a side effect but is not the primary security reason.
Q12. During a high‑load benchmark, a system exhibits frequent copy‑on‑write faults on stack pages, leading to memory exhaustion. Which architectural change would most directly mitigate this problem?
📖 Explanation: Larger page sizes reduce the number of pages needed for a given memory region, so fewer copy‑on‑write faults occur for the same amount of stack growth, directly easing memory pressure. Option C is correct. Eager copying (A) would increase memory usage. Adding more physical memory (B) helps but does not change the fault frequency. Disabling paging (D) is impractical and unrelated.
Q13. A kernel developer proposes to store pre‑zeroed pages in a dedicated slab to serve copy‑on‑write requests faster. What potential drawback must the developer consider?
📖 Explanation: Maintaining a slab of pre‑zeroed pages adds overhead: the kernel must track allocation, deallocation, and periodic refilling, consuming CPU cycles. This cost is the main drawback, making option A correct. The pages are zeroed, so stale data risk (B) is low. Slabs can be designed for a single page size, so flexibility (C) is manageable. Fragmentation (D) is less of a concern because pages are uniform.
Q14. In a system that implements both copy‑on‑write and zero‑fill‑on‑demand, which sequence occurs when a forked child first writes to a previously unmodified page?
📖 Explanation: When a page has never been written, it is typically marked copy‑on‑write at fork time. The first write by the child triggers the copy‑on‑write mechanism, which allocates a new page (often from the zero‑fill‑on‑demand pool) and copies the original content. However, the logical step is the copy‑on‑write duplication; zero‑fill occurs as part of allocating the new page but is not a separate step in the sequence presented. Thus option D best matches the described behavior. Options A and B misorder the operations, and C ignores the duplication entirely.
Q15. Which of the following best explains why executable code pages are never marked copy‑on‑write during a fork?
📖 Explanation: Executable code pages contain instructions that never change at runtime, making them immutable. Because they are read‑only, both parent and child can map the same physical pages without risking unintended modifications, eliminating the need for copy‑on‑write. Option A captures this reasoning. While hardware may enforce read‑only (B), the primary reason is immutability. Lack of free pages (C) is unrelated, and copy‑on‑write is not limited to heap pages (D).