scsi: core: free sgtables in case command setup fails
authorJohannes Thumshirn <johannes.thumshirn@wdc.com>
Tue, 28 Apr 2020 10:45:55 +0000 (19:45 +0900)
committerMartin K. Petersen <martin.petersen@oracle.com>
Thu, 30 Apr 2020 02:04:24 +0000 (22:04 -0400)
commit20a66f2bf280277ab5bb22e27445153b4eb0ac88
tree5d8f106088b12fbbefee9d742196f7fc02324318
parentea941016abf7e6f81b130f8eb792e9ad0971237a
scsi: core: free sgtables in case command setup fails

In case scsi_setup_fs_cmnd() fails we're not freeing the sgtables allocated
by scsi_init_io(), thus we leak the allocated memory.

Free the sgtables allocated by scsi_init_io() in case scsi_setup_fs_cmnd()
fails.

Technically scsi_setup_scsi_cmnd() does not suffer from this problem as it
can only fail if scsi_init_io() fails, so it does not have sgtables
allocated. But to maintain symmetry and as a measure of defensive
programming, free the sgtables on scsi_setup_scsi_cmnd() failure as well.
scsi_mq_free_sgtables() has safeguards against double-freeing of memory so
this is safe to do.

While we're at it, rename scsi_mq_free_sgtables() to scsi_free_sgtables().

Link: https://bugzilla.kernel.org/show_bug.cgi?id=205595
Link: https://lore.kernel.org/r/20200428104605.8143-2-johannes.thumshirn@wdc.com
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Daniel Wagner <dwagner@suse.de>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/scsi/scsi_lib.c