Commit 81cc4408 authored by Matthew Wilcox's avatar Matthew Wilcox Committed by Jason Gunthorpe

ucm: Convert ctx_id_table to XArray

Signed-off-by: default avatarMatthew Wilcox <willy@infradead.org>
Signed-off-by: default avatarJason Gunthorpe <jgg@mellanox.com>
parent 8e5a9d61
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
#include <linux/file.h> #include <linux/file.h>
#include <linux/mount.h> #include <linux/mount.h>
#include <linux/cdev.h> #include <linux/cdev.h>
#include <linux/idr.h> #include <linux/xarray.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/slab.h> #include <linux/slab.h>
...@@ -125,23 +125,22 @@ static struct ib_client ucm_client = { ...@@ -125,23 +125,22 @@ static struct ib_client ucm_client = {
.remove = ib_ucm_remove_one .remove = ib_ucm_remove_one
}; };
static DEFINE_MUTEX(ctx_id_mutex); static DEFINE_XARRAY_ALLOC(ctx_id_table);
static DEFINE_IDR(ctx_id_table);
static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES); static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id) static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
{ {
struct ib_ucm_context *ctx; struct ib_ucm_context *ctx;
mutex_lock(&ctx_id_mutex); xa_lock(&ctx_id_table);
ctx = idr_find(&ctx_id_table, id); ctx = xa_load(&ctx_id_table, id);
if (!ctx) if (!ctx)
ctx = ERR_PTR(-ENOENT); ctx = ERR_PTR(-ENOENT);
else if (ctx->file != file) else if (ctx->file != file)
ctx = ERR_PTR(-EINVAL); ctx = ERR_PTR(-EINVAL);
else else
atomic_inc(&ctx->ref); atomic_inc(&ctx->ref);
mutex_unlock(&ctx_id_mutex); xa_unlock(&ctx_id_table);
return ctx; return ctx;
} }
...@@ -194,10 +193,7 @@ static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file) ...@@ -194,10 +193,7 @@ static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
ctx->file = file; ctx->file = file;
INIT_LIST_HEAD(&ctx->events); INIT_LIST_HEAD(&ctx->events);
mutex_lock(&ctx_id_mutex); if (xa_alloc(&ctx_id_table, &ctx->id, ctx, xa_limit_32b, GFP_KERNEL))
ctx->id = idr_alloc(&ctx_id_table, ctx, 0, 0, GFP_KERNEL);
mutex_unlock(&ctx_id_mutex);
if (ctx->id < 0)
goto error; goto error;
list_add_tail(&ctx->file_list, &file->ctxs); list_add_tail(&ctx->file_list, &file->ctxs);
...@@ -514,9 +510,7 @@ static ssize_t ib_ucm_create_id(struct ib_ucm_file *file, ...@@ -514,9 +510,7 @@ static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
err2: err2:
ib_destroy_cm_id(ctx->cm_id); ib_destroy_cm_id(ctx->cm_id);
err1: err1:
mutex_lock(&ctx_id_mutex); xa_erase(&ctx_id_table, ctx->id);
idr_remove(&ctx_id_table, ctx->id);
mutex_unlock(&ctx_id_mutex);
kfree(ctx); kfree(ctx);
return result; return result;
} }
...@@ -536,15 +530,15 @@ static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file, ...@@ -536,15 +530,15 @@ static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
if (copy_from_user(&cmd, inbuf, sizeof(cmd))) if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
return -EFAULT; return -EFAULT;
mutex_lock(&ctx_id_mutex); xa_lock(&ctx_id_table);
ctx = idr_find(&ctx_id_table, cmd.id); ctx = xa_load(&ctx_id_table, cmd.id);
if (!ctx) if (!ctx)
ctx = ERR_PTR(-ENOENT); ctx = ERR_PTR(-ENOENT);
else if (ctx->file != file) else if (ctx->file != file)
ctx = ERR_PTR(-EINVAL); ctx = ERR_PTR(-EINVAL);
else else
idr_remove(&ctx_id_table, ctx->id); __xa_erase(&ctx_id_table, ctx->id);
mutex_unlock(&ctx_id_mutex); xa_unlock(&ctx_id_table);
if (IS_ERR(ctx)) if (IS_ERR(ctx))
return PTR_ERR(ctx); return PTR_ERR(ctx);
...@@ -1189,10 +1183,7 @@ static int ib_ucm_close(struct inode *inode, struct file *filp) ...@@ -1189,10 +1183,7 @@ static int ib_ucm_close(struct inode *inode, struct file *filp)
struct ib_ucm_context, file_list); struct ib_ucm_context, file_list);
mutex_unlock(&file->file_mutex); mutex_unlock(&file->file_mutex);
mutex_lock(&ctx_id_mutex); xa_erase(&ctx_id_table, ctx->id);
idr_remove(&ctx_id_table, ctx->id);
mutex_unlock(&ctx_id_mutex);
ib_destroy_cm_id(ctx->cm_id); ib_destroy_cm_id(ctx->cm_id);
ib_ucm_cleanup_events(ctx); ib_ucm_cleanup_events(ctx);
kfree(ctx); kfree(ctx);
...@@ -1352,7 +1343,7 @@ static void __exit ib_ucm_cleanup(void) ...@@ -1352,7 +1343,7 @@ static void __exit ib_ucm_cleanup(void)
class_remove_file(&cm_class, &class_attr_abi_version.attr); class_remove_file(&cm_class, &class_attr_abi_version.attr);
unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_NUM_FIXED_MINOR); unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_NUM_FIXED_MINOR);
unregister_chrdev_region(dynamic_ucm_dev, IB_UCM_NUM_DYNAMIC_MINOR); unregister_chrdev_region(dynamic_ucm_dev, IB_UCM_NUM_DYNAMIC_MINOR);
idr_destroy(&ctx_id_table); WARN_ON(!xa_empty(&ctx_id_table));
} }
module_init(ib_ucm_init); module_init(ib_ucm_init);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment