Presentation of Chapter 4, LINUX Kernel Internals Zhihua (Scott) Jiang Computer Science Department University of Maryland, Baltimore County Baltimore,

Slides:



Advertisements
Similar presentations
Memory Management in Linux Anand Sivasubramaniam.
Advertisements

1/1/ / faculty of Electrical Engineering eindhoven university of technology Memory Management and Protection Part 3:Virtual memory, mode switching,
Part IV: Memory Management
Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
The Linux Kernel: Memory Management
Memory Management in Linux (Chap. 8 in Understanding the Linux Kernel)
Memory management.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 3 Memory Management Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
16.317: Microprocessor System Design I
CMPT 300: Final Review Chapters 8 – Memory Management: Ch. 8, 9 Address spaces Logical (virtual): generated by the CPU Physical: seen by the memory.
Memory Management (II)
CE6105 Linux 作業系統 Linux Operating System 許 富 皓. Chapter 2 Memory Addressing.
Introduction to Kernel
CMPT 300: Final Review Chapters 8 – Memory Management: Ch. 8, 9 Address spaces Logical (virtual): generated by the CPU Physical: seen by the memory.
Chapter 3.2 : Virtual Memory
Chapter 9 Virtual Memory Produced by Lemlem Kebede Monday, July 16, 2001.
File System Implementation
1 Memory Management in Representative Operating Systems.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 8: Main Memory.
Operating Systems Chapter 8
CSNB334 Advanced Operating Systems 5. Memory Management
Topics covered: Memory subsystem CSE243: Introduction to Computer Architecture and Hardware/Software Interface.
Memory Management 3 Tanenbaum Ch. 3 Silberschatz Ch. 8,9.
1 Chapter 3.2 : Virtual Memory What is virtual memory? What is virtual memory? Virtual memory management schemes Virtual memory management schemes Paging.
Chapter 4 Storage Management (Memory Management).
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
1 Memory Management 4.1 Basic memory management 4.2 Swapping 4.3 Virtual memory 4.4 Page replacement algorithms 4.5 Modeling page replacement algorithms.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
1 Linux Operating System 許 富 皓. 2 Memory Addressing.
Chapter 4 Memory Management Virtual Memory.
Memory. Chapter 8: Memory Management Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation.
Page 111/15/2015 CSE 30341: Operating Systems Principles Chapter 11: File System Implementation  Overview  Allocation methods: Contiguous, Linked, Indexed,
File System Implementation
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 9: Virtual Memory.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Main Memory. Chapter 8: Memory Management Background Swapping Contiguous Memory Allocation Paging Structure of the Page Table Segmentation Example: The.
Processes and Virtual Memory
Multilevel Caches Microprocessors are getting faster and including a small high speed cache on the same chip.
File-System Management
Lecture 24 – Paging implementation
Introduction to Kernel
Chapter 2: The Linux System Part 4
Memory Management.
Processes and threads.
Chapter 9: Virtual Memory
OS Virtualization.
Lecture 14 Virtual Memory and the Alpha Memory Hierarchy
Chapter 9: Virtual-Memory Management
MEMORY MANAGEMENT & their issues
An overview of the kernel structure
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 8 11/24/2018.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 12/1/2018.
So far… Text RO …. printf() RW link printf Linking, loading
CS399 New Beginnings Jonathan Walpole.
Outline Allocation Free space management Memory mapped files
Memory Management Tasks
Chapter 8: Memory management
Outline Module 1 and 2 dealt with processes, scheduling and synchronization Next two modules will deal with memory and storage Processes require data to.
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 4/5/2019.
Virtual Memory: Systems CSCI 380: Operating Systems
CSCI 380: Operating Systems William Killian
Buddy Allocation CS 161: Lecture 5 2/11/19.
Chapter 1: Introduction CSS503 Systems Programming
Structure of Processes
Mr. M. D. Jamadar Assistant Professor
CSE 542: Operating Systems
Dirty COW Race Condition Attack
Presentation transcript:

Presentation of Chapter 4, LINUX Kernel Internals Zhihua (Scott) Jiang Computer Science Department University of Maryland, Baltimore County Baltimore, MD 21250

Guideline The Architecture-independent Memory Model in LINUXThe Architecture-independent Memory Model in LINUX The Virtual Address Space for a ProcessThe Virtual Address Space for a Process Block Device CachingBlock Device Caching Paging Under LINUXPaging Under LINUX

The architecture-independent memory model Pages of MemoryPages of Memory Virtual Address SpaceVirtual Address Space Converting the Linear AddressConverting the Linear Address The Page DirectoryThe Page Directory The Page Middle DirectoryThe Page Middle Directory The Page TableThe Page Table

