Skip to contentSkip to conversation

idr: add idr_alloc_cyclic and convert existing cyclic users

PATCH In review Jeff Layton 5 participants 15 messages Mar 27, 2013 – Apr 3, 2013

Subsystems (11)
lore
Share & tools
lore
#0 lore raw

v2 cover idr: add idr_alloc_cyclic and convert existing cyclic users

(Andrew, I think this is probably best routed via -mm since it touches several different places)

This is the second version of this patchset. The main changes are:

- removed an incorrect comment on the new allocator function that stemmed from a misunderstanding of the IDR code. Also fixed up the kerneldoc comment.

- the idr_init_cyclic function has been removed, since it doesn't add any benefit over a "normal" idr_init.

- minor change to the caller in inotify code to make it not allocate an ID of 0. The existing code starts at 1, so this seems like a reasonable precaution.

Original cover letter follows:

As Tejun points out, there are several users of the IDR facility that attempt to use it in a cyclic fashion. These users are likely to see -ENOSPC errors after the counter wraps one or more times however.

This patchset adds a new idr_alloc_cyclic routine and converts several of these users to it. Many of these users are in obscure parts of the kernel, and I don't have a good way to test some of them. The change is pretty straightforward though, so hopefully it won't be an issue.

There is one other cyclic user of idr_alloc that I didn't touch in ipc/util.c. That one is doing some strange stuff that I didn't quite understand, but it looks like it should probably be converted later somehow.

Jeff Layton (6):
idr: introduce idr_alloc_cyclic
amso1100: convert to using idr_alloc_cyclic
mlx4: convert to using idr_alloc_cyclic
nfsd: convert nfs4_alloc_stid to use idr_alloc_cyclic
inotify: convert inotify_add_to_idr to use idr_alloc_cyclic
sctp: convert sctp_assoc_set_id to use idr_alloc_cyclic

 drivers/infiniband/hw/amso1100/c2.h    |  1 -
 drivers/infiniband/hw/amso1100/c2_qp.c |  3 +--
 drivers/infiniband/hw/mlx4/cm.c        |  4 +---
 fs/nfsd/nfs4state.c                    |  7 +------
 fs/notify/inotify/inotify_user.c       |  8 ++------
 include/linux/fsnotify_backend.h       |  1 -
 include/linux/idr.h                    |  2 ++
 lib/idr.c                              | 27 +++++++++++++++++++++++++++
 net/sctp/associola.c                   | 16 ++--------------
 9 files changed, 36 insertions(+), 33 deletions(-)
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH v2 1/6 idr: introduce idr_alloc_cyclic

Thus spake Tejun Heo:

Ooh, BTW, the cyclic allocation is broken. It's prone to -ENOSPC after the first wraparound. There are several cyclic users in the kernel and I think it probably would be best to implement cyclic support in idr.

This patch does that by adding new idr_alloc_cyclic function that such users in the kernel can use. With this, there's no need for a caller to keep track of the last value used as that's now tracked internally. This should prevent the ENOSPC problems that can hit when the "last allocated" counter exceeds INT_MAX.

Later patches will convert existing cyclic users to the new interface.

---
 include/linux/idr.h |  2 ++
 lib/idr.c           | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
include/linux/idr.h +2 -0
index 2640c7e..a470ac3 100644
@@ -42,6 +42,7 @@ struct idr {
 	struct idr_layer	*id_free;
 	int			layers;	/* only valid w/o concurrent changes */
 	int			id_free_cnt;
+	int			cur;	/* current pos for cyclic allocation */
 	spinlock_t		lock;
 };
 
@@ -75,6 +76,7 @@ struct idr {
 void *idr_find_slowpath(struct idr *idp, int id);
 void idr_preload(gfp_t gfp_mask);
 int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask);
+int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask);
 int idr_for_each(struct idr *idp,
 		 int (*fn)(int id, void *p, void *data), void *data);
 void *idr_get_next(struct idr *idp, int *nextid);
