[PATCH 04/10] blk-lib: check for kill signal in ioctl BLKSECDISCARD

Christoph Hellwig
2024-07-01T16:52:41Z

Secure Erase can access a significant capacity and take longer than the
user expected. A user may change their mind about wanting to run that
command and attempt to kill the process and do something else with their
device. But since the task is uninterruptable, they have to wait for it
to finish, which could be many hours.

Open code blkdev_issue_secure_erase in the BLKSECDISCARD ioctl handler
and check for a fatal signal at each iteration so the user doesn't have
to wait for their regretted operation to complete naturally.

Heavily based on an earlier patch from Keith Busch.

Reported-by: Conrad Meyer <conradmeyer@meta.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
block/ioctl.c | 34 +++++++++++++++++++++++++++++++---
1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/block/ioctl.c b/block/ioctl.c
index f53121edb9a15f..45668a21cdb374 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -164,6 +164,9 @@ static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode,
void __user *argp)
{
unsigned int bs_mask = bdev_logical_block_size(bdev) - 1;
+ struct bio *prev = NULL, *bio;
+ sector_t sector, nr_sects;
+ struct blk_plug plug;
uint64_t start, len;
uint64_t range[2];
int err;
@@ -187,11 +190,36 @@ static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode,
if (start + len > bdev_nr_bytes(bdev))
return -EINVAL;

+ sector = start >> SECTOR_SHIFT;
+ nr_sects = len >> SECTOR_SHIFT;
+
filemap_invalidate_lock(bdev->bd_mapping);
err = truncate_bdev_range(bdev, mode, start, start + len - 1);
- if (!err)
- err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9,
- GFP_KERNEL);
+ if (err)
+ goto out_unlock;
+
+ blk_start_plug(&plug);
+ while (1) {
+ if (fatal_signal_pending(current)) {
+ if (prev)
+ bio_await_chain(prev);
+ err = -EINTR;
+ goto out_unplug;
+ }
+ bio = blk_alloc_secure_erase_bio(bdev, &sector, &nr_sects,
+ GFP_KERNEL);
+ if (!bio)
+ break;
+ prev = bio_chain_and_submit(prev, bio);
+ }
+ if (prev) {
+ err = submit_bio_wait(prev);
+ bio_put(prev);
+ }
+
+out_unplug:
+ blk_finish_plug(&plug);
+out_unlock:
filemap_invalidate_unlock(bdev->bd_mapping);
return err;
}
--
2.43.0