rdma_core.c 21.8 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 35 36 37
/*
 * 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>
#include <rdma/ib_verbs.h>
#include <rdma/uverbs_types.h>
#include <linux/rcupdate.h>
38
#include <rdma/uverbs_ioctl.h>
M
Matan Barak 已提交
39
#include <rdma/rdma_user_ioctl.h>
M
Matan Barak 已提交
40 41 42 43
#include "uverbs.h"
#include "core_priv.h"
#include "rdma_core.h"

M
Matan Barak 已提交
44 45 46 47 48 49 50 51 52 53 54
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;
}

55
const struct uverbs_object_spec *uverbs_get_object(struct ib_uverbs_file *ufile,
M
Matan Barak 已提交
56 57
						   uint16_t object)
{
58
	const struct uverbs_root_spec *object_hash = ufile->device->specs_root;
M
Matan Barak 已提交
59 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
	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 已提交
89 90 91 92 93
void uverbs_uobject_get(struct ib_uobject *uobject)
{
	kref_get(&uobject->ref);
}

94
static void uverbs_uobject_free(struct kref *ref)
M
Matan Barak 已提交
95 96 97 98 99 100 101 102 103 104 105 106
{
	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)
{
107
	kref_put(&uobject->ref, uverbs_uobject_free);
M
Matan Barak 已提交
108 109
}

110
static int uverbs_try_lock_object(struct ib_uobject *uobj, bool exclusive)
M
Matan Barak 已提交
111 112
{
	/*
113 114 115 116 117 118 119 120 121
	 * 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 已提交
122
	 */
123
	if (!exclusive)
M
Matan Barak 已提交
124 125 126 127 128 129 130
		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;
}

131 132 133 134 135 136 137 138 139 140 141
/*
 * 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.
 */
int __uobj_perform_destroy(const struct uverbs_obj_type *type, int id,
			   struct ib_uverbs_file *ufile, int success_res)
{
	struct ib_uobject *uobj;
	int ret;

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

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

	return success_res;
}

153
static struct ib_uobject *alloc_uobj(struct ib_uverbs_file *ufile,
M
Matan Barak 已提交
154 155
				     const struct uverbs_obj_type *type)
{
156
	struct ib_uobject *uobj = kzalloc(type->obj_size, GFP_KERNEL);
M
Matan Barak 已提交
157 158 159 160 161 162 163

	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.
	 */
164 165
	uobj->ufile = ufile;
	uobj->context = ufile->ucontext;
M
Matan Barak 已提交
166
	uobj->type = type;
167 168 169 170 171 172
	/*
	 * 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 已提交
173 174 175 176 177 178 179 180 181 182
	kref_init(&uobj->ref);

	return uobj;
}

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

	idr_preload(GFP_KERNEL);
183
	spin_lock(&uobj->ufile->idr_lock);
M
Matan Barak 已提交
184 185 186 187 188 189

	/*
	 * 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.
	 */
190
	ret = idr_alloc(&uobj->ufile->idr, NULL, 0,
M
Matan Barak 已提交
191 192 193 194
			min_t(unsigned long, U32_MAX - 1, INT_MAX), GFP_NOWAIT);
	if (ret >= 0)
		uobj->id = ret;

195
	spin_unlock(&uobj->ufile->idr_lock);
M
Matan Barak 已提交
196 197 198 199 200 201 202 203 204 205 206
	idr_preload_end();

	return ret < 0 ? ret : 0;
}

/*
 * It only removes it from the uobjects list, uverbs_uobject_put() is still
 * required.
 */
static void uverbs_idr_remove_uobj(struct ib_uobject *uobj)
{
207 208 209
	spin_lock(&uobj->ufile->idr_lock);
	idr_remove(&uobj->ufile->idr, uobj->id);
	spin_unlock(&uobj->ufile->idr_lock);
M
Matan Barak 已提交
210 211 212
}

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

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

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

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

250
	if (exclusive)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
		return ERR_PTR(-EOPNOTSUPP);

	f = fget(id);
	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 已提交
