69
Microkernel Construction III Virtual Memory Cristan Szmajda [email protected] 23 October 2003 THEUNIVERSITYOF NEWSOUTHWALES

Microker nel Constr uction III Vi rt ual Memory

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Microker nel Constr uction III Vi rt ual Memory

Microker nel Constr uction III

Virtual Memory

Cr istan [email protected]

23 October 2003

THE UNIVERSITY OFNEW SOUTH WALES

Page 2: Microker nel Constr uction III Vi rt ual Memory

Outline of lecture

• br ief introduction to virtual memory and page tables

• page table structures and the page table problem

• the guarded page table (GPT)

→ theor y, implementation, and perfor mance

• the variable radix page table (VRPT)

→ theor y, implementation, and perfor mance

• advanced hardware VM features

→ super pages

→ suppor t for shared memory

• vir tual memor y implementation in the L4 microker nel

Page 3: Microker nel Constr uction III Vi rt ual Memory

Virtual memory (VM)

VM allows the OS to manage memory and swap to disk.

translated by MMU

virtualpages

physicalframes

virtual address space

physical memory

A memor y management unit (MMU) translates virtual to physical.

Page 4: Microker nel Constr uction III Vi rt ual Memory

Paging

Transla tion: vir tual page number → physical frame number

virtual address

physical address

page number page offset

frame number frame offset

translation

Page 5: Microker nel Constr uction III Vi rt ual Memory

The page table (PT)

• ta ble of translations

• stored in main memory

• cached in translation

lookaside buffer (TLB)

• consulted on every

TLB miss

(hardware OR software)

• uses memory bandwidth

• comes in a var iety

of different for mats

CPU

memory

virtual

address

physical

address

TLB

page

table

VA

data

Page 6: Microker nel Constr uction III Vi rt ual Memory

Page table structures

VM perfor mance is directly limited by page table perfor mance.

Classical page table structures were designed for

• 32-bit address spaces, and

• the Unix two-segment model.

How well do they perfor m in:

• large (64-bit) address spaces?

• spar se address-space distributions?

• micro-ker nel system structures?

Page 7: Microker nel Constr uction III Vi rt ual Memory

Multi-level page table (MLPT)

Tr aditional MLPT is used by many 32-bit processor s .

pagetableroot

framenumber

virtual address

page offset

• Saves space when most of address space is unused.

• more levels → saves space, costs time

Page 8: Microker nel Constr uction III Vi rt ual Memory

Multi-level page table (MLPT)

Disadvantages:

• Extension to 64-bit requires 4 to 5 levels .

pagetableroot

framenumber

virtual address

page offset

• Spar se address spaces waste a lot of space.

Page 9: Microker nel Constr uction III Vi rt ual Memory

Virtual linear page table (VLPT)

Physical memory

Virtual Address

Virtual Array Page Root in

Physical Mem

P Off

P

Off

Equivalent to MPT, but:

• better best-case lookup time

• steals TLB entries from application

• requires nested exception handling

Page 10: Microker nel Constr uction III Vi rt ual Memory

Inverted page table

• indexed by physical (not virtual address)

• hash anchor table (HAT) is for lookup by vir tual address

T P L

Off

Frame Table

F

Virtual Address

T Off

Hash Anchor Table

Page 11: Microker nel Constr uction III Vi rt ual Memory

Inverted page table

Advantages:

• scales with physical, not virtual, memory size

• no problem with virtual sparsity

• one IPT per system, not per address space

• PTEs bigger as need to store tag

• system needs a frame table anyway

Disadvantages:

• newer machines have spar se physical address space

• difficult to support super-pages

• shar ing is hard

Page 12: Microker nel Constr uction III Vi rt ual Memory

Hashed page table (HPT)

HPT merges the IPT and HAT into a single table.

V P

V P

V P

V P

V P V P

V P

hash table

hash equal?

physicalframe

number

page offset

virtual address

collision chains

Each HPT entry contains both virtual and physical page numbers.

Page 13: Microker nel Constr uction III Vi rt ual Memory

Hashed page table (HPT)

Advantages:

• best-case lookup: one memory reference

• spar sity no problem

• hash function independent of address size

Disadvantages:

• collisions

• shar ing

