rdma_core.c 27.9 KB
Newer Older
M
Matan Barak 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright (c) 2016, Mellanox Technologies inc.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/file.h>
#include <linux/anon_inodes.h>
35
#include <linux/sched/mm.h>
M
Matan Barak 已提交
36 37 38
#include <rdma/ib_verbs.h>
#include <rdma/uverbs_types.h>
#include <linux/rcupdate.h>
39
#include <rdma/uverbs_ioctl.h>
M
Matan Barak 已提交
40
#include <rdma/rdma_user_ioctl.h>
M
Matan Barak 已提交
41 42 43 44
#include "uverbs.h"
#include "core_priv.h"
#include "rdma_core.h"

M
Matan Barak 已提交
45 46 47 48 49 50 51 52 53 54 55
int uverbs_ns_idx(u16 *id, unsigned int ns_count)
{
	int ret = (*id & UVERBS_ID_NS_MASK) >> UVERBS_ID_NS_SHIFT;

	if (ret >= ns_count)
		return -EINVAL;

	*id &= ~UVERBS_ID_NS_MASK;
	return ret;
}

56
const struct uverbs_object_spec *uverbs_get_object(struct ib_uverbs_file *ufile,
M
Matan Barak 已提交
57 58
						   uint16_t object)
{
59
	const struct uverbs_root_spec *object_hash = ufile->device->specs_root;
M
Matan Barak 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	const struct uverbs_object_spec_hash *objects;
	int ret = uverbs_ns_idx(&object, object_hash->num_buckets);

	if (ret < 0)
		return NULL;

	objects = object_hash->object_buckets[ret];

	if (object >= objects->num_objects)
		return NULL;

	return objects->objects[object];
}

const struct uverbs_method_spec *uverbs_get_method(const struct uverbs_object_spec *object,
						   uint16_t method)
{
	const struct uverbs_method_spec_hash *methods;
	int ret = uverbs_ns_idx(&method, object->num_buckets);

	if (ret < 0)
		return NULL;

	methods = object->method_buckets[ret];
	if (method >= methods->num_methods)
		return NULL;

	return methods->methods[method];
}

M
Matan Barak 已提交
90 91 92 93 94
void uverbs_uobject_get(struct ib_uobject *uobject)
{
	kref_get(&uobject->ref);
}

95
static void uverbs_uobject_free(struct kref *ref)
M
Matan Barak 已提交
96 97 98 99 100 101 102 103 104 105 106 107
{
	struct ib_uobject *uobj =
		container_of(ref, struct ib_uobject, ref);

	if (uobj->type->type_class->needs_kfree_rcu)
		kfree_rcu(uobj, rcu);
	else
		kfree(uobj);
}

void uverbs_uobject_put(struct ib_uobject *uobject)
{
108
	kref_put(&uobject->ref, uverbs_uobject_free);
M
Matan Barak 已提交
109 110
}

111 112
static int uverbs_try_lock_object(struct ib_uobject *uobj,
				  enum rdma_lookup_mode mode)
M
Matan Barak 已提交
113 114
{
	/*
115 116 117 118 119 120 121 122 123
	 * When a shared access is required, we use a positive counter. Each
	 * shared access request checks that the value != -1 and increment it.
	 * Exclusive access is required for operations like write or destroy.
	 * In exclusive access mode, we check that the counter is zero (nobody
	 * claimed this object) and we set it to -1. Releasing a shared access
	 * lock is done simply by decreasing the counter. As for exclusive
	 * access locks, since only a single one of them is is allowed
	 * concurrently, setting the counter to zero is enough for releasing
	 * this lock.
M
Matan Barak 已提交
124
	 */
125 126
	switch (mode) {
	case UVERBS_LOOKUP_READ:
M
Matan Barak 已提交
127 128
		return __atomic_add_unless(&uobj->usecnt, 1, -1) == -1 ?
			-EBUSY : 0;
129
	case UVERBS_LOOKUP_WRITE:
130
		/* lock is exclusive */
131
		return atomic_cmpxchg(&uobj->usecnt, 0, -1) == 0 ? 0 : -EBUSY;
132 133
	case UVERBS_LOOKUP_DESTROY:
		return 0;
134 135
	}
	return 0;
M
Matan Barak 已提交
136 137
}

138 139
static void assert_uverbs_usecnt(struct ib_uobject *uobj,
				 enum rdma_lookup_mode mode)
140 141
{
#ifdef CONFIG_LOCKDEP
142 143
	switch (mode) {
	case UVERBS_LOOKUP_READ:
144
		WARN_ON(atomic_read(&uobj->usecnt) <= 0);
145 146 147 148
		break;
	case UVERBS_LOOKUP_WRITE:
		WARN_ON(atomic_read(&uobj->usecnt) != -1);
		break;
149 150
	case UVERBS_LOOKUP_DESTROY:
		break;
151
	}
152 153 154 155
#endif
}

/*
156 157
 * This must be called with the hw_destroy_rwsem locked for read or write,
 * also the uobject itself must be locked for write.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
 *
 * Upon return the HW object is guaranteed to be destroyed.
 *
 * For RDMA_REMOVE_ABORT, the hw_destroy_rwsem is not required to be held,
 * however the type's allocat_commit function cannot have been called and the
 * uobject cannot be on the uobjects_lists
 *
 * For RDMA_REMOVE_DESTROY the caller shold be holding a kref (eg via
 * rdma_lookup_get_uobject) and the object is left in a state where the caller
 * needs to call rdma_lookup_put_uobject.
 *
 * For all other destroy modes this function internally unlocks the uobject
 * and consumes the kref on the uobj.
 */
static int uverbs_destroy_uobject(struct ib_uobject *uobj,
				  enum rdma_remove_reason reason)
{
	struct ib_uverbs_file *ufile = uobj->ufile;
	unsigned long flags;
	int ret;

179
	lockdep_assert_held(&ufile->hw_destroy_rwsem);
180
	assert_uverbs_usecnt(uobj, UVERBS_LOOKUP_WRITE);
181 182

	if (uobj->object) {
183
		ret = uobj->type->type_class->destroy_hw(uobj, reason);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
		if (ret) {
			if (ib_is_destroy_retryable(ret, reason, uobj))
				return ret;

			/* Nothing to be done, dangle the memory and move on */
			WARN(true,
			     "ib_uverbs: failed to remove uobject id %d, driver err=%d",
			     uobj->id, ret);
		}

		uobj->object = NULL;
	}

	if (reason == RDMA_REMOVE_ABORT) {
		WARN_ON(!list_empty(&uobj->list));
		WARN_ON(!uobj->context);
		uobj->type->type_class->alloc_abort(uobj);
	}

	uobj->context = NULL;

	/*
	 * For DESTROY the usecnt is held write locked, the caller is expected
207 208
	 * to put it unlock and put the object when done with it. Only DESTROY
	 * can remove the IDR handle.
209 210 211
	 */
	if (reason != RDMA_REMOVE_DESTROY)
		atomic_set(&uobj->usecnt, 0);
212 213
	else
		uobj->type->type_class->remove_handle(uobj);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

	if (!list_empty(&uobj->list)) {
		spin_lock_irqsave(&ufile->uobjects_lock, flags);
		list_del_init(&uobj->list);
		spin_unlock_irqrestore(&ufile->uobjects_lock, flags);

		/*
		 * Pairs with the get in rdma_alloc_commit_uobject(), could
		 * destroy uobj.
		 */
		uverbs_uobject_put(uobj);
	}

	/*
	 * When aborting the stack kref remains owned by the core code, and is
	 * not transferred into the type. Pairs with the get in alloc_uobj
	 */
	if (reason == RDMA_REMOVE_ABORT)
		uverbs_uobject_put(uobj);

	return 0;
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
/*
 * This calls uverbs_destroy_uobject() using the RDMA_REMOVE_DESTROY
 * sequence. It should only be used from command callbacks. On success the
 * caller must pair this with rdma_lookup_put_uobject(LOOKUP_WRITE). This
 * version requires the caller to have already obtained an
 * LOOKUP_DESTROY uobject kref.
 */
int uobj_destroy(struct ib_uobject *uobj)
{
	struct ib_uverbs_file *ufile = uobj->ufile;
	int ret;

	down_read(&ufile->hw_destroy_rwsem);

	ret = uverbs_try_lock_object(uobj, UVERBS_LOOKUP_WRITE);
	if (ret)
		goto out_unlock;

	ret = uverbs_destroy_uobject(uobj, RDMA_REMOVE_DESTROY);
	if (ret) {
		atomic_set(&uobj->usecnt, 0);
		goto out_unlock;
	}

out_unlock:
	up_read(&ufile->hw_destroy_rwsem);
	return ret;
}

266
/*
267 268 269
 * uobj_get_destroy destroys the HW object and returns a handle to the uobj
 * with a NULL object pointer. The caller must pair this with
 * uverbs_put_destroy.
270
 */
271 272
struct ib_uobject *__uobj_get_destroy(const struct uverbs_obj_type *type,
				      u32 id, struct ib_uverbs_file *ufile)
273 274 275 276
{
	struct ib_uobject *uobj;
	int ret;

277
	uobj = rdma_lookup_get_uobject(type, ufile, id, UVERBS_LOOKUP_DESTROY);
278
	if (IS_ERR(uobj))
279
		return uobj;
280

281
	ret = uobj_destroy(uobj);
282
	if (ret) {
283
		rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
284 285
		return ERR_PTR(ret);
	}
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
	return uobj;
}

/*
 * Does both uobj_get_destroy() and uobj_put_destroy().  Returns success_res
 * on success (negative errno on failure). For use by callers that do not need
 * the uobj.
 */
int __uobj_perform_destroy(const struct uverbs_obj_type *type, u32 id,
			   struct ib_uverbs_file *ufile, int success_res)
{
	struct ib_uobject *uobj;

