An Evolutionary Study of Linux Memory Management for Fun and Profit - - PowerPoint PPT Presentation

an evolutionary study of linux memory management for fun
SMART_READER_LITE
LIVE PREVIEW

An Evolutionary Study of Linux Memory Management for Fun and Profit - - PowerPoint PPT Presentation

An Evolutionary Study of Linux Memory Management for Fun and Profit Jian Huang Karsten Schwan Moinuddin K. Qureshi Virtual Memory: A Long History Physical Hardware DRAM Disk 2 Virtual Memory: A Long History Virtual Memory (per process)


slide-1
SLIDE 1

An Evolutionary Study of Linux Memory Management for Fun and Profit

Jian Huang Moinuddin K. Qureshi Karsten Schwan

slide-2
SLIDE 2

Virtual Memory: A Long History

2

DRAM Disk

Physical Hardware

slide-3
SLIDE 3

Virtual Memory: A Long History

Virtual Memory (per process)

2

DRAM Disk

Physical Hardware

slide-4
SLIDE 4

Virtual Memory: A Long History

Virtual Memory (per process)

2

DRAM Disk

Physical Hardware

slide-5
SLIDE 5

Virtual Memory: A Long History

Virtual Memory (per process)

2

DRAM Disk

Physical Hardware

Development OS Core Component+

slide-6
SLIDE 6

Virtual Memory: A Long History

Virtual Memory (per process)

2

DRAM Disk

Physical Hardware

Pervasively Used Development OS Core Component+

slide-7
SLIDE 7

Why Memory Manager Study Matters?

3

Features & Functions

slide-8
SLIDE 8

Why Memory Manager Study Matters?

3

Features & Functions Hardware Support

slide-9
SLIDE 9

Why Memory Manager Study Matters?

3

Features & Functions Hardware Support System Reliability

slide-10
SLIDE 10

Why Memory Manager Study Matters?

3

Features & Functions Hardware Support System Reliability Study on Memory Manager

slide-11
SLIDE 11

Why Memory Manager Study Matters?

3

Features & Functions Hardware Support System Reliability Study on Memory Manager Building Better Memory Manager

slide-12
SLIDE 12

On the Study of Memory Management

4

Understanding the Linux Virtual Memory Manager

[Mel Gorman, July 9, 2007]

slide-13
SLIDE 13

On the Study of Memory Management

4

Understanding the Linux Virtual Memory Manager

[Mel Gorman, July 9, 2007]

Approach: Source code analysis, Linux 2.4, 2.6

slide-14
SLIDE 14

Milestone

On the Study of Memory Management

4

Understanding the Linux Virtual Memory Manager

[Mel Gorman, July 9, 2007]

Approach: Source code analysis, Linux 2.4, 2.6

slide-15
SLIDE 15

Milestone

On the Study of Memory Management

4

Understanding the Linux Virtual Memory Manager

[Mel Gorman, July 9, 2007]

Approach: Source code analysis, Linux 2.4, 2.6 Our Focus: Patch study, Linux 2.6 – 4.0

slide-16
SLIDE 16

Milestone

On the Study of Memory Management

4

Understanding the Linux Virtual Memory Manager

[Mel Gorman, July 9, 2007]

Approach: Source code analysis, Linux 2.4, 2.6 Our Focus: Patch study, Linux 2.6 – 4.0 Pattern Memory Bug

+

Optimization

+

Semantic

+

slide-17
SLIDE 17

Preview of Our Findings

5

  • Code changes are highly concentrated around the key functions
  • 80% of patches  25% of its source code
  • ......
slide-18
SLIDE 18

Preview of Our Findings

5

  • Code changes are highly concentrated around the key functions
  • 80% of patches  25% of its source code
  • ......
  • Memory error – Checking – Concurrency – Logic – Programming
  • Memory errors: Null pointer & page alignment
  • Complex page states  Checking & logic bugs
  • ......
slide-19
SLIDE 19

Preview of Our Findings

5

  • Code changes are highly concentrated around the key functions
  • 80% of patches  25% of its source code
  • ......
  • Memory error – Checking – Concurrency – Logic – Programming
  • Memory errors: Null pointer & page alignment
  • Complex page states  Checking & logic bugs
  • ......
  • Data structures -- Policy trade-off -- Fast path
  • 4 data structures, 5 design trade-offs, 8 types of fast paths
  • ......
