Skip to contentSkip to conversation

ARM: add module autoloading support for crypto modules

PATCH Superseded Ard Biesheuvel 2 participants 11 messages Oct 18, 2016 – Jan 4, 2017

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover ARM: add module autoloading support for crypto modules

This series wires up the crypto modules that use the ARM 32-bit versions of the ARMv8 Crypto Extensions to udev autoloading, by exposing the HWCAP2 feature bits via the CPU modalias. This is very similar to the arm64 implementation, with the notable exception that ARM has its CPU feature definitions split across HWCAP and HWCAP2.

Given that the crypto feature bits are all exposed via HWCAP2, and considering that there are currently no features exposed via HWCAP that are relevant to udev module autoloading, exposing HWCAP2 only should be sufficient, at least for now.

Note to Herbert: patches #2 - #5 all depend on #1, which requires an ack from Russell, so please don't pull anything until #1 has been acked and/or merged.

Ard Biesheuvel (5):
ARM: wire up HWCAP2 feature bits to the CPU modalias
crypto: arm/aes-ce - enable module autoloading based on CPU feature
bits
crypto: arm/ghash-ce - enable module autoloading based on CPU feature
bits
crypto: arm/sha1-ce - enable module autoloading based on CPU feature
bits
crypto: arm/sha2-ce - enable module autoloading based on CPU feature
bits

 arch/arm/Kconfig                  |  1 +
 arch/arm/crypto/aes-ce-glue.c     |  5 ++-
 arch/arm/crypto/ghash-ce-glue.c   |  6 ++--
 arch/arm/crypto/sha1-ce-glue.c    |  5 ++-
 arch/arm/crypto/sha2-ce-glue.c    |  5 ++-
 arch/arm/include/asm/cpufeature.h | 32 ++++++++++++++++++++
 6 files changed, 41 insertions(+), 13 deletions(-)
 create mode 100644 arch/arm/include/asm/cpufeature.h
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/5 ARM: wire up HWCAP2 feature bits to the CPU modalias

Wire up the generic support for exposing CPU feature bits via the modalias in /sys/device/system/cpu. This allows udev to automatically load modules for things like crypto algorithms that are implemented using optional instructions.

---
 arch/arm/Kconfig                  |  1 +
 arch/arm/include/asm/cpufeature.h | 32 ++++++++++++++++++++
 2 files changed, 33 insertions(+)
arch/arm/Kconfig +1 -0
index b5d529fdffab..1a0c6a486a9c 100644
@@ -21,6 +21,7 @@ config ARM
 	select GENERIC_ALLOCATOR
 	select GENERIC_ATOMIC64 if (CPU_V7M || CPU_V6 || !CPU_32v6K || !AEABI)
 	select GENERIC_CLOCKEVENTS_BROADCAST if SMP
+	select GENERIC_CPU_AUTOPROBE
 	select GENERIC_EARLY_IOREMAP
 	select GENERIC_IDLE_POLL_SETUP
 	select GENERIC_IRQ_PROBE
arch/arm/include/asm/cpufeature.h new file +32 -0
new file mode 100644
index 000000000000..19c3dddd901a
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2016 Linaro Ltd. <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef __ASM_CPUFEATURE_H
+#define __ASM_CPUFEATURE_H
+
+#include <asm/hwcap.h>
+
+/*
+ * Due to the fact that ELF_HWCAP is a 32-bit type on ARM, and given the number
+ * of optional CPU features it defines, ARM's CPU capability bits have been
+ * divided across separate elf_hwcap and elf_hwcap2 variables, each of which
+ * covers a subset of the available CPU features.
+ *
+ * Currently, only a few of those are suitable for automatic module loading
+ * (which is the primary use case of this facility) and those happen to be all
+ * covered by HWCAP2. So let's only expose those via the CPU modalias for now.
+ */
+#define MAX_CPU_FEATURES	(8 * sizeof(elf_hwcap2))
+#define cpu_feature(x)		ilog2(HWCAP2_ ## x)
+
+static inline bool cpu_have_feature(unsigned int num)
+{
+	return elf_hwcap2 & (1UL << num);
+}
+
+#endif
Signature · 1 line
2.7.4
#2 lore raw

PATCH 1/5 ARM: wire up HWCAP2 feature bits to the CPU modalias

