devm_kzalloc allocated memory is automatically released when the driver uninstall

In gpio-button-hotplug.c inside, this code:

buttons = devm_kzalloc(dev, pdata->nbuttons * sizeof(struct gpio_keys_button),
           GFP_KERNEL);
if (!buttons) {
    dev_err(dev, "no memory for button data\n");
    return -ENOMEM;
}
memcpy(buttons, pdata->buttons, pdata->nbuttons * sizeof(struct gpio_keys_button));

bdev = devm_kzalloc(dev, sizeof(struct gpio_keys_button_dev) +
           pdata->nbuttons * sizeof(struct gpio_keys_button_data),
           GFP_KERNEL);
if (!bdev) {
    dev_err(dev, "no memory for private data\n");
    return -ENOMEM;
}

If bdev memory allocation fails, the function returns directly, buttons pointer to the memory space will be a memory leak it? The answer is no.

Because there is devm_kzalloc application buttons, the interface application memory is automatically released when the driver uninstall. This function is gpio_keys_button_probe, if returned fails, the driver will be uninstalled, buttons pointing to the space will be freed. So the logic here is not cause memory leaks.

Guess you like

Origin www.cnblogs.com/gimo/p/11784615.html