RDS: Let rds_message_alloc_sgs() return NULL
[cascardo/linux.git] / net / rds / send.c
index ecda3e6..35b9c2e 100644 (file)
@@ -52,8 +52,11 @@ static int send_batch_count = 64;
 module_param(send_batch_count, int, 0444);
 MODULE_PARM_DESC(send_batch_count, " batch factor when working the send queue");
 
+static void rds_send_remove_from_sock(struct list_head *messages, int status);
+
 /*
- * Reset the send state. Caller must hold c_send_lock when calling here.
+ * Reset the send state.  Callers must ensure that this doesn't race with
+ * rds_send_xmit().
  */
 void rds_send_reset(struct rds_connection *conn)
 {
@@ -61,14 +64,16 @@ void rds_send_reset(struct rds_connection *conn)
        unsigned long flags;
 
        if (conn->c_xmit_rm) {
+               rm = conn->c_xmit_rm;
+               conn->c_xmit_rm = NULL;
                /* Tell the user the RDMA op is no longer mapped by the
                 * transport. This isn't entirely true (it's flushed out
                 * independently) but as the connection is down, there's
                 * no ongoing RDMA to/from that memory */
-               rds_message_unmapped(conn->c_xmit_rm);
-               rds_message_put(conn->c_xmit_rm);
-               conn->c_xmit_rm = NULL;
+               rds_message_unmapped(rm);
+               rds_message_put(rm);
        }
+
        conn->c_xmit_sg = 0;
        conn->c_xmit_hdr_off = 0;
        conn->c_xmit_data_off = 0;
@@ -91,6 +96,25 @@ void rds_send_reset(struct rds_connection *conn)
        spin_unlock_irqrestore(&conn->c_lock, flags);
 }
 
+static int acquire_in_xmit(struct rds_connection *conn)
+{
+       return test_and_set_bit(RDS_IN_XMIT, &conn->c_flags) == 0;
+}
+
+static void release_in_xmit(struct rds_connection *conn)
+{
+       clear_bit(RDS_IN_XMIT, &conn->c_flags);
+       smp_mb__after_clear_bit();
+       /*
+        * We don't use wait_on_bit()/wake_up_bit() because our waking is in a
+        * hot path and finding waiters is very rare.  We don't want to walk
+        * the system-wide hashed waitqueue buckets in the fast path only to
+        * almost never find waiters.
+        */
+       if (waitqueue_active(&conn->c_waitq))
+               wake_up_all(&conn->c_waitq);
+}
+
 /*
  * We're making the concious trade-off here to only send one message
  * down the connection at a time.
@@ -110,14 +134,11 @@ int rds_send_xmit(struct rds_connection *conn)
        struct rds_message *rm;
        unsigned long flags;
        unsigned int tmp;
-       unsigned int send_quota = send_batch_count;
        struct scatterlist *sg;
        int ret = 0;
-       int was_empty = 0;
        LIST_HEAD(to_be_dropped);
 
-       if (!rds_conn_up(conn))
-               goto out;
+restart:
 
        /*
         * sendmsg calls here after having queued its message on the send
@@ -126,12 +147,22 @@ int rds_send_xmit(struct rds_connection *conn)
         * avoids blocking the caller and trading per-connection data between
         * caches per message.
         */