Quoted 11 lines · Ard Biesheuvel

Wire up the generic support for exposing CPU feature bits via the modalias in /sys/device/system/cpu. This allows udev to automatically load modules for things like crypto algorithms that are implemented using optional instructions.

---
 arch/arm/Kconfig                  |  1 +
 arch/arm/include/asm/cpufeature.h | 32 ++++++++++++++++++++
 2 files changed, 33 insertions(+)

Russell,

do you have any concerns regarding this patch? If not, I will drop it into the patch system.

Herbert,

I will resend the followup patches in this series to linux-crypto@ once this prerequisite is in place

Thanks,
Ard.

Quoted 53 lines
arch/arm/Kconfig +1 -0
index b5d529fdffab..1a0c6a486a9c 100644
@@ -21,6 +21,7 @@ config ARM
        select GENERIC_ALLOCATOR
        select GENERIC_ATOMIC64 if (CPU_V7M || CPU_V6 || !CPU_32v6K || !AEABI)
        select GENERIC_CLOCKEVENTS_BROADCAST if SMP
+       select GENERIC_CPU_AUTOPROBE
        select GENERIC_EARLY_IOREMAP
        select GENERIC_IDLE_POLL_SETUP
        select GENERIC_IRQ_PROBE
arch/arm/include/asm/cpufeature.h new file +32 -1
new file mode 100644
index 000000000000..19c3dddd901a
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2016 Linaro Ltd. <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef __ASM_CPUFEATURE_H
+#define __ASM_CPUFEATURE_H
+
+#include <asm/hwcap.h>
+
+/*
+ * Due to the fact that ELF_HWCAP is a 32-bit type on ARM, and given the number
+ * of optional CPU features it defines, ARM's CPU capability bits have been
+ * divided across separate elf_hwcap and elf_hwcap2 variables, each of which
+ * covers a subset of the available CPU features.
+ *
+ * Currently, only a few of those are suitable for automatic module loading
+ * (which is the primary use case of this facility) and those happen to be all
+ * covered by HWCAP2. So let's only expose those via the CPU modalias for now.
+ */
+#define MAX_CPU_FEATURES       (8 * sizeof(elf_hwcap2))
+#define cpu_feature(x)         ilog2(HWCAP2_ ## x)
+
+static inline bool cpu_have_feature(unsigned int num)
+{
+       return elf_hwcap2 & (1UL << num);
+}
+
+#endif
--

2.7.4

#3 lore raw

PATCH 1/5 ARM: wire up HWCAP2 feature bits to the CPU modalias

Quoted 13 lines · Ard Biesheuvel
Quoted 11 lines · Ard Biesheuvel

Wire up the generic support for exposing CPU feature bits via the modalias in /sys/device/system/cpu. This allows udev to automatically load modules for things like crypto algorithms that are implemented using optional instructions.

---
 arch/arm/Kconfig                  |  1 +
 arch/arm/include/asm/cpufeature.h | 32 ++++++++++++++++++++
 2 files changed, 33 insertions(+)
Quoted 4 lines

Russell,

do you have any concerns regarding this patch? If not, I will drop it into the patch system.

It's still something I need to look at... I've been offline last week, and sort-of offline the previous week, so I'm catching up.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#4 lore raw

PATCH 1/5 ARM: wire up HWCAP2 feature bits to the CPU modalias

Quoted 14 lines · Russell King - ARM Linux
Quoted 13 lines · Ard Biesheuvel
Quoted 11 lines · Ard Biesheuvel

Wire up the generic support for exposing CPU feature bits via the modalias in /sys/device/system/cpu. This allows udev to automatically load modules for things like crypto algorithms that are implemented using optional instructions.

---
 arch/arm/Kconfig                  |  1 +
 arch/arm/include/asm/cpufeature.h | 32 ++++++++++++++++++++
 2 files changed, 33 insertions(+)
Quoted 8 lines
Quoted 4 lines

Russell,

do you have any concerns regarding this patch? If not, I will drop it into the patch system.

It's still something I need to look at... I've been offline last week, and sort-of offline the previous week, so I'm catching up.

Hi Russell,

Any thoughts yet?

Thanks,
Ard.

#5 lore raw

