Kernel Modules

advertisement
Intro to Kernel Modules and
/proc
Sarah Diesburg
CS 3430
Operating Systems
1
Kernel Modules
Or “drivers”, if you prefer…
2
Kernel Module


A kernel module is a portion of kernel
functionality that can be dynamically loaded
into the operating system at run-time
Example




USB drivers
File system drivers
Disk drivers
Cryptographic libraries
3
Why not just compile everything
into the kernel?

Each machine only needs a certain number
of drivers


Load only the modules you need


For example, should not have to load every single
motherboard driver
Smaller system footprint
Dynamically load modules for new devices

Camera, new printer, etc.
4
Creating a Kernel Module

Hello world example
5
Sample Kernel Module: 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);
6
Sample Kernel Module: hello.c
Module headers
#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);
7
Sample Kernel Module: hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(“Dual BSD/GPL”);
License
declaration
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
Sample Kernel Module: 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;
}
Initialization
function, runs
when module
loaded
static void hello_exit(void)
{
printk(KERN_ALERT “Goodbye, sleepy world.\n”);
}
module_init(hello_init);
module_exit(hello_exit);
Tells kernel which
function to run on
load
9
Sample Kernel Module: 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;
}
Exit function, runs
when module exits
static void hello_exit(void)
{
printk(KERN_ALERT “Goodbye, sleepy world.\n”);
}
module_init(hello_init);
module_exit(hello_exit);
Tells kernel which
function to run on
exit
10
Sample Kernel Module: Makefile
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KERNELDIR ?= \
/lib/modules/`uname -r`/build/
PWD := `pwd`
default:
$(MAKE) -C $(KERNELDIR) \
M=$(PWD) modules
endif
clean:
rm -f *.ko *.o Module* *mod*
11
Compile the Kernel Module
/usr/src/hello$> make

Creates hello.ko – This is the finished kernel
module!
12
Inserting and Removing the Module

insmod – insert a module
/usr/src/hello$> sudo insmod hello.ko

rmmod – remove a module
/usr/src/hello$> sudo rmmod hello.ko
13
Listing Modules

lsmod – lists all running modules
/usr/src/hello$>lsmod
14
Where is it printing?


Look inside /var/log/syslog
Hint – to watch syslog in realtime, issue the
following command in a second terminal:
$> sudo tail –f /var/log/syslog

Demo…
15
Kernel Module vs User Application

All kernel modules are event-driven




No standard C library



Register functions
Wait for requests and service them
Server/client model
Why not?
No floating point support
Segmentation fault could freeze/crash your
system

Kernel ‘oops’!
16
Kernel Functions




printk() instead of printf()
kmalloc() instead of malloc()
kfree() instead of free()
Where can I find definitions of these kernel
functions?
17
Kernel manpages


Section 9 of manpages
Use manpages on prog1
$> sudo apt-get install linux-manual-3.16
$> man printk
18
Kernel Headers







#include <linux/init.h> /* module stuff */
#include <linux/module.h> /* module stuff */
#include <asm/semaphore.h> /* locks */
#include <linux/list.h> /* linked lists */
#include <linux/string.h> /* string functions! */
Look inside linux-4.2.2/include/ for more…
Google is also your friend
19
Project 2: Kernel Modules
20
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
21
Testing Procfs
$>
$>
$>
$>
$>
sudo insmod hello_proc.ko
sudo tail /var/log/syslog
cat /proc/helloworld
echo 2 > /proc/helloworld
sudo rmmod hello_proc
22
Part 2: 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
23
Remember Example
#> echo “Hello there” > /proc/remember
#> cat /proc/backwards
Hello there
#>
24
Part 3: Kitchen Scheduling
25
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
26
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.
27
Your Kitchen


One kitchen
20 order slots


Use a static array of ints?
Four types of dishes




1 - Caesar Salad
2 - Hamburger
3 - Personal Pizza
4 - Beef Wellington
28
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
29
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
30
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.
31
Additional Kitchen Commands

In addition to accepting orders 1-4, a kitchen
should respond to writes of 0 or -1 in the
following ways


0 : start the kitchen
-1: stop the kitchen
32
Starting the Kitchen

Before a kitchen is started, it cannot be
processing orders



But it can receive orders on the queue
The cooks just aren’t working yet!
When a kitchen is stopped, it must finish
processing the current order and cease to
process any more
33
Starting the Kitchen

The actual order processing logic will be run
in a kthread

Introduced in the next recitation lecture
34
Status Information

Performing a read on /proc/kitchen should
return some status information




Kitchen status: running or not running
Current spot being looked at in the queue
Current order being processed
If the kitchen is not running, the last two
pieces of information do not need to be
dislplayed
35
Demo
36
Scheduling Algorithms

A scheduling algorithm considers the state of
the consumers and all requests and tries to
optimize some metric




Throughput: Maximize total requests, minimize
processing total time.
Priorities: Requests now have deadlines.
Maximize number of requests meeting deadlines.
Burst throughput: Maximize peak requests that
can be handled.
Energy: Minimize consumer action
37
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
38
Additional Design Considerations



How to place orders in order slots?
How to check order slots from the kitchen?
What scheduling algorithm to use?
39
Next Time

Locking

Must lock when accessing anything global



Kitchen queue
Kitchen status variables
Kthread

How to start/stop the looping logic that looks in the
order queue and “processes” orders
40
Download