uverbs_ioctl.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
  4. */
  5. #ifndef _UVERBS_IOCTL_
  6. #define _UVERBS_IOCTL_
  7. #include <rdma/uverbs_types.h>
  8. #include <linux/uaccess.h>
  9. #include <rdma/rdma_user_ioctl.h>
  10. #include <rdma/ib_user_ioctl_verbs.h>
  11. #include <rdma/ib_user_ioctl_cmds.h>
  12. /*
  13. * =======================================
  14. * Verbs action specifications
  15. * =======================================
  16. */
  17. enum uverbs_attr_type {
  18. UVERBS_ATTR_TYPE_NA,
  19. UVERBS_ATTR_TYPE_PTR_IN,
  20. UVERBS_ATTR_TYPE_PTR_OUT,
  21. UVERBS_ATTR_TYPE_IDR,
  22. UVERBS_ATTR_TYPE_FD,
  23. UVERBS_ATTR_TYPE_ENUM_IN,
  24. UVERBS_ATTR_TYPE_IDRS_ARRAY,
  25. };
  26. enum uverbs_obj_access {
  27. UVERBS_ACCESS_READ,
  28. UVERBS_ACCESS_WRITE,
  29. UVERBS_ACCESS_NEW,
  30. UVERBS_ACCESS_DESTROY
  31. };
  32. /* Specification of a single attribute inside the ioctl message */
  33. /* good size 16 */
  34. struct uverbs_attr_spec {
  35. u8 type;
  36. /*
  37. * Support extending attributes by length. Allow the user to provide
  38. * more bytes than ptr.len, but check that everything after is zero'd
  39. * by the user.
  40. */
  41. u8 zero_trailing:1;
  42. /*
  43. * Valid only for PTR_IN. Allocate and copy the data inside
  44. * the parser
  45. */
  46. u8 alloc_and_copy:1;
  47. u8 mandatory:1;
  48. /* True if this is from UVERBS_ATTR_UHW */
  49. u8 is_udata:1;
  50. union {
  51. struct {
  52. /* Current known size to kernel */
  53. u16 len;
  54. /* User isn't allowed to provide something < min_len */
  55. u16 min_len;
  56. } ptr;
  57. struct {
  58. /*
  59. * higher bits mean the namespace and lower bits mean
  60. * the type id within the namespace.
  61. */
  62. u16 obj_type;
  63. u8 access;
  64. } obj;
  65. struct {
  66. u8 num_elems;
  67. } enum_def;
  68. } u;
  69. /* This weird split lets us remove some padding */
  70. union {
  71. struct {
  72. /*
  73. * The enum attribute can select one of the attributes
  74. * contained in the ids array. Currently only PTR_IN
  75. * attributes are supported in the ids array.
  76. */
  77. const struct uverbs_attr_spec *ids;
  78. } enum_def;
  79. struct {
  80. /*
  81. * higher bits mean the namespace and lower bits mean
  82. * the type id within the namespace.
  83. */
  84. u16 obj_type;
  85. u16 min_len;
  86. u16 max_len;
  87. u8 access;
  88. } objs_arr;
  89. } u2;
  90. };
  91. /*
  92. * Information about the API is loaded into a radix tree. For IOCTL we start
  93. * with a tuple of:
  94. * object_id, attr_id, method_id
  95. *
  96. * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
  97. * on the current kernel support this is compressed into 16 bit key for the
  98. * radix tree. Since this compression is entirely internal to the kernel the
  99. * below limits can be revised if the kernel gains additional data.
  100. *
  101. * With 64 leafs per node this is a 3 level radix tree.
  102. *
  103. * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
  104. * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
  105. *
  106. * This also encodes the tables for the write() and write() extended commands
  107. * using the coding
  108. * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
  109. * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
  110. * ie the WRITE path is treated as a special method type in the ioctl
  111. * framework.
  112. */
  113. enum uapi_radix_data {
  114. UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
  115. UVERBS_API_ATTR_KEY_BITS = 6,
  116. UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
  117. UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
  118. UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
  119. UVERBS_API_METHOD_KEY_BITS = 5,
  120. UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
  121. UVERBS_API_METHOD_KEY_NUM_CORE = 22,
  122. UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
  123. UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
  124. UVERBS_API_METHOD_KEY_NUM_DRIVER =
  125. (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
  126. UVERBS_API_METHOD_KEY_NUM_CORE,
  127. UVERBS_API_METHOD_KEY_MASK = GENMASK(
  128. UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
  129. UVERBS_API_METHOD_KEY_SHIFT),
  130. UVERBS_API_OBJ_KEY_BITS = 5,
  131. UVERBS_API_OBJ_KEY_SHIFT =
  132. UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
  133. UVERBS_API_OBJ_KEY_NUM_CORE = 20,
  134. UVERBS_API_OBJ_KEY_NUM_DRIVER =
  135. (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
  136. UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
  137. /* This id guaranteed to not exist in the radix tree */
  138. UVERBS_API_KEY_ERR = 0xFFFFFFFF,
  139. };
  140. static inline __attribute_const__ u32 uapi_key_obj(u32 id)
  141. {
  142. if (id & UVERBS_API_NS_FLAG) {
  143. id &= ~UVERBS_API_NS_FLAG;
  144. if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
  145. return UVERBS_API_KEY_ERR;
  146. id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
  147. } else {
  148. if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
  149. return UVERBS_API_KEY_ERR;
  150. }
  151. return id << UVERBS_API_OBJ_KEY_SHIFT;
  152. }
  153. static inline __attribute_const__ bool uapi_key_is_object(u32 key)
  154. {
  155. return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
  156. }
  157. static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
  158. {
  159. if (id & UVERBS_API_NS_FLAG) {
  160. id &= ~UVERBS_API_NS_FLAG;
  161. if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
  162. return UVERBS_API_KEY_ERR;
  163. id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
  164. } else {
  165. id++;
  166. if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
  167. return UVERBS_API_KEY_ERR;
  168. }
  169. return id << UVERBS_API_METHOD_KEY_SHIFT;
  170. }
  171. static inline __attribute_const__ u32 uapi_key_write_method(u32 id)
  172. {
  173. if (id >= UVERBS_API_WRITE_KEY_NUM)
  174. return UVERBS_API_KEY_ERR;
  175. return UVERBS_API_METHOD_IS_WRITE | id;
  176. }
  177. static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)
  178. {
  179. if (id >= UVERBS_API_WRITE_KEY_NUM)
  180. return UVERBS_API_KEY_ERR;
  181. return UVERBS_API_METHOD_IS_WRITE_EX | id;
  182. }
  183. static inline __attribute_const__ u32
  184. uapi_key_attr_to_ioctl_method(u32 attr_key)
  185. {
  186. return attr_key &
  187. (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
  188. }
  189. static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
  190. {
  191. unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
  192. return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
  193. (key & UVERBS_API_ATTR_KEY_MASK) == 0;
  194. }
  195. static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)
  196. {
  197. return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;
  198. }
  199. static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)
  200. {
  201. return (key & UVERBS_API_METHOD_KEY_MASK) ==
  202. UVERBS_API_METHOD_IS_WRITE_EX;
  203. }
  204. static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
  205. {
  206. /* 0 is the method slot itself */
  207. return ioctl_method_key + 1;
  208. }
  209. static inline __attribute_const__ u32 uapi_key_attr(u32 id)
  210. {
  211. /*
  212. * The attr is designed to fit in the typical single radix tree node
  213. * of 64 entries. Since allmost all methods have driver attributes we
  214. * organize things so that the driver and core attributes interleave to
  215. * reduce the length of the attributes array in typical cases.
  216. */
  217. if (id & UVERBS_API_NS_FLAG) {
  218. id &= ~UVERBS_API_NS_FLAG;
  219. id++;
  220. if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
  221. return UVERBS_API_KEY_ERR;
  222. id = (id << 1) | 0;
  223. } else {
  224. if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
  225. return UVERBS_API_KEY_ERR;
  226. id = (id << 1) | 1;
  227. }
  228. return id;
  229. }
  230. /* Only true for ioctl methods */
  231. static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
  232. {
  233. unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
  234. return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
  235. (key & UVERBS_API_ATTR_KEY_MASK) != 0;
  236. }
  237. /*
  238. * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
  239. * basically it undoes the reservation of 0 in the ID numbering. attr_key
  240. * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
  241. * uapi_key_attr().
  242. */
  243. static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
  244. {
  245. return attr_key - 1;
  246. }
  247. static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
  248. {
  249. return attr_bkey + 1;
  250. }
  251. /*
  252. * =======================================
  253. * Verbs definitions
  254. * =======================================
  255. */
  256. struct uverbs_attr_def {
  257. u16 id;
  258. struct uverbs_attr_spec attr;
  259. };
  260. struct uverbs_method_def {
  261. u16 id;
  262. /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
  263. u32 flags;
  264. size_t num_attrs;
  265. const struct uverbs_attr_def * const (*attrs)[];
  266. int (*handler)(struct uverbs_attr_bundle *attrs);
  267. };
  268. struct uverbs_object_def {
  269. u16 id;
  270. const struct uverbs_obj_type *type_attrs;
  271. size_t num_methods;
  272. const struct uverbs_method_def * const (*methods)[];
  273. };
  274. enum uapi_definition_kind {
  275. UAPI_DEF_END = 0,
  276. UAPI_DEF_OBJECT_START,
  277. UAPI_DEF_WRITE,
  278. UAPI_DEF_CHAIN_OBJ_TREE,
  279. UAPI_DEF_CHAIN,
  280. UAPI_DEF_IS_SUPPORTED_FUNC,
  281. UAPI_DEF_IS_SUPPORTED_DEV_FN,
  282. };
  283. enum uapi_definition_scope {
  284. UAPI_SCOPE_OBJECT = 1,
  285. UAPI_SCOPE_METHOD = 2,
  286. };
  287. struct uapi_definition {
  288. u8 kind;
  289. u8 scope;
  290. union {
  291. struct {
  292. u16 object_id;
  293. } object_start;
  294. struct {
  295. u16 command_num;
  296. u8 is_ex:1;
  297. u8 has_udata:1;
  298. u8 has_resp:1;
  299. u8 req_size;
  300. u8 resp_size;
  301. } write;
  302. };
  303. union {
  304. bool (*func_is_supported)(struct ib_device *device);
  305. int (*func_write)(struct uverbs_attr_bundle *attrs);
  306. const struct uapi_definition *chain;
  307. const struct uverbs_object_def *chain_obj_tree;
  308. size_t needs_fn_offset;
  309. };
  310. };
  311. /* Define things connected to object_id */
  312. #define DECLARE_UVERBS_OBJECT(_object_id, ...) \
  313. { \
  314. .kind = UAPI_DEF_OBJECT_START, \
  315. .object_start = { .object_id = _object_id }, \
  316. }, \
  317. ##__VA_ARGS__
  318. /* Use in a var_args of DECLARE_UVERBS_OBJECT */
  319. #define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \
  320. { \
  321. .kind = UAPI_DEF_WRITE, \
  322. .scope = UAPI_SCOPE_OBJECT, \
  323. .write = { .is_ex = 0, .command_num = _command_num }, \
  324. .func_write = _func, \
  325. _cmd_desc, \
  326. }, \
  327. ##__VA_ARGS__
  328. /* Use in a var_args of DECLARE_UVERBS_OBJECT */
  329. #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \
  330. { \
  331. .kind = UAPI_DEF_WRITE, \
  332. .scope = UAPI_SCOPE_OBJECT, \
  333. .write = { .is_ex = 1, .command_num = _command_num }, \
  334. .func_write = _func, \
  335. _cmd_desc, \
  336. }, \
  337. ##__VA_ARGS__
  338. /*
  339. * Object is only supported if the function pointer named ibdev_fn in struct
  340. * ib_device is not NULL.
  341. */
  342. #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \
  343. { \
  344. .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
  345. .scope = UAPI_SCOPE_OBJECT, \
  346. .needs_fn_offset = \
  347. offsetof(struct ib_device_ops, ibdev_fn) + \
  348. BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \
  349. ibdev_fn) != \
  350. sizeof(void *)), \
  351. }
  352. /*
  353. * Method is only supported if the function pointer named ibdev_fn in struct
  354. * ib_device is not NULL.
  355. */
  356. #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \
  357. { \
  358. .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
  359. .scope = UAPI_SCOPE_METHOD, \
  360. .needs_fn_offset = \
  361. offsetof(struct ib_device_ops, ibdev_fn) + \
  362. BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \
  363. ibdev_fn) != \
  364. sizeof(void *)), \
  365. }
  366. /* Call a function to determine if the entire object is supported or not */
  367. #define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \
  368. { \
  369. .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \
  370. .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \
  371. }
  372. /* Include another struct uapi_definition in this one */
  373. #define UAPI_DEF_CHAIN(_def_var) \
  374. { \
  375. .kind = UAPI_DEF_CHAIN, .chain = _def_var, \
  376. }
  377. /* Temporary until the tree base description is replaced */
  378. #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...) \
  379. { \
  380. .kind = UAPI_DEF_CHAIN_OBJ_TREE, \
  381. .object_start = { .object_id = _object_enum }, \
  382. .chain_obj_tree = _object_ptr, \
  383. }, \
  384. ##__VA_ARGS__
  385. #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \
  386. UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum), \
  387. ##__VA_ARGS__)
  388. /*
  389. * =======================================
  390. * Attribute Specifications
  391. * =======================================
  392. */
  393. #define UVERBS_ATTR_SIZE(_min_len, _len) \
  394. .u.ptr.min_len = _min_len, .u.ptr.len = _len
  395. #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
  396. /*
  397. * Specifies a uapi structure that cannot be extended. The user must always
  398. * supply the whole structure and nothing more. The structure must be declared
  399. * in a header under include/uapi/rdma.
  400. */
  401. #define UVERBS_ATTR_TYPE(_type) \
  402. .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
  403. /*
  404. * Specifies a uapi structure where the user must provide at least up to
  405. * member 'last'. Anything after last and up until the end of the structure
  406. * can be non-zero, anything longer than the end of the structure must be
  407. * zero. The structure must be declared in a header under include/uapi/rdma.
  408. */
  409. #define UVERBS_ATTR_STRUCT(_type, _last) \
  410. .zero_trailing = 1, \
  411. UVERBS_ATTR_SIZE(offsetofend(_type, _last), sizeof(_type))
  412. /*
  413. * Specifies at least min_len bytes must be passed in, but the amount can be
  414. * larger, up to the protocol maximum size. No check for zeroing is done.
  415. */
  416. #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
  417. /* Must be used in the '...' of any UVERBS_ATTR */
  418. #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
  419. #define UA_MANDATORY .mandatory = 1
  420. #define UA_OPTIONAL .mandatory = 0
  421. /*
  422. * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
  423. * READ\WRITE accesses are supported.
  424. */
  425. #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
  426. ...) \
  427. (&(const struct uverbs_attr_def){ \
  428. .id = (_attr_id) + \
  429. BUILD_BUG_ON_ZERO((_min_len) == 0 || \
  430. (_max_len) > \
  431. PAGE_SIZE / sizeof(void *) || \
  432. (_min_len) > (_max_len) || \
  433. (_access) == UVERBS_ACCESS_NEW || \
  434. (_access) == UVERBS_ACCESS_DESTROY), \
  435. .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
  436. .u2.objs_arr.obj_type = _idr_type, \
  437. .u2.objs_arr.access = _access, \
  438. .u2.objs_arr.min_len = _min_len, \
  439. .u2.objs_arr.max_len = _max_len, \
  440. __VA_ARGS__ } })
  441. /*
  442. * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,
  443. * the user must validate the type of the uobject instead.
  444. */
  445. #define UVERBS_IDR_ANY_OBJECT 0xFFFF
  446. #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
  447. (&(const struct uverbs_attr_def){ \
  448. .id = _attr_id, \
  449. .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
  450. .u.obj.obj_type = _idr_type, \
  451. .u.obj.access = _access, \
  452. __VA_ARGS__ } })
  453. #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
  454. (&(const struct uverbs_attr_def){ \
  455. .id = (_attr_id) + \
  456. BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
  457. (_access) != UVERBS_ACCESS_READ), \
  458. .attr = { .type = UVERBS_ATTR_TYPE_FD, \
  459. .u.obj.obj_type = _fd_type, \
  460. .u.obj.access = _access, \
  461. __VA_ARGS__ } })
  462. #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
  463. (&(const struct uverbs_attr_def){ \
  464. .id = _attr_id, \
  465. .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
  466. _type, \
  467. __VA_ARGS__ } })
  468. #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
  469. (&(const struct uverbs_attr_def){ \
  470. .id = _attr_id, \
  471. .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
  472. _type, \
  473. __VA_ARGS__ } })
  474. /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
  475. #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
  476. (&(const struct uverbs_attr_def){ \
  477. .id = _attr_id, \
  478. .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
  479. .u2.enum_def.ids = _enum_arr, \
  480. .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
  481. __VA_ARGS__ }, \
  482. })
  483. /* An input value that is a member in the enum _enum_type. */
  484. #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
  485. UVERBS_ATTR_PTR_IN( \
  486. _attr_id, \
  487. UVERBS_ATTR_SIZE( \
  488. sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
  489. sizeof(u64)), \
  490. __VA_ARGS__)
  491. /*
  492. * An input value that is a bitwise combination of values of _enum_type.
  493. * This permits the flag value to be passed as either a u32 or u64, it must
  494. * be retrieved via uverbs_get_flag().
  495. */
  496. #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
  497. UVERBS_ATTR_PTR_IN( \
  498. _attr_id, \
  499. UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
  500. !sizeof(_enum_type *)), \
  501. sizeof(u64)), \
  502. __VA_ARGS__)
  503. /*
  504. * This spec is used in order to pass information to the hardware driver in a
  505. * legacy way. Every verb that could get driver specific data should get this
  506. * spec.
  507. */
  508. #define UVERBS_ATTR_UHW() \
  509. UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
  510. UVERBS_ATTR_MIN_SIZE(0), \
  511. UA_OPTIONAL, \
  512. .is_udata = 1), \
  513. UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
  514. UVERBS_ATTR_MIN_SIZE(0), \
  515. UA_OPTIONAL, \
  516. .is_udata = 1)
  517. /* =================================================
  518. * Parsing infrastructure
  519. * =================================================
  520. */
  521. struct uverbs_ptr_attr {
  522. /*
  523. * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
  524. * used.
  525. */
  526. union {
  527. void *ptr;
  528. u64 data;
  529. };
  530. u16 len;
  531. u16 uattr_idx;
  532. u8 enum_id;
  533. };
  534. struct uverbs_obj_attr {
  535. struct ib_uobject *uobject;
  536. const struct uverbs_api_attr *attr_elm;
  537. };
  538. struct uverbs_objs_arr_attr {
  539. struct ib_uobject **uobjects;
  540. u16 len;
  541. };
  542. struct uverbs_attr {
  543. union {
  544. struct uverbs_ptr_attr ptr_attr;
  545. struct uverbs_obj_attr obj_attr;
  546. struct uverbs_objs_arr_attr objs_arr_attr;
  547. };
  548. };
  549. struct uverbs_attr_bundle {
  550. struct ib_udata driver_udata;
  551. struct ib_udata ucore;
  552. struct ib_uverbs_file *ufile;
  553. struct ib_ucontext *context;
  554. struct ib_uobject *uobject;
  555. DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
  556. struct uverbs_attr attrs[];
  557. };
  558. static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
  559. unsigned int idx)
  560. {
  561. return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
  562. attrs_bundle->attr_present);
  563. }
  564. /**
  565. * rdma_udata_to_drv_context - Helper macro to get the driver's context out of
  566. * ib_udata which is embedded in uverbs_attr_bundle.
  567. *
  568. * If udata is not NULL this cannot fail. Otherwise a NULL udata will result
  569. * in a NULL ucontext pointer, as a safety precaution. Callers should be using
  570. * 'udata' to determine if the driver call is in user or kernel mode, not
  571. * 'ucontext'.
  572. *
  573. */
  574. #define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \
  575. (udata ? container_of(container_of(udata, struct uverbs_attr_bundle, \
  576. driver_udata) \
  577. ->context, \
  578. drv_dev_struct, member) : \
  579. (drv_dev_struct *)NULL)
  580. #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
  581. static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
  582. u16 idx)
  583. {
  584. if (!uverbs_attr_is_valid(attrs_bundle, idx))
  585. return ERR_PTR(-ENOENT);
  586. return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
  587. }
  588. static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
  589. u16 idx)
  590. {
  591. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  592. if (IS_ERR(attr))
  593. return PTR_ERR(attr);
  594. return attr->ptr_attr.enum_id;
  595. }
  596. static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
  597. u16 idx)
  598. {
  599. const struct uverbs_attr *attr;
  600. attr = uverbs_attr_get(attrs_bundle, idx);
  601. if (IS_ERR(attr))
  602. return ERR_CAST(attr);
  603. return attr->obj_attr.uobject->object;
  604. }
  605. static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
  606. u16 idx)
  607. {
  608. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  609. if (IS_ERR(attr))
  610. return ERR_CAST(attr);
  611. return attr->obj_attr.uobject;
  612. }
  613. static inline int
  614. uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
  615. {
  616. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  617. if (IS_ERR(attr))
  618. return PTR_ERR(attr);
  619. return attr->ptr_attr.len;
  620. }
  621. void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle,
  622. u16 idx);
  623. /*
  624. * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
  625. * attribute.
  626. * @attrs: The attribute bundle
  627. * @idx: The ID of the attribute
  628. * @elem_size: The size of the element in the array
  629. */
  630. static inline int
  631. uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
  632. size_t elem_size)
  633. {
  634. int size = uverbs_attr_get_len(attrs, idx);
  635. if (size < 0)
  636. return size;
  637. if (size % elem_size)
  638. return -EINVAL;
  639. return size / elem_size;
  640. }
  641. /**
  642. * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
  643. * UVERBS_ATTR_TYPE_IDRS_ARRAY.
  644. * @arr: Returned pointer to array of pointers for uobjects or NULL if
  645. * the attribute isn't provided.
  646. *
  647. * Return: The array length or 0 if no attribute was provided.
  648. */
  649. static inline int uverbs_attr_get_uobjs_arr(
  650. const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
  651. struct ib_uobject ***arr)
  652. {
  653. const struct uverbs_attr *attr =
  654. uverbs_attr_get(attrs_bundle, attr_idx);
  655. if (IS_ERR(attr)) {
  656. *arr = NULL;
  657. return 0;
  658. }
  659. *arr = attr->objs_arr_attr.uobjects;
  660. return attr->objs_arr_attr.len;
  661. }
  662. static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
  663. {
  664. return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
  665. }
  666. static inline void *uverbs_attr_get_alloced_ptr(
  667. const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
  668. {
  669. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  670. if (IS_ERR(attr))
  671. return (void *)attr;
  672. return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
  673. attr->ptr_attr.ptr;
  674. }
  675. static inline int _uverbs_copy_from(void *to,
  676. const struct uverbs_attr_bundle *attrs_bundle,
  677. size_t idx,
  678. size_t size)
  679. {
  680. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  681. if (IS_ERR(attr))
  682. return PTR_ERR(attr);
  683. /*
  684. * Validation ensures attr->ptr_attr.len >= size. If the caller is
  685. * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
  686. * uverbs_copy_from_or_zero.
  687. */
  688. if (unlikely(size < attr->ptr_attr.len))
  689. return -EINVAL;
  690. if (uverbs_attr_ptr_is_inline(attr))
  691. memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
  692. else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
  693. attr->ptr_attr.len))
  694. return -EFAULT;
  695. return 0;
  696. }
  697. static inline int _uverbs_copy_from_or_zero(void *to,
  698. const struct uverbs_attr_bundle *attrs_bundle,
  699. size_t idx,
  700. size_t size)
  701. {
  702. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  703. size_t min_size;
  704. if (IS_ERR(attr))
  705. return PTR_ERR(attr);
  706. min_size = min_t(size_t, size, attr->ptr_attr.len);
  707. if (uverbs_attr_ptr_is_inline(attr))
  708. memcpy(to, &attr->ptr_attr.data, min_size);
  709. else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
  710. min_size))
  711. return -EFAULT;
  712. if (size > min_size)
  713. memset(to + min_size, 0, size - min_size);
  714. return 0;
  715. }
  716. #define uverbs_copy_from(to, attrs_bundle, idx) \
  717. _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
  718. #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
  719. _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
  720. static inline struct ib_ucontext *
  721. ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
  722. {
  723. return ib_uverbs_get_ucontext_file(attrs->ufile);
  724. }
  725. #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
  726. int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  727. size_t idx, u64 allowed_bits);
  728. int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
  729. size_t idx, u64 allowed_bits);
  730. int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
  731. const void *from, size_t size);
  732. __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
  733. gfp_t flags);
  734. static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
  735. size_t size)
  736. {
  737. return _uverbs_alloc(bundle, size, GFP_KERNEL);
  738. }
  739. static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
  740. size_t size)
  741. {
  742. return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
  743. }
  744. static inline __malloc void *uverbs_kcalloc(struct uverbs_attr_bundle *bundle,
  745. size_t n, size_t size)
  746. {
  747. size_t bytes;
  748. if (unlikely(check_mul_overflow(n, size, &bytes)))
  749. return ERR_PTR(-EOVERFLOW);
  750. return uverbs_zalloc(bundle, bytes);
  751. }
  752. int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  753. size_t idx, s64 lower_bound, u64 upper_bound,
  754. s64 *def_val);
  755. int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
  756. size_t idx, const void *from, size_t size);
  757. #else
  758. static inline int
  759. uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  760. size_t idx, u64 allowed_bits)
  761. {
  762. return -EINVAL;
  763. }
  764. static inline int
  765. uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
  766. size_t idx, u64 allowed_bits)
  767. {
  768. return -EINVAL;
  769. }
  770. static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
  771. size_t idx, const void *from, size_t size)
  772. {
  773. return -EINVAL;
  774. }
  775. static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
  776. size_t size)
  777. {
  778. return ERR_PTR(-EINVAL);
  779. }
  780. static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
  781. size_t size)
  782. {
  783. return ERR_PTR(-EINVAL);
  784. }
  785. static inline int
  786. _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  787. size_t idx, s64 lower_bound, u64 upper_bound,
  788. s64 *def_val)
  789. {
  790. return -EINVAL;
  791. }
  792. static inline int
  793. uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
  794. size_t idx, const void *from, size_t size)
  795. {
  796. return -EINVAL;
  797. }
  798. #endif
  799. #define uverbs_get_const(_to, _attrs_bundle, _idx) \
  800. ({ \
  801. s64 _val; \
  802. int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \
  803. type_min(typeof(*_to)), \
  804. type_max(typeof(*_to)), NULL); \
  805. (*_to) = _val; \
  806. _ret; \
  807. })
  808. #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
  809. ({ \
  810. s64 _val; \
  811. s64 _def_val = _default; \
  812. int _ret = \
  813. _uverbs_get_const(&_val, _attrs_bundle, _idx, \
  814. type_min(typeof(*_to)), \
  815. type_max(typeof(*_to)), &_def_val); \
  816. (*_to) = _val; \
  817. _ret; \
  818. })
  819. #endif