send.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. EXPORT_SYMBOL_GPL(rds_send_xmit);
  387. static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
  388. {
  389. u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  390. assert_spin_locked(&rs->rs_lock);
  391. BUG_ON(rs->rs_snd_bytes < len);
  392. rs->rs_snd_bytes -= len;
  393. if (rs->rs_snd_bytes == 0)
  394. rds_stats_inc(s_send_queue_empty);
  395. }
  396. static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
  397. is_acked_func is_acked)
  398. {
  399. if (is_acked)
  400. return is_acked(rm, ack);
  401. return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
  402. }
  403. /*
  404. * This is pretty similar to what happens below in the ACK
  405. * handling code - except that we call here as soon as we get
  406. * the IB send completion on the RDMA op and the accompanying
  407. * message.
  408. */
  409. void rds_rdma_send_complete(struct rds_message *rm, int status)
  410. {
  411. struct rds_sock *rs = NULL;
  412. struct rm_rdma_op *ro;
  413. struct rds_notifier *notifier;
  414. unsigned long flags;
  415. spin_lock_irqsave(&rm->m_rs_lock, flags);
  416. ro = &rm->rdma;
  417. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
  418. ro->op_active && ro->op_notify && ro->op_notifier) {
  419. notifier = ro->op_notifier;
  420. rs = rm->m_rs;
  421. sock_hold(rds_rs_to_sk(rs));
  422. notifier->n_status = status;
  423. spin_lock(&rs->rs_lock);
  424. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  425. spin_unlock(&rs->rs_lock);
  426. ro->op_notifier = NULL;
  427. }
  428. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  429. if (rs) {
  430. rds_wake_sk_sleep(rs);
  431. sock_put(rds_rs_to_sk(rs));
  432. }
  433. }
  434. EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
  435. /*
  436. * Just like above, except looks at atomic op
  437. */
  438. void rds_atomic_send_complete(struct rds_message *rm, int status)
  439. {
  440. struct rds_sock *rs = NULL;
  441. struct rm_atomic_op *ao;
  442. struct rds_notifier *notifier;
  443. unsigned long flags;
  444. spin_lock_irqsave(&rm->m_rs_lock, flags);
  445. ao = &rm->atomic;
  446. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
  447. && ao->op_active && ao->op_notify && ao->op_notifier) {
  448. notifier = ao->op_notifier;
  449. rs = rm->m_rs;
  450. sock_hold(rds_rs_to_sk(rs));
  451. notifier->n_status = status;
  452. spin_lock(&rs->rs_lock);
  453. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  454. spin_unlock(&rs->rs_lock);
  455. ao->op_notifier = NULL;
  456. }
  457. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  458. if (rs) {
  459. rds_wake_sk_sleep(rs);
  460. sock_put(rds_rs_to_sk(rs));
  461. }
  462. }
  463. EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
  464. /*
  465. * This is the same as rds_rdma_send_complete except we
  466. * don't do any locking - we have all the ingredients (message,
  467. * socket, socket lock) and can just move the notifier.
  468. */
  469. static inline void
  470. __rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
  471. {
  472. struct rm_rdma_op *ro;
  473. struct rm_atomic_op *ao;
  474. ro = &rm->rdma;
  475. if (ro->op_active && ro->op_notify && ro->op_notifier) {
  476. ro->op_notifier->n_status = status;
  477. list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
  478. ro->op_notifier = NULL;
  479. }
  480. ao = &rm->atomic;
  481. if (ao->op_active && ao->op_notify && ao->op_notifier) {
  482. ao->op_notifier->n_status = status;
  483. list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
  484. ao->op_notifier = NULL;
  485. }
  486. /* No need to wake the app - caller does this */
  487. }
  488. /*
  489. * This is called from the IB send completion when we detect
  490. * a RDMA operation that failed with remote access error.
  491. * So speed is not an issue here.
  492. */
  493. struct rds_message *rds_send_get_message(struct rds_connection *conn,
  494. struct rm_rdma_op *op)
  495. {
  496. struct rds_message *rm, *tmp, *found = NULL;
  497. unsigned long flags;
  498. spin_lock_irqsave(&conn->c_lock, flags);
  499. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  500. if (&rm->rdma == op) {
  501. atomic_inc(&rm->m_refcount);
  502. found = rm;
  503. goto out;
  504. }
  505. }
  506. list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
  507. if (&rm->rdma == op) {
  508. atomic_inc(&rm->m_refcount);
  509. found = rm;
  510. break;
  511. }
  512. }
  513. out:
  514. spin_unlock_irqrestore(&conn->c_lock, flags);
  515. return found;
  516. }
  517. EXPORT_SYMBOL_GPL(rds_send_get_message);
  518. /*
  519. * This removes messages from the socket's list if they're on it. The list
  520. * argument must be private to the caller, we must be able to modify it
  521. * without locks. The messages must have a reference held for their
  522. * position on the list. This function will drop that reference after
  523. * removing the messages from the 'messages' list regardless of if it found
  524. * the messages on the socket list or not.
  525. */
  526. static void rds_send_remove_from_sock(struct list_head *messages, int status)
  527. {
  528. unsigned long flags;
  529. struct rds_sock *rs = NULL;
  530. struct rds_message *rm;
  531. while (!list_empty(messages)) {
  532. int was_on_sock = 0;
  533. rm = list_entry(messages->next, struct rds_message,
  534. m_conn_item);
  535. list_del_init(&rm->m_conn_item);
  536. /*
  537. * If we see this flag cleared then we're *sure* that someone
  538. * else beat us to removing it from the sock. If we race
  539. * with their flag update we'll get the lock and then really
  540. * see that the flag has been cleared.
  541. *
  542. * The message spinlock makes sure nobody clears rm->m_rs
  543. * while we're messing with it. It does not prevent the
  544. * message from being removed from the socket, though.
  545. */
  546. spin_lock_irqsave(&rm->m_rs_lock, flags);
  547. if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
  548. goto unlock_and_drop;
  549. if (rs != rm->m_rs) {
  550. if (rs) {
  551. rds_wake_sk_sleep(rs);
  552. sock_put(rds_rs_to_sk(rs));
  553. }
  554. rs = rm->m_rs;
  555. if (rs)
  556. sock_hold(rds_rs_to_sk(rs));
  557. }
  558. if (!rs)
  559. goto unlock_and_drop;
  560. spin_lock(&rs->rs_lock);
  561. if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
  562. struct rm_rdma_op *ro = &rm->rdma;
  563. struct rds_notifier *notifier;
  564. list_del_init(&rm->m_sock_item);
  565. rds_send_sndbuf_remove(rs, rm);
  566. if (ro->op_active && ro->op_notifier &&
  567. (ro->op_notify || (ro->op_recverr && status))) {
  568. notifier = ro->op_notifier;
  569. list_add_tail(&notifier->n_list,
  570. &rs->rs_notify_queue);
  571. if (!notifier->n_status)
  572. notifier->n_status = status;
  573. rm->rdma.op_notifier = NULL;
  574. }
  575. was_on_sock = 1;
  576. rm->m_rs = NULL;
  577. }
  578. spin_unlock(&rs->rs_lock);
  579. unlock_and_drop:
  580. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  581. rds_message_put(rm);
  582. if (was_on_sock)
  583. rds_message_put(rm);
  584. }
  585. if (rs) {
  586. rds_wake_sk_sleep(rs);
  587. sock_put(rds_rs_to_sk(rs));
  588. }
  589. }
  590. /*
  591. * Transports call here when they've determined that the receiver queued
  592. * messages up to, and including, the given sequence number. Messages are
  593. * moved to the retrans queue when rds_send_xmit picks them off the send
  594. * queue. This means that in the TCP case, the message may not have been
  595. * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
  596. * checks the RDS_MSG_HAS_ACK_SEQ bit.
  597. */
  598. void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
  599. is_acked_func is_acked)
  600. {
  601. struct rds_message *rm, *tmp;
  602. unsigned long flags;
  603. LIST_HEAD(list);
  604. spin_lock_irqsave(&conn->c_lock, flags);
  605. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  606. if (!rds_send_is_acked(rm, ack, is_acked))
  607. break;
  608. list_move(&rm->m_conn_item, &list);
  609. clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  610. }
  611. /* order flag updates with spin locks */
  612. if (!list_empty(&list))
  613. smp_mb__after_atomic();
  614. spin_unlock_irqrestore(&conn->c_lock, flags);
  615. /* now remove the messages from the sock list as needed */
  616. rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
  617. }
  618. EXPORT_SYMBOL_GPL(rds_send_drop_acked);
  619. void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
  620. {
  621. struct rds_message *rm, *tmp;
  622. struct rds_connection *conn;
  623. unsigned long flags;
  624. LIST_HEAD(list);
  625. /* get all the messages we're dropping under the rs lock */
  626. spin_lock_irqsave(&rs->rs_lock, flags);
  627. list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
  628. if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
  629. dest->sin_port != rm->m_inc.i_hdr.h_dport))
  630. continue;
  631. list_move(&rm->m_sock_item, &list);
  632. rds_send_sndbuf_remove(rs, rm);
  633. clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  634. }
  635. /* order flag updates with the rs lock */
  636. smp_mb__after_atomic();
  637. spin_unlock_irqrestore(&rs->rs_lock, flags);
  638. if (list_empty(&list))
  639. return;
  640. /* Remove the messages from the conn */
  641. list_for_each_entry(rm, &list, m_sock_item) {
  642. conn = rm->m_inc.i_conn;
  643. spin_lock_irqsave(&conn->c_lock, flags);
  644. /*
  645. * Maybe someone else beat us to removing rm from the conn.
  646. * If we race with their flag update we'll get the lock and
  647. * then really see that the flag has been cleared.
  648. */
  649. if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
  650. spin_unlock_irqrestore(&conn->c_lock, flags);
  651. spin_lock_irqsave(&rm->m_rs_lock, flags);
  652. rm->m_rs = NULL;
  653. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  654. continue;
  655. }
  656. list_del_init(&rm->m_conn_item);
  657. spin_unlock_irqrestore(&conn->c_lock, flags);
  658. /*
  659. * Couldn't grab m_rs_lock in top loop (lock ordering),
  660. * but we can now.
  661. */
  662. spin_lock_irqsave(&rm->m_rs_lock, flags);
  663. spin_lock(&rs->rs_lock);
  664. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  665. spin_unlock(&rs->rs_lock);
  666. rm->m_rs = NULL;
  667. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  668. rds_message_put(rm);
  669. }
  670. rds_wake_sk_sleep(rs);
  671. while (!list_empty(&list)) {
  672. rm = list_entry(list.next, struct rds_message, m_sock_item);
  673. list_del_init(&rm->m_sock_item);
  674. rds_message_wait(rm);
  675. /* just in case the code above skipped this message
  676. * because RDS_MSG_ON_CONN wasn't set, run it again here
  677. * taking m_rs_lock is the only thing that keeps us
  678. * from racing with ack processing.
  679. */
  680. spin_lock_irqsave(&rm->m_rs_lock, flags);
  681. spin_lock(&rs->rs_lock);
  682. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  683. spin_unlock(&rs->rs_lock);
  684. rm->m_rs = NULL;
  685. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  686. rds_message_put(rm);
  687. }
  688. }
  689. /*
  690. * we only want this to fire once so we use the callers 'queued'. It's
  691. * possible that another thread can race with us and remove the
  692. * message from the flow with RDS_CANCEL_SENT_TO.
  693. */
  694. static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
  695. struct rds_message *rm, __be16 sport,
  696. __be16 dport, int *queued)
  697. {
  698. unsigned long flags;
  699. u32 len;
  700. if (*queued)
  701. goto out;
  702. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  703. /* this is the only place which holds both the socket's rs_lock
  704. * and the connection's c_lock */
  705. spin_lock_irqsave(&rs->rs_lock, flags);
  706. /*
  707. * If there is a little space in sndbuf, we don't queue anything,
  708. * and userspace gets -EAGAIN. But poll() indicates there's send
  709. * room. This can lead to bad behavior (spinning) if snd_bytes isn't
  710. * freed up by incoming acks. So we check the *old* value of
  711. * rs_snd_bytes here to allow the last msg to exceed the buffer,
  712. * and poll() now knows no more data can be sent.
  713. */
  714. if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
  715. rs->rs_snd_bytes += len;
  716. /* let recv side know we are close to send space exhaustion.
  717. * This is probably not the optimal way to do it, as this
  718. * means we set the flag on *all* messages as soon as our
  719. * throughput hits a certain threshold.
  720. */
  721. if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
  722. __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  723. list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
  724. set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  725. rds_message_addref(rm);
  726. rm->m_rs = rs;
  727. /* The code ordering is a little weird, but we're
  728. trying to minimize the time we hold c_lock */
  729. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
  730. rm->m_inc.i_conn = conn;
  731. rds_message_addref(rm);
  732. spin_lock(&conn->c_lock);
  733. rm->m_inc.i_hdr.h_sequence = cpu_to_be64(conn->c_next_tx_seq++);
  734. list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
  735. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  736. spin_unlock(&conn->c_lock);
  737. rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
  738. rm, len, rs, rs->rs_snd_bytes,
  739. (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
  740. *queued = 1;
  741. }
  742. spin_unlock_irqrestore(&rs->rs_lock, flags);
  743. out:
  744. return *queued;
  745. }
  746. /*
  747. * rds_message is getting to be quite complicated, and we'd like to allocate
  748. * it all in one go. This figures out how big it needs to be up front.
  749. */
  750. static int rds_rm_size(struct msghdr *msg, int data_len)
  751. {
  752. struct cmsghdr *cmsg;
  753. int size = 0;
  754. int cmsg_groups = 0;
  755. int retval;
  756. for_each_cmsghdr(cmsg, msg) {
  757. if (!CMSG_OK(msg, cmsg))
  758. return -EINVAL;
  759. if (cmsg->cmsg_level != SOL_RDS)
  760. continue;
  761. switch (cmsg->cmsg_type) {
  762. case RDS_CMSG_RDMA_ARGS:
  763. cmsg_groups |= 1;
  764. retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
  765. if (retval < 0)
  766. return retval;
  767. size += retval;
  768. break;
  769. case RDS_CMSG_RDMA_DEST:
  770. case RDS_CMSG_RDMA_MAP:
  771. cmsg_groups |= 2;
  772. /* these are valid but do no add any size */
  773. break;
  774. case RDS_CMSG_ATOMIC_CSWP:
  775. case RDS_CMSG_ATOMIC_FADD:
  776. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  777. case RDS_CMSG_MASKED_ATOMIC_FADD:
  778. cmsg_groups |= 1;
  779. size += sizeof(struct scatterlist);
  780. break;
  781. default:
  782. return -EINVAL;
  783. }
  784. }
  785. size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
  786. /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
  787. if (cmsg_groups == 3)
  788. return -EINVAL;
  789. return size;
  790. }
  791. static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
  792. struct msghdr *msg, int *allocated_mr)
  793. {
  794. struct cmsghdr *cmsg;
  795. int ret = 0;
  796. for_each_cmsghdr(cmsg, msg) {
  797. if (!CMSG_OK(msg, cmsg))
  798. return -EINVAL;
  799. if (cmsg->cmsg_level != SOL_RDS)
  800. continue;
  801. /* As a side effect, RDMA_DEST and RDMA_MAP will set
  802. * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
  803. */
  804. switch (cmsg->cmsg_type) {
  805. case RDS_CMSG_RDMA_ARGS:
  806. ret = rds_cmsg_rdma_args(rs, rm, cmsg);
  807. break;
  808. case RDS_CMSG_RDMA_DEST:
  809. ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
  810. break;
  811. case RDS_CMSG_RDMA_MAP:
  812. ret = rds_cmsg_rdma_map(rs, rm, cmsg);
  813. if (!ret)
  814. *allocated_mr = 1;
  815. break;
  816. case RDS_CMSG_ATOMIC_CSWP:
  817. case RDS_CMSG_ATOMIC_FADD:
  818. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  819. case RDS_CMSG_MASKED_ATOMIC_FADD:
  820. ret = rds_cmsg_atomic(rs, rm, cmsg);
  821. break;
  822. default:
  823. return -EINVAL;
  824. }
  825. if (ret)
  826. break;
  827. }
  828. return ret;
  829. }
  830. int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len)
  831. {
  832. struct sock *sk = sock->sk;
  833. struct rds_sock *rs = rds_sk_to_rs(sk);
  834. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  835. __be32 daddr;
  836. __be16 dport;
  837. struct rds_message *rm = NULL;
  838. struct rds_connection *conn;
  839. int ret = 0;
  840. int queued = 0, allocated_mr = 0;
  841. int nonblock = msg->msg_flags & MSG_DONTWAIT;
  842. long timeo = sock_sndtimeo(sk, nonblock);
  843. /* Mirror Linux UDP mirror of BSD error message compatibility */
  844. /* XXX: Perhaps MSG_MORE someday */
  845. if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
  846. ret = -EOPNOTSUPP;
  847. goto out;
  848. }
  849. if (msg->msg_namelen) {
  850. /* XXX fail non-unicast destination IPs? */
  851. if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
  852. ret = -EINVAL;
  853. goto out;
  854. }
  855. daddr = usin->sin_addr.s_addr;
  856. dport = usin->sin_port;
  857. } else {
  858. /* We only care about consistency with ->connect() */
  859. lock_sock(sk);
  860. daddr = rs->rs_conn_addr;
  861. dport = rs->rs_conn_port;
  862. release_sock(sk);
  863. }
  864. lock_sock(sk);
  865. if (daddr == 0 || rs->rs_bound_addr == 0) {
  866. release_sock(sk);
  867. ret = -ENOTCONN; /* XXX not a great errno */
  868. goto out;
  869. }
  870. release_sock(sk);
  871. if (payload_len > rds_sk_sndbuf(rs)) {
  872. ret = -EMSGSIZE;
  873. goto out;
  874. }
  875. /* size of rm including all sgs */
  876. ret = rds_rm_size(msg, payload_len);
  877. if (ret < 0)
  878. goto out;
  879. rm = rds_message_alloc(ret, GFP_KERNEL);
  880. if (!rm) {
  881. ret = -ENOMEM;
  882. goto out;
  883. }
  884. /* Attach data to the rm */
  885. if (payload_len) {
  886. rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
  887. if (!rm->data.op_sg) {
  888. ret = -ENOMEM;
  889. goto out;
  890. }
  891. ret = rds_message_copy_from_user(rm, &msg->msg_iter);
  892. if (ret)
  893. goto out;
  894. }
  895. rm->data.op_active = 1;
  896. rm->m_daddr = daddr;
  897. /* rds_conn_create has a spinlock that runs with IRQ off.
  898. * Caching the conn in the socket helps a lot. */
  899. if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
  900. conn = rs->rs_conn;
  901. else {
  902. conn = rds_conn_create_outgoing(sock_net(sock->sk),
  903. rs->rs_bound_addr, daddr,
  904. rs->rs_transport,
  905. sock->sk->sk_allocation);
  906. if (IS_ERR(conn)) {
  907. ret = PTR_ERR(conn);
  908. goto out;
  909. }
  910. rs->rs_conn = conn;
  911. }
  912. /* Parse any control messages the user may have included. */
  913. ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
  914. if (ret)
  915. goto out;
  916. if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
  917. printk_ratelimited(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
  918. &rm->rdma, conn->c_trans->xmit_rdma);
  919. ret = -EOPNOTSUPP;
  920. goto out;
  921. }
  922. if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
  923. printk_ratelimited(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
  924. &rm->atomic, conn->c_trans->xmit_atomic);
  925. ret = -EOPNOTSUPP;
  926. goto out;
  927. }
  928. rds_conn_connect_if_down(conn);
  929. ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
  930. if (ret) {
  931. rs->rs_seen_congestion = 1;
  932. goto out;
  933. }
  934. while (!rds_send_queue_rm(rs, conn, rm, rs->rs_bound_port,
  935. dport, &queued)) {
  936. rds_stats_inc(s_send_queue_full);
  937. if (nonblock) {
  938. ret = -EAGAIN;
  939. goto out;
  940. }
  941. timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
  942. rds_send_queue_rm(rs, conn, rm,
  943. rs->rs_bound_port,
  944. dport,
  945. &queued),
  946. timeo);
  947. rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
  948. if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
  949. continue;
  950. ret = timeo;
  951. if (ret == 0)
  952. ret = -ETIMEDOUT;
  953. goto out;
  954. }
  955. /*
  956. * By now we've committed to the send. We reuse rds_send_worker()
  957. * to retry sends in the rds thread if the transport asks us to.
  958. */
  959. rds_stats_inc(s_send_queued);
  960. ret = rds_send_xmit(conn);
  961. if (ret == -ENOMEM || ret == -EAGAIN)
  962. queue_delayed_work(rds_wq, &conn->c_send_w, 1);
  963. rds_message_put(rm);
  964. return payload_len;
  965. out:
  966. /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
  967. * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
  968. * or in any other way, we need to destroy the MR again */
  969. if (allocated_mr)
  970. rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
  971. if (rm)
  972. rds_message_put(rm);
  973. return ret;
  974. }
  975. /*
  976. * Reply to a ping packet.
  977. */
  978. int
  979. rds_send_pong(struct rds_connection *conn, __be16 dport)
  980. {
  981. struct rds_message *rm;
  982. unsigned long flags;
  983. int ret = 0;
  984. rm = rds_message_alloc(0, GFP_ATOMIC);
  985. if (!rm) {
  986. ret = -ENOMEM;
  987. goto out;
  988. }
  989. rm->m_daddr = conn->c_faddr;
  990. rm->data.op_active = 1;
  991. rds_conn_connect_if_down(conn);
  992. ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
  993. if (ret)
  994. goto out;
  995. spin_lock_irqsave(&conn->c_lock, flags);
  996. list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
  997. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  998. rds_message_addref(rm);
  999. rm->m_inc.i_conn = conn;
  1000. rds_message_populate_header(&rm->m_inc.i_hdr, 0, dport,
  1001. conn->c_next_tx_seq);
  1002. conn->c_next_tx_seq++;
  1003. spin_unlock_irqrestore(&conn->c_lock, flags);
  1004. rds_stats_inc(s_send_queued);
  1005. rds_stats_inc(s_send_pong);
  1006. /* schedule the send work on rds_wq */
  1007. queue_delayed_work(rds_wq, &conn->c_send_w, 1);
  1008. rds_message_put(rm);
  1009. return 0;
  1010. out:
  1011. if (rm)
  1012. rds_message_put(rm);
  1013. return ret;
  1014. }