Re: [FIX PATCH v1] powerpc/pseries: Fix reference count leak during CPU unplug
Le 09/03/2017 à 05:37, Bharata B Rao a écrit : The following warning is seen when a CPU is hot unplugged on a PowerKVM guest: refcount_t: underflow; use-after-free. [ cut here ] WARNING: CPU: 0 PID: 53 at lib/refcount.c:128 refcount_sub_and_test+0xd8/0xf0 Modules linked in: CPU: 0 PID: 53 Comm: kworker/u510:1 Not tainted 4.11.0-rc1 #3 Workqueue: pseries hotplug workque pseries_hp_work_fn task: c000fb475000 task.stack: c000fb81c000 NIP: c06f0808 LR: c06f0804 CTR: c07b98c0 REGS: c000fb81f710 TRAP: 0700 Not tainted (4.11.0-rc1) MSR: 8282b033 CR: 4800 XER: 2000 CFAR: c0c438e0 SOFTE: 1 GPR00: c06f0804 c000fb81f990 c1573b00 0026 GPR04: 016c 667265652e0d0a73 652d61667465722d GPR08: 0007 0007 0001 0006 GPR12: 2200 cff4 c010c578 c001f11b9f40 GPR16: c001fe0312a8 c001fe031078 c001fe031020 0001 GPR20: c1454808 fef7 GPR24: c001f1677648 GPR28: 1008 c0e4d3d8 c001eaae07d8 NIP [c06f0808] refcount_sub_and_test+0xd8/0xf0 LR [c06f0804] refcount_sub_and_test+0xd4/0xf0 Call Trace: [c000fb81f990] [c06f0804] refcount_sub_and_test+0xd4/0xf0 (unreliable) [c000fb81f9f0] [c06d04b4] kobject_put+0x44/0x2a0 [c000fb81fa70] [c09d5284] of_node_put+0x34/0x50 [c000fb81faa0] [c00aceb8] dlpar_cpu_remove_by_index+0x108/0x130 [c000fb81fb30] [c00ae128] dlpar_cpu+0x78/0x550 [c000fb81fbe0] [c00a7b40] handle_dlpar_errorlog+0xc0/0x160 [c000fb81fc50] [c00a7c74] pseries_hp_work_fn+0x94/0xa0 [c000fb81fc80] [c0102cec] process_one_work+0x23c/0x540 [c000fb81fd20] [c010309c] worker_thread+0xac/0x620 [c000fb81fdc0] [c010c6c4] kthread+0x154/0x1a0 [c000fb81fe30] [c000bbe0] ret_from_kernel_thread+0x5c/0x7c Fix this by ensuring that of_node_put() is called only from the error path in dlpar_cpu_remove_by_index(). In the normal path, of_node_put() happens as part of dlpar_detach_node(). Signed-off-by: Bharata B Rao Cc: Nathan Fontenot Is this patch still needed ? Thanks Christophe --- Changes in v1: - Fixed the refcount problem in the userspace driven unplug path in addition to in-kernel unplug path. (Sachin Sant) v0: https://patchwork.ozlabs.org/patch/736547/ arch/powerpc/platforms/pseries/hotplug-cpu.c | 12 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/powerpc/platforms/pseries/hotplug-cpu.c b/arch/powerpc/platforms/pseries/hotplug-cpu.c index 7bc0e91..c5ed510 100644 --- a/arch/powerpc/platforms/pseries/hotplug-cpu.c +++ b/arch/powerpc/platforms/pseries/hotplug-cpu.c @@ -619,7 +619,8 @@ static int dlpar_cpu_remove_by_index(u32 drc_index) } rc = dlpar_cpu_remove(dn, drc_index); - of_node_put(dn); + if (rc) + of_node_put(dn); return rc; } @@ -856,9 +857,12 @@ static ssize_t dlpar_cpu_release(const char *buf, size_t count) } rc = dlpar_cpu_remove(dn, drc_index); - of_node_put(dn); - - return rc ? rc : count; + if (rc) { + of_node_put(dn); + return rc; + } else { + return count; + } } #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
Re: [FIX PATCH v1] powerpc/pseries: Fix reference count leak during CPU unplug
I get the error when removing a CPU that has been hotplugged after boot. On 03/14/2017 03:42 PM, Tyrel Datwyler wrote: > On 03/13/2017 03:29 AM, Bharata B Rao wrote: >> On Thu, Mar 09, 2017 at 01:34:00PM -0800, Tyrel Datwyler wrote: >>> On 03/08/2017 08:37 PM, Bharata B Rao wrote: The following warning is seen when a CPU is hot unplugged on a PowerKVM guest: >>> >>> Is this the case with cpus present at boot? What about cpus hotplugged >>> after boot? >> >> I have observed this for CPUs that are hotplugged. > > If removing a cpu present at boot works, but removing one that has been > hotplugged after boot reproduces the problem it is more likely the case > that we failed to take a reference during hotplug or released a > reference we shouldn't have. I'd have to go look at the hot add path. > >> >>> >>> My suspicion is that the refcount was wrong to begin with. See my >>> comments below. The use of the of_node_put() calls is correct as in each >>> case we incremented the ref count earlier in the same function. >>> refcount_t: underflow; use-after-free. [ cut here ] WARNING: CPU: 0 PID: 53 at lib/refcount.c:128 refcount_sub_and_test+0xd8/0xf0 Modules linked in: CPU: 0 PID: 53 Comm: kworker/u510:1 Not tainted 4.11.0-rc1 #3 Workqueue: pseries hotplug workque pseries_hp_work_fn task: c000fb475000 task.stack: c000fb81c000 NIP: c06f0808 LR: c06f0804 CTR: c07b98c0 REGS: c000fb81f710 TRAP: 0700 Not tainted (4.11.0-rc1) MSR: 8282b033CR: 4800 XER: 2000 CFAR: c0c438e0 SOFTE: 1 GPR00: c06f0804 c000fb81f990 c1573b00 0026 GPR04: 016c 667265652e0d0a73 652d61667465722d GPR08: 0007 0007 0001 0006 GPR12: 2200 cff4 c010c578 c001f11b9f40 GPR16: c001fe0312a8 c001fe031078 c001fe031020 0001 GPR20: c1454808 fef7 GPR24: c001f1677648 GPR28: 1008 c0e4d3d8 c001eaae07d8 NIP [c06f0808] refcount_sub_and_test+0xd8/0xf0 LR [c06f0804] refcount_sub_and_test+0xd4/0xf0 Call Trace: [c000fb81f990] [c06f0804] refcount_sub_and_test+0xd4/0xf0 (unreliable) [c000fb81f9f0] [c06d04b4] kobject_put+0x44/0x2a0 [c000fb81fa70] [c09d5284] of_node_put+0x34/0x50 [c000fb81faa0] [c00aceb8] dlpar_cpu_remove_by_index+0x108/0x130 [c000fb81fb30] [c00ae128] dlpar_cpu+0x78/0x550 [c000fb81fbe0] [c00a7b40] handle_dlpar_errorlog+0xc0/0x160 [c000fb81fc50] [c00a7c74] pseries_hp_work_fn+0x94/0xa0 [c000fb81fc80] [c0102cec] process_one_work+0x23c/0x540 [c000fb81fd20] [c010309c] worker_thread+0xac/0x620 [c000fb81fdc0] [c010c6c4] kthread+0x154/0x1a0 [c000fb81fe30] [c000bbe0] ret_from_kernel_thread+0x5c/0x7c Fix this by ensuring that of_node_put() is called only from the error path in dlpar_cpu_remove_by_index(). In the normal path, of_node_put() happens as part of dlpar_detach_node(). Signed-off-by: Bharata B Rao Cc: Nathan Fontenot --- Changes in v1: - Fixed the refcount problem in the userspace driven unplug path in addition to in-kernel unplug path. (Sachin Sant) v0: https://patchwork.ozlabs.org/patch/736547/ arch/powerpc/platforms/pseries/hotplug-cpu.c | 12 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/powerpc/platforms/pseries/hotplug-cpu.c b/arch/powerpc/platforms/pseries/hotplug-cpu.c index 7bc0e91..c5ed510 100644 --- a/arch/powerpc/platforms/pseries/hotplug-cpu.c +++ b/arch/powerpc/platforms/pseries/hotplug-cpu.c @@ -619,7 +619,8 @@ static int dlpar_cpu_remove_by_index(u32 drc_index) } rc = dlpar_cpu_remove(dn, drc_index); - of_node_put(dn); + if (rc) + of_node_put(dn); >>> >>> I think there is another issue at play here because this is wrong. >>> Regardless of whether the dlpar_cpu_remove() succeeds or fails we still >>> need of_node_put() for both cases because we incremented the ref count >>> earlier in this function with a call to cpu_drc_index_to_dn() call. That >>> call doesn't, but shoul, document that it returns a device_node with >>> incremented refcount. >>> return rc; } @@ -856,9 +857,12 @@ static ssize_t dlpar_cpu_release(const char *buf, size_t count) } rc = dlpar_cpu_remove(dn,
Re: [FIX PATCH v1] powerpc/pseries: Fix reference count leak during CPU unplug
>> So you suggest that adding of_node_get() to __of_attach_node_sysfs() >> is the right fix ? > > If I understand that this only creates for hot-added cpus then no. Also > for this to be the correct fix I would expect to see this recreate for > all hot-remove operations such as memory and pci devices as well. > So I can recreate this problem while removing a I/O adapter as well. Here is a trace against 4.11.0-rc1-next20170310 while performing a DLPAR remove operation on a I/O adapter. [ 549.815605] rpaphp: Slot [U78C7.001.RCH0042-P1-C1] registered [ 549.815608] rpadlpar_io: slot PHB 64 added [ 575.267302] iommu: Removing device 0040:01:00.0 from group 1 [ 575.267401] iommu: Removing device 0040:01:00.1 from group 1 [ 575.267842] refcount_t: underflow; use-after-free. [ 575.267855] [ cut here ] [ 575.267862] WARNING: CPU: 2 PID: 3837 at lib/refcount.c:128 refcount_sub_and_test+0xf4/0x110 [ 575.267865] Modules linked in: rpadlpar_io rpaphp dccp_diag dccp tcp_diag udp_diag inet_diag unix_diag af_packet_diag netlink_diag rpcrdma sunrpc ib_isert iscsi_target_mod ib_iser libiscsi scsi_transport_iscsi ib_srpt target_core_mod ib_srp ib_ipoib rdma_ucm ib_ucm ib_uverbs ib_umad rdma_cm ib_cm iw_cm iw_cxgb3 ib_core ghash_generic xts gf128mul tpm_ibmvtpm tpm vmx_crypto pseries_rng sg binfmt_misc ip_tables xfs libcrc32c sr_mod sd_mod cdrom cxgb3 ibmvscsi ibmveth scsi_transport_srp mdio dm_mirror dm_region_hash dm_log dm_mod [ 575.267904] CPU: 2 PID: 3837 Comm: drmgr Not tainted 4.11.0-rc1-next-20170310 #4 [ 575.267907] task: c0076f041600 task.stack: c0076f084000 [ 575.267910] NIP: c1aa69c4 LR: c1aa69c0 CTR: 006338e4 [ 575.267913] REGS: c0076f0878a0 TRAP: 0700 Not tainted (4.11.0-rc1-next-20170310) [ 575.267915] MSR: 80029033[ 575.267920] CR: 42002422 XER: 0007 [ 575.267923] CFAR: c1edb5e0 SOFTE: 1 [ 575.267923] GPR00: c1aa69c0 c0076f087b20 c2605f00 0026 [ 575.267923] GPR04: 80100fe93ec0 00492b9a [ 575.267923] GPR08: 0001 0007 0006 3ff0 [ 575.267923] GPR12: 2200 ce801200 [ 575.267923] GPR16: [ 575.267923] GPR20: [ 575.267923] GPR24: c001db50a78c 10016650 [ 575.267923] GPR28: c001dd1a7500 c001dd1a7200 c001db50a780 c001dd1a7258 [ 575.267955] NIP [c1aa69c4] refcount_sub_and_test+0xf4/0x110 [ 575.267958] LR [c1aa69c0] refcount_sub_and_test+0xf0/0x110 [ 575.267960] Call Trace: [ 575.267962] [c0076f087b20] [c1aa69c0] refcount_sub_and_test+0xf0/0x110 (unreliable) [ 575.267967] [c0076f087b80] [c1a84f1c] kobject_put+0x3c/0xa0 [ 575.267972] [c0076f087bf0] [c1d239b4] of_node_put+0x24/0x40 [ 575.267976] [c0076f087c10] [c165ce74] ofdt_write+0x204/0x6b0 [ 575.267980] [c0076f087cd0] [c197bde0] proc_reg_write+0x80/0xd0 [ 575.267984] [c0076f087d00] [c18df040] __vfs_write+0x40/0x1c0 [ 575.267987] [c0076f087d90] [c18e0998] vfs_write+0xc8/0x240 [ 575.267991] [c0076f087de0] [c18e2600] SyS_write+0x60/0x110 [ 575.267995] [c0076f087e30] [c15cb184] system_call+0x38/0xe0 [ 575.267997] Instruction dump: [ 575.267999] 7863d182 4e800020 7c0802a6 3921 3d42fff8 3c62ffb1 386306a0 992afd41 [ 575.268004] f8010010 f821ffa1 48434be5 6000 <0fe0> 38210060 3860 e8010010 [ 575.268010] ---[ end trace e6c0a4371a0aa4e2 ]— Thanks -Sachin
Re: [FIX PATCH v1] powerpc/pseries: Fix reference count leak during CPU unplug
On 03/13/2017 03:29 AM, Bharata B Rao wrote: > On Thu, Mar 09, 2017 at 01:34:00PM -0800, Tyrel Datwyler wrote: >> On 03/08/2017 08:37 PM, Bharata B Rao wrote: >>> The following warning is seen when a CPU is hot unplugged on a PowerKVM >>> guest: >> >> Is this the case with cpus present at boot? What about cpus hotplugged >> after boot? > > I have observed this for CPUs that are hotplugged. If removing a cpu present at boot works, but removing one that has been hotplugged after boot reproduces the problem it is more likely the case that we failed to take a reference during hotplug or released a reference we shouldn't have. I'd have to go look at the hot add path. > >> >> My suspicion is that the refcount was wrong to begin with. See my >> comments below. The use of the of_node_put() calls is correct as in each >> case we incremented the ref count earlier in the same function. >> >>> >>> refcount_t: underflow; use-after-free. >>> [ cut here ] >>> WARNING: CPU: 0 PID: 53 at lib/refcount.c:128 >>> refcount_sub_and_test+0xd8/0xf0 >>> Modules linked in: >>> CPU: 0 PID: 53 Comm: kworker/u510:1 Not tainted 4.11.0-rc1 #3 >>> Workqueue: pseries hotplug workque pseries_hp_work_fn >>> task: c000fb475000 task.stack: c000fb81c000 >>> NIP: c06f0808 LR: c06f0804 CTR: c07b98c0 >>> REGS: c000fb81f710 TRAP: 0700 Not tainted (4.11.0-rc1) >>> MSR: 8282b033>>> CR: 4800 XER: 2000 >>> CFAR: c0c438e0 SOFTE: 1 >>> GPR00: c06f0804 c000fb81f990 c1573b00 0026 >>> GPR04: 016c 667265652e0d0a73 652d61667465722d >>> GPR08: 0007 0007 0001 0006 >>> GPR12: 2200 cff4 c010c578 c001f11b9f40 >>> GPR16: c001fe0312a8 c001fe031078 c001fe031020 0001 >>> GPR20: c1454808 fef7 >>> GPR24: c001f1677648 >>> GPR28: 1008 c0e4d3d8 c001eaae07d8 >>> NIP [c06f0808] refcount_sub_and_test+0xd8/0xf0 >>> LR [c06f0804] refcount_sub_and_test+0xd4/0xf0 >>> Call Trace: >>> [c000fb81f990] [c06f0804] refcount_sub_and_test+0xd4/0xf0 >>> (unreliable) >>> [c000fb81f9f0] [c06d04b4] kobject_put+0x44/0x2a0 >>> [c000fb81fa70] [c09d5284] of_node_put+0x34/0x50 >>> [c000fb81faa0] [c00aceb8] dlpar_cpu_remove_by_index+0x108/0x130 >>> [c000fb81fb30] [c00ae128] dlpar_cpu+0x78/0x550 >>> [c000fb81fbe0] [c00a7b40] handle_dlpar_errorlog+0xc0/0x160 >>> [c000fb81fc50] [c00a7c74] pseries_hp_work_fn+0x94/0xa0 >>> [c000fb81fc80] [c0102cec] process_one_work+0x23c/0x540 >>> [c000fb81fd20] [c010309c] worker_thread+0xac/0x620 >>> [c000fb81fdc0] [c010c6c4] kthread+0x154/0x1a0 >>> [c000fb81fe30] [c000bbe0] ret_from_kernel_thread+0x5c/0x7c >>> >>> Fix this by ensuring that of_node_put() is called only from the >>> error path in dlpar_cpu_remove_by_index(). In the normal path, >>> of_node_put() happens as part of dlpar_detach_node(). >>> >>> Signed-off-by: Bharata B Rao >>> Cc: Nathan Fontenot >>> --- >>> Changes in v1: >>> - Fixed the refcount problem in the userspace driven unplug path >>> in addition to in-kernel unplug path. (Sachin Sant) >>> >>> v0: https://patchwork.ozlabs.org/patch/736547/ >>> >>> arch/powerpc/platforms/pseries/hotplug-cpu.c | 12 >>> 1 file changed, 8 insertions(+), 4 deletions(-) >>> >>> diff --git a/arch/powerpc/platforms/pseries/hotplug-cpu.c >>> b/arch/powerpc/platforms/pseries/hotplug-cpu.c >>> index 7bc0e91..c5ed510 100644 >>> --- a/arch/powerpc/platforms/pseries/hotplug-cpu.c >>> +++ b/arch/powerpc/platforms/pseries/hotplug-cpu.c >>> @@ -619,7 +619,8 @@ static int dlpar_cpu_remove_by_index(u32 drc_index) >>> } >>> >>> rc = dlpar_cpu_remove(dn, drc_index); >>> - of_node_put(dn); >>> + if (rc) >>> + of_node_put(dn); >> >> I think there is another issue at play here because this is wrong. >> Regardless of whether the dlpar_cpu_remove() succeeds or fails we still >> need of_node_put() for both cases because we incremented the ref count >> earlier in this function with a call to cpu_drc_index_to_dn() call. That >> call doesn't, but shoul, document that it returns a device_node with >> incremented refcount. >> >>> return rc; >>> } >>> >>> @@ -856,9 +857,12 @@ static ssize_t dlpar_cpu_release(const char *buf, >>> size_t count) >>> } >>> >>> rc = dlpar_cpu_remove(dn, drc_index); >>> - of_node_put(dn); >>> - >>> - return rc ? rc : count; >>> + if (rc) { >>> + of_node_put(dn); >>> + return rc; >>> + } else { >>> + return count; >>> + } >> >> Same
Re: [FIX PATCH v1] powerpc/pseries: Fix reference count leak during CPU unplug
On Thu, Mar 09, 2017 at 01:34:00PM -0800, Tyrel Datwyler wrote: > On 03/08/2017 08:37 PM, Bharata B Rao wrote: > > The following warning is seen when a CPU is hot unplugged on a PowerKVM > > guest: > > Is this the case with cpus present at boot? What about cpus hotplugged > after boot? I have observed this for CPUs that are hotplugged. > > My suspicion is that the refcount was wrong to begin with. See my > comments below. The use of the of_node_put() calls is correct as in each > case we incremented the ref count earlier in the same function. > > > > > refcount_t: underflow; use-after-free. > > [ cut here ] > > WARNING: CPU: 0 PID: 53 at lib/refcount.c:128 > > refcount_sub_and_test+0xd8/0xf0 > > Modules linked in: > > CPU: 0 PID: 53 Comm: kworker/u510:1 Not tainted 4.11.0-rc1 #3 > > Workqueue: pseries hotplug workque pseries_hp_work_fn > > task: c000fb475000 task.stack: c000fb81c000 > > NIP: c06f0808 LR: c06f0804 CTR: c07b98c0 > > REGS: c000fb81f710 TRAP: 0700 Not tainted (4.11.0-rc1) > > MSR: 8282b033> > CR: 4800 XER: 2000 > > CFAR: c0c438e0 SOFTE: 1 > > GPR00: c06f0804 c000fb81f990 c1573b00 0026 > > GPR04: 016c 667265652e0d0a73 652d61667465722d > > GPR08: 0007 0007 0001 0006 > > GPR12: 2200 cff4 c010c578 c001f11b9f40 > > GPR16: c001fe0312a8 c001fe031078 c001fe031020 0001 > > GPR20: c1454808 fef7 > > GPR24: c001f1677648 > > GPR28: 1008 c0e4d3d8 c001eaae07d8 > > NIP [c06f0808] refcount_sub_and_test+0xd8/0xf0 > > LR [c06f0804] refcount_sub_and_test+0xd4/0xf0 > > Call Trace: > > [c000fb81f990] [c06f0804] refcount_sub_and_test+0xd4/0xf0 > > (unreliable) > > [c000fb81f9f0] [c06d04b4] kobject_put+0x44/0x2a0 > > [c000fb81fa70] [c09d5284] of_node_put+0x34/0x50 > > [c000fb81faa0] [c00aceb8] dlpar_cpu_remove_by_index+0x108/0x130 > > [c000fb81fb30] [c00ae128] dlpar_cpu+0x78/0x550 > > [c000fb81fbe0] [c00a7b40] handle_dlpar_errorlog+0xc0/0x160 > > [c000fb81fc50] [c00a7c74] pseries_hp_work_fn+0x94/0xa0 > > [c000fb81fc80] [c0102cec] process_one_work+0x23c/0x540 > > [c000fb81fd20] [c010309c] worker_thread+0xac/0x620 > > [c000fb81fdc0] [c010c6c4] kthread+0x154/0x1a0 > > [c000fb81fe30] [c000bbe0] ret_from_kernel_thread+0x5c/0x7c > > > > Fix this by ensuring that of_node_put() is called only from the > > error path in dlpar_cpu_remove_by_index(). In the normal path, > > of_node_put() happens as part of dlpar_detach_node(). > > > > Signed-off-by: Bharata B Rao > > Cc: Nathan Fontenot > > --- > > Changes in v1: > > - Fixed the refcount problem in the userspace driven unplug path > > in addition to in-kernel unplug path. (Sachin Sant) > > > > v0: https://patchwork.ozlabs.org/patch/736547/ > > > > arch/powerpc/platforms/pseries/hotplug-cpu.c | 12 > > 1 file changed, 8 insertions(+), 4 deletions(-) > > > > diff --git a/arch/powerpc/platforms/pseries/hotplug-cpu.c > > b/arch/powerpc/platforms/pseries/hotplug-cpu.c > > index 7bc0e91..c5ed510 100644 > > --- a/arch/powerpc/platforms/pseries/hotplug-cpu.c > > +++ b/arch/powerpc/platforms/pseries/hotplug-cpu.c > > @@ -619,7 +619,8 @@ static int dlpar_cpu_remove_by_index(u32 drc_index) > > } > > > > rc = dlpar_cpu_remove(dn, drc_index); > > - of_node_put(dn); > > + if (rc) > > + of_node_put(dn); > > I think there is another issue at play here because this is wrong. > Regardless of whether the dlpar_cpu_remove() succeeds or fails we still > need of_node_put() for both cases because we incremented the ref count > earlier in this function with a call to cpu_drc_index_to_dn() call. That > call doesn't, but shoul, document that it returns a device_node with > incremented refcount. > > > return rc; > > } > > > > @@ -856,9 +857,12 @@ static ssize_t dlpar_cpu_release(const char *buf, > > size_t count) > > } > > > > rc = dlpar_cpu_remove(dn, drc_index); > > - of_node_put(dn); > > - > > - return rc ? rc : count; > > + if (rc) { > > + of_node_put(dn); > > + return rc; > > + } else { > > + return count; > > + } > > Same comment as above. The call earlier in the function to > of_find_node_by_path() returned a device_node struct with its ref count > incremented. So, regardless of whether dlpar_cpu_remove() succeeds or > fails we need decrement the ref count with of_node_put(). > > Looking closer at the call paths for attach and detach one will notice
Re: [FIX PATCH v1] powerpc/pseries: Fix reference count leak during CPU unplug
On 03/08/2017 08:37 PM, Bharata B Rao wrote: > The following warning is seen when a CPU is hot unplugged on a PowerKVM > guest: Is this the case with cpus present at boot? What about cpus hotplugged after boot? My suspicion is that the refcount was wrong to begin with. See my comments below. The use of the of_node_put() calls is correct as in each case we incremented the ref count earlier in the same function. > > refcount_t: underflow; use-after-free. > [ cut here ] > WARNING: CPU: 0 PID: 53 at lib/refcount.c:128 refcount_sub_and_test+0xd8/0xf0 > Modules linked in: > CPU: 0 PID: 53 Comm: kworker/u510:1 Not tainted 4.11.0-rc1 #3 > Workqueue: pseries hotplug workque pseries_hp_work_fn > task: c000fb475000 task.stack: c000fb81c000 > NIP: c06f0808 LR: c06f0804 CTR: c07b98c0 > REGS: c000fb81f710 TRAP: 0700 Not tainted (4.11.0-rc1) > MSR: 8282b033> CR: 4800 XER: 2000 > CFAR: c0c438e0 SOFTE: 1 > GPR00: c06f0804 c000fb81f990 c1573b00 0026 > GPR04: 016c 667265652e0d0a73 652d61667465722d > GPR08: 0007 0007 0001 0006 > GPR12: 2200 cff4 c010c578 c001f11b9f40 > GPR16: c001fe0312a8 c001fe031078 c001fe031020 0001 > GPR20: c1454808 fef7 > GPR24: c001f1677648 > GPR28: 1008 c0e4d3d8 c001eaae07d8 > NIP [c06f0808] refcount_sub_and_test+0xd8/0xf0 > LR [c06f0804] refcount_sub_and_test+0xd4/0xf0 > Call Trace: > [c000fb81f990] [c06f0804] refcount_sub_and_test+0xd4/0xf0 > (unreliable) > [c000fb81f9f0] [c06d04b4] kobject_put+0x44/0x2a0 > [c000fb81fa70] [c09d5284] of_node_put+0x34/0x50 > [c000fb81faa0] [c00aceb8] dlpar_cpu_remove_by_index+0x108/0x130 > [c000fb81fb30] [c00ae128] dlpar_cpu+0x78/0x550 > [c000fb81fbe0] [c00a7b40] handle_dlpar_errorlog+0xc0/0x160 > [c000fb81fc50] [c00a7c74] pseries_hp_work_fn+0x94/0xa0 > [c000fb81fc80] [c0102cec] process_one_work+0x23c/0x540 > [c000fb81fd20] [c010309c] worker_thread+0xac/0x620 > [c000fb81fdc0] [c010c6c4] kthread+0x154/0x1a0 > [c000fb81fe30] [c000bbe0] ret_from_kernel_thread+0x5c/0x7c > > Fix this by ensuring that of_node_put() is called only from the > error path in dlpar_cpu_remove_by_index(). In the normal path, > of_node_put() happens as part of dlpar_detach_node(). > > Signed-off-by: Bharata B Rao > Cc: Nathan Fontenot > --- > Changes in v1: > - Fixed the refcount problem in the userspace driven unplug path > in addition to in-kernel unplug path. (Sachin Sant) > > v0: https://patchwork.ozlabs.org/patch/736547/ > > arch/powerpc/platforms/pseries/hotplug-cpu.c | 12 > 1 file changed, 8 insertions(+), 4 deletions(-) > > diff --git a/arch/powerpc/platforms/pseries/hotplug-cpu.c > b/arch/powerpc/platforms/pseries/hotplug-cpu.c > index 7bc0e91..c5ed510 100644 > --- a/arch/powerpc/platforms/pseries/hotplug-cpu.c > +++ b/arch/powerpc/platforms/pseries/hotplug-cpu.c > @@ -619,7 +619,8 @@ static int dlpar_cpu_remove_by_index(u32 drc_index) > } > > rc = dlpar_cpu_remove(dn, drc_index); > - of_node_put(dn); > + if (rc) > + of_node_put(dn); I think there is another issue at play here because this is wrong. Regardless of whether the dlpar_cpu_remove() succeeds or fails we still need of_node_put() for both cases because we incremented the ref count earlier in this function with a call to cpu_drc_index_to_dn() call. That call doesn't, but shoul, document that it returns a device_node with incremented refcount. > return rc; > } > > @@ -856,9 +857,12 @@ static ssize_t dlpar_cpu_release(const char *buf, size_t > count) > } > > rc = dlpar_cpu_remove(dn, drc_index); > - of_node_put(dn); > - > - return rc ? rc : count; > + if (rc) { > + of_node_put(dn); > + return rc; > + } else { > + return count; > + } Same comment as above. The call earlier in the function to of_find_node_by_path() returned a device_node struct with its ref count incremented. So, regardless of whether dlpar_cpu_remove() succeeds or fails we need decrement the ref count with of_node_put(). Looking closer at the call paths for attach and detach one will notice that __of_attach_node_sysfs() does not take a device_node reference with of_node_get(), but __of_detach_node_sysfs() does a of_node_put(). In the old days we use to keep the device tree in /proc. Now it lives in sysfs and is symlinked to /proc for userspace ABI reasons. Further, pseries was the only
Re: [FIX PATCH v1] powerpc/pseries: Fix reference count leak during CPU unplug
Bharata B Raowrites: > The following warning is seen when a CPU is hot unplugged on a PowerKVM > guest: > > refcount_t: underflow; use-after-free. ... > > Fix this by ensuring that of_node_put() is called only from the > error path in dlpar_cpu_remove_by_index(). In the normal path, > of_node_put() happens as part of dlpar_detach_node(). Don't we have the same bug in mobility.c ? static int delete_dt_node(__be32 phandle) { struct device_node *dn; dn = of_find_node_by_phandle(be32_to_cpu(phandle)); if (!dn) return -ENOENT; dlpar_detach_node(dn); of_node_put(dn); return 0; } > diff --git a/arch/powerpc/platforms/pseries/hotplug-cpu.c > b/arch/powerpc/platforms/pseries/hotplug-cpu.c > index 7bc0e91..c5ed510 100644 > --- a/arch/powerpc/platforms/pseries/hotplug-cpu.c > +++ b/arch/powerpc/platforms/pseries/hotplug-cpu.c > @@ -619,7 +619,8 @@ static int dlpar_cpu_remove_by_index(u32 drc_index) > } > > rc = dlpar_cpu_remove(dn, drc_index); > - of_node_put(dn); > + if (rc) > + of_node_put(dn); > return rc; > } > > @@ -856,9 +857,12 @@ static ssize_t dlpar_cpu_release(const char *buf, size_t > count) > } > > rc = dlpar_cpu_remove(dn, drc_index); > - of_node_put(dn); > - > - return rc ? rc : count; > + if (rc) { > + of_node_put(dn); > + return rc; > + } else { > + return count; > + } > } Which makes me think that the bug is actually that dlpar_detach_node() should not be dropping the last reference on behalf of its caller. It means that the caller has a pointer to a freed dn, which is wrong. eg. A caller could do: dlpar_detach_node(dn); printk("detached %s\n", dn->name); But dn will have been freed by the time the printk happens. The problem is we call dlpar_detach_node() recursively, and for the child nodes it *does* need to drop the last reference, because that's the whole point. So it needs some refactoring. I also notice there's no error checking on the removal of the child nodes, which is pretty dubious: child = of_get_next_child(dn, NULL); while (child) { dlpar_detach_node(child); child = of_get_next_child(dn, child); } cheers
Re: [FIX PATCH v1] powerpc/pseries: Fix reference count leak during CPU unplug
> Fix this by ensuring that of_node_put() is called only from the > error path in dlpar_cpu_remove_by_index(). In the normal path, > of_node_put() happens as part of dlpar_detach_node(). > > Signed-off-by: Bharata B Rao> Cc: Nathan Fontenot > --- > Changes in v1: > - Fixed the refcount problem in the userspace driven unplug path > in addition to in-kernel unplug path. (Sachin Sant) > > v0: https://patchwork.ozlabs.org/patch/736547/ > Thanks Bharata. Tested cpu hotplug/unplug with this fix on a PowrVM LPAR. Tested-by : Sachin Sant Thanks -Sachin