Skip to contentSkip to conversation

dm: boot a mapped device without an initramfs

PATCH In review Helen Koike 3 participants 5 messages Nov 3, 2018 – Nov 6, 2018

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

v10 cover dm: boot a mapped device without an initramfs

As mentioned in the discussion from the previous version of this patch, Android and Chrome OS do not use initramfs mostly due to boot time and size liability. A practical example as mentioned by Kees is that Chrome OS has a limited amount of storage available for the boot image as it is covered by the static root of trust signature.

So instead of bringing up userspace to perform the required configuration for mapped devices, this patchset allows them to be configured in the kernel command line parameter for use early in the boot process, allowing booting from a mapped device without an initramfs.

The syntax used in the boot param is based on the concise format from the dmsetup tool as described in its man page http://man7.org/linux/man-pages/man8/dmsetup.8.html#CONCISE_FORMAT

Which is:

	dm=<name>,<uuid>,<minor>,<flags>,<table>[,<table>+][;<name>,<uuid>,<minor>,<flags>,<table>[,<table>+]+]

Where,

	<name>		::= The device name.
	<uuid>		::= xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | ""
	<minor>		::= The device minor number | ""
	<flags>		::= "ro" | "rw"
	<table>		::= <start_sector> <num_sectors> <target_type> <target_args>
	<target_type>	::= "verity" | "linear" | ...

Example, the following could be added in the boot parameters. dm="lroot,,,rw, 0 4096 linear 98:16 0, 4096 4096 linear 98:32 0" root=/dev/dm-0

Please check patch 2/2 with the documentation on the format.

The idea to make it compatible with the dmsetup concise format is to make it easier for users, allowing just copy & paste from the output of the command:

	sudo dmsetup table --concise /dev/mapper/lroot

The implementation consists basically in parsing the command line argument and performing the ioctls that would be performed by userspace otherwise, i.e. DM_DEV_CREATE, followed by DM_TABLE_LOAD then DM_DEV_SUSPEND.

Instead of performing the ioctls, we could by-pass it, calling the corresponding functions directly, but the ioctls calls perform some checks and also the implementation stays less invasive. Please let me know if you would prefer a directly call instead of going thought the ioctls.

Changes since v9:
- https://www.redhat.com/archives/linux-lvm/2018-September/msg00016.html
- new file: drivers/md/dm-boot.c
- most of the parsing code was moved from init/do_mounts_dm.c to drivers/md/dm-boot.c
- parsing code was in essence replaced by the concise parser from dmsetup _create_concise function:
https://sourceware.org/git/?p=lvm2.git;a=blob;f=libdm/dm-tools/dmsetup.c;h=835fdcdc75e8f0f0f7c4ed46cc9788a6616f58b8;hb=7498f8383397a93db95655ca227257836cbcac82#l1265
the main reason is that this code is already being used/tested by dmsetup, so we can have some level of confidence that it works as expected. Besides this, it also looks more efficient.
- Not all targets are allowed to be used by dm=, as pointed previously, there are some risks in creating a mapped device without some validation from userspace (see documentation from the patch listing which targets are allowed).
- Instead of using a simple singly linked list (for devices and tables), use the struct list_head. This occupies unnecessary space in the code, but it makes the code cleaner and easier to read and less prone to silly errors.
- Documentation and comments were reviewed and refactored, e.g.:
* "is to possible" was removed
* s/specified as a simple string/specified as a string/
- Added docs above __align function, make it clear that the second parameter @a must be a power of two.
- Clean ups: removal of unnecessary includes, macros, variables, some redundant checks and warnings.
- when calling ioctls, the code was allocating and freeing the same structure a couple of times. So instead of executing kzalloc/kfree 3 times, execute kmalloc once and reuse the structure after a memset, then finally kfree it once.
- update commit message

Changes since v8:
- https://www.redhat.com/archives/linux-lvm/2017-May/msg00055.html
- Add minor number to make it compatible with dmsetup concise format

Changes since v7:
- http://lkml.iu.edu/hypermail/linux/kernel/1705.2/02657.html
- Fix build error due commit
e516db4f67 (dm ioctl: add a new DM_DEV_ARM_POLL ioctl)

Changes since v6:
- https://www.redhat.com/archives/dm-devel/2017-April/msg00316.html
- Add a new function to issue the equivalent of a DM ioctl programatically.
- Use the new ioctl interface to create the devices.
- Use a comma-delimited and semi-colon delimited dmsetup-like commands.

Changes since v5:
- https://www.redhat.com/archives/dm-devel/2016-February/msg00112.html

Enric Balletbo i Serra (1):
dm ioctl: add a device mapper ioctl function.

