Skip to content

Commit 806276b

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.dk/linux-block
Pull block fixes from Jens Axboe: "Five fixes for this series: - a fix from me to ensure that blk-mq drivers that terminate IO in their ->queue_rq() handler by returning QUEUE_ERROR don't stall with a scheduler enabled. - four nbd fixes from Josef and Ratna, fixing various problems that are critical enough to go in for this cycle. They have been well tested" * 'for-linus' of git://git.kernel.dk/linux-block: nbd: replace kill_bdev() with __invalidate_device() nbd: set queue timeout properly nbd: set rq->errors to actual error code nbd: handle ERESTARTSYS properly blk-mq: include errors in did_work calculation
2 parents 52b9c81 + abbbdf1 commit 806276b

File tree

2 files changed

+107
-36
lines changed

2 files changed

+107
-36
lines changed

block/blk-mq.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list)
969969
struct request *rq;
970970
LIST_HEAD(driver_list);
971971
struct list_head *dptr;
972-
int queued, ret = BLK_MQ_RQ_QUEUE_OK;
972+
int errors, queued, ret = BLK_MQ_RQ_QUEUE_OK;
973973

974974
/*
975975
* Start off with dptr being NULL, so we start the first request
@@ -980,7 +980,7 @@ bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list)
980980
/*
981981
* Now process all the entries, sending them to the driver.
982982
*/
983-
queued = 0;
983+
errors = queued = 0;
984984
while (!list_empty(list)) {
985985
struct blk_mq_queue_data bd;
986986

@@ -1037,6 +1037,7 @@ bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list)
10371037
default:
10381038
pr_err("blk-mq: bad return on queue: %d\n", ret);
10391039
case BLK_MQ_RQ_QUEUE_ERROR:
1040+
errors++;
10401041
rq->errors = -EIO;
10411042
blk_mq_end_request(rq, rq->errors);
10421043
break;
@@ -1088,7 +1089,7 @@ bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list)
10881089
blk_mq_run_hw_queue(hctx, true);
10891090
}
10901091

1091-
return queued != 0;
1092+
return (queued + errors) != 0;
10921093
}
10931094

10941095
static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)

drivers/block/nbd.c

Lines changed: 103 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ static DEFINE_MUTEX(nbd_index_mutex);
4747
struct nbd_sock {
4848
struct socket *sock;
4949
struct mutex tx_lock;
50+
struct request *pending;
51+
int sent;
5052
};
5153

5254
#define NBD_TIMEDOUT 0
@@ -124,7 +126,8 @@ static const char *nbdcmd_to_ascii(int cmd)
124126

125127
static int nbd_size_clear(struct nbd_device *nbd, struct block_device *bdev)
126128
{
127-
bd_set_size(bdev, 0);
129+
if (bdev->bd_openers <= 1)
130+
bd_set_size(bdev, 0);
128131
set_capacity(nbd->disk, 0);
129132
kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
130133

@@ -190,7 +193,7 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
190193

191194
dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
192195
set_bit(NBD_TIMEDOUT, &nbd->runtime_flags);
193-
req->errors++;
196+
req->errors = -EIO;
194197

195198
mutex_lock(&nbd->config_lock);
196199
sock_shutdown(nbd);
@@ -202,7 +205,7 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
202205
* Send or receive packet.
203206
*/
204207
static int sock_xmit(struct nbd_device *nbd, int index, int send,
205-
struct iov_iter *iter, int msg_flags)
208+
struct iov_iter *iter, int msg_flags, int *sent)
206209
{
207210
struct socket *sock = nbd->socks[index]->sock;
208211
int result;
@@ -237,6 +240,8 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send,
237240
result = -EPIPE; /* short read */
238241
break;
239242
}
243+
if (sent)
244+
*sent += result;
240245
} while (msg_data_left(&msg));
241246

