Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / infiniband / core / restrack.c
blob3dbc4e4cca415fd94a24443e44d1430a5cbe63d7
1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
2 /*
3 * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
4 */
6 #include <rdma/ib_verbs.h>
7 #include <rdma/restrack.h>
8 #include <linux/mutex.h>
9 #include <linux/sched/task.h>
10 #include <linux/pid_namespace.h>
12 void rdma_restrack_init(struct rdma_restrack_root *res)
14 init_rwsem(&res->rwsem);
17 void rdma_restrack_clean(struct rdma_restrack_root *res)
19 WARN_ON_ONCE(!hash_empty(res->hash));
22 int rdma_restrack_count(struct rdma_restrack_root *res,
23 enum rdma_restrack_type type,
24 struct pid_namespace *ns)
26 struct rdma_restrack_entry *e;
27 u32 cnt = 0;
29 down_read(&res->rwsem);
30 hash_for_each_possible(res->hash, e, node, type) {
31 if (ns == &init_pid_ns ||
32 (!rdma_is_kernel_res(e) &&
33 ns == task_active_pid_ns(e->task)))
34 cnt++;
36 up_read(&res->rwsem);
37 return cnt;
39 EXPORT_SYMBOL(rdma_restrack_count);
41 static void set_kern_name(struct rdma_restrack_entry *res)
43 enum rdma_restrack_type type = res->type;
44 struct ib_qp *qp;
46 if (type != RDMA_RESTRACK_QP)
47 /* PD and CQ types already have this name embedded in */
48 return;
50 qp = container_of(res, struct ib_qp, res);
51 if (!qp->pd) {
52 WARN_ONCE(true, "XRC QPs are not supported\n");
53 /* Survive, despite the programmer's error */
54 res->kern_name = " ";
55 return;
58 res->kern_name = qp->pd->res.kern_name;
61 static struct ib_device *res_to_dev(struct rdma_restrack_entry *res)
63 enum rdma_restrack_type type = res->type;
64 struct ib_device *dev;
65 struct ib_pd *pd;
66 struct ib_cq *cq;
67 struct ib_qp *qp;
69 switch (type) {
70 case RDMA_RESTRACK_PD:
71 pd = container_of(res, struct ib_pd, res);
72 dev = pd->device;
73 break;
74 case RDMA_RESTRACK_CQ:
75 cq = container_of(res, struct ib_cq, res);
76 dev = cq->device;
77 break;
78 case RDMA_RESTRACK_QP:
79 qp = container_of(res, struct ib_qp, res);
80 dev = qp->device;
81 break;
82 default:
83 WARN_ONCE(true, "Wrong resource tracking type %u\n", type);
84 return NULL;
87 return dev;
90 static bool res_is_user(struct rdma_restrack_entry *res)
92 switch (res->type) {
93 case RDMA_RESTRACK_PD:
94 return container_of(res, struct ib_pd, res)->uobject;
95 case RDMA_RESTRACK_CQ:
96 return container_of(res, struct ib_cq, res)->uobject;
97 case RDMA_RESTRACK_QP:
98 return container_of(res, struct ib_qp, res)->uobject;
99 default:
100 WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
101 return false;
105 void rdma_restrack_add(struct rdma_restrack_entry *res)
107 struct ib_device *dev = res_to_dev(res);
109 if (!dev)
110 return;
112 if (res_is_user(res)) {
113 get_task_struct(current);
114 res->task = current;
115 res->kern_name = NULL;
116 } else {
117 set_kern_name(res);
118 res->task = NULL;
121 kref_init(&res->kref);
122 init_completion(&res->comp);
123 res->valid = true;
125 down_write(&dev->res.rwsem);
126 hash_add(dev->res.hash, &res->node, res->type);
127 up_write(&dev->res.rwsem);
129 EXPORT_SYMBOL(rdma_restrack_add);
131 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res)
133 return kref_get_unless_zero(&res->kref);
135 EXPORT_SYMBOL(rdma_restrack_get);
137 static void restrack_release(struct kref *kref)
139 struct rdma_restrack_entry *res;
141 res = container_of(kref, struct rdma_restrack_entry, kref);
142 complete(&res->comp);
145 int rdma_restrack_put(struct rdma_restrack_entry *res)
147 return kref_put(&res->kref, restrack_release);
149 EXPORT_SYMBOL(rdma_restrack_put);
151 void rdma_restrack_del(struct rdma_restrack_entry *res)
153 struct ib_device *dev;
155 if (!res->valid)
156 return;
158 dev = res_to_dev(res);
159 if (!dev)
160 return;
162 rdma_restrack_put(res);
164 wait_for_completion(&res->comp);
166 down_write(&dev->res.rwsem);
167 hash_del(&res->node);
168 res->valid = false;
169 if (res->task)
170 put_task_struct(res->task);
171 up_write(&dev->res.rwsem);
173 EXPORT_SYMBOL(rdma_restrack_del);