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