PATCH 1/5 ARM: wire up HWCAP2 feature bits to the CPU modalias

Quoted 21 lines · Ard Biesheuvel
Quoted 19 lines · Russell King - ARM Linux
Quoted 17 lines · Ard Biesheuvel
Quoted 11 lines · Ard Biesheuvel

Wire up the generic support for exposing CPU feature bits via the modalias in /sys/device/system/cpu. This allows udev to automatically load modules for things like crypto algorithms that are implemented using optional instructions.

---
 arch/arm/Kconfig                  |  1 +
 arch/arm/include/asm/cpufeature.h | 32 ++++++++++++++++++++
 2 files changed, 33 insertions(+)

Russell,

do you have any concerns regarding this patch? If not, I will drop it into the patch system.

Quoted 7 lines
Quoted 3 lines

It's still something I need to look at... I've been offline last week, and sort-of offline the previous week, so I'm catching up.

Hi Russell,

Any thoughts yet?

None, and the patch is well buried now that it'll take me a while to find... back in mid-October? Yea, I'll have to drop everything and go digging through my mailboxes to find it... and I'm just catching up (again) after a week and a bit's time offline - yep, it's wonderful timing. Sorry, no time to look at it right now, you're not the only one wanting my attention at the moment.

Please try again in about a week's time - don't leave it a few months, and please include the patch.

Signature · 3 lines
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
#6 lore raw

PATCH 1/5 ARM: wire up HWCAP2 feature bits to the CPU modalias

Quoted 28 lines · Russell King - ARM Linux
Quoted 27 lines · Ard Biesheuvel
Quoted 14 lines · Russell King - ARM Linux
Quoted 13 lines · Ard Biesheuvel
Quoted 11 lines · Ard Biesheuvel

Wire up the generic support for exposing CPU feature bits via the modalias in /sys/device/system/cpu. This allows udev to automatically load modules for things like crypto algorithms that are implemented using optional instructions.

---
 arch/arm/Kconfig                  |  1 +
 arch/arm/include/asm/cpufeature.h | 32 ++++++++++++++++++++
 2 files changed, 33 insertions(+)
Quoted 8 lines
Quoted 4 lines

Russell,

do you have any concerns regarding this patch? If not, I will drop it into the patch system.

It's still something I need to look at... I've been offline last week, and sort-of offline the previous week, so I'm catching up.

Hi Russell,

Quoted 9 lines
Quoted 1 line

Any thoughts yet?

None, and the patch is well buried now that it'll take me a while to find... back in mid-October? Yea, I'll have to drop everything and go digging through my mailboxes to find it... and I'm just catching up (again) after a week and a bit's time offline - yep, it's wonderful timing. Sorry, no time to look at it right now, you're not the only one wanting my attention at the moment.

No worries. It is not exactly urgent, but it is a useful enhancement nonetheless.

Quoted 3 lines

Please try again in about a week's time - don't leave it a few months, and please include the patch.

OK

#7 lore raw

PATCH 2/5 crypto: arm/aes-ce - enable module autoloading based on CPU feature bits

In reply to Ard Biesheuvel

Make the module autoloadable by tying it to the CPU feature bit that describes whether the optional instructions it relies on are implemented by the current CPU.

---
 arch/arm/crypto/aes-ce-glue.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm/crypto/aes-ce-glue.c +2 -3
index aef022a87c53..5b1af6f8b2b5 100644
@@ -14,6 +14,7 @@
 #include <crypto/aes.h>
 #include <crypto/ablk_helper.h>
 #include <crypto/algapi.h>
+#include <linux/cpufeature.h>
 #include <linux/module.h>
 #include <crypto/xts.h>
 
