public inbox for linux-crypto@vger.kernel.org
 help / color / mirror / Atom feed
From: Heiko Carstens <hca@linux.ibm.com>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Harald Freudenberger <freude@linux.ibm.com>,
	Stefan Liebler <stli@linux.ibm.com>,
	linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-s390@vger.kernel.org
Subject: Re: [PATCH 7/7] s390/vdso: Wire up getrandom() vdso implementation
Date: Sat, 14 Sep 2024 19:42:46 +0200	[thread overview]
Message-ID: <20240914174246.8394-A-hca@linux.ibm.com> (raw)
In-Reply-To: <ZuSRKLFdYI1gCHh9@zx2c4.com>

On Fri, Sep 13, 2024 at 09:23:20PM +0200, Jason A. Donenfeld wrote:
> > > >   CC       vdso_test_chacha
> > > > /home/zx2c4/Projects/random-linux/tools/testing/selftests/../../../tools/arch/s390/vdso/vgetrandom-chacha.S: Assembler messages:
> > > > /home/zx2c4/Projects/random-linux/tools/testing/selftests/../../../tools/arch/s390/vdso/vgetrandom-chacha.S:147: Error: Unrecognized opcode: `alsih'
> > > > 
> > > > Any idea what's up?
> > > 
> > > Looks like I needed `-march=arch9`. I can potentially rebuild my
> > > toolchains to do this by default, though, if that's a normal thing to
> > > have and this is just my toolchain being crappy. Or, if it's not a
> > > normal thing to have, do we need to add it to the selftests Makefile?
> > 
> > That needs to be fixed differently, since the kernel build would also
> > fail when building for z10. Could you squash the below fix into this
> > patch, please?
> 
> Done.
> 
> > So for the kernel itself including the vdso code, everything is
> > correct now. But similar checks are missing within vdso_test_chacha.c.
> > I'll provide something for that, so that the test case will be skipped
> > if the required instructions are missing, but not today.
> 
> Okay. I would assume no rush there, because it's unlikely there are
> those machines part of kselftest fleets anyway?

There was another surprise waiting for me: the ALTERNATIVE macro
within the tools header file is defined in a way that it omits
everything. So I was just lucky that the s390 chacha assembler code
worked, since even without the alternatives the code is working, but
executes code for newer CPU generations, which it shouldn't.

So below is a diff which fixes both:

- Add an s390 specific ALTERNATIVE macro that emits code that is
  supposed to work on older CPU generations, instead of no code

- Add a hwcap check to make sure that all CPU capabilities required to
  run the assembler code are present

It probably makes sense to squash this also into
"s390/vdso: Wire up getrandom() vdso implementation".

Please feel free to change the code in whatever way you like.
If you prefer separate patches, I will provide them.

diff --git a/tools/include/asm/alternative.h b/tools/include/asm/alternative.h
index 7ce02a223732..68dc894c0892 100644
--- a/tools/include/asm/alternative.h
+++ b/tools/include/asm/alternative.h
@@ -2,8 +2,18 @@
 #ifndef _TOOLS_ASM_ALTERNATIVE_ASM_H
 #define _TOOLS_ASM_ALTERNATIVE_ASM_H
 
+#if defined(__s390x__)
+#ifdef __ASSEMBLY__
+.macro ALTERNATIVE oldinstr, newinstr, feature
+	\oldinstr
+.endm
+#endif
+#else
+	
 /* Just disable it so we can build arch/x86/lib/memcpy_64.S for perf bench: */
 
 #define ALTERNATIVE #
 
 #endif
+
+#endif
diff --git a/tools/testing/selftests/vDSO/vdso_test_chacha.c b/tools/testing/selftests/vDSO/vdso_test_chacha.c
index e81d72c9882e..f1eace68a63b 100644
--- a/tools/testing/selftests/vDSO/vdso_test_chacha.c
+++ b/tools/testing/selftests/vDSO/vdso_test_chacha.c
@@ -5,11 +5,34 @@
 
 #include <tools/le_byteshift.h>
 #include <sys/random.h>
+#include <sys/auxv.h>
 #include <string.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include "../kselftest.h"
 
+#if defined(__s390x__)
+
+#ifndef HWCAP_S390_VX
+#define HWCAP_S390_VX 2048
+#endif
+
+static bool cpu_has_capabilities(void)
+{
+	if (getauxval(AT_HWCAP) & HWCAP_S390_VX)
+		return true;
+	return false;
+}
+
+#else
+
+static bool cpu_has_capabilities(void)
+{
+	return true;
+}
+
+#endif
+
 static uint32_t rol32(uint32_t word, unsigned int shift)
 {
 	return (word << (shift & 31)) | (word >> ((-shift) & 31));
@@ -67,6 +90,8 @@ int main(int argc, char *argv[])
 	uint8_t output1[BLOCK_SIZE * BLOCKS], output2[BLOCK_SIZE * BLOCKS];
 
 	ksft_print_header();
+	if (!cpu_has_capabilities())
+		ksft_exit_skip("Required CPU capabilities missing\n");
 	ksft_set_plan(1);
 
 	for (unsigned int trial = 0; trial < TRIALS; ++trial) {

  reply	other threads:[~2024-09-14 17:42 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-13 13:05 [PATCH 0/7] s390/vdso: getrandom() vdso implementation Heiko Carstens
2024-09-13 13:05 ` [PATCH 1/7] s390/facility: Disable compile time optimization for decompressor code Heiko Carstens
2024-09-13 13:05 ` [PATCH 2/7] s390/alternatives: Remove ALT_FACILITY_EARLY Heiko Carstens
2024-09-13 13:05 ` [PATCH 3/7] s390/facility: Let test_facility() generate static branch if possible Heiko Carstens
2024-09-13 13:05 ` [PATCH 4/7] s390/module: Provide find_section() helper Heiko Carstens
2024-09-13 13:05 ` [PATCH 5/7] s390/vdso: Allow alternatives in vdso code Heiko Carstens
2024-09-13 13:05 ` [PATCH 6/7] s390/vdso: Move vdso symbol handling to separate header file Heiko Carstens
2024-09-13 13:05 ` [PATCH 7/7] s390/vdso: Wire up getrandom() vdso implementation Heiko Carstens
2024-09-13 13:53   ` Jason A. Donenfeld
2024-09-13 14:16     ` Heiko Carstens
2024-09-13 14:57       ` Jason A. Donenfeld
2024-09-13 17:37         ` Heiko Carstens
2024-09-13 14:13   ` Harald Freudenberger
2024-09-13 15:13   ` Jason A. Donenfeld
2024-09-13 15:22     ` Jason A. Donenfeld
2024-09-13 15:24       ` Jason A. Donenfeld
2024-09-13 17:32       ` Heiko Carstens
2024-09-13 19:23         ` Jason A. Donenfeld
2024-09-14 17:42           ` Heiko Carstens [this message]
2024-09-16  9:08             ` Jason A. Donenfeld
2024-09-16 11:01               ` Heiko Carstens
2024-09-16 11:23                 ` Jason A. Donenfeld
2024-09-13 13:52 ` [PATCH 0/7] s390/vdso: " Jason A. Donenfeld
2024-09-13 13:56   ` Jason A. Donenfeld
2024-09-13 14:29     ` Heiko Carstens
2024-09-13 14:57       ` Jason A. Donenfeld

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240914174246.8394-A-hca@linux.ibm.com \
    --to=hca@linux.ibm.com \
    --cc=Jason@zx2c4.com \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=freude@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=stli@linux.ibm.com \
    --cc=svens@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox