Remove was_interrupted(), nbd_sched_pending_work(), nbd_pending_cmd_work(), NBD_CMD_PARTIAL_SEND and the nsock->pending, nsock->sent and nsock->work members. Treat a failure of sock_xmit() in nbd_send_cmd() the same way regardless of the error code: mark the socket dead and requeue the request. The partial-send handling was introduced for the case where sendmsg() fails with -ERESTARTSYS or -EINTR because a signal is pending in the task that called nbd_queue_rq(). Since requests are now transmitted from nbd_tx_work(), which runs on a kernel worker thread, no signal can be pending while sending and that code is unreachable. Other send errors, including a send timeout after part of a request has been sent, already mark the socket dead and requeue the request, so a truncated request is never followed by another request on the same socket. Signed-off-by: Bart Van Assche --- drivers/block/nbd.c | 178 +------------------------------------------- 1 file changed, 3 insertions(+), 175 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index f713779b5a5d..3d834e4e1349 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -57,12 +57,9 @@ static int nbd_total_devices = 0; struct nbd_sock { struct socket *sock; struct mutex tx_lock; - struct request *pending; - int sent; bool dead; int fallback_index; int cookie; - struct work_struct work; spinlock_t cmd_lock; struct list_head cmd_list; struct work_struct tx_work; @@ -147,9 +144,6 @@ struct nbd_device { */ #define NBD_CMD_INFLIGHT 2 -/* Just part of request header or data payload is sent successfully */ -#define NBD_CMD_PARTIAL_SEND 3 - struct nbd_cmd { struct nbd_device *nbd; struct mutex lock; @@ -336,8 +330,6 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock, } } WRITE_ONCE(nsock->dead, true); - nsock->pending = NULL; - nsock->sent = 0; } static void nbd_apply_limits(struct queue_limits *lim, u32 flags) @@ -475,12 +467,6 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req) if (!mutex_trylock(&cmd->lock)) return BLK_EH_RESET_TIMER; - /* partial send is handled in nbd_sock's work function */ - if (test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags)) { - mutex_unlock(&cmd->lock); - return BLK_EH_RESET_TIMER; - } - if (!test_bit(NBD_CMD_INFLIGHT, &cmd->flags)) { mutex_unlock(&cmd->lock); return BLK_EH_DONE; @@ -623,47 +609,6 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send, return __sock_xmit(nbd, sock, send, iter, msg_flags, sent); } -/* - * Different settings for sk->sk_sndtimeo can result in different return values - * if there is a signal pending when we enter sendmsg, because reasons? - */ -static inline int was_interrupted(int result) -{ - return result == -ERESTARTSYS || result == -EINTR; -} - -/* - * We've already sent header or part of data payload, have no choice but - * to set pending and schedule it in work. - * - * The request must not be requeued, otherwise this same request may be - * re-dispatched with different tag, but our header has been sent out with - * old tag, and this way does confuse reply handling. - * - * Returns true if nsock->work has been scheduled. Returns false if no - * config_refs reference could be obtained because the device is being torn - * down. In that case the caller must mark @nsock dead, since a truncated - * request has been sent, and fail the request. - */ -static bool nbd_sched_pending_work(struct nbd_device *nbd, - struct nbd_sock *nsock, - struct nbd_cmd *cmd, int sent) -{ - struct request *req = blk_mq_rq_from_pdu(cmd); - - /* pending work should be scheduled only once */ - WARN_ON_ONCE(test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags)); - - if (!refcount_inc_not_zero(&nbd->config_refs)) - return false; - - nsock->pending = req; - nsock->sent = sent; - set_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags); - schedule_work(&nsock->work); - return true; -} - /* * Returns BLK_STS_IOERR if sending failed and cannot be retried. */ @@ -681,7 +626,6 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, u64 handle; u32 type; u32 nbd_cmd_flags = 0; - int sent = nsock->sent, skip = 0; lockdep_assert_held(&cmd->lock); lockdep_assert_held(&nsock->tx_lock); @@ -704,23 +648,7 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, if ((req->cmd_flags & REQ_NOUNMAP) && (type == NBD_CMD_WRITE_ZEROES)) nbd_cmd_flags |= NBD_CMD_FLAG_NO_HOLE; - /* We did a partial send previously, and we at least sent the whole - * request struct, so just go and send the rest of the pages in the - * request. - */ - if (sent) { - if (sent >= sizeof(request)) { - skip = sent - sizeof(request); - - /* initialize handle for tracing purposes */ - handle = nbd_cmd_handle(cmd); - - goto send_pages; - } - iov_iter_advance(&from, sent); - } else { - cmd->cmd_cookie++; - } + cmd->cmd_cookie++; cmd->index = index; cmd->cookie = nsock->cookie; cmd->retries = 0; @@ -738,36 +666,13 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, req, nbdcmd_to_ascii(type), (unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req)); result = sock_xmit(nbd, index, 1, &from, - (type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent); + (type == NBD_CMD_WRITE) ? MSG_MORE : 0, NULL); trace_nbd_header_sent(req, handle); if (result < 0) { - if (was_interrupted(result)) { - /* - * If we haven't sent anything we can just requeue, - * however if we have sent something we need to make - * sure we only allow this req to be sent until we are - * completely done. - */ - if (sent) { - if (nbd_sched_pending_work(nbd, nsock, cmd, - sent)) - return BLK_STS_OK; - /* - * Don't send anything after a truncated request. - * The device is being torn down, so don't notify - * user space that the link is dead. - */ - nbd_mark_nsock_dead(nbd, nsock, 0); - return BLK_STS_IOERR; - } - nbd_requeue_cmd(cmd); - return BLK_STS_OK; - } dev_err_ratelimited(disk_to_dev(nbd->disk), "Send control failed (result %d)\n", result); goto requeue; } -send_pages: if (type != NBD_CMD_WRITE) goto out; @@ -784,23 +689,8 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, dev_dbg(nbd_to_dev(nbd), "request %p: sending %d bytes data\n", req, bvec.bv_len); iov_iter_bvec(&from, ITER_SOURCE, &bvec, 1, bvec.bv_len); - if (skip) { - if (skip >= iov_iter_count(&from)) { - skip -= iov_iter_count(&from); - continue; - } - iov_iter_advance(&from, skip); - skip = 0; - } - result = sock_xmit(nbd, index, 1, &from, flags, &sent); + result = sock_xmit(nbd, index, 1, &from, flags, NULL); if (result < 0) { - if (was_interrupted(result)) { - if (nbd_sched_pending_work(nbd, nsock, - cmd, sent)) - return BLK_STS_OK; - nbd_mark_nsock_dead(nbd, nsock, 0); - return BLK_STS_IOERR; - } dev_err(disk_to_dev(nbd->disk), "Send data failed (result %d)\n", result); @@ -819,20 +709,10 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, } out: trace_nbd_payload_sent(req, handle); - nsock->pending = NULL; - nsock->sent = 0; __set_bit(NBD_CMD_INFLIGHT, &cmd->flags); return BLK_STS_OK; requeue: - /* - * Can't requeue in case we are dealing with partial send - * - * We must run from pending work function. - * */ - if (test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags)) - return BLK_STS_OK; - /* retry on a different socket */ dev_err_ratelimited(disk_to_dev(nbd->disk), "Request send failed, requeueing\n"); @@ -841,44 +721,6 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, return BLK_STS_OK; } -/* handle partial sending */ -static void nbd_pending_cmd_work(struct work_struct *work) -{ - struct nbd_sock *nsock = container_of(work, struct nbd_sock, work); - struct request *req = nsock->pending; - struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req); - struct nbd_device *nbd = cmd->nbd; - unsigned long deadline = READ_ONCE(req->deadline); - unsigned int wait_ms = 2; - - mutex_lock(&cmd->lock); - - WARN_ON_ONCE(test_bit(NBD_CMD_REQUEUED, &cmd->flags)); - if (WARN_ON_ONCE(!test_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags))) - goto out; - - mutex_lock(&nsock->tx_lock); - while (true) { - nbd_send_cmd(nbd, cmd, cmd->index); - if (!nsock->pending) - break; - - /* don't bother timeout handler for partial sending */ - if (READ_ONCE(jiffies) + msecs_to_jiffies(wait_ms) >= deadline) { - cmd->status = BLK_STS_IOERR; - blk_mq_complete_request(req); - break; - } - msleep(wait_ms); - wait_ms *= 2; - } - mutex_unlock(&nsock->tx_lock); - clear_bit(NBD_CMD_PARTIAL_SEND, &cmd->flags); -out: - mutex_unlock(&cmd->lock); - nbd_config_put(nbd); -} - static int nbd_read_reply(struct nbd_device *nbd, struct socket *sock, struct nbd_reply *reply) { @@ -1179,19 +1021,8 @@ static blk_status_t nbd_handle_cmd(struct nbd_cmd *cmd, int index) goto again; } - /* Handle the case that we have a pending request that was partially - * transmitted that _has_ to be serviced first. We need to call requeue - * here so that it gets put _after_ the request that is already on the - * dispatch list. - */ blk_mq_start_request(req); - if (unlikely(nsock->pending && nsock->pending != req)) { - nbd_requeue_cmd(cmd); - ret = BLK_STS_OK; - goto out; - } ret = nbd_send_cmd(nbd, cmd, index); -out: mutex_unlock(&nsock->tx_lock); return ret; } @@ -1374,14 +1205,11 @@ static int nbd_add_socket(struct nbd_device *nbd, unsigned long arg, nsock->dead = false; mutex_init(&nsock->tx_lock); nsock->sock = sock; - nsock->pending = NULL; - nsock->sent = 0; nsock->cookie = 0; nsock->index = config->num_connections; spin_lock_init(&nsock->cmd_lock); INIT_LIST_HEAD(&nsock->cmd_list); INIT_WORK(&nsock->tx_work, nbd_tx_work); - INIT_WORK(&nsock->work, nbd_pending_cmd_work); socks[config->num_connections++] = nsock; atomic_inc(&config->live_connections);