1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3  * Copyright (c) 2019 Mellanox Technologies. All rights reserved.
4  */
5 #include <rdma/ib_verbs.h>
6 #include <rdma/rdma_counter.h>
7 
8 #include "core_priv.h"
9 #include "restrack.h"
10 
11 #define ALL_AUTO_MODE_MASKS (RDMA_COUNTER_MASK_QP_TYPE | RDMA_COUNTER_MASK_PID)
12 
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)
16 {
17 	if ((new_mode == RDMA_COUNTER_MODE_AUTO) &&
18 	    ((new_mask & (~ALL_AUTO_MODE_MASKS)) ||
19 	     (curr->mode != RDMA_COUNTER_MODE_NONE)))
20 		return -EINVAL;
21 
22 	curr->mode = new_mode;
23 	curr->mask = new_mask;
24 	return 0;
25 }
26 
27 /**
28  * rdma_counter_set_auto_mode() - Turn on/off per-port auto mode
29  *
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.
33  */
34 int rdma_counter_set_auto_mode(struct ib_device *dev, u8 port,
35 			       bool on, enum rdma_nl_counter_mask mask)
36 {
37 	struct rdma_port_counter *port_counter;
38 	int ret;
39 
40 	port_counter = &dev->port_data[port].port_counter;
41 	if (!port_counter->hstats)
42 		return -EOPNOTSUPP;
43 
44 	mutex_lock(&port_counter->lock);
45 	if (on) {
46 		ret = __counter_set_mode(&port_counter->mode,
47 					 RDMA_COUNTER_MODE_AUTO, mask);
48 	} else {
49 		if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO) {
50 			ret = -EINVAL;
51 			goto out;
52 		}
53 
54 		if (port_counter->num_counters)
55 			ret = __counter_set_mode(&port_counter->mode,
56 						 RDMA_COUNTER_MODE_MANUAL, 0);
57 		else
58 			ret = __counter_set_mode(&port_counter->mode,
59 						 RDMA_COUNTER_MODE_NONE, 0);
60 	}
61 
62 out:
63 	mutex_unlock(&port_counter->lock);
64 	return ret;
65 }
66 
67 static struct rdma_counter *rdma_counter_alloc(struct ib_device *dev, u8 port,
68 					       enum rdma_nl_counter_mode mode)
69 {
70 	struct rdma_port_counter *port_counter;
71 	struct rdma_counter *counter;
72 	int ret;
73 
74 	if (!dev->ops.counter_dealloc || !dev->ops.counter_alloc_stats)
75 		return NULL;
76 
77 	counter = kzalloc(sizeof(*counter), GFP_KERNEL);
78 	if (!counter)
79 		return NULL;
80 
81 	counter->device    = dev;
82 	counter->port      = port;
83 	counter->res.type  = RDMA_RESTRACK_COUNTER;
84 	counter->stats     = dev->ops.counter_alloc_stats(counter);
85 	if (!counter->stats)
86 		goto err_stats;
87 
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);
93 		if (ret)
94 			goto err_mode;
95 	}
96 
97 	port_counter->num_counters++;
98 	mutex_unlock(&port_counter->lock);
99 
100 	counter->mode.mode = mode;
101 	kref_init(&counter->kref);
102 	mutex_init(&counter->lock);
103 
104 	return counter;
105 
106 err_mode:
107 	mutex_unlock(&port_counter->lock);
108 	kfree(counter->stats);
109 err_stats:
110 	kfree(counter);
111 	return NULL;
112 }
113 
114 static void rdma_counter_free(struct rdma_counter *counter)
115 {
116 	struct rdma_port_counter *port_counter;
117 
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,
124 				   0);
125 
126 	mutex_unlock(&port_counter->lock);
127 
128 	rdma_restrack_del(&counter->res);
129 	kfree(counter->stats);
130 	kfree(counter);
131 }
132 
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)
136 {
137 	struct auto_mode_param *param = &counter->mode.param;
138 
139 	counter->mode.mode = RDMA_COUNTER_MODE_AUTO;
140 	counter->mode.mask = new_mask;
141 
142 	if (new_mask & RDMA_COUNTER_MASK_QP_TYPE)
143 		param->qp_type = qp->qp_type;
144 }
145 
146 static bool auto_mode_match(struct ib_qp *qp, struct rdma_counter *counter,
147 			    enum rdma_nl_counter_mask auto_mask)
148 {
149 	struct auto_mode_param *param = &counter->mode.param;
150 	bool match = true;
151 
152 	if (auto_mask & RDMA_COUNTER_MASK_QP_TYPE)
153 		match &= (param->qp_type == qp->qp_type);
154 
155 	if (auto_mask & RDMA_COUNTER_MASK_PID)
156 		match &= (task_pid_nr(counter->res.task) ==
157 			  task_pid_nr(qp->res.task));
158 
159 	return match;
160 }
161 
162 static int __rdma_counter_bind_qp(struct rdma_counter *counter,
163 				  struct ib_qp *qp)
164 {
165 	int ret;
166 
167 	if (qp->counter)
168 		return -EINVAL;
169 
170 	if (!qp->device->ops.counter_bind_qp)
171 		return -EOPNOTSUPP;
172 
173 	mutex_lock(&counter->lock);
174 	ret = qp->device->ops.counter_bind_qp(counter, qp);
175 	mutex_unlock(&counter->lock);
176 
177 	return ret;
178 }
179 
180 static int __rdma_counter_unbind_qp(struct ib_qp *qp)
181 {
182 	struct rdma_counter *counter = qp->counter;
183 	int ret;
184 
185 	if (!qp->device->ops.counter_unbind_qp)
186 		return -EOPNOTSUPP;
187 
188 	mutex_lock(&counter->lock);
189 	ret = qp->device->ops.counter_unbind_qp(qp);
190 	mutex_unlock(&counter->lock);
191 
192 	return ret;
193 }
194 
195 static void counter_history_stat_update(const struct rdma_counter *counter)
196 {
197 	struct ib_device *dev = counter->device;
198 	struct rdma_port_counter *port_counter;
199 	int i;
200 
201 	port_counter = &dev->port_data[counter->port].port_counter;
202 	if (!port_counter->hstats)
203 		return;
204 
205 	for (i = 0; i < counter->stats->num_counters; i++)
206 		port_counter->hstats->value[i] += counter->stats->value[i];
207 }
208 
209 /**
210  * rdma_get_counter_auto_mode - Find the counter that @qp should be bound
211  *     with in auto mode
212  *
213  * Return: The counter (with ref-count increased) if found
214  */
215 static struct rdma_counter *rdma_get_counter_auto_mode(struct ib_qp *qp,
216 						       u8 port)
217 {
218 	struct rdma_port_counter *port_counter;
219 	struct rdma_counter *counter = NULL;
220 	struct ib_device *dev = qp->device;
221 	struct rdma_restrack_entry *res;
222 	struct rdma_restrack_root *rt;
223 	unsigned long id = 0;
224 
225 	port_counter = &dev->port_data[port].port_counter;
226 	rt = &dev->res[RDMA_RESTRACK_COUNTER];
227 	xa_lock(&rt->xa);
228 	xa_for_each(&rt->xa, id, res) {
229 		counter = container_of(res, struct rdma_counter, res);
230 		if ((counter->device != qp->device) || (counter->port != port))
231 			goto next;
232 
233 		if (auto_mode_match(qp, counter, port_counter->mode.mask))
234 			break;
235 next:
236 		counter = NULL;
237 	}
238 
239 	if (counter && !kref_get_unless_zero(&counter->kref))
240 		counter = NULL;
241 
242 	xa_unlock(&rt->xa);
243 	return counter;
244 }
245 
246 static void rdma_counter_res_add(struct rdma_counter *counter,
247 				 struct ib_qp *qp)
248 {
249 	if (rdma_is_kernel_res(&qp->res)) {
250 		rdma_restrack_set_task(&counter->res, qp->res.kern_name);
251 		rdma_restrack_kadd(&counter->res);
252 	} else {
253 		rdma_restrack_attach_task(&counter->res, qp->res.task);
254 		rdma_restrack_uadd(&counter->res);
255 	}
256 }
257 
258 static void counter_release(struct kref *kref)
259 {
260 	struct rdma_counter *counter;
261 
262 	counter = container_of(kref, struct rdma_counter, kref);
263 	counter_history_stat_update(counter);
264 	counter->device->ops.counter_dealloc(counter);
265 	rdma_counter_free(counter);
266 }
267 
268 /**
269  * rdma_counter_bind_qp_auto - Check and bind the QP to a counter base on
270  *   the auto-mode rule
271  */
272 int rdma_counter_bind_qp_auto(struct ib_qp *qp, u8 port)
273 {
274 	struct rdma_port_counter *port_counter;
275 	struct ib_device *dev = qp->device;
276 	struct rdma_counter *counter;
277 	int ret;
278 
279 	if (!qp->res.valid || rdma_is_kernel_res(&qp->res))
280 		return 0;
281 
282 	if (!rdma_is_port_valid(dev, port))
283 		return -EINVAL;
284 
285 	port_counter = &dev->port_data[port].port_counter;
286 	if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO)
287 		return 0;
288 
289 	counter = rdma_get_counter_auto_mode(qp, port);
290 	if (counter) {
291 		ret = __rdma_counter_bind_qp(counter, qp);
292 		if (ret) {
293 			kref_put(&counter->kref, counter_release);
294 			return ret;
295 		}
296 	} else {
297 		counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_AUTO);
298 		if (!counter)
299 			return -ENOMEM;
300 
301 		auto_mode_init_counter(counter, qp, port_counter->mode.mask);
302 
303 		ret = __rdma_counter_bind_qp(counter, qp);
304 		if (ret) {
305 			rdma_counter_free(counter);
306 			return ret;
307 		}
308 
309 		rdma_counter_res_add(counter, qp);
310 	}
311 
312 	return 0;
313 }
314 
315 /**
316  * rdma_counter_unbind_qp - Unbind a qp from a counter
317  * @force:
318  *   true - Decrease the counter ref-count anyway (e.g., qp destroy)
319  */
320 int rdma_counter_unbind_qp(struct ib_qp *qp, bool force)
321 {
322 	struct rdma_counter *counter = qp->counter;
323 	int ret;
324 
325 	if (!counter)
326 		return -EINVAL;
327 
328 	ret = __rdma_counter_unbind_qp(qp);
329 	if (ret && !force)
330 		return ret;
331 
332 	kref_put(&counter->kref, counter_release);
333 	return 0;
334 }
335 
336 int rdma_counter_query_stats(struct rdma_counter *counter)
337 {
338 	struct ib_device *dev = counter->device;
339 	int ret;
340 
341 	if (!dev->ops.counter_update_stats)
342 		return -EINVAL;
343 
344 	mutex_lock(&counter->lock);
345 	ret = dev->ops.counter_update_stats(counter);
346 	mutex_unlock(&counter->lock);
347 
348 	return ret;
349 }
350 
351 static u64 get_running_counters_hwstat_sum(struct ib_device *dev,
352 					   u8 port, u32 index)
353 {
354 	struct rdma_restrack_entry *res;
355 	struct rdma_restrack_root *rt;
356 	struct rdma_counter *counter;
357 	unsigned long id = 0;
358 	u64 sum = 0;
359 
360 	rt = &dev->res[RDMA_RESTRACK_COUNTER];
361 	xa_lock(&rt->xa);
362 	xa_for_each(&rt->xa, id, res) {
363 		if (!rdma_restrack_get(res))
364 			continue;
365 
366 		xa_unlock(&rt->xa);
367 
368 		counter = container_of(res, struct rdma_counter, res);
369 		if ((counter->device != dev) || (counter->port != port) ||
370 		    rdma_counter_query_stats(counter))
371 			goto next;
372 
373 		sum += counter->stats->value[index];
374 
375 next:
376 		xa_lock(&rt->xa);
377 		rdma_restrack_put(res);
378 	}
379 
380 	xa_unlock(&rt->xa);
381 	return sum;
382 }
383 
384 /**
385  * rdma_counter_get_hwstat_value() - Get the sum value of all counters on a
386  *   specific port, including the running ones and history data
387  */
388 u64 rdma_counter_get_hwstat_value(struct ib_device *dev, u8 port, u32 index)
389 {
390 	struct rdma_port_counter *port_counter;
391 	u64 sum;
392 
393 	port_counter = &dev->port_data[port].port_counter;
394 	if (!port_counter->hstats)
395 		return 0;
396 
397 	sum = get_running_counters_hwstat_sum(dev, port, index);
398 	sum += port_counter->hstats->value[index];
399 
400 	return sum;
401 }
402 
403 static struct ib_qp *rdma_counter_get_qp(struct ib_device *dev, u32 qp_num)
404 {
405 	struct rdma_restrack_entry *res = NULL;
406 	struct ib_qp *qp = NULL;
407 
408 	res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_QP, qp_num);
409 	if (IS_ERR(res))
410 		return NULL;
411 
412 	qp = container_of(res, struct ib_qp, res);
413 	if (qp->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
414 		goto err;
415 
416 	return qp;
417 
418 err:
419 	rdma_restrack_put(res);
420 	return NULL;
421 }
422 
423 static int rdma_counter_bind_qp_manual(struct rdma_counter *counter,
424 				       struct ib_qp *qp)
425 {
426 	if ((counter->device != qp->device) || (counter->port != qp->port))
427 		return -EINVAL;
428 
429 	return __rdma_counter_bind_qp(counter, qp);
430 }
431 
432 static struct rdma_counter *rdma_get_counter_by_id(struct ib_device *dev,
433 						   u32 counter_id)
434 {
435 	struct rdma_restrack_entry *res;
436 	struct rdma_counter *counter;
437 
438 	res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_COUNTER, counter_id);
439 	if (IS_ERR(res))
440 		return NULL;
441 
442 	counter = container_of(res, struct rdma_counter, res);
443 	kref_get(&counter->kref);
444 	rdma_restrack_put(res);
445 
446 	return counter;
447 }
448 
449 /**
450  * rdma_counter_bind_qpn() - Bind QP @qp_num to counter @counter_id
451  */
452 int rdma_counter_bind_qpn(struct ib_device *dev, u8 port,
453 			  u32 qp_num, u32 counter_id)
454 {
455 	struct rdma_port_counter *port_counter;
456 	struct rdma_counter *counter;
457 	struct ib_qp *qp;
458 	int ret;
459 
460 	port_counter = &dev->port_data[port].port_counter;
461 	if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
462 		return -EINVAL;
463 
464 	qp = rdma_counter_get_qp(dev, qp_num);
465 	if (!qp)
466 		return -ENOENT;
467 
468 	counter = rdma_get_counter_by_id(dev, counter_id);
469 	if (!counter) {
470 		ret = -ENOENT;
471 		goto err;
472 	}
473 
474 	if (counter->res.task != qp->res.task) {
475 		ret = -EINVAL;
476 		goto err_task;
477 	}
478 
479 	ret = rdma_counter_bind_qp_manual(counter, qp);
480 	if (ret)
481 		goto err_task;
482 
483 	rdma_restrack_put(&qp->res);
484 	return 0;
485 
486 err_task:
487 	kref_put(&counter->kref, counter_release);
488 err:
489 	rdma_restrack_put(&qp->res);
490 	return ret;
491 }
492 
493 /**
494  * rdma_counter_bind_qpn_alloc() - Alloc a counter and bind QP @qp_num to it
495  *   The id of new counter is returned in @counter_id
496  */
497 int rdma_counter_bind_qpn_alloc(struct ib_device *dev, u8 port,
498 				u32 qp_num, u32 *counter_id)
499 {
500 	struct rdma_port_counter *port_counter;
501 	struct rdma_counter *counter;
502 	struct ib_qp *qp;
503 	int ret;
504 
505 	if (!rdma_is_port_valid(dev, port))
506 		return -EINVAL;
507 
508 	port_counter = &dev->port_data[port].port_counter;
509 	if (!port_counter->hstats)
510 		return -EOPNOTSUPP;
511 
512 	if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
513 		return -EINVAL;
514 
515 	qp = rdma_counter_get_qp(dev, qp_num);
516 	if (!qp)
517 		return -ENOENT;
518 
519 	if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
520 		ret = -EINVAL;
521 		goto err;
522 	}
523 
524 	counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_MANUAL);
525 	if (!counter) {
526 		ret = -ENOMEM;
527 		goto err;
528 	}
529 
530 	ret = rdma_counter_bind_qp_manual(counter, qp);
531 	if (ret)
532 		goto err_bind;
533 
534 	if (counter_id)
535 		*counter_id = counter->id;
536 
537 	rdma_counter_res_add(counter, qp);
538 
539 	rdma_restrack_put(&qp->res);
540 	return ret;
541 
542 err_bind:
543 	rdma_counter_free(counter);
544 err:
545 	rdma_restrack_put(&qp->res);
546 	return ret;
547 }
548 
549 /**
550  * rdma_counter_unbind_qpn() - Unbind QP @qp_num from a counter
551  */
552 int rdma_counter_unbind_qpn(struct ib_device *dev, u8 port,
553 			    u32 qp_num, u32 counter_id)
554 {
555 	struct rdma_port_counter *port_counter;
556 	struct ib_qp *qp;
557 	int ret;
558 
559 	if (!rdma_is_port_valid(dev, port))
560 		return -EINVAL;
561 
562 	qp = rdma_counter_get_qp(dev, qp_num);
563 	if (!qp)
564 		return -ENOENT;
565 
566 	if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
567 		ret = -EINVAL;
568 		goto out;
569 	}
570 
571 	port_counter = &dev->port_data[port].port_counter;
572 	if (!qp->counter || qp->counter->id != counter_id ||
573 	    port_counter->mode.mode != RDMA_COUNTER_MODE_MANUAL) {
574 		ret = -EINVAL;
575 		goto out;
576 	}
577 
578 	ret = rdma_counter_unbind_qp(qp, false);
579 
580 out:
581 	rdma_restrack_put(&qp->res);
582 	return ret;
583 }
584 
585 int rdma_counter_get_mode(struct ib_device *dev, u8 port,
586 			  enum rdma_nl_counter_mode *mode,
587 			  enum rdma_nl_counter_mask *mask)
588 {
589 	struct rdma_port_counter *port_counter;
590 
591 	port_counter = &dev->port_data[port].port_counter;
592 	*mode = port_counter->mode.mode;
593 	*mask = port_counter->mode.mask;
594 
595 	return 0;
596 }
597 
598 void rdma_counter_init(struct ib_device *dev)
599 {
600 	struct rdma_port_counter *port_counter;
601 	u32 port, i;
602 
603 	if (!dev->port_data)
604 		return;
605 
606 	rdma_for_each_port(dev, port) {
607 		port_counter = &dev->port_data[port].port_counter;
608 		port_counter->mode.mode = RDMA_COUNTER_MODE_NONE;
609 		mutex_init(&port_counter->lock);
610 
611 		if (!dev->ops.alloc_hw_stats)
612 			continue;
613 
614 		port_counter->hstats = dev->ops.alloc_hw_stats(dev, port);
615 		if (!port_counter->hstats)
616 			goto fail;
617 	}
618 
619 	return;
620 
621 fail:
622 	for (i = port; i >= rdma_start_port(dev); i--) {
623 		port_counter = &dev->port_data[port].port_counter;
624 		kfree(port_counter->hstats);
625 		port_counter->hstats = NULL;
626 		mutex_destroy(&port_counter->lock);
627 	}
628 }
629 
630 void rdma_counter_release(struct ib_device *dev)
631 {
632 	struct rdma_port_counter *port_counter;
633 	u32 port;
634 
635 	rdma_for_each_port(dev, port) {
636 		port_counter = &dev->port_data[port].port_counter;
637 		kfree(port_counter->hstats);
638 		mutex_destroy(&port_counter->lock);
639 	}
640 }
641