2021-06-04 20:02:36

by Satya Tangirala

[permalink] [raw]
Subject: [PATCH v3 02/10] block: blk-crypto: introduce blk_crypto_bio_sectors_alignment()

The size of any bio must be aligned to the data unit size of the bio crypt
context (if it exists) of that bio. This must also be ensured whenever a
bio is split. Introduce blk_crypto_bio_sectors_alignment() that returns the
required alignment in sectors. The number of sectors passed to any call of
bio_split() must be aligned to blk_crypto_bio_sectors_alignment().

Signed-off-by: Satya Tangirala <[email protected]>
---
block/blk-crypto-internal.h | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/block/blk-crypto-internal.h b/block/blk-crypto-internal.h
index 0d36aae538d7..7f1535cc7e7c 100644
--- a/block/blk-crypto-internal.h
+++ b/block/blk-crypto-internal.h
@@ -60,6 +60,21 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
return rq->crypt_ctx;
}

+/*
+ * Returns the alignment requirement for the number of sectors in this bio based
+ * on its bi_crypt_context. Any bios split from this bio must follow this
+ * alignment requirement as well. Note that a bio must contain a whole number of
+ * crypto data units (which is selected by the submitter of bio), since
+ * encryption/decryption can only be performed on a complete crypto data unit.
+ */
+static inline unsigned int blk_crypto_bio_sectors_alignment(struct bio *bio)
+{
+ if (!bio_has_crypt_ctx(bio))
+ return 1;
+ return bio->bi_crypt_context->bc_key->crypto_cfg.data_unit_size >>
+ SECTOR_SHIFT;
+}
+
#else /* CONFIG_BLK_INLINE_ENCRYPTION */

static inline bool bio_crypt_rq_ctx_compatible(struct request *rq,
@@ -93,6 +108,11 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
return false;
}

+static inline unsigned int blk_crypto_bio_sectors_alignment(struct bio *bio)
+{
+ return 1;
+}
+
#endif /* CONFIG_BLK_INLINE_ENCRYPTION */

void __bio_crypt_advance(struct bio *bio, unsigned int bytes);
--
2.32.0.rc1.229.g3e70b5a671-goog


2021-06-17 02:42:14

by Eric Biggers

[permalink] [raw]
Subject: Re: [PATCH v3 02/10] block: blk-crypto: introduce blk_crypto_bio_sectors_alignment()

On Fri, Jun 04, 2021 at 07:58:52PM +0000, Satya Tangirala wrote:
> The size of any bio must be aligned to the data unit size of the bio crypt
> context (if it exists) of that bio. This must also be ensured whenever a
> bio is split. Introduce blk_crypto_bio_sectors_alignment() that returns the
> required alignment in sectors. The number of sectors passed to any call of
> bio_split() must be aligned to blk_crypto_bio_sectors_alignment().
>
> Signed-off-by: Satya Tangirala <[email protected]>
> ---
> block/blk-crypto-internal.h | 20 ++++++++++++++++++++
> 1 file changed, 20 insertions(+)
>
> diff --git a/block/blk-crypto-internal.h b/block/blk-crypto-internal.h
> index 0d36aae538d7..7f1535cc7e7c 100644
> --- a/block/blk-crypto-internal.h
> +++ b/block/blk-crypto-internal.h
> @@ -60,6 +60,21 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
> return rq->crypt_ctx;
> }
>
> +/*
> + * Returns the alignment requirement for the number of sectors in this bio based
> + * on its bi_crypt_context. Any bios split from this bio must follow this
> + * alignment requirement as well. Note that a bio must contain a whole number of
> + * crypto data units (which is selected by the submitter of bio), since
> + * encryption/decryption can only be performed on a complete crypto data unit.
> + */
> +static inline unsigned int blk_crypto_bio_sectors_alignment(struct bio *bio)
> +{
> + if (!bio_has_crypt_ctx(bio))
> + return 1;
> + return bio->bi_crypt_context->bc_key->crypto_cfg.data_unit_size >>
> + SECTOR_SHIFT;
> +}
> +
> #else /* CONFIG_BLK_INLINE_ENCRYPTION */
>
> static inline bool bio_crypt_rq_ctx_compatible(struct request *rq,
> @@ -93,6 +108,11 @@ static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
> return false;
> }
>
> +static inline unsigned int blk_crypto_bio_sectors_alignment(struct bio *bio)
> +{
> + return 1;
> +}
> +
> #endif /* CONFIG_BLK_INLINE_ENCRYPTION */

Looks fine, but I think we should rework the comment to be a bit easier to
understand. Maybe:

/*
* Return the number of sectors to which the size of this bio (and any bios
* split from it) must be aligned based on its encryption context.
*/
static inline unsigned int blk_crypto_bio_sectors_alignment(struct bio *bio)
{
if (!bio_has_crypt_ctx(bio))
return 1;
/*
* If the bio has an encryption context, its size must be aligned to its
* crypto data unit size (which the submitter of the bio selected), as
* encryption/decryption can only be done on complete crypto data units.
*/
return bio->bi_crypt_context->bc_key->crypto_cfg.data_unit_size >>
SECTOR_SHIFT;
}