The SCSI core sends the REPORT LUNS and INQUIRY commands before calling device_add_disk(). This patch prevents that bio_max_size() triggers the following kernel crash during a SCSI LUN scan: BUG: KASAN: null-ptr-deref in bio_add_hw_page+0xa6/0x310 Read of size 8 at addr 00000000000005a8 by task kworker/u16:0/7 CPU: 0 PID: 7 Comm: kworker/u16:0 Not tainted 5.12.0-rc8-dbg+ #18 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a-rebuilt.opensuse.org 04/01/2014 Workqueue: events_unbound async_run_entry_fn Call Trace: show_stack+0x52/0x58 dump_stack+0x9d/0xcf kasan_report.cold+0x4b/0x50 __asan_load8+0x69/0x90 bio_add_hw_page+0xa6/0x310 bio_add_pc_page+0xaa/0xe0 bio_map_kern+0xdc/0x1a0 blk_rq_map_kern+0xcd/0x2d0 __scsi_execute+0x9a/0x290 [scsi_mod] scsi_probe_lun.constprop.0+0x17c/0x660 [scsi_mod] scsi_probe_and_add_lun+0x178/0x750 [scsi_mod] __scsi_add_device+0x18c/0x1a0 [scsi_mod] ata_scsi_scan_host+0xe5/0x260 [libata] async_port_probe+0x94/0xb0 [libata] async_run_entry_fn+0x7d/0x2d0 process_one_work+0x582/0xac0 worker_thread+0x8f/0x5a0 kthread+0x222/0x250 ret_from_fork+0x1f/0x30 Cc: Christoph Hellwig <hch@xxxxxx> Cc: Ming Lei <ming.lei@xxxxxxxxxx> Cc: Changheun Lee <nanich.lee@xxxxxxxxxxx> Cc: Jaegeuk Kim <jaegeuk@xxxxxxxxxx> Fixes: 15050b63567c ("bio: limit bio max size") Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx> --- block/bio.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/block/bio.c b/block/bio.c index d718c63b3533..a8879ee6c59a 100644 --- a/block/bio.c +++ b/block/bio.c @@ -257,9 +257,10 @@ EXPORT_SYMBOL(bio_init); unsigned int bio_max_size(struct bio *bio) { - struct request_queue *q = bio->bi_bdev->bd_disk->queue; + if (!bio->bi_bdev) + return UINT_MAX; - return q->limits.bio_max_bytes; + return bio->bi_bdev->bd_disk->queue->limits.bio_max_bytes; } /**