Will Drewry (1):
init: add support to directly boot to a mapped device

 .../admin-guide/kernel-parameters.rst         |   1 +
 .../admin-guide/kernel-parameters.txt         |   3 +
 Documentation/device-mapper/dm-boot.txt       |  87 ++++
 drivers/md/Makefile                           |   2 +-
 drivers/md/dm-boot.c                          | 433 ++++++++++++++++++
 drivers/md/dm-ioctl.c                         |  49 ++
 include/linux/device-mapper.h                 |  12 +
 init/Makefile                                 |   1 +
 init/do_mounts.c                              |   1 +
 init/do_mounts.h                              |  10 +
 init/do_mounts_dm.c                           |  46 ++
 11 files changed, 644 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/device-mapper/dm-boot.txt
 create mode 100644 drivers/md/dm-boot.c
 create mode 100644 init/do_mounts_dm.c
Signature · 1 line
2.19.1
#1 lore raw

PATCH v10 1/2 dm ioctl: add a device mapper ioctl function.

From: Enric Balletbo i Serra <[email protected]>

Add a dm_ioctl_cmd to issue the equivalent of a DM ioctl call in kernel.

---

Changes since v9:
- https://www.redhat.com/archives/linux-lvm/2018-September/msg00016.html
- Reorganize variables

---
 drivers/md/dm-ioctl.c         | 49 +++++++++++++++++++++++++++++++++++
 include/linux/device-mapper.h |  6 +++++
 2 files changed, 55 insertions(+)
drivers/md/dm-ioctl.c +49 -0
index f666778ad237..ae34c2030a9c 100644
@@ -2018,3 +2018,52 @@ int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid)
 
 	return r;
 }
+
+/**
+ * dm_ioctl_cmd - Device mapper ioctl's
+ * @command: ioctl command
+ * @param: Pointer to device mapped ioctl struct
+ */
+int dm_ioctl_cmd(uint command, struct dm_ioctl *param)
+{
+	struct file *filp = NULL;
+	size_t input_param_size;
+	int ioctl_flags, r;
+	unsigned int cmd;
+	ioctl_fn fn;
+
+	if (_IOC_TYPE(command) != DM_IOCTL)
+		return -ENOTTY;
+
+	/* DM_DEV_ARM_POLL is not supported */
+	if (command == DM_DEV_ARM_POLL)
+		return -EINVAL;
+
+	cmd = _IOC_NR(command);
+
+	/*
+	 * Nothing more to do for the version command.
+	 */
+	if (cmd == DM_VERSION_CMD)
+		return 0;
+
+	fn = lookup_ioctl(cmd, &ioctl_flags);
+	if (!fn) {
+		DMWARN("dm_ioctl: unknown command 0x%x", command);
+		return -ENOTTY;
+	}
+
+	input_param_size = param->data_size;
+	r = validate_params(cmd, param);
+	if (r)
+		return r;
+
+	param->data_size = sizeof(*param);
+	r = fn(filp, param, input_param_size);
+
+	if (unlikely(param->flags & DM_BUFFER_FULL_FLAG) &&
+	    unlikely(ioctl_flags & IOCTL_FLAGS_NO_PARAMS))
+		DMERR("ioctl %d tried to output some data but has IOCTL_FLAGS_NO_PARAMS set", cmd);
+
+	return r;
+}
include/linux/device-mapper.h +6 -0
index d7bee8669f10..8b2e4ae6a498 100644
@@ -12,6 +12,7 @@
 #include <linux/blkdev.h>
 #include <linux/math64.h>
 #include <linux/ratelimit.h>
+#include <linux/dm-ioctl.h>
 
 struct dm_dev;
 struct dm_target;
@@ -423,6 +424,11 @@ void dm_remap_zone_report(struct dm_target *ti, struct bio *bio,
 			  sector_t start);
 union map_info *dm_get_rq_mapinfo(struct request *rq);
 
