Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3435818imu; Mon, 7 Jan 2019 03:23:06 -0800 (PST) X-Google-Smtp-Source: ALg8bN4GH1V9BQWTf1vEtAKZ27XePrtFZ2VQXCI8FsnHp60Hh26os4oQe7B/vP0P98VGF2lirRGN X-Received: by 2002:a63:314d:: with SMTP id x74mr10505552pgx.10.1546860186245; Mon, 07 Jan 2019 03:23:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546860186; cv=none; d=google.com; s=arc-20160816; b=IiGFDRdvgNyOioPEiYFqiKBEqzdbnKhYajP05jQo4/DkyWAN2mrS8gEUk3kSR/5gGT 9/YjUds8qNmzgGwUsgX2lUZUwIBaQ4rACZOossx8s0Dlv0jEairq0PnVCjGFSXP52lb3 egxmKsvU/ANKH/Ar1OsNsC6PXUq1pCEMihklAdgonGkh/Y4gBRq2ggDc/JfqRUbK2KxB UF/tgwxMKmnU/G9YUlwS5qQKLSj4BpqbpESJ+aClQ0IpLrtW4u4gyxes1RljsWlR8PBk aiZ2HBWHEbECSoEpyVdYCZRexoNjIOo6/WKeNvJWyZbDjKon/B9ycOFQWAV68B1XG6dm 9wFg== 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=bQe3LoMZ1hdw516dOz1GUvkkKtQKI79UmTC+jTWOw4w=; b=IG/26wnivT8p9sOzndjiI2I0x+1XKuKPD4/o4b9vL3NnDgOyJo1WG999fqEpViie3Y +GtgjKNkaweRcmKQ2MxXFuO20LohL8qQ7GMFWuEPLOoGfJm1II045Bb7tu1wU3nrQcFk L3EmMen7j6haTUFpRhCHz4wNrmFmO8AiMaD2gzX5ZmaBDocOj+vr2RaY1qLr7AmoLJUF 5Qjpa9+WDkegPO/QMfc0+97dKn4/pUcDAKKzWaHwOB1CXuYb/yy3ZoiDdz2YzDWgVbZN 3HQetvECjAvMIu7PwSObWUgeZLT8MMBgQRs+KAnZw6Co82LLFqIuzr8eRk4nE3ZXZ9ad htCQ== 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 y1si48428129plt.356.2019.01.07.03.22.51; Mon, 07 Jan 2019 03:23:06 -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 S1727072AbfAGLUC (ORCPT + 99 others); Mon, 7 Jan 2019 06:20:02 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:56968 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726798AbfAGLUC (ORCPT ); Mon, 7 Jan 2019 06:20:02 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9A4301596; Mon, 7 Jan 2019 03:20:01 -0800 (PST) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3D4193F5A0; Mon, 7 Jan 2019 03:19:59 -0800 (PST) Date: Mon, 7 Jan 2019 11:19:54 +0000 From: Mark Rutland To: Torsten Duwe Cc: Steven Rostedt , Will Deacon , Catalin Marinas , Julien Thierry , 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 v6] arm64: implement ftrace with regs Message-ID: <20190107111954.GA11732@lakrids.cambridge.arm.com> References: <20190104141053.360F768D93@newverein.lst.de> <20190104175017.GA7157@lakrids.cambridge.arm.com> <20190104130648.02657f3f@gandalf.local.home> <20190104224145.GA28236@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190104224145.GA28236@lst.de> User-Agent: Mutt/1.11.1+11 (2f07cb52) (2018-12-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 04, 2019 at 11:41:45PM +0100, Torsten Duwe wrote: > On Fri, Jan 04, 2019 at 01:06:48PM -0500, Steven Rostedt wrote: > > On Fri, 4 Jan 2019 17:50:18 +0000 > > Mark Rutland wrote: > > > > > At Linux Plumbers, I had a conversation with Steve Rostedt, and we came > > > to the conclusion that (withut heavyweight synchronization) patching two > > > NOPs at runtime isn't safe, since a CPU might have executed the first > > > NOP as a NOP before another CPU patches both instructions. So a CPU > > > might execute: > > > > > > NOP > > > BL ftrace_regs_caller > > > > > > ... rather than the expected: > > > > > > MOV X9, X30 > > > BL ftrace_regs_caller > > > > > > ... and therefore X9 contains some UNKNOWN value, rather than the > > > original LR value. > > I'm perfectly aware of that; an earlier version had barriers, attempting > to avoid just that, which Mark(?) wrote weren't neccessary. The problem was that even with barriers, the only guarantee you get is that instructions are made visible in order, not what the other CPU has executed. For example: I.e. CPU#1 CPU#2 NOP#1 Patches NOP#1 -> INSN#1 Cache maintenance Barrier // INSN#1 now visible to CPU#2, // but NOP#1 was already // executed as a NOP. Patches NOP#2 -> INSN#2 Cache maintenance Barrier INSN#2 > But is this a realistic scenario? All function entries are aligned 8 bytes. > Are there arm64 implementations out there that fetch only 4 bytes and > give a chance to mess with the 2nd 4 bytes? You at arm.com should know, and > I won't be surprised if the answer is a weird "yes". Or maybe it's just > another erratum lurking somewhere... The alignment of the instructions provides no guarantee here. Regardless of what contemporary implementations *may* do, the architecture provides absolutely no guarantee. For example, even if CPU#2 fetched both NOPs together, the cache maintenance and barrier may cause it to throw away any speculative work after executing NOP#1. Upon re-fetching, it could see both new INSNs, but as it's already executed the first as a NOP, it will not re-execute it as INSN#1. Also consider pre-emption by a hypervisor or firmware may occur mid-sequence. > My point is: those 2 insn will _never_ be split by any alignment > boundary > 8; does that mean anything, have you considered this? This has no impact whatsoever. > > > > I wonder if we could solve that by patching the kernel at build-time, to > > > add the MOV X9, X30 in place of the first NOP. If we were to do that, we > > > could also update the addresses to pooint at the second NOP, simplifying > > > the changes to the runtime code. > > > > You can also patch it at boot up when there's only one CPU running, and > > interrupts are disabled. > > May I remind about possible performance hits? Sure; please get some numbers either way. > Even the NOPs had a tiny impact > on certain in-order implementations. I'd rather switch between the mov and > a "b +2". Be careful; the architecture only permits live patching between certain instructions. Please see ARM DDI 0487D.a, section B2.2.5, "Concurrent modification and execution of instructions". Per that, it's not safe to live-patch MOV->B or B->MOV. It's *also* not safe to live-patch NOP->MOV, or vice-versa. So I strongly suspect we must unconditionally patch the MOV in early. Thanks, Mark.