platform/x86: wdat_wdt: Fix ACPI table leak on probe errors
PATCH 1/1 platform/x86: wdat_wdt: Fix ACPI table leak on probe errors
wdat_wdt_probe() calls acpi_get_table() to obtain the WDAT ACPI table but returns early on multiple error paths without calling acpi_put_table(). This causes a permanent ACPI table memory leak if the driver probe fails.
Add a single cleanup path which calls acpi_put_table() and redirect all error returns to it, ensuring the ACPI table is always released.
--- drivers/watchdog/wdat_wdt.c | 66 ++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 22 deletions(-)
drivers/watchdog/wdat_wdt.c +44 -22
@@ -318,7 +318,7 @@ static int wdat_wdt_probe(struct platform_device *pdev)struct resource *res;void __iomem **regs;acpi_status status; -int i, ret; +int i, ret =0 ; status =acpi_get_table (ACPI_SIG_WDAT ,0 , (struct acpi_table_header **)&tbl);
@@ -326,19 +326,27 @@ static int wdat_wdt_probe(struct platform_device *pdev)return -ENODEV ; wdat =devm_kzalloc (dev,sizeof (*wdat),GFP_KERNEL ); -if (!wdat) -return -ENOMEM ; +if (!wdat) { + ret = -ENOMEM ; + goto out_put_table; + } regs =devm_kcalloc (dev, pdev->num_resources,sizeof (*regs),GFP_KERNEL ); -if (!regs) -return -ENOMEM ; +if (!regs) { + ret = -ENOMEM ; + goto out_put_table; + }/* WDAT specification wants to have >= 1ms period */ -if (tbl->timer_period <1 ) -return -EINVAL ; -if (tbl->min_count > tbl->max_count) -return -EINVAL ; +if (tbl->timer_period <1 ) { + ret = -EINVAL ; + goto out_put_table; + } +if (tbl->min_count > tbl->max_count) { + ret = -EINVAL ; + goto out_put_table; + } wdat->period = tbl->timer_period; wdat->wdd.min_timeout =DIV_ROUND_UP (wdat->period * tbl->min_count,1000 );
@@ -355,15 +363,20 @@ static int wdat_wdt_probe(struct platform_device *pdev) res = &pdev->resource[i];if (resource_type (res) ==IORESOURCE_MEM ) { reg =devm_ioremap_resource (dev, res); -if (IS_ERR (reg)) -return PTR_ERR (reg); +if (IS_ERR (reg)) { + ret =PTR_ERR (reg); + goto out_put_table; + } }else if (resource_type (res) ==IORESOURCE_IO ) { reg =devm_ioport_map (dev, res->start,1 ); -if (!reg) -return -ENOMEM ; +if (!reg) { + ret = -ENOMEM ; + goto out_put_table; + } }else {dev_err (dev,"Unsupported resource\n" ); -return -EINVAL ; + ret = -EINVAL ; + goto out_put_table; } regs[i] = reg;
@@ -385,8 +398,10 @@ static int wdat_wdt_probe(struct platform_device *pdev) } instr =devm_kzalloc (dev,sizeof (*instr),GFP_KERNEL ); -if (!instr) -return -ENOMEM ; +if (!instr) { + ret = -ENOMEM ; + goto out_put_table; + }INIT_LIST_HEAD (&instr->node); instr->entry = entries[i];
@@ -417,7 +432,8 @@ static int wdat_wdt_probe(struct platform_device *pdev)if (!instr->reg) {dev_err (dev,"I/O resource not found\n" ); -return -EINVAL ; + ret = -EINVAL ; + goto out_put_table; } instructions = wdat->instructions[action];
@@ -425,8 +441,10 @@ static int wdat_wdt_probe(struct platform_device *pdev) instructions =devm_kzalloc (dev,sizeof (*instructions),GFP_KERNEL ); -if (!instructions) -return -ENOMEM ; +if (!instructions) { + ret = -ENOMEM ; + goto out_put_table; + }INIT_LIST_HEAD (instructions); wdat->instructions[action] = instructions;
@@ -443,7 +461,7 @@ static int wdat_wdt_probe(struct platform_device *pdev) ret =wdat_wdt_enable_reboot (wdat);if (ret) -return ret; + goto out_put_table;platform_set_drvdata (pdev, wdat);
@@ -460,12 +478,16 @@ static int wdat_wdt_probe(struct platform_device *pdev) ret =wdat_wdt_set_timeout (&wdat->wdd, timeout);if (ret) -return ret; + goto out_put_table;watchdog_set_nowayout (&wdat->wdd, nowayout);watchdog_stop_on_reboot (&wdat->wdd);watchdog_stop_on_unregister (&wdat->wdd); -return devm_watchdog_register_device (dev, &wdat->wdd); + ret =devm_watchdog_register_device (dev, &wdat->wdd); + +out_put_table: +acpi_put_table ((struct acpi_table_header *)tbl); +return ret; }static int wdat_wdt_suspend_noirq (struct device *dev)
Signature · 1 line
2.50.1.windows.1
Guenter Roeck
maintainerQuoted 2 lines · Haotian Zhang
wdat_wdt_probe()callsacpi_get_table()to obtain the WDAT ACPI table but returns early on multiple error paths without callingacpi_put_table().
It _never_ calls acpi_put_table().
Quoted 2 lines
This causes a permanent ACPI table memory leak if the driver probe fails.
Seems to me that itdoesn't just cause a table leak if probe fails.
Quoted 14 lines
Add a single cleanup path which calls
acpi_put_table()and redirect all error returns to it, ensuring the ACPI table is always released.Fixes 058dfc767008 ("ACPI / watchdog: Add support for WDAT hardware watchdog")Signed-off-by Haotian Zhang <[email protected]>--- drivers/watchdog/wdat_wdt.c | 66 ++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 22 deletions(-)drivers/watchdog/wdat_wdt.c +0
-0
drivers/watchdog/wdat_wdt.c static int wdat_wdt_probe(struct platform_device *pdev) · line 321
Quoted 5 lines
drivers/watchdog/wdat_wdt.c +1
-1@@ -318,7 +318,7 @@ static int wdat_wdt_probe(struct platform_device *pdev)struct resource *res;void __iomem **regs;acpi_status status; -int i, ret; +int i, ret =0 ;
Unnecessary initialization.
Guenter
Guenter Roeck
maintainerQuoted 9 lines · Haotian Zhang
wdat_wdt_probe()callsacpi_get_table()to obtain the WDAT ACPI table but returns early on multiple error paths without callingacpi_put_table(). This causes a permanent ACPI table memory leak if the driver probe fails.Add a single cleanup path which calls
acpi_put_table()and redirect all error returns to it, ensuring the ACPI table is always released.Fixes 058dfc767008 ("ACPI / watchdog: Add support for WDAT hardware watchdog")Signed-off-by Haotian Zhang <[email protected]>
I just noticed that the subject line starts with "platform/x86:" instead of "watchdog:".
Why ?
Guenter