ib_recv.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/slab.h>
  35. #include <linux/pci.h>
  36. #include <linux/dma-mapping.h>
  37. #include <rdma/rdma_cm.h>
  38. #include "rds.h"
  39. #include "ib.h"
  40. static struct kmem_cache *rds_ib_incoming_slab;
  41. static struct kmem_cache *rds_ib_frag_slab;
  42. static atomic_t rds_ib_allocation = ATOMIC_INIT(0);
  43. void rds_ib_recv_init_ring(struct rds_ib_connection *ic)
  44. {
  45. struct rds_ib_recv_work *recv;
  46. u32 i;
  47. for (i = 0, recv = ic->i_recvs; i < ic->i_recv_ring.w_nr; i++, recv++) {
  48. struct ib_sge *sge;
  49. recv->r_ibinc = NULL;
  50. recv->r_frag = NULL;
  51. recv->r_wr.next = NULL;
  52. recv->r_wr.wr_id = i;
  53. recv->r_wr.sg_list = recv->r_sge;
  54. recv->r_wr.num_sge = RDS_IB_RECV_SGE;
  55. sge = &recv->r_sge[0];
  56. sge->addr = ic->i_recv_hdrs_dma + (i * sizeof(struct rds_header));
  57. sge->length = sizeof(struct rds_header);
  58. sge->lkey = ic->i_pd->local_dma_lkey;
  59. sge = &recv->r_sge[1];
  60. sge->addr = 0;
  61. sge->length = RDS_FRAG_SIZE;
  62. sge->lkey = ic->i_pd->local_dma_lkey;
  63. }
  64. }
  65. /*
  66. * The entire 'from' list, including the from element itself, is put on
  67. * to the tail of the 'to' list.
  68. */
  69. static void list_splice_entire_tail(struct list_head *from,
  70. struct list_head *to)
  71. {
  72. struct list_head *from_last = from->prev;
  73. list_splice_tail(from_last, to);
  74. list_add_tail(from_last, to);
  75. }
  76. static void rds_ib_cache_xfer_to_ready(struct rds_ib_refill_cache *cache)
  77. {
  78. struct list_head *tmp;
  79. tmp = xchg(&cache->xfer, NULL);
  80. if (tmp) {
  81. if (cache->ready)
  82. list_splice_entire_tail(tmp, cache->ready);
  83. else
  84. cache->ready = tmp;
  85. }
  86. }
  87. static int rds_ib_recv_alloc_cache(struct rds_ib_refill_cache *cache)
  88. {
  89. struct rds_ib_cache_head *head;
  90. int cpu;
  91. cache->percpu = alloc_percpu(struct rds_ib_cache_head);
  92. if (!cache->percpu)
  93. return -ENOMEM;
  94. for_each_possible_cpu(cpu) {
  95. head = per_cpu_ptr(cache->percpu, cpu);
  96. head->first = NULL;
  97. head->count = 0;
  98. }
  99. cache->xfer = NULL;
  100. cache->ready = NULL;
  101. return 0;
  102. }
  103. int rds_ib_recv_alloc_caches(struct rds_ib_connection *ic)
  104. {
  105. int ret;
  106. ret = rds_ib_recv_alloc_cache(&ic->i_cache_incs);
  107. if (!ret) {
  108. ret = rds_ib_recv_alloc_cache(&ic->i_cache_frags);
  109. if (ret)
  110. free_percpu(ic->i_cache_incs.percpu);
  111. }
  112. return ret;
  113. }
  114. static void rds_ib_cache_splice_all_lists(struct rds_ib_refill_cache *cache,
  115. struct list_head *caller_list)
  116. {
  117. struct rds_ib_cache_head *head;
  118. int cpu;
  119. for_each_possible_cpu(cpu) {
  120. head = per_cpu_ptr(cache->percpu, cpu);
  121. if (head->first) {
  122. list_splice_entire_tail(head->first, caller_list);
  123. head->first = NULL;
  124. }
  125. }
  126. if (cache->ready) {
  127. list_splice_entire_tail(cache->ready, caller_list);
  128. cache->ready = NULL;
  129. }
  130. }
  131. void rds_ib_recv_free_caches(struct rds_ib_connection *ic)
  132. {
  133. struct rds_ib_incoming *inc;
  134. struct rds_ib_incoming *inc_tmp;
  135. struct rds_page_frag *frag;
  136. struct rds_page_frag *frag_tmp;
  137. LIST_HEAD(list);
  138. rds_ib_cache_xfer_to_ready(&ic->i_cache_incs);
  139. rds_ib_cache_splice_all_lists(&ic->i_cache_incs, &list);
  140. free_percpu(ic->i_cache_incs.percpu);
  141. list_for_each_entry_safe(inc, inc_tmp, &list, ii_cache_entry) {
  142. list_del(&inc->ii_cache_entry);
  143. WARN_ON(!list_empty(&inc->ii_frags));
  144. kmem_cache_free(rds_ib_incoming_slab, inc);
  145. }
  146. rds_ib_cache_xfer_to_ready(&ic->i_cache_frags);
  147. rds_ib_cache_splice_all_lists(&ic->i_cache_frags, &list);
  148. free_percpu(ic->i_cache_frags.percpu);
  149. list_for_each_entry_safe(frag, frag_tmp, &list, f_cache_entry) {
  150. list_del(&frag->f_cache_entry);
  151. WARN_ON(!list_empty(&frag->f_item));
  152. kmem_cache_free(rds_ib_frag_slab, frag);
  153. }
  154. }
  155. /* fwd decl */
  156. static void rds_ib_recv_cache_put(struct list_head *new_item,
  157. struct rds_ib_refill_cache *cache);
  158. static struct list_head *rds_ib_recv_cache_get(struct rds_ib_refill_cache *cache);
  159. /* Recycle frag and attached recv buffer f_sg */
  160. static void rds_ib_frag_free(struct rds_ib_connection *ic,
  161. struct rds_page_frag *frag)
  162. {
  163. rdsdebug("frag %p page %p\n", frag, sg_page(&frag->f_sg));
  164. rds_ib_recv_cache_put(&frag->f_cache_entry, &ic->i_cache_frags);
  165. }
  166. /* Recycle inc after freeing attached frags */
  167. void rds_ib_inc_free(struct rds_incoming *inc)
  168. {
  169. struct rds_ib_incoming *ibinc;
  170. struct rds_page_frag *frag;
  171. struct rds_page_frag *pos;
  172. struct rds_ib_connection *ic = inc->i_conn->c_transport_data;
  173. ibinc = container_of(inc, struct rds_ib_incoming, ii_inc);
  174. /* Free attached frags */
  175. list_for_each_entry_safe(frag, pos, &ibinc->ii_frags, f_item) {
  176. list_del_init(&frag->f_item);
  177. rds_ib_frag_free(ic, frag);
  178. }
  179. BUG_ON(!list_empty(&ibinc->ii_frags));
  180. rdsdebug("freeing ibinc %p inc %p\n", ibinc, inc);
  181. rds_ib_recv_cache_put(&ibinc->ii_cache_entry, &ic->i_cache_incs);
  182. }
  183. static void rds_ib_recv_clear_one(struct rds_ib_connection *ic,
  184. struct rds_ib_recv_work *recv)
  185. {
  186. if (recv->r_ibinc) {
  187. rds_inc_put(&recv->r_ibinc->ii_inc);
  188. recv->r_ibinc = NULL;
  189. }
  190. if (recv->r_frag) {
  191. ib_dma_unmap_sg(ic->i_cm_id->device, &recv->r_frag->f_sg, 1, DMA_FROM_DEVICE);
  192. rds_ib_frag_free(ic, recv->r_frag);
  193. recv->r_frag = NULL;
  194. }
  195. }
  196. void rds_ib_recv_clear_ring(struct rds_ib_connection *ic)
  197. {
  198. u32 i;
  199. for (i = 0; i < ic->i_recv_ring.w_nr; i++)
  200. rds_ib_recv_clear_one(ic, &ic->i_recvs[i]);
  201. }
  202. static struct rds_ib_incoming *rds_ib_refill_one_inc(struct rds_ib_connection *ic,
  203. gfp_t slab_mask)
  204. {
  205. struct rds_ib_incoming *ibinc;
  206. struct list_head *cache_item;
  207. int avail_allocs;
  208. cache_item = rds_ib_recv_cache_get(&ic->i_cache_incs);
  209. if (cache_item) {
  210. ibinc = container_of(cache_item, struct rds_ib_incoming, ii_cache_entry);
  211. } else {
  212. avail_allocs = atomic_add_unless(&rds_ib_allocation,
  213. 1, rds_ib_sysctl_max_recv_allocation);
  214. if (!avail_allocs) {
  215. rds_ib_stats_inc(s_ib_rx_alloc_limit);
  216. return NULL;
  217. }
  218. ibinc = kmem_cache_alloc(rds_ib_incoming_slab, slab_mask);
  219. if (!ibinc) {
  220. atomic_dec(&rds_ib_allocation);
  221. return NULL;
  222. }
  223. }
  224. INIT_LIST_HEAD(&ibinc->ii_frags);
  225. rds_inc_init(&ibinc->ii_inc, ic->conn, ic->conn->c_faddr);
  226. return ibinc;
  227. }
  228. static struct rds_page_frag *rds_ib_refill_one_frag(struct rds_ib_connection *ic,
  229. gfp_t slab_mask, gfp_t page_mask)
  230. {
  231. struct rds_page_frag *frag;
  232. struct list_head *cache_item;
  233. int ret;
  234. cache_item = rds_ib_recv_cache_get(&ic->i_cache_frags);
  235. if (cache_item) {
  236. frag = container_of(cache_item, struct rds_page_frag, f_cache_entry);
  237. } else {
  238. frag = kmem_cache_alloc(rds_ib_frag_slab, slab_mask);
  239. if (!frag)
  240. return NULL;
  241. sg_init_table(&frag->f_sg, 1);
  242. ret = rds_page_remainder_alloc(&frag->f_sg,
  243. RDS_FRAG_SIZE, page_mask);
  244. if (ret) {
  245. kmem_cache_free(rds_ib_frag_slab, frag);
  246. return NULL;
  247. }
  248. }
  249. INIT_LIST_HEAD(&frag->f_item);
  250. return frag;
  251. }
  252. static int rds_ib_recv_refill_one(struct rds_connection *conn,
  253. struct rds_ib_recv_work *recv, gfp_t gfp)
  254. {
  255. struct rds_ib_connection *ic = conn->c_transport_data;
  256. struct ib_sge *sge;
  257. int ret = -ENOMEM;
  258. gfp_t slab_mask = GFP_NOWAIT;
  259. gfp_t page_mask = GFP_NOWAIT;
  260. if (gfp & __GFP_DIRECT_RECLAIM) {
  261. slab_mask = GFP_KERNEL;
  262. page_mask = GFP_HIGHUSER;
  263. }
  264. if (!ic->i_cache_incs.ready)
  265. rds_ib_cache_xfer_to_ready(&ic->i_cache_incs);
  266. if (!ic->i_cache_frags.ready)
  267. rds_ib_cache_xfer_to_ready(&ic->i_cache_frags);
  268. /*
  269. * ibinc was taken from recv if recv contained the start of a message.
  270. * recvs that were continuations will still have this allocated.
  271. */
  272. if (!recv->r_ibinc) {
  273. recv->r_ibinc = rds_ib_refill_one_inc(ic, slab_mask);
  274. if (!recv->r_ibinc)
  275. goto out;
  276. }
  277. WARN_ON(recv->r_frag); /* leak! */
  278. recv->r_frag = rds_ib_refill_one_frag(ic, slab_mask, page_mask);
  279. if (!recv->r_frag)
  280. goto out;
  281. ret = ib_dma_map_sg(ic->i_cm_id->device, &recv->r_frag->f_sg,
  282. 1, DMA_FROM_DEVICE);
  283. WARN_ON(ret != 1);
  284. sge = &recv->r_sge[0];
  285. sge->addr = ic->i_recv_hdrs_dma + (recv - ic->i_recvs) * sizeof(struct rds_header);
  286. sge->length = sizeof(struct rds_header);
  287. sge = &recv->r_sge[1];
  288. sge->addr = ib_sg_dma_address(ic->i_cm_id->device, &recv->r_frag->f_sg);
  289. sge->length = ib_sg_dma_len(ic->i_cm_id->device, &recv->r_frag->f_sg);
  290. ret = 0;
  291. out:
  292. return ret;
  293. }
  294. static int acquire_refill(struct rds_connection *conn)
  295. {
  296. return test_and_set_bit(RDS_RECV_REFILL, &conn->c_flags) == 0;
  297. }
  298. static void release_refill(struct rds_connection *conn)
  299. {
  300. clear_bit(RDS_RECV_REFILL, &conn->c_flags);
  301. /* We don't use wait_on_bit()/wake_up_bit() because our waking is in a
  302. * hot path and finding waiters is very rare. We don't want to walk
  303. * the system-wide hashed waitqueue buckets in the fast path only to
  304. * almost never find waiters.
  305. */
  306. if (waitqueue_active(&conn->c_waitq))
  307. wake_up_all(&conn->c_waitq);
  308. }
  309. /*
  310. * This tries to allocate and post unused work requests after making sure that
  311. * they have all the allocations they need to queue received fragments into
  312. * sockets.
  313. *
  314. * -1 is returned if posting fails due to temporary resource exhaustion.
  315. */
  316. void rds_ib_recv_refill(struct rds_connection *conn, int prefill, gfp_t gfp)
  317. {
  318. struct rds_ib_connection *ic = conn->c_transport_data;
  319. struct rds_ib_recv_work *recv;
  320. struct ib_recv_wr *failed_wr;
  321. unsigned int posted = 0;
  322. int ret = 0;
  323. bool can_wait = !!(gfp & __GFP_DIRECT_RECLAIM);
  324. u32 pos;
  325. /* the goal here is to just make sure that someone, somewhere
  326. * is posting buffers. If we can't get the refill lock,
  327. * let them do their thing
  328. */
  329. if (!acquire_refill(conn))
  330. return;
  331. while ((prefill || rds_conn_up(conn)) &&
  332. rds_ib_ring_alloc(&ic->i_recv_ring, 1, &pos)) {
  333. if (pos >= ic->i_recv_ring.w_nr) {
  334. printk(KERN_NOTICE "Argh - ring alloc returned pos=%u\n",
  335. pos);
  336. break;
  337. }
  338. recv = &ic->i_recvs[pos];
  339. ret = rds_ib_recv_refill_one(conn, recv, gfp);
  340. if (ret) {
  341. break;
  342. }
  343. /* XXX when can this fail? */
  344. ret = ib_post_recv(ic->i_cm_id->qp, &recv->r_wr, &failed_wr);
  345. rdsdebug("recv %p ibinc %p page %p addr %lu ret %d\n", recv,
  346. recv->r_ibinc, sg_page(&recv->r_frag->f_sg),
  347. (long) ib_sg_dma_address(
  348. ic->i_cm_id->device,
  349. &recv->r_frag->f_sg),
  350. ret);
  351. if (ret) {
  352. rds_ib_conn_error(conn, "recv post on "
  353. "%pI4 returned %d, disconnecting and "
  354. "reconnecting\n", &conn->c_faddr,
  355. ret);
  356. break;
  357. }
  358. posted++;
  359. }
  360. /* We're doing flow control - update the window. */
  361. if (ic->i_flowctl && posted)
  362. rds_ib_advertise_credits(conn, posted);
  363. if (ret)
  364. rds_ib_ring_unalloc(&ic->i_recv_ring, 1);
  365. release_refill(conn);
  366. /* if we're called from the softirq handler, we'll be GFP_NOWAIT.
  367. * in this case the ring being low is going to lead to more interrupts
  368. * and we can safely let the softirq code take care of it unless the
  369. * ring is completely empty.
  370. *
  371. * if we're called from krdsd, we'll be GFP_KERNEL. In this case
  372. * we might have raced with the softirq code while we had the refill
  373. * lock held. Use rds_ib_ring_low() instead of ring_empty to decide
  374. * if we should requeue.
  375. */
  376. if (rds_conn_up(conn) &&
  377. ((can_wait && rds_ib_ring_low(&ic->i_recv_ring)) ||
  378. rds_ib_ring_empty(&ic->i_recv_ring))) {
  379. queue_delayed_work(rds_wq, &conn->c_recv_w, 1);
  380. }
  381. }
  382. /*
  383. * We want to recycle several types of recv allocations, like incs and frags.
  384. * To use this, the *_free() function passes in the ptr to a list_head within
  385. * the recyclee, as well as the cache to put it on.
  386. *
  387. * First, we put the memory on a percpu list. When this reaches a certain size,
  388. * We move it to an intermediate non-percpu list in a lockless manner, with some
  389. * xchg/compxchg wizardry.
  390. *
  391. * N.B. Instead of a list_head as the anchor, we use a single pointer, which can
  392. * be NULL and xchg'd. The list is actually empty when the pointer is NULL, and
  393. * list_empty() will return true with one element is actually present.
  394. */
  395. static void rds_ib_recv_cache_put(struct list_head *new_item,
  396. struct rds_ib_refill_cache *cache)
  397. {
  398. unsigned long flags;
  399. struct list_head *old, *chpfirst;
  400. local_irq_save(flags);
  401. chpfirst = __this_cpu_read(cache->percpu->first);
  402. if (!chpfirst)
  403. INIT_LIST_HEAD(new_item);
  404. else /* put on front */
  405. list_add_tail(new_item, chpfirst);
  406. __this_cpu_write(cache->percpu->first, new_item);
  407. __this_cpu_inc(cache->percpu->count);
  408. if (__this_cpu_read(cache->percpu->count) < RDS_IB_RECYCLE_BATCH_COUNT)
  409. goto end;
  410. /*
  411. * Return our per-cpu first list to the cache's xfer by atomically
  412. * grabbing the current xfer list, appending it to our per-cpu list,
  413. * and then atomically returning that entire list back to the
  414. * cache's xfer list as long as it's still empty.
  415. */
  416. do {
  417. old = xchg(&cache->xfer, NULL);
  418. if (old)
  419. list_splice_entire_tail(old, chpfirst);
  420. old = cmpxchg(&cache->xfer, NULL, chpfirst);
  421. } while (old);
  422. __this_cpu_write(cache->percpu->first, NULL);
  423. __this_cpu_write(cache->percpu->count, 0);
  424. end:
  425. local_irq_restore(flags);
  426. }
  427. static struct list_head *rds_ib_recv_cache_get(struct rds_ib_refill_cache *cache)
  428. {
  429. struct list_head *head = cache->ready;
  430. if (head) {
  431. if (!list_empty(head)) {
  432. cache->ready = head->next;
  433. list_del_init(head);
  434. } else
  435. cache->ready = NULL;
  436. }
  437. return head;
  438. }
  439. int rds_ib_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to)
  440. {
  441. struct rds_ib_incoming *ibinc;
  442. struct rds_page_frag *frag;
  443. unsigned long to_copy;
  444. unsigned long frag_off = 0;
  445. int copied = 0;
  446. int ret;
  447. u32 len;
  448. ibinc = container_of(inc, struct rds_ib_incoming, ii_inc);
  449. frag = list_entry(ibinc->ii_frags.next, struct rds_page_frag, f_item);
  450. len = be32_to_cpu(inc->i_hdr.h_len);
  451. while (iov_iter_count(to) && copied < len) {
  452. if (frag_off == RDS_FRAG_SIZE) {
  453. frag = list_entry(frag->f_item.next,
  454. struct rds_page_frag, f_item);
  455. frag_off = 0;
  456. }
  457. to_copy = min_t(unsigned long, iov_iter_count(to),
  458. RDS_FRAG_SIZE - frag_off);
  459. to_copy = min_t(unsigned long, to_copy, len - copied);
  460. /* XXX needs + offset for multiple recvs per page */
  461. rds_stats_add(s_copy_to_user, to_copy);
  462. ret = copy_page_to_iter(sg_page(&frag->f_sg),
  463. frag->f_sg.offset + frag_off,
  464. to_copy,
  465. to);
  466. if (ret != to_copy)
  467. return -EFAULT;
  468. frag_off += to_copy;
  469. copied += to_copy;
  470. }
  471. return copied;
  472. }
  473. /* ic starts out kzalloc()ed */
  474. void rds_ib_recv_init_ack(struct rds_ib_connection *ic)
  475. {
  476. struct ib_send_wr *wr = &ic->i_ack_wr;
  477. struct ib_sge *sge = &ic->i_ack_sge;
  478. sge->addr = ic->i_ack_dma;
  479. sge->length = sizeof(struct rds_header);
  480. sge->lkey = ic->i_pd->local_dma_lkey;
  481. wr->sg_list = sge;
  482. wr->num_sge = 1;
  483. wr->opcode = IB_WR_SEND;
  484. wr->wr_id = RDS_IB_ACK_WR_ID;
  485. wr->send_flags = IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  486. }
  487. /*
  488. * You'd think that with reliable IB connections you wouldn't need to ack
  489. * messages that have been received. The problem is that IB hardware generates
  490. * an ack message before it has DMAed the message into memory. This creates a
  491. * potential message loss if the HCA is disabled for any reason between when it
  492. * sends the ack and before the message is DMAed and processed. This is only a
  493. * potential issue if another HCA is available for fail-over.
  494. *
  495. * When the remote host receives our ack they'll free the sent message from
  496. * their send queue. To decrease the latency of this we always send an ack
  497. * immediately after we've received messages.
  498. *
  499. * For simplicity, we only have one ack in flight at a time. This puts
  500. * pressure on senders to have deep enough send queues to absorb the latency of
  501. * a single ack frame being in flight. This might not be good enough.
  502. *
  503. * This is implemented by have a long-lived send_wr and sge which point to a
  504. * statically allocated ack frame. This ack wr does not fall under the ring
  505. * accounting that the tx and rx wrs do. The QP attribute specifically makes
  506. * room for it beyond the ring size. Send completion notices its special
  507. * wr_id and avoids working with the ring in that case.
  508. */
  509. #ifndef KERNEL_HAS_ATOMIC64
  510. void rds_ib_set_ack(struct rds_ib_connection *ic, u64 seq, int ack_required)
  511. {
  512. unsigned long flags;
  513. spin_lock_irqsave(&ic->i_ack_lock, flags);
  514. ic->i_ack_next = seq;
  515. if (ack_required)
  516. set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  517. spin_unlock_irqrestore(&ic->i_ack_lock, flags);
  518. }
  519. static u64 rds_ib_get_ack(struct rds_ib_connection *ic)
  520. {
  521. unsigned long flags;
  522. u64 seq;
  523. clear_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  524. spin_lock_irqsave(&ic->i_ack_lock, flags);
  525. seq = ic->i_ack_next;
  526. spin_unlock_irqrestore(&ic->i_ack_lock, flags);
  527. return seq;
  528. }
  529. #else
  530. void rds_ib_set_ack(struct rds_ib_connection *ic, u64 seq, int ack_required)
  531. {
  532. atomic64_set(&ic->i_ack_next, seq);
  533. if (ack_required) {
  534. smp_mb__before_atomic();
  535. set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  536. }
  537. }
  538. static u64 rds_ib_get_ack(struct rds_ib_connection *ic)
  539. {
  540. clear_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  541. smp_mb__after_atomic();
  542. return atomic64_read(&ic->i_ack_next);
  543. }
  544. #endif
  545. static void rds_ib_send_ack(struct rds_ib_connection *ic, unsigned int adv_credits)
  546. {
  547. struct rds_header *hdr = ic->i_ack;
  548. struct ib_send_wr *failed_wr;
  549. u64 seq;
  550. int ret;
  551. seq = rds_ib_get_ack(ic);
  552. rdsdebug("send_ack: ic %p ack %llu\n", ic, (unsigned long long) seq);
  553. rds_message_populate_header(hdr, 0, 0, 0);
  554. hdr->h_ack = cpu_to_be64(seq);
  555. hdr->h_credit = adv_credits;
  556. rds_message_make_checksum(hdr);
  557. ic->i_ack_queued = jiffies;
  558. ret = ib_post_send(ic->i_cm_id->qp, &ic->i_ack_wr, &failed_wr);
  559. if (unlikely(ret)) {
  560. /* Failed to send. Release the WR, and
  561. * force another ACK.
  562. */
  563. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  564. set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  565. rds_ib_stats_inc(s_ib_ack_send_failure);
  566. rds_ib_conn_error(ic->conn, "sending ack failed\n");
  567. } else
  568. rds_ib_stats_inc(s_ib_ack_sent);
  569. }
  570. /*
  571. * There are 3 ways of getting acknowledgements to the peer:
  572. * 1. We call rds_ib_attempt_ack from the recv completion handler
  573. * to send an ACK-only frame.
  574. * However, there can be only one such frame in the send queue
  575. * at any time, so we may have to postpone it.
  576. * 2. When another (data) packet is transmitted while there's
  577. * an ACK in the queue, we piggyback the ACK sequence number
  578. * on the data packet.
  579. * 3. If the ACK WR is done sending, we get called from the
  580. * send queue completion handler, and check whether there's
  581. * another ACK pending (postponed because the WR was on the
  582. * queue). If so, we transmit it.
  583. *
  584. * We maintain 2 variables:
  585. * - i_ack_flags, which keeps track of whether the ACK WR
  586. * is currently in the send queue or not (IB_ACK_IN_FLIGHT)
  587. * - i_ack_next, which is the last sequence number we received
  588. *
  589. * Potentially, send queue and receive queue handlers can run concurrently.
  590. * It would be nice to not have to use a spinlock to synchronize things,
  591. * but the one problem that rules this out is that 64bit updates are
  592. * not atomic on all platforms. Things would be a lot simpler if
  593. * we had atomic64 or maybe cmpxchg64 everywhere.
  594. *
  595. * Reconnecting complicates this picture just slightly. When we
  596. * reconnect, we may be seeing duplicate packets. The peer
  597. * is retransmitting them, because it hasn't seen an ACK for
  598. * them. It is important that we ACK these.
  599. *
  600. * ACK mitigation adds a header flag "ACK_REQUIRED"; any packet with
  601. * this flag set *MUST* be acknowledged immediately.
  602. */
  603. /*
  604. * When we get here, we're called from the recv queue handler.
  605. * Check whether we ought to transmit an ACK.
  606. */
  607. void rds_ib_attempt_ack(struct rds_ib_connection *ic)
  608. {
  609. unsigned int adv_credits;
  610. if (!test_bit(IB_ACK_REQUESTED, &ic->i_ack_flags))
  611. return;
  612. if (test_and_set_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags)) {
  613. rds_ib_stats_inc(s_ib_ack_send_delayed);
  614. return;
  615. }
  616. /* Can we get a send credit? */
  617. if (!rds_ib_send_grab_credits(ic, 1, &adv_credits, 0, RDS_MAX_ADV_CREDIT)) {
  618. rds_ib_stats_inc(s_ib_tx_throttle);
  619. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  620. return;
  621. }
  622. clear_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
  623. rds_ib_send_ack(ic, adv_credits);
  624. }
  625. /*
  626. * We get here from the send completion handler, when the
  627. * adapter tells us the ACK frame was sent.
  628. */
  629. void rds_ib_ack_send_complete(struct rds_ib_connection *ic)
  630. {
  631. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  632. rds_ib_attempt_ack(ic);
  633. }
  634. /*
  635. * This is called by the regular xmit code when it wants to piggyback
  636. * an ACK on an outgoing frame.
  637. */
  638. u64 rds_ib_piggyb_ack(struct rds_ib_connection *ic)
  639. {
  640. if (test_and_clear_bit(IB_ACK_REQUESTED, &ic->i_ack_flags))
  641. rds_ib_stats_inc(s_ib_ack_send_piggybacked);
  642. return rds_ib_get_ack(ic);
  643. }
  644. /*
  645. * It's kind of lame that we're copying from the posted receive pages into
  646. * long-lived bitmaps. We could have posted the bitmaps and rdma written into
  647. * them. But receiving new congestion bitmaps should be a *rare* event, so
  648. * hopefully we won't need to invest that complexity in making it more
  649. * efficient. By copying we can share a simpler core with TCP which has to
  650. * copy.
  651. */
  652. static void rds_ib_cong_recv(struct rds_connection *conn,
  653. struct rds_ib_incoming *ibinc)
  654. {
  655. struct rds_cong_map *map;
  656. unsigned int map_off;
  657. unsigned int map_page;
  658. struct rds_page_frag *frag;
  659. unsigned long frag_off;
  660. unsigned long to_copy;
  661. unsigned long copied;
  662. uint64_t uncongested = 0;
  663. void *addr;
  664. /* catch completely corrupt packets */
  665. if (be32_to_cpu(ibinc->ii_inc.i_hdr.h_len) != RDS_CONG_MAP_BYTES)
  666. return;
  667. map = conn->c_fcong;
  668. map_page = 0;
  669. map_off = 0;
  670. frag = list_entry(ibinc->ii_frags.next, struct rds_page_frag, f_item);
  671. frag_off = 0;
  672. copied = 0;
  673. while (copied < RDS_CONG_MAP_BYTES) {
  674. uint64_t *src, *dst;
  675. unsigned int k;
  676. to_copy = min(RDS_FRAG_SIZE - frag_off, PAGE_SIZE - map_off);
  677. BUG_ON(to_copy & 7); /* Must be 64bit aligned. */
  678. addr = kmap_atomic(sg_page(&frag->f_sg));
  679. src = addr + frag_off;
  680. dst = (void *)map->m_page_addrs[map_page] + map_off;
  681. for (k = 0; k < to_copy; k += 8) {
  682. /* Record ports that became uncongested, ie
  683. * bits that changed from 0 to 1. */
  684. uncongested |= ~(*src) & *dst;
  685. *dst++ = *src++;
  686. }
  687. kunmap_atomic(addr);
  688. copied += to_copy;
  689. map_off += to_copy;
  690. if (map_off == PAGE_SIZE) {
  691. map_off = 0;
  692. map_page++;
  693. }
  694. frag_off += to_copy;
  695. if (frag_off == RDS_FRAG_SIZE) {
  696. frag = list_entry(frag->f_item.next,
  697. struct rds_page_frag, f_item);
  698. frag_off = 0;
  699. }
  700. }
  701. /* the congestion map is in little endian order */
  702. uncongested = le64_to_cpu(uncongested);
  703. rds_cong_map_updated(map, uncongested);
  704. }
  705. static void rds_ib_process_recv(struct rds_connection *conn,
  706. struct rds_ib_recv_work *recv, u32 data_len,
  707. struct rds_ib_ack_state *state)
  708. {
  709. struct rds_ib_connection *ic = conn->c_transport_data;
  710. struct rds_ib_incoming *ibinc = ic->i_ibinc;
  711. struct rds_header *ihdr, *hdr;
  712. /* XXX shut down the connection if port 0,0 are seen? */
  713. rdsdebug("ic %p ibinc %p recv %p byte len %u\n", ic, ibinc, recv,
  714. data_len);
  715. if (data_len < sizeof(struct rds_header)) {
  716. rds_ib_conn_error(conn, "incoming message "
  717. "from %pI4 didn't include a "
  718. "header, disconnecting and "
  719. "reconnecting\n",
  720. &conn->c_faddr);
  721. return;
  722. }
  723. data_len -= sizeof(struct rds_header);
  724. ihdr = &ic->i_recv_hdrs[recv - ic->i_recvs];
  725. /* Validate the checksum. */
  726. if (!rds_message_verify_checksum(ihdr)) {
  727. rds_ib_conn_error(conn, "incoming message "
  728. "from %pI4 has corrupted header - "
  729. "forcing a reconnect\n",
  730. &conn->c_faddr);
  731. rds_stats_inc(s_recv_drop_bad_checksum);
  732. return;
  733. }
  734. /* Process the ACK sequence which comes with every packet */
  735. state->ack_recv = be64_to_cpu(ihdr->h_ack);
  736. state->ack_recv_valid = 1;
  737. /* Process the credits update if there was one */
  738. if (ihdr->h_credit)
  739. rds_ib_send_add_credits(conn, ihdr->h_credit);
  740. if (ihdr->h_sport == 0 && ihdr->h_dport == 0 && data_len == 0) {
  741. /* This is an ACK-only packet. The fact that it gets
  742. * special treatment here is that historically, ACKs
  743. * were rather special beasts.
  744. */
  745. rds_ib_stats_inc(s_ib_ack_received);
  746. /*
  747. * Usually the frags make their way on to incs and are then freed as
  748. * the inc is freed. We don't go that route, so we have to drop the
  749. * page ref ourselves. We can't just leave the page on the recv
  750. * because that confuses the dma mapping of pages and each recv's use
  751. * of a partial page.
  752. *
  753. * FIXME: Fold this into the code path below.
  754. */
  755. rds_ib_frag_free(ic, recv->r_frag);
  756. recv->r_frag = NULL;
  757. return;
  758. }
  759. /*
  760. * If we don't already have an inc on the connection then this
  761. * fragment has a header and starts a message.. copy its header
  762. * into the inc and save the inc so we can hang upcoming fragments
  763. * off its list.
  764. */
  765. if (!ibinc) {
  766. ibinc = recv->r_ibinc;
  767. recv->r_ibinc = NULL;
  768. ic->i_ibinc = ibinc;
  769. hdr = &ibinc->ii_inc.i_hdr;
  770. memcpy(hdr, ihdr, sizeof(*hdr));
  771. ic->i_recv_data_rem = be32_to_cpu(hdr->h_len);
  772. rdsdebug("ic %p ibinc %p rem %u flag 0x%x\n", ic, ibinc,
  773. ic->i_recv_data_rem, hdr->h_flags);
  774. } else {
  775. hdr = &ibinc->ii_inc.i_hdr;
  776. /* We can't just use memcmp here; fragments of a
  777. * single message may carry different ACKs */
  778. if (hdr->h_sequence != ihdr->h_sequence ||
  779. hdr->h_len != ihdr->h_len ||
  780. hdr->h_sport != ihdr->h_sport ||
  781. hdr->h_dport != ihdr->h_dport) {
  782. rds_ib_conn_error(conn,
  783. "fragment header mismatch; forcing reconnect\n");
  784. return;
  785. }
  786. }
  787. list_add_tail(&recv->r_frag->f_item, &ibinc->ii_frags);
  788. recv->r_frag = NULL;
  789. if (ic->i_recv_data_rem > RDS_FRAG_SIZE)
  790. ic->i_recv_data_rem -= RDS_FRAG_SIZE;
  791. else {
  792. ic->i_recv_data_rem = 0;
  793. ic->i_ibinc = NULL;
  794. if (ibinc->ii_inc.i_hdr.h_flags == RDS_FLAG_CONG_BITMAP)
  795. rds_ib_cong_recv(conn, ibinc);
  796. else {
  797. rds_recv_incoming(conn, conn->c_faddr, conn->c_laddr,
  798. &ibinc->ii_inc, GFP_ATOMIC);
  799. state->ack_next = be64_to_cpu(hdr->h_sequence);
  800. state->ack_next_valid = 1;
  801. }
  802. /* Evaluate the ACK_REQUIRED flag *after* we received
  803. * the complete frame, and after bumping the next_rx
  804. * sequence. */
  805. if (hdr->h_flags & RDS_FLAG_ACK_REQUIRED) {
  806. rds_stats_inc(s_recv_ack_required);
  807. state->ack_required = 1;
  808. }
  809. rds_inc_put(&ibinc->ii_inc);
  810. }
  811. }
  812. void rds_ib_recv_cqe_handler(struct rds_ib_connection *ic,
  813. struct ib_wc *wc,
  814. struct rds_ib_ack_state *state)
  815. {
  816. struct rds_connection *conn = ic->conn;
  817. struct rds_ib_recv_work *recv;
  818. rdsdebug("wc wr_id 0x%llx status %u (%s) byte_len %u imm_data %u\n",
  819. (unsigned long long)wc->wr_id, wc->status,
  820. ib_wc_status_msg(wc->status), wc->byte_len,
  821. be32_to_cpu(wc->ex.imm_data));
  822. rds_ib_stats_inc(s_ib_rx_cq_event);
  823. recv = &ic->i_recvs[rds_ib_ring_oldest(&ic->i_recv_ring)];
  824. ib_dma_unmap_sg(ic->i_cm_id->device, &recv->r_frag->f_sg, 1,
  825. DMA_FROM_DEVICE);
  826. /* Also process recvs in connecting state because it is possible
  827. * to get a recv completion _before_ the rdmacm ESTABLISHED
  828. * event is processed.
  829. */
  830. if (wc->status == IB_WC_SUCCESS) {
  831. rds_ib_process_recv(conn, recv, wc->byte_len, state);
  832. } else {
  833. /* We expect errors as the qp is drained during shutdown */
  834. if (rds_conn_up(conn) || rds_conn_connecting(conn))
  835. rds_ib_conn_error(conn, "recv completion on %pI4 had status %u (%s), disconnecting and reconnecting\n",
  836. &conn->c_faddr,
  837. wc->status,
  838. ib_wc_status_msg(wc->status));
  839. }
  840. /* rds_ib_process_recv() doesn't always consume the frag, and
  841. * we might not have called it at all if the wc didn't indicate
  842. * success. We already unmapped the frag's pages, though, and
  843. * the following rds_ib_ring_free() call tells the refill path
  844. * that it will not find an allocated frag here. Make sure we
  845. * keep that promise by freeing a frag that's still on the ring.
  846. */
  847. if (recv->r_frag) {
  848. rds_ib_frag_free(ic, recv->r_frag);
  849. recv->r_frag = NULL;
  850. }
  851. rds_ib_ring_free(&ic->i_recv_ring, 1);
  852. /* If we ever end up with a really empty receive ring, we're
  853. * in deep trouble, as the sender will definitely see RNR
  854. * timeouts. */
  855. if (rds_ib_ring_empty(&ic->i_recv_ring))
  856. rds_ib_stats_inc(s_ib_rx_ring_empty);
  857. if (rds_ib_ring_low(&ic->i_recv_ring))
  858. rds_ib_recv_refill(conn, 0, GFP_NOWAIT);
  859. }
  860. int rds_ib_recv(struct rds_connection *conn)
  861. {
  862. struct rds_ib_connection *ic = conn->c_transport_data;
  863. int ret = 0;
  864. rdsdebug("conn %p\n", conn);
  865. if (rds_conn_up(conn)) {
  866. rds_ib_attempt_ack(ic);
  867. rds_ib_recv_refill(conn, 0, GFP_KERNEL);
  868. }
  869. return ret;
  870. }
  871. int rds_ib_recv_init(void)
  872. {
  873. struct sysinfo si;
  874. int ret = -ENOMEM;
  875. /* Default to 30% of all available RAM for recv memory */
  876. si_meminfo(&si);
  877. rds_ib_sysctl_max_recv_allocation = si.totalram / 3 * PAGE_SIZE / RDS_FRAG_SIZE;
  878. rds_ib_incoming_slab = kmem_cache_create("rds_ib_incoming",
  879. sizeof(struct rds_ib_incoming),
  880. 0, SLAB_HWCACHE_ALIGN, NULL);
  881. if (!rds_ib_incoming_slab)
  882. goto out;
  883. rds_ib_frag_slab = kmem_cache_create("rds_ib_frag",
  884. sizeof(struct rds_page_frag),
  885. 0, SLAB_HWCACHE_ALIGN, NULL);
  886. if (!rds_ib_frag_slab) {
  887. kmem_cache_destroy(rds_ib_incoming_slab);
  888. rds_ib_incoming_slab = NULL;
  889. } else
  890. ret = 0;
  891. out:
  892. return ret;
  893. }
  894. void rds_ib_recv_exit(void)
  895. {
  896. kmem_cache_destroy(rds_ib_incoming_slab);
  897. kmem_cache_destroy(rds_ib_frag_slab);
  898. }