Skip to contentSkip to conversation

powerpc: wire up rng during setup_arch

PATCH Superseded Jason A. Donenfeld 2 participants 13 messages Jun 11, 2022

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover powerpc: wire up rng during setup_arch

The platform's RNG must be available before random_init() in order to be useful for initial seeding, which in turn means that it needs to be called from setup_arch(), rather than from an init call. This series wires that up properly on the three platforms that currently initialize the RNG from the wrong place.

Jason A. Donenfeld (3):
powerpc/microwatt: wire up rng during setup_arch powerpc/powernv: wire up rng during setup_arch powerpc/pseries: wire up rng during setup_arch

 arch/powerpc/platforms/microwatt/rng.c   |  9 ++-------
 arch/powerpc/platforms/microwatt/setup.c |  8 ++++++++
 arch/powerpc/platforms/powernv/rng.c     | 17 ++++-------------
 arch/powerpc/platforms/powernv/setup.c   |  4 ++++
 arch/powerpc/platforms/pseries/rng.c     | 11 ++---------
 arch/powerpc/platforms/pseries/setup.c   |  3 +++
 6 files changed, 23 insertions(+), 29 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 1/3 powerpc/microwatt: wire up rng during setup_arch

The platform's RNG must be available before random_init() in order to be useful for initial seeding, which in turn means that it needs to be called from setup_arch(), rather than from an init call. Fortunately, each platform already has a setup_arch function pointer, which means it's easy to wire this up for each of the three platforms that have an RNG. This commit also removes some noisy log messages that don't add much.

Cc Michael Ellerman <[email protected]>
Cc Christophe Leroy <[email protected]>
Fixes c25769fddaec ("powerpc/microwatt: Add support for hardware random number generator")
---
 arch/powerpc/platforms/microwatt/rng.c   | 9 ++-------
 arch/powerpc/platforms/microwatt/setup.c | 8 ++++++++
 2 files changed, 10 insertions(+), 7 deletions(-)
arch/powerpc/platforms/microwatt/rng.c +2 -7
index 7bc4d1cbfaf0..d13f656910ad 100644
@@ -29,7 +29,7 @@ static int microwatt_get_random_darn(unsigned long *v)
 	return 1;
 }
 
-static __init int rng_init(void)
+__init void microwatt_rng_init(void)
 {
 	unsigned long val;
 	int i;
@@ -37,12 +37,7 @@ static __init int rng_init(void)
 	for (i = 0; i < 10; i++) {
 		if (microwatt_get_random_darn(&val)) {
 			ppc_md.get_random_seed = microwatt_get_random_darn;
-			return 0;
+			return;
 		}
 	}
-
-	pr_warn("Unable to use DARN for get_random_seed()\n");
-
-	return -EIO;
 }
-machine_subsys_initcall(, rng_init);
arch/powerpc/platforms/microwatt/setup.c +8 -0
index 0b02603bdb74..23c996dcc870 100644
@@ -32,10 +32,18 @@ static int __init microwatt_populate(void)
 }
 machine_arch_initcall(microwatt, microwatt_populate);
 
+__init void microwatt_rng_init(void);
+
+static void __init microwatt_setup_arch(void)
+{
+	microwatt_rng_init();
+}
+
 define_machine(microwatt) {
 	.name			= "microwatt",
 	.probe			= microwatt_probe,
 	.init_IRQ		= microwatt_init_IRQ,
+	.setup_arch		= microwatt_setup_arch,
 	.progress		= udbg_progress,
 	.calibrate_decr		= generic_calibrate_decr,
 };
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH v2 1/3] powerpc/microwatt: wire up rng during setup_arch

Le 11/06/2022 à 12:04, Jason A. Donenfeld a écrit :

Quoted 50 lines

The platform's RNG must be available before random_init() in order to be useful for initial seeding, which in turn means that it needs to be called from setup_arch(), rather than from an init call. Fortunately, each platform already has a setup_arch function pointer, which means it's easy to wire this up for each of the three platforms that have an RNG. This commit also removes some noisy log messages that don't add much.

Cc Michael Ellerman <[email protected]>
Cc Christophe Leroy <[email protected]>
Fixes c25769fddaec ("powerpc/microwatt: Add support for hardware random number generator")
---
  arch/powerpc/platforms/microwatt/rng.c   | 9 ++-------
  arch/powerpc/platforms/microwatt/setup.c | 8 ++++++++
  2 files changed, 10 insertions(+), 7 deletions(-)
