From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 940AE46A5FE; Fri, 11 Sep 2026 08:42:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789116151; cv=none; b=evhxUke81Mk2nltLRWBiDc/t/7i24WA6F/vMg3hfiqO5b34wKxHoBmfP1tExLZpIxHilo7lbUGIN099RtYMXGoGNX5nYvpGSKKhTrJqFc/KYpZ3dfLEgzNu0Ys90/hlvM2J3iCc5SXTYHR9vUWnrgVBMRyDgC/f3CZQyT8BkP8U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789116151; c=relaxed/simple; bh=LobGPdevmpn2bu2mA2Gi1ojWcOe+GqoAjV9E9KxFHHY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=PYaaqONZGlktxmWoaYazWGvMRtbhSAnEckRjhv5QWR7pTAy261xOl0FYx0zMjFoAA8/rUDZ9of/PHq5s/YLdRufZMmp9d8553NnXILd6Og30D1yA9cXn1q8rhtuFERYFMaROi5cvacWKWeFB2Rw1Fj8p5oABR+53KGNe9D59EYI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=oSF6WaHP; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=FoysridP; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="oSF6WaHP"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="FoysridP" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 2861B1FA79; Fri, 11 Sep 2026 08:42:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1789116138; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=qEgI7SvgyOOzaYj/SjcnmBWvZe0hxGWWlUZ9bTgElSY=; b=oSF6WaHPW7OVdfEOZx32oULT7pMtMDhVyaDmumJKSZKhq7hzXJjO26fRH2tmqdXVqnKfrT bU2qrynamVWqYQ7VCJeDuX65UZNg0AYVfZPVofp1F07AaNE4HeC9hgmZJmDo06NZ/o8DG9 b7SWCgIgrpmTfq7xXnjKtYE1STyHSlk= Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1789116134; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=qEgI7SvgyOOzaYj/SjcnmBWvZe0hxGWWlUZ9bTgElSY=; b=FoysridPrct4d9peaV2mGDMhi/buusc59Z5h96JWZ/LtXP0raX6tsqe8nnqQuKGTDl2nqo Bob6CgAb9pLxBS7lpuXQkRl5P+LqboMe4Xmcq4HtMkxhKpNY8rzQutn15ewNhQkP+vn5Vi 8l/P7TU7+qnMYbsLklUPkwvjAOfMBV8= Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 23D9613715; Fri, 11 Sep 2026 08:42:13 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 731eB+W+o2qzFAAAD6G6ig (envelope-from ); Fri, 11 Sep 2026 08:42:13 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, virtualization@lists.linux.dev, llvm@lists.linux.dev Cc: Juergen Gross , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Kiryl Shutsemau , Rick Edgecombe , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Long Li , Paolo Bonzini , Vitaly Kuznetsov , Sean Christopherson , Boris Ostrovsky , xen-devel@lists.xenproject.org, Ajay Kaher , Alexey Makhalov , Broadcom internal kernel review list , Andy Lutomirski , Peter Zijlstra , Xin Li , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Josh Poimboeuf Subject: [PATCH v5 00/17] x86/msr: Inline rdmsr/wrmsr instructions Date: Fri, 11 Sep 2026 10:41:54 +0200 Message-ID: <20260911084211.3149957-1-jgross@suse.com> X-Mailer: git-send-email 2.55.0 Precedence: bulk X-Mailing-List: linux-hyperv@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Score: -2.80 X-Spam-Level: X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.995]; MIME_GOOD(-0.10)[text/plain]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_TRACE(0.00)[0:+]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_TWELVE(0.00)[36]; TO_DN_SOME(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.com:mid]; ARC_NA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; RCVD_VIA_SMTP_AUTH(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; R_RATELIMIT(0.00)[to_ip_from(RLfdszjqhz8kzzb9uwpzdm8png)]; RCVD_TLS_ALL(0.00)[] X-Spam-Flag: NO When building a kernel with CONFIG_PARAVIRT_XXL the paravirt infrastructure will always use functions for reading or writing MSRs, even when running on bare metal. Switch to inline RDMSR/WRMSR instructions in this case, reducing the paravirt overhead. The first patch is a prerequisite fix for alternative patching. Its is needed due to the initial indirect call needs to be padded with NOPs in some cases with the following patches. In order to make this less intrusive, some further reorganization of the MSR access helpers is done in the patches 2-5. The next 5 patches are converting the non-paravirt case to use direct inlining of the MSR access instructions, including the WRMSRNS instruction and the immediate variants of RDMSR and WRMSR if possible. Patches 11-13 are some further preparations for making the real switch to directly patch in the native MSR instructions easier. Patch 14 is switching the paravirt MSR function interface from normal call ABI to one more similar to the native MSR instructions. Patch 15 is a little cleanup patch. Patch 16 is the final step for patching in the native MSR instructions when not running as a Xen PV guest. Patch 17 converts the rest of the MSR helpers to __always_inline. This series has been tested to work with Xen PV and on bare metal. Based on [1] and [2]. Changes since V4: - Rebase - dropped patch 3 of V4, as already covered by [1] Changes since V3: - Rebase - wrmsrns() related changes (patches 9+10) Changes since V2: - switch back to the paravirt approach Changes since V1: - Use Xin Li's approach for inlining - Several new patches [1]: https://lore.kernel.org/lkml/20260911074530.3140830-1-jgross@suse.com/T/#t [2]: https://lore.kernel.org/lkml/20260911075216.3142309-1-jgross@suse.com/T/#t Juergen Gross (17): x86/alternative: Support alt_replace_call() with instructions after call coco/tdx: Rename MSR access helpers x86/msr: Minimize usage of native_*() msr access functions x86/msr: Move MSR trace calls one function level up x86/hyperv: Switch from __rdmsr() to native_rdmsrq() x86/opcode: Add immediate form MSR instructions x86/extable: Add support for immediate form MSR instructions x86/msr: Make wrmsrns() a first class citizen x86/msr: Introduce sync_cpu_after_wrmsrns() x86/msr: Use the alternatives mechanism for RDMSR x86/alternatives: Add ALTERNATIVE_4() x86/paravirt: Split off MSR related hooks into new header x86/paravirt: Prepare support of MSR instruction interfaces x86/paravirt: Switch MSR access pv_ops functions to instruction interfaces x86/msr: Reduce number of low level MSR access helpers x86/paravirt: Use alternatives for MSR access with paravirt x86/msr: Make all MSR access functions __always_inline arch/x86/coco/tdx/tdx.c | 8 +- arch/x86/hyperv/hv_crash.c | 6 +- arch/x86/hyperv/ivm.c | 2 +- arch/x86/include/asm/alternative.h | 6 + arch/x86/include/asm/fred.h | 2 +- arch/x86/include/asm/msr.h | 340 +++++++++++++++++----- arch/x86/include/asm/paravirt-msr.h | 180 ++++++++++++ arch/x86/include/asm/paravirt.h | 45 --- arch/x86/include/asm/paravirt_types.h | 57 ++-- arch/x86/include/asm/qspinlock_paravirt.h | 4 +- arch/x86/kernel/alternative.c | 5 +- arch/x86/kernel/cpu/mshyperv.c | 4 +- arch/x86/kernel/kvmclock.c | 2 +- arch/x86/kernel/paravirt.c | 42 ++- arch/x86/kvm/svm/svm.c | 16 +- arch/x86/lib/x86-opcode-map.txt | 5 +- arch/x86/mm/extable.c | 41 ++- arch/x86/xen/enlighten_pv.c | 52 +++- arch/x86/xen/pmu.c | 4 +- tools/arch/x86/lib/x86-opcode-map.txt | 5 +- tools/objtool/check.c | 1 + 21 files changed, 632 insertions(+), 195 deletions(-) create mode 100644 arch/x86/include/asm/paravirt-msr.h -- 2.55.0