19ed3e5f4SJason Gunthorpe // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
29ed3e5f4SJason Gunthorpe /*
39ed3e5f4SJason Gunthorpe  * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
49ed3e5f4SJason Gunthorpe  */
59ed3e5f4SJason Gunthorpe #include <rdma/uverbs_ioctl.h>
69ed3e5f4SJason Gunthorpe #include <rdma/rdma_user_ioctl.h>
79ed3e5f4SJason Gunthorpe #include <linux/bitops.h>
89ed3e5f4SJason Gunthorpe #include "rdma_core.h"
99ed3e5f4SJason Gunthorpe #include "uverbs.h"
109ed3e5f4SJason Gunthorpe 
ib_uverbs_notsupp(struct uverbs_attr_bundle * attrs)11974d6b4bSJason Gunthorpe static int ib_uverbs_notsupp(struct uverbs_attr_bundle *attrs)
126884c6c4SJason Gunthorpe {
136884c6c4SJason Gunthorpe 	return -EOPNOTSUPP;
146884c6c4SJason Gunthorpe }
156884c6c4SJason Gunthorpe 
uapi_add_elm(struct uverbs_api * uapi,u32 key,size_t alloc_size)169ed3e5f4SJason Gunthorpe static void *uapi_add_elm(struct uverbs_api *uapi, u32 key, size_t alloc_size)
179ed3e5f4SJason Gunthorpe {
189ed3e5f4SJason Gunthorpe 	void *elm;
199ed3e5f4SJason Gunthorpe 	int rc;
209ed3e5f4SJason Gunthorpe 
219ed3e5f4SJason Gunthorpe 	if (key == UVERBS_API_KEY_ERR)
229ed3e5f4SJason Gunthorpe 		return ERR_PTR(-EOVERFLOW);
239ed3e5f4SJason Gunthorpe 
249ed3e5f4SJason Gunthorpe 	elm = kzalloc(alloc_size, GFP_KERNEL);
25cac2a301SDan Carpenter 	if (!elm)
26cac2a301SDan Carpenter 		return ERR_PTR(-ENOMEM);
279ed3e5f4SJason Gunthorpe 	rc = radix_tree_insert(&uapi->radix, key, elm);
289ed3e5f4SJason Gunthorpe 	if (rc) {
299ed3e5f4SJason Gunthorpe 		kfree(elm);
309ed3e5f4SJason Gunthorpe 		return ERR_PTR(rc);
319ed3e5f4SJason Gunthorpe 	}
329ed3e5f4SJason Gunthorpe 
339ed3e5f4SJason Gunthorpe 	return elm;
349ed3e5f4SJason Gunthorpe }
359ed3e5f4SJason Gunthorpe 
uapi_add_get_elm(struct uverbs_api * uapi,u32 key,size_t alloc_size,bool * exists)36c27f6aa8SJason Gunthorpe static void *uapi_add_get_elm(struct uverbs_api *uapi, u32 key,
37c27f6aa8SJason Gunthorpe 			      size_t alloc_size, bool *exists)
38c27f6aa8SJason Gunthorpe {
39c27f6aa8SJason Gunthorpe 	void *elm;
40c27f6aa8SJason Gunthorpe 
41c27f6aa8SJason Gunthorpe 	elm = uapi_add_elm(uapi, key, alloc_size);
42c27f6aa8SJason Gunthorpe 	if (!IS_ERR(elm)) {
43c27f6aa8SJason Gunthorpe 		*exists = false;
44c27f6aa8SJason Gunthorpe 		return elm;
45c27f6aa8SJason Gunthorpe 	}
46c27f6aa8SJason Gunthorpe 
47c27f6aa8SJason Gunthorpe 	if (elm != ERR_PTR(-EEXIST))
48c27f6aa8SJason Gunthorpe 		return elm;
49c27f6aa8SJason Gunthorpe 
50c27f6aa8SJason Gunthorpe 	elm = radix_tree_lookup(&uapi->radix, key);
51c27f6aa8SJason Gunthorpe 	if (WARN_ON(!elm))
52c27f6aa8SJason Gunthorpe 		return ERR_PTR(-EINVAL);
53c27f6aa8SJason Gunthorpe 	*exists = true;
54c27f6aa8SJason Gunthorpe 	return elm;
55c27f6aa8SJason Gunthorpe }
56c27f6aa8SJason Gunthorpe 
uapi_create_write(struct uverbs_api * uapi,struct ib_device * ibdev,const struct uapi_definition * def,u32 obj_key,u32 * cur_method_key)57a140692aSJason Gunthorpe static int uapi_create_write(struct uverbs_api *uapi,
58a140692aSJason Gunthorpe 			     struct ib_device *ibdev,
59a140692aSJason Gunthorpe 			     const struct uapi_definition *def,
60a140692aSJason Gunthorpe 			     u32 obj_key,
61a140692aSJason Gunthorpe 			     u32 *cur_method_key)
626884c6c4SJason Gunthorpe {
636884c6c4SJason Gunthorpe 	struct uverbs_api_write_method *method_elm;
646884c6c4SJason Gunthorpe 	u32 method_key = obj_key;
656884c6c4SJason Gunthorpe 	bool exists;
666884c6c4SJason Gunthorpe 
676884c6c4SJason Gunthorpe 	if (def->write.is_ex)
686884c6c4SJason Gunthorpe 		method_key |= uapi_key_write_ex_method(def->write.command_num);
696884c6c4SJason Gunthorpe 	else
706884c6c4SJason Gunthorpe 		method_key |= uapi_key_write_method(def->write.command_num);
716884c6c4SJason Gunthorpe 
726884c6c4SJason Gunthorpe 	method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
736884c6c4SJason Gunthorpe 				      &exists);
746884c6c4SJason Gunthorpe 	if (IS_ERR(method_elm))
756884c6c4SJason Gunthorpe 		return PTR_ERR(method_elm);
766884c6c4SJason Gunthorpe 
77974d6b4bSJason Gunthorpe 	if (WARN_ON(exists && (def->write.is_ex != method_elm->is_ex)))
786884c6c4SJason Gunthorpe 		return -EINVAL;
796884c6c4SJason Gunthorpe 
806884c6c4SJason Gunthorpe 	method_elm->is_ex = def->write.is_ex;
81974d6b4bSJason Gunthorpe 	method_elm->handler = def->func_write;
82bd2a40ccSJason Gunthorpe 	if (!def->write.is_ex)
836884c6c4SJason Gunthorpe 		method_elm->disabled = !(ibdev->uverbs_cmd_mask &
846884c6c4SJason Gunthorpe 					 BIT_ULL(def->write.command_num));
85a140692aSJason Gunthorpe 
86669dac1eSJason Gunthorpe 	if (!def->write.is_ex && def->func_write) {
87669dac1eSJason Gunthorpe 		method_elm->has_udata = def->write.has_udata;
88669dac1eSJason Gunthorpe 		method_elm->has_resp = def->write.has_resp;
89669dac1eSJason Gunthorpe 		method_elm->req_size = def->write.req_size;
90669dac1eSJason Gunthorpe 		method_elm->resp_size = def->write.resp_size;
91669dac1eSJason Gunthorpe 	}
92669dac1eSJason Gunthorpe 
93a140692aSJason Gunthorpe 	*cur_method_key = method_key;
946884c6c4SJason Gunthorpe 	return 0;
956884c6c4SJason Gunthorpe }
966884c6c4SJason Gunthorpe 
uapi_merge_method(struct uverbs_api * uapi,struct uverbs_api_object * obj_elm,u32 obj_key,const struct uverbs_method_def * method,bool is_driver)979ed3e5f4SJason Gunthorpe static int uapi_merge_method(struct uverbs_api *uapi,
989ed3e5f4SJason Gunthorpe 			     struct uverbs_api_object *obj_elm, u32 obj_key,
999ed3e5f4SJason Gunthorpe 			     const struct uverbs_method_def *method,
1009ed3e5f4SJason Gunthorpe 			     bool is_driver)
1019ed3e5f4SJason Gunthorpe {
1029ed3e5f4SJason Gunthorpe 	u32 method_key = obj_key | uapi_key_ioctl_method(method->id);
1039ed3e5f4SJason Gunthorpe 	struct uverbs_api_ioctl_method *method_elm;
1049ed3e5f4SJason Gunthorpe 	unsigned int i;
105c27f6aa8SJason Gunthorpe 	bool exists;
1069ed3e5f4SJason Gunthorpe 
1079ed3e5f4SJason Gunthorpe 	if (!method->attrs)
1089ed3e5f4SJason Gunthorpe 		return 0;
1099ed3e5f4SJason Gunthorpe 
110c27f6aa8SJason Gunthorpe 	method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
111c27f6aa8SJason Gunthorpe 				      &exists);
112c27f6aa8SJason Gunthorpe 	if (IS_ERR(method_elm))
1139ed3e5f4SJason Gunthorpe 		return PTR_ERR(method_elm);
114c27f6aa8SJason Gunthorpe 	if (exists) {
1159ed3e5f4SJason Gunthorpe 		/*
1169ed3e5f4SJason Gunthorpe 		 * This occurs when a driver uses ADD_UVERBS_ATTRIBUTES_SIMPLE
1179ed3e5f4SJason Gunthorpe 		 */
1189ed3e5f4SJason Gunthorpe 		if (WARN_ON(method->handler))
1199ed3e5f4SJason Gunthorpe 			return -EINVAL;
1209ed3e5f4SJason Gunthorpe 	} else {
1219ed3e5f4SJason Gunthorpe 		WARN_ON(!method->handler);
1229ed3e5f4SJason Gunthorpe 		rcu_assign_pointer(method_elm->handler, method->handler);
1239ed3e5f4SJason Gunthorpe 		if (method->handler != uverbs_destroy_def_handler)
1249ed3e5f4SJason Gunthorpe 			method_elm->driver_method = is_driver;
1259ed3e5f4SJason Gunthorpe 	}
1269ed3e5f4SJason Gunthorpe 
1279ed3e5f4SJason Gunthorpe 	for (i = 0; i != method->num_attrs; i++) {
1289ed3e5f4SJason Gunthorpe 		const struct uverbs_attr_def *attr = (*method->attrs)[i];
1299ed3e5f4SJason Gunthorpe 		struct uverbs_api_attr *attr_slot;
1309ed3e5f4SJason Gunthorpe 
1319ed3e5f4SJason Gunthorpe 		if (!attr)
1329ed3e5f4SJason Gunthorpe 			continue;
1339ed3e5f4SJason Gunthorpe 
1349ed3e5f4SJason Gunthorpe 		/*
1359ed3e5f4SJason Gunthorpe 		 * ENUM_IN contains the 'ids' pointer to the driver's .rodata,
1369ed3e5f4SJason Gunthorpe 		 * so if it is specified by a driver then it always makes this
1379ed3e5f4SJason Gunthorpe 		 * into a driver method.
1389ed3e5f4SJason Gunthorpe 		 */
1399ed3e5f4SJason Gunthorpe 		if (attr->attr.type == UVERBS_ATTR_TYPE_ENUM_IN)
1409ed3e5f4SJason Gunthorpe 			method_elm->driver_method |= is_driver;
1419ed3e5f4SJason Gunthorpe 
14270cd20aeSGuy Levi 		/*
14370cd20aeSGuy Levi 		 * Like other uobject based things we only support a single
14470cd20aeSGuy Levi 		 * uobject being NEW'd or DESTROY'd
14570cd20aeSGuy Levi 		 */
14670cd20aeSGuy Levi 		if (attr->attr.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) {
14770cd20aeSGuy Levi 			u8 access = attr->attr.u2.objs_arr.access;
14870cd20aeSGuy Levi 
14970cd20aeSGuy Levi 			if (WARN_ON(access == UVERBS_ACCESS_NEW ||
15070cd20aeSGuy Levi 				    access == UVERBS_ACCESS_DESTROY))
15170cd20aeSGuy Levi 				return -EINVAL;
15270cd20aeSGuy Levi 		}
15370cd20aeSGuy Levi 
1549ed3e5f4SJason Gunthorpe 		attr_slot =
1559ed3e5f4SJason Gunthorpe 			uapi_add_elm(uapi, method_key | uapi_key_attr(attr->id),
1569ed3e5f4SJason Gunthorpe 				     sizeof(*attr_slot));
1579ed3e5f4SJason Gunthorpe 		/* Attributes are not allowed to be modified by drivers */
1589ed3e5f4SJason Gunthorpe 		if (IS_ERR(attr_slot))
1599ed3e5f4SJason Gunthorpe 			return PTR_ERR(attr_slot);
1609ed3e5f4SJason Gunthorpe 
1619ed3e5f4SJason Gunthorpe 		attr_slot->spec = attr->attr;
1629ed3e5f4SJason Gunthorpe 	}
1639ed3e5f4SJason Gunthorpe 
1649ed3e5f4SJason Gunthorpe 	return 0;
1659ed3e5f4SJason Gunthorpe }
1669ed3e5f4SJason Gunthorpe 
uapi_merge_obj_tree(struct uverbs_api * uapi,const struct uverbs_object_def * obj,bool is_driver)1670cbf432dSJason Gunthorpe static int uapi_merge_obj_tree(struct uverbs_api *uapi,
1680cbf432dSJason Gunthorpe 			       const struct uverbs_object_def *obj,
1699ed3e5f4SJason Gunthorpe 			       bool is_driver)
1709ed3e5f4SJason Gunthorpe {
1719ed3e5f4SJason Gunthorpe 	struct uverbs_api_object *obj_elm;
1720cbf432dSJason Gunthorpe 	unsigned int i;
1739ed3e5f4SJason Gunthorpe 	u32 obj_key;
174c27f6aa8SJason Gunthorpe 	bool exists;
1750cbf432dSJason Gunthorpe 	int rc;
1769ed3e5f4SJason Gunthorpe 
1779ed3e5f4SJason Gunthorpe 	obj_key = uapi_key_obj(obj->id);
178c27f6aa8SJason Gunthorpe 	obj_elm = uapi_add_get_elm(uapi, obj_key, sizeof(*obj_elm), &exists);
179c27f6aa8SJason Gunthorpe 	if (IS_ERR(obj_elm))
1809ed3e5f4SJason Gunthorpe 		return PTR_ERR(obj_elm);
1819ed3e5f4SJason Gunthorpe 
1829ed3e5f4SJason Gunthorpe 	if (obj->type_attrs) {
183c27f6aa8SJason Gunthorpe 		if (WARN_ON(obj_elm->type_attrs))
184c27f6aa8SJason Gunthorpe 			return -EINVAL;
185c27f6aa8SJason Gunthorpe 
18604ca16ccSYishai Hadas 		obj_elm->id = obj->id;
187c27f6aa8SJason Gunthorpe 		obj_elm->type_attrs = obj->type_attrs;
1880cbf432dSJason Gunthorpe 		obj_elm->type_class = obj->type_attrs->type_class;
1899ed3e5f4SJason Gunthorpe 		/*
1906bf8f22aSYishai Hadas 		 * Today drivers are only permitted to use idr_class and
1916bf8f22aSYishai Hadas 		 * fd_class types. We can revoke the IDR types during
1926bf8f22aSYishai Hadas 		 * disassociation, and the FD types require the driver to use
1936bf8f22aSYishai Hadas 		 * struct file_operations.owner to prevent the driver module
1946bf8f22aSYishai Hadas 		 * code from unloading while the file is open. This provides
195f7c8416cSJason Gunthorpe 		 * enough safety that uverbs_uobject_fd_release() will
196f7c8416cSJason Gunthorpe 		 * continue to work.  Drivers using FD are responsible to
197f7c8416cSJason Gunthorpe 		 * handle disassociation of the device on their own.
1989ed3e5f4SJason Gunthorpe 		 */
199c27f6aa8SJason Gunthorpe 		if (WARN_ON(is_driver &&
2006bf8f22aSYishai Hadas 			    obj->type_attrs->type_class != &uverbs_idr_class &&
2016bf8f22aSYishai Hadas 			    obj->type_attrs->type_class != &uverbs_fd_class))
2029ed3e5f4SJason Gunthorpe 			return -EINVAL;
2039ed3e5f4SJason Gunthorpe 	}
2049ed3e5f4SJason Gunthorpe 
2059ed3e5f4SJason Gunthorpe 	if (!obj->methods)
2060cbf432dSJason Gunthorpe 		return 0;
2079ed3e5f4SJason Gunthorpe 
2080cbf432dSJason Gunthorpe 	for (i = 0; i != obj->num_methods; i++) {
2090cbf432dSJason Gunthorpe 		const struct uverbs_method_def *method = (*obj->methods)[i];
2100cbf432dSJason Gunthorpe 
2119ed3e5f4SJason Gunthorpe 		if (!method)
2129ed3e5f4SJason Gunthorpe 			continue;
2139ed3e5f4SJason Gunthorpe 
2149ed3e5f4SJason Gunthorpe 		rc = uapi_merge_method(uapi, obj_elm, obj_key, method,
2159ed3e5f4SJason Gunthorpe 				       is_driver);
2169ed3e5f4SJason Gunthorpe 		if (rc)
2179ed3e5f4SJason Gunthorpe 			return rc;
2189ed3e5f4SJason Gunthorpe 	}
2199ed3e5f4SJason Gunthorpe 
2209ed3e5f4SJason Gunthorpe 	return 0;
2219ed3e5f4SJason Gunthorpe }
2229ed3e5f4SJason Gunthorpe 
uapi_disable_elm(struct uverbs_api * uapi,const struct uapi_definition * def,u32 obj_key,u32 method_key)2236829c1c2SJason Gunthorpe static int uapi_disable_elm(struct uverbs_api *uapi,
2246829c1c2SJason Gunthorpe 			    const struct uapi_definition *def,
225a140692aSJason Gunthorpe 			    u32 obj_key,
226a140692aSJason Gunthorpe 			    u32 method_key)
2276829c1c2SJason Gunthorpe {
2286829c1c2SJason Gunthorpe 	bool exists;
2296829c1c2SJason Gunthorpe 
2306829c1c2SJason Gunthorpe 	if (def->scope == UAPI_SCOPE_OBJECT) {
2316829c1c2SJason Gunthorpe 		struct uverbs_api_object *obj_elm;
2326829c1c2SJason Gunthorpe 
2336829c1c2SJason Gunthorpe 		obj_elm = uapi_add_get_elm(
2346829c1c2SJason Gunthorpe 			uapi, obj_key, sizeof(*obj_elm), &exists);
2356829c1c2SJason Gunthorpe 		if (IS_ERR(obj_elm))
2366829c1c2SJason Gunthorpe 			return PTR_ERR(obj_elm);
2376829c1c2SJason Gunthorpe 		obj_elm->disabled = 1;
2386829c1c2SJason Gunthorpe 		return 0;
2396829c1c2SJason Gunthorpe 	}
2406829c1c2SJason Gunthorpe 
241a140692aSJason Gunthorpe 	if (def->scope == UAPI_SCOPE_METHOD &&
242a140692aSJason Gunthorpe 	    uapi_key_is_ioctl_method(method_key)) {
243a140692aSJason Gunthorpe 		struct uverbs_api_ioctl_method *method_elm;
244a140692aSJason Gunthorpe 
245a140692aSJason Gunthorpe 		method_elm = uapi_add_get_elm(uapi, method_key,
246a140692aSJason Gunthorpe 					      sizeof(*method_elm), &exists);
247a140692aSJason Gunthorpe 		if (IS_ERR(method_elm))
248a140692aSJason Gunthorpe 			return PTR_ERR(method_elm);
249a140692aSJason Gunthorpe 		method_elm->disabled = 1;
250a140692aSJason Gunthorpe 		return 0;
251a140692aSJason Gunthorpe 	}
252a140692aSJason Gunthorpe 
253a140692aSJason Gunthorpe 	if (def->scope == UAPI_SCOPE_METHOD &&
254a140692aSJason Gunthorpe 	    (uapi_key_is_write_method(method_key) ||
255a140692aSJason Gunthorpe 	     uapi_key_is_write_ex_method(method_key))) {
256a140692aSJason Gunthorpe 		struct uverbs_api_write_method *write_elm;
257a140692aSJason Gunthorpe 
258a140692aSJason Gunthorpe 		write_elm = uapi_add_get_elm(uapi, method_key,
259a140692aSJason Gunthorpe 					     sizeof(*write_elm), &exists);
260a140692aSJason Gunthorpe 		if (IS_ERR(write_elm))
261a140692aSJason Gunthorpe 			return PTR_ERR(write_elm);
262a140692aSJason Gunthorpe 		write_elm->disabled = 1;
263a140692aSJason Gunthorpe 		return 0;
264a140692aSJason Gunthorpe 	}
265a140692aSJason Gunthorpe 
2666829c1c2SJason Gunthorpe 	WARN_ON(true);
2676829c1c2SJason Gunthorpe 	return -EINVAL;
2686829c1c2SJason Gunthorpe }
2696829c1c2SJason Gunthorpe 
uapi_merge_def(struct uverbs_api * uapi,struct ib_device * ibdev,const struct uapi_definition * def_list,bool is_driver)2706829c1c2SJason Gunthorpe static int uapi_merge_def(struct uverbs_api *uapi, struct ib_device *ibdev,
2710cbf432dSJason Gunthorpe 			  const struct uapi_definition *def_list,
2720cbf432dSJason Gunthorpe 			  bool is_driver)
2730cbf432dSJason Gunthorpe {
2740cbf432dSJason Gunthorpe 	const struct uapi_definition *def = def_list;
2756829c1c2SJason Gunthorpe 	u32 cur_obj_key = UVERBS_API_KEY_ERR;
276a140692aSJason Gunthorpe 	u32 cur_method_key = UVERBS_API_KEY_ERR;
2776884c6c4SJason Gunthorpe 	bool exists;
2780cbf432dSJason Gunthorpe 	int rc;
2790cbf432dSJason Gunthorpe 
2800cbf432dSJason Gunthorpe 	if (!def_list)
2810cbf432dSJason Gunthorpe 		return 0;
2820cbf432dSJason Gunthorpe 
2830cbf432dSJason Gunthorpe 	for (;; def++) {
2840cbf432dSJason Gunthorpe 		switch ((enum uapi_definition_kind)def->kind) {
2850cbf432dSJason Gunthorpe 		case UAPI_DEF_CHAIN:
2866829c1c2SJason Gunthorpe 			rc = uapi_merge_def(uapi, ibdev, def->chain, is_driver);
2870cbf432dSJason Gunthorpe 			if (rc)
2880cbf432dSJason Gunthorpe 				return rc;
2890cbf432dSJason Gunthorpe 			continue;
2900cbf432dSJason Gunthorpe 
2910cbf432dSJason Gunthorpe 		case UAPI_DEF_CHAIN_OBJ_TREE:
2920cbf432dSJason Gunthorpe 			if (WARN_ON(def->object_start.object_id !=
2930cbf432dSJason Gunthorpe 				    def->chain_obj_tree->id))
2940cbf432dSJason Gunthorpe 				return -EINVAL;
2950cbf432dSJason Gunthorpe 
2966829c1c2SJason Gunthorpe 			cur_obj_key = uapi_key_obj(def->object_start.object_id);
2970cbf432dSJason Gunthorpe 			rc = uapi_merge_obj_tree(uapi, def->chain_obj_tree,
2980cbf432dSJason Gunthorpe 						 is_driver);
2990cbf432dSJason Gunthorpe 			if (rc)
3000cbf432dSJason Gunthorpe 				return rc;
3010cbf432dSJason Gunthorpe 			continue;
3020cbf432dSJason Gunthorpe 
3030cbf432dSJason Gunthorpe 		case UAPI_DEF_END:
3040cbf432dSJason Gunthorpe 			return 0;
3056829c1c2SJason Gunthorpe 
3066829c1c2SJason Gunthorpe 		case UAPI_DEF_IS_SUPPORTED_DEV_FN: {
3073023a1e9SKamal Heib 			void **ibdev_fn =
3083023a1e9SKamal Heib 				(void *)(&ibdev->ops) + def->needs_fn_offset;
3096829c1c2SJason Gunthorpe 
3106829c1c2SJason Gunthorpe 			if (*ibdev_fn)
3116829c1c2SJason Gunthorpe 				continue;
312a140692aSJason Gunthorpe 			rc = uapi_disable_elm(
313a140692aSJason Gunthorpe 				uapi, def, cur_obj_key, cur_method_key);
3146829c1c2SJason Gunthorpe 			if (rc)
3156829c1c2SJason Gunthorpe 				return rc;
3166829c1c2SJason Gunthorpe 			continue;
3176829c1c2SJason Gunthorpe 		}
3186829c1c2SJason Gunthorpe 
3196829c1c2SJason Gunthorpe 		case UAPI_DEF_IS_SUPPORTED_FUNC:
3206829c1c2SJason Gunthorpe 			if (def->func_is_supported(ibdev))
3216829c1c2SJason Gunthorpe 				continue;
322a140692aSJason Gunthorpe 			rc = uapi_disable_elm(
323a140692aSJason Gunthorpe 				uapi, def, cur_obj_key, cur_method_key);
3246829c1c2SJason Gunthorpe 			if (rc)
3256829c1c2SJason Gunthorpe 				return rc;
3266829c1c2SJason Gunthorpe 			continue;
3276884c6c4SJason Gunthorpe 
3286884c6c4SJason Gunthorpe 		case UAPI_DEF_OBJECT_START: {
3296884c6c4SJason Gunthorpe 			struct uverbs_api_object *obj_elm;
3306884c6c4SJason Gunthorpe 
3316884c6c4SJason Gunthorpe 			cur_obj_key = uapi_key_obj(def->object_start.object_id);
3326884c6c4SJason Gunthorpe 			obj_elm = uapi_add_get_elm(uapi, cur_obj_key,
3336884c6c4SJason Gunthorpe 						   sizeof(*obj_elm), &exists);
3346884c6c4SJason Gunthorpe 			if (IS_ERR(obj_elm))
3356884c6c4SJason Gunthorpe 				return PTR_ERR(obj_elm);
3366884c6c4SJason Gunthorpe 			continue;
3376884c6c4SJason Gunthorpe 		}
3386884c6c4SJason Gunthorpe 
3396884c6c4SJason Gunthorpe 		case UAPI_DEF_WRITE:
340a140692aSJason Gunthorpe 			rc = uapi_create_write(
341a140692aSJason Gunthorpe 				uapi, ibdev, def, cur_obj_key, &cur_method_key);
3426884c6c4SJason Gunthorpe 			if (rc)
3436884c6c4SJason Gunthorpe 				return rc;
3446884c6c4SJason Gunthorpe 			continue;
3450cbf432dSJason Gunthorpe 		}
3460cbf432dSJason Gunthorpe 		WARN_ON(true);
3470cbf432dSJason Gunthorpe 		return -EINVAL;
3480cbf432dSJason Gunthorpe 	}
3490cbf432dSJason Gunthorpe }
3500cbf432dSJason Gunthorpe 
3519ed3e5f4SJason Gunthorpe static int
uapi_finalize_ioctl_method(struct uverbs_api * uapi,struct uverbs_api_ioctl_method * method_elm,u32 method_key)3529ed3e5f4SJason Gunthorpe uapi_finalize_ioctl_method(struct uverbs_api *uapi,
3539ed3e5f4SJason Gunthorpe 			   struct uverbs_api_ioctl_method *method_elm,
3549ed3e5f4SJason Gunthorpe 			   u32 method_key)
3559ed3e5f4SJason Gunthorpe {
3569ed3e5f4SJason Gunthorpe 	struct radix_tree_iter iter;
3573a863577SJason Gunthorpe 	unsigned int num_attrs = 0;
3589ed3e5f4SJason Gunthorpe 	unsigned int max_bkey = 0;
3599ed3e5f4SJason Gunthorpe 	bool single_uobj = false;
3609ed3e5f4SJason Gunthorpe 	void __rcu **slot;
3619ed3e5f4SJason Gunthorpe 
3629ed3e5f4SJason Gunthorpe 	method_elm->destroy_bkey = UVERBS_API_ATTR_BKEY_LEN;
3639ed3e5f4SJason Gunthorpe 	radix_tree_for_each_slot (slot, &uapi->radix, &iter,
3649ed3e5f4SJason Gunthorpe 				  uapi_key_attrs_start(method_key)) {
3659ed3e5f4SJason Gunthorpe 		struct uverbs_api_attr *elm =
3669ed3e5f4SJason Gunthorpe 			rcu_dereference_protected(*slot, true);
3679ed3e5f4SJason Gunthorpe 		u32 attr_key = iter.index & UVERBS_API_ATTR_KEY_MASK;
3689ed3e5f4SJason Gunthorpe 		u32 attr_bkey = uapi_bkey_attr(attr_key);
3699ed3e5f4SJason Gunthorpe 		u8 type = elm->spec.type;
3709ed3e5f4SJason Gunthorpe 
3716884c6c4SJason Gunthorpe 		if (uapi_key_attr_to_ioctl_method(iter.index) !=
3726884c6c4SJason Gunthorpe 		    uapi_key_attr_to_ioctl_method(method_key))
3739ed3e5f4SJason Gunthorpe 			break;
3749ed3e5f4SJason Gunthorpe 
3759ed3e5f4SJason Gunthorpe 		if (elm->spec.mandatory)
3769ed3e5f4SJason Gunthorpe 			__set_bit(attr_bkey, method_elm->attr_mandatory);
3779ed3e5f4SJason Gunthorpe 
37807f05f40SJason Gunthorpe 		if (elm->spec.is_udata)
37907f05f40SJason Gunthorpe 			method_elm->has_udata = true;
38007f05f40SJason Gunthorpe 
3819ed3e5f4SJason Gunthorpe 		if (type == UVERBS_ATTR_TYPE_IDR ||
3829ed3e5f4SJason Gunthorpe 		    type == UVERBS_ATTR_TYPE_FD) {
3839ed3e5f4SJason Gunthorpe 			u8 access = elm->spec.u.obj.access;
3849ed3e5f4SJason Gunthorpe 
3859ed3e5f4SJason Gunthorpe 			/*
3869ed3e5f4SJason Gunthorpe 			 * Verbs specs may only have one NEW/DESTROY, we don't
3879ed3e5f4SJason Gunthorpe 			 * have the infrastructure to abort multiple NEW's or
3889ed3e5f4SJason Gunthorpe 			 * cope with multiple DESTROY failure.
3899ed3e5f4SJason Gunthorpe 			 */
3909ed3e5f4SJason Gunthorpe 			if (access == UVERBS_ACCESS_NEW ||
3919ed3e5f4SJason Gunthorpe 			    access == UVERBS_ACCESS_DESTROY) {
3929ed3e5f4SJason Gunthorpe 				if (WARN_ON(single_uobj))
3939ed3e5f4SJason Gunthorpe 					return -EINVAL;
3949ed3e5f4SJason Gunthorpe 
3959ed3e5f4SJason Gunthorpe 				single_uobj = true;
3969ed3e5f4SJason Gunthorpe 				if (WARN_ON(!elm->spec.mandatory))
3979ed3e5f4SJason Gunthorpe 					return -EINVAL;
3989ed3e5f4SJason Gunthorpe 			}
3999ed3e5f4SJason Gunthorpe 
4009ed3e5f4SJason Gunthorpe 			if (access == UVERBS_ACCESS_DESTROY)
4019ed3e5f4SJason Gunthorpe 				method_elm->destroy_bkey = attr_bkey;
4029ed3e5f4SJason Gunthorpe 		}
4039ed3e5f4SJason Gunthorpe 
4049ed3e5f4SJason Gunthorpe 		max_bkey = max(max_bkey, attr_bkey);
4053a863577SJason Gunthorpe 		num_attrs++;
4069ed3e5f4SJason Gunthorpe 	}
4079ed3e5f4SJason Gunthorpe 
4089ed3e5f4SJason Gunthorpe 	method_elm->key_bitmap_len = max_bkey + 1;
4099ed3e5f4SJason Gunthorpe 	WARN_ON(method_elm->key_bitmap_len > UVERBS_API_ATTR_BKEY_LEN);
4109ed3e5f4SJason Gunthorpe 
4113a863577SJason Gunthorpe 	uapi_compute_bundle_size(method_elm, num_attrs);
4129ed3e5f4SJason Gunthorpe 	return 0;
4139ed3e5f4SJason Gunthorpe }
4149ed3e5f4SJason Gunthorpe 
uapi_finalize(struct uverbs_api * uapi)4159ed3e5f4SJason Gunthorpe static int uapi_finalize(struct uverbs_api *uapi)
4169ed3e5f4SJason Gunthorpe {
4176884c6c4SJason Gunthorpe 	const struct uverbs_api_write_method **data;
4186884c6c4SJason Gunthorpe 	unsigned long max_write_ex = 0;
4196884c6c4SJason Gunthorpe 	unsigned long max_write = 0;
4209ed3e5f4SJason Gunthorpe 	struct radix_tree_iter iter;
4219ed3e5f4SJason Gunthorpe 	void __rcu **slot;
4229ed3e5f4SJason Gunthorpe 	int rc;
4236884c6c4SJason Gunthorpe 	int i;
4249ed3e5f4SJason Gunthorpe 
4259ed3e5f4SJason Gunthorpe 	radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
4269ed3e5f4SJason Gunthorpe 		struct uverbs_api_ioctl_method *method_elm =
4279ed3e5f4SJason Gunthorpe 			rcu_dereference_protected(*slot, true);
4289ed3e5f4SJason Gunthorpe 
4299ed3e5f4SJason Gunthorpe 		if (uapi_key_is_ioctl_method(iter.index)) {
4309ed3e5f4SJason Gunthorpe 			rc = uapi_finalize_ioctl_method(uapi, method_elm,
4319ed3e5f4SJason Gunthorpe 							iter.index);
4329ed3e5f4SJason Gunthorpe 			if (rc)
4339ed3e5f4SJason Gunthorpe 				return rc;
4349ed3e5f4SJason Gunthorpe 		}
4356884c6c4SJason Gunthorpe 
4366884c6c4SJason Gunthorpe 		if (uapi_key_is_write_method(iter.index))
4376884c6c4SJason Gunthorpe 			max_write = max(max_write,
4386884c6c4SJason Gunthorpe 					iter.index & UVERBS_API_ATTR_KEY_MASK);
4396884c6c4SJason Gunthorpe 		if (uapi_key_is_write_ex_method(iter.index))
4406884c6c4SJason Gunthorpe 			max_write_ex =
4416884c6c4SJason Gunthorpe 				max(max_write_ex,
4426884c6c4SJason Gunthorpe 				    iter.index & UVERBS_API_ATTR_KEY_MASK);
4436884c6c4SJason Gunthorpe 	}
4446884c6c4SJason Gunthorpe 
4456884c6c4SJason Gunthorpe 	uapi->notsupp_method.handler = ib_uverbs_notsupp;
4466884c6c4SJason Gunthorpe 	uapi->num_write = max_write + 1;
4476884c6c4SJason Gunthorpe 	uapi->num_write_ex = max_write_ex + 1;
4486884c6c4SJason Gunthorpe 	data = kmalloc_array(uapi->num_write + uapi->num_write_ex,
4496884c6c4SJason Gunthorpe 			     sizeof(*uapi->write_methods), GFP_KERNEL);
450*0ea8bb08SJiasheng Jiang 	if (!data)
451*0ea8bb08SJiasheng Jiang 		return -ENOMEM;
452*0ea8bb08SJiasheng Jiang 
4536884c6c4SJason Gunthorpe 	for (i = 0; i != uapi->num_write + uapi->num_write_ex; i++)
4546884c6c4SJason Gunthorpe 		data[i] = &uapi->notsupp_method;
4556884c6c4SJason Gunthorpe 	uapi->write_methods = data;
4566884c6c4SJason Gunthorpe 	uapi->write_ex_methods = data + uapi->num_write;
4576884c6c4SJason Gunthorpe 
4586884c6c4SJason Gunthorpe 	radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
4596884c6c4SJason Gunthorpe 		if (uapi_key_is_write_method(iter.index))
4606884c6c4SJason Gunthorpe 			uapi->write_methods[iter.index &
4616884c6c4SJason Gunthorpe 					    UVERBS_API_ATTR_KEY_MASK] =
4626884c6c4SJason Gunthorpe 				rcu_dereference_protected(*slot, true);
4636884c6c4SJason Gunthorpe 		if (uapi_key_is_write_ex_method(iter.index))
4646884c6c4SJason Gunthorpe 			uapi->write_ex_methods[iter.index &
4656884c6c4SJason Gunthorpe 					       UVERBS_API_ATTR_KEY_MASK] =
4666884c6c4SJason Gunthorpe 				rcu_dereference_protected(*slot, true);
4679ed3e5f4SJason Gunthorpe 	}
4689ed3e5f4SJason Gunthorpe 
4699ed3e5f4SJason Gunthorpe 	return 0;
4709ed3e5f4SJason Gunthorpe }
4719ed3e5f4SJason Gunthorpe 
uapi_remove_range(struct uverbs_api * uapi,u32 start,u32 last)4726829c1c2SJason Gunthorpe static void uapi_remove_range(struct uverbs_api *uapi, u32 start, u32 last)
4739ed3e5f4SJason Gunthorpe {
4749ed3e5f4SJason Gunthorpe 	struct radix_tree_iter iter;
4759ed3e5f4SJason Gunthorpe 	void __rcu **slot;
4769ed3e5f4SJason Gunthorpe 
4776829c1c2SJason Gunthorpe 	radix_tree_for_each_slot (slot, &uapi->radix, &iter, start) {
4786829c1c2SJason Gunthorpe 		if (iter.index > last)
4799ed3e5f4SJason Gunthorpe 			return;
4809ed3e5f4SJason Gunthorpe 		kfree(rcu_dereference_protected(*slot, true));
4819ed3e5f4SJason Gunthorpe 		radix_tree_iter_delete(&uapi->radix, &iter, slot);
4829ed3e5f4SJason Gunthorpe 	}
4836829c1c2SJason Gunthorpe }
4846829c1c2SJason Gunthorpe 
uapi_remove_object(struct uverbs_api * uapi,u32 obj_key)4856829c1c2SJason Gunthorpe static void uapi_remove_object(struct uverbs_api *uapi, u32 obj_key)
4866829c1c2SJason Gunthorpe {
4876829c1c2SJason Gunthorpe 	uapi_remove_range(uapi, obj_key,
4886829c1c2SJason Gunthorpe 			  obj_key | UVERBS_API_METHOD_KEY_MASK |
4896829c1c2SJason Gunthorpe 				  UVERBS_API_ATTR_KEY_MASK);
4906829c1c2SJason Gunthorpe }
4916829c1c2SJason Gunthorpe 
uapi_remove_method(struct uverbs_api * uapi,u32 method_key)4926829c1c2SJason Gunthorpe static void uapi_remove_method(struct uverbs_api *uapi, u32 method_key)
4936829c1c2SJason Gunthorpe {
4946829c1c2SJason Gunthorpe 	uapi_remove_range(uapi, method_key,
4956829c1c2SJason Gunthorpe 			  method_key | UVERBS_API_ATTR_KEY_MASK);
4966829c1c2SJason Gunthorpe }
4976829c1c2SJason Gunthorpe 
4986829c1c2SJason Gunthorpe 
uapi_get_obj_id(struct uverbs_attr_spec * spec)4996829c1c2SJason Gunthorpe static u32 uapi_get_obj_id(struct uverbs_attr_spec *spec)
5006829c1c2SJason Gunthorpe {
5016829c1c2SJason Gunthorpe 	if (spec->type == UVERBS_ATTR_TYPE_IDR ||
5026829c1c2SJason Gunthorpe 	    spec->type == UVERBS_ATTR_TYPE_FD)
5036829c1c2SJason Gunthorpe 		return spec->u.obj.obj_type;
5046829c1c2SJason Gunthorpe 	if (spec->type == UVERBS_ATTR_TYPE_IDRS_ARRAY)
5056829c1c2SJason Gunthorpe 		return spec->u2.objs_arr.obj_type;
5066829c1c2SJason Gunthorpe 	return UVERBS_API_KEY_ERR;
5076829c1c2SJason Gunthorpe }
5086829c1c2SJason Gunthorpe 
uapi_key_okay(u32 key)5096884c6c4SJason Gunthorpe static void uapi_key_okay(u32 key)
5106884c6c4SJason Gunthorpe {
5116884c6c4SJason Gunthorpe 	unsigned int count = 0;
5126884c6c4SJason Gunthorpe 
5136884c6c4SJason Gunthorpe 	if (uapi_key_is_object(key))
5146884c6c4SJason Gunthorpe 		count++;
5156884c6c4SJason Gunthorpe 	if (uapi_key_is_ioctl_method(key))
5166884c6c4SJason Gunthorpe 		count++;
5176884c6c4SJason Gunthorpe 	if (uapi_key_is_write_method(key))
5186884c6c4SJason Gunthorpe 		count++;
5196884c6c4SJason Gunthorpe 	if (uapi_key_is_write_ex_method(key))
5206884c6c4SJason Gunthorpe 		count++;
5216884c6c4SJason Gunthorpe 	if (uapi_key_is_attr(key))
5226884c6c4SJason Gunthorpe 		count++;
5233cea7b4aSWenpeng Liang 	WARN(count != 1, "Bad count %u key=%x", count, key);
5246884c6c4SJason Gunthorpe }
5256884c6c4SJason Gunthorpe 
uapi_finalize_disable(struct uverbs_api * uapi)5266829c1c2SJason Gunthorpe static void uapi_finalize_disable(struct uverbs_api *uapi)
5276829c1c2SJason Gunthorpe {
5286829c1c2SJason Gunthorpe 	struct radix_tree_iter iter;
5296829c1c2SJason Gunthorpe 	u32 starting_key = 0;
5306829c1c2SJason Gunthorpe 	bool scan_again = false;
5316829c1c2SJason Gunthorpe 	void __rcu **slot;
5326829c1c2SJason Gunthorpe 
5336829c1c2SJason Gunthorpe again:
5346829c1c2SJason Gunthorpe 	radix_tree_for_each_slot (slot, &uapi->radix, &iter, starting_key) {
5356884c6c4SJason Gunthorpe 		uapi_key_okay(iter.index);
5366884c6c4SJason Gunthorpe 
5376829c1c2SJason Gunthorpe 		if (uapi_key_is_object(iter.index)) {
5386829c1c2SJason Gunthorpe 			struct uverbs_api_object *obj_elm =
5396829c1c2SJason Gunthorpe 				rcu_dereference_protected(*slot, true);
5406829c1c2SJason Gunthorpe 
5416829c1c2SJason Gunthorpe 			if (obj_elm->disabled) {
5426829c1c2SJason Gunthorpe 				/* Have to check all the attrs again */
5436829c1c2SJason Gunthorpe 				scan_again = true;
5446829c1c2SJason Gunthorpe 				starting_key = iter.index;
5456829c1c2SJason Gunthorpe 				uapi_remove_object(uapi, iter.index);
5466829c1c2SJason Gunthorpe 				goto again;
5476829c1c2SJason Gunthorpe 			}
5486829c1c2SJason Gunthorpe 			continue;
5496829c1c2SJason Gunthorpe 		}
5506829c1c2SJason Gunthorpe 
5516829c1c2SJason Gunthorpe 		if (uapi_key_is_ioctl_method(iter.index)) {
5526829c1c2SJason Gunthorpe 			struct uverbs_api_ioctl_method *method_elm =
5536829c1c2SJason Gunthorpe 				rcu_dereference_protected(*slot, true);
5546829c1c2SJason Gunthorpe 
5556829c1c2SJason Gunthorpe 			if (method_elm->disabled) {
5566829c1c2SJason Gunthorpe 				starting_key = iter.index;
5576829c1c2SJason Gunthorpe 				uapi_remove_method(uapi, iter.index);
5586829c1c2SJason Gunthorpe 				goto again;
5596829c1c2SJason Gunthorpe 			}
5606829c1c2SJason Gunthorpe 			continue;
5616829c1c2SJason Gunthorpe 		}
5626829c1c2SJason Gunthorpe 
5636884c6c4SJason Gunthorpe 		if (uapi_key_is_write_method(iter.index) ||
5646884c6c4SJason Gunthorpe 		    uapi_key_is_write_ex_method(iter.index)) {
5656884c6c4SJason Gunthorpe 			struct uverbs_api_write_method *method_elm =
5666884c6c4SJason Gunthorpe 				rcu_dereference_protected(*slot, true);
5676884c6c4SJason Gunthorpe 
5686884c6c4SJason Gunthorpe 			if (method_elm->disabled) {
5696884c6c4SJason Gunthorpe 				kfree(method_elm);
5706884c6c4SJason Gunthorpe 				radix_tree_iter_delete(&uapi->radix, &iter, slot);
5716884c6c4SJason Gunthorpe 			}
5726884c6c4SJason Gunthorpe 			continue;
5736884c6c4SJason Gunthorpe 		}
5746884c6c4SJason Gunthorpe 
5756829c1c2SJason Gunthorpe 		if (uapi_key_is_attr(iter.index)) {
5766829c1c2SJason Gunthorpe 			struct uverbs_api_attr *attr_elm =
5776829c1c2SJason Gunthorpe 				rcu_dereference_protected(*slot, true);
5786829c1c2SJason Gunthorpe 			const struct uverbs_api_object *tmp_obj;
5796829c1c2SJason Gunthorpe 			u32 obj_key;
5806829c1c2SJason Gunthorpe 
5816829c1c2SJason Gunthorpe 			/*
5826829c1c2SJason Gunthorpe 			 * If the method has a mandatory object handle
5836829c1c2SJason Gunthorpe 			 * attribute which relies on an object which is not
5846829c1c2SJason Gunthorpe 			 * present then the entire method is uncallable.
5856829c1c2SJason Gunthorpe 			 */
5866829c1c2SJason Gunthorpe 			if (!attr_elm->spec.mandatory)
5876829c1c2SJason Gunthorpe 				continue;
5886829c1c2SJason Gunthorpe 			obj_key = uapi_get_obj_id(&attr_elm->spec);
5896829c1c2SJason Gunthorpe 			if (obj_key == UVERBS_API_KEY_ERR)
5906829c1c2SJason Gunthorpe 				continue;
5916829c1c2SJason Gunthorpe 			tmp_obj = uapi_get_object(uapi, obj_key);
5924d7e8cc5SYishai Hadas 			if (IS_ERR(tmp_obj)) {
5934d7e8cc5SYishai Hadas 				if (PTR_ERR(tmp_obj) == -ENOMSG)
5946829c1c2SJason Gunthorpe 					continue;
5954d7e8cc5SYishai Hadas 			} else {
5964d7e8cc5SYishai Hadas 				if (!tmp_obj->disabled)
5974d7e8cc5SYishai Hadas 					continue;
5984d7e8cc5SYishai Hadas 			}
5996829c1c2SJason Gunthorpe 
6006829c1c2SJason Gunthorpe 			starting_key = iter.index;
6016829c1c2SJason Gunthorpe 			uapi_remove_method(
6026829c1c2SJason Gunthorpe 				uapi,
6036829c1c2SJason Gunthorpe 				iter.index & (UVERBS_API_OBJ_KEY_MASK |
6046829c1c2SJason Gunthorpe 					      UVERBS_API_METHOD_KEY_MASK));
6056829c1c2SJason Gunthorpe 			goto again;
6066829c1c2SJason Gunthorpe 		}
6076829c1c2SJason Gunthorpe 
6086829c1c2SJason Gunthorpe 		WARN_ON(false);
6096829c1c2SJason Gunthorpe 	}
6106829c1c2SJason Gunthorpe 
6116829c1c2SJason Gunthorpe 	if (!scan_again)
6126829c1c2SJason Gunthorpe 		return;
6136829c1c2SJason Gunthorpe 	scan_again = false;
6146829c1c2SJason Gunthorpe 	starting_key = 0;
6156829c1c2SJason Gunthorpe 	goto again;
6166829c1c2SJason Gunthorpe }
6176829c1c2SJason Gunthorpe 
uverbs_destroy_api(struct uverbs_api * uapi)6186829c1c2SJason Gunthorpe void uverbs_destroy_api(struct uverbs_api *uapi)
6196829c1c2SJason Gunthorpe {
6206829c1c2SJason Gunthorpe 	if (!uapi)
6216829c1c2SJason Gunthorpe 		return;
6226829c1c2SJason Gunthorpe 
6236829c1c2SJason Gunthorpe 	uapi_remove_range(uapi, 0, U32_MAX);
6246884c6c4SJason Gunthorpe 	kfree(uapi->write_methods);
625a9360abdSMark Bloch 	kfree(uapi);
6269ed3e5f4SJason Gunthorpe }
6279ed3e5f4SJason Gunthorpe 
6280cbf432dSJason Gunthorpe static const struct uapi_definition uverbs_core_api[] = {
6293e032c0eSJason Gunthorpe 	UAPI_DEF_CHAIN(uverbs_def_obj_async_fd),
6300bd01f3dSJason Gunthorpe 	UAPI_DEF_CHAIN(uverbs_def_obj_counters),
6310bd01f3dSJason Gunthorpe 	UAPI_DEF_CHAIN(uverbs_def_obj_cq),
6324785860eSJason Gunthorpe 	UAPI_DEF_CHAIN(uverbs_def_obj_device),
6330bd01f3dSJason Gunthorpe 	UAPI_DEF_CHAIN(uverbs_def_obj_dm),
6340bd01f3dSJason Gunthorpe 	UAPI_DEF_CHAIN(uverbs_def_obj_flow_action),
6350cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(uverbs_def_obj_intf),
6360bd01f3dSJason Gunthorpe 	UAPI_DEF_CHAIN(uverbs_def_obj_mr),
6376d1e7ba2SYishai Hadas 	UAPI_DEF_CHAIN(uverbs_def_obj_qp),
638c3eab946SYishai Hadas 	UAPI_DEF_CHAIN(uverbs_def_obj_srq),
639ef3bc084SYishai Hadas 	UAPI_DEF_CHAIN(uverbs_def_obj_wq),
640d120c3c9SJason Gunthorpe 	UAPI_DEF_CHAIN(uverbs_def_write_intf),
6410cbf432dSJason Gunthorpe 	{},
6420cbf432dSJason Gunthorpe };
6430cbf432dSJason Gunthorpe 
uverbs_alloc_api(struct ib_device * ibdev)6446829c1c2SJason Gunthorpe struct uverbs_api *uverbs_alloc_api(struct ib_device *ibdev)
6459ed3e5f4SJason Gunthorpe {
6469ed3e5f4SJason Gunthorpe 	struct uverbs_api *uapi;
6479ed3e5f4SJason Gunthorpe 	int rc;
6489ed3e5f4SJason Gunthorpe 
6499ed3e5f4SJason Gunthorpe 	uapi = kzalloc(sizeof(*uapi), GFP_KERNEL);
6509ed3e5f4SJason Gunthorpe 	if (!uapi)
6519ed3e5f4SJason Gunthorpe 		return ERR_PTR(-ENOMEM);
6529ed3e5f4SJason Gunthorpe 
6539ed3e5f4SJason Gunthorpe 	INIT_RADIX_TREE(&uapi->radix, GFP_KERNEL);
654b9560a41SJason Gunthorpe 	uapi->driver_id = ibdev->ops.driver_id;
6559ed3e5f4SJason Gunthorpe 
6566829c1c2SJason Gunthorpe 	rc = uapi_merge_def(uapi, ibdev, uverbs_core_api, false);
6579ed3e5f4SJason Gunthorpe 	if (rc)
6589ed3e5f4SJason Gunthorpe 		goto err;
6596829c1c2SJason Gunthorpe 	rc = uapi_merge_def(uapi, ibdev, ibdev->driver_def, true);
6609ed3e5f4SJason Gunthorpe 	if (rc)
6619ed3e5f4SJason Gunthorpe 		goto err;
6629ed3e5f4SJason Gunthorpe 
6636829c1c2SJason Gunthorpe 	uapi_finalize_disable(uapi);
6649ed3e5f4SJason Gunthorpe 	rc = uapi_finalize(uapi);
6659ed3e5f4SJason Gunthorpe 	if (rc)
6669ed3e5f4SJason Gunthorpe 		goto err;
6679ed3e5f4SJason Gunthorpe 
6689ed3e5f4SJason Gunthorpe 	return uapi;
6699ed3e5f4SJason Gunthorpe err:
6709ed3e5f4SJason Gunthorpe 	if (rc != -ENOMEM)
6716829c1c2SJason Gunthorpe 		dev_err(&ibdev->dev,
6726829c1c2SJason Gunthorpe 			"Setup of uverbs_api failed, kernel parsing tree description is not valid (%d)??\n",
6739ed3e5f4SJason Gunthorpe 			rc);
6749ed3e5f4SJason Gunthorpe 
6759ed3e5f4SJason Gunthorpe 	uverbs_destroy_api(uapi);
6769ed3e5f4SJason Gunthorpe 	return ERR_PTR(rc);
6779ed3e5f4SJason Gunthorpe }
6789ed3e5f4SJason Gunthorpe 
6799ed3e5f4SJason Gunthorpe /*
6809ed3e5f4SJason Gunthorpe  * The pre version is done before destroying the HW objects, it only blocks
6819ed3e5f4SJason Gunthorpe  * off method access. All methods that require the ib_dev or the module data
6829ed3e5f4SJason Gunthorpe  * must test one of these assignments prior to continuing.
6839ed3e5f4SJason Gunthorpe  */
uverbs_disassociate_api_pre(struct ib_uverbs_device * uverbs_dev)6849ed3e5f4SJason Gunthorpe void uverbs_disassociate_api_pre(struct ib_uverbs_device *uverbs_dev)
6859ed3e5f4SJason Gunthorpe {
6869ed3e5f4SJason Gunthorpe 	struct uverbs_api *uapi = uverbs_dev->uapi;
6879ed3e5f4SJason Gunthorpe 	struct radix_tree_iter iter;
6889ed3e5f4SJason Gunthorpe 	void __rcu **slot;
6899ed3e5f4SJason Gunthorpe 
6909ed3e5f4SJason Gunthorpe 	rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
6919ed3e5f4SJason Gunthorpe 
6929ed3e5f4SJason Gunthorpe 	radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
6939ed3e5f4SJason Gunthorpe 		if (uapi_key_is_ioctl_method(iter.index)) {
6949ed3e5f4SJason Gunthorpe 			struct uverbs_api_ioctl_method *method_elm =
6959ed3e5f4SJason Gunthorpe 				rcu_dereference_protected(*slot, true);
6969ed3e5f4SJason Gunthorpe 
6979ed3e5f4SJason Gunthorpe 			if (method_elm->driver_method)
6989ed3e5f4SJason Gunthorpe 				rcu_assign_pointer(method_elm->handler, NULL);
6999ed3e5f4SJason Gunthorpe 		}
7009ed3e5f4SJason Gunthorpe 	}
7019ed3e5f4SJason Gunthorpe 
7029ed3e5f4SJason Gunthorpe 	synchronize_srcu(&uverbs_dev->disassociate_srcu);
7039ed3e5f4SJason Gunthorpe }
7049ed3e5f4SJason Gunthorpe 
7059ed3e5f4SJason Gunthorpe /*
7069ed3e5f4SJason Gunthorpe  * Called when a driver disassociates from the ib_uverbs_device. The
7079ed3e5f4SJason Gunthorpe  * assumption is that the driver module will unload after. Replace everything
7089ed3e5f4SJason Gunthorpe  * related to the driver with NULL as a safety measure.
7099ed3e5f4SJason Gunthorpe  */
uverbs_disassociate_api(struct uverbs_api * uapi)7109ed3e5f4SJason Gunthorpe void uverbs_disassociate_api(struct uverbs_api *uapi)
7119ed3e5f4SJason Gunthorpe {
7129ed3e5f4SJason Gunthorpe 	struct radix_tree_iter iter;
7139ed3e5f4SJason Gunthorpe 	void __rcu **slot;
7149ed3e5f4SJason Gunthorpe 
7159ed3e5f4SJason Gunthorpe 	radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
7169ed3e5f4SJason Gunthorpe 		if (uapi_key_is_object(iter.index)) {
7179ed3e5f4SJason Gunthorpe 			struct uverbs_api_object *object_elm =
7189ed3e5f4SJason Gunthorpe 				rcu_dereference_protected(*slot, true);
7199ed3e5f4SJason Gunthorpe 
7209ed3e5f4SJason Gunthorpe 			/*
7219ed3e5f4SJason Gunthorpe 			 * Some type_attrs are in the driver module. We don't
7229ed3e5f4SJason Gunthorpe 			 * bother to keep track of which since there should be
7239ed3e5f4SJason Gunthorpe 			 * no use of this after disassociate.
7249ed3e5f4SJason Gunthorpe 			 */
7259ed3e5f4SJason Gunthorpe 			object_elm->type_attrs = NULL;
7269ed3e5f4SJason Gunthorpe 		} else if (uapi_key_is_attr(iter.index)) {
7279ed3e5f4SJason Gunthorpe 			struct uverbs_api_attr *elm =
7289ed3e5f4SJason Gunthorpe 				rcu_dereference_protected(*slot, true);
7299ed3e5f4SJason Gunthorpe 
7309ed3e5f4SJason Gunthorpe 			if (elm->spec.type == UVERBS_ATTR_TYPE_ENUM_IN)
7319ed3e5f4SJason Gunthorpe 				elm->spec.u2.enum_def.ids = NULL;
7329ed3e5f4SJason Gunthorpe 		}
7339ed3e5f4SJason Gunthorpe 	}
7349ed3e5f4SJason Gunthorpe }
735