From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D4C9C433DF for ; Thu, 27 Aug 2020 16:40:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 29B5022BEA for ; Thu, 27 Aug 2020 16:40:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598546446; bh=OxjpBA84KDi5ZWWyg3y0i/wou8N/yNawXlcHxpgAmK0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=W0fBh9WKXfmGZxjhZTVWnUAErBYz5E/fOmHWQ7ZWnq52qCmLuzsJM89ABdPjTJFxK CJOEvs5j9++PP9SSCQK9pbbd+nf1ci1fd6rA+DPgUPNTl30OVvENCb0akxnlVgKDfY 8TgaAQTQgk75g3Vf3lajRLVB82cfW0sEOCRsdzdI= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728139AbgH0Qkn (ORCPT ); Thu, 27 Aug 2020 12:40:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:47824 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727034AbgH0Qkl (ORCPT ); Thu, 27 Aug 2020 12:40:41 -0400 Received: from localhost.localdomain (NE2965lan1.rev.em-net.ne.jp [210.141.244.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 58B072087C; Thu, 27 Aug 2020 16:40:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598546441; bh=OxjpBA84KDi5ZWWyg3y0i/wou8N/yNawXlcHxpgAmK0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M0GdbQMTWIUB8V6oi3UV8hh/VkMuL/pECtmbjy710uYTFW3+I8MlgZbzXCtwK43aD Ukv6xFpI58P3L/6Zj3KrgdH8EIG3Npk/9Em5EJ8OmGpt/By+3jUCE+aPS9SSCPV7zc BD5RjxyxzxVjJL2gIOXhOid/Vvbwae8TGsd+fFlY= From: Masami Hiramatsu To: linux-kernel@vger.kernel.org, Peter Zijlstra Cc: Eddy Wu , x86@kernel.org, "David S . Miller" , Steven Rostedt , Ingo Molnar , "Naveen N . Rao" , Anil S Keshavamurthy , linux-arch@vger.kernel.org, guoren@kernel.org Subject: [PATCH v3 14/16] kprobes: Remove NMI context check Date: Fri, 28 Aug 2020 01:40:37 +0900 Message-Id: <159854643764.736475.26898278150324328.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <159854631442.736475.5062989489155389472.stgit@devnote2> References: <159854631442.736475.5062989489155389472.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Since the commit 9b38cc704e84 ("kretprobe: Prevent triggering kretprobe from within kprobe_flush_task") sets a dummy current kprobe in the trampoline handler by kprobe_busy_begin/end(), it is not possible to run a kretprobe pre handler in kretprobe trampoline handler context even with the NMI. If the NMI interrupts a kretprobe_trampoline_handler() and it hits a kretprobe, the 2nd kretprobe will detect recursion correctly and it will be skipped. This means we have almost no double-lock issue on kretprobes by NMI. The last one point is in cleanup_rp_inst() which also takes kretprobe_table_lock without setting up current kprobes. So adding kprobe_busy_begin/end() there allows us to remove in_nmi() check. The above commit applies kprobe_busy_begin/end() on x86, but now all arch implementation are unified to generic one, we can safely remove the in_nmi() check from arch independent code. Signed-off-by: Masami Hiramatsu --- kernel/kprobes.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index cbd2ad1af7b7..311033e4b8e4 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1359,7 +1359,8 @@ static void cleanup_rp_inst(struct kretprobe *rp) struct hlist_node *next; struct hlist_head *head; - /* No race here */ + /* To avoid recursive kretprobe by NMI, set kprobe busy here */ + kprobe_busy_begin(); for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) { kretprobe_table_lock(hash, &flags); head = &kretprobe_inst_table[hash]; @@ -1369,6 +1370,8 @@ static void cleanup_rp_inst(struct kretprobe *rp) } kretprobe_table_unlock(hash, &flags); } + kprobe_busy_end(); + free_rp_inst(rp); } NOKPROBE_SYMBOL(cleanup_rp_inst); @@ -2038,17 +2041,6 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) unsigned long hash, flags = 0; struct kretprobe_instance *ri; - /* - * To avoid deadlocks, prohibit return probing in NMI contexts, - * just skip the probe and increase the (inexact) 'nmissed' - * statistical counter, so that the user is informed that - * something happened: - */ - if (unlikely(in_nmi())) { - rp->nmissed++; - return 0; - } - /* TODO: consider to only swap the RA after the last pre_handler fired */ hash = hash_ptr(current, KPROBE_HASH_BITS); raw_spin_lock_irqsave(&rp->lock, flags);