Skip to contentSkip to conversation

Wire up getrandom() vDSO implementation on powerpc

PATCH Superseded Christophe Leroy 3 participants 15 messages Aug 16, 2024 – Aug 17, 2024

Subsystems (6)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

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.

It doesn't build with CONFIG_COMPAT. This is because unlike gettimeofday.c, getrandom.c includes a lot of headers from outside of include/vdso/ . The same work as done by
commit 8c59ab839f52 ("lib/vdso: Enable common headers") needs to be done on lib/vdso/getrandom.c

Among the few strange things to be clarified, there is the format on 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.

Christophe Leroy (9):
powerpc/vdso: Don't discard rela sections
powerpc/vdso32: Add crtsavres
vdso: Add __arch_get_k_vdso_rng_data()
vdso: Add missing c-getrandom-y in Makefile
vdso: Avoid call to memset() by getrandom
vdso: Only use MAP_DROPPABLE when VM_DROPPABLE exists
powerpc: Add little endian variants of LHZX_BE and friends powerpc/vdso: Wire up getrandom() vDSO implementation
selftests: [NOT TO BE MERGED] Modifications for testing VDSO getrandom
implementation on PPC32

 arch/powerpc/Kconfig                          |   1 +
 arch/powerpc/include/asm/asm-compat.h         |  40 ++-
 arch/powerpc/include/asm/vdso/getrandom.h     |  61 ++++
 arch/powerpc/include/asm/vdso/vsyscall.h      |   7 +
 arch/powerpc/include/asm/vdso_datapage.h      |   2 +
 arch/powerpc/kernel/asm-offsets.c             |   1 +
 arch/powerpc/kernel/vdso/Makefile             |  44 ++-
 arch/powerpc/kernel/vdso/getrandom.S          |  62 ++++
 arch/powerpc/kernel/vdso/gettimeofday.S       |  13 -
 arch/powerpc/kernel/vdso/vdso32.lds.S         |   5 +-
 arch/powerpc/kernel/vdso/vdso64.lds.S         |   5 +-
 arch/powerpc/kernel/vdso/vgetrandom-chacha.S  | 297 ++++++++++++++++++
 arch/powerpc/kernel/vdso/vgetrandom.c         |  12 +
 arch/x86/include/asm/vdso/vsyscall.h          |   7 +
 drivers/char/random.c                         |   5 +-
 include/asm-generic/vdso/vsyscall.h           |   7 +
 include/vdso/limits.h                         |   4 +-
 lib/vdso/Makefile                             |   1 +
 lib/vdso/getrandom.c                          |  18 +-
 tools/testing/selftests/vDSO/Makefile         |   2 +-
 .../selftests/vDSO/vdso_test_getrandom.c      |   6 +-
 21 files changed, 553 insertions(+), 47 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
Signature · 1 line
2.44.0
#1 lore raw

PATCH 1/9 powerpc/vdso: Don't discard rela sections

After building the VDSO, there is a verification that it contains no dynamic relocation, see commit aff69273af61 ("vdso: Improve cmd_vdso_check to check all dynamic relocations").

This verification uses readelf -r and doesn't work if rela sections are discarded.

Fixes 8ad57add77d3 ("powerpc/build: vdso linker warning for orphan sections")
---
 arch/powerpc/kernel/vdso/vdso32.lds.S | 4 +++-
 arch/powerpc/kernel/vdso/vdso64.lds.S | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)
arch/powerpc/kernel/vdso/vdso32.lds.S +3 -1
index 426e1ccc6971..8f57107000a2 100644
@@ -74,6 +74,8 @@ SECTIONS
 	.got		: { *(.got) }			:text
 	.plt		: { *(.plt) }
 
+	.rela.dyn	: { *(.rela .rela*) }
+
 	_end = .;
 	__end = .;
 	PROVIDE(end = .);
@@ -87,7 +89,7 @@ SECTIONS
 		*(.branch_lt)
 		*(.data .data.* .gnu.linkonce.d.* .sdata*)
 		*(.bss .sbss .dynbss .dynsbss)
