rdma_core.c 24.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
static int uverbs_try_lock_object(struct ib_uobject *uobj, bool exclusive)
M
Matan Barak 已提交
112 113
{
	/*
114 115 116 117 118 119 120 121 122
	 * 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 已提交
123
	 */
124
	if (!exclusive)
M
Matan Barak 已提交
125 126 127 128 129 130 131
		return __atomic_add_unless(&uobj->usecnt, 1, -1) == -1 ?
			-EBUSY : 0;

	/* lock is either WRITE or DESTROY - should be exclusive */
	return atomic_cmpxchg(&uobj->usecnt, 0, -1) == 0 ? 0 : -EBUSY;
}

132 133 134 135 136
/*
 * Does both rdma_lookup_get_uobject() and rdma_remove_commit_uobject(), then
 * returns success_res on success (negative errno on failure). For use by
 * callers that do not need the uobj.
 */
137
int __uobj_perform_destroy(const struct uverbs_obj_type *type, u32 id,
138 139 140 141 142
			   struct ib_uverbs_file *ufile, int success_res)
{
	struct ib_uobject *uobj;
	int ret;

143
	uobj = rdma_lookup_get_uobject(type, ufile, id, true);
144 145 146 147 148 149 150 151 152 153
	if (IS_ERR(uobj))
		return PTR_ERR(uobj);

	ret = rdma_remove_commit_uobject(uobj);
	if (ret)
		return ret;

	return success_res;
}

154
static struct ib_uobject *alloc_uobj(struct ib_uverbs_file *ufile,
M
Matan Barak 已提交
155 156
				     const struct uverbs_obj_type *type)
{
157 158 159 160 161 162
	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 已提交
163

164
	uobj = kzalloc(type->obj_size, GFP_KERNEL);
M
Matan Barak 已提交
165 166 167 168 169 170
	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.
	 */
171
	uobj->ufile = ufile;
172
	uobj->context = ucontext;
173
	INIT_LIST_HEAD(&uobj->list);
M
Matan Barak 已提交
174
	uobj->type = type;
175 176 177 178 179 180
	/*
	 * 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 已提交
181 182 183 184 185 186 187 188 189 190
	kref_init(&uobj->ref);

	return uobj;
}

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

	idr_preload(GFP_KERNEL);
191
	spin_lock(&uobj->ufile->idr_lock);
M
Matan Barak 已提交
192 193 194 195 196 197

	/*
	 * 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.
	 */
198
	ret = idr_alloc(&uobj->ufile->idr, NULL, 0,
M
Matan Barak 已提交
199 200 201 202
			min_t(unsigned long, U32_MAX - 1, INT_MAX), GFP_NOWAIT);
	if (ret >= 0)
		uobj->id = ret;

203
	spin_unlock(&uobj->ufile->idr_lock);
M
Matan Barak 已提交
204 205 206 207 208 209
	idr_preload_end();

	return ret < 0 ? ret : 0;
}

/* Returns the ib_uobject or an error. The caller should check for IS_ERR. */
210 211
static struct ib_uobject *
lookup_get_idr_uobject(const struct uverbs_obj_type *type,
212
		       struct ib_uverbs_file *ufile, s64 id, bool exclusive)
M
Matan Barak 已提交
213 214
{
	struct ib_uobject *uobj;
215 216 217 218
	unsigned long idrno = id;

	if (id < 0 || id > ULONG_MAX)
		return ERR_PTR(-EINVAL);
M
Matan Barak 已提交
219 220 221

	rcu_read_lock();
	/* object won't be released as we're protected in rcu */
222
	uobj = idr_find(&ufile->idr, idrno);
M
Matan Barak 已提交
223 224 225 226 227
	if (!uobj) {
		uobj = ERR_PTR(-ENOENT);
		goto free;
	}

228 229 230 231 232 233 234 235 236
	/*
	 * 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 已提交
237 238 239 240 241
free:
	rcu_read_unlock();
	return uobj;
}

242
static struct ib_uobject *lookup_get_fd_uobject(const struct uverbs_obj_type *type,
243
						struct ib_uverbs_file *ufile,
244
						s64 id, bool exclusive)
245 246 247
{
	struct file *f;
	struct ib_uobject *uobject;
248
	int fdno = id;
249 250 251
	const struct uverbs_obj_fd_type *fd_type =
		container_of(type, struct uverbs_obj_fd_type, type);

252 253 254
	if (fdno != id)
		return ERR_PTR(-EINVAL);

255
	if (exclusive)
256 257
		return ERR_PTR(-EOPNOTSUPP);

258
	f = fget(fdno);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	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 已提交
277
struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
278
					   struct ib_uverbs_file *ufile, s64 id,
279
					   bool exclusive)
M
Matan Barak 已提交
280 281 282 283
{
	struct ib_uobject *uobj;
	int ret;

284
	uobj = type->type_class->lookup_get(type, ufile, id, exclusive);
M
Matan Barak 已提交
285 286 287 288 289 290 291 292
	if (IS_ERR(uobj))
		return uobj;

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

293
	ret = uverbs_try_lock_object(uobj, exclusive);
294
	if (ret)
M
Matan Barak 已提交
295 296 297 298
		goto free;

	return uobj;
free:
299
	uobj->type->type_class->lookup_put(uobj, exclusive);
M
Matan Barak 已提交
300 301 302 303 304
	uverbs_uobject_put(uobj);
	return ERR_PTR(ret);
}

static struct ib_uobject *alloc_begin_idr_uobject(const struct uverbs_obj_type *type,
305
						  struct ib_uverbs_file *ufile)
M
Matan Barak 已提交
306 307 308 309
{
	int ret;
	struct ib_uobject *uobj;

310
	uobj = alloc_uobj(ufile, type);
M
Matan Barak 已提交
311 312 313 314 315 316 317
	if (IS_ERR(uobj))
		return uobj;

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

318
	ret = ib_rdmacg_try_charge(&uobj->cg_obj, uobj->context->device,
M
Matan Barak 已提交
319 320 321 322 323 324 325
				   RDMACG_RESOURCE_HCA_OBJECT);
	if (ret)
		goto idr_remove;

	return uobj;

idr_remove:
326 327 328
	spin_lock(&ufile->idr_lock);
	idr_remove(&ufile->idr, uobj->id);
	spin_unlock(&ufile->idr_lock);
M
Matan Barak 已提交
329 330 331 332 333
uobj_put:
	uverbs_uobject_put(uobj);
	return ERR_PTR(ret);
}

334
static struct ib_uobject *alloc_begin_fd_uobject(const struct uverbs_obj_type *type,
335
						 struct ib_uverbs_file *ufile)
336 337 338 339 340 341 342 343
{
	int new_fd;
	struct ib_uobject *uobj;

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

344
	uobj = alloc_uobj(ufile, type);
345 346 347 348 349
	if (IS_ERR(uobj)) {
		put_unused_fd(new_fd);
		return uobj;
	}

350 351
	uobj->id = new_fd;
	uobj->ufile = ufile;
352 353 354 355

	return uobj;
}

M
Matan Barak 已提交
356
struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
357
					    struct ib_uverbs_file *ufile)
M
Matan Barak 已提交
358
{
359
	return type->type_class->alloc_begin(type, ufile);
M
Matan Barak 已提交
360 361 362 363 364 365 366 367 368 369 370 371
}

static int __must_check remove_commit_idr_uobject(struct ib_uobject *uobj,
						  enum rdma_remove_reason why)
{
	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
372 373
	 * 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 已提交
374
	 */
375
	if (ib_is_destroy_retryable(ret, why, uobj))
M
Matan Barak 已提交
376 377 378 379
		return ret;

	ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
			   RDMACG_RESOURCE_HCA_OBJECT);
