Area Survey: Security in File Systems

Area Survey: Security in File Systems

Kthreads, Mutexes, and Debugging Sarah Diesburg CS 3430 Operating Systems 1 In the old days There were no modules or virtual machines

The kernel is a program Has code, can compile, re-compile, make executable When changes needed to be made, developers make changes in source and re-compile 2 How is the kernel

different from a regular Mostly in how it is executed program? Boot loader loads the kernel image/executable during boot time Sets kernel mode

Jumps to the entry point in the image/executable Remember the generic booting sequence? 3 Quick Question How would you make changes to the kernel and run those changes? 1. 2.

3. 4. 5. Make changes to the source Re-complie the kernel source Re-install the kernel source Make sure the bootloader sees the new kernel image (grub) Reboot and profit! 4

Getting more modern.. Modules were created as bits of code that can be loaded and unloaded by the kernel in kernel mode Made development easier Instead of re-compiling, re-installing, and

rebooting into the new kernel, one could just recompile and load a module 5 Quick Question How would you make changes to a module and run those changes? 1. 2. 3.

Make changes to module source code Re-compile the module Load the new module 6 Kthreads Run the main logic of your module in a kthread! 7

Refresher: hello.c #include #include 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); 8 Kernel Modules Remember, kernel modules are very eventbased We need a way to start an independent thread of execution in response to an event

e.g. start_penguin() for project 2 9 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 10

kthread_run example struct task_struct *t; t = kthread_run(run, NULL, my_thread"); 11 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 12 kthread_stop_example ret=kthread_stop(t); if(ret != -EINTR) printk("Main logic tread stopped.\n); 13

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; } 14

Sample kthread code Take a look at the sample module that spawns a kthread on load You will want to move this kthread to start when user writes a 0 to /proc/penguin You will want to stop the kthread when user writes a -1 to /proc/penguin

15 Concurrency Aspects of Project 2 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 Penguin Queue Concurrency Jobs may appear on the queue at the same time the penguin 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 penguin 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 19 Synchronization Primitives

Semaphores Mutexes

User space Kernel space Spin locks User space Kernel space Kernel space

Atomic Functions Synchronization Primitives MutexesOnly Cover These) (We'll User space

Kernel space Does anyone remember the differences between a mutex and semaphore? The Mutex Caught up in the mutex? 22 Mutexes

Mutex A construct to provide MUTual EXclusion Based on the concept of a semaphore Found at /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() can wait indefinitely mutex_lock_interruptible() locks a mutex as long as it is not interrupted mutex_lock_interruptible(struct mutex *); 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?

26 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); 27 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 28 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 Penguin Scheduling Advice 32 General Advice Just make printer work first

Use a very simple algorithm My printer searches the queue in round-robin fashion and processes every maintenance request Optimize if there is time 33 Round Robin

Method: 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 36 Getting Help

Class time to answer questions Regular office hours Email 37 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

38 Other Hints Set milestones Ask questions early If its a good question, Ill share it with the class

39 Debugging 40 Kernel Debugging Configurations

Timing info on printks Detection of hung tasks Kernel memory leak detector Mutex/lock debugging Kmemcheck Check for stack overflow 41 Select Kernel Hacking 42

Enable Debugging Options 43 Debugging through reads to /proc/penguin Necessary to help you see whats 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 44 Kernel Oops and Other Errors

Kernel errors often only appear on first tty (terminal interface) Why? 45 Oops! 46

Reason for failure 47 Current drivers 48 Call Trace 49

Call Trace 50 Failed command 51

Recently Viewed Presentations

  • Data and Computer Communications

    Data and Computer Communications

    The objective of this chapter and the next is to provide the reader with an intuitive feeling for the nature of these two factors. ... There is a simple relationship between the two sine waves, one in time and one...
  • WALT: about Hooke&#x27;s Law

    WALT: about Hooke's Law

    Who was Hooke. English physicist Robert Hooke. 1635, Isle of Wight,—1703, London. In mechanics, and physics Hooke's law of elasticity is an approximation that states that the extension of a spring is in direct proportion with the load applied to...
  • SLCC PowerPoint Template - Bio-Link

    SLCC PowerPoint Template - Bio-Link

    Vivian Ngan-Winward, PhD, CMQ/OE. WORKING IN A . REGULATED ENVIRONMENT - ARE YOUR . BIOTECH STUDENTS PREPARED? Working in a Regulated Environment / Bio-Link SFF 2013. 5/29/2013. This workshop covers . . . Overview of a regulated environment.
  • 2016 ANNUAL DATA REPORT VOLUME 2: END-STAGE RENAL

    2016 ANNUAL DATA REPORT VOLUME 2: END-STAGE RENAL

    Figure 14.3 ICU admission during the last 90 days of life among Medicare beneficiaries with ESRD overall, and by age, race, ethnicity, sex, and modality, 2000-2013
  • CS152 Computer Architecture and Engineering

    CS152 Computer Architecture and Engineering

    Computer Architecture and Engineering Lecture 3 Performance, Technology & Delay Modeling A Smattering of Prerequisite Topics January 28, 2003 John Kubiatowicz (http ...
  • Peer Tutors for Math Skills and Science Major Retention

    Peer Tutors for Math Skills and Science Major Retention

    Peer Tutors for Math Skills and Science Major Retention Dr. Barbara Reitsma, Assistant Director Math and Science Skills Center, Kenyon College Presented at Enriching the Academic Experience of College Students Science Learning Center, U. Michigan, May 22-24, 2007
  • How effective were the Labour reforms in creating a welfare ...

    How effective were the Labour reforms in creating a welfare ...

    How effective were the Labour reforms in creating a welfare state? Background. ... Historians Sked and Cook describe the NHS 'almost revolutionary social innovation since it improved the quality of life of most of the British people' ... How effective...
  • Fundamental of Programming (C)

    Fundamental of Programming (C)

    Input and Output in C. C has no built-in statements for IO. The I/O library functions are in <stdio.h> All input and output is performed with streams. A stream is a sequence of characters organized into lines. Standard types of...