[PATCH 52/62] ipc: Remove call to idr_preload

Matthew Wilcox
2017-11-22T21:11:38Z

The ipc code follows a different pattern to most IDR preload users;
the lock that it is holding is embedded in the object, not protecting
the IDR (the IDR is protected by an rwsem). Instead of dropping the
lock, allocating memory and retrying, we reserve a slot in the tree
before grabbing the lock, then merely replace the NULL entry with the
initialised object.

Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
---
ipc/util.c | 28 ++++++++++++----------------
1 file changed, 12 insertions(+), 16 deletions(-)

diff --git a/ipc/util.c b/ipc/util.c
index ff045fec8d83..9a20354ce4fb 100644
--- a/ipc/util.c
+++ b/ipc/util.c
@@ -193,10 +193,10 @@ static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
/*
* Specify desired id for next allocated IPC object.
*/
-#define ipc_idr_alloc(ids, new) \
- idr_alloc(&(ids)->ipcs_idr, (new), \
+#define ipc_idr_alloc(ids) \
+ idr_alloc(&(ids)->ipcs_idr, NULL, \
(ids)->next_id < 0 ? 0 : ipcid_to_idx((ids)->next_id),\
- 0, GFP_NOWAIT)
+ 0, GFP_KERNEL)

static inline int ipc_buildid(int id, struct ipc_ids *ids,
struct kern_ipc_perm *new)
@@ -214,8 +214,8 @@ static inline int ipc_buildid(int id, struct ipc_ids *ids,
}

#else
-#define ipc_idr_alloc(ids, new) \
- idr_alloc(&(ids)->ipcs_idr, (new), 0, 0, GFP_NOWAIT)
+#define ipc_idr_alloc(ids) \
+ idr_alloc(&(ids)->ipcs_idr, NULL, 0, 0, GFP_KERNEL)

static inline int ipc_buildid(int id, struct ipc_ids *ids,
struct kern_ipc_perm *new)
@@ -254,34 +254,30 @@ int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
if (!ids->tables_initialized || ids->in_use >= limit)
return -ENOSPC;

- idr_preload(GFP_KERNEL);
+ id = ipc_idr_alloc(ids);
+ if (id < 0)
+ return id;

refcount_set(&new->refcount, 1);
spin_lock_init(&new->lock);
new->deleted = false;
- rcu_read_lock();
spin_lock(&new->lock);

current_euid_egid(&euid, &egid);
new->cuid = new->uid = euid;
new->gid = new->cgid = egid;

- id = ipc_idr_alloc(ids, new);
- idr_preload_end();
+ idr_replace(&ids->ipcs_idr, new, id);

- if (id >= 0 && new->key != IPC_PRIVATE) {
+ if (new->key != IPC_PRIVATE) {
err = rhashtable_insert_fast(&ids->key_ht, &new->khtnode,
ipc_kht_params);
if (err < 0) {
idr_remove(&ids->ipcs_idr, id);
- id = err;
+ spin_unlock(&new->lock);
+ return err;
}
}
- if (id < 0) {
- spin_unlock(&new->lock);
- rcu_read_unlock();
- return id;
- }

ids->in_use++;
if (id > ids->max_id)
--
2.15.0