Received: by 10.223.185.116 with SMTP id b49csp1018579wrg; Fri, 16 Feb 2018 10:55:16 -0800 (PST) X-Google-Smtp-Source: AH8x224QPj4dOA0aUJ2siROvwM7auZdwkbb0KeJlP/jSzpOz6Mu4HI2WFdaNvHN39BFNtuXg3l2c X-Received: by 10.98.9.138 with SMTP id 10mr7010940pfj.122.1518807315956; Fri, 16 Feb 2018 10:55:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518807315; cv=none; d=google.com; s=arc-20160816; b=wLh4l+18rcHVvkpZmz/zKvo748LNezS0PjhymKYqEuSFZPWDkFQAMdgRMYXH2BcKtC yFm0PckE8QbWiZJmKOwGWHzNp15jpP1Ib1rgghF7QKJFHwIPHXpdQT7tJOdIx8c7Whoh aA0pTr5jGLrWUFTO/TCzT3bro0303S/XMAIEypTEkmoKvT1xwacd0RJOT8XmA6Sxr4dW lS/+9s4/93UKPxVr1id/ZoiVpPTpSxDDxLF33K9IRWckKfQ5Lu7tzWsYzSU9s88+N2ye JyL4X3BW9CXna5oj+vq7ZS0zYrN1PZaGQiHx0wxhYswtm7BoSci2SpNjEUMszkROYQ9E 2D/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:arc-authentication-results; bh=/Za5oYoPizLR9IyO49MoURpDouNslB/J6GsfVSp9/34=; b=UBO3eyeOjWAS5P6ZrbhvZNBF72yTyCX5lKG2SRgKhLcQv4vQ8b67Y94YHNdLZ96Fsv sFFtb6klKB2l/HAml/k/wOD7/dFBFBl701C06WF3RkCSE7qf98REqJRvaAkA2s/V8i4l hLy/P3ngGqQwIk+djZOHmOBv5amMGx1umUg9PXtv36QXVtrkpY/sP5o1VOhXvdpBZOmj u64JqbtnzTk654HRHhoI28CM/AylODe47+yGlWf6BRblhMMAxDT+60s1SiKgVCSbfceW t01yZh2vDIeP4gVpAONOaCrA29FXnLB2sHSbg2xdatzjJ9nOQe+7y9HBWkSrO2G/w0WD 1TjQ== 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 g124si11222826pgc.114.2018.02.16.10.55.00; Fri, 16 Feb 2018 10:55:15 -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 S932462AbeBPJrm (ORCPT + 99 others); Fri, 16 Feb 2018 04:47:42 -0500 Received: from terminus.zytor.com ([198.137.202.136]:52377 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932325AbeBPJrj (ORCPT ); Fri, 16 Feb 2018 04:47:39 -0500 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTP id w1G9l2S0025717; Fri, 16 Feb 2018 01:47:02 -0800 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w1G9l11P025714; Fri, 16 Feb 2018 01:47:01 -0800 Date: Fri, 16 Feb 2018 01:47:01 -0800 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Jessica Yu Message-ID: Cc: davem@davemloft.net, linux-kernel@vger.kernel.org, joe.lawrence@redhat.com, mhiramat@kernel.org, tglx@linutronix.de, ananth@linux.vnet.ibm.com, jikos@kernel.org, anil.s.keshavamurthy@intel.com, rostedt@goodmis.org, mingo@kernel.org, torvalds@linux-foundation.org, jeyu@kernel.org, hpa@zytor.com, jpoimboe@redhat.com, pmladek@suse.com, peterz@infradead.org, mbenes@suse.cz Reply-To: hpa@zytor.com, mingo@kernel.org, jeyu@kernel.org, torvalds@linux-foundation.org, jpoimboe@redhat.com, jikos@kernel.org, anil.s.keshavamurthy@intel.com, rostedt@goodmis.org, peterz@infradead.org, mbenes@suse.cz, pmladek@suse.com, mhiramat@kernel.org, joe.lawrence@redhat.com, davem@davemloft.net, linux-kernel@vger.kernel.org, ananth@linux.vnet.ibm.com, tglx@linutronix.de In-Reply-To: <20180109235124.30886-3-jeyu@kernel.org> References: <20180109235124.30886-3-jeyu@kernel.org> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/urgent] kprobes: Propagate error from disarm_kprobe_ftrace() Git-Commit-ID: 297f9233b53a08fd457815e19f1d6f2c3389857b 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 X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 297f9233b53a08fd457815e19f1d6f2c3389857b Gitweb: https://git.kernel.org/tip/297f9233b53a08fd457815e19f1d6f2c3389857b Author: Jessica Yu AuthorDate: Wed, 10 Jan 2018 00:51:24 +0100 Committer: Ingo Molnar CommitDate: Fri, 16 Feb 2018 09:12:58 +0100 kprobes: Propagate error from disarm_kprobe_ftrace() Improve error handling when disarming ftrace-based kprobes. Like with arm_kprobe_ftrace(), propagate any errors from disarm_kprobe_ftrace() so that we do not disable/unregister kprobes that are still armed. In other words, unregister_kprobe() and disable_kprobe() should not report success if the kprobe could not be disarmed. disarm_all_kprobes() keeps its current behavior and attempts to disarm all kprobes. It returns the last encountered error and gives a warning if not all probes could be disarmed. This patch is based on Petr Mladek's original patchset (patches 2 and 3) back in 2015, which improved kprobes error handling, found here: https://lkml.org/lkml/2015/2/26/452 However, further work on this had been paused since then and the patches were not upstreamed. Based-on-patches-by: Petr Mladek Signed-off-by: Jessica Yu Acked-by: Masami Hiramatsu Cc: Ananth N Mavinakayanahalli Cc: Anil S Keshavamurthy Cc: David S . Miller Cc: Jiri Kosina Cc: Joe Lawrence Cc: Josh Poimboeuf Cc: Linus Torvalds Cc: Miroslav Benes Cc: Peter Zijlstra Cc: Petr Mladek Cc: Steven Rostedt Cc: Thomas Gleixner Cc: live-patching@vger.kernel.org Link: http://lkml.kernel.org/r/20180109235124.30886-3-jeyu@kernel.org Signed-off-by: Ingo Molnar --- kernel/kprobes.c | 78 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 2d98814..102160f 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1011,23 +1011,27 @@ err_ftrace: } /* Caller must lock kprobe_mutex */ -static void disarm_kprobe_ftrace(struct kprobe *p) +static int disarm_kprobe_ftrace(struct kprobe *p) { - int ret; + int ret = 0; - kprobe_ftrace_enabled--; - if (kprobe_ftrace_enabled == 0) { + if (kprobe_ftrace_enabled == 1) { ret = unregister_ftrace_function(&kprobe_ftrace_ops); - WARN(ret < 0, "Failed to init kprobe-ftrace (%d)\n", ret); + if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret)) + return ret; } + + kprobe_ftrace_enabled--; + ret = ftrace_set_filter_ip(&kprobe_ftrace_ops, (unsigned long)p->addr, 1, 0); WARN(ret < 0, "Failed to disarm kprobe-ftrace at %p (%d)\n", p->addr, ret); + return ret; } #else /* !CONFIG_KPROBES_ON_FTRACE */ #define prepare_kprobe(p) arch_prepare_kprobe(p) #define arm_kprobe_ftrace(p) (-ENODEV) -#define disarm_kprobe_ftrace(p) do {} while (0) +#define disarm_kprobe_ftrace(p) (-ENODEV) #endif /* Arm a kprobe with text_mutex */ @@ -1046,18 +1050,18 @@ static int arm_kprobe(struct kprobe *kp) } /* Disarm a kprobe with text_mutex */ -static void disarm_kprobe(struct kprobe *kp, bool reopt) +static int disarm_kprobe(struct kprobe *kp, bool reopt) { - if (unlikely(kprobe_ftrace(kp))) { - disarm_kprobe_ftrace(kp); - return; - } + if (unlikely(kprobe_ftrace(kp))) + return disarm_kprobe_ftrace(kp); cpus_read_lock(); mutex_lock(&text_mutex); __disarm_kprobe(kp, reopt); mutex_unlock(&text_mutex); cpus_read_unlock(); + + return 0; } /* @@ -1639,11 +1643,12 @@ static int aggr_kprobe_disabled(struct kprobe *ap) static struct kprobe *__disable_kprobe(struct kprobe *p) { struct kprobe *orig_p; + int ret; /* Get an original kprobe for return */ orig_p = __get_valid_kprobe(p); if (unlikely(orig_p == NULL)) - return NULL; + return ERR_PTR(-EINVAL); if (!kprobe_disabled(p)) { /* Disable probe if it is a child probe */ @@ -1657,8 +1662,13 @@ static struct kprobe *__disable_kprobe(struct kprobe *p) * should have already been disarmed, so * skip unneed disarming process. */ - if (!kprobes_all_disarmed) - disarm_kprobe(orig_p, true); + if (!kprobes_all_disarmed) { + ret = disarm_kprobe(orig_p, true); + if (ret) { + p->flags &= ~KPROBE_FLAG_DISABLED; + return ERR_PTR(ret); + } + } orig_p->flags |= KPROBE_FLAG_DISABLED; } } @@ -1675,8 +1685,8 @@ static int __unregister_kprobe_top(struct kprobe *p) /* Disable kprobe. This will disarm it if needed. */ ap = __disable_kprobe(p); - if (ap == NULL) - return -EINVAL; + if (IS_ERR(ap)) + return PTR_ERR(ap); if (ap == p) /* @@ -2109,12 +2119,14 @@ static void kill_kprobe(struct kprobe *p) int disable_kprobe(struct kprobe *kp) { int ret = 0; + struct kprobe *p; mutex_lock(&kprobe_mutex); /* Disable this kprobe */ - if (__disable_kprobe(kp) == NULL) - ret = -EINVAL; + p = __disable_kprobe(kp); + if (IS_ERR(p)) + ret = PTR_ERR(p); mutex_unlock(&kprobe_mutex); return ret; @@ -2486,34 +2498,50 @@ already_enabled: return ret; } -static void disarm_all_kprobes(void) +static int disarm_all_kprobes(void) { struct hlist_head *head; struct kprobe *p; - unsigned int i; + unsigned int i, total = 0, errors = 0; + int err, ret = 0; mutex_lock(&kprobe_mutex); /* If kprobes are already disarmed, just return */ if (kprobes_all_disarmed) { mutex_unlock(&kprobe_mutex); - return; + return 0; } kprobes_all_disarmed = true; - printk(KERN_INFO "Kprobes globally disabled\n"); for (i = 0; i < KPROBE_TABLE_SIZE; i++) { head = &kprobe_table[i]; + /* Disarm all kprobes on a best-effort basis */ hlist_for_each_entry_rcu(p, head, hlist) { - if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) - disarm_kprobe(p, false); + if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) { + err = disarm_kprobe(p, false); + if (err) { + errors++; + ret = err; + } + total++; + } } } + + if (errors) + pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n", + errors, total); + else + pr_info("Kprobes globally disabled\n"); + mutex_unlock(&kprobe_mutex); /* Wait for disarming all kprobes by optimizer */ wait_for_kprobe_optimizer(); + + return ret; } /* @@ -2556,7 +2584,7 @@ static ssize_t write_enabled_file_bool(struct file *file, case 'n': case 'N': case '0': - disarm_all_kprobes(); + ret = disarm_all_kprobes(); break; default: return -EINVAL;