242247
tsk_restore_flags(current, pflags, PF_MEMALLOC);
@@ -248,6 +253,7 @@ static int sock_xmit(struct nbd_device *nbd, int index, int send,
248253
static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
249254
{
250255
struct request *req = blk_mq_rq_from_pdu(cmd);
256+
struct nbd_sock *nsock = nbd->socks[index];
251257
int result;
252258
struct nbd_request request = {.magic = htonl(NBD_REQUEST_MAGIC)};
253259
struct kvec iov = {.iov_base = &request, .iov_len = sizeof(request)};
@@ -256,6 +262,7 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
256262
struct bio *bio;
257263
u32 type;
258264
u32 tag = blk_mq_unique_tag(req);
265+
int sent = nsock->sent, skip = 0;
259266

260267
iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
261268

@@ -283,6 +290,17 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
283290
return -EIO;
284291
}
285292

293+
/* We did a partial send previously, and we at least sent the whole
294+
* request struct, so just go and send the rest of the pages in the
295+
* request.
296+
*/
297+
if (sent) {
298+
if (sent >= sizeof(request)) {
299+
skip = sent - sizeof(request);
300+
goto send_pages;
301+
}
302+
iov_iter_advance(&from, sent);
303+
}
286304
request.type = htonl(type);
287305
if (type != NBD_CMD_FLUSH) {
288306
request.from = cpu_to_be64((u64)blk_rq_pos(req) << 9);
@@ -294,15 +312,27 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
294312
cmd, nbdcmd_to_ascii(type),
295313
(unsigned long long)blk_rq_pos(req) << 9, blk_rq_bytes(req));
296314
result = sock_xmit(nbd, index, 1, &from,
297-
(type == NBD_CMD_WRITE) ? MSG_MORE : 0);
315+
(type == NBD_CMD_WRITE) ? MSG_MORE : 0, &sent);
298316
if (result <= 0) {
317+
if (result == -ERESTARTSYS) {
318+
/* If we havne't sent anything we can just return BUSY,
319+
* however if we have sent something we need to make
320+
* sure we only allow this req to be sent until we are
321+
* completely done.
322+
*/
323+
if (sent) {
324+
nsock->pending = req;
325+
nsock->sent = sent;
326+
}
327+
return BLK_MQ_RQ_QUEUE_BUSY;
328+
}
299329
dev_err_ratelimited(disk_to_dev(nbd->disk),
300330
"Send control failed (result %d)\n", result);
301331
return -EIO;
302332
}
303-
333+
send_pages:
304334
if (type != NBD_CMD_WRITE)
305-
return 0;
335+
goto out;
306336

307337
bio = req->bio;
308338
while (bio) {
@@ -318,8 +348,25 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
318348
cmd, bvec.bv_len);
319349
iov_iter_bvec(&from, ITER_BVEC | WRITE,
320350
&bvec, 1, bvec.bv_len);
321-
result = sock_xmit(nbd, index, 1, &from, flags);
351+
if (skip) {
352+
if (skip >= iov_iter_count(&from)) {
353+
skip -= iov_iter_count(&from);
354+
continue;
355+
}
356+
iov_iter_advance(&from, skip);
357+
skip = 0;
358+
}
359+
result = sock_xmit(nbd, index, 1, &from, flags, &sent);
322360
if (result <= 0) {
361+
if (result == -ERESTARTSYS) {
362+
/* We've already sent the header, we
363+
* have no choice but to set pending and
364+
* return BUSY.
365+
*/
366+
nsock->pending = req;
367+
nsock->sent = sent;
368+
return BLK_MQ_RQ_QUEUE_BUSY;
369+
}
323370
dev_err(disk_to_dev(nbd->disk),
324371
"Send data failed (result %d)\n",
325372
result);
@@ -336,6 +383,9 @@ static int nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd, int index)
336383
}
337384
bio = next;
338385
}
386+
out:
387+
nsock->pending = NULL;
388+
nsock->sent = 0;
339389
return 0;
340390
}
341391