272
struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
273 274
					   struct ib_uverbs_file *ufile, int id,
					   bool exclusive)
M
Matan Barak 已提交
275 276 277 278
{
	struct ib_uobject *uobj;
	int ret;

279
	uobj = type->type_class->lookup_get(type, ufile, id, exclusive);
M
Matan Barak 已提交
280 281 282 283 284 285 286 287
	if (IS_ERR(uobj))
		return uobj;

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

288
	ret = uverbs_try_lock_object(uobj, exclusive);
M
Matan Barak 已提交
289
	if (ret) {
290
		WARN(uobj->ufile->cleanup_reason,
M
Matan Barak 已提交
291 292 293 294 295 296
		     "ib_uverbs: Trying to lookup_get while cleanup context\n");
		goto free;
	}

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

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

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

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

316
	ret = ib_rdmacg_try_charge(&uobj->cg_obj, ufile->ucontext->device,
M
Matan Barak 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329
				   RDMACG_RESOURCE_HCA_OBJECT);
	if (ret)
		goto idr_remove;

	return uobj;

idr_remove:
	uverbs_idr_remove_uobj(uobj);
uobj_put:
	uverbs_uobject_put(uobj);
	return ERR_PTR(ret);
}

330
static struct ib_uobject *alloc_begin_fd_uobject(const struct uverbs_obj_type *type,
331
						 struct ib_uverbs_file *ufile)
332 333 334 335 336 337 338 339 340 341 342 343
{
	const struct uverbs_obj_fd_type *fd_type =
		container_of(type, struct uverbs_obj_fd_type, type);
	int new_fd;
	struct ib_uobject *uobj;
	struct ib_uobject_file *uobj_file;
	struct file *filp;

	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 350 351 352 353 354 355 356 357 358 359 360 361 362
	if (IS_ERR(uobj)) {
		put_unused_fd(new_fd);
		return uobj;
	}

	uobj_file = container_of(uobj, struct ib_uobject_file, uobj);
	filp = anon_inode_getfile(fd_type->name,
				  fd_type->fops,
				  uobj_file,
				  fd_type->flags);
	if (IS_ERR(filp)) {
		put_unused_fd(new_fd);
		uverbs_uobject_put(uobj);
		return (void *)filp;
	}

	uobj_file->uobj.id = new_fd;
	uobj_file->uobj.object = filp;
363
	uobj_file->ufile = ufile;
364 365 366 367 368 369
	INIT_LIST_HEAD(&uobj->list);
	kref_get(&uobj_file->ufile->ref);

	return uobj;
}

M
Matan Barak 已提交
370
struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
371
					    struct ib_uverbs_file *ufile)
M
Matan Barak 已提交
372
{
373
	return type->type_class->alloc_begin(type, ufile);
M
Matan Barak 已提交
374 375 376 377 378 379 380 381 382 383 384 385
}

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
386 387
	 * 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 已提交
388
	 */
389
	if (ib_is_destroy_retryable(ret, why, uobj))
M
Matan Barak 已提交
390 391 392 393 394 395 396 397 398
		return ret;

	ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
			   RDMACG_RESOURCE_HCA_OBJECT);
	uverbs_idr_remove_uobj(uobj);

	return ret;
}

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
static void alloc_abort_fd_uobject(struct ib_uobject *uobj)
{
	struct ib_uobject_file *uobj_file =
		container_of(uobj, struct ib_uobject_file, uobj);
	struct file *filp = uobj->object;
	int id = uobj_file->uobj.id;

	/* Unsuccessful NEW */
	fput(filp);
	put_unused_fd(id);
}

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);
	struct ib_uobject_file *uobj_file =
		container_of(uobj, struct ib_uobject_file, uobj);
	int ret = fd_type->context_closed(uobj_file, why);

420
	if (ib_is_destroy_retryable(ret, why, uobj))
421 422 423 424 425 426 427 428 429 430 431
		return ret;

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

	uobj_file->uobj.context = NULL;
	return ret;
}

