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 #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);
6
Sample Kernel Module: hello.c Module headers #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);
7
Sample Kernel Module: hello.c #include #include 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 #include 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 #include 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
$> man printk
18
Kernel Headers
#include /* module stuff */ #include /* module stuff */ #include /* locks */ #include /* linked lists */ #include /* string functions! */ Look inside linux-2.6.39.4/include/ for more… Google is also your friend 19
Project 2: Kernel Modules
20
procfs Kernel Module
procfs “hello world” example
Steps
Creates a read-only procfs entry Create entry in module_init function Register reading function with procfs_read Delete entry in module_cleanup function
Reference
Linux Kernel Module Programming Guide: Proc FS 21
Procfs: Headers and Global Data #include #include #include MODULE_LICENSE(“GPL”); #define ENTRY_NAME “helloworld” #define PERMS 0644 #define PARENT NULL struct proc_dir_entry *proc_entry; int procfile_read(char *buf, char **buf_location, off_t offset, int buffer_length, int *eof, void *data); 22
Procfs: Creation int hello_proc_init(void) { proc_entry = create_proc_entry(ENTRY_NAME, PERMS,PARENT); /* check proc_entry != NULL */ proc_entry->read_proc = procfile_read;
proc_entry->write_proc = procfile_write; proc_entry->mode = S_IFREG | S_IRUGO; proc_entry->uid = 0; proc_entry->gid = 0; proc_entry->size = 11; printk(“/proc/%s created\n”, ENTRY_NAME); return 0; }
23
Procfs: Reading int procfile_read(char *buf, char **buf_location, off_t offset, int buffer_length, int *eof, void *data) { int ret; printk(“/proc/%s read called.\n”, ENTRY_NAME); /* Setting eof. We exhaust all data in one shot */ *eof = 1; ret = sprintf(buf, “Hello World!\n”); return ret; } 24
Procfs: Writing int procfile_write( struct file *file, const char __user *buffer, unsigned long count, void *data ) { char *page; /* don't touch */ if (!page) return -ENOMEM; /* Copy the data from the user space. Data is placed in page. */ if (copy_from_user(page, buffer, count)) { vfree(page); return -EFAULT; } /* Now do something with the data, here we just print it */ printk( "Got [%s]\n", page); vfree(page); return count; } 25
Procfs: Deletion void hello_proc_exit(void) { remove_proc_entry(ENTRY_NAME, NULL); printk(“Removing /proc/%s.\n”, ENTRY_NAME); }
26
Procfs: Registration module_init(hello_proc_init); module_exit(hello_proc_exit);
27
Testing Procfs $> $> $> $> $>
sudo insmod hello_proc.ko sudo tail /var/log/syslog cat /proc/helloworld echo 2 > /proc/helloworld sudo rmmod hello_proc
28
Part 2: Backwards
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, only backwards
29
Backwards Example $> echo “Hello there” > /proc/remember $> cat /proc/backwards ereht olleH $>
30
Part 3: Printer Scheduling
31
Part 3: The Penguin Printer
Implement a /proc kernel module that simulates a busy printer Your /proc/penguin file must accept writes of different printer job sizes
Each dish takes some time to process
Your /proc/penguin file must return status information of the printer when read
32
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.
33
Your Printer
One printer 20 job slots
Use a static array of ints?
Four types of print jobs
1-page document (internally represented with a 1) 2-page document (internally represented with a 2) 3-page document (internally represented with a 3) 4-page document (internally represented with a 4)
34
Print jobs
A write of 1-4 to /proc/penguin will fill a job slot with the job corresponding to the number
You decide how the job slots get filled (e.g. round robin or other way)
Printer takes 1 second to look in a job slot
Regardless if empty or containing a job
35
Print Jobs
The printer can only process one job at a time Each job takes a different amount of time to process
2 seconds for a 1-page document 3 seconds for a 2-page document 4 seconds for a 3-page document 5 seconds for a 4-page document
36
Print Jobs
Once a job is processed, the printer can mark that job slot as empty and should look at other job slots to find more jobs to process.
37
A wrench in the plans!
In addition, the printer should also accept a “maintenance” job, internally represented with a 5.
Maintenance takes a whopping 8 seconds.
38
Additional Printer Commands
In addition to accepting jobs 1-5, the printer should respond to writes of 0 or -1 in the following ways
0 : start the printer -1: stop the printer
39
Starting the Printer
Before the printer is started, it cannot be processing jobs
But it can receive jobs on the queue It just isn’t printing yet!
When a printer is stopped, it must finish processing the current job and cease to process any more
40
Starting the Printer
The actual job processing logic will be run in a kthread
Introduced in the next project lecture
41
Status Information
Performing a read on /proc/penguin should return some status information
printer status: running or not running Current spot being looked at in the queue Current job being processed
If the printer is not running, the last two pieces of information do not need to be displayed 42
Demo
43
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 44
Kernel Time Constraints #include void ssleep(unsigned int seconds);
A call to ssleep will have the program cease to the task scheduler for seconds number of seconds
45
Additional Design Considerations
How to place jobs in job slots? How to check job slots from the printer? What scheduling algorithm to use?
46
Next Time
Locking
Must lock when accessing anything global
printer queue Printer status variables
Kthread
How to start/stop the looping logic that looks in the job queue and “processes” jobs
47