380 381 382 383 384 385 386

	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);
M
Matan Barak 已提交
387 388 389 390

	return ret;
}

391 392
static void alloc_abort_fd_uobject(struct ib_uobject *uobj)
{
393
	put_unused_fd(uobj->id);
394

395 396
	/* Pairs with the kref from alloc_begin_idr_uobject */
	uverbs_uobject_put(uobj);
397 398 399 400 401 402 403
}

static int __must_check remove_commit_fd_uobject(struct ib_uobject *uobj,
						 enum rdma_remove_reason why)
{
	const struct uverbs_obj_fd_type *fd_type =
		container_of(uobj->type, struct uverbs_obj_fd_type, type);
404
	int ret = fd_type->context_closed(uobj, why);
405

406
	if (ib_is_destroy_retryable(ret, why, uobj))
407 408 409 410 411 412 413
		return ret;

	if (why == RDMA_REMOVE_DURING_CLEANUP) {
		alloc_abort_fd_uobject(uobj);
		return ret;
	}

414
	uobj->context = NULL;
415 416 417
	return ret;
}

418
static void assert_uverbs_usecnt(struct ib_uobject *uobj, bool exclusive)
M
Matan Barak 已提交
419 420
{
#ifdef CONFIG_LOCKDEP
421
	if (exclusive)
422
		WARN_ON(atomic_read(&uobj->usecnt) != -1);
M
Matan Barak 已提交
423
	else
424
		WARN_ON(atomic_read(&uobj->usecnt) <= 0);
M
Matan Barak 已提交
425 426 427 428
#endif
}

static int __must_check _rdma_remove_commit_uobject(struct ib_uobject *uobj,
429
						    enum rdma_remove_reason why)
M
Matan Barak 已提交
430
{
431
	struct ib_uverbs_file *ufile = uobj->ufile;
M
Matan Barak 已提交
432 433
	int ret;

434 435 436
	if (!uobj->object)
		return 0;

M
Matan Barak 已提交
437
	ret = uobj->type->type_class->remove_commit(uobj, why);
438 439 440 441 442
	if (ib_is_destroy_retryable(ret, why, uobj))
		return ret;

	uobj->object = NULL;

443
	spin_lock_irq(&ufile->uobjects_lock);
444
	list_del(&uobj->list);
445
	spin_unlock_irq(&ufile->uobjects_lock);
446 447
	/* Pairs with the get in rdma_alloc_commit_uobject() */
	uverbs_uobject_put(uobj);
M
Matan Barak 已提交
448 449 450 451

	return ret;
}

452 453 454 455 456
/* This is called only for user requested DESTROY reasons
 * rdma_lookup_get_uobject(exclusive=true) must have been called to get uobj,
 * and after this returns the corresponding put has been done, and the kref
 * for uobj has been consumed.
 */
M
Matan Barak 已提交
457 458 459 460
int __must_check rdma_remove_commit_uobject(struct ib_uobject *uobj)
{
	int ret;

461 462 463
	ret = rdma_explicit_destroy(uobj);
	/* Pairs with the lookup_get done by the caller */
	rdma_lookup_put_uobject(uobj, true);
M
Matan Barak 已提交
464 465 466
	return ret;
}

467 468 469
int rdma_explicit_destroy(struct ib_uobject *uobject)
{
	int ret;
470
	struct ib_uverbs_file *ufile = uobject->ufile;
471 472

	/* Cleanup is running. Calling this should have been impossible */
473
	if (!down_read_trylock(&ufile->hw_destroy_rwsem)) {
474 475 476
		WARN(true, "ib_uverbs: Cleanup is running while removing an uobject\n");
		return 0;
	}
477
	assert_uverbs_usecnt(uobject, true);
478
	ret = _rdma_remove_commit_uobject(uobject, RDMA_REMOVE_DESTROY);
479

480
	up_read(&ufile->hw_destroy_rwsem);
481
	return ret;
482 483
}

