Skip to contentSkip to conversation

block: make long runnint operations killable

PATCH Superseded Keith Busch 5 participants 17 messages Feb 22, 2024 – Feb 23, 2024 Block layer

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover block: make long runnint operations killable

From: Keith Busch <[email protected]>

Changes from v2:

Wait for chained bio's to complete before returning from kill signal.

Miscellaneous cleanup patches for style and consistency.

Keith Busch (4):
block: blkdev_issue_secure_erase loop style
block: cleanup __blkdev_issue_write_zeroes
block: io wait hang check helper
blk-lib: check for kill signal

 block/bio.c     | 12 +----------
 block/blk-lib.c | 57 ++++++++++++++++++++++++++++++++++++++-----------
 block/blk-mq.c  | 19 +++--------------
 block/blk.h     | 13 +++++++++++
 4 files changed, 61 insertions(+), 40 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/4 block: blkdev_issue_secure_erase loop style

From: Keith Busch <[email protected]>

Use consistent coding style in this file. All the other loops for the same purpose use "while (nr_sects)", so they win.

---
 block/blk-lib.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
block/blk-lib.c +5 -6
index e59c3069e8351..91770da2239f2 100644
@@ -322,7 +322,7 @@ int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
 		return -EPERM;
 
 	blk_start_plug(&plug);
