Skip to contentSkip to conversation

rapidio/rio_cm: avoid GFP_KERNEL in atomic context

PATCH Applied Alexandre Bounine 2 participants 3 messages Sep 15, 2016 – Sep 19, 2016 Rapidio

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rapidio/rio_cm: avoid GFP_KERNEL in atomic context

As reported by Alexey Khoroshilov <[email protected]> (see https://lkml.org/lkml/2016/9/9/737):
riocm_send_close() is called from rio_cm_shutdown() under spin_lock_bh(idr_lock), but riocm_send_close() uses a GFP_KERNEL allocation.

Fix by taking riocm_send_close() outside of spinlock protected code.

---
 drivers/rapidio/rio_cm.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/rapidio/rio_cm.c +17 -3
index 3226983..0e91335 100644
@@ -2242,17 +2242,31 @@ static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code,
 {
 	struct rio_channel *ch;
 	unsigned int i;
+	LIST_HEAD(list);
 
 	riocm_debug(EXIT, ".");
 
+	/*
+	 * If there are any channels left in connected state send
+	 * close notification to the connection partner.
+	 * First build a list of channels that require a closing
+	 * notification because function riocm_send_close() should
+	 * be called outside of spinlock protected code.
+	 */
 	spin_lock_bh(&idr_lock);
 	idr_for_each_entry(&ch_idr, ch, i) {
-		riocm_debug(EXIT, "close ch %d", ch->id);
-		if (ch->state == RIO_CM_CONNECTED)
-			riocm_send_close(ch);
+		if (ch->state == RIO_CM_CONNECTED) {
+			riocm_debug(EXIT, "close ch %d", ch->id);
+			idr_remove(&ch_idr, ch->id);
+			list_add(&ch->ch_node, &list);
+		}
 	}
 	spin_unlock_bh(&idr_lock);
 
+	if (!list_empty(&list))
+		list_for_each_entry(ch, &list, ch_node)
+			riocm_send_close(ch);
+
 	return NOTIFY_DONE;
 }
 
Signature · 1 line
2.9.0
#1 lore raw
Quoted 34 lines · On Thu, 15 Sep 2016 13:54:02 -0400 Alexandre Bounine…

As reported by Alexey Khoroshilov <[email protected]> (see https://lkml.org/lkml/2016/9/9/737):
riocm_send_close() is called from rio_cm_shutdown() under spin_lock_bh(idr_lock), but riocm_send_close() uses a GFP_KERNEL allocation.

Fix by taking riocm_send_close() outside of spinlock protected code.

drivers/rapidio/rio_cm.c +12 -3
@@ -2242,17 +2242,31 @@ static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code,
 {
 	struct rio_channel *ch;
 	unsigned int i;
+	LIST_HEAD(list);
 
 	riocm_debug(EXIT, ".");
 
+	/*
+	 * If there are any channels left in connected state send
+	 * close notification to the connection partner.
+	 * First build a list of channels that require a closing
+	 * notification because function riocm_send_close() should
+	 * be called outside of spinlock protected code.
+	 */
 	spin_lock_bh(&idr_lock);
 	idr_for_each_entry(&ch_idr, ch, i) {
-		riocm_debug(EXIT, "close ch %d", ch->id);
-		if (ch->state == RIO_CM_CONNECTED)
-			riocm_send_close(ch);
+		if (ch->state == RIO_CM_CONNECTED) {
+			riocm_debug(EXIT, "close ch %d", ch->id);
+			idr_remove(&ch_idr, ch->id);
+			list_add(&ch->ch_node, &list);

drivers/rapidio/rio_cm.c static int rio_cm_shutdown(struct notifier_block *nb, unsigned long code, · line 2271

Quoted 10 lines
drivers/rapidio/rio_cm.c +5 -0
+		}
 	}
 	spin_unlock_bh(&idr_lock);
 
+	if (!list_empty(&list))
+		list_for_each_entry(ch, &list, ch_node)
+			riocm_send_close(ch);
+
 	return NOTIFY_DONE;
 }

Fair enough.

Can we remove the !list_empty() test?

drivers/rapidio/rio_cm.c renamed from drivers/rapidio/rio_cm.c~rapidio-rio_cm-avoid-gfp_kernel-in-atomic-context-fix +2 -3
@@ -2268,9 +2268,8 @@ static int rio_cm_shutdown(struct notifi
 	}
 	spin_unlock_bh(&idr_lock);
 
-	if (!list_empty(&list))
-		list_for_each_entry(ch, &list, ch_node)
-			riocm_send_close(ch);
+	list_for_each_entry(ch, &list, ch_node)
+		riocm_send_close(ch);
 
 	return NOTIFY_DONE;
 }

_

#2 lore raw
Quoted 11 lines · Andrew Morton

drivers/rapidio/rio_cm.c static int rio_cm_shutdown(struct notifi · line 2275

Quoted 7 lines
drivers/rapidio/rio_cm.c renamed from drivers/rapidio/rio_cm.c~rapidio-rio_cm-avoid-gfp_kernel-in-atomic-context-fix +4 -0
+	if (!list_empty(&list))
+		list_for_each_entry(ch, &list, ch_node)
+			riocm_send_close(ch);
+
 	return NOTIFY_DONE;
 }

Fair enough.

Can we remove the !list_empty() test?

Sure. This check is not needed.

Quoted 17 lines

--- a/drivers/rapidio/rio_cm.c~rapidio-rio_cm-avoid-gfp_kernel-in- atomic-context-fix
+++ a/drivers/rapidio/rio_cm.c

drivers/rapidio/rio_cm.c renamed from drivers/rapidio/rio_cm.c~rapidio-rio_cm-avoid-gfp_kernel-in-atomic-context-fix +2 -3
@@ -2268,9 +2268,8 @@ static int rio_cm_shutdown(struct notifi
 	}
 	spin_unlock_bh(&idr_lock);
 
-	if (!list_empty(&list))
-		list_for_each_entry(ch, &list, ch_node)
-			riocm_send_close(ch);
+	list_for_each_entry(ch, &list, ch_node)
+		riocm_send_close(ch);
 
 	return NOTIFY_DONE;
 }

_