[ofa-general] [PATCH 6/11] core: XRC - file descriptors associated with XRC domains

Jack Morgenstein jackm at dev.mellanox.co.il
Mon Jun 23 06:01:37 PDT 2008


Add XRC support for working with file descriptors, to allow
sharing XRC domains between processes.

Signed-off-by: Jack Morgenstein <jackm at dev.mellanox.co.il>

Index: infiniband/drivers/infiniband/core/uverbs_cmd.c
===================================================================
--- infiniband.orig/drivers/infiniband/core/uverbs_cmd.c	2008-06-23 13:59:52.000000000 +0300
+++ infiniband/drivers/infiniband/core/uverbs_cmd.c	2008-06-23 14:00:01.000000000 +0300
@@ -37,6 +37,7 @@
 #include <linux/fs.h>
 
 #include <asm/uaccess.h>
+#include <asm/fcntl.h>
 
 #include "uverbs.h"
 
@@ -254,14 +255,18 @@ static void put_srq_read(struct ib_srq *
 	put_uobj_read(srq->uobject);
 }
 
-static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context)
+static struct ib_xrcd *idr_read_xrcd(int xrcd_handle,
+				     struct ib_ucontext *context,
+				     struct ib_uobject **uobj)
 {
-	return idr_read_obj(&ib_uverbs_xrc_domain_idr, xrcd_handle, context, 0);
+	*uobj = idr_read_uobj(&ib_uverbs_xrc_domain_idr, xrcd_handle,
+			      context, 0);
+	return *uobj ? (*uobj)->object : NULL;
 }
 
-static void put_xrcd_read(struct ib_xrcd *xrcd)
+static void put_xrcd_read(struct ib_uobject *uobj)
 {
-	put_uobj_read(xrcd->uobject);
+	put_uobj_read(uobj);
 }
 
 ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
