Back to the Club's homepage

The Right Way: Managed Resource Allocation in Linux Device Drivers - Eli Billauer


Linux device drivers typically call kmalloc(), request_mem_region(),  ioremap() and other kernel API functions to obtain resources. Failing to release them correctly when the device is removed leads to resource leaks and possibly oopses. Even worse, if the initialization fails in the middle, the allocation must be unrolled, which is typically done with goto statements which are bug-prone.

The Device Managed Resources (devres, devm) API was added to the kernel in 2007 (2.6.21) to solve this problem: The driver is only required to use special functions to allocate its resources, but not to release them. The kernel is responsible to free the resources when the device is removed or if it fails to initialize. This doesn’t just make the code shorter, but ensures that the removal of the device takes place correctly.

This method makes so much sense, that it isn’t clear how it’s not mandatory. In fact, many essential allocation function don’t have a managed counterpart in the kernel, which shows that this issue is quite neglected.

Aside from preaching, this talk will focus on showing code snippets and examples to clarify how this works. Subjects to be covered include

Download the lecture's slides in pdf format.

Download an audio recording of the talk.

Back to the Club's homepage