Error handling in the kernel

A general practice in the kernel is after calling the function, to check whether the function has succeeded or failed. Depending on the function there are ways of checking for failure of a function.

Sometimes kernel developers do not add error checks on a function. This is actually not good practice. Error handling is indeed a practice that should be employed everywhere.

There are two types of error checks mainly: IS_ERR check and a NULL check.

var = somefunction(…);

An IS_ERR check looks something like this:

if (IS_ERR(var))

return PTR_ERR(var);

Whereas a NULL check may look like this:

if (!var)

return -ENOMEM;

You can find out the correct type of error handling for each function by checking the definition of the function, lxr is the easiest way to find out. Check what the function is returning in case of error. If the function returns NULL, we need a NULL check and if the function is returning an ERR_PTR, we need a IS_ERR check.

So for the last 2-3 weeks, I worked on an additional script wherein I found out functions in the kernel which are using both NULL check and IS_ERR checks which is mostly wrong. One particular function should have only one type of error handling. I found around 53 results where error checks are ambiguous and now I am consistently checking each of these results and finding out the correct error check and modifying the cases where incorrect check has been used.


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s