Received: by 10.223.164.221 with SMTP id h29csp3987004wrb; Tue, 31 Oct 2017 07:59:08 -0700 (PDT) X-Google-Smtp-Source: ABhQp+Tyh8Tl4lNaX5/rgJjxNIBF35LatqdSlt80yD73EQG2ACniukbagggcorwIf1YFd0KsW8M7 X-Received: by 10.84.238.136 with SMTP id v8mr2101853plk.116.1509461948194; Tue, 31 Oct 2017 07:59:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509461948; cv=none; d=google.com; s=arc-20160816; b=AfRTnloASSqgpYWL3AqXZybSYLFLvwwfAXJ2e0gXNytijobqwe5IoXnVcGPaQR5dBT mpV/COa7MYaayHIIwwprQR1EjFlQg5Vz8oW6cK4PIVXgr4arbXMT/K8XxTgrU+QndJls ck7DdD87RHVVEHqknbI1uYoWZu9wWLPs4WbVD8hZXOnuU+SoFV4Kd2fj++9kPrZJ2p+h l5F0wBA15jN1DrqiGrZt72U7upoup3OQIx7e6E950yILyaLM2xxtdFqyWcwYhxaErdnM wUVi+HWIr5Shg57FdHP6xquWivVSonk47ZRdxWWgx9fbtnT/Y8AvW1QMZncwHyVNGXJv gOqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :arc-authentication-results; bh=KHWtbpNSqFM506h48Cz1ukYwuyniflhFcjT8jtZaK3E=; b=zajJSQbHqHlVVCG8yDqJTgfUK6mYnoLiKH2TPprxZm7BKnILrEXY2PSJArO0ctz21g RP1KNQICLeRNr6EfyDM8/7H7zB1Xo8YRNEv64Rjy3/KBpP/IjyAbPwwlr3clAvamsB+6 7ew77QSM7V3Gqrc8qdpfOyfUloebGbt2g4ytGtB5DQrzOQaeS9UsVGA79m0L07x8TbFo 50eD/T2ypUQnmbi/wvP9neA4YwDj8PDHE6A6T2C3Af2lhJrHQCwBOU6XzK2aBOil2q+o sCJCaqSNJRc+Fly5wuXObcW3nZhaIhMfnlY+bbtqFTfsT/fjkJLkKSnDuzvljzaqNBvS eKlw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1si1718325plx.498.2017.10.31.07.58.55; Tue, 31 Oct 2017 07:59:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753520AbdJaO6E (ORCPT + 99 others); Tue, 31 Oct 2017 10:58:04 -0400 Received: from mail.free-electrons.com ([62.4.15.54]:57379 "EHLO mail.free-electrons.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751807AbdJaO6C (ORCPT ); Tue, 31 Oct 2017 10:58:02 -0400 Received: by mail.free-electrons.com (Postfix, from userid 110) id D4DEF207EA; Tue, 31 Oct 2017 15:57:59 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on mail.free-electrons.com X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,SHORTCIRCUIT shortcircuit=ham autolearn=disabled version=3.4.0 Received: from bbrezillon (LStLambert-657-1-97-87.w90-63.abo.wanadoo.fr [90.63.216.87]) by mail.free-electrons.com (Postfix) with ESMTPSA id 94BE3207C6; Tue, 31 Oct 2017 15:57:59 +0100 (CET) Date: Tue, 31 Oct 2017 15:58:00 +0100 From: Boris Brezillon To: Christophe JAILLET 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 Message-ID: <20171031155800.378cfa0b@bbrezillon> In-Reply-To: <20171022082831.26560-1-christophe.jaillet@wanadoo.fr> References: <20171022082831.26560-1-christophe.jaillet@wanadoo.fr> X-Mailer: Claws Mail 3.14.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Christophe, On Sun, 22 Oct 2017 10:28:31 +0200 Christophe JAILLET 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 > --- > 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. From 1581945686320678652@xxx Sun Oct 22 08:29:30 +0000 2017 X-GM-THRID: 1581945686320678652 X-Gmail-Labels: Inbox,Category Forums