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;
152 if (rdma_is_kernel_res(&counter->res) != rdma_is_kernel_res(&qp->res))
155 /* Ensure that counter belong to right PID */
156 if (!rdma_is_kernel_res(&counter->res) &&
157 !rdma_is_kernel_res(&qp->res) &&
158 (task_pid_vnr(counter->res.task) != current->pid))
161 if (auto_mask & RDMA_COUNTER_MASK_QP_TYPE)
162 match &= (param->qp_type == qp->qp_type);
167 static int __rdma_counter_bind_qp(struct rdma_counter *counter,
175 if (!qp->device->ops.counter_bind_qp)
178 mutex_lock(&counter->lock);
179 ret = qp->device->ops.counter_bind_qp(counter, qp);
180 mutex_unlock(&counter->lock);
185 static int __rdma_counter_unbind_qp(struct ib_qp *qp)
187 struct rdma_counter *counter = qp->counter;
190 if (!qp->device->ops.counter_unbind_qp)
193 mutex_lock(&counter->lock);
194 ret = qp->device->ops.counter_unbind_qp(qp);
195 mutex_unlock(&counter->lock);
200 static void counter_history_stat_update(const struct rdma_counter *counter)
202 struct ib_device *dev = counter->device;
203 struct rdma_port_counter *port_counter;
206 port_counter = &dev->port_data[counter->port].port_counter;
207 if (!port_counter->hstats)
210 for (i = 0; i < counter->stats->num_counters; i++)
211 port_counter->hstats->value[i] += counter->stats->value[i];
215 * rdma_get_counter_auto_mode - Find the counter that @qp should be bound
218 * Return: The counter (with ref-count increased) if found
220 static struct rdma_counter *rdma_get_counter_auto_mode(struct ib_qp *qp,
223 struct rdma_port_counter *port_counter;
224 struct rdma_counter *counter = NULL;
225 struct ib_device *dev = qp->device;
226 struct rdma_restrack_entry *res;
227 struct rdma_restrack_root *rt;
228 unsigned long id = 0;
230 port_counter = &dev->port_data[port].port_counter;
231 rt = &dev->res[RDMA_RESTRACK_COUNTER];
233 xa_for_each(&rt->xa, id, res) {
234 if (!rdma_is_visible_in_pid_ns(res))
237 counter = container_of(res, struct rdma_counter, res);
238 if ((counter->device != qp->device) || (counter->port != port))
241 if (auto_mode_match(qp, counter, port_counter->mode.mask))
247 if (counter && !kref_get_unless_zero(&counter->kref))
254 static void rdma_counter_res_add(struct rdma_counter *counter,
257 if (rdma_is_kernel_res(&qp->res)) {
258 rdma_restrack_set_task(&counter->res, qp->res.kern_name);
259 rdma_restrack_kadd(&counter->res);
261 rdma_restrack_attach_task(&counter->res, qp->res.task);
262 rdma_restrack_uadd(&counter->res);
266 static void counter_release(struct kref *kref)
268 struct rdma_counter *counter;
270 counter = container_of(kref, struct rdma_counter, kref);
271 counter_history_stat_update(counter);
272 counter->device->ops.counter_dealloc(counter);
273 rdma_counter_free(counter);
277 * rdma_counter_bind_qp_auto - Check and bind the QP to a counter base on
280 int rdma_counter_bind_qp_auto(struct ib_qp *qp, u8 port)
282 struct rdma_port_counter *port_counter;
283 struct ib_device *dev = qp->device;
284 struct rdma_counter *counter;
287 if (!rdma_is_port_valid(dev, port))
290 port_counter = &dev->port_data[port].port_counter;
291 if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO)
294 counter = rdma_get_counter_auto_mode(qp, port);
296 ret = __rdma_counter_bind_qp(counter, qp);
298 kref_put(&counter->kref, counter_release);
302 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_AUTO);
306 auto_mode_init_counter(counter, qp, port_counter->mode.mask);
308 ret = __rdma_counter_bind_qp(counter, qp);
310 rdma_counter_free(counter);
314 rdma_counter_res_add(counter, qp);
321 * rdma_counter_unbind_qp - Unbind a qp from a counter
323 * true - Decrease the counter ref-count anyway (e.g., qp destroy)
325 int rdma_counter_unbind_qp(struct ib_qp *qp, bool force)
327 struct rdma_counter *counter = qp->counter;
333 ret = __rdma_counter_unbind_qp(qp);
337 kref_put(&counter->kref, counter_release);
341 int rdma_counter_query_stats(struct rdma_counter *counter)
343 struct ib_device *dev = counter->device;
346 if (!dev->ops.counter_update_stats)
349 mutex_lock(&counter->lock);
350 ret = dev->ops.counter_update_stats(counter);
351 mutex_unlock(&counter->lock);
356 static u64 get_running_counters_hwstat_sum(struct ib_device *dev,
359 struct rdma_restrack_entry *res;
360 struct rdma_restrack_root *rt;
361 struct rdma_counter *counter;
362 unsigned long id = 0;
365 rt = &dev->res[RDMA_RESTRACK_COUNTER];
367 xa_for_each(&rt->xa, id, res) {
368 if (!rdma_restrack_get(res))
373 counter = container_of(res, struct rdma_counter, res);
374 if ((counter->device != dev) || (counter->port != port) ||
375 rdma_counter_query_stats(counter))
378 sum += counter->stats->value[index];
382 rdma_restrack_put(res);
390 * rdma_counter_get_hwstat_value() - Get the sum value of all counters on a
391 * specific port, including the running ones and history data
393 u64 rdma_counter_get_hwstat_value(struct ib_device *dev, u8 port, u32 index)
395 struct rdma_port_counter *port_counter;
398 port_counter = &dev->port_data[port].port_counter;
399 if (!port_counter->hstats)
402 sum = get_running_counters_hwstat_sum(dev, port, index);
403 sum += port_counter->hstats->value[index];
408 static struct ib_qp *rdma_counter_get_qp(struct ib_device *dev, u32 qp_num)
410 struct rdma_restrack_entry *res = NULL;
411 struct ib_qp *qp = NULL;
413 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_QP, qp_num);
417 if (!rdma_is_visible_in_pid_ns(res))
420 qp = container_of(res, struct ib_qp, res);
421 if (qp->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
427 rdma_restrack_put(&qp->res);
431 static int rdma_counter_bind_qp_manual(struct rdma_counter *counter,
434 if ((counter->device != qp->device) || (counter->port != qp->port))
437 return __rdma_counter_bind_qp(counter, qp);
440 static struct rdma_counter *rdma_get_counter_by_id(struct ib_device *dev,
443 struct rdma_restrack_entry *res;
444 struct rdma_counter *counter;
446 res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_COUNTER, counter_id);
450 if (!rdma_is_visible_in_pid_ns(res)) {
451 rdma_restrack_put(res);
455 counter = container_of(res, struct rdma_counter, res);
456 kref_get(&counter->kref);
457 rdma_restrack_put(res);
463 * rdma_counter_bind_qpn() - Bind QP @qp_num to counter @counter_id
465 int rdma_counter_bind_qpn(struct ib_device *dev, u8 port,
466 u32 qp_num, u32 counter_id)
468 struct rdma_counter *counter;
472 qp = rdma_counter_get_qp(dev, qp_num);
476 counter = rdma_get_counter_by_id(dev, counter_id);
482 if (counter->res.task != qp->res.task) {
487 ret = rdma_counter_bind_qp_manual(counter, qp);
491 rdma_restrack_put(&qp->res);
495 kref_put(&counter->kref, counter_release);
497 rdma_restrack_put(&qp->res);
502 * rdma_counter_bind_qpn_alloc() - Alloc a counter and bind QP @qp_num to it
503 * The id of new counter is returned in @counter_id
505 int rdma_counter_bind_qpn_alloc(struct ib_device *dev, u8 port,
506 u32 qp_num, u32 *counter_id)
508 struct rdma_counter *counter;
512 if (!rdma_is_port_valid(dev, port))
515 if (!dev->port_data[port].port_counter.hstats)
518 qp = rdma_counter_get_qp(dev, qp_num);
522 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
527 counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_MANUAL);
533 ret = rdma_counter_bind_qp_manual(counter, qp);
538 *counter_id = counter->id;
540 rdma_counter_res_add(counter, qp);
542 rdma_restrack_put(&qp->res);
546 rdma_counter_free(counter);
548 rdma_restrack_put(&qp->res);
553 * rdma_counter_unbind_qpn() - Unbind QP @qp_num from a counter
555 int rdma_counter_unbind_qpn(struct ib_device *dev, u8 port,
556 u32 qp_num, u32 counter_id)
558 struct rdma_port_counter *port_counter;
562 if (!rdma_is_port_valid(dev, port))
565 qp = rdma_counter_get_qp(dev, qp_num);
569 if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
574 port_counter = &dev->port_data[port].port_counter;
575 if (!qp->counter || qp->counter->id != counter_id ||
576 port_counter->mode.mode != RDMA_COUNTER_MODE_MANUAL) {
581 ret = rdma_counter_unbind_qp(qp, false);
584 rdma_restrack_put(&qp->res);
588 int rdma_counter_get_mode(struct ib_device *dev, u8 port,
589 enum rdma_nl_counter_mode *mode,
590 enum rdma_nl_counter_mask *mask)
592 struct rdma_port_counter *port_counter;
594 port_counter = &dev->port_data[port].port_counter;
595 *mode = port_counter->mode.mode;
596 *mask = port_counter->mode.mask;
601 void rdma_counter_init(struct ib_device *dev)
603 struct rdma_port_counter *port_counter;
609 rdma_for_each_port(dev, port) {
610 port_counter = &dev->port_data[port].port_counter;
611 port_counter->mode.mode = RDMA_COUNTER_MODE_NONE;
612 mutex_init(&port_counter->lock);
614 if (!dev->ops.alloc_hw_stats)
617 port_counter->hstats = dev->ops.alloc_hw_stats(dev, port);
618 if (!port_counter->hstats)
625 for (i = port; i >= rdma_start_port(dev); i--) {
626 port_counter = &dev->port_data[port].port_counter;
627 kfree(port_counter->hstats);
628 port_counter->hstats = NULL;
629 mutex_destroy(&port_counter->lock);
633 void rdma_counter_release(struct ib_device *dev)
635 struct rdma_port_counter *port_counter;
638 rdma_for_each_port(dev, port) {
639 port_counter = &dev->port_data[port].port_counter;
640 kfree(port_counter->hstats);
641 mutex_destroy(&port_counter->lock);