484
static int alloc_commit_idr_uobject(struct ib_uobject *uobj)
M
Matan Barak 已提交
485
{
486 487 488
	struct ib_uverbs_file *ufile = uobj->ufile;

	spin_lock(&ufile->idr_lock);
M
Matan Barak 已提交
489 490 491
	/*
	 * We already allocated this IDR with a NULL object, so
	 * this shouldn't fail.
492 493 494
	 *
	 * 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 已提交
495
	 */
496 497
	WARN_ON(idr_replace(&ufile->idr, uobj, uobj->id));
	spin_unlock(&ufile->idr_lock);
498 499

	return 0;
M
Matan Barak 已提交
500 501
}

502
static int alloc_commit_fd_uobject(struct ib_uobject *uobj)
503
{
504 505
	const struct uverbs_obj_fd_type *fd_type =
		container_of(uobj->type, struct uverbs_obj_fd_type, type);
506
	int fd = uobj->id;
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
	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);
526

527
	/* This shouldn't be used anymore. Use the file object instead */
528
	uobj->id = 0;
529 530 531 532 533

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

	return 0;
537 538
}

539 540
/*
 * In all cases rdma_alloc_commit_uobject() consumes the kref to uobj and the
541 542
 * caller can no longer assume uobj is valid. If this function fails it
 * destroys the uboject, including the attached HW object.
543
 */
544
int __must_check rdma_alloc_commit_uobject(struct ib_uobject *uobj)
M
Matan Barak 已提交
545
{
546
	struct ib_uverbs_file *ufile = uobj->ufile;
547
	int ret;
548

M
Matan Barak 已提交
549
	/* Cleanup is running. Calling this should have been impossible */
550
	if (!down_read_trylock(&ufile->hw_destroy_rwsem)) {
M
Matan Barak 已提交
551 552 553 554 555 556 557 558 559
		WARN(true, "ib_uverbs: Cleanup is running while allocating an uobject\n");
		ret = uobj->type->type_class->remove_commit(uobj,
							    RDMA_REMOVE_DURING_CLEANUP);
		if (ret)
			pr_warn("ib_uverbs: cleanup of idr object %d failed\n",
				uobj->id);
		return ret;
	}

560
	assert_uverbs_usecnt(uobj, true);
561 562 563 564 565 566 567 568 569 570 571

	/* alloc_commit consumes the uobj kref */
	ret = uobj->type->type_class->alloc_commit(uobj);
	if (ret) {
		if (uobj->type->type_class->remove_commit(
			    uobj, RDMA_REMOVE_DURING_CLEANUP))
			pr_warn("ib_uverbs: cleanup of idr object %d failed\n",
				uobj->id);
		up_read(&ufile->hw_destroy_rwsem);
		return ret;
	}
572

573 574
	/* kref is held so long as the uobj is on the uobj list. */
	uverbs_uobject_get(uobj);
575
	spin_lock_irq(&ufile->uobjects_lock);
576
	list_add(&uobj->list, &ufile->uobjects);
577
	spin_unlock_irq(&ufile->uobjects_lock);
578

579 580 581
	/* matches atomic_set(-1) in alloc_uobj */
	atomic_set(&uobj->usecnt, 0);

582
	up_read(&ufile->hw_destroy_rwsem);
M
Matan Barak 已提交
583 584 585 586 587 588 589 590

	return 0;
}

static void alloc_abort_idr_uobject(struct ib_uobject *uobj)
{
	ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
			   RDMACG_RESOURCE_HCA_OBJECT);
591 592 593 594 595 596 597

	spin_lock(&uobj->ufile->idr_lock);
	/* The value of the handle in the IDR is NULL at this point. */
	idr_remove(&uobj->ufile->idr, uobj->id);
	spin_unlock(&uobj->ufile->idr_lock);

	/* Pairs with the kref from alloc_begin_idr_uobject */