• creation & deletion costs

• traver sal

• assumes a single page size

Page 14: Microker nel Constr uction III Vi rt ual Memory

Clustered page table (CPT)

P1 P2 P3P

B

0

Virtual Address

Hash Table

T

BT Off

Off

L

• an HPT with multiple pages per PTE

• can load multiple pages into the TLB per miss

• improv es perfor mance in presence of spatial locality

• cluster ing also used in MIPS R4000 hardware TLB entry

Page 15: Microker nel Constr uction III Vi rt ual Memory

Software TLB

• a direct-mapped cache of TLB entries in main memory

• fast lookup; can achieve >95% hit rate

• also called TLB cache or level 2 TLB

Software TLB

Virtual Address

T Off

T P

Off

hitmiss

Page Table

• also simple enough for hardware implementation

• difficult to support super-pages

Page 16: Microker nel Constr uction III Vi rt ual Memory

The page table problem

What’s a computer architect to do?

• Provide PT walker in hardware.

→ complexity (e.g., microcode, cache coherency)

→ PT for mat difficult to change

• Provide a hardware HPT walker, but trap to software on

every collision.

→ could be used as hardware-assisted HPT, or

→ could be used to cache some other PT for mat

• Tr ap to software on every TLB miss.

→ PT for mat completely at discretion of software

→ exceptions can be expensive

Page 17: Microker nel Constr uction III Vi rt ual Memory

TLB perfor mance

TLB overhead is getting wor se in modern architectures .

• Clar k and Emer [1985] repor t 5.4 – 8.7% (VAX-11/780).

Page 18: Microker nel Constr uction III Vi rt ual Memory

TLB perfor mance

TLB overhead is getting wor se in modern architectures .

• Clar k and Emer [1985] repor t 5.4 – 8.7% (VAX-11/780).

• Chen, Borg, and Jouppi [1992] repor t 2.9% – 47% (R2000).

Page 19: Microker nel Constr uction III Vi rt ual Memory

TLB perfor mance

TLB overhead is getting wor se in modern architectures .

• Clar k and Emer [1985] repor t 5.4 – 8.7% (VAX-11/780).

• Chen, Borg, and Jouppi [1992] repor t 2.9% – 47% (R2000).

• Romer et al. [1995] repor t 5.5 – 71% (Alpha 21064).

Page 20: Microker nel Constr uction III Vi rt ual Memory

TLB perfor mance

TLB overhead is getting wor se in modern architectures .

• Clar k and Emer [1985] repor t 5.4 – 8.7% (VAX-11/780).

• Chen, Borg, and Jouppi [1992] repor t 2.9% – 47% (R2000).

• Romer et al. [1995] repor t 5.5 – 71% (Alpha 21064).

• Navarro et al. [2002] repor t 0 – 83% (Alpha 21264).

Extreme case: matr ix benchmar k: 650%

Page 21: Microker nel Constr uction III Vi rt ual Memory

TLB perfor mance

TLB overhead is getting wor se in modern architectures .

• Clar k and Emer [1985] repor t 5.4 – 8.7% (VAX-11/780).

• Chen, Borg, and Jouppi [1992] repor t 2.9% – 47% (R2000).

• Romer et al. [1995] repor t 5.5 – 71% (Alpha 21064).

• Navarro et al. [2002] repor t 0 – 83% (Alpha 21264).

Extreme case: matr ix benchmar k: 650%

Av erage and worst-case ov erheads are trending up. Why?

Page 22: Microker nel Constr uction III Vi rt ual Memory

Why has TLB overhead increased?

• Software expands to fill availa ble space.

• Widening gap between processor and memory speed.

→ lots of attention on cache hierarchies

→ not so much attention on TLBs and page table

→ TLB miss penalty is high

• 64-bit addresses

→ widen CAM in TLB (which is virtually-tagged)

→ complicate the page table

• Wide sharing → improv es memor y (but not TLB) coverage

TLB perfor mance is improving slower than memory perfor mance.

Page 23: Microker nel Constr uction III Vi rt ual Memory

TLB capacity

0.01 %

0.1 %

1 %

10 %

’86 ’88 ’90 ’92 ’94 ’96 ’98 ’00 ’02TLB

