Kthreads, Mutexes, and Debugging

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CSCC69: Operating Systems
1 Week 8 Locking, Linked Lists, Scheduling Algorithms Classes COP4610 / CGS5765 Florida State University.
Homework 6 Sarah Diesburg Operating Systems CS 3430.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
Processes CSCI 444/544 Operating Systems Fall 2008.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
1 Week 6 Intro to Kernel Modules, Project 2 Sarah Diesburg Florida State University.
Introduction to Processes CS Intoduction to Operating Systems.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Week 3 January 22, 2004 Adrienne Noble. Today CVS – a great tool to use with your groups Threads – basic thread operations Intro to synchronization Hand.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
1 Week 7 System Calls, Kernel Threads, Kernel Debugging Sarah Diesburg Florida State University.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
1 Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems.
Introduction to operating systems What is an operating system? An operating system is a program that, from a programmer’s perspective, adds a variety of.
pThread synchronization
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Tutorial 2: Homework 1 and Project 1
CS703 - Advanced Operating Systems
Review Array Array Elements Accessing array elements
PROCESS MANAGEMENT IN MACH
Lectures linked lists Chapter 6 of textbook
CS703 – Advanced Operating Systems
CS399 New Beginnings Jonathan Walpole.
Multithreading Tutorial
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Thread synchronization
Kthreads, Mutexes, and Debugging
Process management Information maintained by OS for process management
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
COP 4600 Operating Systems Spring 2011
Jonathan Walpole Computer Science Portland State University
Intro to Kernel Modules and /proc
Review and Q/A.
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Threading And Parallel Programming Constructs
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
Synchronization Primitives – Semaphore and Mutex
Multithreading Tutorial
Kernel Synchronization II
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Foundations and Definitions
Lecture 20: Synchronization
CSE 451 Section 1/27/2000.
CSE 153 Design of Operating Systems Winter 2019
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Mr. M. D. Jamadar Assistant Professor
Presentation transcript:

Kthreads, Mutexes, and Debugging Sarah Diesburg CS 3430 Operating Systems

Refresher: procfs Kernel Module procfs “hello world” example Creates a read/write procfs entry Steps Create entry in module_init function Registers reading/writing function using file_operations structure Delete entry in module_cleanup function

Testing Procfs #> insmod hello_proc.ko #> tail /var/log/syslog #> cat /proc/helloworld #> echo “hi” > /proc/helloworld #> rmmod hello_proc

Project 3: Remember You will write your own proc module called remember that Allows the user to write a string to /proc/remember (max length 80) Allows the user to read /proc/remember and get back the string that was just added If no string was added, the string EMPTY should be read instead

Remember Example #> echo “Hello there” > /proc/remember #> cat /proc/remember Hello there #>

Run the main logic of your module in a kthread! Kthreads Run the main logic of your module in a kthread!

Refresher: hello.c #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) printk(KERN_ALERT “Goodbye, sleepy world.\n”); module_init(hello_init); module_exit(hello_exit);

Kernel Modules Remember, kernel modules are very event-based How can we start an independent thread of execution to run in the background?

kthread_run kthread_run(threadfn, data, namefmt, ...) Creates a new thread and tells it to run threadfn – the name of the function the thread should run data – data pointer for threadfn (can be NULL if the function does not take any args) namefmt – name of the thread (displayed during “ps” command) Returns a task_struct

