Message ID | 20230328084602.20729-1-jgross@suse.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2065570vqo; Tue, 28 Mar 2023 01:53:09 -0700 (PDT) X-Google-Smtp-Source: AKy350YgEiOS/kww9dpZQizdTjJ4JC1pYzS2oAJZq0C1265Xfu/GbsZ1IrrBgTpcxB93Ntl5x+FZ X-Received: by 2002:a17:907:205b:b0:931:cac0:60ec with SMTP id pg27-20020a170907205b00b00931cac060ecmr13980838ejb.48.1679993588983; Tue, 28 Mar 2023 01:53:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679993588; cv=none; d=google.com; s=arc-20160816; b=Hriwb0z3qMJrM+vwLNYsRAF4J1zzAk35pdFry52yyg/5QUv1s5kj7B6NI1MLIjaxJK e7gVxIQWXYDH7uDz02TWdGToHK3SeoY8EUS8flKuIfE34ZrlT/C4+kMHqG+3z0EwaHty vghJGD0/5h8MRb/0jZQnjSQxtiOSsHSOEz8pvqO5sPvF9plWbiklE/kKAgm1p8SJaIU6 jSHbkihTmWRoSaNPgG2wjr0iVmgoP5jamrWHrYqT2fzXeC9KBie2eFoUdrbcxgWZRaFk F0Eny3DP3XAHYLc80l3G9SHHmEc/IsLYla1Bv92NUBsI0zCGmnaItTjqgwr86e6oNWTe /eAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=XkRrVX8XzPB5UHUjHXjJ+xWWlRyOClSfTOm/ZrUGMDw=; b=YJDW+Z8YWK8ZJO6GRaeRs9qwa/3khZZoHYrklROZKrde1pZxVq3JlEjUYjtlr5NKWD JSraV7XLV2yufKN6yWAbFP77T/lTAs5KnaeVCQsgG6ZJ2fxNgE9vZVt3/ZtUH3/jiFSi cO5d4B7sQ3W5pUB9psiiohHDX39ImyqRY6b5JBNK3JempO/b2/ZUVDeuuycnVsFpnGnD aMlQ+bhxYLKl0p9ixxI2nhoNrykA3SW0nL34PVFUF1uY7/iPM6W0huQcXNSGiJfGeoqj NwGnMyChLHGBP7s8vBzvu7uVI5+/T69vKv/vBQOPy6+oajVTtoUVamAXmg54XGXm6elY z4VA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=BXUn2XOt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v22-20020a1709061dd600b008b85ed1bbc7si21305919ejh.366.2023.03.28.01.52.45; Tue, 28 Mar 2023 01:53:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=BXUn2XOt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231840AbjC1IqR (ORCPT <rfc822;kartikey406@gmail.com> + 99 others); Tue, 28 Mar 2023 04:46:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231819AbjC1IqL (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 28 Mar 2023 04:46:11 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CFC455A1; Tue, 28 Mar 2023 01:46:06 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id F2E521FDA1; Tue, 28 Mar 2023 08:46:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1679993165; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=XkRrVX8XzPB5UHUjHXjJ+xWWlRyOClSfTOm/ZrUGMDw=; b=BXUn2XOtBlkO6BfqJ9xz1Y8JkP8uIsgjzDECMGZ0Jh/UurP91nuLzaw9ZDMKbiiO4tpgKn 48XtNjyA4FGeWwJDGjFzuzqKiaGnm/Xh/6L5tLJypGtfmM2kE3IixxVyNd5tURiLH/24Hi XN7f7GP61eB39zwsjvWYRnfODaJQtPY= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id AC5A91390D; Tue, 28 Mar 2023 08:46:04 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id X72GKEypImQKLwAAMHmgww (envelope-from <jgross@suse.com>); Tue, 28 Mar 2023 08:46:04 +0000 From: Juergen Gross <jgross@suse.com> To: linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org Cc: Juergen Gross <jgross@suse.com>, Stefano Stabellini <sstabellini@kernel.org>, Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com>, xen-devel@lists.xenproject.org, Dan Carpenter <error27@gmail.com> Subject: [PATCH] xen/scsiback: don't call scsiback_free_translation_entry() under lock Date: Tue, 28 Mar 2023 10:46:02 +0200 Message-Id: <20230328084602.20729-1-jgross@suse.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761600957544662240?= X-GMAIL-MSGID: =?utf-8?q?1761600957544662240?= |
Series |
xen/scsiback: don't call scsiback_free_translation_entry() under lock
|
|
Commit Message
Juergen Gross
March 28, 2023, 8:46 a.m. UTC
scsiback_free_translation_entry() shouldn't be called under spinlock,
as it can sleep.
This requires to split removing a translation entry from the v2p list
from actually calling kref_put() for the entry.
Reported-by: Dan Carpenter <error27@gmail.com>
Link: https://lore.kernel.org/lkml/Y+JUIl64UDmdkboh@kadam/
Signed-off-by: Juergen Gross <jgross@suse.com>
---
drivers/xen/xen-scsiback.c | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)
Comments
On 28.03.23 11:46, Juergen Gross wrote: Hello Juergen > scsiback_free_translation_entry() shouldn't be called under spinlock, > as it can sleep. > > This requires to split removing a translation entry from the v2p list > from actually calling kref_put() for the entry. > > Reported-by: Dan Carpenter <error27@gmail.com> > Link: https://urldefense.com/v3/__https://lore.kernel.org/lkml/Y*JUIl64UDmdkboh@kadam/__;Kw!!GF_29dbcQIUBPA!23IKdVhamoFq8ptUnprd_TubDMObj-0QAalsGiffBHCeEdOuwrq7z4ohg92Sj0olgl0nh73oXvSr-i1zqXhY$ [lore[.]kernel[.]org] > Signed-off-by: Juergen Gross <jgross@suse.com> > --- > drivers/xen/xen-scsiback.c | 27 ++++++++++++++------------- > 1 file changed, 14 insertions(+), 13 deletions(-) > > diff --git a/drivers/xen/xen-scsiback.c b/drivers/xen/xen-scsiback.c > index 954188b0b858..294f29cdc7aa 100644 > --- a/drivers/xen/xen-scsiback.c > +++ b/drivers/xen/xen-scsiback.c > @@ -1010,12 +1010,6 @@ static int scsiback_add_translation_entry(struct vscsibk_info *info, > return err; > } > > -static void __scsiback_del_translation_entry(struct v2p_entry *entry) > -{ > - list_del(&entry->l); > - kref_put(&entry->kref, scsiback_free_translation_entry); > -} > - > /* > Delete the translation entry specified > */ > @@ -1024,18 +1018,20 @@ static int scsiback_del_translation_entry(struct vscsibk_info *info, > { > struct v2p_entry *entry; > unsigned long flags; > - int ret = 0; > > spin_lock_irqsave(&info->v2p_lock, flags); > /* Find out the translation entry specified */ > entry = scsiback_chk_translation_entry(info, v); > if (entry) > - __scsiback_del_translation_entry(entry); > - else > - ret = -ENOENT; > + list_del(&entry->l); > > spin_unlock_irqrestore(&info->v2p_lock, flags); > - return ret; > + > + if (!entry) > + return -ENOENT; > + > + kref_put(&entry->kref, scsiback_free_translation_entry); > + return 0; > } > > static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state, > @@ -1239,14 +1235,19 @@ static void scsiback_release_translation_entry(struct vscsibk_info *info) > { > struct v2p_entry *entry, *tmp; > struct list_head *head = &(info->v2p_entry_lists); > + struct list_head tmp_list; I would use LIST_HEAD(tmp_list); > unsigned long flags; > > spin_lock_irqsave(&info->v2p_lock, flags); > > - list_for_each_entry_safe(entry, tmp, head, l) > - __scsiback_del_translation_entry(entry); > + list_cut_before(&tmp_list, head, head); so we just move all entries from head to tmp_list here to be processed... > > spin_unlock_irqrestore(&info->v2p_lock, flags); ... when the lock is not held, ok Patch LGTM, but one (maybe stupid) question to clarify. Why do we need to use a lock here in the first place? The scsiback_release_translation_entry() gets called when the driver instance is about to be removed and *after* the disconnection from otherend (so no requests are expected), so what else might cause this list to be accessed concurrently? > + > + list_for_each_entry_safe(entry, tmp, &tmp_list, l) { > + list_del(&entry->l); > + kref_put(&entry->kref, scsiback_free_translation_entry); > + } > } > > static void scsiback_remove(struct xenbus_device *dev)
On 28.03.23 17:47, Oleksandr Tyshchenko wrote: > > > On 28.03.23 11:46, Juergen Gross wrote: > > Hello Juergen > >> scsiback_free_translation_entry() shouldn't be called under spinlock, >> as it can sleep. >> >> This requires to split removing a translation entry from the v2p list >> from actually calling kref_put() for the entry. >> >> Reported-by: Dan Carpenter <error27@gmail.com> >> Link: https://urldefense.com/v3/__https://lore.kernel.org/lkml/Y*JUIl64UDmdkboh@kadam/__;Kw!!GF_29dbcQIUBPA!23IKdVhamoFq8ptUnprd_TubDMObj-0QAalsGiffBHCeEdOuwrq7z4ohg92Sj0olgl0nh73oXvSr-i1zqXhY$ [lore[.]kernel[.]org] >> Signed-off-by: Juergen Gross <jgross@suse.com> >> --- >> drivers/xen/xen-scsiback.c | 27 ++++++++++++++------------- >> 1 file changed, 14 insertions(+), 13 deletions(-) >> >> diff --git a/drivers/xen/xen-scsiback.c b/drivers/xen/xen-scsiback.c >> index 954188b0b858..294f29cdc7aa 100644 >> --- a/drivers/xen/xen-scsiback.c >> +++ b/drivers/xen/xen-scsiback.c >> @@ -1010,12 +1010,6 @@ static int scsiback_add_translation_entry(struct vscsibk_info *info, >> return err; >> } >> >> -static void __scsiback_del_translation_entry(struct v2p_entry *entry) >> -{ >> - list_del(&entry->l); >> - kref_put(&entry->kref, scsiback_free_translation_entry); >> -} >> - >> /* >> Delete the translation entry specified >> */ >> @@ -1024,18 +1018,20 @@ static int scsiback_del_translation_entry(struct vscsibk_info *info, >> { >> struct v2p_entry *entry; >> unsigned long flags; >> - int ret = 0; >> >> spin_lock_irqsave(&info->v2p_lock, flags); >> /* Find out the translation entry specified */ >> entry = scsiback_chk_translation_entry(info, v); >> if (entry) >> - __scsiback_del_translation_entry(entry); >> - else >> - ret = -ENOENT; >> + list_del(&entry->l); >> >> spin_unlock_irqrestore(&info->v2p_lock, flags); >> - return ret; >> + >> + if (!entry) >> + return -ENOENT; >> + >> + kref_put(&entry->kref, scsiback_free_translation_entry); >> + return 0; >> } >> >> static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state, >> @@ -1239,14 +1235,19 @@ static void scsiback_release_translation_entry(struct vscsibk_info *info) >> { >> struct v2p_entry *entry, *tmp; >> struct list_head *head = &(info->v2p_entry_lists); >> + struct list_head tmp_list; > > > I would use LIST_HEAD(tmp_list); There is no need to initialize it, so I think I will keep it as is. > >> unsigned long flags; >> >> spin_lock_irqsave(&info->v2p_lock, flags); >> >> - list_for_each_entry_safe(entry, tmp, head, l) >> - __scsiback_del_translation_entry(entry); >> + list_cut_before(&tmp_list, head, head); > > so we just move all entries from head to tmp_list here to be processed... Correct. > >> >> spin_unlock_irqrestore(&info->v2p_lock, flags); > > ... when the lock is not held, ok > > Patch LGTM, but one (maybe stupid) question to clarify. > > Why do we need to use a lock here in the first place? The > scsiback_release_translation_entry() gets called when the driver > instance is about to be removed and *after* the disconnection from > otherend (so no requests are expected), so what else might cause this > list to be accessed concurrently? Maybe nothing, but I think it is good practice to keep the lock in order to avoid future code changes to cause problems. Juergen
On 29.03.23 09:20, Juergen Gross wrote: Hello Juergen > On 28.03.23 17:47, Oleksandr Tyshchenko wrote: >> >> >> On 28.03.23 11:46, Juergen Gross wrote: >> >> Hello Juergen >> >>> scsiback_free_translation_entry() shouldn't be called under spinlock, >>> as it can sleep. >>> >>> This requires to split removing a translation entry from the v2p list >>> from actually calling kref_put() for the entry. >>> >>> Reported-by: Dan Carpenter <error27@gmail.com> >>> Link: >>> https://urldefense.com/v3/__https://lore.kernel.org/lkml/Y*JUIl64UDmdkboh@kadam/__;Kw!!GF_29dbcQIUBPA!23IKdVhamoFq8ptUnprd_TubDMObj-0QAalsGiffBHCeEdOuwrq7z4ohg92Sj0olgl0nh73oXvSr-i1zqXhY$ [lore[.]kernel[.]org] >>> Signed-off-by: Juergen Gross <jgross@suse.com> >>> --- >>> drivers/xen/xen-scsiback.c | 27 ++++++++++++++------------- >>> 1 file changed, 14 insertions(+), 13 deletions(-) >>> >>> diff --git a/drivers/xen/xen-scsiback.c b/drivers/xen/xen-scsiback.c >>> index 954188b0b858..294f29cdc7aa 100644 >>> --- a/drivers/xen/xen-scsiback.c >>> +++ b/drivers/xen/xen-scsiback.c >>> @@ -1010,12 +1010,6 @@ static int >>> scsiback_add_translation_entry(struct vscsibk_info *info, >>> return err; >>> } >>> -static void __scsiback_del_translation_entry(struct v2p_entry *entry) >>> -{ >>> - list_del(&entry->l); >>> - kref_put(&entry->kref, scsiback_free_translation_entry); >>> -} >>> - >>> /* >>> Delete the translation entry specified >>> */ >>> @@ -1024,18 +1018,20 @@ static int >>> scsiback_del_translation_entry(struct vscsibk_info *info, >>> { >>> struct v2p_entry *entry; >>> unsigned long flags; >>> - int ret = 0; >>> spin_lock_irqsave(&info->v2p_lock, flags); >>> /* Find out the translation entry specified */ >>> entry = scsiback_chk_translation_entry(info, v); >>> if (entry) >>> - __scsiback_del_translation_entry(entry); >>> - else >>> - ret = -ENOENT; >>> + list_del(&entry->l); >>> spin_unlock_irqrestore(&info->v2p_lock, flags); >>> - return ret; >>> + >>> + if (!entry) >>> + return -ENOENT; >>> + >>> + kref_put(&entry->kref, scsiback_free_translation_entry); >>> + return 0; >>> } >>> static void scsiback_do_add_lun(struct vscsibk_info *info, const >>> char *state, >>> @@ -1239,14 +1235,19 @@ static void >>> scsiback_release_translation_entry(struct vscsibk_info *info) >>> { >>> struct v2p_entry *entry, *tmp; >>> struct list_head *head = &(info->v2p_entry_lists); >>> + struct list_head tmp_list; >> >> >> I would use LIST_HEAD(tmp_list); > > There is no need to initialize it, so I think I will keep it as is. > >> >>> unsigned long flags; >>> spin_lock_irqsave(&info->v2p_lock, flags); >>> - list_for_each_entry_safe(entry, tmp, head, l) >>> - __scsiback_del_translation_entry(entry); >>> + list_cut_before(&tmp_list, head, head); >> >> so we just move all entries from head to tmp_list here to be processed... > > Correct. > >> >>> spin_unlock_irqrestore(&info->v2p_lock, flags); >> >> ... when the lock is not held, ok >> >> Patch LGTM, but one (maybe stupid) question to clarify. >> >> Why do we need to use a lock here in the first place? The >> scsiback_release_translation_entry() gets called when the driver >> instance is about to be removed and *after* the disconnection from >> otherend (so no requests are expected), so what else might cause this >> list to be accessed concurrently? > > Maybe nothing, but I think it is good practice to keep the lock in order > to avoid future code changes to cause problems. Thanks for the explanation, it sounds reasonable to me. Reviewed-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com> > > > Juergen >
diff --git a/drivers/xen/xen-scsiback.c b/drivers/xen/xen-scsiback.c index 954188b0b858..294f29cdc7aa 100644 --- a/drivers/xen/xen-scsiback.c +++ b/drivers/xen/xen-scsiback.c @@ -1010,12 +1010,6 @@ static int scsiback_add_translation_entry(struct vscsibk_info *info, return err; } -static void __scsiback_del_translation_entry(struct v2p_entry *entry) -{ - list_del(&entry->l); - kref_put(&entry->kref, scsiback_free_translation_entry); -} - /* Delete the translation entry specified */ @@ -1024,18 +1018,20 @@ static int scsiback_del_translation_entry(struct vscsibk_info *info, { struct v2p_entry *entry; unsigned long flags; - int ret = 0; spin_lock_irqsave(&info->v2p_lock, flags); /* Find out the translation entry specified */ entry = scsiback_chk_translation_entry(info, v); if (entry) - __scsiback_del_translation_entry(entry); - else - ret = -ENOENT; + list_del(&entry->l); spin_unlock_irqrestore(&info->v2p_lock, flags); - return ret; + + if (!entry) + return -ENOENT; + + kref_put(&entry->kref, scsiback_free_translation_entry); + return 0; } static void scsiback_do_add_lun(struct vscsibk_info *info, const char *state, @@ -1239,14 +1235,19 @@ static void scsiback_release_translation_entry(struct vscsibk_info *info) { struct v2p_entry *entry, *tmp; struct list_head *head = &(info->v2p_entry_lists); + struct list_head tmp_list; unsigned long flags; spin_lock_irqsave(&info->v2p_lock, flags); - list_for_each_entry_safe(entry, tmp, head, l) - __scsiback_del_translation_entry(entry); + list_cut_before(&tmp_list, head, head); spin_unlock_irqrestore(&info->v2p_lock, flags); + + list_for_each_entry_safe(entry, tmp, &tmp_list, l) { + list_del(&entry->l); + kref_put(&entry->kref, scsiback_free_translation_entry); + } } static void scsiback_remove(struct xenbus_device *dev)