-	for (;;) {
+	while (nr_sects) {
 		unsigned int len = min_t(sector_t, nr_sects, max_sectors);
 
 		bio = blk_next_bio(bio, bdev, 0, REQ_OP_SECURE_ERASE, gfp);
@@ -331,13 +331,12 @@ int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
 
 		sector += len;
 		nr_sects -= len;
-		if (!nr_sects) {
-			ret = submit_bio_wait(bio);
-			bio_put(bio);
-			break;
-		}
 		cond_resched();
 	}
+	if (bio) {
+		ret = submit_bio_wait(bio);
+		bio_put(bio);
+	}
 	blk_finish_plug(&plug);
 
 	return ret;
Signature · 1 line
2.34.1
#4 lore raw

PATCH v3 2/4 block: __blkdev_issue_write_zeroes cleanup

In reply to Keith Busch

From: Keith Busch <[email protected]>

Use min to calculate the next number of sectors like everyone else.

---
 block/blk-lib.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
block/blk-lib.c +5 -8
index 91770da2239f2..d4c476cf3784a 100644
@@ -132,19 +132,16 @@ static int __blkdev_issue_write_zeroes(struct block_device *bdev,
 		return -EOPNOTSUPP;
 
 	while (nr_sects) {
+		unsigned int len = min_t(sector_t, nr_sects, max_write_zeroes_sectors);
+
 		bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask);
 		bio->bi_iter.bi_sector = sector;
 		if (flags & BLKDEV_ZERO_NOUNMAP)
 			bio->bi_opf |= REQ_NOUNMAP;
 
-		if (nr_sects > max_write_zeroes_sectors) {
-			bio->bi_iter.bi_size = max_write_zeroes_sectors << 9;
-			nr_sects -= max_write_zeroes_sectors;
-			sector += max_write_zeroes_sectors;
-		} else {
-			bio->bi_iter.bi_size = nr_sects << 9;
-			nr_sects = 0;
-		}
+		bio->bi_iter.bi_size = len << 9;
+		nr_sects -= len;
+		sector += len;
 		cond_resched();
 	}
 
Signature · 1 line
2.34.1
Reviewed-by on v3 2/4
#5 lore raw

Re: [PATCHv3 2/4] block: __blkdev_issue_write_zeroes cleanup

Quoted 6 lines · Keith Busch

From: Keith Busch <[email protected]>

Use min to calculate the next number of sectors like everyone else.

Quoted 7 lines · Keith Busch

From: Keith Busch <[email protected]>

Use consistent coding style in this file. All the other loops for the same purpose use "while (nr_sects)", so they win.

#6 lore raw

Re: [PATCHv3 2/4] block: __blkdev_issue_write_zeroes cleanup

In reply to Keith Busch
Quoted 14 lines · Keith Busch

From: Keith Busch <[email protected]>

Use min to calculate the next number of sectors like everyone else.

---
 block/blk-lib.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
block/blk-lib.c +0 -0
index 91770da2239f2..d4c476cf3784a 100644

block/blk-lib.c static int __blkdev_issue_write_zeroes(struct block_device *bdev, · line 135

Quoted 4 lines
block/blk-lib.c +1 -0
@@ -132,19 +132,16 @@ static int __blkdev_issue_write_zeroes(struct block_device *bdev,
 		return -EOPNOTSUPP;
 
 	while (nr_sects) {
+		unsigned int len = min_t(sector_t, nr_sects, max_write_zeroes_sectors);

Please avoid the overly long line.

block/blk-lib.c static int __blkdev_issue_write_zeroes(struct block_device *bdev, · line 142

Quoted 1 line
block/blk-lib.c +1 -0
+		bio->bi_iter.bi_size = len << 9;

And if you touch this anyway use SECTOR_SHIFT here.

Otherwise this looks good.

#7 lore raw

PATCH v3 3/4 block: introduce io wait hang check helper

In reply to Keith Busch

From: Keith Busch <[email protected]>

This is the same in two places, and another will be added soon. Create a helper for it.

---
 block/bio.c    | 12 +-----------
 block/blk-mq.c | 19 +++----------------
 block/blk.h    | 13 +++++++++++++
 3 files changed, 17 insertions(+), 27 deletions(-)
block/bio.c +1 -11
index 00847ff1415c3..496867b51609f 100644
@@ -16,7 +16,6 @@
 #include <linux/workqueue.h>
 #include <linux/cgroup.h>
 #include <linux/highmem.h>
-#include <linux/sched/sysctl.h>
 #include <linux/blk-crypto.h>
 #include <linux/xarray.h>
 
@@ -1371,21 +1370,12 @@ int submit_bio_wait(struct bio *bio)
 {
 	DECLARE_COMPLETION_ONSTACK_MAP(done,
 			bio->bi_bdev->bd_disk->lockdep_map);
-	unsigned long hang_check;
 
 	bio->bi_private = &done;
 	bio->bi_end_io = submit_bio_wait_endio;
 	bio->bi_opf |= REQ_SYNC;
 	submit_bio(bio);
-
-	/* Prevent hang_check timer from firing at us during very long I/O */
-	hang_check = sysctl_hung_task_timeout_secs;
-	if (hang_check)
-		while (!wait_for_completion_io_timeout(&done,
-					hang_check * (HZ/2)))
-			;
-	else
-		wait_for_completion_io(&done);
+	blk_wait_io(&done);
 
 	return blk_status_to_errno(bio->bi_status);
 }
block/blk-mq.c +3 -16
index 6abb4ce46baa1..45f994c100446 100644
@@ -21,7 +21,6 @@
 #include <linux/llist.h>
 #include <linux/cpu.h>
 #include <linux/cache.h>
-#include <linux/sched/sysctl.h>
 #include <linux/sched/topology.h>
 #include <linux/sched/signal.h>
 #include <linux/delay.h>
@@ -1409,22 +1408,10 @@ blk_status_t blk_execute_rq(struct request *rq, bool at_head)
 	blk_mq_insert_request(rq, at_head ? BLK_MQ_INSERT_AT_HEAD : 0);
 	blk_mq_run_hw_queue(hctx, false);
 
-	if (blk_rq_is_poll(rq)) {
+	if (blk_rq_is_poll(rq))
 		blk_rq_poll_completion(rq, &wait.done);
-	} else {
-		/*
-		 * Prevent hang_check timer from firing at us during very long
-		 * I/O
-		 */
-		unsigned long hang_check = sysctl_hung_task_timeout_secs;
-
-		if (hang_check)
-			while (!wait_for_completion_io_timeout(&wait.done,
-					hang_check * (HZ/2)))
-				;
-		else
-			wait_for_completion_io(&wait.done);
-	}
+	else
+		blk_wait_io(&wait.done);
 
 	return wait.ret;
 }
block/blk.h +13 -0
index 7c30e2ac8ebcd..6c2749d122ab5 100644
@@ -4,6 +4,7 @@
 
 #include <linux/blk-crypto.h>
 #include <linux/memblock.h>	/* for max_pfn/max_low_pfn */
+#include <linux/sched/sysctl.h>
 #include <linux/timekeeping.h>
 #include <xen/xen.h>
 #include "blk-crypto-internal.h"
@@ -71,6 +72,18 @@ static inline int bio_queue_enter(struct bio *bio)
 	return __bio_queue_enter(q, bio);
 }
 
+static inline void blk_wait_io(struct completion *done)
+{
+	/* Prevent hang_check timer from firing at us during very long I/O */
+	unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
+
+	if (timeout)
+		while (!wait_for_completion_io_timeout(done, timeout))
+			;
+	else
+		wait_for_completion_io(done);
+}
+
 #define BIO_INLINE_VECS 4
 struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,
 		gfp_t gfp_mask);
Signature · 1 line
2.34.1
Reviewed-by on v3 3/4
#8 lore raw

Re: [PATCHv3 3/4] block: introduce io wait hang check helper

Quoted 7 lines · Keith Busch

From: Keith Busch <[email protected]>

This is the same in two places, and another will be added soon. Create a helper for it.

Quoted 7 lines · Keith Busch

From: Keith Busch <[email protected]>

Use consistent coding style in this file. All the other loops for the same purpose use "while (nr_sects)", so they win.

#10 lore raw

PATCH v3 4/4 blk-lib: check for kill signal

In reply to Keith Busch

From: Keith Busch <[email protected]>

Some of these block operations 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.

Check for a fatal signal at each iteration so the user doesn't have to wait for their regretted operation to complete naturally.

---
 block/blk-lib.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
block/blk-lib.c +35 -0
index d4c476cf3784a..9e594f641ce72 100644
@@ -35,6 +35,23 @@ static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector)
 	return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT;
 }
 