432
static void assert_uverbs_usecnt(struct ib_uobject *uobj, bool exclusive)
M
Matan Barak 已提交
433 434
{
#ifdef CONFIG_LOCKDEP
435
	if (exclusive)
436
		WARN_ON(atomic_read(&uobj->usecnt) != -1);
M
Matan Barak 已提交
437
	else
438
		WARN_ON(atomic_read(&uobj->usecnt) <= 0);
M
Matan Barak 已提交
439 440 441 442
#endif
}

static int __must_check _rdma_remove_commit_uobject(struct ib_uobject *uobj,
443
						    enum rdma_remove_reason why)
M
Matan Barak 已提交
444
{
445
	struct ib_uverbs_file *ufile = uobj->ufile;
M
Matan Barak 已提交
446 447 448
	int ret;

	ret = uobj->type->type_class->remove_commit(uobj, why);
449
	if (ib_is_destroy_retryable(ret, why, uobj)) {
M
Matan Barak 已提交
450 451 452 453
		/* We couldn't remove the object, so just unlock the uobject */
		atomic_set(&uobj->usecnt, 0);
		uobj->type->type_class->lookup_put(uobj, true);
	} else {
454
		mutex_lock(&ufile->uobjects_lock);
M
Matan Barak 已提交
455
		list_del(&uobj->list);
456
		mutex_unlock(&ufile->uobjects_lock);
M
Matan Barak 已提交
457 458 459 460 461 462 463 464 465 466 467
		/* put the ref we took when we created the object */
		uverbs_uobject_put(uobj);
	}

	return ret;
}

