Operating Systems. 11. Memory Management Part 3 Kernel Memory Allocation. Paul Krzyzanowski Rutgers University Spring 2015

Similar documents
Memory Allocators. Pradipta De

Kernel Memory Management

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

The Art and Science of Memory Allocation

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Operating Systems. Week 9 Recitation: Exam 2 Preview Review of Exam 2, Spring Paul Krzyzanowski. Rutgers University.

Processes, Address Spaces, and Memory Management. CS449 Spring 2016

Final Exam Preparation Questions

Chapter 10: Case Studies. So what happens in a real operating system?

RAM, Design Space, Examples. January Winter Term 2008/09 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

OPERATING SYSTEM. Chapter 9: Virtual Memory

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Memory Management III Memory Alloca0on

CS 5523 Operating Systems: Memory Management (SGG-8)

Memory Management william stallings, maurizio pizzonia - sistemi operativi

1. With respect to space, linked lists are and arrays are.

Memory Management. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

Memory Management Basics

CS61C : Machine Structures

Operating Systems Design Exam 2 Review: Spring 2012

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

Chapter 9: Virtual Memory. Operating System Concepts 9th Edition

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Chapter 10: Virtual Memory

Operating System Concepts 9 th Edition

Status of the Linux Slab Allocators

Chapter 9: Virtual Memory. Operating System Concepts 9 th Edition

Engine Support System. asyrani.com

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

CIS Operating Systems Contiguous Memory Allocation. Professor Qiang Zeng Spring 2018

A Heap of Trouble Exploiting the Linux Kernel SLOB Allocator

ECE 598 Advanced Operating Systems Lecture 10

Operating Systems Design Exam 2 Review: Fall 2010

Machine Problem 1: A Simple Memory Allocator. 100 points Due date: To Be Announced

Review! Lecture 5 C Memory Management !

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

CS61C : Machine Structures

Virtual Memory Outline

CS510 Operating System Foundations. Jonathan Walpole

CS61C : Machine Structures

CS 416: Opera-ng Systems Design March 23, 2012

The Art and Science of Memory Alloca4on

Lectures 13 & 14. memory management

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

File System Implementation. Sunu Wibirama

CS61C : Machine Structures

CIS Operating Systems Non-contiguous Memory Allocation. Professor Qiang Zeng Spring 2018

CSE506: Operating Systems CSE 506: Operating Systems

Memory Management. Today. Next Time. Basic memory management Swapping Kernel memory allocation. Virtual memory

Operating Systems Design Exam 2 Review: Spring 2011

CSE506: Operating Systems CSE 506: Operating Systems

Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libraries, and Toolchain

Memory Management. Outline. Memory. Virtual Memory. Instructor: Dr. Tongping Liu

FILE SYSTEM IMPLEMENTATION. Sunu Wibirama

Memory Management. Memory Management

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

Operating System Support

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 13: Address Translation

CS 550 Operating Systems Spring Memory Management: Paging

Memory Management. Dr. Yingwu Zhu

Chapter 11: Implementing File Systems. Operating System Concepts 8 th Edition,

Preview. Memory Management

10.1. CS356 Unit 10. Memory Allocation & Heap Management

CIS Operating Systems Memory Management. Professor Qiang Zeng Fall 2017

CS61C : Machine Structures

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

File System Internals. Jo, Heeseung

Dynamic Memory Allocation

Operating System Labs. Yuanbin Wu

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

Memory management. Johan Montelius KTH

CS 333 Introduction to Operating Systems. Class 11 Virtual Memory (1) Jonathan Walpole Computer Science Portland State University

20-EECE-4029 Operating Systems Spring, 2013 John Franco

Memory Management. Goals of Memory Management. Mechanism. Policies

Machine Problem 1: A Simple Memory Allocator

Memory: Overview. CS439: Principles of Computer Systems February 26, 2018

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Dynamic Memory Allocation. Zhaoguo Wang

e-pg Pathshala Subject: Computer Science Paper: Operating Systems Module 35: File Allocation Methods Module No: CS/OS/35 Quadrant 1 e-text