@@ -515,8 +516,6 @@ static struct crypto_alg aes_algs[] = { {
 
 static int __init aes_init(void)
 {
-	if (!(elf_hwcap2 & HWCAP2_AES))
-		return -ENODEV;
 	return crypto_register_algs(aes_algs, ARRAY_SIZE(aes_algs));
 }
 
@@ -525,5 +524,5 @@ static void __exit aes_exit(void)
 	crypto_unregister_algs(aes_algs, ARRAY_SIZE(aes_algs));
 }
 
-module_init(aes_init);
+module_cpu_feature_match(AES, aes_init);
 module_exit(aes_exit);
Signature · 1 line
2.7.4
#8 lore raw

PATCH 3/5 crypto: arm/ghash-ce - enable module autoloading based on CPU feature bits

In reply to Ard Biesheuvel

Make the module autoloadable by tying it to the CPU feature bit that describes whether the optional instructions it relies on are implemented by the current CPU.

---
 arch/arm/crypto/ghash-ce-glue.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/arm/crypto/ghash-ce-glue.c +2 -4
index 7546b3c02466..6bac8bea9f1e 100644
@@ -15,6 +15,7 @@
 #include <crypto/cryptd.h>
 #include <crypto/internal/hash.h>
 #include <crypto/gf128mul.h>
+#include <linux/cpufeature.h>
 #include <linux/crypto.h>
 #include <linux/module.h>
 
@@ -311,9 +312,6 @@ static int __init ghash_ce_mod_init(void)
 {
 	int err;
 
-	if (!(elf_hwcap2 & HWCAP2_PMULL))
-		return -ENODEV;
-
 	err = crypto_register_shash(&ghash_alg);
 	if (err)
 		return err;
@@ -334,5 +332,5 @@ static void __exit ghash_ce_mod_exit(void)
 	crypto_unregister_shash(&ghash_alg);
 }
 
-module_init(ghash_ce_mod_init);
+module_cpu_feature_match(PMULL, ghash_ce_mod_init);
 module_exit(ghash_ce_mod_exit);
Signature · 1 line
2.7.4
#9 lore raw

PATCH 4/5 crypto: arm/sha1-ce - enable module autoloading based on CPU feature bits

In reply to Ard Biesheuvel

Make the module autoloadable by tying it to the CPU feature bit that describes whether the optional instructions it relies on are implemented by the current CPU.

---
 arch/arm/crypto/sha1-ce-glue.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm/crypto/sha1-ce-glue.c +2 -3
index 80bc2fcd241a..555f72b5e659 100644
@@ -11,6 +11,7 @@
 #include <crypto/internal/hash.h>
 #include <crypto/sha.h>
 #include <crypto/sha1_base.h>
+#include <linux/cpufeature.h>
 #include <linux/crypto.h>
 #include <linux/module.h>
 
@@ -82,8 +83,6 @@ static struct shash_alg alg = {
 
 static int __init sha1_ce_mod_init(void)
 {
-	if (!(elf_hwcap2 & HWCAP2_SHA1))
-		return -ENODEV;
 	return crypto_register_shash(&alg);
 }
 
@@ -92,5 +91,5 @@ static void __exit sha1_ce_mod_fini(void)
 	crypto_unregister_shash(&alg);
 }
 
-module_init(sha1_ce_mod_init);
+module_cpu_feature_match(SHA1, sha1_ce_mod_init);
 module_exit(sha1_ce_mod_fini);
Signature · 1 line
2.7.4
#10 lore raw

PATCH 5/5 crypto: arm/sha2-ce - enable module autoloading based on CPU feature bits

In reply to Ard Biesheuvel

Make the module autoloadable by tying it to the CPU feature bit that describes whether the optional instructions it relies on are implemented by the current CPU.

---
 arch/arm/crypto/sha2-ce-glue.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm/crypto/sha2-ce-glue.c +2 -3
index 0755b2d657f3..df4dcef054ae 100644
@@ -11,6 +11,7 @@
 #include <crypto/internal/hash.h>
 #include <crypto/sha.h>
 #include <crypto/sha256_base.h>
+#include <linux/cpufeature.h>
 #include <linux/crypto.h>
 #include <linux/module.h>
 
@@ -100,8 +101,6 @@ static struct shash_alg algs[] = { {
 
 static int __init sha2_ce_mod_init(void)
 {
-	if (!(elf_hwcap2 & HWCAP2_SHA2))
-		return -ENODEV;
 	return crypto_register_shashes(algs, ARRAY_SIZE(algs));
 }
 
@@ -110,5 +109,5 @@ static void __exit sha2_ce_mod_fini(void)
 	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
 }
 
-module_init(sha2_ce_mod_init);
+module_cpu_feature_match(SHA2, sha2_ce_mod_init);
 module_exit(sha2_ce_mod_fini);
Signature · 1 line
2.7.4