xen/scsiback: don't call scsiback_free_translation_entry() under lock
Subsystems (3)
Juergen Gross
maintainerPATCH 1/1 xen/scsiback: don't call scsiback_free_translation_entry() under lock
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.
--- drivers/xen/xen-scsiback.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
drivers/xen/xen-scsiback.c +14 -13
@@ -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)
Signature · 1 line
2.35.3
Oleksandr Tyshchenko
reviewerOn 28.03.23 11:46, Juergen Gross wrote:
Hello Juergen
Quoted 57 lines
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 <[email protected]>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 <[email protected]>--- drivers/xen/xen-scsiback.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/xen/xen-scsiback.c +7
-11@@ -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,
drivers/xen/xen-scsiback.c static void scsiback_release_translation_entry(struct vscsibk_info *info) · line 1238
Quoted 4 lines
drivers/xen/xen-scsiback.c +1
-0@@ -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);
drivers/xen/xen-scsiback.c static void scsiback_release_translation_entry(struct vscsibk_info *info) · line 1243
Quoted 7 lines
drivers/xen/xen-scsiback.c +1
-2unsigned 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...
drivers/xen/xen-scsiback.c static void scsiback_release_translation_entry(struct vscsibk_info *info) · line 1244
Quoted 2 lines
drivers/xen/xen-scsiback.c +0
-0spin_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?
Quoted 8 lines
drivers/xen/xen-scsiback.c +5
-0+ +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)
Juergen Gross
maintainerQuoted 61 lines · Oleksandr Tyshchenko
On 28.03.23 11:46, Juergen Gross wrote:
Hello Juergen
Quoted 55 lines
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 <[email protected]>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 <[email protected]>--- drivers/xen/xen-scsiback.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/xen/xen-scsiback.c +7
-11@@ -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 ; }
Quoted 9 lines
drivers/xen/xen-scsiback.c static void scsiback_release_translation_entry(struct vscsibk_info *info) · line 1238
Quoted 6 lines
drivers/xen/xen-scsiback.c +1
-0static 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.
Quoted 10 lines
drivers/xen/xen-scsiback.c static void scsiback_release_translation_entry(struct vscsibk_info *info) · line 1243
Quoted 8 lines
drivers/xen/xen-scsiback.c +1
-2unsigned 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_listhere to be processed...
Correct.
Quoted 6 lines
drivers/xen/xen-scsiback.c static void scsiback_release_translation_entry(struct vscsibk_info *info) · line 1244
Quoted 3 lines
drivers/xen/xen-scsiback.c +0
-0spin_unlock_irqrestore (&info->v2p_lock, flags);... when the lock is not held, ok
Quoted 7 lines
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
Quoted 93 lines · Oleksandr Tyshchenko
Quoted 63 lines · Juergen Gross
Hello Juergen
Quoted 57 lines
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 <[email protected]>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 <[email protected]> --- drivers/xen/xen-scsiback.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)drivers/xen/xen-scsiback.c +0
-6@@ -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
*/drivers/xen/xen-scsiback.c +7
-5@@ -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,drivers/xen/xen-scsiback.c +0
-0@@ -1239,14 +1235,19 @@ static voidQuoted 8 lines
drivers/xen/xen-scsiback.c static void scsiback_release_translation_entry(struct vscsibk_info *info) · line 1238
Quoted 5 lines
drivers/xen/xen-scsiback.c +1
-0scsiback_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.
Quoted 8 lines
drivers/xen/xen-scsiback.c static void scsiback_release_translation_entry(struct vscsibk_info *info) · line 1243
Quoted 6 lines
drivers/xen/xen-scsiback.c +1
-2unsigned 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_listhere to be processed...Correct.
Quoted 7 lines
drivers/xen/xen-scsiback.c static void · line 1243
Quoted 2 lines
drivers/xen/xen-scsiback.c +0
-0spin_unlock_irqrestore(&info->v2p_lock, flags);... when the lock is not held, ok
Patch LGTM, but one (maybe stupid) question to clarify.
Quoted 8 lines
Quoted 5 lines
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.
Quoted 4 lines
Juergen