counters.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. #include "core_priv.h"
  8. #include "restrack.h"
  9. #define ALL_AUTO_MODE_MASKS (RDMA_COUNTER_MASK_QP_TYPE | RDMA_COUNTER_MASK_PID)
  10. static int __counter_set_mode(struct rdma_counter_mode *curr,
  11. enum rdma_nl_counter_mode new_mode,
  12. enum rdma_nl_counter_mask new_mask)
  13. {
  14. if ((new_mode == RDMA_COUNTER_MODE_AUTO) &&
  15. ((new_mask & (~ALL_AUTO_MODE_MASKS)) ||
  16. (curr->mode != RDMA_COUNTER_MODE_NONE)))
  17. return -EINVAL;
  18. curr->mode = new_mode;
  19. curr->mask = new_mask;
  20. return 0;
  21. }
  22. /**
  23. * rdma_counter_set_auto_mode() - Turn on/off per-port auto mode
  24. *
  25. * When @on is true, the @mask must be set; When @on is false, it goes
  26. * into manual mode if there's any counter, so that the user is able to
  27. * manually access them.
  28. */
  29. int rdma_counter_set_auto_mode(struct ib_device *dev, u8 port,
  30. bool on, enum rdma_nl_counter_mask mask)
  31. {
  32. struct rdma_port_counter *port_counter;
  33. int ret;
  34. port_counter = &dev->port_data[port].port_counter;
  35. if (!port_counter->hstats)
  36. return -EOPNOTSUPP;
  37. mutex_lock(&port_counter->lock);
  38. if (on) {
  39. ret = __counter_set_mode(&port_counter->mode,
  40. RDMA_COUNTER_MODE_AUTO, mask);
  41. } else {
  42. if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO) {
  43. ret = -EINVAL;
  44. goto out;
  45. }
  46. if (port_counter->num_counters)
  47. ret = __counter_set_mode(&port_counter->mode,
  48. RDMA_COUNTER_MODE_MANUAL, 0);
  49. else
  50. ret = __counter_set_mode(&port_counter->mode,
  51. RDMA_COUNTER_MODE_NONE, 0);
  52. }
  53. out:
  54. mutex_unlock(&port_counter->lock);
  55. return ret;
  56. }
  57. static struct rdma_counter *rdma_counter_alloc(struct ib_device *dev, u8 port,
  58. enum rdma_nl_counter_mode mode)
  59. {
  60. struct rdma_port_counter *port_counter;
  61. struct rdma_counter *counter;
  62. int ret;
  63. if (!dev->ops.counter_dealloc || !dev->ops.counter_alloc_stats)
  64. return NULL;
  65. counter = kzalloc(sizeof(*counter), GFP_KERNEL);
  66. if (!counter)
  67. return NULL;
  68. counter->device = dev;
  69. counter->port = port;
  70. rdma_restrack_new(&counter->res, RDMA_RESTRACK_COUNTER);
  71. counter->stats = dev->ops.counter_alloc_stats(counter);
  72. if (!counter->stats)
  73. goto err_stats;
  74. port_counter = &dev->port_data[port].port_counter;
  75. mutex_lock(&port_counter->lock);
  76. if (mode == RDMA_COUNTER_MODE_MANUAL) {
  77. ret = __counter_set_mode(&port_counter->mode,
  78. RDMA_COUNTER_MODE_MANUAL, 0);
  79. if (ret)
  80. goto err_mode;
  81. }
  82. port_counter->num_counters++;
  83. mutex_unlock(&port_counter->lock);
  84. counter->mode.mode = mode;
  85. kref_init(&counter->kref);
  86. mutex_init(&counter->lock);
  87. return counter;
  88. err_mode:
  89. mutex_unlock(&port_counter->lock);
  90. kfree(counter->stats);
  91. err_stats:
  92. rdma_restrack_put(&counter->res);
  93. kfree(counter);
  94. return NULL;
  95. }
  96. static void rdma_counter_free(struct rdma_counter *counter)
  97. {
  98. struct rdma_port_counter *port_counter;
  99. port_counter = &counter->device->port_data[counter->port].port_counter;
  100. mutex_lock(&port_counter->lock);
  101. port_counter->num_counters--;
  102. if (!port_counter->num_counters &&
  103. (port_counter->mode.mode == RDMA_COUNTER_MODE_MANUAL))
  104. __counter_set_mode(&port_counter->mode, RDMA_COUNTER_MODE_NONE,
  105. 0);
  106. mutex_unlock(&port_counter->lock);
  107. rdma_restrack_del(&counter->res);
  108. kfree(counter->stats);
  109. kfree(counter);
  110. }
  111. static void auto_mode_init_counter(struct rdma_counter *counter,
  112. const struct ib_qp *qp,
  113. enum rdma_nl_counter_mask new_mask)
  114. {
  115. struct auto_mode_param *param = &counter->mode.param;
  116. counter->mode.mode = RDMA_COUNTER_MODE_AUTO;
  117. counter->mode.mask = new_mask;
  118. if (new_mask & RDMA_COUNTER_MASK_QP_TYPE)
  119. param->qp_type = qp->qp_type;
  120. }
  121. static bool auto_mode_match(struct ib_qp *qp, struct rdma_counter *counter,
  122. enum rdma_nl_counter_mask auto_mask)
  123. {
  124. struct auto_mode_param *param = &counter->mode.param;
  125. bool match = true;
  126. if (auto_mask & RDMA_COUNTER_MASK_QP_TYPE)
  127. match &= (param->qp_type == qp->qp_type);
  128. if (auto_mask & RDMA_COUNTER_MASK_PID)
  129. match &= (task_pid_nr(counter->res.task) ==
  130. task_pid_nr(qp->res.task));
  131. return match;
  132. }
  133. static int __rdma_counter_bind_qp(struct rdma_counter *counter,
  134. struct ib_qp *qp)
  135. {
  136. int ret;
  137. if (qp->counter)
  138. return -EINVAL;
  139. if (!qp->device->ops.counter_bind_qp)
  140. return -EOPNOTSUPP;
  141. mutex_lock(&counter->lock);
  142. ret = qp->device->ops.counter_bind_qp(counter, qp);
  143. mutex_unlock(&counter->lock);
  144. return ret;
  145. }
  146. static int __rdma_counter_unbind_qp(struct ib_qp *qp)
  147. {
  148. struct rdma_counter *counter = qp->counter;
  149. int ret;
  150. if (!qp->device->ops.counter_unbind_qp)
  151. return -EOPNOTSUPP;
  152. mutex_lock(&counter->lock);
  153. ret = qp->device->ops.counter_unbind_qp(qp);
  154. mutex_unlock(&counter->lock);
  155. return ret;
  156. }
  157. static void counter_history_stat_update(struct rdma_counter *counter)
  158. {
  159. struct ib_device *dev = counter->device;
  160. struct rdma_port_counter *port_counter;
  161. int i;
  162. port_counter = &dev->port_data[counter->port].port_counter;
  163. if (!port_counter->hstats)
  164. return;
  165. rdma_counter_query_stats(counter);
  166. for (i = 0; i < counter->stats->num_counters; i++)
  167. port_counter->hstats->value[i] += counter->stats->value[i];
  168. }
  169. /**
  170. * rdma_get_counter_auto_mode - Find the counter that @qp should be bound
  171. * with in auto mode
  172. *
  173. * Return: The counter (with ref-count increased) if found
  174. */
  175. static struct rdma_counter *rdma_get_counter_auto_mode(struct ib_qp *qp,
  176. u8 port)
  177. {
  178. struct rdma_port_counter *port_counter;
  179. struct rdma_counter *counter = NULL;
  180. struct ib_device *dev = qp->device;
  181. struct rdma_restrack_entry *res;
  182. struct rdma_restrack_root *rt;
  183. unsigned long id = 0;
  184. port_counter = &dev->port_data[port].port_counter;
  185. rt = &dev->res[RDMA_RESTRACK_COUNTER];
  186. xa_lock(&rt->xa);
  187. xa_for_each(&rt->xa, id, res) {
  188. counter = container_of(res, struct rdma_counter, res);
  189. if ((counter->device != qp->device) || (counter->port != port))
  190. goto next;
  191. if (auto_mode_match(qp, counter, port_counter->mode.mask))
  192. break;
  193. next:
  194. counter = NULL;
  195. }
  196. if (counter && !kref_get_unless_zero(&counter->kref))
  197. counter = NULL;
  198. xa_unlock(&rt->xa);
  199. return counter;
  200. }
  201. static void rdma_counter_res_add(struct rdma_counter *counter,
  202. struct ib_qp *qp)
  203. {
  204. rdma_restrack_parent_name(&counter->res, &qp->res);
  205. rdma_restrack_add(&counter->res);
  206. }
  207. static void counter_release(struct kref *kref)
  208. {
  209. struct rdma_counter *counter;
  210. counter = container_of(kref, struct rdma_counter, kref);
  211. counter_history_stat_update(counter);
  212. counter->device->ops.counter_dealloc(counter);
  213. rdma_counter_free(counter);
  214. }
  215. /**
  216. * rdma_counter_bind_qp_auto - Check and bind the QP to a counter base on
  217. * the auto-mode rule
  218. */
  219. int rdma_counter_bind_qp_auto(struct ib_qp *qp, u8 port)
  220. {
  221. struct rdma_port_counter *port_counter;
  222. struct ib_device *dev = qp->device;
  223. struct rdma_counter *counter;
  224. int ret;
  225. if (!qp->res.valid || rdma_is_kernel_res(&qp->res))
  226. return 0;
  227. if (!rdma_is_port_valid(dev, port))
  228. return -EINVAL;
  229. port_counter = &dev->port_data[port].port_counter;
  230. if (port_counter->mode.mode != RDMA_COUNTER_MODE_AUTO)
  231. return 0;
  232. counter = rdma_get_counter_auto_mode(qp, port);
  233. if (counter) {
  234. ret = __rdma_counter_bind_qp(counter, qp);
  235. if (ret) {
  236. kref_put(&counter->kref, counter_release);
  237. return ret;
  238. }
  239. } else {
  240. counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_AUTO);
  241. if (!counter)
  242. return -ENOMEM;
  243. auto_mode_init_counter(counter, qp, port_counter->mode.mask);
  244. ret = __rdma_counter_bind_qp(counter, qp);
  245. if (ret) {
  246. rdma_counter_free(counter);
  247. return ret;
  248. }
  249. rdma_counter_res_add(counter, qp);
  250. }
  251. return 0;
  252. }
  253. /**
  254. * rdma_counter_unbind_qp - Unbind a qp from a counter
  255. * @force:
  256. * true - Decrease the counter ref-count anyway (e.g., qp destroy)
  257. */
  258. int rdma_counter_unbind_qp(struct ib_qp *qp, bool force)
  259. {
  260. struct rdma_counter *counter = qp->counter;
  261. int ret;
  262. if (!counter)
  263. return -EINVAL;
  264. ret = __rdma_counter_unbind_qp(qp);
  265. if (ret && !force)
  266. return ret;
  267. kref_put(&counter->kref, counter_release);
  268. return 0;
  269. }
  270. int rdma_counter_query_stats(struct rdma_counter *counter)
  271. {
  272. struct ib_device *dev = counter->device;
  273. int ret;
  274. if (!dev->ops.counter_update_stats)
  275. return -EINVAL;
  276. mutex_lock(&counter->lock);
  277. ret = dev->ops.counter_update_stats(counter);
  278. mutex_unlock(&counter->lock);
  279. return ret;
  280. }
  281. static u64 get_running_counters_hwstat_sum(struct ib_device *dev,
  282. u8 port, u32 index)
  283. {
  284. struct rdma_restrack_entry *res;
  285. struct rdma_restrack_root *rt;
  286. struct rdma_counter *counter;
  287. unsigned long id = 0;
  288. u64 sum = 0;
  289. rt = &dev->res[RDMA_RESTRACK_COUNTER];
  290. xa_lock(&rt->xa);
  291. xa_for_each(&rt->xa, id, res) {
  292. if (!rdma_restrack_get(res))
  293. continue;
  294. xa_unlock(&rt->xa);
  295. counter = container_of(res, struct rdma_counter, res);
  296. if ((counter->device != dev) || (counter->port != port) ||
  297. rdma_counter_query_stats(counter))
  298. goto next;
  299. sum += counter->stats->value[index];
  300. next:
  301. xa_lock(&rt->xa);
  302. rdma_restrack_put(res);
  303. }
  304. xa_unlock(&rt->xa);
  305. return sum;
  306. }
  307. /**
  308. * rdma_counter_get_hwstat_value() - Get the sum value of all counters on a
  309. * specific port, including the running ones and history data
  310. */
  311. u64 rdma_counter_get_hwstat_value(struct ib_device *dev, u8 port, u32 index)
  312. {
  313. struct rdma_port_counter *port_counter;
  314. u64 sum;
  315. port_counter = &dev->port_data[port].port_counter;
  316. if (!port_counter->hstats)
  317. return 0;
  318. sum = get_running_counters_hwstat_sum(dev, port, index);
  319. sum += port_counter->hstats->value[index];
  320. return sum;
  321. }
  322. static struct ib_qp *rdma_counter_get_qp(struct ib_device *dev, u32 qp_num)
  323. {
  324. struct rdma_restrack_entry *res = NULL;
  325. struct ib_qp *qp = NULL;
  326. res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_QP, qp_num);
  327. if (IS_ERR(res))
  328. return NULL;
  329. qp = container_of(res, struct ib_qp, res);
  330. if (qp->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
  331. goto err;
  332. return qp;
  333. err:
  334. rdma_restrack_put(res);
  335. return NULL;
  336. }
  337. static int rdma_counter_bind_qp_manual(struct rdma_counter *counter,
  338. struct ib_qp *qp)
  339. {
  340. if ((counter->device != qp->device) || (counter->port != qp->port))
  341. return -EINVAL;
  342. return __rdma_counter_bind_qp(counter, qp);
  343. }
  344. static struct rdma_counter *rdma_get_counter_by_id(struct ib_device *dev,
  345. u32 counter_id)
  346. {
  347. struct rdma_restrack_entry *res;
  348. struct rdma_counter *counter;
  349. res = rdma_restrack_get_byid(dev, RDMA_RESTRACK_COUNTER, counter_id);
  350. if (IS_ERR(res))
  351. return NULL;
  352. counter = container_of(res, struct rdma_counter, res);
  353. kref_get(&counter->kref);
  354. rdma_restrack_put(res);
  355. return counter;
  356. }
  357. /**
  358. * rdma_counter_bind_qpn() - Bind QP @qp_num to counter @counter_id
  359. */
  360. int rdma_counter_bind_qpn(struct ib_device *dev, u8 port,
  361. u32 qp_num, u32 counter_id)
  362. {
  363. struct rdma_port_counter *port_counter;
  364. struct rdma_counter *counter;
  365. struct ib_qp *qp;
  366. int ret;
  367. port_counter = &dev->port_data[port].port_counter;
  368. if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
  369. return -EINVAL;
  370. qp = rdma_counter_get_qp(dev, qp_num);
  371. if (!qp)
  372. return -ENOENT;
  373. counter = rdma_get_counter_by_id(dev, counter_id);
  374. if (!counter) {
  375. ret = -ENOENT;
  376. goto err;
  377. }
  378. if (rdma_is_kernel_res(&counter->res) != rdma_is_kernel_res(&qp->res)) {
  379. ret = -EINVAL;
  380. goto err_task;
  381. }
  382. ret = rdma_counter_bind_qp_manual(counter, qp);
  383. if (ret)
  384. goto err_task;
  385. rdma_restrack_put(&qp->res);
  386. return 0;
  387. err_task:
  388. kref_put(&counter->kref, counter_release);
  389. err:
  390. rdma_restrack_put(&qp->res);
  391. return ret;
  392. }
  393. /**
  394. * rdma_counter_bind_qpn_alloc() - Alloc a counter and bind QP @qp_num to it
  395. * The id of new counter is returned in @counter_id
  396. */
  397. int rdma_counter_bind_qpn_alloc(struct ib_device *dev, u8 port,
  398. u32 qp_num, u32 *counter_id)
  399. {
  400. struct rdma_port_counter *port_counter;
  401. struct rdma_counter *counter;
  402. struct ib_qp *qp;
  403. int ret;
  404. if (!rdma_is_port_valid(dev, port))
  405. return -EINVAL;
  406. port_counter = &dev->port_data[port].port_counter;
  407. if (!port_counter->hstats)
  408. return -EOPNOTSUPP;
  409. if (port_counter->mode.mode == RDMA_COUNTER_MODE_AUTO)
  410. return -EINVAL;
  411. qp = rdma_counter_get_qp(dev, qp_num);
  412. if (!qp)
  413. return -ENOENT;
  414. if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
  415. ret = -EINVAL;
  416. goto err;
  417. }
  418. counter = rdma_counter_alloc(dev, port, RDMA_COUNTER_MODE_MANUAL);
  419. if (!counter) {
  420. ret = -ENOMEM;
  421. goto err;
  422. }
  423. ret = rdma_counter_bind_qp_manual(counter, qp);
  424. if (ret)
  425. goto err_bind;
  426. if (counter_id)
  427. *counter_id = counter->id;
  428. rdma_counter_res_add(counter, qp);
  429. rdma_restrack_put(&qp->res);
  430. return ret;
  431. err_bind:
  432. rdma_counter_free(counter);
  433. err:
  434. rdma_restrack_put(&qp->res);
  435. return ret;
  436. }
  437. /**
  438. * rdma_counter_unbind_qpn() - Unbind QP @qp_num from a counter
  439. */
  440. int rdma_counter_unbind_qpn(struct ib_device *dev, u8 port,
  441. u32 qp_num, u32 counter_id)
  442. {
  443. struct rdma_port_counter *port_counter;
  444. struct ib_qp *qp;
  445. int ret;
  446. if (!rdma_is_port_valid(dev, port))
  447. return -EINVAL;
  448. qp = rdma_counter_get_qp(dev, qp_num);
  449. if (!qp)
  450. return -ENOENT;
  451. if (rdma_is_port_valid(dev, qp->port) && (qp->port != port)) {
  452. ret = -EINVAL;
  453. goto out;
  454. }
  455. port_counter = &dev->port_data[port].port_counter;
  456. if (!qp->counter || qp->counter->id != counter_id ||
  457. port_counter->mode.mode != RDMA_COUNTER_MODE_MANUAL) {
  458. ret = -EINVAL;
  459. goto out;
  460. }
  461. ret = rdma_counter_unbind_qp(qp, false);
  462. out:
  463. rdma_restrack_put(&qp->res);
  464. return ret;
  465. }
  466. int rdma_counter_get_mode(struct ib_device *dev, u8 port,
  467. enum rdma_nl_counter_mode *mode,
  468. enum rdma_nl_counter_mask *mask)
  469. {
  470. struct rdma_port_counter *port_counter;
  471. port_counter = &dev->port_data[port].port_counter;
  472. *mode = port_counter->mode.mode;
  473. *mask = port_counter->mode.mask;
  474. return 0;
  475. }
  476. void rdma_counter_init(struct ib_device *dev)
  477. {
  478. struct rdma_port_counter *port_counter;
  479. u32 port, i;
  480. if (!dev->port_data)
  481. return;
  482. rdma_for_each_port(dev, port) {
  483. port_counter = &dev->port_data[port].port_counter;
  484. port_counter->mode.mode = RDMA_COUNTER_MODE_NONE;
  485. mutex_init(&port_counter->lock);
  486. if (!dev->ops.alloc_hw_stats)
  487. continue;
  488. port_counter->hstats = dev->ops.alloc_hw_stats(dev, port);
  489. if (!port_counter->hstats)
  490. goto fail;
  491. }
  492. return;
  493. fail:
  494. for (i = port; i >= rdma_start_port(dev); i--) {
  495. port_counter = &dev->port_data[port].port_counter;
  496. kfree(port_counter->hstats);
  497. port_counter->hstats = NULL;
  498. mutex_destroy(&port_counter->lock);
  499. }
  500. }
  501. void rdma_counter_release(struct ib_device *dev)
  502. {
  503. struct rdma_port_counter *port_counter;
  504. u32 port;
  505. rdma_for_each_port(dev, port) {
  506. port_counter = &dev->port_data[port].port_counter;
  507. kfree(port_counter->hstats);
  508. mutex_destroy(&port_counter->lock);
  509. }
  510. }