uverbs_cmd.c

来自「linux 内核源代码」· C语言 代码 · 共 2,158 行 · 第 1/4 页

C
2,158
字号
			     int in_len, int out_len){	struct ib_uverbs_dealloc_pd cmd;	struct ib_uobject          *uobj;	int                         ret;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);	if (!uobj)		return -EINVAL;	ret = ib_dealloc_pd(uobj->object);	if (!ret)		uobj->live = 0;	put_uobj_write(uobj);	if (ret)		return ret;	idr_remove_uobj(&ib_uverbs_pd_idr, uobj);	mutex_lock(&file->mutex);	list_del(&uobj->list);	mutex_unlock(&file->mutex);	put_uobj(uobj);	return in_len;}ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,			 const char __user *buf, int in_len,			 int out_len){	struct ib_uverbs_reg_mr      cmd;	struct ib_uverbs_reg_mr_resp resp;	struct ib_udata              udata;	struct ib_uobject           *uobj;	struct ib_pd                *pd;	struct ib_mr                *mr;	int                          ret;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	INIT_UDATA(&udata, buf + sizeof cmd,		   (unsigned long) cmd.response + sizeof resp,		   in_len - sizeof cmd, out_len - sizeof resp);	if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))		return -EINVAL;	/*	 * Local write permission is required if remote write or	 * remote atomic permission is also requested.	 */	if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&	    !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))		return -EINVAL;	uobj = kmalloc(sizeof *uobj, GFP_KERNEL);	if (!uobj)		return -ENOMEM;	init_uobj(uobj, 0, file->ucontext, &mr_lock_key);	down_write(&uobj->mutex);	pd = idr_read_pd(cmd.pd_handle, file->ucontext);	if (!pd) {		ret = -EINVAL;		goto err_free;	}	mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,				     cmd.access_flags, &udata);	if (IS_ERR(mr)) {		ret = PTR_ERR(mr);		goto err_put;	}	mr->device  = pd->device;	mr->pd      = pd;	mr->uobject = uobj;	atomic_inc(&pd->usecnt);	atomic_set(&mr->usecnt, 0);	uobj->object = mr;	ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);	if (ret)		goto err_unreg;	memset(&resp, 0, sizeof resp);	resp.lkey      = mr->lkey;	resp.rkey      = mr->rkey;	resp.mr_handle = uobj->id;	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp)) {		ret = -EFAULT;		goto err_copy;	}	put_pd_read(pd);	mutex_lock(&file->mutex);	list_add_tail(&uobj->list, &file->ucontext->mr_list);	mutex_unlock(&file->mutex);	uobj->live = 1;	up_write(&uobj->mutex);	return in_len;err_copy:	idr_remove_uobj(&ib_uverbs_mr_idr, uobj);err_unreg:	ib_dereg_mr(mr);err_put:	put_pd_read(pd);err_free:	put_uobj_write(uobj);	return ret;}ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,			   const char __user *buf, int in_len,			   int out_len){	struct ib_uverbs_dereg_mr cmd;	struct ib_mr             *mr;	struct ib_uobject	 *uobj;	int                       ret = -EINVAL;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);	if (!uobj)		return -EINVAL;	mr = uobj->object;	ret = ib_dereg_mr(mr);	if (!ret)		uobj->live = 0;	put_uobj_write(uobj);	if (ret)		return ret;	idr_remove_uobj(&ib_uverbs_mr_idr, uobj);	mutex_lock(&file->mutex);	list_del(&uobj->list);	mutex_unlock(&file->mutex);	put_uobj(uobj);	return in_len;}ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,				      const char __user *buf, int in_len,				      int out_len){	struct ib_uverbs_create_comp_channel	   cmd;	struct ib_uverbs_create_comp_channel_resp  resp;	struct file				  *filp;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	filp = ib_uverbs_alloc_event_file(file, 0, &resp.fd);	if (IS_ERR(filp))		return PTR_ERR(filp);	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp)) {		put_unused_fd(resp.fd);		fput(filp);		return -EFAULT;	}	fd_install(resp.fd, filp);	return in_len;}ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,			    const char __user *buf, int in_len,			    int out_len){	struct ib_uverbs_create_cq      cmd;	struct ib_uverbs_create_cq_resp resp;	struct ib_udata                 udata;	struct ib_ucq_object           *obj;	struct ib_uverbs_event_file    *ev_file = NULL;	struct ib_cq                   *cq;	int                             ret;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	INIT_UDATA(&udata, buf + sizeof cmd,		   (unsigned long) cmd.response + sizeof resp,		   in_len - sizeof cmd, out_len - sizeof resp);	if (cmd.comp_vector >= file->device->num_comp_vectors)		return -EINVAL;	obj = kmalloc(sizeof *obj, GFP_KERNEL);	if (!obj)		return -ENOMEM;	init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);	down_write(&obj->uobject.mutex);	if (cmd.comp_channel >= 0) {		ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);		if (!ev_file) {			ret = -EINVAL;			goto err;		}	}	obj->uverbs_file	   = file;	obj->comp_events_reported  = 0;	obj->async_events_reported = 0;	INIT_LIST_HEAD(&obj->comp_list);	INIT_LIST_HEAD(&obj->async_list);	cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,					     cmd.comp_vector,					     file->ucontext, &udata);	if (IS_ERR(cq)) {		ret = PTR_ERR(cq);		goto err_file;	}	cq->device        = file->device->ib_dev;	cq->uobject       = &obj->uobject;	cq->comp_handler  = ib_uverbs_comp_handler;	cq->event_handler = ib_uverbs_cq_event_handler;	cq->cq_context    = ev_file;	atomic_set(&cq->usecnt, 0);	obj->uobject.object = cq;	ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);	if (ret)		goto err_free;	memset(&resp, 0, sizeof resp);	resp.cq_handle = obj->uobject.id;	resp.cqe       = cq->cqe;	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp)) {		ret = -EFAULT;		goto err_copy;	}	mutex_lock(&file->mutex);	list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);	mutex_unlock(&file->mutex);	obj->uobject.live = 1;	up_write(&obj->uobject.mutex);	return in_len;err_copy:	idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);err_free:	ib_destroy_cq(cq);err_file:	if (ev_file)		ib_uverbs_release_ucq(file, ev_file, obj);err:	put_uobj_write(&obj->uobject);	return ret;}ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,			    const char __user *buf, int in_len,			    int out_len){	struct ib_uverbs_resize_cq	cmd;	struct ib_uverbs_resize_cq_resp	resp;	struct ib_udata                 udata;	struct ib_cq			*cq;	int				ret = -EINVAL;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	INIT_UDATA(&udata, buf + sizeof cmd,		   (unsigned long) cmd.response + sizeof resp,		   in_len - sizeof cmd, out_len - sizeof resp);	cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);	if (!cq)		return -EINVAL;	ret = cq->device->resize_cq(cq, cmd.cqe, &udata);	if (ret)		goto out;	resp.cqe = cq->cqe;	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp.cqe))		ret = -EFAULT;out:	put_cq_read(cq);	return ret ? ret : in_len;}ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,			  const char __user *buf, int in_len,			  int out_len){	struct ib_uverbs_poll_cq       cmd;	struct ib_uverbs_poll_cq_resp *resp;	struct ib_cq                  *cq;	struct ib_wc                  *wc;	int                            ret = 0;	int                            i;	int                            rsize;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	wc = kmalloc(cmd.ne * sizeof *wc, GFP_KERNEL);	if (!wc)		return -ENOMEM;	rsize = sizeof *resp + cmd.ne * sizeof(struct ib_uverbs_wc);	resp = kmalloc(rsize, GFP_KERNEL);	if (!resp) {		ret = -ENOMEM;		goto out_wc;	}	cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);	if (!cq) {		ret = -EINVAL;		goto out;	}	resp->count = ib_poll_cq(cq, cmd.ne, wc);	put_cq_read(cq);	for (i = 0; i < resp->count; i++) {		resp->wc[i].wr_id 	   = wc[i].wr_id;		resp->wc[i].status 	   = wc[i].status;		resp->wc[i].opcode 	   = wc[i].opcode;		resp->wc[i].vendor_err 	   = wc[i].vendor_err;		resp->wc[i].byte_len 	   = wc[i].byte_len;		resp->wc[i].imm_data 	   = (__u32 __force) wc[i].imm_data;		resp->wc[i].qp_num 	   = wc[i].qp->qp_num;		resp->wc[i].src_qp 	   = wc[i].src_qp;		resp->wc[i].wc_flags 	   = wc[i].wc_flags;		resp->wc[i].pkey_index 	   = wc[i].pkey_index;		resp->wc[i].slid 	   = wc[i].slid;		resp->wc[i].sl 		   = wc[i].sl;		resp->wc[i].dlid_path_bits = wc[i].dlid_path_bits;		resp->wc[i].port_num 	   = wc[i].port_num;	}	if (copy_to_user((void __user *) (unsigned long) cmd.response, resp, rsize))		ret = -EFAULT;out:	kfree(resp);out_wc:	kfree(wc);	return ret ? ret : in_len;}ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,				const char __user *buf, int in_len,				int out_len){	struct ib_uverbs_req_notify_cq cmd;	struct ib_cq                  *cq;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);	if (!cq)		return -EINVAL;	ib_req_notify_cq(cq, cmd.solicited_only ?			 IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);	put_cq_read(cq);	return in_len;}ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,			     const char __user *buf, int in_len,			     int out_len){	struct ib_uverbs_destroy_cq      cmd;	struct ib_uverbs_destroy_cq_resp resp;	struct ib_uobject		*uobj;	struct ib_cq               	*cq;	struct ib_ucq_object        	*obj;	struct ib_uverbs_event_file	*ev_file;	int                        	 ret = -EINVAL;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);	if (!uobj)		return -EINVAL;	cq      = uobj->object;	ev_file = cq->cq_context;	obj     = container_of(cq->uobject, struct ib_ucq_object, uobject);	ret = ib_destroy_cq(cq);	if (!ret)		uobj->live = 0;	put_uobj_write(uobj);	if (ret)		return ret;	idr_remove_uobj(&ib_uverbs_cq_idr, uobj);	mutex_lock(&file->mutex);	list_del(&uobj->list);	mutex_unlock(&file->mutex);	ib_uverbs_release_ucq(file, ev_file, obj);	memset(&resp, 0, sizeof resp);	resp.comp_events_reported  = obj->comp_events_reported;	resp.async_events_reported = obj->async_events_reported;	put_uobj(uobj);	if (copy_to_user((void __user *) (unsigned long) cmd.response,			 &resp, sizeof resp))		return -EFAULT;	return in_len;}ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,			    const char __user *buf, int in_len,			    int out_len){	struct ib_uverbs_create_qp      cmd;	struct ib_uverbs_create_qp_resp resp;	struct ib_udata                 udata;	struct ib_uqp_object           *obj;	struct ib_pd                   *pd;	struct ib_cq                   *scq, *rcq;	struct ib_srq                  *srq;	struct ib_qp                   *qp;	struct ib_qp_init_attr          attr;	int ret;	if (out_len < sizeof resp)		return -ENOSPC;	if (copy_from_user(&cmd, buf, sizeof cmd))		return -EFAULT;	INIT_UDATA(&udata, buf + sizeof cmd,		   (unsigned long) cmd.response + sizeof resp,		   in_len - sizeof cmd, out_len - sizeof resp);	obj = kmalloc(sizeof *obj, GFP_KERNEL);	if (!obj)		return -ENOMEM;	init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);	down_write(&obj->uevent.uobject.mutex);	srq = cmd.is_srq ? idr_read_srq(cmd.srq_handle, file->ucontext) : 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 ?		scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);	if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {		ret = -EINVAL;		goto err_put;	}	attr.event_handler = ib_uverbs_qp_event_handler;	attr.qp_context    = file;	attr.send_cq       = scq;	attr.recv_cq       = rcq;	attr.srq           = srq;	attr.sq_sig_type   = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;	attr.qp_type       = cmd.qp_type;	attr.cap.max_send_wr     = cmd.max_send_wr;	attr.cap.max_recv_wr     = cmd.max_recv_wr;	attr.cap.max_send_sge    = cmd.max_send_sge;	attr.cap.max_recv_sge    = cmd.max_recv_sge;	attr.cap.max_inline_data = cmd.max_inline_data;	obj->uevent.events_reported     = 0;	INIT_LIST_HEAD(&obj->uevent.event_list);	INIT_LIST_HEAD(&obj->mcast_list);	qp = pd->device->create_qp(pd, &attr, &udata);	if (IS_ERR(qp)) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?