+/*
+ * Device mapper ioctl function.
+ */
+int dm_ioctl_cmd(unsigned int command, struct dm_ioctl *param);
+
 struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
 
 /*
Signature · 1 line
2.19.1
#2 lore raw

PATCH v10 2/2 init: add support to directly boot to a mapped device

In reply to Helen Koike

From: Will Drewry <[email protected]>

Add a dm= kernel parameter.
It allows device-mapper targets to be configured at boot time for use early in the boot process (as the root device or otherwise).

[rework to use dm_ioctl calls]
[rework to use concise format | rework for upstream]

---

Hello,

In this patch, I constrained the targets allowed to be used by dm=, but I am not entirely familiar with all the targets. I blacklisted the ones mentioned previously and some other that I think doesn't make much sense, but please let me know if you think there are others I should blacklist.

Changes since v9:
- https://www.redhat.com/archives/linux-lvm/2018-September/msg00016.html
- new file: drivers/md/dm-boot.c
- most of the parsing code was moved from init/do_mounts_dm.c to drivers/md/dm-boot.c
- parsing code was in essence replaced by the concise parser from dmsetup _create_concise function:
https://sourceware.org/git/?p=lvm2.git;a=blob;f=libdm/dm-tools/dmsetup.c;h=835fdcdc75e8f0f0f7c4ed46cc9788a6616f58b8;hb=7498f8383397a93db95655ca227257836cbcac82#l1265
the main reason is that this code is already being used/tested by dmsetup, so we can have some level of confidence that it works as expected. Besides this, it also looks more efficient.
- Not all targets are allowed to be used by dm=, as pointed previously, there are some risks in creating a mapped device without some validation from userspace (see documentation from the patch listing which targets are allowed).
- Instead of using a simple singly linked list (for devices and tables), use the struct list_head. This occupies unnecessary space in the code, but it makes the code cleaner and easier to read and less prone to silly errors.
- Documentation and comments were reviewed and refactored, e.g.:
* "is to possible" was removed
* s/specified as a simple string/specified as a string/
- Added docs above __align function, make it clear that the second parameter @a must be a power of two.
- Clean ups: removal of unnecessary includes, macros, variables, some redundant checks and warnings.
- when calling ioctls, the code was allocating and freeing the same structure a couple of times. So instead of executing kzalloc/kfree 3 times, execute kmalloc once and reuse the structure after a memset, then finally kfree it once.
- update commit message

Thanks

---
 .../admin-guide/kernel-parameters.rst         |   1 +
 .../admin-guide/kernel-parameters.txt         |   3 +
 Documentation/device-mapper/dm-boot.txt       |  87 ++++
 drivers/md/Makefile                           |   2 +-
 drivers/md/dm-boot.c                          | 433 ++++++++++++++++++
 include/linux/device-mapper.h                 |   6 +
 init/Makefile                                 |   1 +
 init/do_mounts.c                              |   1 +
 init/do_mounts.h                              |  10 +
 init/do_mounts_dm.c                           |  46 ++
 10 files changed, 589 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/device-mapper/dm-boot.txt
 create mode 100644 drivers/md/dm-boot.c
 create mode 100644 init/do_mounts_dm.c
Documentation/admin-guide/kernel-parameters.rst +1 -0
index b8d0bc07ed0a..bd628865f66f 100644
@@ -91,6 +91,7 @@ parameter is applicable::
 	AX25	Appropriate AX.25 support is enabled.
 	CLK	Common clock infrastructure is enabled.
 	CMA	Contiguous Memory Area support is enabled.
+	DM	Device mapper support is enabled.
 	DRM	Direct Rendering Management support is enabled.
 	DYNAMIC_DEBUG Build in debug messages and enable them at runtime
 	EDD	BIOS Enhanced Disk Drive Services (EDD) is enabled
Documentation/admin-guide/kernel-parameters.txt +3 -0
index 92eb1f42240d..a3ff7192b980 100644
@@ -880,6 +880,9 @@
 
 	dis_ucode_ldr	[X86] Disable the microcode loader.
 
+	dm=		[DM] Allows early creation of a device-mapper device.
+			See Documentation/device-mapper/dm-boot.txt.
+
 	dma_debug=off	If the kernel is compiled with DMA_API_DEBUG support,
 			this option disables the debugging code at boot.
 
Documentation/device-mapper/dm-boot.txt new file +87 -0

87 lines not on this page · raw message

drivers/md/Makefile +1 -1
index 822f4e8753bc..26ffe4536247 100644
@@ -5,7 +5,7 @@
 
 dm-mod-y	+= dm.o dm-table.o dm-target.o dm-linear.o dm-stripe.o \
 		   dm-ioctl.o dm-io.o dm-kcopyd.o dm-sysfs.o dm-stats.o \
-		   dm-rq.o
+		   dm-rq.o dm-boot.o
 dm-multipath-y	+= dm-path-selector.o dm-mpath.o
 dm-snapshot-y	+= dm-snap.o dm-exception-store.o dm-snap-transient.o \
 		    dm-snap-persistent.o
drivers/md/dm-boot.c new file +433 -0

433 lines not on this page · raw message

include/linux/device-mapper.h +6 -0
index 8b2e4ae6a498..a3ad379fd2b7 100644
@@ -429,6 +429,12 @@ union map_info *dm_get_rq_mapinfo(struct request *rq);
  */
 int dm_ioctl_cmd(unsigned int command, struct dm_ioctl *param);
 