Pages of memory Defined by the PAGE_SIZE macro in the asm/page.hDefined by the PAGE_SIZE macro in the asm/page.h For X86, the size is 4k bytesFor X86, the size is 4k bytes For Alpha uses 8K bytesFor Alpha uses 8K bytes

Virtual address space Given by reference to a segment selector and the offset within the segmentGiven by reference to a segment selector and the offset within the segment C pointers hold the offsetsC pointers hold the offsets Defined in asm/segment.hDefined in asm/segment.h –KERNERL_DS (segment selector for kernel data) –USER_DS (segment selector for user data) By carrying out a conversion on the segment selector register, a system function can be given pointers to the kernel segment.By carrying out a conversion on the segment selector register, a system function can be given pointers to the kernel segment. –Used by UMSDOS file system to simulate a Unix file system

Continued MMU of an x86 processor converts the virtual address to a linear addressMMU of an x86 processor converts the virtual address to a linear address 4 Gbytes by width of the linear address4 Gbytes by width of the linear address –3 Gbytes for user segment –1 Gbyte for kernel segment Alpha does not support segmentationAlpha does not support segmentation –Offset addresses for the user segment not permitted to overlap with the offset addresses for the kernel segment

Converting the linear address Linear address conversion in the architecture-independent memory model Linear address

The virtual address space for a process The User SegmentThe User Segment Virtual Memory AreasVirtual Memory Areas The System Call brkThe System Call brk Mapping FunctionsMapping Functions The Kernel SegmentThe Kernel Segment Static Memory Allocation in the Kernel SegmentStatic Memory Allocation in the Kernel Segment Dynamic Memory Allocation in the Kernel SegmentDynamic Memory Allocation in the Kernel Segment

The user segment In user mode, access only in user segmentIn user mode, access only in user segment Individual page tables for different processesIndividual page tables for different processes system call forksystem call fork –child and parent processes have different page directories and page tables –however, in the kernel segment page tables are shared by all processes system call clonesystem call clone –old and new threads share the memory fully

Continued Some explanation for shared libraries in the user segmentSome explanation for shared libraries in the user segment –Originally, linked into one binary, lead to efficiency –Drawback is the growth of the length –Stored in separate files and loaded at program start –Linked to static addresses –With ELF, allowed shared libraries to be loaded during program execution –No absolute address references in the compiled code

Virtual memory areas Process not use all functions at any timeProcess not use all functions at any time Process can share codes if they are run by the same executable fileProcess can share codes if they are run by the same executable file Copy-on-write strategy used for memory managementCopy-on-write strategy used for memory management

The system call brk The brk field points to the end of the BSS segment for non- statically initialized dataThe brk field points to the end of the BSS segment for non- statically initialized data Used for allocating or releasing dynamic memoryUsed for allocating or releasing dynamic memory The system call brk can be used to find the current value of the pointer or to set it to a new one under protection checkThe system call brk can be used to find the current value of the pointer or to set it to a new one under protection check Rejected if the mem required exceeds the estimated sizeRejected if the mem required exceeds the estimated size function sys_brk() calls do_map() to map a private and anonymous area between the old & new values of brkfunction sys_brk() calls do_map() to map a private and anonymous area between the old & new values of brk

Mapping functions C library provides 3 functions in sys/mman.hC library provides 3 functions in sys/mman.h –caddr_t mmap(caddr_t addr, size_t len, int prot, int flags, int fd, off_t off); –int munmap(caddr_t addr, size_t len); –int mprotect(caddr_t addr, size_t len, int prot); –int msync;

The kernel segment In x86 architecture, a system call is generally initiated by the software interrupt 128 (0x80) being triggered.In x86 architecture, a system call is generally initiated by the software interrupt 128 (0x80) being triggered. Any processes in system mode will encounter the same kernel segmentAny processes in system mode will encounter the same kernel segment Kernel segment in alpha architecture cannot start at addr 0Kernel segment in alpha architecture cannot start at addr 0 A PAGE_OFFSET is provided between physical & virtual addrsA PAGE_OFFSET is provided between physical & virtual addrs

Static memory allocation in the kernel segment Initialization routine for character-oriented devices is called as followsInitialization routine for character-oriented devices is called as follows memory_start = console_init(memory_start, memory_end); Reserves memory by returning a value higher than the parameter memory_startReserves memory by returning a value higher than the parameter memory_start The memory between the return value and memory_start can be used as desired by the initialized componentThe memory between the return value and memory_start can be used as desired by the initialized component

