mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2] mtd: don't try to detect UBI in too small flash partitions
@ 2024-02-29 16:23 Ahmad Fatoum
  2024-03-04  9:19 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2024-02-29 16:23 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The detect callback will try to detect an UBI volume inside a partition.

This can misbehave for partitions without UBI:

  - For partitions smaller than 512 bytes, detect fails with -EINVAL,
    because it wasn't possible to read as much

  - For partitions smaller than 64 PEBs, detect will erroneously do
    64 lookups and attempt reading beyond the partition

  - For partition bigger than 64 PEBs, but without UBI, detect will
    read 512 bytes off each PEB without ever finding the UBI.

Fix this by not attempting to read more PEBs than they are and ensure
that there at least 5 PEBs, because anything lower wouldn't account for
UBI overhead[1].

[1]: http://www.linux-mtd.infradead.org/doc/ubi.html#L_overhead

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - change minimum PEB count to 5 instead of 1 (Sascha)
  - change erroneous max() to min() (Sascha)
  - reword commit message to avoid confusion around 512 byte read
    (Sascha)

---
 drivers/mtd/core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index 97a7996cf68e..ec2c3ff7bb41 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -599,7 +599,10 @@ static int mtd_detect(struct device *dev)
 	enum filetype filetype;
 	int npebs = mtd_div_by_eb(mtd->size, mtd);
 
-	npebs = max(npebs, 64);
+	/* No point in looking for UBI on a partition that's too small */
+	npebs = min(npebs, 64);
+	if (npebs < 5)
+		return 0;
 
 	/*
 	 * Do not try to attach an UBI device if this device has partitions
-- 
2.39.2




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] mtd: don't try to detect UBI in too small flash partitions
  2024-02-29 16:23 [PATCH v2] mtd: don't try to detect UBI in too small flash partitions Ahmad Fatoum
@ 2024-03-04  9:19 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2024-03-04  9:19 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Thu, 29 Feb 2024 17:23:23 +0100, Ahmad Fatoum wrote:
> The detect callback will try to detect an UBI volume inside a partition.
> 
> This can misbehave for partitions without UBI:
> 
>   - For partitions smaller than 512 bytes, detect fails with -EINVAL,
>     because it wasn't possible to read as much
> 
> [...]

Applied, thanks!

[1/1] mtd: don't try to detect UBI in too small flash partitions
      https://git.pengutronix.de/cgit/barebox/commit/?id=023cae6caa63 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-04  9:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-29 16:23 [PATCH v2] mtd: don't try to detect UBI in too small flash partitions Ahmad Fatoum
2024-03-04  9:19 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox