Re: [PATCH] ipmi: use dynamic memory for DMI driver override

From: Corey Minyard
Date: Mon Jan 22 2018 - 08:46:23 EST


On 01/17/2018 11:06 AM, John Garry wrote:
On 17/01/2018 16:49, Corey Minyard wrote:
On 01/17/2018 10:36 AM, John Garry wrote:
Currently a crash can be seen if we reach the "err"
label in dmi_add_platform_ipmi(), calling
platform_device_put(), like here:
[ÂÂÂ 7.270584]Â (null): ipmi:dmi: Unable to add resources: -16
[ÂÂÂ 7.330229] ------------[ cut here ]------------
[ÂÂÂ 7.334889] kernel BUG at mm/slub.c:3894!
[ÂÂÂ 7.338936] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP
[ÂÂÂ 7.344475] Modules linked in:
[ÂÂÂ 7.347556] CPU: 1 PID: 1 Comm: swapper/0 Not tainted
4.15.0-rc2-00004-gbe9cb7b-dirty #114
[ÂÂÂ 7.355907] Hardware name: Huawei Taishan 2280 /D05, BIOS Hisilicon
D05 IT17 Nemo 2.0 RC0 11/29/2017
[ÂÂÂ 7.365137] task: 00000000c211f6d3 task.stack: 00000000f276e9af
[ÂÂÂ 7.371116] pstate: 60000005 (nZCv daif -PAN -UAO)
[ÂÂÂ 7.375957] pc : kfree+0x194/0x1b4
[ÂÂÂ 7.379389] lr : platform_device_release+0xcc/0xd8
[ÂÂÂ 7.384225] sp : ffff0000092dba90
[ÂÂÂ 7.387567] x29: ffff0000092dba90 x28: ffff000008a83000
[ÂÂÂ 7.392933] x27: ffff0000092dbc10 x26: 00000000000000e6
[ÂÂÂ 7.398297] x25: 0000000000000003 x24: ffff0000085b51e8
[ÂÂÂ 7.403662] x23: 0000000000000100 x22: ffff7e0000234cc0
[ÂÂÂ 7.409027] x21: ffff000008af3660 x20: ffff8017d21acc10
[ÂÂÂ 7.414392] x19: ffff8017d21acc00 x18: 0000000000000002
[ÂÂÂ 7.419757] x17: 0000000000000001 x16: 0000000000000008
[ÂÂÂ 7.425121] x15: 0000000000000001 x14: 6666666678303d65
[ÂÂÂ 7.430486] x13: 6469727265766f5f x12: 7265766972642e76
[ÂÂÂ 7.435850] x11: 6564703e2d617020 x10: 6530326435373638
[ÂÂÂ 7.441215] x9 : 3030303030303030 x8 : 3d76656420657361
[ÂÂÂ 7.446580] x7 : ffff000008f59df8 x6 : ffff8017fbe0ea50
[ÂÂÂ 7.451945] x5 : 0000000000000000 x4 : 0000000000000000
[ÂÂÂ 7.457309] x3 : ffffffffffffffff x2 : 0000000000000000
[ÂÂÂ 7.462674] x1 : 0fffc00000000800 x0 : ffff7e0000234ce0
[ÂÂÂ 7.468039] Process swapper/0 (pid: 1, stack limit =
0x00000000f276e9af)
[ÂÂÂ 7.474809] Call trace:
[ÂÂÂ 7.477272]Â kfree+0x194/0x1b4
[ÂÂÂ 7.480351]Â platform_device_release+0xcc/0xd8
[ÂÂÂ 7.484837]Â device_release+0x34/0x90
[ÂÂÂ 7.488531]Â kobject_put+0x70/0xcc
[ÂÂÂ 7.491961]Â put_device+0x14/0x1c
[ÂÂÂ 7.495304]Â platform_device_put+0x14/0x1c
[ÂÂÂ 7.499439]Â dmi_add_platform_ipmi+0x348/0x3ac
[ÂÂÂ 7.503923]Â scan_for_dmi_ipmi+0xfc/0x10c
[ÂÂÂ 7.507970]Â do_one_initcall+0x38/0x124
[ÂÂÂ 7.511840]Â kernel_init_freeable+0x188/0x228
[ÂÂÂ 7.516238]Â kernel_init+0x10/0x100
[ÂÂÂ 7.519756]Â ret_from_fork+0x10/0x18
[ÂÂÂ 7.523362] Code: f94002c0 37780080 f94012c0 37000040 (d4210000)
[ÂÂÂ 7.529552] ---[ end trace 11750e4787deef9e ]---
[ÂÂÂ 7.534228] Kernel panic - not syncing: Attempted to kill init!
exitcode=0x0000000b
[ÂÂÂ 7.534228]

This is because when the device is released in
platform_device_release(), we try to free
pdev.driver_override. This is a const string, hence
the crash.
Fix by using dynamic memory for pdev->driver_override.

Signed-off-by: John Garry <john.garry@xxxxxxxxxx>

Oops, sorry about that. Yes, queued for the next release and 4.14.x.

-corey


diff --git a/drivers/char/ipmi/ipmi_dmi.c b/drivers/char/ipmi/ipmi_dmi.c
index ab78b3b..c5112b1 100644
--- a/drivers/char/ipmi/ipmi_dmi.c
+++ b/drivers/char/ipmi/ipmi_dmi.c
@@ -106,7 +106,10 @@ static void __init dmi_add_platform_ipmi(unsigned
long base_addr,
ÂÂÂÂÂÂÂÂÂ pr_err("ipmi:dmi: Error allocation IPMI platform device\n");
ÂÂÂÂÂÂÂÂÂ return;
ÂÂÂÂÂ }
-ÂÂÂ pdev->driver_override = override;
+ÂÂÂ pdev->driver_override = kasprintf(GFP_KERNEL, "%s",
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ override);

It would be simpler to use managed variant (to avoid the kfree()), but I don't think it's possible until the device probes.


Actually, I don't think you should do the kfree() in ipmi_si_intf.c. It's done in
platform_device_release(), and will cause a double free if done in the IPMI code,
unless you set it to NULL. I'm going to add a patch to remove that hunk.

-corey

John

+ÂÂÂ if (!pdev->driver_override)
+ÂÂÂÂÂÂÂ goto err;
ÂÂÂÂÂÂÂ if (type == IPMI_DMI_TYPE_SSIF) {
ÂÂÂÂÂÂÂÂÂ set_prop_entry(p[pidx++], "i2c-addr", u16, base_addr);
diff --git a/drivers/char/ipmi/ipmi_si_intf.c
b/drivers/char/ipmi/ipmi_si_intf.c
index 779869e..2a19aa8 100644
--- a/drivers/char/ipmi/ipmi_si_intf.c
+++ b/drivers/char/ipmi/ipmi_si_intf.c
@@ -2426,6 +2426,7 @@ int ipmi_si_remove_by_dev(struct device *dev)
 {
ÂÂÂÂÂ struct smi_info *e;
ÂÂÂÂÂ int rv = -ENOENT;
+ÂÂÂ struct platform_device *pdev = to_platform_device(dev);
ÂÂÂÂÂÂÂ mutex_lock(&smi_infos_lock);
ÂÂÂÂÂ list_for_each_entry(e, &smi_infos, link) {
@@ -2436,6 +2437,7 @@ int ipmi_si_remove_by_dev(struct device *dev)
ÂÂÂÂÂÂÂÂÂ }
ÂÂÂÂÂ }
ÂÂÂÂÂ mutex_unlock(&smi_infos_lock);
+ÂÂÂ kfree(pdev->driver_override);
ÂÂÂÂÂÂÂ return rv;
 }



.