From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935624AbdGTOu0 (ORCPT ); Thu, 20 Jul 2017 10:50:26 -0400 Received: from terminus.zytor.com ([65.50.211.136]:38957 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934466AbdGTOuY (ORCPT ); Thu, 20 Jul 2017 10:50:24 -0400 Date: Thu, 20 Jul 2017 07:49:09 -0700 From: tip-bot for Ethan Barnes Message-ID: Cc: srivatsa@mit.edu, bigeasy@linutronix.d, mingo@kernel.or, mingo@kernel.org, tglx@linutronix.de, linux-kernel@vger.kernel.org, Ethan.Barnes@wdc.com, hpa@zytor.com, paulmck@linux.vnet.ibm.com, ethan.barnes@sandisk.com Reply-To: bigeasy@linutronix.d, srivatsa@mit.edu, mingo@kernel.or, tglx@linutronix.de, mingo@kernel.org, hpa@zytor.com, linux-kernel@vger.kernel.org, Ethan.Barnes@wdc.com, paulmck@linux.vnet.ibm.com, ethan.barnes@sandisk.com In-Reply-To: References: To: linux-tip-commits@vger.kernel.org Subject: [tip:smp/hotplug] smp/hotplug: Handle removal correctly in cpuhp_store_callbacks() Git-Commit-ID: 0c96b27305faf06c068b45e07d28336c80dac286 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 0c96b27305faf06c068b45e07d28336c80dac286 Gitweb: http://git.kernel.org/tip/0c96b27305faf06c068b45e07d28336c80dac286 Author: Ethan Barnes AuthorDate: Wed, 19 Jul 2017 22:36:00 +0000 Committer: Thomas Gleixner 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 Signed-off-by: Thomas Gleixner Cc: Ingo Molnar Cc: "Srivatsa S. Bhat" Cc: Sebastian Siewior Cc: Paul McKenney 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;