+static void abort_bio_endio(struct bio *bio)
+{
+	complete(bio->bi_private);
+	bio_put(bio);
+}
+
+static void abort_bio(struct bio *bio)
+{
+	DECLARE_COMPLETION_ONSTACK_MAP(done,
+			bio->bi_bdev->bd_disk->lockdep_map);
+
+	bio->bi_private = &done;
+	bio->bi_end_io = abort_bio_endio;
+	bio_endio(bio);
+	blk_wait_io(&done);
+}
+
 int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 		sector_t nr_sects, gfp_t gfp_mask, struct bio **biop)
 {
@@ -77,6 +94,10 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 		 * is disabled.
 		 */
 		cond_resched();
+		if (fatal_signal_pending(current)) {
+			abort_bio(bio);
+			return -EINTR;
+		}
 	}
 
 	*biop = bio;
@@ -143,6 +164,10 @@ static int __blkdev_issue_write_zeroes(struct block_device *bdev,
 		nr_sects -= len;
 		sector += len;
 		cond_resched();
+		if (fatal_signal_pending(current)) {
+			abort_bio(bio);
+			return -EINTR;
+		}
 	}
 
 	*biop = bio;
@@ -187,6 +212,10 @@ static int __blkdev_issue_zero_pages(struct block_device *bdev,
 				break;
 		}
 		cond_resched();
+		if (fatal_signal_pending(current)) {
+			abort_bio(bio);
+			return -EINTR;
+		}
 	}
 
 	*biop = bio;
@@ -329,6 +358,12 @@ int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
 		sector += len;
 		nr_sects -= len;
 		cond_resched();
+		if (fatal_signal_pending(current)) {
+			abort_bio(bio);
+			ret = -EINTR;
+			bio = NULL;
+			break;
+		}
 	}
 	if (bio) {
 		ret = submit_bio_wait(bio);
Signature · 1 line
2.34.1
Reviewed-by on v3 4/4
#11 lore raw

Re: [PATCHv3 4/4] blk-lib: check for kill signal

Quoted 9 lines · Keith Busch

From: Keith Busch <[email protected]>

Some of these block operations 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.

Quoted 5 lines

Check for a fatal signal at each iteration so the user doesn't have to wait for their regretted operation to complete naturally.

Looks fine,

#12 lore raw

Re: [PATCHv3 4/4] blk-lib: check for kill signal

In reply to Keith Busch

block/blk-lib.c static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector) · line 53

Quoted 10 lines
block/blk-lib.c +10 -0
+static void abort_bio(struct bio *bio)
+{
+	DECLARE_COMPLETION_ONSTACK_MAP(done,
+			bio->bi_bdev->bd_disk->lockdep_map);
+
+	bio->bi_private = &done;
+	bio->bi_end_io = abort_bio_endio;
+	bio_endio(bio);
+	blk_wait_io(&done);
+}

Without seeing the context below this looks pretty weird, but once seeing that we're waiting for the previously submitted bios it all makes sense. Please write a a good comment explaining this, and maybe also use a name that is a bit more descriptive.

#13 lore raw

Re: [PATCHv3 4/4] blk-lib: check for kill signal

In reply to Keith Busch
Quoted 55 lines · Keith Busch

From: Keith Busch <[email protected]>

Some of these block operations 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.

Check for a fatal signal at each iteration so the user doesn't have to wait for their regretted operation to complete naturally.

---
 block/blk-lib.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
block/blk-lib.c +21 -0
index d4c476cf3784a..9e594f641ce72 100644
@@ -35,6 +35,23 @@ static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector)
 	return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT;
 }
 
+static void abort_bio_endio(struct bio *bio)
+{
+	complete(bio->bi_private);
+	bio_put(bio);
+}
+
+static void abort_bio(struct bio *bio)
+{
+	DECLARE_COMPLETION_ONSTACK_MAP(done,
+			bio->bi_bdev->bd_disk->lockdep_map);
+
+	bio->bi_private = &done;
+	bio->bi_end_io = abort_bio_endio;
+	bio_endio(bio);
+	blk_wait_io(&done);
+}
+
 