	uobj = __uobj_get_destroy(type, id, ufile);
	if (IS_ERR(uobj))
		return PTR_ERR(uobj);

304 305 306 307 308
	/*
	 * FIXME: After destroy this is not safe. We no longer hold the rwsem
	 * so disassociation could have completed and unloaded the module that
	 * backs the uobj->type pointer.
	 */
309
	rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
310 311 312
	return success_res;
}

313
/* alloc_uobj must be undone by uverbs_destroy_uobject() */
314
static struct ib_uobject *alloc_uobj(struct ib_uverbs_file *ufile,
M
Matan Barak 已提交
315 316
				     const struct uverbs_obj_type *type)
{
317 318 319 320 321 322
	struct ib_uobject *uobj;
	struct ib_ucontext *ucontext;

	ucontext = ib_uverbs_get_ucontext(ufile);
	if (IS_ERR(ucontext))
		return ERR_CAST(ucontext);
M
Matan Barak 已提交
323

324
	uobj = kzalloc(type->obj_size, GFP_KERNEL);
M
Matan Barak 已提交
325 326 327 328 329 330
	if (!uobj)
		return ERR_PTR(-ENOMEM);
	/*
	 * user_handle should be filled by the handler,
	 * The object is added to the list in the commit stage.
	 */
331
	uobj->ufile = ufile;
332
	uobj->context = ucontext;
333
	INIT_LIST_HEAD(&uobj->list);
M
Matan Barak 已提交
334
	uobj->type = type;
335 336 337 338 339 340
	/*
	 * Allocated objects start out as write locked to deny any other
	 * syscalls from accessing them until they are committed. See
	 * rdma_alloc_commit_uobject
	 */
	atomic_set(&uobj->usecnt, -1);
M
Matan Barak 已提交
341 342 343 344 345 346 347 348 349 350
	kref_init(&uobj->ref);

	return uobj;
}

static int idr_add_uobj(struct ib_uobject *uobj)
{
	int ret;

	idr_preload(GFP_KERNEL);
351
	spin_lock(&uobj->ufile->idr_lock);
M
Matan Barak 已提交
352 353 354 355 356 357

	/*
	 * We start with allocating an idr pointing to NULL. This represents an
	 * object which isn't initialized yet. We'll replace it later on with
	 * the real object once we commit.
	 */
358
	ret = idr_alloc(&uobj->ufile->idr, NULL, 0,
M
Matan Barak 已提交
359 360 361 362
			min_t(unsigned long, U32_MAX - 1, INT_MAX), GFP_NOWAIT);
	if (ret >= 0)
		uobj->id = ret;

363
	spin_unlock(&uobj->ufile->idr_lock);
M
Matan Barak 已提交
364 365 366 367 368 369
	idr_preload_end();

	return ret < 0 ? ret : 0;
}

/* Returns the ib_uobject or an error. The caller should check for IS_ERR. */
370 371
static struct ib_uobject *
lookup_get_idr_uobject(const struct uverbs_obj_type *type,
372 373
		       struct ib_uverbs_file *ufile, s64 id,
		       enum rdma_lookup_mode mode)
M
Matan Barak 已提交
374 375
{
	struct ib_uobject *uobj;
376 377 378 379
	unsigned long idrno = id;

	if (id < 0 || id > ULONG_MAX)
		return ERR_PTR(-EINVAL);
M
Matan Barak 已提交
380 381 382

	rcu_read_lock();
	/* object won't be released as we're protected in rcu */
383
	uobj = idr_find(&ufile->idr, idrno);
M
Matan Barak 已提交
384 385 386 387 388
	if (!uobj) {
		uobj = ERR_PTR(-ENOENT);
		goto free;
	}

389 390 391 392 393 394 395 396 397
	/*
	 * The idr_find is guaranteed to return a pointer to something that
	 * isn't freed yet, or NULL, as the free after idr_remove goes through
	 * kfree_rcu(). However the object may still have been released and
	 * kfree() could be called at any time.
	 */
	if (!kref_get_unless_zero(&uobj->ref))
		uobj = ERR_PTR(-ENOENT);

M
Matan Barak 已提交
398 399 400 401 402
free:
	rcu_read_unlock();
	return uobj;
}

403 404 405 406
static struct ib_uobject *
lookup_get_fd_uobject(const struct uverbs_obj_type *type,
		      struct ib_uverbs_file *ufile, s64 id,
		      enum rdma_lookup_mode mode)
407 408 409
{
	struct file *f;
	struct ib_uobject *uobject;
410
	int fdno = id;
411 412 413
	const struct uverbs_obj_fd_type *fd_type =
		container_of(type, struct uverbs_obj_fd_type, type);

414 415 416
	if (fdno != id)
		return ERR_PTR(-EINVAL);

417
	if (mode != UVERBS_LOOKUP_READ)
418 419
		return ERR_PTR(-EOPNOTSUPP);

420
	f = fget(fdno);
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
	if (!f)
		return ERR_PTR(-EBADF);

	uobject = f->private_data;
	/*
	 * fget(id) ensures we are not currently running uverbs_close_fd,
	 * and the caller is expected to ensure that uverbs_close_fd is never
	 * done while a call top lookup is possible.
	 */
	if (f->f_op != fd_type->fops) {
		fput(f);
		return ERR_PTR(-EBADF);
	}

	uverbs_uobject_get(uobject);
	return uobject;
}

M
Matan Barak 已提交
439
struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
440
					   struct ib_uverbs_file *ufile, s64 id,
441
					   enum rdma_lookup_mode mode)
M
Matan Barak 已提交
442 443 444 445
{
	struct ib_uobject *uobj;
	int ret;

446
	uobj = type->type_class->lookup_get(type, ufile, id, mode);
M
Matan Barak 已提交
447 448 449 450 451 452 453 454
	if (IS_ERR(uobj))
		return uobj;

	if (uobj->type != type) {
		ret = -EINVAL;
		goto free;
	}

455 456 457 458 459 460 461 462 463 464 465
	/*
	 * If we have been disassociated block every command except for
	 * DESTROY based commands.
	 */
	if (mode != UVERBS_LOOKUP_DESTROY &&
	    !srcu_dereference(ufile->device->ib_dev,
			      &ufile->device->disassociate_srcu)) {
		ret = -EIO;
		goto free;
	}

466
	ret = uverbs_try_lock_object(uobj, mode);
467
	if (ret)
M
Matan Barak 已提交
468 469 470 471
		goto free;

	return uobj;
free:
472
	uobj->type->type_class->lookup_put(uobj, mode);
M
Matan Barak 已提交
473 474 475 476 477
	uverbs_uobject_put(uobj);
	return ERR_PTR(ret);
}

static struct ib_uobject *alloc_begin_idr_uobject(const struct uverbs_obj_type *type,
478
						  struct ib_uverbs_file *ufile)
M
Matan Barak 已提交
479 480 481 482
{
	int ret;
	struct ib_uobject *uobj;

483
	uobj = alloc_uobj(ufile, type);
M
Matan Barak 已提交
484 485 486 487 488 489 490
	if (IS_ERR(uobj))
		return uobj;

	ret = idr_add_uobj(uobj);
	if (ret)
		goto uobj_put;

491
	ret = ib_rdmacg_try_charge(&uobj->cg_obj, uobj->context->device,
M
Matan Barak 已提交
492 493 494 495 496 497 498
				   RDMACG_RESOURCE_HCA_OBJECT);
	if (ret)
		goto idr_remove;

	return uobj;

idr_remove:
499 500 501
	spin_lock(&ufile->idr_lock);
	idr_remove(&ufile->idr, uobj->id);
	spin_unlock(&ufile->idr_lock);
M
Matan Barak 已提交
502 503 504 505 506
uobj_put:
	uverbs_uobject_put(uobj);
	return ERR_PTR(ret);
}

507
static struct ib_uobject *alloc_begin_fd_uobject(const struct uverbs_obj_type *type,
508
						 struct ib_uverbs_file *ufile)
509 510 511 512 513 514 515 516
{
	int new_fd;
	struct ib_uobject *uobj;

	new_fd = get_unused_fd_flags(O_CLOEXEC);
	if (new_fd < 0)
		return ERR_PTR(new_fd);

517
	uobj = alloc_uobj(ufile, type);
518 519 520 521 522
	if (IS_ERR(uobj)) {
		put_unused_fd(new_fd);
		return uobj;
	}

523 524
	uobj->id = new_fd;
	uobj->ufile = ufile;
525 526 527 528

	return uobj;
}

M
Matan Barak 已提交
529
struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
530
					    struct ib_uverbs_file *ufile)
M
Matan Barak 已提交
531
{
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	struct ib_uobject *ret;

	/*
	 * The hw_destroy_rwsem is held across the entire object creation and
	 * released during rdma_alloc_commit_uobject or
	 * rdma_alloc_abort_uobject
	 */
	if (!down_read_trylock(&ufile->hw_destroy_rwsem))
		return ERR_PTR(-EIO);

	ret = type->type_class->alloc_begin(type, ufile);
	if (IS_ERR(ret)) {
		up_read(&ufile->hw_destroy_rwsem);
		return ret;
	}
	return ret;
M
Matan Barak 已提交
548 549
}

550 551 552 553 554 555 556 557 558 559
static void alloc_abort_idr_uobject(struct ib_uobject *uobj)
{
	ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
			   RDMACG_RESOURCE_HCA_OBJECT);

	spin_lock(&uobj->ufile->idr_lock);
	idr_remove(&uobj->ufile->idr, uobj->id);
	spin_unlock(&uobj->ufile->idr_lock);
}