@@ -353,7 +403,7 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
353403

354404
reply.magic = 0;
355405
iov_iter_kvec(&to, READ | ITER_KVEC, &iov, 1, sizeof(reply));
356-
result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL);
406+
result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
357407
if (result <= 0) {
358408
if (!test_bit(NBD_DISCONNECTED, &nbd->runtime_flags) &&
359409
!test_bit(NBD_DISCONNECT_REQUESTED, &nbd->runtime_flags))
@@ -383,7 +433,7 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
383433
if (ntohl(reply.error)) {
384434
dev_err(disk_to_dev(nbd->disk), "Other side returned error (%d)\n",
385435
ntohl(reply.error));
386-
req->errors++;
436+
req->errors = -EIO;
387437
return cmd;
388438
}
389439

@@ -395,11 +445,11 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
395445
rq_for_each_segment(bvec, req, iter) {
396446
iov_iter_bvec(&to, ITER_BVEC | READ,
397447
&bvec, 1, bvec.bv_len);
398-
result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL);
448+
result = sock_xmit(nbd, index, 0, &to, MSG_WAITALL, NULL);
399449
if (result <= 0) {
400450
dev_err(disk_to_dev(nbd->disk), "Receive data failed (result %d)\n",
401451
result);
402-
req->errors++;
452+
req->errors = -EIO;
403453
return cmd;
404454
}
405455
dev_dbg(nbd_to_dev(nbd), "request %p: got %d bytes data\n",
@@ -469,7 +519,7 @@ static void nbd_clear_req(struct request *req, void *data, bool reserved)
469519
if (!blk_mq_request_started(req))
470520
return;
471521
cmd = blk_mq_rq_to_pdu(req);
472-
req->errors++;
522+
req->errors = -EIO;
473523
nbd_end_request(cmd);
474524
}
475525

@@ -482,22 +532,23 @@ static void nbd_clear_que(struct nbd_device *nbd)
482532
}
483533

484534

485-
static void nbd_handle_cmd(struct nbd_cmd *cmd, int index)
535+
static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
486536
{
487537
struct request *req = blk_mq_rq_from_pdu(cmd);
488538
struct nbd_device *nbd = cmd->nbd;
489539
struct nbd_sock *nsock;
540+
int ret;
490541

491542
if (index >= nbd->num_connections) {
492543
dev_err_ratelimited(disk_to_dev(nbd->disk),
493544
"Attempted send on invalid socket\n");
494-
goto error_out;
545+
return -EINVAL;
495546
}
496547

497548
if (test_bit(NBD_DISCONNECTED, &nbd->runtime_flags)) {
498549
dev_err_ratelimited(disk_to_dev(nbd->disk),
499550
"Attempted send on closed socket\n");
500-
goto error_out;
551+
return -EINVAL;
501552
}
502553

503554
req->errors = 0;
@@ -508,29 +559,30 @@ static void nbd_handle_cmd(struct nbd_cmd *cmd, int index)
508559
mutex_unlock(&nsock->tx_lock);
509560
dev_err_ratelimited(disk_to_dev(nbd->disk),
510561
"Attempted send on closed socket\n");
511-
goto error_out;
562+
return -EINVAL;
512563
}
513564

514-
if (nbd_send_cmd(nbd, cmd, index) != 0) {
515-
dev_err_ratelimited(disk_to_dev(nbd->disk),
516-
"Request send failed\n");
517-
req->errors++;
518-
nbd_end_request(cmd);
565+
/* Handle the case that we have a pending request that was partially
566+
* transmitted that _has_ to be serviced first. We need to call requeue
567+
* here so that it gets put _after_ the request that is already on the
568+
* dispatch list.
569+
*/
570+
if (unlikely(nsock->pending && nsock->pending != req)) {
571+
blk_mq_requeue_request(req, true);
572+
ret = 0;
573+
goto out;
519574
}
520-
575+
ret = nbd_send_cmd(nbd, cmd, index);
576+
out:
521577
mutex_unlock(&nsock->tx_lock);
522-
523-
return;
524-
525-
error_out:
526-
req->errors++;
527-
nbd_end_request(cmd);
578+
return ret;
528579
}
529580