cov

erag

e / m

ain

mem

ory

size

(%

)

Year of workstation manufacture

Source: Navarro et al. [2002]

Page 24: Microker nel Constr uction III Vi rt ual Memory

TLB miss penalty

Increasing due to more complex and pipelined processor s .

• i80386: 9 to 13 cycles

• VAX-11/780: 17 to 23 cycles

• Pentium 4: 31 to 48 cycles (assuming L2 cache hits)

• Po w erPC 604: 120 cycles (assuming software TLB hit)

Page 25: Microker nel Constr uction III Vi rt ual Memory

Why not just make bigger and faster TLBs?

• large CAMs are slow and hot

• often flushed (context switch, address space teardown,

protection change, etc.)

• MHz, MBytes, and caches sell computers, not TLBs

Why not just increase the page size?

• fragmentation

• I/O latency

• iner tia

The solutions provided by computer architects are different⋅⋅⋅

Page 26: Microker nel Constr uction III Vi rt ual Memory

Smar t TLBs in modern architectures

How have computer architects addressed the problem?

• super pages

→ more TLB coverage with same number of entries

• shared tags (e.g., IA-64 protection keys)

→ addresses the effect of sharing on TLB coverage

• hardware assistance for TLB refill

→ faster exceptions

→ par tial or full hardware PT walker

TLBs are becoming smar ter, not lar ger and faster.

Page 27: Microker nel Constr uction III Vi rt ual Memory

A smar t page table

Need a page table that:

• minimizes the number of references to slow memor y

• works well with 64-bit addresses

• works well with sparse address spaces

• is fast to create, destroy, and traver se

• suppor ts mixed page sizes

• suppor ts shar ing explicitly

Page 28: Microker nel Constr uction III Vi rt ual Memory

Pa th compression

Spar sity often creates one-way paths of tables in MLPT.

pagetableroot

framenumber

virtual address

a one-way path

page offset

Idea: bypass these paths.

Page 29: Microker nel Constr uction III Vi rt ual Memory

Guarded page table (GPT)

A path-compresed MLPT, invented by Liedtke [1995].

pagetableroot

equal?

framenumber

virtual address

page offset

• check skipped address bits (‘guards’) during lookup

• takes advantage of sparsity in the address space

Page 30: Microker nel Constr uction III Vi rt ual Memory

GPT perfor mance

Elphinstone (1999) studied GPT and var ious other page tables ,

using L4/MIPS as a testbed.

source name size (M) type remar ks

go 0.8 I game of go

swim 14.2 F PDE solver

SPEC gcc 9.3 I GNU C compiler

CPU95 compress 34.9 I file (un)compression

apsi 2.2 F PDE solver

wave5 40.4 F PDE solver

c4 5.1 I game of connect four

nsieve 4.9 I pr ime number generator

hea psor t 4.0 I sor ting large arrays

mm 7.7 F matr ix multiply

tfftdp 4.0 F fast four ier transfor m

Albur to

Page 31: Microker nel Constr uction III Vi rt ual Memory

GPT refill time

0

50

100

150

200

250

300

G2 G4 G8 G16 G32 G64 G128 G256

cycl

es

entries per GPT node

Page 32: Microker nel Constr uction III Vi rt ual Memory

GPT ver sus other page tables

0

20

40

60

80

100

120

140

160

MPT G16 H8k H128k C128k G16S8k/ G16

S128k/

cycl

es

Page 33: Microker nel Constr uction III Vi rt ual Memory

GPT depth

0

2

4

6

8

10

12

14

G2 G4 G8 G16 G32 G64 G128 G256

dept

h

entries per GPT node

Page 34: Microker nel Constr uction III Vi rt ual Memory

GPT space

0

10

20

30

40

50

60

70

80

G2 G4 G8 G16 G32 G64 G128 G256

byte

s pe

r PT

E

page table

Page 35: Microker nel Constr uction III Vi rt ual Memory

GPT ver sus other page tables

0

50

100

150

200

250

300

350

MPT G16 H8k H128k C128k

byte

s pe

r P

TE

Page 36: Microker nel Constr uction III Vi rt ual Memory

Address space establishment/teardown cost

0

1000

2000

3000

4000

5000

