30
MEMORY MANAGEMENT Linux kernel training, session 8 Eugeniu Roşca

Linux Memory Management

Embed Size (px)

DESCRIPTION

Linux training Session8

Citation preview

Page 1: Linux Memory Management

MEMORY MANAGEMENTLinux kernel training, session 8

Eugeniu Roşca

Page 2: Linux Memory Management

ARM MMU. For docs see

http://infocenter.arm.com

3G/1G split

LINUX ADDRESS SPACES. FIST GLANCE

Page 3: Linux Memory Management

CONFIG_PAGE_OFFSET

cat /proc/vc-mem

LINUX ADDRESS SPACES. RASPBERRY PI.

Page 4: Linux Memory Management

Raspberry PI: Addresses in ARM Linux are:

• issued as virtual addresses by the ARM core, then

• mapped into a physical address by the ARM MMU, then

• mapped into a bus address by the ARM mapping MMU, and finally

• used to select the appropriate peripheral or location in RAM.

Address types in Linux, LDD3:1. virtual addresses = addresses allocated for user-space programs;

2. physical addresses = addresses used between the processor and the system’s memory;

In case of RasPi, translation done by ARM MMU.

3. bus addresses = addresses used between peripheral buses and memory.

Translation done by unit called IOMMU (in RasPi case: GPU MMU).

4. kernel logical addresses: normal address space of the kernel. One-to-one mapping to

physical addresses (+offset).

5. kernel virtual addresses: Also belong to kernel space only. But, not having one-to-one

mapping to physical. Cannot be represented by a void pointer.

ADDRESS TYPES. CORRELATION WITH LDD

Page 5: Linux Memory Management

GPU (VC)

MMU

BUS address

ARM classic

Raspberry PI

MMU. NORMAL CASE VS RASPBERRY PI.

Page 6: Linux Memory Management

/INCLUDE/LINUX/MMZONE.H

MEMORY ZONES. GENERIC CASE.

Page 7: Linux Memory Management

/INCLUDE/LINUX/MMZONE.H

MEMORY ZONES. RASPBERRY PI CASE.

Page 8: Linux Memory Management

MEMORY ZONES. EXPLAINED.

- ZONE DMA:

- is memory in the lower (0-16 MB) physical memory range that certain ISA

devices require;

- At this point it exists for historical reasons => long time ago, there was

hardware that could only do DMA into this area of physical memory;

- ZONE DMA32:

- exists only in 64-bit Linux;

- it is the low 4 GBytes of memory, more or less;

- It exists because the transition to large memory 64-bit machines has created a

class of hardware that can only do DMA to the low 4 GBytes of memory

- ZONE NORMAL (for 32-bit machines):

- directly mapped by the kernel into the upper region of the linear address space;

- all RAM from 16 MB to 896 MB for complex and somewhat historical reasons;

- ZONE NORMAL (for 64-bit machines):

- all RAM from 4GB (or so) to the topmost limit;

- ZONE HIGHMEM

- exists ONLY on 32bit machines;

- all RAM above 896 MB;

- not directly mapped by the kernel;

Page 9: Linux Memory Management

/INCLUDE/LINUX/MMZONE.H

KSWAPD. MEMORY ZONE WATERMARKS.

Page 10: Linux Memory Management

DOCUMENTATION / ARM / MEMORY.TXT

MEMORY LAYOUT ON ARM LINUX. 1.

Page 11: Linux Memory Management

DOCUMENTATION / ARM / MEMORY.TXT

MEMORY LAYOUT ON ARM LINUX. 2.

VMALLOC_START VMALLOC_END

PAGE_OFFSET high_memory

PKMAP_BSE PAGE_OFFSET - 1

MODULES_VADDR MODULES_END

TASK_SIZE - 1

Check Kernel

Definitions in

source code !

Page 12: Linux Memory Management

GOING DEEPER IN MEMORY

MANAGEMENT. STATIC

Page 13: Linux Memory Management

INCLUDE / LINUX / MMZONE.H

BUDDY ALLOCATOR.

- It is responsible for the management of the page allocations in the

entire system;

- It splits memory into pairs of 2n pages where n is in range

from 0 to MAX_ORDER;

- It stores information about the free blocks of pages in the array of

lists as follows:

Page 14: Linux Memory Management

INCLUDE / LINUX / MMZONE.H

BUDDY ALLOCATOR.

- Each list consists of free physically contiguous blocks of 2^i

memory pages, where i is the list number;

- Each of such blocks, except the block that consists of 1 page, can be

split into two halves and used as 2 blocks of a half size.

- So if no entries exist in the requested list, an entry from the next

upper list is broken into two separate clusters and one is returned

to the caller while the other one is added to the next lower list.

- On the other hand, every two blocks of memory of the same size,

