linux memory management

16
Linux Memory Management How does the Linux kernel keep track of the Virtual Memory Areas that each process uses?

Upload: fuller

Post on 06-Jan-2016

27 views

Category:

Documents


0 download

DESCRIPTION

Linux Memory Management. How does the Linux kernel keep track of the Virtual Memory Areas that each process uses?. Hardware/Firmware/Software. The PC ‘s memory management system is a cooperative venture involving hardware, software, and firmware - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Linux Memory Management

Linux Memory Management

How does the Linux kernel keep track of the Virtual Memory Areas

that each process uses?

Page 2: Linux Memory Management

Hardware/Firmware/Software

• The PC ‘s memory management system is a cooperative venture involving hardware, software, and firmware

• The hardware and firmware would be the same no matter which Operating System we choose to run (i.e., Linux or Windows)

• An operating system utilizes the hardware (in its own unique way) to provide multiple processes with protected memory-spaces

Page 3: Linux Memory Management

The Linux/x86 scheme

• Let’s focus on the particular approach that Linux takes in utilizing the Intel x86 CPU’s memory-addressing capabilities

• There are two conceptual levels (because “processor independence” is a goal Linux strives for): much of the Linux ‘mm’ code runs on almost any CPU, yet there’s some code is (unavoidably) specific to the x86

Page 4: Linux Memory Management

Source-code organization

High-level ‘mm’ code is processor-independent (It’s in the ‘/usr/src/linux/mm’ subdirectory)

Low-level ‘mm’ code is processor-specific (It’s in the ‘/usr/src/linux/arch/’ subdirectories)

i386/mm ppc/mm mips/mm alpha/mm . . .

Page 5: Linux Memory Management

Our machines have 1GB of RAM

MainMemory(1 GB)

Intel PentiumCPU

0x00000000

0x3FFFFFFF

system bus

NIC VGAFIFO

(4 KB)VRAM

(32 MB) some deviceshave RAM, too

Page 6: Linux Memory Management

Booting up

• At power-on, the CPU sees only 1-MB of the physical memory (including the ROMs)

• The OS initialization code must “activate” the processor’s “protected-mode” MMU so that all the ‘Extended Memory’ (including device-memory) will become addressable

• Finally the OS enables “virtual memory” to allow the illusion of a 4-GB address-space

Page 7: Linux Memory Management

Kernel memory-mapping

Virtual Memory

0x00000000

0xFFFFFFFF

4-GB

Physical memory0x00000000

0x3FFFFFFF

1-GB896-MB 896-MB

0xC0000000

display memory

device memory

mappings

Page 8: Linux Memory Management

Application memory-mapping

kernelspace

user space

text

data

stack

text

data

stack

physical memory

virtual memory

mappings

3-GB

Page 9: Linux Memory Management

‘mm_struct’

• Each application-process uses a different mapping of physical memory regions into the Pentium’s “virtual” address-space, so one process can’t access memory owned by another process (i.e., private regions)

• The OS switches these mapping when it suspends one process to resume another

• Each task’s mapping is in its ‘task_struct’

Page 10: Linux Memory Management

How Linux tracks mappings

task_struct

mm

mm_struct

pgd

start, end, etc

vm_area_struct

start, end, etc

vm_area_struct

start, end, etc

vm_area_struct

start, end, etc

vm_area_struct

vma

mapping-tablesmapping-tables

mapping-tables

Page 11: Linux Memory Management

What does ‘malloc()’ do?

• The standard C runtime environment lets a user program allocate additional memory

void *malloc( unsigned long nbytes );

• This memory is released after being used

void free( void *vaddr );

• Using suitable kernel modules we can see how the kernel keeps track of this memory

Page 12: Linux Memory Management

‘mm.c’ and ‘vma.c’

• These modules create pseudo-files that allow us to view memory-management information associated with a user task

• We can read such pseudo-files before, and after, we perform a call to ‘malloc()’

• Our ‘domalloc.cpp’ demo illistrates this

• We can compare the file-outputs to see what changes occurred in the kernel data

Page 13: Linux Memory Management

When a program ‘forks()’

• We already know how new processes get created in the UNIX/Linux environment (i.e., via the ‘fork()’ function or system-call)

• A child-process gets constructed using the same code and data as its parent-process

• Yet each task’s memory-regions are kept ‘private’ (i.e., accessible to it alone)

• So how exactly is this feat achieved?

Page 14: Linux Memory Management

user space

Similar memory-mappings

kernelspace

user space

text

data

stack

parent-process

kernelspace

text

data

stack

child-process

text

data

data

stack

stack

physical memoryvirtual memory virtual memory

Page 15: Linux Memory Management

Another demo

• You could write a program, similar to our ‘domalloc.cpp’ demo, that would let a user compare the list of memory-regions which a parent-process owns and the list that its child-process has inherited

Page 16: Linux Memory Management

In-class exercise

• Write an application program that allows a user to compare BOTH the ‘mm_struct’ and the ‘vm_area_struct’ information that belong to a parent-process and to its child

• You can use our ‘mm.c’ and ‘vma.c’ Linux kernel modules, and you can imitate the programming ideas used in ‘domalloc.cpp’

• TURN IN printouts of your code and output