Operating Systems and Computer Networks. Memory Management. Dr.-Ing. Pascal A. Klein

UNIT III MEMORY MANAGEMENT

Dynamic Memory Management! Goals of this Lecture!

2 nd Half. Memory management Disk management Network and Security Virtual machine

Objectives and Functions Convenience. William Stallings Computer Organization and Architecture 7 th Edition. Efficiency

CS2506 Quick Revision

Dynamic Memory Management

Operating Systems CSE 410, Spring Virtual Memory. Stephen Wagner Michigan State University

Disks and I/O Hakan Uraz - File Organization 1


Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

Paging. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Linux Memory Analysis with Volatility. Andrew Case Digital Forensics Solutions

SCSI overview. SCSI domain consists of devices and an SDS

Chapter 9: Virtual-Memory Management. Operating System Concepts 8 th Edition,

First-In-First-Out (FIFO) Algorithm

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Dynamic Storage Allocation

CS 3733 Operating Systems:

Transcription:

Operating Systems 11. Memory Management Part 3 Kernel Memory Allocation Paul Krzyzanowski Rutgers University Spring 2015 1

Kernel memory The kernel also needs memory User code calls malloc kernel functions call kmalloc Lowest-level memory management: page allocator Allocate and free pages (for kernel and user processes) But need to manage smaller chunks of memory too Examples: semaphores, TCP connection state, network sockets, open file information, vm_area_struct Need to minimize wasted memory Page-size allocation is too wasteful Reuse memory and avoid fragmentation Sometimes we need physically-contiguous memory 2

Kernel Memory Allocation 3

Kernel Page Allocator 4

Page allocator With VM, processes can use non-contiguous pages Memory translation makes them look contiguous Sometimes you need contiguous allocation E.g., DMA logic ignores paging If we rely on DMA, we need contiguous pages 5

Page allocator Linux kernel support for contiguous buffers free_area: keep track of lists of free pages 1 st element: free single pages 2 nd element: free blocks of 2 contiguous pages 3 rd element: free blocks of 4 contiguous pages 10 th element: free blocks of 512 contiguous pages 6

Buddy System Try to get the best usable allocation unit If not available, get the next biggest one & split Coalesce upon free Example We want 8 contiguous pages Do we have a block of 8? Suppose no. Do we have a block of 16? Suppose no. Do we have a block of 32? Suppose yes. Split the 32 block into two blocks of 16. Back up. Do we have a block of 16? Yes! Split one of the 16 blocks into two blocks of eight. Back up. Do we have a block of 8? Yes! 7

Buddy System: Coalescence When a block is freed, see if we can merge buddies Two blocks are buddies if: They are the same size, b They are contiguous The address of the first page of the lower # block is a multiple of 2b page_size If two blocks are buddies, they are merged Repeat the process. 8

Buddy System Example 512 512 blocks 256 blocks 128 blocks 64 blocks We want a 64-block allocation. None available. Any 128-block chunks to split? No. Any 256-block chunks to split? No. Any 512-block chunks to split? Yes. 9

Buddy System Example 512 256 256 512 blocks 256 blocks 128 blocks 64 blocks Split a 512-block chunk into two 256-block chunks. Try again. We want a 64-block allocation. None available. Any 128-block chunks to split? No. Any 256-block chunks to split? Yes. 10

Buddy System Example 512 256 256 128 128 512 blocks 256 blocks 128 blocks 64 blocks Split a 256-block chunk into two 128-block chunks. Try again. We want a 64-block allocation. None available. Any 128-block chunks to split? Yes. 11

Buddy System Example 512 256 256 128 128 64 64 512 blocks 256 blocks 128 blocks 64 blocks Split a 128-block chunk into two 64-block chunks. Try again. We want a 64-block allocation. Got it! 12

Buddy System Example 512 256 256 128 128 64 64 512 blocks 256 blocks 128 blocks 64 blocks Requester gets the 64-block chunk. Later, it is no longer needed and is returned. 13

Buddy System Example 512 256 256 128 128 64 64 512 blocks 256 blocks 128 blocks 64 blocks Combine (coalesce) the two 64-block chunks into a128-block chunk 14