560 561
static int __must_check destroy_hw_idr_uobject(struct ib_uobject *uobj,
					       enum rdma_remove_reason why)
M
Matan Barak 已提交
562 563 564 565 566 567 568 569
{
	const struct uverbs_obj_idr_type *idr_type =
		container_of(uobj->type, struct uverbs_obj_idr_type,
			     type);
	int ret = idr_type->destroy_object(uobj, why);

	/*
	 * We can only fail gracefully if the user requested to destroy the
570 571
	 * object or when a retry may be called upon an error.
	 * In the rest of the cases, just remove whatever you can.
M
Matan Barak 已提交
572
	 */
573
	if (ib_is_destroy_retryable(ret, why, uobj))
M
Matan Barak 已提交
574 575
		return ret;

576 577
	if (why == RDMA_REMOVE_ABORT)
		return 0;
578

579 580
	ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
			   RDMACG_RESOURCE_HCA_OBJECT);
M
Matan Barak 已提交
581

582
	return 0;
M
Matan Barak 已提交
583 584
}

585 586 587 588 589 590 591 592 593
static void remove_handle_idr_uobject(struct ib_uobject *uobj)
{
	spin_lock(&uobj->ufile->idr_lock);
	idr_remove(&uobj->ufile->idr, uobj->id);
	spin_unlock(&uobj->ufile->idr_lock);
	/* Matches the kref in alloc_commit_idr_uobject */
	uverbs_uobject_put(uobj);
}

594 595
static void alloc_abort_fd_uobject(struct ib_uobject *uobj)
{
596
	put_unused_fd(uobj->id);
597 598
}

599 600
static int __must_check destroy_hw_fd_uobject(struct ib_uobject *uobj,
					      enum rdma_remove_reason why)
601 602 603
{
	const struct uverbs_obj_fd_type *fd_type =
		container_of(uobj->type, struct uverbs_obj_fd_type, type);
604
	int ret = fd_type->context_closed(uobj, why);
605

606
	if (ib_is_destroy_retryable(ret, why, uobj))
607 608
		return ret;

609
	return 0;
M
Matan Barak 已提交
610 611
}

612 613 614 615
static void remove_handle_fd_uobject(struct ib_uobject *uobj)
{
}

616
static int alloc_commit_idr_uobject(struct ib_uobject *uobj)
M
Matan Barak 已提交
617
{
618 619 620
	struct ib_uverbs_file *ufile = uobj->ufile;

	spin_lock(&ufile->idr_lock);
M
Matan Barak 已提交
621 622 623
	/*
	 * We already allocated this IDR with a NULL object, so
	 * this shouldn't fail.
624 625 626
	 *
	 * NOTE: Once we set the IDR we loose ownership of our kref on uobj.
	 * It will be put by remove_commit_idr_uobject()
M
Matan Barak 已提交
627
	 */
628 629
	WARN_ON(idr_replace(&ufile->idr, uobj, uobj->id));
	spin_unlock(&ufile->idr_lock);
630 631

	return 0;
M
Matan Barak 已提交
632 633
}

634
static int alloc_commit_fd_uobject(struct ib_uobject *uobj)
635
{
636 637
	const struct uverbs_obj_fd_type *fd_type =
		container_of(uobj->type, struct uverbs_obj_fd_type, type);
638
	int fd = uobj->id;
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	struct file *filp;

	/*
	 * The kref for uobj is moved into filp->private data and put in
	 * uverbs_close_fd(). Once alloc_commit() succeeds uverbs_close_fd()
	 * must be guaranteed to be called from the provided fops release
	 * callback.
	 */
	filp = anon_inode_getfile(fd_type->name,
				  fd_type->fops,
				  uobj,
				  fd_type->flags);
	if (IS_ERR(filp))
		return PTR_ERR(filp);

	uobj->object = filp;

	/* Matching put will be done in uverbs_close_fd() */
	kref_get(&uobj->ufile->ref);
658

659
	/* This shouldn't be used anymore. Use the file object instead */
660
	uobj->id = 0;
661 662 663 664 665

	/*
	 * NOTE: Once we install the file we loose ownership of our kref on
	 * uobj. It will be put by uverbs_close_fd()
	 */
666 667 668
	fd_install(fd, filp);

	return 0;
669 670
}

671 672
/*
 * In all cases rdma_alloc_commit_uobject() consumes the kref to uobj and the
673 674
 * caller can no longer assume uobj is valid. If this function fails it
 * destroys the uboject, including the attached HW object.
675
 */
