Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7379754imu; Tue, 22 Jan 2019 05:12:20 -0800 (PST) X-Google-Smtp-Source: ALg8bN6q2GA9nQMc2gT6fac00S9QA/hqGie4ROkaLJt2TZRYOHobBGuOGVx+ih35QEfrJlLaYdLx X-Received: by 2002:a65:6491:: with SMTP id e17mr4758982pgv.418.1548162739973; Tue, 22 Jan 2019 05:12:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548162739; cv=none; d=google.com; s=arc-20160816; b=BUpFiin6g5RTXOx93qBv92kPfmDQGkO+FZenYKgauolV0+00UQlh44RgIp1J8R4auk MWa5k3YC6/BbXLAWrYblnnPyeaMIIPji7AIiBk97UwBoH22S2+fITktVi6UfDs61AfMk HkwAdowBg6z1cWkKIPvp5qFNUx5OX7T8CvzT2e/1S5lBIIUIytVLe3kU/yhTLn7xvvvh 0atc7qKo2QTkX1T2p+Va6hk7eIHzlgzcFw3flvvyfdtxSF6w0Io51V9dQe6pv/VsFXcD 18EdtMbrDf9mz+jMBHUEL11fHsP09RVknZf5UZGQdtKWMBB0trgooLBeYG+tmPiFr9ox 1ubg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=Nj6etZgGYdN7AS8HPynLKZZDobRpcW9BVS+O8xVLzPQ=; b=YBmqYG0PjhbS/AGtO8PXOx2MuQJRiV+Bt7+vE2WJant96Cr9yx6O+bdJk/bSqO9xN3 igqlvdejXQyfBCIn4VfyxjgN8xAZYe25f4FG/HFHESm/Iqw/IQGzPltB458rx6WHJIUQ DjliJb9Lske1c15NrJgLJd5DB+mxb3qlL+FdcJOkpjrdh3dLhMWydekc1d81+HnZfSX6 YbLwrKVDKsz6N697IKoyodfuzfBzbyepRvHfA2YhUcEU5Q9fS2odEr3HBVIYuHTxGVeq Ee/OlTFCCwkPVqWij+GilLgPuh6pfhT8rOCXTfQqd9+phwhzi1eNXmSmelmBbbVSBHYZ pnPA== 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 y7si14234659pga.296.2019.01.22.05.12.04; Tue, 22 Jan 2019 05:12:19 -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 S1728504AbfAVNKB (ORCPT + 99 others); Tue, 22 Jan 2019 08:10:01 -0500 Received: from verein.lst.de ([213.95.11.211]:36361 "EHLO newverein.lst.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728390AbfAVNKA (ORCPT ); Tue, 22 Jan 2019 08:10:00 -0500 Received: by newverein.lst.de (Postfix, from userid 2005) id 051806FA7F; Tue, 22 Jan 2019 14:09:59 +0100 (CET) Date: Tue, 22 Jan 2019 14:09:58 +0100 From: Torsten Duwe To: "Singh, Balbir" Cc: Mark Rutland , Will Deacon , Catalin Marinas , Julien Thierry , Steven Rostedt , Josh Poimboeuf , Ingo Molnar , Ard Biesheuvel , Arnd Bergmann , AKASHI Takahiro , Amit Daniel Kachhap , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, live-patching@vger.kernel.org Subject: Re: [PATCH v7 2/3] arm64: implement ftrace with regs Message-ID: <20190122130958.GA16778@lst.de> References: <20190118163736.6A99268CEB@newverein.lst.de> <20190118163908.E338E68D93@newverein.lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.17 (2007-11-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Balbir! On Tue, Jan 22, 2019 at 02:39:32PM +1300, Singh, Balbir wrote: > > On 1/19/19 5:39 AM, Torsten Duwe wrote: > > + */ > > +ftrace_common_return: > > + /* restore function args */ > > + ldp x0, x1, [sp] > > + ldp x2, x3, [sp, #S_X2] > > + ldp x4, x5, [sp, #S_X4] > > + ldp x6, x7, [sp, #S_X6] > > + ldr x8, [sp, #S_X8] > > + > > + /* restore fp and x28 */ > > + ldp x28, x29, [sp, #S_X28] > > + > > + ldr lr, [sp, #S_LR] > > + ldr x9, [sp, #S_PC] > > Is it fair to assume that we never modify registers beyond LR and PC as a result of ftrace/livepatching? I presume it is, but just checking. These are either callee-save or scratch. Whatever is called, ftrace framework functions or replacement functions, must preserve the callee-saved regs; and the caller, who made a function call (sic!-) saves caller-saved and marks the rest dead on return. So it's the arguments that matter after all. As you can see, disabling IPA-RA is cruicial here. Or are you talking about deliberate argument manipulation? > > + unsigned long pc = rec->ip + REC_IP_BRANCH_OFFSET; > > + u32 old, new; > > + > > + old = aarch64_insn_gen_branch_imm(pc, old_addr, true); > > + new = aarch64_insn_gen_branch_imm(pc, addr, true); > > + > > Is this a branch or a call? Does addr always fit in the immediate limits? As Julien has now pointed out, the correct enum value AARCH64_INSN_BRANCH_LINK should clarify this. It will surely fit for the kernel proper, and the modules are handled with the trampolines. > > + return ftrace_modify_code(pc, old, new, true); > > Can you talk to the semantics of whether this operation is atomic w.r.t system? Will old and new return consistent values? Given the nature of ftrace, I presume it's well isolated. aarch64_insn_patch_text_nosync() does a __flush_icache_range() on success. Mark wrote that this is already sufficient IIRC. (I had memory barriers there, when I was still trying to modify 2 insns every time). > > > + if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && > > + addr == MCOUNT_ADDR) { > > + old = aarch64_insn_gen_nop(); > > + new = MOV_X9_X30; > > + pc -= REC_IP_BRANCH_OFFSET; > > + return ftrace_modify_code(pc, old, new, validate); > > I presume all the icache flush and barrier handling is in ftrace_modify_code()? Yes, see above. > > + } > > + > > if (offset < -SZ_128M || offset >= SZ_128M) { > > #ifdef CONFIG_ARM64_MODULE_PLTS > > u32 replaced; > > --- a/arch/arm64/include/asm/module.h > > +++ b/arch/arm64/include/asm/module.h > > @@ -32,7 +32,8 @@ struct mod_arch_specific { > > struct mod_plt_sec init; > > > > /* for CONFIG_DYNAMIC_FTRACE */ > > - struct plt_entry *ftrace_trampoline; > > + struct plt_entry *ftrace_trampolines; > > +#define MOD_ARCH_NR_FTRACE_TRAMPOLINES 2 > > I don't see the generation of ftrace_trampolines[1] > That was further up, install_ftrace_trampoline() in kernel/ftrace.c. + if (*addr == FTRACE_ADDR) + mod_trampoline = &mod->arch.ftrace_trampolines[0]; + else if (*addr == FTRACE_REGS_ADDR) + mod_trampoline = &mod->arch.ftrace_trampolines[1]; [...] + trampoline = get_plt_entry(*addr, mod_trampoline); + + if (!plt_entries_equal(mod_trampoline, &trampoline)) { [...] get_plt_entry() generates a small bunch of instructions that easily fit into the argument registers. Compare commit bdb85cd1d20669dfae8 for the new trampoline insns. Hope I've covered all your concerns, Torsten