* [PATCH] cpu/hotplug: Serialize callback invocations proper
@ 2017-04-28 16:27 Sebastian Andrzej Siewior
2017-04-29 5:15 ` Greg KH
2017-04-30 14:10 ` Greg KH
0 siblings, 2 replies; 4+ messages in thread
From: Sebastian Andrzej Siewior @ 2017-04-28 16:27 UTC (permalink / raw)
To: stable
Upstream commit dc434e056fe1dada20df7ba07f32739d3a701adf
The setup/remove_state/instance() functions in the hotplug core code are
serialized against concurrent CPU hotplug, but unfortunately not serialized
against themself.
As a consequence a concurrent invocation of these function results in
corruption of the callback machinery because two instances try to invoke
callbacks on remote cpus at the same time. This results in missing callback
invocations and initiator threads waiting forever on the completion.
The obvious solution to replace get_cpu_online() with cpu_hotplug_begin()
is not possible because at least one callsite calls into these functions
from a get_online_cpu() locked region.
Extend the protection scope of the cpuhp_state_mutex from solely protecting
the state arrays to cover the callback invocation machinery as well.
Fixes: 5b7aa87e0482 ("cpu/hotplug: Implement setup/removal interface")
Reported-and-tested-by: Bart Van Assche <Bart.VanAssche@sandisk.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: hpa@zytor.com
Cc: mingo@kernel.org
Cc: akpm@linux-foundation.org
Cc: torvalds@linux-foundation.org
Link: http://lkml.kernel.org/r/20170314150645.g4tdyoszlcbajmna@linutronix.de
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
kernel/cpu.c | 28 ++++++++++++++--------------
1 file changed, 14 insertions(+), 14 deletions(-)
diff --git a/kernel/cpu.c b/kernel/cpu.c
index f7c063239fa5..37b223e4fc05 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -1335,26 +1335,21 @@ static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
struct cpuhp_step *sp;
int ret = 0;
- mutex_lock(&cpuhp_state_mutex);
-
if (state == CPUHP_AP_ONLINE_DYN || state == CPUHP_BP_PREPARE_DYN) {
ret = cpuhp_reserve_state(state);
if (ret < 0)
- goto out;
+ return ret;
state = ret;
}
sp = cpuhp_get_step(state);
- if (name && sp->name) {
- ret = -EBUSY;
- goto out;
- }
+ if (name && sp->name)
+ return -EBUSY;
+
sp->startup.single = startup;
sp->teardown.single = teardown;
sp->name = name;
sp->multi_instance = multi_instance;
INIT_HLIST_HEAD(&sp->list);
-out:
- mutex_unlock(&cpuhp_state_mutex);
return ret;
}
@@ -1428,6 +1423,7 @@ int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
return -EINVAL;
get_online_cpus();
+ mutex_lock(&cpuhp_state_mutex);
if (!invoke || !sp->startup.multi)
goto add_node;
@@ -1447,16 +1443,14 @@ int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
if (ret) {
if (sp->teardown.multi)
cpuhp_rollback_install(cpu, state, node);
- goto err;
+ goto unlock;
}
}
add_node:
ret = 0;
- mutex_lock(&cpuhp_state_mutex);
hlist_add_head(node, &sp->list);
+unlock:
mutex_unlock(&cpuhp_state_mutex);
-
-err:
put_online_cpus();
return ret;
}
@@ -1491,6 +1485,7 @@ int __cpuhp_setup_state(enum cpuhp_state state,
return -EINVAL;
get_online_cpus();
+ mutex_lock(&cpuhp_state_mutex);
ret = cpuhp_store_callbacks(state, name, startup, teardown,
multi_instance);
@@ -1524,6 +1519,7 @@ int __cpuhp_setup_state(enum cpuhp_state state,
}
}
out:
+ mutex_unlock(&cpuhp_state_mutex);
put_online_cpus();
/*
* If the requested state is CPUHP_AP_ONLINE_DYN, return the
@@ -1547,6 +1543,8 @@ int __cpuhp_state_remove_instance(enum cpuhp_state state,
return -EINVAL;
get_online_cpus();
+ mutex_lock(&cpuhp_state_mutex);
+
if (!invoke || !cpuhp_get_teardown_cb(state))
goto remove;
/*
@@ -1563,7 +1561,6 @@ int __cpuhp_state_remove_instance(enum cpuhp_state state,
}
remove:
- mutex_lock(&cpuhp_state_mutex);
hlist_del(node);
mutex_unlock(&cpuhp_state_mutex);
put_online_cpus();
@@ -1571,6 +1568,7 @@ int __cpuhp_state_remove_instance(enum cpuhp_state state,
return 0;
}
EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
+
/**
* __cpuhp_remove_state - Remove the callbacks for an hotplug machine state
* @state: The state to remove
@@ -1589,6 +1587,7 @@ void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
get_online_cpus();
+ mutex_lock(&cpuhp_state_mutex);
if (sp->multi_instance) {
WARN(!hlist_empty(&sp->list),
"Error: Removing state %d which has instances left.\n",
@@ -1613,6 +1612,7 @@ void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
}
remove:
cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
+ mutex_unlock(&cpuhp_state_mutex);
put_online_cpus();
}
EXPORT_SYMBOL(__cpuhp_remove_state);
--
2.11.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] cpu/hotplug: Serialize callback invocations proper
2017-04-28 16:27 [PATCH] cpu/hotplug: Serialize callback invocations proper Sebastian Andrzej Siewior
@ 2017-04-29 5:15 ` Greg KH
2017-04-30 14:10 ` Greg KH
1 sibling, 0 replies; 4+ messages in thread
From: Greg KH @ 2017-04-29 5:15 UTC (permalink / raw)
To: Sebastian Andrzej Siewior; +Cc: stable
On Fri, Apr 28, 2017 at 06:27:28PM +0200, Sebastian Andrzej Siewior wrote:
> Upstream commit dc434e056fe1dada20df7ba07f32739d3a701adf
>
> The setup/remove_state/instance() functions in the hotplug core code are
> serialized against concurrent CPU hotplug, but unfortunately not serialized
> against themself.
>
> As a consequence a concurrent invocation of these function results in
> corruption of the callback machinery because two instances try to invoke
> callbacks on remote cpus at the same time. This results in missing callback
> invocations and initiator threads waiting forever on the completion.
>
> The obvious solution to replace get_cpu_online() with cpu_hotplug_begin()
> is not possible because at least one callsite calls into these functions
> from a get_online_cpu() locked region.
>
> Extend the protection scope of the cpuhp_state_mutex from solely protecting
> the state arrays to cover the callback invocation machinery as well.
>
> Fixes: 5b7aa87e0482 ("cpu/hotplug: Implement setup/removal interface")
> Reported-and-tested-by: Bart Van Assche <Bart.VanAssche@sandisk.com>
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> Cc: hpa@zytor.com
> Cc: mingo@kernel.org
> Cc: akpm@linux-foundation.org
> Cc: torvalds@linux-foundation.org
> Link: http://lkml.kernel.org/r/20170314150645.g4tdyoszlcbajmna@linutronix.de
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> ---
> kernel/cpu.c | 28 ++++++++++++++--------------
> 1 file changed, 14 insertions(+), 14 deletions(-)
What stable tree(s) do you want htis patch applied to?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] cpu/hotplug: Serialize callback invocations proper
2017-04-28 16:27 [PATCH] cpu/hotplug: Serialize callback invocations proper Sebastian Andrzej Siewior
2017-04-29 5:15 ` Greg KH
@ 2017-04-30 14:10 ` Greg KH
2017-05-01 16:59 ` [PATCH v4.9] " Sebastian Andrzej Siewior
1 sibling, 1 reply; 4+ messages in thread
From: Greg KH @ 2017-04-30 14:10 UTC (permalink / raw)
To: Sebastian Andrzej Siewior; +Cc: stable
On Fri, Apr 28, 2017 at 06:27:28PM +0200, Sebastian Andrzej Siewior wrote:
> Upstream commit dc434e056fe1dada20df7ba07f32739d3a701adf
>
> The setup/remove_state/instance() functions in the hotplug core code are
> serialized against concurrent CPU hotplug, but unfortunately not serialized
> against themself.
>
> As a consequence a concurrent invocation of these function results in
> corruption of the callback machinery because two instances try to invoke
> callbacks on remote cpus at the same time. This results in missing callback
> invocations and initiator threads waiting forever on the completion.
>
> The obvious solution to replace get_cpu_online() with cpu_hotplug_begin()
> is not possible because at least one callsite calls into these functions
> from a get_online_cpu() locked region.
>
> Extend the protection scope of the cpuhp_state_mutex from solely protecting
> the state arrays to cover the callback invocation machinery as well.
>
> Fixes: 5b7aa87e0482 ("cpu/hotplug: Implement setup/removal interface")
> Reported-and-tested-by: Bart Van Assche <Bart.VanAssche@sandisk.com>
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> Cc: hpa@zytor.com
> Cc: mingo@kernel.org
> Cc: akpm@linux-foundation.org
> Cc: torvalds@linux-foundation.org
> Link: http://lkml.kernel.org/r/20170314150645.g4tdyoszlcbajmna@linutronix.de
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> ---
> kernel/cpu.c | 28 ++++++++++++++--------------
> 1 file changed, 14 insertions(+), 14 deletions(-)
Doesn't apply to 4.9-stable, can you provide a backport there if you
want it applied to that tree (I think you do...)
thanks,
greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v4.9] cpu/hotplug: Serialize callback invocations proper
2017-04-30 14:10 ` Greg KH
@ 2017-05-01 16:59 ` Sebastian Andrzej Siewior
0 siblings, 0 replies; 4+ messages in thread
From: Sebastian Andrzej Siewior @ 2017-05-01 16:59 UTC (permalink / raw)
To: Greg KH; +Cc: stable
Backport of upstream commit dc434e056fe1dada20df7ba07f32739d3a701adf
The setup/remove_state/instance() functions in the hotplug core code are
serialized against concurrent CPU hotplug, but unfortunately not serialized
against themself.
As a consequence a concurrent invocation of these function results in
corruption of the callback machinery because two instances try to invoke
callbacks on remote cpus at the same time. This results in missing callback
invocations and initiator threads waiting forever on the completion.
The obvious solution to replace get_cpu_online() with cpu_hotplug_begin()
is not possible because at least one callsite calls into these functions
from a get_online_cpu() locked region.
Extend the protection scope of the cpuhp_state_mutex from solely protecting
the state arrays to cover the callback invocation machinery as well.
Fixes: 5b7aa87e0482 ("cpu/hotplug: Implement setup/removal interface")
Reported-and-tested-by: Bart Van Assche <Bart.VanAssche@sandisk.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: hpa@zytor.com
Cc: mingo@kernel.org
Cc: akpm@linux-foundation.org
Cc: torvalds@linux-foundation.org
Link: http://lkml.kernel.org/r/20170314150645.g4tdyoszlcbajmna@linutronix.de
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
This is backported for v4.9.
kernel/cpu.c | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/kernel/cpu.c b/kernel/cpu.c
index 217fd2e7f435..99c6c568bc55 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -1441,14 +1441,12 @@ static void cpuhp_store_callbacks(enum cpuhp_state state,
/* (Un)Install the callbacks for further cpu hotplug operations */
struct cpuhp_step *sp;
- mutex_lock(&cpuhp_state_mutex);
sp = cpuhp_get_step(state);
sp->startup.single = startup;
sp->teardown.single = teardown;
sp->name = name;
sp->multi_instance = multi_instance;
INIT_HLIST_HEAD(&sp->list);
- mutex_unlock(&cpuhp_state_mutex);
}
static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
@@ -1518,16 +1516,13 @@ static int cpuhp_reserve_state(enum cpuhp_state state)
{
enum cpuhp_state i;
- mutex_lock(&cpuhp_state_mutex);
for (i = CPUHP_AP_ONLINE_DYN; i <= CPUHP_AP_ONLINE_DYN_END; i++) {
if (cpuhp_ap_states[i].name)
continue;
cpuhp_ap_states[i].name = "Reserved";
- mutex_unlock(&cpuhp_state_mutex);
return i;
}
- mutex_unlock(&cpuhp_state_mutex);
WARN(1, "No more dynamic states available for CPU hotplug\n");
return -ENOSPC;
}
@@ -1544,6 +1539,7 @@ int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
return -EINVAL;
get_online_cpus();
+ mutex_lock(&cpuhp_state_mutex);
if (!invoke || !sp->startup.multi)
goto add_node;
@@ -1568,11 +1564,10 @@ int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
}
add_node:
ret = 0;
- mutex_lock(&cpuhp_state_mutex);
hlist_add_head(node, &sp->list);
- mutex_unlock(&cpuhp_state_mutex);
err:
+ mutex_unlock(&cpuhp_state_mutex);
put_online_cpus();
return ret;
}
@@ -1601,6 +1596,7 @@ int __cpuhp_setup_state(enum cpuhp_state state,
return -EINVAL;
get_online_cpus();
+ mutex_lock(&cpuhp_state_mutex);
/* currently assignments for the ONLINE state are possible */
if (state == CPUHP_AP_ONLINE_DYN) {
@@ -1636,6 +1632,8 @@ int __cpuhp_setup_state(enum cpuhp_state state,
}
}
out:
+ mutex_unlock(&cpuhp_state_mutex);
+
put_online_cpus();
if (!ret && dyn_state)
return state;
@@ -1655,6 +1653,8 @@ int __cpuhp_state_remove_instance(enum cpuhp_state state,
return -EINVAL;
get_online_cpus();
+ mutex_lock(&cpuhp_state_mutex);
+
if (!invoke || !cpuhp_get_teardown_cb(state))
goto remove;
/*
@@ -1671,7 +1671,6 @@ int __cpuhp_state_remove_instance(enum cpuhp_state state,
}
remove:
- mutex_lock(&cpuhp_state_mutex);
hlist_del(node);
mutex_unlock(&cpuhp_state_mutex);
put_online_cpus();
@@ -1696,6 +1695,7 @@ void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
BUG_ON(cpuhp_cb_check(state));
get_online_cpus();
+ mutex_lock(&cpuhp_state_mutex);
if (sp->multi_instance) {
WARN(!hlist_empty(&sp->list),
@@ -1721,6 +1721,7 @@ void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
}
remove:
cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
+ mutex_unlock(&cpuhp_state_mutex);
put_online_cpus();
}
EXPORT_SYMBOL(__cpuhp_remove_state);
--
2.11.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-05-01 16:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-28 16:27 [PATCH] cpu/hotplug: Serialize callback invocations proper Sebastian Andrzej Siewior
2017-04-29 5:15 ` Greg KH
2017-04-30 14:10 ` Greg KH
2017-05-01 16:59 ` [PATCH v4.9] " Sebastian Andrzej Siewior
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).