Skip to contentSkip to conversation

idr: final idr_alloc() conversions and deprecation of old interface

PATCH New Tejun Heo 3 participants 12 messages Mar 5, 2013

Subsystems (8)
lore
Share & tools
lore

Tejun Heo

author maintainer
#0 lore raw

PATCH 1/1 idr: final idr_alloc() conversions and deprecation of old interface

Hello,

These are the final pieces of idr_alloc() conversion and deprecation of idr_pre_get() and idr_get_new*().

This patchset contains the following seven patches.

0001-nfsd-remove-unused-get_new_stid.patch 0002-nfsd-convert-to-idr_alloc.patch 0003-workqueue-convert-to-idr_alloc.patch 0004-mlx4-remove-leftover-idr_pre_get-call.patch 0005-zcache-convert-to-idr_alloc.patch 0006-tidspbridge-convert-to-idr_alloc.patch 0007-idr-deprecate-idr_pre_get-and-idr_get_new-_above.patch

The patches based on top of v3.9-rc1 and are also available in the following git branch.

git://git.kernel.org/pub/scm/linux/kernel/git/tj/misc.git idr_alloc-final

Andrew, can you please route these through -mm?

diffstat follows, thanks.

 drivers/infiniband/hw/mlx4/cm.c        |    1 
 drivers/staging/tidspbridge/rmgr/drv.c |   70 ++++++++++++---------------------
 drivers/staging/zcache/ramster/tcp.c   |   25 ++++-------
 fs/nfsd/nfs4state.c                    |   36 ----------------
 include/linux/idr.h                    |   66 +++++++++++++++++++++++--------
 kernel/workqueue.c                     |    7 +--
 lib/idr.c                              |   41 ++-----------------
 7 files changed, 97 insertions(+), 149 deletions(-)

--
tejun

author maintainer
#1 lore raw

PATCH 1/7 nfsd: remove unused get_new_stid()

get_new_stid() is no longer used since 3abdb607125 ("nfsd4: simplify idr allocation"). Remove it.

---
 fs/nfsd/nfs4state.c | 31 -------------------------------
 1 file changed, 31 deletions(-)
fs/nfsd/nfs4state.c +0 -31
index 16d39c6..d91d6db 100644
@@ -230,37 +230,6 @@ static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
 		__nfs4_file_put_access(fp, oflag);
 }
 
-static inline int get_new_stid(struct nfs4_stid *stid)
-{
-	static int min_stateid = 0;
-	struct idr *stateids = &stid->sc_client->cl_stateids;
-	int new_stid;
-	int error;
-
-	error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
-	/*
-	 * Note: the necessary preallocation was done in
-	 * nfs4_alloc_stateid().  The idr code caps the number of
-	 * preallocations that can exist at a time, but the state lock
-	 * prevents anyone from using ours before we get here:
-	 */
-	WARN_ON_ONCE(error);
-	/*
-	 * It shouldn't be a problem to reuse an opaque stateid value.
-	 * I don't think it is for 4.1.  But with 4.0 I worry that, for
-	 * example, a stray write retransmission could be accepted by
-	 * the server when it should have been rejected.  Therefore,
-	 * adopt a trick from the sctp code to attempt to maximize the
-	 * amount of time until an id is reused, by ensuring they always
-	 * "increase" (mod INT_MAX):
-	 */
-
-	min_stateid = new_stid+1;
-	if (min_stateid == INT_MAX)
-		min_stateid = 0;
-	return new_stid;
-}
-
 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
 kmem_cache *slab)
 {
Signature · 1 line
1.8.1.4
author maintainer
#3 lore raw

PATCH 2/7 nfsd: convert to idr_alloc()

In reply to Tejun Heo

idr_get_new*() and friends are about to be deprecated. Convert to the new idr_alloc() interface.

Only compile-tested.

---
 fs/nfsd/nfs4state.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +2 -3
index d91d6db..2e27430 100644
@@ -242,9 +242,8 @@ kmem_cache *slab)
 	if (!stid)
 		return NULL;
 
-	if (!idr_pre_get(stateids, GFP_KERNEL))
-		goto out_free;
-	if (idr_get_new_above(stateids, stid, min_stateid, &new_id))
+	new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
+	if (new_id < 0)
 		goto out_free;
 	stid->sc_client = cl;
 	stid->sc_type = 0;
Signature · 1 line
1.8.1.4
author maintainer
#5 lore raw

