Skip to content (access key 's')
Logo of Technion
Logo of CS Department
Logo of CS4People

The Taub Faculty of Computer Science Events and Talks

Haifux Club: The Right Way: Managed Resource Allocation in Linux Device Drivers
event speaker icon
Eli Billauer:
event date icon
Monday, 26.05.2014, 18:30
event location icon
Taub 6
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

* How to use managed resources
* How to free managed resources manually, in particular unrolling a partial initialization sequence (”Undo”)
* How to add custom function callbacks for specific rollback tasks
* How to write a custom managed resource allocator
* (maybe) The special functions for PCI