.. | .. |
---|
8 | 8 | #include "rdma_core.h" |
---|
9 | 9 | #include "uverbs.h" |
---|
10 | 10 | |
---|
| 11 | +static int ib_uverbs_notsupp(struct uverbs_attr_bundle *attrs) |
---|
| 12 | +{ |
---|
| 13 | + return -EOPNOTSUPP; |
---|
| 14 | +} |
---|
| 15 | + |
---|
11 | 16 | static void *uapi_add_elm(struct uverbs_api *uapi, u32 key, size_t alloc_size) |
---|
12 | 17 | { |
---|
13 | 18 | void *elm; |
---|
.. | .. |
---|
28 | 33 | return elm; |
---|
29 | 34 | } |
---|
30 | 35 | |
---|
| 36 | +static void *uapi_add_get_elm(struct uverbs_api *uapi, u32 key, |
---|
| 37 | + size_t alloc_size, bool *exists) |
---|
| 38 | +{ |
---|
| 39 | + void *elm; |
---|
| 40 | + |
---|
| 41 | + elm = uapi_add_elm(uapi, key, alloc_size); |
---|
| 42 | + if (!IS_ERR(elm)) { |
---|
| 43 | + *exists = false; |
---|
| 44 | + return elm; |
---|
| 45 | + } |
---|
| 46 | + |
---|
| 47 | + if (elm != ERR_PTR(-EEXIST)) |
---|
| 48 | + return elm; |
---|
| 49 | + |
---|
| 50 | + elm = radix_tree_lookup(&uapi->radix, key); |
---|
| 51 | + if (WARN_ON(!elm)) |
---|
| 52 | + return ERR_PTR(-EINVAL); |
---|
| 53 | + *exists = true; |
---|
| 54 | + return elm; |
---|
| 55 | +} |
---|
| 56 | + |
---|
| 57 | +static int uapi_create_write(struct uverbs_api *uapi, |
---|
| 58 | + struct ib_device *ibdev, |
---|
| 59 | + const struct uapi_definition *def, |
---|
| 60 | + u32 obj_key, |
---|
| 61 | + u32 *cur_method_key) |
---|
| 62 | +{ |
---|
| 63 | + struct uverbs_api_write_method *method_elm; |
---|
| 64 | + u32 method_key = obj_key; |
---|
| 65 | + bool exists; |
---|
| 66 | + |
---|
| 67 | + if (def->write.is_ex) |
---|
| 68 | + method_key |= uapi_key_write_ex_method(def->write.command_num); |
---|
| 69 | + else |
---|
| 70 | + method_key |= uapi_key_write_method(def->write.command_num); |
---|
| 71 | + |
---|
| 72 | + method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm), |
---|
| 73 | + &exists); |
---|
| 74 | + if (IS_ERR(method_elm)) |
---|
| 75 | + return PTR_ERR(method_elm); |
---|
| 76 | + |
---|
| 77 | + if (WARN_ON(exists && (def->write.is_ex != method_elm->is_ex))) |
---|
| 78 | + return -EINVAL; |
---|
| 79 | + |
---|
| 80 | + method_elm->is_ex = def->write.is_ex; |
---|
| 81 | + method_elm->handler = def->func_write; |
---|
| 82 | + if (def->write.is_ex) |
---|
| 83 | + method_elm->disabled = !(ibdev->uverbs_ex_cmd_mask & |
---|
| 84 | + BIT_ULL(def->write.command_num)); |
---|
| 85 | + else |
---|
| 86 | + method_elm->disabled = !(ibdev->uverbs_cmd_mask & |
---|
| 87 | + BIT_ULL(def->write.command_num)); |
---|
| 88 | + |
---|
| 89 | + if (!def->write.is_ex && def->func_write) { |
---|
| 90 | + method_elm->has_udata = def->write.has_udata; |
---|
| 91 | + method_elm->has_resp = def->write.has_resp; |
---|
| 92 | + method_elm->req_size = def->write.req_size; |
---|
| 93 | + method_elm->resp_size = def->write.resp_size; |
---|
| 94 | + } |
---|
| 95 | + |
---|
| 96 | + *cur_method_key = method_key; |
---|
| 97 | + return 0; |
---|
| 98 | +} |
---|
| 99 | + |
---|
31 | 100 | static int uapi_merge_method(struct uverbs_api *uapi, |
---|
32 | 101 | struct uverbs_api_object *obj_elm, u32 obj_key, |
---|
33 | 102 | const struct uverbs_method_def *method, |
---|
.. | .. |
---|
36 | 105 | u32 method_key = obj_key | uapi_key_ioctl_method(method->id); |
---|
37 | 106 | struct uverbs_api_ioctl_method *method_elm; |
---|
38 | 107 | unsigned int i; |
---|
| 108 | + bool exists; |
---|
39 | 109 | |
---|
40 | 110 | if (!method->attrs) |
---|
41 | 111 | return 0; |
---|
42 | 112 | |
---|
43 | | - method_elm = uapi_add_elm(uapi, method_key, sizeof(*method_elm)); |
---|
44 | | - if (IS_ERR(method_elm)) { |
---|
45 | | - if (method_elm != ERR_PTR(-EEXIST)) |
---|
46 | | - return PTR_ERR(method_elm); |
---|
47 | | - |
---|
| 113 | + method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm), |
---|
| 114 | + &exists); |
---|
| 115 | + if (IS_ERR(method_elm)) |
---|
| 116 | + return PTR_ERR(method_elm); |
---|
| 117 | + if (exists) { |
---|
48 | 118 | /* |
---|
49 | 119 | * This occurs when a driver uses ADD_UVERBS_ATTRIBUTES_SIMPLE |
---|
50 | 120 | */ |
---|
51 | 121 | if (WARN_ON(method->handler)) |
---|
52 | | - return -EINVAL; |
---|
53 | | - method_elm = radix_tree_lookup(&uapi->radix, method_key); |
---|
54 | | - if (WARN_ON(!method_elm)) |
---|
55 | 122 | return -EINVAL; |
---|
56 | 123 | } else { |
---|
57 | 124 | WARN_ON(!method->handler); |
---|
.. | .. |
---|
75 | 142 | if (attr->attr.type == UVERBS_ATTR_TYPE_ENUM_IN) |
---|
76 | 143 | method_elm->driver_method |= is_driver; |
---|
77 | 144 | |
---|
| 145 | + /* |
---|
| 146 | + * Like other uobject based things we only support a single |
---|
| 147 | + * uobject being NEW'd or DESTROY'd |
---|
| 148 | + */ |
---|
| 149 | + if (attr->attr.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) { |
---|
| 150 | + u8 access = attr->attr.u2.objs_arr.access; |
---|
| 151 | + |
---|
| 152 | + if (WARN_ON(access == UVERBS_ACCESS_NEW || |
---|
| 153 | + access == UVERBS_ACCESS_DESTROY)) |
---|
| 154 | + return -EINVAL; |
---|
| 155 | + } |
---|
| 156 | + |
---|
78 | 157 | attr_slot = |
---|
79 | 158 | uapi_add_elm(uapi, method_key | uapi_key_attr(attr->id), |
---|
80 | 159 | sizeof(*attr_slot)); |
---|
.. | .. |
---|
88 | 167 | return 0; |
---|
89 | 168 | } |
---|
90 | 169 | |
---|
91 | | -static int uapi_merge_tree(struct uverbs_api *uapi, |
---|
92 | | - const struct uverbs_object_tree_def *tree, |
---|
93 | | - bool is_driver) |
---|
| 170 | +static int uapi_merge_obj_tree(struct uverbs_api *uapi, |
---|
| 171 | + const struct uverbs_object_def *obj, |
---|
| 172 | + bool is_driver) |
---|
94 | 173 | { |
---|
95 | | - unsigned int i, j; |
---|
| 174 | + struct uverbs_api_object *obj_elm; |
---|
| 175 | + unsigned int i; |
---|
| 176 | + u32 obj_key; |
---|
| 177 | + bool exists; |
---|
96 | 178 | int rc; |
---|
97 | 179 | |
---|
98 | | - if (!tree->objects) |
---|
| 180 | + obj_key = uapi_key_obj(obj->id); |
---|
| 181 | + obj_elm = uapi_add_get_elm(uapi, obj_key, sizeof(*obj_elm), &exists); |
---|
| 182 | + if (IS_ERR(obj_elm)) |
---|
| 183 | + return PTR_ERR(obj_elm); |
---|
| 184 | + |
---|
| 185 | + if (obj->type_attrs) { |
---|
| 186 | + if (WARN_ON(obj_elm->type_attrs)) |
---|
| 187 | + return -EINVAL; |
---|
| 188 | + |
---|
| 189 | + obj_elm->id = obj->id; |
---|
| 190 | + obj_elm->type_attrs = obj->type_attrs; |
---|
| 191 | + obj_elm->type_class = obj->type_attrs->type_class; |
---|
| 192 | + /* |
---|
| 193 | + * Today drivers are only permitted to use idr_class and |
---|
| 194 | + * fd_class types. We can revoke the IDR types during |
---|
| 195 | + * disassociation, and the FD types require the driver to use |
---|
| 196 | + * struct file_operations.owner to prevent the driver module |
---|
| 197 | + * code from unloading while the file is open. This provides |
---|
| 198 | + * enough safety that uverbs_uobject_fd_release() will |
---|
| 199 | + * continue to work. Drivers using FD are responsible to |
---|
| 200 | + * handle disassociation of the device on their own. |
---|
| 201 | + */ |
---|
| 202 | + if (WARN_ON(is_driver && |
---|
| 203 | + obj->type_attrs->type_class != &uverbs_idr_class && |
---|
| 204 | + obj->type_attrs->type_class != &uverbs_fd_class)) |
---|
| 205 | + return -EINVAL; |
---|
| 206 | + } |
---|
| 207 | + |
---|
| 208 | + if (!obj->methods) |
---|
99 | 209 | return 0; |
---|
100 | 210 | |
---|
101 | | - for (i = 0; i != tree->num_objects; i++) { |
---|
102 | | - const struct uverbs_object_def *obj = (*tree->objects)[i]; |
---|
103 | | - struct uverbs_api_object *obj_elm; |
---|
104 | | - u32 obj_key; |
---|
| 211 | + for (i = 0; i != obj->num_methods; i++) { |
---|
| 212 | + const struct uverbs_method_def *method = (*obj->methods)[i]; |
---|
105 | 213 | |
---|
106 | | - if (!obj) |
---|
| 214 | + if (!method) |
---|
107 | 215 | continue; |
---|
108 | 216 | |
---|
109 | | - obj_key = uapi_key_obj(obj->id); |
---|
110 | | - obj_elm = uapi_add_elm(uapi, obj_key, sizeof(*obj_elm)); |
---|
111 | | - if (IS_ERR(obj_elm)) { |
---|
112 | | - if (obj_elm != ERR_PTR(-EEXIST)) |
---|
113 | | - return PTR_ERR(obj_elm); |
---|
114 | | - |
---|
115 | | - /* This occurs when a driver uses ADD_UVERBS_METHODS */ |
---|
116 | | - if (WARN_ON(obj->type_attrs)) |
---|
117 | | - return -EINVAL; |
---|
118 | | - obj_elm = radix_tree_lookup(&uapi->radix, obj_key); |
---|
119 | | - if (WARN_ON(!obj_elm)) |
---|
120 | | - return -EINVAL; |
---|
121 | | - } else { |
---|
122 | | - obj_elm->type_attrs = obj->type_attrs; |
---|
123 | | - if (obj->type_attrs) { |
---|
124 | | - obj_elm->type_class = |
---|
125 | | - obj->type_attrs->type_class; |
---|
126 | | - /* |
---|
127 | | - * Today drivers are only permitted to use |
---|
128 | | - * idr_class types. They cannot use FD types |
---|
129 | | - * because we currently have no way to revoke |
---|
130 | | - * the fops pointer after device |
---|
131 | | - * disassociation. |
---|
132 | | - */ |
---|
133 | | - if (WARN_ON(is_driver && |
---|
134 | | - obj->type_attrs->type_class != |
---|
135 | | - &uverbs_idr_class)) |
---|
136 | | - return -EINVAL; |
---|
137 | | - } |
---|
138 | | - } |
---|
139 | | - |
---|
140 | | - if (!obj->methods) |
---|
141 | | - continue; |
---|
142 | | - |
---|
143 | | - for (j = 0; j != obj->num_methods; j++) { |
---|
144 | | - const struct uverbs_method_def *method = |
---|
145 | | - (*obj->methods)[j]; |
---|
146 | | - if (!method) |
---|
147 | | - continue; |
---|
148 | | - |
---|
149 | | - rc = uapi_merge_method(uapi, obj_elm, obj_key, method, |
---|
150 | | - is_driver); |
---|
151 | | - if (rc) |
---|
152 | | - return rc; |
---|
153 | | - } |
---|
| 217 | + rc = uapi_merge_method(uapi, obj_elm, obj_key, method, |
---|
| 218 | + is_driver); |
---|
| 219 | + if (rc) |
---|
| 220 | + return rc; |
---|
154 | 221 | } |
---|
155 | 222 | |
---|
156 | 223 | return 0; |
---|
| 224 | +} |
---|
| 225 | + |
---|
| 226 | +static int uapi_disable_elm(struct uverbs_api *uapi, |
---|
| 227 | + const struct uapi_definition *def, |
---|
| 228 | + u32 obj_key, |
---|
| 229 | + u32 method_key) |
---|
| 230 | +{ |
---|
| 231 | + bool exists; |
---|
| 232 | + |
---|
| 233 | + if (def->scope == UAPI_SCOPE_OBJECT) { |
---|
| 234 | + struct uverbs_api_object *obj_elm; |
---|
| 235 | + |
---|
| 236 | + obj_elm = uapi_add_get_elm( |
---|
| 237 | + uapi, obj_key, sizeof(*obj_elm), &exists); |
---|
| 238 | + if (IS_ERR(obj_elm)) |
---|
| 239 | + return PTR_ERR(obj_elm); |
---|
| 240 | + obj_elm->disabled = 1; |
---|
| 241 | + return 0; |
---|
| 242 | + } |
---|
| 243 | + |
---|
| 244 | + if (def->scope == UAPI_SCOPE_METHOD && |
---|
| 245 | + uapi_key_is_ioctl_method(method_key)) { |
---|
| 246 | + struct uverbs_api_ioctl_method *method_elm; |
---|
| 247 | + |
---|
| 248 | + method_elm = uapi_add_get_elm(uapi, method_key, |
---|
| 249 | + sizeof(*method_elm), &exists); |
---|
| 250 | + if (IS_ERR(method_elm)) |
---|
| 251 | + return PTR_ERR(method_elm); |
---|
| 252 | + method_elm->disabled = 1; |
---|
| 253 | + return 0; |
---|
| 254 | + } |
---|
| 255 | + |
---|
| 256 | + if (def->scope == UAPI_SCOPE_METHOD && |
---|
| 257 | + (uapi_key_is_write_method(method_key) || |
---|
| 258 | + uapi_key_is_write_ex_method(method_key))) { |
---|
| 259 | + struct uverbs_api_write_method *write_elm; |
---|
| 260 | + |
---|
| 261 | + write_elm = uapi_add_get_elm(uapi, method_key, |
---|
| 262 | + sizeof(*write_elm), &exists); |
---|
| 263 | + if (IS_ERR(write_elm)) |
---|
| 264 | + return PTR_ERR(write_elm); |
---|
| 265 | + write_elm->disabled = 1; |
---|
| 266 | + return 0; |
---|
| 267 | + } |
---|
| 268 | + |
---|
| 269 | + WARN_ON(true); |
---|
| 270 | + return -EINVAL; |
---|
| 271 | +} |
---|
| 272 | + |
---|
| 273 | +static int uapi_merge_def(struct uverbs_api *uapi, struct ib_device *ibdev, |
---|
| 274 | + const struct uapi_definition *def_list, |
---|
| 275 | + bool is_driver) |
---|
| 276 | +{ |
---|
| 277 | + const struct uapi_definition *def = def_list; |
---|
| 278 | + u32 cur_obj_key = UVERBS_API_KEY_ERR; |
---|
| 279 | + u32 cur_method_key = UVERBS_API_KEY_ERR; |
---|
| 280 | + bool exists; |
---|
| 281 | + int rc; |
---|
| 282 | + |
---|
| 283 | + if (!def_list) |
---|
| 284 | + return 0; |
---|
| 285 | + |
---|
| 286 | + for (;; def++) { |
---|
| 287 | + switch ((enum uapi_definition_kind)def->kind) { |
---|
| 288 | + case UAPI_DEF_CHAIN: |
---|
| 289 | + rc = uapi_merge_def(uapi, ibdev, def->chain, is_driver); |
---|
| 290 | + if (rc) |
---|
| 291 | + return rc; |
---|
| 292 | + continue; |
---|
| 293 | + |
---|
| 294 | + case UAPI_DEF_CHAIN_OBJ_TREE: |
---|
| 295 | + if (WARN_ON(def->object_start.object_id != |
---|
| 296 | + def->chain_obj_tree->id)) |
---|
| 297 | + return -EINVAL; |
---|
| 298 | + |
---|
| 299 | + cur_obj_key = uapi_key_obj(def->object_start.object_id); |
---|
| 300 | + rc = uapi_merge_obj_tree(uapi, def->chain_obj_tree, |
---|
| 301 | + is_driver); |
---|
| 302 | + if (rc) |
---|
| 303 | + return rc; |
---|
| 304 | + continue; |
---|
| 305 | + |
---|
| 306 | + case UAPI_DEF_END: |
---|
| 307 | + return 0; |
---|
| 308 | + |
---|
| 309 | + case UAPI_DEF_IS_SUPPORTED_DEV_FN: { |
---|
| 310 | + void **ibdev_fn = |
---|
| 311 | + (void *)(&ibdev->ops) + def->needs_fn_offset; |
---|
| 312 | + |
---|
| 313 | + if (*ibdev_fn) |
---|
| 314 | + continue; |
---|
| 315 | + rc = uapi_disable_elm( |
---|
| 316 | + uapi, def, cur_obj_key, cur_method_key); |
---|
| 317 | + if (rc) |
---|
| 318 | + return rc; |
---|
| 319 | + continue; |
---|
| 320 | + } |
---|
| 321 | + |
---|
| 322 | + case UAPI_DEF_IS_SUPPORTED_FUNC: |
---|
| 323 | + if (def->func_is_supported(ibdev)) |
---|
| 324 | + continue; |
---|
| 325 | + rc = uapi_disable_elm( |
---|
| 326 | + uapi, def, cur_obj_key, cur_method_key); |
---|
| 327 | + if (rc) |
---|
| 328 | + return rc; |
---|
| 329 | + continue; |
---|
| 330 | + |
---|
| 331 | + case UAPI_DEF_OBJECT_START: { |
---|
| 332 | + struct uverbs_api_object *obj_elm; |
---|
| 333 | + |
---|
| 334 | + cur_obj_key = uapi_key_obj(def->object_start.object_id); |
---|
| 335 | + obj_elm = uapi_add_get_elm(uapi, cur_obj_key, |
---|
| 336 | + sizeof(*obj_elm), &exists); |
---|
| 337 | + if (IS_ERR(obj_elm)) |
---|
| 338 | + return PTR_ERR(obj_elm); |
---|
| 339 | + continue; |
---|
| 340 | + } |
---|
| 341 | + |
---|
| 342 | + case UAPI_DEF_WRITE: |
---|
| 343 | + rc = uapi_create_write( |
---|
| 344 | + uapi, ibdev, def, cur_obj_key, &cur_method_key); |
---|
| 345 | + if (rc) |
---|
| 346 | + return rc; |
---|
| 347 | + continue; |
---|
| 348 | + } |
---|
| 349 | + WARN_ON(true); |
---|
| 350 | + return -EINVAL; |
---|
| 351 | + } |
---|
157 | 352 | } |
---|
158 | 353 | |
---|
159 | 354 | static int |
---|
.. | .. |
---|
176 | 371 | u32 attr_bkey = uapi_bkey_attr(attr_key); |
---|
177 | 372 | u8 type = elm->spec.type; |
---|
178 | 373 | |
---|
179 | | - if (uapi_key_attr_to_method(iter.index) != |
---|
180 | | - uapi_key_attr_to_method(method_key)) |
---|
| 374 | + if (uapi_key_attr_to_ioctl_method(iter.index) != |
---|
| 375 | + uapi_key_attr_to_ioctl_method(method_key)) |
---|
181 | 376 | break; |
---|
182 | 377 | |
---|
183 | 378 | if (elm->spec.mandatory) |
---|
184 | 379 | __set_bit(attr_bkey, method_elm->attr_mandatory); |
---|
| 380 | + |
---|
| 381 | + if (elm->spec.is_udata) |
---|
| 382 | + method_elm->has_udata = true; |
---|
185 | 383 | |
---|
186 | 384 | if (type == UVERBS_ATTR_TYPE_IDR || |
---|
187 | 385 | type == UVERBS_ATTR_TYPE_FD) { |
---|
.. | .. |
---|
219 | 417 | |
---|
220 | 418 | static int uapi_finalize(struct uverbs_api *uapi) |
---|
221 | 419 | { |
---|
| 420 | + const struct uverbs_api_write_method **data; |
---|
| 421 | + unsigned long max_write_ex = 0; |
---|
| 422 | + unsigned long max_write = 0; |
---|
222 | 423 | struct radix_tree_iter iter; |
---|
223 | 424 | void __rcu **slot; |
---|
224 | 425 | int rc; |
---|
| 426 | + int i; |
---|
225 | 427 | |
---|
226 | 428 | radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) { |
---|
227 | 429 | struct uverbs_api_ioctl_method *method_elm = |
---|
.. | .. |
---|
233 | 435 | if (rc) |
---|
234 | 436 | return rc; |
---|
235 | 437 | } |
---|
| 438 | + |
---|
| 439 | + if (uapi_key_is_write_method(iter.index)) |
---|
| 440 | + max_write = max(max_write, |
---|
| 441 | + iter.index & UVERBS_API_ATTR_KEY_MASK); |
---|
| 442 | + if (uapi_key_is_write_ex_method(iter.index)) |
---|
| 443 | + max_write_ex = |
---|
| 444 | + max(max_write_ex, |
---|
| 445 | + iter.index & UVERBS_API_ATTR_KEY_MASK); |
---|
| 446 | + } |
---|
| 447 | + |
---|
| 448 | + uapi->notsupp_method.handler = ib_uverbs_notsupp; |
---|
| 449 | + uapi->num_write = max_write + 1; |
---|
| 450 | + uapi->num_write_ex = max_write_ex + 1; |
---|
| 451 | + data = kmalloc_array(uapi->num_write + uapi->num_write_ex, |
---|
| 452 | + sizeof(*uapi->write_methods), GFP_KERNEL); |
---|
| 453 | + if (!data) |
---|
| 454 | + return -ENOMEM; |
---|
| 455 | + |
---|
| 456 | + for (i = 0; i != uapi->num_write + uapi->num_write_ex; i++) |
---|
| 457 | + data[i] = &uapi->notsupp_method; |
---|
| 458 | + uapi->write_methods = data; |
---|
| 459 | + uapi->write_ex_methods = data + uapi->num_write; |
---|
| 460 | + |
---|
| 461 | + radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) { |
---|
| 462 | + if (uapi_key_is_write_method(iter.index)) |
---|
| 463 | + uapi->write_methods[iter.index & |
---|
| 464 | + UVERBS_API_ATTR_KEY_MASK] = |
---|
| 465 | + rcu_dereference_protected(*slot, true); |
---|
| 466 | + if (uapi_key_is_write_ex_method(iter.index)) |
---|
| 467 | + uapi->write_ex_methods[iter.index & |
---|
| 468 | + UVERBS_API_ATTR_KEY_MASK] = |
---|
| 469 | + rcu_dereference_protected(*slot, true); |
---|
236 | 470 | } |
---|
237 | 471 | |
---|
238 | 472 | return 0; |
---|
239 | 473 | } |
---|
240 | 474 | |
---|
241 | | -void uverbs_destroy_api(struct uverbs_api *uapi) |
---|
| 475 | +static void uapi_remove_range(struct uverbs_api *uapi, u32 start, u32 last) |
---|
242 | 476 | { |
---|
243 | 477 | struct radix_tree_iter iter; |
---|
244 | 478 | void __rcu **slot; |
---|
245 | 479 | |
---|
246 | | - if (!uapi) |
---|
247 | | - return; |
---|
248 | | - |
---|
249 | | - radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) { |
---|
| 480 | + radix_tree_for_each_slot (slot, &uapi->radix, &iter, start) { |
---|
| 481 | + if (iter.index > last) |
---|
| 482 | + return; |
---|
250 | 483 | kfree(rcu_dereference_protected(*slot, true)); |
---|
251 | 484 | radix_tree_iter_delete(&uapi->radix, &iter, slot); |
---|
252 | 485 | } |
---|
| 486 | +} |
---|
| 487 | + |
---|
| 488 | +static void uapi_remove_object(struct uverbs_api *uapi, u32 obj_key) |
---|
| 489 | +{ |
---|
| 490 | + uapi_remove_range(uapi, obj_key, |
---|
| 491 | + obj_key | UVERBS_API_METHOD_KEY_MASK | |
---|
| 492 | + UVERBS_API_ATTR_KEY_MASK); |
---|
| 493 | +} |
---|
| 494 | + |
---|
| 495 | +static void uapi_remove_method(struct uverbs_api *uapi, u32 method_key) |
---|
| 496 | +{ |
---|
| 497 | + uapi_remove_range(uapi, method_key, |
---|
| 498 | + method_key | UVERBS_API_ATTR_KEY_MASK); |
---|
| 499 | +} |
---|
| 500 | + |
---|
| 501 | + |
---|
| 502 | +static u32 uapi_get_obj_id(struct uverbs_attr_spec *spec) |
---|
| 503 | +{ |
---|
| 504 | + if (spec->type == UVERBS_ATTR_TYPE_IDR || |
---|
| 505 | + spec->type == UVERBS_ATTR_TYPE_FD) |
---|
| 506 | + return spec->u.obj.obj_type; |
---|
| 507 | + if (spec->type == UVERBS_ATTR_TYPE_IDRS_ARRAY) |
---|
| 508 | + return spec->u2.objs_arr.obj_type; |
---|
| 509 | + return UVERBS_API_KEY_ERR; |
---|
| 510 | +} |
---|
| 511 | + |
---|
| 512 | +static void uapi_key_okay(u32 key) |
---|
| 513 | +{ |
---|
| 514 | + unsigned int count = 0; |
---|
| 515 | + |
---|
| 516 | + if (uapi_key_is_object(key)) |
---|
| 517 | + count++; |
---|
| 518 | + if (uapi_key_is_ioctl_method(key)) |
---|
| 519 | + count++; |
---|
| 520 | + if (uapi_key_is_write_method(key)) |
---|
| 521 | + count++; |
---|
| 522 | + if (uapi_key_is_write_ex_method(key)) |
---|
| 523 | + count++; |
---|
| 524 | + if (uapi_key_is_attr(key)) |
---|
| 525 | + count++; |
---|
| 526 | + WARN(count != 1, "Bad count %d key=%x", count, key); |
---|
| 527 | +} |
---|
| 528 | + |
---|
| 529 | +static void uapi_finalize_disable(struct uverbs_api *uapi) |
---|
| 530 | +{ |
---|
| 531 | + struct radix_tree_iter iter; |
---|
| 532 | + u32 starting_key = 0; |
---|
| 533 | + bool scan_again = false; |
---|
| 534 | + void __rcu **slot; |
---|
| 535 | + |
---|
| 536 | +again: |
---|
| 537 | + radix_tree_for_each_slot (slot, &uapi->radix, &iter, starting_key) { |
---|
| 538 | + uapi_key_okay(iter.index); |
---|
| 539 | + |
---|
| 540 | + if (uapi_key_is_object(iter.index)) { |
---|
| 541 | + struct uverbs_api_object *obj_elm = |
---|
| 542 | + rcu_dereference_protected(*slot, true); |
---|
| 543 | + |
---|
| 544 | + if (obj_elm->disabled) { |
---|
| 545 | + /* Have to check all the attrs again */ |
---|
| 546 | + scan_again = true; |
---|
| 547 | + starting_key = iter.index; |
---|
| 548 | + uapi_remove_object(uapi, iter.index); |
---|
| 549 | + goto again; |
---|
| 550 | + } |
---|
| 551 | + continue; |
---|
| 552 | + } |
---|
| 553 | + |
---|
| 554 | + if (uapi_key_is_ioctl_method(iter.index)) { |
---|
| 555 | + struct uverbs_api_ioctl_method *method_elm = |
---|
| 556 | + rcu_dereference_protected(*slot, true); |
---|
| 557 | + |
---|
| 558 | + if (method_elm->disabled) { |
---|
| 559 | + starting_key = iter.index; |
---|
| 560 | + uapi_remove_method(uapi, iter.index); |
---|
| 561 | + goto again; |
---|
| 562 | + } |
---|
| 563 | + continue; |
---|
| 564 | + } |
---|
| 565 | + |
---|
| 566 | + if (uapi_key_is_write_method(iter.index) || |
---|
| 567 | + uapi_key_is_write_ex_method(iter.index)) { |
---|
| 568 | + struct uverbs_api_write_method *method_elm = |
---|
| 569 | + rcu_dereference_protected(*slot, true); |
---|
| 570 | + |
---|
| 571 | + if (method_elm->disabled) { |
---|
| 572 | + kfree(method_elm); |
---|
| 573 | + radix_tree_iter_delete(&uapi->radix, &iter, slot); |
---|
| 574 | + } |
---|
| 575 | + continue; |
---|
| 576 | + } |
---|
| 577 | + |
---|
| 578 | + if (uapi_key_is_attr(iter.index)) { |
---|
| 579 | + struct uverbs_api_attr *attr_elm = |
---|
| 580 | + rcu_dereference_protected(*slot, true); |
---|
| 581 | + const struct uverbs_api_object *tmp_obj; |
---|
| 582 | + u32 obj_key; |
---|
| 583 | + |
---|
| 584 | + /* |
---|
| 585 | + * If the method has a mandatory object handle |
---|
| 586 | + * attribute which relies on an object which is not |
---|
| 587 | + * present then the entire method is uncallable. |
---|
| 588 | + */ |
---|
| 589 | + if (!attr_elm->spec.mandatory) |
---|
| 590 | + continue; |
---|
| 591 | + obj_key = uapi_get_obj_id(&attr_elm->spec); |
---|
| 592 | + if (obj_key == UVERBS_API_KEY_ERR) |
---|
| 593 | + continue; |
---|
| 594 | + tmp_obj = uapi_get_object(uapi, obj_key); |
---|
| 595 | + if (IS_ERR(tmp_obj)) { |
---|
| 596 | + if (PTR_ERR(tmp_obj) == -ENOMSG) |
---|
| 597 | + continue; |
---|
| 598 | + } else { |
---|
| 599 | + if (!tmp_obj->disabled) |
---|
| 600 | + continue; |
---|
| 601 | + } |
---|
| 602 | + |
---|
| 603 | + starting_key = iter.index; |
---|
| 604 | + uapi_remove_method( |
---|
| 605 | + uapi, |
---|
| 606 | + iter.index & (UVERBS_API_OBJ_KEY_MASK | |
---|
| 607 | + UVERBS_API_METHOD_KEY_MASK)); |
---|
| 608 | + goto again; |
---|
| 609 | + } |
---|
| 610 | + |
---|
| 611 | + WARN_ON(false); |
---|
| 612 | + } |
---|
| 613 | + |
---|
| 614 | + if (!scan_again) |
---|
| 615 | + return; |
---|
| 616 | + scan_again = false; |
---|
| 617 | + starting_key = 0; |
---|
| 618 | + goto again; |
---|
| 619 | +} |
---|
| 620 | + |
---|
| 621 | +void uverbs_destroy_api(struct uverbs_api *uapi) |
---|
| 622 | +{ |
---|
| 623 | + if (!uapi) |
---|
| 624 | + return; |
---|
| 625 | + |
---|
| 626 | + uapi_remove_range(uapi, 0, U32_MAX); |
---|
| 627 | + kfree(uapi->write_methods); |
---|
253 | 628 | kfree(uapi); |
---|
254 | 629 | } |
---|
255 | 630 | |
---|
256 | | -struct uverbs_api *uverbs_alloc_api( |
---|
257 | | - const struct uverbs_object_tree_def *const *driver_specs, |
---|
258 | | - enum rdma_driver_id driver_id) |
---|
| 631 | +static const struct uapi_definition uverbs_core_api[] = { |
---|
| 632 | + UAPI_DEF_CHAIN(uverbs_def_obj_async_fd), |
---|
| 633 | + UAPI_DEF_CHAIN(uverbs_def_obj_counters), |
---|
| 634 | + UAPI_DEF_CHAIN(uverbs_def_obj_cq), |
---|
| 635 | + UAPI_DEF_CHAIN(uverbs_def_obj_device), |
---|
| 636 | + UAPI_DEF_CHAIN(uverbs_def_obj_dm), |
---|
| 637 | + UAPI_DEF_CHAIN(uverbs_def_obj_flow_action), |
---|
| 638 | + UAPI_DEF_CHAIN(uverbs_def_obj_intf), |
---|
| 639 | + UAPI_DEF_CHAIN(uverbs_def_obj_mr), |
---|
| 640 | + UAPI_DEF_CHAIN(uverbs_def_obj_qp), |
---|
| 641 | + UAPI_DEF_CHAIN(uverbs_def_obj_srq), |
---|
| 642 | + UAPI_DEF_CHAIN(uverbs_def_obj_wq), |
---|
| 643 | + UAPI_DEF_CHAIN(uverbs_def_write_intf), |
---|
| 644 | + {}, |
---|
| 645 | +}; |
---|
| 646 | + |
---|
| 647 | +struct uverbs_api *uverbs_alloc_api(struct ib_device *ibdev) |
---|
259 | 648 | { |
---|
260 | 649 | struct uverbs_api *uapi; |
---|
261 | 650 | int rc; |
---|
.. | .. |
---|
265 | 654 | return ERR_PTR(-ENOMEM); |
---|
266 | 655 | |
---|
267 | 656 | INIT_RADIX_TREE(&uapi->radix, GFP_KERNEL); |
---|
268 | | - uapi->driver_id = driver_id; |
---|
| 657 | + uapi->driver_id = ibdev->ops.driver_id; |
---|
269 | 658 | |
---|
270 | | - rc = uapi_merge_tree(uapi, uverbs_default_get_objects(), false); |
---|
| 659 | + rc = uapi_merge_def(uapi, ibdev, uverbs_core_api, false); |
---|
| 660 | + if (rc) |
---|
| 661 | + goto err; |
---|
| 662 | + rc = uapi_merge_def(uapi, ibdev, ibdev->driver_def, true); |
---|
271 | 663 | if (rc) |
---|
272 | 664 | goto err; |
---|
273 | 665 | |
---|
274 | | - for (; driver_specs && *driver_specs; driver_specs++) { |
---|
275 | | - rc = uapi_merge_tree(uapi, *driver_specs, true); |
---|
276 | | - if (rc) |
---|
277 | | - goto err; |
---|
278 | | - } |
---|
279 | | - |
---|
| 666 | + uapi_finalize_disable(uapi); |
---|
280 | 667 | rc = uapi_finalize(uapi); |
---|
281 | 668 | if (rc) |
---|
282 | 669 | goto err; |
---|
.. | .. |
---|
284 | 671 | return uapi; |
---|
285 | 672 | err: |
---|
286 | 673 | if (rc != -ENOMEM) |
---|
287 | | - pr_err("Setup of uverbs_api failed, kernel parsing tree description is not valid (%d)??\n", |
---|
288 | | - rc); |
---|
| 674 | + dev_err(&ibdev->dev, |
---|
| 675 | + "Setup of uverbs_api failed, kernel parsing tree description is not valid (%d)??\n", |
---|
| 676 | + rc); |
---|
289 | 677 | |
---|
290 | 678 | uverbs_destroy_api(uapi); |
---|
291 | 679 | return ERR_PTR(rc); |
---|