uverbs_ioctl.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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. UVERBS_ATTR_TYPE_IDRS_ARRAY,
  52. };
  53. enum uverbs_obj_access {
  54. UVERBS_ACCESS_READ,
  55. UVERBS_ACCESS_WRITE,
  56. UVERBS_ACCESS_NEW,
  57. UVERBS_ACCESS_DESTROY
  58. };
  59. /* Specification of a single attribute inside the ioctl message */
  60. /* good size 16 */
  61. struct uverbs_attr_spec {
  62. u8 type;
  63. /*
  64. * Support extending attributes by length. Allow the user to provide
  65. * more bytes than ptr.len, but check that everything after is zero'd
  66. * by the user.
  67. */
  68. u8 zero_trailing:1;
  69. /*
  70. * Valid only for PTR_IN. Allocate and copy the data inside
  71. * the parser
  72. */
  73. u8 alloc_and_copy:1;
  74. u8 mandatory:1;
  75. union {
  76. struct {
  77. /* Current known size to kernel */
  78. u16 len;
  79. /* User isn't allowed to provide something < min_len */
  80. u16 min_len;
  81. } ptr;
  82. struct {
  83. /*
  84. * higher bits mean the namespace and lower bits mean
  85. * the type id within the namespace.
  86. */
  87. u16 obj_type;
  88. u8 access;
  89. } obj;
  90. struct {
  91. u8 num_elems;
  92. } enum_def;
  93. } u;
  94. /* This weird split lets us remove some padding */
  95. union {
  96. struct {
  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. struct {
  105. /*
  106. * higher bits mean the namespace and lower bits mean
  107. * the type id within the namespace.
  108. */
  109. u16 obj_type;
  110. u16 min_len;
  111. u16 max_len;
  112. u8 access;
  113. } objs_arr;
  114. } u2;
  115. };
  116. /*
  117. * Information about the API is loaded into a radix tree. For IOCTL we start
  118. * with a tuple of:
  119. * object_id, attr_id, method_id
  120. *
  121. * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
  122. * on the current kernel support this is compressed into 16 bit key for the
  123. * radix tree. Since this compression is entirely internal to the kernel the
  124. * below limits can be revised if the kernel gains additional data.
  125. *
  126. * With 64 leafs per node this is a 3 level radix tree.
  127. *
  128. * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
  129. * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
  130. */
  131. enum uapi_radix_data {
  132. UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
  133. UVERBS_API_ATTR_KEY_BITS = 6,
  134. UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
  135. UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
  136. UVERBS_API_METHOD_KEY_BITS = 5,
  137. UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
  138. UVERBS_API_METHOD_KEY_NUM_CORE = 24,
  139. UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) -
  140. UVERBS_API_METHOD_KEY_NUM_CORE,
  141. UVERBS_API_METHOD_KEY_MASK = GENMASK(
  142. UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
  143. UVERBS_API_METHOD_KEY_SHIFT),
  144. UVERBS_API_OBJ_KEY_BITS = 5,
  145. UVERBS_API_OBJ_KEY_SHIFT =
  146. UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
  147. UVERBS_API_OBJ_KEY_NUM_CORE = 24,
  148. UVERBS_API_OBJ_KEY_NUM_DRIVER =
  149. (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
  150. UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
  151. /* This id guaranteed to not exist in the radix tree */
  152. UVERBS_API_KEY_ERR = 0xFFFFFFFF,
  153. };
  154. static inline __attribute_const__ u32 uapi_key_obj(u32 id)
  155. {
  156. if (id & UVERBS_API_NS_FLAG) {
  157. id &= ~UVERBS_API_NS_FLAG;
  158. if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
  159. return UVERBS_API_KEY_ERR;
  160. id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
  161. } else {
  162. if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
  163. return UVERBS_API_KEY_ERR;
  164. }
  165. return id << UVERBS_API_OBJ_KEY_SHIFT;
  166. }
  167. static inline __attribute_const__ bool uapi_key_is_object(u32 key)
  168. {
  169. return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
  170. }
  171. static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
  172. {
  173. if (id & UVERBS_API_NS_FLAG) {
  174. id &= ~UVERBS_API_NS_FLAG;
  175. if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
  176. return UVERBS_API_KEY_ERR;
  177. id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
  178. } else {
  179. id++;
  180. if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
  181. return UVERBS_API_KEY_ERR;
  182. }
  183. return id << UVERBS_API_METHOD_KEY_SHIFT;
  184. }
  185. static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key)
  186. {
  187. return attr_key &
  188. (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
  189. }
  190. static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
  191. {
  192. return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
  193. (key & UVERBS_API_ATTR_KEY_MASK) == 0;
  194. }
  195. static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
  196. {
  197. /* 0 is the method slot itself */
  198. return ioctl_method_key + 1;
  199. }
  200. static inline __attribute_const__ u32 uapi_key_attr(u32 id)
  201. {
  202. /*
  203. * The attr is designed to fit in the typical single radix tree node
  204. * of 64 entries. Since allmost all methods have driver attributes we
  205. * organize things so that the driver and core attributes interleave to
  206. * reduce the length of the attributes array in typical cases.
  207. */
  208. if (id & UVERBS_API_NS_FLAG) {
  209. id &= ~UVERBS_API_NS_FLAG;
  210. id++;
  211. if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
  212. return UVERBS_API_KEY_ERR;
  213. id = (id << 1) | 0;
  214. } else {
  215. if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
  216. return UVERBS_API_KEY_ERR;
  217. id = (id << 1) | 1;
  218. }
  219. return id;
  220. }
  221. static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
  222. {
  223. return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
  224. (key & UVERBS_API_ATTR_KEY_MASK) != 0;
  225. }
  226. /*
  227. * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
  228. * basically it undoes the reservation of 0 in the ID numbering. attr_key
  229. * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
  230. * uapi_key_attr().
  231. */
  232. static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
  233. {
  234. return attr_key - 1;
  235. }
  236. static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
  237. {
  238. return attr_bkey + 1;
  239. }
  240. /*
  241. * =======================================
  242. * Verbs definitions
  243. * =======================================
  244. */
  245. struct uverbs_attr_def {
  246. u16 id;
  247. struct uverbs_attr_spec attr;
  248. };
  249. struct uverbs_method_def {
  250. u16 id;
  251. /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
  252. u32 flags;
  253. size_t num_attrs;
  254. const struct uverbs_attr_def * const (*attrs)[];
  255. int (*handler)(struct ib_uverbs_file *ufile,
  256. struct uverbs_attr_bundle *ctx);
  257. };
  258. struct uverbs_object_def {
  259. u16 id;
  260. const struct uverbs_obj_type *type_attrs;
  261. size_t num_methods;
  262. const struct uverbs_method_def * const (*methods)[];
  263. };
  264. struct uverbs_object_tree_def {
  265. size_t num_objects;
  266. const struct uverbs_object_def * const (*objects)[];
  267. };
  268. /*
  269. * =======================================
  270. * Attribute Specifications
  271. * =======================================
  272. */
  273. #define UVERBS_ATTR_SIZE(_min_len, _len) \
  274. .u.ptr.min_len = _min_len, .u.ptr.len = _len
  275. #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
  276. /*
  277. * Specifies a uapi structure that cannot be extended. The user must always
  278. * supply the whole structure and nothing more. The structure must be declared
  279. * in a header under include/uapi/rdma.
  280. */
  281. #define UVERBS_ATTR_TYPE(_type) \
  282. .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
  283. /*
  284. * Specifies a uapi structure where the user must provide at least up to
  285. * member 'last'. Anything after last and up until the end of the structure
  286. * can be non-zero, anything longer than the end of the structure must be
  287. * zero. The structure must be declared in a header under include/uapi/rdma.
  288. */
  289. #define UVERBS_ATTR_STRUCT(_type, _last) \
  290. .zero_trailing = 1, \
  291. UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
  292. sizeof(_type))
  293. /*
  294. * Specifies at least min_len bytes must be passed in, but the amount can be
  295. * larger, up to the protocol maximum size. No check for zeroing is done.
  296. */
  297. #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
  298. /* Must be used in the '...' of any UVERBS_ATTR */
  299. #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
  300. #define UA_MANDATORY .mandatory = 1
  301. #define UA_OPTIONAL .mandatory = 0
  302. /*
  303. * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
  304. * READ\WRITE accesses are supported.
  305. */
  306. #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
  307. ...) \
  308. (&(const struct uverbs_attr_def){ \
  309. .id = (_attr_id) + \
  310. BUILD_BUG_ON_ZERO((_min_len) == 0 || \
  311. (_max_len) > \
  312. PAGE_SIZE / sizeof(void *) || \
  313. (_min_len) > (_max_len) || \
  314. (_access) == UVERBS_ACCESS_NEW || \
  315. (_access) == UVERBS_ACCESS_DESTROY), \
  316. .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
  317. .u2.objs_arr.obj_type = _idr_type, \
  318. .u2.objs_arr.access = _access, \
  319. .u2.objs_arr.min_len = _min_len, \
  320. .u2.objs_arr.max_len = _max_len, \
  321. __VA_ARGS__ } })
  322. #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
  323. (&(const struct uverbs_attr_def){ \
  324. .id = _attr_id, \
  325. .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
  326. .u.obj.obj_type = _idr_type, \
  327. .u.obj.access = _access, \
  328. __VA_ARGS__ } })
  329. #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
  330. (&(const struct uverbs_attr_def){ \
  331. .id = (_attr_id) + \
  332. BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
  333. (_access) != UVERBS_ACCESS_READ), \
  334. .attr = { .type = UVERBS_ATTR_TYPE_FD, \
  335. .u.obj.obj_type = _fd_type, \
  336. .u.obj.access = _access, \
  337. __VA_ARGS__ } })
  338. #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
  339. (&(const struct uverbs_attr_def){ \
  340. .id = _attr_id, \
  341. .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
  342. _type, \
  343. __VA_ARGS__ } })
  344. #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
  345. (&(const struct uverbs_attr_def){ \
  346. .id = _attr_id, \
  347. .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
  348. _type, \
  349. __VA_ARGS__ } })
  350. /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
  351. #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
  352. (&(const struct uverbs_attr_def){ \
  353. .id = _attr_id, \
  354. .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
  355. .u2.enum_def.ids = _enum_arr, \
  356. .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
  357. __VA_ARGS__ }, \
  358. })
  359. /* An input value that is a member in the enum _enum_type. */
  360. #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
  361. UVERBS_ATTR_PTR_IN( \
  362. _attr_id, \
  363. UVERBS_ATTR_SIZE( \
  364. sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
  365. sizeof(u64)), \
  366. __VA_ARGS__)
  367. /*
  368. * An input value that is a bitwise combination of values of _enum_type.
  369. * This permits the flag value to be passed as either a u32 or u64, it must
  370. * be retrieved via uverbs_get_flag().
  371. */
  372. #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
  373. UVERBS_ATTR_PTR_IN( \
  374. _attr_id, \
  375. UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
  376. !sizeof(_enum_type *)), \
  377. sizeof(u64)), \
  378. __VA_ARGS__)
  379. /*
  380. * This spec is used in order to pass information to the hardware driver in a
  381. * legacy way. Every verb that could get driver specific data should get this
  382. * spec.
  383. */
  384. #define UVERBS_ATTR_UHW() \
  385. UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
  386. UVERBS_ATTR_MIN_SIZE(0), \
  387. UA_OPTIONAL), \
  388. UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
  389. UVERBS_ATTR_MIN_SIZE(0), \
  390. UA_OPTIONAL)
  391. /*
  392. * =======================================
  393. * Declaration helpers
  394. * =======================================
  395. */
  396. #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
  397. static const struct uverbs_object_def *const _name##_ptr[] = { \
  398. __VA_ARGS__, \
  399. }; \
  400. static const struct uverbs_object_tree_def _name = { \
  401. .num_objects = ARRAY_SIZE(_name##_ptr), \
  402. .objects = &_name##_ptr, \
  403. }
  404. /* =================================================
  405. * Parsing infrastructure
  406. * =================================================
  407. */
  408. struct uverbs_ptr_attr {
  409. /*
  410. * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
  411. * used.
  412. */
  413. union {
  414. void *ptr;
  415. u64 data;
  416. };
  417. u16 len;
  418. u16 uattr_idx;
  419. u8 enum_id;
  420. };
  421. struct uverbs_obj_attr {
  422. struct ib_uobject *uobject;
  423. const struct uverbs_api_attr *attr_elm;
  424. };
  425. struct uverbs_objs_arr_attr {
  426. struct ib_uobject **uobjects;
  427. u16 len;
  428. };
  429. struct uverbs_attr {
  430. union {
  431. struct uverbs_ptr_attr ptr_attr;
  432. struct uverbs_obj_attr obj_attr;
  433. struct uverbs_objs_arr_attr objs_arr_attr;
  434. };
  435. };
  436. struct uverbs_attr_bundle {
  437. struct ib_uverbs_file *ufile;
  438. DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
  439. struct uverbs_attr attrs[];
  440. };
  441. static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
  442. unsigned int idx)
  443. {
  444. return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
  445. attrs_bundle->attr_present);
  446. }
  447. #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
  448. static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
  449. u16 idx)
  450. {
  451. if (!uverbs_attr_is_valid(attrs_bundle, idx))
  452. return ERR_PTR(-ENOENT);
  453. return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
  454. }
  455. static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
  456. u16 idx)
  457. {
  458. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  459. if (IS_ERR(attr))
  460. return PTR_ERR(attr);
  461. return attr->ptr_attr.enum_id;
  462. }
  463. static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
  464. u16 idx)
  465. {
  466. const struct uverbs_attr *attr;
  467. attr = uverbs_attr_get(attrs_bundle, idx);
  468. if (IS_ERR(attr))
  469. return ERR_CAST(attr);
  470. return attr->obj_attr.uobject->object;
  471. }
  472. static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
  473. u16 idx)
  474. {
  475. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  476. if (IS_ERR(attr))
  477. return ERR_CAST(attr);
  478. return attr->obj_attr.uobject;
  479. }
  480. static inline int
  481. uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
  482. {
  483. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  484. if (IS_ERR(attr))
  485. return PTR_ERR(attr);
  486. return attr->ptr_attr.len;
  487. }
  488. /**
  489. * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
  490. * UVERBS_ATTR_TYPE_IDRS_ARRAY.
  491. * @arr: Returned pointer to array of pointers for uobjects or NULL if
  492. * the attribute isn't provided.
  493. *
  494. * Return: The array length or 0 if no attribute was provided.
  495. */
  496. static inline int uverbs_attr_get_uobjs_arr(
  497. const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
  498. struct ib_uobject ***arr)
  499. {
  500. const struct uverbs_attr *attr =
  501. uverbs_attr_get(attrs_bundle, attr_idx);
  502. if (IS_ERR(attr)) {
  503. *arr = NULL;
  504. return 0;
  505. }
  506. *arr = attr->objs_arr_attr.uobjects;
  507. return attr->objs_arr_attr.len;
  508. }
  509. static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
  510. {
  511. return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
  512. }
  513. static inline void *uverbs_attr_get_alloced_ptr(
  514. const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
  515. {
  516. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  517. if (IS_ERR(attr))
  518. return (void *)attr;
  519. return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
  520. attr->ptr_attr.ptr;
  521. }
  522. static inline int _uverbs_copy_from(void *to,
  523. const struct uverbs_attr_bundle *attrs_bundle,
  524. size_t idx,
  525. size_t size)
  526. {
  527. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  528. if (IS_ERR(attr))
  529. return PTR_ERR(attr);
  530. /*
  531. * Validation ensures attr->ptr_attr.len >= size. If the caller is
  532. * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
  533. * uverbs_copy_from_or_zero.
  534. */
  535. if (unlikely(size < attr->ptr_attr.len))
  536. return -EINVAL;
  537. if (uverbs_attr_ptr_is_inline(attr))
  538. memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
  539. else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
  540. attr->ptr_attr.len))
  541. return -EFAULT;
  542. return 0;
  543. }
  544. static inline int _uverbs_copy_from_or_zero(void *to,
  545. const struct uverbs_attr_bundle *attrs_bundle,
  546. size_t idx,
  547. size_t size)
  548. {
  549. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  550. size_t min_size;
  551. if (IS_ERR(attr))
  552. return PTR_ERR(attr);
  553. min_size = min_t(size_t, size, attr->ptr_attr.len);
  554. if (uverbs_attr_ptr_is_inline(attr))
  555. memcpy(to, &attr->ptr_attr.data, min_size);
  556. else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
  557. min_size))
  558. return -EFAULT;
  559. if (size > min_size)
  560. memset(to + min_size, 0, size - min_size);
  561. return 0;
  562. }
  563. #define uverbs_copy_from(to, attrs_bundle, idx) \
  564. _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
  565. #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
  566. _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
  567. #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
  568. int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  569. size_t idx, u64 allowed_bits);
  570. int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
  571. size_t idx, u64 allowed_bits);
  572. int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
  573. const void *from, size_t size);
  574. __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
  575. gfp_t flags);
  576. static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
  577. size_t size)
  578. {
  579. return _uverbs_alloc(bundle, size, GFP_KERNEL);
  580. }
  581. static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
  582. size_t size)
  583. {
  584. return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
  585. }
  586. int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  587. size_t idx, s64 lower_bound, u64 upper_bound,
  588. s64 *def_val);
  589. #else
  590. static inline int
  591. uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  592. size_t idx, u64 allowed_bits)
  593. {
  594. return -EINVAL;
  595. }
  596. static inline int
  597. uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
  598. size_t idx, u64 allowed_bits)
  599. {
  600. return -EINVAL;
  601. }
  602. static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
  603. size_t idx, const void *from, size_t size)
  604. {
  605. return -EINVAL;
  606. }
  607. static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
  608. size_t size)
  609. {
  610. return ERR_PTR(-EINVAL);
  611. }
  612. static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
  613. size_t size)
  614. {
  615. return ERR_PTR(-EINVAL);
  616. }
  617. static inline int
  618. _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  619. size_t idx, s64 lower_bound, u64 upper_bound,
  620. s64 *def_val)
  621. {
  622. return -EINVAL;
  623. }
  624. #endif
  625. #define uverbs_get_const(_to, _attrs_bundle, _idx) \
  626. ({ \
  627. s64 _val; \
  628. int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \
  629. type_min(typeof(*_to)), \
  630. type_max(typeof(*_to)), NULL); \
  631. (*_to) = _val; \
  632. _ret; \
  633. })
  634. #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
  635. ({ \
  636. s64 _val; \
  637. s64 _def_val = _default; \
  638. int _ret = \
  639. _uverbs_get_const(&_val, _attrs_bundle, _idx, \
  640. type_min(typeof(*_to)), \
  641. type_max(typeof(*_to)), &_def_val); \
  642. (*_to) = _val; \
  643. _ret; \
  644. })
  645. #endif