Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp932987imu; Wed, 9 Jan 2019 08:43:54 -0800 (PST) X-Google-Smtp-Source: ALg8bN5/3e2Q9EH/W5TXAKODa5dA9pVVJBHqY2KsC6JG0pKqjC4Og9yI2xCs0fJf9d3JXffsQvBj X-Received: by 2002:a62:5d0c:: with SMTP id r12mr6894349pfb.0.1547052234538; Wed, 09 Jan 2019 08:43:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547052234; cv=none; d=google.com; s=arc-20160816; b=AZmD1GB4DLTSbvz7JEYkTtbopR5rKlkToXza6cTYLxCdfmvt2LzKxJTV6ncsCezyq+ GoH8PqVYnKlLll8AoNtnON2k+Dp58AgUVK3fjg8ak0f3M0mtBZ61zh/u+f7MjGh3JYoI q6YeaofbmgicBCr/Chcv+AOgBPMRfM2+KKs9OqJy5NJRQ/I/elFA8Lefo12Oh/32powN VaZItQvA+2sOLK/mk080RiDpQ7vOKA4rGUFJWRJyiU83oi+Zgri/xt1teAHDMHqWyWMK zSvPjMGd5UjVxU6XnMuz19580cQtDz1eHsFuC7ysHP/yX2UjxMH/tZ85SHKh8Lb2Qi87 SRnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=K1tRkeI5BeLyxOOB2TcgP1RCH+8dNqc0HvAOTmOdEQ8=; b=NIV6H2bph3487klfDZu1pkBYQ6fAo3iLf2ZthWDYFzz6QQ90Uk+8+l1Y14Nle888+o DYgX7IWgYmM43LjJ5btjxq+mZQiONuN/izWa/MGnBSZJSCd0GdlBzIiQRA9fr/l+5XLs N1xAR+1wvlf5hgG6vsTMmgaSjKr4jGT7rcdHcuO6KJItn74C/ShqREpykLFzOjEVLqZD koZC1E2KmRD8nw9xXnqKgAcHGk2/xSVZSgVR8aQEb2F7ihmj5E4hmai5DzpVrIrQibF8 kKT70CfMq7g8S9blAQr+9QVUh0ZoFFf+9vDFUwp2lI7FA/VgRS8PpcGm0+wH6DoCmwOz Wuzw== 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 i129si76203982pfb.32.2019.01.09.08.43.38; Wed, 09 Jan 2019 08:43:54 -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 S1732600AbfAIQIU (ORCPT + 99 others); Wed, 9 Jan 2019 11:08:20 -0500 Received: from mail.kernel.org ([198.145.29.99]:36120 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731508AbfAIQIT (ORCPT ); Wed, 9 Jan 2019 11:08:19 -0500 Received: from gandalf.local.home (cpe-66-24-56-78.stny.res.rr.com [66.24.56.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 23921206BB; Wed, 9 Jan 2019 16:08:18 +0000 (UTC) Date: Wed, 9 Jan 2019 11:08:16 -0500 From: Steven Rostedt To: Masami Hiramatsu Cc: Ingo Molnar , peterz@infradead.org, Mathieu Desnoyers , linux-kernel , Andrea Righi , stable@vger.kernel.org Subject: Re: [PATCH v2 3/3] x86/kprobes: Fix to avoid kretprobe recursion Message-ID: <20190109110816.72a7ae85@gandalf.local.home> In-Reply-To: <154692272225.1133.799265848498671353.stgit@devbox> References: <154692263564.1133.17363562046971295490.stgit@devbox> <154692272225.1133.799265848498671353.stgit@devbox> X-Mailer: Claws Mail 3.16.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 8 Jan 2019 13:45:22 +0900 Masami Hiramatsu wrote: > Fix to avoid kretprobe recursion loop by setting a dummy > kprobes to current_kprobe per-cpu variable. > > This bug has been introduced with the asm-coded trampoline > code, since previously it used another kprobe for hooking > the function return placeholder (which only has a nop) and > trampoline handler was called from that kprobe. > > This revives the old lost kprobe again. > > With this fix, we don't see deadlock anymore. > > # echo "r:event_1 __fdget" >> kprobe_events > # echo "r:event_2 _raw_spin_lock_irqsave" >> kprobe_events > # echo 1 > events/kprobes/enable > > And you can see that all inner-called kretprobe are skipped. > > # cat kprobe_profile > event_1 235 0 > event_2 19375 19612 > > The 1st column is recorded count and the 2nd is missed count. > Above shows (event_1 rec) + (event_2 rec) ~= (event_2 missed) I don't quite understand the above. Is the miss count because we missed event_2 events for both event_1 and event_2? trace raw_spin_lock() handler calls raw_spin_lock() trace raw_spin_lock() [ skip ] I'm also guessing that the 2 extra (19612 - (235 + 19375) = 2) are possibly due to the displaying being racy? > > Signed-off-by: Masami Hiramatsu > Reported-by: Andrea Righi > Fixes: c9becf58d935 ("[PATCH] kretprobe: kretprobe-booster") > Cc: stable@vger.kernel.org > --- > arch/x86/kernel/kprobes/core.c | 22 ++++++++++++++++++++-- > 1 file changed, 20 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c > index 69b6400d1ce2..f4b954ff5b89 100644 > --- a/arch/x86/kernel/kprobes/core.c > +++ b/arch/x86/kernel/kprobes/core.c > @@ -749,11 +749,16 @@ asm( > NOKPROBE_SYMBOL(kretprobe_trampoline); > STACK_FRAME_NON_STANDARD(kretprobe_trampoline); > > +static struct kprobe kretprobe_kprobe = { > + .addr = (void *)kretprobe_trampoline, > +}; > + > /* > * Called from kretprobe_trampoline > */ > static __used void *trampoline_handler(struct pt_regs *regs) > { > + struct kprobe_ctlblk *kcb; > struct kretprobe_instance *ri = NULL; > struct hlist_head *head, empty_rp; > struct hlist_node *tmp; > @@ -763,6 +768,17 @@ static __used void *trampoline_handler(struct pt_regs *regs) > void *frame_pointer; > bool skipped = false; > > + preempt_disable(); > + > + /* > + * Set a dummy kprobe for avoiding kretprobe recursion. > + * Since kretprobe never run in kprobe handler, kprobe must not > + * be running at this point. > + */ > + kcb = get_kprobe_ctlblk(); > + __this_cpu_write(current_kprobe, &kretprobe_kprobe); If an interrupt comes in here, is this still safe, if the interrupt handler has a kretprobe too? -- Steve > + kcb->kprobe_status = KPROBE_HIT_ACTIVE; > + > INIT_HLIST_HEAD(&empty_rp); > kretprobe_hash_lock(current, &head, &flags); > /* fixup registers */ > @@ -838,10 +854,9 @@ static __used void *trampoline_handler(struct pt_regs *regs) > orig_ret_address = (unsigned long)ri->ret_addr; > if (ri->rp && ri->rp->handler) { > __this_cpu_write(current_kprobe, &ri->rp->kp); > - get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE; > ri->ret_addr = correct_ret_addr; > ri->rp->handler(ri, regs); > - __this_cpu_write(current_kprobe, NULL); > + __this_cpu_write(current_kprobe, &kretprobe_kprobe); > } > > recycle_rp_inst(ri, &empty_rp); > @@ -857,6 +872,9 @@ static __used void *trampoline_handler(struct pt_regs *regs) > > kretprobe_hash_unlock(current, &flags); > > + __this_cpu_write(current_kprobe, NULL); > + preempt_enable(); > + > hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) { > hlist_del(&ri->hlist); > kfree(ri);