[PATCH] mtd: virt_concat: join the parts in part-concat-next order

Julius Bairaktaris julius at bairaktaris.de
Sun Nov 29 13:23:16 PST 2026


The partition that carries part-concat-next is the first part of the
concatenated device, and the partitions it lists follow in list order.
mtd_virt_concat_add() however appends each part to concat->subdev[]
when its partition is added, so the parts end up in partition table
order, and mtd_virt_concat_create_join() concatenates them as they
are. When the partition carrying the property is not the first of its
chain in the table, the device is assembled in the wrong order. With

	part1: partition at 0 {
		reg = <0x0 0x10000>;
	};
	part0: partition at 10000 {
		reg = <0x10000 0x10000>;
		part-concat-next = <&part1>;
	};

the first 64 KiB of "part1-part0-concat" are part1, so every access to
the concatenated device lands at the wrong flash offset.

Sort the parts into the order of item->nodes[], which holds them in
part-concat-next order, once all of them have been added and before the
device is created.

Fixes: 43db6366fc2d ("mtd: Add driver for concatenating devices")
Signed-off-by: Julius Bairaktaris <julius at bairaktaris.de>
---

Notes:
    Found while checking whether OpenWrt's concatenated flash layouts can
    move to part-concat-next. Reproduced on mtd/next in UML booted with a device tree: a test
    driver registers a RAM-backed MTD device whose 64 KiB blocks hold 'a',
    'b', 'c' and so on, and whose partitions p0 to p7 form three chains:
    p0 -> p1, p3 -> p2 and p6 -> p4, p5. Before the patch /proc/mtd lists
    "p2-p3-concat" and "p4-p5-p6-concat", whose blocks read "cd" and "efg";
    with it, "p3-p2-concat" and "p6-p4-p5-concat" read "dc" and "gef". The
    chain p0 -> p1 reads "ab" in both. mtd_virt_concat.o builds without
    warnings at W=1 for arm64 and for UML x86_64.
    
    Yuqiang Sun's teardown rework [1] and James Hilliard's retained
    concatenation patch [2] also keep each part in the slot of its node, as
    part of larger changes. This patch only orders the parts, once all of
    them are present, so subdev[] stays dense for the put and teardown
    paths and the fix can be backported on its own.
    
    [1] https://lore.kernel.org/all/20261103143953.69642-1-izaiahsun@gmail.com/
    [2] https://lore.kernel.org/all/20261115-submit-rawnand-retained-handle-lifetime-v1-v5-4-cbc49c5c5e74@gmail.com/
    
    I used Claude (Anthropic) to help write these patches.

 drivers/mtd/mtd_virt_concat.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/drivers/mtd/mtd_virt_concat.c b/drivers/mtd/mtd_virt_concat.c
index 6abc88251..37cac1ba4 100644
--- a/drivers/mtd/mtd_virt_concat.c
+++ b/drivers/mtd/mtd_virt_concat.c
@@ -284,6 +284,30 @@ int mtd_virt_concat_node_create(void)
 	return ret;
 }
 
+/**
+ * mtd_virt_concat_order - Put the parts of a concatenation in DT order
+ * @item:        concat item whose parts have all been added
+ *
+ * mtd_virt_concat_add() appends each part when its partition is added,
+ * which follows the order of the partitions in the partition table. Sort
+ * the parts into the order of @item->nodes[], which is the partition that
+ * carries "part-concat-next" followed by the partitions it lists.
+ */
+static void mtd_virt_concat_order(struct mtd_virt_concat_node *item)
+{
+	struct mtd_concat *concat = item->concat;
+	int i, j;
+
+	for (i = 0; i < concat->num_subdev; i++) {
+		for (j = i; j < concat->num_subdev; j++) {
+			if (concat->subdev[j]->dev.of_node == item->nodes[i]) {
+				swap(concat->subdev[i], concat->subdev[j]);
+				break;
+			}
+		}
+	}
+}
+
 /**
  * mtd_virt_concat_create_join - Create and register the concatenated
  *                                 MTD device.
@@ -310,6 +334,8 @@ int mtd_virt_concat_create_join(void)
 		if (item->count != concat->num_subdev) {
 			continue;
 		} else {
+			mtd_virt_concat_order(item);
+
 			/* Calculate the legth of the name of the virtual device */
 			for (idx = 0, name_sz = 0; idx < concat->num_subdev; idx++)
 				name_sz += (strlen(concat->subdev[idx]->name) + 1);

base-commit: b3b86fbd14b98c6a1786308ba0ecd666827dcb0e
-- 
2.53.0




More information about the linux-mtd mailing list