PATCH 3/7 workqueue: convert to idr_alloc()

In reply to Tejun Heo

idr_get_new*() and friends are about to be deprecated. Convert to the new idr_alloc() interface.

---
 kernel/workqueue.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/workqueue.c +4 -3
index 81f2457..55fac5b 100644
@@ -457,11 +457,12 @@ static int worker_pool_assign_id(struct worker_pool *pool)
 	int ret;
 
 	mutex_lock(&worker_pool_idr_mutex);
-	idr_pre_get(&worker_pool_idr, GFP_KERNEL);
-	ret = idr_get_new(&worker_pool_idr, pool, &pool->id);
+	ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);
+	if (ret >= 0)
+		pool->id = ret;
 	mutex_unlock(&worker_pool_idr_mutex);
 
-	return ret;
+	return ret < 0 ? ret : 0;
 }
 
 /*
Signature · 1 line
1.8.1.4
author maintainer
#9 lore raw

PATCH 6/7 tidspbridge: convert to idr_alloc()

In reply to Tejun Heo

idr_get_new*() and friends are about to be deprecated. Convert to the new idr_alloc() interface.

There are some peculiarities and possible bugs in the converted functions. This patch preserves those.

* drv_insert_node_res_element() returns -ENOMEM on alloc failure, -EFAULT if id space is exhausted. -EFAULT is at best misleading.

* drv_proc_insert_strm_res_element() is even weirder. It returns -EFAULT if kzalloc() fails, -ENOMEM if idr preloading fails and -EPERM if id space is exhausted. What's going on here?

* drv_proc_insert_strm_res_element() doesn't free *pstrm_res after failure.

Only compile tested.

Cc Greg Kroah-Hartman <[email protected]>
Cc Víctor Manuel Jáquez Leal <[email protected]>
Cc Rene Sapiens <[email protected]>
Cc Armando Uribe <[email protected]>
Cc Omar Ramirez Luna <[email protected]>
---
 drivers/staging/tidspbridge/rmgr/drv.c | 70 +++++++++++++---------------------
 1 file changed, 26 insertions(+), 44 deletions(-)
drivers/staging/tidspbridge/rmgr/drv.c +26 -44
index db1da28..be26917 100644
@@ -76,37 +76,28 @@ int drv_insert_node_res_element(void *hnode, void *node_resource,
 	struct node_res_object **node_res_obj =
 	    (struct node_res_object **)node_resource;
 	struct process_context *ctxt = (struct process_context *)process_ctxt;
-	int status = 0;
 	int retval;
 
 	*node_res_obj = kzalloc(sizeof(struct node_res_object), GFP_KERNEL);
-	if (!*node_res_obj) {
-		status = -ENOMEM;
-		goto func_end;
-	}
+	if (!*node_res_obj)
+		return -ENOMEM;
 
 	(*node_res_obj)->node = hnode;
-	retval = idr_get_new(ctxt->node_id, *node_res_obj,
-						&(*node_res_obj)->id);
-	if (retval == -EAGAIN) {
-		if (!idr_pre_get(ctxt->node_id, GFP_KERNEL)) {
-			pr_err("%s: OUT OF MEMORY\n", __func__);
-			status = -ENOMEM;
-			goto func_end;
-		}
-
-		retval = idr_get_new(ctxt->node_id, *node_res_obj,
-						&(*node_res_obj)->id);
+	retval = idr_alloc(ctxt->node_id, *node_res_obj, 0, 0, GFP_KERNEL);
+	if (retval >= 0) {
+		(*node_res_obj)->id = retval;
+		return 0;
 	}
-	if (retval) {
+
+	kfree(*node_res_obj);
+
+	if (retval == -ENOSPC) {
 		pr_err("%s: FAILED, IDR is FULL\n", __func__);
-		status = -EFAULT;
+		return -EFAULT;
+	} else {
+		pr_err("%s: OUT OF MEMORY\n", __func__);
+		return -ENOMEM;
 	}
-func_end:
-	if (status)
-		kfree(*node_res_obj);
-
-	return status;
 }
 
 /* Release all Node resources and its context
@@ -201,35 +192,26 @@ int drv_proc_insert_strm_res_element(void *stream_obj,
 	struct strm_res_object **pstrm_res =
 	    (struct strm_res_object **)strm_res;
 	struct process_context *ctxt = (struct process_context *)process_ctxt;
-	int status = 0;
 	int retval;
 
 	*pstrm_res = kzalloc(sizeof(struct strm_res_object), GFP_KERNEL);
-	if (*pstrm_res == NULL) {
-		status = -EFAULT;
-		goto func_end;
-	}
+	if (*pstrm_res == NULL)
+		return -EFAULT;
 
 	(*pstrm_res)->stream = stream_obj;
-	retval = idr_get_new(ctxt->stream_id, *pstrm_res,
-						&(*pstrm_res)->id);
-	if (retval == -EAGAIN) {
-		if (!idr_pre_get(ctxt->stream_id, GFP_KERNEL)) {
-			pr_err("%s: OUT OF MEMORY\n", __func__);
-			status = -ENOMEM;
-			goto func_end;
-		}
-
-		retval = idr_get_new(ctxt->stream_id, *pstrm_res,
-						&(*pstrm_res)->id);
+	retval = idr_alloc(ctxt->stream_id, *pstrm_res, 0, 0, GFP_KERNEL);
+	if (retval >= 0) {
+		(*pstrm_res)->id = retval;
+		return 0;
 	}
-	if (retval) {
+
+	if (retval == -ENOSPC) {
 		pr_err("%s: FAILED, IDR is FULL\n", __func__);
-		status = -EPERM;
+		return -EPERM;
+	} else {
+		pr_err("%s: OUT OF MEMORY\n", __func__);
+		return -ENOMEM;
 	}
-
-func_end:
-	return status;
 }
 
 static int drv_proc_free_strm_res(int id, void *p, void *process_ctxt)
Signature · 1 line
1.8.1.4
author maintainer
#11 lore raw

PATCH 7/7 idr: deprecate idr_pre_get() and idr_get_new[_above]()

In reply to Tejun Heo

Now that all in-kernel users are converted to ues the new alloc interface, mark the old interface deprecated. We should be able to remove these in a few releases.

Cc Rusty Russell <[email protected]>
---
 include/linux/idr.h | 66 ++++++++++++++++++++++++++++++++++++++++-------------
 lib/idr.c           | 41 ++++-----------------------------
 2 files changed, 55 insertions(+), 52 deletions(-)
include/linux/idr.h +50 -16
index a6f38b5..1fd0bde 100644
@@ -73,8 +73,6 @@ struct idr {
  */
 
 void *idr_find_slowpath(struct idr *idp, int id);