lib/idr.c +27 -0
index 322e281..cca4b93 100644
@@ -495,6 +495,33 @@ int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
 }
 EXPORT_SYMBOL_GPL(idr_alloc);
 
+/**
+ * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
+ * @idr: the (initialized) idr
+ * @ptr: pointer to be associated with the new id
+ * @start: the minimum id (inclusive)
+ * @end: the maximum id (exclusive, <= 0 for max)
+ * @gfp_mask: memory allocation flags
+ *
+ * Essentially the same as idr_alloc, but prefers to allocate progressively
+ * higher ids if it can. If the "cur" counter wraps, then it will start again
+ * at the "start" end of the range and allocate one that has already been used.
+ */
+int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end,
+			gfp_t gfp_mask)
+{
+	int id;
+
+	id = idr_alloc(idr, ptr, max(start, idr->cur), end, gfp_mask);
+	if (id == -ENOSPC)
+		id = idr_alloc(idr, ptr, start, end, gfp_mask);
+
+	if (likely(id >= 0))
+		idr->cur = id + 1;
+	return id;
+}
+EXPORT_SYMBOL(idr_alloc_cyclic);
+
 static void idr_remove_warning(int id)
 {
 	printk(KERN_WARNING
Signature · 1 line
1.7.11.7
#2 lore raw

PATCH v2 2/6 amso1100: convert to using idr_alloc_cyclic

In reply to Jeff Layton

(Note: compile-tested only)

---
 drivers/infiniband/hw/amso1100/c2.h    | 1 -
 drivers/infiniband/hw/amso1100/c2_qp.c | 3 +--
 2 files changed, 1 insertion(+), 3 deletions(-)
drivers/infiniband/hw/amso1100/c2.h +0 -1
index ba7a1208..d619d73 100644
@@ -265,7 +265,6 @@ struct c2_pd_table {
 struct c2_qp_table {
 	struct idr idr;
 	spinlock_t lock;
-	int last;
 };
 
 struct c2_element {
drivers/infiniband/hw/amso1100/c2_qp.c +1 -2
index 0ab826b..86708de 100644
@@ -385,8 +385,7 @@ static int c2_alloc_qpn(struct c2_dev *c2dev, struct c2_qp *qp)
 	idr_preload(GFP_KERNEL);
 	spin_lock_irq(&c2dev->qp_table.lock);
 
-	ret = idr_alloc(&c2dev->qp_table.idr, qp, c2dev->qp_table.last++, 0,
-			GFP_NOWAIT);
+	ret = idr_alloc_cyclic(&c2dev->qp_table.idr, qp, 0, 0, GFP_NOWAIT);
 	if (ret >= 0)
 		qp->qpn = ret;
 
Signature · 6 lines
1.7.11.7

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reviewed-by on v2 2/6
#3 lore raw

Re: [PATCH v2 2/6] amso1100: convert to using idr_alloc_cyclic

Quoted 7 lines · Jeff Layton

(Note: compile-tested only)

Thanks.

Signature · 5 lines
tejun
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#4 lore raw

PATCH v2 3/6 mlx4: convert to using idr_alloc_cyclic

In reply to Jeff Layton

(Note: compile tested only)

Cc Tejun Heo <[email protected]>
Cc Jack Morgenstein <[email protected]>
Cc Or Gerlitz <[email protected]>
Cc Roland Dreier <[email protected]>
---
 drivers/infiniband/hw/mlx4/cm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/infiniband/hw/mlx4/cm.c +1 -3
index add98d0..d1f5f1d 100644
@@ -204,7 +204,6 @@ static struct id_map_entry *
 id_map_alloc(struct ib_device *ibdev, int slave_id, u32 sl_cm_id)
 {
 	int ret;
-	static int next_id;
 	struct id_map_entry *ent;
 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
 
@@ -223,9 +222,8 @@ id_map_alloc(struct ib_device *ibdev, int slave_id, u32 sl_cm_id)
 	idr_preload(GFP_KERNEL);
 	spin_lock(&to_mdev(ibdev)->sriov.id_map_lock);
 
-	ret = idr_alloc(&sriov->pv_id_table, ent, next_id, 0, GFP_NOWAIT);
+	ret = idr_alloc_cyclic(&sriov->pv_id_table, ent, 0, 0, GFP_NOWAIT);
 	if (ret >= 0) {
-		next_id = max(ret + 1, 0);
 		ent->pv_cm_id = (u32)ret;
 		sl_id_map_add(ibdev, ent);
 		list_add_tail(&ent->list, &sriov->cm_list);
Signature · 1 line
1.7.11.7
#5 lore raw

PATCH v2 4/6 nfsd: convert nfs4_alloc_stid to use idr_alloc_cyclic

In reply to Jeff Layton
---
 fs/nfsd/nfs4state.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
fs/nfsd/nfs4state.c +1 -6
index 2e27430..417c848 100644
@@ -234,7 +234,6 @@ static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
 kmem_cache *slab)
 {
 	struct idr *stateids = &cl->cl_stateids;
-	static int min_stateid = 0;
 	struct nfs4_stid *stid;
 	int new_id;
 
@@ -242,7 +241,7 @@ kmem_cache *slab)
 	if (!stid)
 		return NULL;
 
-	new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
+	new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
 	if (new_id < 0)
 		goto out_free;
 	stid->sc_client = cl;
@@ -261,10 +260,6 @@ kmem_cache *slab)
 	 * amount of time until an id is reused, by ensuring they always
 	 * "increase" (mod INT_MAX):
 	 */
-
-	min_stateid = new_id+1;
-	if (min_stateid == INT_MAX)
-		min_stateid = 0;
 	return stid;
 out_free:
 	kfree(stid);
Signature · 1 line
1.7.11.7
#6 lore raw

Re: [PATCH v2 4/6] nfsd: convert nfs4_alloc_stid to use idr_alloc_cyclic

Err, and ack v2.--b.

Quoted 42 lines · Jeff Layton
---
 fs/nfsd/nfs4state.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
fs/nfsd/nfs4state.c +1 -6
index 2e27430..417c848 100644
@@ -234,7 +234,6 @@ static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
 kmem_cache *slab)
 {
 	struct idr *stateids = &cl->cl_stateids;
-	static int min_stateid = 0;
 	struct nfs4_stid *stid;
 	int new_id;
 
@@ -242,7 +241,7 @@ kmem_cache *slab)
 	if (!stid)
 		return NULL;
 
-	new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
+	new_id = idr_alloc_cyclic(stateids, stid, 0, 0, GFP_KERNEL);
 	if (new_id < 0)
 		goto out_free;
 	stid->sc_client = cl;
@@ -261,10 +260,6 @@ kmem_cache *slab)
 	 * amount of time until an id is reused, by ensuring they always
 	 * "increase" (mod INT_MAX):
 	 */
-
-	min_stateid = new_id+1;
-	if (min_stateid == INT_MAX)
-		min_stateid = 0;
 	return stid;
 out_free:
 	kfree(stid);
Signature · 1 line
1.7.11.7
#7 lore raw

PATCH v2 5/6 inotify: convert inotify_add_to_idr to use idr_alloc_cyclic

In reply to Jeff Layton
Cc John McCutchan <[email protected]>
Cc Robert Love <[email protected]>
Cc Eric Paris <[email protected]>
---
 fs/notify/inotify/inotify_user.c | 8 ++------
 include/linux/fsnotify_backend.h | 1 -
 2 files changed, 2 insertions(+), 7 deletions(-)
fs/notify/inotify/inotify_user.c +2 -6
index e0f7c12..8562bd3 100644
@@ -359,7 +359,6 @@ static int inotify_find_inode(const char __user *dirname, struct path *path, uns
 }
 
 static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
-			      int *last_wd,
 			      struct inotify_inode_mark *i_mark)
 {
 	int ret;
@@ -367,11 +366,10 @@ static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
 	idr_preload(GFP_KERNEL);
 	spin_lock(idr_lock);
 
-	ret = idr_alloc(idr, i_mark, *last_wd + 1, 0, GFP_NOWAIT);
+	ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
 	if (ret >= 0) {
 		/* we added the mark to the idr, take a reference */
 		i_mark->wd = ret;
-		*last_wd = i_mark->wd;
 		fsnotify_get_mark(&i_mark->fsn_mark);
 	}
 
@@ -638,8 +636,7 @@ static int inotify_new_watch(struct fsnotify_group *group,
 	if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
 		goto out_err;
 
-	ret = inotify_add_to_idr(idr, idr_lock, &group->inotify_data.last_wd,
-				 tmp_i_mark);
+	ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
 	if (ret)
 		goto out_err;
 
@@ -697,7 +694,6 @@ static struct fsnotify_group *inotify_new_group(unsigned int max_events)
 
 	spin_lock_init(&group->inotify_data.idr_lock);
 	idr_init(&group->inotify_data.idr);
-	group->inotify_data.last_wd = 0;
 	group->inotify_data.user = get_current_user();
 
 	if (atomic_inc_return(&group->inotify_data.user->inotify_devs) >
include/linux/fsnotify_backend.h +0 -1
index d5b0910..4b2ee8d 100644
@@ -157,7 +157,6 @@ struct fsnotify_group {
 		struct inotify_group_private_data {
 			spinlock_t	idr_lock;
 			struct idr      idr;
-			u32             last_wd;
 			struct user_struct      *user;
 		} inotify_data;
 #endif
Signature · 1 line
1.7.11.7
#8 lore raw

PATCH v2 6/6 sctp: convert sctp_assoc_set_id to use idr_alloc_cyclic

In reply to Jeff Layton

(Note: compile-tested only)

Cc Vlad Yasevich <[email protected]>
Cc Sridhar Samudrala <[email protected]>
Cc Neil Horman <[email protected]>
Cc "David S. Miller" <[email protected]>
---
 net/sctp/associola.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
net/sctp/associola.c +2 -14
index d2709e2..fa261a3 100644
@@ -66,13 +66,6 @@ static void sctp_assoc_bh_rcv(struct work_struct *work);
 static void sctp_assoc_free_asconf_acks(struct sctp_association *asoc);
 static void sctp_assoc_free_asconf_queue(struct sctp_association *asoc);
 
-/* Keep track of the new idr low so that we don't re-use association id
- * numbers too fast.  It is protected by they idr spin lock is in the
- * range of 1 - INT_MAX.
- */
-static u32 idr_low = 1;
-
-
 /* 1st Level Abstractions. */
 
 /* Initialize a new association from provided memory. */
@@ -1601,13 +1594,8 @@ int sctp_assoc_set_id(struct sctp_association *asoc, gfp_t gfp)
 	if (preload)
 		idr_preload(gfp);
 	spin_lock_bh(&sctp_assocs_id_lock);
-	/* 0 is not a valid id, idr_low is always >= 1 */
-	ret = idr_alloc(&sctp_assocs_id, asoc, idr_low, 0, GFP_NOWAIT);
-	if (ret >= 0) {
-		idr_low = ret + 1;
-		if (idr_low = INT_MAX)
-			idr_low = 1;
-	}
+	/* 0 is not a valid assoc_id, must be >= 1 */
+	ret = idr_alloc_cyclic(&sctp_assocs_id, asoc, 1, 0, GFP_NOWAIT);
 	spin_unlock_bh(&sctp_assocs_id_lock);
 	if (preload)
 		idr_preload_end();
Signature · 1 line
1.7.11.7
#9 lore raw

Re: [PATCH v2 6/6] sctp: convert sctp_assoc_set_id to use idr_alloc_cyclic

Quoted 9 lines · Jeff Layton

(Note: compile-tested only)

Cc Vlad Yasevich <[email protected]>
Cc Sridhar Samudrala <[email protected]>
Cc Neil Horman <[email protected]>
Cc "David S. Miller" <[email protected]>
Quoted 4 lines
---
 net/sctp/associola.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)

I don't see anything wrong with this patch per-se, but the idr_alloc_cyclic call isn't integrated with net/net-next or Linus' tree yet. If we don't gate this patch on that integration, we'll break the build.
Neil

Acked-by
#10 lore raw

Re: [PATCH v2 6/6] sctp: convert sctp_assoc_set_id to use idr_alloc_cyclic

Quoted 14 lines · Neil Horman
Quoted 9 lines · Jeff Layton

(Note: compile-tested only)

Cc Vlad Yasevich <[email protected]>
Cc Sridhar Samudrala <[email protected]>
Cc Neil Horman <[email protected]>
Cc "David S. Miller" <[email protected]>
Quoted 4 lines
---
 net/sctp/associola.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
Quoted 5 lines

I don't see anything wrong with this patch per-se, but the idr_alloc_cyclic call isn't integrated with net/net-next or Linus' tree yet. If we don't gate this patch on that integration, we'll break the build.
Neil

Actually, I just noticed that you only sent us 6/6 here, I'm assuming a prior patch in the series adds the idr_alloc_cyclic code? if so, I've seen the prior version

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-sctp" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#11 lore raw

Re: [PATCH v2 6/6] sctp: convert sctp_assoc_set_id to use idr_alloc_cyclic

Quoted 15 lines · On Thu, 28 Mar 2013 10:04:06 -0400 Neil Horman <[email protected]> wrote:
Quoted 14 lines · Neil Horman
Quoted 13 lines · Jeff Layton

(Note: compile-tested only)

Cc Vlad Yasevich <[email protected]>
Cc Sridhar Samudrala <[email protected]>
Cc Neil Horman <[email protected]>
Cc "David S. Miller" <[email protected]>
---
 net/sctp/associola.c | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
Quoted 8 lines
Quoted 5 lines

I don't see anything wrong with this patch per-se, but the idr_alloc_cyclic call isn't integrated with net/net-next or Linus' tree yet. If we don't gate this patch on that integration, we'll break the build.
Neil

Actually, I just noticed that you only sent us 6/6 here, I'm assuming a prior patch in the series adds the idr_alloc_cyclic code? if so, I've seen the prior version

Yes, idr_alloc_cyclic is added in patch #1. v2 has some slight changes from v1 but it's essentially the same concept.

Quoted 3 lines

Thanks!

Signature · 1 line
Jeff Layton <[email protected]>
#12 lore raw
In reply to Jeff Layton
Quoted 2 lines · Jeff Layton

(Andrew, I think this is probably best routed via -mm since it touches several different places)

Yeap, I think it'd be best to route these through -mm.

Please feel free to add Reviewed-by: Tejun Heo <[email protected]> for the whole series.

Thanks!

Signature · 1 line
tejun
maintainer
#13 lore raw
Quoted 8 lines · On Wed, 27 Mar 2013 12:38:47 -0700 Tejun Heo <[email protected]> wrote:
Quoted 2 lines · Jeff Layton

(Andrew, I think this is probably best routed via -mm since it touches several different places)

Yeap, I think it'd be best to route these through -mm.

Please feel free to add Reviewed-by: Tejun Heo <[email protected]> for the whole series.

I understand that Jeff will be sending a v2 series?

btw, I'll be away-from-keyboard until the end of next week. Nobody be alarmed ;)

#14 lore raw
Quoted 6 lines · On Wed, 27 Mar 2013 12:50:53 -0700 Andrew Morton <[email protected]>…
Quoted 4 lines · On Wed, 27 Mar 2013 12:38:47 -0700 Tejun Heo <[email protected]> wrote:
Quoted 2 lines · Jeff Layton

(Andrew, I think this is probably best routed via -mm since it touches several different places)

Quoted 7 lines
Quoted 4 lines

Yeap, I think it'd be best to route these through -mm.

Please feel free to add Reviewed-by: Tejun Heo <[email protected]> for the whole series.

I understand that Jeff will be sending a v2 series?

Yes, just sent a few mins ago...

Quoted 3 lines

btw, I'll be away-from-keyboard until the end of next week. Nobody be alarmed ;)

Thanks!

Signature · 1 line
Jeff Layton <[email protected]>