Re: [PATCH 01/10] RDMA: Add ib_copy_validate_udata_in()

Junxian Huang
2026-02-09T06:11:41Z

On 2026/2/6 9:45, Jason Gunthorpe wrote:
> Add a new function to consolidate the required compatibility pattern for
> driver data of checking against a minimum size, and checking for unknown
> trailing bytes to be zero into a function.
>
> This new function uses the faster copy_struct_from_user() instead of
> trying to directly check for zero.
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
> include/rdma/ib_verbs.h | 37 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 37 insertions(+)
>
> diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h
> index 8bd020da774531..32dc674ef78cf1 100644
> --- a/include/rdma/ib_verbs.h
> +++ b/include/rdma/ib_verbs.h
> @@ -3119,6 +3119,43 @@ static inline bool ib_is_udata_cleared(struct ib_udata *udata,
> return ib_is_buffer_cleared(udata->inbuf + offset, len);
> }
>
> +static inline int _ib_copy_validate_udata_in(struct ib_udata *udata, void *req,
> + size_t kernel_size,
> + size_t minimum_size)
> +{
> + int err;
> +
> + if (udata->inlen < minimum_size)
> + return -EINVAL;
> + err = copy_struct_from_user(req, kernel_size, udata->inbuf,
> + udata->inlen);
> + if (err) {
> + if (err == E2BIG)

The E2BIG should be negative.

> + return -EOPNOTSUPP;
> + return err;
> + }
> + return 0;

The logic here can be simplified like this:

err = copy_struct_from_user(req, kernel_size, udata->inbuf,
udata->inlen);
if (err == -E2BIG)
return -EOPNOTSUPP;
return err;

Junxian

> +}
> +
> +/**
> + * ib_copy_validate_udata_in - Copy and validate that the request structure is
> + * compatible with this kernel
> + * @_udata: The system calls ib_udata struct
> + * @_req: The name of an on-stack structure that holds the driver data
> + * @_end_member: The member in the struct that is the original end of struct
> + * from the first kernel to introduce it.
> + *
> + * Check that the udata input request struct is properly formed for this kernel.
> + * Then copy it into req
> + */
> +#define ib_copy_validate_udata_in(_udata, _req, _end_member) \
> + ({ \
> + static_assert(__same_type(*(typeof(&(_req)))0, (_req))); \
> + _ib_copy_validate_udata_in(_udata, &(_req), sizeof(_req), \
> + offsetofend(typeof(_req), \
> + _end_member)); \
> + })
> +
> /**
> * ib_modify_qp_is_ok - Check that the supplied attribute mask
> * contains all required attributes and no attributes not allowed for