linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ida: make ida_simple_get/put() IRQ safe
@ 2011-10-26 20:33 Tejun Heo
  2011-10-28 23:01 ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Tejun Heo @ 2011-10-26 20:33 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel, rusty

It's often convenient to be able to release resource from IRQ context.
Make ida_simple_*() use irqsave/restore spin ops so that they are IRQ
safe.

Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Rusty Russell <rusty@rustcorp.com.au>
---
Andrew, can you please route this through mm?

Thank you.

 lib/idr.c |   11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Index: work/lib/idr.c
===================================================================
--- work.orig/lib/idr.c
+++ work/lib/idr.c
@@ -944,6 +944,7 @@ int ida_simple_get(struct ida *ida, unsi
 {
 	int ret, id;
 	unsigned int max;
+	unsigned long flags;
 
 	BUG_ON((int)start < 0);
 	BUG_ON((int)end < 0);
@@ -959,7 +960,7 @@ again:
 	if (!ida_pre_get(ida, gfp_mask))
 		return -ENOMEM;
 
-	spin_lock(&simple_ida_lock);
+	spin_lock_irqsave(&simple_ida_lock, flags);
 	ret = ida_get_new_above(ida, start, &id);
 	if (!ret) {
 		if (id > max) {
@@ -969,7 +970,7 @@ again:
 			ret = id;
 		}
 	}
-	spin_unlock(&simple_ida_lock);
+	spin_unlock_irqrestore(&simple_ida_lock, flags);
 
 	if (unlikely(ret == -EAGAIN))
 		goto again;
@@ -985,10 +986,12 @@ EXPORT_SYMBOL(ida_simple_get);
  */
 void ida_simple_remove(struct ida *ida, unsigned int id)
 {
+	unsigned long flags;
+
 	BUG_ON((int)id < 0);
-	spin_lock(&simple_ida_lock);
+	spin_lock_irqsave(&simple_ida_lock, flags);
 	ida_remove(ida, id);
-	spin_unlock(&simple_ida_lock);
+	spin_unlock_irqrestore(&simple_ida_lock, flags);
 }
 EXPORT_SYMBOL(ida_simple_remove);
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ida: make ida_simple_get/put() IRQ safe
  2011-10-26 20:33 [PATCH] ida: make ida_simple_get/put() IRQ safe Tejun Heo
@ 2011-10-28 23:01 ` Andrew Morton
  2011-10-28 23:11   ` Tejun Heo
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2011-10-28 23:01 UTC (permalink / raw)
  To: Tejun Heo; +Cc: linux-kernel, rusty

On Wed, 26 Oct 2011 13:33:11 -0700
Tejun Heo <tj@kernel.org> wrote:

> It's often convenient to be able to release resource from IRQ context.
> Make ida_simple_*() use irqsave/restore spin ops so that they are IRQ
> safe.

The patch also accidentally makes ida_simple_get() callable from
interrupt context.  That's a somewhat unreliable operation due to
-ENOMEM possibilities even with GFP_ATOMIC.

> --- work.orig/lib/idr.c
> +++ work/lib/idr.c
> @@ -944,6 +944,7 @@ int ida_simple_get(struct ida *ida, unsi
>  {
>  	int ret, id;
>  	unsigned int max;
> +	unsigned long flags;
>  
>  	BUG_ON((int)start < 0);
>  	BUG_ON((int)end < 0);
> @@ -959,7 +960,7 @@ again:
>  	if (!ida_pre_get(ida, gfp_mask))
>  		return -ENOMEM;
>  
> -	spin_lock(&simple_ida_lock);
> +	spin_lock_irqsave(&simple_ida_lock, flags);
>  	ret = ida_get_new_above(ida, start, &id);
>  	if (!ret) {
>  		if (id > max) {
> @@ -969,7 +970,7 @@ again:
>  			ret = id;
>  		}
>  	}
> -	spin_unlock(&simple_ida_lock);
> +	spin_unlock_irqrestore(&simple_ida_lock, flags);
>  
>  	if (unlikely(ret == -EAGAIN))
>  		goto again;
> @@ -985,10 +986,12 @@ EXPORT_SYMBOL(ida_simple_get);
>   */
>  void ida_simple_remove(struct ida *ida, unsigned int id)
>  {
> +	unsigned long flags;
> +
>  	BUG_ON((int)id < 0);
> -	spin_lock(&simple_ida_lock);
> +	spin_lock_irqsave(&simple_ida_lock, flags);
>  	ida_remove(ida, id);
> -	spin_unlock(&simple_ida_lock);
> +	spin_unlock_irqrestore(&simple_ida_lock, flags);
>  }
>  EXPORT_SYMBOL(ida_simple_remove);

It didn't update the kerneldoc.  I wouldn't have bothered either ;)
We're generally bad about documenting permissible calling environments
and preconditions.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ida: make ida_simple_get/put() IRQ safe
  2011-10-28 23:01 ` Andrew Morton
@ 2011-10-28 23:11   ` Tejun Heo
  2011-10-28 23:16     ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Tejun Heo @ 2011-10-28 23:11 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, rusty

On Fri, Oct 28, 2011 at 04:01:15PM -0700, Andrew Morton wrote:
> On Wed, 26 Oct 2011 13:33:11 -0700
> Tejun Heo <tj@kernel.org> wrote:
> 
> > It's often convenient to be able to release resource from IRQ context.
> > Make ida_simple_*() use irqsave/restore spin ops so that they are IRQ
> > safe.
> 
> The patch also accidentally makes ida_simple_get() callable from
> interrupt context.  That's a somewhat unreliable operation due to
> -ENOMEM possibilities even with GFP_ATOMIC.

Hmmm... We can add a WARN_ON_ONCE() there but do we really care?  If
the caller is using GFP_ATOMIC, it should be expecting unreliability.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ida: make ida_simple_get/put() IRQ safe
  2011-10-28 23:11   ` Tejun Heo
@ 2011-10-28 23:16     ` Andrew Morton
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2011-10-28 23:16 UTC (permalink / raw)
  To: Tejun Heo; +Cc: linux-kernel, rusty

On Fri, 28 Oct 2011 16:11:49 -0700
Tejun Heo <tj@kernel.org> wrote:

> On Fri, Oct 28, 2011 at 04:01:15PM -0700, Andrew Morton wrote:
> > On Wed, 26 Oct 2011 13:33:11 -0700
> > Tejun Heo <tj@kernel.org> wrote:
> > 
> > > It's often convenient to be able to release resource from IRQ context.
> > > Make ida_simple_*() use irqsave/restore spin ops so that they are IRQ
> > > safe.
> > 
> > The patch also accidentally makes ida_simple_get() callable from
> > interrupt context.  That's a somewhat unreliable operation due to
> > -ENOMEM possibilities even with GFP_ATOMIC.
> 
> Hmmm... We can add a WARN_ON_ONCE() there but do we really care?  If
> the caller is using GFP_ATOMIC, it should be expecting unreliability.
> 

No, I don't think we care - I was just chin-scratching and augmenting
some deficient changeloggery.  Also, showing that I'd actually read the
thing ;)



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-10-28 23:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-26 20:33 [PATCH] ida: make ida_simple_get/put() IRQ safe Tejun Heo
2011-10-28 23:01 ` Andrew Morton
2011-10-28 23:11   ` Tejun Heo
2011-10-28 23:16     ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).