Skip to contentSkip to conversation

crypto: hw_random - Add new Exynos RNG driver

PATCH Superseded Krzysztof Kozlowski 3 participants 16 messages Mar 24, 2017

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Krzysztof Kozlowski

author maintainer
#0 lore raw

cover crypto: hw_random - Add new Exynos RNG driver

Hi,

This is a follow up of my questions around exynos-rng [1].

The existing exynos-rng has many issues. The most important one is that it is a pseudo RNG device but uses hw_random interface which does not allow proper seeding.

The RNG module on Exynos4 requires seeding. On newer SoCs (like Exynos5420) it can seed itself from a true RNG. Converting the existing driver to use TRNG would effectively drop support for Exynos4 and break compatibility with existing users.

Instead I decided to convert it to crypto API. In the future I hope to add support for seeding from TRNG module.

Tested with app [2].

Patches are independent. I will take the defconfig changes (2/3 and 3/3) through samsung-soc tree.

Best regards,
Krzysztof

[1] https://www.spinics.net/lists/arm-kernel/msg569641.html
[2] https://www.spinics.net/lists/arm-kernel/msg571184.html

Krzysztof Kozlowski (3):
crypto: hw_random - Add new Exynos RNG driver
ARM: exynos_defconfig: Enable Exynos RNG and user-space crypto API
ARM: multi_v7_defconfig: Enable Exynos RNG and user-space crypto API

 MAINTAINERS                         |   8 +
 arch/arm/configs/exynos_defconfig   |   6 +
 arch/arm/configs/multi_v7_defconfig |   6 +
 drivers/char/hw_random/Kconfig      |  14 --
 drivers/char/hw_random/Makefile     |   1 -
 drivers/char/hw_random/exynos-rng.c | 231 ---------------------------
 drivers/crypto/Kconfig              |  15 ++
 drivers/crypto/Makefile             |   1 +
 drivers/crypto/exynos-rng.c         | 306 ++++++++++++++++++++++++++++++++++++
 9 files changed, 342 insertions(+), 246 deletions(-)
 delete mode 100644 drivers/char/hw_random/exynos-rng.c
 create mode 100644 drivers/crypto/exynos-rng.c
Signature · 1 line
2.9.3
author maintainer
#1 lore raw

PATCH 1/3 crypto: hw_random - Add new Exynos RNG driver

Replace existing hw_ranndom/exynos-rng driver with a new, reworked one. This is a driver for pseudo random number generator block which on Exynos4 chipsets must be seeded with some value. On newer Exynos5420 chipsets it might seed itself from true random number generator block but this is not implemented yet.

New driver is a complete rework to use the crypto ALGAPI instead of hw_random API. Rationale for the change:
1. hw_random interface is for true RNG devices.
2. The old driver was seeding itself with jiffies which is not a reliable source for randomness.
3. Device generates five random numbers in each pass but old driver was returning only one thus its performance was reduced.

Compatibility with DeviceTree bindings is preserved.

New driver does not use runtime power management but manually enables and disables the clock when needed. This is preferred approach because using runtime PM just to toggle clock is huge overhead. Another difference is using the same seed after resuming from system suspend (previously driver was re-seeding itself again with jiffies).

---
 MAINTAINERS                         |   8 +
 drivers/char/hw_random/Kconfig      |  14 --
 drivers/char/hw_random/Makefile     |   1 -
 drivers/char/hw_random/exynos-rng.c | 231 ---------------------------
 drivers/crypto/Kconfig              |  15 ++
 drivers/crypto/Makefile             |   1 +
 drivers/crypto/exynos-rng.c         | 306 ++++++++++++++++++++++++++++++++++++
 7 files changed, 330 insertions(+), 246 deletions(-)
 delete mode 100644 drivers/char/hw_random/exynos-rng.c
 create mode 100644 drivers/crypto/exynos-rng.c
MAINTAINERS +8 -0
index affecc6d59f4..371fda859d43 100644
@@ -10977,6 +10977,14 @@ L:	alsa-devel at alsa-project.org (moderated for non-subscribers)
 S:	Supported
 F:	sound/soc/samsung/
 
