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: [PATCH 3/7] s390/facility: Let test_facility() generate static branch if possible
Date: Fri, 13 Sep 2024 15:05:39 +0200 [thread overview]
Message-ID: <20240913130544.2398678-4-hca@linux.ibm.com> (raw)
In-Reply-To: <20240913130544.2398678-1-hca@linux.ibm.com>
Let test_facility() generate a branch instruction if the tested facility is
a constant, and where the result cannot be evaluated during compile
time. The branch instruction defaults to "false" and is patched to nop
(branch not taken) if the tested facility is available.
This avoids runtime checks and is similar to x86's static_cpu_has() and
arm64's alternative_has_cap_likely().
Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
---
arch/s390/include/asm/facility.h | 37 +++++++++++++++++++++++++-------
1 file changed, 29 insertions(+), 8 deletions(-)
diff --git a/arch/s390/include/asm/facility.h b/arch/s390/include/asm/facility.h
index 65ebf86506cd..715bcf8fb69a 100644
--- a/arch/s390/include/asm/facility.h
+++ b/arch/s390/include/asm/facility.h
@@ -14,7 +14,7 @@
#include <linux/string.h>
#include <linux/types.h>
#include <linux/preempt.h>
-
+#include <asm/alternative.h>
#include <asm/lowcore.h>
#define MAX_FACILITY_BIT (sizeof(stfle_fac_list) * 8)
@@ -39,30 +39,51 @@ static inline void __clear_facility(unsigned long nr, void *facilities)
ptr[nr >> 3] &= ~(0x80 >> (nr & 7));
}
-static inline int __test_facility(unsigned long nr, void *facilities)
+static __always_inline bool __test_facility(unsigned long nr, void *facilities)
{
unsigned char *ptr;
if (nr >= MAX_FACILITY_BIT)
- return 0;
+ return false;
ptr = (unsigned char *) facilities + (nr >> 3);
return (*ptr & (0x80 >> (nr & 7))) != 0;
}
+/*
+ * __test_facility_constant() generates a single instruction branch. If the
+ * tested facility is available (likely) the branch is patched into a nop.
+ *
+ * Do not use this function unless you know what you are doing. All users are
+ * supposed to use test_facility() which will do the right thing.
+ */
+static __always_inline bool __test_facility_constant(unsigned long nr)
+{
+ asm goto(
+ ALTERNATIVE("brcl 15,%l[l_no]", "brcl 0,0", ALT_FACILITY(%[nr]))
+ :
+ : [nr] "i" (nr)
+ :
+ : l_no);
+ return true;
+l_no:
+ return false;
+}
+
/*
* The test_facility function uses the bit ordering where the MSB is bit 0.
* That makes it easier to query facility bits with the bit number as
* documented in the Principles of Operation.
*/
-static inline int test_facility(unsigned long nr)
+static __always_inline bool test_facility(unsigned long nr)
{
unsigned long facilities_als[] = { FACILITIES_ALS };
- if (__builtin_constant_p(nr) && nr < sizeof(facilities_als) * 8) {
- if (__test_facility(nr, &facilities_als)) {
- if (!__is_defined(__DECOMPRESSOR))
- return 1;
+ if (!__is_defined(__DECOMPRESSOR) && __builtin_constant_p(nr)) {
+ if (nr < sizeof(facilities_als) * 8) {
+ if (__test_facility(nr, &facilities_als))
+ return true;
}
+ return __test_facility_constant(nr);
}
return __test_facility(nr, &stfle_fac_list);
}
--
2.43.0
next prev parent reply other threads:[~2024-09-13 13:05 UTC|newest]
Thread overview: 27+ 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 ` Heiko Carstens [this message]
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
2024-09-14 23:11 ` [PATCH] selftests: vDSO: check cpu caps before running chacha test Jason A. Donenfeld
2024-09-16 9:08 ` [PATCH 7/7] s390/vdso: Wire up getrandom() vdso implementation 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=20240913130544.2398678-4-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