1 /*
2 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33 #include <linux/kernel.h>
34 #include <linux/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
43 #include "rds.h"
44
45 /* When transmitting messages in rds_send_xmit, we need to emerge from
46 * time to time and briefly release the CPU. Otherwise the softlock watchdog
47 * will kick our shin.
48 * Also, it seems fairer to not let one busy connection stall all the
49 * others.
50 *
51 * send_batch_count is the number of times we'll loop in send_xmit. Setting
52 * it to 0 will restore the old behavior (where we looped until we had
53 * drained the queue).
54 */
55 static int send_batch_count = SZ_1K;
56 module_param(send_batch_count, int, 0444);
57 MODULE_PARM_DESC(send_batch_count, " batch factor when working the send queue");
58
59 static void rds_send_remove_from_sock(struct list_head *messages, int status);
60
61 /*
62 * Reset the send state. Callers must ensure that this doesn't race with
63 * rds_send_xmit().
64 */
rds_send_path_reset(struct rds_conn_path * cp)65 void rds_send_path_reset(struct rds_conn_path *cp)
66 {
67 struct rds_message *rm, *tmp;
68 unsigned long flags;
69
70 if (cp->cp_xmit_rm) {
71 rm = cp->cp_xmit_rm;
72 cp->cp_xmit_rm = NULL;
73 /* Tell the user the RDMA op is no longer mapped by the
74 * transport. This isn't entirely true (it's flushed out
75 * independently) but as the connection is down, there's
76 * no ongoing RDMA to/from that memory */
77 rds_message_unmapped(rm);
78 rds_message_put(rm);
79 }
80
81 cp->cp_xmit_sg = 0;
82 cp->cp_xmit_hdr_off = 0;
83 cp->cp_xmit_data_off = 0;
84 cp->cp_xmit_atomic_sent = 0;
85 cp->cp_xmit_rdma_sent = 0;
86 cp->cp_xmit_data_sent = 0;
87
88 cp->cp_conn->c_map_queued = 0;
89
90 cp->cp_unacked_packets = rds_sysctl_max_unacked_packets;
91 cp->cp_unacked_bytes = rds_sysctl_max_unacked_bytes;
92
93 /* Mark messages as retransmissions, and move them to the send q */
94 spin_lock_irqsave(&cp->cp_lock, flags);
95 list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) {
96 set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
97 set_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags);
98 }
99 list_splice_init(&cp->cp_retrans, &cp->cp_send_queue);
100 spin_unlock_irqrestore(&cp->cp_lock, flags);
101 }
102 EXPORT_SYMBOL_GPL(rds_send_path_reset);
103
acquire_in_xmit(struct rds_conn_path * cp)104 static int acquire_in_xmit(struct rds_conn_path *cp)
105 {
106 return test_and_set_bit_lock(RDS_IN_XMIT, &cp->cp_flags) == 0;
107 }
108
release_in_xmit(struct rds_conn_path * cp)109 static void release_in_xmit(struct rds_conn_path *cp)
110 {
111 clear_bit_unlock(RDS_IN_XMIT, &cp->cp_flags);
112 /*
113 * We don't use wait_on_bit()/wake_up_bit() because our waking is in a
114 * hot path and finding waiters is very rare. We don't want to walk
115 * the system-wide hashed waitqueue buckets in the fast path only to
116 * almost never find waiters.
117 */
118 if (waitqueue_active(&cp->cp_waitq))
119 wake_up_all(&cp->cp_waitq);
120 }
121
122 /*
123 * Helper function for multipath fanout to ensure lane 0 transmits queued
124 * messages before other lanes to prevent out-of-order delivery.
125 *
126 * Returns true if lane 0 still has messages or false otherwise
127 */
rds_mprds_cp0_catchup(struct rds_connection * conn)128 static bool rds_mprds_cp0_catchup(struct rds_connection *conn)
129 {
130 struct rds_conn_path *cp0 = conn->c_path;
131 struct rds_message *rm0;
132 unsigned long flags;
133 bool ret = false;
134
135 spin_lock_irqsave(&cp0->cp_lock, flags);
136
137 /* the oldest / first message in the retransmit queue
138 * has to be at or beyond c_cp0_mprds_catchup_tx_seq
139 */
140 if (!list_empty(&cp0->cp_retrans)) {
141 rm0 = list_entry(cp0->cp_retrans.next, struct rds_message,
142 m_conn_item);
143 if (be64_to_cpu(rm0->m_inc.i_hdr.h_sequence) <
144 conn->c_cp0_mprds_catchup_tx_seq) {
145 /* the retransmit queue of cp_index#0 has not
146 * quite caught up yet
147 */
148 ret = true;
149 goto unlock;
150 }
151 }
152
153 /* the oldest / first message of the send queue
154 * has to be at or beyond c_cp0_mprds_catchup_tx_seq
155 */
156 rm0 = cp0->cp_xmit_rm;
157 if (!rm0 && !list_empty(&cp0->cp_send_queue))
158 rm0 = list_entry(cp0->cp_send_queue.next, struct rds_message,
159 m_conn_item);
160 if (rm0 && be64_to_cpu(rm0->m_inc.i_hdr.h_sequence) <
161 conn->c_cp0_mprds_catchup_tx_seq) {
162 /* the send queue of cp_index#0 has not quite
163 * caught up yet
164 */
165 ret = true;
166 }
167
168 unlock:
169 spin_unlock_irqrestore(&cp0->cp_lock, flags);
170 return ret;
171 }
172
173 /*
174 * We're making the conscious trade-off here to only send one message
175 * down the connection at a time.
176 * Pro:
177 * - tx queueing is a simple fifo list
178 * - reassembly is optional and easily done by transports per conn
179 * - no per flow rx lookup at all, straight to the socket
180 * - less per-frag memory and wire overhead
181 * Con:
182 * - queued acks can be delayed behind large messages
183 * Depends:
184 * - small message latency is higher behind queued large messages
185 * - large message latency isn't starved by intervening small sends
186 */
rds_send_xmit(struct rds_conn_path * cp)187 int rds_send_xmit(struct rds_conn_path *cp)
188 {
189 struct rds_connection *conn = cp->cp_conn;
190 struct rds_message *rm;
191 unsigned long flags;
192 unsigned int tmp;
193 struct scatterlist *sg;
194 int ret = 0;
195 LIST_HEAD(to_be_dropped);
196 int batch_count;
197 unsigned long send_gen = 0;
198 int same_rm = 0;
199
200 restart:
201 batch_count = 0;
202
203 /* The drop processing after over_batch relies on
204 * rds_send_remove_from_sock() emptying to_be_dropped entry by
205 * entry; warn if that post-condition ever stops holding, and
206 * re-initialize the list head.
207 */
208 WARN_ON_ONCE(!list_empty(&to_be_dropped));
209 INIT_LIST_HEAD(&to_be_dropped);
210
211 /*
212 * sendmsg calls here after having queued its message on the send
213 * queue. We only have one task feeding the connection at a time. If
214 * another thread is already feeding the queue then we back off. This
215 * avoids blocking the caller and trading per-connection data between
216 * caches per message.
217 */
218 if (!acquire_in_xmit(cp)) {
219 rds_stats_inc(s_send_lock_contention);
220 ret = -ENOMEM;
221 goto out;
222 }
223
224 if (rds_destroy_pending(cp->cp_conn)) {
225 release_in_xmit(cp);
226 ret = -ENETUNREACH; /* dont requeue send work */
227 goto out;
228 }
229
230 /*
231 * we record the send generation after doing the xmit acquire.
232 * if someone else manages to jump in and do some work, we'll use
233 * this to avoid a goto restart farther down.
234 *
235 * The acquire_in_xmit() check above ensures that only one
236 * caller can increment c_send_gen at any time.
237 */
238 send_gen = READ_ONCE(cp->cp_send_gen) + 1;
239 WRITE_ONCE(cp->cp_send_gen, send_gen);
240
241 /*
242 * rds_conn_shutdown() sets the conn state and then tests RDS_IN_XMIT,
243 * we do the opposite to avoid races.
244 */
245 if (!rds_conn_path_up(cp)) {
246 release_in_xmit(cp);
247 ret = 0;
248 goto out;
249 }
250
251 if (conn->c_trans->xmit_path_prepare)
252 conn->c_trans->xmit_path_prepare(cp);
253
254 /*
255 * spin trying to push headers and data down the connection until
256 * the connection doesn't make forward progress.
257 */
258 while (1) {
259
260 rm = cp->cp_xmit_rm;
261
262 if (!rm) {
263 same_rm = 0;
264 } else {
265 same_rm++;
266 if (same_rm >= 4096) {
267 rds_stats_inc(s_send_stuck_rm);
268 ret = -EAGAIN;
269 break;
270 }
271 }
272
273 /*
274 * If between sending messages, we can send a pending congestion
275 * map update.
276 */
277 if (!rm && test_and_clear_bit(0, &conn->c_map_queued)) {
278 rm = rds_cong_update_alloc(conn);
279 if (IS_ERR(rm)) {
280 ret = PTR_ERR(rm);
281 break;
282 }
283 rm->data.op_active = 1;
284 rm->m_inc.i_conn_path = cp;
285 rm->m_inc.i_conn = cp->cp_conn;
286
287 cp->cp_xmit_rm = rm;
288 }
289
290 /*
291 * If not already working on one, grab the next message.
292 *
293 * cp_xmit_rm holds a ref while we're sending this message down
294 * the connection. We can use this ref while holding the
295 * send_sem.. rds_send_path_reset() is serialized with it.
296 */
297 if (!rm) {
298 unsigned int len;
299
300 batch_count++;
301
302 /* we want to process as big a batch as we can, but
303 * we also want to avoid softlockups. If we've been
304 * through a lot of messages, lets back off and see
305 * if anyone else jumps in
306 */
307 if (batch_count >= send_batch_count)
308 goto over_batch;
309
310 /* make sure cp_index#0 caught up during fan-out in
311 * order to avoid lane races
312 */
313 if (cp->cp_index > 0 && rds_mprds_cp0_catchup(conn)) {
314 rds_stats_inc(s_mprds_catchup_tx0_retries);
315 goto over_batch;
316 }
317
318 spin_lock_irqsave(&cp->cp_lock, flags);
319
320 if (!list_empty(&cp->cp_send_queue)) {
321 rm = list_entry(cp->cp_send_queue.next,
322 struct rds_message,
323 m_conn_item);
324 rds_message_addref(rm);
325
326 /*
327 * Move the message from the send queue to the retransmit
328 * list right away.
329 */
330 list_move_tail(&rm->m_conn_item,
331 &cp->cp_retrans);
332 }
333
334 spin_unlock_irqrestore(&cp->cp_lock, flags);
335
336 if (!rm)
337 break;
338
339 /* Unfortunately, the way Infiniband deals with
340 * RDMA to a bad MR key is by moving the entire
341 * queue pair to error state. We could possibly
342 * recover from that, but right now we drop the
343 * connection.
344 * Therefore, we never retransmit messages with RDMA ops.
345 */
346 if (test_bit(RDS_MSG_FLUSH, &rm->m_flags) ||
347 (rm->rdma.op_active &&
348 test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))) {
349 spin_lock_irqsave(&cp->cp_lock, flags);
350 if (test_and_clear_bit(RDS_MSG_ON_CONN,
351 &rm->m_flags)) {
352 /* our ref is put after the batch */
353 list_move(&rm->m_conn_item,
354 &to_be_dropped);
355 spin_unlock_irqrestore(&cp->cp_lock,
356 flags);
357 } else {
358 /* already off the conn list; drop
359 * the ref taken above ourselves
360 */
361 spin_unlock_irqrestore(&cp->cp_lock,
362 flags);
363 rds_message_put(rm);
364 }
365 continue;
366 }
367
368 /* Require an ACK every once in a while */
369 len = ntohl(rm->m_inc.i_hdr.h_len);
370 if (cp->cp_unacked_packets == 0 ||
371 cp->cp_unacked_bytes < len) {
372 set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
373
374 cp->cp_unacked_packets =
375 rds_sysctl_max_unacked_packets;
376 cp->cp_unacked_bytes =
377 rds_sysctl_max_unacked_bytes;
378 rds_stats_inc(s_send_ack_required);
379 } else {
380 cp->cp_unacked_bytes -= len;
381 cp->cp_unacked_packets--;
382 }
383
384 cp->cp_xmit_rm = rm;
385 }
386
387 /* The transport either sends the whole rdma or none of it */
388 if (rm->rdma.op_active && !cp->cp_xmit_rdma_sent) {
389 rm->m_final_op = &rm->rdma;
390 /* The transport owns the mapped memory for now.
391 * You can't unmap it while it's on the send queue
392 */
393 set_bit(RDS_MSG_MAPPED, &rm->m_flags);
394 ret = conn->c_trans->xmit_rdma(conn, &rm->rdma);
395 if (ret) {
396 clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
397 wake_up_interruptible(&rm->m_flush_wait);
398 break;
399 }
400 cp->cp_xmit_rdma_sent = 1;
401
402 }
403
404 if (rm->atomic.op_active && !cp->cp_xmit_atomic_sent) {
405 rm->m_final_op = &rm->atomic;
406 /* The transport owns the mapped memory for now.
407 * You can't unmap it while it's on the send queue
408 */
409 set_bit(RDS_MSG_MAPPED, &rm->m_flags);
410 ret = conn->c_trans->xmit_atomic(conn, &rm->atomic);
411 if (ret) {
412 clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
413 wake_up_interruptible(&rm->m_flush_wait);
414 break;
415 }
416 cp->cp_xmit_atomic_sent = 1;
417
418 }
419
420 /*
421 * A number of cases require an RDS header to be sent
422 * even if there is no data.
423 * We permit 0-byte sends; rds-ping depends on this.
424 * However, if there are exclusively attached silent ops,
425 * we skip the hdr/data send, to enable silent operation.
426 */
427 if (rm->data.op_nents == 0) {
428 int ops_present;
429 int all_ops_are_silent = 1;
430
431 ops_present = (rm->atomic.op_active || rm->rdma.op_active);
432 if (rm->atomic.op_active && !rm->atomic.op_silent)
433 all_ops_are_silent = 0;
434 if (rm->rdma.op_active && !rm->rdma.op_silent)
435 all_ops_are_silent = 0;
436
437 if (ops_present && all_ops_are_silent
438 && !rm->m_rdma_cookie)
439 rm->data.op_active = 0;
440 }
441
442 if (rm->data.op_active && !cp->cp_xmit_data_sent) {
443 rm->m_final_op = &rm->data;
444
445 ret = conn->c_trans->xmit(conn, rm,
446 cp->cp_xmit_hdr_off,
447 cp->cp_xmit_sg,
448 cp->cp_xmit_data_off);
449 if (ret <= 0)
450 break;
451
452 if (cp->cp_xmit_hdr_off < sizeof(struct rds_header)) {
453 tmp = min_t(int, ret,
454 sizeof(struct rds_header) -
455 cp->cp_xmit_hdr_off);
456 cp->cp_xmit_hdr_off += tmp;
457 ret -= tmp;
458 }
459
460 sg = &rm->data.op_sg[cp->cp_xmit_sg];
461 while (ret) {
462 tmp = min_t(int, ret, sg->length -
463 cp->cp_xmit_data_off);
464 cp->cp_xmit_data_off += tmp;
465 ret -= tmp;
466 if (cp->cp_xmit_data_off == sg->length) {
467 cp->cp_xmit_data_off = 0;
468 sg++;
469 cp->cp_xmit_sg++;
470 BUG_ON(ret != 0 && cp->cp_xmit_sg ==
471 rm->data.op_nents);
472 }
473 }
474
475 if (cp->cp_xmit_hdr_off == sizeof(struct rds_header) &&
476 (cp->cp_xmit_sg == rm->data.op_nents))
477 cp->cp_xmit_data_sent = 1;
478 }
479
480 /*
481 * A rm will only take multiple times through this loop
482 * if there is a data op. Thus, if the data is sent (or there was
483 * none), then we're done with the rm.
484 */
485 if (!rm->data.op_active || cp->cp_xmit_data_sent) {
486 cp->cp_xmit_rm = NULL;
487 cp->cp_xmit_sg = 0;
488 cp->cp_xmit_hdr_off = 0;
489 cp->cp_xmit_data_off = 0;
490 cp->cp_xmit_rdma_sent = 0;
491 cp->cp_xmit_atomic_sent = 0;
492 cp->cp_xmit_data_sent = 0;
493
494 rds_message_put(rm);
495 }
496 }
497
498 over_batch:
499 if (conn->c_trans->xmit_path_complete)
500 conn->c_trans->xmit_path_complete(cp);
501 release_in_xmit(cp);
502
503 /* Nuke any messages we decided not to retransmit. */
504 if (!list_empty(&to_be_dropped)) {
505 /* irqs on here, so we can put(), unlike above */
506 list_for_each_entry(rm, &to_be_dropped, m_conn_item)
507 rds_message_put(rm);
508 rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED);
509 }
510
511 /*
512 * Other senders can queue a message after we last test the send queue
513 * but before we clear RDS_IN_XMIT. In that case they'd back off and
514 * not try and send their newly queued message. We need to check the
515 * send queue after having cleared RDS_IN_XMIT so that their message
516 * doesn't get stuck on the send queue.
517 *
518 * If the transport cannot continue (i.e ret != 0), then it must
519 * call us when more room is available, such as from the tx
520 * completion handler.
521 *
522 * We have an extra generation check here so that if someone manages
523 * to jump in after our release_in_xmit, we'll see that they have done
524 * some work and we will skip our goto
525 */
526 if (ret == 0) {
527 bool raced;
528
529 smp_mb();
530 raced = send_gen != READ_ONCE(cp->cp_send_gen);
531
532 if ((test_bit(0, &conn->c_map_queued) ||
533 !list_empty(&cp->cp_send_queue)) && !raced) {
534 if (batch_count < send_batch_count)
535 goto restart;
536 rcu_read_lock();
537 if (rds_destroy_pending(cp->cp_conn))
538 ret = -ENETUNREACH;
539 else
540 queue_delayed_work(cp->cp_wq,
541 &cp->cp_send_w, 1);
542 rcu_read_unlock();
543 } else if (raced) {
544 rds_stats_inc(s_send_lock_queue_raced);
545 }
546 }
547 out:
548 return ret;
549 }
550 EXPORT_SYMBOL_GPL(rds_send_xmit);
551
rds_send_sndbuf_remove(struct rds_sock * rs,struct rds_message * rm)552 static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
553 {
554 u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
555
556 assert_spin_locked(&rs->rs_lock);
557
558 BUG_ON(rs->rs_snd_bytes < len);
559 rs->rs_snd_bytes -= len;
560
561 if (rs->rs_snd_bytes == 0)
562 rds_stats_inc(s_send_queue_empty);
563 }
564
rds_send_is_acked(struct rds_message * rm,u64 ack,is_acked_func is_acked)565 static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
566 is_acked_func is_acked)
567 {
568 if (is_acked)
569 return is_acked(rm, ack);
570 return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
571 }
572
573 /*
574 * This is pretty similar to what happens below in the ACK
575 * handling code - except that we call here as soon as we get
576 * the IB send completion on the RDMA op and the accompanying
577 * message.
578 */
rds_rdma_send_complete(struct rds_message * rm,int status)579 void rds_rdma_send_complete(struct rds_message *rm, int status)
580 {
581 struct rds_sock *rs = NULL;
582 struct rm_rdma_op *ro;
583 struct rds_notifier *notifier;
584 unsigned long flags;
585
586 spin_lock_irqsave(&rm->m_rs_lock, flags);
587
588 ro = &rm->rdma;
589 if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
590 ro->op_active && ro->op_notify && ro->op_notifier) {
591 notifier = ro->op_notifier;
592 rs = rm->m_rs;
593 sock_hold(rds_rs_to_sk(rs));
594
595 notifier->n_status = status;
596 spin_lock(&rs->rs_lock);
597 list_add_tail(¬ifier->n_list, &rs->rs_notify_queue);
598 spin_unlock(&rs->rs_lock);
599
600 ro->op_notifier = NULL;
601 }
602
603 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
604
605 if (rs) {
606 rds_wake_sk_sleep(rs);
607 sock_put(rds_rs_to_sk(rs));
608 }
609 }
610 EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
611
612 /*
613 * Just like above, except looks at atomic op
614 */
rds_atomic_send_complete(struct rds_message * rm,int status)615 void rds_atomic_send_complete(struct rds_message *rm, int status)
616 {
617 struct rds_sock *rs = NULL;
618 struct rm_atomic_op *ao;
619 struct rds_notifier *notifier;
620 unsigned long flags;
621
622 spin_lock_irqsave(&rm->m_rs_lock, flags);
623
624 ao = &rm->atomic;
625 if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
626 && ao->op_active && ao->op_notify && ao->op_notifier) {
627 notifier = ao->op_notifier;
628 rs = rm->m_rs;
629 sock_hold(rds_rs_to_sk(rs));
630
631 notifier->n_status = status;
632 spin_lock(&rs->rs_lock);
633 list_add_tail(¬ifier->n_list, &rs->rs_notify_queue);
634 spin_unlock(&rs->rs_lock);
635
636 ao->op_notifier = NULL;
637 }
638
639 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
640
641 if (rs) {
642 rds_wake_sk_sleep(rs);
643 sock_put(rds_rs_to_sk(rs));
644 }
645 }
646 EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
647
648 /*
649 * This is the same as rds_rdma_send_complete except we
650 * don't do any locking - we have all the ingredients (message,
651 * socket, socket lock) and can just move the notifier.
652 */
653 static inline void
__rds_send_complete(struct rds_sock * rs,struct rds_message * rm,int status)654 __rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
655 {
656 struct rm_rdma_op *ro;
657 struct rm_atomic_op *ao;
658
659 ro = &rm->rdma;
660 if (ro->op_active && ro->op_notify && ro->op_notifier) {
661 ro->op_notifier->n_status = status;
662 list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
663 ro->op_notifier = NULL;
664 }
665
666 ao = &rm->atomic;
667 if (ao->op_active && ao->op_notify && ao->op_notifier) {
668 ao->op_notifier->n_status = status;
669 list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
670 ao->op_notifier = NULL;
671 }
672
673 /* No need to wake the app - caller does this */
674 }
675
676 /*
677 * This removes messages from the socket's list if they're on it. The list
678 * argument must be private to the caller, we must be able to modify it
679 * without locks. The messages must have a reference held for their
680 * position on the list. This function will drop that reference after
681 * removing the messages from the 'messages' list regardless of if it found
682 * the messages on the socket list or not.
683 */
rds_send_remove_from_sock(struct list_head * messages,int status)684 static void rds_send_remove_from_sock(struct list_head *messages, int status)
685 {
686 unsigned long flags;
687 struct rds_sock *rs = NULL;
688 struct rds_message *rm;
689
690 while (!list_empty(messages)) {
691 int was_on_sock = 0;
692
693 rm = list_entry(messages->next, struct rds_message,
694 m_conn_item);
695 list_del_init(&rm->m_conn_item);
696
697 /*
698 * If we see this flag cleared then we're *sure* that someone
699 * else beat us to removing it from the sock. If we race
700 * with their flag update we'll get the lock and then really
701 * see that the flag has been cleared.
702 *
703 * The message spinlock makes sure nobody clears rm->m_rs
704 * while we're messing with it. It does not prevent the
705 * message from being removed from the socket, though.
706 */
707 spin_lock_irqsave(&rm->m_rs_lock, flags);
708 if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
709 goto unlock_and_drop;
710
711 if (rs != rm->m_rs) {
712 if (rs) {
713 rds_wake_sk_sleep(rs);
714 sock_put(rds_rs_to_sk(rs));
715 }
716 rs = rm->m_rs;
717 if (rs)
718 sock_hold(rds_rs_to_sk(rs));
719 }
720 if (!rs)
721 goto unlock_and_drop;
722 spin_lock(&rs->rs_lock);
723
724 if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
725 struct rm_rdma_op *ro = &rm->rdma;
726 struct rds_notifier *notifier;
727
728 list_del_init(&rm->m_sock_item);
729 rds_send_sndbuf_remove(rs, rm);
730
731 if (ro->op_active && ro->op_notifier &&
732 (ro->op_notify || (ro->op_recverr && status))) {
733 notifier = ro->op_notifier;
734 list_add_tail(¬ifier->n_list,
735 &rs->rs_notify_queue);
736 if (!notifier->n_status)
737 notifier->n_status = status;
738 rm->rdma.op_notifier = NULL;
739 }
740 was_on_sock = 1;
741 }
742 spin_unlock(&rs->rs_lock);
743
744 unlock_and_drop:
745 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
746 rds_message_put(rm);
747 if (was_on_sock)
748 rds_message_put(rm);
749 }
750
751 if (rs) {
752 rds_wake_sk_sleep(rs);
753 sock_put(rds_rs_to_sk(rs));
754 }
755 }
756
757 /*
758 * Transports call here when they've determined that the receiver queued
759 * messages up to, and including, the given sequence number. Messages are
760 * moved to the retrans queue when rds_send_xmit picks them off the send
761 * queue. This means that in the TCP case, the message may not have been
762 * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
763 * checks the RDS_MSG_HAS_ACK_SEQ bit.
764 */
rds_send_path_drop_acked(struct rds_conn_path * cp,u64 ack,is_acked_func is_acked)765 void rds_send_path_drop_acked(struct rds_conn_path *cp, u64 ack,
766 is_acked_func is_acked)
767 {
768 struct rds_message *rm, *tmp;
769 unsigned long flags;
770 LIST_HEAD(list);
771
772 spin_lock_irqsave(&cp->cp_lock, flags);
773
774 list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) {
775 if (!rds_send_is_acked(rm, ack, is_acked))
776 break;
777
778 list_move(&rm->m_conn_item, &list);
779 clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
780 }
781
782 /* order flag updates with spin locks */
783 if (!list_empty(&list))
784 smp_mb__after_atomic();
785
786 spin_unlock_irqrestore(&cp->cp_lock, flags);
787
788 /* now remove the messages from the sock list as needed */
789 rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
790 }
791 EXPORT_SYMBOL_GPL(rds_send_path_drop_acked);
792
rds_send_drop_acked(struct rds_connection * conn,u64 ack,is_acked_func is_acked)793 void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
794 is_acked_func is_acked)
795 {
796 WARN_ON(conn->c_trans->t_mp_capable);
797 rds_send_path_drop_acked(&conn->c_path[0], ack, is_acked);
798 }
799 EXPORT_SYMBOL_GPL(rds_send_drop_acked);
800
rds_send_drop_to(struct rds_sock * rs,struct sockaddr_in6 * dest)801 void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in6 *dest)
802 {
803 struct rds_message *rm, *tmp;
804 struct rds_connection *conn;
805 struct rds_conn_path *cp;
806 unsigned long flags;
807 LIST_HEAD(list);
808
809 /* get all the messages we're dropping under the rs lock */
810 spin_lock_irqsave(&rs->rs_lock, flags);
811
812 list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
813 if (dest &&
814 (!ipv6_addr_equal(&dest->sin6_addr, &rm->m_daddr) ||
815 dest->sin6_port != rm->m_inc.i_hdr.h_dport))
816 continue;
817
818 list_move(&rm->m_sock_item, &list);
819 rds_send_sndbuf_remove(rs, rm);
820 clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
821 }
822
823 /* order flag updates with the rs lock */
824 smp_mb__after_atomic();
825
826 spin_unlock_irqrestore(&rs->rs_lock, flags);
827
828 if (list_empty(&list))
829 return;
830
831 /* Remove the messages from the conn */
832 list_for_each_entry(rm, &list, m_sock_item) {
833
834 conn = rm->m_inc.i_conn;
835 if (conn->c_trans->t_mp_capable)
836 cp = rm->m_inc.i_conn_path;
837 else
838 cp = &conn->c_path[0];
839
840 spin_lock_irqsave(&cp->cp_lock, flags);
841 /*
842 * Maybe someone else beat us to removing rm from the conn.
843 * If we race with their flag update we'll get the lock and
844 * then really see that the flag has been cleared.
845 */
846 if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
847 spin_unlock_irqrestore(&cp->cp_lock, flags);
848 continue;
849 }
850 list_del_init(&rm->m_conn_item);
851 spin_unlock_irqrestore(&cp->cp_lock, flags);
852
853 /*
854 * Couldn't grab m_rs_lock in top loop (lock ordering),
855 * but we can now.
856 */
857 spin_lock_irqsave(&rm->m_rs_lock, flags);
858
859 spin_lock(&rs->rs_lock);
860 __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
861 spin_unlock(&rs->rs_lock);
862
863 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
864
865 rds_message_put(rm);
866 }
867
868 rds_wake_sk_sleep(rs);
869
870 while (!list_empty(&list)) {
871 rm = list_entry(list.next, struct rds_message, m_sock_item);
872 list_del_init(&rm->m_sock_item);
873 rds_message_wait(rm);
874
875 /* just in case the code above skipped this message
876 * because RDS_MSG_ON_CONN wasn't set, run it again here
877 * taking m_rs_lock is the only thing that keeps us
878 * from racing with ack processing.
879 */
880 spin_lock_irqsave(&rm->m_rs_lock, flags);
881
882 spin_lock(&rs->rs_lock);
883 __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
884 spin_unlock(&rs->rs_lock);
885
886 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
887
888 rds_message_put(rm);
889 }
890 }
891
892 /*
893 * we only want this to fire once so we use the callers 'queued'. It's
894 * possible that another thread can race with us and remove the
895 * message from the flow with RDS_CANCEL_SENT_TO.
896 */
rds_send_queue_rm(struct rds_sock * rs,struct rds_connection * conn,struct rds_conn_path * cp,struct rds_message * rm,__be16 sport,__be16 dport,int * queued)897 static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
898 struct rds_conn_path *cp,
899 struct rds_message *rm, __be16 sport,
900 __be16 dport, int *queued)
901 {
902 unsigned long flags;
903 u32 len;
904
905 if (*queued)
906 goto out;
907
908 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
909
910 /* this is the only place which holds both the socket's rs_lock
911 * and the connection's c_lock */
912 spin_lock_irqsave(&rs->rs_lock, flags);
913
914 /*
915 * If there is a little space in sndbuf, we don't queue anything,
916 * and userspace gets -EAGAIN. But poll() indicates there's send
917 * room. This can lead to bad behavior (spinning) if snd_bytes isn't
918 * freed up by incoming acks. So we check the *old* value of
919 * rs_snd_bytes here to allow the last msg to exceed the buffer,
920 * and poll() now knows no more data can be sent.
921 */
922 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
923 rs->rs_snd_bytes += len;
924
925 /* let recv side know we are close to send space exhaustion.
926 * This is probably not the optimal way to do it, as this
927 * means we set the flag on *all* messages as soon as our
928 * throughput hits a certain threshold.
929 */
930 if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
931 set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
932
933 list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
934 set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
935 rds_message_addref(rm);
936 sock_hold(rds_rs_to_sk(rs));
937 rm->m_rs = rs;
938
939 /* The code ordering is a little weird, but we're
940 trying to minimize the time we hold c_lock */
941 rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
942 rm->m_inc.i_conn = conn;
943 rm->m_inc.i_conn_path = cp;
944 rds_message_addref(rm);
945
946 spin_lock(&cp->cp_lock);
947 rm->m_inc.i_hdr.h_sequence = cpu_to_be64(cp->cp_next_tx_seq++);
948 list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
949 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
950 spin_unlock(&cp->cp_lock);
951
952 rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
953 rm, len, rs, rs->rs_snd_bytes,
954 (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
955
956 *queued = 1;
957 }
958
959 spin_unlock_irqrestore(&rs->rs_lock, flags);
960 out:
961 return *queued;
962 }
963
964 /*
965 * rds_message is getting to be quite complicated, and we'd like to allocate
966 * it all in one go. This figures out how big it needs to be up front.
967 */
rds_rm_size(struct msghdr * msg,int num_sgs,struct rds_iov_vector_arr * vct)968 static int rds_rm_size(struct msghdr *msg, int num_sgs,
969 struct rds_iov_vector_arr *vct)
970 {
971 struct cmsghdr *cmsg;
972 int size = 0;
973 int cmsg_groups = 0;
974 int retval;
975 bool zcopy_cookie = false;
976 struct rds_iov_vector *iov, *tmp_iov;
977
978 if (num_sgs < 0)
979 return -EINVAL;
980
981 for_each_cmsghdr(cmsg, msg) {
982 if (!CMSG_OK(msg, cmsg))
983 return -EINVAL;
984
985 if (cmsg->cmsg_level != SOL_RDS)
986 continue;
987
988 switch (cmsg->cmsg_type) {
989 case RDS_CMSG_RDMA_ARGS:
990 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct rds_rdma_args)))
991 return -EINVAL;
992 if (vct->indx >= vct->len) {
993 vct->len += vct->incr;
994 tmp_iov = krealloc_array(vct->vec, vct->len,
995 sizeof(*vct->vec), GFP_KERNEL);
996 if (!tmp_iov) {
997 vct->len -= vct->incr;
998 return -ENOMEM;
999 }
1000 vct->vec = tmp_iov;
1001 }
1002 iov = &vct->vec[vct->indx];
1003 memset(iov, 0, sizeof(struct rds_iov_vector));
1004 vct->indx++;
1005 cmsg_groups |= 1;
1006 retval = rds_rdma_extra_size(CMSG_DATA(cmsg), iov);
1007 if (retval < 0)
1008 return retval;
1009 size += retval;
1010
1011 break;
1012
1013 case RDS_CMSG_ZCOPY_COOKIE:
1014 zcopy_cookie = true;
1015 fallthrough;
1016
1017 case RDS_CMSG_RDMA_DEST:
1018 case RDS_CMSG_RDMA_MAP:
1019 cmsg_groups |= 2;
1020 /* these are valid but do no add any size */
1021 break;
1022
1023 case RDS_CMSG_ATOMIC_CSWP:
1024 case RDS_CMSG_ATOMIC_FADD:
1025 case RDS_CMSG_MASKED_ATOMIC_CSWP:
1026 case RDS_CMSG_MASKED_ATOMIC_FADD:
1027 cmsg_groups |= 1;
1028 size += sizeof(struct scatterlist);
1029 break;
1030
1031 default:
1032 return -EINVAL;
1033 }
1034
1035 }
1036
1037 if ((msg->msg_flags & MSG_ZEROCOPY) && !zcopy_cookie)
1038 return -EINVAL;
1039
1040 size += num_sgs * sizeof(struct scatterlist);
1041
1042 /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
1043 if (cmsg_groups == 3)
1044 return -EINVAL;
1045
1046 return size;
1047 }
1048
rds_cmsg_zcopy(struct rds_sock * rs,struct rds_message * rm,struct cmsghdr * cmsg)1049 static int rds_cmsg_zcopy(struct rds_sock *rs, struct rds_message *rm,
1050 struct cmsghdr *cmsg)
1051 {
1052 u32 *cookie;
1053
1054 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*cookie)) ||
1055 !rm->data.op_mmp_znotifier)
1056 return -EINVAL;
1057 cookie = CMSG_DATA(cmsg);
1058 rm->data.op_mmp_znotifier->z_cookie = *cookie;
1059 return 0;
1060 }
1061
rds_cmsg_send(struct rds_sock * rs,struct rds_message * rm,struct msghdr * msg,int * allocated_mr,struct rds_iov_vector_arr * vct)1062 static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
1063 struct msghdr *msg, int *allocated_mr,
1064 struct rds_iov_vector_arr *vct)
1065 {
1066 struct cmsghdr *cmsg;
1067 int ret = 0, ind = 0;
1068
1069 for_each_cmsghdr(cmsg, msg) {
1070 if (!CMSG_OK(msg, cmsg))
1071 return -EINVAL;
1072
1073 if (cmsg->cmsg_level != SOL_RDS)
1074 continue;
1075
1076 /* As a side effect, RDMA_DEST and RDMA_MAP will set
1077 * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
1078 */
1079 switch (cmsg->cmsg_type) {
1080 case RDS_CMSG_RDMA_ARGS:
1081 if (ind >= vct->indx)
1082 return -ENOMEM;
1083 ret = rds_cmsg_rdma_args(rs, rm, cmsg, &vct->vec[ind]);
1084 ind++;
1085 break;
1086
1087 case RDS_CMSG_RDMA_DEST:
1088 ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
1089 break;
1090
1091 case RDS_CMSG_RDMA_MAP:
1092 ret = rds_cmsg_rdma_map(rs, rm, cmsg);
1093 if (!ret)
1094 *allocated_mr = 1;
1095 else if (ret == -ENODEV)
1096 /* Accommodate the get_mr() case which can fail
1097 * if connection isn't established yet.
1098 */
1099 ret = -EAGAIN;
1100 break;
1101 case RDS_CMSG_ATOMIC_CSWP:
1102 case RDS_CMSG_ATOMIC_FADD:
1103 case RDS_CMSG_MASKED_ATOMIC_CSWP:
1104 case RDS_CMSG_MASKED_ATOMIC_FADD:
1105 ret = rds_cmsg_atomic(rs, rm, cmsg);
1106 break;
1107
1108 case RDS_CMSG_ZCOPY_COOKIE:
1109 ret = rds_cmsg_zcopy(rs, rm, cmsg);
1110 break;
1111
1112 default:
1113 return -EINVAL;
1114 }
1115
1116 if (ret)
1117 break;
1118 }
1119
1120 return ret;
1121 }
1122
rds_rdma_bytes(struct msghdr * msg,size_t * rdma_bytes)1123 static int rds_rdma_bytes(struct msghdr *msg, size_t *rdma_bytes)
1124 {
1125 struct rds_rdma_args *args;
1126 struct cmsghdr *cmsg;
1127
1128 for_each_cmsghdr(cmsg, msg) {
1129 if (!CMSG_OK(msg, cmsg))
1130 return -EINVAL;
1131
1132 if (cmsg->cmsg_level != SOL_RDS)
1133 continue;
1134
1135 if (cmsg->cmsg_type == RDS_CMSG_RDMA_ARGS) {
1136 if (cmsg->cmsg_len <
1137 CMSG_LEN(sizeof(struct rds_rdma_args)))
1138 return -EINVAL;
1139 args = CMSG_DATA(cmsg);
1140 *rdma_bytes += args->remote_vec.bytes;
1141 }
1142 }
1143 return 0;
1144 }
1145
rds_sendmsg(struct socket * sock,struct msghdr * msg,size_t payload_len)1146 int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len)
1147 {
1148 struct sock *sk = sock->sk;
1149 struct rds_sock *rs = rds_sk_to_rs(sk);
1150 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
1151 DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
1152 __be16 dport;
1153 struct rds_message *rm = NULL;
1154 struct rds_connection *conn;
1155 int ret = 0;
1156 int queued = 0, allocated_mr = 0;
1157 int nonblock = msg->msg_flags & MSG_DONTWAIT;
1158 long timeo = sock_sndtimeo(sk, nonblock);
1159 struct rds_conn_path *cpath;
1160 struct in6_addr daddr;
1161 __u32 scope_id = 0;
1162 size_t rdma_payload_len = 0;
1163 bool zcopy = ((msg->msg_flags & MSG_ZEROCOPY) &&
1164 sock_flag(rds_rs_to_sk(rs), SOCK_ZEROCOPY));
1165 int num_sgs = DIV_ROUND_UP(payload_len, PAGE_SIZE);
1166 int namelen;
1167 struct rds_iov_vector_arr vct;
1168 int ind;
1169
1170 memset(&vct, 0, sizeof(vct));
1171
1172 /* expect 1 RDMA CMSG per rds_sendmsg. can still grow if more needed. */
1173 vct.incr = 1;
1174
1175 /* Mirror Linux UDP mirror of BSD error message compatibility */
1176 /* XXX: Perhaps MSG_MORE someday */
1177 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT | MSG_ZEROCOPY)) {
1178 ret = -EOPNOTSUPP;
1179 goto out;
1180 }
1181
1182 namelen = msg->msg_namelen;
1183 if (namelen != 0) {
1184 if (namelen < sizeof(*usin)) {
1185 ret = -EINVAL;
1186 goto out;
1187 }
1188 switch (usin->sin_family) {
1189 case AF_INET:
1190 if (usin->sin_addr.s_addr == htonl(INADDR_ANY) ||
1191 usin->sin_addr.s_addr == htonl(INADDR_BROADCAST) ||
1192 ipv4_is_multicast(usin->sin_addr.s_addr)) {
1193 ret = -EINVAL;
1194 goto out;
1195 }
1196 ipv6_addr_set_v4mapped(usin->sin_addr.s_addr, &daddr);
1197 dport = usin->sin_port;
1198 break;
1199
1200 #if IS_ENABLED(CONFIG_IPV6)
1201 case AF_INET6: {
1202 int addr_type;
1203
1204 if (namelen < sizeof(*sin6)) {
1205 ret = -EINVAL;
1206 goto out;
1207 }
1208 addr_type = ipv6_addr_type(&sin6->sin6_addr);
1209 if (!(addr_type & IPV6_ADDR_UNICAST)) {
1210 __be32 addr4;
1211
1212 if (!(addr_type & IPV6_ADDR_MAPPED)) {
1213 ret = -EINVAL;
1214 goto out;
1215 }
1216
1217 /* It is a mapped address. Need to do some
1218 * sanity checks.
1219 */
1220 addr4 = sin6->sin6_addr.s6_addr32[3];
1221 if (addr4 == htonl(INADDR_ANY) ||
1222 addr4 == htonl(INADDR_BROADCAST) ||
1223 ipv4_is_multicast(addr4)) {
1224 ret = -EINVAL;
1225 goto out;
1226 }
1227 }
1228 if (addr_type & IPV6_ADDR_LINKLOCAL) {
1229 if (sin6->sin6_scope_id == 0) {
1230 ret = -EINVAL;
1231 goto out;
1232 }
1233 scope_id = sin6->sin6_scope_id;
1234 }
1235
1236 daddr = sin6->sin6_addr;
1237 dport = sin6->sin6_port;
1238 break;
1239 }
1240 #endif
1241
1242 default:
1243 ret = -EINVAL;
1244 goto out;
1245 }
1246 } else {
1247 /* We only care about consistency with ->connect() */
1248 lock_sock(sk);
1249 daddr = rs->rs_conn_addr;
1250 dport = rs->rs_conn_port;
1251 scope_id = rs->rs_bound_scope_id;
1252 release_sock(sk);
1253 }
1254
1255 lock_sock(sk);
1256 if (ipv6_addr_any(&rs->rs_bound_addr) || ipv6_addr_any(&daddr)) {
1257 release_sock(sk);
1258 ret = -ENOTCONN;
1259 goto out;
1260 } else if (namelen != 0) {
1261 /* Cannot send to an IPv4 address using an IPv6 source
1262 * address and cannot send to an IPv6 address using an
1263 * IPv4 source address.
1264 */
1265 if (ipv6_addr_v4mapped(&daddr) ^
1266 ipv6_addr_v4mapped(&rs->rs_bound_addr)) {
1267 release_sock(sk);
1268 ret = -EOPNOTSUPP;
1269 goto out;
1270 }
1271 /* If the socket is already bound to a link local address,
1272 * it can only send to peers on the same link. But allow
1273 * communicating between link local and non-link local address.
1274 */
1275 if (scope_id != rs->rs_bound_scope_id) {
1276 if (!scope_id) {
1277 scope_id = rs->rs_bound_scope_id;
1278 } else if (rs->rs_bound_scope_id) {
1279 release_sock(sk);
1280 ret = -EINVAL;
1281 goto out;
1282 }
1283 }
1284 }
1285 release_sock(sk);
1286
1287 ret = rds_rdma_bytes(msg, &rdma_payload_len);
1288 if (ret)
1289 goto out;
1290
1291 if (max_t(size_t, payload_len, rdma_payload_len) > RDS_MAX_MSG_SIZE) {
1292 ret = -EMSGSIZE;
1293 goto out;
1294 }
1295
1296 if (payload_len > rds_sk_sndbuf(rs)) {
1297 ret = -EMSGSIZE;
1298 goto out;
1299 }
1300
1301 if (zcopy) {
1302 if (rs->rs_transport->t_type != RDS_TRANS_TCP) {
1303 ret = -EOPNOTSUPP;
1304 goto out;
1305 }
1306 num_sgs = iov_iter_npages(&msg->msg_iter, INT_MAX);
1307 }
1308 /* size of rm including all sgs */
1309 ret = rds_rm_size(msg, num_sgs, &vct);
1310 if (ret < 0)
1311 goto out;
1312
1313 rm = rds_message_alloc(ret, GFP_KERNEL);
1314 if (!rm) {
1315 ret = -ENOMEM;
1316 goto out;
1317 }
1318
1319 /* Attach data to the rm */
1320 if (payload_len) {
1321 rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs);
1322 if (IS_ERR(rm->data.op_sg)) {
1323 ret = PTR_ERR(rm->data.op_sg);
1324 goto out;
1325 }
1326 ret = rds_message_copy_from_user(rm, &msg->msg_iter, zcopy);
1327 if (ret)
1328 goto out;
1329 }
1330 rm->data.op_active = 1;
1331
1332 rm->m_daddr = daddr;
1333
1334 /* rds_conn_create has a spinlock that runs with IRQ off.
1335 * Caching the conn in the socket helps a lot. */
1336 if (rs->rs_conn && ipv6_addr_equal(&rs->rs_conn->c_faddr, &daddr) &&
1337 rs->rs_tos == rs->rs_conn->c_tos) {
1338 conn = rs->rs_conn;
1339 } else {
1340 conn = rds_conn_create_outgoing(sock_net(sock->sk),
1341 &rs->rs_bound_addr, &daddr,
1342 rs->rs_transport, rs->rs_tos,
1343 sock->sk->sk_allocation,
1344 scope_id);
1345 if (IS_ERR(conn)) {
1346 ret = PTR_ERR(conn);
1347 goto out;
1348 }
1349 rs->rs_conn = conn;
1350 }
1351
1352 if (conn->c_trans->t_mp_capable) {
1353 /* Use c_path[0] until we learn that
1354 * the peer supports more (c_npaths > 1)
1355 */
1356 cpath = &conn->c_path[RDS_MPATH_HASH(rs, conn->c_npaths ? : 1)];
1357 } else {
1358 cpath = &conn->c_path[0];
1359 }
1360
1361 /* If we're multipath capable and path 0 is down, queue reconnect
1362 * and send a ping. This initiates the multipath handshake through
1363 * rds_send_probe(), which sends RDS_EXTHDR_NPATHS to the peer,
1364 * starting multipath capability negotiation.
1365 */
1366 if (conn->c_trans->t_mp_capable &&
1367 !rds_conn_path_up(&conn->c_path[0])) {
1368 /* Ensures that only one request is queued. And
1369 * rds_send_ping() ensures that only one ping is
1370 * outstanding.
1371 */
1372 if (!test_and_set_bit(RDS_RECONNECT_PENDING,
1373 &conn->c_path[0].cp_flags))
1374 queue_delayed_work(conn->c_path[0].cp_wq,
1375 &conn->c_path[0].cp_conn_w, 0);
1376 rds_send_ping(conn, 0);
1377 }
1378
1379 rm->m_conn_path = cpath;
1380
1381 /* Parse any control messages the user may have included. */
1382 ret = rds_cmsg_send(rs, rm, msg, &allocated_mr, &vct);
1383 if (ret)
1384 goto out;
1385
1386 if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
1387 printk_ratelimited(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
1388 &rm->rdma, conn->c_trans->xmit_rdma);
1389 ret = -EOPNOTSUPP;
1390 goto out;
1391 }
1392
1393 if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
1394 printk_ratelimited(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
1395 &rm->atomic, conn->c_trans->xmit_atomic);
1396 ret = -EOPNOTSUPP;
1397 goto out;
1398 }
1399
1400 if (rds_destroy_pending(conn)) {
1401 ret = -EAGAIN;
1402 goto out;
1403 }
1404
1405 if (rds_conn_path_down(cpath))
1406 rds_check_all_paths(conn);
1407
1408 ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
1409 if (ret) {
1410 WRITE_ONCE(rs->rs_seen_congestion, 1);
1411 goto out;
1412 }
1413 while (!rds_send_queue_rm(rs, conn, cpath, rm, rs->rs_bound_port,
1414 dport, &queued)) {
1415 rds_stats_inc(s_send_queue_full);
1416
1417 if (nonblock) {
1418 ret = -EAGAIN;
1419 goto out;
1420 }
1421
1422 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
1423 rds_send_queue_rm(rs, conn, cpath, rm,
1424 rs->rs_bound_port,
1425 dport,
1426 &queued),
1427 timeo);
1428 rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
1429 if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
1430 continue;
1431
1432 ret = timeo;
1433 if (ret == 0)
1434 ret = -ETIMEDOUT;
1435 goto out;
1436 }
1437
1438 /*
1439 * By now we've committed to the send. We reuse rds_send_worker()
1440 * to retry sends in the rds thread if the transport asks us to.
1441 */
1442 rds_stats_inc(s_send_queued);
1443
1444 ret = rds_send_xmit(cpath);
1445 if (ret == -ENOMEM || ret == -EAGAIN) {
1446 ret = 0;
1447 rcu_read_lock();
1448 if (rds_destroy_pending(cpath->cp_conn))
1449 ret = -ENETUNREACH;
1450 else
1451 queue_delayed_work(cpath->cp_wq, &cpath->cp_send_w, 1);
1452 rcu_read_unlock();
1453
1454 if (ret)
1455 goto out;
1456 }
1457
1458 rds_message_put(rm);
1459
1460 for (ind = 0; ind < vct.indx; ind++)
1461 kfree(vct.vec[ind].iov);
1462 kfree(vct.vec);
1463
1464 return payload_len;
1465
1466 out:
1467 for (ind = 0; ind < vct.indx; ind++)
1468 kfree(vct.vec[ind].iov);
1469 kfree(vct.vec);
1470
1471 /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
1472 * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
1473 * or in any other way, we need to destroy the MR again */
1474 if (allocated_mr)
1475 rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
1476
1477 if (rm)
1478 rds_message_put(rm);
1479 return ret;
1480 }
1481
1482 /*
1483 * send out a probe. Can be shared by rds_send_ping,
1484 * rds_send_pong, rds_send_hb.
1485 * rds_send_hb should use h_flags
1486 * RDS_FLAG_HB_PING|RDS_FLAG_ACK_REQUIRED
1487 * or
1488 * RDS_FLAG_HB_PONG|RDS_FLAG_ACK_REQUIRED
1489 */
1490 static int
rds_send_probe(struct rds_conn_path * cp,__be16 sport,__be16 dport,u8 h_flags)1491 rds_send_probe(struct rds_conn_path *cp, __be16 sport,
1492 __be16 dport, u8 h_flags)
1493 {
1494 struct rds_message *rm;
1495 unsigned long flags;
1496 int ret = 0;
1497
1498 rm = rds_message_alloc(0, GFP_ATOMIC);
1499 if (!rm) {
1500 ret = -ENOMEM;
1501 goto out;
1502 }
1503
1504 rm->m_daddr = cp->cp_conn->c_faddr;
1505 rm->data.op_active = 1;
1506
1507 rds_conn_path_connect_if_down(cp);
1508
1509 ret = rds_cong_wait(cp->cp_conn->c_fcong, dport, 1, NULL);
1510 if (ret)
1511 goto out;
1512
1513 spin_lock_irqsave(&cp->cp_lock, flags);
1514 list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
1515 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
1516 rds_message_addref(rm);
1517 rm->m_inc.i_conn = cp->cp_conn;
1518 rm->m_inc.i_conn_path = cp;
1519
1520 rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport,
1521 cp->cp_next_tx_seq);
1522 rm->m_inc.i_hdr.h_flags |= h_flags;
1523 cp->cp_next_tx_seq++;
1524
1525 if (RDS_HS_PROBE(be16_to_cpu(sport), be16_to_cpu(dport)) &&
1526 cp->cp_conn->c_trans->t_mp_capable) {
1527 __be16 npaths = cpu_to_be16(RDS_MPATH_WORKERS);
1528 __be32 my_gen_num = cpu_to_be32(cp->cp_conn->c_my_gen_num);
1529 u8 dummy = 0;
1530
1531 rds_message_add_extension(&rm->m_inc.i_hdr,
1532 RDS_EXTHDR_NPATHS, &npaths);
1533 rds_message_add_extension(&rm->m_inc.i_hdr,
1534 RDS_EXTHDR_GEN_NUM,
1535 &my_gen_num);
1536 rds_message_add_extension(&rm->m_inc.i_hdr,
1537 RDS_EXTHDR_SPORT_IDX,
1538 &dummy);
1539 }
1540 spin_unlock_irqrestore(&cp->cp_lock, flags);
1541
1542 rds_stats_inc(s_send_queued);
1543 rds_stats_inc(s_send_pong);
1544
1545 /* schedule the send work on cp_wq */
1546 rcu_read_lock();
1547 if (!rds_destroy_pending(cp->cp_conn))
1548 queue_delayed_work(cp->cp_wq, &cp->cp_send_w, 1);
1549 rcu_read_unlock();
1550
1551 rds_message_put(rm);
1552 return 0;
1553
1554 out:
1555 if (rm)
1556 rds_message_put(rm);
1557 return ret;
1558 }
1559
1560 int
rds_send_pong(struct rds_conn_path * cp,__be16 dport)1561 rds_send_pong(struct rds_conn_path *cp, __be16 dport)
1562 {
1563 return rds_send_probe(cp, 0, dport, 0);
1564 }
1565
1566 void
rds_send_ping(struct rds_connection * conn,int cp_index)1567 rds_send_ping(struct rds_connection *conn, int cp_index)
1568 {
1569 unsigned long flags;
1570 struct rds_conn_path *cp = &conn->c_path[cp_index];
1571
1572 spin_lock_irqsave(&cp->cp_lock, flags);
1573 if (conn->c_ping_triggered) {
1574 spin_unlock_irqrestore(&cp->cp_lock, flags);
1575 return;
1576 }
1577 conn->c_ping_triggered = 1;
1578 spin_unlock_irqrestore(&cp->cp_lock, flags);
1579 rds_send_probe(cp, cpu_to_be16(RDS_FLAG_PROBE_PORT), 0, 0);
1580 }
1581 EXPORT_SYMBOL_GPL(rds_send_ping);
1582