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