Skip to contentSkip to conversation

rockchip: board: Increase rng-seed size to make it sufficient for modern Linux

PATCH In review Alex Shumsky 4 participants 4 messages Oct 16, 2024 – Oct 21, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 rockchip: board: Increase rng-seed size to make it sufficient for modern Linux

Increase rng-seed size to make Linux happy and initialize rng pool instantly. Linux 5.19+ requires 32 bytes of entropy to initialize random pool, but u-boot currently provides only 8 bytes.
Linux 5.18 and probably some versions before it used to require 64 bytes. Bump min value to 64 bytes to be on a safe side.

Boot with 8 byte rng-seed (Linux 6.11):

    # dmesg | grep crng
    [   12.089286] random: crng init done
Boot with 32 byte rng-seed (Linux 6.11):
    # dmesg | grep crng
    [    0.000000] random: crng init done

Linux source references:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/char/random.c?h=v5.19#n551
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/char/random.c?h=v5.18#n236

Fixes d2048bafae40 ("rockchip: board: Add board_rng_seed() for all Rockchip devices")

---

Changes in v6:
- fix wording in docs and debug message

Changes in v5:
- add docs

Changes in v4:
- fix typos and code style

Changes in v3:
- reword warning
- increase default rng_seed_size to support older linux as well
- reword commit message

Changes in v2:
- add env config knob rng_seed_size
- add warning for small rng_seed_size
- 12-character commit SHA in Fixes

 arch/arm/mach-rockchip/board.c | 11 ++++++++++-
 common/Kconfig                 |  3 +++
 doc/usage/environment.rst      |  5 +++++
 include/fdt_support.h          |  3 ++-
 4 files changed, 20 insertions(+), 2 deletions(-)
