From: Boris Brezillon <boris.brezillon@free-electrons.com>
To: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Cc: dwmw2@infradead.org, computersforpeace@gmail.com,
marek.vasut@gmail.com, richard@nod.at,
cyrille.pitchen@wedev4u.fr, linux-mtd@lists.infradead.org,
linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] mtd: lpddr: Fix a potential double mutex_lock
Date: Tue, 31 Oct 2017 15:58:00 +0100 [thread overview]
Message-ID: <20171031155800.378cfa0b@bbrezillon> (raw)
In-Reply-To: <20171022082831.26560-1-christophe.jaillet@wanadoo.fr>
Hi Christophe,
On Sun, 22 Oct 2017 10:28:31 +0200
Christophe JAILLET <christophe.jaillet@wanadoo.fr> wrote:
> If 'chip->state == FL_SYNCING', we will 'goto retry' with the mutex
> '&shared->lock' already taken.
> In such a case, the 'mutex_lock' at line 927 can never succeed.
>
> In order to avoid a deadlock, move the 'mutex_lock(&shared->lock)' at the
> very end of the block.
>
> This has been spotted with the following coccinelle script:
>
> @find@
> expression x, t;
> @@
>
> mutex_lock(x);
> ... when != mutex_unlock(x)
> mutex_lock(t);
>
> @@
> expression find.t;
> expression find.x;
> @@
>
> * mutex_lock(t);
> ... when != mutex_unlock(t)
> * mutex_lock(x);
>
>
> Fixes: c68264711ca6 ("[MTD] LPDDR Command set driver")
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
> Review carefuly, untested.
> ---
> drivers/mtd/lpddr/lpddr_cmds.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mtd/lpddr/lpddr_cmds.c b/drivers/mtd/lpddr/lpddr_cmds.c
> index 018c75faadb3..830dd0855ab3 100644
> --- a/drivers/mtd/lpddr/lpddr_cmds.c
> +++ b/drivers/mtd/lpddr/lpddr_cmds.c
> @@ -237,7 +237,6 @@ static int get_chip(struct map_info *map, struct flchip *chip, int mode)
> mutex_unlock(&contender->mutex);
> return ret;
> }
> - mutex_lock(&shared->lock);
>
> /* We should not own chip if it is already in FL_SYNCING
> * state. Put contender and retry. */
> @@ -247,6 +246,8 @@ static int get_chip(struct map_info *map, struct flchip *chip, int mode)
> goto retry;
> }
> mutex_unlock(&contender->mutex);
> +
> + mutex_lock(&shared->lock);
I had a quick look at this code and I must say the locking in this
driver is a real nightmare (apparently it was copied from CFI driver).
It's not entirely clear to me if the shared lock should be taken before
releasing the contender one (as done today) or if it can safely be
moved at the end of the block (as you suggest).
Unless we have someone who knows about the locking scheme of
cfi/lpddr and can confirm that moving the mutex_lock() at the end of
the block is safe, I'd recommend that we take a conservative approach
and add a mutex_unlock(&shared->lock) in the retry path and keep the
existing mutex_lock() in place.
> }
>
> /* Check if we have suspended erase on this chip.
next prev parent reply other threads:[~2017-10-31 14:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-22 8:28 [PATCH] mtd: lpddr: Fix a potential double mutex_lock Christophe JAILLET
2017-10-31 14:58 ` Boris Brezillon [this message]
2017-11-03 6:21 ` Christophe JAILLET
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=20171031155800.378cfa0b@bbrezillon \
--to=boris.brezillon@free-electrons.com \
--cc=christophe.jaillet@wanadoo.fr \
--cc=computersforpeace@gmail.com \
--cc=cyrille.pitchen@wedev4u.fr \
--cc=dwmw2@infradead.org \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=marek.vasut@gmail.com \
--cc=richard@nod.at \
/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