All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/5] Fix various issues with RFD and FTLs
@ 2021-08-07 21:45 Sean Young
  2021-08-07 21:45 ` [PATCH v2 1/5] mtd: rfd_ftl: allow use of MTD_RAM for testing purposes Sean Young
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Sean Young @ 2021-08-07 21:45 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-mtd

Changes since v2:
 - Review by Miquel Raynal
 - Improved commit message for discard and mtdram
 - Split use-after-free commit into two commits; see container-of()

Sean Young (5):
  mtd: rfd_ftl: allow use of MTD_RAM for testing purposes
  mtd: rfd_ftl: add discard support
  mtd: blk_devs: make discard work on FTLs
  mtd: rfd_ftl: fix use-after-free
  mtd: rfd_ftl: use container_of() rather than cast

 drivers/mtd/mtd_blkdevs.c |  1 +
 drivers/mtd/rfd_ftl.c     | 44 ++++++++++++++++++++++++++++++++-------
 2 files changed, 38 insertions(+), 7 deletions(-)

-- 
2.31.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* [PATCH v2 1/5] mtd: rfd_ftl: allow use of MTD_RAM for testing purposes
  2021-08-07 21:45 [PATCH v2 0/5] Fix various issues with RFD and FTLs Sean Young
@ 2021-08-07 21:45 ` Sean Young
  2021-08-16 14:27   ` Miquel Raynal
  2021-08-07 21:45 ` [PATCH v2 2/5] mtd: rfd_ftl: add discard support Sean Young
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Sean Young @ 2021-08-07 21:45 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-mtd

This allows the rfd_ftl to be used with the mtdram module, so we can
test different mtd sizes and test the rfd_ftl on machines without a
physical nor flash device.

Signed-off-by: Sean Young <sean@mess.org>
---
 drivers/mtd/rfd_ftl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index 6e0d5ce9b010..7b243f2b2fa3 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -720,7 +720,8 @@ static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
 {
 	struct partition *part;
 
-	if (mtd->type != MTD_NORFLASH || mtd->size > UINT_MAX)
+	if ((mtd->type != MTD_NORFLASH && mtd->type != MTD_RAM) ||
+	    mtd->size > UINT_MAX)
 		return;
 
 	part = kzalloc(sizeof(struct partition), GFP_KERNEL);
-- 
2.31.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* [PATCH v2 2/5] mtd: rfd_ftl: add discard support
  2021-08-07 21:45 [PATCH v2 0/5] Fix various issues with RFD and FTLs Sean Young
  2021-08-07 21:45 ` [PATCH v2 1/5] mtd: rfd_ftl: allow use of MTD_RAM for testing purposes Sean Young
@ 2021-08-07 21:45 ` Sean Young
  2021-08-16 14:27   ` Miquel Raynal
  2021-08-07 21:45 ` [PATCH v2 3/5] mtd: blk_devs: make discard work on FTLs Sean Young
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Sean Young @ 2021-08-07 21:45 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-mtd

I proposed this change 16 years ago before discard was a feature in
the block layer: https://lwn.net/Articles/162776/

Now that the block layer has discard, we can finally merge this change.

Discard is also known as trim. By implementing discard, both fstrim and
the discard filesystem option can be used.

Implementing discard in the ftl means that when files are removed, there
is less data in the ftl mapping. This means less stuff to move around for
erasing and also less erasing to do; this means improved wear levelling
and improved performance.

Signed-off-by: Sean Young <sean@mess.org>
---
 drivers/mtd/rfd_ftl.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index 7b243f2b2fa3..7f5f6d247cae 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -705,6 +705,34 @@ static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *
 	return rc;
 }
 
+static int rfd_ftl_discardsect(struct mtd_blktrans_dev *dev,
+			       unsigned long sector, unsigned int nr_sects)
+{
+	struct partition *part = (struct partition *)dev;
+	u_long addr;
+	int rc;
+
+	while (nr_sects) {
+		if (sector >= part->sector_count)
+			return -EIO;
+
+		addr = part->sector_map[sector];
+
+		if (addr != -1) {
+			rc = mark_sector_deleted(part, addr);
+			if (rc)
+				return rc;
+
+			part->sector_map[sector] = -1;
+		}
+
+		sector++;
+		nr_sects--;
+	}
+
+	return 0;
+}
+
 static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
 {
 	struct partition *part = (struct partition*)dev;
@@ -786,6 +814,7 @@ static struct mtd_blktrans_ops rfd_ftl_tr = {
 
 	.readsect	= rfd_ftl_readsect,
 	.writesect	= rfd_ftl_writesect,
+	.discard	= rfd_ftl_discardsect,
 	.getgeo		= rfd_ftl_getgeo,
 	.add_mtd	= rfd_ftl_add_mtd,
 	.remove_dev	= rfd_ftl_remove_dev,
-- 
2.31.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* [PATCH v2 3/5] mtd: blk_devs: make discard work on FTLs
  2021-08-07 21:45 [PATCH v2 0/5] Fix various issues with RFD and FTLs Sean Young
  2021-08-07 21:45 ` [PATCH v2 1/5] mtd: rfd_ftl: allow use of MTD_RAM for testing purposes Sean Young
  2021-08-07 21:45 ` [PATCH v2 2/5] mtd: rfd_ftl: add discard support Sean Young
@ 2021-08-07 21:45 ` Sean Young
  2021-08-16 14:26   ` Miquel Raynal
  2021-08-07 21:45 ` [PATCH v2 4/5] mtd: rfd_ftl: fix use-after-free Sean Young
  2021-08-07 21:45 ` [PATCH v2 5/5] mtd: rfd_ftl: use container_of() rather than cast Sean Young
  4 siblings, 1 reply; 11+ messages in thread
From: Sean Young @ 2021-08-07 21:45 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-mtd

If the discard_granularity is not set, discard will not work and the
following error is logged:

rfda: Error: discard_granularity is 0.

Since all the FTLs use a sector size of 512, this can be hardcoded.

Signed-off-by: Sean Young <sean@mess.org>
---
 drivers/mtd/mtd_blkdevs.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c
index 6ce4bc57f919..486251e058a6 100644
--- a/drivers/mtd/mtd_blkdevs.c
+++ b/drivers/mtd/mtd_blkdevs.c
@@ -419,6 +419,7 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
 	if (tr->discard) {
 		blk_queue_flag_set(QUEUE_FLAG_DISCARD, new->rq);
 		blk_queue_max_discard_sectors(new->rq, UINT_MAX);
+		new->rq->limits.discard_granularity = 512;
 	}
 
 	gd->queue = new->rq;
-- 
2.31.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* [PATCH v2 4/5] mtd: rfd_ftl: fix use-after-free
  2021-08-07 21:45 [PATCH v2 0/5] Fix various issues with RFD and FTLs Sean Young
                   ` (2 preceding siblings ...)
  2021-08-07 21:45 ` [PATCH v2 3/5] mtd: blk_devs: make discard work on FTLs Sean Young
@ 2021-08-07 21:45 ` Sean Young
  2021-08-16 14:26   ` Miquel Raynal
  2021-08-07 21:45 ` [PATCH v2 5/5] mtd: rfd_ftl: use container_of() rather than cast Sean Young
  4 siblings, 1 reply; 11+ messages in thread
From: Sean Young @ 2021-08-07 21:45 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-mtd

del_mtd_blktrans_dev() will kfree part, so after this call both part and
dev point to freed memory. Move the call to avoid use-after-free.

Signed-off-by: Sean Young <sean@mess.org>
---
 drivers/mtd/rfd_ftl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index 7f5f6d247cae..52be9f1fa9a2 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -800,10 +800,10 @@ static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
 			part->mbd.mtd->name, i, part->blocks[i].erases);
 	}
 
-	del_mtd_blktrans_dev(dev);
 	vfree(part->sector_map);
 	kfree(part->header_cache);
 	kfree(part->blocks);
+	del_mtd_blktrans_dev(dev);
 }
 
 static struct mtd_blktrans_ops rfd_ftl_tr = {
-- 
2.31.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* [PATCH v2 5/5] mtd: rfd_ftl: use container_of() rather than cast
  2021-08-07 21:45 [PATCH v2 0/5] Fix various issues with RFD and FTLs Sean Young
                   ` (3 preceding siblings ...)
  2021-08-07 21:45 ` [PATCH v2 4/5] mtd: rfd_ftl: fix use-after-free Sean Young
@ 2021-08-07 21:45 ` Sean Young
  2021-08-16 14:26   ` Miquel Raynal
  4 siblings, 1 reply; 11+ messages in thread
From: Sean Young @ 2021-08-07 21:45 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-mtd

The container_of() is much more readable and also safer.

Signed-off-by: Sean Young <sean@mess.org>
---
 drivers/mtd/rfd_ftl.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index 52be9f1fa9a2..af20a0a71108 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -239,7 +239,7 @@ static int scan_header(struct partition *part)
 
 static int rfd_ftl_readsect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
 {
-	struct partition *part = (struct partition*)dev;
+	struct partition *part = container_of(dev, struct partition, mbd);
 	u_long addr;
 	size_t retlen;
 	int rc;
@@ -600,7 +600,7 @@ static int find_free_sector(const struct partition *part, const struct block *bl
 
 static int do_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf, ulong *old_addr)
 {
-	struct partition *part = (struct partition*)dev;
+	struct partition *part = container_of(dev, struct partition, mbd);
 	struct block *block;
 	u_long addr;
 	int i;
@@ -666,7 +666,7 @@ static int do_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf,
 
 static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
 {
-	struct partition *part = (struct partition*)dev;
+	struct partition *part = container_of(dev, struct partition, mbd);
 	u_long old_addr;
 	int i;
 	int rc = 0;
@@ -708,7 +708,7 @@ static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *
 static int rfd_ftl_discardsect(struct mtd_blktrans_dev *dev,
 			       unsigned long sector, unsigned int nr_sects)
 {
-	struct partition *part = (struct partition *)dev;
+	struct partition *part = container_of(dev, struct partition, mbd);
 	u_long addr;
 	int rc;
 
@@ -735,7 +735,7 @@ static int rfd_ftl_discardsect(struct mtd_blktrans_dev *dev,
 
 static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
 {
-	struct partition *part = (struct partition*)dev;
+	struct partition *part = container_of(dev, struct partition, mbd);
 
 	geo->heads = 1;
 	geo->sectors = SECTORS_PER_TRACK;
@@ -792,7 +792,7 @@ static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
 
 static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
 {
-	struct partition *part = (struct partition*)dev;
+	struct partition *part = container_of(dev, struct partition, mbd);
 	int i;
 
 	for (i=0; i<part->total_blocks; i++) {
@@ -803,7 +803,7 @@ static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
 	vfree(part->sector_map);
 	kfree(part->header_cache);
 	kfree(part->blocks);
-	del_mtd_blktrans_dev(dev);
+	del_mtd_blktrans_dev(&part->mbd);
 }
 
 static struct mtd_blktrans_ops rfd_ftl_tr = {
-- 
2.31.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [PATCH v2 5/5] mtd: rfd_ftl: use container_of() rather than cast
  2021-08-07 21:45 ` [PATCH v2 5/5] mtd: rfd_ftl: use container_of() rather than cast Sean Young
@ 2021-08-16 14:26   ` Miquel Raynal
  0 siblings, 0 replies; 11+ messages in thread
From: Miquel Raynal @ 2021-08-16 14:26 UTC (permalink / raw)
  To: Sean Young, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, linux-mtd

On Sat, 2021-08-07 at 21:45:38 UTC, Sean Young wrote:
> The container_of() is much more readable and also safer.
> 
> Signed-off-by: Sean Young <sean@mess.org>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.

Miquel

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [PATCH v2 4/5] mtd: rfd_ftl: fix use-after-free
  2021-08-07 21:45 ` [PATCH v2 4/5] mtd: rfd_ftl: fix use-after-free Sean Young
@ 2021-08-16 14:26   ` Miquel Raynal
  0 siblings, 0 replies; 11+ messages in thread
From: Miquel Raynal @ 2021-08-16 14:26 UTC (permalink / raw)
  To: Sean Young, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, linux-mtd

On Sat, 2021-08-07 at 21:45:37 UTC, Sean Young wrote:
> del_mtd_blktrans_dev() will kfree part, so after this call both part and
> dev point to freed memory. Move the call to avoid use-after-free.
> 
> Signed-off-by: Sean Young <sean@mess.org>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.

Miquel

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [PATCH v2 3/5] mtd: blk_devs: make discard work on FTLs
  2021-08-07 21:45 ` [PATCH v2 3/5] mtd: blk_devs: make discard work on FTLs Sean Young
@ 2021-08-16 14:26   ` Miquel Raynal
  0 siblings, 0 replies; 11+ messages in thread
From: Miquel Raynal @ 2021-08-16 14:26 UTC (permalink / raw)
  To: Sean Young, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, linux-mtd

On Sat, 2021-08-07 at 21:45:36 UTC, Sean Young wrote:
> If the discard_granularity is not set, discard will not work and the
> following error is logged:
> 
> rfda: Error: discard_granularity is 0.
> 
> Since all the FTLs use a sector size of 512, this can be hardcoded.
> 
> Signed-off-by: Sean Young <sean@mess.org>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.

Miquel

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [PATCH v2 2/5] mtd: rfd_ftl: add discard support
  2021-08-07 21:45 ` [PATCH v2 2/5] mtd: rfd_ftl: add discard support Sean Young
@ 2021-08-16 14:27   ` Miquel Raynal
  0 siblings, 0 replies; 11+ messages in thread
From: Miquel Raynal @ 2021-08-16 14:27 UTC (permalink / raw)
  To: Sean Young, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, linux-mtd

On Sat, 2021-08-07 at 21:45:35 UTC, Sean Young wrote:
> I proposed this change 16 years ago before discard was a feature in
> the block layer: https://lwn.net/Articles/162776/
> 
> Now that the block layer has discard, we can finally merge this change.
> 
> Discard is also known as trim. By implementing discard, both fstrim and
> the discard filesystem option can be used.
> 
> Implementing discard in the ftl means that when files are removed, there
> is less data in the ftl mapping. This means less stuff to move around for
> erasing and also less erasing to do; this means improved wear levelling
> and improved performance.
> 
> Signed-off-by: Sean Young <sean@mess.org>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.

Miquel

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

* Re: [PATCH v2 1/5] mtd: rfd_ftl: allow use of MTD_RAM for testing purposes
  2021-08-07 21:45 ` [PATCH v2 1/5] mtd: rfd_ftl: allow use of MTD_RAM for testing purposes Sean Young
@ 2021-08-16 14:27   ` Miquel Raynal
  0 siblings, 0 replies; 11+ messages in thread
From: Miquel Raynal @ 2021-08-16 14:27 UTC (permalink / raw)
  To: Sean Young, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, linux-mtd

On Sat, 2021-08-07 at 21:45:34 UTC, Sean Young wrote:
> This allows the rfd_ftl to be used with the mtdram module, so we can
> test different mtd sizes and test the rfd_ftl on machines without a
> physical nor flash device.
> 
> Signed-off-by: Sean Young <sean@mess.org>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.

Miquel

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

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

end of thread, other threads:[~2021-08-16 14:28 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-07 21:45 [PATCH v2 0/5] Fix various issues with RFD and FTLs Sean Young
2021-08-07 21:45 ` [PATCH v2 1/5] mtd: rfd_ftl: allow use of MTD_RAM for testing purposes Sean Young
2021-08-16 14:27   ` Miquel Raynal
2021-08-07 21:45 ` [PATCH v2 2/5] mtd: rfd_ftl: add discard support Sean Young
2021-08-16 14:27   ` Miquel Raynal
2021-08-07 21:45 ` [PATCH v2 3/5] mtd: blk_devs: make discard work on FTLs Sean Young
2021-08-16 14:26   ` Miquel Raynal
2021-08-07 21:45 ` [PATCH v2 4/5] mtd: rfd_ftl: fix use-after-free Sean Young
2021-08-16 14:26   ` Miquel Raynal
2021-08-07 21:45 ` [PATCH v2 5/5] mtd: rfd_ftl: use container_of() rather than cast Sean Young
2021-08-16 14:26   ` Miquel Raynal

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.