mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: "Christian Thießen via B4 Relay" <devnull+christian.thiessen.airbus.com@kernel.org>
To: BAREBOX <barebox@lists.infradead.org>
Cc: "Ahmad Fatoum" <a.fatoum@pengutronix.de>,
	"Christian Thießen" <christian.thiessen@airbus.com>
Subject: [PATCH v2] mtd: Make mtd_erase fail gracefully if CONFIG_MTD_WRITE is off
Date: Wed, 03 Sep 2025 16:31:21 +0200	[thread overview]
Message-ID: <20250903-mtd-erase-enosys-v2-1-7b7f28302f25@airbus.com> (raw)

From: Christian Thießen <christian.thiessen@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@pengutronix.de>
Signed-off-by: Christian Thießen <christian.thiessen@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@airbus.com>





             reply	other threads:[~2025-09-03 19:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-03 14:31 Christian Thießen via B4 Relay [this message]
2025-09-04  5:39 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250903-mtd-erase-enosys-v2-1-7b7f28302f25@airbus.com \
    --to=devnull+christian.thiessen.airbus.com@kernel.org \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=christian.thiessen@airbus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox