2023-07-26 15:57:31

by Loic Poulain

[permalink] [raw]
Subject: [PATCH v2] init: Add support for rootwait timeout parameter

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.

Use case:
In case of device mapper usage for the rootfs (e.g. root=/dev/dm-0),
if the mapper is not able to create the virtual block for any reason
(wrong arguments, bad dm-verity signature, etc), the `rootwait` param
causes the kernel to wait forever. It may however be desirable to only
wait for a given time and then panic (force mount) to cause device reset.
This gives the bootloader a chance to detect the problem and to take some
measures, such as marking the booted partition as bad (for A/B case) or
entering a recovery mode.

In success case, mounting happens as soon as the root device is ready,
unlike the existing 'rootdelay' parameter which performs an unconditional
pause.

Signed-off-by: Loic Poulain <[email protected]>
---
v2: rebase + reword: add use case example

.../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 a1457995fd41..387cf9c2a2c5 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -5501,6 +5501,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 1aa015883519..118f2bbe7b38 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>
@@ -71,12 +72,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)
{
@@ -384,14 +393,20 @@ void __init mount_root(char *root_device_name)
/* wait for any asynchronous scanning to complete */
static void __init wait_for_root(char *root_device_name)
{
+ const ktime_t end = ktime_add_ms(ktime_get_raw(), root_wait * MSEC_PER_SEC);
+
if (ROOT_DEV != 0)
return;

pr_info("Waiting for root device %s...\n", root_device_name);

while (!driver_probe_done() ||
- early_lookup_bdev(root_device_name, &ROOT_DEV) < 0)
+ early_lookup_bdev(root_device_name, &ROOT_DEV) < 0) {
msleep(5);
+ if (root_wait > 0 && ktime_after(ktime_get_raw(), end))
+ break;
+ }
+
async_synchronize_full();

}
--
2.34.1



2023-07-27 07:07:29

by Randy Dunlap

[permalink] [raw]
Subject: Re: [PATCH v2] init: Add support for rootwait timeout parameter

Hi--

On 7/26/23 08:22, Loic Poulain wrote:
> 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.
>
> Use case:
> In case of device mapper usage for the rootfs (e.g. root=/dev/dm-0),
> if the mapper is not able to create the virtual block for any reason
> (wrong arguments, bad dm-verity signature, etc), the `rootwait` param
> causes the kernel to wait forever. It may however be desirable to only
> wait for a given time and then panic (force mount) to cause device reset.
> This gives the bootloader a chance to detect the problem and to take some
> measures, such as marking the booted partition as bad (for A/B case) or
> entering a recovery mode.
>
> In success case, mounting happens as soon as the root device is ready,
> unlike the existing 'rootdelay' parameter which performs an unconditional
> pause.
>
> Signed-off-by: Loic Poulain <[email protected]>
> ---
> v2: rebase + reword: add use case example
>
> .../admin-guide/kernel-parameters.txt | 4 ++++
> init/do_mounts.c | 19 +++++++++++++++++--
> 2 files changed, 21 insertions(+), 2 deletions(-)
>

> diff --git a/init/do_mounts.c b/init/do_mounts.c
> index 1aa015883519..118f2bbe7b38 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>
> @@ -71,12 +72,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);

Better to use kstrtoul(). simple_strtoul() says:

* This function has caveats. Please use kstrtoul instead.

and kstrtoul() says:

* Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
* Preferred over simple_strtoul(). Return code must be checked.

> + return 1;
> +}
> +
> +__setup("rootwait=", rootwait_timeout_setup);


--
~Randy

2023-07-27 11:29:29

by Christian Brauner

[permalink] [raw]
Subject: Re: [PATCH v2] init: Add support for rootwait timeout parameter

On Wed, Jul 26, 2023 at 11:08:35PM -0700, Randy Dunlap wrote:
> Hi--
>
> On 7/26/23 08:22, Loic Poulain wrote:
> > 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.
> >
> > Use case:
> > In case of device mapper usage for the rootfs (e.g. root=/dev/dm-0),
> > if the mapper is not able to create the virtual block for any reason
> > (wrong arguments, bad dm-verity signature, etc), the `rootwait` param
> > causes the kernel to wait forever. It may however be desirable to only
> > wait for a given time and then panic (force mount) to cause device reset.
> > This gives the bootloader a chance to detect the problem and to take some
> > measures, such as marking the booted partition as bad (for A/B case) or
> > entering a recovery mode.
> >
> > In success case, mounting happens as soon as the root device is ready,
> > unlike the existing 'rootdelay' parameter which performs an unconditional
> > pause.
> >
> > Signed-off-by: Loic Poulain <[email protected]>
> > ---
> > v2: rebase + reword: add use case example
> >
> > .../admin-guide/kernel-parameters.txt | 4 ++++
> > init/do_mounts.c | 19 +++++++++++++++++--
> > 2 files changed, 21 insertions(+), 2 deletions(-)
> >
>
> > diff --git a/init/do_mounts.c b/init/do_mounts.c
> > index 1aa015883519..118f2bbe7b38 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>
> > @@ -71,12 +72,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);
>
> Better to use kstrtoul(). simple_strtoul() says:
>
> * This function has caveats. Please use kstrtoul instead.
>
> and kstrtoul() says:
>
> * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
> * Preferred over simple_strtoul(). Return code must be checked.

Yes, this should check and at least log an error that rootwait is
ignored and fall back to either indefinite waiting or no waiting.