I have found devm_kzalloc()
and kzalloc()
in device driver programmong. But I don't know when/where to use these functions. Can anyone please specify the importance of these functions and their usage.
-
1Hi, If we use devm_kzalloc() then no need to free this memory (ref: [link](http://docs.blackfin.uclinux.org/kernel/generated/device-drivers/re162.html)). So can we replace all the kzalloc() functions with devm_kzalloc() in the programs since we can reduce the burden of freeing the dynamically allocated memory..? – Raj Sep 04 '12 at 05:34
-
I believe that [devres.txt](https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/driver-model/devres.txt) will answer most questions. The 'devres.txt' should be current for your Linux version (from 2.6.31+). – artless noise Apr 09 '15 at 17:17
-
No. You can not replace **all** allocations with managed analogous, because on has to carefully think about objects life time. – 0andriy Mar 13 '19 at 06:50
2 Answers
kzalloc()
allocates kernel memory like kmalloc()
, but it also zero-initializes the allocated memory. devm_kzalloc()
is managed kzalloc()
. The memory allocated with managed functions is associated with the device. When the device is detached from the system or the driver for the device is unloaded, that memory is freed automatically. If multiple managed resources (memory or some other resource) were allocated for the device, the resource allocated last is freed first.
Managed resources are very helpful to ensure correct operation of the driver both for initialization failure at any point and for successful initialization followed by the device removal.
Please note that managed resources (whether it's memory or some other resource) are meant to be used in code responsible for the probing the device. They are generally a wrong choice for the code used for opening the device, as the device can be closed without being disconnected from the system. Closing the device requires freeing the resources manually, which defeats the purpose of managed resources.
The memory allocated with kzalloc()
should be freed with kfree()
. The memory allocated with devm_kzalloc()
is freed automatically. It can be freed with devm_kfree()
, but it's usually a sign that the managed memory allocation is not a good fit for the task.
-
2It really worth to mention that the use case for managed resources is `->probe()` or alike callback. It would be bad, **bad idea** to use them in callbacks like `->open()`. Also there is a problem with life time of the objects in case of file operations in use. – 0andriy Feb 06 '18 at 17:37
-
I've expanded my reply to mention it, thank you. If there are other reasons for avoid managed resources in open(), please post a link. – proski Feb 20 '18 at 01:58
In simple words devm_kzalloc() and kzalloc() both are used for memory allocation in device driver but the difference is if you allocate memory by kzalloc() than you have to free that memory when the life cycle of that device driver is ended or when it is unloaded from kernel but if you do the same with devm_kzalloc() you need not to worry about freeing memory,that memory is freed automatically by device library itself.
Both of them does the exactly the same thing but by using devm_kzalloc little overhead of freeing memory is released from programmers
Let explain you by giving example, first example by using kzalloc
static int pxa3xx_u2d_probe(struct platform_device *pdev)
{
int err;
u2d = kzalloc(sizeof(struct pxa3xx_u2d_ulpi), GFP_KERNEL); 1
if (!u2d)
return -ENOMEM;
u2d->clk = clk_get(&pdev->dev, NULL);
if (IS_ERR(u2d->clk)) {
err = PTR_ERR(u2d->clk); 2
goto err_free_mem;
}
...
return 0;
err_free_mem:
kfree(u2d);
return err;
}
static int pxa3xx_u2d_remove(struct platform_device *pdev)
{
clk_put(u2d->clk);
kfree(u2d); 3
return 0;
}
In this example you can this in funtion pxa3xx_u2d_remove(), kfree(u2d)(line indicated by 3) is there to free memory allocated by u2d now see the same code by using devm_kzalloc()
static int pxa3xx_u2d_probe(struct platform_device *pdev)
{
int err;
u2d = devm_kzalloc(&pdev->dev, sizeof(struct pxa3xx_u2d_ulpi), GFP_KERNEL);
if (!u2d)
return -ENOMEM;
u2d->clk = clk_get(&pdev->dev, NULL);
if (IS_ERR(u2d->clk)) {
err = PTR_ERR(u2d->clk);
goto err_free_mem;
}
...
return 0;
err_free_mem:
return err;
}
static int pxa3xx_u2d_remove(struct platform_device *pdev)
{
clk_put(u2d->clk);
return 0;
}
there is no kfree() to free function because the same is done by devm_kzalloc()

- 90
- 5

- 55
- 1
- 7