+/*
+ * Device mapper function to parse and create devices specified in the kernel
+ * command line boot parameter "dm="
+ */
+void __init dm_boot_setup_drives(char *boot_param);
+
 struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
 
 /*
init/Makefile +1 -0
index a3e5ce2bcf08..f814f0ff5974 100644
@@ -19,6 +19,7 @@ mounts-y			:= do_mounts.o
 mounts-$(CONFIG_BLK_DEV_RAM)	+= do_mounts_rd.o
 mounts-$(CONFIG_BLK_DEV_INITRD)	+= do_mounts_initrd.o
 mounts-$(CONFIG_BLK_DEV_MD)	+= do_mounts_md.o
+mounts-$(CONFIG_BLK_DEV_DM)	+= do_mounts_dm.o
 
 # dependencies on generated files need to be listed explicitly
 $(obj)/version.o: include/generated/compile.h
init/do_mounts.c +1 -0
index e1c9afa9d8c9..d707f12be6e7 100644
@@ -555,6 +555,7 @@ void __init prepare_namespace(void)
 	wait_for_device_probe();
 
 	md_run_setup();
+	dm_run_setup();
 
 	if (saved_root_name[0]) {
 		root_device_name = saved_root_name;
init/do_mounts.h +10 -0
index 0bb0806de4ce..0f57528ea324 100644
@@ -61,3 +61,13 @@ void md_run_setup(void);
 static inline void md_run_setup(void) {}
 
 #endif
+
+#ifdef CONFIG_BLK_DEV_DM
+
+void dm_run_setup(void);
+
+#else
+
+static inline void dm_run_setup(void) {}
+
+#endif
init/do_mounts_dm.c new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.19.1
#3 lore raw
In reply to Helen Koike

Helen,

Am Samstag, 3. November 2018, 04:53:39 CET schrieb Helen Koike:

Quoted 2 lines

As mentioned in the discussion from the previous version of this patch, Android and Chrome OS do not use initramfs mostly due to boot time and size liability.

Do you have numbers on that?

I understand that using something like dracut with systemd inside is not what you want from a boot time point of view.
But having an initramfs embedded into the kernel image which contains only a single static linked binary can be *very* small and fast.
If you invest a little more time, you don't even need a libc, just fire up some syscalls to setup your dm. I use this technique regularly on deeply embedded systems to setup non-trivial UBIFS/crypto stuff.

Want I'm trying to say, before adding ad-hoc a feature to the kernel, we should be very sure that there is no other way to solve this in a sane manner. We have initramfs support for reasons.

Thanks,
//richard

#4 lore raw

Hi Richard, Helen,

Quoted 8 lines · Richard Weinberger

Helen,

Am Samstag, 3. November 2018, 04:53:39 CET schrieb Helen Koike:

Quoted 2 lines

As mentioned in the discussion from the previous version of this patch, Android and Chrome OS do not use initramfs mostly due to boot time and size liability.

Do you have numbers on that?

Originally, we saved ~200 ms, but I don't think we have recent numbers. (Unless Helen has some!) We first authored and posted this patch in 2010:
- https://marc.info/?l=dm-devel&m=127429492521964&w=2
- https://marc.info/?l=dm-devel&m=127429499422096&w=2
- https://marc.info/?l=dm-devel&m=127429493922000&w=2

Every Chrome OS device uses a variant of this patch as well as Android devices starting last year (if they use AVB 2.0).

Originally, the intent was the measured latency reduction. We get a linear speed
improvement when doing a cryptographic verification of the kernel and initramfs.
Why? More data == more hashes (sha256 w/compute per block). There's additional overhead from bringing up early userspace, but those are the numbers I don't have.

Quoted 11 lines

I understand that using something like dracut with systemd inside is not what you want from a boot time point of view.
But having an initramfs embedded into the kernel image which contains only a single static linked binary can be *very* small and fast.
If you invest a little more time, you don't even need a libc, just fire up some syscalls to setup your dm. I use this technique regularly on deeply embedded systems to setup non-trivial UBIFS/crypto stuff.

Want I'm trying to say, before adding ad-hoc a feature to the kernel, we should be very sure that there is no other way to solve this in a sane manner. We have initramfs support for reasons.

I very much appreciate the perspective, but after 8 years in shipping devices after
integrating feedback from kernel maintainers over the subsequent years, this doesn't feel like an "ad-hoc" feature. It's been effective and fit in well with the
existing kernel functionality, etc (imho :). What level of performance improvement or
other changes might be necessary to make the cut?

Thanks!
will