block: fix infinite loop if the device loses discard capability
authorMikulas Patocka <mpatocka@redhat.com>
Tue, 3 Jul 2018 17:34:22 +0000 (13:34 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 29 Dec 2018 12:40:14 +0000 (13:40 +0100)
[ Upstream commit b88aef36b87c9787a4db724923ec4f57dfd513f3 ]

If __blkdev_issue_discard is in progress and a device mapper device is
reloaded with a table that doesn't support discard,
q->limits.max_discard_sectors is set to zero. This results in infinite
loop in __blkdev_issue_discard.

This patch checks if max_discard_sectors is zero and aborts with
-EOPNOTSUPP.

Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Tested-by: Zdenek Kabelac <mpatocka@redhat.com>
Cc: stable@vger.kernel.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Sasha Levin <sashal@kernel.org>
block/blk-lib.c

index d8b89c58af3d49f528b10a3d2af2543de7527338..af1d26f798783fe858087454fde5c1ccde013640 100644 (file)
@@ -69,6 +69,8 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
                 */
                req_sects = min_t(sector_t, nr_sects,
                                        q->limits.max_discard_sectors);
+               if (!req_sects)
+                       goto fail;
                if (req_sects > UINT_MAX >> 9)
                        req_sects = UINT_MAX >> 9;
 
@@ -106,6 +108,14 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 
        *biop = bio;
        return 0;
+
+fail:
+       if (bio) {
+               submit_bio_wait(bio);
+               bio_put(bio);
+       }
+       *biop = NULL;
+       return -EOPNOTSUPP;
 }
 EXPORT_SYMBOL(__blkdev_issue_discard);