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 if (!port_counter->hstats)
44 mutex_lock(&port_counter->lock);
46 ret = __counter_set_mode(&port_counter->mode,
47 RDMA_COUNTER_MODE_AUTO, mask);
49 if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO) {
54 if (port_counter->num_counters)
55 ret = __counter_set_mode(&port_counter->mode,
56 RDMA_COUNTER_MODE_MANUAL, 0);
58 ret = __counter_set_mode(&port_counter->mode,
59 RDMA_COUNTER_MODE_NONE, 0);
63 mutex_unlock(&port_counter->lock);
67 static struct rdma_counter *rdma_counter_alloc(struct ib_device *dev, u8 port,
68 enum rdma_nl_counter_mode mode)
70 struct rdma_port_counter *port_counter;
71 struct rdma_counter *counter;
74 if (!dev->ops.counter_dealloc || !dev->ops.counter_alloc_stats)
77 counter = kzalloc(sizeof(*counter), GFP_KERNEL);
81 counter->device = dev;
83 counter->res.type = RDMA_RESTRACK_COUNTER;
84 counter->stats = dev->ops.counter_alloc_stats(counter);
88 port_counter = &dev->port_data[port].port_counter;
89 mutex_lock(&port_counter->lock);
90 if (mode == RDMA_COUNTER_MODE_MANUAL) {
91 ret = __counter_set_mode(&port_counter->mode,
92 RDMA_COUNTER_MODE_MANUAL, 0);
97 port_counter->num_counters++;
98 mutex_unlock(&port_counter->lock);
100 counter->mode.mode = mode;
101 kref_init(&counter->kref);
102 mutex_init(&counter->lock);
107 mutex_unlock(&port_counter->lock);
108 kfree(counter->stats);
114 static void rdma_counter_free(struct rdma_counter *counter)
116 struct rdma_port_counter *port_counter;
118 port_counter = &counter->device->port_data[counter->port].port_counter;
119 mutex_lock(&port_counter->lock);
120 port_counter->num_counters--;
121 if (!port_counter->num_counters &&
122 (port_counter->mode.mode == RDMA_COUNTER_MODE_MANUAL))
123 __counter_set_mode(&port_counter->mode, RDMA_COUNTER_MODE_NONE,
126 mutex_unlock(&port_counter->lock);
128 rdma_restrack_del(&counter->res);
129 kfree(counter->stats);
133 static void auto_mode_init_counter(struct rdma_counter *counter,
134 const struct ib_qp *qp,
135 enum rdma_nl_counter_mask new_mask)
137 struct auto_mode_param *param = &counter->mode.param;
139 counter->mode.mode = RDMA_COUNTER_MODE_AUTO;
140 counter->mode.mask = new_mask;
142 if (new_mask & RDMA_COUNTER_MASK_QP_TYPE)
143 param->qp_type = qp->qp_type;
146 static bool auto_mode_match(struct ib_qp *qp, struct rdma_counter *counter,
147 enum rdma_nl_counter_mask auto_mask)
149 struct auto_mode_param *param = &counter->mode.param;
153 * Ensure that counter belongs to the right PID. This operation can
154 * race with user space which kills the process and leaves QP and
157 * It is not a big deal because exitted task will leave both QP and
158 * counter in the same bucket of zombie process. Just ensure that
159 * process is still alive before procedding.
162 if (task_pid_nr(counter->res.task) != task_pid_nr(qp->res.task) ||
163 !task_pid_nr(qp->res.task))
166 if (auto_mask & RDMA_COUNTER_MASK_QP_TYPE)
167 match &= (param->qp_type == qp->qp_type);
172 static int __rdma_counter_bind_qp(struct rdma_counter *counter,
180 if (!qp->device->ops.counter_bind_qp)
183 mutex_lock(&counter->lock);
184 ret = qp->device->ops.counter_bind_qp(counter, qp);
185 mutex_unlock(&counter->lock);
190 static int __rdma_counter_unbind_qp(struct ib_qp *qp)
192 struct rdma_counter *counter = qp->counter;
195 if (!qp->device->ops.counter_unbind_qp)
198 mutex_lock(&counter->lock);
199 ret = qp->device->ops.counter_unbind_qp(qp);
200 mutex_unlock(&counter->lock);
205 static void counter_history_stat_update(const struct rdma_counter *counter)
207 struct ib_device *dev = counter->device;
208 struct rdma_port_counter *port_counter;
211 port_counter = &dev->port_data[counter->port].port_counter;
212 if (!port_counter->hstats)
215 for (i = 0; i < counter->stats->num_counters; i++)
216 port_counter->hstats->value[i] += counter->stats->value[i];
220 * rdma_get_counter_auto_mode - Find the counter that @qp should be bound
223 * Return: The counter (with ref-count increased) if found
225 static struct rdma_counter *rdma_get_counter_auto_mode(struct ib_qp *qp,
228 struct rdma_port_counter *port_counter;
229 struct rdma_counter *counter = NULL;
230 struct ib_device *dev = qp->device;
231 struct rdma_restrack_entry *res;
232 struct rdma_restrack_root *rt;
233 unsigned long id = 0;
235 port_counter = &dev->port_data[port].port_counter;
236 rt = &dev->res[RDMA_RESTRACK_COUNTER];
238 xa_for_each(&rt->xa, id, res) {
239 counter = container_of(res, struct rdma_counter, res);
240 if ((counter->device != qp->device) || (counter->port != port))
243 if (auto_mode_match(qp, counter, port_counter->mode.mask))
249 if (counter && !kref_get_unless_zero(&counter->kref))
256 static void rdma_counter_res_add(struct rdma_counter *counter,
259 if (rdma_is_kernel_res(&qp->res)) {
260 rdma_restrack_set_task(&counter->res, qp->res.kern_name);
261 rdma_restrack_kadd(&counter->res);
263 rdma_restrack_attach_task(&counter->res, qp->res.task);
264 rdma_restrack_uadd(&counter->res);
268 static void counter_release(struct kref *kref)
270 struct rdma_counter *counter;
272 counter = container_of(kref, struct rdma_counter, kref);
273 counter_history_stat_update(counter);
274 counter->device->ops.counter_dealloc(counter);
275 rdma_counter_free(counter);
279 * rdma_counter_bind_qp_auto - Check and bind the QP to a counter base on
282 int rdma_counter_bind_qp_auto(struct ib_qp *qp, u8 port)
284 struct rdma_port_counter *port_counter;
285 struct ib_device *dev = qp->device;
286 struct rdma_counter *counter;
289 if (!rdma_is_port_valid(dev, port))
292 port_counter = &dev->port_data[port].port_counter;
293 if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO)
296 counter = rdma_get_counter_auto_mode(qp, port);
298 ret = __rdma_counter_bind_qp(counter, qp);
300 kref_put(&counter->kref, counter_release);
304 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_AUTO);
308 auto_mode_init_counter(counter, qp, port_counter->mode.mask);
310 ret = __rdma_counter_bind_qp(counter, qp);
312 rdma_counter_free(counter);
316 rdma_counter_res_add(counter, qp);
323 * rdma_counter_unbind_qp - Unbind a qp from a counter
325 * true - Decrease the counter ref-count anyway (e.g., qp destroy)
327 int rdma_counter_unbind_qp(struct ib_qp *qp, bool force)
329 struct rdma_counter *counter = qp->counter;
335 ret = __rdma_counter_unbind_qp(qp);
339 kref_put(&counter->kref, counter_release);
343 int rdma_counter_query_stats(struct rdma_counter *counter)
345 struct ib_device *dev = counter->device;
348 if (!dev->ops.counter_update_stats)
351 mutex_lock(&counter->lock);
352 ret = dev->ops.counter_update_stats(counter);
353 mutex_unlock(&counter->lock);
358 static u64 get_running_counters_hwstat_sum(struct ib_device *dev,
361 struct rdma_restrack_entry *res;
362 struct rdma_restrack_root *rt;
363 struct rdma_counter *counter;
364 unsigned long id = 0;
367 rt = &dev->res[RDMA_RESTRACK_COUNTER];
369 xa_for_each(&rt->xa, id, res) {
370 if (!rdma_restrack_get(res))
375 counter = container_of(res, struct rdma_counter, res);
376 if ((counter->device != dev) || (counter->port != port) ||
377 rdma_counter_query_stats(counter))
380 sum += counter->stats->value[index];
384 rdma_restrack_put(res);
392 * rdma_counter_get_hwstat_value() - Get the sum value of all counters on a
393 * specific port, including the running ones and history data
395 u64 rdma_counter_get_hwstat_value(struct ib_device *dev, u8 port, u32 index)
397 struct rdma_port_counter *port_counter;
400 port_counter = &dev->port_data[port].port_counter;
401 if (!port_counter->hstats)
404 sum = get_running_counters_hwstat_sum(dev, port, index);
405 sum += port_counter->hstats->value[index];
410 static struct ib_qp *rdma_counter_get_qp(struct ib_device *dev, u32 qp_num)
412 struct rdma_restrack_entry *res = NULL;
413 struct ib_qp *qp = NULL;
415 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_QP, qp_num);
419 qp = container_of(res, struct ib_qp, res);
420 if (qp->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
426 rdma_restrack_put(res);
430 static int rdma_counter_bind_qp_manual(struct rdma_counter *counter,
433 if ((counter->device != qp->device) || (counter->port != qp->port))
436 return __rdma_counter_bind_qp(counter, qp);
439 static struct rdma_counter *rdma_get_counter_by_id(struct ib_device *dev,
442 struct rdma_restrack_entry *res;
443 struct rdma_counter *counter;
445 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_COUNTER, counter_id);
449 counter = container_of(res, struct rdma_counter, res);
450 kref_get(&counter->kref);
451 rdma_restrack_put(res);
457 * rdma_counter_bind_qpn() - Bind QP @qp_num to counter @counter_id
459 int rdma_counter_bind_qpn(struct ib_device *dev, u8 port,
460 u32 qp_num, u32 counter_id)
462 struct rdma_port_counter *port_counter;
463 struct rdma_counter *counter;
467 port_counter = &dev->port_data[port].port_counter;
468 if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
471 qp = rdma_counter_get_qp(dev, qp_num);
475 counter = rdma_get_counter_by_id(dev, counter_id);
481 if (counter->res.task != qp->res.task) {
486 ret = rdma_counter_bind_qp_manual(counter, qp);
490 rdma_restrack_put(&qp->res);
494 kref_put(&counter->kref, counter_release);
496 rdma_restrack_put(&qp->res);
501 * rdma_counter_bind_qpn_alloc() - Alloc a counter and bind QP @qp_num to it
502 * The id of new counter is returned in @counter_id
504 int rdma_counter_bind_qpn_alloc(struct ib_device *dev, u8 port,
505 u32 qp_num, u32 *counter_id)
507 struct rdma_port_counter *port_counter;
508 struct rdma_counter *counter;
512 if (!rdma_is_port_valid(dev, port))
515 port_counter = &dev->port_data[port].port_counter;
516 if (!port_counter->hstats)
519 if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
522 qp = rdma_counter_get_qp(dev, qp_num);
526 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
531 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_MANUAL);
537 ret = rdma_counter_bind_qp_manual(counter, qp);
542 *counter_id = counter->id;
544 rdma_counter_res_add(counter, qp);
546 rdma_restrack_put(&qp->res);
550 rdma_counter_free(counter);
552 rdma_restrack_put(&qp->res);
557 * rdma_counter_unbind_qpn() - Unbind QP @qp_num from a counter
559 int rdma_counter_unbind_qpn(struct ib_device *dev, u8 port,
560 u32 qp_num, u32 counter_id)
562 struct rdma_port_counter *port_counter;
566 if (!rdma_is_port_valid(dev, port))
569 qp = rdma_counter_get_qp(dev, qp_num);
573 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
578 port_counter = &dev->port_data[port].port_counter;
579 if (!qp->counter || qp->counter->id != counter_id ||
580 port_counter->mode.mode != RDMA_COUNTER_MODE_MANUAL) {
585 ret = rdma_counter_unbind_qp(qp, false);
588 rdma_restrack_put(&qp->res);
592 int rdma_counter_get_mode(struct ib_device *dev, u8 port,
593 enum rdma_nl_counter_mode *mode,
594 enum rdma_nl_counter_mask *mask)
596 struct rdma_port_counter *port_counter;
598 port_counter = &dev->port_data[port].port_counter;
599 *mode = port_counter->mode.mode;
600 *mask = port_counter->mode.mask;
605 void rdma_counter_init(struct ib_device *dev)
607 struct rdma_port_counter *port_counter;
613 rdma_for_each_port(dev, port) {
614 port_counter = &dev->port_data[port].port_counter;
615 port_counter->mode.mode = RDMA_COUNTER_MODE_NONE;
616 mutex_init(&port_counter->lock);
618 if (!dev->ops.alloc_hw_stats)
621 port_counter->hstats = dev->ops.alloc_hw_stats(dev, port);
622 if (!port_counter->hstats)
629 for (i = port; i >= rdma_start_port(dev); i--) {
630 port_counter = &dev->port_data[port].port_counter;
631 kfree(port_counter->hstats);
632 port_counter->hstats = NULL;
633 mutex_destroy(&port_counter->lock);
637 void rdma_counter_release(struct ib_device *dev)
639 struct rdma_port_counter *port_counter;
642 rdma_for_each_port(dev, port) {
643 port_counter = &dev->port_data[port].port_counter;
644 kfree(port_counter->hstats);
645 mutex_destroy(&port_counter->lock);