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, |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 55 | }; |
| 56 | |
Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 57 | enum uverbs_obj_access { |
| 58 | UVERBS_ACCESS_READ, |
| 59 | UVERBS_ACCESS_WRITE, |
| 60 | UVERBS_ACCESS_NEW, |
| 61 | UVERBS_ACCESS_DESTROY |
| 62 | }; |
| 63 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 64 | enum { |
| 65 | UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0, |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 66 | /* Support extending attributes by length, validate all unknown size == zero */ |
| 67 | UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO = 1U << 1, |
Matan Barak | 8762d14 | 2018-06-17 12:59:52 +0300 | [diff] [blame] | 68 | /* |
| 69 | * Valid only for PTR_IN. Allocate and copy the data inside the parser |
| 70 | */ |
| 71 | UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY = 1U << 2, |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 72 | }; |
| 73 | |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 74 | /* Specification of a single attribute inside the ioctl message */ |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 75 | struct uverbs_attr_spec { |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 76 | u8 type; |
| 77 | u8 flags; |
| 78 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 79 | union { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 80 | struct { |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 81 | /* Current known size to kernel */ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 82 | u16 len; |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 83 | /* User isn't allowed to provide something < min_len */ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 84 | u16 min_len; |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 85 | } ptr; |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 86 | |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 87 | struct { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 88 | /* |
| 89 | * higher bits mean the namespace and lower bits mean |
| 90 | * the type id within the namespace. |
| 91 | */ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 92 | u16 obj_type; |
| 93 | u8 access; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 94 | } obj; |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 95 | |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 96 | struct { |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 97 | u8 num_elems; |
| 98 | } enum_def; |
| 99 | } u; |
| 100 | |
| 101 | /* This weird split of the enum lets us remove some padding */ |
| 102 | union { |
| 103 | struct { |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 104 | /* |
| 105 | * The enum attribute can select one of the attributes |
| 106 | * contained in the ids array. Currently only PTR_IN |
| 107 | * attributes are supported in the ids array. |
| 108 | */ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 109 | const struct uverbs_attr_spec *ids; |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 110 | } enum_def; |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 111 | } u2; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | struct uverbs_attr_spec_hash { |
| 115 | size_t num_attrs; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 116 | unsigned long *mandatory_attrs_bitmask; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 117 | struct uverbs_attr_spec attrs[0]; |
| 118 | }; |
| 119 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 120 | struct uverbs_attr_bundle; |
| 121 | struct ib_uverbs_file; |
| 122 | |
| 123 | enum { |
| 124 | /* |
| 125 | * Action marked with this flag creates a context (or root for all |
| 126 | * objects). |
| 127 | */ |
| 128 | UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0, |
| 129 | }; |
| 130 | |
| 131 | struct uverbs_method_spec { |
| 132 | /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ |
| 133 | u32 flags; |
| 134 | size_t num_buckets; |
| 135 | size_t num_child_attrs; |
| 136 | int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, |
| 137 | struct uverbs_attr_bundle *ctx); |
| 138 | struct uverbs_attr_spec_hash *attr_buckets[0]; |
| 139 | }; |
| 140 | |
| 141 | struct uverbs_method_spec_hash { |
| 142 | size_t num_methods; |
| 143 | struct uverbs_method_spec *methods[0]; |
| 144 | }; |
| 145 | |
| 146 | struct uverbs_object_spec { |
| 147 | const struct uverbs_obj_type *type_attrs; |
| 148 | size_t num_buckets; |
| 149 | struct uverbs_method_spec_hash *method_buckets[0]; |
| 150 | }; |
| 151 | |
| 152 | struct uverbs_object_spec_hash { |
| 153 | size_t num_objects; |
| 154 | struct uverbs_object_spec *objects[0]; |
| 155 | }; |
| 156 | |
| 157 | struct uverbs_root_spec { |
| 158 | size_t num_buckets; |
| 159 | struct uverbs_object_spec_hash *object_buckets[0]; |
| 160 | }; |
| 161 | |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 162 | /* |
| 163 | * ======================================= |
| 164 | * Verbs definitions |
| 165 | * ======================================= |
| 166 | */ |
| 167 | |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 168 | struct uverbs_attr_def { |
| 169 | u16 id; |
| 170 | struct uverbs_attr_spec attr; |
| 171 | }; |
| 172 | |
| 173 | struct uverbs_method_def { |
| 174 | u16 id; |
| 175 | /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ |
| 176 | u32 flags; |
| 177 | size_t num_attrs; |
| 178 | const struct uverbs_attr_def * const (*attrs)[]; |
| 179 | int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, |
| 180 | struct uverbs_attr_bundle *ctx); |
| 181 | }; |
| 182 | |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 183 | struct uverbs_object_def { |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 184 | u16 id; |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 185 | const struct uverbs_obj_type *type_attrs; |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 186 | size_t num_methods; |
| 187 | const struct uverbs_method_def * const (*methods)[]; |
| 188 | }; |
| 189 | |
| 190 | struct uverbs_object_tree_def { |
| 191 | size_t num_objects; |
| 192 | const struct uverbs_object_def * const (*objects)[]; |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 193 | }; |
| 194 | |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 195 | /* |
| 196 | * ======================================= |
| 197 | * Attribute Specifications |
| 198 | * ======================================= |
| 199 | */ |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 200 | |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 201 | /* Use in the _type parameter for attribute specifications */ |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 202 | #define UVERBS_ATTR_TYPE(_type) \ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 203 | .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 204 | #define UVERBS_ATTR_STRUCT(_type, _last) \ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 205 | .u.ptr.min_len = ((uintptr_t)(&((_type *)0)->_last + 1)), .u.ptr.len = sizeof(_type) |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 206 | #define UVERBS_ATTR_SIZE(_min_len, _len) \ |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 207 | .u.ptr.min_len = _min_len, .u.ptr.len = _len |
Matan Barak | 2d9c1bd | 2018-06-17 12:59:53 +0300 | [diff] [blame] | 208 | #define UVERBS_ATTR_MIN_SIZE(_min_len) \ |
| 209 | UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 210 | |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 211 | /* Must be used in the '...' of any UVERBS_ATTR */ |
| 212 | #define UA_FLAGS(_flags) .flags = _flags |
| 213 | |
| 214 | #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ |
| 215 | ((const struct uverbs_attr_def){ \ |
| 216 | .id = _attr_id, \ |
| 217 | .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ |
| 218 | .u.obj.obj_type = _idr_type, \ |
| 219 | .u.obj.access = _access, \ |
| 220 | __VA_ARGS__ } }) |
| 221 | |
| 222 | #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ |
| 223 | ((const struct uverbs_attr_def){ \ |
| 224 | .id = (_attr_id) + \ |
| 225 | BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ |
| 226 | (_access) != UVERBS_ACCESS_READ), \ |
| 227 | .attr = { .type = UVERBS_ATTR_TYPE_FD, \ |
| 228 | .u.obj.obj_type = _fd_type, \ |
| 229 | .u.obj.access = _access, \ |
| 230 | __VA_ARGS__ } }) |
| 231 | |
| 232 | #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ |
| 233 | ((const struct uverbs_attr_def){ \ |
| 234 | .id = _attr_id, \ |
| 235 | .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ |
| 236 | _type, \ |
| 237 | __VA_ARGS__ } }) |
| 238 | |
| 239 | #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ |
| 240 | ((const struct uverbs_attr_def){ \ |
| 241 | .id = _attr_id, \ |
| 242 | .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ |
| 243 | _type, \ |
| 244 | __VA_ARGS__ } }) |
| 245 | |
| 246 | /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ |
| 247 | #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ |
| 248 | ((const struct uverbs_attr_def){ \ |
| 249 | .id = _attr_id, \ |
| 250 | .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ |
| 251 | .u2.enum_def.ids = _enum_arr, \ |
| 252 | .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ |
| 253 | __VA_ARGS__ }, \ |
| 254 | }) |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 255 | |
| 256 | /* |
Jason Gunthorpe | 6c61d2a | 2018-07-04 08:50:27 +0300 | [diff] [blame^] | 257 | * This spec is used in order to pass information to the hardware driver in a |
| 258 | * legacy way. Every verb that could get driver specific data should get this |
| 259 | * spec. |
| 260 | */ |
| 261 | #define UVERBS_ATTR_UHW() \ |
| 262 | &UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ |
| 263 | UVERBS_ATTR_SIZE(0, USHRT_MAX), \ |
| 264 | UA_FLAGS(UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO)), \ |
| 265 | &UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ |
| 266 | UVERBS_ATTR_SIZE(0, USHRT_MAX), \ |
| 267 | UA_FLAGS(UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO)) |
| 268 | |
| 269 | /* |
Jason Gunthorpe | d108dac | 2018-07-04 08:50:25 +0300 | [diff] [blame] | 270 | * ======================================= |
| 271 | * Declaration helpers |
| 272 | * ======================================= |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 273 | */ |
Jason Gunthorpe | 6c61d2a | 2018-07-04 08:50:27 +0300 | [diff] [blame^] | 274 | |
| 275 | #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ |
| 276 | static const struct uverbs_object_def *const _name##_ptr[] = { \ |
| 277 | __VA_ARGS__, \ |
| 278 | }; \ |
| 279 | static const struct uverbs_object_tree_def _name = { \ |
| 280 | .num_objects = ARRAY_SIZE(_name##_ptr), \ |
| 281 | .objects = &_name##_ptr, \ |
| 282 | } |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 283 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 284 | /* ================================================= |
| 285 | * Parsing infrastructure |
| 286 | * ================================================= |
| 287 | */ |
| 288 | |
| 289 | struct uverbs_ptr_attr { |
Matan Barak | 8762d14 | 2018-06-17 12:59:52 +0300 | [diff] [blame] | 290 | /* |
| 291 | * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is |
| 292 | * used. |
| 293 | */ |
| 294 | union { |
| 295 | void *ptr; |
| 296 | u64 data; |
| 297 | }; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 298 | u16 len; |
| 299 | /* Combination of bits from enum UVERBS_ATTR_F_XXXX */ |
| 300 | u16 flags; |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 301 | u8 enum_id; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 302 | }; |
| 303 | |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 304 | struct uverbs_obj_attr { |
| 305 | struct ib_uobject *uobject; |
| 306 | }; |
| 307 | |
| 308 | struct uverbs_attr { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 309 | /* |
| 310 | * pointer to the user-space given attribute, in order to write the |
| 311 | * new uobject's id or update flags. |
| 312 | */ |
| 313 | struct ib_uverbs_attr __user *uattr; |
| 314 | union { |
| 315 | struct uverbs_ptr_attr ptr_attr; |
| 316 | struct uverbs_obj_attr obj_attr; |
| 317 | }; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | struct uverbs_attr_bundle_hash { |
| 321 | /* if bit i is set, it means attrs[i] contains valid information */ |
| 322 | unsigned long *valid_bitmap; |
| 323 | size_t num_attrs; |
| 324 | /* |
| 325 | * arrays of attributes, each element corresponds to the specification |
| 326 | * of the attribute in the same index. |
| 327 | */ |
| 328 | struct uverbs_attr *attrs; |
| 329 | }; |
| 330 | |
| 331 | struct uverbs_attr_bundle { |
| 332 | size_t num_buckets; |
| 333 | struct uverbs_attr_bundle_hash hash[]; |
| 334 | }; |
| 335 | |
| 336 | static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash, |
| 337 | unsigned int idx) |
| 338 | { |
| 339 | return test_bit(idx, attrs_hash->valid_bitmap); |
| 340 | } |
| 341 | |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 342 | static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, |
| 343 | unsigned int idx) |
| 344 | { |
| 345 | u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; |
| 346 | |
| 347 | if (attrs_bundle->num_buckets <= idx_bucket) |
| 348 | return false; |
| 349 | |
| 350 | return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket], |
| 351 | idx & ~UVERBS_ID_NS_MASK); |
| 352 | } |
| 353 | |
Matan Barak | 41b2a71 | 2018-03-19 15:02:38 +0200 | [diff] [blame] | 354 | #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) |
| 355 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 356 | static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, |
| 357 | u16 idx) |
| 358 | { |
| 359 | u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; |
| 360 | |
| 361 | if (!uverbs_attr_is_valid(attrs_bundle, idx)) |
| 362 | return ERR_PTR(-ENOENT); |
| 363 | |
| 364 | return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK]; |
| 365 | } |
| 366 | |
Matan Barak | 494c558 | 2018-03-28 09:27:42 +0300 | [diff] [blame] | 367 | static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, |
| 368 | u16 idx) |
| 369 | { |
| 370 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 371 | |
| 372 | if (IS_ERR(attr)) |
| 373 | return PTR_ERR(attr); |
| 374 | |
| 375 | return attr->ptr_attr.enum_id; |
| 376 | } |
| 377 | |
Ariel Levkovich | be934cc | 2018-04-05 18:53:25 +0300 | [diff] [blame] | 378 | static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, |
| 379 | u16 idx) |
| 380 | { |
Jason Gunthorpe | f4602cb | 2018-05-22 15:56:51 -0600 | [diff] [blame] | 381 | const struct uverbs_attr *attr; |
Ariel Levkovich | be934cc | 2018-04-05 18:53:25 +0300 | [diff] [blame] | 382 | |
Jason Gunthorpe | f4602cb | 2018-05-22 15:56:51 -0600 | [diff] [blame] | 383 | attr = uverbs_attr_get(attrs_bundle, idx); |
| 384 | if (IS_ERR(attr)) |
| 385 | return ERR_CAST(attr); |
Ariel Levkovich | be934cc | 2018-04-05 18:53:25 +0300 | [diff] [blame] | 386 | |
Jason Gunthorpe | f4602cb | 2018-05-22 15:56:51 -0600 | [diff] [blame] | 387 | return attr->obj_attr.uobject->object; |
Ariel Levkovich | be934cc | 2018-04-05 18:53:25 +0300 | [diff] [blame] | 388 | } |
| 389 | |
Matan Barak | 3efa381 | 2018-05-31 16:43:28 +0300 | [diff] [blame] | 390 | static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, |
| 391 | u16 idx) |
| 392 | { |
| 393 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 394 | |
| 395 | if (IS_ERR(attr)) |
| 396 | return ERR_CAST(attr); |
| 397 | |
| 398 | return attr->obj_attr.uobject; |
| 399 | } |
| 400 | |
Matan Barak | 8762d14 | 2018-06-17 12:59:52 +0300 | [diff] [blame] | 401 | static inline int |
| 402 | uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) |
| 403 | { |
| 404 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 405 | |
| 406 | if (IS_ERR(attr)) |
| 407 | return PTR_ERR(attr); |
| 408 | |
| 409 | return attr->ptr_attr.len; |
| 410 | } |
| 411 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 412 | static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 413 | size_t idx, const void *from, size_t size) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 414 | { |
| 415 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 416 | u16 flags; |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 417 | size_t min_size; |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 418 | |
| 419 | if (IS_ERR(attr)) |
| 420 | return PTR_ERR(attr); |
| 421 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 422 | min_size = min_t(size_t, attr->ptr_attr.len, size); |
Jason Gunthorpe | 2f36028 | 2018-02-13 12:18:31 +0200 | [diff] [blame] | 423 | if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size)) |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 424 | return -EFAULT; |
| 425 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 426 | flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT; |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 427 | if (put_user(flags, &attr->uattr->flags)) |
| 428 | return -EFAULT; |
| 429 | |
| 430 | return 0; |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 431 | } |
| 432 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 433 | static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) |
| 434 | { |
| 435 | return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); |
| 436 | } |
| 437 | |
Matan Barak | 8762d14 | 2018-06-17 12:59:52 +0300 | [diff] [blame] | 438 | static inline void *uverbs_attr_get_alloced_ptr( |
| 439 | const struct uverbs_attr_bundle *attrs_bundle, u16 idx) |
| 440 | { |
| 441 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 442 | |
| 443 | if (IS_ERR(attr)) |
| 444 | return (void *)attr; |
| 445 | |
| 446 | return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : |
| 447 | attr->ptr_attr.ptr; |
| 448 | } |
| 449 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 450 | static inline int _uverbs_copy_from(void *to, |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 451 | const struct uverbs_attr_bundle *attrs_bundle, |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 452 | size_t idx, |
| 453 | size_t size) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 454 | { |
| 455 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 456 | |
| 457 | if (IS_ERR(attr)) |
| 458 | return PTR_ERR(attr); |
| 459 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 460 | /* |
| 461 | * Validation ensures attr->ptr_attr.len >= size. If the caller is |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 462 | * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call |
| 463 | * uverbs_copy_from_or_zero. |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 464 | */ |
| 465 | if (unlikely(size < attr->ptr_attr.len)) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | if (uverbs_attr_ptr_is_inline(attr)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 469 | memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); |
Jason Gunthorpe | 2f36028 | 2018-02-13 12:18:31 +0200 | [diff] [blame] | 470 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), |
| 471 | attr->ptr_attr.len)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 472 | return -EFAULT; |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 477 | static inline int _uverbs_copy_from_or_zero(void *to, |
| 478 | const struct uverbs_attr_bundle *attrs_bundle, |
| 479 | size_t idx, |
| 480 | size_t size) |
| 481 | { |
| 482 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 483 | size_t min_size; |
| 484 | |
| 485 | if (IS_ERR(attr)) |
| 486 | return PTR_ERR(attr); |
| 487 | |
| 488 | min_size = min_t(size_t, size, attr->ptr_attr.len); |
| 489 | |
| 490 | if (uverbs_attr_ptr_is_inline(attr)) |
| 491 | memcpy(to, &attr->ptr_attr.data, min_size); |
| 492 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), |
| 493 | min_size)) |
| 494 | return -EFAULT; |
| 495 | |
| 496 | if (size > min_size) |
| 497 | memset(to + min_size, 0, size - min_size); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 502 | #define uverbs_copy_from(to, attrs_bundle, idx) \ |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 503 | _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 504 | |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 505 | #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ |
| 506 | _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) |
| 507 | |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 508 | /* ================================================= |
| 509 | * Definitions -> Specs infrastructure |
| 510 | * ================================================= |
| 511 | */ |
Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 512 | |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 513 | /* |
| 514 | * uverbs_alloc_spec_tree - Merges different common and driver specific feature |
| 515 | * into one parsing tree that every uverbs command will be parsed upon. |
| 516 | * |
| 517 | * @num_trees: Number of trees in the array @trees. |
| 518 | * @trees: Array of pointers to tree root definitions to merge. Each such tree |
| 519 | * possibly contains objects, methods and attributes definitions. |
| 520 | * |
| 521 | * Returns: |
| 522 | * uverbs_root_spec *: The root of the merged parsing tree. |
| 523 | * On error, we return an error code. Error is checked via IS_ERR. |
| 524 | * |
| 525 | * The following merges could take place: |
| 526 | * a. Two trees representing the same method with different handler |
| 527 | * -> We take the handler of the tree that its handler != NULL |
| 528 | * and its index in the trees array is greater. The incentive for that |
| 529 | * is that developers are expected to first merge common trees and then |
| 530 | * merge trees that gives specialized the behaviour. |
| 531 | * b. Two trees representing the same object with different |
| 532 | * type_attrs (struct uverbs_obj_type): |
| 533 | * -> We take the type_attrs of the tree that its type_attr != NULL |
| 534 | * and its index in the trees array is greater. This could be used |
| 535 | * in order to override the free function, allocation size, etc. |
| 536 | * c. Two trees representing the same method attribute (same id but possibly |
| 537 | * different attributes): |
| 538 | * -> ERROR (-ENOENT), we believe that's not the programmer's intent. |
| 539 | * |
| 540 | * An object without any methods is considered invalid and will abort the |
| 541 | * function with -ENOENT error. |
| 542 | */ |
Matan Barak | 5242711 | 2017-08-03 16:07:06 +0300 | [diff] [blame] | 543 | #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 544 | struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, |
| 545 | const struct uverbs_object_tree_def **trees); |
| 546 | void uverbs_free_spec_tree(struct uverbs_root_spec *root); |
Matan Barak | 5242711 | 2017-08-03 16:07:06 +0300 | [diff] [blame] | 547 | #else |
| 548 | static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, |
| 549 | const struct uverbs_object_tree_def **trees) |
| 550 | { |
| 551 | return NULL; |
| 552 | } |
| 553 | |
| 554 | static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) |
| 555 | { |
| 556 | } |
| 557 | #endif |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 558 | |
| 559 | #endif |