-int idr_pre_get(struct idr *idp, gfp_t gfp_mask);
-int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, 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_for_each(struct idr *idp,
@@ -120,19 +118,6 @@ static inline void *idr_find(struct idr *idr, int id)
 }
 
 /**
- * idr_get_new - allocate new idr entry
- * @idp: idr handle
- * @ptr: pointer you want associated with the id
- * @id: pointer to the allocated handle
- *
- * Simple wrapper around idr_get_new_above() w/ @starting_id of zero.
- */
-static inline int idr_get_new(struct idr *idp, void *ptr, int *id)
-{
-	return idr_get_new_above(idp, ptr, 0, id);
-}
-
-/**
  * idr_for_each_entry - iterate over an idr's elements of a given type
  * @idp:     idr handle
  * @entry:   the type * to use as cursor
@@ -143,7 +128,56 @@ static inline int idr_get_new(struct idr *idp, void *ptr, int *id)
 	     entry != NULL;                                             \
 	     ++id, entry = (typeof(entry))idr_get_next((idp), &(id)))
 
-void __idr_remove_all(struct idr *idp);	/* don't use */
+/*
+ * Don't use the following functions.  These exist only to suppress
+ * deprecated warnings on EXPORT_SYMBOL()s.
+ */
+int __idr_pre_get(struct idr *idp, gfp_t gfp_mask);
+int __idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id);
+void __idr_remove_all(struct idr *idp);
+
+/**
+ * idr_pre_get - reserve resources for idr allocation
+ * @idp:	idr handle
+ * @gfp_mask:	memory allocation flags
+ *
+ * Part of old alloc interface.  This is going away.  Use
+ * idr_preload[_end]() and idr_alloc() instead.
+ */
+static inline int __deprecated idr_pre_get(struct idr *idp, gfp_t gfp_mask)
+{
+	return __idr_pre_get(idp, gfp_mask);
+}
+
+/**
+ * idr_get_new_above - allocate new idr entry above or equal to a start id
+ * @idp: idr handle
+ * @ptr: pointer you want associated with the id
+ * @starting_id: id to start search at
+ * @id: pointer to the allocated handle
+ *
+ * Part of old alloc interface.  This is going away.  Use
+ * idr_preload[_end]() and idr_alloc() instead.
+ */
+static inline int __deprecated idr_get_new_above(struct idr *idp, void *ptr,
+						 int starting_id, int *id)
+{
+	return __idr_get_new_above(idp, ptr, starting_id, id);
+}
+
+/**
+ * idr_get_new - allocate new idr entry
+ * @idp: idr handle
+ * @ptr: pointer you want associated with the id
+ * @id: pointer to the allocated handle
+ *
+ * Part of old alloc interface.  This is going away.  Use
+ * idr_preload[_end]() and idr_alloc() instead.
+ */
+static inline int __deprecated idr_get_new(struct idr *idp, void *ptr, int *id)
+{
+	return __idr_get_new_above(idp, ptr, 0, id);
+}
 
 /**
  * idr_remove_all - remove all ids from the given idr tree
lib/idr.c +5 -36

71 lines not on this page · raw message

Signature · 1 line
1.8.1.4
author maintainer
#7 lore raw

PATCH 5/7 zcache: convert to idr_alloc()

In reply to Tejun Heo

idr_get_new*() and friends are about to be deprecated. Convert to the new idr_alloc() interface.

Only compile tested.

Cc Dan Magenheimer <[email protected]>
---
 drivers/staging/zcache/ramster/tcp.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
drivers/staging/zcache/ramster/tcp.c +10 -15
index aa2a1a7..f6e1e52 100644
@@ -300,27 +300,22 @@ static u8 r2net_num_from_nn(struct r2net_node *nn)
 
 static int r2net_prep_nsw(struct r2net_node *nn, struct r2net_status_wait *nsw)
 {
-	int ret = 0;
+	int ret;
 
-	do {
-		if (!idr_pre_get(&nn->nn_status_idr, GFP_ATOMIC)) {
-			ret = -EAGAIN;
-			break;
-		}
-		spin_lock(&nn->nn_lock);
-		ret = idr_get_new(&nn->nn_status_idr, nsw, &nsw->ns_id);
-		if (ret == 0)
-			list_add_tail(&nsw->ns_node_item,
-				      &nn->nn_status_list);
-		spin_unlock(&nn->nn_lock);
-	} while (ret == -EAGAIN);
+	spin_lock(&nn->nn_lock);
+	ret = idr_alloc(&nn->nn_status_idr, nsw, 0, 0, GFP_ATOMIC);
+	if (ret >= 0) {
+		nsw->ns_id = ret;
+		list_add_tail(&nsw->ns_node_item, &nn->nn_status_list);
+	}
+	spin_unlock(&nn->nn_lock);
 
-	if (ret == 0)  {
+	if (ret >= 0) {
 		init_waitqueue_head(&nsw->ns_wq);
 		nsw->ns_sys_status = R2NET_ERR_NONE;
 		nsw->ns_status = 0;
+		return 0;
 	}
-
 	return ret;
 }
 
Signature · 1 line
1.8.1.4
author maintainer
#6 lore raw

PATCH 4/7 mlx4: remove leftover idr_pre_get() call

In reply to Tejun Heo

6a9200603d ("IB/mlx4: convert to idr_alloc()") forgot to remove idr_pre_get() call in mlx4_ib_cm_paravirt_init(). It's unnecessary and idr_pre_get() will soon be deprecated. Remove it.

Cc Jack Morgenstein <[email protected]>
Cc Or Gerlitz <[email protected]>
Cc Roland Dreier <[email protected]>
---
 drivers/infiniband/hw/mlx4/cm.c | 1 -
 1 file changed, 1 deletion(-)
drivers/infiniband/hw/mlx4/cm.c +0 -1
index e0d79b2..add98d0 100644
@@ -362,7 +362,6 @@ void mlx4_ib_cm_paravirt_init(struct mlx4_ib_dev *dev)
 	INIT_LIST_HEAD(&dev->sriov.cm_list);
 	dev->sriov.sl_id_map = RB_ROOT;
 	idr_init(&dev->sriov.pv_id_table);
-	idr_pre_get(&dev->sriov.pv_id_table, GFP_KERNEL);
 }
 
 /* slave = -1 ==> all slaves */
