Skip to contentSkip to conversation

make block layer auto-PI deadlock safe v3

PATCH In review Christoph Hellwig 6 participants 20 messages Nov 3, 2025 – Nov 5, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

Hi all,

currently the automatic block layer PI generation allocates the integrity buffer using kmalloc, and thus could deadlock, or fail I/O request due to memory pressure.

Fix this by adding a mempool, and capping the maximum I/O size on PI capable devices to not exceed the allocation size of the mempool.

This is against the block-6.18 branch as it has a contextual dependency on the PI fix merged there yesterday.

Chances since v2:
- comment typo fixes

Chances since v1:
- keep the gfp flags manipulation local (at least for now)
- fix the maximum size calculation

Diffstat:

 block/bio-integrity-auto.c    |   26 ++--------------------
 block/bio-integrity.c         |   48 ++++++++++++++++++++++++++++++++++++++++++
 block/blk-settings.c          |   21 ++++++++++++++++++
 include/linux/bio-integrity.h |    6 +++++
 include/linux/blk-integrity.h |    5 ++++
 5 files changed, 83 insertions(+), 23 deletions(-)
#1 lore raw

PATCH 1/2 block: blocking mempool_alloc doesn't fail

So remove the error check for it in bio_integrity_prep.

---
 block/bio-integrity-auto.c | 4 ----
 1 file changed, 4 deletions(-)
block/bio-integrity-auto.c +0 -4
index 687952f63bbb..2f4a244749ac 100644
@@ -158,8 +158,6 @@ bool bio_integrity_prep(struct bio *bio)
 	if (!buf)
 		goto err_end_io;
 	bid = mempool_alloc(&bid_pool, GFP_NOIO);
-	if (!bid)
-		goto err_free_buf;
 	bio_integrity_init(bio, &bid->bip, &bid->bvec, 1);
 
 	bid->bio = bio;
@@ -187,8 +185,6 @@ bool bio_integrity_prep(struct bio *bio)
 		bid->saved_bio_iter = bio->bi_iter;
 	return true;
 
-err_free_buf:
-	kfree(buf);
 err_end_io:
 	bio->bi_status = BLK_STS_RESOURCE;
 	bio_endio(bio);
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH 1/2] block: blocking mempool_alloc doesn't fail

In reply to Christoph Hellwig
Quoted 22 lines · Christoph Hellwig

So remove the error check for it in bio_integrity_prep.

---
  block/bio-integrity-auto.c | 4 ----
  1 file changed, 4 deletions(-)
block/bio-integrity-auto.c +0 -2
index 687952f63bbb..2f4a244749ac 100644
@@ -158,8 +158,6 @@ bool bio_integrity_prep(struct bio *bio)
  	if (!buf)
  		goto err_end_io;
  	bid = mempool_alloc(&bid_pool, GFP_NOIO);
-	if (!bid)
-		goto err_free_buf;
  	bio_integrity_init(bio, &bid->bip, &bid->bvec, 1);
 
  	bid->bio = bio;

block/bio-integrity-auto.c bool bio_integrity_prep(struct bio *bio) · line 191

Quoted 5 lines
block/bio-integrity-auto.c +0 -2
@@ -187,8 +185,6 @@ bool bio_integrity_prep(struct bio *bio)
  		bid->saved_bio_iter = bio->bi_iter;
  	return true;
 
-err_free_buf:
-	kfree(buf);

Is the original error handling code correct?

I would assume that if the call to bio_integrity_add_page() fails (when it returns < len), then we should free buf (which does not happen).

Anyway, I see that this code is being changed in the next patch...

Quoted 3 lines
block/bio-integrity-auto.c +0 -0
  err_end_io:
  	bio->bi_status = BLK_STS_RESOURCE;
  	bio_endio(bio);
#4 lore raw

Re: [PATCH 1/2] block: blocking mempool_alloc doesn't fail

Quoted 1 line · John Garry

Is the original error handling code correct?

Does it matter given that it was never called and is removed now? :)

#7 lore raw

PATCH 2/2 block: make bio auto-integrity deadlock safe

In reply to Christoph Hellwig

The current block layer automatic integrity protection allocates the actual integrity buffer, which has three problems:

- because it happens at the bottom of the I/O stack and doesn't use a mempool it can deadlock under load
- because the data size in a bio is almost unbounded when using lage folios it can relatively easily exceed the maximum kmalloc size
- even when it does not exceed the maximum kmalloc size, it could exceed the maximum segment size of the device

