Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp2253200ybd; Thu, 27 Jun 2019 09:14:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqzM/zdXt9voMqN4yN3G+EATyHLqFxAaZfmkE3FWFojo7QfJGTEskXF8E9O6vYfQd5RqkvKr X-Received: by 2002:a63:292:: with SMTP id 140mr4399796pgc.88.1561652057489; Thu, 27 Jun 2019 09:14:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561652057; cv=none; d=google.com; s=arc-20160816; b=DOj+S3E2wtHV8sCAfzggbjhPV4p5pHQUNbFCJENPU/uEVHiFvdPsFD35/935AYtRhz DOomeA+uhH4zlL/MISORaMqTgydJTCMBN3lx7Oq4wUUMiqSr2qrIcNZLjv3l/PGCwdCQ PaqmaDHiA4WlVAkdMwj+aPQtVsY8AgDGprKhX7NBBCEdWk6gzR+LB8Mej4UxaYF0psuL zF6Zh+yf85l6I9v2WS6j+DDhmdgva0glRgUpkBsPZG0Sx6lBZnGvJcV70kwobrvy6x+N cYBqGxsqfMZo7tPBHznI9QqzrdAPBPhFbIaTbtsoOwvubVRPqWwK+AhKKH2bNDefhuS8 b58w== 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=2Du2gHBcEoqwr04yIFXua3XkbCuugEzM1qGf+CyzPRk=; b=zTd/f2ZN6RPMbgAL/3yZBLeRA3jiO5koHp5XDBRf9OlFOb9IIf8nfWLn+VntTs+9V6 xf6QBwjgf9g/7BPI9LBkwo2YTIqmKNAJbTozhjDXl6PMTkiUNuhdnlPJQUg5UApDL6Ft Rz509V9unFJbSHzQ7x9NbGSd5ecgiBvfwr3Bx2uKOT1pWGrxoWUlaKtDD7JNA3LR4wso sGsKi9q3B3aGkQIxWLiwQRpnwv4ldGH7kNL2xbcK79aXB+S+nthP4gu5YL/hCLU08Tfc FAa8gWkLeou8sFsCGsuhUxDp6Ka6lH2DkTIIRFvcMyvZYn2kf+u9uY2JYOrstJbSQAIM 6k/w== 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 u20si2346061pgm.526.2019.06.27.09.14.00; Thu, 27 Jun 2019 09:14:17 -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 S1726508AbfF0QNs (ORCPT + 99 others); Thu, 27 Jun 2019 12:13:48 -0400 Received: from mail.kernel.org ([198.145.29.99]:42826 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726384AbfF0QNr (ORCPT ); Thu, 27 Jun 2019 12:13:47 -0400 Received: from gandalf.local.home (cpe-66-24-58-225.stny.res.rr.com [66.24.58.225]) (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 60B1A20659; Thu, 27 Jun 2019 16:13:46 +0000 (UTC) Date: Thu, 27 Jun 2019 12:13:44 -0400 From: Steven Rostedt To: "Naveen N. Rao" Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Masami Hiramatsu , Ingo Molnar , Michael Ellerman , Nicholas Piggin Subject: Re: [PATCH v2 4/7] powerpc/ftrace: Additionally nop out the preceding mflr with -mprofile-kernel Message-ID: <20190627121344.25b5449a@gandalf.local.home> In-Reply-To: <1561648598.uvetvkj39x.naveen@linux.ibm.com> References: <841386feda429a1f0d4b7442c3ede1ed91466f92.1561634177.git.naveen.n.rao@linux.vnet.ibm.com> <20190627110819.4892780f@gandalf.local.home> <1561648598.uvetvkj39x.naveen@linux.ibm.com> X-Mailer: Claws Mail 3.17.3 (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 Thu, 27 Jun 2019 20:58:20 +0530 "Naveen N. Rao" wrote: > > > But interesting, I don't see a synchronize_rcu_tasks() call > > there. > > We felt we don't need it in this case. We patch the branch to ftrace > with a nop first. Other cpus should see that first. But, now that I > think about it, should we add a memory barrier to ensure the writes get > ordered properly? Do you send an ipi to the other CPUs. I would just to be safe. > >> - if (patch_instruction((unsigned int *)ip, pop)) { > >> + /* > >> + * Our original call site looks like: > >> + * > >> + * bl > >> + * ld r2,XX(r1) > >> + * > >> + * Milton Miller pointed out that we can not simply nop the branch. > >> + * If a task was preempted when calling a trace function, the nops > >> + * will remove the way to restore the TOC in r2 and the r2 TOC will > >> + * get corrupted. > >> + * > >> + * Use a b +8 to jump over the load. > >> + */ > >> + if (patch_instruction((unsigned int *)ip, PPC_INST_BRANCH | 8)) { > >> pr_err("Patching NOP failed.\n"); > >> return -EPERM; > >> } > >> +#endif /* CONFIG_MPROFILE_KERNEL */ > >> > >> return 0; > >> } > >> @@ -421,6 +429,26 @@ static int __ftrace_make_nop_kernel(struct dyn_ftrace *rec, unsigned long addr) > >> return -EPERM; > >> } > >> > >> +#ifdef CONFIG_MPROFILE_KERNEL > > > > I would think you need to break this up into two parts as well, with a > > synchronize_rcu_tasks() in between. > > > > Imagine this scenario: > > > > : > > nop <-- interrupt comes here, and preempts the task > > nop > > > > First change. > > > > : > > mflr r0 > > nop > > > > Second change. > > > > : > > mflr r0 > > bl _mcount > > > > Task returns from interrupt > > > > : > > mflr r0 > > bl _mcount <-- executes here > > > > It never did the mflr r0, because the last command that was executed > > was a nop before it was interrupted. And yes, it can be interrupted > > on a nop! > > We are handling this through ftrace_replace_code() and > __ftrace_make_call_prep() below. For FTRACE_UPDATE_MAKE_CALL, we patch > in the mflr, followed by smp_call_function(isync) and > synchronize_rcu_tasks() before we proceed to patch the branch to ftrace. > > I don't see any other scenario where we end up in > __ftrace_make_nop_kernel() without going through ftrace_replace_code(). > For kernel modules, this can happen during module load/init and so, I > patch out both instructions in __ftrace_make_call() above without any > synchronization. > > Am I missing anything? > No, I think I got confused ;-), it's the patch out that I was worried about, but when I was going through the scenario, I somehow turned it into the patching in (which I already audited :-p). I was going to reply with just the top part of this email, but then the confusion started :-/ OK, yes, patching out should be fine, and you already covered the patching in. Sorry for the noise. Just to confirm and totally remove the confusion, the patch does: : mflr r0 <-- preempt here bl _mcount : mflr r0 nop And this is fine regardless. OK, Reviewed-by: Steven Rostedt (VMware) -- Steve