Skip to contentSkip to conversation

Wire up getrandom() vDSO implementation on powerpc

PATCH Superseded Christophe Leroy 10 participants 65 messages Aug 22, 2024 – Aug 30, 2024

Subsystems (15)
Memory management x86 VFS PowerPC +11 more subsystems

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover Wire up getrandom() vDSO implementation on powerpc

This series wires up getrandom() vDSO implementation on powerpc.

Tested on PPC32.

Performance on powerpc 885 (using kernel selftest):

	~# ./vdso_test_getrandom bench-single
	   vdso: 2500000 times in 7.897495392 seconds
	   libc: 2500000 times in 56.091632232 seconds
	syscall: 2500000 times in 55.704851989 seconds

Performance on powerpc 8321 (using kernel selftest):

	~# ./vdso_test_getrandom bench-single
	   vdso: 2500000 times in 2.017183250 seconds
	   libc: 2500000 times in 13.088533630 seconds
	syscall: 2500000 times in 12.952458068 seconds

Only build tested on PPC64. There is a problem with vdso_test_getrandom selftest, it doesn't find vDSO symbol __kernel_getrandom. There is the same problem with vdso_test_gettimeofday so it is not related to getrandom.

On strange things to be clarified, there is the format of the key passed to __arch_chacha20_blocks_nostack(). In struct vgetrandom_state it is declared as a table of u32, but in reality it seems it is a flat storage that needs to be loaded in reversed byte order, so it should either be defined as a table of bytes, or as a table of __le32 but not a table of u32. But this has no impact and can be clarified later and fixed in a follow-up patch.

Changes in v2:
- Define VM_DROPPABLE for powerpc/32
- Fixes generic vDSO getrandom headers to enable CONFIG_COMPAT build.
- Fixed size of generation counter
- Fixed selftests to work on non x86 architectures

Christophe Leroy (17):
asm-generic/unaligned.h: Extract common header for vDSO
vdso: Clean header inclusion in getrandom
vdso: Add __arch_get_k_vdso_rng_data()
vdso: Add missing c-getrandom-y in Makefile
vdso: Avoid call to memset() by getrandom
vdso: Change getrandom's generation to unsigned long
mm: Define VM_DROPPABLE for powerpc/32
powerpc: Add little endian variants of LWZX_BE and STWX_BE powerpc/vdso32: Add crtsavres
powerpc/vdso: Refactor CFLAGS for CVDSO build powerpc/vdso: Wire up getrandom() vDSO implementation
selftests: vdso: Fix powerpc64 vdso_config
selftests: vdso: Don't hard-code location of vDSO sources
selftests: vdso: Make test_vdso_getrandom look for the right vDSO
function
selftests: vdso: Fix build of test_vdso_chacha
selftests: vdso: Make VDSO function call more generic
selftests: vdso: Add support for vdso_test_random for powerpc

 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/include/asm/asm-compat.h         |   8 +
 arch/powerpc/include/asm/mman.h               |   2 +-
 arch/powerpc/include/asm/vdso/getrandom.h     |  67 ++++
 arch/powerpc/include/asm/vdso/vsyscall.h      |   6 +
 arch/powerpc/include/asm/vdso_datapage.h      |   2 +
 arch/powerpc/kernel/asm-offsets.c             |   1 +
 arch/powerpc/kernel/vdso/Makefile             |  45 ++-
 arch/powerpc/kernel/vdso/getrandom.S          |  58 ++++
 arch/powerpc/kernel/vdso/gettimeofday.S       |  13 -
 arch/powerpc/kernel/vdso/vdso32.lds.S         |   1 +
 arch/powerpc/kernel/vdso/vdso64.lds.S         |   1 +
 arch/powerpc/kernel/vdso/vgetrandom-chacha.S  | 297 ++++++++++++++++++
 arch/powerpc/kernel/vdso/vgetrandom.c         |  14 +
 arch/x86/entry/vdso/vma.c                     |   3 +
 arch/x86/include/asm/pvclock.h                |   1 +
 arch/x86/include/asm/vdso/vsyscall.h          |  10 +-
 drivers/char/random.c                         |   5 +-
 fs/proc/task_mmu.c                            |   4 +-
 include/asm-generic/unaligned.h               |  11 +-
 include/linux/mm.h                            |   4 +-
 include/trace/events/mmflags.h                |   4 +-
 include/vdso/datapage.h                       |   2 +-
 include/vdso/getrandom.h                      |   2 +-
 include/vdso/helpers.h                        |   1 +
 include/vdso/unaligned.h                      |  15 +
 lib/vdso/Makefile                             |   1 +
 lib/vdso/getrandom.c                          |  30 +-
 tools/arch/powerpc/vdso                       |   1 +
 tools/arch/x86/vdso                           |   1 +
 tools/include/linux/linkage.h                 |   4 +
 tools/testing/selftests/vDSO/Makefile         |  12 +-
 tools/testing/selftests/vDSO/vdso_call.h      |  52 +++
 tools/testing/selftests/vDSO/vdso_config.h    |  14 +-
 .../selftests/vDSO/vdso_test_getrandom.c      |  11 +-
 35 files changed, 628 insertions(+), 76 deletions(-)
 create mode 100644 arch/powerpc/include/asm/vdso/getrandom.h
 create mode 100644 arch/powerpc/kernel/vdso/getrandom.S
 create mode 100644 arch/powerpc/kernel/vdso/vgetrandom-chacha.S
 create mode 100644 arch/powerpc/kernel/vdso/vgetrandom.c
 create mode 100644 include/vdso/unaligned.h
 create mode 120000 tools/arch/powerpc/vdso
 create mode 120000 tools/arch/x86/vdso
 create mode 100644 tools/testing/selftests/vDSO/vdso_call.h
Signature · 1 line
2.44.0
#1 lore raw

PATCH v2 1/17 asm-generic/unaligned.h: Extract common header for vDSO

getrandom vDSO implementation requires __put_unaligned_t() and __put_unaligned_t() but including asm-generic/unaligned.h pulls too many other headers.

Follow the same approach as for most things in include/vdso/, see for instance commit 8165b57bca21 ("linux/const.h: Extract common header for vDSO"): Move __get_unaligned_t and __put_unaligned_t into a new unaligned.h living in the vdso/ include directory.

---
 include/asm-generic/unaligned.h | 11 +----------
 include/vdso/unaligned.h        | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 10 deletions(-)
 create mode 100644 include/vdso/unaligned.h
include/asm-generic/unaligned.h +1 -10
index a84c64e5f11e..95acdd70b3b2 100644
@@ -8,16 +8,7 @@
  */
 #include <linux/unaligned/packed_struct.h>
 #include <asm/byteorder.h>
-
-#define __get_unaligned_t(type, ptr) ({						\
-	const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);	\
-	__pptr->x;								\
-})
-
-#define __put_unaligned_t(type, val, ptr) do {					\
-	struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);		\
-	__pptr->x = (val);							\
-} while (0)
+#include <vdso/unaligned.h>
 
 #define get_unaligned(ptr)	__get_unaligned_t(typeof(*(ptr)), (ptr))
 #define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr))
include/vdso/unaligned.h new file +15 -0
new file mode 100644
index 000000000000..eee3d2a4dbe4
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __VDSO_UNALIGNED_H
+#define __VDSO_UNALIGNED_H
+
+#define __get_unaligned_t(type, ptr) ({						\
+	const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);	\
+	__pptr->x;								\
+})
+
+#define __put_unaligned_t(type, val, ptr) do {					\
+	struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);		\
+	__pptr->x = (val);							\
+} while (0)
+
+#endif /* __VDSO_UNALIGNED_H */
Signature · 1 line
2.44.0
#2 lore raw

Re: [PATCH v2 01/17] asm-generic/unaligned.h: Extract common header for vDSO

Quoted 4 lines · Christophe Leroy
 include/asm-generic/unaligned.h | 11 +----------
 include/vdso/unaligned.h        | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 10 deletions(-)
 create mode 100644 include/vdso/unaligned.h

Do you need to also adjust the #include <asm/unaligned.h> inside of lib/vdso/getrandom.c to instead read #include <vdso/unaligned.h>?

Jason

#3 lore raw

Re: [PATCH v2 01/17] asm-generic/unaligned.h: Extract common header for vDSO

Le 26/08/2024 à 09:20, Jason A. Donenfeld a écrit :

Quoted 8 lines · Christophe Leroy
Quoted 4 lines
  include/asm-generic/unaligned.h | 11 +----------
  include/vdso/unaligned.h        | 15 +++++++++++++++
  2 files changed, 16 insertions(+), 10 deletions(-)

create mode 100644 include/vdso/unaligned.h

Do you need to also adjust the #include <asm/unaligned.h> inside of lib/vdso/getrandom.c to instead read #include <vdso/unaligned.h>?

Yes, all adjustments to lib/vdso/getrandom.c are in patch 2.

Christophe

#4 lore raw

PATCH v2 2/17 vdso: Clean header inclusion in getrandom

In reply to Christophe Leroy

Building a VDSO32 on a 64 bits kernel is problematic when some system headers are included. See commit 8c59ab839f52 ("lib/vdso:
Enable common headers") for more details.

Minimise the amount of headers by moving needed items into dedicated common headers.

For PAGE_SIZE and PAGE_MASK, redefine them from CONFIG_PAGE_SHIFT in the same way as commit cffaefd15a8f ("vdso: Use CONFIG_PAGE_SHIFT in vdso/datapage.h")

---
 arch/x86/include/asm/pvclock.h |  1 +
 include/vdso/helpers.h         |  1 +
 lib/vdso/getrandom.c           | 15 ++++++++-------
 3 files changed, 10 insertions(+), 7 deletions(-)
arch/x86/include/asm/pvclock.h +1 -0
index 0c92db84469d..6e4f8fae3ce9 100644
@@ -5,6 +5,7 @@
 #include <asm/clocksource.h>
 #include <asm/pvclock-abi.h>
 
+struct timespec64;
 /* some helper functions for xen and kvm pv clock sources */
 u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
 u64 pvclock_clocksource_read_nowd(struct pvclock_vcpu_time_info *src);
include/vdso/helpers.h +1 -0
index 73501149439d..3ddb03bb05cb 100644
@@ -4,6 +4,7 @@
 
 #ifndef __ASSEMBLY__
 
+#include <asm/barrier.h>
 #include <vdso/datapage.h>
 
 static __always_inline u32 vdso_read_begin(const struct vdso_data *vd)
lib/vdso/getrandom.c +8 -7
index b230f0b10832..cab153c5f9be 100644
@@ -3,15 +3,13 @@
  * Copyright (C) 2022-2024 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  */
 
-#include <linux/cache.h>
-#include <linux/kernel.h>
-#include <linux/time64.h>
+#include <linux/minmax.h>
 #include <vdso/datapage.h>
 #include <vdso/getrandom.h>
+#include <vdso/unaligned.h>
 #include <asm/vdso/getrandom.h>
-#include <asm/vdso/vsyscall.h>
-#include <asm/unaligned.h>
 #include <uapi/linux/mman.h>
+#include <uapi/linux/random.h>
 
 #define MEMCPY_AND_ZERO_SRC(type, dst, src, len) do {				\
 	while (len >= sizeof(type)) {						\
@@ -23,6 +21,9 @@
 	}									\
 } while (0)
 
+#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
+#define _PAGE_MASK (~(_PAGE_SIZE - 1))
+
 static void memcpy_and_zero_src(void *dst, void *src, size_t len)
 {
 	if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
@@ -64,7 +65,7 @@ static __always_inline ssize_t
 __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_t len,
 		       unsigned int flags, void *opaque_state, size_t opaque_len)
 {
-	ssize_t ret = min_t(size_t, INT_MAX & PAGE_MASK /* = MAX_RW_COUNT */, len);
+	ssize_t ret = min_t(size_t, INT_MAX & _PAGE_MASK /* = MAX_RW_COUNT */, len);
 	struct vgetrandom_state *state = opaque_state;
 	size_t batch_len, nblocks, orig_len = len;
 	bool in_use, have_retried = false;
@@ -82,7 +83,7 @@ __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_
 	}
 
 	/* The state must not straddle a page, since pages can be zeroed at any time. */
-	if (unlikely(((unsigned long)opaque_state & ~PAGE_MASK) + sizeof(*state) > PAGE_SIZE))
+	if (unlikely(((unsigned long)opaque_state & ~_PAGE_MASK) + sizeof(*state) > _PAGE_SIZE))
 		return -EFAULT;
 
 	/* If the caller passes the wrong size, which might happen due to CRIU, fallback. */
Signature · 1 line
2.44.0
#5 lore raw

Re: [PATCH v2 02/17] vdso: Clean header inclusion in getrandom

arch/x86/include/asm/pvclock.h line 11

Quoted 12 lines · Christophe Leroy
arch/x86/include/asm/pvclock.h +1 -0
index 0c92db84469d..6e4f8fae3ce9 100644
@@ -5,6 +5,7 @@
 #include <asm/clocksource.h>
 #include <asm/pvclock-abi.h>
 
+struct timespec64;
 /* some helper functions for xen and kvm pv clock sources */
 u64 pvclock_clocksource_read(struct pvclock_vcpu_time_info *src);
 u64 pvclock_clocksource_read_nowd(struct pvclock_vcpu_time_info *src);

This change isn't mentioned in the commit message and could probably benefit from doing so.

Quoted 5 lines
lib/vdso/getrandom.c +0 -0
index b230f0b10832..cab153c5f9be 100644

lib/vdso/getrandom.c line 9

Quoted 10 lines
lib/vdso/getrandom.c +2 -3
@@ -3,15 +3,13 @@
  * Copyright (C) 2022-2024 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  */
 
-#include <linux/cache.h>
-#include <linux/kernel.h>
-#include <linux/time64.h>
+#include <linux/minmax.h>
 #include <vdso/datapage.h>
 #include <vdso/getrandom.h>
+#include <vdso/unaligned.h>

Ah, that's where you do it. Ignore my comment on the previous commit, then.

Quoted 9 lines
lib/vdso/getrandom.c +1 -2
 #include <asm/vdso/getrandom.h>
-#include <asm/vdso/vsyscall.h>
-#include <asm/unaligned.h>
 #include <uapi/linux/mman.h>
+#include <uapi/linux/random.h>
 
 #define MEMCPY_AND_ZERO_SRC(type, dst, src, len) do {				\
 	while (len >= sizeof(type)) {						\

lib/vdso/getrandom.c line 25

Quoted 5 lines
lib/vdso/getrandom.c +2 -0
@@ -23,6 +21,9 @@
 	}									\
 } while (0)
 
+#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
+#define _PAGE_MASK (~(_PAGE_SIZE - 1))

If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE instead of _PAGE_SIZE? But if that's the case, why not put the vdso definition of PAGE_SIZE into some vdso header included by this file?

#6 lore raw

Re: [PATCH v2 02/17] vdso: Clean header inclusion in getrandom

Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :

Quoted 8 lines · Christophe Leroy

lib/vdso/getrandom.c line 25

Quoted 3 lines
lib/vdso/getrandom.c +2 -0
+#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
+#define _PAGE_MASK (~(_PAGE_SIZE - 1))

If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE instead of _PAGE_SIZE? But if that's the case, why not put the vdso definition of PAGE_SIZE into some vdso header included by this file?

It was working ok on powerpc but on x86 I got:

CC arch/x86/entry/vdso/vgetrandom.o
In file included from arch/x86/entry/vdso/vgetrandom.c:7:
arch/x86/entry/vdso/../../../../lib/vdso/getrandom.c:24: error: "PAGE_SIZE" redefined [-Werror]
24 | #define PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)

      |
In file included from ./arch/x86/include/asm/page.h:9,
                 from ./arch/x86/include/asm/thread_info.h:12,
                 from ./include/linux/thread_info.h:60,
                 from ./include/linux/smp.h:118,
                 from ./include/linux/alloc_tag.h:14,
                 from ./include/linux/percpu.h:5,
                 from ./arch/x86/include/asm/msr.h:15,
                 from ./arch/x86/include/asm/vdso/gettimeofday.h:19,
                 from ./include/vdso/datapage.h:164,
                 from 

arch/x86/entry/vdso/../../../../lib/vdso/getrandom.c:7,
from arch/x86/entry/vdso/vgetrandom.c:7:
./arch/x86/include/asm/page_types.h:11: note: this is the location of the previous definition
11 | #define PAGE_SIZE (_AC(1,UL) << PAGE_SHIFT)

      |
