ib_recv.c 30 KB

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