Buddy System Example 512 256 256 128 128 64 64 512 blocks 256 blocks 128 blocks 64 blocks Combine the two 128-block chunks into a 256-block chunk 15

Buddy System Example 512 256 256 128 128 64 64 512 blocks 256 blocks 128 blocks 64 blocks Combine the two 256-block chunks into a 512-block chunk 16

Linux Memory Management 17

Buddy System: page allocation Used for pages not s Manages lists of physically contiguous memory pages Buddy system used within each zone E.g., allocate 8 contiguous pages of DMA-capable DRAM Maps regions of memory to MMU page tables Regions are multiples of 2 x pages Lists of 1 page blocks 2 page blocks 4 page blocks 2 n page blocks (n defined by MAX_ORDER constant) 18

Zone Allocator: memory specification Ranges of pages may have different properties E.g., some architectures allow peripherals to perform DMA only for addresses < 16 MB. All memory is divided into zones DMA: memory accessible for DMA NORMAL HIGHMEM: for system use (file system buffers, user space, etc.) Allocation is handled per zone. An allocation request specifies zones in most- to least-preferred order This is not a memory allocator but a way of qualifying specific page needs 19

Slab Allocator: allocation Kernels often allocate specific s, not arbitrary sizes Initializing an sometimes takes more time than allocating it If possible, keep initialized (e.g., call mutex_init just once) Bring back to its initial state at deallocation Key concept Pre-allocate caches of contiguous memory to make it efficient to allocate allocation requests for s of a specific size. 20

Slab Allocator: components Terms Object: requested unit of allocation Slab: block of contiguous memory (often several pages) Each slab caches similarly-sized s Avoids fragmentation problems Cache: storage for a group of slabs for a specific Each unique type gets a separate cache Slab states Empty all s in the slab are marked as free The slab can be reclaimed by the OS for other purposes Full: all s in the slab are marked as in-use Partial: the slab contains free and in-use s 21

free free free free free free page page page page page page page page page page page page Slab allocator structure cache_chain: linked list of slab caches kmem_cache kmem_cache kmem_cache slabs_full slabs_partial slabs_empty head tail slab slab slab slab Try to find a free location for the requested on a partial slab in a cache for that type of. If not found, then allocate a new slab and assign it to a cache 22

Slab allocator: operations kmem_cache_create: create a new cache Typically used when the kernel initializes or a kernel module is loaded Identifies the name of the cache and size of its s Separate caches for inodes, directory entries, TCP sockets, etc. kmem_cache_destroy: destroy a cache Typically called by a module when it is unloaded kmem_cache_alloc: allocate an from a named cache cache_alloc_refill may be called to add memory to the cache kmalloc / kfree: no (cache) specified Iterate through available caches and find one that can satisfy the size request 23

Slab allocator: advantages Memory always gets allocated in the size requested No internal fragmentation Quick allocation 24

SLOB: Simple List Of Blocks Alternative memory allocator to Slab Designed for small and embedded memory-constrained systems Heap allocator SLOB heap = three singly linked list of pages 1. small s (< 256 bytes) 2. medium (< 1024 bytes) 3. large (< PAGE_SIZE) Lists are grown on demand with calls to get_free_page Blocks < page size returned from the heap Return 8-byte aligned block All blocks, allocated & free contain a header (metadata) Size of this block and offset of next free/allocated block Bytes PAGE_SIZE kmalloc calls get_free_pages directly and keeps a linked list of allocated pages 25

SLOB: Simple List Of Blocks Uses a first-fit allocation algorithm Suffers from fragmentation 26

SLUB allocator Current default kernel memory allocator on Linux Similar to SLAB: same slab structures Reduced performance overhead Support arbitrary number of CPUs and arbitrary # of slabs Metadata that was stored per slab moved to the page structure (info that the kernel uses to keep track of each page) Per-CPU queues removed to improve performance with multiprocessor systems 27

Linux kernel memory allocation Page allocator (Buddy) SLAB SLUB SLOB Choose one Original Default Small systems 28

The End 29