676
int __must_check rdma_alloc_commit_uobject(struct ib_uobject *uobj)
M
Matan Barak 已提交
677
{
678
	struct ib_uverbs_file *ufile = uobj->ufile;
679
	int ret;
680

681 682 683
	/* alloc_commit consumes the uobj kref */
	ret = uobj->type->type_class->alloc_commit(uobj);
	if (ret) {
684
		uverbs_destroy_uobject(uobj, RDMA_REMOVE_ABORT);
685
		up_read(&ufile->hw_destroy_rwsem);
686 687
		return ret;
	}
688

689 690
	/* kref is held so long as the uobj is on the uobj list. */
	uverbs_uobject_get(uobj);
691
	spin_lock_irq(&ufile->uobjects_lock);
692
	list_add(&uobj->list, &ufile->uobjects);
693
	spin_unlock_irq(&ufile->uobjects_lock);
694

695 696 697
	/* matches atomic_set(-1) in alloc_uobj */
	atomic_set(&uobj->usecnt, 0);

698
	/* Matches the down_read in rdma_alloc_begin_uobject */
699
	up_read(&ufile->hw_destroy_rwsem);
M
Matan Barak 已提交
700 701 702 703

	return 0;
}

704 705 706 707
/*
 * This consumes the kref for uobj. It is up to the caller to unwind the HW
 * object and anything else connected to uobj before calling this.
 */
M
Matan Barak 已提交
708 709
void rdma_alloc_abort_uobject(struct ib_uobject *uobj)
{
710 711
	struct ib_uverbs_file *ufile = uobj->ufile;

712 713
	uobj->object = NULL;
	uverbs_destroy_uobject(uobj, RDMA_REMOVE_ABORT);
714 715 716

	/* Matches the down_read in rdma_alloc_begin_uobject */
	up_read(&ufile->hw_destroy_rwsem);
M
Matan Barak 已提交
717 718
}

719 720
static void lookup_put_idr_uobject(struct ib_uobject *uobj,
				   enum rdma_lookup_mode mode)
M
Matan Barak 已提交
721 722 723
{
}

724 725
static void lookup_put_fd_uobject(struct ib_uobject *uobj,
				  enum rdma_lookup_mode mode)
726 727 728
{
	struct file *filp = uobj->object;

729
	WARN_ON(mode != UVERBS_LOOKUP_READ);
730 731 732 733
	/* This indirectly calls uverbs_close_fd and free the object */
	fput(filp);
}

734 735
void rdma_lookup_put_uobject(struct ib_uobject *uobj,
			     enum rdma_lookup_mode mode)
M
Matan Barak 已提交
736
{
737 738
	assert_uverbs_usecnt(uobj, mode);
	uobj->type->type_class->lookup_put(uobj, mode);
M
Matan Barak 已提交
739 740
	/*
	 * In order to unlock an object, either decrease its usecnt for
741
	 * read access or zero it in case of exclusive access. See
M
Matan Barak 已提交
742 743
	 * uverbs_try_lock_object for locking schema information.
	 */
744 745
	switch (mode) {
	case UVERBS_LOOKUP_READ:
M
Matan Barak 已提交
746
		atomic_dec(&uobj->usecnt);
747 748
		break;
	case UVERBS_LOOKUP_WRITE:
M
Matan Barak 已提交
749
		atomic_set(&uobj->usecnt, 0);
750
		break;
751 752
	case UVERBS_LOOKUP_DESTROY:
		break;
753
	}
M
Matan Barak 已提交
754

755
	/* Pairs with the kref obtained by type->lookup_get */
M
Matan Barak 已提交
756 757 758
	uverbs_uobject_put(uobj);
}

759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
void setup_ufile_idr_uobject(struct ib_uverbs_file *ufile)
{
	spin_lock_init(&ufile->idr_lock);
	idr_init(&ufile->idr);
}

void release_ufile_idr_uobject(struct ib_uverbs_file *ufile)
{
	struct ib_uobject *entry;
	int id;

	/*
	 * At this point uverbs_cleanup_ufile() is guaranteed to have run, and
	 * there are no HW objects left, however the IDR is still populated
	 * with anything that has not been cleaned up by userspace. Since the
	 * kref on ufile is 0, nothing is allowed to call lookup_get.
	 *
	 * This is an optimized equivalent to remove_handle_idr_uobject
	 */
	idr_for_each_entry(&ufile->idr, entry, id) {
		WARN_ON(entry->object);
		uverbs_uobject_put(entry);
	}

	idr_destroy(&ufile->idr);
}

M
Matan Barak 已提交
786 787 788 789 790 791
const struct uverbs_obj_type_class uverbs_idr_class = {
	.alloc_begin = alloc_begin_idr_uobject,
	.lookup_get = lookup_get_idr_uobject,
	.alloc_commit = alloc_commit_idr_uobject,
	.alloc_abort = alloc_abort_idr_uobject,
	.lookup_put = lookup_put_idr_uobject,
792 793
	.destroy_hw = destroy_hw_idr_uobject,
	.remove_handle = remove_handle_idr_uobject,
M
Matan Barak 已提交
794 795 796 797 798 799 800 801 802 803
	/*
	 * When we destroy an object, we first just lock it for WRITE and
	 * actually DESTROY it in the finalize stage. So, the problematic
	 * scenario is when we just started the finalize stage of the
	 * destruction (nothing was executed yet). Now, the other thread
	 * fetched the object for READ access, but it didn't lock it yet.
	 * The DESTROY thread continues and starts destroying the object.
	 * When the other thread continue - without the RCU, it would
	 * access freed memory. However, the rcu_read_lock delays the free
	 * until the rcu_read_lock of the READ operation quits. Since the
804
	 * exclusive lock of the object is still taken by the DESTROY flow, the
M
Matan Barak 已提交
805 806 807 808
	 * READ operation will get -EBUSY and it'll just bail out.
	 */
	.needs_kfree_rcu = true,
};
809
EXPORT_SYMBOL(uverbs_idr_class);
M
Matan Barak 已提交
810

