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  * @ns:   PID namespace
111  */
112 int rdma_restrack_count(struct ib_device *dev, enum rdma_restrack_type type,
113 			struct pid_namespace *ns)
114 {
115 	struct rdma_restrack_root *rt = &dev->res[type];
116 	struct rdma_restrack_entry *e;
117 	XA_STATE(xas, &rt->xa, 0);
118 	u32 cnt = 0;
119 
120 	xa_lock(&rt->xa);
121 	xas_for_each(&xas, e, U32_MAX) {
122 		if (ns == &init_pid_ns ||
123 		    (!rdma_is_kernel_res(e) &&
124 		     ns == task_active_pid_ns(e->task)))
125 			cnt++;
126 	}
127 	xa_unlock(&rt->xa);
128 	return cnt;
129 }
130 EXPORT_SYMBOL(rdma_restrack_count);
131 
132 static void set_kern_name(struct rdma_restrack_entry *res)
133 {
134 	struct ib_pd *pd;
135 
136 	switch (res->type) {
137 	case RDMA_RESTRACK_QP:
138 		pd = container_of(res, struct ib_qp, res)->pd;
139 		if (!pd) {
140 			WARN_ONCE(true, "XRC QPs are not supported\n");
141 			/* Survive, despite the programmer's error */
142 			res->kern_name = " ";
143 		}
144 		break;
145 	case RDMA_RESTRACK_MR:
146 		pd = container_of(res, struct ib_mr, res)->pd;
147 		break;
148 	default:
149 		/* Other types set kern_name directly */
150 		pd = NULL;
151 		break;
152 	}
153 
154 	if (pd)
155 		res->kern_name = pd->res.kern_name;
156 }
157 
158 static struct ib_device *res_to_dev(struct rdma_restrack_entry *res)
159 {
160 	switch (res->type) {
161 	case RDMA_RESTRACK_PD:
162 		return container_of(res, struct ib_pd, res)->device;
163 	case RDMA_RESTRACK_CQ:
164 		return container_of(res, struct ib_cq, res)->device;
165 	case RDMA_RESTRACK_QP:
166 		return container_of(res, struct ib_qp, res)->device;
167 	case RDMA_RESTRACK_CM_ID:
168 		return container_of(res, struct rdma_id_private,
169 				    res)->id.device;
170 	case RDMA_RESTRACK_MR:
171 		return container_of(res, struct ib_mr, res)->device;
172 	case RDMA_RESTRACK_CTX:
173 		return container_of(res, struct ib_ucontext, res)->device;
174 	case RDMA_RESTRACK_COUNTER:
175 		return container_of(res, struct rdma_counter, res)->device;
176 	default:
177 		WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
178 		return NULL;
179 	}
180 }
181 
182 void rdma_restrack_set_task(struct rdma_restrack_entry *res,
183 			    const char *caller)
184 {
185 	if (caller) {
186 		res->kern_name = caller;
187 		return;
188 	}
189 
190 	if (res->task)
191 		put_task_struct(res->task);
192 	get_task_struct(current);
193 	res->task = current;
194 }
195 EXPORT_SYMBOL(rdma_restrack_set_task);
196 
197 /**
198  * rdma_restrack_attach_task() - attach the task onto this resource
199  * @res:  resource entry
200  * @task: the task to attach, the current task will be used if it is NULL.
201  */
202 void rdma_restrack_attach_task(struct rdma_restrack_entry *res,
203 			       struct task_struct *task)
204 {
205 	if (res->task)
206 		put_task_struct(res->task);
207 	get_task_struct(task);
208 	res->task = task;
209 }
210 
211 static void rdma_restrack_add(struct rdma_restrack_entry *res)
212 {
213 	struct ib_device *dev = res_to_dev(res);
214 	struct rdma_restrack_root *rt;
215 	int ret;
216 
217 	if (!dev)
218 		return;
219 
220 	rt = &dev->res[res->type];
221 
222 	kref_init(&res->kref);
223 	init_completion(&res->comp);
224 	if (res->type == RDMA_RESTRACK_QP) {
225 		/* Special case to ensure that LQPN points to right QP */
226 		struct ib_qp *qp = container_of(res, struct ib_qp, res);
227 
228 		ret = xa_insert(&rt->xa, qp->qp_num, res, GFP_KERNEL);
229 		res->id = ret ? 0 : qp->qp_num;
230 	} else if (res->type == RDMA_RESTRACK_COUNTER) {
231 		/* Special case to ensure that cntn points to right counter */
232 		struct rdma_counter *counter;
233 
234 		counter = container_of(res, struct rdma_counter, res);
235 		ret = xa_insert(&rt->xa, counter->id, res, GFP_KERNEL);
236 		res->id = ret ? 0 : counter->id;
237 	} else {
238 		ret = xa_alloc_cyclic(&rt->xa, &res->id, res, xa_limit_32b,
239 				      &rt->next_id, GFP_KERNEL);
240 	}
241 
242 	if (!ret)
243 		res->valid = true;
244 }
245 
246 /**
247  * rdma_restrack_kadd() - add kernel object to the reource tracking database
248  * @res:  resource entry
249  */
250 void rdma_restrack_kadd(struct rdma_restrack_entry *res)
251 {
252 	res->task = NULL;
253 	set_kern_name(res);
254 	res->user = false;
255 	rdma_restrack_add(res);
256 }
257 EXPORT_SYMBOL(rdma_restrack_kadd);
258 
259 /**
260  * rdma_restrack_uadd() - add user object to the reource tracking database
261  * @res:  resource entry
262  */
263 void rdma_restrack_uadd(struct rdma_restrack_entry *res)
264 {
265 	if ((res->type != RDMA_RESTRACK_CM_ID) &&
266 	    (res->type != RDMA_RESTRACK_COUNTER))
267 		res->task = NULL;
268 
269 	if (!res->task)
270 		rdma_restrack_set_task(res, NULL);
271 	res->kern_name = NULL;
272 
273 	res->user = true;
274 	rdma_restrack_add(res);
275 }
276 EXPORT_SYMBOL(rdma_restrack_uadd);
277 
278 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res)
279 {
280 	return kref_get_unless_zero(&res->kref);
281 }
282 EXPORT_SYMBOL(rdma_restrack_get);
283 
284 /**
285  * rdma_restrack_get_byid() - translate from ID to restrack object
286  * @dev: IB device
287  * @type: resource track type
288  * @id: ID to take a look
289  *
290  * Return: Pointer to restrack entry or -ENOENT in case of error.
291  */
292 struct rdma_restrack_entry *
293 rdma_restrack_get_byid(struct ib_device *dev,
294 		       enum rdma_restrack_type type, u32 id)
295 {
296 	struct rdma_restrack_root *rt = &dev->res[type];
297 	struct rdma_restrack_entry *res;
298 
299 	xa_lock(&rt->xa);
300 	res = xa_load(&rt->xa, id);
301 	if (!res || !rdma_restrack_get(res))
302 		res = ERR_PTR(-ENOENT);
303 	xa_unlock(&rt->xa);
304 
305 	return res;
306 }
307 EXPORT_SYMBOL(rdma_restrack_get_byid);
308 
309 static void restrack_release(struct kref *kref)
310 {
311 	struct rdma_restrack_entry *res;
312 
313 	res = container_of(kref, struct rdma_restrack_entry, kref);
314 	complete(&res->comp);
315 }
316 
317 int rdma_restrack_put(struct rdma_restrack_entry *res)
318 {
319 	return kref_put(&res->kref, restrack_release);
320 }
321 EXPORT_SYMBOL(rdma_restrack_put);
322 
323 void rdma_restrack_del(struct rdma_restrack_entry *res)
324 {
325 	struct rdma_restrack_entry *old;
326 	struct rdma_restrack_root *rt;
327 	struct ib_device *dev;
328 
329 	if (!res->valid)
330 		goto out;
331 
332 	dev = res_to_dev(res);
333 	if (WARN_ON(!dev))
334 		return;
335 
336 	rt = &dev->res[res->type];
337 
338 	old = xa_erase(&rt->xa, res->id);
339 	WARN_ON(old != res);
340 	res->valid = false;
341 
342 	rdma_restrack_put(res);
343 	wait_for_completion(&res->comp);
344 
345 out:
346 	if (res->task) {
347 		put_task_struct(res->task);
348 		res->task = NULL;
349 	}
350 }
351 EXPORT_SYMBOL(rdma_restrack_del);
352 
353 bool rdma_is_visible_in_pid_ns(struct rdma_restrack_entry *res)
354 {
355 	/*
356 	 * 1. Kern resources should be visible in init
357 	 *    namespace only
358 	 * 2. Present only resources visible in the current
359 	 *     namespace
360 	 */
361 	if (rdma_is_kernel_res(res))
362 		return task_active_pid_ns(current) == &init_pid_ns;
363 	return task_active_pid_ns(current) == task_active_pid_ns(res->task);
364 }
365