Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751067AbdIMEAq (ORCPT ); Wed, 13 Sep 2017 00:00:46 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:6041 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750728AbdIMEAp (ORCPT ); Wed, 13 Sep 2017 00:00:45 -0400 Subject: Re: [PATCH] scsi: shost->async_scan should be protected by mutex_lock To: Tyrel Datwyler , , , CC: , Suoben , "Wulizhen (Pss)" References: <8e8d0a1b-f053-6eab-a85a-ec83201b50be@gmail.com> From: "Ouyangzhaowei (Charles)" Message-ID: <4ec18ead-f6d0-c10c-71ad-d6e7e6fb47d5@huawei.com> Date: Wed, 13 Sep 2017 11:59:59 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <8e8d0a1b-f053-6eab-a85a-ec83201b50be@gmail.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.29.144] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A010201.59B8AD63.001F,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: c2dbe986be2941e01f660feae80c3ef9 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3066 Lines: 94 shost->async_scan should be protected by mutex_lock, otherwise the check of "called twice" won't work. Signed-off-by: Ouyang Zhaowei --- drivers/scsi/scsi_scan.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c index fd88dab..20d539b 100644 --- a/drivers/scsi/scsi_scan.c +++ b/drivers/scsi/scsi_scan.c @@ -1722,9 +1722,10 @@ static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) if (strncmp(scsi_scan_type, "sync", 4) == 0) return NULL; + mutex_lock(&shost->scan_mutex); if (shost->async_scan) { shost_printk(KERN_DEBUG, shost, "%s called twice\n", __func__); - return NULL; + goto unlock; } data = kmalloc(sizeof(*data), GFP_KERNEL); @@ -1735,7 +1736,6 @@ static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) goto err; init_completion(&data->prev_finished); - mutex_lock(&shost->scan_mutex); spin_lock_irqsave(shost->host_lock, flags); shost->async_scan = 1; spin_unlock_irqrestore(shost->host_lock, flags); @@ -1751,6 +1751,8 @@ static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) err: kfree(data); + unlock: + mutex_unlock(&shost->scan_mutex); return NULL; } On 2017.9.11 9:44, Tyrel Datwyler wrote: > On 09/07/2017 11:54 PM, Ouyangzhaowei (Charles) wrote: >> shost->async_scan should be protected by mutex_lock, otherwise the check >> of "called twice" won't work. >> >> Signed-off-by: Ouyang Zhaowei >> --- >> drivers/scsi/scsi_scan.c | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c >> index fd88dab..1d1df51 100644 >> --- a/drivers/scsi/scsi_scan.c >> +++ b/drivers/scsi/scsi_scan.c >> @@ -1722,6 +1722,7 @@ static struct async_scan_data >> *scsi_prep_async_scan(struct Scsi_Host *shost) >> if (strncmp(scsi_scan_type, "sync", 4) == 0) >> return NULL; >> >> + mutex_lock(&shost->scan_mutex); > > The mutex will not be unlocked in the event that either the host has called > scsi_prep_async_scan() twice, or a condition is meet the branches to the "err" label prior > to where the original mutex_lock() was located below. Thanks for the review, I modified the patch, please check it out again. > > -Tyrel > >> if (shost->async_scan) { >> shost_printk(KERN_DEBUG, shost, "%s called twice\n", >> __func__); >> return NULL; >> @@ -1735,7 +1736,6 @@ static struct async_scan_data >> *scsi_prep_async_scan(struct Scsi_Host *shost) >> goto err; >> init_completion(&data->prev_finished); >> >> - mutex_lock(&shost->scan_mutex); >> spin_lock_irqsave(shost->host_lock, flags); >> shost->async_scan = 1; >> spin_unlock_irqrestore(shost->host_lock, flags); >> > > > . >