811 812
void uverbs_close_fd(struct file *f)
{
813
	struct ib_uobject *uobj = f->private_data;
814 815
	struct ib_uverbs_file *ufile = uobj->ufile;

816
	if (down_read_trylock(&ufile->hw_destroy_rwsem)) {
817 818 819 820 821 822
		/*
		 * lookup_get_fd_uobject holds the kref on the struct file any
		 * time a FD uobj is locked, which prevents this release
		 * method from being invoked. Meaning we can always get the
		 * write lock here, or we have a kernel bug.
		 */
823
		WARN_ON(uverbs_try_lock_object(uobj, UVERBS_LOOKUP_WRITE));
824
		uverbs_destroy_uobject(uobj, RDMA_REMOVE_CLOSE);
825
		up_read(&ufile->hw_destroy_rwsem);
826 827 828 829
	}

	/* Matches the get in alloc_begin_fd_uobject */
	kref_put(&ufile->ref, ib_uverbs_release_file);
830

831
	/* Pairs with filp->private_data in alloc_begin_fd_uobject */
832
	uverbs_uobject_put(uobj);
833 834
}

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
static void ufile_disassociate_ucontext(struct ib_ucontext *ibcontext)
{
	struct ib_device *ib_dev = ibcontext->device;
	struct task_struct *owning_process  = NULL;
	struct mm_struct   *owning_mm       = NULL;

	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
	if (!owning_process)
		return;

	owning_mm = get_task_mm(owning_process);
	if (!owning_mm) {
		pr_info("no mm, disassociate ucontext is pending task termination\n");
		while (1) {
			put_task_struct(owning_process);
			usleep_range(1000, 2000);
			owning_process = get_pid_task(ibcontext->tgid,
						      PIDTYPE_PID);
			if (!owning_process ||
			    owning_process->state == TASK_DEAD) {
				pr_info("disassociate ucontext done, task was terminated\n");
				/* in case task was dead need to release the
				 * task struct.
				 */
				if (owning_process)
					put_task_struct(owning_process);
				return;
			}
		}
	}

	down_write(&owning_mm->mmap_sem);
	ib_dev->disassociate_ucontext(ibcontext);
	up_write(&owning_mm->mmap_sem);
	mmput(owning_mm);
	put_task_struct(owning_process);
}

/*
 * Drop the ucontext off the ufile and completely disconnect it from the
 * ib_device
 */
static void ufile_destroy_ucontext(struct ib_uverbs_file *ufile,
				   enum rdma_remove_reason reason)
{
	struct ib_ucontext *ucontext = ufile->ucontext;
	int ret;

	if (reason == RDMA_REMOVE_DRIVER_REMOVE)
		ufile_disassociate_ucontext(ucontext);

	put_pid(ucontext->tgid);
	ib_rdmacg_uncharge(&ucontext->cg_obj, ucontext->device,
			   RDMACG_RESOURCE_HCA_HANDLE);

	/*
	 * FIXME: Drivers are not permitted to fail dealloc_ucontext, remove
	 * the error return.
	 */
894
	ret = ucontext->device->dealloc_ucontext(ucontext);
895 896 897 898 899
	WARN_ON(ret);

	ufile->ucontext = NULL;
}

900 901
static int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile,
				  enum rdma_remove_reason reason)
M
Matan Barak 已提交
902
{
903 904
	struct ib_uobject *obj, *next_obj;
	int ret = -EINVAL;
M
Matan Barak 已提交
905

906 907 908 909 910 911 912 913 914
	/*
	 * This shouldn't run while executing other commands on this
	 * context. Thus, the only thing we should take care of is
	 * releasing a FD while traversing this list. The FD could be
	 * closed and released from the _release fop of this FD.
	 * In order to mitigate this, we add a lock.
	 * We take and release the lock per traversal in order to let
	 * other threads (which might still use the FDs) chance to run.
	 */
915
	list_for_each_entry_safe(obj, next_obj, &ufile->uobjects, list) {
916 917 918 919
		/*
		 * if we hit this WARN_ON, that means we are
		 * racing with a lookup_get.
		 */
920
		WARN_ON(uverbs_try_lock_object(obj, UVERBS_LOOKUP_WRITE));
921 922
		if (!uverbs_destroy_uobject(obj, reason))
			ret = 0;
923 924 925 926
	}
	return ret;
}