arch/powerpc/platforms/microwatt/rng.c +2 -7
index 7bc4d1cbfaf0..d13f656910ad 100644
@@ -29,7 +29,7 @@ static int microwatt_get_random_darn(unsigned long *v)
  	return 1;
  }
 
-static __init int rng_init(void)
+__init void microwatt_rng_init(void)
  {
  	unsigned long val;
  	int i;
@@ -37,12 +37,7 @@ static __init int rng_init(void)
  	for (i = 0; i < 10; i++) {
  		if (microwatt_get_random_darn(&val)) {
  			ppc_md.get_random_seed = microwatt_get_random_darn;
-			return 0;
+			return;
  		}
  	}
-
-	pr_warn("Unable to use DARN for get_random_seed()\n");
-
-	return -EIO;
  }
-machine_subsys_initcall(, rng_init);
arch/powerpc/platforms/microwatt/setup.c +0 -0
index 0b02603bdb74..23c996dcc870 100644

arch/powerpc/platforms/microwatt/setup.c static int __init microwatt_populate(void) · line 35

Quoted 4 lines
arch/powerpc/platforms/microwatt/setup.c +1 -0
@@ -32,10 +32,18 @@ static int __init microwatt_populate(void)
  }
  machine_arch_initcall(microwatt, microwatt_populate);
 
+__init void microwatt_rng_init(void);

This prototype should be declared in a header file, for instance asm/setup.h

checkpatch.pl returns the following warning:

WARNING:AVOID_EXTERNS: externs should be avoided in .c files #59: FILE: arch/powerpc/platforms/microwatt/setup.c:35:
+__init void microwatt_rng_init(void);

And I think the __init keyword usually goes after the type, so should be 'void __init'.

Quoted 14 lines
arch/powerpc/platforms/microwatt/setup.c +7 -0
+
+static void __init microwatt_setup_arch(void)
+{
+	microwatt_rng_init();
+}
+
  define_machine(microwatt) {
  	.name			= "microwatt",
  	.probe			= microwatt_probe,
  	.init_IRQ		= microwatt_init_IRQ,
+	.setup_arch		= microwatt_setup_arch,
  	.progress		= udbg_progress,
  	.calibrate_decr		= generic_calibrate_decr,
  };
#3 lore raw

Re: [PATCH v2 1/3] powerpc/microwatt: wire up rng during setup_arch

Hi Christophe,

Quoted 4 lines · Christophe Leroy
Quoted 2 lines

+__init void microwatt_rng_init(void);

This prototype should be declared in a header file, for instance asm/setup.h

Alright.

Quoted 2 lines

And I think the __init keyword usually goes after the type, so should be 'void __init'.

Indeed I thought so too. It just wasn't like this before, but that doesn't mean I shouldn't fix it.

v3 coming right up.

Jason

#4 lore raw

Re: [PATCH v2 1/3] powerpc/microwatt: wire up rng during setup_arch

Hi again,

Quoted 10 lines · Jason A. Donenfeld

Hi Christophe,

Quoted 4 lines · Christophe Leroy
Quoted 2 lines

+__init void microwatt_rng_init(void);

This prototype should be declared in a header file, for instance asm/setup.h

Alright.

Actually, on second thought, I don't think this part is worth doing. These are per-platform functions, not powerpc-wide.

Jason

#5 lore raw

Re: [PATCH v2 1/3] powerpc/microwatt: wire up rng during setup_arch

Le 11/06/2022 à 16:46, Jason A. Donenfeld a écrit :

Quoted 8 lines

Hi again,

Quoted 5 lines · Jason A. Donenfeld

Hi Christophe,

Quoted 1 line · Christophe Leroy
Quoted 1 line
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+__init void microwatt_rng_init(void);

This prototype should be declared in a header file, for instance asm/setup.h

Alright.

Actually, on second thought, I don't think this part is worth doing. These are per-platform functions, not powerpc-wide.

Then you have:

arch/powerpc/platforms/powernv/powernv.h
arch/powerpc/platforms/pseries/pseries.h

and you can add

arch/powerpc/platforms/microwatt/microwatt.h

#6 lore raw

Re: [PATCH v2 1/3] powerpc/microwatt: wire up rng during setup_arch

Hi Christophe,

Quoted 8 lines · Christophe Leroy

Then you have:

arch/powerpc/platforms/powernv/powernv.h
arch/powerpc/platforms/pseries/pseries.h

