1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2019 Mellanox Technologies. All rights reserved.
5 #include <rdma/ib_verbs.h>
6 #include <rdma/rdma_counter.h>
11 #define ALL_AUTO_MODE_MASKS (RDMA_COUNTER_MASK_QP_TYPE)
13 static int __counter_set_mode(struct rdma_counter_mode *curr,
14 enum rdma_nl_counter_mode new_mode,
15 enum rdma_nl_counter_mask new_mask)
17 if ((new_mode == RDMA_COUNTER_MODE_AUTO) &&
18 ((new_mask & (~ALL_AUTO_MODE_MASKS)) ||
19 (curr->mode != RDMA_COUNTER_MODE_NONE)))
22 curr->mode = new_mode;
23 curr->mask = new_mask;
28 * rdma_counter_set_auto_mode() - Turn on/off per-port auto mode
30 * When @on is true, the @mask must be set; When @on is false, it goes
31 * into manual mode if there's any counter, so that the user is able to
32 * manually access them.
34 int rdma_counter_set_auto_mode(struct ib_device *dev, u8 port,
35 bool on, enum rdma_nl_counter_mask mask)
37 struct rdma_port_counter *port_counter;
40 port_counter = &dev->port_data[port].port_counter;
41 mutex_lock(&port_counter->lock);
43 ret = __counter_set_mode(&port_counter->mode,
44 RDMA_COUNTER_MODE_AUTO, mask);
46 if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO) {
51 if (port_counter->num_counters)
52 ret = __counter_set_mode(&port_counter->mode,
53 RDMA_COUNTER_MODE_MANUAL, 0);
55 ret = __counter_set_mode(&port_counter->mode,
56 RDMA_COUNTER_MODE_NONE, 0);
60 mutex_unlock(&port_counter->lock);
64 static struct rdma_counter *rdma_counter_alloc(struct ib_device *dev, u8 port,
65 enum rdma_nl_counter_mode mode)
67 struct rdma_port_counter *port_counter;
68 struct rdma_counter *counter;
71 if (!dev->ops.counter_dealloc || !dev->ops.counter_alloc_stats)
74 counter = kzalloc(sizeof(*counter), GFP_KERNEL);
78 counter->device = dev;
80 counter->res.type = RDMA_RESTRACK_COUNTER;
81 counter->stats = dev->ops.counter_alloc_stats(counter);
85 port_counter = &dev->port_data[port].port_counter;
86 mutex_lock(&port_counter->lock);
87 if (mode == RDMA_COUNTER_MODE_MANUAL) {
88 ret = __counter_set_mode(&port_counter->mode,
89 RDMA_COUNTER_MODE_MANUAL, 0);
94 port_counter->num_counters++;
95 mutex_unlock(&port_counter->lock);
97 counter->mode.mode = mode;
98 kref_init(&counter->kref);
99 mutex_init(&counter->lock);
104 mutex_unlock(&port_counter->lock);
105 kfree(counter->stats);
111 static void rdma_counter_free(struct rdma_counter *counter)
113 struct rdma_port_counter *port_counter;
115 port_counter = &counter->device->port_data[counter->port].port_counter;
116 mutex_lock(&port_counter->lock);
117 port_counter->num_counters--;
118 if (!port_counter->num_counters &&
119 (port_counter->mode.mode == RDMA_COUNTER_MODE_MANUAL))
120 __counter_set_mode(&port_counter->mode, RDMA_COUNTER_MODE_NONE,
123 mutex_unlock(&port_counter->lock);
125 rdma_restrack_del(&counter->res);
126 kfree(counter->stats);
130 static void auto_mode_init_counter(struct rdma_counter *counter,
131 const struct ib_qp *qp,
132 enum rdma_nl_counter_mask new_mask)
134 struct auto_mode_param *param = &counter->mode.param;
136 counter->mode.mode = RDMA_COUNTER_MODE_AUTO;
137 counter->mode.mask = new_mask;
139 if (new_mask & RDMA_COUNTER_MASK_QP_TYPE)
140 param->qp_type = qp->qp_type;
143 static bool auto_mode_match(struct ib_qp *qp, struct rdma_counter *counter,
144 enum rdma_nl_counter_mask auto_mask)
146 struct auto_mode_param *param = &counter->mode.param;
149 if (rdma_is_kernel_res(&counter->res) != rdma_is_kernel_res(&qp->res))
152 /* Ensure that counter belong to right PID */
153 if (!rdma_is_kernel_res(&counter->res) &&
154 !rdma_is_kernel_res(&qp->res) &&
155 (task_pid_vnr(counter->res.task) != current->pid))
158 if (auto_mask & RDMA_COUNTER_MASK_QP_TYPE)
159 match &= (param->qp_type == qp->qp_type);
164 static int __rdma_counter_bind_qp(struct rdma_counter *counter,
172 if (!qp->device->ops.counter_bind_qp)
175 mutex_lock(&counter->lock);
176 ret = qp->device->ops.counter_bind_qp(counter, qp);
177 mutex_unlock(&counter->lock);
182 static int __rdma_counter_unbind_qp(struct ib_qp *qp)
184 struct rdma_counter *counter = qp->counter;
187 if (!qp->device->ops.counter_unbind_qp)
190 mutex_lock(&counter->lock);
191 ret = qp->device->ops.counter_unbind_qp(qp);
192 mutex_unlock(&counter->lock);
197 static void counter_history_stat_update(const struct rdma_counter *counter)
199 struct ib_device *dev = counter->device;
200 struct rdma_port_counter *port_counter;
203 port_counter = &dev->port_data[counter->port].port_counter;
204 if (!port_counter->hstats)
207 for (i = 0; i < counter->stats->num_counters; i++)
208 port_counter->hstats->value[i] += counter->stats->value[i];
212 * rdma_get_counter_auto_mode - Find the counter that @qp should be bound
215 * Return: The counter (with ref-count increased) if found
217 static struct rdma_counter *rdma_get_counter_auto_mode(struct ib_qp *qp,
220 struct rdma_port_counter *port_counter;
221 struct rdma_counter *counter = NULL;
222 struct ib_device *dev = qp->device;
223 struct rdma_restrack_entry *res;
224 struct rdma_restrack_root *rt;
225 unsigned long id = 0;
227 port_counter = &dev->port_data[port].port_counter;
228 rt = &dev->res[RDMA_RESTRACK_COUNTER];
230 xa_for_each(&rt->xa, id, res) {
231 if (!rdma_is_visible_in_pid_ns(res))
234 counter = container_of(res, struct rdma_counter, res);
235 if ((counter->device != qp->device) || (counter->port != port))
238 if (auto_mode_match(qp, counter, port_counter->mode.mask))
244 if (counter && !kref_get_unless_zero(&counter->kref))
251 static void rdma_counter_res_add(struct rdma_counter *counter,
254 if (rdma_is_kernel_res(&qp->res)) {
255 rdma_restrack_set_task(&counter->res, qp->res.kern_name);
256 rdma_restrack_kadd(&counter->res);
258 rdma_restrack_attach_task(&counter->res, qp->res.task);
259 rdma_restrack_uadd(&counter->res);
263 static void counter_release(struct kref *kref)
265 struct rdma_counter *counter;
267 counter = container_of(kref, struct rdma_counter, kref);
268 counter_history_stat_update(counter);
269 counter->device->ops.counter_dealloc(counter);
270 rdma_counter_free(counter);
274 * rdma_counter_bind_qp_auto - Check and bind the QP to a counter base on
277 int rdma_counter_bind_qp_auto(struct ib_qp *qp, u8 port)
279 struct rdma_port_counter *port_counter;
280 struct ib_device *dev = qp->device;
281 struct rdma_counter *counter;
284 if (!rdma_is_port_valid(dev, port))
287 port_counter = &dev->port_data[port].port_counter;
288 if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO)
291 counter = rdma_get_counter_auto_mode(qp, port);
293 ret = __rdma_counter_bind_qp(counter, qp);
295 kref_put(&counter->kref, counter_release);
299 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_AUTO);
303 auto_mode_init_counter(counter, qp, port_counter->mode.mask);
305 ret = __rdma_counter_bind_qp(counter, qp);
307 rdma_counter_free(counter);
311 rdma_counter_res_add(counter, qp);
318 * rdma_counter_unbind_qp - Unbind a qp from a counter
320 * true - Decrease the counter ref-count anyway (e.g., qp destroy)
322 int rdma_counter_unbind_qp(struct ib_qp *qp, bool force)
324 struct rdma_counter *counter = qp->counter;
330 ret = __rdma_counter_unbind_qp(qp);
334 kref_put(&counter->kref, counter_release);
338 int rdma_counter_query_stats(struct rdma_counter *counter)
340 struct ib_device *dev = counter->device;
343 if (!dev->ops.counter_update_stats)
346 mutex_lock(&counter->lock);
347 ret = dev->ops.counter_update_stats(counter);
348 mutex_unlock(&counter->lock);
353 static u64 get_running_counters_hwstat_sum(struct ib_device *dev,
356 struct rdma_restrack_entry *res;
357 struct rdma_restrack_root *rt;
358 struct rdma_counter *counter;
359 unsigned long id = 0;
362 rt = &dev->res[RDMA_RESTRACK_COUNTER];
364 xa_for_each(&rt->xa, id, res) {
365 if (!rdma_restrack_get(res))
370 counter = container_of(res, struct rdma_counter, res);
371 if ((counter->device != dev) || (counter->port != port) ||
372 rdma_counter_query_stats(counter))
375 sum += counter->stats->value[index];
379 rdma_restrack_put(res);
387 * rdma_counter_get_hwstat_value() - Get the sum value of all counters on a
388 * specific port, including the running ones and history data
390 u64 rdma_counter_get_hwstat_value(struct ib_device *dev, u8 port, u32 index)
392 struct rdma_port_counter *port_counter;
395 port_counter = &dev->port_data[port].port_counter;
396 if (!port_counter->hstats)
399 sum = get_running_counters_hwstat_sum(dev, port, index);
400 sum += port_counter->hstats->value[index];
405 static struct ib_qp *rdma_counter_get_qp(struct ib_device *dev, u32 qp_num)
407 struct rdma_restrack_entry *res = NULL;
408 struct ib_qp *qp = NULL;
410 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_QP, qp_num);
414 if (!rdma_is_visible_in_pid_ns(res))
417 qp = container_of(res, struct ib_qp, res);
418 if (qp->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
424 rdma_restrack_put(&qp->res);
428 static int rdma_counter_bind_qp_manual(struct rdma_counter *counter,
431 if ((counter->device != qp->device) || (counter->port != qp->port))
434 return __rdma_counter_bind_qp(counter, qp);
437 static struct rdma_counter *rdma_get_counter_by_id(struct ib_device *dev,
440 struct rdma_restrack_entry *res;
441 struct rdma_counter *counter;
443 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_COUNTER, counter_id);
447 if (!rdma_is_visible_in_pid_ns(res)) {
448 rdma_restrack_put(res);
452 counter = container_of(res, struct rdma_counter, res);
453 kref_get(&counter->kref);
454 rdma_restrack_put(res);
460 * rdma_counter_bind_qpn() - Bind QP @qp_num to counter @counter_id
462 int rdma_counter_bind_qpn(struct ib_device *dev, u8 port,
463 u32 qp_num, u32 counter_id)
465 struct rdma_counter *counter;
469 qp = rdma_counter_get_qp(dev, qp_num);
473 counter = rdma_get_counter_by_id(dev, counter_id);
479 if (counter->res.task != qp->res.task) {
484 ret = rdma_counter_bind_qp_manual(counter, qp);
488 rdma_restrack_put(&qp->res);
492 kref_put(&counter->kref, counter_release);
494 rdma_restrack_put(&qp->res);
499 * rdma_counter_bind_qpn_alloc() - Alloc a counter and bind QP @qp_num to it
500 * The id of new counter is returned in @counter_id
502 int rdma_counter_bind_qpn_alloc(struct ib_device *dev, u8 port,
503 u32 qp_num, u32 *counter_id)
505 struct rdma_counter *counter;
509 if (!rdma_is_port_valid(dev, port))
512 qp = rdma_counter_get_qp(dev, qp_num);
516 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
521 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_MANUAL);
527 ret = rdma_counter_bind_qp_manual(counter, qp);
532 *counter_id = counter->id;
534 rdma_counter_res_add(counter, qp);
536 rdma_restrack_put(&qp->res);
540 rdma_counter_free(counter);
542 rdma_restrack_put(&qp->res);
547 * rdma_counter_unbind_qpn() - Unbind QP @qp_num from a counter
549 int rdma_counter_unbind_qpn(struct ib_device *dev, u8 port,
550 u32 qp_num, u32 counter_id)
552 struct rdma_port_counter *port_counter;
556 if (!rdma_is_port_valid(dev, port))
559 qp = rdma_counter_get_qp(dev, qp_num);
563 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
568 port_counter = &dev->port_data[port].port_counter;
569 if (!qp->counter || qp->counter->id != counter_id ||
570 port_counter->mode.mode != RDMA_COUNTER_MODE_MANUAL) {
575 ret = rdma_counter_unbind_qp(qp, false);
578 rdma_restrack_put(&qp->res);
582 int rdma_counter_get_mode(struct ib_device *dev, u8 port,
583 enum rdma_nl_counter_mode *mode,
584 enum rdma_nl_counter_mask *mask)
586 struct rdma_port_counter *port_counter;
588 port_counter = &dev->port_data[port].port_counter;
589 *mode = port_counter->mode.mode;
590 *mask = port_counter->mode.mask;
595 void rdma_counter_init(struct ib_device *dev)
597 struct rdma_port_counter *port_counter;
603 rdma_for_each_port(dev, port) {
604 port_counter = &dev->port_data[port].port_counter;
605 port_counter->mode.mode = RDMA_COUNTER_MODE_NONE;
606 mutex_init(&port_counter->lock);
608 if (!dev->ops.alloc_hw_stats)
611 port_counter->hstats = dev->ops.alloc_hw_stats(dev, port);
612 if (!port_counter->hstats)
619 rdma_for_each_port(dev, port) {
620 port_counter = &dev->port_data[port].port_counter;
621 kfree(port_counter->hstats);
622 port_counter->hstats = NULL;
628 void rdma_counter_release(struct ib_device *dev)
630 struct rdma_port_counter *port_counter;
633 rdma_for_each_port(dev, port) {
634 port_counter = &dev->port_data[port].port_counter;
635 kfree(port_counter->hstats);