make block layer auto-PI deadlock safe v2
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 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(-)
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
@@ -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
PATCH 2/2 block: make bio auto-integrity deadlock safe
In reply to Christoph HellwigThe 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.
--- 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
@@ -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
@@ -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 whuich we can fit the integrity buffer into +* the buffer size. Because the the buffer is a single segment it is also +* limited by the maximum segments 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
@@ -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
@@ -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
Re: [PATCH 2/2] block: make bio auto-integrity deadlock safe
Christoph,
Quoted 2 lines
+ * Maximum size of I/O that needs a block layer integrity buffer. Limited
+ * by the number of intervals for whuich we can fit the integrity buffer into
which
Quoted 2 lines
+ * the buffer size. Because the the buffer is a single segment it is also
+ * limited by the maximum segments size.
segment
block/blk-settings.c static int blk_validate_zoned_limits(struct queue_limits *lim) · line 137
Quoted 7 lines
+ */
block/blk-settings.c +6
-0+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); +}
Otherwise OK.
Signature · 1 line
Martin K. Petersen