Preemptive Context Switching

Slides:



Advertisements
Similar presentations
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Advertisements

Tutorial 3 - Linux Interrupt Handling -
The Process Control Block From: A Process Control Block (PCB, also called Task Control Block or Task Struct) is.
CS591 (Spring 2001) The Linux Kernel: Signals & Interrupts.
COMP3221: Microprocessors and Embedded Systems Lecture 15: Interrupts I Lecturer: Hui Wu Session 1, 2005.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
1 Interrupts INPUT/OUTPUT ORGANIZATION: Interrupts CS 147 JOKO SUTOMO.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
MicroC/OS-II Embedded Systems Design and Implementation.
INPUT/OUTPUT ORGANIZATION INTERRUPTS CS147 Summer 2001 Professor: Sin-Min Lee Presented by: Jing Chen.
Computer System Laboratory
Introduction to Embedded Systems
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Real-time Systems Lab, Computer Science and Engineering, ASU Linux Input Systems (ESP – Fall 2014) Computer Science & Engineering Department Arizona State.
The Functions of Operating Systems Interrupts. Learning Objectives Explain how interrupts are used to obtain processor time. Explain how processing of.
Tami Meredith, Ph.D. CSCI  Devices need CPU access  E.g., NIC has a full buffer it needs to empty  These device needs are often asynchronous.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Operating Systems CSE 411 Multi-processor Operating Systems Multi-processor Operating Systems Dec Lecture 30 Instructor: Bhuvan Urgaonkar.
Scheduling Lecture 6. What is Scheduling? An O/S often has many pending tasks. –Threads, async callbacks, device input. The order may matter. –Policy,
Fall 2013 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to Embedded Systems Dr. Jerry Shiao, Silicon Valley University.
Lab 13 Department of Computer Science and Information Engineering National Taiwan University Lab13 – Interrupt + Timer 2014/12/23 1 /16.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
4300 Lines Added 1800 Lines Removed 1500 Lines Modified PER DAY DURING SUSE Lab.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
1 VxWorks 5.4 Group A3: Wafa’ Jaffal Kathryn Bean.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Managing Processors Jeff Chase Duke University. The story so far: protected CPU mode user mode kernel mode kernel “top half” kernel “bottom half” (interrupt.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 3.
Embedded Computer - Definition When a microcomputer is part of a larger product, it is said to be an embedded computer. The embedded computer retrieves.
CHAPTER 7 CONCURRENT SOFTWARE Copyright © 2000, Daniel W. Lewis. All Rights Reserved.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Copyright © Curt Hill More on Operating Systems Continuation of Introduction.
Chapter 3 RTOS Concepts And Definitions Department of Computer Science Hsu Hao Chen Professor Hsung-Pin Chang.
Interrupts and Interrupt Handling David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Overview: Using Hardware.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Interrupts and Exceptions 國立中正大學 資訊工程研究所 羅習五 老師. Interrupt & Exceptions Interrupts – maskable & nonmaskable interrupt Exceptions – Processor-detected.
Preemptive Context Switching
Linux Kernel Development - Robert Love
Interrupts and exceptions
Processes and threads.
Operating Systems CMPSC 473
Interfacing with Hardware
Microprocessor Systems Design I
CS 6560: Operating Systems Design
OPERATING SYSTEMS CS3502 Fall 2017
Anton Burtsev February, 2017
Intro to Processes CSSE 332 Operating Systems
Interrupts and Interrupt Handling
Computer System Overview
Processor Fundamentals
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Chapter 2: The Linux System Part 3
Top Half / Bottom Half Processing
EE 472 – Embedded Systems Dr. Shwetak Patel.
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
COMP3221: Microprocessors and Embedded Systems
Interrupts and Interrupt Handling
Presentation transcript:

Linux Interrupt Processing and Kernel Thread (ESP – Fall 2014) Computer Science & Engineering Department Arizona State University Tempe, AZ 85287 Dr. Yann-Hang Lee yhlee@asu.edu (480) 727-7507

Preemptive Context Switching Hardware Interrupt Thread A Executing in kernel ISR Context Switch Thread B Thread B blocked by an event Ready Executing Scheduler selects highest priority thread that is ready to run. If not the current thread, the current thread is made ready and the new thread resumed. Process Interrupt Request & wake up Thread B IRET (ftp://ftp.prenhall.com/pub/esm/electrical_engineering.s-037/lewis/powerpoint/chapter_7.zip)

Nested Execution of Handlers Generally nesting of kernel code paths is allowed with certain restrictions Exceptions can nest only 2 levels Original exception and possible Page Fault Exception code can block Interrupts can nest arbitrarily deep, but the code can never block (nor should it ever take an exception) (D. P. Bovet and M. Cesati, “Understanding the Linux Kernel”, 3rd Edition)

Interrupt Handling (1) Depends on the type of interrupts I/O interrupts Timer interrupts Interprocessor interrupts Unlike exceptions, interrupts are “out of context” events Generally associated with a specific device that delivers a signal on a specific IRQ IRQs can be shared and several ISRs may be registered for a single IRQ ISRs is unable to sleep, or block Critical: to be executed within the ISR immediately, with maskable interrupts disabled (e.g., acknowledge interrupt) Noncritical: should be finished quickly, so they are executed by theISR immediately, with the interrupts enabled (e.g., read IO data to buffer) Noncritical deferrable: deferrable actions are performed by means of separate functions

Interrupt Handling (2) Interrupts (as opposed to exceptions) are not associated with particular instructions, not associated with a given process When an interrupt occurs, what stack is used? Exceptions: The kernel stack of the current process, whatever it is, is used (There’s always some process running, or the “idle” process, if nothing else) Interrupts: hard IRQ stack (1 per processor) SoftIRQs: soft IRQ stack (1 per processor) These stacks are configured in the IDT and TSS at boot Wme by the kernel. Multiple I/O devices can share a single IRQ (interrupt vector) Interrupt vector  IRQ  ISR’s each ISR can determine whether an action should be taken or not

I/O Interrupt Handling SOFTWARE (Interrupt Handler) HARDWARE Device 1 Device 2 IRQn common_interrupt: SAVE_ALL movl %esp, %eax call do_IRQ jmp ret_from_intr IDT[32+n] INT IRQn_interrupt() PIC interrupt vector (IRQn+32) do_IRQ(n) handle_IRQ_event ISR (irq_action)1 ISR (irq_action) 2 (D. P. Bovet and M. Cesati, “Understanding the Linux Kernel”, 3rd Edition) Execute ISRs associated with all the devices that share the IRQ.

Cat /proc/interrupts IRQ number, the number of that interrupt handled by each CPU core, the interrupt type, and a list of drivers that are registered to receive that interrupt.

IRQ Descriptor http://rock3.info/wp-content/uploads/2013/11/irq_desc.jpg

Why ISR Bottom Half? To have low interrupt latency -- to split interrupt routines into a `top half', which receives the hardware interrupt and a `bottom half', which does the lengthy processing. Top halves have following properties (requirements) need to run as quickly as possible run with some (or all) interrupt levels disabled are often time-critical and they deal with HW do not run in process context and cannot block Bottom halves are to defer work later “Later” is often simply “not now” Often, bottom halves run immediately after interrupt returns They run with all interrupts enabled Code in the Linux kernel runs in one of three contexts: Process context, kernel thread context, and Interrupt.

A World of Bottom Halves Multiple mechanisms are available for bottom halves softirq: (available since 2.3) A set of 32 statically defined bottom halves that can run simultaneously on any processor Even 2 of the same type can run concurrently Used when performance is critical Must be registered statically at compile-time tasklet: (available since 2.3) Are built on top of softirqs Two different tasklets can run simultaneously on different processors But 2 of the same type cannot run simultaneously Used most of the time for its ease and flexibility Code can dynamically register tasklets work queues: (available since 2.5) Queueing work to be performed in process context

Softirqs Statically allocated at compile-time Softirqs are reentrant functions that are serialized on a given CPU, but can run concurrently across CPUs, reduce the amount of locking needed Statically allocated at compile-time In 2.6.7 kernel, only 6 prioritized softirqs are used HI_SOFTIRQ, TIMER_SOFTIRQ, NET_TX_SOFTIRQ, NET_RX_SOFTIRQ, SCSI_SOFTIRQ, TASKLET_SOFTIRQ A softirq often raised (raise_softirq()) from within interrupt handlers and never preempts another softirq Pending softirqs are checked for and executed (call do_softirq() on schedule() ) in the following places: After processing a HW interrupt By the ksoftirqd kernel thread By code that explicitly checks and executes pending softirqs

Tasklets Tasklets are typically functions used by device drivers for deferred processing of interrupts, can be statically or dynamically enqueued on either the TASKLET_SOFTIRQ or the HI_SOFTIRQ softirq Tasklets can only run on one CPU at a time, and are not required to be reentrant (run only once) a tasklet does not begin executing before the handler has completed. locking between the tasklet and other interrupt handlers may still be required locking between multiple tasklets A more formal mechanism of scheduling software interrupts Tasklet struct -- the macro DECLARE_TASKLET(name, func, data) tasklet_schedule(&tasklet_struct) schedules a tasklet for execution. invokes raise_softirq_irqoff( ) to activate the softirq run in software interrupt context with the result that all tasklet code must be atomic.

WorkQueues To request that a function be called at some future time. tasklets execute quickly, for a short period of time, and in atomic mode workqueue functions may have higher latency but need not be atomic Run in the context of a special kernel process (worker thread) more flexibility and workqueue functions can sleep. they are allowed to block (unlike deferred routines) No access to user space A workqueue (workqueue_struct) must be explicitly created Each workqueue has one or more dedicated “kernel threads”, which run functions submitted to the queue. work_struct structure to submit a task to a workqueue DECLARE_WORK(name, void (*function)(void *), void *data); A shared, default workqueue provided by the kernel.

Work Queue Activation The queue_work() routine prepares a work_struct descriptor (holding a function) for a work queue and then: Checks whether the function to be inserted is already present in the work queue (work->pending field equal to 1); if so, terminates Adds the work_struct descriptor to the work queue list, and sets work->pending to 1 If a worker thread is sleeping in the more_work wait queue of the local CPU's cpu_workqueue_struct descriptor, this routine wakes it up The kernel offers a predefined work queue called events, which can be freely used by every kernel developer saves significant system resources when the function is seldom invoked must be careful not to enqueue functions that could block for a long period

Example of Work Structure and Handler #include <linux/kernel.h> #include <linux/module.h> #include <linux/workqueue.h> MODULE_LICENSE("GPL"); static struct workqueue_struct *my_wq; // work queue typedef struct { // work struct work_struct my_work; int x; } my_work_t; my_work_t *work, *work2; static void my_wq_function( struct work_struct *work) // function to be call { my_work_t *my_work = (my_work_t *)work; printk( "my_work.x %d\n", my_work->x ); kfree( (void *)work ); return; } (http://www.ibm.com/developerworks/linux/library/l-tasklets/index.html)

Example of Work and WorkQueue Creation int init_module( void ) { int ret; my_wq = create_workqueue("my_queue"); // create work queue if (my_wq) { work = (my_work_t *)kmalloc(sizeof(my_work_t), GFP_KERNEL); if (work) { // Queue work (item 1) INIT_WORK( (struct work_struct *)work, my_wq_function ); work->x = 1; ret = queue_work( my_wq, (struct work_struct *)work ); } work2 = (my_work_t *)kmalloc(sizeof(my_work_t), GFP_KERNEL); if (work2) { // Queue work (item 2) INIT_WORK( (struct work_struct *)work2, my_wq_function ); work2->x = 2; ret = queue_work( my_wq, (struct work_struct *)work2 ); return 0; (http://www.ibm.com/developerworks/linux/library/l-tasklets/index.html)

ISR SoftIRQ Tasklet WorkQueue KThread Will disable all interrupts? Briefly No Will disable other instances of self? Yes Higher priority than regular scheduled tasks? Yes* Will be run on same processor as ISR? N/A Maybe More than one run can on same CPU? Same one can run on multiple CPUs? Full context switch? Can sleep? (Has own kernel stack) Can access user space? (http://www.cs.columbia.edu/~nahum/w6998/lectures/interrupts.ppt)