@@ -1038,6 +1043,7 @@ ssize_t ib_uverbs_create_qp(struct ib_uv
 	struct ib_qp                   *qp;
 	struct ib_qp_init_attr          attr;
 	struct ib_xrcd	 	       *xrcd;
+	struct ib_uobject		*xrcd_uobj;
 	int ret;
 
 	if (out_len < sizeof resp)
@@ -1060,7 +1066,7 @@ ssize_t ib_uverbs_create_qp(struct ib_uv
 	srq = (cmd.is_srq && cmd.qp_type != IB_QPT_XRC) ?
 		idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
 	xrcd = cmd.qp_type == IB_QPT_XRC ?
-		idr_read_xrcd(cmd.srq_handle, file->ucontext) : NULL;
+		idr_read_xrcd(cmd.srq_handle, file->ucontext, &xrcd_uobj) : NULL;
 	pd  = idr_read_pd(cmd.pd_handle, file->ucontext);
 	scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, 0);
 	rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
@@ -1143,7 +1149,7 @@ ssize_t ib_uverbs_create_qp(struct ib_uv
 	if (srq)
 		put_srq_read(srq);
 	if (xrcd)
-		put_xrcd_read(xrcd);
+		put_xrcd_read(xrcd_uobj);
 
 	mutex_lock(&file->mutex);
 	list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
@@ -1171,7 +1177,7 @@ err_put:
 	if (srq)
 		put_srq_read(srq);
 	if (xrcd)
-		put_xrcd_read(xrcd);
+		put_xrcd_read(xrcd_uobj);
 
 	put_uobj_write(&obj->uevent.uobject);
 	return ret;
@@ -2084,6 +2090,7 @@ ssize_t ib_uverbs_create_xrc_srq(struct 
 	struct ib_cq			*xrc_cq;
 	struct ib_xrcd			*xrcd;
 	struct ib_srq_init_attr          attr;
+	struct ib_uobject		*xrcd_uobj;
 	int ret;
 
 	if (out_len < sizeof resp)
@@ -2115,7 +2122,7 @@ ssize_t ib_uverbs_create_xrc_srq(struct 
 		goto err_put_pd;
 	}
 
-	xrcd  = idr_read_xrcd(cmd.xrcd_handle, file->ucontext);
+	xrcd  = idr_read_xrcd(cmd.xrcd_handle, file->ucontext, &xrcd_uobj);
 	if (!xrcd) {
 		ret = -EINVAL;
 		goto err_put_cq;
@@ -2166,7 +2173,7 @@ ssize_t ib_uverbs_create_xrc_srq(struct 
 		goto err_copy;
 	}
 
-	put_xrcd_read(xrcd);
+	put_xrcd_read(xrcd_uobj);
 	put_cq_read(xrc_cq);
 	put_pd_read(pd);
 
@@ -2187,7 +2194,7 @@ err_destroy:
 	ib_destroy_srq(srq);
 
 err_put:
-	put_xrcd_read(xrcd);
+	put_xrcd_read(xrcd_uobj);
 
 err_put_cq:
 	put_cq_read(xrc_cq);
@@ -2319,6 +2326,117 @@ ssize_t ib_uverbs_destroy_srq(struct ib_
 	return ret ? ret : in_len;
 }
 
+static struct inode * xrc_fd2inode(unsigned int fd)
+{
+	struct file * f = fget(fd);
+
+	if (!f)
+		return NULL;
+
+	return f->f_dentry->d_inode;
+}
+
+struct xrcd_table_entry {
+	struct rb_node node;
+	struct inode * inode;
+	struct ib_xrcd *xrcd;
+};
+
+static int xrcd_table_insert(struct ib_device *dev,
+			     struct inode *i_n,
+			     struct ib_xrcd *xrcd)
+{
+	struct xrcd_table_entry *entry, *scan;
+	struct rb_node **p = &dev->ib_uverbs_xrcd_table.rb_node;
+	struct rb_node *parent = NULL;
+
+	entry = kmalloc(sizeof(struct xrcd_table_entry), GFP_KERNEL);
+	if (!entry)
+		return -ENOMEM;
+
+	entry->inode = i_n;
+	entry->xrcd = xrcd;
+
+	while (*p)
+	{
+		parent = *p;
+		scan = rb_entry(parent, struct xrcd_table_entry, node);
+
+		if (i_n < scan->inode)
+			p = &(*p)->rb_left;
+		else if (i_n > scan->inode)
+			p = &(*p)->rb_right;
+		else {
+			kfree(entry);
+			return -EEXIST;
+		}
+	}
+
+	rb_link_node(&entry->node, parent, p);
+	rb_insert_color(&entry->node, &dev->ib_uverbs_xrcd_table);
+	return 0;
+}
+
+static int insert_xrcd(struct ib_device *dev, struct inode *i_n,
+		       struct ib_xrcd *xrcd)
+{
+	int ret;
+
+	ret = xrcd_table_insert(dev, i_n, xrcd);
+	if (!ret)
+		igrab(i_n);
+
+	return ret;
+}
+
+static struct xrcd_table_entry * xrcd_table_search(struct ib_device *dev,
+						   struct inode *i_n)
+{
+	struct xrcd_table_entry *scan;
+	struct rb_node **p = &dev->ib_uverbs_xrcd_table.rb_node;
+	struct rb_node *parent = NULL;
+
+	while (*p)
+	{
+		parent = *p;
+		scan = rb_entry(parent, struct xrcd_table_entry, node);
+
+		if (i_n < scan->inode)
+			p = &(*p)->rb_left;
+		else if (i_n > scan->inode)
+			p = &(*p)->rb_right;
+		else
+			return scan;
+	}
+	return NULL;
+}
+
+static int find_xrcd(struct ib_device *dev, struct inode *i_n,
+		     struct ib_xrcd **xrcd)
+{
+	struct xrcd_table_entry *entry;
+
+	entry = xrcd_table_search(dev, i_n);
+	if (!entry)
+		return -EINVAL;
+
+	*xrcd = entry->xrcd;
+	return 0;
+}
+
+
+static void xrcd_table_delete(struct ib_device *dev,
+			      struct inode *i_n)
+{
+	struct xrcd_table_entry *entry = xrcd_table_search(dev, i_n);
+
+	if (entry) {
+		iput(i_n);
+		rb_erase(&entry->node, &dev->ib_uverbs_xrcd_table);
+		kfree(entry);
+	}
+}
+
 ssize_t ib_uverbs_open_xrc_domain(struct ib_uverbs_file *file,
 				  const char __user *buf, int in_len,
 				  int out_len)
@@ -2327,8 +2445,10 @@ ssize_t ib_uverbs_open_xrc_domain(struct
 	struct ib_uverbs_open_xrc_domain_resp resp;
 	struct ib_udata			 udata;
 	struct ib_uobject             	*uobj;
-	struct ib_xrcd			*xrcd;
-	int				 ret;
+	struct ib_xrcd			*xrcd = NULL;
+	struct inode			*inode = NULL;
+	int				 ret = 0;
+	int				 new_xrcd = 0;
 
 	if (out_len < sizeof resp)
 		return -ENOSPC;
@@ -2336,35 +2456,55 @@ ssize_t ib_uverbs_open_xrc_domain(struct
 	if (copy_from_user(&cmd, buf, sizeof cmd))
 		return -EFAULT;
 
-	/* file descriptors/inodes not yet implemented */
-	if (cmd.fd != (u32) (-1))
-		return -ENOSYS;
-
 	INIT_UDATA(&udata, buf + sizeof cmd,
 		   (unsigned long) cmd.response + sizeof resp,
 		   in_len - sizeof cmd, out_len - sizeof resp);
 
+	mutex_lock(&file->device->ib_dev->xrcd_table_mutex);
+	if (cmd.fd != (u32) (-1)) {
+		/* search for file descriptor */
+		inode = xrc_fd2inode(cmd.fd);
+		if (!inode) {
+			ret = -EBADF;
+			goto err_table_mutex_unlock;
+		}
+
+		ret = find_xrcd(file->device->ib_dev, inode, &xrcd);
+		if (ret && !(cmd.oflags & O_CREAT)) {
+			/* no file descriptor. Need CREATE flag */
+			ret = -EAGAIN;
+			goto err_table_mutex_unlock;
+        	}
+
+        	if (xrcd && cmd.oflags & O_EXCL){
+			ret = -EINVAL;
+			goto err_table_mutex_unlock;
+		}
+	}
+
 	uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
-	if (!uobj)
-		return -ENOMEM;
+	if (!uobj) {
+		ret = -ENOMEM;
+		goto err_table_mutex_unlock;
+	}
 
 	init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
 	down_write(&uobj->mutex);
 
-
-	xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
-						file->ucontext, &udata);
-	if (IS_ERR(xrcd)) {
-		ret = PTR_ERR(xrcd);
-		goto err;
+	if (!xrcd) {
+		xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
+							file->ucontext, &udata);
+		if (IS_ERR(xrcd)) {
+			ret = PTR_ERR(xrcd);
+			goto err;
+		}
+		xrcd->uobject = (cmd.fd == -1) ? uobj : NULL;
+		xrcd->inode = inode;
+		xrcd->device  = file->device->ib_dev;
+		atomic_set(&xrcd->usecnt, 0);
+		new_xrcd = 1;
 	}
 
-	xrcd->fd = cmd.fd;
-	xrcd->flags = cmd.oflags;
-	xrcd->uobject = uobj;
-	xrcd->device  = file->device->ib_dev;
-	atomic_set(&xrcd->usecnt, 0);
-
 	uobj->object = xrcd;
 	ret = idr_add_uobj(&ib_uverbs_xrc_domain_idr, uobj);
 	if (ret)
@@ -2373,6 +2513,16 @@ ssize_t ib_uverbs_open_xrc_domain(struct
 	memset(&resp, 0, sizeof resp);
 	resp.xrcd_handle = uobj->id;
 
+	if (inode) {
+		if (new_xrcd) {
+		/* create new inode/xrcd table entry */
+			ret = insert_xrcd(file->device->ib_dev, inode, xrcd);
+			if (ret)
+				goto err_insert_xrcd;
+		}
+		atomic_inc(&xrcd->usecnt);
+	}
+
 	if (copy_to_user((void __user *) (unsigned long) cmd.response,
 			 &resp, sizeof resp)) {
 		ret = -EFAULT;
@@ -2387,16 +2537,29 @@ ssize_t ib_uverbs_open_xrc_domain(struct
 
 	up_write(&uobj->mutex);
 
+	mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
 	return in_len;
 
 err_copy:
-	idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
+
+	if (inode) {
+		if (new_xrcd)
+			xrcd_table_delete(file->device->ib_dev, inode);
+		atomic_dec(&xrcd->usecnt);
+	}
+
+err_insert_xrcd:
+	idr_remove_uobj(&ib_uverbs_xrc_domain_idr, uobj);
 
 err_idr:
 	ib_dealloc_xrcd(xrcd);
 
 err:
 	put_uobj_write(uobj);
+
+err_table_mutex_unlock:
+
+	mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
 	return ret;
 }
 
@@ -2406,14 +2569,25 @@ ssize_t ib_uverbs_close_xrc_domain(struc
 {
 	struct ib_uverbs_close_xrc_domain cmd;
 	struct ib_uobject          *uobj;
-	int                         ret;
+	struct ib_xrcd             *xrcd = NULL;
+	struct inode		   *inode = NULL;
+	int                         ret = 0;
 
 	if (copy_from_user(&cmd, buf, sizeof cmd))
 		return -EFAULT;
 
+	mutex_lock(&file->device->ib_dev->xrcd_table_mutex);
 	uobj = idr_write_uobj(&ib_uverbs_xrc_domain_idr, cmd.xrcd_handle, file->ucontext);
-	if (!uobj)
-		return -EINVAL;
+	if (!uobj) {
+		ret = -EINVAL;
+		goto err_unlock_mutex;
+	}
+
+	xrcd = (struct ib_xrcd *) (uobj->object);
+	inode = xrcd->inode;
+
+	if (inode)
+		atomic_dec(&xrcd->usecnt);
 
 	ret = ib_dealloc_xrcd(uobj->object);
 	if (!ret)
@@ -2421,8 +2595,11 @@ ssize_t ib_uverbs_close_xrc_domain(struc
 
 	put_uobj_write(uobj);
 
-	if (ret)
-		return ret;
+	if (ret && !inode)
+		goto err_unlock_mutex;
+
+	if (!ret && inode)
+		xrcd_table_delete(file->device->ib_dev, inode);
 
 	idr_remove_uobj(&ib_uverbs_xrc_domain_idr, uobj);
 
@@ -2432,6 +2609,27 @@ ssize_t ib_uverbs_close_xrc_domain(struc
 
 	put_uobj(uobj);
 
+	mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
 	return in_len;
+
+err_unlock_mutex:
+	mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
+	return ret;
 }
 
+void ib_uverbs_dealloc_xrcd(struct ib_device *ib_dev,
+				   struct ib_xrcd *xrcd)
+{
+	struct inode		   *inode = NULL;
+	int                         ret = 0;
+
+	inode = xrcd->inode;
+	if (inode)
+		atomic_dec(&xrcd->usecnt);
+
+	ret = ib_dealloc_xrcd(xrcd);
+	if (!ret && inode)
+		xrcd_table_delete(ib_dev, inode);
+}
+
+
Index: infiniband/include/rdma/ib_verbs.h
===================================================================
--- infiniband.orig/include/rdma/ib_verbs.h	2008-06-23 13:59:52.000000000 +0300
+++ infiniband/include/rdma/ib_verbs.h	2008-06-23 14:00:01.000000000 +0300
@@ -50,6 +50,8 @@
 
 #include <asm/atomic.h>
 #include <asm/uaccess.h>
+#include <linux/rbtree.h>
+#include <linux/mutex.h>
 
 union ib_gid {
 	u8	raw[16];
@@ -796,11 +798,8 @@ struct ib_pd {
 struct ib_xrcd {
 	struct ib_device       *device;
 	struct ib_uobject      *uobject;
-	struct rb_node	        node;
-	u32			xrc_domain_num;
 	struct inode	       *inode;
-	int			fd;
-	u32			flags;
+	struct rb_node	        node;
 	atomic_t          	usecnt; /* count all resources */
 };
 
@@ -1125,6 +1124,8 @@ struct ib_device {
 	__be64			     node_guid;
 	u8                           node_type;
 	u8                           phys_port_cnt;
+	struct rb_root		     ib_uverbs_xrcd_table;
+	struct mutex		     xrcd_table_mutex;
 };
 
 struct ib_client {
Index: infiniband/drivers/infiniband/core/device.c
===================================================================
--- infiniband.orig/drivers/infiniband/core/device.c	2008-06-23 11:43:29.000000000 +0300
+++ infiniband/drivers/infiniband/core/device.c	2008-06-23 14:00:01.000000000 +0300
@@ -288,6 +288,8 @@ int ib_register_device(struct ib_device 
 	INIT_LIST_HEAD(&device->client_data_list);
 	spin_lock_init(&device->event_handler_lock);
 	spin_lock_init(&device->client_data_lock);
+	device->ib_uverbs_xrcd_table = RB_ROOT;
+	mutex_init(&device->xrcd_table_mutex);
 
 	ret = read_port_table_lengths(device);
 	if (ret) {
Index: infiniband/drivers/infiniband/core/uverbs_main.c
===================================================================
--- infiniband.orig/drivers/infiniband/core/uverbs_main.c	2008-06-23 13:59:52.000000000 +0300
+++ infiniband/drivers/infiniband/core/uverbs_main.c	2008-06-23 14:00:01.000000000 +0300
@@ -249,13 +249,15 @@ static int ib_uverbs_cleanup_ucontext(st
 		kfree(uobj);
 	}
 
+	mutex_lock(&file->device->ib_dev->xrcd_table_mutex);
 	list_for_each_entry_safe(uobj, tmp, &context->xrc_domain_list, list) {
 		struct ib_xrcd *xrcd = uobj->object;
 
 		idr_remove_uobj(&ib_uverbs_xrc_domain_idr, uobj);
-		ib_dealloc_xrcd(xrcd);
+		ib_uverbs_dealloc_xrcd(file->device->ib_dev, xrcd);
 		kfree(uobj);
 	}
+	mutex_unlock(&file->device->ib_dev->xrcd_table_mutex);
 
 	list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
 		struct ib_pd *pd = uobj->object;
Index: infiniband/drivers/infiniband/core/uverbs.h
===================================================================
--- infiniband.orig/drivers/infiniband/core/uverbs.h	2008-06-23 13:59:52.000000000 +0300
+++ infiniband/drivers/infiniband/core/uverbs.h	2008-06-23 14:00:01.000000000 +0300
@@ -161,6 +161,8 @@ void ib_uverbs_qp_event_handler(struct i
 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
 void ib_uverbs_event_handler(struct ib_event_handler *handler,
 			     struct ib_event *event);
+void ib_uverbs_dealloc_xrcd(struct ib_device *ib_dev,
+			    struct ib_xrcd *xrcd);
 
 #define IB_UVERBS_DECLARE_CMD(name)					\
 	ssize_t ib_uverbs_##name(struct ib_uverbs_file *file,		\



More information about the general mailing list