/* This is called only for user requested DESTROY reasons */
int __must_check rdma_remove_commit_uobject(struct ib_uobject *uobj)
{
	int ret;
468
	struct ib_uverbs_file *ufile = uobj->ufile;
M
Matan Barak 已提交
469 470 471 472

	/* put the ref count we took at lookup_get */
	uverbs_uobject_put(uobj);
	/* Cleanup is running. Calling this should have been impossible */
473
	if (!down_read_trylock(&ufile->cleanup_rwsem)) {
M
Matan Barak 已提交
474 475 476
		WARN(true, "ib_uverbs: Cleanup is running while removing an uobject\n");
		return 0;
	}
477
	assert_uverbs_usecnt(uobj, true);
478
	ret = _rdma_remove_commit_uobject(uobj, RDMA_REMOVE_DESTROY);
M
Matan Barak 已提交
479

480
	up_read(&ufile->cleanup_rwsem);
M
Matan Barak 已提交
481 482 483
	return ret;
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
static int null_obj_type_class_remove_commit(struct ib_uobject *uobj,
					     enum rdma_remove_reason why)
{
	return 0;
}

static const struct uverbs_obj_type null_obj_type = {
	.type_class = &((const struct uverbs_obj_type_class){
			.remove_commit = null_obj_type_class_remove_commit,
			/* be cautious */
			.needs_kfree_rcu = true}),
};

int rdma_explicit_destroy(struct ib_uobject *uobject)
{
	int ret;
500
	struct ib_uverbs_file *ufile = uobject->ufile;
501 502

	/* Cleanup is running. Calling this should have been impossible */
503
	if (!down_read_trylock(&ufile->cleanup_rwsem)) {
504 505 506
		WARN(true, "ib_uverbs: Cleanup is running while removing an uobject\n");
		return 0;
	}
507
	assert_uverbs_usecnt(uobject, true);
508 509 510
	ret = uobject->type->type_class->remove_commit(uobject,
						       RDMA_REMOVE_DESTROY);
	if (ret)
511
		goto out;
512 513 514

	uobject->type = &null_obj_type;

515
out:
516
	up_read(&ufile->cleanup_rwsem);
517
	return ret;
518 519
}

M
Matan Barak 已提交
520 521
static void alloc_commit_idr_uobject(struct ib_uobject *uobj)
{
522
	spin_lock(&uobj->ufile->idr_lock);
M
Matan Barak 已提交
523 524 525 526
	/*
	 * We already allocated this IDR with a NULL object, so
	 * this shouldn't fail.
	 */
527 528
	WARN_ON(idr_replace(&uobj->ufile->idr, uobj, uobj->id));
	spin_unlock(&uobj->ufile->idr_lock);
M
Matan Barak 已提交
529 530
}

531 532 533 534 535 536 537 538 539 540 541 542
static void alloc_commit_fd_uobject(struct ib_uobject *uobj)
{
	struct ib_uobject_file *uobj_file =
		container_of(uobj, struct ib_uobject_file, uobj);

	fd_install(uobj_file->uobj.id, uobj->object);
	/* This shouldn't be used anymore. Use the file object instead */
	uobj_file->uobj.id = 0;
	/* Get another reference as we export this to the fops */
	uverbs_uobject_get(&uobj_file->uobj);
}

M
Matan Barak 已提交
543 544
int rdma_alloc_commit_uobject(struct ib_uobject *uobj)
{
545 546
	struct ib_uverbs_file *ufile = uobj->ufile;

M
Matan Barak 已提交
547
	/* Cleanup is running. Calling this should have been impossible */
548
	if (!down_read_trylock(&ufile->cleanup_rwsem)) {
M
Matan Barak 已提交
549 550 551 552 553 554 555 556 557 558 559
		int ret;

		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
	/* matches atomic_set(-1) in alloc_uobj */
561
	assert_uverbs_usecnt(uobj, true);
562 563
	atomic_set(&uobj->usecnt, 0);

564 565 566
	mutex_lock(&ufile->uobjects_lock);
	list_add(&uobj->list, &ufile->uobjects);
	mutex_unlock(&ufile->uobjects_lock);
567

M
Matan Barak 已提交
568
	uobj->type->type_class->alloc_commit(uobj);
569
	up_read(&ufile->cleanup_rwsem);
M
Matan Barak 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

	return 0;
}

static void alloc_abort_idr_uobject(struct ib_uobject *uobj)
{
	uverbs_idr_remove_uobj(uobj);
	ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
			   RDMACG_RESOURCE_HCA_OBJECT);
	uverbs_uobject_put(uobj);
}

void rdma_alloc_abort_uobject(struct ib_uobject *uobj)
{
	uobj->type->type_class->alloc_abort(uobj);
}

587
static void lookup_put_idr_uobject(struct ib_uobject *uobj, bool exclusive)
M
Matan Barak 已提交
588 589 590
{
}

591
static void lookup_put_fd_uobject(struct ib_uobject *uobj, bool exclusive)
592 593 594
{
	struct file *filp = uobj->object;

595
	WARN_ON(exclusive);
596 597 598 599
	/* This indirectly calls uverbs_close_fd and free the object */
	fput(filp);
}

600
void rdma_lookup_put_uobject(struct ib_uobject *uobj, bool exclusive)
M
Matan Barak 已提交
601
{
602
	assert_uverbs_usecnt(uobj, exclusive);
603
	uobj->type->type_class->lookup_put(uobj, exclusive);
M
Matan Barak 已提交
604 605
	/*
	 * In order to unlock an object, either decrease its usecnt for
606
	 * read access or zero it in case of exclusive access. See
M
Matan Barak 已提交
607 608
	 * uverbs_try_lock_object for locking schema information.
	 */
609
	if (!exclusive)
M
Matan Barak 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
		atomic_dec(&uobj->usecnt);
	else
		atomic_set(&uobj->usecnt, 0);

	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
634
	 * exclusive lock of the object is still taken by the DESTROY flow, the
M
Matan Barak 已提交
635 636 637 638
	 * READ operation will get -EBUSY and it'll just bail out.
	 */
	.needs_kfree_rcu = true,
};
639
EXPORT_SYMBOL(uverbs_idr_class);
M
Matan Barak 已提交
640

641 642 643 644 645
static void _uverbs_close_fd(struct ib_uobject_file *uobj_file)
{
	struct ib_uverbs_file *ufile = uobj_file->ufile;
	int ret;

646
	mutex_lock(&ufile->cleanup_mutex);
647 648 649

	/* uobject was either already cleaned up or is cleaned up right now anyway */
	if (!uobj_file->uobj.context ||
650
	    !down_read_trylock(&ufile->cleanup_rwsem))
651 652
		goto unlock;

653
	ret = _rdma_remove_commit_uobject(&uobj_file->uobj, RDMA_REMOVE_CLOSE);
654
	up_read(&ufile->cleanup_rwsem);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
	if (ret)
		pr_warn("uverbs: unable to clean up uobject file in uverbs_close_fd.\n");
unlock:
	mutex_unlock(&ufile->cleanup_mutex);
}

void uverbs_close_fd(struct file *f)
{
	struct ib_uobject_file *uobj_file = f->private_data;
	struct kref *uverbs_file_ref = &uobj_file->ufile->ref;

	_uverbs_close_fd(uobj_file);
	uverbs_uobject_put(&uobj_file->uobj);
	kref_put(uverbs_file_ref, ib_uverbs_release_file);
}

671 672
static int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile,
				  enum rdma_remove_reason reason)
