Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp1952454ybd; Thu, 27 Jun 2019 04:25:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqz1Xnm2NCS8cNDZ/mmiLfq4FVebb7QQ4aDrZXKqcBweH5rSPydClrDVfYPVF9hs/XIZm1jK X-Received: by 2002:a65:6104:: with SMTP id z4mr3296162pgu.319.1561634719539; Thu, 27 Jun 2019 04:25:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561634719; cv=none; d=google.com; s=arc-20160816; b=DRxHD6fteIMea1zQ3+5k/x8vBR2XNul+q5BHcNm4O7/gTEoybf1FDxgz/4LDUDDM10 SQ3DxXmx7icVgJVGEM7f8f8iaFWOJc5vWNVVm2Xlam9CVjvFLM7A7qm/4t4nQqaMMw0e MQ52Nnh03L1cI5gX6VgQpTK+iAvXrFVAE34QCmnfWdmDyeSPDEROiU8hvMq9W7Q34bhY iNmKVow8sU+Ay2gUk53Hce72lgkoiYmE5+yqkgHk/2MtK8fTuEnLwnFlDgVaj0B/22CR sot5FGyxkNtvytSiYQRIOM6wDacghBj0/5XkhU8f6e05+5DkVfBtWgLvPQEkPpQ9mCQx NoNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:content-transfer-encoding :mime-version:references:in-reply-to:date:subject:cc:to:from; bh=QfaGOnRzORwG1e/rxlE/2OQB7W62CcVoO7fORPEd8oE=; b=cO3BJzNj+VqdQdCRf2MHHp2I5jpVhY6kSuwsZYPvzMaA3Ru2gxxfD5loNRCoEGusDT wWUj14bjin2PdoXTm13fs+Q0wlbjdtem3laUDwlJMC3HDT0MsRAicVSWz2zPf5rOfSy2 ZtoCOV9tOQV+o9B+52xB4qnJcqEVt+/EebJciwl/WVRldczF82/iQuo1pS3oy7WEBHTO dIT+iyolvAQFBjY8tKiCTHZU4lP2XaxSwKsBr7croz8RSFj/VMnRGdvzWc5OMsGoL+qW cDKYvgchDWsMsfY/nuOO9KRDfrQmx6+2zEpOkmbStdNIn+RLgK5H10XskCHiSvuqtCRn zsqg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a17si2412088pfk.246.2019.06.27.04.25.03; Thu, 27 Jun 2019 04:25:19 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726839AbfF0LYe (ORCPT + 99 others); Thu, 27 Jun 2019 07:24:34 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:18132 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726816AbfF0LYa (ORCPT ); Thu, 27 Jun 2019 07:24:30 -0400 Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x5RBMnXJ085959 for ; Thu, 27 Jun 2019 07:24:29 -0400 Received: from e06smtp05.uk.ibm.com (e06smtp05.uk.ibm.com [195.75.94.101]) by mx0a-001b2d01.pphosted.com with ESMTP id 2tcunubfqn-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 27 Jun 2019 07:24:29 -0400 Received: from localhost by e06smtp05.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 27 Jun 2019 12:24:27 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp05.uk.ibm.com (192.168.101.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Thu, 27 Jun 2019 12:24:23 +0100 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x5RBOMYd51314740 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 27 Jun 2019 11:24:22 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7CCA6A405B; Thu, 27 Jun 2019 11:24:22 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7ED1CA4062; Thu, 27 Jun 2019 11:24:20 +0000 (GMT) Received: from naverao1-tp.ibmuc.com (unknown [9.85.73.27]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 27 Jun 2019 11:24:20 +0000 (GMT) From: "Naveen N. Rao" To: Michael Ellerman , Steven Rostedt , Masami Hiramatsu , Ingo Molnar , Nicholas Piggin Cc: , Subject: [PATCH v2 7/7] powerpc/kprobes: Allow probing on any ftrace address Date: Thu, 27 Jun 2019 16:53:55 +0530 X-Mailer: git-send-email 2.22.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 x-cbid: 19062711-0020-0000-0000-0000034DDFCC X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19062711-0021-0000-0000-000021A15A29 Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-06-27_06:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=1 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=1 clxscore=1015 lowpriorityscore=0 mlxscore=1 impostorscore=0 mlxlogscore=223 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1906270133 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With KPROBES_ON_FTRACE, kprobe is allowed to be inserted on instructions that branch to _mcount (referred to as ftrace location). With -mprofile-kernel, we now include the preceding 'mflr r0' as being part of the ftrace location. However, by default, probing on an instruction that is not actually the branch to _mcount() is prohibited, as that is considered to not be at an instruction boundary. This is not the case on powerpc, so allow the same by overriding arch_check_ftrace_location() In addition, we update kprobe_ftrace_handler() to detect this scenarios and to pass the proper nip to the pre and post probe handlers. Signed-off-by: Naveen N. Rao --- arch/powerpc/kernel/kprobes-ftrace.c | 32 +++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/kernel/kprobes-ftrace.c b/arch/powerpc/kernel/kprobes-ftrace.c index 972cb28174b2..23c840748183 100644 --- a/arch/powerpc/kernel/kprobes-ftrace.c +++ b/arch/powerpc/kernel/kprobes-ftrace.c @@ -12,15 +12,35 @@ #include #include +/* + * With -mprofile-kernel, we patch two instructions -- the branch to _mcount + * as well as the preceding 'mflr r0'. Both these instructions are claimed + * by ftrace and we should allow probing on either instruction. + */ +int arch_check_ftrace_location(struct kprobe *p) +{ + if (ftrace_location((unsigned long)p->addr)) + p->flags |= KPROBE_FLAG_FTRACE; + return 0; +} + /* Ftrace callback handler for kprobes */ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip, struct ftrace_ops *ops, struct pt_regs *regs) { struct kprobe *p; + int mflr_kprobe = 0; struct kprobe_ctlblk *kcb; p = get_kprobe((kprobe_opcode_t *)nip); - if (unlikely(!p) || kprobe_disabled(p)) + if (!p) { + p = get_kprobe((kprobe_opcode_t *)(nip - MCOUNT_INSN_SIZE)); + if (unlikely(!p)) + return; + mflr_kprobe = 1; + } + + if (kprobe_disabled(p)) return; kcb = get_kprobe_ctlblk(); @@ -33,6 +53,9 @@ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip, */ regs->nip -= MCOUNT_INSN_SIZE; + if (mflr_kprobe) + regs->nip -= MCOUNT_INSN_SIZE; + __this_cpu_write(current_kprobe, p); kcb->kprobe_status = KPROBE_HIT_ACTIVE; if (!p->pre_handler || !p->pre_handler(p, regs)) { @@ -45,6 +68,8 @@ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip, kcb->kprobe_status = KPROBE_HIT_SSDONE; p->post_handler(p, regs, 0); } + if (mflr_kprobe) + regs->nip += MCOUNT_INSN_SIZE; } /* * If pre_handler returns !0, it changes regs->nip. We have to @@ -57,6 +82,11 @@ NOKPROBE_SYMBOL(kprobe_ftrace_handler); int arch_prepare_kprobe_ftrace(struct kprobe *p) { + if ((unsigned long)p->addr & 0x03) { + pr_err("Attempt to register kprobe at an unaligned address\n"); + return -EILSEQ; + } + p->ainsn.insn = NULL; p->ainsn.boostable = -1; return 0; -- 2.22.0