uverbs_ioctl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #ifndef _UVERBS_IOCTL_
  33. #define _UVERBS_IOCTL_
  34. #include <rdma/uverbs_types.h>
  35. #include <linux/uaccess.h>
  36. #include <rdma/rdma_user_ioctl.h>
  37. #include <rdma/ib_user_ioctl_verbs.h>
  38. #include <rdma/ib_user_ioctl_cmds.h>
  39. /*
  40. * =======================================
  41. * Verbs action specifications
  42. * =======================================
  43. */
  44. enum uverbs_attr_type {
  45. UVERBS_ATTR_TYPE_NA,
  46. UVERBS_ATTR_TYPE_PTR_IN,
  47. UVERBS_ATTR_TYPE_PTR_OUT,
  48. UVERBS_ATTR_TYPE_IDR,
  49. UVERBS_ATTR_TYPE_FD,
  50. UVERBS_ATTR_TYPE_ENUM_IN,
  51. };
  52. enum uverbs_obj_access {
  53. UVERBS_ACCESS_READ,
  54. UVERBS_ACCESS_WRITE,
  55. UVERBS_ACCESS_NEW,
  56. UVERBS_ACCESS_DESTROY
  57. };
  58. enum {
  59. UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0,
  60. /* Support extending attributes by length, validate all unknown size == zero */
  61. UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO = 1U << 1,
  62. };
  63. /* Specification of a single attribute inside the ioctl message */
  64. struct uverbs_attr_spec {
  65. union {
  66. /* Header shared by all following union members - to reduce space. */
  67. struct {
  68. enum uverbs_attr_type type;
  69. /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
  70. u8 flags;
  71. };
  72. struct {
  73. enum uverbs_attr_type type;
  74. /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
  75. u8 flags;
  76. /* Current known size to kernel */
  77. u16 len;
  78. /* User isn't allowed to provide something < min_len */
  79. u16 min_len;
  80. } ptr;
  81. struct {
  82. enum uverbs_attr_type type;
  83. /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
  84. u8 flags;
  85. /*
  86. * higher bits mean the namespace and lower bits mean
  87. * the type id within the namespace.
  88. */
  89. u16 obj_type;
  90. u8 access;
  91. } obj;
  92. struct {
  93. enum uverbs_attr_type type;
  94. /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
  95. u8 flags;
  96. u8 num_elems;
  97. /*
  98. * The enum attribute can select one of the attributes
  99. * contained in the ids array. Currently only PTR_IN
  100. * attributes are supported in the ids array.
  101. */
  102. const struct uverbs_attr_spec *ids;
  103. } enum_def;
  104. };
  105. };
  106. struct uverbs_attr_spec_hash {
  107. size_t num_attrs;
  108. unsigned long *mandatory_attrs_bitmask;
  109. struct uverbs_attr_spec attrs[0];
  110. };
  111. struct uverbs_attr_bundle;
  112. struct ib_uverbs_file;
  113. enum {
  114. /*
  115. * Action marked with this flag creates a context (or root for all
  116. * objects).
  117. */
  118. UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
  119. };
  120. struct uverbs_method_spec {
  121. /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
  122. u32 flags;
  123. size_t num_buckets;
  124. size_t num_child_attrs;
  125. int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
  126. struct uverbs_attr_bundle *ctx);
  127. struct uverbs_attr_spec_hash *attr_buckets[0];
  128. };
  129. struct uverbs_method_spec_hash {
  130. size_t num_methods;
  131. struct uverbs_method_spec *methods[0];
  132. };
  133. struct uverbs_object_spec {
  134. const struct uverbs_obj_type *type_attrs;
  135. size_t num_buckets;
  136. struct uverbs_method_spec_hash *method_buckets[0];
  137. };
  138. struct uverbs_object_spec_hash {
  139. size_t num_objects;
  140. struct uverbs_object_spec *objects[0];
  141. };
  142. struct uverbs_root_spec {
  143. size_t num_buckets;
  144. struct uverbs_object_spec_hash *object_buckets[0];
  145. };
  146. /*
  147. * =======================================
  148. * Verbs definitions
  149. * =======================================
  150. */
  151. struct uverbs_attr_def {
  152. u16 id;
  153. struct uverbs_attr_spec attr;
  154. };
  155. struct uverbs_method_def {
  156. u16 id;
  157. /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
  158. u32 flags;
  159. size_t num_attrs;
  160. const struct uverbs_attr_def * const (*attrs)[];
  161. int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
  162. struct uverbs_attr_bundle *ctx);
  163. };
  164. struct uverbs_object_def {
  165. u16 id;
  166. const struct uverbs_obj_type *type_attrs;
  167. size_t num_methods;
  168. const struct uverbs_method_def * const (*methods)[];
  169. };
  170. struct uverbs_object_tree_def {
  171. size_t num_objects;
  172. const struct uverbs_object_def * const (*objects)[];
  173. };
  174. #define UA_FLAGS(_flags) .flags = _flags
  175. #define __UVERBS_ATTR0(_id, _type, _fld, _attr, ...) \
  176. ((const struct uverbs_attr_def) \
  177. {.id = _id, .attr = {{._fld = {.type = _type, _attr, .flags = 0, } }, } })
  178. #define __UVERBS_ATTR1(_id, _type, _fld, _attr, _extra1, ...) \
  179. ((const struct uverbs_attr_def) \
  180. {.id = _id, .attr = {{._fld = {.type = _type, _attr, _extra1 } },} })
  181. #define __UVERBS_ATTR2(_id, _type, _fld, _attr, _extra1, _extra2) \
  182. ((const struct uverbs_attr_def) \
  183. {.id = _id, .attr = {{._fld = {.type = _type, _attr, _extra1, _extra2 } },} })
  184. #define __UVERBS_ATTR(_id, _type, _fld, _attr, _extra1, _extra2, _n, ...) \
  185. __UVERBS_ATTR##_n(_id, _type, _fld, _attr, _extra1, _extra2)
  186. #define UVERBS_ATTR_TYPE(_type) \
  187. .min_len = sizeof(_type), .len = sizeof(_type)
  188. #define UVERBS_ATTR_STRUCT(_type, _last) \
  189. .min_len = ((uintptr_t)(&((_type *)0)->_last + 1)), .len = sizeof(_type)
  190. #define UVERBS_ATTR_SIZE(_min_len, _len) \
  191. .min_len = _min_len, .len = _len
  192. /*
  193. * In new compiler, UVERBS_ATTR could be simplified by declaring it as
  194. * [_id] = {.type = _type, .len = _len, ##__VA_ARGS__}
  195. * But since we support older compilers too, we need the more complex code.
  196. */
  197. #define UVERBS_ATTR(_id, _type, _fld, _attr, ...) \
  198. __UVERBS_ATTR(_id, _type, _fld, _attr, ##__VA_ARGS__, 2, 1, 0)
  199. #define UVERBS_ATTR_PTR_IN_SZ(_id, _len, ...) \
  200. UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_PTR_IN, ptr, _len, ##__VA_ARGS__)
  201. /* If sizeof(_type) <= sizeof(u64), this will be inlined rather than a pointer */
  202. #define UVERBS_ATTR_PTR_IN(_id, _type, ...) \
  203. UVERBS_ATTR_PTR_IN_SZ(_id, _type, ##__VA_ARGS__)
  204. #define UVERBS_ATTR_PTR_OUT_SZ(_id, _len, ...) \
  205. UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_PTR_OUT, ptr, _len, ##__VA_ARGS__)
  206. #define UVERBS_ATTR_PTR_OUT(_id, _type, ...) \
  207. UVERBS_ATTR_PTR_OUT_SZ(_id, _type, ##__VA_ARGS__)
  208. #define UVERBS_ATTR_ENUM_IN(_id, _enum_arr, ...) \
  209. UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_ENUM_IN, enum_def, \
  210. .ids = (_enum_arr), \
  211. .num_elems = ARRAY_SIZE(_enum_arr), ##__VA_ARGS__)
  212. /*
  213. * In new compiler, UVERBS_ATTR_IDR (and FD) could be simplified by declaring
  214. * it as
  215. * {.id = _id, \
  216. * .attr {.type = __obj_class, \
  217. * .obj = {.obj_type = _idr_type, \
  218. * .access = _access \
  219. * }, ##__VA_ARGS__ } }
  220. * But since we support older compilers too, we need the more complex code.
  221. */
  222. #define ___UVERBS_ATTR_OBJ0(_id, _obj_class, _obj_type, _access, ...)\
  223. ((const struct uverbs_attr_def) \
  224. {.id = _id, \
  225. .attr = { {.obj = {.type = _obj_class, .obj_type = _obj_type, \
  226. .access = _access, .flags = 0 } }, } })
  227. #define ___UVERBS_ATTR_OBJ1(_id, _obj_class, _obj_type, _access, _flags)\
  228. ((const struct uverbs_attr_def) \
  229. {.id = _id, \
  230. .attr = { {.obj = {.type = _obj_class, .obj_type = _obj_type, \
  231. .access = _access, _flags} }, } })
  232. #define ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, _flags, \
  233. _n, ...) \
  234. ___UVERBS_ATTR_OBJ##_n(_id, _obj_class, _obj_type, _access, _flags)
  235. #define __UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, ...) \
  236. ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, \
  237. ##__VA_ARGS__, 1, 0)
  238. #define UVERBS_ATTR_IDR(_id, _idr_type, _access, ...) \
  239. __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_IDR, _idr_type, _access,\
  240. ##__VA_ARGS__)
  241. #define UVERBS_ATTR_FD(_id, _fd_type, _access, ...) \
  242. __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_FD, _fd_type, \
  243. (_access) + BUILD_BUG_ON_ZERO( \
  244. (_access) != UVERBS_ACCESS_NEW && \
  245. (_access) != UVERBS_ACCESS_READ), \
  246. ##__VA_ARGS__)
  247. #define DECLARE_UVERBS_ATTR_SPEC(_name, ...) \
  248. const struct uverbs_attr_def _name = __VA_ARGS__
  249. #define DECLARE_UVERBS_ENUM(_name, ...) \
  250. const struct uverbs_enum_spec _name = { \
  251. .len = ARRAY_SIZE(((struct uverbs_attr_spec[]){__VA_ARGS__})),\
  252. .ids = {__VA_ARGS__}, \
  253. }
  254. #define _UVERBS_METHOD_ATTRS_SZ(...) \
  255. (sizeof((const struct uverbs_attr_def * const []){__VA_ARGS__}) /\
  256. sizeof(const struct uverbs_attr_def *))
  257. #define _UVERBS_METHOD(_id, _handler, _flags, ...) \
  258. ((const struct uverbs_method_def) { \
  259. .id = _id, \
  260. .flags = _flags, \
  261. .handler = _handler, \
  262. .num_attrs = _UVERBS_METHOD_ATTRS_SZ(__VA_ARGS__), \
  263. .attrs = &(const struct uverbs_attr_def * const []){__VA_ARGS__} })
  264. #define DECLARE_UVERBS_METHOD(_name, _id, _handler, ...) \
  265. const struct uverbs_method_def _name = \
  266. _UVERBS_METHOD(_id, _handler, 0, ##__VA_ARGS__)
  267. #define DECLARE_UVERBS_CTX_METHOD(_name, _id, _handler, _flags, ...) \
  268. const struct uverbs_method_def _name = \
  269. _UVERBS_METHOD(_id, _handler, \
  270. UVERBS_ACTION_FLAG_CREATE_ROOT, \
  271. ##__VA_ARGS__)
  272. #define _UVERBS_OBJECT_METHODS_SZ(...) \
  273. (sizeof((const struct uverbs_method_def * const []){__VA_ARGS__}) / \
  274. sizeof(const struct uverbs_method_def *))
  275. #define _UVERBS_OBJECT(_id, _type_attrs, ...) \
  276. ((const struct uverbs_object_def) { \
  277. .id = _id, \
  278. .type_attrs = _type_attrs, \
  279. .num_methods = _UVERBS_OBJECT_METHODS_SZ(__VA_ARGS__), \
  280. .methods = &(const struct uverbs_method_def * const []){__VA_ARGS__} })
  281. #define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...) \
  282. const struct uverbs_object_def _name = \
  283. _UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__)
  284. #define _UVERBS_TREE_OBJECTS_SZ(...) \
  285. (sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \
  286. sizeof(const struct uverbs_object_def *))
  287. #define _UVERBS_OBJECT_TREE(...) \
  288. ((const struct uverbs_object_tree_def) { \
  289. .num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__), \
  290. .objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} })
  291. #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
  292. const struct uverbs_object_tree_def _name = \
  293. _UVERBS_OBJECT_TREE(__VA_ARGS__)
  294. /* =================================================
  295. * Parsing infrastructure
  296. * =================================================
  297. */
  298. struct uverbs_ptr_attr {
  299. u64 data;
  300. u16 len;
  301. /* Combination of bits from enum UVERBS_ATTR_F_XXXX */
  302. u16 flags;
  303. u8 enum_id;
  304. };
  305. struct uverbs_obj_attr {
  306. /* pointer to the kernel descriptor -> type, access, etc */
  307. const struct uverbs_obj_type *type;
  308. struct ib_uobject *uobject;
  309. /* fd or id in idr of this object */
  310. int id;
  311. };
  312. struct uverbs_attr {
  313. /*
  314. * pointer to the user-space given attribute, in order to write the
  315. * new uobject's id or update flags.
  316. */
  317. struct ib_uverbs_attr __user *uattr;
  318. union {
  319. struct uverbs_ptr_attr ptr_attr;
  320. struct uverbs_obj_attr obj_attr;
  321. };
  322. };
  323. struct uverbs_attr_bundle_hash {
  324. /* if bit i is set, it means attrs[i] contains valid information */
  325. unsigned long *valid_bitmap;
  326. size_t num_attrs;
  327. /*
  328. * arrays of attributes, each element corresponds to the specification
  329. * of the attribute in the same index.
  330. */
  331. struct uverbs_attr *attrs;
  332. };
  333. struct uverbs_attr_bundle {
  334. size_t num_buckets;
  335. struct uverbs_attr_bundle_hash hash[];
  336. };
  337. static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
  338. unsigned int idx)
  339. {
  340. return test_bit(idx, attrs_hash->valid_bitmap);
  341. }
  342. static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
  343. unsigned int idx)
  344. {
  345. u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
  346. if (attrs_bundle->num_buckets <= idx_bucket)
  347. return false;
  348. return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
  349. idx & ~UVERBS_ID_NS_MASK);
  350. }
  351. #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
  352. static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
  353. u16 idx)
  354. {
  355. u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
  356. if (!uverbs_attr_is_valid(attrs_bundle, idx))
  357. return ERR_PTR(-ENOENT);
  358. return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
  359. }
  360. static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
  361. u16 idx)
  362. {
  363. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  364. if (IS_ERR(attr))
  365. return PTR_ERR(attr);
  366. return attr->ptr_attr.enum_id;
  367. }
  368. static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
  369. u16 idx)
  370. {
  371. const struct uverbs_attr *attr;
  372. attr = uverbs_attr_get(attrs_bundle, idx);
  373. if (IS_ERR(attr))
  374. return ERR_CAST(attr);
  375. return attr->obj_attr.uobject->object;
  376. }
  377. static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
  378. u16 idx)
  379. {
  380. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  381. if (IS_ERR(attr))
  382. return ERR_CAST(attr);
  383. return attr->obj_attr.uobject;
  384. }
  385. static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
  386. size_t idx, const void *from, size_t size)
  387. {
  388. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  389. u16 flags;
  390. size_t min_size;
  391. if (IS_ERR(attr))
  392. return PTR_ERR(attr);
  393. min_size = min_t(size_t, attr->ptr_attr.len, size);
  394. if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
  395. return -EFAULT;
  396. flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
  397. if (put_user(flags, &attr->uattr->flags))
  398. return -EFAULT;
  399. return 0;
  400. }
  401. static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
  402. {
  403. return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
  404. }
  405. static inline int _uverbs_copy_from(void *to,
  406. const struct uverbs_attr_bundle *attrs_bundle,
  407. size_t idx,
  408. size_t size)
  409. {
  410. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  411. if (IS_ERR(attr))
  412. return PTR_ERR(attr);
  413. /*
  414. * Validation ensures attr->ptr_attr.len >= size. If the caller is
  415. * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
  416. * uverbs_copy_from_or_zero.
  417. */
  418. if (unlikely(size < attr->ptr_attr.len))
  419. return -EINVAL;
  420. if (uverbs_attr_ptr_is_inline(attr))
  421. memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
  422. else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
  423. attr->ptr_attr.len))
  424. return -EFAULT;
  425. return 0;
  426. }
  427. static inline int _uverbs_copy_from_or_zero(void *to,
  428. const struct uverbs_attr_bundle *attrs_bundle,
  429. size_t idx,
  430. size_t size)
  431. {
  432. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  433. size_t min_size;
  434. if (IS_ERR(attr))
  435. return PTR_ERR(attr);
  436. min_size = min_t(size_t, size, attr->ptr_attr.len);
  437. if (uverbs_attr_ptr_is_inline(attr))
  438. memcpy(to, &attr->ptr_attr.data, min_size);
  439. else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
  440. min_size))
  441. return -EFAULT;
  442. if (size > min_size)
  443. memset(to + min_size, 0, size - min_size);
  444. return 0;
  445. }
  446. #define uverbs_copy_from(to, attrs_bundle, idx) \
  447. _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
  448. #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
  449. _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
  450. /* =================================================
  451. * Definitions -> Specs infrastructure
  452. * =================================================
  453. */
  454. /*
  455. * uverbs_alloc_spec_tree - Merges different common and driver specific feature
  456. * into one parsing tree that every uverbs command will be parsed upon.
  457. *
  458. * @num_trees: Number of trees in the array @trees.
  459. * @trees: Array of pointers to tree root definitions to merge. Each such tree
  460. * possibly contains objects, methods and attributes definitions.
  461. *
  462. * Returns:
  463. * uverbs_root_spec *: The root of the merged parsing tree.
  464. * On error, we return an error code. Error is checked via IS_ERR.
  465. *
  466. * The following merges could take place:
  467. * a. Two trees representing the same method with different handler
  468. * -> We take the handler of the tree that its handler != NULL
  469. * and its index in the trees array is greater. The incentive for that
  470. * is that developers are expected to first merge common trees and then
  471. * merge trees that gives specialized the behaviour.
  472. * b. Two trees representing the same object with different
  473. * type_attrs (struct uverbs_obj_type):
  474. * -> We take the type_attrs of the tree that its type_attr != NULL
  475. * and its index in the trees array is greater. This could be used
  476. * in order to override the free function, allocation size, etc.
  477. * c. Two trees representing the same method attribute (same id but possibly
  478. * different attributes):
  479. * -> ERROR (-ENOENT), we believe that's not the programmer's intent.
  480. *
  481. * An object without any methods is considered invalid and will abort the
  482. * function with -ENOENT error.
  483. */
  484. #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
  485. struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
  486. const struct uverbs_object_tree_def **trees);
  487. void uverbs_free_spec_tree(struct uverbs_root_spec *root);
  488. #else
  489. static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
  490. const struct uverbs_object_tree_def **trees)
  491. {
  492. return NULL;
  493. }
  494. static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
  495. {
  496. }
  497. #endif
  498. #endif