ib_recv.c 31 KB

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