which have common border (arranged in memory sequentially,

from the standpoint of physical addresses), may be united into the

single block of the bigger size.

Page 15: Linux Memory Management

INCLUDE / LINUX / MMZONE.H

BUDDY ALLOCATOR.

Page 16: Linux Memory Management

INTERNAL FRAGMENTATION VS EXTERNAL

FRAGMENTATION

External fragm: Space wasted outside of allocated memory blocks. Small

pieces of memory exist among the allocated blocks. However, a larger

allocation request cannot be fulfilled, because the free pieces are not

physically contiguous.

Internal fragm: Space wasted inside of allocated memory blocks because

of restriction on the allowed sizes of allocated blocks.

Page 17: Linux Memory Management

AVAILABLE KERNEL-SPACE MEMORY

ALLOCATION METHODS

Page 18: Linux Memory Management

MEMORY ALLOCATION API

GET PAGES DIRECTLY. BYPASS SLAB.

Page 19: Linux Memory Management

MEMORY ALLOCATION API

GET PAGES DIRECTLY. BYPASS SLAB.

Page 20: Linux Memory Management

MEMORY ALLOCATION API

GET PAGES DIRECTLY. BYPASS SLAB.

Page 21: Linux Memory Management

MEMORY ALLOCATION API

GET PAGES DIRECTLY. BYPASS SLAB.

Page 22: Linux Memory Management

MEMORY ALLOCATION API

GET MEMORY THROUGH SLAB.

Page 23: Linux Memory Management

SLAB ALLOCATOR.

size-4194304 0 0 4194304 1 1024 : tunables 1 1 0 : slabdata 0 0 0

size-2097152 0 0 2097152 1 512 : tunables 1 1 0 : slabdata 0 0 0

size-1048576 0 0 1048576 1 256 : tunables 1 1 0 : slabdata 0 0 0

size-524288 0 0 524288 1 128 : tunables 1 1 0 : slabdata 0 0 0

size-262144 0 0 262144 1 64 : tunables 1 1 0 : slabdata 0 0 0

size-131072 0 0 131072 1 32 : tunables 8 4 0 : slabdata 0 0 0

size-65536 0 0 65536 1 16 : tunables 8 4 0 : slabdata 0 0 0

size-32768 8 8 32768 1 8 : tunables 8 4 0 : slabdata 8 8 0

size-16384 1 1 16384 1 4 : tunables 8 4 0 : slabdata 1 1 0

size-8192 8 8 8192 1 2 : tunables 8 4 0 : slabdata 8 8 0

size-4096 25 25 4096 1 1 : tunables 24 12 0 : slabdata 25 25 0

size-2048 24 24 2048 2 1 : tunables 24 12 0 : slabdata 12 12 0

size-1024 84 84 1024 4 1 : tunables 54 27 0 : slabdata 21 21 0

size-512 376 376 512 8 1 : tunables 54 27 0 : slabdata 47 47 0

size-256 210 210 256 15 1 : tunables 120 60 0 : slabdata 14 14 0

size-192 141 160 192 20 1 : tunables 120 60 0 : slabdata 8 8 0

size-128 570 570 128 30 1 : tunables 120 60 0 : slabdata 19 19 0

size-96 996 1000 96 40 1 : tunables 120 60 0 : slabdata 25 25 0

size-64 882 1062 64 59 1 : tunables 120 60 0 : slabdata 18 18 0

size-32 3337 3390 32 113 1 : tunables 120 60 0 : slabdata 30 30 0

kmem_cache 135 160 96 40 1 : tunables 120 60 0 : slabdata 4 4 0

cat /proc/slabinfo output:

Page 24: Linux Memory Management

SLAB ALLOCATOR.

Page 25: Linux Memory Management

SLAB ALLOCATOR.

Page 26: Linux Memory Management

KMALLOC ALLOCATOR.

Page 27: Linux Memory Management

KMALLOC ALLOCATOR.

Page 28: Linux Memory Management

KMALLOC ALLOCATOR.

Page 29: Linux Memory Management

VMALLOC ALLOCATOR.

Page 30: Linux Memory Management

/PROC INFORMATION

- /proc/iomem

- /proc/meminfo = see /Documentation/filesystems/proc.txt

- /proc/zoneinfo = see page_alloc.c

- /proc/vmstat/

- /proc/buddyinfo = debug External fragmentation. Lots of order 0

pages mean lots of fragmentation, (kernel can't even find two

adjacent aligned pages to merge into an 8kb order 1 chunk)

- /proc/sys/vm/* = see Documentation/sysctl/vm.txt

- /proc/pagetypeinfo = goal same as of buddyinfo (debug External

fragmentation)

- /proc/vmallocinfo = see /Documentation/filesystems/proc.txt

(iotable_init, ioremap, vmalloc)

- /proc/vc-mem/