In file included from arch/x86/entry/vdso/vgetrandom.c:7:
arch/x86/entry/vdso/../../../../lib/vdso/getrandom.c:25: error: "PAGE_MASK" redefined [-Werror]
   25 | #define PAGE_MASK (~(PAGE_SIZE - 1))
      |
In file included from ./arch/x86/include/asm/page.h:9,
                 from ./arch/x86/include/asm/thread_info.h:12,
                 from ./include/linux/thread_info.h:60,
                 from ./include/linux/smp.h:118,
                 from ./include/linux/alloc_tag.h:14,
                 from ./include/linux/percpu.h:5,
                 from ./arch/x86/include/asm/msr.h:15,
                 from ./arch/x86/include/asm/vdso/gettimeofday.h:19,
                 from ./include/vdso/datapage.h:164,
                 from 

arch/x86/entry/vdso/../../../../lib/vdso/getrandom.c:7,
from arch/x86/entry/vdso/vgetrandom.c:7:
./arch/x86/include/asm/page_types.h:12: note: this is the location of the previous definition
12 | #define PAGE_MASK (~(PAGE_SIZE-1))

      |

cc1: all warnings being treated as errors

Christophe

#7 lore raw

Re: [PATCH v2 02/17] vdso: Clean header inclusion in getrandom

Quoted 5 lines · Christophe Leroy

Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :

Quoted 2 lines · Christophe Leroy
Quoted 1 line
Quoted 8 lines
Quoted 6 lines

lib/vdso/getrandom.c line 25

Quoted 2 lines
lib/vdso/getrandom.c +2 -0
+#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
+#define _PAGE_MASK (~(_PAGE_SIZE - 1))

If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE instead of _PAGE_SIZE? But if that's the case, why not put the vdso definition of PAGE_SIZE into some vdso header included by this file?

It was working ok on powerpc but on x86 I got:

Seems like there might be some more fiddling to do, then? Or did you conclude it's impossible?

#8 lore raw

Re: [PATCH v2 02/17] vdso: Clean header inclusion in getrandom

Le 26/08/2024 à 10:58, Jason A. Donenfeld a écrit :

Quoted 13 lines · Christophe Leroy
Quoted 12 lines

Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :

Quoted 8 lines · Christophe Leroy

lib/vdso/getrandom.c line 25

Quoted 3 lines
lib/vdso/getrandom.c +2 -0
+#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
+#define _PAGE_MASK (~(_PAGE_SIZE - 1))

If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE instead of _PAGE_SIZE? But if that's the case, why not put the vdso definition of PAGE_SIZE into some vdso header included by this file?

Quoted 4 lines
Quoted 1 line

It was working ok on powerpc but on x86 I got:

Seems like there might be some more fiddling to do, then? Or did you conclude it's impossible?

Maybe someone who knows x86 in details could helps but after a first look I gave up because it looks very x86 specific, indeed that's x86/asm/vdso/gettimeofday.h that pulls several x86/asm/ headers , and the same type of issue might arise for any new architecture coming in.

For me it looked cleaner to just do as commit cffaefd15a8f ("vdso: Use CONFIG_PAGE_SHIFT in vdso/datapage.h") and not use PAGE_SIZE at all. But I didn't want to directly use (1UL << CONFIG_PAGE_SHIFT) and (~(1UL << (CONFIG_PAGE_SHIFT - 1))) in the code directly hence the new macros with a leading underscore to avoid any conflict with existing macros.

Christophe

#9 lore raw

Re: [PATCH v2 02/17] vdso: Clean header inclusion in getrandom

Quoted 26 lines · Christophe Leroy

Le 26/08/2024 à 10:58, Jason A. Donenfeld a écrit :

Quoted 13 lines · Christophe Leroy
Quoted 12 lines

Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :

Quoted 8 lines · Christophe Leroy

lib/vdso/getrandom.c line 25

Quoted 3 lines
lib/vdso/getrandom.c +2 -0
+#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
+#define _PAGE_MASK (~(_PAGE_SIZE - 1))

If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE instead of _PAGE_SIZE? But if that's the case, why not put the vdso definition of PAGE_SIZE into some vdso header included by this file?

Quoted 4 lines
Quoted 1 line

It was working ok on powerpc but on x86 I got:

Seems like there might be some more fiddling to do, then? Or did you conclude it's impossible?

Maybe someone who knows x86 in details could helps but after a first look I gave up because it looks very x86 specific, indeed that's x86/asm/vdso/gettimeofday.h that pulls several x86/asm/ headers , and the same type of issue might arise for any new architecture coming in.

Quoted 5 lines

For me it looked cleaner to just do as commit cffaefd15a8f ("vdso: Use CONFIG_PAGE_SHIFT in vdso/datapage.h") and not use PAGE_SIZE at all. But I didn't want to directly use (1UL << CONFIG_PAGE_SHIFT) and (~(1UL << (CONFIG_PAGE_SHIFT - 1))) in the code directly hence the new macros with a leading underscore to avoid any conflict with existing macros.

If that's the case and you can't sort it out or it's impossible, then it seems like adding #ifndef PAGE_SIZE, #define... inside of some vdso header (not inside getrandom.c) would make sense.

Jason

#10 lore raw

Re: [PATCH v2 02/17] vdso: Clean header inclusion in getrandom

In reply to Christophe Leroy
Quoted 16 lines · Christophe Leroy

Le 26/08/2024 à 10:58, Jason A. Donenfeld a écrit :

Quoted 15 lines · Christophe Leroy
Quoted 13 lines

Le 26/08/2024 à 10:07, Jason A. Donenfeld a écrit :

Quoted 8 lines · Christophe Leroy

lib/vdso/getrandom.c line 25

Quoted 3 lines
lib/vdso/getrandom.c +2 -0
+#define _PAGE_SIZE (1UL << CONFIG_PAGE_SHIFT)
+#define _PAGE_MASK (~(_PAGE_SIZE - 1))

If PAGE_SIZE isn't defined at this point, why not just call it PAGE_SIZE instead of _PAGE_SIZE? But if that's the case, why not put the vdso definition of PAGE_SIZE into some vdso header included by this file?

It was working ok on powerpc but on x86 I got:

Quoted 7 lines
Quoted 2 lines

Seems like there might be some more fiddling to do, then? Or did you conclude it's impossible?

Maybe someone who knows x86 in details could helps but after a first look I gave up because it looks very x86 specific, indeed that's x86/asm/vdso/gettimeofday.h that pulls several x86/asm/ headers , and the same type of issue might arise for any new architecture coming in.

Of course :)

Quoted 5 lines

For me it looked cleaner to just do as commit cffaefd15a8f ("vdso: Use CONFIG_PAGE_SHIFT in vdso/datapage.h") and not use PAGE_SIZE at all. But I didn't want to directly use (1UL << CONFIG_PAGE_SHIFT) and (~(1UL << (CONFIG_PAGE_SHIFT - 1))) in the code directly hence the new macros with a leading underscore to avoid any conflict with existing macros.

#ifndef PAGE_SIZE

# define
#endif

Perhaps?

Thanks,

tglx

#11 lore raw

PATCH v2 3/17 vdso: Add __arch_get_k_vdso_rng_data()

In reply to Christophe Leroy

_vdso_data is specific to x86 and __arch_get_k_vdso_data() is provided so that all architectures can provide the requested pointer.

Do the same with _vdso_rng_data, provide __arch_get_k_vdso_rng_data() and don't use x86 _vdso_rng_data directly.

Until now vdso/vsyscall.h was only included by time/vsyscall.c but now it will also be included in char/random.c, leading to a duplicate declaration of _vdso_data and _vdso_rng_data.

To fix this issue, move declaration in a C file. vma.c looks like the most appropriate candidate. Don't need to replace the definitions in vsyscall.h by declarations as declarations are already in asm/vvar.h

---
v2: Move x86 DEFINE_VVAR(_vdso_data) and DEFINE_VVAR(_vdso_rng_data) in vma.c

---
 arch/x86/entry/vdso/vma.c            |  3 +++
 arch/x86/include/asm/vdso/vsyscall.h | 10 +++++++---
 drivers/char/random.c                |  5 +++--
 3 files changed, 13 insertions(+), 5 deletions(-)
arch/x86/entry/vdso/vma.c +3 -0
index 6d83ceb7f1ba..b8fed8b8b9cc 100644
@@ -38,6 +38,9 @@ struct vdso_data *arch_get_vdso_data(void *vvar_page)
 }
 #undef EMIT_VVAR
 
+DEFINE_VVAR(struct vdso_data, _vdso_data);
+DEFINE_VVAR_SINGLE(struct vdso_rng_data, _vdso_rng_data);
+
 unsigned int vclocks_used __read_mostly;
 
 #if defined(CONFIG_X86_64)
arch/x86/include/asm/vdso/vsyscall.h +7 -3
index 972415a8be31..67fedf1698b5 100644
@@ -9,9 +9,6 @@
 #include <asm/vgtod.h>
 #include <asm/vvar.h>
 
-DEFINE_VVAR(struct vdso_data, _vdso_data);
-DEFINE_VVAR_SINGLE(struct vdso_rng_data, _vdso_rng_data);
-
 /*
  * Update the vDSO data page to keep in sync with kernel timekeeping.
  */
@@ -22,6 +19,13 @@ struct vdso_data *__x86_get_k_vdso_data(void)
 }
 #define __arch_get_k_vdso_data __x86_get_k_vdso_data
 
+static __always_inline
+struct vdso_rng_data *__x86_get_k_vdso_rng_data(void)
+{
+	return &_vdso_rng_data;
+}
+#define __arch_get_k_vdso_rng_data __x86_get_k_vdso_rng_data
+
 /* The asm-generic header needs to be included after the definitions above */
 #include <asm-generic/vdso/vsyscall.h>
 
drivers/char/random.c +3 -2
index 87fe61295ea1..77968309e2c2 100644
@@ -59,6 +59,7 @@
 #ifdef CONFIG_VDSO_GETRANDOM
 #include <vdso/getrandom.h>
 #include <vdso/datapage.h>
+#include <vdso/vsyscall.h>
 #endif
 #include <asm/archrandom.h>
 #include <asm/processor.h>
@@ -282,7 +283,7 @@ static void crng_reseed(struct work_struct *work)
 	 * is ordered with the write above to base_crng.generation. Pairs with
 	 * the smp_rmb() before the syscall in the vDSO code.
 	 */
-	smp_store_release(&_vdso_rng_data.generation, next_gen + 1);
+	smp_store_release(&__arch_get_k_vdso_rng_data()->generation, next_gen + 1);
 #endif
 	if (!static_branch_likely(&crng_is_ready))
 		crng_init = CRNG_READY;
@@ -735,7 +736,7 @@ static void __cold _credit_init_bits(size_t bits)
 			queue_work(system_unbound_wq, &set_ready);
 		atomic_notifier_call_chain(&random_ready_notifier, 0, NULL);
 #ifdef CONFIG_VDSO_GETRANDOM
-		WRITE_ONCE(_vdso_rng_data.is_ready, true);
+		WRITE_ONCE(__arch_get_k_vdso_rng_data()->is_ready, true);
 #endif
 		wake_up_interruptible(&crng_init_wait);
 		kill_fasync(&fasync, SIGIO, POLL_IN);
Signature · 1 line
2.44.0
#12 lore raw

Re: [PATCH v2 03/17] vdso: Add __arch_get_k_vdso_rng_data()

Quoted 10 lines · Christophe Leroy

_vdso_data is specific to x86 and __arch_get_k_vdso_data() is provided so that all architectures can provide the requested pointer.

Do the same with _vdso_rng_data, provide __arch_get_k_vdso_rng_data() and don't use x86 _vdso_rng_data directly.

Until now vdso/vsyscall.h was only included by time/vsyscall.c but now it will also be included in char/random.c, leading to a duplicate declaration of _vdso_data and _vdso_rng_data.

Quoted 7 lines

To fix this issue, move declaration in a C file. vma.c looks like the most appropriate candidate. Don't need to replace the definitions in vsyscall.h by declarations as declarations are already in asm/vvar.h

---
v2: Move x86 DEFINE_VVAR(_vdso_data) and DEFINE_VVAR(_vdso_rng_data) in vma.c

Thanks, this seems sane to me. I'll apply this now to random.git as a fix; it should have been done this way before and is going to be a headache to coordinate later.

Jason

#13 lore raw

PATCH v2 4/17 vdso: Add missing c-getrandom-y in Makefile

In reply to Christophe Leroy

Same as for gettimeofday CVDSO implementation, add c-getrandom-y to ease the inclusion of lib/vdso/getrandom.c in architectures VDSO builds.

---
 lib/vdso/Makefile | 1 +
 1 file changed, 1 insertion(+)
lib/vdso/Makefile +1 -0
index 9f031eafc465..cedbf15f8087 100644
@@ -4,6 +4,7 @@ GENERIC_VDSO_MK_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
 GENERIC_VDSO_DIR := $(dir $(GENERIC_VDSO_MK_PATH))
 
 c-gettimeofday-$(CONFIG_GENERIC_GETTIMEOFDAY) := $(addprefix $(GENERIC_VDSO_DIR), gettimeofday.c)
+c-getrandom-$(CONFIG_VDSO_GETRANDOM) := $(addprefix $(GENERIC_VDSO_DIR), getrandom.c)
 
 # This cmd checks that the vdso library does not contain dynamic relocations.
 # It has to be called after the linking of the vdso library and requires it
Signature · 1 line
2.44.0
#14 lore raw

Re: [PATCH v2 04/17] vdso: Add missing c-getrandom-y in Makefile

Quoted 3 lines · Christophe Leroy

Same as for gettimeofday CVDSO implementation, add c-getrandom-y to ease the inclusion of lib/vdso/getrandom.c in architectures VDSO builds.

Thanks, seems straight forward. I'll apply this now as a fix.

#15 lore raw

PATCH v2 5/17 vdso: Avoid call to memset() by getrandom

In reply to Christophe Leroy

With the current implementation, __cvdso_getrandom_data() calls memset(), which is unexpected in the VDSO.

Rewrite opaque data initialisation to avoid memset().

---
 lib/vdso/getrandom.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
lib/vdso/getrandom.c +10 -5
index cab153c5f9be..4a56f45141b4 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/minmax.h>
+#include <linux/array_size.h>
 #include <vdso/datapage.h>
 #include <vdso/getrandom.h>
 #include <vdso/unaligned.h>
@@ -74,11 +75,15 @@ __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_
 	u32 counter[2] = { 0 };
 
 	if (unlikely(opaque_len == ~0UL && !buffer && !len && !flags)) {
-		*(struct vgetrandom_opaque_params *)opaque_state = (struct vgetrandom_opaque_params) {
-			.size_of_opaque_state = sizeof(*state),
-			.mmap_prot = PROT_READ | PROT_WRITE,
-			.mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS
-		};
+		struct vgetrandom_opaque_params *params = opaque_state;
+		int i;
+
+		params->size_of_opaque_state = sizeof(*state);
+		params->mmap_prot = PROT_READ | PROT_WRITE;
+		params->mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS;
+		for (i = 0; i < ARRAY_SIZE(params->reserved); i++)
+			params->reserved[i] = 0;
+
 		return 0;
 	}
 
Signature · 1 line
2.44.0
#16 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 4 lines · Christophe Leroy

With the current implementation, __cvdso_getrandom_data() calls memset(), which is unexpected in the VDSO.

Rewrite opaque data initialisation to avoid memset().

I think of the various ways I've seen of fixing this -- e.g. adding a memset() arch-by-arch -- this is the cleanest and simplest. I'll add this as a fix to random.git now.

Jason

#17 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

In reply to Christophe Leroy
Quoted 32 lines · Christophe Leroy

With the current implementation, __cvdso_getrandom_data() calls memset(), which is unexpected in the VDSO.

Rewrite opaque data initialisation to avoid memset().

---
 lib/vdso/getrandom.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
lib/vdso/getrandom.c +2 -5
index cab153c5f9be..4a56f45141b4 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/minmax.h>
+#include <linux/array_size.h>
 #include <vdso/datapage.h>
 #include <vdso/getrandom.h>
 #include <vdso/unaligned.h>
