Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2612750ybi; Mon, 17 Jun 2019 07:43:21 -0700 (PDT) X-Google-Smtp-Source: APXvYqyFP2iHOjF3j8kSKhAXrgGLY6hsYKPQ/6llmXEHtuNW5g92guGWryJQ/RkEnWBx+uDtYVOF X-Received: by 2002:a17:902:ba8e:: with SMTP id k14mr44165040pls.256.1560782601632; Mon, 17 Jun 2019 07:43:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560782601; cv=none; d=google.com; s=arc-20160816; b=GgQxR93lGK3XxcsG3WZcs2v6FMAg5k0PONdSVDGSz/Z3GUJ9hAR8XXvamv84G8lqgh Jiyq642ibuD5aYFPveE4mN3zLcpSPtMTVEMPHSqCCFJujF39pHh8PRooiktyiRf78WLG 0IbG1UHwJFTV55nBBWD1J9PDgjgOyKVDzvTiLK6Zgpm506PNpHVyYjgpldv0bMi0vYY/ wYvafFo3lsGrXpcVd2sG49dcJT1a1OrdvmSIxMQowPvCMupoWaC3ovSf2bWB2auqcWOg SZhBkkhk+AiMEmmjJCTxBaC3i4aui1j18cl1tIxJVFRhIu5uXY6P/Mn4vCPeisuSnYGC M+sg== 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-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=hiAU/ZObZlu6bcVKe/I5QdmTIpALzdfMSuN4Et+MDEI=; b=YG1eLoM/ka9HE5s1fTbQFeZ1RxtxmhaVl+6Ts1aOyImkGhTboEPOSvVKmloWdSBYTu TAnWC2yrs/9Qgouiq1JiJ+a+umitPhPvaITtPQNMQVCUgyqQqIgecHEkwSCAFmuQkppX 36pmzGnvt9Pq4t2cRqslFPr3I26zUMX6deNTGxHNOMZl/5hFPTO208sXqt6IO/eZrBje GlecNOq7jm/KPKjfZsgnaDs2K/DBF4O05lQyEnsauobTWUVNNw0a7Fc/IWCnZ6qRPgr9 J0TGfSlxhY5ovVbOIJU6KJZsJwYGYVdOdmW+zmx5qFu4Bs27KEQJOT8LV9C6zeMi/paK iL7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=mHeXv7Gu; 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 t17si718657pfe.269.2019.06.17.07.43.06; Mon, 17 Jun 2019 07:43:21 -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; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=mHeXv7Gu; 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 S1728288AbfFQOm7 (ORCPT + 99 others); Mon, 17 Jun 2019 10:42:59 -0400 Received: from merlin.infradead.org ([205.233.59.134]:34250 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726614AbfFQOm6 (ORCPT ); Mon, 17 Jun 2019 10:42:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Transfer-Encoding: Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Sender:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=hiAU/ZObZlu6bcVKe/I5QdmTIpALzdfMSuN4Et+MDEI=; b=mHeXv7Gu6VGHGFwa7W7g0CKyhd KtNJuKxVNRWaE+ypaxnhDhURuOAaNc4t78rwhlN4o5qwucuBwV7MeW8LMgli5uYiKH7aOyyRgSzz2 QRGHmsKUrImGVBsL8YFYQmvUdoNLhzbUd9lvvx5JDeMo8whN+IAGGrsNKwvZG/Tm+zX49pJZYHsQm mc0VSQitDyQMeK4JzGkEzn6xEJ+F9nKYEFfqDyknOE1c1PI0lUWNL6ivehelQzaqMomMttbCoLtx3 1Z9sbNNNY2h7/MJGN2+YBC7j6m43TGLeiIFQu9HBsa8ueJo4sywJNwKk4kJQ3X6BLz27TWjwx+PU1 1Ifmoy3Q==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92 #3 (Red Hat Linux)) id 1hcspq-0008Hf-RF; Mon, 17 Jun 2019 14:42:15 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 9A98A201D1C98; Mon, 17 Jun 2019 16:42:13 +0200 (CEST) Date: Mon, 17 Jun 2019 16:42:13 +0200 From: Peter Zijlstra To: Nadav Amit Cc: Steven Rostedt , Andy Lutomirski , Masami Hiramatsu , the arch/x86 maintainers , LKML , Ard Biesheuvel , Andy Lutomirski , Ingo Molnar , Thomas Gleixner , Linus Torvalds , Jason Baron , Jiri Kosina , David Laight , Borislav Petkov , Julia Cartwright , Jessica Yu , "H. Peter Anvin" , Rasmus Villemoes , Edward Cree , Daniel Bristot de Oliveira Subject: Re: [PATCH 08/15] x86/alternatives: Teach text_poke_bp() to emulate instructions Message-ID: <20190617144213.GE3436@hirez.programming.kicks-ass.net> References: <20190605130753.327195108@infradead.org> <20190605131945.005681046@infradead.org> <20190608004708.7646b287151cf613838ce05f@kernel.org> <20190607173427.GK3436@hirez.programming.kicks-ass.net> <3DA961AB-950B-4886-9656-C0D268D521F1@amacapital.net> <20190611080307.GN3436@hirez.programming.kicks-ass.net> <20190611112254.576226fe@gandalf.local.home> <20190611155537.GB3436@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 12, 2019 at 07:44:12PM +0000, Nadav Amit wrote: > I have run into similar problems before. > > I had two problematic scenarios. In the first case, I had a “call” in the > middle of the patched code-block, but this call was always followed by a > “jump” to the end of the potentially patched code-block, so I did not have > the problem. > > In the second case, I had an indirect call (which is shorter than a direct Longer, 6 bytes vs 5 if I'm not mistaken. > call) being patched into a direct call. In this case, I preceded the > indirect call with NOPs so indeed the indirect call was at the end of the > patched block. > > In certain cases, if a shorter instruction should be potentially patched > into a longer one, the shorter one can be preceded by some prefixes. If > there are multiple REX prefixes, for instance, the CPU only uses the last > one, IIRC. This can allow to avoid synchronize_sched() when patching a > single instruction into another instruction with a different length. > > Not sure how helpful this information is, but sharing - just in case. I think we can patch multiple instructions provided: - all but one instruction are a NOP, - there are no branch targets inside the range. By poking INT3 at every instruction in the range and then doing the machine wide IPI+SYNC, we'll trap every CPU that is in-side the range. Because all but one instruction are a NOP, we can emulate only the one instruction (assuming the real instruction is always last), otherwise NOP when we're behind the real instruction. Then we can write new instructions, leaving the initial INT3 until last. Something like this might be useful if we want to support immediate instructions (like patch_data_* in paravirt_patch.c) for static_call().