M
Matan Barak 已提交
598 599 600
	uverbs_uobject_put(uobj);
}

601 602 603 604
/*
 * 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 已提交
605 606 607 608 609
void rdma_alloc_abort_uobject(struct ib_uobject *uobj)
{
	uobj->type->type_class->alloc_abort(uobj);
}

610
static void lookup_put_idr_uobject(struct ib_uobject *uobj, bool exclusive)
M
Matan Barak 已提交
611 612 613
{
}

614
static void lookup_put_fd_uobject(struct ib_uobject *uobj, bool exclusive)
615 616 617
{
	struct file *filp = uobj->object;

618
	WARN_ON(exclusive);
619 620 621 622
	/* This indirectly calls uverbs_close_fd and free the object */
	fput(filp);
}

623
void rdma_lookup_put_uobject(struct ib_uobject *uobj, bool exclusive)
M
Matan Barak 已提交
624
{
625
	assert_uverbs_usecnt(uobj, exclusive);
626
	uobj->type->type_class->lookup_put(uobj, exclusive);
M
Matan Barak 已提交
627 628
	/*
	 * In order to unlock an object, either decrease its usecnt for
629
	 * read access or zero it in case of exclusive access. See
M
Matan Barak 已提交
630 631
	 * uverbs_try_lock_object for locking schema information.
	 */
632
	if (!exclusive)
M
Matan Barak 已提交
633 634 635 636
		atomic_dec(&uobj->usecnt);
	else
		atomic_set(&uobj->usecnt, 0);

637
	/* Pairs with the kref obtained by type->lookup_get */
M
Matan Barak 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	uverbs_uobject_put(uobj);
}

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,
	.remove_commit = remove_commit_idr_uobject,
	/*
	 * 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
658
	 * exclusive lock of the object is still taken by the DESTROY flow, the
M
Matan Barak 已提交
659 660 661 662
	 * READ operation will get -EBUSY and it'll just bail out.
	 */
	.needs_kfree_rcu = true,
};
663
EXPORT_SYMBOL(uverbs_idr_class);
M
Matan Barak 已提交
664

665
static void _uverbs_close_fd(struct ib_uobject *uobj)
666 667 668
{
	int ret;

669 670 671 672 673 674
	/*
	 * uobject was already cleaned up, remove_commit_fd_uobject
	 * sets this
	 */
	if (!uobj->context)
		return;
675

676 677 678 679 680 681 682 683 684
	/*
	 * 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. If so dangle the pointers and bail.
	 */
	ret = uverbs_try_lock_object(uobj, true);
	if (WARN(ret, "uverbs_close_fd() racing with lookup_get_fd_uobject()"))
		return;
685

686
	ret = _rdma_remove_commit_uobject(uobj, RDMA_REMOVE_CLOSE);
687
	if (ret)
688 689 690
		pr_warn("Unable to clean up uobject file in %s\n", __func__);

	atomic_set(&uobj->usecnt, 0);
691 692 693 694
}

void uverbs_close_fd(struct file *f)
{
695
	struct ib_uobject *uobj = f->private_data;
696 697
	struct ib_uverbs_file *ufile = uobj->ufile;

698
	if (down_read_trylock(&ufile->hw_destroy_rwsem)) {
699
		_uverbs_close_fd(uobj);
700
		up_read(&ufile->hw_destroy_rwsem);
701 702 703 704 705
	}

	uobj->object = NULL;
	/* Matches the get in alloc_begin_fd_uobject */
	kref_put(&ufile->ref, ib_uverbs_release_file);
706

707
	/* Pairs with filp->private_data in alloc_begin_fd_uobject */
708
	uverbs_uobject_put(uobj);
709 710
}

711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
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.
	 */
770
	ret = ucontext->device->dealloc_ucontext(ucontext);
771 772 773 774 775
	WARN_ON(ret);

	ufile->ucontext = NULL;
}

