Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1692235 > unrolled thread

[PATCH] cpu hotplug: Prevent Page Fault in cpuhp_remove_callbacks()

Started byEthan Barnes <Ethan.Barnes@wdc.com>
First post2017-07-20 00:40 +0200
Last post2017-07-20 17:00 +0200
Articles 3 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] cpu hotplug: Prevent Page Fault in cpuhp_remove_callbacks() Ethan Barnes <Ethan.Barnes@wdc.com> - 2017-07-20 00:40 +0200
    Re: [PATCH] cpu hotplug: Prevent Page Fault in  cpuhp_remove_callbacks() Thomas Gleixner <tglx@linutronix.de> - 2017-07-20 16:10 +0200
    [tip:smp/hotplug] smp/hotplug: Handle removal correctly in  cpuhp_store_callbacks() tip-bot for Ethan Barnes <tipbot@zytor.com> - 2017-07-20 17:00 +0200

#1692235 — [PATCH] cpu hotplug: Prevent Page Fault in cpuhp_remove_callbacks()

FromEthan Barnes <Ethan.Barnes@wdc.com>
Date2017-07-20 00:40 +0200
Subject[PATCH] cpu hotplug: Prevent Page Fault in cpuhp_remove_callbacks()
Message-ID<u55No-6BP-7@gated-at.bofh.it>
Hi,

There is a page fault in cpu hotplug when removing the callbacks for the 
last dynamic state.
The last dynamic state will not be removed, causing a page fault when 
hotplug thinks the callbacks still exists and calls it.

The problem is as follows:
- __cpuhp_remove_state() eventually calls cpuhp_store_callbacks() with 
NULLs for most params.
- If state is the last state (i.e. CPUHP_AP_ONLINE_DYN or 
CPUHP_BP_PREPARE_STATE) then cpuhp_store_callbacks() calls 
cpuhp_reserve_state(), which returns the *next* available DYN state.
- The NULLs are stored in that *next* state (i.e. AP_ONLINE_DYN + 1 or 
BP_PREPARE_DYN + 1) instead of in AP_ONLINE_DYN or BP_PREPARE_DYN. Thus, 
the last state is never cleared. The callbacks now point to invalid memory.
- When the cpu is onlined again and/or offlined, then the invalid 
callback is invoked, causing a page fault.

The patch above solves this by detecting when a state is being removed, 
and not calling cpuhp_reserve_state().

I have brought this up before, but hoping to get some traction on it 
this time:
https://lkml.org/lkml/2017/7/5/574

Thx,
Ethan


Signed-off-by: Ethan Barnes <ethan.barnes@sandisk.com>
---