MPT C128k H128k H8k G16 G16S8k/ G16

S128k/

µsec

0

50

100

150

200

H8k G16 G16S8k/ G16

S128k/

Page 37: Microker nel Constr uction III Vi rt ual Memory

Other benchmarks

• spar se benchmar k: unifor mly distr ibuted pages

• file benchmar k: unifor mly distr ibuted objects

0

200

400

600

800

1000

G2 G4 G8 G16 G32 G64 G128 G256

byte

s pe

r P

TE

worstuniform

fileconv

0

20

40

60

80

100

G2 G4 G8 G16

Page 38: Microker nel Constr uction III Vi rt ual Memory

GPT conclusions

• low esta blishment/teardown cost

• small GPT node size saves space, especially for sparse

distr ibutions

• tree depth can become a problem, especially for dense

distr ibutions

L4/MIPS solution: use GPT with a software TLB.

Page 39: Microker nel Constr uction III Vi rt ual Memory

Level compression

Density creates a lot of complete subtrees in MLPT and GPT.

complete subtree

levelcompression

Idea: compress complete subtrees .

Page 40: Microker nel Constr uction III Vi rt ual Memory

Variable radix page table (VRPT)

Page table applying both level compression and path

compression.

pagetableroot

framenumber

equal?

virtual address

page offset

All guard compar isons deferred until a leaf is reached.

Page 41: Microker nel Constr uction III Vi rt ual Memory

VRPT structure

• page table size may be any power of two

• best of both wor lds: shallow and space-efficient

• chea p to create, delete, and traver se

page table root

virtual address space

Leaves may occur at any level of the page table.

Page 42: Microker nel Constr uction III Vi rt ual Memory

VRPT implementation

Each node:

• skips a var ia ble number of bits (called skip)

• indexes with a var ia ble number of bits (called size)

ignored index

virtual address

skip size

In software, each indexing step takes two SHIFTs and an OR.

Hardware could use a bitfield extraction unit.

Page 43: Microker nel Constr uction III Vi rt ual Memory

VRPT vs. GPT

Elphinstone (1999) derived the following GPT algorithm.

repeat {

u = v >> (vlen − s)

g = ( p + 32u)→guard

glen = ( p + 32u)→guardlen

if g == (v >> (vlen − s − glen)) and (2glen − 1) {

v’len = vlen − s − glen

v’ = v and (2glen)

s’ = ( p + 32u)→size’

p’ = ( p + 32u)→table’

} else

page fault

} until p is a leaf

Page 44: Microker nel Constr uction III Vi rt ual Memory

VRPT vs. GPT

After common subexpression elimination, the GPT loop has 17

ar ithmetic and load operations .

VRPT is much simpler.

repeat {

p = & p→ptr[v << p→skip >> p→size]

} until p is a leaf

if p→virt ≠ v

page fault

All guard checking is deferred until the end.

The inner loop on the MIPS R4000 requires only 7 instructions .

Page 45: Microker nel Constr uction III Vi rt ual Memory

VRPT implementation

Problem: how to choose radix at each level?

→ Can complex restr uctur ing algor ithms be avoided?

Page 46: Microker nel Constr uction III Vi rt ual Memory

VRPT implementation

Problem: how to choose radix at each level?

→ Can complex restr uctur ing algor ithms be avoided?

In VRPT:

• greedy strategy allocates large tables

• unused space may be reclaimed at any time, and the

retur ned to the memory manager

• power of two regions are managed by a buddy system

allocator

Page table structure converges to best time–space tradeoff.

Page 47: Microker nel Constr uction III Vi rt ual Memory

VRPT perfor mance

Benchmar ks: selected from SPEC CPU95 and CPU2000.

Page tables:

3LPT three-level page table (43-bit only)

HPT hashed page table

CPT hashed page table with clustered entries

GPT guarded page table

CACHE guarded page table with HPT-style cache

VRPT var ia ble radix page table

Pla tfor ms:

IA-64 IDT MIPS R4700 running L4/MIPS

MIPS64 Intel Itanium running a custom ker nel

Page 48: Microker nel Constr uction III Vi rt ual Memory

Methodology

Compare execution times with paging on and off.

• counts direct and indirect costs of VM