arch/arm/mach-rockchip/board.c +10 -1
index 3fadf7e412..75d6693e28 100644
@@ -472,9 +472,18 @@ __weak int misc_init_r(void)
 __weak int board_rng_seed(struct abuf *buf)
 {
 	struct udevice *dev;
-	size_t len = 0x8;
+	ulong len = env_get_ulong("rng_seed_size", 10, 64);
 	u64 *data;
 
+	if (len < 64) {
+		/*
+		 * rng_seed_size should be at least 32 bytes for Linux 5.19+,
+		 * or 64 for older Linux kernel versions
+		 */
+		log_warning("Value for rng_seed_size (%lu) too low, Linux kernel RNG may fail to initialize early\n",
+			    len);
+	}
+
 	data = malloc(len);
 	if (!data) {
 		printf("Out of memory\n");
common/Kconfig +3 -0
index 957de0c5c0..8098f90683 100644
@@ -927,6 +927,9 @@ config BOARD_RNG_SEED
 	  new seed for use on subsequent boots, and whether or not the
 	  kernel should account any entropy from the given seed.
 
+	  Default seed size (64 bytes) can be overridden by a decimal
+	  environment variable rng_seed_size.
+
 endmenu
 
 menu "Update support"
doc/usage/environment.rst +5 -0
index cc33d3ec0f..7bd9ffce8d 100644
@@ -323,6 +323,11 @@ netretry
     Useful on scripts which control the retry operation
     themselves.
 
+rng_seed_size
+    Size of random value added to device-tree node /chosen/rng-seed.
+    This variable is given as a decimal number.
+    If unset, 64 bytes is used as the default.
+
 silent_linux
     If set then Linux will be told to boot silently, by
     adding 'console=' to its command line. If "yes" it will be
include/fdt_support.h +2 -1
index 741e2360c2..9447a64e06 100644
@@ -202,7 +202,8 @@ int ft_board_setup(void *blob, struct bd_info *bd);
  *
  * This function is called if CONFIG_BOARD_RNG_SEED is set, and must
  * be provided by the board. It should return, via @buf, some suitable
- * seed value to pass to the kernel.
+ * seed value to pass to the kernel. Seed size could be set in a decimal
+ * environment variable rng_seed_size and it defaults to 64 bytes.
  *
  * @param buf         A struct abuf for returning the seed and its size.
  * @return            0 if ok, negative on error.
Signature · 1 line
2.34.1
#1 lore raw

Hello Alex,

Quoted 19 lines · Alex Shumsky

Increase rng-seed size to make Linux happy and initialize rng pool instantly. Linux 5.19+ requires 32 bytes of entropy to initialize random pool, but u-boot currently provides only 8 bytes.
Linux 5.18 and probably some versions before it used to require 64 bytes. Bump min value to 64 bytes to be on a safe side.

Boot with 8 byte rng-seed (Linux 6.11):

    # dmesg | grep crng
    [   12.089286] random: crng init done
Boot with 32 byte rng-seed (Linux 6.11):
    # dmesg | grep crng
    [    0.000000] random: crng init done

Linux source references:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/char/random.c?h=v5.19#n551
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/char/random.c?h=v5.18#n236

Fixes d2048bafae40 ("rockchip: board: Add board_rng_seed() for all
Quoted 7 lines

Rockchip devices")

---

Changes in v6:
- fix wording in docs and debug message

Thanks for the v6, it's looking good to me. I think I have no more suggestions, :) and I'm sorry for the little bit of back and forth.

Quoted 93 lines

Changes in v5:
- add docs

Changes in v4:
- fix typos and code style

Changes in v3:
- reword warning
- increase default rng_seed_size to support older linux as well
- reword commit message

Changes in v2:
- add env config knob rng_seed_size
- add warning for small rng_seed_size
- 12-character commit SHA in Fixes

 arch/arm/mach-rockchip/board.c | 11 ++++++++++-
 common/Kconfig                 |  3 +++
 doc/usage/environment.rst      |  5 +++++
 include/fdt_support.h          |  3 ++-
 4 files changed, 20 insertions(+), 2 deletions(-)
arch/arm/mach-rockchip/board.c +10 -1
index 3fadf7e412..75d6693e28 100644
@@ -472,9 +472,18 @@ __weak int misc_init_r(void)
 __weak int board_rng_seed(struct abuf *buf)
 {
 	struct udevice *dev;
-	size_t len = 0x8;
+	ulong len = env_get_ulong("rng_seed_size", 10, 64);
 	u64 *data;
 
+	if (len < 64) {
+		/*
+		 * rng_seed_size should be at least 32 bytes for Linux 5.19+,
+		 * or 64 for older Linux kernel versions
+		 */
+		log_warning("Value for rng_seed_size (%lu) too low, Linux kernel
 RNG may fail to initialize early\n",
+			    len);
+	}
+
 	data = malloc(len);
 	if (!data) {
arch/arm/mach-rockchip/board.c +0 -0
 		printf("Out of memory\n");
common/Kconfig +3 -0
index 957de0c5c0..8098f90683 100644
@@ -927,6 +927,9 @@ config BOARD_RNG_SEED
 	  new seed for use on subsequent boots, and whether or not the
 	  kernel should account any entropy from the given seed.
 
+	  Default seed size (64 bytes) can be overridden by a decimal
+	  environment variable rng_seed_size.
+
 endmenu
 
 menu "Update support"
doc/usage/environment.rst +5 -0
index cc33d3ec0f..7bd9ffce8d 100644
@@ -323,6 +323,11 @@ netretry
     Useful on scripts which control the retry operation
     themselves.
 
+rng_seed_size
+    Size of random value added to device-tree node /chosen/rng-seed.
+    This variable is given as a decimal number.
+    If unset, 64 bytes is used as the default.
+
 silent_linux
     If set then Linux will be told to boot silently, by
     adding 'console=' to its command line. If "yes" it will be
include/fdt_support.h +1 -1
index 741e2360c2..9447a64e06 100644
@@ -202,7 +202,8 @@ int ft_board_setup(void *blob, struct bd_info *bd);
  *
  * This function is called if CONFIG_BOARD_RNG_SEED is set, and must
  * be provided by the board. It should return, via @buf, some suitable
- * seed value to pass to the kernel.
+ * seed value to pass to the kernel. Seed size could be set in a 

decimal
+ * environment variable rng_seed_size and it defaults to 64 bytes. *
* @param buf A struct abuf for returning the seed and its
size.
* @return 0 if ok, negative on error.

Reviewed-by on v6 1/1
#2 lore raw
In reply to Alex Shumsky

Hi Alex,

Quoted 13 lines · Alex Shumsky

Increase rng-seed size to make Linux happy and initialize rng pool instantly. Linux 5.19+ requires 32 bytes of entropy to initialize random pool, but u-boot currently provides only 8 bytes.
Linux 5.18 and probably some versions before it used to require 64 bytes. Bump min value to 64 bytes to be on a safe side.

Boot with 8 byte rng-seed (Linux 6.11):

     # dmesg | grep crng
     [   12.089286] random: crng init done
Boot with 32 byte rng-seed (Linux 6.11):
     # dmesg | grep crng
     [    0.000000] random: crng init done
Quoted 8 lines

Linux source references:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/char/random.c?h=v5.19#n551
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/char/random.c?h=v5.18#n236

Fixes d2048bafae40 ("rockchip: board: Add board_rng_seed() for all Rockchip devices")

Thanks!
Quentin

Reviewed-by on v6 1/1
#3 lore raw
In reply to Alex Shumsky
Quoted 13 lines · Alex Shumsky

Increase rng-seed size to make Linux happy and initialize rng pool instantly. Linux 5.19+ requires 32 bytes of entropy to initialize random pool, but u-boot currently provides only 8 bytes.
Linux 5.18 and probably some versions before it used to require 64 bytes. Bump min value to 64 bytes to be on a safe side.

Boot with 8 byte rng-seed (Linux 6.11):

     # dmesg | grep crng
     [   12.089286] random: crng init done
Boot with 32 byte rng-seed (Linux 6.11):
     # dmesg | grep crng
     [    0.000000] random: crng init done
Quoted 8 lines

Linux source references:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/char/random.c?h=v5.19#n551
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/char/random.c?h=v5.18#n236

Fixes d2048bafae40 ("rockchip: board: Add board_rng_seed() for all Rockchip devices")

Thanks,
- Kever

Quoted 95 lines

---

Changes in v6:
- fix wording in docs and debug message

Changes in v5:
- add docs

Changes in v4:
- fix typos and code style

Changes in v3:
- reword warning
- increase default rng_seed_size to support older linux as well
- reword commit message

Changes in v2:
- add env config knob rng_seed_size
- add warning for small rng_seed_size
- 12-character commit SHA in Fixes

  arch/arm/mach-rockchip/board.c | 11 ++++++++++-
  common/Kconfig                 |  3 +++
  doc/usage/environment.rst      |  5 +++++
  include/fdt_support.h          |  3 ++-
  4 files changed, 20 insertions(+), 2 deletions(-)
arch/arm/mach-rockchip/board.c +10 -1
index 3fadf7e412..75d6693e28 100644
@@ -472,9 +472,18 @@ __weak int misc_init_r(void)
  __weak int board_rng_seed(struct abuf *buf)
  {
  	struct udevice *dev;
-	size_t len = 0x8;
+	ulong len = env_get_ulong("rng_seed_size", 10, 64);
  	u64 *data;
 
+	if (len < 64) {
+		/*
+		 * rng_seed_size should be at least 32 bytes for Linux 5.19+,
+		 * or 64 for older Linux kernel versions
+		 */
+		log_warning("Value for rng_seed_size (%lu) too low, Linux kernel RNG may fail to initialize early\n",
+			    len);
+	}
+
  	data = malloc(len);
  	if (!data) {
  		printf("Out of memory\n");
common/Kconfig +3 -0
index 957de0c5c0..8098f90683 100644
@@ -927,6 +927,9 @@ config BOARD_RNG_SEED
  	  new seed for use on subsequent boots, and whether or not the
  	  kernel should account any entropy from the given seed.
 
+	  Default seed size (64 bytes) can be overridden by a decimal
+	  environment variable rng_seed_size.
+
  endmenu
 
  menu "Update support"
doc/usage/environment.rst +5 -0
index cc33d3ec0f..7bd9ffce8d 100644
@@ -323,6 +323,11 @@ netretry
      Useful on scripts which control the retry operation
      themselves.
 
+rng_seed_size
+    Size of random value added to device-tree node /chosen/rng-seed.
+    This variable is given as a decimal number.
+    If unset, 64 bytes is used as the default.
+
  silent_linux
      If set then Linux will be told to boot silently, by
      adding 'console=' to its command line. If "yes" it will be
include/fdt_support.h +2 -1
index 741e2360c2..9447a64e06 100644
@@ -202,7 +202,8 @@ int ft_board_setup(void *blob, struct bd_info *bd);
   *
   * This function is called if CONFIG_BOARD_RNG_SEED is set, and must
   * be provided by the board. It should return, via @buf, some suitable
- * seed value to pass to the kernel.
+ * seed value to pass to the kernel. Seed size could be set in a decimal
+ * environment variable rng_seed_size and it defaults to 64 bytes.
   *
   * @param buf         A struct abuf for returning the seed and its size.
   * @return            0 if ok, negative on error.