mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@cam.ac.uk>
To: linux-kernel@vger.kernel.org
Cc: lm-sensors@lm-sensors.org, rtc-linux@googlegroups.com,
	osd-dev@open-osd.org, linux-scsi@vger.kernel.org,
	dri-devel@lists.freedesktop.org, jkosina@suse.cz,
	naota@elisp.net, rusty@rustcorp.com.au,
	paulmck@linux.vnet.ibm.com, namhyung@gmail.com,
	randy.dunlap@oracle.com, tj@kernel.org,
	cabarnes@indesign-llc.com, akpm@linux-foundation.org,
	airlied@redhat.com, thellstrom@vmware.com, johnpol@2ka.mipt.ru,
	JBottomley@parallels.com, bhalevy@panasas.com,
	bharrosh@panasas.com, a.zummo@towertech.it,
	guenter.roeck@ericsson.com, khali@linux-fr.org, airlied@linux.ie
Subject: [PATCH 1/8] ida: Simplified functions for id allocation.
Date: Fri, 22 Jul 2011 17:41:10 +0100	[thread overview]
Message-ID: <1311352886-4047-2-git-send-email-jic23@cam.ac.uk> (raw)
In-Reply-To: <1311352886-4047-1-git-send-email-jic23@cam.ac.uk>

From: Rusty Russell <rusty@rustcorp.com.au>

The current hyper-optimized functions are overkill if you simply want
to allocate an id for a device.  Create versions which use an internal
lock.

Thanks to Tejun for feedback.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Jonathan Cameron <jic23@cam.ac.uk>
---
 include/linux/idr.h |    4 +++
 lib/idr.c           |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/include/linux/idr.h b/include/linux/idr.h
index 13a801f..255491c 100644
--- a/include/linux/idr.h
+++ b/include/linux/idr.h
@@ -146,6 +146,10 @@ void ida_remove(struct ida *ida, int id);
 void ida_destroy(struct ida *ida);
 void ida_init(struct ida *ida);
 
+int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
+		   gfp_t gfp_mask);
+void ida_simple_remove(struct ida *ida, unsigned int id);
+
 void __init idr_init_cache(void);
 
 #endif /* __IDR_H__ */
diff --git a/lib/idr.c b/lib/idr.c
index e15502e..db040ce 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -34,8 +34,10 @@
 #include <linux/err.h>
 #include <linux/string.h>
 #include <linux/idr.h>
+#include <linux/spinlock.h>
 
 static struct kmem_cache *idr_layer_cache;