-       if (!spin_trylock_irqsave(&conn->c_send_lock, flags)) {
+       if (!acquire_in_xmit(conn)) {
                rds_stats_inc(s_send_lock_contention);
                ret = -ENOMEM;
                goto out;
        }
 
+       /*
+        * rds_conn_shutdown() sets the conn state and then tests RDS_IN_XMIT,
+        * we do the opposite to avoid races.
+        */
+       if (!rds_conn_up(conn)) {
+               release_in_xmit(conn);
+               ret = 0;
+               goto out;
+       }
+
        if (conn->c_trans->xmit_prepare)
                conn->c_trans->xmit_prepare(conn);
 
@@ -139,7 +170,7 @@ int rds_send_xmit(struct rds_connection *conn)
         * spin trying to push headers and data down the connection until
         * the connection doesn't make forward progress.
         */
-       while (--send_quota) {
+       while (1) {
 
                rm = conn->c_xmit_rm;
 
@@ -168,7 +199,7 @@ int rds_send_xmit(struct rds_connection *conn)
                if (!rm) {
                        unsigned int len;
 
-                       spin_lock(&conn->c_lock);
+                       spin_lock_irqsave(&conn->c_lock, flags);
 
                        if (!list_empty(&conn->c_send_queue)) {
                                rm = list_entry(conn->c_send_queue.next,
@@ -183,12 +214,10 @@ int rds_send_xmit(struct rds_connection *conn)
                                list_move_tail(&rm->m_conn_item, &conn->c_retrans);
                        }
 
-                       spin_unlock(&conn->c_lock);
+                       spin_unlock_irqrestore(&conn->c_lock, flags);
 
-                       if (!rm) {
-                               was_empty = 1;
+                       if (!rm)
                                break;
-                       }
 
                        /* Unfortunately, the way Infiniband deals with
                         * RDMA to a bad MR key is by moving the entire
@@ -199,10 +228,10 @@ int rds_send_xmit(struct rds_connection *conn)
                         */
                        if (rm->rdma.op_active &&
                            test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags)) {
-                               spin_lock(&conn->c_lock);
+                               spin_lock_irqsave(&conn->c_lock, flags);
                                if (test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags))
                                        list_move(&rm->m_conn_item, &to_be_dropped);
-                               spin_unlock(&conn->c_lock);
+                               spin_unlock_irqrestore(&conn->c_lock, flags);
                                continue;
                        }
 
@@ -328,19 +357,7 @@ int rds_send_xmit(struct rds_connection *conn)
        if (conn->c_trans->xmit_complete)
                conn->c_trans->xmit_complete(conn);
 
-       /*
-        * We might be racing with another sender who queued a message but
-        * backed off on noticing that we held the c_send_lock.  If we check
-        * for queued messages after dropping the sem then either we'll
-        * see the queued message or the queuer will get the sem.  If we
-        * notice the queued message then we trigger an immediate retry.
-        *
-        * We need to be careful only to do this when we stopped processing
-        * the send queue because it was empty.  It's the only way we
-        * stop processing the loop when the transport hasn't taken
-        * responsibility for forward progress.
-        */
-       spin_unlock_irqrestore(&conn->c_send_lock, flags);
+       release_in_xmit(conn);
 
        /* Nuke any messages we decided not to retransmit. */
        if (!list_empty(&to_be_dropped)) {
@@ -350,22 +367,23 @@ int rds_send_xmit(struct rds_connection *conn)
                rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED);
        }
 
-       if (send_quota == 0 && !was_empty) {
-               /* We exhausted the send quota, but there's work left to
-                * do. Return and (re-)schedule the send worker.
-                */
-               ret = -EAGAIN;
-       }
-
-       if (ret == 0 && was_empty) {
-               /* A simple bit test would be way faster than taking the
-                * spin lock */
-               spin_lock_irqsave(&conn->c_lock, flags);
+       /*
+        * Other senders can queue a message after we last test the send queue
+        * but before we clear RDS_IN_XMIT.  In that case they'd back off and
+        * not try and send their newly queued message.  We need to check the
+        * send queue after having cleared RDS_IN_XMIT so that their message
+        * doesn't get stuck on the send queue.
+        *
+        * If the transport cannot continue (i.e ret != 0), then it must
+        * call us when more room is available, such as from the tx
+        * completion handler.
+        */
+       if (ret == 0) {
+               smp_mb();
                if (!list_empty(&conn->c_send_queue)) {
                        rds_stats_inc(s_send_lock_queue_raced);
-                       ret = -EAGAIN;
+                       goto restart;
                }
-               spin_unlock_irqrestore(&conn->c_lock, flags);
        }
 out:
        return ret;
@@ -392,35 +410,6 @@ static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
        return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
 }
 
-/*
- * Returns true if there are no messages on the send and retransmit queues
- * which have a sequence number greater than or equal to the given sequence
- * number.
- */
-int rds_send_acked_before(struct rds_connection *conn, u64 seq)
-{
-       struct rds_message *rm, *tmp;
-       int ret = 1;
-
-       spin_lock(&conn->c_lock);
-
-       list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
-               if (be64_to_cpu(rm->m_inc.i_hdr.h_sequence) < seq)
-                       ret = 0;
-               break;
-       }
-
-       list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
-               if (be64_to_cpu(rm->m_inc.i_hdr.h_sequence) < seq)
-                       ret = 0;
-               break;
-       }
-
-       spin_unlock(&conn->c_lock);
-
-       return ret;
-}
-
 /*
  * This is pretty similar to what happens below in the ACK
  * handling code - except that we call here as soon as we get
@@ -568,7 +557,7 @@ EXPORT_SYMBOL_GPL(rds_send_get_message);
  * removing the messages from the 'messages' list regardless of if it found
  * the messages on the socket list or not.
  */
