uverbs_ioctl.h 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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, \
  388. PTR_IF(IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS), \
  389. &UVERBS_OBJECT(_object_enum)), \
  390. ##__VA_ARGS__)
  391. /*
  392. * =======================================
  393. * Attribute Specifications
  394. * =======================================
  395. */
  396. #define UVERBS_ATTR_SIZE(_min_len, _len) \
  397. .u.ptr.min_len = _min_len, .u.ptr.len = _len
  398. #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
  399. /*
  400. * Specifies a uapi structure that cannot be extended. The user must always
  401. * supply the whole structure and nothing more. The structure must be declared
  402. * in a header under include/uapi/rdma.
  403. */
  404. #define UVERBS_ATTR_TYPE(_type) \
  405. .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
  406. /*
  407. * Specifies a uapi structure where the user must provide at least up to
  408. * member 'last'. Anything after last and up until the end of the structure
  409. * can be non-zero, anything longer than the end of the structure must be
  410. * zero. The structure must be declared in a header under include/uapi/rdma.
  411. */
  412. #define UVERBS_ATTR_STRUCT(_type, _last) \
  413. .zero_trailing = 1, \
  414. UVERBS_ATTR_SIZE(offsetofend(_type, _last), sizeof(_type))
  415. /*
  416. * Specifies at least min_len bytes must be passed in, but the amount can be
  417. * larger, up to the protocol maximum size. No check for zeroing is done.
  418. */
  419. #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
  420. /* Must be used in the '...' of any UVERBS_ATTR */
  421. #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
  422. #define UA_MANDATORY .mandatory = 1
  423. #define UA_OPTIONAL .mandatory = 0
  424. /*
  425. * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
  426. * READ\WRITE accesses are supported.
  427. */
  428. #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
  429. ...) \
  430. (&(const struct uverbs_attr_def){ \
  431. .id = (_attr_id) + \
  432. BUILD_BUG_ON_ZERO((_min_len) == 0 || \
  433. (_max_len) > \
  434. PAGE_SIZE / sizeof(void *) || \
  435. (_min_len) > (_max_len) || \
  436. (_access) == UVERBS_ACCESS_NEW || \
  437. (_access) == UVERBS_ACCESS_DESTROY), \
  438. .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
  439. .u2.objs_arr.obj_type = _idr_type, \
  440. .u2.objs_arr.access = _access, \
  441. .u2.objs_arr.min_len = _min_len, \
  442. .u2.objs_arr.max_len = _max_len, \
  443. __VA_ARGS__ } })
  444. /*
  445. * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,
  446. * the user must validate the type of the uobject instead.
  447. */
  448. #define UVERBS_IDR_ANY_OBJECT 0xFFFF
  449. #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
  450. (&(const struct uverbs_attr_def){ \
  451. .id = _attr_id, \
  452. .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
  453. .u.obj.obj_type = _idr_type, \
  454. .u.obj.access = _access, \
  455. __VA_ARGS__ } })
  456. #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
  457. (&(const struct uverbs_attr_def){ \
  458. .id = (_attr_id) + \
  459. BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
  460. (_access) != UVERBS_ACCESS_READ), \
  461. .attr = { .type = UVERBS_ATTR_TYPE_FD, \
  462. .u.obj.obj_type = _fd_type, \
  463. .u.obj.access = _access, \
  464. __VA_ARGS__ } })
  465. #define UVERBS_ATTR_RAW_FD(_attr_id, ...) \
  466. (&(const struct uverbs_attr_def){ \
  467. .id = (_attr_id), \
  468. .attr = { .type = UVERBS_ATTR_TYPE_RAW_FD, __VA_ARGS__ } })
  469. #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
  470. (&(const struct uverbs_attr_def){ \
  471. .id = _attr_id, \
  472. .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
  473. _type, \
  474. __VA_ARGS__ } })
  475. #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
  476. (&(const struct uverbs_attr_def){ \
  477. .id = _attr_id, \
  478. .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
  479. _type, \
  480. __VA_ARGS__ } })
  481. /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
  482. #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
  483. (&(const struct uverbs_attr_def){ \
  484. .id = _attr_id, \
  485. .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
  486. .u2.enum_def.ids = _enum_arr, \
  487. .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
  488. __VA_ARGS__ }, \
  489. })
  490. /* An input value that is a member in the enum _enum_type. */
  491. #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
  492. UVERBS_ATTR_PTR_IN( \
  493. _attr_id, \
  494. UVERBS_ATTR_SIZE( \
  495. sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
  496. sizeof(u64)), \
  497. __VA_ARGS__)
  498. /*
  499. * An input value that is a bitwise combination of values of _enum_type.
  500. * This permits the flag value to be passed as either a u32 or u64, it must
  501. * be retrieved via uverbs_get_flag().
  502. */
  503. #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
  504. UVERBS_ATTR_PTR_IN( \
  505. _attr_id, \
  506. UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
  507. !sizeof(_enum_type *)), \
  508. sizeof(u64)), \
  509. __VA_ARGS__)
  510. /*
  511. * This spec is used in order to pass information to the hardware driver in a
  512. * legacy way. Every verb that could get driver specific data should get this
  513. * spec.
  514. */
  515. #define UVERBS_ATTR_UHW() \
  516. UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
  517. UVERBS_ATTR_MIN_SIZE(0), \
  518. UA_OPTIONAL, \
  519. .is_udata = 1), \
  520. UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
  521. UVERBS_ATTR_MIN_SIZE(0), \
  522. UA_OPTIONAL, \
  523. .is_udata = 1)
  524. /* =================================================
  525. * Parsing infrastructure
  526. * =================================================
  527. */
  528. struct uverbs_ptr_attr {
  529. /*
  530. * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
  531. * used.
  532. */
  533. union {
  534. void *ptr;
  535. u64 data;
  536. };
  537. u16 len;
  538. u16 uattr_idx;
  539. u8 enum_id;
  540. };
  541. struct uverbs_obj_attr {
  542. struct ib_uobject *uobject;
  543. const struct uverbs_api_attr *attr_elm;
  544. };
  545. struct uverbs_objs_arr_attr {
  546. struct ib_uobject **uobjects;
  547. u16 len;
  548. };
  549. struct uverbs_attr {
  550. union {
  551. struct uverbs_ptr_attr ptr_attr;
  552. struct uverbs_obj_attr obj_attr;
  553. struct uverbs_objs_arr_attr objs_arr_attr;
  554. };
  555. };
  556. struct uverbs_attr_bundle {
  557. struct_group_tagged(uverbs_attr_bundle_hdr, hdr,
  558. struct ib_udata driver_udata;
  559. struct ib_udata ucore;
  560. struct ib_uverbs_file *ufile;
  561. struct ib_ucontext *context;
  562. struct ib_uobject *uobject;
  563. DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
  564. );
  565. struct uverbs_attr attrs[];
  566. };
  567. static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
  568. unsigned int idx)
  569. {
  570. return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
  571. attrs_bundle->attr_present);
  572. }
  573. /**
  574. * rdma_udata_to_drv_context - Helper macro to get the driver's context out of
  575. * ib_udata which is embedded in uverbs_attr_bundle.
  576. *
  577. * If udata is not NULL this cannot fail. Otherwise a NULL udata will result
  578. * in a NULL ucontext pointer, as a safety precaution. Callers should be using
  579. * 'udata' to determine if the driver call is in user or kernel mode, not
  580. * 'ucontext'.
  581. *
  582. */
  583. static inline struct uverbs_attr_bundle *
  584. rdma_udata_to_uverbs_attr_bundle(struct ib_udata *udata)
  585. {
  586. return container_of(udata, struct uverbs_attr_bundle, driver_udata);
  587. }
  588. #define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \
  589. (udata ? container_of(rdma_udata_to_uverbs_attr_bundle(udata)->context, \
  590. drv_dev_struct, member) : (drv_dev_struct *)NULL)
  591. #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
  592. static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
  593. u16 idx)
  594. {
  595. if (!uverbs_attr_is_valid(attrs_bundle, idx))
  596. return ERR_PTR(-ENOENT);
  597. return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
  598. }
  599. static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
  600. u16 idx)
  601. {
  602. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  603. if (IS_ERR(attr))
  604. return PTR_ERR(attr);
  605. return attr->ptr_attr.enum_id;
  606. }
  607. static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
  608. u16 idx)
  609. {
  610. const struct uverbs_attr *attr;
  611. attr = uverbs_attr_get(attrs_bundle, idx);
  612. if (IS_ERR(attr))
  613. return ERR_CAST(attr);
  614. return attr->obj_attr.uobject->object;
  615. }
  616. static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
  617. u16 idx)
  618. {
  619. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  620. if (IS_ERR(attr))
  621. return ERR_CAST(attr);
  622. return attr->obj_attr.uobject;
  623. }
  624. static inline int
  625. uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
  626. {
  627. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  628. if (IS_ERR(attr))
  629. return PTR_ERR(attr);
  630. return attr->ptr_attr.len;
  631. }
  632. void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle,
  633. u16 idx);
  634. /*
  635. * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
  636. * attribute.
  637. * @attrs: The attribute bundle
  638. * @idx: The ID of the attribute
  639. * @elem_size: The size of the element in the array
  640. */
  641. static inline int
  642. uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
  643. size_t elem_size)
  644. {
  645. int size = uverbs_attr_get_len(attrs, idx);
  646. if (size < 0)
  647. return size;
  648. if (size % elem_size)
  649. return -EINVAL;
  650. return size / elem_size;
  651. }
  652. /**
  653. * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
  654. * UVERBS_ATTR_TYPE_IDRS_ARRAY.
  655. * @arr: Returned pointer to array of pointers for uobjects or NULL if
  656. * the attribute isn't provided.
  657. *
  658. * Return: The array length or 0 if no attribute was provided.
  659. */
  660. static inline int uverbs_attr_get_uobjs_arr(
  661. const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
  662. struct ib_uobject ***arr)
  663. {
  664. const struct uverbs_attr *attr =
  665. uverbs_attr_get(attrs_bundle, attr_idx);
  666. if (IS_ERR(attr)) {
  667. *arr = NULL;
  668. return 0;
  669. }
  670. *arr = attr->objs_arr_attr.uobjects;
  671. return attr->objs_arr_attr.len;
  672. }
  673. static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
  674. {
  675. return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
  676. }
  677. static inline void *uverbs_attr_get_alloced_ptr(
  678. const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
  679. {
  680. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  681. if (IS_ERR(attr))
  682. return (void *)attr;
  683. return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
  684. attr->ptr_attr.ptr;
  685. }
  686. static inline int _uverbs_copy_from(void *to,
  687. const struct uverbs_attr_bundle *attrs_bundle,
  688. size_t idx,
  689. size_t size)
  690. {
  691. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  692. if (IS_ERR(attr))
  693. return PTR_ERR(attr);
  694. /*
  695. * Validation ensures attr->ptr_attr.len >= size. If the caller is
  696. * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
  697. * uverbs_copy_from_or_zero.
  698. */
  699. if (unlikely(size < attr->ptr_attr.len))
  700. return -EINVAL;
  701. if (uverbs_attr_ptr_is_inline(attr))
  702. memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
  703. else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
  704. attr->ptr_attr.len))
  705. return -EFAULT;
  706. return 0;
  707. }
  708. static inline int _uverbs_copy_from_or_zero(void *to,
  709. const struct uverbs_attr_bundle *attrs_bundle,
  710. size_t idx,
  711. size_t size)
  712. {
  713. const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
  714. size_t min_size;
  715. if (IS_ERR(attr))
  716. return PTR_ERR(attr);
  717. min_size = min_t(size_t, size, attr->ptr_attr.len);
  718. if (uverbs_attr_ptr_is_inline(attr))
  719. memcpy(to, &attr->ptr_attr.data, min_size);
  720. else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
  721. min_size))
  722. return -EFAULT;
  723. if (size > min_size)
  724. memset(to + min_size, 0, size - min_size);
  725. return 0;
  726. }
  727. #define uverbs_copy_from(to, attrs_bundle, idx) \
  728. _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
  729. #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
  730. _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
  731. static inline struct ib_ucontext *
  732. ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
  733. {
  734. return ib_uverbs_get_ucontext_file(attrs->ufile);
  735. }
  736. #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
  737. int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  738. size_t idx, u64 allowed_bits);
  739. int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
  740. size_t idx, u64 allowed_bits);
  741. int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
  742. const void *from, size_t size);
  743. __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
  744. gfp_t flags);
  745. static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
  746. size_t size)
  747. {
  748. return _uverbs_alloc(bundle, size, GFP_KERNEL);
  749. }
  750. static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
  751. size_t size)
  752. {
  753. return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
  754. }
  755. static inline __malloc void *uverbs_kcalloc(struct uverbs_attr_bundle *bundle,
  756. size_t n, size_t size)
  757. {
  758. size_t bytes;
  759. if (unlikely(check_mul_overflow(n, size, &bytes)))
  760. return ERR_PTR(-EOVERFLOW);
  761. return uverbs_zalloc(bundle, bytes);
  762. }
  763. int _uverbs_get_const_signed(s64 *to,
  764. const struct uverbs_attr_bundle *attrs_bundle,
  765. size_t idx, s64 lower_bound, u64 upper_bound,
  766. s64 *def_val);
  767. int _uverbs_get_const_unsigned(u64 *to,
  768. const struct uverbs_attr_bundle *attrs_bundle,
  769. size_t idx, u64 upper_bound, u64 *def_val);
  770. int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
  771. size_t idx, const void *from, size_t size);
  772. #else
  773. static inline int
  774. uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  775. size_t idx, u64 allowed_bits)
  776. {
  777. return -EINVAL;
  778. }
  779. static inline int
  780. uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
  781. size_t idx, u64 allowed_bits)
  782. {
  783. return -EINVAL;
  784. }
  785. static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
  786. size_t idx, const void *from, size_t size)
  787. {
  788. return -EINVAL;
  789. }
  790. static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
  791. size_t size)
  792. {
  793. return ERR_PTR(-EINVAL);
  794. }
  795. static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
  796. size_t size)
  797. {
  798. return ERR_PTR(-EINVAL);
  799. }
  800. static inline int
  801. _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
  802. size_t idx, s64 lower_bound, u64 upper_bound,
  803. s64 *def_val)
  804. {
  805. return -EINVAL;
  806. }
  807. static inline int
  808. uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
  809. size_t idx, const void *from, size_t size)
  810. {
  811. return -EINVAL;
  812. }
  813. static inline int
  814. _uverbs_get_const_signed(s64 *to,
  815. const struct uverbs_attr_bundle *attrs_bundle,
  816. size_t idx, s64 lower_bound, u64 upper_bound,
  817. s64 *def_val)
  818. {
  819. return -EINVAL;
  820. }
  821. static inline int
  822. _uverbs_get_const_unsigned(u64 *to,
  823. const struct uverbs_attr_bundle *attrs_bundle,
  824. size_t idx, u64 upper_bound, u64 *def_val)
  825. {
  826. return -EINVAL;
  827. }
  828. #endif
  829. #define uverbs_get_const_signed(_to, _attrs_bundle, _idx) \
  830. ({ \
  831. s64 _val; \
  832. int _ret = \
  833. _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \
  834. type_min(typeof(*(_to))), \
  835. type_max(typeof(*(_to))), NULL); \
  836. (*(_to)) = _val; \
  837. _ret; \
  838. })
  839. #define uverbs_get_const_unsigned(_to, _attrs_bundle, _idx) \
  840. ({ \
  841. u64 _val; \
  842. int _ret = \
  843. _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \
  844. type_max(typeof(*(_to))), NULL); \
  845. (*(_to)) = _val; \
  846. _ret; \
  847. })
  848. #define uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, _default) \
  849. ({ \
  850. s64 _val; \
  851. s64 _def_val = _default; \
  852. int _ret = \
  853. _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \
  854. type_min(typeof(*(_to))), \
  855. type_max(typeof(*(_to))), &_def_val); \
  856. (*(_to)) = _val; \
  857. _ret; \
  858. })
  859. #define uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, _default) \
  860. ({ \
  861. u64 _val; \
  862. u64 _def_val = _default; \
  863. int _ret = \
  864. _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \
  865. type_max(typeof(*(_to))), &_def_val); \
  866. (*(_to)) = _val; \
  867. _ret; \
  868. })
  869. #define uverbs_get_const(_to, _attrs_bundle, _idx) \
  870. (is_signed_type(typeof(*(_to))) ? \
  871. uverbs_get_const_signed(_to, _attrs_bundle, _idx) : \
  872. uverbs_get_const_unsigned(_to, _attrs_bundle, _idx)) \
  873. #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
  874. (is_signed_type(typeof(*(_to))) ? \
  875. uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, \
  876. _default) : \
  877. uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, \
  878. _default))
  879. static inline int
  880. uverbs_get_raw_fd(int *to, const struct uverbs_attr_bundle *attrs_bundle,
  881. size_t idx)
  882. {
  883. return uverbs_get_const_signed(to, attrs_bundle, idx);
  884. }
  885. #endif