blob: 3b7fa0ccaa08a228d0d4523cb0a19a2cc96b23c2 [file] [log] [blame]
Leon Romanovsky33edc3b2018-06-05 07:48:08 +03001// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
Leon Romanovsky02d88832018-01-28 11:17:20 +02002/*
3 * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
4 */
5
Steve Wise00313982018-03-01 13:57:44 -08006#include <rdma/rdma_cm.h>
Leon Romanovsky02d88832018-01-28 11:17:20 +02007#include <rdma/ib_verbs.h>
8#include <rdma/restrack.h>
9#include <linux/mutex.h>
10#include <linux/sched/task.h>
Leon Romanovsky02d88832018-01-28 11:17:20 +020011#include <linux/pid_namespace.h>
12
Steve Wise00313982018-03-01 13:57:44 -080013#include "cma_priv.h"
14
Steve Wiseda5c8502018-05-03 08:41:30 -070015static int fill_res_noop(struct sk_buff *msg,
16 struct rdma_restrack_entry *entry)
17{
18 return 0;
19}
20
Leon Romanovsky02d88832018-01-28 11:17:20 +020021void rdma_restrack_init(struct rdma_restrack_root *res)
22{
23 init_rwsem(&res->rwsem);
Steve Wiseda5c8502018-05-03 08:41:30 -070024 res->fill_res_entry = fill_res_noop;
Leon Romanovsky02d88832018-01-28 11:17:20 +020025}
26
Leon Romanovsky03286032018-03-21 17:12:42 +020027static const char *type2str(enum rdma_restrack_type type)
28{
29 static const char * const names[RDMA_RESTRACK_MAX] = {
30 [RDMA_RESTRACK_PD] = "PD",
31 [RDMA_RESTRACK_CQ] = "CQ",
32 [RDMA_RESTRACK_QP] = "QP",
33 [RDMA_RESTRACK_CM_ID] = "CM_ID",
34 [RDMA_RESTRACK_MR] = "MR",
35 };
36
37 return names[type];
38};
39
Leon Romanovsky02d88832018-01-28 11:17:20 +020040void rdma_restrack_clean(struct rdma_restrack_root *res)
41{
Leon Romanovsky03286032018-03-21 17:12:42 +020042 struct rdma_restrack_entry *e;
43 char buf[TASK_COMM_LEN];
44 struct ib_device *dev;
45 const char *owner;
46 int bkt;
47
48 if (hash_empty(res->hash))
49 return;
50
51 dev = container_of(res, struct ib_device, res);
52 pr_err("restrack: %s", CUT_HERE);
53 pr_err("restrack: BUG: RESTRACK detected leak of resources on %s\n",
54 dev->name);
55 hash_for_each(res->hash, bkt, e, node) {
56 if (rdma_is_kernel_res(e)) {
57 owner = e->kern_name;
58 } else {
59 /*
60 * There is no need to call get_task_struct here,
61 * because we can be here only if there are more
62 * get_task_struct() call than put_task_struct().
63 */
64 get_task_comm(buf, e->task);
65 owner = buf;
66 }
67
68 pr_err("restrack: %s %s object allocated by %s is not freed\n",
69 rdma_is_kernel_res(e) ? "Kernel" : "User",
70 type2str(e->type), owner);
71 }
72 pr_err("restrack: %s", CUT_HERE);
Leon Romanovsky02d88832018-01-28 11:17:20 +020073}
74
75int rdma_restrack_count(struct rdma_restrack_root *res,
76 enum rdma_restrack_type type,
77 struct pid_namespace *ns)
78{
79 struct rdma_restrack_entry *e;
80 u32 cnt = 0;
81
82 down_read(&res->rwsem);
83 hash_for_each_possible(res->hash, e, node, type) {
84 if (ns == &init_pid_ns ||
85 (!rdma_is_kernel_res(e) &&
86 ns == task_active_pid_ns(e->task)))
87 cnt++;
88 }
89 up_read(&res->rwsem);
90 return cnt;
91}
92EXPORT_SYMBOL(rdma_restrack_count);
93
94static void set_kern_name(struct rdma_restrack_entry *res)
95{
Steve Wisefccec5b2018-03-01 13:58:13 -080096 struct ib_pd *pd;
Leon Romanovsky02d88832018-01-28 11:17:20 +020097
Steve Wisefccec5b2018-03-01 13:58:13 -080098 switch (res->type) {
99 case RDMA_RESTRACK_QP:
100 pd = container_of(res, struct ib_qp, res)->pd;
101 if (!pd) {
102 WARN_ONCE(true, "XRC QPs are not supported\n");
103 /* Survive, despite the programmer's error */
104 res->kern_name = " ";
105 }
106 break;
107 case RDMA_RESTRACK_MR:
108 pd = container_of(res, struct ib_mr, res)->pd;
109 break;
110 default:
111 /* Other types set kern_name directly */
112 pd = NULL;
113 break;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200114 }
115
Steve Wisefccec5b2018-03-01 13:58:13 -0800116 if (pd)
117 res->kern_name = pd->res.kern_name;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200118}
119
120static struct ib_device *res_to_dev(struct rdma_restrack_entry *res)
121{
Steve Wise88831a22018-03-01 13:57:22 -0800122 switch (res->type) {
Leon Romanovsky02d88832018-01-28 11:17:20 +0200123 case RDMA_RESTRACK_PD:
Steve Wise88831a22018-03-01 13:57:22 -0800124 return container_of(res, struct ib_pd, res)->device;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200125 case RDMA_RESTRACK_CQ:
Steve Wise88831a22018-03-01 13:57:22 -0800126 return container_of(res, struct ib_cq, res)->device;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200127 case RDMA_RESTRACK_QP:
Steve Wise88831a22018-03-01 13:57:22 -0800128 return container_of(res, struct ib_qp, res)->device;
Steve Wise00313982018-03-01 13:57:44 -0800129 case RDMA_RESTRACK_CM_ID:
130 return container_of(res, struct rdma_id_private,
131 res)->id.device;
Steve Wisefccec5b2018-03-01 13:58:13 -0800132 case RDMA_RESTRACK_MR:
133 return container_of(res, struct ib_mr, res)->device;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200134 default:
Steve Wise88831a22018-03-01 13:57:22 -0800135 WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
Leon Romanovsky02d88832018-01-28 11:17:20 +0200136 return NULL;
137 }
Leon Romanovsky02d88832018-01-28 11:17:20 +0200138}
139
Steve Wise2f08ee32018-02-14 18:43:36 -0800140static bool res_is_user(struct rdma_restrack_entry *res)
141{
142 switch (res->type) {
143 case RDMA_RESTRACK_PD:
144 return container_of(res, struct ib_pd, res)->uobject;
145 case RDMA_RESTRACK_CQ:
146 return container_of(res, struct ib_cq, res)->uobject;
147 case RDMA_RESTRACK_QP:
148 return container_of(res, struct ib_qp, res)->uobject;
Steve Wise00313982018-03-01 13:57:44 -0800149 case RDMA_RESTRACK_CM_ID:
150 return !res->kern_name;
Steve Wisefccec5b2018-03-01 13:58:13 -0800151 case RDMA_RESTRACK_MR:
152 return container_of(res, struct ib_mr, res)->pd->uobject;
Steve Wise2f08ee32018-02-14 18:43:36 -0800153 default:
154 WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
155 return false;
156 }
157}
158
Leon Romanovsky02d88832018-01-28 11:17:20 +0200159void rdma_restrack_add(struct rdma_restrack_entry *res)
160{
161 struct ib_device *dev = res_to_dev(res);
162
163 if (!dev)
164 return;
165
Leon Romanovsky7d9a9352018-03-15 11:10:42 +0200166 if (res->type != RDMA_RESTRACK_CM_ID || !res_is_user(res))
167 res->task = NULL;
168
Steve Wise2f08ee32018-02-14 18:43:36 -0800169 if (res_is_user(res)) {
Steve Wise00313982018-03-01 13:57:44 -0800170 if (!res->task)
171 rdma_restrack_set_task(res, current);
Leon Romanovsky02d88832018-01-28 11:17:20 +0200172 res->kern_name = NULL;
173 } else {
174 set_kern_name(res);
Leon Romanovsky02d88832018-01-28 11:17:20 +0200175 }
176
177 kref_init(&res->kref);
178 init_completion(&res->comp);
179 res->valid = true;
180
181 down_write(&dev->res.rwsem);
182 hash_add(dev->res.hash, &res->node, res->type);
183 up_write(&dev->res.rwsem);
184}
185EXPORT_SYMBOL(rdma_restrack_add);
186
187int __must_check rdma_restrack_get(struct rdma_restrack_entry *res)
188{
189 return kref_get_unless_zero(&res->kref);
190}
191EXPORT_SYMBOL(rdma_restrack_get);
192
193static void restrack_release(struct kref *kref)
194{
195 struct rdma_restrack_entry *res;
196
197 res = container_of(kref, struct rdma_restrack_entry, kref);
198 complete(&res->comp);
199}
200
201int rdma_restrack_put(struct rdma_restrack_entry *res)
202{
203 return kref_put(&res->kref, restrack_release);
204}
205EXPORT_SYMBOL(rdma_restrack_put);
206
207void rdma_restrack_del(struct rdma_restrack_entry *res)
208{
209 struct ib_device *dev;
210
211 if (!res->valid)
212 return;
213
214 dev = res_to_dev(res);
215 if (!dev)
216 return;
217
218 rdma_restrack_put(res);
219
220 wait_for_completion(&res->comp);
221
222 down_write(&dev->res.rwsem);
223 hash_del(&res->node);
224 res->valid = false;
225 if (res->task)
226 put_task_struct(res->task);
227 up_write(&dev->res.rwsem);
228}
229EXPORT_SYMBOL(rdma_restrack_del);