M
Matan Barak 已提交
673
{
674 675 676
	struct ib_uobject *obj, *next_obj;
	int ret = -EINVAL;
	int err = 0;
M
Matan Barak 已提交
677

678 679 680 681 682 683 684 685 686
	/*
	 * 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.
	 */
687 688 689
	mutex_lock(&ufile->uobjects_lock);
	ufile->cleanup_reason = reason;
	list_for_each_entry_safe(obj, next_obj, &ufile->uobjects, list) {
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
		/*
		 * 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);
		/* put the ref we took when we created the object */
		uverbs_uobject_put(obj);
		ret = 0;
	}
713
	mutex_unlock(&ufile->uobjects_lock);
714 715 716
	return ret;
}

717
void uverbs_cleanup_ufile(struct ib_uverbs_file *ufile, bool device_removed)
718 719 720 721
{
	enum rdma_remove_reason reason = device_removed ?
					RDMA_REMOVE_DRIVER_REMOVE :
					RDMA_REMOVE_CLOSE;
722

M
Matan Barak 已提交
723 724 725 726 727
	/*
	 * Waits for all remove_commit and alloc_commit to finish. Logically, We
	 * want to hold this forever as the context is going to be destroyed,
	 * but we'll release it since it causes a "held lock freed" BUG message.
	 */
728 729 730
	down_write(&ufile->cleanup_rwsem);
	ufile->ucontext->cleanup_retryable = true;
	while (!list_empty(&ufile->uobjects))
731
		if (__uverbs_cleanup_ufile(ufile, reason)) {
732 733 734 735 736 737
			/*
			 * No entry was cleaned-up successfully during this
			 * iteration
			 */
			break;
		}
M
Matan Barak 已提交
738

739 740
	ufile->ucontext->cleanup_retryable = false;
	if (!list_empty(&ufile->uobjects))
741
		__uverbs_cleanup_ufile(ufile, reason);
M
Matan Barak 已提交
742

743
	up_write(&ufile->cleanup_rwsem);
M
Matan Barak 已提交
744 745
}

746 747 748 749 750 751 752 753 754
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,
};
755
EXPORT_SYMBOL(uverbs_fd_class);
756

757 758 759 760
struct ib_uobject *
uverbs_get_uobject_from_file(const struct uverbs_obj_type *type_attrs,
			     struct ib_uverbs_file *ufile,
			     enum uverbs_obj_access access, int id)
761 762 763
{
	switch (access) {
	case UVERBS_ACCESS_READ:
764
		return rdma_lookup_get_uobject(type_attrs, ufile, id, false);
765 766
	case UVERBS_ACCESS_DESTROY:
	case UVERBS_ACCESS_WRITE:
767
		return rdma_lookup_get_uobject(type_attrs, ufile, id, true);
768
	case UVERBS_ACCESS_NEW:
769
		return rdma_alloc_begin_uobject(type_attrs, ufile);
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	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:
		if (commit)
			ret = rdma_remove_commit_uobject(uobj);
		else
			rdma_lookup_put_uobject(uobj, true);
		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;
}