+static DEFINE_SPINLOCK(simple_ida_lock);
 
 static struct idr_layer *get_from_free_list(struct idr *idp)
 {
@@ -926,6 +928,71 @@ void ida_destroy(struct ida *ida)
 EXPORT_SYMBOL(ida_destroy);
 
 /**
+ * ida_simple_get - get a new id.
+ * @ida: the (initialized) ida.
+ * @start: the minimum id (inclusive, < 0x8000000)
+ * @end: the maximum id (exclusive, < 0x8000000 or 0)
+ * @gfp_mask: memory allocation flags
+ *
+ * Allocates an id in the range start <= id < end, or returns -ENOSPC.
+ * On memory allocation failure, returns -ENOMEM.
+ *
+ * Use ida_simple_remove() to get rid of an id.
+ */
+int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
+		   gfp_t gfp_mask)
+{
+	int ret, id;
+	unsigned int max;
+
+	BUG_ON((int)start < 0);
+	BUG_ON((int)end < 0);
+
+	if (end == 0)
+		max = 0x80000000;
+	else {
+		BUG_ON(end < start);
+		max = end - 1;
+	}
+
+again:
+	if (!ida_pre_get(ida, gfp_mask))
+		return -ENOMEM;
+
+	spin_lock(&simple_ida_lock);
+	ret = ida_get_new_above(ida, start, &id);
+	if (!ret) {
+		if (id > max) {
+			ida_remove(ida, id);
+			ret = -ENOSPC;
+		} else {
+			ret = id;
+		}
+	}
+	spin_unlock(&simple_ida_lock);
+
+	if (unlikely(ret == -EAGAIN))
+		goto again;
+
+	return ret;
+}
+EXPORT_SYMBOL(ida_simple_get);
+
+/**
+ * ida_simple_remove - remove an allocated id.
+ * @ida: the (initialized) ida.
+ * @id: the id returned by ida_simple_get.
+ */
+void ida_simple_remove(struct ida *ida, unsigned int id)
+{
+	BUG_ON((int)id < 0);
+	spin_lock(&simple_ida_lock);
+	ida_remove(ida, id);
+	spin_unlock(&simple_ida_lock);
+}
+EXPORT_SYMBOL(ida_simple_remove);
+
+/**
  * ida_init - initialize ida handle
  * @ida:	ida handle
  *
-- 
1.7.3.4


  reply	other threads:[~2011-07-22 16:41 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-22 16:41 [PATCH 0/8] RFC: Introduce ida_simple interfaces and use them Jonathan Cameron
2011-07-22 16:41 ` Jonathan Cameron [this message]
2011-07-22 16:41 ` [PATCH 2/8] hwmon: convert idr to ida and use ida_simple interface Jonathan Cameron
2011-07-22 16:41 ` [PATCH 3/8] hwmon: ibmaem: convert idr to ida and use ida_simple_get Jonathan Cameron
2011-07-26 18:04   ` Darrick J. Wong
2011-07-22 16:41 ` [PATCH 4/8] [SCSI] use ida_simple_get and ida_simple remove in place of boilerplate code Jonathan Cameron
2011-07-22 16:41 ` [PATCH 5/8] drm/vmwgfx: use ida_simple_get for id allocation Jonathan Cameron
2011-07-22 16:41 ` [PATCH 6/8] [SCSI] osduld: use ida_simple_get to handle id Jonathan Cameron
2011-07-22 21:47   ` [osd-dev] " Boaz Harrosh
2011-07-25 11:06     ` Jonathan Cameron
2011-07-22 16:41 ` [PATCH 7/8] w1: ds2760 and ds2780, use ida for id and ida_simple_get to get it Jonathan Cameron
2011-07-22 16:41 ` [PATCH 8/8] rtc: class idr converted to ida and ida_simple_get used Jonathan Cameron
2011-07-23 10:17   ` Tejun Heo
2011-07-22 16:41 ` [PATCH 0/8] RFC: Introduce ida_simple interfaces and use them Jonathan Cameron
2011-07-22 16:41 ` [PATCH 1/8] ida: Simplified functions for id allocation Jonathan Cameron
2011-07-23 10:04   ` Tejun Heo
2011-07-22 16:41 ` [PATCH 2/8] hwmon: convert idr to ida and use ida_simple interface Jonathan Cameron
2011-07-22 16:41 ` [PATCH 3/8] hwmon: ibmaem: convert idr to ida and use ida_simple_get Jonathan Cameron
2011-07-22 16:41 ` [PATCH 4/8] [SCSI] use ida_simple_get and ida_simple remove in place of boilerplate code Jonathan Cameron
2011-07-22 16:41 ` [PATCH 5/8] drm/vmwgfx: use ida_simple_get for id allocation Jonathan Cameron
2011-07-22 16:41 ` [PATCH 6/8] [SCSI] osduld: use ida_simple_get to handle id Jonathan Cameron
2011-07-22 16:41 ` [PATCH 7/8] w1: ds2760 and ds2780, use ida for id and ida_simple_get to get it Jonathan Cameron
2011-07-22 16:41 ` [PATCH 8/8] rtc: class idr converted to ida and ida_simple_get used Jonathan Cameron
2011-07-22 16:48 ` [PATCH 0/8] RFC: Introduce ida_simple interfaces and use them Jonathan Cameron

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=1311352886-4047-2-git-send-email-jic23@cam.ac.uk \
    --to=jic23@cam.ac.uk \
    --cc=JBottomley@parallels.com \
    --cc=a.zummo@towertech.it \
    --cc=airlied@linux.ie \
    --cc=airlied@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=bhalevy@panasas.com \
    --cc=bharrosh@panasas.com \
    --cc=cabarnes@indesign-llc.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=guenter.roeck@ericsson.com \
    --cc=jkosina@suse.cz \
    --cc=johnpol@2ka.mipt.ru \
    --cc=khali@linux-fr.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=lm-sensors@lm-sensors.org \
    --cc=namhyung@gmail.com \
    --cc=naota@elisp.net \
    --cc=osd-dev@open-osd.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=randy.dunlap@oracle.com \
    --cc=rtc-linux@googlegroups.com \
    --cc=rusty@rustcorp.com.au \
    --cc=thellstrom@vmware.com \
    --cc=tj@kernel.org \
    /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

all inboxes | Powered by JetHome®