public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH tip] x86: vdso: Compute timens page offset statically
@ 2024-09-06 19:06 Jason A. Donenfeld
  2024-09-09 15:44 ` Jason A. Donenfeld
  0 siblings, 1 reply; 6+ messages in thread
From: Jason A. Donenfeld @ 2024-09-06 19:06 UTC (permalink / raw)
  To: tglx, mingo, bp, dave.hansen, x86, linux-kernel; +Cc: Jason A. Donenfeld

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-12-01 13:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-06 19:06 [PATCH tip] x86: vdso: Compute timens page offset statically Jason A. Donenfeld
2024-09-09 15:44 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox