Fix various issues with RFD and FTLs
v2 cover Fix various issues with RFD and FTLs
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(-)
Signature · 6 lines
2.31.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
PATCH v2 1/5 mtd: rfd_ftl: allow use of MTD_RAM for testing purposes
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.
--- drivers/mtd/rfd_ftl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mtd/rfd_ftl.c +2 -1
@@ -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 );
Signature · 6 lines
2.31.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Re: [PATCH v2 1/5] mtd: rfd_ftl: allow use of MTD_RAM for testing purposes
Quoted 5 lines · Sean Young
This allows the
rfd_ftlto be used with the mtdram module, so we can test different mtd sizes and test therfd_ftlon machines without a physical nor flash device.Signed-off-by Sean Young <[email protected]>
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/
PATCH v2 2/5 mtd: rfd_ftl: add discard support
In reply to Sean YoungI 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.
--- drivers/mtd/rfd_ftl.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
drivers/mtd/rfd_ftl.c +29 -0
@@ -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,
Signature · 6 lines
2.31.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Re: [PATCH v2 2/5] mtd: rfd_ftl: add discard support
Quoted 8 lines · Sean Young
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.
Quoted 6 lines
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 <[email protected]>
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/
PATCH v2 3/5 mtd: blk_devs: make discard work on FTLs
In reply to Sean YoungIf 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.
--- drivers/mtd/mtd_blkdevs.c | 1 + 1 file changed, 1 insertion(+)
drivers/mtd/mtd_blkdevs.c +1 -0
@@ -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;
Signature · 6 lines
2.31.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Re: [PATCH v2 3/5] mtd: blk_devs: make discard work on FTLs
Quoted 8 lines · Sean Young
If the
discard_granularityis not set, discard will not work and the following error is logged:rfda: Error:
discard_granularityis 0.Since all the FTLs use a sector size of 512, this can be hardcoded.
Signed-off-by Sean Young <[email protected]>
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/
PATCH v2 4/5 mtd: rfd_ftl: fix use-after-free
In reply to Sean Youngdel_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.
--- drivers/mtd/rfd_ftl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mtd/rfd_ftl.c +1 -1
@@ -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 = {
Signature · 6 lines
2.31.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Re: [PATCH v2 4/5] mtd: rfd_ftl: fix use-after-free
Quoted 4 lines · Sean Young
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 <[email protected]>
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/
PATCH v2 5/5 mtd: rfd_ftl: use container_of() rather than cast
In reply to Sean YoungThe container_of() is much more readable and also safer.
--- drivers/mtd/rfd_ftl.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/mtd/rfd_ftl.c +7 -7
@@ -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 *blstatic 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 = {
Signature · 6 lines
2.31.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Re: [PATCH v2 5/5] mtd: rfd_ftl: use container_of() rather than cast
Quoted 3 lines · Sean Young
The
container_of()is much more readable and also safer.Signed-off-by Sean Young <[email protected]>
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/