→ cost of exceptions

→ TLB refill handler

→ cache pollution

• expressed as per cent overhead

Page 49: Microker nel Constr uction III Vi rt ual Memory

VRPT perfor mance (MIPS64)

TLB overhead

0% 5% 10% 15% 20% 25% 30% 35% 40% 45%

wave5

HPTCPT

GPTCACHE

VRPT

swim

HPTCPT

GPTCACHE

VRPT

gcc

HPTCPT

GPTCACHE

VRPT

compress

HPTCPT

GPTCACHE

VRPT

Page 50: Microker nel Constr uction III Vi rt ual Memory

VRPT perfor mance (IA-64)

TLB overhead

0% 10% 20% 30% 40% 50% 60% 70% 80% 90%

gzipHPT

3LPTVRPT

wupwiseHPT

3LPTVRPT

swimHPT

3LPTVRPT

mgridHPT

3LPTVRPT

appluHPT

3LPTVRPT

vprHPT

3LPTVRPT

gccHPT

3LPTVRPT

Page 51: Microker nel Constr uction III Vi rt ual Memory

Super pages

Suppor ted by most modern machines .

machine ITLB DTLB page sizes

StrongARM 32 32 4k, 64k, 1M

Pentium III 32 64 4k, 4M

4k, 8k, 16k, 64k, 256k, 1M,

4M, 16M, 64M, 256M, 4GItanium 64 96

Alpha 21264 128 128 8k, 64k, 512k, 4M

UltraSPARC 64 8k, 64k, 512k, 4M

MIPS R4000 96 4k, 16k, 64k, 256k, 1M, 4M, 16M

Po w erPC 601 256 4k

Page 52: Microker nel Constr uction III Vi rt ual Memory

Super pages

OS may use superpages in a var iety of ways .

• globally increase page size (easiest)

• page size per process or segment

• arbitrar y page size mixtures (hardest)

Navarro et al. [2002] achieved significant speedup with mixed

page sizes.

→ ‘‘perfor mance benefit often exceeds 30%’’

Page 53: Microker nel Constr uction III Vi rt ual Memory

Super page representa tion in the page table

In many PTs it is difficult to accommodate mixed page sizes.

• HPT cannot support super pages: PTEs must be replicated

• MLPT supports a limited number of superpages

pagetableroot

basepage

super-page

page offset

Other page sizes must be replicated.

Page 54: Microker nel Constr uction III Vi rt ual Memory

Super page factor ization in VRPT

VRPT accommodates mixed page sizes using PTE factor ization.

16k 16k

4k 4k 4k 4k 4k 4k

Problem: fragmentation if too many sizes . Solution: key expansion.

expanded keys

4k 4k 4k 4k 4k 4k 16k 16k

Expansion and factor ization can be applied flexibly as desired.

Page 55: Microker nel Constr uction III Vi rt ual Memory

Super page perfor mance (MIPS64)

TLB overhead

0% 2% 4% 6% 8% 10%

wave54k only

4k, 16k4k-64k

4k-256k4k-1M

swim4k only

4k, 16k4k-64k

4k-256k4k-1M

gcc4k only

4k, 16k4k-64k

4k-256k4k-1M

compress4k only

4k, 16k4k-64k

4k-256k4k-1M

Page 56: Microker nel Constr uction III Vi rt ual Memory

Super page perfor mance (IA-64)

TLB overhead

0% 10% 20% 30% 40% 50% 60%

gzip 8k onlyall sizes

wupwise 8k onlyall sizes

swim 8k onlyall sizes

mgrid 8k onlyall sizes

applu 8k onlyall sizes

vpr 8k onlyall sizes

gcc 8k onlyall sizes

Page 57: Microker nel Constr uction III Vi rt ual Memory

Shar ing in VRPT

VRPT can use cross-linked page tables to share segments .

shared

page

tables

Technique is also availa ble with MLPT, but less flexible.

Page 58: Microker nel Constr uction III Vi rt ual Memory

Shar ing in VRPT

Advantages:

• Updating a shared segment is easier.

• Shar ing is explicit: easy to apply shared TLB tags.

• Each page doesn’t have to be mapped to each space

individually.

→ reduce minor page faults

• A shared segment can have different protection