927 928 929 930 931 932 933 934 935 936
/*
 * Destroy the uncontext and every uobject associated with it. If called with
 * reason != RDMA_REMOVE_CLOSE this will not return until the destruction has
 * been completed and ufile->ucontext is NULL.
 *
 * This is internally locked and can be called in parallel from multiple
 * contexts.
 */
void uverbs_destroy_ufile_hw(struct ib_uverbs_file *ufile,
			     enum rdma_remove_reason reason)
937
{
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
	if (reason == RDMA_REMOVE_CLOSE) {
		/*
		 * During destruction we might trigger something that
		 * synchronously calls release on any file descriptor. For
		 * this reason all paths that come from file_operations
		 * release must use try_lock. They can progress knowing that
		 * there is an ongoing uverbs_destroy_ufile_hw that will clean
		 * up the driver resources.
		 */
		if (!mutex_trylock(&ufile->ucontext_lock))
			return;

	} else {
		mutex_lock(&ufile->ucontext_lock);
	}

	down_write(&ufile->hw_destroy_rwsem);
955

M
Matan Barak 已提交
956
	/*
957 958
	 * If a ucontext was never created then we can't have any uobjects to
	 * cleanup, nothing to do.
M
Matan Barak 已提交
959
	 */
960 961 962 963
	if (!ufile->ucontext)
		goto done;

	ufile->ucontext->closing = true;
964 965
	ufile->ucontext->cleanup_retryable = true;
	while (!list_empty(&ufile->uobjects))
966
		if (__uverbs_cleanup_ufile(ufile, reason)) {
967 968 969 970 971 972
			/*
			 * No entry was cleaned-up successfully during this
			 * iteration
			 */
			break;
		}
M
Matan Barak 已提交
973

974 975
	ufile->ucontext->cleanup_retryable = false;
	if (!list_empty(&ufile->uobjects))
976
		__uverbs_cleanup_ufile(ufile, reason);
M
Matan Barak 已提交
977

978 979 980
	ufile_destroy_ucontext(ufile, reason);

done:
981
	up_write(&ufile->hw_destroy_rwsem);
982
	mutex_unlock(&ufile->ucontext_lock);
M
Matan Barak 已提交
983 984
}

985 986 987 988 989 990
const struct uverbs_obj_type_class uverbs_fd_class = {
	.alloc_begin = alloc_begin_fd_uobject,
	.lookup_get = lookup_get_fd_uobject,
	.alloc_commit = alloc_commit_fd_uobject,
	.alloc_abort = alloc_abort_fd_uobject,
	.lookup_put = lookup_put_fd_uobject,
991 992
	.destroy_hw = destroy_hw_fd_uobject,
	.remove_handle = remove_handle_fd_uobject,
993 994
	.needs_kfree_rcu = false,
};
995
EXPORT_SYMBOL(uverbs_fd_class);
996

997 998 999
struct ib_uobject *
uverbs_get_uobject_from_file(const struct uverbs_obj_type *type_attrs,
			     struct ib_uverbs_file *ufile,
1000
			     enum uverbs_obj_access access, s64 id)
1001 1002 1003
{
	switch (access) {
	case UVERBS_ACCESS_READ:
1004 1005
		return rdma_lookup_get_uobject(type_attrs, ufile, id,
					       UVERBS_LOOKUP_READ);
1006
	case UVERBS_ACCESS_DESTROY:
1007 1008 1009
		/* Actual destruction is done inside uverbs_handle_method */
		return rdma_lookup_get_uobject(type_attrs, ufile, id,
					       UVERBS_LOOKUP_DESTROY);
1010
	case UVERBS_ACCESS_WRITE:
1011 1012
		return rdma_lookup_get_uobject(type_attrs, ufile, id,
					       UVERBS_LOOKUP_WRITE);
1013
	case UVERBS_ACCESS_NEW:
1014
		return rdma_alloc_begin_uobject(type_attrs, ufile);
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	default:
		WARN_ON(true);
		return ERR_PTR(-EOPNOTSUPP);
	}
}

int uverbs_finalize_object(struct ib_uobject *uobj,
			   enum uverbs_obj_access access,
			   bool commit)
{
	int ret = 0;

	/*
	 * refcounts should be handled at the object level and not at the
	 * uobject level. Refcounts of the objects themselves are done in
	 * handlers.
	 */

	switch (access) {
	case UVERBS_ACCESS_READ:
1035
		rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_READ);
1036 1037
		break;
	case UVERBS_ACCESS_WRITE:
1038
		rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
1039 1040
		break;
	case UVERBS_ACCESS_DESTROY:
1041 1042
		if (uobj)
			rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
		break;
	case UVERBS_ACCESS_NEW:
		if (commit)
			ret = rdma_alloc_commit_uobject(uobj);
		else
			rdma_alloc_abort_uobject(uobj);
		break;
	default:
		WARN_ON(true);
		ret = -EOPNOTSUPP;
	}

	return ret;
}