send.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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/moduleparam.h>
  35. #include <linux/gfp.h>
  36. #include <net/sock.h>
  37. #include <linux/in.h>
  38. #include <linux/list.h>
  39. #include <linux/ratelimit.h>
  40. #include <linux/export.h>
  41. #include <linux/sizes.h>
  42. #include "rds.h"
  43. /* When transmitting messages in rds_send_xmit, we need to emerge from
  44. * time to time and briefly release the CPU. Otherwise the softlock watchdog
  45. * will kick our shin.
  46. * Also, it seems fairer to not let one busy connection stall all the
  47. * others.
  48. *
  49. * send_batch_count is the number of times we'll loop in send_xmit. Setting
  50. * it to 0 will restore the old behavior (where we looped until we had
  51. * drained the queue).
  52. */
  53. static int send_batch_count = SZ_1K;
  54. module_param(send_batch_count, int, 0444);
  55. MODULE_PARM_DESC(send_batch_count, " batch factor when working the send queue");
  56. static void rds_send_remove_from_sock(struct list_head *messages, int status);
  57. /*
  58. * Reset the send state. Callers must ensure that this doesn't race with
  59. * rds_send_xmit().
  60. */
  61. void rds_send_reset(struct rds_connection *conn)
  62. {
  63. struct rds_message *rm, *tmp;
  64. unsigned long flags;
  65. if (conn->c_xmit_rm) {
  66. rm = conn->c_xmit_rm;
  67. conn->c_xmit_rm = NULL;
  68. /* Tell the user the RDMA op is no longer mapped by the
  69. * transport. This isn't entirely true (it's flushed out
  70. * independently) but as the connection is down, there's
  71. * no ongoing RDMA to/from that memory */
  72. rds_message_unmapped(rm);
  73. rds_message_put(rm);
  74. }
  75. conn->c_xmit_sg = 0;
  76. conn->c_xmit_hdr_off = 0;
  77. conn->c_xmit_data_off = 0;
  78. conn->c_xmit_atomic_sent = 0;
  79. conn->c_xmit_rdma_sent = 0;
  80. conn->c_xmit_data_sent = 0;
  81. conn->c_map_queued = 0;
  82. conn->c_unacked_packets = rds_sysctl_max_unacked_packets;
  83. conn->c_unacked_bytes = rds_sysctl_max_unacked_bytes;
  84. /* Mark messages as retransmissions, and move them to the send q */
  85. spin_lock_irqsave(&conn->c_lock, flags);
  86. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  87. set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  88. set_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags);
  89. }
  90. list_splice_init(&conn->c_retrans, &conn->c_send_queue);
  91. spin_unlock_irqrestore(&conn->c_lock, flags);
  92. }
  93. static int acquire_in_xmit(struct rds_connection *conn)
  94. {
  95. return test_and_set_bit(RDS_IN_XMIT, &conn->c_flags) == 0;
  96. }
  97. static void release_in_xmit(struct rds_connection *conn)
  98. {
  99. clear_bit(RDS_IN_XMIT, &conn->c_flags);
  100. smp_mb__after_atomic();
  101. /*
  102. * We don't use wait_on_bit()/wake_up_bit() because our waking is in a
  103. * hot path and finding waiters is very rare. We don't want to walk
  104. * the system-wide hashed waitqueue buckets in the fast path only to
  105. * almost never find waiters.
  106. */
  107. if (waitqueue_active(&conn->c_waitq))
  108. wake_up_all(&conn->c_waitq);
  109. }
  110. /*
  111. * We're making the conscious trade-off here to only send one message
  112. * down the connection at a time.
  113. * Pro:
  114. * - tx queueing is a simple fifo list
  115. * - reassembly is optional and easily done by transports per conn
  116. * - no per flow rx lookup at all, straight to the socket
  117. * - less per-frag memory and wire overhead
  118. * Con:
  119. * - queued acks can be delayed behind large messages
  120. * Depends:
  121. * - small message latency is higher behind queued large messages
  122. * - large message latency isn't starved by intervening small sends
  123. */
  124. int rds_send_xmit(struct rds_connection *conn)
  125. {
  126. struct rds_message *rm;
  127. unsigned long flags;
  128. unsigned int tmp;
  129. struct scatterlist *sg;
  130. int ret = 0;
  131. LIST_HEAD(to_be_dropped);
  132. int batch_count;
  133. unsigned long send_gen = 0;
  134. restart:
  135. batch_count = 0;
  136. /*
  137. * sendmsg calls here after having queued its message on the send
  138. * queue. We only have one task feeding the connection at a time. If
  139. * another thread is already feeding the queue then we back off. This
  140. * avoids blocking the caller and trading per-connection data between
  141. * caches per message.
  142. */
  143. if (!acquire_in_xmit(conn)) {
  144. rds_stats_inc(s_send_lock_contention);
  145. ret = -ENOMEM;
  146. goto out;
  147. }
  148. /*
  149. * we record the send generation after doing the xmit acquire.
  150. * if someone else manages to jump in and do some work, we'll use
  151. * this to avoid a goto restart farther down.
  152. *
  153. * The acquire_in_xmit() check above ensures that only one
  154. * caller can increment c_send_gen at any time.
  155. */
  156. conn->c_send_gen++;
  157. send_gen = conn->c_send_gen;
  158. /*
  159. * rds_conn_shutdown() sets the conn state and then tests RDS_IN_XMIT,
  160. * we do the opposite to avoid races.
  161. */
  162. if (!rds_conn_up(conn)) {
  163. release_in_xmit(conn);
  164. ret = 0;
  165. goto out;
  166. }
  167. if (conn->c_trans->xmit_prepare)
  168. conn->c_trans->xmit_prepare(conn);
  169. /*
  170. * spin trying to push headers and data down the connection until
  171. * the connection doesn't make forward progress.
  172. */
  173. while (1) {
  174. rm = conn->c_xmit_rm;
  175. /*
  176. * If between sending messages, we can send a pending congestion
  177. * map update.
  178. */
  179. if (!rm && test_and_clear_bit(0, &conn->c_map_queued)) {
  180. rm = rds_cong_update_alloc(conn);
  181. if (IS_ERR(rm)) {
  182. ret = PTR_ERR(rm);
  183. break;
  184. }
  185. rm->data.op_active = 1;
  186. conn->c_xmit_rm = rm;
  187. }
  188. /*
  189. * If not already working on one, grab the next message.
  190. *
  191. * c_xmit_rm holds a ref while we're sending this message down
  192. * the connction. We can use this ref while holding the
  193. * send_sem.. rds_send_reset() is serialized with it.
  194. */
  195. if (!rm) {
  196. unsigned int len;
  197. batch_count++;
  198. /* we want to process as big a batch as we can, but
  199. * we also want to avoid softlockups. If we've been
  200. * through a lot of messages, lets back off and see
  201. * if anyone else jumps in
  202. */
  203. if (batch_count >= send_batch_count)
  204. goto over_batch;
  205. spin_lock_irqsave(&conn->c_lock, flags);
  206. if (!list_empty(&conn->c_send_queue)) {
  207. rm = list_entry(conn->c_send_queue.next,
  208. struct rds_message,
  209. m_conn_item);
  210. rds_message_addref(rm);
  211. /*
  212. * Move the message from the send queue to the retransmit
  213. * list right away.
  214. */
  215. list_move_tail(&rm->m_conn_item, &conn->c_retrans);
  216. }
  217. spin_unlock_irqrestore(&conn->c_lock, flags);
  218. if (!rm)
  219. break;
  220. /* Unfortunately, the way Infiniband deals with
  221. * RDMA to a bad MR key is by moving the entire
  222. * queue pair to error state. We cold possibly
  223. * recover from that, but right now we drop the
  224. * connection.
  225. * Therefore, we never retransmit messages with RDMA ops.
  226. */
  227. if (rm->rdma.op_active &&
  228. test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags)) {
  229. spin_lock_irqsave(&conn->c_lock, flags);
  230. if (test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags))
  231. list_move(&rm->m_conn_item, &to_be_dropped);
  232. spin_unlock_irqrestore(&conn->c_lock, flags);
  233. continue;
  234. }
  235. /* Require an ACK every once in a while */
  236. len = ntohl(rm->m_inc.i_hdr.h_len);
  237. if (conn->c_unacked_packets == 0 ||
  238. conn->c_unacked_bytes < len) {
  239. __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  240. conn->c_unacked_packets = rds_sysctl_max_unacked_packets;
  241. conn->c_unacked_bytes = rds_sysctl_max_unacked_bytes;
  242. rds_stats_inc(s_send_ack_required);
  243. } else {
  244. conn->c_unacked_bytes -= len;
  245. conn->c_unacked_packets--;
  246. }
  247. conn->c_xmit_rm = rm;
  248. }
  249. /* The transport either sends the whole rdma or none of it */
  250. if (rm->rdma.op_active && !conn->c_xmit_rdma_sent) {
  251. rm->m_final_op = &rm->rdma;
  252. /* The transport owns the mapped memory for now.
  253. * You can't unmap it while it's on the send queue
  254. */
  255. set_bit(RDS_MSG_MAPPED, &rm->m_flags);
  256. ret = conn->c_trans->xmit_rdma(conn, &rm->rdma);
  257. if (ret) {
  258. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  259. wake_up_interruptible(&rm->m_flush_wait);
  260. break;
  261. }
  262. conn->c_xmit_rdma_sent = 1;
  263. }
  264. if (rm->atomic.op_active && !conn->c_xmit_atomic_sent) {
  265. rm->m_final_op = &rm->atomic;
  266. /* The transport owns the mapped memory for now.
  267. * You can't unmap it while it's on the send queue
  268. */
  269. set_bit(RDS_MSG_MAPPED, &rm->m_flags);
  270. ret = conn->c_trans->xmit_atomic(conn, &rm->atomic);
  271. if (ret) {
  272. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  273. wake_up_interruptible(&rm->m_flush_wait);
  274. break;
  275. }
  276. conn->c_xmit_atomic_sent = 1;
  277. }
  278. /*
  279. * A number of cases require an RDS header to be sent
  280. * even if there is no data.
  281. * We permit 0-byte sends; rds-ping depends on this.
  282. * However, if there are exclusively attached silent ops,
  283. * we skip the hdr/data send, to enable silent operation.
  284. */
  285. if (rm->data.op_nents == 0) {
  286. int ops_present;
  287. int all_ops_are_silent = 1;
  288. ops_present = (rm->atomic.op_active || rm->rdma.op_active);
  289. if (rm->atomic.op_active && !rm->atomic.op_silent)
  290. all_ops_are_silent = 0;
  291. if (rm->rdma.op_active && !rm->rdma.op_silent)
  292. all_ops_are_silent = 0;
  293. if (ops_present && all_ops_are_silent
  294. && !rm->m_rdma_cookie)
  295. rm->data.op_active = 0;
  296. }
  297. if (rm->data.op_active && !conn->c_xmit_data_sent) {
  298. rm->m_final_op = &rm->data;
  299. ret = conn->c_trans->xmit(conn, rm,
  300. conn->c_xmit_hdr_off,
  301. conn->c_xmit_sg,
  302. conn->c_xmit_data_off);
  303. if (ret <= 0)
  304. break;
  305. if (conn->c_xmit_hdr_off < sizeof(struct rds_header)) {
  306. tmp = min_t(int, ret,
  307. sizeof(struct rds_header) -
  308. conn->c_xmit_hdr_off);
  309. conn->c_xmit_hdr_off += tmp;
  310. ret -= tmp;
  311. }
  312. sg = &rm->data.op_sg[conn->c_xmit_sg];
  313. while (ret) {
  314. tmp = min_t(int, ret, sg->length -
  315. conn->c_xmit_data_off);
  316. conn->c_xmit_data_off += tmp;
  317. ret -= tmp;
  318. if (conn->c_xmit_data_off == sg->length) {
  319. conn->c_xmit_data_off = 0;
  320. sg++;
  321. conn->c_xmit_sg++;
  322. BUG_ON(ret != 0 &&
  323. conn->c_xmit_sg == rm->data.op_nents);
  324. }
  325. }
  326. if (conn->c_xmit_hdr_off == sizeof(struct rds_header) &&
  327. (conn->c_xmit_sg == rm->data.op_nents))
  328. conn->c_xmit_data_sent = 1;
  329. }
  330. /*
  331. * A rm will only take multiple times through this loop
  332. * if there is a data op. Thus, if the data is sent (or there was
  333. * none), then we're done with the rm.
  334. */
  335. if (!rm->data.op_active || conn->c_xmit_data_sent) {
  336. conn->c_xmit_rm = NULL;
  337. conn->c_xmit_sg = 0;
  338. conn->c_xmit_hdr_off = 0;
  339. conn->c_xmit_data_off = 0;
  340. conn->c_xmit_rdma_sent = 0;
  341. conn->c_xmit_atomic_sent = 0;
  342. conn->c_xmit_data_sent = 0;
  343. rds_message_put(rm);
  344. }
  345. }
  346. over_batch:
  347. if (conn->c_trans->xmit_complete)
  348. conn->c_trans->xmit_complete(conn);
  349. release_in_xmit(conn);
  350. /* Nuke any messages we decided not to retransmit. */
  351. if (!list_empty(&to_be_dropped)) {
  352. /* irqs on here, so we can put(), unlike above */
  353. list_for_each_entry(rm, &to_be_dropped, m_conn_item)
  354. rds_message_put(rm);
  355. rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED);
  356. }
  357. /*
  358. * Other senders can queue a message after we last test the send queue
  359. * but before we clear RDS_IN_XMIT. In that case they'd back off and
  360. * not try and send their newly queued message. We need to check the
  361. * send queue after having cleared RDS_IN_XMIT so that their message
  362. * doesn't get stuck on the send queue.
  363. *
  364. * If the transport cannot continue (i.e ret != 0), then it must
  365. * call us when more room is available, such as from the tx
  366. * completion handler.
  367. *
  368. * We have an extra generation check here so that if someone manages
  369. * to jump in after our release_in_xmit, we'll see that they have done
  370. * some work and we will skip our goto
  371. */
  372. if (ret == 0) {
  373. smp_mb();
  374. if ((test_bit(0, &conn->c_map_queued) ||
  375. !list_empty(&conn->c_send_queue)) &&
  376. send_gen == conn->c_send_gen) {
  377. rds_stats_inc(s_send_lock_queue_raced);
  378. if (batch_count < send_batch_count)
  379. goto restart;
  380. queue_delayed_work(rds_wq, &conn->c_send_w, 1);
  381. }
  382. }
  383. out:
  384. return ret;
  385. }
  386. static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
  387. {
  388. u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  389. assert_spin_locked(&rs->rs_lock);
  390. BUG_ON(rs->rs_snd_bytes < len);
  391. rs->rs_snd_bytes -= len;
  392. if (rs->rs_snd_bytes == 0)
  393. rds_stats_inc(s_send_queue_empty);
  394. }
  395. static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
  396. is_acked_func is_acked)
  397. {
  398. if (is_acked)
  399. return is_acked(rm, ack);
  400. return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
  401. }
  402. /*
  403. * This is pretty similar to what happens below in the ACK
  404. * handling code - except that we call here as soon as we get
  405. * the IB send completion on the RDMA op and the accompanying
  406. * message.
  407. */
  408. void rds_rdma_send_complete(struct rds_message *rm, int status)
  409. {
  410. struct rds_sock *rs = NULL;
  411. struct rm_rdma_op *ro;
  412. struct rds_notifier *notifier;
  413. unsigned long flags;
  414. spin_lock_irqsave(&rm->m_rs_lock, flags);
  415. ro = &rm->rdma;
  416. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
  417. ro->op_active && ro->op_notify && ro->op_notifier) {
  418. notifier = ro->op_notifier;
  419. rs = rm->m_rs;
  420. sock_hold(rds_rs_to_sk(rs));
  421. notifier->n_status = status;
  422. spin_lock(&rs->rs_lock);
  423. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  424. spin_unlock(&rs->rs_lock);
  425. ro->op_notifier = NULL;
  426. }
  427. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  428. if (rs) {
  429. rds_wake_sk_sleep(rs);
  430. sock_put(rds_rs_to_sk(rs));
  431. }
  432. }
  433. EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
  434. /*
  435. * Just like above, except looks at atomic op
  436. */
  437. void rds_atomic_send_complete(struct rds_message *rm, int status)
  438. {
  439. struct rds_sock *rs = NULL;
  440. struct rm_atomic_op *ao;
  441. struct rds_notifier *notifier;
  442. unsigned long flags;
  443. spin_lock_irqsave(&rm->m_rs_lock, flags);
  444. ao = &rm->atomic;
  445. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
  446. && ao->op_active && ao->op_notify && ao->op_notifier) {
  447. notifier = ao->op_notifier;
  448. rs = rm->m_rs;
  449. sock_hold(rds_rs_to_sk(rs));
  450. notifier->n_status = status;
  451. spin_lock(&rs->rs_lock);
  452. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  453. spin_unlock(&rs->rs_lock);
  454. ao->op_notifier = NULL;
  455. }
  456. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  457. if (rs) {
  458. rds_wake_sk_sleep(rs);
  459. sock_put(rds_rs_to_sk(rs));
  460. }
  461. }
  462. EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
  463. /*
  464. * This is the same as rds_rdma_send_complete except we
  465. * don't do any locking - we have all the ingredients (message,
  466. * socket, socket lock) and can just move the notifier.
  467. */
  468. static inline void
  469. __rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
  470. {
  471. struct rm_rdma_op *ro;
  472. struct rm_atomic_op *ao;
  473. ro = &rm->rdma;
  474. if (ro->op_active && ro->op_notify && ro->op_notifier) {
  475. ro->op_notifier->n_status = status;
  476. list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
  477. ro->op_notifier = NULL;
  478. }
  479. ao = &rm->atomic;
  480. if (ao->op_active && ao->op_notify && ao->op_notifier) {
  481. ao->op_notifier->n_status = status;
  482. list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
  483. ao->op_notifier = NULL;
  484. }
  485. /* No need to wake the app - caller does this */
  486. }
  487. /*
  488. * This is called from the IB send completion when we detect
  489. * a RDMA operation that failed with remote access error.
  490. * So speed is not an issue here.
  491. */
  492. struct rds_message *rds_send_get_message(struct rds_connection *conn,
  493. struct rm_rdma_op *op)
  494. {
  495. struct rds_message *rm, *tmp, *found = NULL;
  496. unsigned long flags;
  497. spin_lock_irqsave(&conn->c_lock, flags);
  498. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  499. if (&rm->rdma == op) {
  500. atomic_inc(&rm->m_refcount);
  501. found = rm;
  502. goto out;
  503. }
  504. }
  505. list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
  506. if (&rm->rdma == op) {
  507. atomic_inc(&rm->m_refcount);
  508. found = rm;
  509. break;
  510. }
  511. }
  512. out:
  513. spin_unlock_irqrestore(&conn->c_lock, flags);
  514. return found;
  515. }
  516. EXPORT_SYMBOL_GPL(rds_send_get_message);
  517. /*
  518. * This removes messages from the socket's list if they're on it. The list
  519. * argument must be private to the caller, we must be able to modify it
  520. * without locks. The messages must have a reference held for their
  521. * position on the list. This function will drop that reference after
  522. * removing the messages from the 'messages' list regardless of if it found
  523. * the messages on the socket list or not.
  524. */
  525. static void rds_send_remove_from_sock(struct list_head *messages, int status)
  526. {
  527. unsigned long flags;
  528. struct rds_sock *rs = NULL;
  529. struct rds_message *rm;
  530. while (!list_empty(messages)) {
  531. int was_on_sock = 0;
  532. rm = list_entry(messages->next, struct rds_message,
  533. m_conn_item);
  534. list_del_init(&rm->m_conn_item);
  535. /*
  536. * If we see this flag cleared then we're *sure* that someone
  537. * else beat us to removing it from the sock. If we race
  538. * with their flag update we'll get the lock and then really
  539. * see that the flag has been cleared.
  540. *
  541. * The message spinlock makes sure nobody clears rm->m_rs
  542. * while we're messing with it. It does not prevent the
  543. * message from being removed from the socket, though.
  544. */
  545. spin_lock_irqsave(&rm->m_rs_lock, flags);
  546. if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
  547. goto unlock_and_drop;
  548. if (rs != rm->m_rs) {
  549. if (rs) {
  550. rds_wake_sk_sleep(rs);
  551. sock_put(rds_rs_to_sk(rs));
  552. }
  553. rs = rm->m_rs;
  554. if (rs)
  555. sock_hold(rds_rs_to_sk(rs));
  556. }
  557. if (!rs)
  558. goto unlock_and_drop;
  559. spin_lock(&rs->rs_lock);
  560. if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
  561. struct rm_rdma_op *ro = &rm->rdma;
  562. struct rds_notifier *notifier;
  563. list_del_init(&rm->m_sock_item);
  564. rds_send_sndbuf_remove(rs, rm);
  565. if (ro->op_active && ro->op_notifier &&
  566. (ro->op_notify || (ro->op_recverr && status))) {
  567. notifier = ro->op_notifier;
  568. list_add_tail(&notifier->n_list,
  569. &rs->rs_notify_queue);
  570. if (!notifier->n_status)
  571. notifier->n_status = status;
  572. rm->rdma.op_notifier = NULL;
  573. }
  574. was_on_sock = 1;
  575. rm->m_rs = NULL;
  576. }
  577. spin_unlock(&rs->rs_lock);
  578. unlock_and_drop:
  579. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  580. rds_message_put(rm);
  581. if (was_on_sock)
  582. rds_message_put(rm);
  583. }
  584. if (rs) {
  585. rds_wake_sk_sleep(rs);
  586. sock_put(rds_rs_to_sk(rs));
  587. }
  588. }
  589. /*
  590. * Transports call here when they've determined that the receiver queued
  591. * messages up to, and including, the given sequence number. Messages are
  592. * moved to the retrans queue when rds_send_xmit picks them off the send
  593. * queue. This means that in the TCP case, the message may not have been
  594. * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
  595. * checks the RDS_MSG_HAS_ACK_SEQ bit.
  596. */
  597. void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
  598. is_acked_func is_acked)
  599. {
  600. struct rds_message *rm, *tmp;
  601. unsigned long flags;
  602. LIST_HEAD(list);
  603. spin_lock_irqsave(&conn->c_lock, flags);
  604. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  605. if (!rds_send_is_acked(rm, ack, is_acked))
  606. break;
  607. list_move(&rm->m_conn_item, &list);
  608. clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  609. }
  610. /* order flag updates with spin locks */
  611. if (!list_empty(&list))
  612. smp_mb__after_atomic();
  613. spin_unlock_irqrestore(&conn->c_lock, flags);
  614. /* now remove the messages from the sock list as needed */
  615. rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
  616. }
  617. EXPORT_SYMBOL_GPL(rds_send_drop_acked);
  618. void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
  619. {
  620. struct rds_message *rm, *tmp;
  621. struct rds_connection *conn;
  622. unsigned long flags;
  623. LIST_HEAD(list);
  624. /* get all the messages we're dropping under the rs lock */
  625. spin_lock_irqsave(&rs->rs_lock, flags);
  626. list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
  627. if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
  628. dest->sin_port != rm->m_inc.i_hdr.h_dport))
  629. continue;
  630. list_move(&rm->m_sock_item, &list);
  631. rds_send_sndbuf_remove(rs, rm);
  632. clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  633. }
  634. /* order flag updates with the rs lock */
  635. smp_mb__after_atomic();
  636. spin_unlock_irqrestore(&rs->rs_lock, flags);
  637. if (list_empty(&list))
  638. return;
  639. /* Remove the messages from the conn */
  640. list_for_each_entry(rm, &list, m_sock_item) {
  641. conn = rm->m_inc.i_conn;
  642. spin_lock_irqsave(&conn->c_lock, flags);
  643. /*
  644. * Maybe someone else beat us to removing rm from the conn.
  645. * If we race with their flag update we'll get the lock and
  646. * then really see that the flag has been cleared.
  647. */
  648. if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
  649. spin_unlock_irqrestore(&conn->c_lock, flags);
  650. spin_lock_irqsave(&rm->m_rs_lock, flags);
  651. rm->m_rs = NULL;
  652. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  653. continue;
  654. }
  655. list_del_init(&rm->m_conn_item);
  656. spin_unlock_irqrestore(&conn->c_lock, flags);
  657. /*
  658. * Couldn't grab m_rs_lock in top loop (lock ordering),
  659. * but we can now.
  660. */
  661. spin_lock_irqsave(&rm->m_rs_lock, flags);
  662. spin_lock(&rs->rs_lock);
  663. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  664. spin_unlock(&rs->rs_lock);
  665. rm->m_rs = NULL;
  666. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  667. rds_message_put(rm);
  668. }
  669. rds_wake_sk_sleep(rs);
  670. while (!list_empty(&list)) {
  671. rm = list_entry(list.next, struct rds_message, m_sock_item);
  672. list_del_init(&rm->m_sock_item);
  673. rds_message_wait(rm);
  674. /* just in case the code above skipped this message
  675. * because RDS_MSG_ON_CONN wasn't set, run it again here
  676. * taking m_rs_lock is the only thing that keeps us
  677. * from racing with ack processing.
  678. */
  679. spin_lock_irqsave(&rm->m_rs_lock, flags);
  680. spin_lock(&rs->rs_lock);
  681. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  682. spin_unlock(&rs->rs_lock);
  683. rm->m_rs = NULL;
  684. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  685. rds_message_put(rm);
  686. }
  687. }
  688. /*
  689. * we only want this to fire once so we use the callers 'queued'. It's
  690. * possible that another thread can race with us and remove the
  691. * message from the flow with RDS_CANCEL_SENT_TO.
  692. */
  693. static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
  694. struct rds_message *rm, __be16 sport,
  695. __be16 dport, int *queued)
  696. {
  697. unsigned long flags;
  698. u32 len;
  699. if (*queued)
  700. goto out;
  701. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  702. /* this is the only place which holds both the socket's rs_lock
  703. * and the connection's c_lock */
  704. spin_lock_irqsave(&rs->rs_lock, flags);
  705. /*
  706. * If there is a little space in sndbuf, we don't queue anything,
  707. * and userspace gets -EAGAIN. But poll() indicates there's send
  708. * room. This can lead to bad behavior (spinning) if snd_bytes isn't
  709. * freed up by incoming acks. So we check the *old* value of
  710. * rs_snd_bytes here to allow the last msg to exceed the buffer,
  711. * and poll() now knows no more data can be sent.
  712. */
  713. if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
  714. rs->rs_snd_bytes += len;
  715. /* let recv side know we are close to send space exhaustion.
  716. * This is probably not the optimal way to do it, as this
  717. * means we set the flag on *all* messages as soon as our
  718. * throughput hits a certain threshold.
  719. */
  720. if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
  721. __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  722. list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
  723. set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  724. rds_message_addref(rm);
  725. rm->m_rs = rs;
  726. /* The code ordering is a little weird, but we're
  727. trying to minimize the time we hold c_lock */
  728. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
  729. rm->m_inc.i_conn = conn;
  730. rds_message_addref(rm);
  731. spin_lock(&conn->c_lock);
  732. rm->m_inc.i_hdr.h_sequence = cpu_to_be64(conn->c_next_tx_seq++);
  733. list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
  734. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  735. spin_unlock(&conn->c_lock);
  736. rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
  737. rm, len, rs, rs->rs_snd_bytes,
  738. (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
  739. *queued = 1;
  740. }
  741. spin_unlock_irqrestore(&rs->rs_lock, flags);
  742. out:
  743. return *queued;
  744. }
  745. /*
  746. * rds_message is getting to be quite complicated, and we'd like to allocate
  747. * it all in one go. This figures out how big it needs to be up front.
  748. */
  749. static int rds_rm_size(struct msghdr *msg, int data_len)
  750. {
  751. struct cmsghdr *cmsg;
  752. int size = 0;
  753. int cmsg_groups = 0;
  754. int retval;
  755. for_each_cmsghdr(cmsg, msg) {
  756. if (!CMSG_OK(msg, cmsg))
  757. return -EINVAL;
  758. if (cmsg->cmsg_level != SOL_RDS)
  759. continue;
  760. switch (cmsg->cmsg_type) {
  761. case RDS_CMSG_RDMA_ARGS:
  762. cmsg_groups |= 1;
  763. retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
  764. if (retval < 0)
  765. return retval;
  766. size += retval;
  767. break;
  768. case RDS_CMSG_RDMA_DEST:
  769. case RDS_CMSG_RDMA_MAP:
  770. cmsg_groups |= 2;
  771. /* these are valid but do no add any size */
  772. break;
  773. case RDS_CMSG_ATOMIC_CSWP:
  774. case RDS_CMSG_ATOMIC_FADD:
  775. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  776. case RDS_CMSG_MASKED_ATOMIC_FADD:
  777. cmsg_groups |= 1;
  778. size += sizeof(struct scatterlist);
  779. break;
  780. default:
  781. return -EINVAL;
  782. }
  783. }
  784. size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
  785. /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
  786. if (cmsg_groups == 3)
  787. return -EINVAL;
  788. return size;
  789. }
  790. static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
  791. struct msghdr *msg, int *allocated_mr)
  792. {
  793. struct cmsghdr *cmsg;
  794. int ret = 0;
  795. for_each_cmsghdr(cmsg, msg) {
  796. if (!CMSG_OK(msg, cmsg))
  797. return -EINVAL;
  798. if (cmsg->cmsg_level != SOL_RDS)
  799. continue;
  800. /* As a side effect, RDMA_DEST and RDMA_MAP will set
  801. * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
  802. */
  803. switch (cmsg->cmsg_type) {
  804. case RDS_CMSG_RDMA_ARGS:
  805. ret = rds_cmsg_rdma_args(rs, rm, cmsg);
  806. break;
  807. case RDS_CMSG_RDMA_DEST:
  808. ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
  809. break;
  810. case RDS_CMSG_RDMA_MAP:
  811. ret = rds_cmsg_rdma_map(rs, rm, cmsg);
  812. if (!ret)
  813. *allocated_mr = 1;
  814. break;
  815. case RDS_CMSG_ATOMIC_CSWP:
  816. case RDS_CMSG_ATOMIC_FADD:
  817. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  818. case RDS_CMSG_MASKED_ATOMIC_FADD:
  819. ret = rds_cmsg_atomic(rs, rm, cmsg);
  820. break;
  821. default:
  822. return -EINVAL;
  823. }
  824. if (ret)
  825. break;
  826. }
  827. return ret;
  828. }
  829. int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len)
  830. {
  831. struct sock *sk = sock->sk;
  832. struct rds_sock *rs = rds_sk_to_rs(sk);
  833. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  834. __be32 daddr;
  835. __be16 dport;
  836. struct rds_message *rm = NULL;
  837. struct rds_connection *conn;
  838. int ret = 0;
  839. int queued = 0, allocated_mr = 0;
  840. int nonblock = msg->msg_flags & MSG_DONTWAIT;
  841. long timeo = sock_sndtimeo(sk, nonblock);
  842. /* Mirror Linux UDP mirror of BSD error message compatibility */
  843. /* XXX: Perhaps MSG_MORE someday */
  844. if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
  845. ret = -EOPNOTSUPP;
  846. goto out;
  847. }
  848. if (msg->msg_namelen) {
  849. /* XXX fail non-unicast destination IPs? */
  850. if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
  851. ret = -EINVAL;
  852. goto out;
  853. }
  854. daddr = usin->sin_addr.s_addr;
  855. dport = usin->sin_port;
  856. } else {
  857. /* We only care about consistency with ->connect() */
  858. lock_sock(sk);
  859. daddr = rs->rs_conn_addr;
  860. dport = rs->rs_conn_port;
  861. release_sock(sk);
  862. }
  863. /* racing with another thread binding seems ok here */
  864. if (daddr == 0 || rs->rs_bound_addr == 0) {
  865. ret = -ENOTCONN; /* XXX not a great errno */
  866. goto out;
  867. }
  868. if (payload_len > rds_sk_sndbuf(rs)) {
  869. ret = -EMSGSIZE;
  870. goto out;
  871. }
  872. /* size of rm including all sgs */
  873. ret = rds_rm_size(msg, payload_len);
  874. if (ret < 0)
  875. goto out;
  876. rm = rds_message_alloc(ret, GFP_KERNEL);
  877. if (!rm) {
  878. ret = -ENOMEM;
  879. goto out;
  880. }
  881. /* Attach data to the rm */
  882. if (payload_len) {
  883. rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
  884. if (!rm->data.op_sg) {
  885. ret = -ENOMEM;
  886. goto out;
  887. }
  888. ret = rds_message_copy_from_user(rm, &msg->msg_iter);
  889. if (ret)
  890. goto out;
  891. }
  892. rm->data.op_active = 1;
  893. rm->m_daddr = daddr;
  894. /* rds_conn_create has a spinlock that runs with IRQ off.
  895. * Caching the conn in the socket helps a lot. */
  896. if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
  897. conn = rs->rs_conn;
  898. else {
  899. conn = rds_conn_create_outgoing(sock_net(sock->sk),
  900. rs->rs_bound_addr, daddr,
  901. rs->rs_transport,
  902. sock->sk->sk_allocation);
  903. if (IS_ERR(conn)) {
  904. ret = PTR_ERR(conn);
  905. goto out;
  906. }
  907. rs->rs_conn = conn;
  908. }
  909. /* Parse any control messages the user may have included. */
  910. ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
  911. if (ret)
  912. goto out;
  913. if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
  914. printk_ratelimited(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
  915. &rm->rdma, conn->c_trans->xmit_rdma);
  916. ret = -EOPNOTSUPP;
  917. goto out;
  918. }
  919. if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
  920. printk_ratelimited(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
  921. &rm->atomic, conn->c_trans->xmit_atomic);
  922. ret = -EOPNOTSUPP;
  923. goto out;
  924. }
  925. rds_conn_connect_if_down(conn);
  926. ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
  927. if (ret) {
  928. rs->rs_seen_congestion = 1;
  929. goto out;
  930. }
  931. while (!rds_send_queue_rm(rs, conn, rm, rs->rs_bound_port,
  932. dport, &queued)) {
  933. rds_stats_inc(s_send_queue_full);
  934. if (nonblock) {
  935. ret = -EAGAIN;
  936. goto out;
  937. }
  938. timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
  939. rds_send_queue_rm(rs, conn, rm,
  940. rs->rs_bound_port,
  941. dport,
  942. &queued),
  943. timeo);
  944. rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
  945. if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
  946. continue;
  947. ret = timeo;
  948. if (ret == 0)
  949. ret = -ETIMEDOUT;
  950. goto out;
  951. }
  952. /*
  953. * By now we've committed to the send. We reuse rds_send_worker()
  954. * to retry sends in the rds thread if the transport asks us to.
  955. */
  956. rds_stats_inc(s_send_queued);
  957. ret = rds_send_xmit(conn);
  958. if (ret == -ENOMEM || ret == -EAGAIN)
  959. queue_delayed_work(rds_wq, &conn->c_send_w, 1);
  960. rds_message_put(rm);
  961. return payload_len;
  962. out:
  963. /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
  964. * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
  965. * or in any other way, we need to destroy the MR again */
  966. if (allocated_mr)
  967. rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
  968. if (rm)
  969. rds_message_put(rm);
  970. return ret;
  971. }
  972. /*
  973. * Reply to a ping packet.
  974. */
  975. int
  976. rds_send_pong(struct rds_connection *conn, __be16 dport)
  977. {
  978. struct rds_message *rm;
  979. unsigned long flags;
  980. int ret = 0;
  981. rm = rds_message_alloc(0, GFP_ATOMIC);
  982. if (!rm) {
  983. ret = -ENOMEM;
  984. goto out;
  985. }
  986. rm->m_daddr = conn->c_faddr;
  987. rm->data.op_active = 1;
  988. rds_conn_connect_if_down(conn);
  989. ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
  990. if (ret)
  991. goto out;
  992. spin_lock_irqsave(&conn->c_lock, flags);
  993. list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
  994. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  995. rds_message_addref(rm);
  996. rm->m_inc.i_conn = conn;
  997. rds_message_populate_header(&rm->m_inc.i_hdr, 0, dport,
  998. conn->c_next_tx_seq);
  999. conn->c_next_tx_seq++;
  1000. spin_unlock_irqrestore(&conn->c_lock, flags);
  1001. rds_stats_inc(s_send_queued);
  1002. rds_stats_inc(s_send_pong);
  1003. ret = rds_send_xmit(conn);
  1004. if (ret == -ENOMEM || ret == -EAGAIN)
  1005. queue_delayed_work(rds_wq, &conn->c_send_w, 1);
  1006. rds_message_put(rm);
  1007. return 0;
  1008. out:
  1009. if (rm)
  1010. rds_message_put(rm);
  1011. return ret;
  1012. }