From: Ben Hutchings <ben.hutchings@codethink.co.uk>
To: Ben Dooks <ben.dooks@codethink.co.uk>
Cc: linux-kernel@lists.codethink.co.uk,
David Woodhouse <dwmw2@infradead.org>,
Brian Norris <computersforpeace@gmail.com>,
linux-mtd@lists.infradead.org
Subject: Re: [Linux-kernel] [PATCH 1/1] mtd: mtdblock: avoid __might_sleep warnings in mtd_erase
Date: Wed, 26 Apr 2017 19:18:54 +0100 [thread overview]
Message-ID: <1493230734.10415.140.camel@codethink.co.uk> (raw)
In-Reply-To: <20170426174609.29433-2-ben.dooks@codethink.co.uk>
On Wed, 2017-04-26 at 18:46 +0100, Ben Dooks wrote:
> The mtd_erase() call can hit code that will trigger warnings
> from __might_sleep(), such as the do_erase_oneblock() function
> on the cfi_cmdset_0002.c file.
>
> This is due to some of the erase functions doing the work in the
> thread they are called in, which means that the erase_write()
> should only go into TASK_INTERRUPTIBLE once the mtd_erase call
> has returned.
[...]
> diff --git a/drivers/mtd/mtdblock.c b/drivers/mtd/mtdblock.c
> index bb4c14f83c75..4b1cd464f919 100644
> --- a/drivers/mtd/mtdblock.c
> +++ b/drivers/mtd/mtdblock.c
> @@ -68,6 +68,7 @@ static int erase_write (struct mtd_info *mtd, unsigned long pos,
> DECLARE_WAITQUEUE(wait, current);
> wait_queue_head_t wait_q;
> size_t retlen;
> + long timeout = 1;
> int ret;
>
> /*
> @@ -81,12 +82,10 @@ static int erase_write (struct mtd_info *mtd, unsigned long pos,
> erase.len = len;
> erase.priv = (u_long)&wait_q;
>
> - set_current_state(TASK_INTERRUPTIBLE);
> add_wait_queue(&wait_q, &wait);
>
> ret = mtd_erase(mtd, &erase);
> if (ret) {
> - set_current_state(TASK_RUNNING);
> remove_wait_queue(&wait_q, &wait);
> printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
> "on \"%s\" failed\n",
> @@ -94,8 +93,18 @@ static int erase_write (struct mtd_info *mtd, unsigned long pos,
> return ret;
> }
>
> - schedule(); /* Wait for erase to finish. */
> + if (erase->state != MTD_ERASE_DONE &&
> + erase->state != MTD_ERASE_FAILED)
> + timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
> + MAX_SCHEDULE_TIMEOUT);
If mtd_erase() returns 0 then the wait queue either has been woken or
will be woken. Since we're already on the wait queue, it's safe to wait
unconditionally.
I think that making the wait conditional results in a race condition
that could result in returning too early.
Also there seems to be another existing problem here: if this is
interrupted and we return early then the driver can use-after-free the
wait queue and erase structure. mtdchar waits uninterruptibly for
exactly this reason.
We really ought to have an always-synchronous wrapper for mtd_erase(),
because this seems to be hard to get right...
Ben.
> remove_wait_queue(&wait_q, &wait);
> + if (timeout == 0) {
> + printk (KERN_WARNING "mtdblock: erase of region [0x%lx, 0x%x] "
> + "on \"%s\" failed\n",
> + pos, len, mtd->name);
> + return -ETIMEDOUT;
> + }
>
> /*
> * Next, write the data to flash.
--
Ben Hutchings
Software Developer, Codethink Ltd.
next prev parent reply other threads:[~2017-04-26 18:19 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-26 17:46 RFC: fixup for mtdblock issue with erase warnings Ben Dooks
2017-04-26 17:46 ` [PATCH 1/1] mtd: mtdblock: avoid __might_sleep warnings in mtd_erase Ben Dooks
2017-04-26 18:18 ` Ben Hutchings [this message]
2017-04-27 8:27 ` [Linux-kernel] " Ben Dooks
2017-04-27 12:04 ` Ben Hutchings
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=1493230734.10415.140.camel@codethink.co.uk \
--to=ben.hutchings@codethink.co.uk \
--cc=ben.dooks@codethink.co.uk \
--cc=computersforpeace@gmail.com \
--cc=dwmw2@infradead.org \
--cc=linux-kernel@lists.codethink.co.uk \
--cc=linux-mtd@lists.infradead.org \
/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