[PATCH v2] mtd: Make mtd_erase fail gracefully if CONFIG_MTD_WRITE is off

Christian Thießen via B4 Relay devnull+christian.thiessen.airbus.com at kernel.org
Wed Sep 3 07:31:21 PDT 2025


From: Christian Thießen <christian.thiessen at airbus.com>

When CONFIG_MTD_WRITE is unset, the mtd->_erase member of partition
devices does not get initialized, so it remains NULL.
However when running ubiattach on an UBI partition, mtd_erase() still
gets called, tries to call mtd->_erase and crashes. Make it fail
gracefully by returning ENOSYS ("Function not implemented") instead.

Reviewed-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
Signed-off-by: Christian Thießen <christian.thiessen at airbus.com>
---
As discussed in https://github.com/barebox/barebox/issues/39 this patch
enables ubiattach-ing a NAND partition even if CONFIG_MTD_WRITE is
turned off, by making mtd_erase fail gracefully.
---
Hi,
thank you for the warm welcome and the constructive feedback!
Changes in v2: Addressed review comments.
- Changed from ifdef to IS_ENABLED
- Moved the sign-off tag to the commit message (why does b4 add it to
  the cover letter?)
- Link to v1: https://lore.kernel.org/r/20250903-mtd-erase-enosys-v1-1-967a95172c4e@airbus.com
---
 drivers/mtd/core.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index 6162ec450fc1c3a0fa6339042c4d627e0f9567d0..bb579a8f5249981ec1623060ddb1b7056a06c1a0 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -436,6 +436,8 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
 {
 	if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
 		return -EINVAL;
+	if (!IS_ENABLED(CONFIG_MTD_WRITE))
+		return -ENOSYS;
 	if (!(mtd->flags & MTD_WRITEABLE))
 		return -EROFS;
 	instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;

---
base-commit: b5561f3bdd4845b478e5b35aab04f1d8e71ea93b
change-id: 20250902-mtd-erase-enosys-2d80e4bc77c8

Best regards,
-- 
Christian Thießen <christian.thiessen at airbus.com>





More information about the barebox mailing list