-		*(.got1 .glink .iplt .rela*)
+		*(.got1 .glink .iplt)
 	}
 }
 
arch/powerpc/kernel/vdso/vdso64.lds.S +2 -2
index bda6c8cdd459..400819258c06 100644
@@ -69,7 +69,7 @@ SECTIONS
 	.eh_frame_hdr	: { *(.eh_frame_hdr) }		:text	:eh_frame_hdr
 	.eh_frame	: { KEEP (*(.eh_frame)) }	:text
 	.gcc_except_table : { *(.gcc_except_table) }
-	.rela.dyn ALIGN(8) : { *(.rela.dyn) }
+	.rela.dyn ALIGN(8) : { *(.rela .rela*) }
 
 	.got ALIGN(8)	: { *(.got .toc) }
 
@@ -86,7 +86,7 @@ SECTIONS
 		*(.data .data.* .gnu.linkonce.d.* .sdata*)
 		*(.bss .sbss .dynbss .dynsbss)
 		*(.opd)
-		*(.glink .iplt .plt .rela*)
+		*(.glink .iplt .plt)
 	}
 }
 
Signature · 1 line
2.44.0
#2 lore raw

PATCH 2/9 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
#3 lore raw

PATCH 3/9 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.

---
 arch/x86/include/asm/vdso/vsyscall.h | 7 +++++++
 drivers/char/random.c                | 5 +++--
 include/asm-generic/vdso/vsyscall.h  | 7 +++++++
 3 files changed, 17 insertions(+), 2 deletions(-)
arch/x86/include/asm/vdso/vsyscall.h +7 -0
index 972415a8be31..92b85189b8fe 100644
@@ -22,6 +22,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);
include/asm-generic/vdso/vsyscall.h +7 -0
index c835607f78ae..d44a828e34a4 100644
@@ -11,6 +11,13 @@ static __always_inline struct vdso_data *__arch_get_k_vdso_data(void)
 }
 #endif /* __arch_get_k_vdso_data */
 
+#ifndef __arch_get_k_vdso_rng_data
+static __always_inline struct vdso_rng_data *__arch_get_k_vdso_rng_data(void)
+{
+	return NULL;
+}
+#endif /* __arch_get_k_vdso_rng_data */
+
 #ifndef __arch_update_vsyscall
 static __always_inline void __arch_update_vsyscall(struct vdso_data *vdata,
 						   struct timekeeper *tk)
Signature · 1 line
2.44.0
#4 lore raw

Re: [PATCH 3/9] vdso: Add __arch_get_k_vdso_rng_data()

kernel test robot noticed the following build errors:

Hi Christophe,

kernel test robot noticed the following build errors:

