Received: by 10.192.165.148 with SMTP id m20csp2538400imm; Sun, 22 Apr 2018 09:07:09 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+Qpnii0yVMNJalSeQp/YKDwcpfWozR7Tyk/6DXCJbjrNtzSGw+8oaep/N1ztFl4dLw+i8g X-Received: by 10.99.37.196 with SMTP id l187mr14680680pgl.221.1524413229886; Sun, 22 Apr 2018 09:07:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524413229; cv=none; d=google.com; s=arc-20160816; b=RgbPS6/9XTTrSJNP9SKzaknTtozW2xGKxtDCA7Vk09ZncLr8bM/vderfNgvPj2eqg5 ee4zAd+4/4ji7XCMhDNiJAZLsaCOpiaoOecPdASW+iYDoUeqe/qYxX0tP6jRDOWLwxxP VNk284ZXDF74pqFpU7u1tcJfKl2BKeKDs7/INvMm4LjhHD7LfSN23JZ2HXW9bGY9APs5 G/9q9n0a4+biIQOsn4xArhFKTOJCRxlOTOdJez9Fa4dxe7ereCalXngpSyyI307nkbwp DyJelr6vmHqjgA5z/kqjE2qkhDYpl0JRRIcLMnXu6DeVwv4Bj/I6Y/+TUARWbuUY72QY uRJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=8SIe16ExkdHKPcNgbgEgu5/NcG85zzVBpqiR0UBut8Y=; b=wnjpbx5zbUXEOVUVBMrGeUsmygg7gyDXU+dmQSjJXiQA6t1xaoGPQqhgAaQs/N4msa /gdiQLh/BIcZXcto4jIaL52gD45nBzrb+Q6u0aEjk02fflVl2K5pkfEdgRlC83ATTMuC EkRMJSyngNd+lPs5lRNACGi45QttbylHA/ThA0w2hpQdfXhVtepgqkpnmpPFfefCR7G6 SgGmiZbCyjH6gGknDFkPQERUhwaVktioDit/KtbAA+6hYLEMyapkkDo0wJfWX2LBmQ0a nebTjwjNL4iBMrgWldEWGzN/XHkhG7PUseKwDiJlA0TtedsPGAwIwEXk+2LyySLcgXS4 rX7w== 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 d16si2322775pgn.563.2018.04.22.09.06.55; Sun, 22 Apr 2018 09:07:09 -0700 (PDT) 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 S1754115AbeDVQFz (ORCPT + 99 others); Sun, 22 Apr 2018 12:05:55 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:45618 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753948AbeDVN5P (ORCPT ); Sun, 22 Apr 2018 09:57:15 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 8CE6B9CE; Sun, 22 Apr 2018 13:57:14 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Michael Ellerman , "Naveen N. Rao" , Ananth N Mavinakayanahalli Subject: [PATCH 4.16 065/196] powerpc/kprobes: Fix call trace due to incorrect preempt count Date: Sun, 22 Apr 2018 15:51:25 +0200 Message-Id: <20180422135107.613454427@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180422135104.278511750@linuxfoundation.org> References: <20180422135104.278511750@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Naveen N. Rao commit e6e133c47e6bd4d5dac05b35d06634a8e5648615 upstream. Michael Ellerman reported the following call trace when running ftracetest: BUG: using __this_cpu_write() in preemptible [00000000] code: ftracetest/6178 caller is opt_pre_handler+0xc4/0x110 CPU: 1 PID: 6178 Comm: ftracetest Not tainted 4.15.0-rc7-gcc6x-gb2cd1df #1 Call Trace: [c0000000f9ec39c0] [c000000000ac4304] dump_stack+0xb4/0x100 (unreliable) [c0000000f9ec3a00] [c00000000061159c] check_preemption_disabled+0x15c/0x170 [c0000000f9ec3a90] [c000000000217e84] opt_pre_handler+0xc4/0x110 [c0000000f9ec3af0] [c00000000004cf68] optimized_callback+0x148/0x170 [c0000000f9ec3b40] [c00000000004d954] optinsn_slot+0xec/0x10000 [c0000000f9ec3e30] [c00000000004bae0] kretprobe_trampoline+0x0/0x10 This is showing up since OPTPROBES is now enabled with CONFIG_PREEMPT. trampoline_probe_handler() considers itself to be a special kprobe handler for kretprobes. In doing so, it expects to be called from kprobe_handler() on a trap, and re-enables preemption before returning a non-zero return value so as to suppress any subsequent processing of the trap by the kprobe_handler(). However, with optprobes, we don't deal with special handlers (we ignore the return code) and just try to re-enable preemption causing the above trace. To address this, modify trampoline_probe_handler() to not be special. The only additional processing done in kprobe_handler() is to emulate the instruction (in this case, a 'nop'). We adjust the value of regs->nip for the purpose and delegate the job of re-enabling preemption and resetting current kprobe to the probe handlers (kprobe_handler() or optimized_callback()). Fixes: 8a2d71a3f273 ("powerpc/kprobes: Disable preemption before invoking probe handler for optprobes") Cc: stable@vger.kernel.org # v4.15+ Reported-by: Michael Ellerman Signed-off-by: Naveen N. Rao Acked-by: Ananth N Mavinakayanahalli Signed-off-by: Michael Ellerman Signed-off-by: Greg Kroah-Hartman --- arch/powerpc/kernel/kprobes.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) --- a/arch/powerpc/kernel/kprobes.c +++ b/arch/powerpc/kernel/kprobes.c @@ -455,29 +455,33 @@ static int trampoline_probe_handler(stru } kretprobe_assert(ri, orig_ret_address, trampoline_address); - regs->nip = orig_ret_address; + /* - * Make LR point to the orig_ret_address. - * When the 'nop' inside the kretprobe_trampoline - * is optimized, we can do a 'blr' after executing the - * detour buffer code. + * We get here through one of two paths: + * 1. by taking a trap -> kprobe_handler() -> here + * 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here + * + * When going back through (1), we need regs->nip to be setup properly + * as it is used to determine the return address from the trap. + * For (2), since nip is not honoured with optprobes, we instead setup + * the link register properly so that the subsequent 'blr' in + * kretprobe_trampoline jumps back to the right instruction. + * + * For nip, we should set the address to the previous instruction since + * we end up emulating it in kprobe_handler(), which increments the nip + * again. */ + regs->nip = orig_ret_address - 4; regs->link = orig_ret_address; - reset_current_kprobe(); kretprobe_hash_unlock(current, &flags); - preempt_enable_no_resched(); hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) { hlist_del(&ri->hlist); kfree(ri); } - /* - * By returning a non-zero value, we are telling - * kprobe_handler() that we don't want the post_handler - * to run (and have re-enabled preemption) - */ - return 1; + + return 0; } NOKPROBE_SYMBOL(trampoline_probe_handler);