make block layer auto-PI deadlock safe
Subsystems (3)
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.
Diffstat:
block/bio-integrity-auto.c | 26 ++--------------------- block/bio-integrity.c | 47 ++++++++++++++++++++++++++++++++++++++++++ block/bio.c | 13 +---------- block/blk-settings.c | 11 +++++++++ include/linux/bio-integrity.h | 6 +++++ include/linux/blk-integrity.h | 5 ++++ include/linux/slab.h | 10 ++++++++ 7 files changed, 84 insertions(+), 34 deletions(-)
PATCH 1/3 slab, block: generalize bvec_alloc_gfp
bvec_alloc_gfp is useful for any place that tries to kmalloc first and then fall back to a mempool. Rename it and move it to blk.h to prepare for using it to allocate the default integrity buffer.
--- block/bio.c | 13 ++----------- include/linux/slab.h | 10 ++++++++++ 2 files changed, 12 insertions(+), 11 deletions(-)
block/bio.c +2 -11
@@ -169,16 +169,6 @@ void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs)kmem_cache_free (biovec_slab (nr_vecs)->slab, bv); } -/* -* Make the first allocation restricted and don't dump info on allocation -* failures, since we'll fall back to the mempool in case of failure. -*/ -static inline gfp_t bvec_alloc_gfp (gfp_t gfp) -{ -return (gfp & ~(__GFP_DIRECT_RECLAIM | __GFP_IO)) | - __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; -} -struct bio_vec *bvec_alloc (mempool_t *pool,unsigned short *nr_vecs,gfp_t gfp_mask) {
@@ -201,7 +191,8 @@ struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,if (*nr_vecs <BIO_MAX_VECS ) {struct bio_vec *bvl; - bvl =kmem_cache_alloc (bvs->slab,bvec_alloc_gfp (gfp_mask)); + bvl =kmem_cache_alloc (bvs->slab, +try_alloc_gfp (gfp_mask & ~__GFP_IO));if (likely (bvl) || !(gfp_mask & __GFP_DIRECT_RECLAIM))return bvl; *nr_vecs =BIO_MAX_VECS ;
include/linux/slab.h +10 -0
@@ -1113,6 +1113,16 @@ void kfree_rcu_scheduler_running(void);*/ size_t kmalloc_size_roundup (size_t size); +/* +* Make the first allocation restricted and don't dump info on allocation +* failures, for callers that will fall back to a mempool in case of failure. +*/ +static inline gfp_t try_alloc_gfp (gfp_t gfp) +{ +return (gfp & ~__GFP_DIRECT_RECLAIM) | + __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; +} +void __init kmem_cache_init_late (void );void __init kvfree_rcu_init (void );
Signature · 1 line
2.47.3
Re: [PATCH 1/3] slab, block: generalize bvec_alloc_gfp
Christoph,
Quoted 3 lines
bvec_alloc_gfpis useful for any place that tries to kmalloc first and then fall back to a mempool. Rename it and move it toblk.hto prepare for using it to allocate the default integrity buffer.
Signature · 1 line
Martin K. Petersen
Re: [PATCH 1/3] slab, block: generalize bvec_alloc_gfp
In reply to Christoph HellwigQuoted 2 lines · Christoph Hellwig
bvec_alloc_gfpis useful for any place that tries to kmalloc first and then fall back to a mempool. Rename it and move it toblk.hto prepare
I wonder if such fall backs are necessary because IIRC mempools try to allocate from the underlying provider (i.e. kmalloc caches first), and only give out the reserves when that fails. Is it done for less overhead or something?
Quoted 3 lines
for using it to allocate the default integrity buffer.
Signed-off-by Christoph Hellwig <[email protected]>
That says blk.h but you move it to slab.h? Assuming you intended slab.h. However gfp flags are not slab only so it should be rather include/linux/gfp.h - added maintainers of that to Cc.
We do have gfp_nested_mask() there which is quite similar but not exactly. Maybe a canonical macro not for nested, but for opportunistic allocations (if a one size fits all solution can be found) would be useful too, as people indeed reinvent those manually in various places with subtle differences.
Quoted 42 lines
--- block/bio.c | 13 ++----------- include/linux/slab.h | 10 ++++++++++ 2 files changed, 12 insertions(+), 11 deletions(-)block/bio.c +2
-11@@ -169,16 +169,6 @@ void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned short nr_vecs)kmem_cache_free (biovec_slab (nr_vecs)->slab, bv); } -/* -* Make the first allocation restricted and don't dump info on allocation -* failures, since we'll fall back to the mempool in case of failure. -*/ -static inline gfp_t bvec_alloc_gfp (gfp_t gfp) -{ -return (gfp & ~(__GFP_DIRECT_RECLAIM | __GFP_IO)) | - __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; -} -struct bio_vec *bvec_alloc (mempool_t *pool,unsigned short *nr_vecs,gfp_t gfp_mask) {@@ -201,7 +191,8 @@ struct bio_vec *bvec_alloc(mempool_t *pool, unsigned short *nr_vecs,if (*nr_vecs <BIO_MAX_VECS ) {struct bio_vec *bvl; - bvl =kmem_cache_alloc (bvs->slab,bvec_alloc_gfp (gfp_mask)); + bvl =kmem_cache_alloc (bvs->slab, +try_alloc_gfp (gfp_mask & ~__GFP_IO));if (likely (bvl) || !(gfp_mask & __GFP_DIRECT_RECLAIM))return bvl; *nr_vecs =BIO_MAX_VECS ;include/linux/slab.h +0
-0@@ -1113,6 +1113,16 @@ void kfree_rcu_scheduler_running(void);*/
include/linux/slab.h void kfree_rcu_scheduler_running(void); · line 1123
Quoted 10 lines
include/linux/slab.h +8
-0size_t kmalloc_size_roundup (size_t size); +/* +* Make the first allocation restricted and don't dump info on allocation +* failures, for callers that will fall back to a mempool in case of failure. +*/ +static inline gfp_t try_alloc_gfp (gfp_t gfp) +{ +return (gfp & ~__GFP_DIRECT_RECLAIM) | + __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
Note without __GFP_DIRECT_RECLAIM the __GFP_NORETRY is most likely redundant (but doesn't hurt).
Quoted 5 lines
include/linux/slab.h +2
-0+} +void __init kmem_cache_init_late (void );void __init kvfree_rcu_init (void );
Re: [PATCH 1/3] slab, block: generalize bvec_alloc_gfp
Quoted 8 lines · Vlastimil Babka
Quoted 2 lines · Christoph Hellwig
bvec_alloc_gfpis useful for any place that tries to kmalloc first and then fall back to a mempool. Rename it and move it toblk.hto prepareI wonder if such fall backs are necessary because IIRC mempools try to allocate from the underlying provider (i.e. kmalloc caches first), and only give out the reserves when that fails. Is it done for less overhead or something?
That's the mempool behavior, yes. But the bvec allocator only has a mempool for the largest possible allocation, while usually trying smaller allocations instead.
Quoted 6 lines
Quoted 4 lines
for using it to allocate the default integrity buffer.
Signed-off-by Christoph Hellwig <[email protected]>That says
blk.hbut you move it toslab.h? Assuming you intendedslab.h.
Yes. I initially had it in blk.h, but it felt more general.
Quoted 2 lines
However gfp flags are not slab only so it should be rather
include/linux/gfp.h- added maintainers of that to Cc.
Ok.
Quoted 4 lines
We do have
gfp_nested_mask()there which is quite similar but not exactly. Maybe a canonical macro not for nested, but for opportunistic allocations (if a one size fits all solution can be found) would be useful too, as people indeed reinvent those manually in various places with subtle differences.
That's exactly what I've been trying to avoid indeed.
Re: [PATCH 1/3] slab, block: generalize bvec_alloc_gfp
In reply to Christoph Hellwiginclude/linux/slab.h void kfree_rcu_scheduler_running(void); · line 1124
Quoted 9 lines · Christoph Hellwig
include/linux/slab.h +9
-0+/* +* Make the first allocation restricted and don't dump info on allocation +* failures, for callers that will fall back to a mempool in case of failure. +*/ +static inline gfp_t try_alloc_gfp (gfp_t gfp) +{ +return (gfp & ~__GFP_DIRECT_RECLAIM) | + __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; +}
Comparing to the kvmalloc code:
flags |= __GFP_NOWARN;
if (!(flags & __GFP_RETRY_MAYFAIL))
flags &= ~__GFP_DIRECT_RECLAIM;
/* nofail semantic is implemented by the vmalloc fallback */
flags &= ~__GFP_NOFAIL;it's quite different. I am by no stretch of the imagination a GFP flags expert, but it seems to me that we should make the two the same since they're both "try to allocate and we have a fallback if necessary". I suspect kvmalloc() is called with a wider range of GFP flags than bvec allocation is, so it's probably better tested.
Is there a reason _not_ to use the kvmalloc code for bvec allocations?
Re: [PATCH 1/3] slab, block: generalize bvec_alloc_gfp
Quoted 7 lines · Matthew Wilcox
it's quite different. I am by no stretch of the imagination a GFP flags expert, but it seems to me that we should make the two the same since they're both "try to allocate and we have a fallback if necessary". I suspect
kvmalloc()is called with a wider range of GFP flags than bvec allocation is, so it's probably better tested.Is there a reason _not_ to use the kvmalloc code for bvec allocations?
It's using a dedicated slab cache, which makes sense for such a frequent and usually short-lived allocation. We also don't use vmalloc backing ever at the moment.
Re: [PATCH 1/3] slab, block: generalize bvec_alloc_gfp
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Matthew Wilcox
it's quite different. I am by no stretch of the imagination a GFP flags expert, but it seems to me that we should make the two the same since they're both "try to allocate and we have a fallback if necessary". I suspect
kvmalloc()is called with a wider range of GFP flags than bvec allocation is, so it's probably better tested.
Quoted 5 lines
Quoted 1 line
Is there a reason _not_ to use the kvmalloc code for bvec allocations?
It's using a dedicated slab cache, which makes sense for such a frequent and usually short-lived allocation. We also don't use vmalloc backing ever at the moment.
That's not what I meant.
What I was proposing was:
+static inline gfp_t try_alloc_gfp(gfp_t gfp)
+{
+ gfp |= __GFP_NOWARN;
+ if (!(gfp & __GFP_RETRY_MAYFAIL))
+ gfp &= ~__GFP_DIRECT_RECLAIM;
+ gfp &= ~__GFP_NOFAIL;
+
+ return gfp;
+}
Re: [PATCH 1/3] slab, block: generalize bvec_alloc_gfp
Quoted 7 lines · Matthew Wilcox
Quoted 5 lines
Quoted 1 line
Is there a reason _not_ to use the kvmalloc code for bvec allocations?
It's using a dedicated slab cache, which makes sense for such a frequent and usually short-lived allocation. We also don't use vmalloc backing ever at the moment.
That's not what I meant.
Oh, not the entirely kvmalloc code, but the helper. Ok.
Review
Quoted 8 lines
What I was proposing was:
(quoted diff) +6
-0+static inline gfp_t try_alloc_gfp(gfp_t gfp) +{ + gfp |= __GFP_NOWARN; + if (!(gfp & __GFP_RETRY_MAYFAIL)) + gfp &= ~__GFP_DIRECT_RECLAIM; + gfp &= ~__GFP_NOFAIL;
That's missing the __GFP_NOMEMALLOC from the original one, and also __GFP_NORETRY. So it'll be pretty different.
For now I think I'll just either duplicate the logic or keep it in block code to get the deadlock fix in, and then we can spend more time analyzing all the flags and documenting the ones needed in various places.
PATCH 2/3 block: blocking mempool_alloc doesn't fail
In reply to Christoph HellwigSo 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
Re: [PATCH 2/3] block: blocking mempool_alloc doesn't fail
Christoph,
Quoted 1 line
So remove the error check for it in
bio_integrity_prep.
Signature · 1 line
Martin K. Petersen
PATCH 3/3 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 allocated at least a 2MiB integrity buffer, i.e. 128MiB for 8 byte PI and 512 byte integrity internals, 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 | 47 +++++++++++++++++++++++++++++++++++ block/blk-settings.c | 11 ++++++++ include/linux/bio-integrity.h | 6 +++++ include/linux/blk-integrity.h | 5 ++++ 5 files changed, 72 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 +47 -0
56 lines not on this page · raw message
block/blk-settings.c +11 -0
@@ -194,6 +194,17 @@ 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. It allocates a single segment. 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 =min3 (lim->max_sectors, +BLK_INTEGRITY_MAX_SIZE / + bi->pi_tuple_size * lim->logical_block_size, + lim->max_segment_size >>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 3/3] block: make bio auto-integrity deadlock safe
Christoph,
Quoted 1 line
Fix this by limiting the I/O size so that we can allocated at least a
allocate
Quoted 2 lines
2MiB integrity buffer, i.e. 128MiB for 8 byte PI and 512 byte integrity internals, and create a mempool as a last resort for this maximum size,
intervals
Quoted 3 lines
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.
Typos aside, this looks good to me.
Signature · 1 line
Martin K. Petersen
Re: [PATCH 3/3] block: make bio auto-integrity deadlock safe
In reply to Christoph HellwigQuoted 4 lines · Christoph Hellwig
block/blk-settings.c +0
-0@@ -194,6 +194,17 @@ static int blk_validate_integrity_limits(struct queue_limits *lim) (1U << bi->interval_exp) - 1); }
block/blk-settings.c static int blk_validate_integrity_limits(struct queue_limits *lim) · line 206
Quoted 10 lines
block/blk-settings.c +10
-0+/* +* The block layer automatically adds integrity data for bios that don't +* already have it. It allocates a single segment. 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 =min3 (lim->max_sectors, +BLK_INTEGRITY_MAX_SIZE / + bi->pi_tuple_size * lim->logical_block_size, + lim->max_segment_size >>SECTOR_SHIFT );
Two issues:
- When underlying device has pi-type 0, pi_tuple_size will be 0 and this will cause divide-by-zero.
- The second value in above min3() is in bytes, and other two in sectors. So this clamping may not be happening correctly.