@@ -143,6 +164,10 @@ static int __blkdev_issue_write_zeroes(struct block_device *bdev,
 		nr_sects -= len;
 		sector += len;
 		cond_resched();
+		if (fatal_signal_pending(current)) {
+			abort_bio(bio);
+			return -EINTR;
+		}
 	}
 
 	*biop = bio;

block/blk-lib.c static int __blkdev_issue_zero_pages(struct block_device *bdev, · line 220

Quoted 9 lines
block/blk-lib.c +4 -0
@@ -187,6 +212,10 @@ static int __blkdev_issue_zero_pages(struct block_device *bdev,
 				break;
 		}
 		cond_resched();
+		if (fatal_signal_pending(current)) {
+			abort_bio(bio);
+			return -EINTR;
+		}
 	}
 

If a device with large capacity supports write zero offload and user kills that long outstanding write zero operation then it appears we run through the fatal_signal_pending() and abort_bio() twice: once under __blkdev_issue_write_zeroes() and then latter under __blkdev_issue_zero_pages(). The entry to __blkdev_issue_zero_pages() happens if __blkdev_issue_write_zeroes() returns the error code and BLKDEV_ZERO_NOFALLBACK is NOT specified in flags.

I think if fatal signal is intercepted while running __blkdev_issue_write_zeroes() then we shouldn't need to re-enter the __blkdev_issue_zero_pages(). We may want to add following code:

@@ -280,7 +306,7 @@ int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,

                bio_put(bio);
        }
        blk_finish_plug(&plug);
-       if (ret && try_write_zeroes) {
+       if (ret && ret != -EINTR && try_write_zeroes) {
                if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
                        try_write_zeroes = false;
                        goto retry;

Thanks,
--Nilay

#14 lore raw

Re: [PATCHv3 4/4] blk-lib: check for kill signal

Review int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, · line 312

Quoted 12 lines · Nilay Shroff

I think if fatal signal is intercepted while running __blkdev_issue_write_zeroes() then we shouldn't need to re-enter the __blkdev_issue_zero_pages(). We may want to add following code:

(quoted diff) +1 -1
@@ -280,7 +306,7 @@ int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
                bio_put(bio);
        }
        blk_finish_plug(&plug);
-       if (ret && try_write_zeroes) {
+       if (ret && ret != -EINTR && try_write_zeroes) {
                if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
                        try_write_zeroes = false;
                        goto retry;

Good point, I'll fold this in.

Tested-by on the v3 series
#15 lore raw
In reply to Keith Busch
Quoted 14 lines · Keith Busch

From: Keith Busch <[email protected]>

Changes from v2:

Wait for chained bio's to complete before returning from kill signal.

Miscellaneous cleanup patches for style and consistency.

Keith Busch (4):
block: blkdev_issue_secure_erase loop style
block: cleanup __blkdev_issue_write_zeroes
block: io wait hang check helper
blk-lib: check for kill signal

Quoted 6 lines
 block/bio.c     | 12 +----------
 block/blk-lib.c | 57 ++++++++++++++++++++++++++++++++++++++-----------
 block/blk-mq.c  | 19 +++--------------
 block/blk.h     | 13 +++++++++++
 4 files changed, 61 insertions(+), 40 deletions(-)

I tried applying patchset using "git am" but it failed to apply. Was the patchset created against the latest v6.8-rc5? Latter I applied the patchet manually.

I have tested the changes on my NVMe with ~1.5 TB capacity and verified that the patch fixed the reported issue.

NVMe details:

# nvme id-ns /dev/nvme0n1 -H 
NVME Identify Namespace 1:
nsze    : 0x1749a956
ncap    : 0x1749a956
nuse    : 0x1749a956
<snip>
nvmcap  : 1600321314816
<snip>
LBA Format  0 : Metadata Size: 0   bytes - Data Size: 4096 bytes - Relative Performance: 0 Best (in use)
<snip>
# cat /sys/block/nvme0n1/queue/write_zeroes_max_bytes 
8388608
# cat /sys/block/nvme0n1/queue/discard_granularity 
4096
# cat /sys/block/nvme0n1/queue/discard_max_bytes 
2199023255040

I tested following cases:
1) Zero-fill all sectors of NVMe using blkdiscard; While test is running kill the 'blkdiscard' from other terminal and check the return status

   # blkdiscars -z /dev/nvme0n1
   Killed
   # echo $?
   137

2) Discard all sectors of NVMe using blkdiscard, While test is running kill the 'blkdiscard' from other terminal

   # blkdiscard /dev/nvme0n1
   Killed
   #echo $?
   137

The bash return the status code 137 which signifies that 'blkdiscard' is killed.

My NVMe doesn't support secure erase operation so I couldn't test it.

   # blkdiscard -s /dev/nvme0n1 
   blkdiscard: /dev/nvme0n1: BLKSECDISCARD ioctl failed: Operation not supported

Feel free to add:

Thanks,
--Nilay