1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
4  */
5 
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>
13 
14 #include "cma_priv.h"
15 #include "restrack.h"
16 
17 /**
18  * rdma_restrack_init() - initialize and allocate resource tracking
19  * @dev:  IB device
20  *
21  * Return: 0 on success
22  */
23 int rdma_restrack_init(struct ib_device *dev)
24 {
25 	struct rdma_restrack_root *rt;
26 	int i;
27 
28 	dev->res = kcalloc(RDMA_RESTRACK_MAX, sizeof(*rt), GFP_KERNEL);
29 	if (!dev->res)
30 		return -ENOMEM;
31 
32 	rt = dev->res;
33 
34 	for (i = 0; i < RDMA_RESTRACK_MAX; i++)
35 		xa_init_flags(&rt[i].xa, XA_FLAGS_ALLOC);
36 
37 	return 0;
38 }
39 
40 static const char *type2str(enum rdma_restrack_type type)
41 {
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 	};
51 
52 	return names[type];
53 };
54 
55 /**
56  * rdma_restrack_clean() - clean resource tracking
57  * @dev:  IB device
58  */
59 void rdma_restrack_clean(struct ib_device *dev)
60 {
61 	struct rdma_restrack_root *rt = dev->res;
62 	struct rdma_restrack_entry *e;
63 	char buf[TASK_COMM_LEN];
64 	bool found = false;
65 	const char *owner;
66 	int i;
67 
68 	for (i = 0 ; i < RDMA_RESTRACK_MAX; i++) {
69 		struct xarray *xa = &dev->res[i].xa;
70 
71 		if (!xa_empty(xa)) {
72 			unsigned long index;
73 
74 			if (!found) {
75 				pr_err("restrack: %s", CUT_HERE);
76 				dev_err(&dev->dev, "BUG: RESTRACK detected leak of resources\n");
77 			}
78 			xa_for_each(xa, index, e) {
79 				if (rdma_is_kernel_res(e)) {
80 					owner = e->kern_name;
81 				} else {
82 					/*
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().
86 					 */
87 					get_task_comm(buf, e->task);
88 					owner = buf;
89 				}
90 
91 				pr_err("restrack: %s %s object allocated by %s is not freed\n",
92 				       rdma_is_kernel_res(e) ? "Kernel" :
93 							       "User",
94 				       type2str(e->type), owner);
95 			}
96 			found = true;
97 		}
98 		xa_destroy(xa);
99 	}
100 	if (found)
101 		pr_err("restrack: %s", CUT_HERE);
102 
103 	kfree(rt);
104 }
105 
106 /**
107  * rdma_restrack_count() - the current usage of specific object
108  * @dev:  IB device
109  * @type: actual type of object to operate
110  */
111 int rdma_restrack_count(struct ib_device *dev, enum rdma_restrack_type type)
112 {
113 	struct rdma_restrack_root *rt = &dev->res[type];
114 	struct rdma_restrack_entry *e;
115 	XA_STATE(xas, &rt->xa, 0);
116 	u32 cnt = 0;
117 
118 	xa_lock(&rt->xa);
119 	xas_for_each(&xas, e, U32_MAX)
120 		cnt++;
121 	xa_unlock(&rt->xa);
122 	return cnt;
123 }
124 EXPORT_SYMBOL(rdma_restrack_count);
125 
126 static struct ib_device *res_to_dev(struct rdma_restrack_entry *res)
127 {
128 	switch (res->type) {
129 	case RDMA_RESTRACK_PD:
130 		return container_of(res, struct ib_pd, res)->device;
131 	case RDMA_RESTRACK_CQ:
132 		return container_of(res, struct ib_cq, res)->device;
133 	case RDMA_RESTRACK_QP:
134 		return container_of(res, struct ib_qp, res)->device;
135 	case RDMA_RESTRACK_CM_ID:
136 		return container_of(res, struct rdma_id_private,
137 				    res)->id.device;
138 	case RDMA_RESTRACK_MR:
139 		return container_of(res, struct ib_mr, res)->device;
140 	case RDMA_RESTRACK_CTX:
141 		return container_of(res, struct ib_ucontext, res)->device;
142 	case RDMA_RESTRACK_COUNTER:
143 		return container_of(res, struct rdma_counter, res)->device;
144 	default:
145 		WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
146 		return NULL;
147 	}
148 }
149 
150 /**
151  * rdma_restrack_attach_task() - attach the task onto this resource,
152  * valid for user space restrack entries.
153  * @res:  resource entry
154  * @task: the task to attach
155  */
156 static void rdma_restrack_attach_task(struct rdma_restrack_entry *res,
157 				      struct task_struct *task)
158 {
159 	if (WARN_ON_ONCE(!task))
160 		return;
161 
162 	if (res->task)
163 		put_task_struct(res->task);
164 	get_task_struct(task);
165 	res->task = task;
166 	res->user = true;
167 }
168 
169 /**
170  * rdma_restrack_set_name() - set the task for this resource
171  * @res:  resource entry
172  * @caller: kernel name, the current task will be used if the caller is NULL.
173  */
174 void rdma_restrack_set_name(struct rdma_restrack_entry *res, const char *caller)
175 {
176 	if (caller) {
177 		res->kern_name = caller;
178 		return;
179 	}
180 
181 	rdma_restrack_attach_task(res, current);
182 }
183 EXPORT_SYMBOL(rdma_restrack_set_name);
184 
185 /**
186  * rdma_restrack_parent_name() - set the restrack name properties based
187  * on parent restrack
188  * @dst: destination resource entry
189  * @parent: parent resource entry
190  */
191 void rdma_restrack_parent_name(struct rdma_restrack_entry *dst,
192 			       const struct rdma_restrack_entry *parent)
193 {
194 	if (rdma_is_kernel_res(parent))
195 		dst->kern_name = parent->kern_name;
196 	else
197 		rdma_restrack_attach_task(dst, parent->task);
198 }
199 EXPORT_SYMBOL(rdma_restrack_parent_name);
200 
201 /**
202  * rdma_restrack_new() - Initializes new restrack entry to allow _put() interface
203  * to release memory in fully automatic way.
204  * @res: Entry to initialize
205  * @type: REstrack type
206  */
207 void rdma_restrack_new(struct rdma_restrack_entry *res,
208 		       enum rdma_restrack_type type)
209 {
210 	kref_init(&res->kref);
211 	init_completion(&res->comp);
212 	res->type = type;
213 }
214 EXPORT_SYMBOL(rdma_restrack_new);
215 
216 /**
217  * rdma_restrack_add() - add object to the reource tracking database
218  * @res:  resource entry
219  */
220 void rdma_restrack_add(struct rdma_restrack_entry *res)
221 {
222 	struct ib_device *dev = res_to_dev(res);
223 	struct rdma_restrack_root *rt;
224 	int ret = 0;
225 
226 	if (!dev)
227 		return;
228 
229 	if (res->no_track)
230 		goto out;
231 
232 	rt = &dev->res[res->type];
233 
234 	if (res->type == RDMA_RESTRACK_QP) {
235 		/* Special case to ensure that LQPN points to right QP */
236 		struct ib_qp *qp = container_of(res, struct ib_qp, res);
237 
238 		WARN_ONCE(qp->qp_num >> 24 || qp->port >> 8,
239 			  "QP number 0x%0X and port 0x%0X", qp->qp_num,
240 			  qp->port);
241 		res->id = qp->qp_num;
242 		if (qp->qp_type == IB_QPT_SMI || qp->qp_type == IB_QPT_GSI)
243 			res->id |= qp->port << 24;
244 		ret = xa_insert(&rt->xa, res->id, res, GFP_KERNEL);
245 		if (ret)
246 			res->id = 0;
247 	} else if (res->type == RDMA_RESTRACK_COUNTER) {
248 		/* Special case to ensure that cntn points to right counter */
249 		struct rdma_counter *counter;
250 
251 		counter = container_of(res, struct rdma_counter, res);
252 		ret = xa_insert(&rt->xa, counter->id, res, GFP_KERNEL);
253 		res->id = ret ? 0 : counter->id;
254 	} else {
255 		ret = xa_alloc_cyclic(&rt->xa, &res->id, res, xa_limit_32b,
256 				      &rt->next_id, GFP_KERNEL);
257 		ret = (ret < 0) ? ret : 0;
258 	}
259 
260 out:
261 	if (!ret)
262 		res->valid = true;
263 }
264 EXPORT_SYMBOL(rdma_restrack_add);
265 
266 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res)
267 {
268 	return kref_get_unless_zero(&res->kref);
269 }
270 EXPORT_SYMBOL(rdma_restrack_get);
271 
272 /**
273  * rdma_restrack_get_byid() - translate from ID to restrack object
274  * @dev: IB device
275  * @type: resource track type
276  * @id: ID to take a look
277  *
278  * Return: Pointer to restrack entry or -ENOENT in case of error.
279  */
280 struct rdma_restrack_entry *
281 rdma_restrack_get_byid(struct ib_device *dev,
282 		       enum rdma_restrack_type type, u32 id)
283 {
284 	struct rdma_restrack_root *rt = &dev->res[type];
285 	struct rdma_restrack_entry *res;
286 
287 	xa_lock(&rt->xa);
288 	res = xa_load(&rt->xa, id);
289 	if (!res || !rdma_restrack_get(res))
290 		res = ERR_PTR(-ENOENT);
291 	xa_unlock(&rt->xa);
292 
293 	return res;
294 }
295 EXPORT_SYMBOL(rdma_restrack_get_byid);
296 
297 static void restrack_release(struct kref *kref)
298 {
299 	struct rdma_restrack_entry *res;
300 
301 	res = container_of(kref, struct rdma_restrack_entry, kref);
302 	if (res->task) {
303 		put_task_struct(res->task);
304 		res->task = NULL;
305 	}
306 	complete(&res->comp);
307 }
308 
309 int rdma_restrack_put(struct rdma_restrack_entry *res)
310 {
311 	return kref_put(&res->kref, restrack_release);
312 }
313 EXPORT_SYMBOL(rdma_restrack_put);
314 
315 /**
316  * rdma_restrack_del() - delete object from the reource tracking database
317  * @res:  resource entry
318  */
319 void rdma_restrack_del(struct rdma_restrack_entry *res)
320 {
321 	struct rdma_restrack_entry *old;
322 	struct rdma_restrack_root *rt;
323 	struct ib_device *dev;
324 
325 	if (!res->valid) {
326 		if (res->task) {
327 			put_task_struct(res->task);
328 			res->task = NULL;
329 		}
330 		return;
331 	}
332 
333 	if (res->no_track)
334 		goto out;
335 
336 	dev = res_to_dev(res);
337 	if (WARN_ON(!dev))
338 		return;
339 
340 	rt = &dev->res[res->type];
341 
342 	old = xa_erase(&rt->xa, res->id);
343 	if (res->type == RDMA_RESTRACK_MR || res->type == RDMA_RESTRACK_QP)
344 		return;
345 	WARN_ON(old != res);
346 
347 out:
348 	res->valid = false;
349 	rdma_restrack_put(res);
350 	wait_for_completion(&res->comp);
351 }
352 EXPORT_SYMBOL(rdma_restrack_del);
353