kthread_run example struct task_struct *t; t = kthread_run(run, NULL, “my_thread");

kthread_stop int kthread_stop(struct task_struct * k); Sets kthread_should_stop for k to return true, wakes the thread, and waits for the thread to exit Returns the result of the thread function

kthread_stop_example ret=kthread_stop(t); if(ret != -EINTR) printk("Main logic tread stopped.\n“);

Thread Function Example static int run(void *arg) { /* Lock here */ while(!kthread_should_stop()) { /* Do stuff */ } /* Unlock here */ printk("%s: kernel thread exits.\n", __FUNCTION__); return 0;

Demo kthread module

Part 3: Kitchen Scheduling

Part 3: Kitchen Scheduling Implement a /proc kernel module that simulates an busy kitchen at a restaurant Your /proc/kitchen file must accept writes of different dishes Each dish takes some time to process Your /proc/kitchen file must return status information when read

Why Scheduling? Classic producer/consumer analogy Similar to disk schedulers File system produces read/write requests Disk consumes requests, optimized for disk head position, rotational delays, etc.

Your Kitchen One kitchen 20 order slots Four types of dishes Use a static array of ints? Four types of dishes 1 - Caesar Salad 2 - Hamburger 3 - Personal Pizza 4 - Beef Wellington

Food orders A write of 1-4 to /proc/kitchen will fill an order slot with the dish corresponding to the number You decide how the order slots get filled (e.g. round robin or other way) Kitchen takes 1 second to look in an order slot Regardless if empty or containing an order

Food Orders The kitchen can only process one order at a time Each order takes a different amount of time to process 2 seconds for a Caesar Salad 3 seconds for a Hamburger 4 seconds for a Personal Pizza 8 seconds for a Beef Wellington

Kernel Time Constraints #include <linux/delay.h> void ssleep(unsigned int seconds); A call to ssleep will have the program cease to the task scheduler for seconds number of seconds

Food Order Once an order is processed, the kitchen can mark that order slot as empty and should look at other order slots to find more orders to process.

Processing the orders? As soon as the kitchen module loads, it should start a persistent background thread that looks at each slot in the array and processes orders The actual order processing logic will be run in a kthread This is where your module should ssleep to look in the queue and “process” an order

Status Information Performing a read on /proc/kitchen should return some status information Current spot being looked at in the queue Current order being processed Number of orders processed If the kitchen is not running, the last two pieces of information do not need to be displayed

Additional Design Considerations How to place orders in order slots? How to check order slots from the kitchen? What scheduling algorithm to use?

Demo Project 3 Specification Working binary Kitchen starter code

Concurrency Aspects of Project 3 Synchronizing access to request queue Multiple producers may access request queue(s) at the same time Multiple consumers may access request queue(s) at the same time Synchronizing access to other global data

Kitchen Queue Concurrency Jobs may appear on the queue at the same time the kitchen module checks the queue The status may be read at the same time that you're updating Number of jobs that you've serviced Which job is currently being processed Which slot in the queue kitchen is looking at How do you guarantee correctness?

Global Data vs. Local Data Global data is declared at global scope, e.g. outside of any function body Often necessary for kernel programming Particularly sensitive to concurrency issues Be extra careful when handling globals

Global Data vs. Local Data Local data is declared within a function Local data is sensitive to concurrency issues when it depends on global data or when parallel access is possible Think carefully about whether it needs to be synchronized

Synchronization Primitives Semaphores User space Kernel space Mutexes Spin locks Atomic Functions

Synchronization Primitives (We'll Only Cover These) Mutexes User space Kernel space Does anyone remember the differences between a mutex and semaphore?

The Mutex Caught up in the mutex?

Mutexes Mutex – A construct to provide MUTual EXclusion Based on the concept of a semaphore Found at <source_dir>/include/linux/mutex.h Can be locked or unlocked Only one thread of execution may hold the lock at a time

Kernel-Space Mutex - Initialization mutex_init(&mutex) Declare and initialize a mutex Only initialize it once

Kernel-Space Mutex - Locking void mutex_lock(struct mutex *); int mutex_lock_interruptible(struct mutex *); mutex_lock() can wait indefinitely mutex_lock_interruptible() locks a mutex as long as it is not interrupted returns 0 if locking succeeded, < 0 if interrupted Use of the interruptible version is typically preferred

Kernel-Space Mutex – Unlocking void mutex_unlock(struct mutex *); Guarantees that the mutex is unlocked Why is there no interruptible version of this function?

Kernel-Space Mutex Example /* Declare your mutex */ struct mutex my_mutex; /* Initialize your mutex */ mutex_init(&my_mutex); /* Lock */ if(mutex_lock_interruptible(&my_mutex)) return -ERESTARTSYS; /* Do stuff to protected global variables */ /* Unlock */ mutex_unlock(&my_mutex);

User-Space Mutex Also used with pthreads in regular user applications pthreads operate very similar to kthreads Might be useful if you are prototyping your code in user-space before porting to kernel

User-Space Mutex - Initialization int pthread_mutex_init(pthread_mutex_t *, NULL); int pthread_mutex_destroy(pthread_mutex_t *); Pthread_mutex_init() dynamically allocates a mutex Pthread_mutex_destroy() frees a mutex

User-Space Mutex - Locking int pthread_mutex_lock(pthread_mutex_t *); Returns 0 on locking, < 0 otherwise

User-Space Mutex - Unlocking int pthread_mutex_unlock(pthread_mutex_t *); Returns 0 on unlocking, < 0 otherwise

Kitchen Scheduling Advice

General Advice Just make kitche work first Optimize if there is time Use a very simple algorithm My kitchen searches the queue in round-robin fashion and processes every maintenance request Optimize if there is time

Round Robin Method: Pros/Cons? Service requests in round-robin fashion (e.g. queue slot 0, 1, 2, 3, etc.) Pros/Cons?

Shortest Job First Method: Service fastest orders first Pros/Cons?

Hybrid Combine methods, come up with something new Up to your creativity

Getting Help Class time to answer questions Regular office hours Email

Other Hints This is not a simple project Setup is different You need to use different files and methods of compilation/running Do NOT wait until 2 days before it is due to start Too late The Internet will likely NOT be very helpful

Other Hints Set milestones Ask questions early If it’s a good question, I’ll share it with the class

Debugging

Kernel Debugging Configurations Timing info on printks Detection of hung tasks Kernel memory leak detector Mutex/lock debugging Kmemcheck Check for stack overflow

Select Kernel Hacking

Enable Debugging Options

Debugging through reads to /proc/penguin Necessary to help you “see” what’s going on! General process Identify data to monitor in your module Run your module Query (read from) /proc/penguin for that information at any time

Kernel Oops and Other Errors Kernel errors often only appear on first tty (terminal interface) Why?

Oops!

Reason for failure

Current drivers

Call Trace

Call Trace

Failed command