[PATCH] init: Add support for rootwait timeout parameter

From: Loic Poulain
Date: Fri May 26 2023 - 09:07:38 EST


Add an optional timeout arg to 'rootwait' as the maximum time in
seconds to wait for the root device to show up before attempting
forced mount of the root filesystem.

This can be helpful to force boot failure and restart in case the
root device does not show up in time, allowing the bootloader to
take any appropriate measures (e.g. recovery, A/B switch, retry...).

In success case, mounting happens as soon as the root device is ready,
contrary to the existing 'rootdelay' parameter (unconditional delay).

Signed-off-by: Loic Poulain <loic.poulain@xxxxxxxxxx>
---
.../admin-guide/kernel-parameters.txt | 4 ++++
init/do_mounts.c | 19 +++++++++++++++++--
2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 9e5bab29685f..6e351d4c84a5 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -5465,6 +5465,10 @@
Useful for devices that are detected asynchronously
(e.g. USB and MMC devices).

+ rootwait= [KNL] Maximum time (in seconds) to wait for root device
+ to show up before attempting to mount the root
+ filesystem.
+
rproc_mem=nn[KMG][@address]
[KNL,ARM,CMA] Remoteproc physical memory block.
Memory area to be used by remote processor image,
diff --git a/init/do_mounts.c b/init/do_mounts.c
index 811e94daf0a8..942458e7d1c0 100644
--- a/init/do_mounts.c
+++ b/init/do_mounts.c
@@ -18,6 +18,7 @@
#include <linux/slab.h>
#include <linux/ramfs.h>
#include <linux/shmem_fs.h>
+#include <linux/ktime.h>

#include <linux/nfs_fs.h>
#include <linux/nfs_fs_sb.h>
@@ -306,12 +307,20 @@ static int __init rootwait_setup(char *str)
{
if (*str)
return 0;
- root_wait = 1;
+ root_wait = -1;
return 1;
}

__setup("rootwait", rootwait_setup);

+static int __init rootwait_timeout_setup(char *str)
+{
+ root_wait = simple_strtoul(str, NULL, 0);
+ return 1;
+}
+
+__setup("rootwait=", rootwait_timeout_setup);
+
static char * __initdata root_mount_data;
static int __init root_data_setup(char *str)
{
@@ -633,11 +642,17 @@ void __init prepare_namespace(void)

/* wait for any asynchronous scanning to complete */
if ((ROOT_DEV == 0) && root_wait) {
+ const ktime_t end = ktime_add_ms(ktime_get_raw(), root_wait * MSEC_PER_SEC);
+
printk(KERN_INFO "Waiting for root device %s...\n",
saved_root_name);
while (driver_probe_done() != 0 ||
- (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
+ (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0) {
msleep(5);
+
+ if (root_wait > 0 && ktime_after(ktime_get_raw(), end))
+ break;
+ }
async_synchronize_full();
}

--
2.34.1