Fix this by limiting the I/O size so that we can allocate at least a 2MiB integrity buffer, i.e. 128MiB for 8 byte PI and 512 byte integrity intervals, and create a mempool as a last resort for this maximum size, mirroring the scheme used for bvecs. As a nice upside none of this can fail now, so we remove the error handling and open code the trivial addition of the bip vec.

The new allocation helpers sit outside of bio-integrity-auto.c because I plan to reuse them for file system based PI in the near future.

Fixes 7ba1ba12eeef ("block: Block layer data integrity support")
---
 block/bio-integrity-auto.c    | 22 +++-------------
 block/bio-integrity.c         | 48 +++++++++++++++++++++++++++++++++++
 block/blk-settings.c          | 21 +++++++++++++++
 include/linux/bio-integrity.h |  6 +++++
 include/linux/blk-integrity.h |  5 ++++
 5 files changed, 83 insertions(+), 19 deletions(-)
block/bio-integrity-auto.c +3 -19
index 2f4a244749ac..9850c338548d 100644
@@ -29,7 +29,7 @@ static void bio_integrity_finish(struct bio_integrity_data *bid)
 {
 	bid->bio->bi_integrity = NULL;
 	bid->bio->bi_opf &= ~REQ_INTEGRITY;
-	kfree(bvec_virt(bid->bip.bip_vec));
+	bio_integrity_free_buf(&bid->bip);
 	mempool_free(bid, &bid_pool);
 }
 
@@ -110,8 +110,6 @@ bool bio_integrity_prep(struct bio *bio)
 	struct bio_integrity_data *bid;
 	bool set_flags = true;
 	gfp_t gfp = GFP_NOIO;
-	unsigned int len;
-	void *buf;
 
 	if (!bi)
 		return true;
@@ -152,17 +150,12 @@ bool bio_integrity_prep(struct bio *bio)
 	if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
 		return true;
 
-	/* Allocate kernel buffer for protection data */
-	len = bio_integrity_bytes(bi, bio_sectors(bio));
-	buf = kmalloc(len, gfp);
-	if (!buf)
-		goto err_end_io;
 	bid = mempool_alloc(&bid_pool, GFP_NOIO);
 	bio_integrity_init(bio, &bid->bip, &bid->bvec, 1);
-
 	bid->bio = bio;
-
 	bid->bip.bip_flags |= BIP_BLOCK_INTEGRITY;
+	bio_integrity_alloc_buf(bio, gfp & __GFP_ZERO);
+
 	bip_set_seed(&bid->bip, bio->bi_iter.bi_sector);
 
 	if (set_flags) {
@@ -174,21 +167,12 @@ bool bio_integrity_prep(struct bio *bio)
 			bid->bip.bip_flags |= BIP_CHECK_REFTAG;
 	}
 
-	if (bio_integrity_add_page(bio, virt_to_page(buf), len,
-			offset_in_page(buf)) < len)
-		goto err_end_io;
-
 	/* Auto-generate integrity metadata if this is a write */
 	if (bio_data_dir(bio) == WRITE && bip_should_check(&bid->bip))
 		blk_integrity_generate(bio);
 	else
 		bid->saved_bio_iter = bio->bi_iter;
 	return true;
-
-err_end_io:
-	bio->bi_status = BLK_STS_RESOURCE;
-	bio_endio(bio);
-	return false;
 }
 EXPORT_SYMBOL(bio_integrity_prep);
 
block/bio-integrity.c +48 -0

57 lines not on this page · raw message

block/blk-settings.c +21 -0
index d74b13ec8e54..547b679c5dc1 100644
@@ -123,6 +123,19 @@ static int blk_validate_zoned_limits(struct queue_limits *lim)
 	return 0;
 }
 
+/*
+ * Maximum size of I/O that needs a block layer integrity buffer.  Limited
+ * by the number of intervals for which we can fit the integrity buffer into
+ * the buffer size.  Because the buffer is a single segment it is also limited
+ * by the maximum segment size.
+ */
+static inline unsigned int max_integrity_io_size(struct queue_limits *lim)
+{
+	return min_t(unsigned int, lim->max_segment_size,
+		(BLK_INTEGRITY_MAX_SIZE / lim->integrity.metadata_size) <<
+			lim->integrity.interval_exp);
+}
+
 static int blk_validate_integrity_limits(struct queue_limits *lim)
 {
 	struct blk_integrity *bi = &lim->integrity;
@@ -194,6 +207,14 @@ static int blk_validate_integrity_limits(struct queue_limits *lim)
 					(1U << bi->interval_exp) - 1);
 	}
 