attr ibutes in different address spaces.

Perfor mance evaluation: future wor k.

Page 59: Microker nel Constr uction III Vi rt ual Memory

VRPT conclusions

• VRPT is competitive with other page tables .

• VRPT is competitive with a memory-based TLB cache.

• VRPT may be suitable for hardware implementation.

• Super pages show dramatic perfor mance improv ement.

VRPT is currently being implemented in the Pistachio L4 ker nel in

a VM subsystem called Calypso.

Page 60: Microker nel Constr uction III Vi rt ual Memory

Microker nels and SASOSes

Some microker nel and SASOS features exacerbate VM load.

• ser vice decomposition

→ frequent address space switches

→ reduces locality

→ kernel consumption of TLB entries

• spar se address space layout

• shared memory

→ increases fragmentation

→ reduces locality

→ requires more TLB entries to cover physical memory

VM perfor mance is critical to the success of microker nel and

SASOS systems.

Page 61: Microker nel Constr uction III Vi rt ual Memory

Implementa tion in L4

L4 provides three operations: ma p, grant, and unma p.

A

B C

D E

L4 must remember the history of ma p operations in the

ma pping da ta base, to allow future undo with unma p.

Memor y management is the responsibility of user-level pager s .

Page 62: Microker nel Constr uction III Vi rt ual Memory

L4 data structures

L4 implementation of recur sive address spaces uses roughly the

following data structures .

guardedpagetable

frametable

mappingdatabase

Direct pointers between GPT and mapping database (green

arrow) were considered by Elphinstone, but rejected to allow PT

implementation freedom.

Page 63: Microker nel Constr uction III Vi rt ual Memory

Calypso data structures

• inter nal nodes store two shift amounts and a pointer

m f prot size skip

ptr

• PTE stores virtual address (and other goodies)

m f gen task hard size skip

ptr

m 0 phys c w v g

virt

Each PTE may represent any (hard) page size.

Page tables may be shared (with an addition to the L4 API).

Page 64: Microker nel Constr uction III Vi rt ual Memory

Calypso mapping data base

Topologically sort each mapping graph into a singly-linked list.

σ 0

A2

B1

D2

B4

D4

E2 C2

E3 E4

σ 0

1

A22

B13

D24

B43

D44

E23

C23

E34

E44

Integrate the mapping database list into the PTEs.

mapping database

page table

Page 65: Microker nel Constr uction III Vi rt ual Memory

Shar ing in L4

Difficult to cross-link page tables and use hardware shar ing

suppor t.

• L4’s ma p pr imitive can only map to one AS at a time.

• Calypso needs an API extension for sharing support.

Extensions to the L4 API must be designed with care.

• shouldn’ t slow down critical functions (IPC and TLB refill)

• shouldn’ t introduce policy into the ker nel

• should match the ‘L4 philosophy’

Page 66: Microker nel Constr uction III Vi rt ual Memory

Link

Exper imental new VM operation: link, which establishes a

shared domain between pager and pagee.

Link is like ma p, except that future maps and unmaps in that

par t of the pager’s address space are automatically seen by

the pagee.

Best illustrated by an analogy.

L4 primitive Unix analogy

unmap rm

ma p cp

grant mv

link ln -s

Page 67: Microker nel Constr uction III Vi rt ual Memory

More on link

Restr ictions:

• vir tual address in pager and pagee must be equal

• fpage size may be restr icted

Advantages:

• natural generalization of ma p and grant

• reduces ker nel crossings

• reduces page fault IPC

• restr icted by L4’s usual IPC confinement model (e.g. clans

and chiefs)

Page 68: Microker nel Constr uction III Vi rt ual Memory

Conclusions

• VM overhead is a critical perfor mance issue in modern

hardware and operating systems.

• Conventional page tables don’t perfor m well in these

conditions .

• Software TLB (or hardware TLB cache) is the best solution

to a slow page table.

• VRPT can perfor m as well as software TLB.

– Fur ther perfor mance evaluation required.

• Optimization of the critical path pays off.

– but only after evaluation and measurement.

Page 69: Microker nel Constr uction III Vi rt ual Memory

References and further infor mation

http://www.cse.unsw.edu.au/˜cls/