Coccinellery – Part 2

As a continuation to my last post, I will continuetalking about the interesting scripts in Coccinellery.

simplify_ioremap_resource.cocci is a script that detects case where there is error handling for a platform_get_resource call just before a call to devm_ioremap_resource. This error handling is not required and should be removed. Thisthis and this are example patches that have been added to the kernel already.

simple_kzalloc.cocci finds cases where kmalloc and memset 0 are used since they can be replaced by a direct call to kzalloc. After using this script Julia suggested finding more functions like these where memset and the function call can be replaced by a direct function call. For example: vmalloc/vzalloc is a similar case and a patch accepted for this case.

kmemdup.cocci is used to find cases where memory is allocated followed by a call to memcpy. These two calls can be replaced by a direct call to kmemdup. This is an example patch.

moduleparam.cocci is a script to find cases where both module.h and moduleparam.h are included. module.h contains a call to moduleparam.h making the call to it redundant and useless. These are some accepted patches for the same: one and two.

export_static.cocci finds cases where a static function is being exported which is not possible. Sadly could not find any cases to submit patches on.


Coccinellery – Part 1

This week I’m going to talk of some of the interesting scripts I’ve found in Coccinellery.

There are A LOT of scripts in coccinellery which you can see here.

One of the first few scripts that I submitted a patch for was using badcheck.cocci. This script checks the devm_ioremap_resource error detection code. Some functions in the kernel like devm_ioremap_resource use an IS_ERR test instead of a NULL test for error detection but in many places in the kernel developers have not updated the error detection code. Excitingly, this script, if all goes well, after some required updation I will be sending to the kernel 🙂

bugon.cocci is a script that replaces an if condition and a BUG() statement with a simple BUG_ON() statement. You can see an example patch that I submitted and has been applied here.

Some functions like kmem_cache_alloc, kmem_cache_zalloc, etc have a corresponding freeing function known as kmem_cache_free; but some developers have used kfree instead of kmem_cache_free. A script to solve this issue is given here.

countptr.cocci is a script that removes an exceptional ‘&’ in front of a function name. Interestingly this one gives a lot of results in the kernel and is a good semantic patch to submit and get patches accepted. It is very easy too 🙂

Typically, we use devm_kzalloc in probe function of a driver to allocate managed resources. But sometimes, both devm_kzalloc and kzalloc are used in the probe function. This is generally an incorrect practice and devm functions should be used consistently. These inconsistencies can be found with the help of this script.

Two scripts on devm functions are given here and here which find places where freeing functions like iounmap and kfree have been used for devm_ allocated data.

There are lots of interesting scripts on devm functions and a number of opportunities to submit patches on the same.


Interesting times ahead indeed!