xref: /linux/drivers/infiniband/core/uverbs_ioctl.c (revision ab2d2b2872aed03cbf0b7c85eeb45fda34d8059b)
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 #include <rdma/rdma_user_ioctl.h>
34 #include <rdma/uverbs_ioctl.h>
35 #include "rdma_core.h"
36 #include "uverbs.h"
37 
38 /*
39  * Each method has an absolute minimum amount of memory it needs to allocate,
40  * precompute that amount and determine if the onstack memory can be used or
41  * if allocation is need.
42  */
43 void uapi_compute_bundle_size(struct uverbs_api_ioctl_method *method_elm,
44 			      unsigned int num_attrs)
45 {
46 	struct bundle_priv *pbundle;
47 	struct uverbs_attr_bundle *bundle;
48 	size_t bundle_size =
49 		offsetof(struct bundle_priv, internal_buffer) +
50 		sizeof(*bundle->attrs) * method_elm->key_bitmap_len +
51 		sizeof(*pbundle->uattrs) * num_attrs;
52 
53 	method_elm->use_stack = bundle_size <= sizeof(*pbundle);
54 	method_elm->bundle_size =
55 		ALIGN(bundle_size + 256, sizeof(*pbundle->internal_buffer));
56 
57 	/* Do not want order-2 allocations for this. */
58 	WARN_ON_ONCE(method_elm->bundle_size > PAGE_SIZE);
59 }
60 
61 static bool uverbs_is_attr_cleared(const struct ib_uverbs_attr *uattr,
62 				   u16 len)
63 {
64 	if (uattr->len > sizeof_field(struct ib_uverbs_attr, data))
65 		return ib_is_buffer_cleared(u64_to_user_ptr(uattr->data) + len,
66 					    uattr->len - len);
67 
68 	return !memchr_inv((const void *)&uattr->data + len,
69 			   0, uattr->len - len);
70 }
71 
72 static int uverbs_process_idrs_array(struct bundle_priv *pbundle,
73 				     const struct uverbs_api_attr *attr_uapi,
74 				     struct uverbs_objs_arr_attr *attr,
75 				     struct ib_uverbs_attr *uattr,
76 				     u32 attr_bkey)
77 {
78 	struct uverbs_attr_bundle *bundle =
79 		container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
80 	const struct uverbs_attr_spec *spec = &attr_uapi->spec;
81 	size_t array_len;
82 	u32 *idr_vals;
83 	int ret = 0;
84 	size_t i;
85 
86 	if (uattr->attr_data.reserved)
87 		return -EINVAL;
88 
89 	if (uattr->len % sizeof(u32))
90 		return -EINVAL;
91 
92 	array_len = uattr->len / sizeof(u32);
93 	if (array_len < spec->u2.objs_arr.min_len ||
94 	    array_len > spec->u2.objs_arr.max_len)
95 		return -EINVAL;
96 
97 	attr->uobjects =
98 		uverbs_alloc(bundle,
99 			     array_size(array_len, sizeof(*attr->uobjects)));
100 	if (IS_ERR(attr->uobjects))
101 		return PTR_ERR(attr->uobjects);
102 
103 	/*
104 	 * Since idr is 4B and *uobjects is >= 4B, we can use attr->uobjects
105 	 * to store idrs array and avoid additional memory allocation. The
106 	 * idrs array is offset to the end of the uobjects array so we will be
107 	 * able to read idr and replace with a pointer.
108 	 */
109 	idr_vals = (u32 *)(attr->uobjects + array_len) - array_len;
110 
111 	if (uattr->len > sizeof(uattr->data)) {
112 		ret = copy_from_user(idr_vals, u64_to_user_ptr(uattr->data),
113 				     uattr->len);
114 		if (ret)
115 			return -EFAULT;
116 	} else {
117 		memcpy(idr_vals, &uattr->data, uattr->len);
118 	}
119 
120 	for (i = 0; i != array_len; i++) {
121 		attr->uobjects[i] = uverbs_get_uobject_from_file(
122 			spec->u2.objs_arr.obj_type, spec->u2.objs_arr.access,
123 			idr_vals[i], bundle);
124 		if (IS_ERR(attr->uobjects[i])) {
125 			ret = PTR_ERR(attr->uobjects[i]);
126 			break;
127 		}
128 	}
129 
130 	attr->len = i;
131 	__set_bit(attr_bkey, pbundle->spec_finalize);
132 	return ret;
133 }
134 
135 static void uverbs_free_idrs_array(const struct uverbs_api_attr *attr_uapi,
136 				   struct uverbs_objs_arr_attr *attr,
137 				   bool commit,
138 				   struct uverbs_attr_bundle *attrs)
139 {
140 	const struct uverbs_attr_spec *spec = &attr_uapi->spec;
141 	size_t i;
142 
143 	for (i = 0; i != attr->len; i++)
144 		uverbs_finalize_object(attr->uobjects[i],
145 				       spec->u2.objs_arr.access, false, commit,
146 				       attrs);
147 }
148 
149 static int uverbs_process_attr(struct bundle_priv *pbundle,
150 			       const struct uverbs_api_attr *attr_uapi,
151 			       struct ib_uverbs_attr *uattr, u32 attr_bkey)
152 {
153 	const struct uverbs_attr_spec *spec = &attr_uapi->spec;
154 	struct uverbs_attr_bundle *bundle =
155 		container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
156 	struct uverbs_attr *e = &bundle->attrs[attr_bkey];
157 	const struct uverbs_attr_spec *val_spec = spec;
158 	struct uverbs_obj_attr *o_attr;
159 
160 	switch (spec->type) {
161 	case UVERBS_ATTR_TYPE_ENUM_IN:
162 		if (uattr->attr_data.enum_data.elem_id >= spec->u.enum_def.num_elems)
163 			return -EOPNOTSUPP;
164 
165 		if (uattr->attr_data.enum_data.reserved)
166 			return -EINVAL;
167 
168 		val_spec = &spec->u2.enum_def.ids[uattr->attr_data.enum_data.elem_id];
169 
170 		/* Currently we only support PTR_IN based enums */
171 		if (val_spec->type != UVERBS_ATTR_TYPE_PTR_IN)
172 			return -EOPNOTSUPP;
173 
174 		e->ptr_attr.enum_id = uattr->attr_data.enum_data.elem_id;
175 		fallthrough;
176 	case UVERBS_ATTR_TYPE_PTR_IN:
177 		/* Ensure that any data provided by userspace beyond the known
178 		 * struct is zero. Userspace that knows how to use some future
179 		 * longer struct will fail here if used with an old kernel and
180 		 * non-zero content, making ABI compat/discovery simpler.
181 		 */
182 		if (uattr->len > val_spec->u.ptr.len &&
183 		    val_spec->zero_trailing &&
184 		    !uverbs_is_attr_cleared(uattr, val_spec->u.ptr.len))
185 			return -EOPNOTSUPP;
186 
187 		fallthrough;
188 	case UVERBS_ATTR_TYPE_PTR_OUT:
189 		if (uattr->len < val_spec->u.ptr.min_len ||
190 		    (!val_spec->zero_trailing &&
191 		     uattr->len > val_spec->u.ptr.len))
192 			return -EINVAL;
193 
194 		if (spec->type != UVERBS_ATTR_TYPE_ENUM_IN &&
195 		    uattr->attr_data.reserved)
196 			return -EINVAL;
197 
198 		e->ptr_attr.uattr_idx = uattr - pbundle->uattrs;
199 		e->ptr_attr.len = uattr->len;
200 
201 		if (val_spec->alloc_and_copy && !uverbs_attr_ptr_is_inline(e)) {
202 			void *p;
203 
204 			p = uverbs_alloc(bundle, uattr->len);
205 			if (IS_ERR(p))
206 				return PTR_ERR(p);
207 
208 			e->ptr_attr.ptr = p;
209 
210 			if (copy_from_user(p, u64_to_user_ptr(uattr->data),
211 					   uattr->len))
212 				return -EFAULT;
213 		} else {
214 			e->ptr_attr.data = uattr->data;
215 		}
216 		break;
217 
218 	case UVERBS_ATTR_TYPE_IDR:
219 	case UVERBS_ATTR_TYPE_FD:
220 		if (uattr->attr_data.reserved)
221 			return -EINVAL;
222 
223 		if (uattr->len != 0)
224 			return -EINVAL;
225 
226 		o_attr = &e->obj_attr;
227 		o_attr->attr_elm = attr_uapi;
228 
229 		/*
230 		 * The type of uattr->data is u64 for UVERBS_ATTR_TYPE_IDR and
231 		 * s64 for UVERBS_ATTR_TYPE_FD. We can cast the u64 to s64
232 		 * here without caring about truncation as we know that the
233 		 * IDR implementation today rejects negative IDs
234 		 */
235 		o_attr->uobject = uverbs_get_uobject_from_file(
236 			spec->u.obj.obj_type, spec->u.obj.access,
237 			uattr->data_s64, bundle);
238 		if (IS_ERR(o_attr->uobject))
239 			return PTR_ERR(o_attr->uobject);
240 		__set_bit(attr_bkey, pbundle->uobj_finalize);
241 
242 		if (spec->u.obj.access == UVERBS_ACCESS_NEW) {
243 			unsigned int uattr_idx = uattr - pbundle->uattrs;
244 			s64 id = o_attr->uobject->id;
245 
246 			/* Copy the allocated id to the user-space */
247 			if (put_user(id, &pbundle->user_attrs[uattr_idx].data))
248 				return -EFAULT;
249 		}
250 
251 		break;
252 
253 	case UVERBS_ATTR_TYPE_RAW_FD:
254 		if (uattr->attr_data.reserved || uattr->len != 0 ||
255 		    uattr->data_s64 < INT_MIN || uattr->data_s64 > INT_MAX)
256 			return -EINVAL;
257 		/* _uverbs_get_const_signed() is the accessor */
258 		e->ptr_attr.data = uattr->data_s64;
259 		break;
260 
261 	case UVERBS_ATTR_TYPE_IDRS_ARRAY:
262 		return uverbs_process_idrs_array(pbundle, attr_uapi,
263 						 &e->objs_arr_attr, uattr,
264 						 attr_bkey);
265 	default:
266 		return -EOPNOTSUPP;
267 	}
268 
269 	return 0;
270 }
271 
272 /*
273  * We search the radix tree with the method prefix and now we want to fast
274  * search the suffix bits to get a particular attribute pointer. It is not
275  * totally clear to me if this breaks the radix tree encasulation or not, but
276  * it uses the iter data to determine if the method iter points at the same
277  * chunk that will store the attribute, if so it just derefs it directly. By
278  * construction in most kernel configs the method and attrs will all fit in a
279  * single radix chunk, so in most cases this will have no search. Other cases
280  * this falls back to a full search.
281  */
282 static void __rcu **uapi_get_attr_for_method(struct bundle_priv *pbundle,
283 					     u32 attr_key)
284 {
285 	void __rcu **slot;
286 
287 	if (likely(attr_key < pbundle->radix_slots_len)) {
288 		void *entry;
289 
290 		slot = pbundle->radix_slots + attr_key;
291 		entry = rcu_dereference_raw(*slot);
292 		if (likely(!radix_tree_is_internal_node(entry) && entry))
293 			return slot;
294 	}
295 
296 	return radix_tree_lookup_slot(pbundle->radix,
297 				      pbundle->method_key | attr_key);
298 }
299 
300 static int uverbs_set_attr(struct bundle_priv *pbundle,
301 			   struct ib_uverbs_attr *uattr)
302 {
303 	u32 attr_key = uapi_key_attr(uattr->attr_id);
304 	u32 attr_bkey = uapi_bkey_attr(attr_key);
305 	const struct uverbs_api_attr *attr;
306 	void __rcu **slot;
307 	int ret;
308 
309 	slot = uapi_get_attr_for_method(pbundle, attr_key);
310 	if (!slot) {
311 		/*
312 		 * Kernel does not support the attribute but user-space says it
313 		 * is mandatory
314 		 */
315 		if (uattr->flags & UVERBS_ATTR_F_MANDATORY)
316 			return -EPROTONOSUPPORT;
317 		return 0;
318 	}
319 	attr = rcu_dereference_protected(*slot, true);
320 
321 	/* Reject duplicate attributes from user-space */
322 	if (test_bit(attr_bkey, pbundle->bundle.attr_present))
323 		return -EINVAL;
324 
325 	ret = uverbs_process_attr(pbundle, attr, uattr, attr_bkey);
326 	if (ret)
327 		return ret;
328 
329 	__set_bit(attr_bkey, pbundle->bundle.attr_present);
330 
331 	return 0;
332 }
333 
334 static int ib_uverbs_run_method(struct bundle_priv *pbundle,
335 				unsigned int num_attrs)
336 {
337 	int (*handler)(struct uverbs_attr_bundle *attrs);
338 	struct uverbs_attr_bundle *bundle =
339 		container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
340 	size_t uattrs_size = array_size(sizeof(*pbundle->uattrs), num_attrs);
341 	unsigned int destroy_bkey = bundle->method_elm->destroy_bkey;
342 	unsigned int i;
343 	int ret;
344 
345 	/* See uverbs_disassociate_api() */
346 	handler = srcu_dereference(
347 		bundle->method_elm->handler,
348 		&pbundle->bundle.ufile->device->disassociate_srcu);
349 	if (!handler)
350 		return -EIO;
351 
352 	pbundle->uattrs = uverbs_alloc(bundle, uattrs_size);
353 	if (IS_ERR(pbundle->uattrs))
354 		return PTR_ERR(pbundle->uattrs);
355 	if (copy_from_user(pbundle->uattrs, pbundle->user_attrs, uattrs_size))
356 		return -EFAULT;
357 
358 	for (i = 0; i != num_attrs; i++) {
359 		ret = uverbs_set_attr(pbundle, &pbundle->uattrs[i]);
360 		if (unlikely(ret))
361 			return ret;
362 	}
363 
364 	/* User space did not provide all the mandatory attributes */
365 	if (unlikely(!bitmap_subset(bundle->method_elm->attr_mandatory,
366 				    pbundle->bundle.attr_present,
367 				    bundle->method_elm->key_bitmap_len)))
368 		return -EINVAL;
369 
370 	if (bundle->method_elm->has_udata)
371 		uverbs_fill_udata(bundle, &pbundle->bundle.driver_udata,
372 				  UVERBS_ATTR_UHW_IN, UVERBS_ATTR_UHW_OUT);
373 	else
374 		pbundle->bundle.driver_udata = (struct ib_udata){};
375 
376 	if (destroy_bkey != UVERBS_API_ATTR_BKEY_LEN) {
377 		struct uverbs_obj_attr *destroy_attr = &bundle->attrs[destroy_bkey].obj_attr;
378 
379 		ret = uobj_destroy(destroy_attr->uobject, bundle);
380 		if (ret)
381 			return ret;
382 		__clear_bit(destroy_bkey, pbundle->uobj_finalize);
383 
384 		ret = handler(bundle);
385 		uobj_put_destroy(destroy_attr->uobject);
386 	} else {
387 		ret = handler(bundle);
388 	}
389 
390 	/*
391 	 * Until the drivers are revised to use the bundle directly we have to
392 	 * assume that the driver wrote to its UHW_OUT and flag userspace
393 	 * appropriately.
394 	 */
395 	if (!ret && bundle->method_elm->has_udata) {
396 		const struct uverbs_attr *attr =
397 			uverbs_attr_get(bundle, UVERBS_ATTR_UHW_OUT);
398 
399 		if (!IS_ERR(attr))
400 			ret = uverbs_set_output(bundle, attr);
401 	}
402 
403 	/*
404 	 * EPROTONOSUPPORT is ONLY to be returned if the ioctl framework can
405 	 * not invoke the method because the request is not supported.  No
406 	 * other cases should return this code.
407 	 */
408 	if (WARN_ON_ONCE(ret == -EPROTONOSUPPORT))
409 		return -EINVAL;
410 
411 	return ret;
412 }
413 
414 static void bundle_destroy(struct bundle_priv *pbundle, bool commit)
415 {
416 	unsigned int key_bitmap_len = pbundle->bundle.method_elm->key_bitmap_len;
417 	struct uverbs_attr_bundle *bundle =
418 		container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
419 	struct bundle_alloc_head *memblock;
420 	unsigned int i;
421 
422 	/* fast path for simple uobjects */
423 	i = -1;
424 	while ((i = find_next_bit(pbundle->uobj_finalize, key_bitmap_len,
425 				  i + 1)) < key_bitmap_len) {
426 		struct uverbs_attr *attr = &bundle->attrs[i];
427 
428 		uverbs_finalize_object(
429 			attr->obj_attr.uobject,
430 			attr->obj_attr.attr_elm->spec.u.obj.access,
431 			test_bit(i, pbundle->uobj_hw_obj_valid),
432 			commit, bundle);
433 	}
434 
435 	i = -1;
436 	while ((i = find_next_bit(pbundle->spec_finalize, key_bitmap_len,
437 				  i + 1)) < key_bitmap_len) {
438 		struct uverbs_attr *attr = &bundle->attrs[i];
439 		const struct uverbs_api_attr *attr_uapi;
440 		void __rcu **slot;
441 
442 		slot = uapi_get_attr_for_method(
443 			pbundle,
444 			pbundle->method_key | uapi_bkey_to_key_attr(i));
445 		if (WARN_ON(!slot))
446 			continue;
447 
448 		attr_uapi = rcu_dereference_protected(*slot, true);
449 
450 		if (attr_uapi->spec.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) {
451 			uverbs_free_idrs_array(attr_uapi, &attr->objs_arr_attr,
452 					       commit, bundle);
453 		}
454 	}
455 
456 	for (memblock = pbundle->allocated_mem; memblock;) {
457 		struct bundle_alloc_head *tmp = memblock;
458 
459 		memblock = memblock->next;
460 		kvfree(tmp);
461 	}
462 }
463 
464 static int ib_uverbs_cmd_verbs(struct ib_uverbs_file *ufile,
465 			       struct ib_uverbs_ioctl_hdr *hdr,
466 			       struct ib_uverbs_attr __user *user_attrs)
467 {
468 	const struct uverbs_api_ioctl_method *method_elm;
469 	struct uverbs_api *uapi = ufile->device->uapi;
470 	struct radix_tree_iter attrs_iter;
471 	struct bundle_priv *pbundle;
472 	struct bundle_priv onstack;
473 	void __rcu **slot;
474 	int ret;
475 
476 	if (unlikely(hdr->driver_id != uapi->driver_id))
477 		return -EINVAL;
478 
479 	slot = radix_tree_iter_lookup(
480 		&uapi->radix, &attrs_iter,
481 		uapi_key_obj(hdr->object_id) |
482 			uapi_key_ioctl_method(hdr->method_id));
483 	if (unlikely(!slot))
484 		return -EPROTONOSUPPORT;
485 	method_elm = rcu_dereference_protected(*slot, true);
486 
487 	if (!method_elm->use_stack) {
488 		pbundle = kmalloc(method_elm->bundle_size, GFP_KERNEL);
489 		if (!pbundle)
490 			return -ENOMEM;
491 		pbundle->internal_avail =
492 			method_elm->bundle_size -
493 			offsetof(struct bundle_priv, internal_buffer);
494 		pbundle->alloc_head.next = NULL;
495 		pbundle->allocated_mem = container_of(&pbundle->alloc_head,
496 						struct bundle_alloc_head, hdr);
497 	} else {
498 		pbundle = &onstack;
499 		pbundle->internal_avail = sizeof(pbundle->internal_buffer);
500 		pbundle->allocated_mem = NULL;
501 	}
502 
503 	/* Space for the pbundle->bundle.attrs flex array */
504 	pbundle->bundle.method_elm = method_elm;
505 	pbundle->method_key = attrs_iter.index;
506 	pbundle->bundle.ufile = ufile;
507 	pbundle->bundle.context = NULL; /* only valid if bundle has uobject */
508 	pbundle->radix = &uapi->radix;
509 	pbundle->radix_slots = slot;
510 	pbundle->radix_slots_len = radix_tree_chunk_size(&attrs_iter);
511 	pbundle->user_attrs = user_attrs;
512 
513 	pbundle->internal_used = ALIGN(
514 		pbundle->bundle.method_elm->key_bitmap_len *
515 			sizeof(*container_of(&pbundle->bundle,
516 					     struct uverbs_attr_bundle, hdr)
517 					->attrs),
518 		sizeof(*pbundle->internal_buffer));
519 	memset(pbundle->bundle.attr_present, 0,
520 	       sizeof(pbundle->bundle.attr_present));
521 	memset(pbundle->uobj_finalize, 0, sizeof(pbundle->uobj_finalize));
522 	memset(pbundle->spec_finalize, 0, sizeof(pbundle->spec_finalize));
523 	memset(pbundle->uobj_hw_obj_valid, 0,
524 	       sizeof(pbundle->uobj_hw_obj_valid));
525 
526 	ret = ib_uverbs_run_method(pbundle, hdr->num_attrs);
527 	bundle_destroy(pbundle, ret == 0);
528 	return ret;
529 }
530 
531 long ib_uverbs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
532 {
533 	struct ib_uverbs_file *file = filp->private_data;
534 	struct ib_uverbs_ioctl_hdr __user *user_hdr =
535 		(struct ib_uverbs_ioctl_hdr __user *)arg;
536 	struct ib_uverbs_ioctl_hdr hdr;
537 	int srcu_key;
538 	int err;
539 
540 	if (unlikely(cmd != RDMA_VERBS_IOCTL))
541 		return -ENOIOCTLCMD;
542 
543 	err = copy_from_user(&hdr, user_hdr, sizeof(hdr));
544 	if (err)
545 		return -EFAULT;
546 
547 	if (hdr.length > PAGE_SIZE ||
548 	    hdr.length != struct_size(&hdr, attrs, hdr.num_attrs))
549 		return -EINVAL;
550 
551 	if (hdr.reserved1 || hdr.reserved2)
552 		return -EPROTONOSUPPORT;
553 
554 	srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
555 	err = ib_uverbs_cmd_verbs(file, &hdr, user_hdr->attrs);
556 	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
557 	return err;
558 }
559 
560 /*
561  * Fill a ib_udata struct (core or uhw) using the given attribute IDs.
562  * This is primarily used to convert the UVERBS_ATTR_UHW() into the
563  * ib_udata format used by the drivers.
564  */
565 void uverbs_fill_udata(struct uverbs_attr_bundle *bundle,
566 		       struct ib_udata *udata, unsigned int attr_in,
567 		       unsigned int attr_out)
568 {
569 	struct bundle_priv *pbundle =
570 		container_of(&bundle->hdr, struct bundle_priv, bundle);
571 	struct uverbs_attr_bundle *bundle_aux =
572 		container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
573 	const struct uverbs_attr *in =
574 		uverbs_attr_get(bundle_aux, attr_in);
575 	const struct uverbs_attr *out =
576 		uverbs_attr_get(bundle_aux, attr_out);
577 
578 	if (!IS_ERR(in)) {
579 		udata->inlen = in->ptr_attr.len;
580 		if (uverbs_attr_ptr_is_inline(in))
581 			udata->inbuf =
582 				&pbundle->user_attrs[in->ptr_attr.uattr_idx]
583 					 .data;
584 		else
585 			udata->inbuf = u64_to_user_ptr(in->ptr_attr.data);
586 	} else {
587 		udata->inbuf = NULL;
588 		udata->inlen = 0;
589 	}
590 
591 	if (!IS_ERR(out)) {
592 		udata->outbuf = u64_to_user_ptr(out->ptr_attr.data);
593 		udata->outlen = out->ptr_attr.len;
594 	} else {
595 		udata->outbuf = NULL;
596 		udata->outlen = 0;
597 	}
598 }
599 
600 /*
601  * This is only used if the caller has directly used copy_to_use to write the
602  * data.  It signals to user space that the buffer is filled in.
603  */
604 int uverbs_output_written(const struct uverbs_attr_bundle *bundle, size_t idx)
605 {
606 	const struct uverbs_attr *attr = uverbs_attr_get(bundle, idx);
607 
608 	if (IS_ERR(attr))
609 		return PTR_ERR(attr);
610 
611 	return uverbs_set_output(bundle, attr);
612 }
613