All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mtd: Make sure the device supports erase operations in mtd_erase()
@ 2018-01-22  9:38 Boris Brezillon
  2018-01-23  8:00 ` Miquel Raynal
  2018-02-17  8:38 ` Boris Brezillon
  0 siblings, 2 replies; 4+ messages in thread
From: Boris Brezillon @ 2018-01-22  9:38 UTC (permalink / raw)
  To: David Woodhouse, Brian Norris, Boris Brezillon, Marek Vasut,
	Richard Weinberger, Cyrille Pitchen, linux-mtd
  Cc: Peter Pan, Frieder Schrempf, Miquel Raynal

Some devices do not implement ->_erase() or have an invalid ->erasesize
value. In this case, mtd_erase() should return -ENOTSUPP.

Note that the test is not done on the MTD_NO_ERASE flag because this
flag means 'erasing a block before writing to it is unnecessary',
not 'the erase operation is not supported'. Actually, some drivers are
setting the MTD_NO_ERASE flag but still implementing the ->_erase()
hook and setting a valid ->erasesize value.

Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
---
 drivers/mtd/mtdcore.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index d7ab091b36b2..f24144cbc99c 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -971,10 +971,14 @@ EXPORT_SYMBOL_GPL(__put_mtd_device);
  */
 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
 {
+	if (!mtd->erasesize || !mtd->_erase)
+		return -ENOTSUPP;
+
 	if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
 		return -EINVAL;
 	if (!(mtd->flags & MTD_WRITEABLE))
 		return -EROFS;
+
 	instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
 	if (!instr->len) {
 		instr->state = MTD_ERASE_DONE;
-- 
2.11.0

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

* Re: [PATCH] mtd: Make sure the device supports erase operations in mtd_erase()
  2018-01-22  9:38 [PATCH] mtd: Make sure the device supports erase operations in mtd_erase() Boris Brezillon
@ 2018-01-23  8:00 ` Miquel Raynal
  2018-01-23 10:24   ` Boris Brezillon
  2018-02-17  8:38 ` Boris Brezillon
  1 sibling, 1 reply; 4+ messages in thread
From: Miquel Raynal @ 2018-01-23  8:00 UTC (permalink / raw)
  To: Boris Brezillon
  Cc: David Woodhouse, Brian Norris, Marek Vasut, Richard Weinberger,
	Cyrille Pitchen, linux-mtd, Peter Pan, Frieder Schrempf

Hello Boris,

On Mon, 22 Jan 2018 10:38:01 +0100
Boris Brezillon <boris.brezillon@free-electrons.com> wrote:

> Some devices do not implement ->_erase() or have an invalid ->erasesize
> value. In this case, mtd_erase() should return -ENOTSUPP.
> 
> Note that the test is not done on the MTD_NO_ERASE flag because this
> flag means 'erasing a block before writing to it is unnecessary',
> not 'the erase operation is not supported'. Actually, some drivers are
> setting the MTD_NO_ERASE flag but still implementing the ->_erase()
> hook and setting a valid ->erasesize value.
> 
> Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
> ---
>  drivers/mtd/mtdcore.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> index d7ab091b36b2..f24144cbc99c 100644
> --- a/drivers/mtd/mtdcore.c
> +++ b/drivers/mtd/mtdcore.c
> @@ -971,10 +971,14 @@ EXPORT_SYMBOL_GPL(__put_mtd_device);
>   */
>  int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
>  {
> +	if (!mtd->erasesize || !mtd->_erase)
> +		return -ENOTSUPP;
> +
>  	if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
>  		return -EINVAL;
>  	if (!(mtd->flags & MTD_WRITEABLE))
>  		return -EROFS;

This remark is not inherent to this patch in particular but as we are
adding a new error path, I thought it might be interesting to also
patch:
- INFTL_formatblock() from inftlmount.c [1] (mtd_erase called twice)
- NFTL_formatblock() from nftlmount.c [2]

They both call mtd_erase() without checking the return code and then
error out only if instr->state == MTD_ERASE_FAILED, which has not been
set before quitting mtd_erase() in the conditions above. I guess the
right thing to do is to add another condition in both functions on the
return code of mtd_erase(). What do you think?

Otherwise:
Reviewed-by: Miquel Raynal <miquel.raynal@free-electrons.com>

Have a good day,
Miquèl

[1]
http://elixir.free-electrons.com/linux/latest/source/drivers/mtd/inftlmount.c#L396
[2]
http://elixir.free-electrons.com/linux/latest/source/drivers/mtd/nftlmount.c#L334

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

* Re: [PATCH] mtd: Make sure the device supports erase operations in mtd_erase()
  2018-01-23  8:00 ` Miquel Raynal
@ 2018-01-23 10:24   ` Boris Brezillon
  0 siblings, 0 replies; 4+ messages in thread
From: Boris Brezillon @ 2018-01-23 10:24 UTC (permalink / raw)
  To: Miquel Raynal
  Cc: David Woodhouse, Brian Norris, Marek Vasut, Richard Weinberger,
	Cyrille Pitchen, linux-mtd, Peter Pan, Frieder Schrempf

On Tue, 23 Jan 2018 09:00:46 +0100
Miquel Raynal <miquel.raynal@free-electrons.com> wrote:

> Hello Boris,
> 
> On Mon, 22 Jan 2018 10:38:01 +0100
> Boris Brezillon <boris.brezillon@free-electrons.com> wrote:
> 
> > Some devices do not implement ->_erase() or have an invalid ->erasesize
> > value. In this case, mtd_erase() should return -ENOTSUPP.
> > 
> > Note that the test is not done on the MTD_NO_ERASE flag because this
> > flag means 'erasing a block before writing to it is unnecessary',
> > not 'the erase operation is not supported'. Actually, some drivers are
> > setting the MTD_NO_ERASE flag but still implementing the ->_erase()
> > hook and setting a valid ->erasesize value.
> > 
> > Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
> > ---
> >  drivers/mtd/mtdcore.c | 4 ++++
> >  1 file changed, 4 insertions(+)
> > 
> > diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> > index d7ab091b36b2..f24144cbc99c 100644
> > --- a/drivers/mtd/mtdcore.c
> > +++ b/drivers/mtd/mtdcore.c
> > @@ -971,10 +971,14 @@ EXPORT_SYMBOL_GPL(__put_mtd_device);
> >   */
> >  int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
> >  {
> > +	if (!mtd->erasesize || !mtd->_erase)
> > +		return -ENOTSUPP;
> > +
> >  	if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
> >  		return -EINVAL;
> >  	if (!(mtd->flags & MTD_WRITEABLE))
> >  		return -EROFS;  
> 
> This remark is not inherent to this patch in particular but as we are
> adding a new error path, I thought it might be interesting to also
> patch:
> - INFTL_formatblock() from inftlmount.c [1] (mtd_erase called twice)
> - NFTL_formatblock() from nftlmount.c [2]
> 
> They both call mtd_erase() without checking the return code and then
> error out only if instr->state == MTD_ERASE_FAILED, which has not been
> set before quitting mtd_erase() in the conditions above. I guess the
> right thing to do is to add another condition in both functions on the
> return code of mtd_erase(). What do you think?

This sounds reasonable.

> 
> Otherwise:
> Reviewed-by: Miquel Raynal <miquel.raynal@free-electrons.com>

Thanks.

Boris

> 
> Have a good day,
> Miquèl
> 
> [1]
> http://elixir.free-electrons.com/linux/latest/source/drivers/mtd/inftlmount.c#L396
> [2]
> http://elixir.free-electrons.com/linux/latest/source/drivers/mtd/nftlmount.c#L334

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

* Re: [PATCH] mtd: Make sure the device supports erase operations in mtd_erase()
  2018-01-22  9:38 [PATCH] mtd: Make sure the device supports erase operations in mtd_erase() Boris Brezillon
  2018-01-23  8:00 ` Miquel Raynal
@ 2018-02-17  8:38 ` Boris Brezillon
  1 sibling, 0 replies; 4+ messages in thread
From: Boris Brezillon @ 2018-02-17  8:38 UTC (permalink / raw)
  To: Boris Brezillon
  Cc: David Woodhouse, Brian Norris, Marek Vasut, Richard Weinberger,
	Cyrille Pitchen, linux-mtd, Peter Pan, Frieder Schrempf,
	Miquel Raynal

On Mon, 22 Jan 2018 10:38:01 +0100
Boris Brezillon <boris.brezillon@free-electrons.com> wrote:

> Some devices do not implement ->_erase() or have an invalid ->erasesize
> value. In this case, mtd_erase() should return -ENOTSUPP.
> 
> Note that the test is not done on the MTD_NO_ERASE flag because this
> flag means 'erasing a block before writing to it is unnecessary',
> not 'the erase operation is not supported'. Actually, some drivers are
> setting the MTD_NO_ERASE flag but still implementing the ->_erase()
> hook and setting a valid ->erasesize value.

Applied.

> 
> Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
> ---
>  drivers/mtd/mtdcore.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
> index d7ab091b36b2..f24144cbc99c 100644
> --- a/drivers/mtd/mtdcore.c
> +++ b/drivers/mtd/mtdcore.c
> @@ -971,10 +971,14 @@ EXPORT_SYMBOL_GPL(__put_mtd_device);
>   */
>  int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
>  {
> +	if (!mtd->erasesize || !mtd->_erase)
> +		return -ENOTSUPP;
> +
>  	if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
>  		return -EINVAL;
>  	if (!(mtd->flags & MTD_WRITEABLE))
>  		return -EROFS;
> +
>  	instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
>  	if (!instr->len) {
>  		instr->state = MTD_ERASE_DONE;



-- 
Boris Brezillon, Bootlin (formerly Free Electrons)
Embedded Linux and Kernel engineering
http://bootlin.com

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

end of thread, other threads:[~2018-02-17  8:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-22  9:38 [PATCH] mtd: Make sure the device supports erase operations in mtd_erase() Boris Brezillon
2018-01-23  8:00 ` Miquel Raynal
2018-01-23 10:24   ` Boris Brezillon
2018-02-17  8:38 ` Boris Brezillon

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.