Clean the kernel code

Hi all,

This post will cover about some widely used tools to clean up the Linux kernel code. I am going to write about the three tools that I use for serving the purpose. So, here goes the list:

  • Sparse : a tool for static code analysis that helps kernel developers to detect coding errors. Kernel code that is prone to mistakes is annotated by kernel developers using the ‘attribute’ specifiers. Sparse tool uses these specifiers to pinpoint coding mistakes.

How to install and use sparse? might help.

Also, see the documentation here.

  • : a perl script to check if your code follows Linux kernel coding style.

Following is the syntax to use it on a file:

$./scripts/ --file --terse <relative-path-to-the-c-file>

If you are cleaning up staging driver, make sure that you are under git/kernels/staging directory to run above command else replace ‘.’ with the absolute path to checkpatch.

  • Coccinelle : a program matching and transformation engine which provides the language SmPL (Semantic Patch Language) for specifying desired matches and transformations in C code.

What does this tool do? Well, it can do almost everything. Got to find out useless code? You have coccinelle do it for you. Need to find out unnecessary casts? You have coccinelle. How about checking for correct resource allocation? Coccinelle will do it for you. Basically, Coccinelle is a wonderful tool that can find out almost all kinds of bugs in a code provided you write the correct script for it.

I don’t really know how to write cocci scripts. I just know very basic ones. I used the already made scripts to find out bugs. Those scripts, their need and description has been provided here : Rules for coccinelle. I was helped a lot by Julia Lawall about writing scripts for finding our unnecessary casts, unnecessary wrapper functions, etc. She maintains and develops the tool. You can contact her directly if you want to contribute to coccinelle or report any errors. She has a very good and elaborate presentation on Introduction to coccinelle.

So, these are some of the tools that can help to clean up the kernel code. There are other tools too but I’m not writing about them as I have no experience with them.

Tip : It would be good to automate the process of finding out errors in the kernel code by writing shell scripts.

I did that and it was then convenient to look up for bugs in the generated logs. It saves a lot of effort and typing.

Explore other tools. Use the above mentioned too. Submit as many patches as you can.

Thanks for reading.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s