counters.c 15 KB

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