slide-20
SLIDE 20

Preview of Our Findings

5

  • Code changes are highly concentrated around the key functions
  • 80% of patches  25% of its source code
  • ......
  • Memory error – Checking – Concurrency – Logic – Programming
  • Memory errors: Null pointer & page alignment
  • Complex page states  Checking & logic bugs
  • ......
  • Data structures -- Policy trade-off -- Fast path
  • 4 data structures, 5 design trade-offs, 8 types of fast paths
  • ......
  • 35 key functionalities in 13 hot files
  • The well-developed memory allocators still have many checking & lock bugs
  • ......
slide-21
SLIDE 21

Methodology Used in Our Study

6

Memory Allocation Virtual Memory Management Resource Controller Garbage Collection Swapping Page Cache & Write-back Exception Handling Misc (e.g., data structure)

8 components

slide-22
SLIDE 22

Methodology Used in Our Study

6

Memory Allocation Virtual Memory Management Resource Controller Garbage Collection Swapping Page Cache & Write-back Exception Handling Misc (e.g., data structure)

8 components 4587 patches in 5 years

slide-23
SLIDE 23

Methodology Used in Our Study

6

Patches

Description Follow-up Discussions Source Code Analysis

slide-24
SLIDE 24

Methodology Used in Our Study

6

Patches

Description Follow-up Discussions Source Code Analysis BugID Commit Time Component Type Causes Involved Functions

……

MPatch

Labeling & MChecker

slide-25
SLIDE 25

How Is the Memory Manager Changed?

7

10 20 30 40 50 60 70 80 2.6.32 (2009) 2.6.33 (2010) 2.6.38 (2011) 3.2 (2012) 3.10 (2013) 3.14 (2014) 4.0-rc4 (2015) Lines of Code (x1000) Linux version (released year)

slide-26
SLIDE 26

How Is the Memory Manager Changed?

7

10 20 30 40 50 60 70 80 2.6.32 (2009) 2.6.33 (2010) 2.6.38 (2011) 3.2 (2012) 3.10 (2013) 3.14 (2014) 4.0-rc4 (2015) Lines of Code (x1000) Linux version (released year)

The LoC has increased by 60% since Linux 2.6.32.

slide-27
SLIDE 27

Where Is the Memory Manager Changing?

8

slide-28
SLIDE 28

Where Is the Memory Manager Changing?

8

Memory Manager Components

slide-29
SLIDE 29

Where Is the Memory Manager Changing?

8

Linux Version

slide-30
SLIDE 30

Where Is the Memory Manager Changing?

8

Number of Committed Patches

slide-31
SLIDE 31

Where Is the Memory Manager Changing?

8

slide-32
SLIDE 32

Where Is the Memory Manager Changing?

8

80% of the code changes  25% of the source code

slide-33
SLIDE 33

Where Is the Memory Manager Changing?

8

slide-34
SLIDE 34

Where Is the Memory Manager Changing?

8

slide-35
SLIDE 35

Where Is the Memory Manager Changing?

8

slide-36
SLIDE 36

Where Is the Memory Manager Changing?

8

slide-37
SLIDE 37

Where Is the Memory Manager Changing?

8

13 hot files from 90 files  recent development focus

slide-38
SLIDE 38

10 20 30 40 50 60 2.6.33 (2010) 2.6.38 (2011) 3.2 (2012) 3.10 (2013) 3.14 (2014) 4.0-rc4 (2015) Percentage (%) Linux version (released year) Bug Code Maintenance Optimization New Feature

Why Is the Memory Manager Changed?

9

slide-39
SLIDE 39

10 20 30 40 50 60 2.6.33 (2010) 2.6.38 (2011) 3.2 (2012) 3.10 (2013) 3.14 (2014) 4.0-rc4 (2015) Percentage (%) Linux version (released year) Code Maintenance New Feature

Why Is the Memory Manager Changed?

9

slide-40
SLIDE 40

10 20 30 40 50 60 2.6.33 (2010) 2.6.38 (2011) 3.2 (2012) 3.10 (2013) 3.14 (2014) 4.0-rc4 (2015) Percentage (%) Linux version (released year) Bug Optimization

Why Is the Memory Manager Changed?

9

70% more bugs in well-developed memory manager!

