]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/5.0.6/scsi-sd-fix-a-race-between-closing-an-sd-device-and-sd-i-o.patch
Linux 5.0.6
[thirdparty/kernel/stable-queue.git] / releases / 5.0.6 / scsi-sd-fix-a-race-between-closing-an-sd-device-and-sd-i-o.patch
CommitLineData
8095cb16
GKH
1From c14a57264399efd39514a2329c591a4b954246d8 Mon Sep 17 00:00:00 2001
2From: Bart Van Assche <bvanassche@acm.org>
3Date: Mon, 25 Mar 2019 10:01:46 -0700
4Subject: scsi: sd: Fix a race between closing an sd device and sd I/O
5
6From: Bart Van Assche <bvanassche@acm.org>
7
8commit c14a57264399efd39514a2329c591a4b954246d8 upstream.
9
10The scsi_end_request() function calls scsi_cmd_to_driver() indirectly and
11hence needs the disk->private_data pointer. Avoid that that pointer is
12cleared before all affected I/O requests have finished. This patch avoids
13that the following crash occurs:
14
15Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
16Call trace:
17 scsi_mq_uninit_cmd+0x1c/0x30
18 scsi_end_request+0x7c/0x1b8
19 scsi_io_completion+0x464/0x668
20 scsi_finish_command+0xbc/0x160
21 scsi_eh_flush_done_q+0x10c/0x170
22 sas_scsi_recover_host+0x84c/0xa98 [libsas]
23 scsi_error_handler+0x140/0x5b0
24 kthread+0x100/0x12c
25 ret_from_fork+0x10/0x18
26
27Cc: Christoph Hellwig <hch@lst.de>
28Cc: Ming Lei <ming.lei@redhat.com>
29Cc: Hannes Reinecke <hare@suse.com>
30Cc: Johannes Thumshirn <jthumshirn@suse.de>
31Cc: Jason Yan <yanaijie@huawei.com>
32Cc: <stable@vger.kernel.org>
33Signed-off-by: Bart Van Assche <bvanassche@acm.org>
34Reported-by: Jason Yan <yanaijie@huawei.com>
35Reviewed-by: Christoph Hellwig <hch@lst.de>
36Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
37Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
38
39---
40 drivers/scsi/sd.c | 19 +++++++++++++------
41 1 file changed, 13 insertions(+), 6 deletions(-)
42
43--- a/drivers/scsi/sd.c
44+++ b/drivers/scsi/sd.c
45@@ -1398,11 +1398,6 @@ static void sd_release(struct gendisk *d
46 scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
47 }
48
49- /*
50- * XXX and what if there are packets in flight and this close()
51- * XXX is followed by a "rmmod sd_mod"?
52- */
53-
54 scsi_disk_put(sdkp);
55 }
56
57@@ -3488,9 +3483,21 @@ static void scsi_disk_release(struct dev
58 {
59 struct scsi_disk *sdkp = to_scsi_disk(dev);
60 struct gendisk *disk = sdkp->disk;
61-
62+ struct request_queue *q = disk->queue;
63+
64 ida_free(&sd_index_ida, sdkp->index);
65
66+ /*
67+ * Wait until all requests that are in progress have completed.
68+ * This is necessary to avoid that e.g. scsi_end_request() crashes
69+ * due to clearing the disk->private_data pointer. Wait from inside
70+ * scsi_disk_release() instead of from sd_release() to avoid that
71+ * freezing and unfreezing the request queue affects user space I/O
72+ * in case multiple processes open a /dev/sd... node concurrently.
73+ */
74+ blk_mq_freeze_queue(q);
75+ blk_mq_unfreeze_queue(q);
76+
77 disk->private_data = NULL;
78 put_disk(disk);
79 put_device(&sdkp->device->sdev_gendev);