Signature · 1 line
1.8.1.4
Acked-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] nfsd: remove unused get_new_stid()

In reply to Tejun Heo
Quoted 2 lines · Tejun Heo

get_new_stid() is no longer used since 3abdb607125 ("nfsd4: simplify idr allocation"). Remove it.

Whoops, thanks for catching that.

Quoted 53 lines
---
 fs/nfsd/nfs4state.c | 31 -------------------------------
 1 file changed, 31 deletions(-)
fs/nfsd/nfs4state.c +0 -31
index 16d39c6..d91d6db 100644
@@ -230,37 +230,6 @@ static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
 		__nfs4_file_put_access(fp, oflag);
 }
 
-static inline int get_new_stid(struct nfs4_stid *stid)
-{
-	static int min_stateid = 0;
-	struct idr *stateids = &stid->sc_client->cl_stateids;
-	int new_stid;
-	int error;
-
-	error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
-	/*
-	 * Note: the necessary preallocation was done in
-	 * nfs4_alloc_stateid().  The idr code caps the number of
-	 * preallocations that can exist at a time, but the state lock
-	 * prevents anyone from using ours before we get here:
-	 */
-	WARN_ON_ONCE(error);
-	/*
-	 * It shouldn't be a problem to reuse an opaque stateid value.
-	 * I don't think it is for 4.1.  But with 4.0 I worry that, for
-	 * example, a stray write retransmission could be accepted by
-	 * the server when it should have been rejected.  Therefore,
-	 * adopt a trick from the sctp code to attempt to maximize the
-	 * amount of time until an id is reused, by ensuring they always
-	 * "increase" (mod INT_MAX):
-	 */
-
-	min_stateid = new_stid+1;
-	if (min_stateid == INT_MAX)
-		min_stateid = 0;
-	return new_stid;
-}
-
 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
 kmem_cache *slab)
 {
Signature · 1 line
1.8.1.4
maintainer Acked-by on v1 6/7
#10 lore raw

Re: [PATCH 6/7] tidspbridge: convert to idr_alloc()

In reply to Tejun Heo
Quoted 24 lines · Tejun Heo

idr_get_new*() and friends are about to be deprecated. Convert to the new idr_alloc() interface.

There are some peculiarities and possible bugs in the converted functions. This patch preserves those.

* drv_insert_node_res_element() returns -ENOMEM on alloc failure, -EFAULT if id space is exhausted. -EFAULT is at best misleading.

* drv_proc_insert_strm_res_element() is even weirder. It returns -EFAULT if kzalloc() fails, -ENOMEM if idr preloading fails and -EPERM if id space is exhausted. What's going on here?

* drv_proc_insert_strm_res_element() doesn't free *pstrm_res after failure.

Only compile tested.

Cc Greg Kroah-Hartman <[email protected]>
Cc Víctor Manuel Jáquez Leal <[email protected]>
Cc Rene Sapiens <[email protected]>
Cc Armando Uribe <[email protected]>
Cc Omar Ramirez Luna <[email protected]>
Quoted 3 lines
---
 drivers/staging/tidspbridge/rmgr/drv.c | 70 +++++++++++++---------------------
 1 file changed, 26 insertions(+), 44 deletions(-)
maintainer Acked-by on v1 5/7
#8 lore raw

Re: [PATCH 5/7] zcache: convert to idr_alloc()

In reply to Tejun Heo
Quoted 10 lines · Tejun Heo

idr_get_new*() and friends are about to be deprecated. Convert to the new idr_alloc() interface.

Only compile tested.

Cc Dan Magenheimer <[email protected]>
---
 drivers/staging/zcache/ramster/tcp.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
Acked-by on v1 2/7
#4 lore raw

Re: [PATCH 2/7] nfsd: convert to idr_alloc()

In reply to Tejun Heo
Quoted 4 lines · Tejun Heo

idr_get_new*() and friends are about to be deprecated. Convert to the new idr_alloc() interface.

Only compile-tested.

Runs fine too--thanks.

--b.

Quoted 27 lines
---
 fs/nfsd/nfs4state.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/nfsd/nfs4state.c +2 -3
index d91d6db..2e27430 100644
@@ -242,9 +242,8 @@ kmem_cache *slab)
 	if (!stid)
 		return NULL;
 
-	if (!idr_pre_get(stateids, GFP_KERNEL))
-		goto out_free;
-	if (idr_get_new_above(stateids, stid, min_stateid, &new_id))
+	new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
+	if (new_id < 0)
 		goto out_free;
 	stid->sc_client = cl;
 	stid->sc_type = 0;
Signature · 1 line
1.8.1.4