public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Roland Dreier <rdreier@cisco.com>
To: akpm@osdl.org
Cc: "Arjan van de Ven" <arjan@infradead.org>,
	"Ingo Molnar" <mingo@elte.hu>,
	"Zach Brown" <zach.brown@oracle.com>,
	openib-general@openib.org, linux-kernel@vger.kernel.org
Subject: [PATCH] Convert idr's internal locking to _irqsave variant
Date: Wed, 12 Jul 2006 13:45:12 -0700	[thread overview]
Message-ID: <adaveq2v9gn.fsf@cisco.com> (raw)
In-Reply-To: 20060712093820.GA9218@elte.hu

Currently, the code in lib/idr.c uses a bare spin_lock(&idp->lock) to
do internal locking.  This is a nasty trap for code that might call
idr functions from different contexts; for example, it seems perfectly
reasonable to call idr_get_new() from process context and idr_remove()
from interrupt context -- but with the current locking this would lead
to a potential deadlock.

The simplest fix for this is to just convert the idr locking to use
spin_lock_irqsave().

In particular, this fixes a very complicated locking issue detected by
lockdep, involving the ib_ipoib driver's priv->lock and dev->_xmit_lock,
which get involved with the ib_sa module's query_idr.lock.

Cc: Arjan van de Ven <arjan@infradead.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Zach Brown <zach.brown@oracle.com>,
Signed-off-by: Roland Dreier <rolandd@cisco.com>

---

diff --git a/lib/idr.c b/lib/idr.c
index 4d09681..16d2143 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -38,14 +38,15 @@ static kmem_cache_t *idr_layer_cache;
 static struct idr_layer *alloc_layer(struct idr *idp)
 {
 	struct idr_layer *p;
+	unsigned long flags;
 
-	spin_lock(&idp->lock);
+	spin_lock_irqsave(&idp->lock, flags);
 	if ((p = idp->id_free)) {
 		idp->id_free = p->ary[0];
 		idp->id_free_cnt--;
 		p->ary[0] = NULL;
 	}
-	spin_unlock(&idp->lock);
+	spin_unlock_irqrestore(&idp->lock, flags);
 	return(p);
 }
 
@@ -59,12 +60,14 @@ static void __free_layer(struct idr *idp
 
 static void free_layer(struct idr *idp, struct idr_layer *p)
 {
+	unsigned long flags;
+
 	/*
 	 * Depends on the return element being zeroed.
 	 */
-	spin_lock(&idp->lock);
+	spin_lock_irqsave(&idp->lock, flags);
 	__free_layer(idp, p);
-	spin_unlock(&idp->lock);
+	spin_unlock_irqrestore(&idp->lock, flags);
 }
 
 /**
@@ -168,6 +171,7 @@ static int idr_get_new_above_int(struct 
 {
 	struct idr_layer *p, *new;
 	int layers, v, id;
+	unsigned long flags;
 
 	id = starting_id;
 build_up:
@@ -191,14 +195,14 @@ build_up:
 			 * The allocation failed.  If we built part of
 			 * the structure tear it down.
 			 */
-			spin_lock(&idp->lock);
+			spin_lock_irqsave(&idp->lock, flags);
 			for (new = p; p && p != idp->top; new = p) {
 				p = p->ary[0];
 				new->ary[0] = NULL;
 				new->bitmap = new->count = 0;
 				__free_layer(idp, new);
 			}
-			spin_unlock(&idp->lock);
+			spin_unlock_irqrestore(&idp->lock, flags);
 			return -1;
 		}
 		new->ary[0] = p;

  parent reply	other threads:[~2006-07-12 20:45 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-11 20:10 ipoib lockdep warning Zach Brown
2006-07-11 21:16 ` Michael S. Tsirkin
2006-07-11 21:40   ` Sean Hefty
2006-07-11 21:50     ` [openib-general] " Zach Brown
2006-07-11 22:54 ` Roland Dreier
2006-07-11 23:27   ` Zach Brown
2006-07-11 23:43     ` Roland Dreier
2006-07-11 23:53       ` Zach Brown
2006-07-12  0:06         ` Roland Dreier
2006-07-12  9:38       ` Ingo Molnar
2006-07-12 11:09         ` Michael S. Tsirkin
2006-07-12 16:31           ` [openib-general] " Sean Hefty
2006-07-12 18:56           ` Roland Dreier
2006-07-13 15:55           ` [PATCH] IB/core: use correct gfp_mask in sa_query Michael S. Tsirkin
2006-07-12 19:06         ` [openib-general] ipoib lockdep warning Roland Dreier
2006-07-12 20:45         ` Roland Dreier [this message]
2006-07-12 21:14           ` [PATCH] Convert idr's internal locking to _irqsave variant Ingo Molnar
2006-07-13  1:30           ` Andrew Morton
2006-07-13 15:42             ` Roland Dreier
2006-07-13 20:54               ` Andrew Morton
2006-07-13 21:03                 ` Roland Dreier
2006-07-13 21:05                   ` Arjan van de Ven
2006-07-14  0:18                     ` Roland Dreier
2006-07-14  6:20                       ` Arjan van de Ven
2006-07-13 21:43                   ` Andrew Morton
2006-07-14  1:08                     ` Roland Dreier
2006-07-14  1:18                       ` Andrew Morton
2006-07-14  1:30                         ` Andrew Morton
2006-07-17 15:57                           ` Roland Dreier
2006-07-12  2:33 ` [openib-general] ipoib lockdep warning Roland Dreier
2006-07-12  6:49   ` Arjan van de Ven
2006-07-12 19:01     ` Roland Dreier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=adaveq2v9gn.fsf@cisco.com \
    --to=rdreier@cisco.com \
    --cc=akpm@osdl.org \
    --cc=arjan@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=openib-general@openib.org \
    --cc=zach.brown@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox