Received: by 10.223.185.116 with SMTP id b49csp817527wrg; Sat, 10 Feb 2018 21:03:43 -0800 (PST) X-Google-Smtp-Source: AH8x224i0+WmAQJRQ+voMr/xT6sA1/Pm6QnHabE2HluPQCfmxjuZR8Ar7PILBmhIgAWxE6bSZ5SF X-Received: by 2002:a17:902:309:: with SMTP id 9-v6mr7175755pld.149.1518325423085; Sat, 10 Feb 2018 21:03:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518325423; cv=none; d=google.com; s=arc-20160816; b=BqLWFTAo7kHHDMFCIaI3QYTh2IVyOjKLypZZ8EHRed7ESlSrCFXpByzyrUFLljEbZD GZfZbTbMOizg3JhbVnlbFIL3yuX/E96AGsP+WW1z1xssyoKTRHh1Gn71Zwqnzy0syJ5/ h5vhYxyrBKet4JdchkqHbqaeS86gbhqS9yXs8DBoi1w4wD0pRXN3UbUT2cTcFDtIapei +Iy+qkZe3AiyP02TZlxkc7rQI6qZBI8g/DQREw+AP4ASgeaBH9FTRB3ym3cRxgGl2SrU guoLtSj0hsnCrrYhrNE9BYyq5K4r5TlCbGyBg22cFU0sPBobzZywZW9ZhVxqC6UfbnaM xaXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=vQQ6WdzzJ3LpWByhiDRmQV1H2GusuU3I9Jh56uE/oMk=; b=k1NkUeY4PwUJ0edn7/H1IQpRBG2KT8x1DIaHBesGftRyyVKAd/oTQNSyLurFUdqnee dlunZdOiNyoDls7uvM6STLEh4VAMY89/3k3ejYfdqggKDSWDZ3k1vBlAdzP1n/mBOj7i LegNj6VtS78rIAPt5y6CroDSYhNcB9Q24GyRWtCZXgzioRMyZkGQQo4b2PWC0D46IRYg QMyx8DufsvaRWIO+cgfWi4AQa0poYcNO8fcFgPhKckxmLhdSENf5LsKd2b/ihu9a8wlH ZPFiBuKtJds274+R2pwTQ8EgnvOzBgOHIFWCTCZOJS2Xrur7LMfyYZojY02qY3Z7yUE5 3HcA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i7-v6si3960010plt.572.2018.02.10.21.03.29; Sat, 10 Feb 2018 21:03:43 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753597AbeBKFCN (ORCPT + 99 others); Sun, 11 Feb 2018 00:02:13 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:41609 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752750AbeBKEdq (ORCPT ); Sat, 10 Feb 2018 23:33:46 -0500 Received: from [2a02:8011:400e:2:6f00:88c8:c921:d332] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ekjKe-0002hL-Qn; Sun, 11 Feb 2018 04:33:40 +0000 Received: from ben by deadeye with local (Exim 4.90) (envelope-from ) id 1ekjKX-0004Tq-Px; Sun, 11 Feb 2018 04:33:33 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Ingo Molnar" , "Peter Zijlstra" , bp@suse.de, "Josh Poimboeuf" , "Thomas Gleixner" , "Linus Torvalds" , "Andy Lutomirski" , "Brian Gerst" , "Denys Vlasenko" , "Zhou Chengming" , "Steven Rostedt (VMware)" , "Borislav Petkov" , "H. Peter Anvin" , "Masami Hiramatsu" Date: Sun, 11 Feb 2018 04:20:06 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.2 30/79] kprobes, x86/alternatives: Use text_mutex to protect smp_alt_modules In-Reply-To: X-SA-Exim-Connect-IP: 2a02:8011:400e:2:6f00:88c8:c921:d332 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.2.99-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Zhou Chengming commit e846d13958066828a9483d862cc8370a72fadbb6 upstream. We use alternatives_text_reserved() to check if the address is in the fixed pieces of alternative reserved, but the problem is that we don't hold the smp_alt mutex when call this function. So the list traversal may encounter a deleted list_head if another path is doing alternatives_smp_module_del(). One solution is that we can hold smp_alt mutex before call this function, but the difficult point is that the callers of this functions, arch_prepare_kprobe() and arch_prepare_optimized_kprobe(), are called inside the text_mutex. So we must hold smp_alt mutex before we go into these arch dependent code. But we can't now, the smp_alt mutex is the arch dependent part, only x86 has it. Maybe we can export another arch dependent callback to solve this. But there is a simpler way to handle this problem. We can reuse the text_mutex to protect smp_alt_modules instead of using another mutex. And all the arch dependent checks of kprobes are inside the text_mutex, so it's safe now. Signed-off-by: Zhou Chengming Reviewed-by: Masami Hiramatsu Acked-by: Steven Rostedt (VMware) Cc: Andy Lutomirski Cc: Borislav Petkov Cc: Brian Gerst Cc: Denys Vlasenko Cc: H. Peter Anvin Cc: Josh Poimboeuf Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: bp@suse.de Fixes: 2cfa197 "ftrace/alternatives: Introducing *_text_reserved functions" Link: http://lkml.kernel.org/r/1509585501-79466-1-git-send-email-zhouchengming1@huawei.com Signed-off-by: Ingo Molnar [bwh: Backported to 3.2: adjust context] Signed-off-by: Ben Hutchings --- arch/x86/kernel/alternative.c | 26 +++++++++++++------------- kernel/extable.c | 2 ++ 2 files changed, 15 insertions(+), 13 deletions(-) --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -407,7 +407,6 @@ static void alternatives_smp_lock(const { const s32 *poff; - mutex_lock(&text_mutex); for (poff = start; poff < end; poff++) { u8 *ptr = (u8 *)poff + *poff; @@ -417,7 +416,6 @@ static void alternatives_smp_lock(const if (*ptr == 0x3e) text_poke(ptr, ((unsigned char []){0xf0}), 1); }; - mutex_unlock(&text_mutex); } static void alternatives_smp_unlock(const s32 *start, const s32 *end, @@ -425,7 +423,6 @@ static void alternatives_smp_unlock(cons { const s32 *poff; - mutex_lock(&text_mutex); for (poff = start; poff < end; poff++) { u8 *ptr = (u8 *)poff + *poff; @@ -435,7 +432,6 @@ static void alternatives_smp_unlock(cons if (*ptr == 0xf0) text_poke(ptr, ((unsigned char []){0x3E}), 1); }; - mutex_unlock(&text_mutex); } struct smp_alt_module { @@ -454,8 +450,7 @@ struct smp_alt_module { struct list_head next; }; static LIST_HEAD(smp_alt_modules); -static DEFINE_MUTEX(smp_alt); -static bool uniproc_patched = false; /* protected by smp_alt */ +static bool uniproc_patched = false; /* protected by text_mutex */ void __init_or_module alternatives_smp_module_add(struct module *mod, char *name, @@ -464,7 +459,7 @@ void __init_or_module alternatives_smp_m { struct smp_alt_module *smp; - mutex_lock(&smp_alt); + mutex_lock(&text_mutex); if (!uniproc_patched) goto unlock; @@ -491,14 +486,14 @@ void __init_or_module alternatives_smp_m smp_unlock: alternatives_smp_unlock(locks, locks_end, text, text_end); unlock: - mutex_unlock(&smp_alt); + mutex_unlock(&text_mutex); } void __init_or_module alternatives_smp_module_del(struct module *mod) { struct smp_alt_module *item; - mutex_lock(&smp_alt); + mutex_lock(&text_mutex); list_for_each_entry(item, &smp_alt_modules, next) { if (mod != item->mod) continue; @@ -506,7 +501,7 @@ void __init_or_module alternatives_smp_m kfree(item); break; } - mutex_unlock(&smp_alt); + mutex_unlock(&text_mutex); } void alternatives_enable_smp(void) @@ -527,7 +522,7 @@ void alternatives_enable_smp(void) /* Why bother if there are no other CPUs? */ BUG_ON(num_possible_cpus() == 1); - mutex_lock(&smp_alt); + mutex_lock(&text_mutex); if (uniproc_patched) { printk(KERN_INFO "SMP alternatives: switching to SMP code\n"); @@ -539,10 +534,13 @@ void alternatives_enable_smp(void) mod->text, mod->text_end); uniproc_patched = false; } - mutex_unlock(&smp_alt); + mutex_unlock(&text_mutex); } -/* Return 1 if the address range is reserved for smp-alternatives */ +/* + * Return 1 if the address range is reserved for SMP-alternatives. + * Must hold text_mutex. + */ int alternatives_text_reserved(void *start, void *end) { struct smp_alt_module *mod; @@ -550,6 +548,8 @@ int alternatives_text_reserved(void *sta u8 *text_start = start; u8 *text_end = end; + lockdep_assert_held(&text_mutex); + list_for_each_entry(mod, &smp_alt_modules, next) { if (mod->text > text_end || mod->text_end < text_start) continue; --- a/kernel/extable.c +++ b/kernel/extable.c @@ -28,6 +28,8 @@ * mutex protecting text section modification (dynamic code patching). * some users need to sleep (allocating memory...) while they hold this lock. * + * Note: Also protects SMP-alternatives modification on x86. + * * NOT exported to modules - patching kernel text is a really delicate matter. */ DEFINE_MUTEX(text_mutex);