@@ -74,11 +75,15 @@ __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_
 	u32 counter[2] = { 0 };
 
 	if (unlikely(opaque_len == ~0UL && !buffer && !len && !flags)) {
-		*(struct vgetrandom_opaque_params *)opaque_state = (struct vgetrandom_opaque_params) {
-			.size_of_opaque_state = sizeof(*state),
-			.mmap_prot = PROT_READ | PROT_WRITE,
-			.mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS
-		};
+		struct vgetrandom_opaque_params *params = opaque_state;

lib/vdso/getrandom.c __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_ · line 88

Quoted 10 lines
lib/vdso/getrandom.c +8 -0
+		int i;
+
+		params->size_of_opaque_state = sizeof(*state);
+		params->mmap_prot = PROT_READ | PROT_WRITE;
+		params->mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS;
+		for (i = 0; i < ARRAY_SIZE(params->reserved); i++)
+			params->reserved[i] = 0;
+
 		return 0;
 	}

Is there a compiler flag that could be used to disable the generation of calls to memset?

- Eric

#18 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 38 lines · Eric Biggers
Quoted 37 lines · Christophe Leroy

With the current implementation, __cvdso_getrandom_data() calls memset(), which is unexpected in the VDSO.

Rewrite opaque data initialisation to avoid memset().

---
 lib/vdso/getrandom.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
lib/vdso/getrandom.c +7 -5
index cab153c5f9be..4a56f45141b4 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/minmax.h>
+#include <linux/array_size.h>
 #include <vdso/datapage.h>
 #include <vdso/getrandom.h>
 #include <vdso/unaligned.h>
@@ -74,11 +75,15 @@ __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_
 	u32 counter[2] = { 0 };
 
 	if (unlikely(opaque_len == ~0UL && !buffer && !len && !flags)) {
-		*(struct vgetrandom_opaque_params *)opaque_state = (struct vgetrandom_opaque_params) {
-			.size_of_opaque_state = sizeof(*state),
-			.mmap_prot = PROT_READ | PROT_WRITE,
-			.mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS
-		};
+		struct vgetrandom_opaque_params *params = opaque_state;
+		int i;
+
+		params->size_of_opaque_state = sizeof(*state);
+		params->mmap_prot = PROT_READ | PROT_WRITE;
+		params->mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS;
Quoted 8 lines

lib/vdso/getrandom.c __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_ · line 88

Quoted 5 lines
lib/vdso/getrandom.c +3 -0
+		for (i = 0; i < ARRAY_SIZE(params->reserved); i++)
+			params->reserved[i] = 0;
+
 		return 0;
 	}

Is there a compiler flag that could be used to disable the generation of calls to memset?

Apparently not: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90701

-ffreestanding disables the most but still can generate calls to memcpy and memset, and the bug was closed as "RESOLVED INVALID". Surprising, but maybe it's one of those "functions are part of language" things.

#19 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

In reply to Eric Biggers
Quoted 38 lines · Eric Biggers
Quoted 37 lines · Christophe Leroy

With the current implementation, __cvdso_getrandom_data() calls memset(), which is unexpected in the VDSO.

Rewrite opaque data initialisation to avoid memset().

---
 lib/vdso/getrandom.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
lib/vdso/getrandom.c +7 -5
index cab153c5f9be..4a56f45141b4 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/minmax.h>
+#include <linux/array_size.h>
 #include <vdso/datapage.h>
 #include <vdso/getrandom.h>
 #include <vdso/unaligned.h>
@@ -74,11 +75,15 @@ __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_
 	u32 counter[2] = { 0 };
 
 	if (unlikely(opaque_len == ~0UL && !buffer && !len && !flags)) {
-		*(struct vgetrandom_opaque_params *)opaque_state = (struct vgetrandom_opaque_params) {
-			.size_of_opaque_state = sizeof(*state),
-			.mmap_prot = PROT_READ | PROT_WRITE,
-			.mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS
-		};
+		struct vgetrandom_opaque_params *params = opaque_state;
+		int i;
+
+		params->size_of_opaque_state = sizeof(*state);
+		params->mmap_prot = PROT_READ | PROT_WRITE;
+		params->mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS;
Quoted 8 lines

lib/vdso/getrandom.c __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_ · line 88

Quoted 5 lines
lib/vdso/getrandom.c +3 -0
+		for (i = 0; i < ARRAY_SIZE(params->reserved); i++)
+			params->reserved[i] = 0;
+
 		return 0;
 	}

Is there a compiler flag that could be used to disable the generation of calls to memset?

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

Have fun,

Segher

#20 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 48 lines · Segher Boessenkool
Quoted 38 lines · Eric Biggers
Quoted 37 lines · Christophe Leroy

With the current implementation, __cvdso_getrandom_data() calls memset(), which is unexpected in the VDSO.

Rewrite opaque data initialisation to avoid memset().

---
 lib/vdso/getrandom.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
lib/vdso/getrandom.c +7 -5
index cab153c5f9be..4a56f45141b4 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/minmax.h>
+#include <linux/array_size.h>
 #include <vdso/datapage.h>
 #include <vdso/getrandom.h>
 #include <vdso/unaligned.h>
@@ -74,11 +75,15 @@ __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_
 	u32 counter[2] = { 0 };
 
 	if (unlikely(opaque_len == ~0UL && !buffer && !len && !flags)) {
-		*(struct vgetrandom_opaque_params *)opaque_state = (struct vgetrandom_opaque_params) {
-			.size_of_opaque_state = sizeof(*state),
-			.mmap_prot = PROT_READ | PROT_WRITE,
-			.mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS
-		};
+		struct vgetrandom_opaque_params *params = opaque_state;
+		int i;
+
+		params->size_of_opaque_state = sizeof(*state);
+		params->mmap_prot = PROT_READ | PROT_WRITE;
+		params->mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS;
Quoted 8 lines

lib/vdso/getrandom.c __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_ · line 88

Quoted 5 lines
lib/vdso/getrandom.c +3 -0
+		for (i = 0; i < ARRAY_SIZE(params->reserved); i++)
+			params->reserved[i] = 0;
+
 		return 0;
 	}

Is there a compiler flag that could be used to disable the generation of calls to memset?

Quoted 3 lines

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

maintainer
#21 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 6 lines · Jason A. Donenfeld
Quoted 5 lines · Segher Boessenkool
Quoted 3 lines · Eric Biggers

Is there a compiler flag that could be used to disable the generation of calls to memset?

Quoted 7 lines
Quoted 3 lines

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

Maybe -ffreestanding can help here? That should cause the vdso to be built with the assumption that there is no libc, so it would neither add nor remove standard library calls. Not sure if that causes other problems, e.g. if the calling conventions are different.

Arnd

#22 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 12 lines · Arnd Bergmann
Quoted 10 lines · Jason A. Donenfeld
Quoted 8 lines · Segher Boessenkool
Quoted 3 lines · Eric Biggers

Is there a compiler flag that could be used to disable the generation of calls to memset?

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

Quoted 8 lines
Quoted 3 lines

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

Maybe -ffreestanding can help here? That should cause the vdso to be built with the assumption that there is no libc, so it would neither add nor remove standard library calls. Not sure if that causes other problems, e.g. if the calling conventions are different.

From https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90701:

| You need -ffreestanding but that is documented to emit memset and memcpy still.

#23 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 22 lines · Jason A. Donenfeld
Quoted 20 lines · Arnd Bergmann
Quoted 6 lines · Jason A. Donenfeld
Quoted 5 lines · Segher Boessenkool
Quoted 3 lines · Eric Biggers

Is there a compiler flag that could be used to disable the generation of calls to memset?

Quoted 7 lines
Quoted 3 lines

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

Maybe -ffreestanding can help here? That should cause the vdso to be built with the assumption that there is no libc, so it would neither add nor remove standard library calls. Not sure if that causes other problems, e.g. if the calling conventions are different.

Quoted 4 lines
Quoted 1 line

From https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90701:

| You need -ffreestanding but that is documented to emit memset and memcpy still.

Yeah.

'-nostdlib'
Do not use the standard system startup files or libraries when linking.

This won't help a bit, the compiler will still optimise your initialisation loop to a memset() call, it just won't link to libgcc.a and crt*.o and its ilk (which is not where mem* are implemented in the first place!)

Segher

#24 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

In reply to Arnd Bergmann
Quoted 11 lines · Arnd Bergmann
Quoted 10 lines · Jason A. Donenfeld
Quoted 8 lines · Segher Boessenkool
Quoted 3 lines · Eric Biggers

Is there a compiler flag that could be used to disable the generation of calls to memset?

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

Quoted 8 lines
Quoted 3 lines

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

Maybe -ffreestanding can help here? That should cause the vdso to be built with the assumption that there is no libc, so it would neither add nor remove standard library calls. Not sure if that causes other problems, e.g. if the calling conventions are different.

"GCC requires the freestanding
environment provide 'memcpy', 'memmove', 'memset' and 'memcmp'."

This is precisely to implement things like struct initialisation. Maybe we should have a "-ffreeerstanding" or "-ffreefloating" or think of something funnier still environment as well, this problem has been there since the -ffreestanding flag has existed, but the problem is as old as the night.

-fno-builtin might help a bit more, but just attack the problem at its root, like I suggested?

(This isn't a new problem, originally it showed up as "GCC replaces (part of) my memcpy() implementation by a (recursive) call to memcpy()" and, well, that doesn't quite work!)

Segher

#25 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 25 lines · Segher Boessenkool
Quoted 11 lines · Arnd Bergmann
Quoted 10 lines · Jason A. Donenfeld
Quoted 8 lines · Segher Boessenkool
Quoted 3 lines · Eric Biggers

Is there a compiler flag that could be used to disable the generation of calls to memset?

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

Quoted 8 lines
Quoted 3 lines

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

Maybe -ffreestanding can help here? That should cause the vdso to be built with the assumption that there is no libc, so it would neither add nor remove standard library calls. Not sure if that causes other problems, e.g. if the calling conventions are different.

"GCC requires the freestanding
environment provide 'memcpy', 'memmove', 'memset' and 'memcmp'."

Quoted 9 lines

This is precisely to implement things like struct initialisation. Maybe we should have a "-ffreeerstanding" or "-ffreefloating" or think of something funnier still environment as well, this problem has been there since the -ffreestanding flag has existed, but the problem is as old as the night.

-fno-builtin might help a bit more, but just attack the problem at its root, like I suggested?

In my experience, this is likely to do the opposite: it causes the compiler to 'forget' the semantics of memcpy() and memset(), so that explicit trivial calls will no longer be elided and replaced with plain loads and stores (as it can no longer guarantee the equivalence)

Quoted 4 lines

(This isn't a new problem, originally it showed up as "GCC replaces (part of) my memcpy() implementation by a (recursive) call to memcpy()" and, well, that doesn't quite work!)

This needs to be fixed for Clang as well, so throwing GCC specific flags at it will at best be a partial solution.

Omitting the struct assignment is a reasonable way to reduce the likelihood that a memset() will be emitted, so for this patch

It is not a complete solution, unfortunately, and I guess there may be other situations (compiler/arch combinations) where this might pop up again.

#26 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Hi!

Quoted 33 lines · Ard Biesheuvel
Quoted 31 lines · Segher Boessenkool
Quoted 11 lines · Arnd Bergmann
Quoted 10 lines · Jason A. Donenfeld
Quoted 8 lines · Segher Boessenkool
Quoted 3 lines · Eric Biggers

Is there a compiler flag that could be used to disable the generation of calls to memset?

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

Quoted 8 lines
Quoted 3 lines

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

Maybe -ffreestanding can help here? That should cause the vdso to be built with the assumption that there is no libc, so it would neither add nor remove standard library calls. Not sure if that causes other problems, e.g. if the calling conventions are different.

"GCC requires the freestanding
environment provide 'memcpy', 'memmove', 'memset' and 'memcmp'."

This is precisely to implement things like struct initialisation. Maybe we should have a "-ffreeerstanding" or "-ffreefloating" or think of something funnier still environment as well, this problem has been there since the -ffreestanding flag has existed, but the problem is as old as the night.

Quoted 8 lines
Quoted 3 lines

-fno-builtin might help a bit more, but just attack the problem at its root, like I suggested?

In my experience, this is likely to do the opposite: it causes the compiler to 'forget' the semantics of memcpy() and memset(), so that explicit trivial calls will no longer be elided and replaced with plain loads and stores (as it can no longer guarantee the equivalence)

No, the compiler will never forget those semantics. But if you tell it your function named memset() is not the actual standard memset -- via -fno-builtin-memset for example -- the compiler won't optimise things involving it quite as much. You told it so eh?

You can also tell it not to have a __builtin_memset function, but in this particular case that won;t quite work, since the compiler does need to have that builtin available to do struct and array initialisations and the like.

Quoted 7 lines
Quoted 4 lines

(This isn't a new problem, originally it showed up as "GCC replaces (part of) my memcpy() implementation by a (recursive) call to memcpy()" and, well, that doesn't quite work!)

This needs to be fixed for Clang as well, so throwing GCC specific flags at it will at best be a partial solution.

clang says it is a 100% plug-in replacement for GCC, so they will have to accept all GCC flags. And in many cases they do. Cases where they don't are bugs.

Quoted 3 lines

It is not a complete solution, unfortunately, and I guess there may be other situations (compiler/arch combinations) where this might pop up again.

Why do mem* not work in VDSOs? Fix that, and all these problems disappear, and you do not need workrarounds :-)

Segher

#27 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 46 lines · Segher Boessenkool

Hi!

Quoted 33 lines · Ard Biesheuvel
Quoted 31 lines · Segher Boessenkool
Quoted 11 lines · Arnd Bergmann
Quoted 10 lines · Jason A. Donenfeld
Quoted 8 lines · Segher Boessenkool
Quoted 3 lines · Eric Biggers

Is there a compiler flag that could be used to disable the generation of calls to memset?

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

Quoted 8 lines
Quoted 3 lines

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

Maybe -ffreestanding can help here? That should cause the vdso to be built with the assumption that there is no libc, so it would neither add nor remove standard library calls. Not sure if that causes other problems, e.g. if the calling conventions are different.

"GCC requires the freestanding
environment provide 'memcpy', 'memmove', 'memset' and 'memcmp'."

This is precisely to implement things like struct initialisation. Maybe we should have a "-ffreeerstanding" or "-ffreefloating" or think of something funnier still environment as well, this problem has been there since the -ffreestanding flag has existed, but the problem is as old as the night.

Quoted 8 lines
Quoted 3 lines

-fno-builtin might help a bit more, but just attack the problem at its root, like I suggested?

In my experience, this is likely to do the opposite: it causes the compiler to 'forget' the semantics of memcpy() and memset(), so that explicit trivial calls will no longer be elided and replaced with plain loads and stores (as it can no longer guarantee the equivalence)

Quoted 5 lines

No, the compiler will never forget those semantics. But if you tell it your function named memset() is not the actual standard memset -- via -fno-builtin-memset for example -- the compiler won't optimise things involving it quite as much. You told it so eh?

That is exactly the point I am making. So how would this help in this case?

Quoted 10 lines

You can also tell it not to have a __builtin_memset function, but in this particular case that won;t quite work, since the compiler does need to have that builtin available to do struct and array initialisations and the like.

Quoted 5 lines
Quoted 4 lines

(This isn't a new problem, originally it showed up as "GCC replaces (part of) my memcpy() implementation by a (recursive) call to memcpy()" and, well, that doesn't quite work!)

Quoted 7 lines
Quoted 2 lines

This needs to be fixed for Clang as well, so throwing GCC specific flags at it will at best be a partial solution.

clang says it is a 100% plug-in replacement for GCC, so they will have to accept all GCC flags. And in many cases they do. Cases where they don't are bugs.

Even if this were true, we support Clang versions today that do not support -fno-tree-loop-distribute-patterns so my point stands.

Quoted 7 lines
Quoted 3 lines

It is not a complete solution, unfortunately, and I guess there may be other situations (compiler/arch combinations) where this might pop up again.

Why do mem* not work in VDSOs? Fix that, and all these problems disappear, and you do not need workrarounds :-)

Good point. We should just import memcpy and memset in the VDSO ELF metadata.

Not sure about static binaries, though: do those even use the VDSO?

#28 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 7 lines · Ard Biesheuvel
Quoted 5 lines · Segher Boessenkool
Quoted 4 lines

In my experience, this is likely to do the opposite: it causes the compiler to 'forget' the semantics of memcpy() and memset(), so that explicit trivial calls will no longer be elided and replaced with plain loads and stores (as it can no longer guarantee the equivalence)

