cs 153 design of operating systems spring 2015 lecture 19: page replacement and memory war

20
CS 153 CS 153 Design of Operating Design of Operating Systems Systems Spring 2015 Spring 2015 Lecture 19: Page Replacement and Lecture 19: Page Replacement and Memory War Memory War

Upload: frank-jenkins

Post on 19-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

CS 153CS 153Design of Operating SystemsDesign of Operating Systems

Spring 2015Spring 2015

Lecture 19: Page Replacement and Memory Lecture 19: Page Replacement and Memory WarWar

Page 2: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

2

Working Set ModelWorking Set Model A working set of a process is used to model the

dynamic locality of its memory usage Defined by Peter Denning in 60s

Definition WS(t,w) = {set of pages P, such that every page in P was

referenced in the time interval (t, t-w)} t – time, w – working set window (measured in page refs)

A page is in the working set (WS) only if it was referenced in the last w references

Page 3: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

3

Working Set SizeWorking Set Size The working set size is the number of pages in the

working set The number of pages referenced in the interval (t, t-w)

The working set size changes with program locality During periods of poor locality, you reference more pages Within that period of time, the working set size is larger

Intuitively, want the working set to be the set of pages a process needs in memory to prevent heavy faulting

Each process has a parameter w that determines a working set with few faults

Denning: Don’t run a process unless working set is in memory

Page 4: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

Example: gcc Working SetExample: gcc Working Set

4

Page 5: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

5

Working Set ProblemsWorking Set Problems Problems

How do we determine w? How do we know when the working set changes?

Too hard to answer So, working set is not used in practice as a page replacement

algorithm However, it is still used as an abstraction

The intuition is still valid When people ask, “How much memory does Firefox need?”,

they are in effect asking for the size of Firefox’s working set

Page 6: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

6

Page Fault Frequency (PFF)Page Fault Frequency (PFF) Page Fault Frequency (PFF) is a variable space

algorithm that uses a more ad-hoc approach Monitor the fault rate for each process If the fault rate is above a high threshold, give it more memory

» So that it faults less

» But not always (FIFO, Belady’s Anomaly) If the fault rate is below a low threshold, take away memory

» Should fault more

» But not always

Hard to use PFF to distinguish between changes in locality and changes in size of working set

Page 7: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

7

ThrashingThrashing Page replacement algorithms avoid thrashing

When most of the time is spent by the OS in paging data back and forth from disk

No time spent doing useful work (making progress) In this situation, the system is overcommitted

» No idea which pages should be in memory to reduce faults

» Could just be that there isn’t enough physical memory for all of the processes in the system

» Ex: Running Windows95 with 4 MB of memory… Possible solutions

» Swapping – write out all pages of a process

» Buy more memory

Page 8: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

Avoiding Thrashing with Avoiding Thrashing with Minimum MemoryMinimum Memory

W: minimum amount of memory needed

8

Page 9: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

Memory-related Security Memory-related Security ProblemsProblems

9

Stack

0x00000000

0xFFFFFFFF

Code(Text Segment)

Static Data(Data Segment)

Heap(Dynamic Memory Alloc)

AddressSpace

SP

PC

Static

Dynamic

Page 10: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

Stack Buffer OverflowStack Buffer Overflow Example from Wikipedia

Page 11: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

Common Defenses 1/3Common Defenses 1/3Stack canariesStack canaries

Canary = random value inserted at each stack frame. It is checked at every call return

Effects: Attacks cannot easily guess the value and have to overwrite it

Page 12: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

Memory War - Buffer OverflowMemory War - Buffer Overflow

12

Stack

0x00000000

0xFFFFFFFF

Code(Text Segment)

Static Data(Data Segment)

Heap(Dynamic Memory Alloc)

AddressSpace

Page Protection bits?

Page 13: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

Commonly deployed defenses 2/3 Commonly deployed defenses 2/3 Non-executable stackNon-executable stack

Stack memory pages cannot be executed with this protection mechanism --- stack was designed as read/write memory only anyways

Effects: Attacks can only change return address to code pages (that are executable):

Return-to-libc ROP (Return-Oriented

Programming)

Page 14: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

Commonly defenses 3/3 Commonly defenses 3/3 Address Space Layout Randomization Address Space Layout Randomization (ASLR)(ASLR)

Base address of stacks, heaps, and binaries/libraries/kernel-modules are randomized

Effects: Attackers do not know what address to use, even for the state-of-the art ROP attack method

Page 15: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

15

SummarySummary Page replacement algorithms

Belady’s – optimal replacement (minimum # of faults) FIFO – replace page loaded furthest in past LRU – replace page referenced furthest in past

» Approximate using PTE reference bit LRU Clock – replace page that is “old enough” Working Set – keep the set of pages in memory that has

minimal fault rate (the “working set”) Page Fault Frequency – grow/shrink page set as a function of

fault rate Multiprogramming

Should a process replace its own page, or that of another?

Page 16: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

16

Next time…Next time… File systems

Chapters 11, 12, and 13

Page 17: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

17

Page 18: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

18

Example: ComparisonExample: Comparison

Page 19: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

19

Example: Belady's AnomalyExample: Belady's Anomaly

Page 20: CS 153 Design of Operating Systems Spring 2015 Lecture 19: Page Replacement and Memory War

20

Working Set: ExampleWorking Set: Example