the benefits and costs of writing a posix kernel in …...the benefits and costs of writing a posix...

58
The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT CSAIL 1 / 38

Upload: others

Post on 12-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

The benefits and costs of writing a POSIXkernel in a high-level language

Cody Cutler, M. Frans Kaashoek, Robert T. Morris

MIT CSAIL

1 / 38

Page 2: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Should we use high-level languages to buildOS kernels?

2 / 38

Page 3: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

HLL Benefits

Easier to programSimpler concurrency with GCPrevents classes of kernel bugs

3 / 38

Page 4: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Kernel memory safety matters

Inspected Linux kernel execute code CVEs for 2017

40 CVEs due to just memory-safety bugs

HLL would have prevented code execution

4 / 38

Page 5: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Kernel memory safety matters

Inspected Linux kernel execute code CVEs for 2017

40 CVEs due to just memory-safety bugs

HLL would have prevented code execution

4 / 38

Page 6: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

HLL downside: safety costs performance

Bounds, cast, nil-pointer checksReflectionGarbage collection

5 / 38

Page 7: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Goal: measure HLL impact

Pros:Reduction of bugsSimpler code

Cons:HLL safety taxGC CPU and memory overheadGC pause times

6 / 38

Page 8: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Methodology

Build new HLL kernel, compare with Linux

Isolate HLL impact:

Same apps, POSIX interface, and monolithic organization

7 / 38

Page 9: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Previous work

Taos(ASPLOS’87), Spin(SOSP’95), Singularity(SOSP’07),Tock(SOSP’17), J-kernel(ATC’98), KaffeOS(ATC’00),House(ICFP’05),...

Explore new ideasDifferent architectures

Several studies of HLL versus C for user programsKernels different from user programs

None measure HLL impact in a monolithic POSIX kernel

8 / 38

Page 10: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Previous work

Taos(ASPLOS’87), Spin(SOSP’95), Singularity(SOSP’07),Tock(SOSP’17), J-kernel(ATC’98), KaffeOS(ATC’00),House(ICFP’05),...

Explore new ideasDifferent architectures

Several studies of HLL versus C for user programsKernels different from user programs

None measure HLL impact in a monolithic POSIX kernel

8 / 38

Page 11: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Contributions

BISCUIT, new x86-64 Go kernelRuns unmodified Linux applicationswith good performance

Measurements of HLL costs for NGINX, Redis, and CMailbench

Description of qualitative ways HLL helped

New scheme to deal with heap exhaustion

9 / 38

Page 12: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Which HLL?

Go is a good choice:Easy to call asmCompiled to machine code w/good compilerEasy concurrencyEasy static analysisGC

10 / 38

Page 13: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Go’s GC

Concurrent mark and sweep

Stop-the-world pauses of 10s of µs

11 / 38

Page 14: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

BISCUIT overview

58 syscalls, LOC: 28k Go,1.5k assembly (boot, entry/exit)

12 / 38

Page 15: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Features

MulticoreThreadsJournaled FS (7k LOC)Virtual memory (2k LOC)TCP/IP stack (5k LOC)Drivers: AHCI and Intel 10G NIC (3k LOC)

13 / 38

Page 16: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

No fundamental challenges due to HLL

But many implementation puzzlesInterruptsKernel threads are lightweightRuntime on bare-metal...

Surprising puzzle: heap exhaustion

14 / 38

Page 17: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

No fundamental challenges due to HLL

But many implementation puzzlesInterruptsKernel threads are lightweightRuntime on bare-metal...

Surprising puzzle: heap exhaustion

14 / 38

Page 18: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Puzzle: Heap exhaustion

15 / 38

Page 19: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Puzzle: Heap exhaustion

15 / 38

Page 20: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Puzzle: Heap exhaustion

15 / 38

Page 21: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Puzzle: Heap exhaustion

15 / 38

Page 22: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Puzzle: Heap exhaustion

Can’t allocate heap memory =⇒ nothing worksAll kernels face this problem

15 / 38

Page 23: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

How to recover?

Strawman 1: Wait for memory in allocator?

May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

Difficult to get rightCan’t! Go doesn’t expose failed allocationsand implicitly allocates

Both cause problems for Linux; see “too small to fail” rule

16 / 38

Page 24: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

How to recover?

Strawman 1: Wait for memory in allocator?

May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

Difficult to get rightCan’t! Go doesn’t expose failed allocationsand implicitly allocates

Both cause problems for Linux; see “too small to fail” rule

16 / 38

Page 25: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

How to recover?

Strawman 1: Wait for memory in allocator?

May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

Difficult to get rightCan’t! Go doesn’t expose failed allocationsand implicitly allocates

Both cause problems for Linux; see “too small to fail” rule

16 / 38

Page 26: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

How to recover?

Strawman 1: Wait for memory in allocator?

May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

Difficult to get right

Can’t! Go doesn’t expose failed allocationsand implicitly allocates

Both cause problems for Linux; see “too small to fail” rule

16 / 38

Page 27: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

How to recover?

Strawman 1: Wait for memory in allocator?

May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

Difficult to get rightCan’t! Go doesn’t expose failed allocationsand implicitly allocates

Both cause problems for Linux; see “too small to fail” rule

16 / 38

Page 28: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

BISCUIT solution: reserve memory

To execute syscall...

17 / 38

Page 29: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

BISCUIT solution: reserve memory

To execute syscall...

17 / 38

Page 30: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

BISCUIT solution: reserve memory

To execute syscall...

17 / 38

Page 31: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

BISCUIT solution: reserve memory

To execute syscall...

17 / 38

Page 32: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

BISCUIT solution: reserve memory

To execute syscall...

17 / 38

Page 33: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

BISCUIT solution: reserve memory

