cq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 HGST, a Western Digital Company.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/slab.h>
  7. #include <rdma/ib_verbs.h>
  8. #include "core_priv.h"
  9. #include <trace/events/rdma_core.h>
  10. /* Max size for shared CQ, may require tuning */
  11. #define IB_MAX_SHARED_CQ_SZ 4096U
  12. /* # of WCs to poll for with a single call to ib_poll_cq */
  13. #define IB_POLL_BATCH 16
  14. #define IB_POLL_BATCH_DIRECT 8
  15. /* # of WCs to iterate over before yielding */
  16. #define IB_POLL_BUDGET_IRQ 256
  17. #define IB_POLL_BUDGET_WORKQUEUE 65536
  18. #define IB_POLL_FLAGS \
  19. (IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS)
  20. static const struct dim_cq_moder
  21. rdma_dim_prof[RDMA_DIM_PARAMS_NUM_PROFILES] = {
  22. {1, 0, 1, 0},
  23. {1, 0, 4, 0},
  24. {2, 0, 4, 0},
  25. {2, 0, 8, 0},
  26. {4, 0, 8, 0},
  27. {16, 0, 8, 0},
  28. {16, 0, 16, 0},
  29. {32, 0, 16, 0},
  30. {32, 0, 32, 0},
  31. };
  32. static void ib_cq_rdma_dim_work(struct work_struct *w)
  33. {
  34. struct dim *dim = container_of(w, struct dim, work);
  35. struct ib_cq *cq = dim->priv;
  36. u16 usec = rdma_dim_prof[dim->profile_ix].usec;
  37. u16 comps = rdma_dim_prof[dim->profile_ix].comps;
  38. dim->state = DIM_START_MEASURE;
  39. trace_cq_modify(cq, comps, usec);
  40. cq->device->ops.modify_cq(cq, comps, usec);
  41. }
  42. static void rdma_dim_init(struct ib_cq *cq)
  43. {
  44. struct dim *dim;
  45. if (!cq->device->ops.modify_cq || !cq->device->use_cq_dim ||
  46. cq->poll_ctx == IB_POLL_DIRECT)
  47. return;
  48. dim = kzalloc_obj(struct dim);
  49. if (!dim)
  50. return;
  51. dim->state = DIM_START_MEASURE;
  52. dim->tune_state = DIM_GOING_RIGHT;
  53. dim->profile_ix = RDMA_DIM_START_PROFILE;
  54. dim->priv = cq;
  55. cq->dim = dim;
  56. INIT_WORK(&dim->work, ib_cq_rdma_dim_work);
  57. }
  58. static void rdma_dim_destroy(struct ib_cq *cq)
  59. {
  60. if (!cq->dim)
  61. return;
  62. cancel_work_sync(&cq->dim->work);
  63. kfree(cq->dim);
  64. }
  65. static int __poll_cq(struct ib_cq *cq, int num_entries, struct ib_wc *wc)
  66. {
  67. int rc;
  68. rc = ib_poll_cq(cq, num_entries, wc);
  69. trace_cq_poll(cq, num_entries, rc);
  70. return rc;
  71. }
  72. static int __ib_process_cq(struct ib_cq *cq, int budget, struct ib_wc *wcs,
  73. int batch)
  74. {
  75. int i, n, completed = 0;
  76. trace_cq_process(cq);
  77. /*
  78. * budget might be (-1) if the caller does not
  79. * want to bound this call, thus we need unsigned
  80. * minimum here.
  81. */
  82. while ((n = __poll_cq(cq, min_t(u32, batch,
  83. budget - completed), wcs)) > 0) {
  84. for (i = 0; i < n; i++) {
  85. struct ib_wc *wc = &wcs[i];
  86. if (wc->wr_cqe)
  87. wc->wr_cqe->done(cq, wc);
  88. else
  89. WARN_ON_ONCE(wc->status == IB_WC_SUCCESS);
  90. }
  91. completed += n;
  92. if (n != batch || (budget != -1 && completed >= budget))
  93. break;
  94. }
  95. return completed;
  96. }
  97. /**
  98. * ib_process_cq_direct - process a CQ in caller context
  99. * @cq: CQ to process
  100. * @budget: number of CQEs to poll for
  101. *
  102. * This function is used to process all outstanding CQ entries.
  103. * It does not offload CQ processing to a different context and does
  104. * not ask for completion interrupts from the HCA.
  105. * Using direct processing on CQ with non IB_POLL_DIRECT type may trigger
  106. * concurrent processing.
  107. *
  108. * Note: do not pass -1 as %budget unless it is guaranteed that the number
  109. * of completions that will be processed is small.
  110. */
  111. int ib_process_cq_direct(struct ib_cq *cq, int budget)
  112. {
  113. struct ib_wc wcs[IB_POLL_BATCH_DIRECT];
  114. return __ib_process_cq(cq, budget, wcs, IB_POLL_BATCH_DIRECT);
  115. }
  116. EXPORT_SYMBOL(ib_process_cq_direct);
  117. static void ib_cq_completion_direct(struct ib_cq *cq, void *private)
  118. {
  119. WARN_ONCE(1, "got unsolicited completion for CQ 0x%p\n", cq);
  120. }
  121. static int ib_poll_handler(struct irq_poll *iop, int budget)
  122. {
  123. struct ib_cq *cq = container_of(iop, struct ib_cq, iop);
  124. struct dim *dim = cq->dim;
  125. int completed;
  126. completed = __ib_process_cq(cq, budget, cq->wc, IB_POLL_BATCH);
  127. if (completed < budget) {
  128. irq_poll_complete(&cq->iop);
  129. if (ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0) {
  130. trace_cq_reschedule(cq);
  131. irq_poll_sched(&cq->iop);
  132. }
  133. }
  134. if (dim)
  135. rdma_dim(dim, completed);
  136. return completed;
  137. }
  138. static void ib_cq_completion_softirq(struct ib_cq *cq, void *private)
  139. {
  140. trace_cq_schedule(cq);
  141. irq_poll_sched(&cq->iop);
  142. }
  143. static void ib_cq_poll_work(struct work_struct *work)
  144. {
  145. struct ib_cq *cq = container_of(work, struct ib_cq, work);
  146. int completed;
  147. completed = __ib_process_cq(cq, IB_POLL_BUDGET_WORKQUEUE, cq->wc,
  148. IB_POLL_BATCH);
  149. if (completed >= IB_POLL_BUDGET_WORKQUEUE ||
  150. ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
  151. queue_work(cq->comp_wq, &cq->work);
  152. else if (cq->dim)
  153. rdma_dim(cq->dim, completed);
  154. }
  155. static void ib_cq_completion_workqueue(struct ib_cq *cq, void *private)
  156. {
  157. trace_cq_schedule(cq);
  158. queue_work(cq->comp_wq, &cq->work);
  159. }
  160. /**
  161. * __ib_alloc_cq - allocate a completion queue
  162. * @dev: device to allocate the CQ for
  163. * @private: driver private data, accessible from cq->cq_context
  164. * @nr_cqe: number of CQEs to allocate
  165. * @comp_vector: HCA completion vectors for this CQ
  166. * @poll_ctx: context to poll the CQ from.
  167. * @caller: module owner name.
  168. *
  169. * This is the proper interface to allocate a CQ for in-kernel users. A
  170. * CQ allocated with this interface will automatically be polled from the
  171. * specified context. The ULP must use wr->wr_cqe instead of wr->wr_id
  172. * to use this CQ abstraction.
  173. */
  174. struct ib_cq *__ib_alloc_cq(struct ib_device *dev, void *private, int nr_cqe,
  175. int comp_vector, enum ib_poll_context poll_ctx,
  176. const char *caller)
  177. {
  178. struct ib_cq_init_attr cq_attr = {
  179. .cqe = nr_cqe,
  180. .comp_vector = comp_vector,
  181. };
  182. struct ib_cq *cq;
  183. int ret = -ENOMEM;
  184. cq = rdma_zalloc_drv_obj(dev, ib_cq);
  185. if (!cq)
  186. return ERR_PTR(ret);
  187. cq->device = dev;
  188. cq->cq_context = private;
  189. cq->poll_ctx = poll_ctx;
  190. atomic_set(&cq->usecnt, 0);
  191. cq->comp_vector = comp_vector;
  192. cq->wc = kmalloc_objs(*cq->wc, IB_POLL_BATCH);
  193. if (!cq->wc)
  194. goto out_free_cq;
  195. rdma_restrack_new(&cq->res, RDMA_RESTRACK_CQ);
  196. rdma_restrack_set_name(&cq->res, caller);
  197. ret = dev->ops.create_cq(cq, &cq_attr, NULL);
  198. if (ret)
  199. goto out_free_wc;
  200. rdma_dim_init(cq);
  201. switch (cq->poll_ctx) {
  202. case IB_POLL_DIRECT:
  203. cq->comp_handler = ib_cq_completion_direct;
  204. break;
  205. case IB_POLL_SOFTIRQ:
  206. cq->comp_handler = ib_cq_completion_softirq;
  207. irq_poll_init(&cq->iop, IB_POLL_BUDGET_IRQ, ib_poll_handler);
  208. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  209. break;
  210. case IB_POLL_WORKQUEUE:
  211. case IB_POLL_UNBOUND_WORKQUEUE:
  212. cq->comp_handler = ib_cq_completion_workqueue;
  213. INIT_WORK(&cq->work, ib_cq_poll_work);
  214. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  215. cq->comp_wq = (cq->poll_ctx == IB_POLL_WORKQUEUE) ?
  216. ib_comp_wq : ib_comp_unbound_wq;
  217. break;
  218. default:
  219. ret = -EINVAL;
  220. goto out_destroy_cq;
  221. }
  222. rdma_restrack_add(&cq->res);
  223. trace_cq_alloc(cq, nr_cqe, comp_vector, poll_ctx);
  224. return cq;
  225. out_destroy_cq:
  226. rdma_dim_destroy(cq);
  227. cq->device->ops.destroy_cq(cq, NULL);
  228. out_free_wc:
  229. rdma_restrack_put(&cq->res);
  230. kfree(cq->wc);
  231. out_free_cq:
  232. kfree(cq);
  233. trace_cq_alloc_error(nr_cqe, comp_vector, poll_ctx, ret);
  234. return ERR_PTR(ret);
  235. }
  236. EXPORT_SYMBOL(__ib_alloc_cq);
  237. /**
  238. * __ib_alloc_cq_any - allocate a completion queue
  239. * @dev: device to allocate the CQ for
  240. * @private: driver private data, accessible from cq->cq_context
  241. * @nr_cqe: number of CQEs to allocate
  242. * @poll_ctx: context to poll the CQ from
  243. * @caller: module owner name
  244. *
  245. * Attempt to spread ULP Completion Queues over each device's interrupt
  246. * vectors. A simple best-effort mechanism is used.
  247. */
  248. struct ib_cq *__ib_alloc_cq_any(struct ib_device *dev, void *private,
  249. int nr_cqe, enum ib_poll_context poll_ctx,
  250. const char *caller)
  251. {
  252. static atomic_t counter;
  253. int comp_vector = 0;
  254. if (dev->num_comp_vectors > 1)
  255. comp_vector =
  256. atomic_inc_return(&counter) %
  257. min_t(int, dev->num_comp_vectors, num_online_cpus());
  258. return __ib_alloc_cq(dev, private, nr_cqe, comp_vector, poll_ctx,
  259. caller);
  260. }
  261. EXPORT_SYMBOL(__ib_alloc_cq_any);
  262. /**
  263. * ib_free_cq - free a completion queue
  264. * @cq: completion queue to free.
  265. */
  266. void ib_free_cq(struct ib_cq *cq)
  267. {
  268. int ret = 0;
  269. if (WARN_ON_ONCE(atomic_read(&cq->usecnt)))
  270. return;
  271. if (WARN_ON_ONCE(cq->cqe_used))
  272. return;
  273. if (cq->device->ops.pre_destroy_cq) {
  274. ret = cq->device->ops.pre_destroy_cq(cq);
  275. WARN_ONCE(ret, "Disable of kernel CQ shouldn't fail");
  276. }
  277. switch (cq->poll_ctx) {
  278. case IB_POLL_DIRECT:
  279. break;
  280. case IB_POLL_SOFTIRQ:
  281. irq_poll_disable(&cq->iop);
  282. break;
  283. case IB_POLL_WORKQUEUE:
  284. case IB_POLL_UNBOUND_WORKQUEUE:
  285. cancel_work_sync(&cq->work);
  286. break;
  287. default:
  288. WARN_ON_ONCE(1);
  289. }
  290. rdma_dim_destroy(cq);
  291. trace_cq_free(cq);
  292. if (cq->device->ops.post_destroy_cq)
  293. cq->device->ops.post_destroy_cq(cq);
  294. else
  295. ret = cq->device->ops.destroy_cq(cq, NULL);
  296. WARN_ONCE(ret, "Destroy of kernel CQ shouldn't fail");
  297. rdma_restrack_del(&cq->res);
  298. kfree(cq->wc);
  299. kfree(cq);
  300. }
  301. EXPORT_SYMBOL(ib_free_cq);
  302. void ib_cq_pool_cleanup(struct ib_device *dev)
  303. {
  304. struct ib_cq *cq, *n;
  305. unsigned int i;
  306. for (i = 0; i < ARRAY_SIZE(dev->cq_pools); i++) {
  307. list_for_each_entry_safe(cq, n, &dev->cq_pools[i],
  308. pool_entry) {
  309. WARN_ON(cq->cqe_used);
  310. list_del(&cq->pool_entry);
  311. cq->shared = false;
  312. ib_free_cq(cq);
  313. }
  314. }
  315. }
  316. static int ib_alloc_cqs(struct ib_device *dev, unsigned int nr_cqes,
  317. enum ib_poll_context poll_ctx)
  318. {
  319. LIST_HEAD(tmp_list);
  320. unsigned int nr_cqs, i;
  321. struct ib_cq *cq, *n;
  322. int ret;
  323. if (poll_ctx > IB_POLL_LAST_POOL_TYPE) {
  324. WARN_ON_ONCE(poll_ctx > IB_POLL_LAST_POOL_TYPE);
  325. return -EINVAL;
  326. }
  327. /*
  328. * Allocate at least as many CQEs as requested, and otherwise
  329. * a reasonable batch size so that we can share CQs between
  330. * multiple users instead of allocating a larger number of CQs.
  331. */
  332. nr_cqes = min_t(unsigned int, dev->attrs.max_cqe,
  333. max(nr_cqes, IB_MAX_SHARED_CQ_SZ));
  334. nr_cqs = min_t(unsigned int, dev->num_comp_vectors, num_online_cpus());
  335. for (i = 0; i < nr_cqs; i++) {
  336. cq = ib_alloc_cq(dev, NULL, nr_cqes, i, poll_ctx);
  337. if (IS_ERR(cq)) {
  338. ret = PTR_ERR(cq);
  339. goto out_free_cqs;
  340. }
  341. cq->shared = true;
  342. list_add_tail(&cq->pool_entry, &tmp_list);
  343. }
  344. spin_lock_irq(&dev->cq_pools_lock);
  345. list_splice(&tmp_list, &dev->cq_pools[poll_ctx]);
  346. spin_unlock_irq(&dev->cq_pools_lock);
  347. return 0;
  348. out_free_cqs:
  349. list_for_each_entry_safe(cq, n, &tmp_list, pool_entry) {
  350. cq->shared = false;
  351. ib_free_cq(cq);
  352. }
  353. return ret;
  354. }
  355. /**
  356. * ib_cq_pool_get() - Find the least used completion queue that matches
  357. * a given cpu hint (or least used for wild card affinity) and fits
  358. * nr_cqe.
  359. * @dev: rdma device
  360. * @nr_cqe: number of needed cqe entries
  361. * @comp_vector_hint: completion vector hint (-1) for the driver to assign
  362. * a comp vector based on internal counter
  363. * @poll_ctx: cq polling context
  364. *
  365. * Finds a cq that satisfies @comp_vector_hint and @nr_cqe requirements and
  366. * claim entries in it for us. In case there is no available cq, allocate
  367. * a new cq with the requirements and add it to the device pool.
  368. * IB_POLL_DIRECT cannot be used for shared cqs so it is not a valid value
  369. * for @poll_ctx.
  370. */
  371. struct ib_cq *ib_cq_pool_get(struct ib_device *dev, unsigned int nr_cqe,
  372. int comp_vector_hint,
  373. enum ib_poll_context poll_ctx)
  374. {
  375. static unsigned int default_comp_vector;
  376. unsigned int vector, num_comp_vectors;
  377. struct ib_cq *cq, *found = NULL;
  378. int ret;
  379. if (poll_ctx > IB_POLL_LAST_POOL_TYPE) {
  380. WARN_ON_ONCE(poll_ctx > IB_POLL_LAST_POOL_TYPE);
  381. return ERR_PTR(-EINVAL);
  382. }
  383. num_comp_vectors =
  384. min_t(unsigned int, dev->num_comp_vectors, num_online_cpus());
  385. /* Project the affinty to the device completion vector range */
  386. if (comp_vector_hint < 0) {
  387. comp_vector_hint =
  388. (READ_ONCE(default_comp_vector) + 1) % num_comp_vectors;
  389. WRITE_ONCE(default_comp_vector, comp_vector_hint);
  390. }
  391. vector = comp_vector_hint % num_comp_vectors;
  392. /*
  393. * Find the least used CQ with correct affinity and
  394. * enough free CQ entries
  395. */
  396. while (!found) {
  397. spin_lock_irq(&dev->cq_pools_lock);
  398. list_for_each_entry(cq, &dev->cq_pools[poll_ctx],
  399. pool_entry) {
  400. /*
  401. * Check to see if we have found a CQ with the
  402. * correct completion vector
  403. */
  404. if (vector != cq->comp_vector)
  405. continue;
  406. if (cq->cqe_used + nr_cqe > cq->cqe)
  407. continue;
  408. found = cq;
  409. break;
  410. }
  411. if (found) {
  412. found->cqe_used += nr_cqe;
  413. spin_unlock_irq(&dev->cq_pools_lock);
  414. return found;
  415. }
  416. spin_unlock_irq(&dev->cq_pools_lock);
  417. /*
  418. * Didn't find a match or ran out of CQs in the device
  419. * pool, allocate a new array of CQs.
  420. */
  421. ret = ib_alloc_cqs(dev, nr_cqe, poll_ctx);
  422. if (ret)
  423. return ERR_PTR(ret);
  424. }
  425. return found;
  426. }
  427. EXPORT_SYMBOL(ib_cq_pool_get);
  428. /**
  429. * ib_cq_pool_put - Return a CQ taken from a shared pool.
  430. * @cq: The CQ to return.
  431. * @nr_cqe: The max number of cqes that the user had requested.
  432. */
  433. void ib_cq_pool_put(struct ib_cq *cq, unsigned int nr_cqe)
  434. {
  435. if (WARN_ON_ONCE(nr_cqe > cq->cqe_used))
  436. return;
  437. spin_lock_irq(&cq->device->cq_pools_lock);
  438. cq->cqe_used -= nr_cqe;
  439. spin_unlock_irq(&cq->device->cq_pools_lock);
  440. }
  441. EXPORT_SYMBOL(ib_cq_pool_put);