+	/*
+	 * The block layer automatically adds integrity data for bios that don't
+	 * already have it.  Limit the I/O size so that a single maximum size
+	 * metadata segment can cover the integrity data for the entire I/O.
+	 */
+	lim->max_sectors = min(lim->max_sectors,
+		max_integrity_io_size(lim) >> SECTOR_SHIFT);
+
 	return 0;
 }
 
include/linux/bio-integrity.h +6 -0
index 851254f36eb3..3d05296a5afe 100644
@@ -14,6 +14,8 @@ enum bip_flags {
 	BIP_CHECK_REFTAG	= 1 << 6, /* reftag check */
 	BIP_CHECK_APPTAG	= 1 << 7, /* apptag check */
 	BIP_P2P_DMA		= 1 << 8, /* using P2P address */
+
+	BIP_MEMPOOL		= 1 << 15, /* buffer backed by mempool */
 };
 
 struct bio_integrity_payload {
@@ -140,4 +142,8 @@ static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
 	return 0;
 }
 #endif /* CONFIG_BLK_DEV_INTEGRITY */
+
+void bio_integrity_alloc_buf(struct bio *bio, bool zero_buffer);
+void bio_integrity_free_buf(struct bio_integrity_payload *bip);
+
 #endif /* _LINUX_BIO_INTEGRITY_H */
include/linux/blk-integrity.h +5 -0
index b659373788f6..c2030fd8ba0a 100644
@@ -8,6 +8,11 @@
 
 struct request;
 