-void rds_send_remove_from_sock(struct list_head *messages, int status)
+static void rds_send_remove_from_sock(struct list_head *messages, int status)
 {
        unsigned long flags;
        struct rds_sock *rs = NULL;
@@ -856,6 +845,8 @@ static int rds_rm_size(struct msghdr *msg, int data_len)
 
                case RDS_CMSG_ATOMIC_CSWP:
                case RDS_CMSG_ATOMIC_FADD:
+               case RDS_CMSG_MASKED_ATOMIC_CSWP:
+               case RDS_CMSG_MASKED_ATOMIC_FADD:
                        cmsg_groups |= 1;
                        size += sizeof(struct scatterlist);
                        break;
@@ -907,6 +898,8 @@ static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
                        break;
                case RDS_CMSG_ATOMIC_CSWP:
                case RDS_CMSG_ATOMIC_FADD:
+               case RDS_CMSG_MASKED_ATOMIC_CSWP:
+               case RDS_CMSG_MASKED_ATOMIC_FADD:
                        ret = rds_cmsg_atomic(rs, rm, cmsg);
                        break;
 
@@ -980,6 +973,10 @@ int rds_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
        /* Attach data to the rm */
        if (payload_len) {
                rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
+               if (!rm->data.op_sg) {
+                       ret = -ENOMEM;
+                       goto out;
+               }
                ret = rds_message_copy_from_user(rm, msg->msg_iov, payload_len);
                if (ret)
                        goto out;
@@ -1024,13 +1021,7 @@ int rds_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
                goto out;
        }
 
-       /* If the connection is down, trigger a connect. We may
-        * have scheduled a delayed reconnect however - in this case
-        * we should not interfere.
-        */
-       if (rds_conn_state(conn) == RDS_CONN_DOWN &&
-           !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
-               queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
+       rds_conn_connect_if_down(conn);
 
        ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
        if (ret) {
@@ -1108,14 +1099,9 @@ rds_send_pong(struct rds_connection *conn, __be16 dport)
        }
 
        rm->m_daddr = conn->c_faddr;
+       rm->data.op_active = 1;
 
-       /* If the connection is down, trigger a connect. We may
-        * have scheduled a delayed reconnect however - in this case
-        * we should not interfere.
-        */
-       if (rds_conn_state(conn) == RDS_CONN_DOWN &&
-           !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
-               queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
+       rds_conn_connect_if_down(conn);
 
        ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
        if (ret)
@@ -1135,7 +1121,9 @@ rds_send_pong(struct rds_connection *conn, __be16 dport)
        rds_stats_inc(s_send_queued);
        rds_stats_inc(s_send_pong);
 
-       queue_delayed_work(rds_wq, &conn->c_send_w, 0);
+       if (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags))
+               rds_send_xmit(conn);
+
        rds_message_put(rm);
        return 0;