Path: blob/main/sys/ofed/include/rdma/uverbs_ioctl.h
39482 views
/*1* Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*/3132#ifndef _UVERBS_IOCTL_33#define _UVERBS_IOCTL_3435#include <rdma/uverbs_types.h>36#include <linux/uaccess.h>37#include <rdma/rdma_user_ioctl.h>38#include <rdma/ib_user_ioctl_verbs.h>39#include <rdma/ib_user_ioctl_cmds.h>4041/*42* =======================================43* Verbs action specifications44* =======================================45*/4647enum uverbs_attr_type {48UVERBS_ATTR_TYPE_NA,49UVERBS_ATTR_TYPE_PTR_IN,50UVERBS_ATTR_TYPE_PTR_OUT,51UVERBS_ATTR_TYPE_IDR,52UVERBS_ATTR_TYPE_FD,53UVERBS_ATTR_TYPE_ENUM_IN,54UVERBS_ATTR_TYPE_IDRS_ARRAY,55};5657enum uverbs_obj_access {58UVERBS_ACCESS_READ,59UVERBS_ACCESS_WRITE,60UVERBS_ACCESS_NEW,61UVERBS_ACCESS_DESTROY62};6364/* Specification of a single attribute inside the ioctl message */65/* good size 16 */66struct uverbs_attr_spec {67u8 type;6869/*70* Support extending attributes by length. Allow the user to provide71* more bytes than ptr.len, but check that everything after is zero'd72* by the user.73*/74u8 zero_trailing:1;75/*76* Valid only for PTR_IN. Allocate and copy the data inside77* the parser78*/79u8 alloc_and_copy:1;80u8 mandatory:1;81/* True if this is from UVERBS_ATTR_UHW */82u8 is_udata:1;8384union {85struct {86/* Current known size to kernel */87u16 len;88/* User isn't allowed to provide something < min_len */89u16 min_len;90} ptr;9192struct {93/*94* higher bits mean the namespace and lower bits mean95* the type id within the namespace.96*/97u16 obj_type;98u8 access;99} obj;100101struct {102u8 num_elems;103} enum_def;104} u;105106/* This weird split lets us remove some padding */107union {108struct {109/*110* The enum attribute can select one of the attributes111* contained in the ids array. Currently only PTR_IN112* attributes are supported in the ids array.113*/114const struct uverbs_attr_spec *ids;115} enum_def;116117struct {118/*119* higher bits mean the namespace and lower bits mean120* the type id within the namespace.121*/122u16 obj_type;123u16 min_len;124u16 max_len;125u8 access;126} objs_arr;127} u2;128};129130/*131* Information about the API is loaded into a radix tree. For IOCTL we start132* with a tuple of:133* object_id, attr_id, method_id134*135* Which is a 48 bit value, with most of the bits guaranteed to be zero. Based136* on the current kernel support this is compressed into 16 bit key for the137* radix tree. Since this compression is entirely internal to the kernel the138* below limits can be revised if the kernel gains additional data.139*140* With 64 leafs per node this is a 3 level radix tree.141*142* The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns143* the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.144*145* This also encodes the tables for the write() and write() extended commands146* using the coding147* OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #148* OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #149* ie the WRITE path is treated as a special method type in the ioctl150* framework.151*/152enum uapi_radix_data {153UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,154155UVERBS_API_ATTR_KEY_BITS = 6,156UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),157UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,158UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,159160UVERBS_API_METHOD_KEY_BITS = 5,161UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,162UVERBS_API_METHOD_KEY_NUM_CORE = 22,163UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,164UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,165UVERBS_API_METHOD_KEY_NUM_DRIVER =166(UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -167UVERBS_API_METHOD_KEY_NUM_CORE,168UVERBS_API_METHOD_KEY_MASK = GENMASK(169UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,170UVERBS_API_METHOD_KEY_SHIFT),171172UVERBS_API_OBJ_KEY_BITS = 5,173UVERBS_API_OBJ_KEY_SHIFT =174UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,175UVERBS_API_OBJ_KEY_NUM_CORE = 20,176UVERBS_API_OBJ_KEY_NUM_DRIVER =177(1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,178UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),179180/* This id guaranteed to not exist in the radix tree */181UVERBS_API_KEY_ERR = 0xFFFFFFFF,182};183184static inline __attribute_const__ u32 uapi_key_obj(u32 id)185{186if (id & UVERBS_API_NS_FLAG) {187id &= ~UVERBS_API_NS_FLAG;188if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)189return UVERBS_API_KEY_ERR;190id = id + UVERBS_API_OBJ_KEY_NUM_CORE;191} else {192if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)193return UVERBS_API_KEY_ERR;194}195196return id << UVERBS_API_OBJ_KEY_SHIFT;197}198199static inline __attribute_const__ bool uapi_key_is_object(u32 key)200{201return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;202}203204static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)205{206if (id & UVERBS_API_NS_FLAG) {207id &= ~UVERBS_API_NS_FLAG;208if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)209return UVERBS_API_KEY_ERR;210id = id + UVERBS_API_METHOD_KEY_NUM_CORE;211} else {212id++;213if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)214return UVERBS_API_KEY_ERR;215}216217return id << UVERBS_API_METHOD_KEY_SHIFT;218}219220static inline __attribute_const__ u32 uapi_key_write_method(u32 id)221{222if (id >= UVERBS_API_WRITE_KEY_NUM)223return UVERBS_API_KEY_ERR;224return UVERBS_API_METHOD_IS_WRITE | id;225}226227static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)228{229if (id >= UVERBS_API_WRITE_KEY_NUM)230return UVERBS_API_KEY_ERR;231return UVERBS_API_METHOD_IS_WRITE_EX | id;232}233234static inline __attribute_const__ u32235uapi_key_attr_to_ioctl_method(u32 attr_key)236{237return attr_key &238(UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);239}240241static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)242{243unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;244245return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&246(key & UVERBS_API_ATTR_KEY_MASK) == 0;247}248249static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)250{251return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;252}253254static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)255{256return (key & UVERBS_API_METHOD_KEY_MASK) ==257UVERBS_API_METHOD_IS_WRITE_EX;258}259260static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)261{262/* 0 is the method slot itself */263return ioctl_method_key + 1;264}265266static inline __attribute_const__ u32 uapi_key_attr(u32 id)267{268/*269* The attr is designed to fit in the typical single radix tree node270* of 64 entries. Since allmost all methods have driver attributes we271* organize things so that the driver and core attributes interleave to272* reduce the length of the attributes array in typical cases.273*/274if (id & UVERBS_API_NS_FLAG) {275id &= ~UVERBS_API_NS_FLAG;276id++;277if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))278return UVERBS_API_KEY_ERR;279id = (id << 1) | 0;280} else {281if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))282return UVERBS_API_KEY_ERR;283id = (id << 1) | 1;284}285286return id;287}288289/* Only true for ioctl methods */290static inline __attribute_const__ bool uapi_key_is_attr(u32 key)291{292unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;293294return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&295(key & UVERBS_API_ATTR_KEY_MASK) != 0;296}297298/*299* This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),300* basically it undoes the reservation of 0 in the ID numbering. attr_key301* must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of302* uapi_key_attr().303*/304static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)305{306return attr_key - 1;307}308309static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)310{311return attr_bkey + 1;312}313314/*315* =======================================316* Verbs definitions317* =======================================318*/319320struct uverbs_attr_def {321u16 id;322struct uverbs_attr_spec attr;323};324325struct uverbs_method_def {326u16 id;327/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */328u32 flags;329size_t num_attrs;330const struct uverbs_attr_def * const (*attrs)[];331int (*handler)(struct uverbs_attr_bundle *attrs);332};333334struct uverbs_object_def {335u16 id;336const struct uverbs_obj_type *type_attrs;337size_t num_methods;338const struct uverbs_method_def * const (*methods)[];339};340341enum uapi_definition_kind {342UAPI_DEF_END = 0,343UAPI_DEF_OBJECT_START,344UAPI_DEF_WRITE,345UAPI_DEF_CHAIN_OBJ_TREE,346UAPI_DEF_CHAIN,347UAPI_DEF_IS_SUPPORTED_FUNC,348UAPI_DEF_IS_SUPPORTED_DEV_FN,349};350351enum uapi_definition_scope {352UAPI_SCOPE_OBJECT = 1,353UAPI_SCOPE_METHOD = 2,354};355356struct uapi_definition {357u8 kind;358u8 scope;359union {360struct {361u16 object_id;362} object_start;363struct {364u16 command_num;365u8 is_ex:1;366u8 has_udata:1;367u8 has_resp:1;368u8 req_size;369u8 resp_size;370} write;371};372373union {374bool (*func_is_supported)(struct ib_device *device);375int (*func_write)(struct uverbs_attr_bundle *attrs);376const struct uapi_definition *chain;377const struct uverbs_object_def *chain_obj_tree;378size_t needs_fn_offset;379};380};381382/* Define things connected to object_id */383#define DECLARE_UVERBS_OBJECT(_object_id, ...) \384{ \385.kind = UAPI_DEF_OBJECT_START, \386.object_start = { .object_id = _object_id }, \387}, \388##__VA_ARGS__389390/* Use in a var_args of DECLARE_UVERBS_OBJECT */391#define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \392{ \393.kind = UAPI_DEF_WRITE, \394.scope = UAPI_SCOPE_OBJECT, \395.write = { .is_ex = 0, .command_num = _command_num }, \396.func_write = _func, \397_cmd_desc, \398}, \399##__VA_ARGS__400401/* Use in a var_args of DECLARE_UVERBS_OBJECT */402#define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \403{ \404.kind = UAPI_DEF_WRITE, \405.scope = UAPI_SCOPE_OBJECT, \406.write = { .is_ex = 1, .command_num = _command_num }, \407.func_write = _func, \408_cmd_desc, \409}, \410##__VA_ARGS__411412/*413* Object is only supported if the function pointer named ibdev_fn in struct414* ib_device is not NULL.415*/416#define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \417{ \418.kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \419.scope = UAPI_SCOPE_OBJECT, \420.needs_fn_offset = \421offsetof(struct ib_device, ibdev_fn) + \422BUILD_BUG_ON_ZERO( \423sizeof(((struct ib_device *)0)->ibdev_fn) != \424sizeof(void *)), \425}426427/*428* Method is only supported if the function pointer named ibdev_fn in struct429* ib_device is not NULL.430*/431#define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \432{ \433.kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \434.scope = UAPI_SCOPE_METHOD, \435.needs_fn_offset = \436offsetof(struct ib_device, ibdev_fn) + \437BUILD_BUG_ON_ZERO( \438sizeof(((struct ib_device *)0)->ibdev_fn) != \439sizeof(void *)), \440}441442/* Call a function to determine if the entire object is supported or not */443#define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \444{ \445.kind = UAPI_DEF_IS_SUPPORTED_FUNC, \446.scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \447}448449/* Include another struct uapi_definition in this one */450#define UAPI_DEF_CHAIN(_def_var) \451{ \452.kind = UAPI_DEF_CHAIN, .chain = _def_var, \453}454455/* Temporary until the tree base description is replaced */456#define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...) \457{ \458.kind = UAPI_DEF_CHAIN_OBJ_TREE, \459.object_start = { .object_id = _object_enum }, \460.chain_obj_tree = _object_ptr, \461}, \462##__VA_ARGS__463#define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \464UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum), \465##__VA_ARGS__)466467/*468* =======================================469* Attribute Specifications470* =======================================471*/472473#define UVERBS_ATTR_SIZE(_min_len, _len) \474.u.ptr.min_len = _min_len, .u.ptr.len = _len475476#define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)477478/*479* Specifies a uapi structure that cannot be extended. The user must always480* supply the whole structure and nothing more. The structure must be declared481* in a header under include/uapi/rdma.482*/483#define UVERBS_ATTR_TYPE(_type) \484.u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)485/*486* Specifies a uapi structure where the user must provide at least up to487* member 'last'. Anything after last and up until the end of the structure488* can be non-zero, anything longer than the end of the structure must be489* zero. The structure must be declared in a header under include/uapi/rdma.490*/491#define UVERBS_ATTR_STRUCT(_type, _last) \492.zero_trailing = 1, \493UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \494sizeof(_type))495/*496* Specifies at least min_len bytes must be passed in, but the amount can be497* larger, up to the protocol maximum size. No check for zeroing is done.498*/499#define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)500501/* Must be used in the '...' of any UVERBS_ATTR */502#define UA_ALLOC_AND_COPY .alloc_and_copy = 1503#define UA_MANDATORY .mandatory = 1504#define UA_OPTIONAL .mandatory = 0505506/*507* min_len must be bigger than 0 and _max_len must be smaller than 4095. Only508* READ\WRITE accesses are supported.509*/510#define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \511...) \512(&(const struct uverbs_attr_def){ \513.id = (_attr_id) + \514BUILD_BUG_ON_ZERO((_min_len) == 0 || \515(_max_len) > \516PAGE_SIZE / sizeof(void *) || \517(_min_len) > (_max_len) || \518(_access) == UVERBS_ACCESS_NEW || \519(_access) == UVERBS_ACCESS_DESTROY), \520.attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \521.u2.objs_arr.obj_type = _idr_type, \522.u2.objs_arr.access = _access, \523.u2.objs_arr.min_len = _min_len, \524.u2.objs_arr.max_len = _max_len, \525__VA_ARGS__ } })526527/*528* Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,529* the user must validate the type of the uobject instead.530*/531#define UVERBS_IDR_ANY_OBJECT 0xFFFF532533#define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \534(&(const struct uverbs_attr_def){ \535.id = _attr_id, \536.attr = { .type = UVERBS_ATTR_TYPE_IDR, \537.u.obj.obj_type = _idr_type, \538.u.obj.access = _access, \539__VA_ARGS__ } })540541#define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \542(&(const struct uverbs_attr_def){ \543.id = (_attr_id) + \544BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \545(_access) != UVERBS_ACCESS_READ), \546.attr = { .type = UVERBS_ATTR_TYPE_FD, \547.u.obj.obj_type = _fd_type, \548.u.obj.access = _access, \549__VA_ARGS__ } })550551#define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \552(&(const struct uverbs_attr_def){ \553.id = _attr_id, \554.attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \555_type, \556__VA_ARGS__ } })557558#define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \559(&(const struct uverbs_attr_def){ \560.id = _attr_id, \561.attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \562_type, \563__VA_ARGS__ } })564565/* _enum_arry should be a 'static const union uverbs_attr_spec[]' */566#define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \567(&(const struct uverbs_attr_def){ \568.id = _attr_id, \569.attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \570.u2.enum_def.ids = _enum_arr, \571.u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \572__VA_ARGS__ }, \573})574575/* An input value that is a member in the enum _enum_type. */576#define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \577UVERBS_ATTR_PTR_IN( \578_attr_id, \579UVERBS_ATTR_SIZE( \580sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \581sizeof(u64)), \582__VA_ARGS__)583584/*585* An input value that is a bitwise combination of values of _enum_type.586* This permits the flag value to be passed as either a u32 or u64, it must587* be retrieved via uverbs_get_flag().588*/589#define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \590UVERBS_ATTR_PTR_IN( \591_attr_id, \592UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \593!sizeof(_enum_type *)), \594sizeof(u64)), \595__VA_ARGS__)596597/*598* This spec is used in order to pass information to the hardware driver in a599* legacy way. Every verb that could get driver specific data should get this600* spec.601*/602#define UVERBS_ATTR_UHW() \603UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \604UVERBS_ATTR_MIN_SIZE(0), \605UA_OPTIONAL, \606.is_udata = 1), \607UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \608UVERBS_ATTR_MIN_SIZE(0), \609UA_OPTIONAL, \610.is_udata = 1)611612/* =================================================613* Parsing infrastructure614* =================================================615*/616617618struct uverbs_ptr_attr {619/*620* If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is621* used.622*/623union {624void *ptr;625u64 data;626};627u16 len;628u16 uattr_idx;629u8 enum_id;630};631632struct uverbs_obj_attr {633struct ib_uobject *uobject;634const struct uverbs_api_attr *attr_elm;635};636637struct uverbs_objs_arr_attr {638struct ib_uobject **uobjects;639u16 len;640};641642struct uverbs_attr {643union {644struct uverbs_ptr_attr ptr_attr;645struct uverbs_obj_attr obj_attr;646struct uverbs_objs_arr_attr objs_arr_attr;647};648};649650struct uverbs_attr_bundle {651struct ib_udata driver_udata;652struct ib_udata ucore;653struct ib_uverbs_file *ufile;654struct ib_ucontext *context;655DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);656struct uverbs_attr attrs[0];657};658659static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,660unsigned int idx)661{662return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),663attrs_bundle->attr_present);664}665666/**667* rdma_udata_to_drv_context - Helper macro to get the driver's context out of668* ib_udata which is embedded in uverbs_attr_bundle.669*670* If udata is not NULL this cannot fail. Otherwise a NULL udata will result671* in a NULL ucontext pointer, as a safety precaution. Callers should be using672* 'udata' to determine if the driver call is in user or kernel mode, not673* 'ucontext'.674*675*/676#define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \677(udata ? container_of(container_of(udata, struct uverbs_attr_bundle, \678driver_udata) \679->context, \680drv_dev_struct, member) : \681(drv_dev_struct *)NULL)682683#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)684685static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,686u16 idx)687{688if (!uverbs_attr_is_valid(attrs_bundle, idx))689return ERR_PTR(-ENOENT);690691return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];692}693694static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,695u16 idx)696{697const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);698699if (IS_ERR(attr))700return PTR_ERR(attr);701702return attr->ptr_attr.enum_id;703}704705static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,706u16 idx)707{708const struct uverbs_attr *attr;709710attr = uverbs_attr_get(attrs_bundle, idx);711if (IS_ERR(attr))712return ERR_CAST(attr);713714return attr->obj_attr.uobject->object;715}716717static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,718u16 idx)719{720const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);721722if (IS_ERR(attr))723return ERR_CAST(attr);724725return attr->obj_attr.uobject;726}727728static inline int729uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)730{731const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);732733if (IS_ERR(attr))734return PTR_ERR(attr);735736return attr->ptr_attr.len;737}738739/*740* uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr741* attribute.742* @attrs: The attribute bundle743* @idx: The ID of the attribute744* @elem_size: The size of the element in the array745*/746static inline int747uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,748size_t elem_size)749{750int size = uverbs_attr_get_len(attrs, idx);751752if (size < 0)753return size;754755if (size % elem_size)756return -EINVAL;757758return size / elem_size;759}760761/**762* uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for763* UVERBS_ATTR_TYPE_IDRS_ARRAY.764* @arr: Returned pointer to array of pointers for uobjects or NULL if765* the attribute isn't provided.766*767* Return: The array length or 0 if no attribute was provided.768*/769static inline int uverbs_attr_get_uobjs_arr(770const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,771struct ib_uobject ***arr)772{773const struct uverbs_attr *attr =774uverbs_attr_get(attrs_bundle, attr_idx);775776if (IS_ERR(attr)) {777*arr = NULL;778return 0;779}780781*arr = attr->objs_arr_attr.uobjects;782783return attr->objs_arr_attr.len;784}785786static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)787{788return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);789}790791static inline void *uverbs_attr_get_alloced_ptr(792const struct uverbs_attr_bundle *attrs_bundle, u16 idx)793{794struct uverbs_attr *attr = __DECONST(struct uverbs_attr *, uverbs_attr_get(attrs_bundle, idx));795796if (IS_ERR(attr))797return (void *)attr;798799return uverbs_attr_ptr_is_inline(attr) ?800(void *)&attr->ptr_attr.data : attr->ptr_attr.ptr;801}802803static inline int _uverbs_copy_from(void *to,804const struct uverbs_attr_bundle *attrs_bundle,805size_t idx,806size_t size)807{808const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);809810if (IS_ERR(attr))811return PTR_ERR(attr);812813/*814* Validation ensures attr->ptr_attr.len >= size. If the caller is815* using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call816* uverbs_copy_from_or_zero.817*/818if (unlikely(size < attr->ptr_attr.len))819return -EINVAL;820821if (uverbs_attr_ptr_is_inline(attr))822memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);823else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),824attr->ptr_attr.len))825return -EFAULT;826827return 0;828}829830static inline int _uverbs_copy_from_or_zero(void *to,831const struct uverbs_attr_bundle *attrs_bundle,832size_t idx,833size_t size)834{835const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);836size_t min_size;837838if (IS_ERR(attr))839return PTR_ERR(attr);840841min_size = min_t(size_t, size, attr->ptr_attr.len);842843if (uverbs_attr_ptr_is_inline(attr))844memcpy(to, &attr->ptr_attr.data, min_size);845else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),846min_size))847return -EFAULT;848849if (size > min_size)850memset((char *)to + min_size, 0, size - min_size);851852return 0;853}854855#define uverbs_copy_from(to, attrs_bundle, idx) \856_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))857858#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \859_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))860861static inline struct ib_ucontext *862ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)863{864return ib_uverbs_get_ucontext_file(attrs->ufile);865}866867#if 1 /* IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) */868int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,869size_t idx, u64 allowed_bits);870int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,871size_t idx, u64 allowed_bits);872int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,873const void *from, size_t size);874__malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,875gfp_t flags);876877static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,878size_t size)879{880return _uverbs_alloc(bundle, size, GFP_KERNEL);881}882883static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,884size_t size)885{886return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);887}888int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,889size_t idx, s64 lower_bound, u64 upper_bound,890s64 *def_val);891int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,892size_t idx, const void *from, size_t size);893#else894static inline int895uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,896size_t idx, u64 allowed_bits)897{898return -EINVAL;899}900static inline int901uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,902size_t idx, u64 allowed_bits)903{904return -EINVAL;905}906static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,907size_t idx, const void *from, size_t size)908{909return -EINVAL;910}911static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,912size_t size)913{914return ERR_PTR(-EINVAL);915}916static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,917size_t size)918{919return ERR_PTR(-EINVAL);920}921static inline int922_uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,923size_t idx, s64 lower_bound, u64 upper_bound,924s64 *def_val)925{926return -EINVAL;927}928static inline int929uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,930size_t idx, const void *from, size_t size)931{932return -EINVAL;933}934#endif935936#define uverbs_get_const(_to, _attrs_bundle, _idx) \937({ \938s64 _val; \939int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \940type_min(typeof(*_to)), \941type_max(typeof(*_to)), NULL); \942(*_to) = _val; \943_ret; \944})945946#define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \947({ \948s64 _val; \949s64 _def_val = _default; \950int _ret = \951_uverbs_get_const(&_val, _attrs_bundle, _idx, \952type_min(typeof(*_to)), \953type_max(typeof(*_to)), &_def_val); \954(*_to) = _val; \955_ret; \956})957#endif958959960