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",
50 [RDMA_RESTRACK_SRQ] = "SRQ",
57 * rdma_restrack_clean() - clean resource tracking
60 void rdma_restrack_clean(struct ib_device *dev)
62 struct rdma_restrack_root *rt = dev->res;
63 struct rdma_restrack_entry *e;
64 char buf[TASK_COMM_LEN];
69 for (i = 0 ; i < RDMA_RESTRACK_MAX; i++) {
70 struct xarray *xa = &dev->res[i].xa;
76 pr_err("restrack: %s", CUT_HERE);
77 dev_err(&dev->dev, "BUG: RESTRACK detected leak of resources\n");
79 xa_for_each(xa, index, e) {
80 if (rdma_is_kernel_res(e)) {
84 * There is no need to call get_task_struct here,
85 * because we can be here only if there are more
86 * get_task_struct() call than put_task_struct().
88 get_task_comm(buf, e->task);
92 pr_err("restrack: %s %s object allocated by %s is not freed\n",
93 rdma_is_kernel_res(e) ? "Kernel" :
95 type2str(e->type), owner);
102 pr_err("restrack: %s", CUT_HERE);
108 * rdma_restrack_count() - the current usage of specific object
110 * @type: actual type of object to operate
112 int rdma_restrack_count(struct ib_device *dev, enum rdma_restrack_type type)
114 struct rdma_restrack_root *rt = &dev->res[type];
115 struct rdma_restrack_entry *e;
116 XA_STATE(xas, &rt->xa, 0);
120 xas_for_each(&xas, e, U32_MAX)
125 EXPORT_SYMBOL(rdma_restrack_count);
127 static struct ib_device *res_to_dev(struct rdma_restrack_entry *res)
130 case RDMA_RESTRACK_PD:
131 return container_of(res, struct ib_pd, res)->device;
132 case RDMA_RESTRACK_CQ:
133 return container_of(res, struct ib_cq, res)->device;
134 case RDMA_RESTRACK_QP:
135 return container_of(res, struct ib_qp, res)->device;
136 case RDMA_RESTRACK_CM_ID:
137 return container_of(res, struct rdma_id_private,
139 case RDMA_RESTRACK_MR:
140 return container_of(res, struct ib_mr, res)->device;
141 case RDMA_RESTRACK_CTX:
142 return container_of(res, struct ib_ucontext, res)->device;
143 case RDMA_RESTRACK_COUNTER:
144 return container_of(res, struct rdma_counter, res)->device;
145 case RDMA_RESTRACK_SRQ:
146 return container_of(res, struct ib_srq, res)->device;
148 WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
154 * rdma_restrack_attach_task() - attach the task onto this resource,
155 * valid for user space restrack entries.
156 * @res: resource entry
157 * @task: the task to attach
159 static void rdma_restrack_attach_task(struct rdma_restrack_entry *res,
160 struct task_struct *task)
162 if (WARN_ON_ONCE(!task))
166 put_task_struct(res->task);
167 get_task_struct(task);
173 * rdma_restrack_set_name() - set the task for this resource
174 * @res: resource entry
175 * @caller: kernel name, the current task will be used if the caller is NULL.
177 void rdma_restrack_set_name(struct rdma_restrack_entry *res, const char *caller)
180 res->kern_name = caller;
184 rdma_restrack_attach_task(res, current);
186 EXPORT_SYMBOL(rdma_restrack_set_name);
189 * rdma_restrack_parent_name() - set the restrack name properties based
191 * @dst: destination resource entry
192 * @parent: parent resource entry
194 void rdma_restrack_parent_name(struct rdma_restrack_entry *dst,
195 const struct rdma_restrack_entry *parent)
197 if (rdma_is_kernel_res(parent))
198 dst->kern_name = parent->kern_name;
200 rdma_restrack_attach_task(dst, parent->task);
202 EXPORT_SYMBOL(rdma_restrack_parent_name);
205 * rdma_restrack_new() - Initializes new restrack entry to allow _put() interface
206 * to release memory in fully automatic way.
207 * @res: Entry to initialize
208 * @type: REstrack type
210 void rdma_restrack_new(struct rdma_restrack_entry *res,
211 enum rdma_restrack_type type)
213 kref_init(&res->kref);
214 init_completion(&res->comp);
217 EXPORT_SYMBOL(rdma_restrack_new);
220 * rdma_restrack_add() - add object to the reource tracking database
221 * @res: resource entry
223 void rdma_restrack_add(struct rdma_restrack_entry *res)
225 struct ib_device *dev = res_to_dev(res);
226 struct rdma_restrack_root *rt;
235 rt = &dev->res[res->type];
237 if (res->type == RDMA_RESTRACK_QP) {
238 /* Special case to ensure that LQPN points to right QP */
239 struct ib_qp *qp = container_of(res, struct ib_qp, res);
241 WARN_ONCE(qp->qp_num >> 24 || qp->port >> 8,
242 "QP number 0x%0X and port 0x%0X", qp->qp_num,
244 res->id = qp->qp_num;
245 if (qp->qp_type == IB_QPT_SMI || qp->qp_type == IB_QPT_GSI)
246 res->id |= qp->port << 24;
247 ret = xa_insert(&rt->xa, res->id, res, GFP_KERNEL);
250 } else if (res->type == RDMA_RESTRACK_COUNTER) {
251 /* Special case to ensure that cntn points to right counter */
252 struct rdma_counter *counter;
254 counter = container_of(res, struct rdma_counter, res);
255 ret = xa_insert(&rt->xa, counter->id, res, GFP_KERNEL);
256 res->id = ret ? 0 : counter->id;
258 ret = xa_alloc_cyclic(&rt->xa, &res->id, res, xa_limit_32b,
259 &rt->next_id, GFP_KERNEL);
260 ret = (ret < 0) ? ret : 0;
267 EXPORT_SYMBOL(rdma_restrack_add);
269 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res)
271 return kref_get_unless_zero(&res->kref);
273 EXPORT_SYMBOL(rdma_restrack_get);
276 * rdma_restrack_get_byid() - translate from ID to restrack object
278 * @type: resource track type
279 * @id: ID to take a look
281 * Return: Pointer to restrack entry or -ENOENT in case of error.
283 struct rdma_restrack_entry *
284 rdma_restrack_get_byid(struct ib_device *dev,
285 enum rdma_restrack_type type, u32 id)
287 struct rdma_restrack_root *rt = &dev->res[type];
288 struct rdma_restrack_entry *res;
291 res = xa_load(&rt->xa, id);
292 if (!res || !rdma_restrack_get(res))
293 res = ERR_PTR(-ENOENT);
298 EXPORT_SYMBOL(rdma_restrack_get_byid);
300 static void restrack_release(struct kref *kref)
302 struct rdma_restrack_entry *res;
304 res = container_of(kref, struct rdma_restrack_entry, kref);
306 put_task_struct(res->task);
309 complete(&res->comp);
312 int rdma_restrack_put(struct rdma_restrack_entry *res)
314 return kref_put(&res->kref, restrack_release);
316 EXPORT_SYMBOL(rdma_restrack_put);
319 * rdma_restrack_del() - delete object from the reource tracking database
320 * @res: resource entry
322 void rdma_restrack_del(struct rdma_restrack_entry *res)
324 struct rdma_restrack_entry *old;
325 struct rdma_restrack_root *rt;
326 struct ib_device *dev;
330 put_task_struct(res->task);
339 dev = res_to_dev(res);
343 rt = &dev->res[res->type];
345 old = xa_erase(&rt->xa, res->id);
346 if (res->type == RDMA_RESTRACK_MR || res->type == RDMA_RESTRACK_QP)
352 rdma_restrack_put(res);
353 wait_for_completion(&res->comp);
355 EXPORT_SYMBOL(rdma_restrack_del);