slide-41
SLIDE 41

On the Bugs in Memory Manager

10

slide-42
SLIDE 42

On the Bugs in Memory Manager

10

Types of Memory Bugs

slide-43
SLIDE 43

On the Bugs in Memory Manager

10

Memory Manager Component

slide-44
SLIDE 44

On the Bugs in Memory Manager

10

Memory Allocation: 26%, Virtual Memory Management: 22%, GC: 14%

slide-45
SLIDE 45

On the Bugs in Memory Manager

10

slide-46
SLIDE 46

On the Bugs in Memory Manager

10

  • Page alignment
  • Null pointer
slide-47
SLIDE 47

On the Bugs in Memory Manager

10

  • Page alignment
  • Null pointer
  • Inappropriate check
  • Missing check
  • Wrong check
slide-48
SLIDE 48

On the Bugs in Memory Manager

10

  • Page alignment
  • Null pointer
  • Inappropriate check
  • Missing check
  • Wrong check
  • Lock contention
  • Missing lock
slide-49
SLIDE 49

On the Bugs in Memory Manager

10

  • Page alignment
  • Null pointer
  • Inappropriate check
  • Missing check
  • Wrong check
  • Lock contention
  • Missing lock
  • Fault handler
  • State update
  • Case-by-case
slide-50
SLIDE 50

On the Bugs in Memory Manager

10

  • Page alignment
  • Null pointer
  • Inappropriate check
  • Missing check
  • Wrong check
  • Lock contention
  • Missing lock
  • Fault handler
  • State update
  • Case-by-case
  • API
  • Misc
slide-51
SLIDE 51

Memory Bugs: Case Studies

11

Page Alignment