To execute syscall...

No checks, no error handling code, no deadlock

17 / 38

Page 34: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Reservations

HLL easy to analyze

Tool computes reservation via escape analysisUsing Go’s static analysis packages

≈ three days of expert effort to apply tool

18 / 38

Page 35: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Building BISCUIT was similar to other kernels

BISCUIT adopted many Linux optimizations:large pages for kernel textper-CPU NIC transmit queuesRCU-like directory cacheconcurrent FS transactionspad structs to remove false sharing

Good OS performance more about optimizations, less about HLL

19 / 38

Page 36: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Building BISCUIT was similar to other kernels

BISCUIT adopted many Linux optimizations:large pages for kernel textper-CPU NIC transmit queuesRCU-like directory cacheconcurrent FS transactionspad structs to remove false sharing

Good OS performance more about optimizations, less about HLL

19 / 38

Page 37: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Eval questions

Should we use high-level languages to build OS kernels?

1 Did BISCUIT benefit from HLL features?2 Is BISCUIT performance in the same league as Linux?3 What is the breakdown of HLL tax?4 What is the performance cost of Go compared to C?

More experiments in paper

20 / 38

Page 38: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

1: Qualitative benefits of HLL features

Simpler code with:

GC’ed allocationdefer

multi-valued returnclosuresmaps

21 / 38

Page 39: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

HLL example benefits

Example 1: Memory safety

Example 2: Simpler concurrency

22 / 38

Page 40: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

1: BISCUIT benefits from memory safety

Inspected fixes for all publicly-available execute code CVEs inLinux kernel for 2017

Category # Outcome in Go

— 11 unknownlogic 14 sameuse-after-free/double-free 8 disappear due to GCout-of-bounds 32 panic or disappear due to GC

panic likely better than malicious code execution

23 / 38

Page 41: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

1: BISCUIT benefits from simpler concurrency

Generally, concurrency with GC simpler

Particularly, GC greatly simplifies read-lock-free data structures

Challenge: In C, how to determine when last reader is done?

Main purpose of read-copy update (RCU) (PDCS’98)Linux uses RCU, but it’s not easy

Code to start and end RCU sectionsNo sleeping/scheduling in RCU sections...

In Go, no extra code — GC takes care of it

24 / 38

Page 42: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Experimental setup

Hardware:4 core 2.8Ghz Xeon-X346016 GB RAMHyperthreads disabled

Eval application:

NGINX (1.11.5) – webserverRedis (3.0.5) – key/value storeCMailbench – mail-server benchmark

25 / 38

Page 43: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Applications are kernel intensive

No idle time

79%-92% kernel time

In-memory FS

Run for a minute

512MB heap RAM for BISCUIT

26 / 38

Page 44: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

2: Is BISCUIT perf in the same league as Linux?

Debian 9.4, Linux 4.9.82

Disabled expensive features:

page-table isolationretpolinekernel address space layout randomizationtransparent huge-pages...

27 / 38

Page 45: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

2: Biscuit is in the same league

BISCUIT ops/s Linux ops/s Ratio

CMailbench (mem) 15,862 17,034 1.07NGINX 88,592 94,492 1.07Redis 711,792 775,317 1.09

28 / 38

Page 46: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

2: Biscuit is in the same league

BISCUIT ops/s Linux ops/s Ratio

CMailbench (mem) 15,862 17,034 1.07NGINX 88,592 94,492 1.07Redis 711,792 775,317 1.09

28 / 38

Page 47: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

HLL cost unclear from comparison

May understate Linux performance due to features:NUMA awarenessOptimizations for large number of cores (>4)...

Focus on HLL costs:

Measure CPU cycles BISCUIT pays for HLL taxCompare code paths that differ only by language

29 / 38

Page 48: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

3: What is the breakdown of HLL tax?

Measure HLL tax:GC cyclesPrologue cyclesWrite barrier cyclesSafety cycles

30 / 38

Page 49: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

3: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

31 / 38

Page 50: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

3: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

31 / 38

Page 51: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

3: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

31 / 38

Page 52: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

3: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

31 / 38

Page 53: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

3: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

Benchmarks allocate kernel heap rapidlybut have little persistent kernel heap data

Cycles used by GC increase with size of live kernel heapDedicate 2 or 3× memory⇒ low GC cycles

31 / 38

Page 54: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

4: What is the cost of Go compared to C?

Make code paths same in BISCUIT and Linux

Two code paths in paperpipe ping-pong (systems calls, context switching)page-fault handler (exceptions, VM)

Focus on pipe ping-pong:LOC: 1.2k Go, 1.8k CNo allocation; no GCTop-10 most expensive instructions match

32 / 38

Page 55: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

4: C is 15% faster

C Go(ops/s) (ops/s) Ratio

536,193 465,811 1.15

Prologue/safety-checks⇒ 16% more instructions

33 / 38

Page 56: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Should one use HLL for a new kernel?

The HLL worked well for kernel development

Performance is paramount⇒ use C (up to 15%)

Minimize memory use⇒ use C (↓ mem. budget, ↑ GC cost)

Safety is paramount⇒ use HLL (40 CVEs stopped)

Performance merely important⇒ use HLL (pay 15%, memory)

34 / 38

Page 57: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

Questions?

The HLL worked well for kernel development

Performance is paramount⇒ use C (up to 15%)

Minimize memory use⇒ use C (↓ mem. budget, ↑ GC cost)

Safety is paramount⇒ use HLL (40 CVEs stopped)

Performance merely important⇒ use HLL (pay 15%, memory)

git clone https://github.com/mit-pdos/biscuit.git 35 / 38

Page 58: The benefits and costs of writing a POSIX kernel in …...The benefits and costs of writing a POSIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris

36 / 38