xref: /linux-6.15/include/rdma/uverbs_types.h (revision 87ad80ab)
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_TYPES_
34 #define _UVERBS_TYPES_
35 
36 #include <linux/kernel.h>
37 #include <rdma/ib_verbs.h>
38 
39 struct uverbs_obj_type;
40 
41 /*
42  * The following sequences are valid:
43  * Success flow:
44  *   alloc_begin
45  *   alloc_commit
46  *    [..]
47  * Access flow:
48  *   lookup_get(exclusive=false) & uverbs_try_lock_object
49  *   lookup_put(exclusive=false) via rdma_lookup_put_uobject
50  * Destruction flow:
51  *   lookup_get(exclusive=true) & uverbs_try_lock_object
52  *   remove_commit
53  *   lookup_put(exclusive=true) via rdma_lookup_put_uobject
54  *
55  * Allocate Error flow #1
56  *   alloc_begin
57  *   alloc_abort
58  * Allocate Error flow #2
59  *   alloc_begin
60  *   remove_commit
61  *   alloc_abort
62  * Allocate Error flow #3
63  *   alloc_begin
64  *   alloc_commit (fails)
65  *   remove_commit
66  *   alloc_abort
67  *
68  * In all cases the caller must hold the ufile kref until alloc_commit or
69  * alloc_abort returns.
70  */
71 struct uverbs_obj_type_class {
72 	struct ib_uobject *(*alloc_begin)(const struct uverbs_obj_type *type,
73 					  struct ib_uverbs_file *ufile);
74 	/* This consumes the kref on uobj */
75 	int (*alloc_commit)(struct ib_uobject *uobj);
76 	/* This does not consume the kref on uobj */
77 	void (*alloc_abort)(struct ib_uobject *uobj);
78 
79 	struct ib_uobject *(*lookup_get)(const struct uverbs_obj_type *type,
80 					 struct ib_uverbs_file *ufile, s64 id,
81 					 bool exclusive);
82 	void (*lookup_put)(struct ib_uobject *uobj, bool exclusive);
83 	/* This does not consume the kref on uobj */
84 	int __must_check (*remove_commit)(struct ib_uobject *uobj,
85 					  enum rdma_remove_reason why);
86 	u8    needs_kfree_rcu;
87 };
88 
89 struct uverbs_obj_type {
90 	const struct uverbs_obj_type_class * const type_class;
91 	size_t	     obj_size;
92 };
93 
94 /*
95  * Objects type classes which support a detach state (object is still alive but
96  * it's not attached to any context need to make sure:
97  * (a) no call through to a driver after a detach is called
98  * (b) detach isn't called concurrently with context_cleanup
99  */
100 
101 struct uverbs_obj_idr_type {
102 	/*
103 	 * In idr based objects, uverbs_obj_type_class points to a generic
104 	 * idr operations. In order to specialize the underlying types (e.g. CQ,
105 	 * QPs, etc.), we add destroy_object specific callbacks.
106 	 */
107 	struct uverbs_obj_type  type;
108 
109 	/* Free driver resources from the uobject, make the driver uncallable,
110 	 * and move the uobject to the detached state. If the object was
111 	 * destroyed by the user's request, a failure should leave the uobject
112 	 * completely unchanged.
113 	 */
114 	int __must_check (*destroy_object)(struct ib_uobject *uobj,
115 					   enum rdma_remove_reason why);
116 };
117 
118 struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
119 					   struct ib_uverbs_file *ufile,
120 					   s64 id, bool exclusive);
121 void rdma_lookup_put_uobject(struct ib_uobject *uobj, bool exclusive);
122 struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
123 					    struct ib_uverbs_file *ufile);
124 void rdma_alloc_abort_uobject(struct ib_uobject *uobj);
125 int __must_check rdma_alloc_commit_uobject(struct ib_uobject *uobj);
126 int rdma_explicit_destroy(struct ib_uobject *uobject);
127 
128 struct uverbs_obj_fd_type {
129 	/*
130 	 * In fd based objects, uverbs_obj_type_ops points to generic
131 	 * fd operations. In order to specialize the underlying types (e.g.
132 	 * completion_channel), we use fops, name and flags for fd creation.
133 	 * context_closed is called when the context is closed either when
134 	 * the driver is removed or the process terminated.
135 	 */
136 	struct uverbs_obj_type  type;
137 	int (*context_closed)(struct ib_uobject *uobj,
138 			      enum rdma_remove_reason why);
139 	const struct file_operations	*fops;
140 	const char			*name;
141 	int				flags;
142 };
143 
144 extern const struct uverbs_obj_type_class uverbs_idr_class;
145 extern const struct uverbs_obj_type_class uverbs_fd_class;
146 
147 #define UVERBS_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) -	\
148 				   sizeof(char))
149 #define UVERBS_TYPE_ALLOC_FD(_obj_size, _context_closed, _fops, _name, _flags)\
150 	((&((const struct uverbs_obj_fd_type)				\
151 	 {.type = {							\
152 		.type_class = &uverbs_fd_class,				\
153 		.obj_size = (_obj_size) +				\
154 			UVERBS_BUILD_BUG_ON((_obj_size) <               \
155 					    sizeof(struct ib_uobject)), \
156 	 },								\
157 	 .context_closed = _context_closed,				\
158 	 .fops = _fops,							\
159 	 .name = _name,							\
160 	 .flags = _flags}))->type)
161 #define UVERBS_TYPE_ALLOC_IDR_SZ(_size, _destroy_object)	\
162 	((&((const struct uverbs_obj_idr_type)				\
163 	 {.type = {							\
164 		.type_class = &uverbs_idr_class,			\
165 		.obj_size = (_size) +					\
166 			UVERBS_BUILD_BUG_ON((_size) <			\
167 					    sizeof(struct ib_uobject))	\
168 	 },								\
169 	 .destroy_object = _destroy_object,}))->type)
170 #define UVERBS_TYPE_ALLOC_IDR(_destroy_object)			\
171 	 UVERBS_TYPE_ALLOC_IDR_SZ(sizeof(struct ib_uobject),	\
172 				  _destroy_object)
173 
174 #endif
175