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