and you can add

arch/powerpc/platforms/microwatt/microwatt.h

Oh, terrific, thanks. I'll do that.

Jason

#7 lore raw

PATCH v2 2/3 powerpc/powernv: wire up rng during setup_arch

In reply to Jason A. Donenfeld

The platform's RNG must be available before random_init() in order to be useful for initial seeding, which in turn means that it needs to be called from setup_arch(), rather than from an init call. Fortunately, each platform already has a setup_arch function pointer, which means it's easy to wire this up for each of the three platforms that have an RNG. This commit also removes some noisy log messages that don't add much.

Cc Michael Ellerman <[email protected]>
Cc Christophe Leroy <[email protected]>
Fixes a4da0d50b2a0 ("powerpc: Implement arch_get_random_long/int() for powernv")
---
 arch/powerpc/platforms/powernv/rng.c   | 17 ++++-------------
 arch/powerpc/platforms/powernv/setup.c |  4 ++++
 2 files changed, 8 insertions(+), 13 deletions(-)
arch/powerpc/platforms/powernv/rng.c +4 -13
index e3d44b36ae98..ef24e72a1b69 100644
@@ -84,24 +84,20 @@ static int powernv_get_random_darn(unsigned long *v)
 	return 1;
 }
 
-static int __init initialise_darn(void)
+static void __init initialise_darn(void)
 {
 	unsigned long val;
 	int i;
 
 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
-		return -ENODEV;
+		return;
 
 	for (i = 0; i < 10; i++) {
 		if (powernv_get_random_darn(&val)) {
 			ppc_md.get_random_seed = powernv_get_random_darn;
-			return 0;
+			return;
 		}
 	}
-
-	pr_warn("Unable to use DARN for get_random_seed()\n");
-
-	return -EIO;
 }
 
 int powernv_get_random_long(unsigned long *v)
@@ -163,14 +159,12 @@ static __init int rng_create(struct device_node *dn)
 
 	rng_init_per_cpu(rng, dn);
 
-	pr_info_once("Registering arch random hook.\n");
-
 	ppc_md.get_random_seed = powernv_get_random_long;
 
 	return 0;
 }
 
-static __init int rng_init(void)
+__init void powernv_rng_init(void)
 {
 	struct device_node *dn;
 	int rc;
@@ -188,7 +182,4 @@ static __init int rng_init(void)
 	}
 
 	initialise_darn();
-
-	return 0;
 }
-machine_subsys_initcall(powernv, rng_init);
arch/powerpc/platforms/powernv/setup.c +4 -0
index 824c3ad7a0fa..a0c5217bc5c0 100644
@@ -184,6 +184,8 @@ static void __init pnv_check_guarded_cores(void)
 	}
 }
 
+__init void powernv_rng_init(void);
+
 static void __init pnv_setup_arch(void)
 {
 	set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
@@ -203,6 +205,8 @@ static void __init pnv_setup_arch(void)
 	pnv_check_guarded_cores();
 
 	/* XXX PMCS */
+
+	powernv_rng_init();
 }
 
 static void __init pnv_init(void)
Signature · 1 line
2.35.1
#8 lore raw

Re: [PATCH v2 2/3] powerpc/powernv: wire up rng during setup_arch

Le 11/06/2022 à 12:04, Jason A. Donenfeld a écrit :

Quoted 79 lines

The platform's RNG must be available before random_init() in order to be useful for initial seeding, which in turn means that it needs to be called from setup_arch(), rather than from an init call. Fortunately, each platform already has a setup_arch function pointer, which means it's easy to wire this up for each of the three platforms that have an RNG. This commit also removes some noisy log messages that don't add much.

Cc Michael Ellerman <[email protected]>
Cc Christophe Leroy <[email protected]>
Fixes a4da0d50b2a0 ("powerpc: Implement arch_get_random_long/int() for powernv")
---
  arch/powerpc/platforms/powernv/rng.c   | 17 ++++-------------
  arch/powerpc/platforms/powernv/setup.c |  4 ++++
  2 files changed, 8 insertions(+), 13 deletions(-)
arch/powerpc/platforms/powernv/rng.c +4 -13
index e3d44b36ae98..ef24e72a1b69 100644
@@ -84,24 +84,20 @@ static int powernv_get_random_darn(unsigned long *v)
  	return 1;
  }
 
