1 / 105

Light-weight Contexts: An OS Abstraction for Safety and Performance

This article discusses the concept of lightweight contexts in operating systems, explaining the difference between processes and threads, the management of non-running threads, the use of thread control blocks, and the switching of threads. It also covers the distinction between kernel and user threads, the organization of memory, and the handling of interrupts.

lilla
Download Presentation

Light-weight Contexts: An OS Abstraction for Safety and Performance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Light-weight Contexts: An OS Abstraction for Safety and Performance Landon Cox February 19, 2018

  2. What is a process? • Informal • A program in execution • Running code + things it can read/write • Process ≠ program • Formal • ≥ 1 threadsin their own address space • (soon threads will share an address space)

  3. Parts of a process • Thread • Sequence of executing instructions • Active: does things • Address space • Data the process uses as it runs • Passive: acted upon by threads

  4. Play analogy • Process is like a play performance • Program is like the play’s script What are the threads? Threads What is the address space? Address space

  5. Threads that aren’t running • What is a non-running thread? • thread=“sequence of executing instructions” • non-running thread=“paused execution” • Must save thread’s private state • To re-run, re-load private state • Want thread to start where it left off

  6. Thread control block (TCB) • What needs to access threads’ private data? • The CPU • This info is stored in the PC, SP, other registers • The OS needs pointers to non-running threads’ data • Thread control block (TCB) • Container for non-running threads’ private data • Values of PC, code, SP, stack, registers

  7. Thread control block Address Space TCB1 PC SP registers TCB2 PC SP registers TCB3 PC SP registers Ready queue Code Code Code Stack Stack Stack Thread 1 running CPU PC SP registers

  8. Thread control block Address Space TCB2 PC SP registers TCB3 PC SP registers Ready queue Code Stack Stack Stack Thread 1 running CPU PC SP registers

  9. Switching threads • What needs to happen to switch threads? • Thread returns control to scheduler • For example, via timer interrupt • Scheduler chooses next thread to run • Scheduler saves state of current thread • To its thread control block • Scheduler loads context of next thread • From its thread control block • Jump to PC in next thread’s TCB

  10. Kernel vs user threads • Kernel threads • Scheduler and queues reside in the kernel • User threads • Scheduler and queues reside in user space

  11. 32-bit address space 4GB Kernel data (same for all processes) 3GB (0xc0000000) User data (different for every process) 0GB Virtual memory

  12. Switching threads 4GB 3GB Where is the interrupt handler code? In the kernel 0GB Virtual memory

  13. Switching threads 4GB SP Interrupt-handler code PC 3GB On which stack is the IRQ handled? A kernel stack 0GB Virtual memory

  14. Switching threads 4GB SP Interrupt-handler code PC 3GB Where are the threads’ stacks? In user space 0GB Virtual memory

  15. Switching threads 4GB SP Interrupt-handler code PC 3GB Where are the threads’ stacks? In user space 0GB Virtual memory

  16. Switching threads 4GB SP Interrupt-handler code PC 3GB So far, everything is the same for user-level and kernel threads 0GB Virtual memory

  17. Kernel threads Scheduler code Handler code Interrupt-handler code For kernel threads, the thread scheduler and TCB queues are in the kernel TCB TCB TCB

  18. Kernel threads Scheduler code Where is the lock/cv code? Handler code Interrupt-handler code TCB TCB TCB

  19. Kernel threads Scheduler code Synchron. code Where is the lock/cv code? Handler code Interrupt-handler code TCB TCB Also, in the kernel TCB

  20. User-level threads Handler code Interrupt-handler code For user-level threads, the thread scheduler and queues are in user space TCB TCB TCB Scheduler code

  21. User-level threads Where is the lock/cv code? Handler code Interrupt-handler code TCB TCB Also, in the user space TCB Scheduler code Synchron. code

  22. Switching to a new user-level thread

  23. User-level threads Handler code Interrupt-handler code SP TCB TCB PC Scheduler code Thread running user code Synchron. code Program code

  24. User-level threads SP Handler code Interrupt-handler code TCB PC TCB Timer interrupt! Scheduler code Synchron. code Program code

  25. User-level threads SP Handler code Interrupt-handler code TCB PC TCB Does the kernel know where user left off? Scheduler code Synchron. code Yes, CPU stores this on kernel stack Program code

  26. User-level threads SP Handler code Interrupt-handler code TCB PC TCB What does kernel do next? Scheduler code Synchron. code Depends on meaning of the timer … Program code

  27. User-level threads SP Handler code Interrupt-handler code TCB PC TCB Assuming it’s a signal for the process? Scheduler code Synchron. code Kernel delivers signal to process Program code

  28. User-level threads SP Handler code Interrupt-handler code TCB PC TCB To deliver a signal to user code: make it look like a forced function call to signal handler. Scheduler code Synchron. code Program code

  29. User-level threads SP Handler code Interrupt-handler code TCB PC TCB Where is the process’s timer signal handler? Scheduler code Synchron. code In scheduler code (e.g., yield) Program code

  30. User-level threads SP Handler code Interrupt-handler code TCB PC TCB On what stack should the scheduler code run? Scheduler code Synchron. code yield Stack of interrupted thread Program code

  31. User-level threads PC Handler code Interrupt-handler code TCB SP TCB On what stack should the scheduler code run? Scheduler code Synchron. code yield Stack of interrupted thread Program code

  32. User-level threads PC Handler code Interrupt-handler code TCB SP TCB Could the kernel transfer control to any other thread? Scheduler code Synchron. code yield No, it’s only aware of interrupted one Program code

  33. Switching to a new kernel thread

  34. Kernel threads Scheduler code Synchron. code Handler code Interrupt-handler code SP TCB TCB PC TCB Thread running user code Program code

  35. Kernel threads Scheduler code Synchron. code SP Handler code Interrupt-handler code TCB PC TCB TCB Timer interrupt! Program code

  36. Kernel threads Scheduler code Synchron. code SP Handler code Interrupt-handler code TCB PC TCB TCB Which thread could run next? Any thread. Kernel aware of all of them Program code

  37. Advantages of user-level threads • Synchronization primitives are just a function call • Why are kernel threads more expensive? • Accessing thread management must cross kernel boundary • CPU protection checks, argument checks, handler dispatch, etc. • Switching back to thread also crosses kernel boundary • For user threads, easy to tune scheduling policy to application • Why is this harder for kernel threads? • All processes share the same scheduler • Per-process policies require wider, more complex API

  38. Advantage of kernel threads SP Handler code Interrupt-handler code TCB PC TCB Which thread could run next? Scheduler code Synchron. code Only the one that was interrupted Program code

  39. Advantage of kernel threads SP Handler code Interrupt-handler code TCB PC TCB Why might we be unable to run the interrupted thread? Scheduler code Synchron. code Thread could have made a blocking system call Program code

  40. Advantage of kernel threads SP Handler code Interrupt-handler code TCB PC TCB Scheduler code Synchron. code Program code

  41. Advantage of kernel threads SP Handler code Interrupt-handler code TCB PC TCB Why is this wasteful? Scheduler code Synchron. code There are other threads that could run! Kernel doesn’t know they exist Program code

  42. Parts of a process (revised) • Thread • Sequence of executing instructions • Active: does things • Address space • Data the process uses as it runs • Passive: acted upon by threads • File descriptors • Communication channels (e.g., files, sockets, pipes) • Passive: read-from/written-to by threads

  43. Parts of a process (revised) Process Thread 1 Thread 2 File descriptors Page table Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file Each thread has its own stack, but everything else is shared: same pages, file descriptors

  44. Parts of a process (revised) Threads are not well isolated from each other. Process Thread 1 Thread 2 File descriptors Page table Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file Threads can run in parallel on different cores.

  45. Parts of a process (revised) Process Thread 1 Thread 2 File descriptors Page table Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file If one thread fails, entire process fails.

  46. Parts of a process (revised) Process Thread 2 File descriptors Page table Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file Compromising a thread, compromises all data.

  47. Parts of a process (revised) Thread 2 Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file Compromising a thread, compromises all data.

  48. Parts of a process (revised) Thread 2 Stack 1 Stack 2 Readable pages Writable pages TCP socket Open file See: heartbleed, cloudbleed, etc.

  49. Heartbleed

  50. Heartbleed bug • Nasty bug in OpenSSL implementation • Made public by Google in April, 2014 • “A missing bounds check in the handling of the TLS heartbeat extension.” • What is a heartbeat message? • A small message to check if machine is alive (like in Raft) • In SSL, keeps peers from needlessly renegotiating keys

More Related