Skip to contentSkip to conversation

x86: vdso: Compute timens page offset statically

PATCH In review Jason A. Donenfeld 2 participants 6 messages Sep 6, 2024 – Dec 1, 2024

Subsystems (4)
lore
Share & tools
lore

Jason A. Donenfeld

author maintainer
#0 lore raw

PATCH 1/1 x86: vdso: Compute timens page offset statically

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.

---
 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(-)
arch/x86/entry/vdso/vdso-layout.lds.S +5 -6
index bafa73f09e92..ddd6999b6946 100644
@@ -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. */
arch/x86/include/asm/vdso/getrandom.h +1 -1
index b96e674cafde..3569fe95aa4e 100644
@@ -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;
 }
 
arch/x86/include/asm/vvar.h +5 -0
index 9d9af37f7cab..d2a2ffa72909 100644
@@ -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
Signature · 1 line
2.46.0
author maintainer
#1 lore raw

Hey tglx,

Quoted 10 lines · 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.

I noticed we've only got a week left til the merge window opens, so I thought I should poke you about this, if you want to take this through tip. I can also take it through my random.git tree with your ack, if that's easier for you. (Assuming, of course, that this patch is actually correct.) Let me know.

Jason

#2 lore raw
Quoted 12 lines · Jason A. Donenfeld
Quoted 10 lines · 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.

Quoted 5 lines

I noticed we've only got a week left til the merge window opens, so I thought I should poke you about this, if you want to take this through tip. I can also take it through my random.git tree with your ack, if that's easier for you. (Assuming, of course, that this patch is actually correct.) Let me know.

It's not the end of the world if this does not go in now. It's in my back log and that VDSO stuff needs more care than this particular thing as the recent discussion about vdso random on other architectures show.

Thanks,

tglx

author maintainer
#3 lore raw
Quoted 19 lines · Thomas Gleixner
Quoted 12 lines · Jason A. Donenfeld
Quoted 10 lines · 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.

Quoted 5 lines

I noticed we've only got a week left til the merge window opens, so I thought I should poke you about this, if you want to take this through tip. I can also take it through my random.git tree with your ack, if that's easier for you. (Assuming, of course, that this patch is actually correct.) Let me know.

Quoted 3 lines

It's not the end of the world if this does not go in now. It's in my back log and that VDSO stuff needs more care than this particular thing as the recent discussion about vdso random on other architectures show.

Okay, that makes sense. And thanks for taking a look.

Jason

author maintainer
#4 lore raw
Quoted 20 lines · Jason A. Donenfeld
Quoted 19 lines · Thomas Gleixner
Quoted 17 lines · Jason A. Donenfeld
Quoted 10 lines · 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.

I noticed we've only got a week left til the merge window opens, so I thought I should poke you about this, if you want to take this through tip. I can also take it through my random.git tree with your ack, if that's easier for you. (Assuming, of course, that this patch is actually correct.) Let me know.

Quoted 5 lines
Quoted 3 lines

It's not the end of the world if this does not go in now. It's in my back log and that VDSO stuff needs more care than this particular thing as the recent discussion about vdso random on other architectures show.

Okay, that makes sense. And thanks for taking a look.

Thought I'd poke again about this. It looks like this still applies to tip.

Jason

#5 lore raw
Quoted 21 lines · Jason A. Donenfeld
Quoted 20 lines · Jason A. Donenfeld
Quoted 19 lines · Thomas Gleixner
Quoted 17 lines · Jason A. Donenfeld
Quoted 10 lines · 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.

I noticed we've only got a week left til the merge window opens, so I thought I should poke you about this, if you want to take this through tip. I can also take it through my random.git tree with your ack, if that's easier for you. (Assuming, of course, that this patch is actually correct.) Let me know.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

It's not the end of the world if this does not go in now. It's in my back log and that VDSO stuff needs more care than this particular thing as the recent discussion about vdso random on other architectures show.

Okay, that makes sense. And thanks for taking a look.

Thought I'd poke again about this. It looks like this still applies to tip.

It's on my radar...