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 <linux/mutex.h>
10 #include <linux/sched/task.h>
11 #include <linux/pid_namespace.h>
17 * rdma_restrack_init() - initialize and allocate resource tracking
20 * Return: 0 on success
22 int rdma_restrack_init(struct ib_device *dev)
24 struct rdma_restrack_root *rt;
27 dev->res = kcalloc(RDMA_RESTRACK_MAX, sizeof(*rt), GFP_KERNEL);
33 for (i = 0; i < RDMA_RESTRACK_MAX; i++)
34 xa_init_flags(&rt[i].xa, XA_FLAGS_ALLOC);
39 static const char *type2str(enum rdma_restrack_type type)
41 static const char * const names[RDMA_RESTRACK_MAX] = {
42 [RDMA_RESTRACK_PD] = "PD",
43 [RDMA_RESTRACK_CQ] = "CQ",
44 [RDMA_RESTRACK_QP] = "QP",
45 [RDMA_RESTRACK_CM_ID] = "CM_ID",
46 [RDMA_RESTRACK_MR] = "MR",
47 [RDMA_RESTRACK_CTX] = "CTX",
54 * rdma_restrack_clean() - clean resource tracking
57 void rdma_restrack_clean(struct ib_device *dev)
59 struct rdma_restrack_root *rt = dev->res;
60 struct rdma_restrack_entry *e;
61 char buf[TASK_COMM_LEN];
66 for (i = 0 ; i < RDMA_RESTRACK_MAX; i++) {
67 struct xarray *xa = &dev->res[i].xa;
73 pr_err("restrack: %s", CUT_HERE);
74 dev_err(&dev->dev, "BUG: RESTRACK detected leak of resources\n");
76 xa_for_each(xa, index, e) {
77 if (rdma_is_kernel_res(e)) {
81 * There is no need to call get_task_struct here,
82 * because we can be here only if there are more
83 * get_task_struct() call than put_task_struct().
85 get_task_comm(buf, e->task);
89 pr_err("restrack: %s %s object allocated by %s is not freed\n",
90 rdma_is_kernel_res(e) ? "Kernel" :
92 type2str(e->type), owner);
99 pr_err("restrack: %s", CUT_HERE);
105 * rdma_restrack_count() - the current usage of specific object
107 * @type: actual type of object to operate
110 int rdma_restrack_count(struct ib_device *dev, enum rdma_restrack_type type,
111 struct pid_namespace *ns)
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 (ns == &init_pid_ns ||
121 (!rdma_is_kernel_res(e) &&
122 ns == task_active_pid_ns(e->task)))
128 EXPORT_SYMBOL(rdma_restrack_count);
130 static void set_kern_name(struct rdma_restrack_entry *res)
135 case RDMA_RESTRACK_QP:
136 pd = container_of(res, struct ib_qp, res)->pd;
138 WARN_ONCE(true, "XRC QPs are not supported\n");
139 /* Survive, despite the programmer's error */
140 res->kern_name = " ";
143 case RDMA_RESTRACK_MR:
144 pd = container_of(res, struct ib_mr, res)->pd;
147 /* Other types set kern_name directly */
153 res->kern_name = pd->res.kern_name;
156 static struct ib_device *res_to_dev(struct rdma_restrack_entry *res)
159 case RDMA_RESTRACK_PD:
160 return container_of(res, struct ib_pd, res)->device;
161 case RDMA_RESTRACK_CQ:
162 return container_of(res, struct ib_cq, res)->device;
163 case RDMA_RESTRACK_QP:
164 return container_of(res, struct ib_qp, res)->device;
165 case RDMA_RESTRACK_CM_ID:
166 return container_of(res, struct rdma_id_private,
168 case RDMA_RESTRACK_MR:
169 return container_of(res, struct ib_mr, res)->device;
170 case RDMA_RESTRACK_CTX:
171 return container_of(res, struct ib_ucontext, res)->device;
173 WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
178 void rdma_restrack_set_task(struct rdma_restrack_entry *res,
182 res->kern_name = caller;
187 put_task_struct(res->task);
188 get_task_struct(current);
191 EXPORT_SYMBOL(rdma_restrack_set_task);
193 static void rdma_restrack_add(struct rdma_restrack_entry *res)
195 struct ib_device *dev = res_to_dev(res);
196 struct rdma_restrack_root *rt;
202 rt = &dev->res[res->type];
204 kref_init(&res->kref);
205 init_completion(&res->comp);
206 if (res->type != RDMA_RESTRACK_QP)
207 ret = xa_alloc_cyclic(&rt->xa, &res->id, res, xa_limit_32b,
208 &rt->next_id, GFP_KERNEL);
210 /* Special case to ensure that LQPN points to right QP */
211 struct ib_qp *qp = container_of(res, struct ib_qp, res);
213 ret = xa_insert(&rt->xa, qp->qp_num, res, GFP_KERNEL);
214 res->id = ret ? 0 : qp->qp_num;
222 * rdma_restrack_kadd() - add kernel object to the reource tracking database
223 * @res: resource entry
225 void rdma_restrack_kadd(struct rdma_restrack_entry *res)
230 rdma_restrack_add(res);
232 EXPORT_SYMBOL(rdma_restrack_kadd);
235 * rdma_restrack_uadd() - add user object to the reource tracking database
236 * @res: resource entry
238 void rdma_restrack_uadd(struct rdma_restrack_entry *res)
240 if (res->type != RDMA_RESTRACK_CM_ID)
244 rdma_restrack_set_task(res, NULL);
245 res->kern_name = NULL;
248 rdma_restrack_add(res);
250 EXPORT_SYMBOL(rdma_restrack_uadd);
252 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res)
254 return kref_get_unless_zero(&res->kref);
256 EXPORT_SYMBOL(rdma_restrack_get);
259 * rdma_restrack_get_byid() - translate from ID to restrack object
261 * @type: resource track type
262 * @id: ID to take a look
264 * Return: Pointer to restrack entry or -ENOENT in case of error.
266 struct rdma_restrack_entry *
267 rdma_restrack_get_byid(struct ib_device *dev,
268 enum rdma_restrack_type type, u32 id)
270 struct rdma_restrack_root *rt = &dev->res[type];
271 struct rdma_restrack_entry *res;
274 res = xa_load(&rt->xa, id);
275 if (!res || !rdma_restrack_get(res))
276 res = ERR_PTR(-ENOENT);
281 EXPORT_SYMBOL(rdma_restrack_get_byid);
283 static void restrack_release(struct kref *kref)
285 struct rdma_restrack_entry *res;
287 res = container_of(kref, struct rdma_restrack_entry, kref);
288 complete(&res->comp);
291 int rdma_restrack_put(struct rdma_restrack_entry *res)
293 return kref_put(&res->kref, restrack_release);
295 EXPORT_SYMBOL(rdma_restrack_put);
297 void rdma_restrack_del(struct rdma_restrack_entry *res)
299 struct rdma_restrack_entry *old;
300 struct rdma_restrack_root *rt;
301 struct ib_device *dev;
306 dev = res_to_dev(res);
310 rt = &dev->res[res->type];
312 old = xa_erase(&rt->xa, res->id);
316 rdma_restrack_put(res);
317 wait_for_completion(&res->comp);
321 put_task_struct(res->task);
325 EXPORT_SYMBOL(rdma_restrack_del);