send.c 31 KB

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