From nobody Wed Dec 31 13:03:34 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18F8AC4167B for ; Thu, 2 Nov 2023 11:29:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376276AbjKBL3K (ORCPT ); Thu, 2 Nov 2023 07:29:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229754AbjKBL3H (ORCPT ); Thu, 2 Nov 2023 07:29:07 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93093131 for ; Thu, 2 Nov 2023 04:29:04 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-307d58b3efbso464807f8f.0 for ; Thu, 02 Nov 2023 04:29:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698924543; x=1699529343; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nK9f/0DyWoovHMW6tPJeem6iX8A0uEcMjFuwkUDDCuM=; b=Ab/KOJpQwGaxoUiUb8senjwYjA7Qkzd6BAtIQkeTbwMIGyayD9csK9X+QmFG1/H+D4 75v1KAFY/7WVX9isuTarnUdFIg0NyjJ16GcZHiA3dNi/jIZGPPeyR+A/mxEhvCUQDZm5 H3Kcy4091V9U3USTHPvVJE/zVC8ZqcIGclUHuujqwYT8WVUE3aOHA7mUknQGZYRzRZss epsw6wOx1Yzk9y2zZZr+f3UrTHS8zVl2X3bJSfwo1UOuaj493D9UAeMYl/vWPC98HJss yMlegsJbDFzc7Zf6O8rPvqQxS9d64ZOZQgvIuDsAo3wY1SfUTs3si0XAUprciEYT/Kxg v/nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698924543; x=1699529343; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nK9f/0DyWoovHMW6tPJeem6iX8A0uEcMjFuwkUDDCuM=; b=JGMn1A3XZ9zxIh8qCX8eQxZZb6bNihbz7yu8aHQkkN4k/TTeXEmDcgPB2UAkFbo0JQ ZO2IEmCd1mUGpWwel00hkom2VWtP0RkGENUUrDA8edLOANmS7L322xDFlLzZdVIVyW4R L072+vXRJN4V5+TNDniSgLlNBf6XI++STGthLgDpVuM+eCE90EEDDKxWPvcEWesZ+wxh wsquh8eCDQBv3BFM4uiDKt7PHW1ejZqIC7n4Ua/q2tQzehr5F1NGPN/Cs5xjqbn54ZoV yXzFgFfGhBtoSv7uPrkI9vlUE182eJaxCXyZFaz3hEpSonop9F6mzPbRoC4TB+POD7yR /pPQ== X-Gm-Message-State: AOJu0YxWvm9bK5xME8ydV90O4BIiWAdokisHSMI507fDjk1GvI/XK4Xl e24GbwiJhfhE+1YirwzuQ9A= X-Google-Smtp-Source: AGHT+IEy8/D8BKIcTWAHmXk2m6V5ixzICj5Yq8nTO4nn5gpqYSz5QdxR3dN/9kGA2hPh0HnHh8LwYg== X-Received: by 2002:adf:f646:0:b0:31f:db1b:7296 with SMTP id x6-20020adff646000000b0031fdb1b7296mr11670913wrp.21.1698924542495; Thu, 02 Nov 2023 04:29:02 -0700 (PDT) Received: from localhost.localdomain ([46.248.82.114]) by smtp.gmail.com with ESMTPSA id d10-20020a5d4f8a000000b003232f167df5sm2168227wru.108.2023.11.02.04.29.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 04:29:02 -0700 (PDT) From: Uros Bizjak To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Uros Bizjak , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Peter Zijlstra Subject: [PATCH 1/3] x86/callthunks: Move call thunk template to .S file Date: Thu, 2 Nov 2023 12:25:46 +0100 Message-ID: <20231102112850.3448745-2-ubizjak@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231102112850.3448745-1-ubizjak@gmail.com> References: <20231102112850.3448745-1-ubizjak@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently INCREMENT_CALL_DEPTH explicitly defines %gs: segment register prefix for its percpu variable. This is not compatible with !CONFIG_SMP, which requires non-prefixed percpu variables. Move call thunk template to its own callthunks-tmpl.S assembly file where PER_CPU_VAR macro from percpu.h can be used to conditionally use %gs: segment register prefix, depending on CONFIG_SMP. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: Peter Zijlstra Signed-off-by: Uros Bizjak --- arch/x86/kernel/Makefile | 2 +- arch/x86/kernel/callthunks-tmpl.S | 11 +++++++++++ arch/x86/kernel/callthunks.c | 10 ---------- 3 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 arch/x86/kernel/callthunks-tmpl.S diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 3269a0e23d3a..6b6b68ef4c3b 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -143,7 +143,7 @@ obj-$(CONFIG_AMD_MEM_ENCRYPT) +=3D sev.o =20 obj-$(CONFIG_CFI_CLANG) +=3D cfi.o =20 -obj-$(CONFIG_CALL_THUNKS) +=3D callthunks.o +obj-$(CONFIG_CALL_THUNKS) +=3D callthunks.o callthunks-tmpl.o =20 obj-$(CONFIG_X86_CET) +=3D cet.o =20 diff --git a/arch/x86/kernel/callthunks-tmpl.S b/arch/x86/kernel/callthunks= -tmpl.S new file mode 100644 index 000000000000..e82c473bd1b1 --- /dev/null +++ b/arch/x86/kernel/callthunks-tmpl.S @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include + + .section .rodata + .global skl_call_thunk_template + .global skl_call_thunk_tail + +skl_call_thunk_template: + INCREMENT_CALL_DEPTH +skl_call_thunk_tail: diff --git a/arch/x86/kernel/callthunks.c b/arch/x86/kernel/callthunks.c index e9ad518a5003..d0922cf94c90 100644 --- a/arch/x86/kernel/callthunks.c +++ b/arch/x86/kernel/callthunks.c @@ -62,16 +62,6 @@ static const struct core_text builtin_coretext =3D { .name =3D "builtin", }; =20 -asm ( - ".pushsection .rodata \n" - ".global skl_call_thunk_template \n" - "skl_call_thunk_template: \n" - __stringify(INCREMENT_CALL_DEPTH)" \n" - ".global skl_call_thunk_tail \n" - "skl_call_thunk_tail: \n" - ".popsection \n" -); - extern u8 skl_call_thunk_template[]; extern u8 skl_call_thunk_tail[]; =20 --=20 2.41.0 From nobody Wed Dec 31 13:03:34 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C606C4167D for ; Thu, 2 Nov 2023 11:29:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347408AbjKBL3N (ORCPT ); Thu, 2 Nov 2023 07:29:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347360AbjKBL3I (ORCPT ); Thu, 2 Nov 2023 07:29:08 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19BD3132 for ; Thu, 2 Nov 2023 04:29:05 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-40838915cecso6059665e9.2 for ; Thu, 02 Nov 2023 04:29:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698924543; x=1699529343; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ENp6FOnR5GB+Ukedmf+eZL7XOs80yvuA3UZNbpkC5yw=; b=G1tZ92eN5IySeufq2ZBdtSRSw7mWs+skrMYclDOcRVZe4WsIwizZ5r65TSheopW/sp HXvIL9OYaFr30xNuv2Os3ESCHp2FTJx6UpIIuPrlvdotPMOYEh8FOOELKRwwIyvBA0o7 coLu3P+wbbOBhTdmtV1H+6V283ooP3/3Uxe0eVYAkJUimEuOIi3e30iEdCOEhb6DS6Wv j/1ha5z10KRtlLh8tbUEM6yrvw2VW43oo+75DdNeCDkLSL78YHnWFIUEbnFUhxLnyThc LktOqAB6avs5GESbdtpsHZxojtIwzldTkXDa6tLiNSL6iukB1K2dFFs967EdnSXYkOQ1 uiAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698924543; x=1699529343; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ENp6FOnR5GB+Ukedmf+eZL7XOs80yvuA3UZNbpkC5yw=; b=Rz2N3PSEXJX6emBREhE67BwCZv2hoaemZ8v/OXaSOfwRQ/COtSINQ0CPmCP9WUA3GC 1pNhuNHAcw3tWmx6HmwvNJ/Hcw85ZKZJBiCrGVfKT8cZvLHbCFdARyZiYaVwZnpfSffZ VnWOCcnu7nVi4KGBmdw3HoK4CO4GmlV4yWPX1L0kG0TeqKIVgibOKp+3ba4wSyCvLpqm bsvvADKhJu3IhSpNEanOE5XH/CCj7Kmh1DH3CtP9/R+8E1Jv3eRVHibZxXKE2Fcy8L17 jKrqF2Nkr1eXFgUZT/uRM7okCCdDAEdVyYX4TiETK3BTtSZbnzfKzRv0cClgkE+17+5K dtKQ== X-Gm-Message-State: AOJu0YyUaAkwtD6CG3xgA3kWcRdrFy4KNGrjklgdf3vBys9qtOlO4nrA 3FKi32NWK6ScJOo9plYHUcU= X-Google-Smtp-Source: AGHT+IEndYIk0+Gp9DXyJ9utA2kH9YVSYrwleZ0ulRNCDC2KtYX9W4mCslo1kZ3QVmWe+zQP7Uy/Bw== X-Received: by 2002:a05:6000:686:b0:32f:9ac4:6f29 with SMTP id bo6-20020a056000068600b0032f9ac46f29mr6281726wrb.70.1698924543282; Thu, 02 Nov 2023 04:29:03 -0700 (PDT) Received: from localhost.localdomain ([46.248.82.114]) by smtp.gmail.com with ESMTPSA id d10-20020a5d4f8a000000b003232f167df5sm2168227wru.108.2023.11.02.04.29.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 04:29:03 -0700 (PDT) From: Uros Bizjak To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Uros Bizjak , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Peter Zijlstra Subject: [PATCH 2/3] x86/callthunks: Handle %rip-relative relocations in call thunk template Date: Thu, 2 Nov 2023 12:25:47 +0100 Message-ID: <20231102112850.3448745-3-ubizjak@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231102112850.3448745-1-ubizjak@gmail.com> References: <20231102112850.3448745-1-ubizjak@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Contrary to alternatives, relocations are currently not supported in call thunk templates. Implement minimal support for relocations when copying template from its storage location to handle %rip-relative addresses. Support for relocations will be needed when PER_CPU_VAR macro switches to %rip-relative addressing. The patch allows unification of ASM_INCREMENT_CALL_DEPTH, which already uses PER_CPU_VAR macro, with INCREMENT_CALL_DEPTH, used in call thunk template, which is currently limited to use absolute address. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: Peter Zijlstra Signed-off-by: Uros Bizjak --- arch/x86/kernel/callthunks.c | 63 ++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/callthunks.c b/arch/x86/kernel/callthunks.c index d0922cf94c90..bda09d82bff7 100644 --- a/arch/x86/kernel/callthunks.c +++ b/arch/x86/kernel/callthunks.c @@ -24,6 +24,8 @@ =20 static int __initdata_or_module debug_callthunks; =20 +#define MAX_PATCH_LEN (255-1) + #define prdbg(fmt, args...) \ do { \ if (debug_callthunks) \ @@ -166,13 +168,51 @@ static const u8 nops[] =3D { 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, }; =20 +#define apply_reloc_n(n_, p_, d_) \ + do { \ + s32 v =3D *(s##n_ *)(p_); \ + v +=3D (d_); \ + BUG_ON((v >> 31) !=3D (v >> (n_-1))); \ + *(s##n_ *)(p_) =3D (s##n_)v; \ + } while (0) + +static __always_inline +void apply_reloc(int n, void *ptr, uintptr_t diff) +{ + switch (n) { + case 4: apply_reloc_n(32, ptr, diff); break; + default: BUG(); + } +} + +static void apply_relocation(u8 *buf, size_t len, u8 *dest, u8 *src) +{ + for (int next, i =3D 0; i < len; i =3D next) { + struct insn insn; + + if (WARN_ON_ONCE(insn_decode_kernel(&insn, &buf[i]))) + return; + + next =3D i + insn.length; + + if (insn_rip_relative(&insn)) + apply_reloc(insn.displacement.nbytes, + buf + i + insn_offset_displacement(&insn), + src - dest); + } +} + static void *patch_dest(void *dest, bool direct) { unsigned int tsize =3D SKL_TMPL_SIZE; + u8 insn_buff[MAX_PATCH_LEN]; u8 *pad =3D dest - tsize; =20 + memcpy(insn_buff, skl_call_thunk_template, tsize); + apply_relocation(insn_buff, tsize, pad, skl_call_thunk_template); + /* Already patched? */ - if (!bcmp(pad, skl_call_thunk_template, tsize)) + if (!bcmp(pad, insn_buff, tsize)) return pad; =20 /* Ensure there are nops */ @@ -182,9 +222,9 @@ static void *patch_dest(void *dest, bool direct) } =20 if (direct) - memcpy(pad, skl_call_thunk_template, tsize); + memcpy(pad, insn_buff, tsize); else - text_poke_copy_locked(pad, skl_call_thunk_template, tsize, true); + text_poke_copy_locked(pad, insn_buff, tsize, true); return pad; } =20 @@ -281,20 +321,26 @@ void *callthunks_translate_call_dest(void *dest) static bool is_callthunk(void *addr) { unsigned int tmpl_size =3D SKL_TMPL_SIZE; - void *tmpl =3D skl_call_thunk_template; + u8 insn_buff[MAX_PATCH_LEN]; unsigned long dest; + u8 *pad; =20 dest =3D roundup((unsigned long)addr, CONFIG_FUNCTION_ALIGNMENT); if (!thunks_initialized || skip_addr((void *)dest)) return false; =20 - return !bcmp((void *)(dest - tmpl_size), tmpl, tmpl_size); + *pad =3D dest - tmpl_size; + + memcpy(insn_buff, skl_call_thunk_template, tmpl_size); + apply_relocation(insn_buff, tmpl_size, pad, skl_call_thunk_template); + + return !bcmp(pad, insn_buff, tmpl_size); } =20 int x86_call_depth_emit_accounting(u8 **pprog, void *func) { unsigned int tmpl_size =3D SKL_TMPL_SIZE; - void *tmpl =3D skl_call_thunk_template; + u8 insn_buff[MAX_PATCH_LEN]; =20 if (!thunks_initialized) return 0; @@ -303,7 +349,10 @@ int x86_call_depth_emit_accounting(u8 **pprog, void *f= unc) if (func && is_callthunk(func)) return 0; =20 - memcpy(*pprog, tmpl, tmpl_size); + memcpy(insn_buff, skl_call_thunk_template, tmpl_size); + apply_relocation(insn_buff, tmpl_size, *pprog, skl_call_thunk_template); + + memcpy(*pprog, insn_buff, tmpl_size); *pprog +=3D tmpl_size; return tmpl_size; } --=20 2.41.0 From nobody Wed Dec 31 13:03:34 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03C6CC4332F for ; Thu, 2 Nov 2023 11:29:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376300AbjKBL3Q (ORCPT ); Thu, 2 Nov 2023 07:29:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376270AbjKBL3J (ORCPT ); Thu, 2 Nov 2023 07:29:09 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10578111 for ; Thu, 2 Nov 2023 04:29:07 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-507973f3b65so984015e87.3 for ; Thu, 02 Nov 2023 04:29:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698924545; x=1699529345; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tgAqePGVhUCu3U56kTFLSJugpfg6qt/lSZ7uytS2Gf4=; b=BQb08mpbYh4FwfaddGcZDs6LMGQnnXbzI0le3TBrHcv1i3VvxZ6K4o2jAR9Z81J//U OzR5vtBq+WdIuym23V3smYHtt+mrRKJ012WMHImhqZ9pqo3fUp8gOoiPeqbfS8tEdZOe YiJqlTlegHZkL2SYFi/jpdcvnU/cC26Tts7euW0wYEmE50bQ0KG1SXDdmnfttrjopwlu ZGpzCRQ6/e7a62wj19wkm6oHOxE0GhipaQwJ5w/OU9L8oCKxS/wanYGP8m/5XlTHFSb7 hxPzYr++/0wRXmTuHQ1hnV40jgLrq4qqZ0v7S578+ZiY8rRpe2qddCF4EkWybRak/Dyo WZow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698924545; x=1699529345; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tgAqePGVhUCu3U56kTFLSJugpfg6qt/lSZ7uytS2Gf4=; b=kVcUYyi3QCMHygkg8XU72NlXW8AmSc2iHX1cN1+LguvIifXgFNdUtBvZauT1QqLu6H Vv3kyfibAe0EH8IrKrEeyxWp3/CUvdiX79iTSRCKa6c9P+lyvNM5mXeSyKwYUpanwDaw c5a1lq0Te8cTXihda64h9h3lTcIxU9iRPYkkHyqUG4iyJksM4cso3BRRjIDrJLqMv7+z /wAMp1ULU4tH8C4W+cN3TAW70Kyq9He2/vAAB1fdSESq6nIJn2GV8ZVD08uEe7H5vnwN jhDOwfvTh6rblQJKKsK5cKzI63C2lEdc9sHhRyI24YBvuNljS09ka4tK9GTxiqj+VNDt PMxw== X-Gm-Message-State: AOJu0YxaiEzqDHN4BMupSiBiZamZwZWeIYqEN/rgod60DpBInJzdfztR efXaTBdXlr4UkQ6YMtPwapk= X-Google-Smtp-Source: AGHT+IG1aAKgdGzC3k/REBwV6JyV6IBliK6pMT84b9AjnExv2LuJIAXVc6W6Py1AsuS5MaK/OhKUzw== X-Received: by 2002:a05:6512:2248:b0:4fe:7e7f:1328 with SMTP id i8-20020a056512224800b004fe7e7f1328mr17608450lfu.16.1698924544906; Thu, 02 Nov 2023 04:29:04 -0700 (PDT) Received: from localhost.localdomain ([46.248.82.114]) by smtp.gmail.com with ESMTPSA id d10-20020a5d4f8a000000b003232f167df5sm2168227wru.108.2023.11.02.04.29.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 04:29:04 -0700 (PDT) From: Uros Bizjak To: x86@kernel.org, linux-kernel@vger.kernel.org Cc: Uros Bizjak , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Peter Zijlstra Subject: [PATCH 3/3] x86/callthunks: Fix and unify call thunks assembly snippets Date: Thu, 2 Nov 2023 12:25:48 +0100 Message-ID: <20231102112850.3448745-4-ubizjak@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231102112850.3448745-1-ubizjak@gmail.com> References: <20231102112850.3448745-1-ubizjak@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently thunk debug macros explicitly define %gs: segment register prefix for their percpu variables. This is not compatible with !CONFIG_SMP, which requires non-prefixed percpu variables. Fix call thunks debug macros to use PER_CPU_VAR macro from percpu.h to conditionally use %gs: segment register prefix, depending on CONFIG_SMP. Finally, unify ASM_ prefixed assembly macros with their non-prefixed variants. With support of %rip-relative relocations in place, call thunk templates allow %rip-relative addressing, so unified assembly snippet can be used everywhere. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: Peter Zijlstra Signed-off-by: Uros Bizjak --- arch/x86/include/asm/nospec-branch.h | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/no= spec-branch.h index f93e9b96927a..6f677be6bdb9 100644 --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -59,13 +59,13 @@ =20 #ifdef CONFIG_CALL_THUNKS_DEBUG # define CALL_THUNKS_DEBUG_INC_CALLS \ - incq %gs:__x86_call_count; + incq PER_CPU_VAR(__x86_call_count); # define CALL_THUNKS_DEBUG_INC_RETS \ - incq %gs:__x86_ret_count; + incq PER_CPU_VAR(__x86_ret_count); # define CALL_THUNKS_DEBUG_INC_STUFFS \ - incq %gs:__x86_stuffs_count; + incq PER_CPU_VAR(__x86_stuffs_count); # define CALL_THUNKS_DEBUG_INC_CTXSW \ - incq %gs:__x86_ctxsw_count; + incq PER_CPU_VAR(__x86_ctxsw_count); #else # define CALL_THUNKS_DEBUG_INC_CALLS # define CALL_THUNKS_DEBUG_INC_RETS @@ -80,9 +80,6 @@ #define CREDIT_CALL_DEPTH \ movq $-1, PER_CPU_VAR(pcpu_hot + X86_call_depth); =20 -#define ASM_CREDIT_CALL_DEPTH \ - movq $-1, PER_CPU_VAR(pcpu_hot + X86_call_depth); - #define RESET_CALL_DEPTH \ xor %eax, %eax; \ bts $63, %rax; \ @@ -95,20 +92,14 @@ CALL_THUNKS_DEBUG_INC_CALLS =20 #define INCREMENT_CALL_DEPTH \ - sarq $5, %gs:pcpu_hot + X86_call_depth; \ - CALL_THUNKS_DEBUG_INC_CALLS - -#define ASM_INCREMENT_CALL_DEPTH \ sarq $5, PER_CPU_VAR(pcpu_hot + X86_call_depth); \ CALL_THUNKS_DEBUG_INC_CALLS =20 #else #define CREDIT_CALL_DEPTH -#define ASM_CREDIT_CALL_DEPTH #define RESET_CALL_DEPTH -#define INCREMENT_CALL_DEPTH -#define ASM_INCREMENT_CALL_DEPTH #define RESET_CALL_DEPTH_FROM_CALL +#define INCREMENT_CALL_DEPTH #endif =20 /* @@ -158,7 +149,7 @@ jnz 771b; \ /* barrier for jnz misprediction */ \ lfence; \ - ASM_CREDIT_CALL_DEPTH \ + CREDIT_CALL_DEPTH \ CALL_THUNKS_DEBUG_INC_CTXSW #else /* @@ -311,7 +302,7 @@ .macro CALL_DEPTH_ACCOUNT #ifdef CONFIG_CALL_DEPTH_TRACKING ALTERNATIVE "", \ - __stringify(ASM_INCREMENT_CALL_DEPTH), X86_FEATURE_CALL_DEPTH + __stringify(INCREMENT_CALL_DEPTH), X86_FEATURE_CALL_DEPTH #endif .endm =20 --=20 2.41.0