mm/nommu.c @@ -1762,6 +1765,8 @@ unsigned long do_mremap(unsigned long addr, struct vm_area_struct *vma; /* insanity checks first */ if (old_len == 0 || new_len == 0) return (unsigned long) -EINVAL;

slide-52
SLIDE 52

Memory Bugs: Case Studies

11

Page Alignment

mm/nommu.c @@ -1762,6 +1765,8 @@ unsigned long do_mremap(unsigned long addr, struct vm_area_struct *vma; /* insanity checks first */ if (old_len == 0 || new_len == 0) return (unsigned long) -EINVAL;

Bug: device drivers’ mmap() failed. Cause: NOMMU does not do page_align(), which is inconsistent with MMU arch.

slide-53
SLIDE 53

Memory Bugs: Case Studies

11

Page Alignment

mm/nommu.c @@ -1762,6 +1765,8 @@ unsigned long do_mremap(unsigned long addr, struct vm_area_struct *vma; /* insanity checks first */ +

  • ld_len = PAGE_ALIGN(old_len);

+ new_len = PAGE_ALIGN(new_len); if (old_len == 0 || new_len == 0) return (unsigned long) -EINVAL;

Bug: device drivers’ mmap() failed. Cause: NOMMU does not do page_align(), which is inconsistent with MMU arch.

slide-54
SLIDE 54

Memory Bugs: Case Studies

12

Checking

mm/bootmem.c @@ -156,21 +157,31 @@ static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,

slide-55
SLIDE 55

Memory Bugs: Case Studies

12

Checking

mm/bootmem.c @@ -156,21 +157,31 @@ static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,

Bug: free pages wrongly. Cause: miss boundary checking.

slide-56
SLIDE 56

Memory Bugs: Case Studies

12

Checking

mm/bootmem.c @@ -156,21 +157,31 @@ static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr, + BUG_ON(!size); + + /* out range */ + if (addr + size < bdata->node_boot_start || + PFN_DOWN(addr) > bdata->node_low_pfn) + return;

Bug: free pages wrongly. Cause: miss boundary checking.

slide-57
SLIDE 57

Memory Optimizations

13

Radix Tree Red-black Tree Bitmap List Data Structures

4

slide-58
SLIDE 58

Memory Optimizations

13

Radix Tree Red-black Tree Bitmap List Data Structures

Decentralize data structures: per-core/per-node/per-device approaches.

4

slide-59
SLIDE 59

Memory Optimizations

13

Radix Tree Red-black Tree Bitmap List Data Structures

4 5

Policy Trade-offs Latency Vs. Throughput Synchronous Vs. Asynchronous Lazy Vs. Non-lazy Local Vs. Global Fairness Vs. Performance

slide-60
SLIDE 60

Memory Optimizations

13

Radix Tree Red-black Tree Bitmap List Data Structures

4 5

Policy Trade-offs Latency Vs. Throughput Synchronous Vs. Asynchronous Lazy Vs. Non-lazy Local Vs. Global Fairness Vs. Performance

8

Fast Paths Code Reduction Lockless Optimization Inline Code Shifting New Function State Caching Group Execution Optimistic Barrier

slide-61
SLIDE 61

Memory Optimizations: Policy Trade-offs

14

33% 22% 18% 16% 11% Latency Vs. Throughput Fairness Vs. Performance Lazy Vs. Non-lazy Synchronous Vs. Asynchronous Local Vs. Global

slide-62
SLIDE 62

Memory Optimizations: Policy Trade-offs

14

33% 22% 18% 16% 11% Latency Vs. Throughput Fairness Vs. Performance Lazy Vs. Non-lazy Synchronous Vs. Asynchronous Local Vs. Global

137 patches committed especially for reducing the latencies of memory operations.

slide-63
SLIDE 63

Memory Optimizations: Policy Trade-offs

14

33% 22% 18% 16% 11% Latency Vs. Throughput Fairness Vs. Performance Lazy Vs. Non-lazy Synchronous Vs. Asynchronous Local Vs. Global

Lazy policy: delay expensive operations. May change the execution order of functions.

slide-64
SLIDE 64

Memory Optimizations: Policy Trade-offs

14

33% 22% 18% 16% 11% Latency Vs. Throughput Fairness Vs. Performance Lazy Vs. Non-lazy Synchronous Vs. Asynchronous Local Vs. Global

Lazy policy: delay expensive operations. May change the execution order of functions.

vmalloc Lazy TLB flush, lazy unmapping mempolicy Lazy page migration between nodes huge_memory Lazy huge zero page allocation

slide-65
SLIDE 65

Memory Optimizations: Policy Trade-offs

14

33% 22% 18% 16% 11% Latency Vs. Throughput Fairness Vs. Performance Lazy Vs. Non-lazy Synchronous Vs. Asynchronous Local Vs. Global

Mostly considered in memory allocation & GC

slide-66
SLIDE 66

Memory Optimizations: Policy Trade-offs

14

33% 22% 18% 16% 11% Latency Vs. Throughput Fairness Vs. Performance Lazy Vs. Non-lazy Synchronous Vs. Asynchronous Local Vs. Global

Async is popular, but be careful to its fault handlers!

slide-67
SLIDE 67

Memory Optimizations: Policy Trade-offs

14

33% 22% 18% 16% 11% Latency Vs. Throughput Fairness Vs. Performance Lazy Vs. Non-lazy Synchronous Vs. Asynchronous Local Vs. Global

Async is popular, but be careful to its fault handlers!

E.g., early termination

slide-68
SLIDE 68

Memory Optimizations: Policy Trade-offs

14

33% 22% 18% 16% 11% Latency Vs. Throughput Fairness Vs. Performance Lazy Vs. Non-lazy Synchronous Vs. Asynchronous Local Vs. Global

Decentralizing global structures for better scalability

slide-69
SLIDE 69

Memory Optimizations: Policy Trade-offs

14

33% 22% 18% 16% 11% Latency Vs. Throughput Fairness Vs. Performance Lazy Vs. Non-lazy Synchronous Vs. Asynchronous Local Vs. Global

Decentralizing global structures for better scalability

E.g., dynamic per-cpu allocator.

slide-70
SLIDE 70

Memory Optimizations: Fast Path

15

4% 4% 5% 6% 8% 12% 27% 34%

Code Reduction New Function Lockless Optimization State Caching Inline Code Shifting Group Execution Optimistic Barrier

slide-71
SLIDE 71

Memory Optimizations: Fast Path

15

4% 4% 5% 6% 8% 12% 27% 34%

Code Reduction New Function Lockless Optimization State Caching Inline Code Shifting Group Execution Optimistic Barrier

Simplify the slow path logic

slide-72
SLIDE 72

mm/memory.c @@ -303,8 +303,10 @@ static void __munlock_pagevec( if (PageLRU(page)) { lruvec = mem_cgroup_page_lruvec(page, zone); lru = page_lru(page);

  • get_page(page);

+ /* + * We already have pin from follow_page_mask() + * so we can spare the get_page() here. + */

Memory Optimizations: Fast Path

15

4% 4% 5% 6% 8% 12% 27% 34%

Code Reduction New Function Lockless Optimization State Caching Inline Code Shifting Group Execution Optimistic Barrier

Simplify the slow path logic

E.g., Avoid redundant get/put_page in munlock_vma_range as pages will not be referred anymore.

slide-73
SLIDE 73

Memory Optimizations: Fast Path

15

4% 4% 5% 6% 8% 12% 27% 34%

Code Reduction New Function Lockless Optimization State Caching Inline Code Shifting Group Execution Optimistic Barrier

Reduce the usage of lock and atomic operations

slide-74
SLIDE 74

Memory Optimizations: Fast Path

15

4% 4% 5% 6% 8% 12% 27% 34%

Code Reduction New Function Lockless Optimization State Caching Inline Code Shifting Group Execution Optimistic Barrier

Reduce the usage of lock and atomic operations

E.g., lockless memory allocator in SLUB

slide-75
SLIDE 75

Memory Optimizations: Fast Path

15

4% 4% 5% 6% 8% 12% 27% 34%

Code Reduction New Function Lockless Optimization State Caching Inline Code Shifting Group Execution Optimistic Barrier

Cache states to avoid expensive operations

slide-76
SLIDE 76

Memory Optimizations: Fast Path

15

4% 4% 5% 6% 8% 12% 27% 34%

Code Reduction New Function Lockless Optimization State Caching Inline Code Shifting Group Execution Optimistic Barrier

Cache states to avoid expensive operations

E.g., pre-calculate the number of online nodes vs. always calling expensive num_online_nodes

slide-77
SLIDE 77

Memory Optimizations: Fast Path

15

4% 4% 5% 6% 8% 12% 27% 34%

Code Reduction New Function Lockless Optimization State Caching Inline Code Shifting Group Execution Optimistic Barrier

Move infrequently executed code from fast path to slow path

slide-78
SLIDE 78

Memory Optimizations: Fast Path

15

4% 4% 5% 6% 8% 12% 27% 34%

Code Reduction New Function Lockless Optimization State Caching Inline Code Shifting Group Execution Optimistic Barrier

Move infrequently executed code from fast path to slow path

E.g., in SLUB allocator, slow path executes the interrupt enable/disable handlers, fast path executes them only at fallback

slide-79
SLIDE 79

Memory Semantics

16

Memory Resource Controller

memory cgroup

charge/uncharge cgroup management memcontrol.c

slide-80
SLIDE 80

Memory Semantics

16

Memory Resource Controller

memory cgroup

charge/uncharge cgroup management memcontrol.c

Bug: Concurrency issues

slide-81
SLIDE 81

Memory Semantics

16

Memory Resource Controller

memory cgroup

charge/uncharge cgroup management memcontrol.c

Bug: Concurrency issues Cause: missing locks in charging & uncharging pages

(truncation, reclaim, swapout and migration)

slide-82
SLIDE 82

Memory Semantics

17

Virtual Memory Management

memory policy

slide-83
SLIDE 83

Memory Semantics

17

Virtual Memory Management

memory policy

policy definition policy enforcement mempolicy.c

slide-84
SLIDE 84

Memory Semantics

17

Virtual Memory Management

memory policy

policy definition policy enforcement mempolicy.c

Bug: policy enforcement failure

slide-85
SLIDE 85

Memory Semantics

17

Virtual Memory Management

memory policy

policy definition policy enforcement mempolicy.c

Bug: policy enforcement failure Cause: missing check on page states & statistics,

e.g., whether a page is dirty, cache hit/miss rate

slide-86
SLIDE 86

Conclusion

18

Pattern Memory Bug

+

Optimization

+

Semantic

+

slide-87
SLIDE 87

Conclusion

18

Pattern Memory Bug

+

Optimization

+

Semantic

+

  • Complex page states  Concurrency bugs  Simplified page management
  • Fast path  Introduce new errors  Fast path verification
  • Bugs in checking  Model checking for memory manager
  • ......
slide-88
SLIDE 88

19

Jian Huang jian.huang@gatech.edu

Moinuddin K. Qureshi Karsten Schwan

Thanks! Q&A