hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/infiniband/core/restrack.c
....@@ -6,22 +6,35 @@
66 #include <rdma/rdma_cm.h>
77 #include <rdma/ib_verbs.h>
88 #include <rdma/restrack.h>
9
+#include <rdma/rdma_counter.h>
910 #include <linux/mutex.h>
1011 #include <linux/sched/task.h>
1112 #include <linux/pid_namespace.h>
1213
1314 #include "cma_priv.h"
15
+#include "restrack.h"
1416
15
-static int fill_res_noop(struct sk_buff *msg,
16
- struct rdma_restrack_entry *entry)
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)
1724 {
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
+
1837 return 0;
19
-}
20
-
21
-void rdma_restrack_init(struct rdma_restrack_root *res)
22
-{
23
- init_rwsem(&res->rwsem);
24
- res->fill_res_entry = fill_res_noop;
2538 }
2639
2740 static const char *type2str(enum rdma_restrack_type type)
....@@ -32,90 +45,83 @@
3245 [RDMA_RESTRACK_QP] = "QP",
3346 [RDMA_RESTRACK_CM_ID] = "CM_ID",
3447 [RDMA_RESTRACK_MR] = "MR",
48
+ [RDMA_RESTRACK_CTX] = "CTX",
49
+ [RDMA_RESTRACK_COUNTER] = "COUNTER",
3550 };
3651
3752 return names[type];
3853 };
3954
40
-void rdma_restrack_clean(struct rdma_restrack_root *res)
55
+/**
56
+ * rdma_restrack_clean() - clean resource tracking
57
+ * @dev: IB device
58
+ */
59
+void rdma_restrack_clean(struct ib_device *dev)
4160 {
61
+ struct rdma_restrack_root *rt = dev->res;
4262 struct rdma_restrack_entry *e;
4363 char buf[TASK_COMM_LEN];
44
- struct ib_device *dev;
64
+ bool found = false;
4565 const char *owner;
46
- int bkt;
66
+ int i;
4767
48
- if (hash_empty(res->hash))
49
- return;
68
+ for (i = 0 ; i < RDMA_RESTRACK_MAX; i++) {
69
+ struct xarray *xa = &dev->res[i].xa;
5070
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;
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;
6697 }
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);
98
+ xa_destroy(xa);
7199 }
72
- pr_err("restrack: %s", CUT_HERE);
100
+ if (found)
101
+ pr_err("restrack: %s", CUT_HERE);
102
+
103
+ kfree(rt);
73104 }
74105
75
-int rdma_restrack_count(struct rdma_restrack_root *res,
76
- enum rdma_restrack_type type,
77
- struct pid_namespace *ns)
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)
78112 {
113
+ struct rdma_restrack_root *rt = &dev->res[type];
79114 struct rdma_restrack_entry *e;
115
+ XA_STATE(xas, &rt->xa, 0);
80116 u32 cnt = 0;
81117
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);
118
+ xa_lock(&rt->xa);
119
+ xas_for_each(&xas, e, U32_MAX)
120
+ cnt++;
121
+ xa_unlock(&rt->xa);
90122 return cnt;
91123 }
92124 EXPORT_SYMBOL(rdma_restrack_count);
93
-
94
-static void set_kern_name(struct rdma_restrack_entry *res)
95
-{
96
- struct ib_pd *pd;
97
-
98
- 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;
114
- }
115
-
116
- if (pd)
117
- res->kern_name = pd->res.kern_name;
118
-}
119125
120126 static struct ib_device *res_to_dev(struct rdma_restrack_entry *res)
121127 {
....@@ -131,56 +137,118 @@
131137 res)->id.device;
132138 case RDMA_RESTRACK_MR:
133139 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;
134144 default:
135145 WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
136146 return NULL;
137147 }
138148 }
139149
140
-static bool res_is_user(struct rdma_restrack_entry *res)
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)
141158 {
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;
149
- case RDMA_RESTRACK_CM_ID:
150
- return !res->kern_name;
151
- case RDMA_RESTRACK_MR:
152
- return container_of(res, struct ib_mr, res)->pd->uobject;
153
- default:
154
- WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
155
- return false;
156
- }
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;
157167 }
158168
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
+ */
159220 void rdma_restrack_add(struct rdma_restrack_entry *res)
160221 {
161222 struct ib_device *dev = res_to_dev(res);
223
+ struct rdma_restrack_root *rt;
224
+ int ret;
162225
163226 if (!dev)
164227 return;
165228
166
- if (res->type != RDMA_RESTRACK_CM_ID || !res_is_user(res))
167
- res->task = NULL;
229
+ rt = &dev->res[res->type];
168230
169
- if (res_is_user(res)) {
170
- if (!res->task)
171
- rdma_restrack_set_task(res, current);
172
- res->kern_name = NULL;
231
+ if (res->type == RDMA_RESTRACK_QP) {
232
+ /* Special case to ensure that LQPN points to right QP */
233
+ struct ib_qp *qp = container_of(res, struct ib_qp, res);
234
+
235
+ ret = xa_insert(&rt->xa, qp->qp_num, res, GFP_KERNEL);
236
+ res->id = ret ? 0 : qp->qp_num;
237
+ } else if (res->type == RDMA_RESTRACK_COUNTER) {
238
+ /* Special case to ensure that cntn points to right counter */
239
+ struct rdma_counter *counter;
240
+
241
+ counter = container_of(res, struct rdma_counter, res);
242
+ ret = xa_insert(&rt->xa, counter->id, res, GFP_KERNEL);
243
+ res->id = ret ? 0 : counter->id;
173244 } else {
174
- set_kern_name(res);
245
+ ret = xa_alloc_cyclic(&rt->xa, &res->id, res, xa_limit_32b,
246
+ &rt->next_id, GFP_KERNEL);
247
+ ret = (ret < 0) ? ret : 0;
175248 }
176249
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);
250
+ if (!ret)
251
+ res->valid = true;
184252 }
185253 EXPORT_SYMBOL(rdma_restrack_add);
186254
....@@ -190,11 +258,40 @@
190258 }
191259 EXPORT_SYMBOL(rdma_restrack_get);
192260
261
+/**
262
+ * rdma_restrack_get_byid() - translate from ID to restrack object
263
+ * @dev: IB device
264
+ * @type: resource track type
265
+ * @id: ID to take a look
266
+ *
267
+ * Return: Pointer to restrack entry or -ENOENT in case of error.
268
+ */
269
+struct rdma_restrack_entry *
270
+rdma_restrack_get_byid(struct ib_device *dev,
271
+ enum rdma_restrack_type type, u32 id)
272
+{
273
+ struct rdma_restrack_root *rt = &dev->res[type];
274
+ struct rdma_restrack_entry *res;
275
+
276
+ xa_lock(&rt->xa);
277
+ res = xa_load(&rt->xa, id);
278
+ if (!res || !rdma_restrack_get(res))
279
+ res = ERR_PTR(-ENOENT);
280
+ xa_unlock(&rt->xa);
281
+
282
+ return res;
283
+}
284
+EXPORT_SYMBOL(rdma_restrack_get_byid);
285
+
193286 static void restrack_release(struct kref *kref)
194287 {
195288 struct rdma_restrack_entry *res;
196289
197290 res = container_of(kref, struct rdma_restrack_entry, kref);
291
+ if (res->task) {
292
+ put_task_struct(res->task);
293
+ res->task = NULL;
294
+ }
198295 complete(&res->comp);
199296 }
200297
....@@ -204,30 +301,37 @@
204301 }
205302 EXPORT_SYMBOL(rdma_restrack_put);
206303
304
+/**
305
+ * rdma_restrack_del() - delete object from the reource tracking database
306
+ * @res: resource entry
307
+ */
207308 void rdma_restrack_del(struct rdma_restrack_entry *res)
208309 {
310
+ struct rdma_restrack_entry *old;
311
+ struct rdma_restrack_root *rt;
209312 struct ib_device *dev;
210313
211
- if (!res->valid)
212
- goto out;
314
+ if (!res->valid) {
315
+ if (res->task) {
316
+ put_task_struct(res->task);
317
+ res->task = NULL;
318
+ }
319
+ return;
320
+ }
213321
214322 dev = res_to_dev(res);
215
- if (!dev)
323
+ if (WARN_ON(!dev))
216324 return;
217325
218
- rdma_restrack_put(res);
326
+ rt = &dev->res[res->type];
219327
220
- wait_for_completion(&res->comp);
221
-
222
- down_write(&dev->res.rwsem);
223
- hash_del(&res->node);
328
+ old = xa_erase(&rt->xa, res->id);
329
+ if (res->type == RDMA_RESTRACK_MR || res->type == RDMA_RESTRACK_QP)
330
+ return;
331
+ WARN_ON(old != res);
224332 res->valid = false;
225
- up_write(&dev->res.rwsem);
226333
227
-out:
228
- if (res->task) {
229
- put_task_struct(res->task);
230
- res->task = NULL;
231
- }
334
+ rdma_restrack_put(res);
335
+ wait_for_completion(&res->comp);
232336 }
233337 EXPORT_SYMBOL(rdma_restrack_del);