]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/infiniband/core/uverbs_uapi.c
1e880f1d9d625c5eda83746d3b4b44909af23b6c
[linux.git] / drivers / infiniband / core / uverbs_uapi.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
4  */
5 #include <rdma/uverbs_ioctl.h>
6 #include <rdma/rdma_user_ioctl.h>
7 #include <linux/bitops.h>
8 #include "rdma_core.h"
9 #include "uverbs.h"
10
11 static ssize_t ib_uverbs_notsupp(struct ib_uverbs_file *file,
12                                  const char __user *buf, int in_len,
13                                  int out_len)
14 {
15         return -EOPNOTSUPP;
16 }
17
18 static int ib_uverbs_ex_notsupp(struct ib_uverbs_file *file,
19                                 struct ib_udata *ucore, struct ib_udata *uhw)
20 {
21         return -EOPNOTSUPP;
22 }
23
24 static void *uapi_add_elm(struct uverbs_api *uapi, u32 key, size_t alloc_size)
25 {
26         void *elm;
27         int rc;
28
29         if (key == UVERBS_API_KEY_ERR)
30                 return ERR_PTR(-EOVERFLOW);
31
32         elm = kzalloc(alloc_size, GFP_KERNEL);
33         rc = radix_tree_insert(&uapi->radix, key, elm);
34         if (rc) {
35                 kfree(elm);
36                 return ERR_PTR(rc);
37         }
38
39         return elm;
40 }
41
42 static void *uapi_add_get_elm(struct uverbs_api *uapi, u32 key,
43                               size_t alloc_size, bool *exists)
44 {
45         void *elm;
46
47         elm = uapi_add_elm(uapi, key, alloc_size);
48         if (!IS_ERR(elm)) {
49                 *exists = false;
50                 return elm;
51         }
52
53         if (elm != ERR_PTR(-EEXIST))
54                 return elm;
55
56         elm = radix_tree_lookup(&uapi->radix, key);
57         if (WARN_ON(!elm))
58                 return ERR_PTR(-EINVAL);
59         *exists = true;
60         return elm;
61 }
62
63 static int uapi_create_write(struct uverbs_api *uapi, struct ib_device *ibdev,
64                              const struct uapi_definition *def, u32 obj_key)
65 {
66         struct uverbs_api_write_method *method_elm;
67         u32 method_key = obj_key;
68         bool exists;
69
70         if (def->write.is_ex)
71                 method_key |= uapi_key_write_ex_method(def->write.command_num);
72         else
73                 method_key |= uapi_key_write_method(def->write.command_num);
74
75         method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
76                                       &exists);
77         if (IS_ERR(method_elm))
78                 return PTR_ERR(method_elm);
79
80         if (WARN_ON(exists && (def->write.is_ex != method_elm->is_ex ||
81                                method_elm->handler_ex || method_elm->handler)))
82                 return -EINVAL;
83
84         method_elm->is_ex = def->write.is_ex;
85         if (def->write.is_ex) {
86                 method_elm->handler_ex = def->func_write_ex;
87
88                 method_elm->disabled = !(ibdev->uverbs_ex_cmd_mask &
89                                          BIT_ULL(def->write.command_num));
90         } else {
91                 method_elm->handler = def->func_write;
92
93                 method_elm->disabled = !(ibdev->uverbs_cmd_mask &
94                                          BIT_ULL(def->write.command_num));
95         }
96         return 0;
97 }
98
99 static int uapi_merge_method(struct uverbs_api *uapi,
100                              struct uverbs_api_object *obj_elm, u32 obj_key,
101                              const struct uverbs_method_def *method,
102                              bool is_driver)
103 {
104         u32 method_key = obj_key | uapi_key_ioctl_method(method->id);
105         struct uverbs_api_ioctl_method *method_elm;
106         unsigned int i;
107         bool exists;
108
109         if (!method->attrs)
110                 return 0;
111
112         method_elm = uapi_add_get_elm(uapi, method_key, sizeof(*method_elm),
113                                       &exists);
114         if (IS_ERR(method_elm))
115                 return PTR_ERR(method_elm);
116         if (exists) {
117                 /*
118                  * This occurs when a driver uses ADD_UVERBS_ATTRIBUTES_SIMPLE
119                  */
120                 if (WARN_ON(method->handler))
121                         return -EINVAL;
122         } else {
123                 WARN_ON(!method->handler);
124                 rcu_assign_pointer(method_elm->handler, method->handler);
125                 if (method->handler != uverbs_destroy_def_handler)
126                         method_elm->driver_method = is_driver;
127         }
128
129         for (i = 0; i != method->num_attrs; i++) {
130                 const struct uverbs_attr_def *attr = (*method->attrs)[i];
131                 struct uverbs_api_attr *attr_slot;
132
133                 if (!attr)
134                         continue;
135
136                 /*
137                  * ENUM_IN contains the 'ids' pointer to the driver's .rodata,
138                  * so if it is specified by a driver then it always makes this
139                  * into a driver method.
140                  */
141                 if (attr->attr.type == UVERBS_ATTR_TYPE_ENUM_IN)
142                         method_elm->driver_method |= is_driver;
143
144                 /*
145                  * Like other uobject based things we only support a single
146                  * uobject being NEW'd or DESTROY'd
147                  */
148                 if (attr->attr.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) {
149                         u8 access = attr->attr.u2.objs_arr.access;
150
151                         if (WARN_ON(access == UVERBS_ACCESS_NEW ||
152                                     access == UVERBS_ACCESS_DESTROY))
153                                 return -EINVAL;
154                 }
155
156                 attr_slot =
157                         uapi_add_elm(uapi, method_key | uapi_key_attr(attr->id),
158                                      sizeof(*attr_slot));
159                 /* Attributes are not allowed to be modified by drivers */
160                 if (IS_ERR(attr_slot))
161                         return PTR_ERR(attr_slot);
162
163                 attr_slot->spec = attr->attr;
164         }
165
166         return 0;
167 }
168
169 static int uapi_merge_obj_tree(struct uverbs_api *uapi,
170                                const struct uverbs_object_def *obj,
171                                bool is_driver)
172 {
173         struct uverbs_api_object *obj_elm;
174         unsigned int i;
175         u32 obj_key;
176         bool exists;
177         int rc;
178
179         obj_key = uapi_key_obj(obj->id);
180         obj_elm = uapi_add_get_elm(uapi, obj_key, sizeof(*obj_elm), &exists);
181         if (IS_ERR(obj_elm))
182                 return PTR_ERR(obj_elm);
183
184         if (obj->type_attrs) {
185                 if (WARN_ON(obj_elm->type_attrs))
186                         return -EINVAL;
187
188                 obj_elm->type_attrs = obj->type_attrs;
189                 obj_elm->type_class = obj->type_attrs->type_class;
190                 /*
191                  * Today drivers are only permitted to use idr_class
192                  * types. They cannot use FD types because we currently have
193                  * no way to revoke the fops pointer after device
194                  * disassociation.
195                  */
196                 if (WARN_ON(is_driver &&
197                             obj->type_attrs->type_class != &uverbs_idr_class))
198                         return -EINVAL;
199         }
200
201         if (!obj->methods)
202                 return 0;
203
204         for (i = 0; i != obj->num_methods; i++) {
205                 const struct uverbs_method_def *method = (*obj->methods)[i];
206
207                 if (!method)
208                         continue;
209
210                 rc = uapi_merge_method(uapi, obj_elm, obj_key, method,
211                                        is_driver);
212                 if (rc)
213                         return rc;
214         }
215
216         return 0;
217 }
218
219 static int uapi_disable_elm(struct uverbs_api *uapi,
220                             const struct uapi_definition *def,
221                             u32 obj_key)
222 {
223         bool exists;
224
225         if (def->scope == UAPI_SCOPE_OBJECT) {
226                 struct uverbs_api_object *obj_elm;
227
228                 obj_elm = uapi_add_get_elm(
229                         uapi, obj_key, sizeof(*obj_elm), &exists);
230                 if (IS_ERR(obj_elm))
231                         return PTR_ERR(obj_elm);
232                 obj_elm->disabled = 1;
233                 return 0;
234         }
235
236         WARN_ON(true);
237         return -EINVAL;
238 }
239
240 static int uapi_merge_def(struct uverbs_api *uapi, struct ib_device *ibdev,
241                           const struct uapi_definition *def_list,
242                           bool is_driver)
243 {
244         const struct uapi_definition *def = def_list;
245         u32 cur_obj_key = UVERBS_API_KEY_ERR;
246         bool exists;
247         int rc;
248
249         if (!def_list)
250                 return 0;
251
252         for (;; def++) {
253                 switch ((enum uapi_definition_kind)def->kind) {
254                 case UAPI_DEF_CHAIN:
255                         rc = uapi_merge_def(uapi, ibdev, def->chain, is_driver);
256                         if (rc)
257                                 return rc;
258                         continue;
259
260                 case UAPI_DEF_CHAIN_OBJ_TREE:
261                         if (WARN_ON(def->object_start.object_id !=
262                                     def->chain_obj_tree->id))
263                                 return -EINVAL;
264
265                         cur_obj_key = uapi_key_obj(def->object_start.object_id);
266                         rc = uapi_merge_obj_tree(uapi, def->chain_obj_tree,
267                                                  is_driver);
268                         if (rc)
269                                 return rc;
270                         continue;
271
272                 case UAPI_DEF_END:
273                         return 0;
274
275                 case UAPI_DEF_IS_SUPPORTED_DEV_FN: {
276                         void **ibdev_fn = (void *)ibdev + def->needs_fn_offset;
277
278                         if (*ibdev_fn)
279                                 continue;
280                         rc = uapi_disable_elm(uapi, def, cur_obj_key);
281                         if (rc)
282                                 return rc;
283                         continue;
284                 }
285
286                 case UAPI_DEF_IS_SUPPORTED_FUNC:
287                         if (def->func_is_supported(ibdev))
288                                 continue;
289                         rc = uapi_disable_elm(uapi, def, cur_obj_key);
290                         if (rc)
291                                 return rc;
292                         continue;
293
294                 case UAPI_DEF_OBJECT_START: {
295                         struct uverbs_api_object *obj_elm;
296
297                         cur_obj_key = uapi_key_obj(def->object_start.object_id);
298                         obj_elm = uapi_add_get_elm(uapi, cur_obj_key,
299                                                    sizeof(*obj_elm), &exists);
300                         if (IS_ERR(obj_elm))
301                                 return PTR_ERR(obj_elm);
302                         continue;
303                 }
304
305                 case UAPI_DEF_WRITE:
306                         rc = uapi_create_write(uapi, ibdev, def, cur_obj_key);
307                         if (rc)
308                                 return rc;
309                         continue;
310                 }
311                 WARN_ON(true);
312                 return -EINVAL;
313         }
314 }
315
316 static int
317 uapi_finalize_ioctl_method(struct uverbs_api *uapi,
318                            struct uverbs_api_ioctl_method *method_elm,
319                            u32 method_key)
320 {
321         struct radix_tree_iter iter;
322         unsigned int num_attrs = 0;
323         unsigned int max_bkey = 0;
324         bool single_uobj = false;
325         void __rcu **slot;
326
327         method_elm->destroy_bkey = UVERBS_API_ATTR_BKEY_LEN;
328         radix_tree_for_each_slot (slot, &uapi->radix, &iter,
329                                   uapi_key_attrs_start(method_key)) {
330                 struct uverbs_api_attr *elm =
331                         rcu_dereference_protected(*slot, true);
332                 u32 attr_key = iter.index & UVERBS_API_ATTR_KEY_MASK;
333                 u32 attr_bkey = uapi_bkey_attr(attr_key);
334                 u8 type = elm->spec.type;
335
336                 if (uapi_key_attr_to_ioctl_method(iter.index) !=
337                     uapi_key_attr_to_ioctl_method(method_key))
338                         break;
339
340                 if (elm->spec.mandatory)
341                         __set_bit(attr_bkey, method_elm->attr_mandatory);
342
343                 if (type == UVERBS_ATTR_TYPE_IDR ||
344                     type == UVERBS_ATTR_TYPE_FD) {
345                         u8 access = elm->spec.u.obj.access;
346
347                         /*
348                          * Verbs specs may only have one NEW/DESTROY, we don't
349                          * have the infrastructure to abort multiple NEW's or
350                          * cope with multiple DESTROY failure.
351                          */
352                         if (access == UVERBS_ACCESS_NEW ||
353                             access == UVERBS_ACCESS_DESTROY) {
354                                 if (WARN_ON(single_uobj))
355                                         return -EINVAL;
356
357                                 single_uobj = true;
358                                 if (WARN_ON(!elm->spec.mandatory))
359                                         return -EINVAL;
360                         }
361
362                         if (access == UVERBS_ACCESS_DESTROY)
363                                 method_elm->destroy_bkey = attr_bkey;
364                 }
365
366                 max_bkey = max(max_bkey, attr_bkey);
367                 num_attrs++;
368         }
369
370         method_elm->key_bitmap_len = max_bkey + 1;
371         WARN_ON(method_elm->key_bitmap_len > UVERBS_API_ATTR_BKEY_LEN);
372
373         uapi_compute_bundle_size(method_elm, num_attrs);
374         return 0;
375 }
376
377 static int uapi_finalize(struct uverbs_api *uapi)
378 {
379         const struct uverbs_api_write_method **data;
380         unsigned long max_write_ex = 0;
381         unsigned long max_write = 0;
382         struct radix_tree_iter iter;
383         void __rcu **slot;
384         int rc;
385         int i;
386
387         radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
388                 struct uverbs_api_ioctl_method *method_elm =
389                         rcu_dereference_protected(*slot, true);
390
391                 if (uapi_key_is_ioctl_method(iter.index)) {
392                         rc = uapi_finalize_ioctl_method(uapi, method_elm,
393                                                         iter.index);
394                         if (rc)
395                                 return rc;
396                 }
397
398                 if (uapi_key_is_write_method(iter.index))
399                         max_write = max(max_write,
400                                         iter.index & UVERBS_API_ATTR_KEY_MASK);
401                 if (uapi_key_is_write_ex_method(iter.index))
402                         max_write_ex =
403                                 max(max_write_ex,
404                                     iter.index & UVERBS_API_ATTR_KEY_MASK);
405         }
406
407         uapi->notsupp_method.handler = ib_uverbs_notsupp;
408         uapi->notsupp_method.handler_ex = ib_uverbs_ex_notsupp;
409         uapi->num_write = max_write + 1;
410         uapi->num_write_ex = max_write_ex + 1;
411         data = kmalloc_array(uapi->num_write + uapi->num_write_ex,
412                              sizeof(*uapi->write_methods), GFP_KERNEL);
413         for (i = 0; i != uapi->num_write + uapi->num_write_ex; i++)
414                 data[i] = &uapi->notsupp_method;
415         uapi->write_methods = data;
416         uapi->write_ex_methods = data + uapi->num_write;
417
418         radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
419                 if (uapi_key_is_write_method(iter.index))
420                         uapi->write_methods[iter.index &
421                                             UVERBS_API_ATTR_KEY_MASK] =
422                                 rcu_dereference_protected(*slot, true);
423                 if (uapi_key_is_write_ex_method(iter.index))
424                         uapi->write_ex_methods[iter.index &
425                                                UVERBS_API_ATTR_KEY_MASK] =
426                                 rcu_dereference_protected(*slot, true);
427         }
428
429         return 0;
430 }
431
432 static void uapi_remove_range(struct uverbs_api *uapi, u32 start, u32 last)
433 {
434         struct radix_tree_iter iter;
435         void __rcu **slot;
436
437         radix_tree_for_each_slot (slot, &uapi->radix, &iter, start) {
438                 if (iter.index > last)
439                         return;
440                 kfree(rcu_dereference_protected(*slot, true));
441                 radix_tree_iter_delete(&uapi->radix, &iter, slot);
442         }
443 }
444
445 static void uapi_remove_object(struct uverbs_api *uapi, u32 obj_key)
446 {
447         uapi_remove_range(uapi, obj_key,
448                           obj_key | UVERBS_API_METHOD_KEY_MASK |
449                                   UVERBS_API_ATTR_KEY_MASK);
450 }
451
452 static void uapi_remove_method(struct uverbs_api *uapi, u32 method_key)
453 {
454         uapi_remove_range(uapi, method_key,
455                           method_key | UVERBS_API_ATTR_KEY_MASK);
456 }
457
458
459 static u32 uapi_get_obj_id(struct uverbs_attr_spec *spec)
460 {
461         if (spec->type == UVERBS_ATTR_TYPE_IDR ||
462             spec->type == UVERBS_ATTR_TYPE_FD)
463                 return spec->u.obj.obj_type;
464         if (spec->type == UVERBS_ATTR_TYPE_IDRS_ARRAY)
465                 return spec->u2.objs_arr.obj_type;
466         return UVERBS_API_KEY_ERR;
467 }
468
469 static void uapi_key_okay(u32 key)
470 {
471         unsigned int count = 0;
472
473         if (uapi_key_is_object(key))
474                 count++;
475         if (uapi_key_is_ioctl_method(key))
476                 count++;
477         if (uapi_key_is_write_method(key))
478                 count++;
479         if (uapi_key_is_write_ex_method(key))
480                 count++;
481         if (uapi_key_is_attr(key))
482                 count++;
483         WARN(count != 1, "Bad count %d key=%x", count, key);
484 }
485
486 static void uapi_finalize_disable(struct uverbs_api *uapi)
487 {
488         struct radix_tree_iter iter;
489         u32 starting_key = 0;
490         bool scan_again = false;
491         void __rcu **slot;
492
493 again:
494         radix_tree_for_each_slot (slot, &uapi->radix, &iter, starting_key) {
495                 uapi_key_okay(iter.index);
496
497                 if (uapi_key_is_object(iter.index)) {
498                         struct uverbs_api_object *obj_elm =
499                                 rcu_dereference_protected(*slot, true);
500
501                         if (obj_elm->disabled) {
502                                 /* Have to check all the attrs again */
503                                 scan_again = true;
504                                 starting_key = iter.index;
505                                 uapi_remove_object(uapi, iter.index);
506                                 goto again;
507                         }
508                         continue;
509                 }
510
511                 if (uapi_key_is_ioctl_method(iter.index)) {
512                         struct uverbs_api_ioctl_method *method_elm =
513                                 rcu_dereference_protected(*slot, true);
514
515                         if (method_elm->disabled) {
516                                 starting_key = iter.index;
517                                 uapi_remove_method(uapi, iter.index);
518                                 goto again;
519                         }
520                         continue;
521                 }
522
523                 if (uapi_key_is_write_method(iter.index) ||
524                     uapi_key_is_write_ex_method(iter.index)) {
525                         struct uverbs_api_write_method *method_elm =
526                                 rcu_dereference_protected(*slot, true);
527
528                         if (method_elm->disabled) {
529                                 kfree(method_elm);
530                                 radix_tree_iter_delete(&uapi->radix, &iter, slot);
531                         }
532                         continue;
533                 }
534
535                 if (uapi_key_is_attr(iter.index)) {
536                         struct uverbs_api_attr *attr_elm =
537                                 rcu_dereference_protected(*slot, true);
538                         const struct uverbs_api_object *tmp_obj;
539                         u32 obj_key;
540
541                         /*
542                          * If the method has a mandatory object handle
543                          * attribute which relies on an object which is not
544                          * present then the entire method is uncallable.
545                          */
546                         if (!attr_elm->spec.mandatory)
547                                 continue;
548                         obj_key = uapi_get_obj_id(&attr_elm->spec);
549                         if (obj_key == UVERBS_API_KEY_ERR)
550                                 continue;
551                         tmp_obj = uapi_get_object(uapi, obj_key);
552                         if (tmp_obj && !tmp_obj->disabled)
553                                 continue;
554
555                         starting_key = iter.index;
556                         uapi_remove_method(
557                                 uapi,
558                                 iter.index & (UVERBS_API_OBJ_KEY_MASK |
559                                               UVERBS_API_METHOD_KEY_MASK));
560                         goto again;
561                 }
562
563                 WARN_ON(false);
564         }
565
566         if (!scan_again)
567                 return;
568         scan_again = false;
569         starting_key = 0;
570         goto again;
571 }
572
573 void uverbs_destroy_api(struct uverbs_api *uapi)
574 {
575         if (!uapi)
576                 return;
577
578         uapi_remove_range(uapi, 0, U32_MAX);
579         kfree(uapi->write_methods);
580         kfree(uapi);
581 }
582
583 static const struct uapi_definition uverbs_core_api[] = {
584         UAPI_DEF_CHAIN(uverbs_def_obj_counters),
585         UAPI_DEF_CHAIN(uverbs_def_obj_cq),
586         UAPI_DEF_CHAIN(uverbs_def_obj_dm),
587         UAPI_DEF_CHAIN(uverbs_def_obj_flow_action),
588         UAPI_DEF_CHAIN(uverbs_def_obj_intf),
589         UAPI_DEF_CHAIN(uverbs_def_obj_mr),
590         UAPI_DEF_CHAIN(uverbs_def_write_intf),
591         {},
592 };
593
594 struct uverbs_api *uverbs_alloc_api(struct ib_device *ibdev)
595 {
596         struct uverbs_api *uapi;
597         int rc;
598
599         uapi = kzalloc(sizeof(*uapi), GFP_KERNEL);
600         if (!uapi)
601                 return ERR_PTR(-ENOMEM);
602
603         INIT_RADIX_TREE(&uapi->radix, GFP_KERNEL);
604         uapi->driver_id = ibdev->driver_id;
605
606         rc = uapi_merge_def(uapi, ibdev, uverbs_core_api, false);
607         if (rc)
608                 goto err;
609         rc = uapi_merge_def(uapi, ibdev, ibdev->driver_def, true);
610         if (rc)
611                 goto err;
612
613         uapi_finalize_disable(uapi);
614         rc = uapi_finalize(uapi);
615         if (rc)
616                 goto err;
617
618         return uapi;
619 err:
620         if (rc != -ENOMEM)
621                 dev_err(&ibdev->dev,
622                         "Setup of uverbs_api failed, kernel parsing tree description is not valid (%d)??\n",
623                         rc);
624
625         uverbs_destroy_api(uapi);
626         return ERR_PTR(rc);
627 }
628
629 /*
630  * The pre version is done before destroying the HW objects, it only blocks
631  * off method access. All methods that require the ib_dev or the module data
632  * must test one of these assignments prior to continuing.
633  */
634 void uverbs_disassociate_api_pre(struct ib_uverbs_device *uverbs_dev)
635 {
636         struct uverbs_api *uapi = uverbs_dev->uapi;
637         struct radix_tree_iter iter;
638         void __rcu **slot;
639
640         rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
641
642         radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
643                 if (uapi_key_is_ioctl_method(iter.index)) {
644                         struct uverbs_api_ioctl_method *method_elm =
645                                 rcu_dereference_protected(*slot, true);
646
647                         if (method_elm->driver_method)
648                                 rcu_assign_pointer(method_elm->handler, NULL);
649                 }
650         }
651
652         synchronize_srcu(&uverbs_dev->disassociate_srcu);
653 }
654
655 /*
656  * Called when a driver disassociates from the ib_uverbs_device. The
657  * assumption is that the driver module will unload after. Replace everything
658  * related to the driver with NULL as a safety measure.
659  */
660 void uverbs_disassociate_api(struct uverbs_api *uapi)
661 {
662         struct radix_tree_iter iter;
663         void __rcu **slot;
664
665         radix_tree_for_each_slot (slot, &uapi->radix, &iter, 0) {
666                 if (uapi_key_is_object(iter.index)) {
667                         struct uverbs_api_object *object_elm =
668                                 rcu_dereference_protected(*slot, true);
669
670                         /*
671                          * Some type_attrs are in the driver module. We don't
672                          * bother to keep track of which since there should be
673                          * no use of this after disassociate.
674                          */
675                         object_elm->type_attrs = NULL;
676                 } else if (uapi_key_is_attr(iter.index)) {
677                         struct uverbs_api_attr *elm =
678                                 rcu_dereference_protected(*slot, true);
679
680                         if (elm->spec.type == UVERBS_ATTR_TYPE_ENUM_IN)
681                                 elm->spec.u2.enum_def.ids = NULL;
682                 }
683         }
684 }