ib_recv.c 31 KB

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