Quoted 6 lines
Quoted 4 lines

No, the compiler will never forget those semantics. But if you tell it your function named memset() is not the actual standard memset -- via -fno-builtin-memset for example -- the compiler won't optimise things involving it quite as much. You told it so eh?

That is exactly the point I am making. So how would this help in this case?

I think we agree? :-)

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

This needs to be fixed for Clang as well, so throwing GCC specific flags at it will at best be a partial solution.

clang says it is a 100% plug-in replacement for GCC, so they will have to accept all GCC flags. And in many cases they do. Cases where they don't are bugs.

Even if this were true, we support Clang versions today that do not support -fno-tree-loop-distribute-patterns so my point stands.

It is true. Yes, this cause problems for their users.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

It is not a complete solution, unfortunately, and I guess there may be other situations (compiler/arch combinations) where this might pop up again.

Why do mem* not work in VDSOs? Fix that, and all these problems disappear, and you do not need workrarounds :-)

Good point. We should just import memcpy and memset in the VDSO ELF metadata.

Yeah. In many cases GCC will replace such calls by (faster and/or smaller) inline code anyway, but when it does leave a call, there needs to be an external function implementing it :-)

Quoted 1 line

Not sure about static binaries, though: do those even use the VDSO?

With "static binary" people usually mean "a binary not using any DSOs", I think the VDSO is a DSO, also in this respect? As always, -static builds are *way* less problematic (and faster and smaller :-) )

Segher

#29 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Le 28/08/2024 à 19:25, Segher Boessenkool a écrit :

Quoted 7 lines
Quoted 2 lines

Not sure about static binaries, though: do those even use the VDSO?

With "static binary" people usually mean "a binary not using any DSOs", I think the VDSO is a DSO, also in this respect? As always, -static builds are *way* less problematic (and faster and smaller :-) )

AFAIK on powerpc even static binaries use the vDSO, otherwise signals don't work.

Christophe

#30 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 6 lines · Christophe Leroy

Le 28/08/2024 à 19:25, Segher Boessenkool a écrit :

Quoted 3 lines
Quoted 2 lines

Not sure about static binaries, though: do those even use the VDSO?

Quoted 7 lines
Quoted 4 lines

With "static binary" people usually mean "a binary not using any DSOs", I think the VDSO is a DSO, also in this respect? As always, -static builds are *way* less problematic (and faster and smaller :-) )

AFAIK on powerpc even static binaries use the vDSO, otherwise signals don't work.

How can that work? Non-dynamic binaries do not use ld.so (that is the definition of a dynamic binary, even). So they cannot link (at runtime) to any DSO (unless that is done manually?!)

Maybe there is something at a fixed offset in the vDSO, or something like that? Is this documented somewhere?

Segher

#31 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Le 29/08/2024 à 20:02, Segher Boessenkool a écrit :

Quoted 15 lines · Christophe Leroy
Quoted 6 lines

Le 28/08/2024 à 19:25, Segher Boessenkool a écrit :

Quoted 3 lines
Quoted 2 lines

Not sure about static binaries, though: do those even use the VDSO?

Quoted 7 lines
Quoted 4 lines

With "static binary" people usually mean "a binary not using any DSOs", I think the VDSO is a DSO, also in this respect? As always, -static builds are *way* less problematic (and faster and smaller :-) )

AFAIK on powerpc even static binaries use the vDSO, otherwise signals don't work.

Quoted 7 lines

How can that work? Non-dynamic binaries do not use ld.so (that is the definition of a dynamic binary, even). So they cannot link (at runtime) to any DSO (unless that is done manually?!)

Maybe there is something at a fixed offset in the vDSO, or something like that? Is this documented somewhere?

You've got some explanation here : https://github.com/torvalds/linux/blob/master/Documentation/ABI/stable/vdso

#32 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

In reply to Segher Boessenkool
Quoted 12 lines · Segher Boessenkool
Quoted 11 lines · Christophe Leroy

Le 28/08/2024 à 19:25, Segher Boessenkool a écrit :

Quoted 7 lines
Quoted 2 lines

Not sure about static binaries, though: do those even use the VDSO?

With "static binary" people usually mean "a binary not using any DSOs", I think the VDSO is a DSO, also in this respect? As always, -static builds are *way* less problematic (and faster and smaller :-) )

Quoted 6 lines
Quoted 2 lines

AFAIK on powerpc even static binaries use the vDSO, otherwise signals don't work.

How can that work? Non-dynamic binaries do not use ld.so (that is the definition of a dynamic binary, even). So they cannot link (at runtime) to any DSO (unless that is done manually?!)

At least for signals I don't think the application needs to know anything about the VDSO. The kernel sets up the return to the signal trampoline (in the VDSO), and as long as userspace returns from its signal handler with blr it will land back on the trampoline.

cheers

#33 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

In reply to Jason A. Donenfeld
Quoted 4 lines · Jason A. Donenfeld
Quoted 3 lines · Segher Boessenkool
Quoted 2 lines · Eric Biggers

lib/vdso/getrandom.c __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_ · line 85

Quoted 2 lines
lib/vdso/getrandom.c +2 -0
+		for (i = 0; i < ARRAY_SIZE(params->reserved); i++)
+			params->reserved[i] = 0;

This is a loop. With -ftree-loop-distribute-patterns, the default at -O2, this is optimised to

    memset(params->reserved, 0, ...);

(which is perfectly fine, since memset is required to be there even for freestanding environments, this is documented!)

Quoted 7 lines
Quoted 3 lines

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

It very much *does* help. Try harder? Maybe you typoed?

Segher

#34 lore raw

Re: [PATCH v2 05/17] vdso: Avoid call to memset() by getrandom

Quoted 18 lines · Segher Boessenkool
Quoted 4 lines · Jason A. Donenfeld
Quoted 3 lines · Segher Boessenkool
Quoted 2 lines · Eric Biggers

lib/vdso/getrandom.c __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_ · line 85

Quoted 2 lines
lib/vdso/getrandom.c +2 -0
+		for (i = 0; i < ARRAY_SIZE(params->reserved); i++)
+			params->reserved[i] = 0;

This is a loop. With -ftree-loop-distribute-patterns, the default at -O2, this is optimised to

    memset(params->reserved, 0, ...);

(which is perfectly fine, since memset is required to be there even for freestanding environments, this is documented!)

Quoted 4 lines
Quoted 3 lines

-fno-tree-loop-distribute-patterns . But, as always, read up on it, see what it actually does (and how it avoids your problem, and mostly: learn what the actual problem *was*!)

Quoted 5 lines
Quoted 3 lines

This might help with various loops, but it doesn't help with the matter that this patch fixes, which is struct initialization. I just tried it with the arm64 patch to no avail.

It very much *does* help. Try harder? Maybe you typoed?

Scroll up and reread the original patch. You are confused. The loop is what fixes the matter. It's struct initialization that generates the memset.

#35 lore raw

PATCH v2 6/17 vdso: Change getrandom's generation to unsigned long

In reply to Christophe Leroy

Performing SMP atomic operations on u64 fails on powerpc32.

Random driver generation is handled as unsigned long not u64, see for instance base_cnrg or struct crng.

Use the same type for vDSO's getrandom as it gets copied from the above. This is also in line with the local current_generation which is already an unsigned long.

---
 include/vdso/datapage.h  | 2 +-
 include/vdso/getrandom.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/vdso/datapage.h +1 -1
index b85f24cac3f5..00b66a9b5778 100644
@@ -123,7 +123,7 @@ struct vdso_data {
  * @is_ready:	boolean signaling whether the RNG is initialized
  */
 struct vdso_rng_data {
-	u64	generation;
+	unsigned long	generation;
 	u8	is_ready;
 };
 
include/vdso/getrandom.h +1 -1
index a8b7c14b0ae0..36fd166bf330 100644
@@ -38,7 +38,7 @@ struct vgetrandom_state {
 		};
 		u8		batch_key[CHACHA_BLOCK_SIZE * 2];
 	};
-	u64			generation;
+	unsigned long		generation;
 	u8			pos;
 	bool 			in_use;
 };
Signature · 1 line
2.44.0
#36 lore raw

Re: [PATCH v2 06/17] vdso: Change getrandom's generation to unsigned long

Quoted 8 lines · Christophe Leroy

Performing SMP atomic operations on u64 fails on powerpc32.

Random driver generation is handled as unsigned long not u64, see for instance base_cnrg or struct crng.

Use the same type for vDSO's getrandom as it gets copied from the above. This is also in line with the local current_generation which is already an unsigned long.

This isn't going to work when 32-bit userspace tries to access a 64-bit kernel.

I had "fixed" this with a vdso_kernel_ulong type way back in an earlier
version: https://lore.kernel.org/lkml/[email protected]/#Z31include:vdso:types.h

But tglx pointed out in that thread that this actually isn't necessary:

| All of this is pointless because if a 32-bit application runs on a
| 64-bit kernel it has to use the 64-bit 'generation'. So why on earth do
| we need magic here for a 32-bit kernel?
| 
| Just use u64 for both and spare all this voodoo. We're seriously not
| "optimizing" for 32-bit kernels.
|
| All what happens on a 32-bit kernel is that the RNG will store the
| unsigned long (32bit) generation into a 64bit variable:
| 
| 	smp_store_release(&_vdso_rng_data.generation, next_gen + 1);
| 
| As the upper 32bit are always zero, there is no issue vs. load store
| tearing at all. So there is zero benefit for this aside of slightly
| "better" user space code when running on a 32-bit kernel. Who cares?

So I just got rid of it and used a u64 as he suggested.