Dynamic memory allocation in the kernel segment In LINUX kernel, kmalloc() and kfree() used for dynamic memory allocationIn LINUX kernel, kmalloc() and kfree() used for dynamic memory allocation –void * kmalloc(size_t size, int priority); –void kfree(void *obj); To increase efficiency, the memory reserved is not initializedTo increase efficiency, the memory reserved is not initialized In LINUX kernel 1.2, __get_free_pages() only to reserve contiguous areas of memory of 4, 8, 16, 32, 64, and 128 Kbytes in sizeIn LINUX kernel 1.2, __get_free_pages() only to reserve contiguous areas of memory of 4, 8, 16, 32, 64, and 128 Kbytes in size kmalloc() can reserve far smaller areas of memorykmalloc() can reserve far smaller areas of memory

Continued Sizes[] contains descriptors for different for different sizes of memory areaSizes[] contains descriptors for different for different sizes of memory area –one manages memory suitable for DMA –the other is responsible for ordinary memory

Continued Structures for kmalloc

Continued Kmalloc() and kfree() restricted to the size of one page of memKmalloc() and kfree() restricted to the size of one page of mem vmalloc() and vfree() improved to multiple of the size of one page of memvmalloc() and vfree() improved to multiple of the size of one page of mem The max of value of size is limited by the amount of physical memory availableThe max of value of size is limited by the amount of physical memory available Memory reserved by vmalloc() won’t be copied to external storageMemory reserved by vmalloc() won’t be copied to external storage

Continued Comparison of vmalloc() and kmalloc()Comparison of vmalloc() and kmalloc() –the size of the area of memory requested can be better adjusted to actual needs –Limited only by the size of free physical memory and not by its segmentation (as kmalloc() is) –Does not return any physical address –reserved memory can be non-consecutive pages –not suitable for reserving memory for DMA

Block Device Caching Block BufferingBlock Buffering The update and bdflush ProcessesThe update and bdflush Processes List Structures for the Buffer CacheList Structures for the Buffer Cache Using the Buffer CacheUsing the Buffer Cache

Block Buffering Block size may be 512, 1024, 2048, or 4096 bytesBlock size may be 512, 1024, 2048, or 4096 bytes Held in memory via a buffering systemHeld in memory via a buffering system A special case applies for blocks taken from files opened with the flag 0_SYNCA special case applies for blocks taken from files opened with the flag 0_SYNC –Transferred to disk every time their contents are modified Data is organized as frequently requested data lie every close together & can be kept in the processor cacheData is organized as frequently requested data lie every close together & can be kept in the processor cache

The update and bdflush Processes At periodic intervals, update process calls the system call bdflush with an parameterAt periodic intervals, update process calls the system call bdflush with an parameter All modified buffer blocks are written back to disk with all superblock and inode informationAll modified buffer blocks are written back to disk with all superblock and inode information bdflush, writes back the number of blocks buffers marked “dirty” given in the bdflush parameterbdflush, writes back the number of blocks buffers marked “dirty” given in the bdflush parameter Always activated when a block is released by means of brelse()Always activated when a block is released by means of brelse() Also activated when new block buffers are requested or the size of the buffer cache needs to be reducedAlso activated when new block buffers are requested or the size of the buffer cache needs to be reduced

List structure for the buffer cache LINUX manages its block buffers via a number of different doubly linked listsLINUX manages its block buffers via a number of different doubly linked lists Block buffers in use are managed in a set of special LRU listsBlock buffers in use are managed in a set of special LRU lists

Using the buffer cache Function bread () is called for block readFunction bread () is called for block read Variance of bread(), breada(), reads not the block requested into the buffer cache but a number of following blocksVariance of bread(), breada(), reads not the block requested into the buffer cache but a number of following blocks

Paging under LINUX Page Cache and ManagementPage Cache and Management Finding a Free PageFinding a Free Page Page Errors and Reloading a PagePage Errors and Reloading a Page

Page Cache and Management LINUX can save pages to extenral media in 2 waysLINUX can save pages to extenral media in 2 ways –a complete block device as the external medium, typically a partition on a hard disk –fixed-length files on a file system for its external storage Data that belong together are stored in a cache line (16 bytes)Data that belong together are stored in a cache line (16 bytes)

Finding a free page __get_free_pages() is called after physical pages of mem reserved__get_free_pages() is called after physical pages of mem reserved –unsigned long __get_free_pages(int priority, unsigned long order, int dma) ;

Page errors and reloading a page do_page_fault() is called when there generates a page fault interruptdo_page_fault() is called when there generates a page fault interrupt –void do_page_fault(struct pt_regs *regs, unsigned long error_code); do_no_page() or do_wp_page() is called when the address is in a virtual memory area, the legality of the read or write operation is checked by reference to the flags for the virtual memdo_no_page() or do_wp_page() is called when the address is in a virtual memory area, the legality of the read or write operation is checked by reference to the flags for the virtual mem