[auto build test ERROR on powerpc/next]
[also build test ERROR on powerpc/fixes crng-random/master shuah-kselftest/next shuah-kselftest/fixes linus/master v6.11-rc3 next-20240816] [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/powerpc-vdso-Don-t-discard-rela-sections/20240816-223917
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
patch link:    https://lore.kernel.org/r/a7bdbbb14d8635c1e33ada7982cf2cd1a8321e5c.1723817900.git.christophe.leroy%40csgroup.eu
patch subject: [PATCH 3/9] vdso: Add __arch_get_k_vdso_rng_data()
config: x86_64-randconfig-003-20240817 (https://download.01.org/0day-ci/archive/20240817/[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/20240817/[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 errors (new ones prefixed by >>):

>> ld: drivers/char/random.o:arch/x86/include/asm/vdso/vsyscall.h:13: multiple definition of `_vdso_rng_data'; kernel/time/vsyscall.o:arch/x86/include/asm/vdso/vsyscall.h:13: first defined here
>> ld: drivers/char/random.o:arch/x86/include/asm/vdso/vsyscall.h:12: multiple definition of `_vdso_data'; kernel/time/vsyscall.o:arch/x86/include/asm/vdso/vsyscall.h:12: first defined here
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

Re: [PATCH 3/9] vdso: Add __arch_get_k_vdso_rng_data()

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

Hi Christophe,

kernel test robot noticed the following build errors:

[auto build test ERROR on powerpc/next]
[also build test ERROR on powerpc/fixes crng-random/master shuah-kselftest/next shuah-kselftest/fixes linus/master v6.11-rc3 next-20240816] [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/powerpc-vdso-Don-t-discard-rela-sections/20240816-223917
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
patch link:    https://lore.kernel.org/r/a7bdbbb14d8635c1e33ada7982cf2cd1a8321e5c.1723817900.git.christophe.leroy%40csgroup.eu
patch subject: [PATCH 3/9] vdso: Add __arch_get_k_vdso_rng_data()
config: x86_64-buildonly-randconfig-002-20240817 (https://download.01.org/0day-ci/archive/20240817/[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/20240817/[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 errors (new ones prefixed by >>):

>> ld: drivers/char/random.o:(.vvar__vdso_rng_data+0x0): multiple definition of `_vdso_rng_data'; kernel/time/vsyscall.o:(.vvar__vdso_rng_data+0x0): first defined here
>> ld: drivers/char/random.o:(.vvar__vdso_data+0x0): multiple definition of `_vdso_data'; kernel/time/vsyscall.o:(.vvar__vdso_data+0x0): first defined here
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#6 lore raw

PATCH 4/9 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 including 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
#7 lore raw

PATCH 5/9 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 | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
lib/vdso/getrandom.c +9 -5
index b230f0b10832..425a422651de 100644
@@ -73,11 +73,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
#8 lore raw

PATCH 6/9 vdso: Only use MAP_DROPPABLE when VM_DROPPABLE exists

In reply to Christophe Leroy

Commit 9651fcedf7b9 ("mm: add MAP_DROPPABLE for designating always lazily freeable mappings") only adds MAP_DROPPABLE for 64 bits architectures, so don't use it on 32 bits as it leads to ENOTSUPP on mmap().

---
 lib/vdso/getrandom.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
lib/vdso/getrandom.c +5 -1
index 425a422651de..11ba6dbf3b5e 100644
@@ -78,7 +78,11 @@ __cvdso_getrandom_data(const struct vdso_rng_data *rng_info, void *buffer, size_
 
 		params->size_of_opaque_state = sizeof(*state);
 		params->mmap_prot = PROT_READ | PROT_WRITE;
-		params->mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS;
+		if (IS_ENABLED(CONFIG_64BIT))
+			params->mmap_flags = MAP_DROPPABLE | MAP_ANONYMOUS;
+		else
+			params->mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
+
 		for (i = 0; i < ARRAY_SIZE(params->reserved); i++)
 			params->reserved[i] = 0;
 
Signature · 1 line
2.44.0
#9 lore raw

PATCH 7/9 powerpc: Add little endian variants of LHZX_BE and friends

In reply to Christophe Leroy

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

And move it outside of __powerpc64__ #ifdef so that it can also be used for PPC32.

---
 arch/powerpc/include/asm/asm-compat.h | 40 +++++++++++++++++----------
 1 file changed, 26 insertions(+), 14 deletions(-)
arch/powerpc/include/asm/asm-compat.h +26 -14
index 2bc53c646ccd..ef8e79ae669a 100644
@@ -25,20 +25,6 @@
 #define PPC_LR_STKOFF	16
 #define PPC_MIN_STKFRM	112
 
-#ifdef __BIG_ENDIAN__
-#define LHZX_BE	stringify_in_c(lhzx)
-#define LWZX_BE	stringify_in_c(lwzx)
-#define LDX_BE	stringify_in_c(ldx)
-#define STWX_BE	stringify_in_c(stwx)
-#define STDX_BE	stringify_in_c(stdx)
-#else
-#define LHZX_BE	stringify_in_c(lhbrx)
-#define LWZX_BE	stringify_in_c(lwbrx)
-#define LDX_BE	stringify_in_c(ldbrx)
-#define STWX_BE	stringify_in_c(stwbrx)
-#define STDX_BE	stringify_in_c(stdbrx)
-#endif
-
 #else /* 32-bit */
 
 /* operations for longs and pointers */
@@ -61,4 +47,30 @@
 
 #endif
 
+#ifdef __BIG_ENDIAN__
+#define LHZX_BE	stringify_in_c(lhzx)
+#define LWZX_BE	stringify_in_c(lwzx)
+#define LDX_BE	stringify_in_c(ldx)
+#define STWX_BE	stringify_in_c(stwx)
+#define STDX_BE	stringify_in_c(stdx)
+
+#define LHZX_LE	stringify_in_c(lhbrx)
+#define LWZX_LE	stringify_in_c(lwbrx)
+#define LDX_LE	stringify_in_c(ldbrx)
+#define STWX_LE	stringify_in_c(stwbrx)
+#define STDX_LE	stringify_in_c(stdbrx)
+#else
+#define LHZX_BE	stringify_in_c(lhbrx)
+#define LWZX_BE	stringify_in_c(lwbrx)
+#define LDX_BE	stringify_in_c(ldbrx)
+#define STWX_BE	stringify_in_c(stwbrx)
+#define STDX_BE	stringify_in_c(stdbrx)
+
+#define LHZX_LE	stringify_in_c(lhzx)
+#define LWZX_LE	stringify_in_c(lwzx)
+#define LDX_LE	stringify_in_c(ldx)
+#define STWX_LE	stringify_in_c(stwx)
+#define STDX_LE	stringify_in_c(stdx)
+#endif
+
 #endif /* _ASM_POWERPC_ASM_COMPAT_H */
Signature · 1 line
2.44.0
#10 lore raw

PATCH 8/9 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.
	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/vdso/getrandom.h    |  61 ++++
 arch/powerpc/include/asm/vdso/vsyscall.h     |   7 +
 arch/powerpc/include/asm/vdso_datapage.h     |   2 +
 arch/powerpc/kernel/asm-offsets.c            |   1 +
 arch/powerpc/kernel/vdso/Makefile            |  41 ++-
 arch/powerpc/kernel/vdso/getrandom.S         |  62 ++++
 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        |  12 +
 11 files changed, 482 insertions(+), 4 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..f61cfa9ac360 100644
@@ -311,6 +311,7 @@ config PPC
 	select SYSCTL_EXCEPTION_TRACE
 	select THREAD_INFO_IN_TASK
 	select TRACE_IRQFLAGS_SUPPORT
+	select VDSO_GETRANDOM			if !COMPAT
 	#
 	# Please keep this list sorted alphabetically.
 	#
arch/powerpc/include/asm/vdso/getrandom.h new file +61 -0

61 lines not on this page · raw message

arch/powerpc/include/asm/vdso/vsyscall.h +7 -0
index 48cf23f1e273..ed60a21bba40 100644
@@ -17,6 +17,13 @@ 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;
+}
+#define __arch_get_k_vdso_rng_data __arch_get_k_vdso_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 +37 -4

78 lines not on this page · raw message

arch/powerpc/kernel/vdso/getrandom.S new file +62 -0

62 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 +12 -0
new file mode 100644
index 000000000000..f442396c3715
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Powerpc userspace implementations of getrandom()
+ */
+#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
#11 lore raw

PATCH 9/9 selftests: [NOT TO BE MERGED] Modifications for testing VDSO getrandom implementation on PPC32

In reply to Christophe Leroy

arch/.../entry/vdso/ is specific to x86. Every architecture has a different path. On powerpc it is in arch/.../kernel/vdso/

vdso_test_getrandom is a bit too long with 25000000.

Something is wrong with macros INT_MAX ... :

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
      |      ^~~~~~~~
---
 include/vdso/limits.h                              | 4 ++--
 tools/testing/selftests/vDSO/Makefile              | 2 +-
 tools/testing/selftests/vDSO/vdso_test_getrandom.c | 6 +++---
 3 files changed, 6 insertions(+), 6 deletions(-)
include/vdso/limits.h +2 -2
index 0197888ad0e0..b0459332e45f 100644
@@ -5,10 +5,10 @@
 #define USHRT_MAX	((unsigned short)~0U)
 #define SHRT_MAX	((short)(USHRT_MAX >> 1))
 #define SHRT_MIN	((short)(-SHRT_MAX - 1))
-#define INT_MAX		((int)(~0U >> 1))
+#define INT_MAX		2147483647
 #define INT_MIN		(-INT_MAX - 1)
 #define UINT_MAX	(~0U)
-#define LONG_MAX	((long)(~0UL >> 1))
+#define LONG_MAX	2147483647
 #define LONG_MIN	(-LONG_MAX - 1)
 #define ULONG_MAX	(~0UL)
 #define LLONG_MAX	((long long)(~0ULL >> 1))
tools/testing/selftests/vDSO/Makefile +1 -1
index 3de8e7e052ae..8010e7be66c6 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)/arch/$(ARCH)/kernel/vdso/vgetrandom-chacha.S
 $(OUTPUT)/vdso_test_chacha: CFLAGS += -idirafter $(top_srcdir)/tools/include \
                                       -isystem $(top_srcdir)/arch/$(ARCH)/include \
                                       -isystem $(top_srcdir)/include \
tools/testing/selftests/vDSO/vdso_test_getrandom.c +3 -3
index 05122425a873..f25301c9d46b 100644
@@ -115,9 +115,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("LINUX_2.6.15", "__kernel_getrandom");
 	if (!grnd_ctx.fn) {
-		printf("__vdso_getrandom is missing!\n");
+		printf("__kernel_getrandom is missing!\n");
 		exit(KSFT_FAIL);
 	}
 	if (grnd_ctx.fn(NULL, 0, 0, &grnd_ctx.params, ~0UL) != 0) {
@@ -146,7 +146,7 @@ static ssize_t vgetrandom(void *buf, size_t len, unsigned long flags)
 	return grnd_ctx.fn(buf, len, flags, state, grnd_ctx.params.size_of_opaque_state);
 }
 
-enum { TRIALS = 25000000, THREADS = 256 };
+enum { TRIALS = 2500000, THREADS = 256 };
 
 static void *test_vdso_getrandom(void *)
 {
Signature · 1 line
2.44.0
#12 lore raw

Re: [PATCH 9/9] selftests: [NOT TO BE MERGED] Modifications for testing VDSO getrandom implementation on PPC32

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-rc3] [cannot apply to crng-random/master next-20240816]
[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/powerpc-vdso-Don-t-discard-rela-sections/20240816-223917
base:   https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next
patch link:    https://lore.kernel.org/r/376843e024ffa73793e8ed99b72d299c6b239799.1723817900.git.christophe.leroy%40csgroup.eu
patch subject: [PATCH 9/9] selftests: [NOT TO BE MERGED] Modifications for testing VDSO getrandom implementation on PPC32
config: x86_64-randconfig-161-20240817 (https://download.01.org/0day-ci/archive/20240818/[email protected]/config)
compiler: clang version 18.1.5 (https://github.com/llvm/llvm-project 617a15a9eac96088ae5e9134248d8236e34b91b1)

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]/

smatch warnings:
kernel/time/time.c:622 timespec64_to_jiffies() warn: always true condition '(sec >= ((((((((2147483647 >> 1) - 1) >> (32 - 10)) * ((1000000000 + 1000 / 2) / 1000)) / (1000000000)) << (1)) + (((((((2147483647 >> 1) - 1) >> (32 - 10)) * ((1000000000 + 1000 / 2) / 1000)) % (1000000000)) << (1)) + (1000000000) / 2) / (1000000000)) - 1)) => (0-u64max >= 0)'

vim +622 kernel/time/time.c

8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  595  
67b3f564cb1e769 kernel/time/time.c Randy Dunlap  2023-07-03  596  /**
67b3f564cb1e769 kernel/time/time.c Randy Dunlap  2023-07-03  597   * timespec64_to_jiffies - convert a timespec64 value to jiffies
67b3f564cb1e769 kernel/time/time.c Randy Dunlap  2023-07-03  598   * @value: pointer to &struct timespec64
67b3f564cb1e769 kernel/time/time.c Randy Dunlap  2023-07-03  599   *
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  600   * The TICK_NSEC - 1 rounds up the value to the next resolution.  Note
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  601   * that a remainder subtract here would not do the right thing as the
4bf07f6562a01a4 kernel/time/time.c Ingo Molnar   2021-03-22  602   * resolution values don't fall on second boundaries.  I.e. the line:
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  603   * nsec -= nsec % TICK_NSEC; is NOT a correct resolution rounding.
d78c9300c51d6ce kernel/time/time.c Andrew Hunter 2014-09-04  604   * Note that due to the small error in the multiplier here, this
d78c9300c51d6ce kernel/time/time.c Andrew Hunter 2014-09-04  605   * rounding is incorrect for sufficiently large values of tv_nsec, but
d78c9300c51d6ce kernel/time/time.c Andrew Hunter 2014-09-04  606   * well formed timespecs should have tv_nsec < NSEC_PER_SEC, so we're
d78c9300c51d6ce kernel/time/time.c Andrew Hunter 2014-09-04  607   * OK.
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  608   *
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  609   * Rather, we just shift the bits off the right.
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  610   *
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  611   * The >> (NSEC_JIFFIE_SC - SEC_JIFFIE_SC) converts the scaled nsec
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  612   * value to a scaled second value.
67b3f564cb1e769 kernel/time/time.c Randy Dunlap  2023-07-03  613   *
67b3f564cb1e769 kernel/time/time.c Randy Dunlap  2023-07-03  614   * Return: jiffies value
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  615   */
751addac78b6f20 kernel/time/time.c Arnd Bergmann 2019-10-24  616  unsigned long
751addac78b6f20 kernel/time/time.c Arnd Bergmann 2019-10-24  617  timespec64_to_jiffies(const struct timespec64 *value)
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  618  {
751addac78b6f20 kernel/time/time.c Arnd Bergmann 2019-10-24  619  	u64 sec = value->tv_sec;
751addac78b6f20 kernel/time/time.c Arnd Bergmann 2019-10-24  620  	long nsec = value->tv_nsec + TICK_NSEC - 1;
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  621  
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16 @622  	if (sec >= MAX_SEC_IN_JIFFIES){
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  623  		sec = MAX_SEC_IN_JIFFIES;
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  624  		nsec = 0;
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  625  	}
9ca308506062fc4 kernel/time/time.c Baolin Wang   2015-07-29  626  	return ((sec * SEC_CONVERSION) +
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  627  		(((u64)nsec * NSEC_CONVERSION) >>
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  628  		 (NSEC_JIFFIE_SC - SEC_JIFFIE_SC))) >> SEC_JIFFIE_SC;
8b9365d753d9870 kernel/time.c      Ingo Molnar   2007-02-16  629  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#13 lore raw
In reply to Christophe Leroy

Hi Christophe,

Thanks for this series. I'm looking forward to taking a close look at it. I'm currently traveling until the 26th without a laptop and no email except for lore+lei+mutt on my phone. So I'll review this in about 1 week. But I'm very happy to see it here.

On first glance, patch number 7 isn't okay. If you want this to work on 32bit, find an available bit for VM_DROPPABLE. Otherwise, just do this 64bit-only like many new features.

Jason

#14 lore raw
Quoted 1 line · Jason A. Donenfeld

On first glance, patch number 7 isn't okay. If you want this to work on

Sorry, I meant 6.