+/*
+ * Maximum contiguous integrity buffer allocation.
+ */
+#define BLK_INTEGRITY_MAX_SIZE		SZ_2M
+
 enum blk_integrity_flags {
 	BLK_INTEGRITY_NOVERIFY		= 1 << 0,
 	BLK_INTEGRITY_NOGENERATE	= 1 << 1,
Signature · 1 line
2.47.3
Reviewed-by on v1 2/2
#10 lore raw

Re: [PATCH 2/2] block: make bio auto-integrity deadlock safe

In reply to Christoph Hellwig

block/bio-integrity.c struct bio_integrity_alloc { · line 28

Quoted 10 lines · Christoph Hellwig
block/bio-integrity.c +10 -0
+void bio_integrity_alloc_buf(struct bio *bio, bool zero_buffer)
+{
+	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
+	struct bio_integrity_payload *bip = bio_integrity(bio);
+	unsigned int len = bio_integrity_bytes(bi, bio_sectors(bio));
+	gfp_t gfp = GFP_NOIO | (zero_buffer ? __GFP_ZERO : 0);
+	void *buf;
+
+	buf = kmalloc(len, (gfp & ~__GFP_DIRECT_RECLAIM) |
+			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN);

Why can't we clear the flags when assigning gfp, or at least outside of kmalloc()s parameter list?

Quoted 5 lines
block/bio-integrity.c +5 -0
+	if (unlikely(!buf)) {
+		struct page *page;
+
+		page = mempool_alloc(&integrity_buf_pool, GFP_NOFS);
+		if (zero_buffer)

block/bio-integrity.c struct bio_integrity_alloc { · line 43

Quoted 10 lines
block/bio-integrity.c +10 -0
+			memset(page_address(page), 0, len);
+		bvec_set_page(&bip->bip_vec[0], page, len, 0);
+		bip->bip_flags |= BIP_MEMPOOL;
+	} else {
+		bvec_set_page(&bip->bip_vec[0], virt_to_page(buf), len,
+				offset_in_page(buf));
+	}
+
+	bip->bip_vcnt = 1;
+	bip->bip_iter.bi_size = len;

gfp is unused for the rest of the function.

include/linux/bio-integrity.h enum bip_flags { · line 20

Quoted 12 lines
include/linux/bio-integrity.h +2 -0
index 851254f36eb3..3d05296a5afe 100644
@@ -14,6 +14,8 @@ enum bip_flags {
  	BIP_CHECK_REFTAG	= 1 << 6, /* reftag check */
  	BIP_CHECK_APPTAG	= 1 << 7, /* apptag check */
  	BIP_P2P_DMA		= 1 << 8, /* using P2P address */
+
+	BIP_MEMPOOL		= 1 << 15, /* buffer backed by mempool */
  };
 

Any specific reason for the hole?

Otherwise,

author
#11 lore raw

Re: [PATCH 2/2] block: make bio auto-integrity deadlock safe

Quoted 15 lines · Johannes Thumshirn

block/bio-integrity.c struct bio_integrity_alloc { · line 28

Quoted 10 lines · Christoph Hellwig
block/bio-integrity.c +10 -0
+void bio_integrity_alloc_buf(struct bio *bio, bool zero_buffer)
+{
+	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
+	struct bio_integrity_payload *bip = bio_integrity(bio);
+	unsigned int len = bio_integrity_bytes(bi, bio_sectors(bio));
+	gfp_t gfp = GFP_NOIO | (zero_buffer ? __GFP_ZERO : 0);
+	void *buf;
+
+	buf = kmalloc(len, (gfp & ~__GFP_DIRECT_RECLAIM) |
+			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN);

Why can't we clear the flags when assigning gfp, or at least outside of kmalloc()s parameter list?

We could, but what's the point?

Quoted 11 lines

include/linux/bio-integrity.h enum bip_flags { · line 20

Quoted 9 lines

+++ b/include/linux/bio-integrity.h

include/linux/bio-integrity.h +2 -0
@@ -14,6 +14,8 @@ enum bip_flags {
  	BIP_CHECK_REFTAG	= 1 << 6, /* reftag check */
  	BIP_CHECK_APPTAG	= 1 << 7, /* apptag check */
  	BIP_P2P_DMA		= 1 << 8, /* using P2P address */
+
+	BIP_MEMPOOL		= 1 << 15, /* buffer backed by mempool */
  };
 

Any specific reason for the hole?

Because it's really just an internal flag. (So is BIP_P2P_DMA, but that should go away this merge window).

#12 lore raw

Re: [PATCH 2/2] block: make bio auto-integrity deadlock safe

Quoted 15 lines · hch
Quoted 14 lines · Johannes Thumshirn

block/bio-integrity.c struct bio_integrity_alloc { · line 28

Quoted 10 lines · Christoph Hellwig
block/bio-integrity.c +10 -0
+void bio_integrity_alloc_buf(struct bio *bio, bool zero_buffer)
+{
+	struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
+	struct bio_integrity_payload *bip = bio_integrity(bio);
+	unsigned int len = bio_integrity_bytes(bi, bio_sectors(bio));
+	gfp_t gfp = GFP_NOIO | (zero_buffer ? __GFP_ZERO : 0);
+	void *buf;
+
+	buf = kmalloc(len, (gfp & ~__GFP_DIRECT_RECLAIM) |
+			__GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN);

Why can't we clear the flags when assigning gfp, or at least outside of kmalloc()s parameter list?

Quoted 1 line

We could, but what's the point?

From a reader pov it's kind of annoying to it touched twice like this,
but I guess that's just my personal style preference here.

Quoted 14 lines
Quoted 10 lines

include/linux/bio-integrity.h enum bip_flags { · line 20

Quoted 9 lines

+++ b/include/linux/bio-integrity.h

include/linux/bio-integrity.h +2 -0
@@ -14,6 +14,8 @@ enum bip_flags {
   	BIP_CHECK_REFTAG	= 1 << 6, /* reftag check */
   	BIP_CHECK_APPTAG	= 1 << 7, /* apptag check */
   	BIP_P2P_DMA		= 1 << 8, /* using P2P address */
+
+	BIP_MEMPOOL		= 1 << 15, /* buffer backed by mempool */
   };
 

Any specific reason for the hole?

Because it's really just an internal flag. (So is BIP_P2P_DMA, but that should go away this merge window).

author
#13 lore raw

Re: [PATCH 2/2] block: make bio auto-integrity deadlock safe

Quoted 6 lines · Johannes Thumshirn
Quoted 3 lines
Quoted 2 lines

Why can't we clear the flags when assigning gfp, or at least outside of kmalloc()s parameter list?

We could, but what's the point?

From a reader pov it's kind of annoying to it touched twice like this,
but I guess that's just my personal style preference here.

Note that the manipulations should eventually disappear in a common mm-level helper as in v1. I'm just deferring this to get the fix in without a cross-subsystem dependency that causes discussion on the detailed approach.

maintainer
#14 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig

Hi all,

currently the automatic block layer PI generation allocates the integrity buffer using kmalloc, and thus could deadlock, or fail I/O request due to memory pressure.

Fix this by adding a mempool, and capping the maximum I/O size on PI capable devices to not exceed the allocation size of the mempool.

This is against the block-6.18 branch as it has a contextual dependency on the PI fix merged there yesterday.

Yesterday? Guessing this got copy/pasted from the v1 posting, as it must be this one:

commit 4c8cf6bd28d6fea23819f082ddc8063fd6fa963a (tag: block-6.18-20251023)

Author: Christoph Hellwig <[email protected]>
Date:   Wed Oct 22 10:33:31 2025 +0200

block: require LBA dma_alignment when using PI

which is a bit longer ago.

But why? Surely this is 6.19 material?

Signature · 1 line
Jens Axboe
#15 lore raw
Quoted 8 lines · Jens Axboe
Quoted 5 lines

Fix this by adding a mempool, and capping the maximum I/O size on PI capable devices to not exceed the allocation size of the mempool.

This is against the block-6.18 branch as it has a contextual dependency on the PI fix merged there yesterday.

Yesterday? Guessing this got copy/pasted from the v1 posting, as it must be this one:

Yesterday as of the first post of the series, I should have updated this, sorry.

Quoted 10 lines

commit 4c8cf6bd28d6fea23819f082ddc8063fd6fa963a (tag: block-6.18-20251023)

Author: Christoph Hellwig <[email protected]>
Date:   Wed Oct 22 10:33:31 2025 +0200

block: require LBA dma_alignment when using PI

which is a bit longer ago.

But why? Surely this is 6.19 material?

This is 6.19 material. That's just a heads up that you need a merge (or rebase which I was hoping for as of the first round, but it's probably too late now)

maintainer
#16 lore raw
Quoted 6 lines · Christoph Hellwig
Quoted 6 lines

commit 4c8cf6bd28d6fea23819f082ddc8063fd6fa963a (tag: block-6.18-20251023)

Author: Christoph Hellwig <[email protected]>
Date:   Wed Oct 22 10:33:31 2025 +0200

block: require LBA dma_alignment when using PI

Quoted 7 lines
Quoted 3 lines

which is a bit longer ago.

But why? Surely this is 6.19 material?

This is 6.19 material. That's just a heads up that you need a merge (or rebase which I was hoping for as of the first round, but it's probably too late now)

But why not generate it against the 6.19 tree then? As-is, I have to first resolve the conflict in that tree, then resolve it again when merging to for-next. Not a huge issue, just don't quite follow the logic behind generating it against block-6.18.

Signature · 1 line
Jens Axboe
#17 lore raw
Quoted 8 lines · Jens Axboe
Quoted 3 lines

This is 6.19 material. That's just a heads up that you need a merge (or rebase which I was hoping for as of the first round, but it's probably too late now)

But why not generate it against the 6.19 tree then? As-is, I have to first resolve the conflict in that tree, then resolve it again when merging to for-next. Not a huge issue, just don't quite follow the logic behind generating it against block-6.18.

Mostly because I though you'd rebase when I did the first version and then didn't really think about the base more after that.

Btw, we'll need the merge anyway for Keith's unaligned PI patch if we end up adding that for 6.19.

maintainer
#18 lore raw
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Jens Axboe
Quoted 3 lines

This is 6.19 material. That's just a heads up that you need a merge (or rebase which I was hoping for as of the first round, but it's probably too late now)

Quoted 7 lines
Quoted 4 lines

But why not generate it against the 6.19 tree then? As-is, I have to first resolve the conflict in that tree, then resolve it again when merging to for-next. Not a huge issue, just don't quite follow the logic behind generating it against block-6.18.

Mostly because I though you'd rebase when I did the first version and then didn't really think about the base more after that.

Gotcha, yeah would've perhaps made sense back then, but way too late at this point.

Quoted 2 lines

Btw, we'll need the merge anyway for Keith's unaligned PI patch if we end up adding that for 6.19.

Yep did notice that. But since we already have a conflict, not big deal.

Signature · 1 line
Jens Axboe
maintainer Applied
#19 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig

currently the automatic block layer PI generation allocates the integrity buffer using kmalloc, and thus could deadlock, or fail I/O request due to memory pressure.

Fix this by adding a mempool, and capping the maximum I/O size on PI capable devices to not exceed the allocation size of the mempool.

[...]

Applied, thanks!

[1/2] block: blocking mempool_alloc doesn't fail

      commit: eef09f742be2a89126742f9f6f6a0d5d7c83cba8
[2/2] block: make bio auto-integrity deadlock safe
      commit: ec7f31b2a2d3bf6b9e4d4b8cd156587f1d0607d5

Best regards,

Signature · 1 line
Jens Axboe