Skip to content

Commit

Permalink
block: warn if !__GFP_DIRECT_RECLAIM in bio_crypt_set_ctx()
Browse files Browse the repository at this point in the history
bio_crypt_set_ctx() assumes its gfp_mask argument always includes
__GFP_DIRECT_RECLAIM, so that the mempool_alloc() will always succeed.

For now this assumption is still fine, since no callers violate it.
Making bio_crypt_set_ctx() able to fail would add unneeded complexity.

However, if a caller didn't use __GFP_DIRECT_RECLAIM, it would be very
hard to notice the bug.  Make it easier by adding a WARN_ON_ONCE().

Signed-off-by: Eric Biggers <[email protected]>
Reviewed-by: Satya Tangirala <[email protected]>
Cc: Miaohe Lin <[email protected]>
Cc: Satya Tangirala <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
ebiggers authored and axboe committed Oct 5, 2020
1 parent 93f221a commit cf785af
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion block/blk-crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ subsys_initcall(bio_crypt_ctx_init);
void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key,
const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE], gfp_t gfp_mask)
{
struct bio_crypt_ctx *bc = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
struct bio_crypt_ctx *bc;

/*
* The caller must use a gfp_mask that contains __GFP_DIRECT_RECLAIM so
* that the mempool_alloc() can't fail.
*/
WARN_ON_ONCE(!(gfp_mask & __GFP_DIRECT_RECLAIM));

bc = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);

bc->bc_key = key;
memcpy(bc->bc_dun, dun, sizeof(bc->bc_dun));
Expand Down

0 comments on commit cf785af

Please sign in to comment.