-static int __init initialise_darn(void)
+static void __init initialise_darn(void)
  {
  	unsigned long val;
  	int i;
 
  	if (!cpu_has_feature(CPU_FTR_ARCH_300))
-		return -ENODEV;
+		return;
 
  	for (i = 0; i < 10; i++) {
  		if (powernv_get_random_darn(&val)) {
  			ppc_md.get_random_seed = powernv_get_random_darn;
-			return 0;
+			return;
  		}
  	}
-
-	pr_warn("Unable to use DARN for get_random_seed()\n");
-
-	return -EIO;
  }
 
  int powernv_get_random_long(unsigned long *v)
@@ -163,14 +159,12 @@ static __init int rng_create(struct device_node *dn)
 
  	rng_init_per_cpu(rng, dn);
 
-	pr_info_once("Registering arch random hook.\n");
-
  	ppc_md.get_random_seed = powernv_get_random_long;
 
  	return 0;
  }
 
-static __init int rng_init(void)
+__init void powernv_rng_init(void)
  {
  	struct device_node *dn;
  	int rc;
@@ -188,7 +182,4 @@ static __init int rng_init(void)
  	}
 
  	initialise_darn();
-
-	return 0;
  }
-machine_subsys_initcall(powernv, rng_init);
arch/powerpc/platforms/powernv/setup.c +0 -0
index 824c3ad7a0fa..a0c5217bc5c0 100644

arch/powerpc/platforms/powernv/setup.c static void __init pnv_check_guarded_cores(void) · line 188

Quoted 5 lines
arch/powerpc/platforms/powernv/setup.c +2 -0
@@ -184,6 +184,8 @@ static void __init pnv_check_guarded_cores(void)
  	}
  }
 
+__init void powernv_rng_init(void);
+

Same here, the prototype should go in a header file., should be 'void __init' (and indeed the __init is pointless in the prototype, only matters in the function definition).

Maybe the name should be pnv_rng_init() like the setup arch below.

Quoted 12 lines
arch/powerpc/platforms/powernv/setup.c +2 -0
  static void __init pnv_setup_arch(void)
  {
  	set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
@@ -203,6 +205,8 @@ static void __init pnv_setup_arch(void)
  	pnv_check_guarded_cores();
 
  	/* XXX PMCS */
+
+	powernv_rng_init();
  }
 
  static void __init pnv_init(void)
#9 lore raw

Re: [PATCH v2 2/3] powerpc/powernv: wire up rng during setup_arch

Hi Christophe,

Quoted 3 lines · Christophe Leroy

Same here, the prototype should go in a header file., should be 'void __init' (and indeed the __init is pointless in the prototype, only matters in the function definition).

I'll change the order, but I don't see a good place for the prototype other than the .c file. It's not a big deal to keep it there.

Quoted 2 lines

Maybe the name should be pnv_rng_init() like the setup arch below.

All the rng.c files are powernv_ prefixed, not pnv_ prefixed.

Jason

#10 lore raw

PATCH v2 3/3 powerpc/pseries: wire up rng during setup_arch

In reply to Jason A. Donenfeld

The platform's RNG must be available before random_init() in order to be useful for initial seeding, which in turn means that it needs to be called from setup_arch(), rather than from an init call. Fortunately, each platform already has a setup_arch function pointer, which means it's easy to wire this up for each of the three platforms that have an RNG. This commit also removes some noisy log messages that don't add much.

Cc Michael Ellerman <[email protected]>
Cc Christophe Leroy <[email protected]>
Fixes a489043f4626 ("powerpc/pseries: Implement arch_get_random_long() based on H_RANDOM")
---
 arch/powerpc/platforms/pseries/rng.c   | 11 ++---------
 arch/powerpc/platforms/pseries/setup.c |  3 +++
 2 files changed, 5 insertions(+), 9 deletions(-)
arch/powerpc/platforms/pseries/rng.c +2 -9
index 6268545947b8..d39bfce39aa1 100644
@@ -24,19 +24,12 @@ static int pseries_get_random_long(unsigned long *v)
 	return 0;
 }
 
-static __init int rng_init(void)
+__init void pseries_rng_init(void)
 {
 	struct device_node *dn;
-
 	dn = of_find_compatible_node(NULL, NULL, "ibm,random");
 	if (!dn)
-		return -ENODEV;
-
-	pr_info("Registering arch random hook.\n");
-
+		return;
 	ppc_md.get_random_seed = pseries_get_random_long;
-
 	of_node_put(dn);
-	return 0;
 }
