From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de,
dave.hansen@linux.intel.com, x86@kernel.org,
linux-kernel@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH tip] x86: vdso: Compute timens page offset statically
Date: Fri, 6 Sep 2024 21:06:55 +0200 [thread overview]
Message-ID: <20240906190655.2777023-1-Jason@zx2c4.com> (raw)
The expression `((void *)&__timens_vdso_data - (void *)&__vdso_data)`
seems harmless, but it actually results in quite a bit of code and two
jumps, in a place that's supposed to be somewhat lean on code. The value
of that calculation is always 3*PAGE_SIZE, as it turns out. Changing it
to that results in a more modest cmov instruction being emitted. It also
makes it a bit more clear what's happening.
To accomplish this, define offset macros in vvar.h, which can be shared
by C code and by the linker script that decides where these pages will
actually go.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
arch/x86/entry/vdso/vdso-layout.lds.S | 11 +++++------
arch/x86/include/asm/vdso/getrandom.h | 2 +-
arch/x86/include/asm/vvar.h | 5 +++++
3 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/arch/x86/entry/vdso/vdso-layout.lds.S b/arch/x86/entry/vdso/vdso-layout.lds.S
index bafa73f09e92..ddd6999b6946 100644
--- a/arch/x86/entry/vdso/vdso-layout.lds.S
+++ b/arch/x86/entry/vdso/vdso-layout.lds.S
@@ -16,17 +16,16 @@ SECTIONS
* segment.
*/
- vvar_start = . - 4 * PAGE_SIZE;
- vvar_page = vvar_start;
-
/* Place all vvars at the offsets in asm/vvar.h. */
#define EMIT_VVAR(name, offset) vvar_ ## name = vvar_page + offset;
#include <asm/vvar.h>
#undef EMIT_VVAR
- pvclock_page = vvar_start + PAGE_SIZE;
- hvclock_page = vvar_start + 2 * PAGE_SIZE;
- timens_page = vvar_start + 3 * PAGE_SIZE;
+ vvar_start = . - 4 * PAGE_SIZE;
+ vvar_page = vvar_start + VVAR_PAGE_OFFSET * PAGE_SIZE;
+ pvclock_page = vvar_start + PVCLOCK_PAGE_OFFSET * PAGE_SIZE;
+ hvclock_page = vvar_start + HVCLOCK_PAGE_OFFSET * PAGE_SIZE;
+ timens_page = vvar_start + TIMENS_PAGE_OFFSET * PAGE_SIZE;
#undef _ASM_X86_VVAR_H
/* Place all vvars in timens too at the offsets in asm/vvar.h. */
diff --git a/arch/x86/include/asm/vdso/getrandom.h b/arch/x86/include/asm/vdso/getrandom.h
index b96e674cafde..3569fe95aa4e 100644
--- a/arch/x86/include/asm/vdso/getrandom.h
+++ b/arch/x86/include/asm/vdso/getrandom.h
@@ -33,7 +33,7 @@ static __always_inline ssize_t getrandom_syscall(void *buffer, size_t len, unsig
static __always_inline const struct vdso_rng_data *__arch_get_vdso_rng_data(void)
{
if (IS_ENABLED(CONFIG_TIME_NS) && __vdso_data->clock_mode == VDSO_CLOCKMODE_TIMENS)
- return (void *)&__vdso_rng_data + ((void *)&__timens_vdso_data - (void *)&__vdso_data);
+ return (void *)&__vdso_rng_data + (TIMENS_PAGE_OFFSET << CONFIG_PAGE_SHIFT);
return &__vdso_rng_data;
}
diff --git a/arch/x86/include/asm/vvar.h b/arch/x86/include/asm/vvar.h
index 9d9af37f7cab..d2a2ffa72909 100644
--- a/arch/x86/include/asm/vvar.h
+++ b/arch/x86/include/asm/vvar.h
@@ -19,6 +19,11 @@
#ifndef _ASM_X86_VVAR_H
#define _ASM_X86_VVAR_H
+#define VVAR_PAGE_OFFSET 0
+#define PVCLOCK_PAGE_OFFSET 1
+#define HVCLOCK_PAGE_OFFSET 2
+#define TIMENS_PAGE_OFFSET 3
+
#ifdef EMIT_VVAR
/*
* EMIT_VVAR() is used by the kernel linker script to put vvars in the
--
2.46.0
next reply other threads:[~2024-09-06 19:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-06 19:06 Jason A. Donenfeld [this message]
2024-09-09 15:44 ` [PATCH tip] x86: vdso: Compute timens page offset statically Jason A. Donenfeld
2024-09-10 12:08 ` Thomas Gleixner
2024-09-10 12:20 ` Jason A. Donenfeld
2024-11-18 2:18 ` Jason A. Donenfeld
2024-12-01 13:03 ` Thomas Gleixner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240906190655.2777023-1-Jason@zx2c4.com \
--to=jason@zx2c4.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.