+SAMSUNG EXYNOS PSEUDO RANDOM NUMBER GENERATOR (RNG) DRIVER
+M:	Krzysztof Kozlowski <[email protected]>
+L:	linux-crypto at vger.kernel.org
+L:	linux-samsung-soc at vger.kernel.org
+S:	Maintained
+F:	drivers/crypto/exynos-rng.c
+F:	Documentation/devicetree/bindings/rng/samsung,exynos-rng4.txt
+
 SAMSUNG FRAMEBUFFER DRIVER
 M:	Jingoo Han <[email protected]>
 L:	linux-fbdev at vger.kernel.org
drivers/char/hw_random/Kconfig +0 -14
index 0cafe08919c9..bdae802e7154 100644
@@ -294,20 +294,6 @@ config HW_RANDOM_POWERNV
 
 	  If unsure, say Y.
 
-config HW_RANDOM_EXYNOS
-	tristate "EXYNOS HW random number generator support"
-	depends on ARCH_EXYNOS || COMPILE_TEST
-	depends on HAS_IOMEM
-	default HW_RANDOM
-	---help---
-	  This driver provides kernel-side support for the Random Number
-	  Generator hardware found on EXYNOS SOCs.
-
-	  To compile this driver as a module, choose M here: the
-	  module will be called exynos-rng.
-
-	  If unsure, say Y.
-
 config HW_RANDOM_TPM
 	tristate "TPM HW Random Number Generator support"
 	depends on TCG_TPM
drivers/char/hw_random/Makefile +0 -1
index 5f52b1e4e7be..6f1eecc2045c 100644
@@ -24,7 +24,6 @@ obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o
 obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
 obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
-obj-$(CONFIG_HW_RANDOM_EXYNOS)	+= exynos-rng.o
 obj-$(CONFIG_HW_RANDOM_HISI)	+= hisi-rng.o
 obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
 obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
drivers/char/hw_random/exynos-rng.c deleted +0 -231

231 lines not on this page · raw message

drivers/crypto/Kconfig +15 -0
index 1a60626937e4..7d1fdf6a751c 100644
@@ -388,6 +388,21 @@ config CRYPTO_DEV_MXC_SCC
 	  This option enables support for the Security Controller (SCC)
 	  found in Freescale i.MX25 chips.
 
+config CRYPTO_DEV_EXYNOS_RNG
+	tristate "EXYNOS HW pseudo random number generator support"
+	depends on ARCH_EXYNOS || COMPILE_TEST
+	depends on HAS_IOMEM
+	select CRYPTO_RNG
+	---help---
+	  This driver provides kernel-side support through the
+	  cryptographic API for the pseudo random number generator hardware
+	  found on Exynos SoCs.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called exynos-rng.
+
+	  If unsure, say Y.
+
 config CRYPTO_DEV_S5P
 	tristate "Support for Samsung S5PV210/Exynos crypto accelerator"
 	depends on ARCH_S5PV210 || ARCH_EXYNOS || COMPILE_TEST
drivers/crypto/Makefile +1 -0
index 41ca339e89d3..9603f1862b30 100644
@@ -6,6 +6,7 @@ obj-$(CONFIG_CRYPTO_DEV_CAVIUM_ZIP) += cavium/
 obj-$(CONFIG_CRYPTO_DEV_CCP) += ccp/
 obj-$(CONFIG_CRYPTO_DEV_CHELSIO) += chelsio/
 obj-$(CONFIG_CRYPTO_DEV_CPT) += cavium/cpt/
+obj-$(CONFIG_CRYPTO_DEV_EXYNOS_RNG) += exynos-rng.o
 obj-$(CONFIG_CRYPTO_DEV_FSL_CAAM) += caam/
 obj-$(CONFIG_CRYPTO_DEV_GEODE) += geode-aes.o
 obj-$(CONFIG_CRYPTO_DEV_HIFN_795X) += hifn_795x.o
drivers/crypto/exynos-rng.c new file +306 -0

306 lines not on this page · raw message

Signature · 1 line
2.9.3
#2 lore raw

Am Freitag, 24. M?rz 2017, 15:24:44 CET schrieb Krzysztof Kozlowski:

Hi Krzysztof,