776 777
static int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile,
				  enum rdma_remove_reason reason)
M
Matan Barak 已提交
778
{
779 780 781
	struct ib_uobject *obj, *next_obj;
	int ret = -EINVAL;
	int err = 0;
M
Matan Barak 已提交
782

783 784 785 786 787 788 789 790 791
	/*
	 * 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.
	 */
792
	list_for_each_entry_safe(obj, next_obj, &ufile->uobjects, list) {
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
		/*
		 * if we hit this WARN_ON, that means we are
		 * racing with a lookup_get.
		 */
		WARN_ON(uverbs_try_lock_object(obj, true));
		err = obj->type->type_class->remove_commit(obj, reason);

		if (ib_is_destroy_retryable(err, reason, obj)) {
			pr_debug("ib_uverbs: failed to remove uobject id %d err %d\n",
				 obj->id, err);
			atomic_set(&obj->usecnt, 0);
			continue;
		}

		if (err)
			pr_err("ib_uverbs: unable to remove uobject id %d err %d\n",
				obj->id, err);

		list_del(&obj->list);
812
		/* Pairs with the get in rdma_alloc_commit_uobject() */
813 814 815 816 817 818
		uverbs_uobject_put(obj);
		ret = 0;
	}
	return ret;
}

819 820 821 822 823 824 825 826 827 828
/*
 * 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)
829
{
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
	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);
847

M
Matan Barak 已提交
848
	/*
849 850
	 * If a ucontext was never created then we can't have any uobjects to
	 * cleanup, nothing to do.
M
Matan Barak 已提交
851
	 */
852 853 854 855
	if (!ufile->ucontext)
		goto done;

	ufile->ucontext->closing = true;
856 857
	ufile->ucontext->cleanup_retryable = true;
	while (!list_empty(&ufile->uobjects))
858
		if (__uverbs_cleanup_ufile(ufile, reason)) {
859 860 861 862 863 864
			/*
			 * No entry was cleaned-up successfully during this
			 * iteration
			 */
			break;
		}
M
Matan Barak 已提交
865

866 867
	ufile->ucontext->cleanup_retryable = false;
	if (!list_empty(&ufile->uobjects))
868
		__uverbs_cleanup_ufile(ufile, reason);
M
Matan Barak 已提交
869

870 871 872
	ufile_destroy_ucontext(ufile, reason);

done:
873
	up_write(&ufile->hw_destroy_rwsem);
874
	mutex_unlock(&ufile->ucontext_lock);
M
Matan Barak 已提交
875 876
}

877 878 879 880 881 882 883 884 885
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,
	.remove_commit = remove_commit_fd_uobject,
	.needs_kfree_rcu = false,
};
886
EXPORT_SYMBOL(uverbs_fd_class);
887

888 889 890
struct ib_uobject *
uverbs_get_uobject_from_file(const struct uverbs_obj_type *type_attrs,
			     struct ib_uverbs_file *ufile,
891
			     enum uverbs_obj_access access, s64 id)
892 893 894
{
	switch (access) {
	case UVERBS_ACCESS_READ:
895
		return rdma_lookup_get_uobject(type_attrs, ufile, id, false);
896 897
	case UVERBS_ACCESS_DESTROY:
	case UVERBS_ACCESS_WRITE:
898
		return rdma_lookup_get_uobject(type_attrs, ufile, id, true);
899
	case UVERBS_ACCESS_NEW:
900
		return rdma_alloc_begin_uobject(type_attrs, ufile);
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
	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:
		rdma_lookup_put_uobject(uobj, false);
		break;
	case UVERBS_ACCESS_WRITE:
		rdma_lookup_put_uobject(uobj, true);
		break;
	case UVERBS_ACCESS_DESTROY:
927
		rdma_lookup_put_uobject(uobj, true);
928 929 930 931 932 933 934 935 936 937 938 939 940 941
		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;
}