CSE 551 -- Programming Assignment #2

Out: Thursday April 12th, 2007
Due: Tuesday April 24th, 2007, before class

Overview

For this assignment, you will write a linux kernel module. When inserted, your module should create a new entry in the /proc filesystem called:

/proc/get_num_pagefaults
When somebody cat's that file, it should print out the number of page faults that the operating system has handled since it booted. So, for example, you ought to be able to reproduce the following session once you have your new kernel module compiled:
[root@localhost assignment2]# ls -al /proc/get_num_pagefaults
ls: /proc/get_num_pagefaults: No such file or directory
[root@localhost assignment2]# /sbin/insmod ./gribble_module.ko 
[root@localhost assignment2]# ls -al /proc/get_num_pagefaults
-r--r--r--  1 root root 37 Apr 11 21:38 /proc/get_num_pagefaults
[root@localhost assignment2]# cat /proc/get_num_pagefaults 
658103
[root@localhost assignment2]# cat /proc/get_num_pagefaults 
658295
[root@localhost assignment2]# cat /proc/get_num_pagefaults 
658485
[root@localhost assignment2]# /sbin/rmmod gribble_module
[root@localhost assignment2]# ls -al /proc/get_num_pagefaults
ls: /proc/get_num_pagefaults: No such file or directory
[root@localhost assignment2]#

Getting started

First, you need to get yourself a development environment that will allow you to experiment with kernel modules. For this, you'll need a machine that you can install linux on, get root access, compile new kernels, and compile/install new kernel modules. I strongly recommend you do your work inside a virtual machine. So, here are some steps you can take to get going: Next, you need to try compiling and installing a simple "Hello World" kernel module. For a nice tutorial on linux kernel module programming, check out http://tldp.org/LDP/lkmpg/2.6/html/index.html. This tutorial includes several hello world examples, as well as information on how to insert yourself into the /proc filesystem.

Next steps

Now, you're ready to go ahead and solve the assignment. I found it useful to break things down into a few steps. First, successfully create yourself a kernel module that prints out a fixed string when somebody cat's the /proc/get_num_pagefaults file. Next, figure out how you are going to get statistics on the number of pagefaults from the kernel.

Hint: the linux kernel already keeps track of this statistic, and the kernel exports the appropriate symbol that you need to access that statistic from your module.

Hint 2: if you look in the arch/i386/mm/fault.c source code file of the linux kernel, you'll see a function called do_page_fault, which is the first real function in the kernel that gets called on a page fault. Inside that function, you can see that it invokes another function called "handle_mm_fault" to actually handle the page fault. Find the handle_mm_fault function (it's somewhere else in the kernel source tree, in the archtecture-independent part), and keep poking through the code path to see if you find something useful.

Hint 3: not all functions and variables in the kernel code are accessible to your kernel module. A symbol has to be explicitly exported by the kernel to be accessible to your module's code. The kernel uses the EXPORT_SYMBOL macro to export a particular symbol, making it accessible to your module.

Hint 4: you might need to declare that your kernel module is licensed under the GPL; some kernel symbols are only exported to kernel modules that declare themselves to do this. To declare it, include the following line of code in your kernel module source, just after your includes:

MODULE_LICENSE("GPL");

What to turn in

Please email Steve and Elizabeth the following: