1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
6 #include <rdma/rdma_cm.h>
7 #include <rdma/ib_verbs.h>
8 #include <rdma/restrack.h>
9 #include <rdma/rdma_counter.h>
10 #include <linux/mutex.h>
11 #include <linux/sched/task.h>
12 #include <linux/pid_namespace.h>
18 * rdma_restrack_init() - initialize and allocate resource tracking
21 * Return: 0 on success
23 int rdma_restrack_init(struct ib_device *dev)
25 struct rdma_restrack_root *rt;
28 dev->res = kcalloc(RDMA_RESTRACK_MAX, sizeof(*rt), GFP_KERNEL);
34 for (i = 0; i < RDMA_RESTRACK_MAX; i++)
35 xa_init_flags(&rt[i].xa, XA_FLAGS_ALLOC);
40 static const char *type2str(enum rdma_restrack_type type)
42 static const char * const names[RDMA_RESTRACK_MAX] = {
43 [RDMA_RESTRACK_PD] = "PD",
44 [RDMA_RESTRACK_CQ] = "CQ",
45 [RDMA_RESTRACK_QP] = "QP",
46 [RDMA_RESTRACK_CM_ID] = "CM_ID",
47 [RDMA_RESTRACK_MR] = "MR",
48 [RDMA_RESTRACK_CTX] = "CTX",
49 [RDMA_RESTRACK_COUNTER] = "COUNTER",
56 * rdma_restrack_clean() - clean resource tracking
59 void rdma_restrack_clean(struct ib_device *dev)
61 struct rdma_restrack_root *rt = dev->res;
62 struct rdma_restrack_entry *e;
63 char buf[TASK_COMM_LEN];
68 for (i = 0 ; i < RDMA_RESTRACK_MAX; i++) {
69 struct xarray *xa = &dev->res[i].xa;
75 pr_err("restrack: %s", CUT_HERE);
76 dev_err(&dev->dev, "BUG: RESTRACK detected leak of resources\n");
78 xa_for_each(xa, index, e) {
79 if (rdma_is_kernel_res(e)) {
83 * There is no need to call get_task_struct here,
84 * because we can be here only if there are more
85 * get_task_struct() call than put_task_struct().
87 get_task_comm(buf, e->task);
91 pr_err("restrack: %s %s object allocated by %s is not freed\n",
92 rdma_is_kernel_res(e) ? "Kernel" :
94 type2str(e->type), owner);
101 pr_err("restrack: %s", CUT_HERE);
107 * rdma_restrack_count() - the current usage of specific object
109 * @type: actual type of object to operate
111 int rdma_restrack_count(struct ib_device *dev, enum rdma_restrack_type type)
113 struct rdma_restrack_root *rt = &dev->res[type];
114 struct rdma_restrack_entry *e;
115 XA_STATE(xas, &rt->xa, 0);
119 xas_for_each(&xas, e, U32_MAX) {
120 if (!rdma_is_visible_in_pid_ns(e))
127 EXPORT_SYMBOL(rdma_restrack_count);
129 static void set_kern_name(struct rdma_restrack_entry *res)
134 case RDMA_RESTRACK_QP:
135 pd = container_of(res, struct ib_qp, res)->pd;
137 WARN_ONCE(true, "XRC QPs are not supported\n");
138 /* Survive, despite the programmer's error */
139 res->kern_name = " ";
142 case RDMA_RESTRACK_MR:
143 pd = container_of(res, struct ib_mr, res)->pd;
146 /* Other types set kern_name directly */
152 res->kern_name = pd->res.kern_name;
155 static struct ib_device *res_to_dev(struct rdma_restrack_entry *res)
158 case RDMA_RESTRACK_PD:
159 return container_of(res, struct ib_pd, res)->device;
160 case RDMA_RESTRACK_CQ:
161 return container_of(res, struct ib_cq, res)->device;
162 case RDMA_RESTRACK_QP:
163 return container_of(res, struct ib_qp, res)->device;
164 case RDMA_RESTRACK_CM_ID:
165 return container_of(res, struct rdma_id_private,
167 case RDMA_RESTRACK_MR:
168 return container_of(res, struct ib_mr, res)->device;
169 case RDMA_RESTRACK_CTX:
170 return container_of(res, struct ib_ucontext, res)->device;
171 case RDMA_RESTRACK_COUNTER:
172 return container_of(res, struct rdma_counter, res)->device;
174 WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
179 void rdma_restrack_set_task(struct rdma_restrack_entry *res,
183 res->kern_name = caller;
188 put_task_struct(res->task);
189 get_task_struct(current);
192 EXPORT_SYMBOL(rdma_restrack_set_task);
195 * rdma_restrack_attach_task() - attach the task onto this resource
196 * @res: resource entry
197 * @task: the task to attach, the current task will be used if it is NULL.
199 void rdma_restrack_attach_task(struct rdma_restrack_entry *res,
200 struct task_struct *task)
203 put_task_struct(res->task);
204 get_task_struct(task);
208 static void rdma_restrack_add(struct rdma_restrack_entry *res)
210 struct ib_device *dev = res_to_dev(res);
211 struct rdma_restrack_root *rt;
217 rt = &dev->res[res->type];
219 kref_init(&res->kref);
220 init_completion(&res->comp);
221 if (res->type == RDMA_RESTRACK_QP) {
222 /* Special case to ensure that LQPN points to right QP */
223 struct ib_qp *qp = container_of(res, struct ib_qp, res);
225 ret = xa_insert(&rt->xa, qp->qp_num, res, GFP_KERNEL);
226 res->id = ret ? 0 : qp->qp_num;
227 } else if (res->type == RDMA_RESTRACK_COUNTER) {
228 /* Special case to ensure that cntn points to right counter */
229 struct rdma_counter *counter;
231 counter = container_of(res, struct rdma_counter, res);
232 ret = xa_insert(&rt->xa, counter->id, res, GFP_KERNEL);
233 res->id = ret ? 0 : counter->id;
235 ret = xa_alloc_cyclic(&rt->xa, &res->id, res, xa_limit_32b,
236 &rt->next_id, GFP_KERNEL);
244 * rdma_restrack_kadd() - add kernel object to the reource tracking database
245 * @res: resource entry
247 void rdma_restrack_kadd(struct rdma_restrack_entry *res)
252 rdma_restrack_add(res);
254 EXPORT_SYMBOL(rdma_restrack_kadd);
257 * rdma_restrack_uadd() - add user object to the reource tracking database
258 * @res: resource entry
260 void rdma_restrack_uadd(struct rdma_restrack_entry *res)
262 if ((res->type != RDMA_RESTRACK_CM_ID) &&
263 (res->type != RDMA_RESTRACK_COUNTER))
267 rdma_restrack_set_task(res, NULL);
268 res->kern_name = NULL;
271 rdma_restrack_add(res);
273 EXPORT_SYMBOL(rdma_restrack_uadd);
275 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res)
277 return kref_get_unless_zero(&res->kref);
279 EXPORT_SYMBOL(rdma_restrack_get);
282 * rdma_restrack_get_byid() - translate from ID to restrack object
284 * @type: resource track type
285 * @id: ID to take a look
287 * Return: Pointer to restrack entry or -ENOENT in case of error.
289 struct rdma_restrack_entry *
290 rdma_restrack_get_byid(struct ib_device *dev,
291 enum rdma_restrack_type type, u32 id)
293 struct rdma_restrack_root *rt = &dev->res[type];
294 struct rdma_restrack_entry *res;
297 res = xa_load(&rt->xa, id);
298 if (!res || !rdma_restrack_get(res))
299 res = ERR_PTR(-ENOENT);
304 EXPORT_SYMBOL(rdma_restrack_get_byid);
306 static void restrack_release(struct kref *kref)
308 struct rdma_restrack_entry *res;
310 res = container_of(kref, struct rdma_restrack_entry, kref);
311 complete(&res->comp);
314 int rdma_restrack_put(struct rdma_restrack_entry *res)
316 return kref_put(&res->kref, restrack_release);
318 EXPORT_SYMBOL(rdma_restrack_put);
320 void rdma_restrack_del(struct rdma_restrack_entry *res)
322 struct rdma_restrack_entry *old;
323 struct rdma_restrack_root *rt;
324 struct ib_device *dev;
329 dev = res_to_dev(res);
333 rt = &dev->res[res->type];
335 old = xa_erase(&rt->xa, res->id);
339 rdma_restrack_put(res);
340 wait_for_completion(&res->comp);
344 put_task_struct(res->task);
348 EXPORT_SYMBOL(rdma_restrack_del);
350 bool rdma_is_visible_in_pid_ns(struct rdma_restrack_entry *res)
353 * 1. Kern resources should be visible in init
355 * 2. Present only resources visible in the current
358 if (rdma_is_kernel_res(res))
359 return task_active_pid_ns(current) == &init_pid_ns;
361 /* PID 0 means that resource is not found in current namespace */
362 return task_pid_vnr(res->task);