send.c 32 KB

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