-machine_subsys_initcall(pseries, rng_init);
arch/powerpc/platforms/pseries/setup.c +3 -0
index afb074269b42..7f3ee2658163 100644
@@ -779,6 +779,8 @@ static resource_size_t pseries_pci_iov_resource_alignment(struct pci_dev *pdev,
 }
 #endif
 
+__init void pseries_rng_init(void);
+
 static void __init pSeries_setup_arch(void)
 {
 	set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
@@ -839,6 +841,7 @@ static void __init pSeries_setup_arch(void)
 	}
 
 	ppc_md.pcibios_root_bridge_prepare = pseries_root_bridge_prepare;
+	pseries_rng_init();
 }
 
 static void pseries_panic(char *str)
Signature · 1 line
2.35.1
#11 lore raw

Re: [PATCH v2 3/3] powerpc/pseries: wire up rng during setup_arch

Le 11/06/2022 à 12:04, Jason A. Donenfeld a écrit :

Quoted 23 lines

The platform's RNG must be available before random_init() in order to be useful for initial seeding, which in turn means that it needs to be called from setup_arch(), rather than from an init call. Fortunately, each platform already has a setup_arch function pointer, which means it's easy to wire this up for each of the three platforms that have an RNG. This commit also removes some noisy log messages that don't add much.

Cc Michael Ellerman <[email protected]>
Cc Christophe Leroy <[email protected]>
Fixes a489043f4626 ("powerpc/pseries: Implement arch_get_random_long() based on H_RANDOM")
---
  arch/powerpc/platforms/pseries/rng.c   | 11 ++---------
  arch/powerpc/platforms/pseries/setup.c |  3 +++
  2 files changed, 5 insertions(+), 9 deletions(-)
arch/powerpc/platforms/pseries/rng.c +0 -0
index 6268545947b8..d39bfce39aa1 100644

arch/powerpc/platforms/pseries/rng.c static int pseries_get_random_long(unsigned long *v) · line 30

Quoted 8 lines
arch/powerpc/platforms/pseries/rng.c +1 -2
@@ -24,19 +24,12 @@ static int pseries_get_random_long(unsigned long *v)
  	return 0;
  }
 
-static __init int rng_init(void)
+__init void pseries_rng_init(void)
  {
  	struct device_node *dn;
-

There must be a empty line between declarations and code.

Quoted 18 lines
arch/powerpc/platforms/pseries/rng.c +1 -7
  	dn = of_find_compatible_node(NULL, NULL, "ibm,random");
  	if (!dn)
-		return -ENODEV;
-
-	pr_info("Registering arch random hook.\n");
-
+		return;
  	ppc_md.get_random_seed = pseries_get_random_long;
-
  	of_node_put(dn);
-	return 0;
  }
-machine_subsys_initcall(pseries, rng_init);
arch/powerpc/platforms/pseries/setup.c +0 -0
index afb074269b42..7f3ee2658163 100644

arch/powerpc/platforms/pseries/setup.c static resource_size_t pseries_pci_iov_resource_alignment(struct pci_dev *pdev, · line 783

Quoted 5 lines
arch/powerpc/platforms/pseries/setup.c +2 -0
@@ -779,6 +779,8 @@ static resource_size_t pseries_pci_iov_resource_alignment(struct pci_dev *pdev,
  }
  #endif
 
+__init void pseries_rng_init(void);
+

Prototype has to go in a header file, and should be pSeries maybe allthough camelCase in throw up on.

Quoted 11 lines
arch/powerpc/platforms/pseries/setup.c +1 -0
  static void __init pSeries_setup_arch(void)
  {
  	set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
@@ -839,6 +841,7 @@ static void __init pSeries_setup_arch(void)
  	}
 
  	ppc_md.pcibios_root_bridge_prepare = pseries_root_bridge_prepare;
+	pseries_rng_init();
  }
 
  static void pseries_panic(char *str)
#12 lore raw

Re: [PATCH v2 3/3] powerpc/pseries: wire up rng during setup_arch

Hi Christophe,

Quoted 2 lines · Christophe Leroy

There must be a empty line between declarations and code.

Ack.

Quoted 1 line

Prototype has to go in a header file

Already voiced disagreement about this in the other thread.

Quoted 2 lines

and should be pSeries maybe
allthough camelCase in throw up on.

All the rng.c functions use pseries_ in lower case, so I'll stick with that, as that's where the function is defined.

Jason