uverbs_ioctl.h 33 KB

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