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