Quoted 28 lines
drivers/crypto/exynos-rng.c new file +28 -0
+
+static int exynos_rng_set_seed(struct exynos_rng_dev *rng,
+			       const u8 *seed, unsigned int slen)
+{
+	int ret, i;
+	u32 val;
+
+	dev_dbg(rng->dev, "Seeding with %u bytes\n", slen);
+
+	ret = clk_prepare_enable(rng->clk);
+	if (ret)
+		return ret;
+
+	if (slen < EXYNOS_RNG_SEED_SIZE) {
+		dev_warn(rng->dev, "Seed too short (only %u bytes)\n", slen);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	for (i = 0 ; i < EXYNOS_RNG_SEED_REGS ; i++) {
+		val = seed[i * 4] << 24;
+		val |= seed[i * 4 + 1] << 16;
+		val |= seed[i * 4 + 2] << 8;
+		val |= seed[i * 4 + 3] << 0;
+
+		exynos_rng_writel(rng, val, EXYNOS_RNG_SEED(i));
+	}
+

drivers/crypto/exynos-rng.c line 181

Quoted 10 lines
drivers/crypto/exynos-rng.c new file +10 -0
+	val = exynos_rng_readl(rng, EXYNOS_RNG_STATUS);
+	if (!(val & EXYNOS_RNG_STATUS_SEED_SETTING_DONE)) {
+		dev_warn(rng->dev, "Seed setting not finished\n");
+		ret = -EIO;
+		goto out;
+	}
+
+	ret = 0;
+	/* Save seed for suspend */
+	memcpy(rng->seed_save, seed, slen);

Is this really necessary? If you need to save some seed, shouldn't that be an output of the DRNG and not the real seed?

Besides, how do you know that slen is not larger than EXYNOS_RNG_SEED_SIZE?

Ciao
Stephan

author maintainer
#3 lore raw
Quoted 37 lines · Stephan M?ller

Am Freitag, 24. M?rz 2017, 15:24:44 CET schrieb Krzysztof Kozlowski:

Hi Krzysztof,

Quoted 33 lines
drivers/crypto/exynos-rng.c new file +33 -0
+
+static int exynos_rng_set_seed(struct exynos_rng_dev *rng,
+			       const u8 *seed, unsigned int slen)
+{
+	int ret, i;
+	u32 val;
+
+	dev_dbg(rng->dev, "Seeding with %u bytes\n", slen);
+
+	ret = clk_prepare_enable(rng->clk);
+	if (ret)
+		return ret;
+
+	if (slen < EXYNOS_RNG_SEED_SIZE) {
+		dev_warn(rng->dev, "Seed too short (only %u bytes)\n", slen);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	for (i = 0 ; i < EXYNOS_RNG_SEED_REGS ; i++) {
+		val = seed[i * 4] << 24;
+		val |= seed[i * 4 + 1] << 16;
+		val |= seed[i * 4 + 2] << 8;
+		val |= seed[i * 4 + 3] << 0;
+
+		exynos_rng_writel(rng, val, EXYNOS_RNG_SEED(i));
+	}
+
+	val = exynos_rng_readl(rng, EXYNOS_RNG_STATUS);
+	if (!(val & EXYNOS_RNG_STATUS_SEED_SETTING_DONE)) {
+		dev_warn(rng->dev, "Seed setting not finished\n");
+		ret = -EIO;
+		goto out;
Quoted 8 lines

drivers/crypto/exynos-rng.c line 181

Quoted 5 lines
drivers/crypto/exynos-rng.c new file +5 -0
+	}
+
+	ret = 0;
+	/* Save seed for suspend */
+	memcpy(rng->seed_save, seed, slen);

Is this really necessary? If you need to save some seed, shouldn't that be an output of the DRNG and not the real seed?

When suspended to RAM device will loose the contents of registers (including SEED registers) so it has to be initialized with something on resume.

The seed registers are write-only so I cannot read them and store contents just before suspend.

I understand that real seed should not be stored... but then if I am not able to re-seed it with same values, I will loose the continuous and reproducible pseudo-random generation after suspend. Aren't this expected out of PRNG?

Quoted 1 line

Besides, how do you know that slen is not larger than EXYNOS_RNG_SEED_SIZE?

Right, there is a overflow here. It should be sizeof(rng->seed_save);

Thanks for review!

Best regards,
Krzysztof

#4 lore raw

Am Freitag, 24. M?rz 2017, 15:43:48 CET schrieb Krzysztof Kozlowski:

Hi Krzysztof,

Quoted 51 lines · Stephan M?ller
Quoted 37 lines

Am Freitag, 24. M?rz 2017, 15:24:44 CET schrieb Krzysztof Kozlowski:

Hi Krzysztof,

Quoted 33 lines
drivers/crypto/exynos-rng.c new file +33 -0
+
+static int exynos_rng_set_seed(struct exynos_rng_dev *rng,
+			       const u8 *seed, unsigned int slen)
+{
+	int ret, i;
+	u32 val;
+
+	dev_dbg(rng->dev, "Seeding with %u bytes\n", slen);
+
+	ret = clk_prepare_enable(rng->clk);
+	if (ret)
+		return ret;
+
+	if (slen < EXYNOS_RNG_SEED_SIZE) {
+		dev_warn(rng->dev, "Seed too short (only %u bytes)\n", slen);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	for (i = 0 ; i < EXYNOS_RNG_SEED_REGS ; i++) {
+		val = seed[i * 4] << 24;
+		val |= seed[i * 4 + 1] << 16;
+		val |= seed[i * 4 + 2] << 8;
+		val |= seed[i * 4 + 3] << 0;
+
+		exynos_rng_writel(rng, val, EXYNOS_RNG_SEED(i));
+	}
+
+	val = exynos_rng_readl(rng, EXYNOS_RNG_STATUS);
+	if (!(val & EXYNOS_RNG_STATUS_SEED_SETTING_DONE)) {
+		dev_warn(rng->dev, "Seed setting not finished\n");
+		ret = -EIO;
+		goto out;
Quoted 8 lines

drivers/crypto/exynos-rng.c line 181

Quoted 5 lines
drivers/crypto/exynos-rng.c new file +5 -0
+	}
+
+	ret = 0;
+	/* Save seed for suspend */
+	memcpy(rng->seed_save, seed, slen);

Is this really necessary? If you need to save some seed, shouldn't that be an output of the DRNG and not the real seed?

When suspended to RAM device will loose the contents of registers (including SEED registers) so it has to be initialized with something on resume.

Quoted 7 lines

The seed registers are write-only so I cannot read them and store contents just before suspend.

I understand that real seed should not be stored... but then if I am not able to re-seed it with same values, I will loose the continuous and reproducible pseudo-random generation after suspend. Aren't this expected out of PRNG?

An RNG has to be stateless from the perspective of the caller -- this is the core implication of entropy.

Then, if you add the initial seed after the RNG lost its state implies that the same sequence of random numbers starts again. I.e. where is the randomness?

Quoted 5 lines
Quoted 3 lines

Besides, how do you know that slen is not larger than EXYNOS_RNG_SEED_SIZE?

Right, there is a overflow here. It should be sizeof(rng->seed_save);

shouldn't it be min(rng->seed_save, slen)?

Quoted 5 lines

Thanks for review!

Best regards,
Krzysztof

Ciao
Stephan

author maintainer
#5 lore raw
Quoted 63 lines · Stephan M?ller

Am Freitag, 24. M?rz 2017, 15:43:48 CET schrieb Krzysztof Kozlowski:

Hi Krzysztof,

Quoted 51 lines · Stephan M?ller
Quoted 37 lines

Am Freitag, 24. M?rz 2017, 15:24:44 CET schrieb Krzysztof Kozlowski:

Hi Krzysztof,

Quoted 33 lines
drivers/crypto/exynos-rng.c new file +33 -0
+
+static int exynos_rng_set_seed(struct exynos_rng_dev *rng,
+			       const u8 *seed, unsigned int slen)
+{
+	int ret, i;
+	u32 val;
+
+	dev_dbg(rng->dev, "Seeding with %u bytes\n", slen);
+
+	ret = clk_prepare_enable(rng->clk);
+	if (ret)
+		return ret;
+
+	if (slen < EXYNOS_RNG_SEED_SIZE) {
+		dev_warn(rng->dev, "Seed too short (only %u bytes)\n", slen);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	for (i = 0 ; i < EXYNOS_RNG_SEED_REGS ; i++) {
+		val = seed[i * 4] << 24;
+		val |= seed[i * 4 + 1] << 16;
+		val |= seed[i * 4 + 2] << 8;
+		val |= seed[i * 4 + 3] << 0;
+
+		exynos_rng_writel(rng, val, EXYNOS_RNG_SEED(i));
+	}
+
+	val = exynos_rng_readl(rng, EXYNOS_RNG_STATUS);
+	if (!(val & EXYNOS_RNG_STATUS_SEED_SETTING_DONE)) {
+		dev_warn(rng->dev, "Seed setting not finished\n");
+		ret = -EIO;
+		goto out;
Quoted 8 lines

drivers/crypto/exynos-rng.c line 181

Quoted 5 lines
drivers/crypto/exynos-rng.c new file +5 -0
+	}
+
+	ret = 0;
+	/* Save seed for suspend */
+	memcpy(rng->seed_save, seed, slen);

Is this really necessary? If you need to save some seed, shouldn't that be an output of the DRNG and not the real seed?

When suspended to RAM device will loose the contents of registers (including SEED registers) so it has to be initialized with something on resume.

Quoted 7 lines

The seed registers are write-only so I cannot read them and store contents just before suspend.

I understand that real seed should not be stored... but then if I am not able to re-seed it with same values, I will loose the continuous and reproducible pseudo-random generation after suspend. Aren't this expected out of PRNG?

Quoted 6 lines

An RNG has to be stateless from the perspective of the caller -- this is the core implication of entropy.

Then, if you add the initial seed after the RNG lost its state implies that the same sequence of random numbers starts again. I.e. where is the randomness?

Ahhh, yes, you are right. The device would not continue on its random generation because all state is lost anyway.

I'll use the generated random numbers.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Besides, how do you know that slen is not larger than EXYNOS_RNG_SEED_SIZE?

Right, there is a overflow here. It should be sizeof(rng->seed_save);

shouldn't it be min(rng->seed_save, slen)?

Now it is irrelevant but anyway I am not allowing the seed to be less then EXYNOS_RNG_SEED_SIZE (== sizeof(rng->seed_save)). The device expects all five channels (registers) to be seeded.

Best regards,
Krzysztof

#6 lore raw

Am Freitag, 24. M?rz 2017, 15:51:56 CET schrieb Krzysztof Kozlowski:

Hi Krzysztof,

Quoted 2 lines

I'll use the generated random numbers.

If you do that, may I propse that you update this seed field periodically? E.g. with every (or every other) user request for random data, you may update that field with new data.

Ciao
Stephan

author maintainer
#7 lore raw
Quoted 9 lines · Stephan M?ller

Am Freitag, 24. M?rz 2017, 15:51:56 CET schrieb Krzysztof Kozlowski:

Hi Krzysztof,

Quoted 2 lines

I'll use the generated random numbers.

If you do that, may I propse that you update this seed field periodically? E.g. with every (or every other) user request for random data, you may update that field with new data.

Sounds reasonable. Thanks for feedback, it is much appreciated.

Best regards,
Krzysztof

#8 lore raw
In reply to Krzysztof Kozlowski

Hi,

Firstly, thanks for working on this.

The patch looks fine overall for me, some review comments below.

Quoted 14 lines · Krzysztof Kozlowski

Replace existing hw_ranndom/exynos-rng driver with a new, reworked one. This is a driver for pseudo random number generator block which on Exynos4 chipsets must be seeded with some value. On newer Exynos5420 chipsets it might seed itself from true random number generator block but this is not implemented yet.

New driver is a complete rework to use the crypto ALGAPI instead of hw_random API. Rationale for the change:
1. hw_random interface is for true RNG devices.
2. The old driver was seeding itself with jiffies which is not a reliable source for randomness.
3. Device generates five random numbers in each pass but old driver was returning only one thus its performance was reduced.

Quoted 5 lines

Compatibility with DeviceTree bindings is preserved.

New driver does not use runtime power management but manually enables and disables the clock when needed. This is preferred approach because using runtime PM just to toggle clock is huge overhead. Another

I'm not entirely convinced that the new approach is better.

With the old approach exynos_rng_generate() can be called more than once before PM autosuspend kicks in and thus clk_prepare_enable()/ clk_disable()_unprepare() operations will be done only once. This would give better performance on the "burst" operations.

[ The above assumes that clock operations are more costly than
going through PM core to check the current device state. ]

drivers/crypto/exynos-rng.c line 97

Quoted 5 lines
drivers/crypto/exynos-rng.c new file +5 -0
+static int exynos_rng_get_random(struct exynos_rng_dev *rng,
+				 u8 *dst, unsigned int dlen,
+				 unsigned int *read)
+{
+	int retry = 100;

I know that this is copied verbatim from the old driver but please use define for the maximum number of retries.

drivers/crypto/exynos-rng.c line 227

Quoted 8 lines
drivers/crypto/exynos-rng.c new file +8 -0
+static int exynos_rng_probe(struct platform_device *pdev)
+{
+	struct exynos_rng_dev *rng;
+	struct resource *res;
+	int ret;
+
+	if (exynos_rng_dev)
+		return -EEXIST;

How this condition could ever happen?

The probe function will never be called twice.

Best regards,
--
Bartlomiej Zolnierkiewicz
Samsung R&D Institute Poland
Samsung Electronics

author maintainer
#9 lore raw
Quoted 30 lines · Bartlomiej Zolnierkiewicz

Hi,

Firstly, thanks for working on this.

The patch looks fine overall for me, some review comments below.

Quoted 14 lines · Krzysztof Kozlowski

Replace existing hw_ranndom/exynos-rng driver with a new, reworked one. This is a driver for pseudo random number generator block which on Exynos4 chipsets must be seeded with some value. On newer Exynos5420 chipsets it might seed itself from true random number generator block but this is not implemented yet.

New driver is a complete rework to use the crypto ALGAPI instead of hw_random API. Rationale for the change:
1. hw_random interface is for true RNG devices.
2. The old driver was seeding itself with jiffies which is not a reliable source for randomness.
3. Device generates five random numbers in each pass but old driver was returning only one thus its performance was reduced.

Quoted 5 lines

Compatibility with DeviceTree bindings is preserved.

New driver does not use runtime power management but manually enables and disables the clock when needed. This is preferred approach because using runtime PM just to toggle clock is huge overhead. Another

I'm not entirely convinced that the new approach is better.

Quoted 7 lines

With the old approach exynos_rng_generate() can be called more than once before PM autosuspend kicks in and thus clk_prepare_enable()/ clk_disable()_unprepare() operations will be done only once. This would give better performance on the "burst" operations.

[ The above assumes that clock operations are more costly than
going through PM core to check the current device state. ]

I agree that we loose the "burst" mode but:
1. At least on Exynso4 SSS is part of TOP power domain so it will not help to reduce any more power consumption (on Exynos5422 it is mentioned in G2D... which seems incorrect).
2. I think the overhead of clk operations is much smaller. These are only two locks (prepare mutex + enable spin), simple tree traversal and play with few SFRs.

The power domain code in comparison to that is huge and complicated with inter-device links and dependencies. Also the actual runtime PM suspend would anyway fall back at then end to clk prepare/enable locks and paths.

We've been talking about this a lot with Marek Szyprowski (cc'ed) and he was always (AFAIR) against attempts of runtime power management of a single clock...

Quoted 9 lines

drivers/crypto/exynos-rng.c line 97

Quoted 6 lines
drivers/crypto/exynos-rng.c new file +5 -0
+static int exynos_rng_get_random(struct exynos_rng_dev *rng,
+				 u8 *dst, unsigned int dlen,
+				 unsigned int *read)
+{
+	int retry = 100;

I know that this is copied verbatim from the old driver but please use define for the maximum number of retries.

No problem. Number is chosen arbitrarily so there will not be any additional information coming from the define.

Quoted 11 lines

drivers/crypto/exynos-rng.c line 227

Quoted 8 lines
drivers/crypto/exynos-rng.c new file +8 -0
+static int exynos_rng_probe(struct platform_device *pdev)
+{
+	struct exynos_rng_dev *rng;
+	struct resource *res;
+	int ret;
+
+	if (exynos_rng_dev)
+		return -EEXIST;

How this condition could ever happen?

Quoted 1 line

The probe function will never be called twice.

I really do not like global or file-scope variables. I do not like drivers using them. Actually I hate them.

Quoted 1 line

From time to time I encounter a driver which was designed with that

approach - static fields and hidden assumption that there will be only one instance. Usually that assumption is really hidden...

... and then it happens that I want to use two instances which of course fails.

This code serves as a clear documentation for this assumption - only one instance is allowed. You can look at it as a self-documenting requirement.

And I think the probe might be called twice, for example in case of mistake in DTB.

Best regards,
Krzysztof

#10 lore raw
Quoted 47 lines · Krzysztof Kozlowski
Quoted 30 lines · Bartlomiej Zolnierkiewicz

Hi,

Firstly, thanks for working on this.

The patch looks fine overall for me, some review comments below.

Quoted 14 lines · Krzysztof Kozlowski

Replace existing hw_ranndom/exynos-rng driver with a new, reworked one. This is a driver for pseudo random number generator block which on Exynos4 chipsets must be seeded with some value. On newer Exynos5420 chipsets it might seed itself from true random number generator block but this is not implemented yet.

New driver is a complete rework to use the crypto ALGAPI instead of hw_random API. Rationale for the change:
1. hw_random interface is for true RNG devices.
2. The old driver was seeding itself with jiffies which is not a reliable source for randomness.
3. Device generates five random numbers in each pass but old driver was returning only one thus its performance was reduced.

Quoted 5 lines

Compatibility with DeviceTree bindings is preserved.

New driver does not use runtime power management but manually enables and disables the clock when needed. This is preferred approach because using runtime PM just to toggle clock is huge overhead. Another

I'm not entirely convinced that the new approach is better.

Quoted 7 lines

With the old approach exynos_rng_generate() can be called more than once before PM autosuspend kicks in and thus clk_prepare_enable()/ clk_disable()_unprepare() operations will be done only once. This would give better performance on the "burst" operations.

[ The above assumes that clock operations are more costly than
going through PM core to check the current device state. ]

I agree that we loose the "burst" mode but:
1. At least on Exynso4 SSS is part of TOP power domain so it will not help to reduce any more power consumption (on Exynos5422 it is mentioned in G2D... which seems incorrect).
2. I think the overhead of clk operations is much smaller. These are only two locks (prepare mutex + enable spin), simple tree traversal and play with few SFRs.

Quoted 8 lines

The power domain code in comparison to that is huge and complicated with inter-device links and dependencies. Also the actual runtime PM suspend would anyway fall back at then end to clk prepare/enable locks and paths.

We've been talking about this a lot with Marek Szyprowski (cc'ed) and he was always (AFAIR) against attempts of runtime power management of a single clock...

OK, thanks for explanation.

Quoted 20 lines
Quoted 11 lines

drivers/crypto/exynos-rng.c line 227

Quoted 8 lines
drivers/crypto/exynos-rng.c new file +8 -0
+static int exynos_rng_probe(struct platform_device *pdev)
+{
+	struct exynos_rng_dev *rng;
+	struct resource *res;
+	int ret;
+
+	if (exynos_rng_dev)
+		return -EEXIST;

How this condition could ever happen?

Quoted 1 line

The probe function will never be called twice.

I really do not like global or file-scope variables. I do not like drivers using them. Actually I hate them.

From time to time I encounter a driver which was designed with that approach - static fields and hidden assumption that there will be only one instance. Usually that assumption is really hidden...

Quoted 6 lines

... and then it happens that I want to use two instances which of course fails.

This code serves as a clear documentation for this assumption - only one instance is allowed. You can look at it as a self-documenting requirement.

For me it looks as needless case of defensive programming and when I see the code like this it always raises questions about the real intentions of the code. I find it puzzling and not helpful.

Quoted 2 lines

And I think the probe might be called twice, for example in case of mistake in DTB.

Even if this is possible resource allocation code in the driver will take take care of handling it just fine,

Best regards,
--
Bartlomiej Zolnierkiewicz
Samsung R&D Institute Poland
Samsung Electronics

author maintainer
#11 lore raw
Quoted 11 lines · Bartlomiej Zolnierkiewicz
Quoted 10 lines · Krzysztof Kozlowski

I really do not like global or file-scope variables. I do not like drivers using them. Actually I hate them.

From time to time I encounter a driver which was designed with that approach - static fields and hidden assumption that there will be only one instance. Usually that assumption is really hidden...

... and then it happens that I want to use two instances which of course fails.

Quoted 7 lines
Quoted 3 lines

This code serves as a clear documentation for this assumption - only one instance is allowed. You can look at it as a self-documenting requirement.

For me it looks as needless case of defensive programming and when I see the code like this it always raises questions about the real intentions of the code. I find it puzzling and not helpful.

I do not understand what might be puzzling about check for static file-scope value. It is of course subjective, but for me that looks pretty self-explanatory.

Quoted 6 lines
Quoted 3 lines

And I think the probe might be called twice, for example in case of mistake in DTB.

Even if this is possible resource allocation code in the driver will take take care of handling it just fine,

Indeed, the devm_ioremap_resource() solves the case. I can drop the check then.

Best regards,
Krzysztof

#12 lore raw
Quoted 16 lines · Krzysztof Kozlowski
Quoted 15 lines · Bartlomiej Zolnierkiewicz
Quoted 13 lines · Krzysztof Kozlowski

I really do not like global or file-scope variables. I do not like drivers using them. Actually I hate them.

From time to time I encounter a driver which was designed with that approach - static fields and hidden assumption that there will be only one instance. Usually that assumption is really hidden...

... and then it happens that I want to use two instances which of course fails.

This code serves as a clear documentation for this assumption - only one instance is allowed. You can look at it as a self-documenting requirement.

Quoted 7 lines
Quoted 3 lines

For me it looks as needless case of defensive programming and when I see the code like this it always raises questions about the real intentions of the code. I find it puzzling and not helpful.

I do not understand what might be puzzling about check for static file-scope value. It is of course subjective, but for me that looks pretty self-explanatory.

The check should never happen given that ->probe will not happen twice.

However it seems that this is possible now with DT platform devices and incorrect DTB.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

And I think the probe might be called twice, for example in case of mistake in DTB.

Even if this is possible resource allocation code in the driver will take take care of handling it just fine,

Indeed, the devm_ioremap_resource() solves the case. I can drop the check then.

Looking on this a bit more it seems that devm_ioremap_resource() will not cover all mistakes (using compatible by mistake in some other DTB node).

Leave the check, I take my objection back.

Best regards,
--
Bartlomiej Zolnierkiewicz
Samsung R&D Institute Poland
Samsung Electronics

author maintainer
#13 lore raw
Quoted 6 lines · Bartlomiej Zolnierkiewicz
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

And I think the probe might be called twice, for example in case of mistake in DTB.

Even if this is possible resource allocation code in the driver will take take care of handling it just fine,

Quoted 8 lines
Quoted 2 lines

Indeed, the devm_ioremap_resource() solves the case. I can drop the check then.

Looking on this a bit more it seems that devm_ioremap_resource() will not cover all mistakes (using compatible by mistake in some other DTB node).

Leave the check, I take my objection back.

Great! Thanks for feedback.

Best regards,
Krzysztof

author maintainer
#14 lore raw

PATCH 2/3 ARM: exynos_defconfig: Enable Exynos RNG and user-space crypto API

In reply to Krzysztof Kozlowski

Enable the new Exynos pseudo random number generator driver and user-space API to access crypto algorithms and devices (not only RNG).

---
 arch/arm/configs/exynos_defconfig | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm/configs/exynos_defconfig +6 -0
index 6dc661c4a2c1..960d55445e05 100644
@@ -265,6 +265,12 @@ CONFIG_DEBUG_RT_MUTEXES=y
 CONFIG_DEBUG_SPINLOCK=y
 CONFIG_DEBUG_MUTEXES=y
 CONFIG_DEBUG_USER=y
+CONFIG_CRYPTO_USER=m
+CONFIG_CRYPTO_USER_API_HASH=m
+CONFIG_CRYPTO_USER_API_SKCIPHER=m
+CONFIG_CRYPTO_USER_API_RNG=m
+CONFIG_CRYPTO_USER_API_AEAD=m
+CONFIG_CRYPTO_DEV_EXYNOS_RNG=y
 CONFIG_CRYPTO_DEV_S5P=y
 CONFIG_ARM_CRYPTO=y
 CONFIG_CRYPTO_SHA1_ARM_NEON=m
Signature · 1 line
2.9.3
author maintainer
#15 lore raw

PATCH 3/3 ARM: multi_v7_defconfig: Enable Exynos RNG and user-space crypto API

In reply to Krzysztof Kozlowski

Enable the new Exynos pseudo random number generator driver and user-space API to access crypto algorithms and devices (not only RNG).

---
 arch/arm/configs/multi_v7_defconfig | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm/configs/multi_v7_defconfig +6 -0
index 36c1b39cfb54..9bc1bd7f2afa 100644
@@ -935,7 +935,13 @@ CONFIG_CPUFREQ_DT=y
 CONFIG_KEYSTONE_IRQ=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_ST=y
+CONFIG_CRYPTO_USER=m
+CONFIG_CRYPTO_USER_API_HASH=m
+CONFIG_CRYPTO_USER_API_SKCIPHER=m
+CONFIG_CRYPTO_USER_API_RNG=m
+CONFIG_CRYPTO_USER_API_AEAD=m
 CONFIG_CRYPTO_DEV_MARVELL_CESA=m
+CONFIG_CRYPTO_DEV_EXYNOS_RNG=m
 CONFIG_CRYPTO_DEV_S5P=m
 CONFIG_CRYPTO_DEV_SUN4I_SS=m
 CONFIG_CRYPTO_DEV_ROCKCHIP=m
Signature · 1 line
2.9.3