diff --git a/kernel/cpu.c b/kernel/cpu.c
index eee0331..f7fda16 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -1252,7 +1252,8 @@ static int cpuhp_store_callbacks(enum cpuhp_state 
state, const char *name,
         struct cpuhp_step *sp;
         int ret = 0;

-       if (state == CPUHP_AP_ONLINE_DYN || state == CPUHP_BP_PREPARE_DYN) {
+       if (name &&
+           (state == CPUHP_AP_ONLINE_DYN || state == 
CPUHP_BP_PREPARE_DYN)) {
                 ret = cpuhp_reserve_state(state);
                 if (ret < 0)
                         return ret;

[toc] | [next] | [standalone]


#1692966 — Re: [PATCH] cpu hotplug: Prevent Page Fault in cpuhp_remove_callbacks()

FromThomas Gleixner <tglx@linutronix.de>
Date2017-07-20 16:10 +0200
SubjectRe: [PATCH] cpu hotplug: Prevent Page Fault in cpuhp_remove_callbacks()
Message-ID<u5kjo-7N-5@gated-at.bofh.it>
In reply to#1692235
On Wed, 19 Jul 2017, Ethan Barnes wrote:
> There is a page fault in cpu hotplug when removing the callbacks for the 
> last dynamic state.
> The last dynamic state will not be removed, causing a page fault when 
> hotplug thinks the callbacks still exists and calls it.
> 
> The problem is as follows:
> - __cpuhp_remove_state() eventually calls cpuhp_store_callbacks() with 
> NULLs for most params.
> - If state is the last state (i.e. CPUHP_AP_ONLINE_DYN or 
> CPUHP_BP_PREPARE_STATE) then cpuhp_store_callbacks() calls 
> cpuhp_reserve_state(), which returns the *next* available DYN state.
> - The NULLs are stored in that *next* state (i.e. AP_ONLINE_DYN + 1 or 
> BP_PREPARE_DYN + 1) instead of in AP_ONLINE_DYN or BP_PREPARE_DYN. Thus, 
> the last state is never cleared. The callbacks now point to invalid memory.
> - When the cpu is onlined again and/or offlined, then the invalid 
> callback is invoked, causing a page fault.

Indeed.

> The patch above solves this by detecting when a state is being removed, 
> and not calling cpuhp_reserve_state().
> 
> I have brought this up before, but hoping to get some traction on it 
> this time:
> https://lkml.org/lkml/2017/7/5/574

Escaped my attention.

> diff --git a/kernel/cpu.c b/kernel/cpu.c
> index eee0331..f7fda16 100644
> --- a/kernel/cpu.c
> +++ b/kernel/cpu.c
> @@ -1252,7 +1252,8 @@ static int cpuhp_store_callbacks(enum cpuhp_state 
> state, const char *name,

That patch is corrupted by your mail-client or mail-server or both.

>          struct cpuhp_step *sp;
>          int ret = 0;
> 
> -       if (state == CPUHP_AP_ONLINE_DYN || state == CPUHP_BP_PREPARE_DYN) {
> +       if (name &&
> +           (state == CPUHP_AP_ONLINE_DYN || state == 
> CPUHP_BP_PREPARE_DYN)) {
>                  ret = cpuhp_reserve_state(state);
>                  if (ret < 0)
>                          return ret;
> Western Digital Corporation (and its subsidiaries) E-mail Confidentiality Notice & Disclaimer:
>

Can you please make sure that there is a blank line before that
automatically inserted disclaimer?

Thanks,

	tglx

[toc] | [prev] | [next] | [standalone]


#1693004 — [tip:smp/hotplug] smp/hotplug: Handle removal correctly in cpuhp_store_callbacks()

Fromtip-bot for Ethan Barnes <tipbot@zytor.com>
Date2017-07-20 17:00 +0200
Subject[tip:smp/hotplug] smp/hotplug: Handle removal correctly in cpuhp_store_callbacks()
Message-ID<u5l5N-pl-13@gated-at.bofh.it>
In reply to#1692235
Commit-ID:  0c96b27305faf06c068b45e07d28336c80dac286
Gitweb:     http://git.kernel.org/tip/0c96b27305faf06c068b45e07d28336c80dac286
Author:     Ethan Barnes <Ethan.Barnes@wdc.com>
AuthorDate: Wed, 19 Jul 2017 22:36:00 +0000
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Thu, 20 Jul 2017 16:40:24 +0200

smp/hotplug: Handle removal correctly in cpuhp_store_callbacks()

If cpuhp_store_callbacks() is called for CPUHP_AP_ONLINE_DYN or
CPUHP_BP_PREPARE_DYN, which are the indicators for dynamically allocated
states, then cpuhp_store_callbacks() allocates a new dynamic state. The
first allocation in each range returns CPUHP_AP_ONLINE_DYN or
CPUHP_BP_PREPARE_DYN.

If cpuhp_remove_state() is invoked for one of these states, then there is
no protection against the allocation mechanism. So the removal, which
should clear the callbacks and the name, gets a new state assigned and
clears that one.

As a consequence the state which should be cleared stays initialized. A
consecutive CPU hotplug operation dereferences the state callbacks and
accesses either freed or reused memory, resulting in crashes.

Add a protection against this by checking the name argument for NULL. If
it's NULL it's a removal. If not, it's an allocation.

[ tglx: Added a comment and massaged changelog ]

Fixes: 5b7aa87e0482 ("cpu/hotplug: Implement setup/removal interface")
Signed-off-by: Ethan Barnes <ethan.barnes@sandisk.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@kernel.or>
Cc: "Srivatsa S. Bhat" <srivatsa@mit.edu>
Cc: Sebastian Siewior <bigeasy@linutronix.d>
Cc: Paul McKenney <paulmck@linux.vnet.ibm.com>
Cc: stable@vger.kernel.org
Link: http://lkml.kernel.org/r/DM2PR04MB398242FC7776D603D9F99C894A60@DM2PR04MB398.namprd04.prod.outlook.com
---
 kernel/cpu.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/kernel/cpu.c b/kernel/cpu.c
index eee0331..a88c29a 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -1252,7 +1252,17 @@ static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
 	struct cpuhp_step *sp;
 	int ret = 0;
 
-	if (state == CPUHP_AP_ONLINE_DYN || state == CPUHP_BP_PREPARE_DYN) {
+	/*
+	 * If name is NULL, then the state gets removed.
+	 *
+	 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
+	 * the first allocation from these dynamic ranges, so the removal
+	 * would trigger a new allocation and clear the wrong (already
+	 * empty) state, leaving the callbacks of the to be cleared state
+	 * dangling, which causes wreckage on the next hotplug operation.
+	 */
+	if (name && (state == CPUHP_AP_ONLINE_DYN ||
+		     state == CPUHP_BP_PREPARE_DYN)) {
 		ret = cpuhp_reserve_state(state);
 		if (ret < 0)
 			return ret;

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web