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