uverbs_ioctl.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. /*
  39. * =======================================
  40. * Verbs action specifications
  41. * =======================================
  42. */
  43. enum uverbs_attr_type {
  44. UVERBS_ATTR_TYPE_NA,
  45. UVERBS_ATTR_TYPE_PTR_IN,
  46. UVERBS_ATTR_TYPE_PTR_OUT,
  47. UVERBS_ATTR_TYPE_IDR,
  48. UVERBS_ATTR_TYPE_FD,
  49. };
  50. enum uverbs_obj_access {
  51. UVERBS_ACCESS_READ,
  52. UVERBS_ACCESS_WRITE,
  53. UVERBS_ACCESS_NEW,
  54. UVERBS_ACCESS_DESTROY
  55. };
  56. enum {
  57. UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0,
  58. /* Support extending attributes by length */
  59. UVERBS_ATTR_SPEC_F_MIN_SZ = 1U << 1,
  60. };
  61. struct uverbs_attr_spec {
  62. enum uverbs_attr_type type;
  63. union {
  64. u16 len;
  65. struct {
  66. /*
  67. * higher bits mean the namespace and lower bits mean
  68. * the type id within the namespace.
  69. */
  70. u16 obj_type;
  71. u8 access;
  72. } obj;
  73. };
  74. /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
  75. u8 flags;
  76. };
  77. struct uverbs_attr_spec_hash {
  78. size_t num_attrs;
  79. unsigned long *mandatory_attrs_bitmask;
  80. struct uverbs_attr_spec attrs[0];
  81. };
  82. struct uverbs_attr_bundle;
  83. struct ib_uverbs_file;
  84. enum {
  85. /*
  86. * Action marked with this flag creates a context (or root for all
  87. * objects).
  88. */
  89. UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
  90. };
  91. struct uverbs_method_spec {
  92. /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
  93. u32 flags;
  94. size_t num_buckets;
  95. size_t num_child_attrs;
  96. int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
  97. struct uverbs_attr_bundle *ctx);
  98. struct uverbs_attr_spec_hash *attr_buckets[0];
  99. };
  100. struct uverbs_method_spec_hash {
  101. size_t num_methods;
  102. struct uverbs_method_spec *methods[0];
  103. };
  104. struct uverbs_object_spec {
  105. const struct uverbs_obj_type *type_attrs;
  106. size_t num_buckets;
  107. struct uverbs_method_spec_hash *method_buckets[0];
  108. };
  109. struct uverbs_object_spec_hash {
  110. size_t num_objects;
  111. struct uverbs_object_spec *objects[0];
  112. };
  113. struct uverbs_root_spec {
  114. size_t num_buckets;
  115. struct uverbs_object_spec_hash *object_buckets[0];
  116. };
  117. /*
  118. * =======================================
  119. * Verbs definitions
  120. * =======================================
  121. */
  122. struct uverbs_attr_def {
  123. u16 id;
  124. struct uverbs_attr_spec attr;
  125. };
  126. struct uverbs_method_def {
  127. u16 id;
  128. /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
  129. u32 flags;
  130. size_t num_attrs;
  131. const struct uverbs_attr_def * const (*attrs)[];
  132. int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
  133. struct uverbs_attr_bundle *ctx);
  134. };
  135. struct uverbs_object_def {
  136. u16 id;
  137. const struct uverbs_obj_type *type_attrs;
  138. size_t num_methods;
  139. const struct uverbs_method_def * const (*methods)[];
  140. };
  141. struct uverbs_object_tree_def {
  142. size_t num_objects;
  143. const struct uverbs_object_def * const (*objects)[];
  144. };
  145. #define UA_FLAGS(_flags) .flags = _flags
  146. #define __UVERBS_ATTR0(_id, _len, _type, ...) \
  147. ((const struct uverbs_attr_def) \
  148. {.id = _id, .attr = {.type = _type, {.len = _len}, .flags = 0, } })
  149. #define __UVERBS_ATTR1(_id, _len, _type, _flags) \
  150. ((const struct uverbs_attr_def) \
  151. {.id = _id, .attr = {.type = _type, {.len = _len}, _flags, } })
  152. #define __UVERBS_ATTR(_id, _len, _type, _flags, _n, ...) \
  153. __UVERBS_ATTR##_n(_id, _len, _type, _flags)
  154. /*
  155. * In new compiler, UVERBS_ATTR could be simplified by declaring it as
  156. * [_id] = {.type = _type, .len = _len, ##__VA_ARGS__}
  157. * But since we support older compilers too, we need the more complex code.
  158. */
  159. #define UVERBS_ATTR(_id, _len, _type, ...) \
  160. __UVERBS_ATTR(_id, _len, _type, ##__VA_ARGS__, 1, 0)
  161. #define UVERBS_ATTR_PTR_IN_SZ(_id, _len, ...) \
  162. UVERBS_ATTR(_id, _len, UVERBS_ATTR_TYPE_PTR_IN, ##__VA_ARGS__)
  163. /* If sizeof(_type) <= sizeof(u64), this will be inlined rather than a pointer */
  164. #define UVERBS_ATTR_PTR_IN(_id, _type, ...) \
  165. UVERBS_ATTR_PTR_IN_SZ(_id, sizeof(_type), ##__VA_ARGS__)
  166. #define UVERBS_ATTR_PTR_OUT_SZ(_id, _len, ...) \
  167. UVERBS_ATTR(_id, _len, UVERBS_ATTR_TYPE_PTR_OUT, ##__VA_ARGS__)
  168. #define UVERBS_ATTR_PTR_OUT(_id, _type, ...) \
  169. UVERBS_ATTR_PTR_OUT_SZ(_id, sizeof(_type), ##__VA_ARGS__)
  170. /*
  171. * In new compiler, UVERBS_ATTR_IDR (and FD) could be simplified by declaring
  172. * it as
  173. * {.id = _id, \
  174. * .attr {.type = __obj_class, \
  175. * .obj = {.obj_type = _idr_type, \
  176. * .access = _access \
  177. * }, ##__VA_ARGS__ } }
  178. * But since we support older compilers too, we need the more complex code.
  179. */
  180. #define ___UVERBS_ATTR_OBJ0(_id, _obj_class, _obj_type, _access, ...)\
  181. ((const struct uverbs_attr_def) \
  182. {.id = _id, \
  183. .attr = {.type = _obj_class, \
  184. {.obj = {.obj_type = _obj_type, .access = _access } },\
  185. .flags = 0} })
  186. #define ___UVERBS_ATTR_OBJ1(_id, _obj_class, _obj_type, _access, _flags)\
  187. ((const struct uverbs_attr_def) \
  188. {.id = _id, \
  189. .attr = {.type = _obj_class, \
  190. {.obj = {.obj_type = _obj_type, .access = _access} }, \
  191. _flags} })
  192. #define ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, _flags, \
  193. _n, ...) \
  194. ___UVERBS_ATTR_OBJ##_n(_id, _obj_class, _obj_type, _access, _flags)
  195. #define __UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, ...) \
  196. ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, \
  197. ##__VA_ARGS__, 1, 0)
  198. #define UVERBS_ATTR_IDR(_id, _idr_type, _access, ...) \
  199. __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_IDR, _idr_type, _access,\
  200. ##__VA_ARGS__)
  201. #define UVERBS_ATTR_FD(_id, _fd_type, _access, ...) \
  202. __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_FD, _fd_type, \
  203. (_access) + BUILD_BUG_ON_ZERO( \
  204. (_access) != UVERBS_ACCESS_NEW && \
  205. (_access) != UVERBS_ACCESS_READ), \
  206. ##__VA_ARGS__)
  207. #define DECLARE_UVERBS_ATTR_SPEC(_name, ...) \
  208. const struct uverbs_attr_def _name = __VA_ARGS__
  209. #define _UVERBS_METHOD_ATTRS_SZ(...) \
  210. (sizeof((const struct uverbs_attr_def * const []){__VA_ARGS__}) /\
  211. sizeof(const struct uverbs_attr_def *))
  212. #define _UVERBS_METHOD(_id, _handler, _flags, ...) \
  213. ((const struct uverbs_method_def) { \
  214. .id = _id, \
  215. .flags = _flags, \
  216. .handler = _handler, \
  217. .num_attrs = _UVERBS_METHOD_ATTRS_SZ(__VA_ARGS__), \
  218. .attrs = &(const struct uverbs_attr_def * const []){__VA_ARGS__} })
  219. #define DECLARE_UVERBS_METHOD(_name, _id, _handler, ...) \
  220. const struct uverbs_method_def _name = \
  221. _UVERBS_METHOD(_id, _handler, 0, ##__VA_ARGS__)
  222. #define DECLARE_UVERBS_CTX_METHOD(_name, _id, _handler, _flags, ...) \
  223. const struct uverbs_method_def _name = \
  224. _UVERBS_METHOD(_id, _handler, \
  225. UVERBS_ACTION_FLAG_CREATE_ROOT, \
  226. ##__VA_ARGS__)
  227. #define _UVERBS_OBJECT_METHODS_SZ(...) \
  228. (sizeof((const struct uverbs_method_def * const []){__VA_ARGS__}) / \
  229. sizeof(const struct uverbs_method_def *))
  230. #define _UVERBS_OBJECT(_id, _type_attrs, ...) \
  231. ((const struct uverbs_object_def) { \
  232. .id = _id, \
  233. .type_attrs = _type_attrs, \
  234. .num_methods = _UVERBS_OBJECT_METHODS_SZ(__VA_ARGS__), \
  235. .methods = &(const struct uverbs_method_def * const []){__VA_ARGS__} })
  236. #define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...) \
  237. const struct uverbs_object_def _name = \
  238. _UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__)
  239. #define _UVERBS_TREE_OBJECTS_SZ(...) \
  240. (sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \
  241. sizeof(const struct uverbs_object_def *))
  242. #define _UVERBS_OBJECT_TREE(...) \
  243. ((const struct uverbs_object_tree_def) { \
  244. .num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__), \
  245. .objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} })
  246. #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
  247. const struct uverbs_object_tree_def _name = \
  248. _UVERBS_OBJECT_TREE(__VA_ARGS__)
  249. /* =================================================
  250. * Parsing infrastructure
  251. * =================================================
  252. */
  253. struct uverbs_ptr_attr {
  254. union {
  255. u64 data;
  256. void __user *ptr;
  257. };
  258. u16 len;
  259. /* Combination of bits from enum UVERBS_ATTR_F_XXXX */
  260. u16 flags;
  261. };
  262. struct uverbs_obj_attr {
  263. /* pointer to the kernel descriptor -> type, access, etc */
  264. const struct uverbs_obj_type *type;
  265. struct ib_uobject *uobject;
  266. /* fd or id in idr of this object */
  267. int id;
  268. };
  269. struct uverbs_attr {
  270. /*
  271. * pointer to the user-space given attribute, in order to write the
  272. * new uobject's id or update flags.
  273. */
  274. struct ib_uverbs_attr __user *uattr;
  275. union {
  276. struct uverbs_ptr_attr ptr_attr;
  277. struct uverbs_obj_attr obj_attr;
  278. };
  279. };
  280. struct uverbs_attr_bundle_hash {
  281. /* if bit i is set, it means attrs[i] contains valid information */
  282. unsigned long *valid_bitmap;
  283. size_t num_attrs;
  284. /*
  285. * arrays of attributes, each element corresponds to the specification
  286. * of the attribute in the same index.
  287. */
  288. struct uverbs_attr *attrs;
  289. };
  290. struct uverbs_attr_bundle {
  291. size_t num_buckets;
  292. struct uverbs_attr_bundle_hash hash[];
  293. };
  294. static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
  295. unsigned int idx)
  296. {
  297. return test_bit(idx, attrs_hash->valid_bitmap);
  298. }
  299. static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
  300. unsigned int idx)
  301. {
  302. u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
  303. if (attrs_bundle->num_buckets <= idx_bucket)
  304. return false;
  305. return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
  306. idx & ~UVERBS_ID_NS_MASK);
  307. }
  308. static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
  309. u16 idx)
  310. {
  311. u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
  312. if (!uverbs_attr_is_valid(attrs_bundle, idx))
  313. return ERR_PTR(-ENOENT);
  314. return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
  315. }
  316. static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
  317. size_t idx, const void *from)
  318. {
  319. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  320. u16 flags;
  321. if (IS_ERR(attr))
  322. return PTR_ERR(attr);
  323. flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
  324. return (!copy_to_user(attr->ptr_attr.ptr, from, attr->ptr_attr.len) &&
  325. !put_user(flags, &attr->uattr->flags)) ? 0 : -EFAULT;
  326. }
  327. static inline int _uverbs_copy_from(void *to, size_t to_size,
  328. const struct uverbs_attr_bundle *attrs_bundle,
  329. size_t idx)
  330. {
  331. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  332. if (IS_ERR(attr))
  333. return PTR_ERR(attr);
  334. if (to_size <= sizeof(((struct ib_uverbs_attr *)0)->data))
  335. memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
  336. else if (copy_from_user(to, attr->ptr_attr.ptr, attr->ptr_attr.len))
  337. return -EFAULT;
  338. return 0;
  339. }
  340. #define uverbs_copy_from(to, attrs_bundle, idx) \
  341. _uverbs_copy_from(to, sizeof(*(to)), attrs_bundle, idx)
  342. /* =================================================
  343. * Definitions -> Specs infrastructure
  344. * =================================================
  345. */
  346. /*
  347. * uverbs_alloc_spec_tree - Merges different common and driver specific feature
  348. * into one parsing tree that every uverbs command will be parsed upon.
  349. *
  350. * @num_trees: Number of trees in the array @trees.
  351. * @trees: Array of pointers to tree root definitions to merge. Each such tree
  352. * possibly contains objects, methods and attributes definitions.
  353. *
  354. * Returns:
  355. * uverbs_root_spec *: The root of the merged parsing tree.
  356. * On error, we return an error code. Error is checked via IS_ERR.
  357. *
  358. * The following merges could take place:
  359. * a. Two trees representing the same method with different handler
  360. * -> We take the handler of the tree that its handler != NULL
  361. * and its index in the trees array is greater. The incentive for that
  362. * is that developers are expected to first merge common trees and then
  363. * merge trees that gives specialized the behaviour.
  364. * b. Two trees representing the same object with different
  365. * type_attrs (struct uverbs_obj_type):
  366. * -> We take the type_attrs of the tree that its type_attr != NULL
  367. * and its index in the trees array is greater. This could be used
  368. * in order to override the free function, allocation size, etc.
  369. * c. Two trees representing the same method attribute (same id but possibly
  370. * different attributes):
  371. * -> ERROR (-ENOENT), we believe that's not the programmer's intent.
  372. *
  373. * An object without any methods is considered invalid and will abort the
  374. * function with -ENOENT error.
  375. */
  376. #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
  377. struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
  378. const struct uverbs_object_tree_def **trees);
  379. void uverbs_free_spec_tree(struct uverbs_root_spec *root);
  380. #else
  381. static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
  382. const struct uverbs_object_tree_def **trees)
  383. {
  384. return NULL;
  385. }
  386. static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
  387. {
  388. }
  389. #endif
  390. #endif