Possible mem leak in drivers handling multiple HIDs with single driver->ops.add
Hi ACPI folk,
I was looking at the following ACPI code in 2.6.16-rc3. I'm trying to understand if there maybe a possible memory leak in the following scenario. Here's what I see:
drivers/acpi/button.c calls:
464 result = acpi_bus_register_driver(&acpi_button_driver);
with acpi_button_driver containing:
77 .ids = "ACPI_FPB,ACPI_FSB,PNP0C0D,PNP0C0C,PNP0C0E",
Now, acpi_bus_register_driver calls acpi_driver_attach which does the following:
560 list_for_each_safe(node, next, &acpi_device_list) {
...
568 if (!acpi_bus_match(dev, drv)) {
569 if (!acpi_bus_driver_init(dev, drv)) {
In iterating over the acpi_device_list, are there possible scenarios of hardware containing multiple matching HIDS where acpi_bus_match (which matches individual device HIDs to the driver's supported .ids) ends up matching for a single driver and then calling acpi_bus_driver_init which ends up calling that single driver's add function multiple times:
514 result = driver->ops.add(device);
In the button.c case, this calls:
300 static int acpi_button_add(struct acpi_device *device)
...
311 button = kmalloc(sizeof(struct acpi_button), GFP_KERNEL);
In which case, acpi_button_add is called multiple times and we leak all but the last kmalloc of button, as well as other stuff like proc setup, etc.
To summarize the question, is it possible for one overall ACPI setup to have multiple HIDs that then map to a single driver's add function? If so, then I believe that button.c and container.c could leak memory.
If you think that this situation is possible, then I'll do a small patch to fixup button.c and container.c.
Thanks,
jayakumar
Quoted 16 lines · Jaya Kumar
... I'm trying to
understand if there maybe a possible memory leak in the following scenario. Here's what I see:
drivers/acpi/button.ccalls:
464 result =acpi_bus_register_driver(&acpi_button_driver);with
acpi_button_drivercontaining:
77 .ids = "ACPI_FPB,ACPI_FSB,PNP0C0D,PNP0C0C,PNP0C0E",Now,
acpi_bus_register_drivercallsacpi_driver_attachwhich does the following:
560list_for_each_safe(node, next, &acpi_device_list){
...
568 if (!acpi_bus_match(dev, drv)) {
569 if (!acpi_bus_driver_init(dev, drv)) {
Quoted 8 lines
In iterating over the
acpi_device_list, are there possible scenarios of hardware containing multiple matching HIDS whereacpi_bus_match(which matches individual device HIDs to the driver's supported .ids) ends up matching for a single driver and then callingacpi_bus_driver_initwhich ends up calling that single driver's add function multiple times:514 result =
driver->ops.add(device);
An ACPI device may have multiple IDs (a _HID and zero or more _CIDs).
But the loop body in acpi_driver_attach() is only executed once per device (not once per device ID), so acpi_bus_driver_init() is called at most once per device. And acpi_bus_driver_init() calls the .add() method only once.
I don't see the scenario you're concerned about. Did I miss something?
Quoted 19 lines
In the
button.ccase, this calls:300 static int
acpi_button_add(struct acpi_device *device)
...
311 button =kmalloc(sizeof(struct acpi_button), GFP_KERNEL);In which case,
acpi_button_addis called multiple times and we leak all but the last kmalloc of button, as well as other stuff like proc setup, etc.To summarize the question, is it possible for one overall ACPI setup to have multiple HIDs that then map to a single driver's add function? If so, then I believe that
button.candcontainer.ccould leak memory.If you think that this situation is possible, then I'll do a small patch to fixup
button.candcontainer.c.Thanks,
jayakumar
Quoted 9 lines · Bjorn Helgaas
But the loop body in
acpi_driver_attach()is only executed once per device (not once per device ID), soacpi_bus_driver_init()is called at most once per device. Andacpi_bus_driver_init()calls the .add() method only once.I don't see the scenario you're concerned about. Did I miss something?
The scenario I was asking about is different. I'm asking whether it's possible that a system has multiple ACPI devices that each have at least one HID that maps to a single driver add(). Here's a scenario I was thinking about, let's say the BIOS states that I have an ACPI device that handle miscellaneous control buttons:
Device (FOO1)
{
Name (_HID, "PNP0C0D")and on the same system, the BIOS states that I have another ACPI device that handles the power button:
Device (FOO2)
{
Name (_HID, "ACPI_FPB")then this scenario would result in button.c's add() getting called twice. Right?
Thanks,
jayakumar
Quoted 15 lines · Jaya Kumar
Quoted 5 lines · Bjorn Helgaas
But the loop body in
acpi_driver_attach()is only executed once per device (not once per device ID), soacpi_bus_driver_init()is called at most once per device. Andacpi_bus_driver_init()calls the .add() method only once.The scenario I was asking about is different. I'm asking whether it's possible that a system has multiple ACPI devices that each have at least one HID that maps to a single driver
add(). Here's a scenario I was thinking about, let's say the BIOS states that I have an ACPI device that handle miscellaneous control buttons:Device (FOO1) { Name (_HID, "PNP0C0D")
Quoted 7 lines
and on the same system, the BIOS states that I have another ACPI device that handles the power button:
Device (FOO2) { Name (_HID, "ACPI_FPB")then this scenario would result in button.c's
add()getting called twice. Right?
Yes. But you have two devices, and acpi_button_add() will get called once for each of them, which is exactly what we want. Your original question was about this line in acpi_button_add():
311 button = kmalloc(sizeof(struct acpi_button), GFP_KERNEL);
This kmalloc will happen for every button device, but the pointer is attached to the acpi_device:
acpi_driver_data(device) = button;
and the corresponding kfree is in acpi_button_remove().
If you still think there is a problem, maybe it will be clear to me if you post the patch to fix it.
Bjorn
Quoted 4 lines · Bjorn Helgaas
Yes. But you have two devices, and
acpi_button_add()will get called once for each of them, which is exactly what we want. Your original question was about this line inacpi_button_add():
Quoted 8 lines
311 button =
kmalloc(sizeof(struct acpi_button), GFP_KERNEL);This kmalloc will happen for every button device, but the pointer is attached to the
acpi_device:acpi_driver_data(device) = button;and the corresponding kfree is in
acpi_button_remove().
You are right. I see acpi_button_add_fs() also handles the multiple call case correctly. Thanks for helping explain this.
jayakumar