However, there's also an additional reason why it's not worth churning further over this - because VM_DROPPABLE is 64-bit only (due to flags in vma bits), likely so is vDSO getrandom() for the time being. So I think it makes more sense to retool this series to be ppc64, and then if you really really want 32-bit and can convince folks it matters, then all of these parts (for example, here, the fact that the smp helper doesn't want to tear) can be fixed up in a separate series.

Jason

#37 lore raw

Re: [PATCH v2 06/17] vdso: Change getrandom's generation to unsigned long

Le 26/08/2024 à 09:50, Jason A. Donenfeld a écrit :

Quoted 36 lines · Christophe Leroy
Quoted 8 lines

Performing SMP atomic operations on u64 fails on powerpc32.

Random driver generation is handled as unsigned long not u64, see for instance base_cnrg or struct crng.

Use the same type for vDSO's getrandom as it gets copied from the above. This is also in line with the local current_generation which is already an unsigned long.

This isn't going to work when 32-bit userspace tries to access a 64-bit kernel.

I had "fixed" this with a vdso_kernel_ulong type way back in an earlier
version: https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Flkml%2F20240528122352.2485958-5-Jason%40zx2c4.com%2F%23Z31include%3Avdso%3Atypes.h&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7C41747dd989164267c1cc08dcc5a3c424%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638602554376441761%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=Tf9ShSN6aOOFZ1HymAmHhj0xhQ6BUtHJX95t50gsp9k%3D&reserved=0

But tglx pointed out in that thread that this actually isn't necessary:

| All of this is pointless because if a 32-bit application runs on a
| 64-bit kernel it has to use the 64-bit 'generation'. So why on earth do
| we need magic here for a 32-bit kernel?
|
| Just use u64 for both and spare all this voodoo. We're seriously not
| "optimizing" for 32-bit kernels.
|
| All what happens on a 32-bit kernel is that the RNG will store the
| unsigned long (32bit) generation into a 64bit variable:
|
| 	smp_store_release(&_vdso_rng_data.generation, next_gen + 1);
|
| As the upper 32bit are always zero, there is no issue vs. load store
| tearing at all. So there is zero benefit for this aside of slightly
| "better" user space code when running on a 32-bit kernel. Who cares?

So I just got rid of it and used a u64 as he suggested.

Quoted 7 lines

However, there's also an additional reason why it's not worth churning further over this - because VM_DROPPABLE is 64-bit only (due to flags in vma bits), likely so is vDSO getrandom() for the time being. So I think it makes more sense to retool this series to be ppc64, and then if you really really want 32-bit and can convince folks it matters, then all of these parts (for example, here, the fact that the smp helper doesn't want to tear) can be fixed up in a separate series.

So yes I really really want it on ppc32 because this is the only type of boards I have and this is really were we need getrandom() to be optimised, indeed ppc64 was sherry-on-the-cake in my series, I just added it because it was easy to do after doing ppc32.

Christophe

#38 lore raw

Re: [PATCH v2 06/17] vdso: Change getrandom's generation to unsigned long

Quoted 47 lines · Christophe Leroy

Le 26/08/2024 à 09:50, Jason A. Donenfeld a écrit :

Quoted 36 lines · Christophe Leroy
Quoted 8 lines

Performing SMP atomic operations on u64 fails on powerpc32.

Random driver generation is handled as unsigned long not u64, see for instance base_cnrg or struct crng.

Use the same type for vDSO's getrandom as it gets copied from the above. This is also in line with the local current_generation which is already an unsigned long.

This isn't going to work when 32-bit userspace tries to access a 64-bit kernel.

I had "fixed" this with a vdso_kernel_ulong type way back in an earlier
version: https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Flkml%2F20240528122352.2485958-5-Jason%40zx2c4.com%2F%23Z31include%3Avdso%3Atypes.h&data=05%7C02%7Cchristophe.leroy%40csgroup.eu%7C41747dd989164267c1cc08dcc5a3c424%7C8b87af7d86474dc78df45f69a2011bb5%7C0%7C0%7C638602554376441761%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=Tf9ShSN6aOOFZ1HymAmHhj0xhQ6BUtHJX95t50gsp9k%3D&reserved=0

But tglx pointed out in that thread that this actually isn't necessary:

| All of this is pointless because if a 32-bit application runs on a
| 64-bit kernel it has to use the 64-bit 'generation'. So why on earth do
| we need magic here for a 32-bit kernel?
|
| Just use u64 for both and spare all this voodoo. We're seriously not
| "optimizing" for 32-bit kernels.
|
| All what happens on a 32-bit kernel is that the RNG will store the
| unsigned long (32bit) generation into a 64bit variable:
|
| 	smp_store_release(&_vdso_rng_data.generation, next_gen + 1);
|
| As the upper 32bit are always zero, there is no issue vs. load store
| tearing at all. So there is zero benefit for this aside of slightly
| "better" user space code when running on a 32-bit kernel. Who cares?

So I just got rid of it and used a u64 as he suggested.

Quoted 7 lines

However, there's also an additional reason why it's not worth churning further over this - because VM_DROPPABLE is 64-bit only (due to flags in vma bits), likely so is vDSO getrandom() for the time being. So I think it makes more sense to retool this series to be ppc64, and then if you really really want 32-bit and can convince folks it matters, then all of these parts (for example, here, the fact that the smp helper doesn't want to tear) can be fixed up in a separate series.

Quoted 4 lines

So yes I really really want it on ppc32 because this is the only type of boards I have and this is really were we need getrandom() to be optimised, indeed ppc64 was sherry-on-the-cake in my series, I just added it because it was easy to do after doing ppc32.

I saw that you did in fact find a bit on ppc32 for VM_DROPPABLE. So it looks at least possible. Because of this generation counter business, I still think it might make sense to do in two steps, though, first doing 64-bit, and then doing 32-bit after.

As for the generation counter error you're seeing, I guess what we want is smp_store_release memory ordering semantics, but letting tearing happen (since the upper 32-bits will be zero anyway). I'm not sure the best way to do this, whether it's a new helper, or doing a WRITE_ONCE together with an smp barrier, or what. But I imagine it's something like that.

Jason

#39 lore raw

Re: [PATCH v2 06/17] vdso: Change getrandom's generation to unsigned long

In reply to Christophe Leroy
Quoted 29 lines · Christophe Leroy

Le 26/08/2024 à 09:50, Jason A. Donenfeld a écrit :

Quoted 20 lines

But tglx pointed out in that thread that this actually isn't necessary:

| All of this is pointless because if a 32-bit application runs on a
| 64-bit kernel it has to use the 64-bit 'generation'. So why on earth do
| we need magic here for a 32-bit kernel?
|
| Just use u64 for both and spare all this voodoo. We're seriously not
| "optimizing" for 32-bit kernels.
|
| All what happens on a 32-bit kernel is that the RNG will store the
| unsigned long (32bit) generation into a 64bit variable:
|
| 	smp_store_release(&_vdso_rng_data.generation, next_gen + 1);
|
| As the upper 32bit are always zero, there is no issue vs. load store
| tearing at all. So there is zero benefit for this aside of slightly
| "better" user space code when running on a 32-bit kernel. Who cares?

So I just got rid of it and used a u64 as he suggested.

Quoted 7 lines

However, there's also an additional reason why it's not worth churning further over this - because VM_DROPPABLE is 64-bit only (due to flags in vma bits), likely so is vDSO getrandom() for the time being. So I think it makes more sense to retool this series to be ppc64, and then if you really really want 32-bit and can convince folks it matters, then all of these parts (for example, here, the fact that the smp helper doesn't want to tear) can be fixed up in a separate series.

Quoted 3 lines

So yes I really really want it on ppc32 because this is the only type of boards I have and this is really were we need getrandom() to be optimised,

For nostalgic reasons?

Quoted 2 lines

indeed ppc64 was sherry-on-the-cake in my series, I just added it because it was easy to do after doing ppc32.

The rng problem for ppc32 seems to be:

   smp_store_release(&_vdso_rng_data.generation, next_gen + 1);

right?

Your proposed type change creates inconsistency for 32-bit userspace running on 64-bit kernels because the data struct layout changes.

As explained before, there is no problem with store or load tearing on 32bit systems because the generation counter is only 32bit wide. So the obvious solution is to only update 32 bits on a 32bit kernel:

drivers/char/random.c +1 -1
@@ -282,7 +282,7 @@ static void crng_reseed(struct work_stru
 	 * is ordered with the write above to base_crng.generation. Pairs with
 	 * the smp_rmb() before the syscall in the vDSO code.
 	 */
-	smp_store_release(&_vdso_rng_data.generation, next_gen + 1);
+	smp_store_release((unsigned long *)&_vdso_rng_data.generation, next_gen + 1);
 #endif
 	if (!static_branch_likely(&crng_is_ready))
 		crng_init = CRNG_READY;

Which is perfectly fine on 32-bit independent of endianess because the user space side does READ_ONCE(data->generation) and the read value is solely used for comparison so it does not matter at all whether the generation information is in the upper or the lower 32bit of the u64.

No?

But that's a trivial fix compared to making VM_DROPPABLE work on 32-bit correclty. :)

Thanks,

tglx

#40 lore raw

Re: [PATCH v2 06/17] vdso: Change getrandom's generation to unsigned long

Quoted 7 lines · Thomas Gleixner

As explained before, there is no problem with store or load tearing on 32bit systems because the generation counter is only 32bit wide. So the obvious solution is to only update 32 bits on a 32bit kernel:

drivers/char/random.c static void crng_reseed(struct work_stru · line 288

Quoted 8 lines
drivers/char/random.c +1 -1
@@ -282,7 +282,7 @@ static void crng_reseed(struct work_stru
 	 * is ordered with the write above to base_crng.generation. Pairs with
 	 * the smp_rmb() before the syscall in the vDSO code.
 	 */
-	smp_store_release(&_vdso_rng_data.generation, next_gen + 1);
+	smp_store_release((unsigned long *)&_vdso_rng_data.generation, next_gen + 1);
 #endif
 	if (!static_branch_likely(&crng_is_ready))
 		crng_init = CRNG_READY;

That seems like a pretty clean fix.

Quoted 2 lines

But that's a trivial fix compared to making VM_DROPPABLE work on 32-bit correclty. :)

My initial response too, and then I noticed he posted this:
https://lore.kernel.org/all/315e3a268b165b6edad7dcb723b0d8a506a56c4e.1724309198.git.christophe.leroy@csgroup.eu/
If that's correct, maybe it's not so bad, at least here. I haven't yet looked into the details of it.

Jason

#41 lore raw

Re: [PATCH v2 06/17] vdso: Change getrandom's generation to unsigned long

In reply to Thomas Gleixner

Le 26/08/2024 à 11:43, Thomas Gleixner a écrit :

Quoted 30 lines · Christophe Leroy
Quoted 29 lines

Le 26/08/2024 à 09:50, Jason A. Donenfeld a écrit :

Quoted 27 lines

But tglx pointed out in that thread that this actually isn't necessary:

| All of this is pointless because if a 32-bit application runs on a
| 64-bit kernel it has to use the 64-bit 'generation'. So why on earth do
| we need magic here for a 32-bit kernel?
|
| Just use u64 for both and spare all this voodoo. We're seriously not
| "optimizing" for 32-bit kernels.
|
| All what happens on a 32-bit kernel is that the RNG will store the
| unsigned long (32bit) generation into a 64bit variable:
|
| 	smp_store_release(&_vdso_rng_data.generation, next_gen + 1);
|
| As the upper 32bit are always zero, there is no issue vs. load store
| tearing at all. So there is zero benefit for this aside of slightly
| "better" user space code when running on a 32-bit kernel. Who cares?

So I just got rid of it and used a u64 as he suggested.

However, there's also an additional reason why it's not worth churning further over this - because VM_DROPPABLE is 64-bit only (due to flags in vma bits), likely so is vDSO getrandom() for the time being. So I think it makes more sense to retool this series to be ppc64, and then if you really really want 32-bit and can convince folks it matters, then all of these parts (for example, here, the fact that the smp helper doesn't want to tear) can be fixed up in a separate series.

Quoted 5 lines
Quoted 3 lines

So yes I really really want it on ppc32 because this is the only type of boards I have and this is really were we need getrandom() to be optimised,

For nostalgic reasons?

No nostalgy here. We deliver voice communication systems for air trafic control that we are commited to maintain for at least the next 15 years.

The MPC 885 was manufactured by NXP until recently and we have several hundreds in stock in case one of our customer needs to extend an existing system.

The MPC 8321 is still manufactured by NXP and the boards on new systems we deliver use that CPU.

Both are 32 bits powerpc.

Quoted 9 lines
Quoted 3 lines

indeed ppc64 was sherry-on-the-cake in my series, I just added it because it was easy to do after doing ppc32.

The rng problem for ppc32 seems to be:

    smp_store_release(&_vdso_rng_data.generation, next_gen + 1);

right?

right.

Quoted 20 lines

Your proposed type change creates inconsistency for 32-bit userspace running on 64-bit kernels because the data struct layout changes.

As explained before, there is no problem with store or load tearing on 32bit systems because the generation counter is only 32bit wide. So the obvious solution is to only update 32 bits on a 32bit kernel:

drivers/char/random.c +1 -1
@@ -282,7 +282,7 @@ static void crng_reseed(struct work_stru
  	 * is ordered with the write above to base_crng.generation. Pairs with
  	 * the smp_rmb() before the syscall in the vDSO code.
  	 */
-	smp_store_release(&_vdso_rng_data.generation, next_gen + 1);
+	smp_store_release((unsigned long *)&_vdso_rng_data.generation, next_gen + 1);
  #endif
  	if (!static_branch_likely(&crng_is_ready))
  		crng_init = CRNG_READY;
Quoted 5 lines

Which is perfectly fine on 32-bit independent of endianess because the user space side does READ_ONCE(data->generation) and the read value is solely used for comparison so it does not matter at all whether the generation information is in the upper or the lower 32bit of the u64. > No?

Yes that looks ok, it will only require a big fat comment to explain that. I'll give it a try.

Quoted 3 lines

But that's a trivial fix compared to making VM_DROPPABLE work on 32-bit correclty. :)

Euh ... Ok. What's the problem here ? I have used VM_ARCH_1 which is available. Is there more to do ?

Thanks
Christophe

#42 lore raw

PATCH v2 7/17 mm: Define VM_DROPPABLE for powerpc/32

In reply to Christophe Leroy

Commit 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings") only adds VM_DROPPABLE for 64 bits architectures.

In order to also use the getrandom vDSO implementation on powerpc/32, use VM_ARCH_1 for VM_DROPPABLE on powerpc/32. This is possible because VM_ARCH_1 is used for VM_SAO on powerpc and VM_SAO is only for powerpc/64.

---
 fs/proc/task_mmu.c             | 4 +++-
 include/linux/mm.h             | 4 +++-
 include/trace/events/mmflags.h | 4 ++--
 3 files changed, 8 insertions(+), 4 deletions(-)
fs/proc/task_mmu.c +3 -1
index 5f171ad7b436..4a3fe961cbf6 100644
@@ -987,8 +987,10 @@ static void show_smap_vma_flags(struct seq_file *m, struct vm_area_struct *vma)
 #ifdef CONFIG_X86_USER_SHADOW_STACK
 		[ilog2(VM_SHADOW_STACK)] = "ss",
 #endif
-#ifdef CONFIG_64BIT
+#if VM_DROPPABLE != VM_NONE
 		[ilog2(VM_DROPPABLE)] = "dp",
+#endif
+#ifdef CONFIG_64BIT
 		[ilog2(VM_SEALED)] = "sl",
 #endif
 	};
include/linux/mm.h +3 -1
index c4b238a20b76..865d3e21ee5e 100644
@@ -359,7 +359,7 @@ extern unsigned int kobjsize(const void *objp);
 
 #if defined(CONFIG_X86)
 # define VM_PAT		VM_ARCH_1	/* PAT reserves whole VMA at once (x86) */
-#elif defined(CONFIG_PPC)
+#elif defined(CONFIG_PPC64)
 # define VM_SAO		VM_ARCH_1	/* Strong Access Ordering (powerpc) */
 #elif defined(CONFIG_PARISC)
 # define VM_GROWSUP	VM_ARCH_1
@@ -409,6 +409,8 @@ extern unsigned int kobjsize(const void *objp);
 #ifdef CONFIG_64BIT
 #define VM_DROPPABLE_BIT	40
 #define VM_DROPPABLE		BIT(VM_DROPPABLE_BIT)
+#elif defined(CONFIG_PPC32)
+#define VM_DROPPABLE		VM_ARCH_1
 #else
 #define VM_DROPPABLE		VM_NONE
 #endif
include/trace/events/mmflags.h +2 -2
index b63d211bd141..9820cbfbcb14 100644
@@ -143,7 +143,7 @@ IF_HAVE_PG_ARCH_X(arch_3)
 
 #if defined(CONFIG_X86)
 #define __VM_ARCH_SPECIFIC_1 {VM_PAT,     "pat"           }
-#elif defined(CONFIG_PPC)
+#elif defined(CONFIG_PPC64)
 #define __VM_ARCH_SPECIFIC_1 {VM_SAO,     "sao"           }
 #elif defined(CONFIG_PARISC)
 #define __VM_ARCH_SPECIFIC_1 {VM_GROWSUP,	"growsup"	}
@@ -165,7 +165,7 @@ IF_HAVE_PG_ARCH_X(arch_3)
 # define IF_HAVE_UFFD_MINOR(flag, name)
 #endif
 
-#ifdef CONFIG_64BIT
+#if VM_DROPPABLE != VM_NONE
 # define IF_HAVE_VM_DROPPABLE(flag, name) {flag, name},
 #else
 # define IF_HAVE_VM_DROPPABLE(flag, name)
Signature · 1 line
2.44.0
#43 lore raw

Re: [PATCH v2 07/17] mm: Define VM_DROPPABLE for powerpc/32

kernel test robot noticed the following build warnings:

Hi Christophe,

kernel test robot noticed the following build warnings:

[auto build test WARNING on powerpc/next]
[also build test WARNING on powerpc/fixes shuah-kselftest/next shuah-kselftest/fixes crng-random/master linus/master v6.11-rc5 next-20240823] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Christophe-Leroy/asm-generic-unaligned-h-Extract-common-header-for-vDSO/20240826-103525
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
patch link:    https://lore.kernel.org/r/315e3a268b165b6edad7dcb723b0d8a506a56c4e.1724309198.git.christophe.leroy%40csgroup.eu
patch subject: [PATCH v2 07/17] mm: Define VM_DROPPABLE for powerpc/32
config: x86_64-buildonly-randconfig-002-20240826 (https://download.01.org/0day-ci/archive/20240826/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240826/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from include/trace/events/page_ref.h:11,

                    from mm/debug_page_ref.c:6:
>> include/trace/events/mmflags.h:168:5: warning: "VM_DROPPABLE" is not defined, evaluates to 0 [-Wundef]
     168 | #if VM_DROPPABLE != VM_NONE
         |     ^~~~~~~~~~~~
>> include/trace/events/mmflags.h:168:21: warning: "VM_NONE" is not defined, evaluates to 0 [-Wundef]
     168 | #if VM_DROPPABLE != VM_NONE
         |                     ^~~~~~~
   In file included from include/trace/events/page_ref.h:11,
                    from include/trace/define_trace.h:95,
                    from include/trace/events/page_ref.h:135:
>> include/trace/events/mmflags.h:168:5: warning: "VM_DROPPABLE" is not defined, evaluates to 0 [-Wundef]
     168 | #if VM_DROPPABLE != VM_NONE
         |     ^~~~~~~~~~~~
>> include/trace/events/mmflags.h:168:21: warning: "VM_NONE" is not defined, evaluates to 0 [-Wundef]
     168 | #if VM_DROPPABLE != VM_NONE
         |                     ^~~~~~~
   In file included from include/trace/events/page_ref.h:11,
                    from include/trace/trace_events.h:94,
                    from include/trace/define_trace.h:102:
>> include/trace/events/mmflags.h:169: warning: "IF_HAVE_VM_DROPPABLE" redefined

169 | # define IF_HAVE_VM_DROPPABLE(flag, name) {flag, name},
|
include/trace/events/mmflags.h:171: note: this is the location of the previous definition
171 | # define IF_HAVE_VM_DROPPABLE(flag, name)
|

vim +/VM_DROPPABLE +168 include/trace/events/mmflags.h

167

 > 168	#if VM_DROPPABLE != VM_NONE
 > 169	# define IF_HAVE_VM_DROPPABLE(flag, name) {flag, name},

170 #else
171 # define IF_HAVE_VM_DROPPABLE(flag, name)
172 #endif
173

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#44 lore raw

Re: [PATCH v2 07/17] mm: Define VM_DROPPABLE for powerpc/32

In reply to Christophe Leroy
kernel test robot noticed the following build warnings:

Hi Christophe,

kernel test robot noticed the following build warnings:

[auto build test WARNING on powerpc/next]
[also build test WARNING on powerpc/fixes shuah-kselftest/next shuah-kselftest/fixes linus/master v6.11-rc5 next-20240823] [cannot apply to crng-random/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Christophe-Leroy/asm-generic-unaligned-h-Extract-common-header-for-vDSO/20240826-103525
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
patch link:    https://lore.kernel.org/r/315e3a268b165b6edad7dcb723b0d8a506a56c4e.1724309198.git.christophe.leroy%40csgroup.eu
patch subject: [PATCH v2 07/17] mm: Define VM_DROPPABLE for powerpc/32
config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20240826/[email protected]/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240826/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from mm/debug_page_ref.c:6:
In file included from include/trace/events/page_ref.h:11:
include/trace/events/mmflags.h:168:5: warning: 'VM_DROPPABLE' is not defined, evaluates to 0 [-Wundef]

     168 | #if VM_DROPPABLE != VM_NONE
         |     ^
   include/trace/events/mmflags.h:168:21: warning: 'VM_NONE' is not defined, evaluates to 0 [-Wundef]
     168 | #if VM_DROPPABLE != VM_NONE
         |                     ^
   In file included from mm/debug_page_ref.c:6:
   In file included from include/trace/events/page_ref.h:135:
   In file included from include/trace/define_trace.h:95:
   In file included from include/trace/events/page_ref.h:11:
   include/trace/events/mmflags.h:168:5: warning: 'VM_DROPPABLE' is not defined, evaluates to 0 [-Wundef]
     168 | #if VM_DROPPABLE != VM_NONE
         |     ^
   include/trace/events/mmflags.h:168:21: warning: 'VM_NONE' is not defined, evaluates to 0 [-Wundef]
     168 | #if VM_DROPPABLE != VM_NONE
         |                     ^
   In file included from mm/debug_page_ref.c:6:
   In file included from include/trace/events/page_ref.h:135:
   In file included from include/trace/define_trace.h:102:
   In file included from include/trace/trace_events.h:94:
   In file included from include/trace/events/page_ref.h:11:
>> include/trace/events/mmflags.h:169:10: warning: 'IF_HAVE_VM_DROPPABLE' macro redefined [-Wmacro-redefined]

169 | # define IF_HAVE_VM_DROPPABLE(flag, name) {flag, name},
| ^
include/trace/events/mmflags.h:171:10: note: previous definition is here
171 | # define IF_HAVE_VM_DROPPABLE(flag, name)
| ^
5 warnings generated.

vim +/IF_HAVE_VM_DROPPABLE +169 include/trace/events/mmflags.h

7677f7fd8be766 Axel Rasmussen 2021-05-04 167

41e2c674b334ed Christophe Leroy   2024-08-22 @168  #if VM_DROPPABLE != VM_NONE
9651fcedf7b92d Jason A. Donenfeld 2022-12-08 @169  # define IF_HAVE_VM_DROPPABLE(flag, name) {flag, name},
9651fcedf7b92d Jason A. Donenfeld 2022-12-08  170  #else
9651fcedf7b92d Jason A. Donenfeld 2022-12-08  171  # define IF_HAVE_VM_DROPPABLE(flag, name)
9651fcedf7b92d Jason A. Donenfeld 2022-12-08  172  #endif

9651fcedf7b92d Jason A. Donenfeld 2022-12-08 173

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#45 lore raw

Re: [PATCH v2 07/17] mm: Define VM_DROPPABLE for powerpc/32

In reply to Christophe Leroy
kernel test robot noticed the following build warnings:

Hi Christophe,

kernel test robot noticed the following build warnings:

[auto build test WARNING on powerpc/next]
[also build test WARNING on powerpc/fixes shuah-kselftest/next shuah-kselftest/fixes linus/master v6.11-rc5 next-20240826] [cannot apply to crng-random/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Christophe-Leroy/asm-generic-unaligned-h-Extract-common-header-for-vDSO/20240826-103525
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
patch link:    https://lore.kernel.org/r/315e3a268b165b6edad7dcb723b0d8a506a56c4e.1724309198.git.christophe.leroy%40csgroup.eu
patch subject: [PATCH v2 07/17] mm: Define VM_DROPPABLE for powerpc/32
config: um-randconfig-r122-20240826 (https://download.01.org/0day-ci/archive/20240827/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240827/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)
mm/debug_page_ref.c: note: in included file (through include/trace/events/page_ref.h):

>> include/trace/events/mmflags.h:168:5: sparse: sparse: undefined preprocessor identifier 'VM_DROPPABLE'
>> include/trace/events/mmflags.h:168:21: sparse: sparse: undefined preprocessor identifier 'VM_NONE'
   mm/debug_page_ref.c: note: in included file (through include/trace/events/page_ref.h, include/trace/define_trace.h, include/trace/events/page_ref.h):
>> include/trace/events/mmflags.h:168:5: sparse: sparse: undefined preprocessor identifier 'VM_DROPPABLE'
>> include/trace/events/mmflags.h:168:21: sparse: sparse: undefined preprocessor identifier 'VM_NONE'

mm/debug_page_ref.c: note: in included file (through include/trace/events/page_ref.h, include/trace/trace_events.h, include/trace/define_trace.h, ...):
include/trace/events/mmflags.h:169:10: sparse: sparse: preprocessor token IF_HAVE_VM_DROPPABLE redefined
mm/debug_page_ref.c: note: in included file (through include/trace/events/page_ref.h):
include/trace/events/mmflags.h:171:10: sparse: this was the original definition

vim +/VM_DROPPABLE +168 include/trace/events/mmflags.h

167

 > 168	#if VM_DROPPABLE != VM_NONE

169 # define IF_HAVE_VM_DROPPABLE(flag, name) {flag, name},
170 #else
171 # define IF_HAVE_VM_DROPPABLE(flag, name)
172 #endif
173

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#46 lore raw

PATCH v2 8/17 powerpc: Add little endian variants of LWZX_BE and STWX_BE

In reply to Christophe Leroy

To support getrandom in VDSO which is based on little endian storage, add macros equivalent to LWZX_BE and STWX_BE for little endian accesses.

Put them outside of __powerpc64__ #ifdef so that they can also be used for PPC32.

---
 arch/powerpc/include/asm/asm-compat.h | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/powerpc/include/asm/asm-compat.h +8 -0
index 2bc53c646ccd..6e53a80917ef 100644
@@ -61,4 +61,12 @@
 
 #endif
 
+#ifdef __BIG_ENDIAN__
+#define LWZX_LE	stringify_in_c(lwbrx)
+#define STWX_LE	stringify_in_c(stwbrx)
+#else
+#define LWZX_LE	stringify_in_c(lwzx)
+#define STWX_LE	stringify_in_c(stwx)
+#endif
+
 #endif /* _ASM_POWERPC_ASM_COMPAT_H */
Signature · 1 line
2.44.0
#47 lore raw

PATCH v2 9/17 powerpc/vdso32: Add crtsavres

In reply to Christophe Leroy

Commit 08c18b63d965 ("powerpc/vdso32: Add missing _restgpr_31_x to fix build failure") added _restgpr_31_x to the vdso for gettimeofday, but the work on getrandom shows that we will need more of those functions.

Remove _restgpr_31_x and link in crtsavres.o so that we get all save/restore functions when optimising the kernel for size.

---
 arch/powerpc/kernel/vdso/Makefile       |  5 ++++-
 arch/powerpc/kernel/vdso/gettimeofday.S | 13 -------------
 2 files changed, 4 insertions(+), 14 deletions(-)
arch/powerpc/kernel/vdso/Makefile +4 -1
index 1425b6edc66b..c07a425b8f78 100644
@@ -43,6 +43,7 @@ else
 endif
 
 targets := $(obj-vdso32) vdso32.so.dbg vgettimeofday-32.o
+targets += crtsavres-32.o
 obj-vdso32 := $(addprefix $(obj)/, $(obj-vdso32))
 targets += $(obj-vdso64) vdso64.so.dbg vgettimeofday-64.o
 obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64))
@@ -68,7 +69,7 @@ targets += vdso64.lds
 CPPFLAGS_vdso64.lds += -P -C
 
 # link rule for the .so file, .lds has to be first
-$(obj)/vdso32.so.dbg: $(obj)/vdso32.lds $(obj-vdso32) $(obj)/vgettimeofday-32.o FORCE
+$(obj)/vdso32.so.dbg: $(obj)/vdso32.lds $(obj-vdso32) $(obj)/vgettimeofday-32.o $(obj)/crtsavres-32.o FORCE
 	$(call if_changed,vdso32ld_and_check)
 $(obj)/vdso64.so.dbg: $(obj)/vdso64.lds $(obj-vdso64) $(obj)/vgettimeofday-64.o FORCE
 	$(call if_changed,vdso64ld_and_check)
@@ -76,6 +77,8 @@ $(obj)/vdso64.so.dbg: $(obj)/vdso64.lds $(obj-vdso64) $(obj)/vgettimeofday-64.o
 # assembly rules for the .S files
 $(obj-vdso32): %-32.o: %.S FORCE
 	$(call if_changed_dep,vdso32as)
+$(obj)/crtsavres-32.o: %-32.o: $(srctree)/arch/powerpc/lib/crtsavres.S FORCE
+	$(call if_changed_dep,vdso32as)
 $(obj)/vgettimeofday-32.o: %-32.o: %.c FORCE
 	$(call if_changed_dep,vdso32cc)
 $(obj-vdso64): %-64.o: %.S FORCE
arch/powerpc/kernel/vdso/gettimeofday.S +0 -13
index 48fc6658053a..67254ac9c8bb 100644
@@ -118,16 +118,3 @@ V_FUNCTION_END(__kernel_clock_getres)
 V_FUNCTION_BEGIN(__kernel_time)
 	cvdso_call __c_kernel_time call_time=1
 V_FUNCTION_END(__kernel_time)
-
-/* Routines for restoring integer registers, called by the compiler.  */
-/* Called with r11 pointing to the stack header word of the caller of the */
-/* function, just beyond the end of the integer restore area.  */
-#ifndef __powerpc64__
-_GLOBAL(_restgpr_31_x)
-_GLOBAL(_rest32gpr_31_x)
-	lwz	r0,4(r11)
-	lwz	r31,-4(r11)
-	mtlr	r0
-	mr	r1,r11
-	blr
-#endif
Signature · 1 line
2.44.0
#48 lore raw

PATCH v2 10/17 powerpc/vdso: Refactor CFLAGS for CVDSO build

In reply to Christophe Leroy

In order to avoid duplication when we add new VDSO functionnalities in C like getrandom, refactor common CFLAGS.

---
 arch/powerpc/kernel/vdso/Makefile | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
arch/powerpc/kernel/vdso/Makefile +5 -10
index c07a425b8f78..cd4ed09864f0 100644
@@ -10,11 +10,6 @@ obj-vdso64 = sigtramp64-64.o gettimeofday-64.o datapage-64.o cacheflush-64.o not
 
 ifneq ($(c-gettimeofday-y),)
   CFLAGS_vgettimeofday-32.o += -include $(c-gettimeofday-y)
-  CFLAGS_vgettimeofday-32.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
-  CFLAGS_vgettimeofday-32.o += $(call cc-option, -fno-stack-protector)
-  CFLAGS_vgettimeofday-32.o += -DDISABLE_BRANCH_PROFILING
-  CFLAGS_vgettimeofday-32.o += -ffreestanding -fasynchronous-unwind-tables
-  CFLAGS_REMOVE_vgettimeofday-32.o = $(CC_FLAGS_FTRACE)
   CFLAGS_REMOVE_vgettimeofday-32.o += -mcmodel=medium -mabi=elfv1 -mabi=elfv2 -mcall-aixdesc
   # This flag is supported by clang for 64-bit but not 32-bit so it will cause
   # an unused command line flag warning for this file.
@@ -22,11 +17,6 @@ ifneq ($(c-gettimeofday-y),)
   CFLAGS_REMOVE_vgettimeofday-32.o += -fno-stack-clash-protection
   endif
   CFLAGS_vgettimeofday-64.o += -include $(c-gettimeofday-y)
-  CFLAGS_vgettimeofday-64.o += $(DISABLE_LATENT_ENTROPY_PLUGIN)
-  CFLAGS_vgettimeofday-64.o += $(call cc-option, -fno-stack-protector)
-  CFLAGS_vgettimeofday-64.o += -DDISABLE_BRANCH_PROFILING
-  CFLAGS_vgettimeofday-64.o += -ffreestanding -fasynchronous-unwind-tables
-  CFLAGS_REMOVE_vgettimeofday-64.o = $(CC_FLAGS_FTRACE)
 # Go prior to 1.16.x assumes r30 is not clobbered by any VDSO code. That used to be true
 # by accident when the VDSO was hand-written asm code, but may not be now that the VDSO is
 # compiler generated. To avoid breaking Go tell GCC not to use r30. Impact on code
@@ -49,7 +39,12 @@ targets += $(obj-vdso64) vdso64.so.dbg vgettimeofday-64.o
 obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64))
 
 ccflags-y := -fno-common -fno-builtin
+ccflags-y += $(DISABLE_LATENT_ENTROPY_PLUGIN)
+ccflags-y += $(call cc-option, -fno-stack-protector)
+ccflags-y += -DDISABLE_BRANCH_PROFILING
+ccflags-y += -ffreestanding -fasynchronous-unwind-tables
 ldflags-y := -Wl,--hash-style=both -nostdlib -shared -z noexecstack $(CLANG_FLAGS)
+ccflags-remove-y := $(CC_FLAGS_FTRACE)
 ldflags-$(CONFIG_LD_IS_LLD) += $(call cc-option,--ld-path=$(LD),-fuse-ld=lld)
 ldflags-$(CONFIG_LD_ORPHAN_WARN) += -Wl,--orphan-handling=$(CONFIG_LD_ORPHAN_WARN_LEVEL)
 
Signature · 1 line
2.44.0
#49 lore raw

PATCH v2 11/17 powerpc/vdso: Wire up getrandom() vDSO implementation

In reply to Christophe Leroy

To be consistent with other VDSO functions, the function is called __kernel_getrandom()

__arch_chacha20_blocks_nostack() fonction is implemented basically with 32 bits operations. It performs 4 QUARTERROUND operations in parallele. There are enough registers to avoid using the stack:

On input:
r3: output bytes
r4: 32-byte key input
r5: 8-byte counter input/output
r6: number of 64-byte blocks to write to output

During operation:

	r0: counter of blocks (initialised with r6)
	r4: Value '4' after key has been read, used for indexing
	r6-r13: key
	r14-r15: block counter
	r16-r31: chacha state

At the end:

	r0, r6-r13: Zeroised
	r14-r31: Restored

Performance on powerpc 885 (using kernel selftest):

	~# ./vdso_test_getrandom bench-single
	   vdso: 2500000 times in 7.897495392 seconds
	   libc: 2500000 times in 56.091632232 seconds
	syscall: 2500000 times in 55.704851989 seconds

Performance on powerpc 8321 (using kernel selftest):

	~# ./vdso_test_getrandom bench-single
	   vdso: 2500000 times in 2.017183250 seconds
	   libc: 2500000 times in 13.088533630 seconds
	syscall: 2500000 times in 12.952458068 seconds
---
 arch/powerpc/Kconfig                         |   1 +
 arch/powerpc/include/asm/mman.h              |   2 +-
 arch/powerpc/include/asm/vdso/getrandom.h    |  67 +++++
 arch/powerpc/include/asm/vdso/vsyscall.h     |   6 +
 arch/powerpc/include/asm/vdso_datapage.h     |   2 +
 arch/powerpc/kernel/asm-offsets.c            |   1 +
 arch/powerpc/kernel/vdso/Makefile            |  27 +-
 arch/powerpc/kernel/vdso/getrandom.S         |  58 ++++
 arch/powerpc/kernel/vdso/vdso32.lds.S        |   1 +
 arch/powerpc/kernel/vdso/vdso64.lds.S        |   1 +
 arch/powerpc/kernel/vdso/vgetrandom-chacha.S | 297 +++++++++++++++++++
 arch/powerpc/kernel/vdso/vgetrandom.c        |  14 +
 12 files changed, 471 insertions(+), 6 deletions(-)
 create mode 100644 arch/powerpc/include/asm/vdso/getrandom.h
 create mode 100644 arch/powerpc/kernel/vdso/getrandom.S
 create mode 100644 arch/powerpc/kernel/vdso/vgetrandom-chacha.S
 create mode 100644 arch/powerpc/kernel/vdso/vgetrandom.c
arch/powerpc/Kconfig +1 -0
index d7b09b064a8a..b45452ac4a73 100644
@@ -311,6 +311,7 @@ config PPC
 	select SYSCTL_EXCEPTION_TRACE
 	select THREAD_INFO_IN_TASK
 	select TRACE_IRQFLAGS_SUPPORT
+	select VDSO_GETRANDOM
 	#
 	# Please keep this list sorted alphabetically.
 	#
arch/powerpc/include/asm/mman.h +1 -1
index 17a77d47ed6d..42a51a993d94 100644
@@ -6,7 +6,7 @@
 
 #include <uapi/asm/mman.h>
 
-#ifdef CONFIG_PPC64
+#if defined(CONFIG_PPC64) && !defined(BUILD_VDSO)
 
 #include <asm/cputable.h>
 #include <linux/mm.h>
arch/powerpc/include/asm/vdso/getrandom.h new file +67 -0

67 lines not on this page · raw message

arch/powerpc/include/asm/vdso/vsyscall.h +6 -0
index 48cf23f1e273..92f480d8cc6d 100644
@@ -17,6 +17,12 @@ struct vdso_data *__arch_get_k_vdso_data(void)
 }
 #define __arch_get_k_vdso_data __arch_get_k_vdso_data
 
+static __always_inline
+struct vdso_rng_data *__arch_get_k_vdso_rng_data(void)
+{
+	return &vdso_data->rng_data;
+}
+
 /* The asm-generic header needs to be included after the definitions above */
 #include <asm-generic/vdso/vsyscall.h>
 
arch/powerpc/include/asm/vdso_datapage.h +2 -0
index a585c8e538ff..e17500c5237e 100644
@@ -83,6 +83,7 @@ struct vdso_arch_data {
 	__u32 compat_syscall_map[SYSCALL_MAP_SIZE];	/* Map of compat syscalls */
 
 	struct vdso_data data[CS_BASES];
+	struct vdso_rng_data rng_data;
 };
 
 #else /* CONFIG_PPC64 */
@@ -95,6 +96,7 @@ struct vdso_arch_data {
 	__u32 syscall_map[SYSCALL_MAP_SIZE]; /* Map of syscalls */
 	__u32 compat_syscall_map[0];	/* No compat syscalls on PPC32 */
 	struct vdso_data data[CS_BASES];
+	struct vdso_rng_data rng_data;
 };
 
 #endif /* CONFIG_PPC64 */
arch/powerpc/kernel/asm-offsets.c +1 -0
index 23733282de4d..eedb2e04c785 100644
@@ -335,6 +335,7 @@ int main(void)
 
 	/* datapage offsets for use by vdso */
 	OFFSET(VDSO_DATA_OFFSET, vdso_arch_data, data);
+	OFFSET(VDSO_RNG_DATA_OFFSET, vdso_arch_data, rng_data);
 	OFFSET(CFG_TB_TICKS_PER_SEC, vdso_arch_data, tb_ticks_per_sec);
 #ifdef CONFIG_PPC64
 	OFFSET(CFG_ICACHE_BLOCKSZ, vdso_arch_data, icache_block_size);
arch/powerpc/kernel/vdso/Makefile +22 -5
index cd4ed09864f0..9109677b2f39 100644
@@ -8,6 +8,9 @@ include $(srctree)/lib/vdso/Makefile
 obj-vdso32 = sigtramp32-32.o gettimeofday-32.o datapage-32.o cacheflush-32.o note-32.o getcpu-32.o
 obj-vdso64 = sigtramp64-64.o gettimeofday-64.o datapage-64.o cacheflush-64.o note-64.o getcpu-64.o
 
+obj-vdso32 += getrandom-32.o vgetrandom-chacha-32.o
+obj-vdso64 += getrandom-64.o vgetrandom-chacha-64.o
+
 ifneq ($(c-gettimeofday-y),)
   CFLAGS_vgettimeofday-32.o += -include $(c-gettimeofday-y)
   CFLAGS_REMOVE_vgettimeofday-32.o += -mcmodel=medium -mabi=elfv1 -mabi=elfv2 -mcall-aixdesc
@@ -24,6 +27,16 @@ ifneq ($(c-gettimeofday-y),)
   CFLAGS_vgettimeofday-64.o += $(call cc-option, -ffixed-r30)
 endif
 
+ifneq ($(c-getrandom-y),)
+  CFLAGS_vgetrandom-32.o += -include $(c-getrandom-y)
+  CFLAGS_REMOVE_vgetrandom-32.o += -mcmodel=medium -mabi=elfv1 -mabi=elfv2 -mcall-aixdesc
+  ifdef CONFIG_CC_IS_CLANG
+  CFLAGS_REMOVE_vgetrandom-32.o += -fno-stack-clash-protection
+  endif
+  CFLAGS_vgetrandom-64.o += -include $(c-getrandom-y)
+  CFLAGS_vgetrandom-64.o += $(call cc-option, -ffixed-r30)
+endif
+
 # Build rules
 
 ifdef CROSS32_COMPILE
@@ -32,13 +45,13 @@ else
     VDSOCC := $(CC)
 endif
 
-targets := $(obj-vdso32) vdso32.so.dbg vgettimeofday-32.o
+targets := $(obj-vdso32) vdso32.so.dbg vgettimeofday-32.o vgetrandom-32.o
 targets += crtsavres-32.o
 obj-vdso32 := $(addprefix $(obj)/, $(obj-vdso32))
-targets += $(obj-vdso64) vdso64.so.dbg vgettimeofday-64.o
+targets += $(obj-vdso64) vdso64.so.dbg vgettimeofday-64.o vgetrandom-64.o
 obj-vdso64 := $(addprefix $(obj)/, $(obj-vdso64))
 
-ccflags-y := -fno-common -fno-builtin
+ccflags-y := -fno-common -fno-builtin -DBUILD_VDSO
 ccflags-y += $(DISABLE_LATENT_ENTROPY_PLUGIN)
 ccflags-y += $(call cc-option, -fno-stack-protector)
 ccflags-y += -DDISABLE_BRANCH_PROFILING
@@ -64,9 +77,9 @@ targets += vdso64.lds
 CPPFLAGS_vdso64.lds += -P -C
 
 # link rule for the .so file, .lds has to be first
-$(obj)/vdso32.so.dbg: $(obj)/vdso32.lds $(obj-vdso32) $(obj)/vgettimeofday-32.o $(obj)/crtsavres-32.o FORCE
+$(obj)/vdso32.so.dbg: $(obj)/vdso32.lds $(obj-vdso32) $(obj)/vgettimeofday-32.o $(obj)/vgetrandom-32.o $(obj)/crtsavres-32.o FORCE
 	$(call if_changed,vdso32ld_and_check)
-$(obj)/vdso64.so.dbg: $(obj)/vdso64.lds $(obj-vdso64) $(obj)/vgettimeofday-64.o FORCE
+$(obj)/vdso64.so.dbg: $(obj)/vdso64.lds $(obj-vdso64) $(obj)/vgettimeofday-64.o $(obj)/vgetrandom-64.o FORCE
 	$(call if_changed,vdso64ld_and_check)
 
 # assembly rules for the .S files
@@ -76,10 +89,14 @@ $(obj)/crtsavres-32.o: %-32.o: $(srctree)/arch/powerpc/lib/crtsavres.S FORCE
 	$(call if_changed_dep,vdso32as)
 $(obj)/vgettimeofday-32.o: %-32.o: %.c FORCE
 	$(call if_changed_dep,vdso32cc)
+$(obj)/vgetrandom-32.o: %-32.o: %.c FORCE
+	$(call if_changed_dep,vdso32cc)
 $(obj-vdso64): %-64.o: %.S FORCE
 	$(call if_changed_dep,vdso64as)
 $(obj)/vgettimeofday-64.o: %-64.o: %.c FORCE
 	$(call if_changed_dep,cc_o_c)
+$(obj)/vgetrandom-64.o: %-64.o: %.c FORCE
+	$(call if_changed_dep,cc_o_c)
 
 # Generate VDSO offsets using helper script
 gen-vdso32sym := $(src)/gen_vdso32_offsets.sh
arch/powerpc/kernel/vdso/getrandom.S new file +58 -0

58 lines not on this page · raw message

arch/powerpc/kernel/vdso/vdso32.lds.S +1 -0
index 8f57107000a2..7b41d5d256e8 100644
@@ -130,6 +130,7 @@ VERSION
 #if defined(CONFIG_PPC64) || !defined(CONFIG_SMP)
 		__kernel_getcpu;
 #endif
+		__kernel_getrandom;
 
 	local: *;
 	};
arch/powerpc/kernel/vdso/vdso64.lds.S +1 -0
index 400819258c06..9481e4b892ed 100644
@@ -123,6 +123,7 @@ VERSION
 		__kernel_sigtramp_rt64;
 		__kernel_getcpu;
 		__kernel_time;
+		__kernel_getrandom;
 
 	local: *;
 	};
arch/powerpc/kernel/vdso/vgetrandom-chacha.S new file +297 -0

297 lines not on this page · raw message

arch/powerpc/kernel/vdso/vgetrandom.c new file +14 -0
new file mode 100644
index 000000000000..5f855d45fb7b
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Powerpc userspace implementation of getrandom()
+ *
+ * Copyright (C) 2024 Christophe Leroy <[email protected]>, CS GROUP France
+ */
+#include <linux/time.h>
+#include <linux/types.h>
+
+ssize_t __c_kernel_getrandom(void *buffer, size_t len, unsigned int flags, void *opaque_state,
+			     size_t opaque_len, const struct vdso_rng_data *vd)
+{
+	return __cvdso_getrandom_data(vd, buffer, len, flags, opaque_state, opaque_len);
+}
Signature · 1 line
2.44.0
#50 lore raw

PATCH v2 12/17 selftests: vdso: Fix powerpc64 vdso_config

In reply to Christophe Leroy

__powerpc__ is also defined on powerpc64 so __powerpc64__ needs to be checked first.

Fixes 693f5ca08ca0 ("kselftest: Extend vDSO selftest")
---
 tools/testing/selftests/vDSO/vdso_config.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/vDSO/vdso_config.h +3 -3
index 7b543e7f04d7..00bfed6e4922 100644
@@ -18,13 +18,13 @@
 #elif defined(__aarch64__)
 #define VDSO_VERSION		3
 #define VDSO_NAMES		0
-#elif defined(__powerpc__)
+#elif defined(__powerpc64__)
 #define VDSO_VERSION		1
 #define VDSO_NAMES		0
-#define VDSO_32BIT		1
-#elif defined(__powerpc64__)
+#elif defined(__powerpc__)
 #define VDSO_VERSION		1
 #define VDSO_NAMES		0
+#define VDSO_32BIT		1
 #elif defined (__s390__)
 #define VDSO_VERSION		2
 #define VDSO_NAMES		0
Signature · 1 line
2.44.0
#51 lore raw

PATCH v2 13/17 selftests: vdso: Don't hard-code location of vDSO sources

In reply to Christophe Leroy

Architectures use different location for vDSO sources:

	arch/mips/vdso
	arch/sparc/vdso
	arch/arm64/kernel/vdso
	arch/riscv/kernel/vdso
	arch/csky/kernel/vdso
	arch/x86/um/vdso
	arch/x86/entry/vdso
	arch/powerpc/kernel/vdso
	arch/arm/vdso
	arch/loongarch/vdso

Don't hard-code vdso sources location in selftest Makefile, instead create a vdso/ symbolic link in tools/arch/$arch/ and update Makefile accordingly.

---
 tools/arch/x86/vdso                   | 1 +
 tools/testing/selftests/vDSO/Makefile | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 120000 tools/arch/x86/vdso
tools/arch/x86/vdso new file +1 -0
new file mode 120000
index 000000000000..7eb962fd3454
@@ -0,0 +1 @@
+../../../arch/x86/entry/vdso/
\ No newline at end of file
tools/testing/selftests/vDSO/Makefile +1 -1
index 3de8e7e052ae..c9a819cacbf2 100644
@@ -40,7 +40,7 @@ $(OUTPUT)/vdso_test_getrandom: parse_vdso.c
 $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \
                                          -isystem $(top_srcdir)/include/uapi
 
-$(OUTPUT)/vdso_test_chacha: $(top_srcdir)/arch/$(ARCH)/entry/vdso/vgetrandom-chacha.S
+$(OUTPUT)/vdso_test_chacha: $(top_srcdir)/tools/arch/$(ARCH)/vdso/vgetrandom-chacha.S
 $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -isystem $(top_srcdir)/arch/$(ARCH)/include \
                                       -isystem $(top_srcdir)/include \
Signature · 1 line
2.44.0
#52 lore raw

Re: [PATCH v2 13/17] selftests: vdso: Don't hard-code location of vDSO sources

Quoted 12 lines · Christophe Leroy

Architectures use different location for vDSO sources:

	arch/mips/vdso
	arch/sparc/vdso
	arch/arm64/kernel/vdso
	arch/riscv/kernel/vdso
	arch/csky/kernel/vdso
	arch/x86/um/vdso
	arch/x86/entry/vdso
	arch/powerpc/kernel/vdso
	arch/arm/vdso
	arch/loongarch/vdso
Quoted 3 lines

Don't hard-code vdso sources location in selftest Makefile, instead create a vdso/ symbolic link in tools/arch/$arch/ and update Makefile accordingly.

That seems like a good way of handling it. As archs add their implementations, they can just add the symlink and do no more work. It will also make review slightly easier: if I don't see the symlink added, I'll know the submitter didn't actually test their chacha :-P.

I'll take this into my random.git tree now as a fix.

Jason

#53 lore raw

PATCH v2 14/17 selftests: vdso: Make test_vdso_getrandom look for the right vDSO function

In reply to Christophe Leroy

Don't hard-code x86 specific names, use vdso_config definitions to find the correct function matching the architecture.

Add random VDSO function names in names[][]. Remove the #ifdef CONFIG_VDSO32, having the name there all the time is harmless and guaranties a steady index for following strings.

---
 tools/testing/selftests/vDSO/vdso_config.h         | 8 +++-----
 tools/testing/selftests/vDSO/vdso_test_getrandom.c | 8 ++++++--
 2 files changed, 9 insertions(+), 7 deletions(-)
tools/testing/selftests/vDSO/vdso_config.h +3 -5
index 00bfed6e4922..740ce8c98d2e 100644
@@ -68,16 +68,15 @@ static const char *versions[7] = {
 	"LINUX_5.10"
 };
 
-static const char *names[2][6] = {
+static const char *names[2][7] = {
 	{
 		"__kernel_gettimeofday",
 		"__kernel_clock_gettime",
 		"__kernel_time",
 		"__kernel_clock_getres",
 		"__kernel_getcpu",
-#if defined(VDSO_32BIT)
 		"__kernel_clock_gettime64",
-#endif
+		"__kernel_getrandom",
 	},
 	{
 		"__vdso_gettimeofday",
@@ -85,9 +84,8 @@ static const char *names[2][6] = {
 		"__vdso_time",
 		"__vdso_clock_getres",
 		"__vdso_getcpu",
-#if defined(VDSO_32BIT)
 		"__vdso_clock_gettime64",
-#endif
+		"__vdso_getrandom",
 	},
 };
 
tools/testing/selftests/vDSO/vdso_test_getrandom.c +6 -2
index 05122425a873..02bcffc23e0c 100644
@@ -21,6 +21,7 @@
 
 #include "../kselftest.h"
 #include "parse_vdso.h"
+#include "vdso_config.h"
 
 #ifndef timespecsub
 #define	timespecsub(tsp, usp, vsp)					\
@@ -107,6 +108,9 @@ static void vgetrandom_put_state(void *state)
 
 static void vgetrandom_init(void)
 {
+	const char *version = versions[VDSO_VERSION];
+	const char **name = (const char **)&names[VDSO_NAMES];
+
 	if (pthread_key_create(&grnd_ctx.key, vgetrandom_put_state) != 0)
 		return;
 	unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
@@ -115,9 +119,9 @@ static void vgetrandom_init(void)
 		exit(KSFT_SKIP);
 	}
 	vdso_init_from_sysinfo_ehdr(sysinfo_ehdr);
-	grnd_ctx.fn = (__typeof__(grnd_ctx.fn))vdso_sym("LINUX_2.6", "__vdso_getrandom");
+	grnd_ctx.fn = (__typeof__(grnd_ctx.fn))vdso_sym(version, name[6]);
 	if (!grnd_ctx.fn) {
-		printf("__vdso_getrandom is missing!\n");
+		printf("%s is missing!\n", name[6]);
 		exit(KSFT_FAIL);
 	}
 	if (grnd_ctx.fn(NULL, 0, 0, &grnd_ctx.params, ~0UL) != 0) {
Signature · 1 line
2.44.0
#54 lore raw

Re: [PATCH v2 14/17] selftests: vdso: Make test_vdso_getrandom look for the right vDSO function

Quoted 6 lines · Christophe Leroy

Don't hard-code x86 specific names, use vdso_config definitions to find the correct function matching the architecture.

Add random VDSO function names in names[][]. Remove the #ifdef CONFIG_VDSO32, having the name there all the time is harmless and guaranties a steady index for following strings.

This is indeed the right way of doing it. Thanks. I'll take this now, though with one small fixup:

tools/testing/selftests/vDSO/vdso_test_getrandom.c static void vgetrandom_put_state(void *state) · line 112

Quoted 2 lines
tools/testing/selftests/vDSO/vdso_test_getrandom.c +2 -0
+	const char *version = versions[VDSO_VERSION];
+	const char **name = (const char **)&names[VDSO_NAMES];

I'll just do:

        const char *name = names[VDSO_NAMES][6];

Instead of referring to name[6] everywhere after. Seems more straight forward.

Jason

#55 lore raw

Re: [PATCH v2 14/17] selftests: vdso: Make test_vdso_getrandom look for the right vDSO function

Le 26/08/2024 à 09:28, Jason A. Donenfeld a écrit :

Quoted 16 lines · Christophe Leroy
Quoted 6 lines

Don't hard-code x86 specific names, use vdso_config definitions to find the correct function matching the architecture.

Add random VDSO function names in names[][]. Remove the #ifdef CONFIG_VDSO32, having the name there all the time is harmless and guaranties a steady index for following strings.

This is indeed the right way of doing it. Thanks. I'll take this now, though with one small fixup:

tools/testing/selftests/vDSO/vdso_test_getrandom.c static void vgetrandom_put_state(void *state) · line 112

Quoted 2 lines
tools/testing/selftests/vDSO/vdso_test_getrandom.c +2 -0
+	const char *version = versions[VDSO_VERSION];
+	const char **name = (const char **)&names[VDSO_NAMES];

I'll just do:

Quoted 4 lines
         const char *name = names[VDSO_NAMES][6];

Instead of referring to name[6] everywhere after. Seems more straight forward.

Yes you are right that's more straight forward when using only one of the names.
I copy-pasted it from the gettimeofday test which uses several names from the table.

Christophe

#56 lore raw

PATCH v2 15/17 selftests: vdso: Fix build of test_vdso_chacha

In reply to Christophe Leroy

Building test_vdso_chacha on powerpc leads to following issue:

In file included from /home/chleroy/linux-powerpc/include/linux/limits.h:7,

                   from /opt/powerpc64-e5500--glibc--stable-2024.02-1/powerpc64-buildroot-linux-gnu/sysroot/usr/include/bits/local_lim.h:38,
                   from /opt/powerpc64-e5500--glibc--stable-2024.02-1/powerpc64-buildroot-linux-gnu/sysroot/usr/include/bits/posix1_lim.h:161,
                   from /opt/powerpc64-e5500--glibc--stable-2024.02-1/powerpc64-buildroot-linux-gnu/sysroot/usr/include/limits.h:195,
                   from /opt/powerpc64-e5500--glibc--stable-2024.02-1/lib/gcc/powerpc64-buildroot-linux-gnu/12.3.0/include-fixed/limits.h:203,
                   from /opt/powerpc64-e5500--glibc--stable-2024.02-1/lib/gcc/powerpc64-buildroot-linux-gnu/12.3.0/include-fixed/syslimits.h:7,
                   from /opt/powerpc64-e5500--glibc--stable-2024.02-1/lib/gcc/powerpc64-buildroot-linux-gnu/12.3.0/include-fixed/limits.h:34,
                   from /tmp/sodium/usr/local/include/sodium/export.h:7,
                   from /tmp/sodium/usr/local/include/sodium/crypto_stream_chacha20.h:14,
                   from vdso_test_chacha.c:6:
  /opt/powerpc64-e5500--glibc--stable-2024.02-1/powerpc64-buildroot-linux-gnu/sysroot/usr/include/bits/xopen_lim.h:99:6: error: missing binary operator before token "("
     99 | # if INT_MAX == 32767
        |      ^~~~~~~
  /opt/powerpc64-e5500--glibc--stable-2024.02-1/powerpc64-buildroot-linux-gnu/sysroot/usr/include/bits/xopen_lim.h:102:7: error: missing binary operator before token "("
    102 | #  if INT_MAX == 2147483647
        |       ^~~~~~~
  /opt/powerpc64-e5500--glibc--stable-2024.02-1/powerpc64-buildroot-linux-gnu/sysroot/usr/include/bits/xopen_lim.h:126:6: error: missing binary operator before token "("
    126 | # if LONG_MAX == 2147483647
        |      ^~~~~~~~

This is due to kernel include/linux/limits.h being included instead of libc's limits.h

This is because directory include/ is added through option -isystem so it goes prior to glibc's include directory.

Replace -isystem by -idirafter

But this implies that now tools/include/linux/linkage.h is included instead of include/linux/linkage.h, so define a stub for SYM_FUNC_START() and SYM_FUNC_END().

---
 tools/include/linux/linkage.h         | 4 ++++
 tools/testing/selftests/vDSO/Makefile | 4 ++--
 2 files changed, 6 insertions(+), 2 deletions(-)
tools/include/linux/linkage.h +4 -0
index bc763d500262..a48ff086899c 100644
@@ -1,4 +1,8 @@
 #ifndef _TOOLS_INCLUDE_LINUX_LINKAGE_H
 #define _TOOLS_INCLUDE_LINUX_LINKAGE_H
 
+#define SYM_FUNC_START(x) .globl x; x:
+
+#define SYM_FUNC_END(x)
+
 #endif /* _TOOLS_INCLUDE_LINUX_LINKAGE_H */
tools/testing/selftests/vDSO/Makefile +2 -2
index c9a819cacbf2..10ffdda3f2fa 100644
@@ -42,7 +42,7 @@ $(OUTPUT)/vdso_test_getrandom: CFLAGS += -isystem $(top_srcdir)/tools/include \
 
 $(OUTPUT)/vdso_test_chacha: $(top_srcdir)/tools/arch/$(ARCH)/vdso/vgetrandom-chacha.S
 $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
-                                      -isystem $(top_srcdir)/arch/$(ARCH)/include \
-                                      -isystem $(top_srcdir)/include \
+                                      -idirafter $(top_srcdir)/arch/$(ARCH)/include \
+                                      -idirafter $(top_srcdir)/include \
                                       -D__ASSEMBLY__ -DBULID_VDSO -DCONFIG_FUNCTION_ALIGNMENT=0 \
                                       -Wa,--noexecstack $(SODIUM)
Signature · 1 line
2.44.0
#57 lore raw

Re: [PATCH v2 15/17] selftests: vdso: Fix build of test_vdso_chacha

Quoted 5 lines · Christophe Leroy

Replace -isystem by -idirafter

But this implies that now tools/include/linux/linkage.h is included instead of include/linux/linkage.h, so define a stub for SYM_FUNC_START() and SYM_FUNC_END().

Thanks! I got step 1, but didn't figure out step 2, and tried to do things the lazy way instead:

-#include <sodium/crypto_stream_chacha20.h>
#include <sys/random.h>
#include <string.h>
#include <stdint.h>
#include "../kselftest.h"

extern void __arch_chacha20_blocks_nostack(uint8_t *dst_bytes, const uint8_t *key, uint32_t *counter, size_t nblocks);
+extern int crypto_stream_chacha20(uint8_t *dst_bytes, uint64_t dst_len, const uint8_t *nonce, const uint8_t *key);

Yours is obviously much better, so I'll queue that up instead.

#58 lore raw

PATCH v2 16/17 selftests: vdso: Make VDSO function call more generic

In reply to Christophe Leroy

On powerpc, a call to a VDSO function is not a standard C function call. Unlike x86 that returns a negated error code in case of an error, powerpc sets CR[SO] and returns the error code as a positive value.

So use a macro called VDSO_CALL() which takes a pointer to the function to call, the number of arguments and the arguments.

---
 tools/testing/selftests/vDSO/vdso_call.h           | 12 ++++++++++++
 tools/testing/selftests/vDSO/vdso_test_getrandom.c |  3 ++-
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/vDSO/vdso_call.h
tools/testing/selftests/vDSO/vdso_call.h new file +12 -0
new file mode 100644
index 000000000000..ca5db2220925
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Macro to call vDSO functions
+ *
+ * Copyright (C) 2024 Christophe Leroy <[email protected]>, CS GROUP France
+ */
+#ifndef __VDSO_CALL_H__
+#define __VDSO_CALL_H__
+
+#define VDSO_CALL(fn, nr, args...)	fn(args)
+
+#endif
tools/testing/selftests/vDSO/vdso_test_getrandom.c +2 -1
index 02bcffc23e0c..16ad400721c3 100644
@@ -22,6 +22,7 @@
 #include "../kselftest.h"
 #include "parse_vdso.h"
 #include "vdso_config.h"
+#include "vdso_call.h"
 
 #ifndef timespecsub
 #define	timespecsub(tsp, usp, vsp)					\
@@ -147,7 +148,7 @@ static ssize_t vgetrandom(void *buf, size_t len, unsigned long flags)
 			exit(KSFT_FAIL);
 		}
 	}
-	return grnd_ctx.fn(buf, len, flags, state, grnd_ctx.params.size_of_opaque_state);
+	return VDSO_CALL(grnd_ctx.fn, 5, buf, len, flags, state, grnd_ctx.params.size_of_opaque_state);
 }
 
 enum { TRIALS = 25000000, THREADS = 256 };
Signature · 1 line
2.44.0
#59 lore raw

Re: [PATCH v2 16/17] selftests: vdso: Make VDSO function call more generic

Quoted 7 lines · Christophe Leroy

On powerpc, a call to a VDSO function is not a standard C function call. Unlike x86 that returns a negated error code in case of an error, powerpc sets CR[SO] and returns the error code as a positive value.

So use a macro called VDSO_CALL() which takes a pointer to the function to call, the number of arguments and the arguments.

You'll probably want to move to VDSO_CALL() for the whole test suite, not just the getrandom one, right?

#60 lore raw

Re: [PATCH v2 16/17] selftests: vdso: Make VDSO function call more generic

Le 26/08/2024 à 09:37, Jason A. Donenfeld a écrit :

Quoted 11 lines · Christophe Leroy
Quoted 7 lines

On powerpc, a call to a VDSO function is not a standard C function call. Unlike x86 that returns a negated error code in case of an error, powerpc sets CR[SO] and returns the error code as a positive value.

So use a macro called VDSO_CALL() which takes a pointer to the function to call, the number of arguments and the arguments.

You'll probably want to move to VDSO_CALL() for the whole test suite, not just the getrandom one, right?

Yes indeed, the following needs it as well:

vdso_test_abi.c
vdso_test_getcpu.c
vdso_test_gettimeofday.c
vdso_test_correctness.c

Christophe

#61 lore raw

PATCH v2 17/17 selftests: vdso: Add support for vdso_test_random for powerpc

In reply to Christophe Leroy

Add the necessary symbolic link and tell Makefile to build vdso_test_random for powerpc.

In makefile, don't use $(uname_M) which is wrong when cross-building for powerpc on an x86_64.

Implement the required VDSO_CALL macro to correctly handle errors.

---
 tools/arch/powerpc/vdso                  |  1 +
 tools/testing/selftests/vDSO/Makefile    |  6 ++++
 tools/testing/selftests/vDSO/vdso_call.h | 40 ++++++++++++++++++++++++
 3 files changed, 47 insertions(+)
 create mode 120000 tools/arch/powerpc/vdso
tools/arch/powerpc/vdso new file +1 -0
new file mode 120000
index 000000000000..d31004bf8f55
@@ -0,0 +1 @@
+../../../arch/powerpc/kernel/vdso/
\ No newline at end of file
tools/testing/selftests/vDSO/Makefile +6 -0
index 10ffdda3f2fa..7e7c9fd200d3 100644
@@ -17,6 +17,12 @@ ifneq ($(SODIUM),)
 TEST_GEN_PROGS += vdso_test_chacha
 endif
 endif
+ifeq ($(ARCH),powerpc)
+TEST_GEN_PROGS += vdso_test_getrandom
+ifneq ($(SODIUM),)
+TEST_GEN_PROGS += vdso_test_chacha
+endif
+endif
 
 CFLAGS := -std=gnu99
 
tools/testing/selftests/vDSO/vdso_call.h +40 -0
index ca5db2220925..2a33c25756dc 100644
@@ -7,6 +7,46 @@
 #ifndef __VDSO_CALL_H__
 #define __VDSO_CALL_H__
 
+#ifdef __powerpc__
+
+#define LOADARGS_5(fn, __arg1, __arg2, __arg3, __arg4, __arg5) do { 	\
+	_r0 = fn;							\
+	_r3 = (long)__arg1;						\
+	_r4 = (long)__arg2;						\
+	_r5 = (long)__arg3;						\
+	_r6 = (long)__arg4;						\
+	_r7 = (long)__arg5;						\
+} while (0)
+
+#define VDSO_CALL(fn, nr, args...) ({					\
+	register void *_r0 asm ("r0");					\
+	register long _r3 asm ("r3");					\
+	register long _r4 asm ("r4");					\
+	register long _r5 asm ("r5");					\
+	register long _r6 asm ("r6");					\
+	register long _r7 asm ("r7");					\
+	register long _r8 asm ("r8");					\
+	register long _rval asm ("r3");					\
+									\
+	LOADARGS_##nr(fn, args);					\
+									\
+	asm volatile(							\
+		"	mtctr %0\n"					\
+		"	bctrl\n"					\
+		"	bns+	1f\n"					\
+		"	neg	3, 3\n"					\
+		"1:"							\
+		: "+r" (_r0), "=r" (_r3), "+r" (_r4), "+r" (_r5),	\
+		  "+r" (_r6), "+r" (_r7), "+r" (_r8)			\
+		: "r" (_rval)						\
+		: "r9", "r10", "r11", "r12", "cr0", "cr1", "cr5",	\
+		  "cr6", "cr7", "xer", "lr", "ctr", "memory"		\
+	);								\
+	_rval;								\
+})
+
+#else
 #define VDSO_CALL(fn, nr, args...)	fn(args)
+#endif
 
 #endif
Signature · 1 line
2.44.0
#62 lore raw
In reply to Christophe Leroy

Hi Christophe,

Thanks for this series. There are quite a few preliminary patches in it, before you get to the PPC part, which fix up general build system or test harness correctness issues. Since some of those affect all architectures that are adding vDSO getrandom() support for 6.12, I'm going to take those into my random.git tree as a fix for 6.11 now, in hopes that the new archs can mostly go into arch trees without too many tree interdependencies.

So I'll reply to individual patches for that, mentioning which ones I extract.

Jason

#63 lore raw
Quoted 10 lines · Jason A. Donenfeld

Hi Christophe,

Thanks for this series. There are quite a few preliminary patches in it, before you get to the PPC part, which fix up general build system or test harness correctness issues. Since some of those affect all architectures that are adding vDSO getrandom() support for 6.12, I'm going to take those into my random.git tree as a fix for 6.11 now, in hopes that the new archs can mostly go into arch trees without too many tree interdependencies.

Quoted 2 lines

So I'll reply to individual patches for that, mentioning which ones I extract.

I've committed a bunch of these to:

https://git.kernel.org/pub/scm/linux/kernel/git/crng/random.git/log/

For a v3, if you rebase on there, that'd make things easier for me to keep picking patches that I intend to send out for 6.11-rc6 later this week. And then hopefully your 6.12 ppc implementation can just go in via the ppc tree with my eventual ack on the crypto part, without needing these interdependencies.

Jason

#64 lore raw

getrandom() vDSO archs (arm64, ppc, loongarch) for 6.12 [Was: Re: [PATCH v2 00/17] Wire up getrandom() vDSO implementation on powerpc]

In reply to Jason A. Donenfeld

Hey again,

Quoted 10 lines · Jason A. Donenfeld

Thanks for this series. There are quite a few preliminary patches in it, before you get to the PPC part, which fix up general build system or test harness correctness issues. Since some of those affect all architectures that are adding vDSO getrandom() support for 6.12, I'm going to take those into my random.git tree as a fix for 6.11 now, in hopes that the new archs can mostly go into arch trees without too many tree interdependencies.

So I'll reply to individual patches for that, mentioning which ones I extract.

Seeing the volume of these and the amount of ground they touch, I'm now having second thoughts about rushing this into 6.11. Particularly with the header changes, I think it might be smart to let it cook in linux-next for a bit before sending it to Linus.

  $ git --no-pager diff --name-only linus/master
  arch/x86/entry/vdso/vma.c
  arch/x86/include/asm/pvclock.h
  arch/x86/include/asm/vdso/vsyscall.h
  drivers/char/random.c
  include/asm-generic/unaligned.h
  include/vdso/helpers.h
  include/vdso/unaligned.h
  lib/vdso/Makefile
  lib/vdso/getrandom.c
  tools/arch/x86/vdso
  tools/include/linux/linkage.h
  tools/testing/selftests/vDSO/Makefile
  tools/testing/selftests/vDSO/vdso_config.h
  tools/testing/selftests/vDSO/vdso_test_getrandom.c

So I think what I'll do is, for 6.11-rc6, send in the real bug fixes, which right now amount to:

- random: vDSO: reject unknown getrandom() flags
- random: vDSO: don't use 64-bit atomics on 32-bit architectures

  $ git --no-pager diff --name-only linus/master..a90592ab7cad
  drivers/char/random.c
  lib/vdso/getrandom.c

And then for the pending aarm64, ppc64(/32?), and loongarch enablement patches for 6.12, I'll just take those through my random.git tree, which have all of these build-system preliminaries. And then we'll obviously require acks from the maintainers of the archs for each of those arch enablement patches.

If that sounds like an acceptable plan, you might want to mention in the cover letter that you're basing your arch-specific patches on
https://git.kernel.org/pub/scm/linux/kernel/git/crng/random.git/ and want an ack from the arch maintainer, etc.

Jason