brintos

brintos / linux-shallow public Read only

0
0
Text · 34.2 KiB · 9445418 Raw
1314 lines · c
1/*2 * Copyright (c) 2005 Topspin Communications.  All rights reserved.3 * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.5 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.6 * Copyright (c) 2005 PathScale, Inc. All rights reserved.7 *8 * This software is available to you under a choice of one of two9 * licenses.  You may choose to be licensed under the terms of the GNU10 * General Public License (GPL) Version 2, available from the file11 * COPYING in the main directory of this source tree, or the12 * OpenIB.org BSD license below:13 *14 *     Redistribution and use in source and binary forms, with or15 *     without modification, are permitted provided that the following16 *     conditions are met:17 *18 *      - Redistributions of source code must retain the above19 *        copyright notice, this list of conditions and the following20 *        disclaimer.21 *22 *      - Redistributions in binary form must reproduce the above23 *        copyright notice, this list of conditions and the following24 *        disclaimer in the documentation and/or other materials25 *        provided with the distribution.26 *27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE34 * SOFTWARE.35 */36 37#include <linux/module.h>38#include <linux/init.h>39#include <linux/device.h>40#include <linux/err.h>41#include <linux/fs.h>42#include <linux/poll.h>43#include <linux/sched.h>44#include <linux/file.h>45#include <linux/cdev.h>46#include <linux/anon_inodes.h>47#include <linux/slab.h>48#include <linux/sched/mm.h>49 50#include <linux/uaccess.h>51 52#include <rdma/ib.h>53#include <rdma/uverbs_std_types.h>54#include <rdma/rdma_netlink.h>55 56#include "uverbs.h"57#include "core_priv.h"58#include "rdma_core.h"59 60MODULE_AUTHOR("Roland Dreier");61MODULE_DESCRIPTION("InfiniBand userspace verbs access");62MODULE_LICENSE("Dual BSD/GPL");63 64enum {65	IB_UVERBS_MAJOR       = 231,66	IB_UVERBS_BASE_MINOR  = 192,67	IB_UVERBS_MAX_DEVICES = RDMA_MAX_PORTS,68	IB_UVERBS_NUM_FIXED_MINOR = 32,69	IB_UVERBS_NUM_DYNAMIC_MINOR = IB_UVERBS_MAX_DEVICES - IB_UVERBS_NUM_FIXED_MINOR,70};71 72#define IB_UVERBS_BASE_DEV	MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)73 74static dev_t dynamic_uverbs_dev;75 76static DEFINE_IDA(uverbs_ida);77static int ib_uverbs_add_one(struct ib_device *device);78static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);79 80static char *uverbs_devnode(const struct device *dev, umode_t *mode)81{82	if (mode)83		*mode = 0666;84	return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));85}86 87static const struct class uverbs_class = {88	.name = "infiniband_verbs",89	.devnode = uverbs_devnode,90};91 92/*93 * Must be called with the ufile->device->disassociate_srcu held, and the lock94 * must be held until use of the ucontext is finished.95 */96struct ib_ucontext *ib_uverbs_get_ucontext_file(struct ib_uverbs_file *ufile)97{98	/*99	 * We do not hold the hw_destroy_rwsem lock for this flow, instead100	 * srcu is used. It does not matter if someone races this with101	 * get_context, we get NULL or valid ucontext.102	 */103	struct ib_ucontext *ucontext = smp_load_acquire(&ufile->ucontext);104 105	if (!srcu_dereference(ufile->device->ib_dev,106			      &ufile->device->disassociate_srcu))107		return ERR_PTR(-EIO);108 109	if (!ucontext)110		return ERR_PTR(-EINVAL);111 112	return ucontext;113}114EXPORT_SYMBOL(ib_uverbs_get_ucontext_file);115 116int uverbs_dealloc_mw(struct ib_mw *mw)117{118	struct ib_pd *pd = mw->pd;119	int ret;120 121	ret = mw->device->ops.dealloc_mw(mw);122	if (ret)123		return ret;124 125	atomic_dec(&pd->usecnt);126	kfree(mw);127	return ret;128}129 130static void ib_uverbs_release_dev(struct device *device)131{132	struct ib_uverbs_device *dev =133			container_of(device, struct ib_uverbs_device, dev);134 135	uverbs_destroy_api(dev->uapi);136	cleanup_srcu_struct(&dev->disassociate_srcu);137	mutex_destroy(&dev->lists_mutex);138	mutex_destroy(&dev->xrcd_tree_mutex);139	kfree(dev);140}141 142void ib_uverbs_release_ucq(struct ib_uverbs_completion_event_file *ev_file,143			   struct ib_ucq_object *uobj)144{145	struct ib_uverbs_event *evt, *tmp;146 147	if (ev_file) {148		spin_lock_irq(&ev_file->ev_queue.lock);149		list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {150			list_del(&evt->list);151			kfree(evt);152		}153		spin_unlock_irq(&ev_file->ev_queue.lock);154 155		uverbs_uobject_put(&ev_file->uobj);156	}157 158	ib_uverbs_release_uevent(&uobj->uevent);159}160 161void ib_uverbs_release_uevent(struct ib_uevent_object *uobj)162{163	struct ib_uverbs_async_event_file *async_file = uobj->event_file;164	struct ib_uverbs_event *evt, *tmp;165 166	if (!async_file)167		return;168 169	spin_lock_irq(&async_file->ev_queue.lock);170	list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {171		list_del(&evt->list);172		kfree(evt);173	}174	spin_unlock_irq(&async_file->ev_queue.lock);175	uverbs_uobject_put(&async_file->uobj);176}177 178void ib_uverbs_detach_umcast(struct ib_qp *qp,179			     struct ib_uqp_object *uobj)180{181	struct ib_uverbs_mcast_entry *mcast, *tmp;182 183	list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {184		ib_detach_mcast(qp, &mcast->gid, mcast->lid);185		list_del(&mcast->list);186		kfree(mcast);187	}188}189 190static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)191{192	complete(&dev->comp);193}194 195void ib_uverbs_release_file(struct kref *ref)196{197	struct ib_uverbs_file *file =198		container_of(ref, struct ib_uverbs_file, ref);199	struct ib_device *ib_dev;200	int srcu_key;201 202	release_ufile_idr_uobject(file);203 204	srcu_key = srcu_read_lock(&file->device->disassociate_srcu);205	ib_dev = srcu_dereference(file->device->ib_dev,206				  &file->device->disassociate_srcu);207	if (ib_dev && !ib_dev->ops.disassociate_ucontext)208		module_put(ib_dev->ops.owner);209	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);210 211	if (refcount_dec_and_test(&file->device->refcount))212		ib_uverbs_comp_dev(file->device);213 214	if (file->default_async_file)215		uverbs_uobject_put(&file->default_async_file->uobj);216	put_device(&file->device->dev);217 218	if (file->disassociate_page)219		__free_pages(file->disassociate_page, 0);220	mutex_destroy(&file->umap_lock);221	mutex_destroy(&file->ucontext_lock);222	kfree(file);223}224 225static ssize_t ib_uverbs_event_read(struct ib_uverbs_event_queue *ev_queue,226				    struct file *filp, char __user *buf,227				    size_t count, loff_t *pos,228				    size_t eventsz)229{230	struct ib_uverbs_event *event;231	int ret = 0;232 233	spin_lock_irq(&ev_queue->lock);234 235	while (list_empty(&ev_queue->event_list)) {236		if (ev_queue->is_closed) {237			spin_unlock_irq(&ev_queue->lock);238			return -EIO;239		}240 241		spin_unlock_irq(&ev_queue->lock);242		if (filp->f_flags & O_NONBLOCK)243			return -EAGAIN;244 245		if (wait_event_interruptible(ev_queue->poll_wait,246					     (!list_empty(&ev_queue->event_list) ||247					      ev_queue->is_closed)))248			return -ERESTARTSYS;249 250		spin_lock_irq(&ev_queue->lock);251	}252 253	event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);254 255	if (eventsz > count) {256		ret   = -EINVAL;257		event = NULL;258	} else {259		list_del(ev_queue->event_list.next);260		if (event->counter) {261			++(*event->counter);262			list_del(&event->obj_list);263		}264	}265 266	spin_unlock_irq(&ev_queue->lock);267 268	if (event) {269		if (copy_to_user(buf, event, eventsz))270			ret = -EFAULT;271		else272			ret = eventsz;273	}274 275	kfree(event);276 277	return ret;278}279 280static ssize_t ib_uverbs_async_event_read(struct file *filp, char __user *buf,281					  size_t count, loff_t *pos)282{283	struct ib_uverbs_async_event_file *file = filp->private_data;284 285	return ib_uverbs_event_read(&file->ev_queue, filp, buf, count, pos,286				    sizeof(struct ib_uverbs_async_event_desc));287}288 289static ssize_t ib_uverbs_comp_event_read(struct file *filp, char __user *buf,290					 size_t count, loff_t *pos)291{292	struct ib_uverbs_completion_event_file *comp_ev_file =293		filp->private_data;294 295	return ib_uverbs_event_read(&comp_ev_file->ev_queue, filp, buf, count,296				    pos,297				    sizeof(struct ib_uverbs_comp_event_desc));298}299 300static __poll_t ib_uverbs_event_poll(struct ib_uverbs_event_queue *ev_queue,301					 struct file *filp,302					 struct poll_table_struct *wait)303{304	__poll_t pollflags = 0;305 306	poll_wait(filp, &ev_queue->poll_wait, wait);307 308	spin_lock_irq(&ev_queue->lock);309	if (!list_empty(&ev_queue->event_list))310		pollflags = EPOLLIN | EPOLLRDNORM;311	else if (ev_queue->is_closed)312		pollflags = EPOLLERR;313	spin_unlock_irq(&ev_queue->lock);314 315	return pollflags;316}317 318static __poll_t ib_uverbs_async_event_poll(struct file *filp,319					       struct poll_table_struct *wait)320{321	struct ib_uverbs_async_event_file *file = filp->private_data;322 323	return ib_uverbs_event_poll(&file->ev_queue, filp, wait);324}325 326static __poll_t ib_uverbs_comp_event_poll(struct file *filp,327					      struct poll_table_struct *wait)328{329	struct ib_uverbs_completion_event_file *comp_ev_file =330		filp->private_data;331 332	return ib_uverbs_event_poll(&comp_ev_file->ev_queue, filp, wait);333}334 335static int ib_uverbs_async_event_fasync(int fd, struct file *filp, int on)336{337	struct ib_uverbs_async_event_file *file = filp->private_data;338 339	return fasync_helper(fd, filp, on, &file->ev_queue.async_queue);340}341 342static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on)343{344	struct ib_uverbs_completion_event_file *comp_ev_file =345		filp->private_data;346 347	return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);348}349 350const struct file_operations uverbs_event_fops = {351	.owner	 = THIS_MODULE,352	.read	 = ib_uverbs_comp_event_read,353	.poll    = ib_uverbs_comp_event_poll,354	.release = uverbs_uobject_fd_release,355	.fasync  = ib_uverbs_comp_event_fasync,356};357 358const struct file_operations uverbs_async_event_fops = {359	.owner	 = THIS_MODULE,360	.read	 = ib_uverbs_async_event_read,361	.poll    = ib_uverbs_async_event_poll,362	.release = uverbs_async_event_release,363	.fasync  = ib_uverbs_async_event_fasync,364};365 366void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)367{368	struct ib_uverbs_event_queue   *ev_queue = cq_context;369	struct ib_ucq_object	       *uobj;370	struct ib_uverbs_event	       *entry;371	unsigned long			flags;372 373	if (!ev_queue)374		return;375 376	spin_lock_irqsave(&ev_queue->lock, flags);377	if (ev_queue->is_closed) {378		spin_unlock_irqrestore(&ev_queue->lock, flags);379		return;380	}381 382	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);383	if (!entry) {384		spin_unlock_irqrestore(&ev_queue->lock, flags);385		return;386	}387 388	uobj = cq->uobject;389 390	entry->desc.comp.cq_handle = cq->uobject->uevent.uobject.user_handle;391	entry->counter		   = &uobj->comp_events_reported;392 393	list_add_tail(&entry->list, &ev_queue->event_list);394	list_add_tail(&entry->obj_list, &uobj->comp_list);395	spin_unlock_irqrestore(&ev_queue->lock, flags);396 397	wake_up_interruptible(&ev_queue->poll_wait);398	kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);399}400 401void ib_uverbs_async_handler(struct ib_uverbs_async_event_file *async_file,402			     __u64 element, __u64 event,403			     struct list_head *obj_list, u32 *counter)404{405	struct ib_uverbs_event *entry;406	unsigned long flags;407 408	if (!async_file)409		return;410 411	spin_lock_irqsave(&async_file->ev_queue.lock, flags);412	if (async_file->ev_queue.is_closed) {413		spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);414		return;415	}416 417	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);418	if (!entry) {419		spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);420		return;421	}422 423	entry->desc.async.element = element;424	entry->desc.async.event_type = event;425	entry->desc.async.reserved = 0;426	entry->counter = counter;427 428	list_add_tail(&entry->list, &async_file->ev_queue.event_list);429	if (obj_list)430		list_add_tail(&entry->obj_list, obj_list);431	spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);432 433	wake_up_interruptible(&async_file->ev_queue.poll_wait);434	kill_fasync(&async_file->ev_queue.async_queue, SIGIO, POLL_IN);435}436 437static void uverbs_uobj_event(struct ib_uevent_object *eobj,438			      struct ib_event *event)439{440	ib_uverbs_async_handler(eobj->event_file,441				eobj->uobject.user_handle, event->event,442				&eobj->event_list, &eobj->events_reported);443}444 445void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)446{447	uverbs_uobj_event(&event->element.cq->uobject->uevent, event);448}449 450void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)451{452	/* for XRC target qp's, check that qp is live */453	if (!event->element.qp->uobject)454		return;455 456	uverbs_uobj_event(&event->element.qp->uobject->uevent, event);457}458 459void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)460{461	uverbs_uobj_event(&event->element.wq->uobject->uevent, event);462}463 464void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)465{466	uverbs_uobj_event(&event->element.srq->uobject->uevent, event);467}468 469static void ib_uverbs_event_handler(struct ib_event_handler *handler,470				    struct ib_event *event)471{472	ib_uverbs_async_handler(473		container_of(handler, struct ib_uverbs_async_event_file,474			     event_handler),475		event->element.port_num, event->event, NULL, NULL);476}477 478void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)479{480	spin_lock_init(&ev_queue->lock);481	INIT_LIST_HEAD(&ev_queue->event_list);482	init_waitqueue_head(&ev_queue->poll_wait);483	ev_queue->is_closed   = 0;484	ev_queue->async_queue = NULL;485}486 487void ib_uverbs_init_async_event_file(488	struct ib_uverbs_async_event_file *async_file)489{490	struct ib_uverbs_file *uverbs_file = async_file->uobj.ufile;491	struct ib_device *ib_dev = async_file->uobj.context->device;492 493	ib_uverbs_init_event_queue(&async_file->ev_queue);494 495	/* The first async_event_file becomes the default one for the file. */496	mutex_lock(&uverbs_file->ucontext_lock);497	if (!uverbs_file->default_async_file) {498		/* Pairs with the put in ib_uverbs_release_file */499		uverbs_uobject_get(&async_file->uobj);500		smp_store_release(&uverbs_file->default_async_file, async_file);501	}502	mutex_unlock(&uverbs_file->ucontext_lock);503 504	INIT_IB_EVENT_HANDLER(&async_file->event_handler, ib_dev,505			      ib_uverbs_event_handler);506	ib_register_event_handler(&async_file->event_handler);507}508 509static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,510			  struct ib_uverbs_ex_cmd_hdr *ex_hdr, size_t count,511			  const struct uverbs_api_write_method *method_elm)512{513	if (method_elm->is_ex) {514		count -= sizeof(*hdr) + sizeof(*ex_hdr);515 516		if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)517			return -EINVAL;518 519		if (hdr->in_words * 8 < method_elm->req_size)520			return -ENOSPC;521 522		if (ex_hdr->cmd_hdr_reserved)523			return -EINVAL;524 525		if (ex_hdr->response) {526			if (!hdr->out_words && !ex_hdr->provider_out_words)527				return -EINVAL;528 529			if (hdr->out_words * 8 < method_elm->resp_size)530				return -ENOSPC;531 532			if (!access_ok(u64_to_user_ptr(ex_hdr->response),533				       (hdr->out_words + ex_hdr->provider_out_words) * 8))534				return -EFAULT;535		} else {536			if (hdr->out_words || ex_hdr->provider_out_words)537				return -EINVAL;538		}539 540		return 0;541	}542 543	/* not extended command */544	if (hdr->in_words * 4 != count)545		return -EINVAL;546 547	if (count < method_elm->req_size + sizeof(*hdr)) {548		/*549		 * rdma-core v18 and v19 have a bug where they send DESTROY_CQ550		 * with a 16 byte write instead of 24. Old kernels didn't551		 * check the size so they allowed this. Now that the size is552		 * checked provide a compatibility work around to not break553		 * those userspaces.554		 */555		if (hdr->command == IB_USER_VERBS_CMD_DESTROY_CQ &&556		    count == 16) {557			hdr->in_words = 6;558			return 0;559		}560		return -ENOSPC;561	}562	if (hdr->out_words * 4 < method_elm->resp_size)563		return -ENOSPC;564 565	return 0;566}567 568static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,569			     size_t count, loff_t *pos)570{571	struct ib_uverbs_file *file = filp->private_data;572	const struct uverbs_api_write_method *method_elm;573	struct uverbs_api *uapi = file->device->uapi;574	struct ib_uverbs_ex_cmd_hdr ex_hdr;575	struct ib_uverbs_cmd_hdr hdr;576	struct uverbs_attr_bundle bundle;577	int srcu_key;578	ssize_t ret;579 580	if (!ib_safe_file_access(filp)) {581		pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",582			    task_tgid_vnr(current), current->comm);583		return -EACCES;584	}585 586	if (count < sizeof(hdr))587		return -EINVAL;588 589	if (copy_from_user(&hdr, buf, sizeof(hdr)))590		return -EFAULT;591 592	method_elm = uapi_get_method(uapi, hdr.command);593	if (IS_ERR(method_elm))594		return PTR_ERR(method_elm);595 596	if (method_elm->is_ex) {597		if (count < (sizeof(hdr) + sizeof(ex_hdr)))598			return -EINVAL;599		if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))600			return -EFAULT;601	}602 603	ret = verify_hdr(&hdr, &ex_hdr, count, method_elm);604	if (ret)605		return ret;606 607	srcu_key = srcu_read_lock(&file->device->disassociate_srcu);608 609	buf += sizeof(hdr);610 611	memset(bundle.attr_present, 0, sizeof(bundle.attr_present));612	bundle.ufile = file;613	bundle.context = NULL; /* only valid if bundle has uobject */614	bundle.uobject = NULL;615	if (!method_elm->is_ex) {616		size_t in_len = hdr.in_words * 4 - sizeof(hdr);617		size_t out_len = hdr.out_words * 4;618		u64 response = 0;619 620		if (method_elm->has_udata) {621			bundle.driver_udata.inlen =622				in_len - method_elm->req_size;623			in_len = method_elm->req_size;624			if (bundle.driver_udata.inlen)625				bundle.driver_udata.inbuf = buf + in_len;626			else627				bundle.driver_udata.inbuf = NULL;628		} else {629			memset(&bundle.driver_udata, 0,630			       sizeof(bundle.driver_udata));631		}632 633		if (method_elm->has_resp) {634			/*635			 * The macros check that if has_resp is set636			 * then the command request structure starts637			 * with a '__aligned u64 response' member.638			 */639			ret = get_user(response, (const u64 __user *)buf);640			if (ret)641				goto out_unlock;642 643			if (method_elm->has_udata) {644				bundle.driver_udata.outlen =645					out_len - method_elm->resp_size;646				out_len = method_elm->resp_size;647				if (bundle.driver_udata.outlen)648					bundle.driver_udata.outbuf =649						u64_to_user_ptr(response +650								out_len);651				else652					bundle.driver_udata.outbuf = NULL;653			}654		} else {655			bundle.driver_udata.outlen = 0;656			bundle.driver_udata.outbuf = NULL;657		}658 659		ib_uverbs_init_udata_buf_or_null(660			&bundle.ucore, buf, u64_to_user_ptr(response),661			in_len, out_len);662	} else {663		buf += sizeof(ex_hdr);664 665		ib_uverbs_init_udata_buf_or_null(&bundle.ucore, buf,666					u64_to_user_ptr(ex_hdr.response),667					hdr.in_words * 8, hdr.out_words * 8);668 669		ib_uverbs_init_udata_buf_or_null(670			&bundle.driver_udata, buf + bundle.ucore.inlen,671			u64_to_user_ptr(ex_hdr.response) + bundle.ucore.outlen,672			ex_hdr.provider_in_words * 8,673			ex_hdr.provider_out_words * 8);674 675	}676 677	ret = method_elm->handler(&bundle);678	if (bundle.uobject)679		uverbs_finalize_object(bundle.uobject, UVERBS_ACCESS_NEW, true,680				       !ret, &bundle);681out_unlock:682	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);683	return (ret) ? : count;684}685 686static const struct vm_operations_struct rdma_umap_ops;687 688static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)689{690	struct ib_uverbs_file *file = filp->private_data;691	struct ib_ucontext *ucontext;692	int ret = 0;693	int srcu_key;694 695	srcu_key = srcu_read_lock(&file->device->disassociate_srcu);696	ucontext = ib_uverbs_get_ucontext_file(file);697	if (IS_ERR(ucontext)) {698		ret = PTR_ERR(ucontext);699		goto out;700	}701	vma->vm_ops = &rdma_umap_ops;702	ret = ucontext->device->ops.mmap(ucontext, vma);703out:704	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);705	return ret;706}707 708/*709 * The VMA has been dup'd, initialize the vm_private_data with a new tracking710 * struct711 */712static void rdma_umap_open(struct vm_area_struct *vma)713{714	struct ib_uverbs_file *ufile = vma->vm_file->private_data;715	struct rdma_umap_priv *opriv = vma->vm_private_data;716	struct rdma_umap_priv *priv;717 718	if (!opriv)719		return;720 721	/* We are racing with disassociation */722	if (!down_read_trylock(&ufile->hw_destroy_rwsem))723		goto out_zap;724	/*725	 * Disassociation already completed, the VMA should already be zapped.726	 */727	if (!ufile->ucontext)728		goto out_unlock;729 730	priv = kzalloc(sizeof(*priv), GFP_KERNEL);731	if (!priv)732		goto out_unlock;733	rdma_umap_priv_init(priv, vma, opriv->entry);734 735	up_read(&ufile->hw_destroy_rwsem);736	return;737 738out_unlock:739	up_read(&ufile->hw_destroy_rwsem);740out_zap:741	/*742	 * We can't allow the VMA to be created with the actual IO pages, that743	 * would break our API contract, and it can't be stopped at this744	 * point, so zap it.745	 */746	vma->vm_private_data = NULL;747	zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);748}749 750static void rdma_umap_close(struct vm_area_struct *vma)751{752	struct ib_uverbs_file *ufile = vma->vm_file->private_data;753	struct rdma_umap_priv *priv = vma->vm_private_data;754 755	if (!priv)756		return;757 758	/*759	 * The vma holds a reference on the struct file that created it, which760	 * in turn means that the ib_uverbs_file is guaranteed to exist at761	 * this point.762	 */763	mutex_lock(&ufile->umap_lock);764	if (priv->entry)765		rdma_user_mmap_entry_put(priv->entry);766 767	list_del(&priv->list);768	mutex_unlock(&ufile->umap_lock);769	kfree(priv);770}771 772/*773 * Once the zap_vma_ptes has been called touches to the VMA will come here and774 * we return a dummy writable zero page for all the pfns.775 */776static vm_fault_t rdma_umap_fault(struct vm_fault *vmf)777{778	struct ib_uverbs_file *ufile = vmf->vma->vm_file->private_data;779	struct rdma_umap_priv *priv = vmf->vma->vm_private_data;780	vm_fault_t ret = 0;781 782	if (!priv)783		return VM_FAULT_SIGBUS;784 785	/* Read only pages can just use the system zero page. */786	if (!(vmf->vma->vm_flags & (VM_WRITE | VM_MAYWRITE))) {787		vmf->page = ZERO_PAGE(vmf->address);788		get_page(vmf->page);789		return 0;790	}791 792	mutex_lock(&ufile->umap_lock);793	if (!ufile->disassociate_page)794		ufile->disassociate_page =795			alloc_pages(vmf->gfp_mask | __GFP_ZERO, 0);796 797	if (ufile->disassociate_page) {798		/*799		 * This VMA is forced to always be shared so this doesn't have800		 * to worry about COW.801		 */802		vmf->page = ufile->disassociate_page;803		get_page(vmf->page);804	} else {805		ret = VM_FAULT_SIGBUS;806	}807	mutex_unlock(&ufile->umap_lock);808 809	return ret;810}811 812static const struct vm_operations_struct rdma_umap_ops = {813	.open = rdma_umap_open,814	.close = rdma_umap_close,815	.fault = rdma_umap_fault,816};817 818void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)819{820	struct rdma_umap_priv *priv, *next_priv;821 822	lockdep_assert_held(&ufile->hw_destroy_rwsem);823 824	while (1) {825		struct mm_struct *mm = NULL;826 827		/* Get an arbitrary mm pointer that hasn't been cleaned yet */828		mutex_lock(&ufile->umap_lock);829		while (!list_empty(&ufile->umaps)) {830			int ret;831 832			priv = list_first_entry(&ufile->umaps,833						struct rdma_umap_priv, list);834			mm = priv->vma->vm_mm;835			ret = mmget_not_zero(mm);836			if (!ret) {837				list_del_init(&priv->list);838				if (priv->entry) {839					rdma_user_mmap_entry_put(priv->entry);840					priv->entry = NULL;841				}842				mm = NULL;843				continue;844			}845			break;846		}847		mutex_unlock(&ufile->umap_lock);848		if (!mm)849			return;850 851		/*852		 * The umap_lock is nested under mmap_lock since it used within853		 * the vma_ops callbacks, so we have to clean the list one mm854		 * at a time to get the lock ordering right. Typically there855		 * will only be one mm, so no big deal.856		 */857		mmap_read_lock(mm);858		mutex_lock(&ufile->umap_lock);859		list_for_each_entry_safe (priv, next_priv, &ufile->umaps,860					  list) {861			struct vm_area_struct *vma = priv->vma;862 863			if (vma->vm_mm != mm)864				continue;865			list_del_init(&priv->list);866 867			zap_vma_ptes(vma, vma->vm_start,868				     vma->vm_end - vma->vm_start);869 870			if (priv->entry) {871				rdma_user_mmap_entry_put(priv->entry);872				priv->entry = NULL;873			}874		}875		mutex_unlock(&ufile->umap_lock);876		mmap_read_unlock(mm);877		mmput(mm);878	}879}880 881/*882 * ib_uverbs_open() does not need the BKL:883 *884 *  - the ib_uverbs_device structures are properly reference counted and885 *    everything else is purely local to the file being created, so886 *    races against other open calls are not a problem;887 *  - there is no ioctl method to race against;888 *  - the open method will either immediately run -ENXIO, or all889 *    required initialization will be done.890 */891static int ib_uverbs_open(struct inode *inode, struct file *filp)892{893	struct ib_uverbs_device *dev;894	struct ib_uverbs_file *file;895	struct ib_device *ib_dev;896	int ret;897	int module_dependent;898	int srcu_key;899 900	dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);901	if (!refcount_inc_not_zero(&dev->refcount))902		return -ENXIO;903 904	get_device(&dev->dev);905	srcu_key = srcu_read_lock(&dev->disassociate_srcu);906	mutex_lock(&dev->lists_mutex);907	ib_dev = srcu_dereference(dev->ib_dev,908				  &dev->disassociate_srcu);909	if (!ib_dev) {910		ret = -EIO;911		goto err;912	}913 914	if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) {915		ret = -EPERM;916		goto err;917	}918 919	/* In case IB device supports disassociate ucontext, there is no hard920	 * dependency between uverbs device and its low level device.921	 */922	module_dependent = !(ib_dev->ops.disassociate_ucontext);923 924	if (module_dependent) {925		if (!try_module_get(ib_dev->ops.owner)) {926			ret = -ENODEV;927			goto err;928		}929	}930 931	file = kzalloc(sizeof(*file), GFP_KERNEL);932	if (!file) {933		ret = -ENOMEM;934		if (module_dependent)935			goto err_module;936 937		goto err;938	}939 940	file->device	 = dev;941	kref_init(&file->ref);942	mutex_init(&file->ucontext_lock);943 944	spin_lock_init(&file->uobjects_lock);945	INIT_LIST_HEAD(&file->uobjects);946	init_rwsem(&file->hw_destroy_rwsem);947	mutex_init(&file->umap_lock);948	INIT_LIST_HEAD(&file->umaps);949 950	filp->private_data = file;951	list_add_tail(&file->list, &dev->uverbs_file_list);952	mutex_unlock(&dev->lists_mutex);953	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);954 955	setup_ufile_idr_uobject(file);956 957	return stream_open(inode, filp);958 959err_module:960	module_put(ib_dev->ops.owner);961 962err:963	mutex_unlock(&dev->lists_mutex);964	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);965	if (refcount_dec_and_test(&dev->refcount))966		ib_uverbs_comp_dev(dev);967 968	put_device(&dev->dev);969	return ret;970}971 972static int ib_uverbs_close(struct inode *inode, struct file *filp)973{974	struct ib_uverbs_file *file = filp->private_data;975 976	uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);977 978	mutex_lock(&file->device->lists_mutex);979	list_del_init(&file->list);980	mutex_unlock(&file->device->lists_mutex);981 982	kref_put(&file->ref, ib_uverbs_release_file);983 984	return 0;985}986 987static const struct file_operations uverbs_fops = {988	.owner	 = THIS_MODULE,989	.write	 = ib_uverbs_write,990	.open	 = ib_uverbs_open,991	.release = ib_uverbs_close,992	.unlocked_ioctl = ib_uverbs_ioctl,993	.compat_ioctl = compat_ptr_ioctl,994};995 996static const struct file_operations uverbs_mmap_fops = {997	.owner	 = THIS_MODULE,998	.write	 = ib_uverbs_write,999	.mmap    = ib_uverbs_mmap,1000	.open	 = ib_uverbs_open,1001	.release = ib_uverbs_close,1002	.unlocked_ioctl = ib_uverbs_ioctl,1003	.compat_ioctl = compat_ptr_ioctl,1004};1005 1006static int ib_uverbs_get_nl_info(struct ib_device *ibdev, void *client_data,1007				 struct ib_client_nl_info *res)1008{1009	struct ib_uverbs_device *uverbs_dev = client_data;1010	int ret;1011 1012	if (res->port != -1)1013		return -EINVAL;1014 1015	res->abi = ibdev->ops.uverbs_abi_ver;1016	res->cdev = &uverbs_dev->dev;1017 1018	/*1019	 * To support DRIVER_ID binding in userspace some of the driver need1020	 * upgrading to expose their PCI dependent revision information1021	 * through get_context instead of relying on modalias matching. When1022	 * the drivers are fixed they can drop this flag.1023	 */1024	if (!ibdev->ops.uverbs_no_driver_id_binding) {1025		ret = nla_put_u32(res->nl_msg, RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID,1026				  ibdev->ops.driver_id);1027		if (ret)1028			return ret;1029	}1030	return 0;1031}1032 1033static struct ib_client uverbs_client = {1034	.name   = "uverbs",1035	.no_kverbs_req = true,1036	.add    = ib_uverbs_add_one,1037	.remove = ib_uverbs_remove_one,1038	.get_nl_info = ib_uverbs_get_nl_info,1039};1040MODULE_ALIAS_RDMA_CLIENT("uverbs");1041 1042static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,1043			  char *buf)1044{1045	struct ib_uverbs_device *dev =1046			container_of(device, struct ib_uverbs_device, dev);1047	int ret = -ENODEV;1048	int srcu_key;1049	struct ib_device *ib_dev;1050 1051	srcu_key = srcu_read_lock(&dev->disassociate_srcu);1052	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);1053	if (ib_dev)1054		ret = sysfs_emit(buf, "%s\n", dev_name(&ib_dev->dev));1055	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);1056 1057	return ret;1058}1059static DEVICE_ATTR_RO(ibdev);1060 1061static ssize_t abi_version_show(struct device *device,1062				struct device_attribute *attr, char *buf)1063{1064	struct ib_uverbs_device *dev =1065			container_of(device, struct ib_uverbs_device, dev);1066	int ret = -ENODEV;1067	int srcu_key;1068	struct ib_device *ib_dev;1069 1070	srcu_key = srcu_read_lock(&dev->disassociate_srcu);1071	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);1072	if (ib_dev)1073		ret = sysfs_emit(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);1074	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);1075 1076	return ret;1077}1078static DEVICE_ATTR_RO(abi_version);1079 1080static struct attribute *ib_dev_attrs[] = {1081	&dev_attr_abi_version.attr,1082	&dev_attr_ibdev.attr,1083	NULL,1084};1085 1086static const struct attribute_group dev_attr_group = {1087	.attrs = ib_dev_attrs,1088};1089 1090static CLASS_ATTR_STRING(abi_version, S_IRUGO,1091			 __stringify(IB_USER_VERBS_ABI_VERSION));1092 1093static int ib_uverbs_create_uapi(struct ib_device *device,1094				 struct ib_uverbs_device *uverbs_dev)1095{1096	struct uverbs_api *uapi;1097 1098	uapi = uverbs_alloc_api(device);1099	if (IS_ERR(uapi))1100		return PTR_ERR(uapi);1101 1102	uverbs_dev->uapi = uapi;1103	return 0;1104}1105 1106static int ib_uverbs_add_one(struct ib_device *device)1107{1108	int devnum;1109	dev_t base;1110	struct ib_uverbs_device *uverbs_dev;1111	int ret;1112 1113	if (!device->ops.alloc_ucontext ||1114	    device->type == RDMA_DEVICE_TYPE_SMI)1115		return -EOPNOTSUPP;1116 1117	uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);1118	if (!uverbs_dev)1119		return -ENOMEM;1120 1121	ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);1122	if (ret) {1123		kfree(uverbs_dev);1124		return -ENOMEM;1125	}1126 1127	device_initialize(&uverbs_dev->dev);1128	uverbs_dev->dev.class = &uverbs_class;1129	uverbs_dev->dev.parent = device->dev.parent;1130	uverbs_dev->dev.release = ib_uverbs_release_dev;1131	uverbs_dev->groups[0] = &dev_attr_group;1132	uverbs_dev->dev.groups = uverbs_dev->groups;1133	refcount_set(&uverbs_dev->refcount, 1);1134	init_completion(&uverbs_dev->comp);1135	uverbs_dev->xrcd_tree = RB_ROOT;1136	mutex_init(&uverbs_dev->xrcd_tree_mutex);1137	mutex_init(&uverbs_dev->lists_mutex);1138	INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);1139	rcu_assign_pointer(uverbs_dev->ib_dev, device);1140	uverbs_dev->num_comp_vectors = device->num_comp_vectors;1141 1142	devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,1143			       GFP_KERNEL);1144	if (devnum < 0) {1145		ret = -ENOMEM;1146		goto err;1147	}1148	uverbs_dev->devnum = devnum;1149	if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)1150		base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;1151	else1152		base = IB_UVERBS_BASE_DEV + devnum;1153 1154	ret = ib_uverbs_create_uapi(device, uverbs_dev);1155	if (ret)1156		goto err_uapi;1157 1158	uverbs_dev->dev.devt = base;1159	dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);1160 1161	cdev_init(&uverbs_dev->cdev,1162		  device->ops.mmap ? &uverbs_mmap_fops : &uverbs_fops);1163	uverbs_dev->cdev.owner = THIS_MODULE;1164 1165	ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);1166	if (ret)1167		goto err_uapi;1168 1169	ib_set_client_data(device, &uverbs_client, uverbs_dev);1170	return 0;1171 1172err_uapi:1173	ida_free(&uverbs_ida, devnum);1174err:1175	if (refcount_dec_and_test(&uverbs_dev->refcount))1176		ib_uverbs_comp_dev(uverbs_dev);1177	wait_for_completion(&uverbs_dev->comp);1178	put_device(&uverbs_dev->dev);1179	return ret;1180}1181 1182static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,1183					struct ib_device *ib_dev)1184{1185	struct ib_uverbs_file *file;1186 1187	/* Pending running commands to terminate */1188	uverbs_disassociate_api_pre(uverbs_dev);1189 1190	mutex_lock(&uverbs_dev->lists_mutex);1191	while (!list_empty(&uverbs_dev->uverbs_file_list)) {1192		file = list_first_entry(&uverbs_dev->uverbs_file_list,1193					struct ib_uverbs_file, list);1194		list_del_init(&file->list);1195		kref_get(&file->ref);1196 1197		/* We must release the mutex before going ahead and calling1198		 * uverbs_cleanup_ufile, as it might end up indirectly calling1199		 * uverbs_close, for example due to freeing the resources (e.g1200		 * mmput).1201		 */1202		mutex_unlock(&uverbs_dev->lists_mutex);1203 1204		uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);1205		kref_put(&file->ref, ib_uverbs_release_file);1206 1207		mutex_lock(&uverbs_dev->lists_mutex);1208	}1209	mutex_unlock(&uverbs_dev->lists_mutex);1210 1211	uverbs_disassociate_api(uverbs_dev->uapi);1212}1213 1214static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)1215{1216	struct ib_uverbs_device *uverbs_dev = client_data;1217	int wait_clients = 1;1218 1219	cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);1220	ida_free(&uverbs_ida, uverbs_dev->devnum);1221 1222	if (device->ops.disassociate_ucontext) {1223		/* We disassociate HW resources and immediately return.1224		 * Userspace will see a EIO errno for all future access.1225		 * Upon returning, ib_device may be freed internally and is not1226		 * valid any more.1227		 * uverbs_device is still available until all clients close1228		 * their files, then the uverbs device ref count will be zero1229		 * and its resources will be freed.1230		 * Note: At this point no more files can be opened since the1231		 * cdev was deleted, however active clients can still issue1232		 * commands and close their open files.1233		 */1234		ib_uverbs_free_hw_resources(uverbs_dev, device);1235		wait_clients = 0;1236	}1237 1238	if (refcount_dec_and_test(&uverbs_dev->refcount))1239		ib_uverbs_comp_dev(uverbs_dev);1240	if (wait_clients)1241		wait_for_completion(&uverbs_dev->comp);1242 1243	put_device(&uverbs_dev->dev);1244}1245 1246static int __init ib_uverbs_init(void)1247{1248	int ret;1249 1250	ret = register_chrdev_region(IB_UVERBS_BASE_DEV,1251				     IB_UVERBS_NUM_FIXED_MINOR,1252				     "infiniband_verbs");1253	if (ret) {1254		pr_err("user_verbs: couldn't register device number\n");1255		goto out;1256	}1257 1258	ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,1259				  IB_UVERBS_NUM_DYNAMIC_MINOR,1260				  "infiniband_verbs");1261	if (ret) {1262		pr_err("couldn't register dynamic device number\n");1263		goto out_alloc;1264	}1265 1266	ret = class_register(&uverbs_class);1267	if (ret) {1268		pr_err("user_verbs: couldn't create class infiniband_verbs\n");1269		goto out_chrdev;1270	}1271 1272	ret = class_create_file(&uverbs_class, &class_attr_abi_version.attr);1273	if (ret) {1274		pr_err("user_verbs: couldn't create abi_version attribute\n");1275		goto out_class;1276	}1277 1278	ret = ib_register_client(&uverbs_client);1279	if (ret) {1280		pr_err("user_verbs: couldn't register client\n");1281		goto out_class;1282	}1283 1284	return 0;1285 1286out_class:1287	class_unregister(&uverbs_class);1288 1289out_chrdev:1290	unregister_chrdev_region(dynamic_uverbs_dev,1291				 IB_UVERBS_NUM_DYNAMIC_MINOR);1292 1293out_alloc:1294	unregister_chrdev_region(IB_UVERBS_BASE_DEV,1295				 IB_UVERBS_NUM_FIXED_MINOR);1296 1297out:1298	return ret;1299}1300 1301static void __exit ib_uverbs_cleanup(void)1302{1303	ib_unregister_client(&uverbs_client);1304	class_unregister(&uverbs_class);1305	unregister_chrdev_region(IB_UVERBS_BASE_DEV,1306				 IB_UVERBS_NUM_FIXED_MINOR);1307	unregister_chrdev_region(dynamic_uverbs_dev,1308				 IB_UVERBS_NUM_DYNAMIC_MINOR);1309	mmu_notifier_synchronize();1310}1311 1312module_init(ib_uverbs_init);1313module_exit(ib_uverbs_cleanup);1314