530581
static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
531582
const struct blk_mq_queue_data *bd)
532583
{
533584
struct nbd_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
585+
int ret;
534586

535587
/*
536588
* Since we look at the bio's to send the request over the network we
@@ -543,10 +595,20 @@ static int nbd_queue_rq(struct blk_mq_hw_ctx *hctx,
543595
*/
544596
init_completion(&cmd->send_complete);
545597
blk_mq_start_request(bd->rq);
546-
nbd_handle_cmd(cmd, hctx->queue_num);
598+
599+
/* We can be called directly from the user space process, which means we
600+
* could possibly have signals pending so our sendmsg will fail. In
601+
* this case we need to return that we are busy, otherwise error out as
602+
* appropriate.
603+
*/
604+
ret = nbd_handle_cmd(cmd, hctx->queue_num);
605+
if (ret < 0)
606+
ret = BLK_MQ_RQ_QUEUE_ERROR;
607+
if (!ret)
608+
ret = BLK_MQ_RQ_QUEUE_OK;
547609
complete(&cmd->send_complete);
548610

549-
return BLK_MQ_RQ_QUEUE_OK;
611+
return ret;
550612
}
551613

552614
static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
@@ -581,6 +643,8 @@ static int nbd_add_socket(struct nbd_device *nbd, struct block_device *bdev,
581643

582644
mutex_init(&nsock->tx_lock);
583645
nsock->sock = sock;
646+
nsock->pending = NULL;
647+
nsock->sent = 0;
584648
socks[nbd->num_connections++] = nsock;
585649

586650
if (max_part)
@@ -602,6 +666,8 @@ static void nbd_reset(struct nbd_device *nbd)
602666

603667
static void nbd_bdev_reset(struct block_device *bdev)
604668
{
669+
if (bdev->bd_openers > 1)
670+
return;
605671
set_device_ro(bdev, false);
606672
bdev->bd_inode->i_size = 0;
607673
if (max_part > 0) {
@@ -634,7 +700,7 @@ static void send_disconnects(struct nbd_device *nbd)
634700

635701
for (i = 0; i < nbd->num_connections; i++) {
636702
iov_iter_kvec(&from, WRITE | ITER_KVEC, &iov, 1, sizeof(request));
637-
ret = sock_xmit(nbd, i, 1, &from, 0);
703+
ret = sock_xmit(nbd, i, 1, &from, 0, NULL);
638704
if (ret <= 0)
639705
dev_err(disk_to_dev(nbd->disk),
640706
"Send disconnect failed %d\n", ret);
@@ -665,7 +731,8 @@ static int nbd_clear_sock(struct nbd_device *nbd, struct block_device *bdev)
665731
{
666732
sock_shutdown(nbd);
667733
nbd_clear_que(nbd);
668-
kill_bdev(bdev);
734+
735+
__invalidate_device(bdev, true);
669736
nbd_bdev_reset(bdev);
670737
/*
671738
* We want to give the run thread a chance to wait for everybody
@@ -781,7 +848,10 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
781848
nbd_size_set(nbd, bdev, nbd->blksize, arg);
782849
return 0;
783850
case NBD_SET_TIMEOUT:
784-
nbd->tag_set.timeout = arg * HZ;
851+
if (arg) {
852+
nbd->tag_set.timeout = arg * HZ;
853+
blk_queue_rq_timeout(nbd->disk->queue, arg * HZ);
854+
}
785855
return 0;
786856

787857
case NBD_SET_FLAGS:

0 commit comments

Comments
 (0)