Skip to contentSkip to conversation

Introduce the bulk mode method when sending request to crypto layer

RFC Superseded Baolin Wang 4 participants 12 messages May 25, 2016 – May 27, 2016

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Introduce the bulk mode method when sending request to crypto layer

This patchset will check if the cipher can support bulk mode, then dm-crypt will handle different ways to send requests to crypto layer according to cipher mode.

Looking forward to any comments and suggestions. Thanks.

Baolin Wang (3):
block: Introduce blk_bio_map_sg() to map one bio
crypto: Introduce CRYPTO_ALG_BULK flag
md: dm-crypt: Introduce the bulk mode method when sending request

 block/blk-merge.c         |   45 +++++++++++
 drivers/md/dm-crypt.c     |  188 ++++++++++++++++++++++++++++++++++++++++++---
 include/crypto/skcipher.h |    7 ++
 include/linux/blkdev.h    |    3 +
 include/linux/crypto.h    |    6 ++
 5 files changed, 237 insertions(+), 12 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

RFC 1/3 block: Introduce blk_bio_map_sg() to map one bio

In dm-crypt, it need to map one bio to scatterlist for improving the hardware engine encryption efficiency. Thus this patch introduces the blk_bio_map_sg() function to map one bio with scatterlists.

---
 block/blk-merge.c      |   45 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/blkdev.h |    3 +++
 2 files changed, 48 insertions(+)
block/blk-merge.c +45 -0
index 2613531..9b92af4 100644
@@ -417,6 +417,51 @@ single_segment:
 }
 
 /*
+ * map a bio to scatterlist, return number of sg entries setup.
+ */
+int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
+		   struct scatterlist *sglist,
+		   struct scatterlist **sg)
+{
+	struct bio_vec bvec, bvprv = { NULL };
+	struct bvec_iter iter;
+	int nsegs, cluster;
+
+	nsegs = 0;
+	cluster = blk_queue_cluster(q);
+
+	if (bio->bi_rw & REQ_DISCARD) {
+		/*
+		 * This is a hack - drivers should be neither modifying the
+		 * biovec, nor relying on bi_vcnt - but because of
+		 * blk_add_request_payload(), a discard bio may or may not have
+		 * a payload we need to set up here (thank you Christoph) and
+		 * bi_vcnt is really the only way of telling if we need to.
+		 */
+
+		if (bio->bi_vcnt)
+			goto single_segment;
+
+		return 0;
+	}
+
+	if (bio->bi_rw & REQ_WRITE_SAME) {
+single_segment:
+		*sg = sglist;
+		bvec = bio_iovec(bio);
+		sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
+		return 1;
+	}
+
+	bio_for_each_segment(bvec, bio, iter)
+		__blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
+				     &nsegs, &cluster);
+
+	return nsegs;
+}
+EXPORT_SYMBOL(blk_bio_map_sg);
+
+/*
  * map a request to scatterlist, return number of sg entries setup. Caller
  * must make sure sg can hold rq->nr_phys_segments entries
  */
include/linux/blkdev.h +3 -0
index 1fd8fdf..e5de4f8 100644
@@ -1013,6 +1013,9 @@ extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fu
 extern struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev);
 
 extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
+extern int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
+			  struct scatterlist *sglist,
+			  struct scatterlist **sg);
 extern void blk_dump_rq_flags(struct request *, char *);
 extern long nr_blockdev_pages(void);
 
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [RFC 1/3] block: Introduce blk_bio_map_sg() to map one bio

Quoted 51 lines · Baolin Wang

In dm-crypt, it need to map one bio to scatterlist for improving the hardware engine encryption efficiency. Thus this patch introduces the blk_bio_map_sg() function to map one bio with scatterlists.

---
 block/blk-merge.c      |   45 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/blkdev.h |    3 +++
 2 files changed, 48 insertions(+)
block/blk-merge.c +33 -0
index 2613531..9b92af4 100644
@@ -417,6 +417,51 @@ single_segment:
 }
 
 /*
+ * map a bio to scatterlist, return number of sg entries setup.
+ */
+int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
+                  struct scatterlist *sglist,
+                  struct scatterlist **sg)
+{
+       struct bio_vec bvec, bvprv = { NULL };
+       struct bvec_iter iter;
+       int nsegs, cluster;
+
+       nsegs = 0;
+       cluster = blk_queue_cluster(q);
+
+       if (bio->bi_rw & REQ_DISCARD) {
+               /*
+                * This is a hack - drivers should be neither modifying the
+                * biovec, nor relying on bi_vcnt - but because of
+                * blk_add_request_payload(), a discard bio may or may not have
+                * a payload we need to set up here (thank you Christoph) and
+                * bi_vcnt is really the only way of telling if we need to.
+                */
+
+               if (bio->bi_vcnt)
+                       goto single_segment;
+
+               return 0;
+       }
+
+       if (bio->bi_rw & REQ_WRITE_SAME) {
+single_segment:
+               *sg = sglist;
+               bvec = bio_iovec(bio);
+               sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);

block/blk-merge.c single_segment: · line 462

Quoted 10 lines
block/blk-merge.c +10 -0
+               return 1;
+       }
+
+       bio_for_each_segment(bvec, bio, iter)
+               __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
+                                    &nsegs, &cluster);
+
+       return nsegs;
+}
+EXPORT_SYMBOL(blk_bio_map_sg);

You can use __blk_bios_map_sg() to implement blk_bio_map_sg(), then code duplication may be avoided.

Quoted 22 lines
block/blk-merge.c +2 -0
+
+/*
  * map a request to scatterlist, return number of sg entries setup. Caller
  * must make sure sg can hold rq->nr_phys_segments entries
  */
include/linux/blkdev.h +3 -1
index 1fd8fdf..e5de4f8 100644
@@ -1013,6 +1013,9 @@ extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fu
 extern struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev);
 
 extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
+extern int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
+                         struct scatterlist *sglist,
+                         struct scatterlist **sg);
 extern void blk_dump_rq_flags(struct request *, char *);
 extern long nr_blockdev_pages(void);
 
--

1.7.9.5

#3 lore raw

Re: [RFC 1/3] block: Introduce blk_bio_map_sg() to map one bio

Quoted 39 lines · Ming Lei
Quoted 39 lines

/*
+ * map a bio to scatterlist, return number of sg entries setup.
+ */

block/blk-merge.c +36 -0
+int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
+                  struct scatterlist *sglist,
+                  struct scatterlist **sg)
+{
+       struct bio_vec bvec, bvprv = { NULL };
+       struct bvec_iter iter;
+       int nsegs, cluster;
+
+       nsegs = 0;
+       cluster = blk_queue_cluster(q);
+
+       if (bio->bi_rw & REQ_DISCARD) {
+               /*
+                * This is a hack - drivers should be neither modifying the
+                * biovec, nor relying on bi_vcnt - but because of
+                * blk_add_request_payload(), a discard bio may or may not have
+                * a payload we need to set up here (thank you Christoph) and
+                * bi_vcnt is really the only way of telling if we need to.
+                */
+
+               if (bio->bi_vcnt)
+                       goto single_segment;
+
+               return 0;
+       }
+
+       if (bio->bi_rw & REQ_WRITE_SAME) {
+single_segment:
+               *sg = sglist;
+               bvec = bio_iovec(bio);
+               sg_set_page(*sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
+               return 1;
+       }
+
+       bio_for_each_segment(bvec, bio, iter)
+               __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
Quoted 8 lines

block/blk-merge.c single_segment: · line 462

Quoted 5 lines
block/blk-merge.c +5 -0
+                                    &nsegs, &cluster);
+
+       return nsegs;
+}
+EXPORT_SYMBOL(blk_bio_map_sg);

You can use __blk_bios_map_sg() to implement blk_bio_map_sg(), then code duplication may be avoided.

OK. I'll re-factor the code to map one bio.

Quoted 23 lines
Quoted 23 lines
block/blk-merge.c +2 -0
+
+/*
  * map a request to scatterlist, return number of sg entries setup. Caller
  * must make sure sg can hold rq->nr_phys_segments entries
  */
include/linux/blkdev.h +3 -1
index 1fd8fdf..e5de4f8 100644
@@ -1013,6 +1013,9 @@ extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fu
 extern struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev);
 
 extern int blk_rq_map_sg(struct request_queue *, struct request *, struct scatterlist *);
+extern int blk_bio_map_sg(struct request_queue *q, struct bio *bio,
+                         struct scatterlist *sglist,
+                         struct scatterlist **sg);
 extern void blk_dump_rq_flags(struct request *, char *);
 extern long nr_blockdev_pages(void);
 
--

1.7.9.5

Signature · 2 lines
Baolin.wang
Best Regards
#4 lore raw

RFC 2/3 crypto: Introduce CRYPTO_ALG_BULK flag

In reply to Baolin Wang

Now some cipher hardware engines prefer to handle bulk block rather than one sector (512 bytes) created by dm-crypt, cause these cipher engines can handle the intermediate values (IV) by themselves in one bulk block. This means we can increase the size of the request by merging request rather than always 512 bytes and thus increase the hardware engine processing speed.

So introduce 'CRYPTO_ALG_BULK' flag to indicate this cipher can support bulk mode.

---
 include/crypto/skcipher.h |    7 +++++++
 include/linux/crypto.h    |    6 ++++++
 2 files changed, 13 insertions(+)
include/crypto/skcipher.h +7 -0
index 0f987f5..d89d29a 100644
@@ -519,5 +519,12 @@ static inline void skcipher_request_set_crypt(
 	req->iv = iv;
 }
 
+static inline unsigned int skcipher_is_bulk_mode(struct crypto_skcipher *sk_tfm)
+{
+	struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
+
+	return crypto_tfm_alg_bulk(tfm);
+}
+
 #endif	/* _CRYPTO_SKCIPHER_H */
 
include/linux/crypto.h +6 -0
index 6e28c89..a315487 100644
@@ -63,6 +63,7 @@
 #define CRYPTO_ALG_DEAD			0x00000020
 #define CRYPTO_ALG_DYING		0x00000040
 #define CRYPTO_ALG_ASYNC		0x00000080
+#define CRYPTO_ALG_BULK			0x00000100
 
 /*
  * Set this bit if and only if the algorithm requires another algorithm of
@@ -623,6 +624,11 @@ static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
 	return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
 }
 
+static inline unsigned int crypto_tfm_alg_bulk(struct crypto_tfm *tfm)
+{
+	return tfm->__crt_alg->cra_flags & CRYPTO_ALG_BULK;
+}
+
 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
 {
 	return tfm->__crt_alg->cra_blocksize;
Signature · 1 line
1.7.9.5
#5 lore raw

Re: [RFC 2/3] crypto: Introduce CRYPTO_ALG_BULK flag

Quoted 5 lines · Baolin Wang

Now some cipher hardware engines prefer to handle bulk block rather than one sector (512 bytes) created by dm-crypt, cause these cipher engines can handle the intermediate values (IV) by themselves in one bulk block. This means we can increase the size of the request by merging request rather than always 512 bytes and thus increase the hardware engine processing speed.

Hi,

could you please elaborate how exactly you are processing independently encrypted sectors? For example with XTS mode. Do you play internally with tweak calculation? Does this keep 512 bytes sector encryption blocks independent?

(If not, it is breaking compatibility everywhere and you are reinventing disk encryption logic here - just for performance reason for some hw not designed for this task... But that was said several times already.)

Quoted 2 lines

So introduce 'CRYPTO_ALG_BULK' flag to indicate this cipher can support bulk mode.

What exactly skcipher will do if this flag is set?

Which drivers it should use? I do not see any posted patch that uses this flag yet. How we can test it?

Milan

Quoted 49 lines
---
 include/crypto/skcipher.h |    7 +++++++
 include/linux/crypto.h    |    6 ++++++
 2 files changed, 13 insertions(+)
include/crypto/skcipher.h +7 -0
index 0f987f5..d89d29a 100644
@@ -519,5 +519,12 @@ static inline void skcipher_request_set_crypt(
 	req->iv = iv;
 }
 
+static inline unsigned int skcipher_is_bulk_mode(struct crypto_skcipher *sk_tfm)
+{
+	struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
+
+	return crypto_tfm_alg_bulk(tfm);
+}
+
 #endif	/* _CRYPTO_SKCIPHER_H */
 
include/linux/crypto.h +6 -0
index 6e28c89..a315487 100644
@@ -63,6 +63,7 @@
 #define CRYPTO_ALG_DEAD			0x00000020
 #define CRYPTO_ALG_DYING		0x00000040
 #define CRYPTO_ALG_ASYNC		0x00000080
+#define CRYPTO_ALG_BULK			0x00000100
 
 /*
  * Set this bit if and only if the algorithm requires another algorithm of
@@ -623,6 +624,11 @@ static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
 	return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
 }
 
+static inline unsigned int crypto_tfm_alg_bulk(struct crypto_tfm *tfm)
+{
+	return tfm->__crt_alg->cra_flags & CRYPTO_ALG_BULK;
+}
+
 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
 {
 	return tfm->__crt_alg->cra_blocksize;
#6 lore raw

Re: [RFC 2/3] crypto: Introduce CRYPTO_ALG_BULK flag

Hi Milan,

Quoted 9 lines · Milan Broz
Quoted 5 lines · Baolin Wang

Now some cipher hardware engines prefer to handle bulk block rather than one sector (512 bytes) created by dm-crypt, cause these cipher engines can handle the intermediate values (IV) by themselves in one bulk block. This means we can increase the size of the request by merging request rather than always 512 bytes and thus increase the hardware engine processing speed.

Hi,

Quoted 7 lines

could you please elaborate how exactly you are processing independently encrypted sectors? For example with XTS mode. Do you play internally with tweak calculation? Does this keep 512 bytes sector encryption blocks independent?

(If not, it is breaking compatibility everywhere and you are reinventing disk encryption logic here - just for performance reason for some hw not designed for this task... But that was said several times already.)

These are what the cipher hardware engine and engine driver should do, for software we just need send one initial IV and bulk data to crypto layer, which is enough.

Quoted 5 lines
Quoted 3 lines

So introduce 'CRYPTO_ALG_BULK' flag to indicate this cipher can support bulk mode.

What exactly skcipher will do if this flag is set?

I think that depends on how to implement the cipher engine driver.

Quoted 3 lines

Which drivers it should use? I do not see any posted patch that uses this flag yet. How we can test it?

Some cipher engine drivers which support bulk mode should use this flag. Yeah, we need upstream one cipher driver with this flag for testing.

Quoted 53 lines

Milan

Quoted 49 lines
---
 include/crypto/skcipher.h |    7 +++++++
 include/linux/crypto.h    |    6 ++++++
 2 files changed, 13 insertions(+)
include/crypto/skcipher.h +7 -0
index 0f987f5..d89d29a 100644
@@ -519,5 +519,12 @@ static inline void skcipher_request_set_crypt(
      req->iv = iv;
 }
 
+static inline unsigned int skcipher_is_bulk_mode(struct crypto_skcipher *sk_tfm)
+{
+     struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
+
+     return crypto_tfm_alg_bulk(tfm);
+}
+
 #endif       /* _CRYPTO_SKCIPHER_H */
 
include/linux/crypto.h +6 -0
index 6e28c89..a315487 100644
@@ -63,6 +63,7 @@
 #define CRYPTO_ALG_DEAD                      0x00000020
 #define CRYPTO_ALG_DYING             0x00000040
 #define CRYPTO_ALG_ASYNC             0x00000080
+#define CRYPTO_ALG_BULK                      0x00000100
 
 /*
  * Set this bit if and only if the algorithm requires another algorithm of
@@ -623,6 +624,11 @@ static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
      return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
 }
 
+static inline unsigned int crypto_tfm_alg_bulk(struct crypto_tfm *tfm)
+{
+     return tfm->__crt_alg->cra_flags & CRYPTO_ALG_BULK;
+}
+
 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
 {
      return tfm->__crt_alg->cra_blocksize;
Signature · 2 lines
Baolin.wang
Best Regards
#7 lore raw

Re: [RFC 2/3] crypto: Introduce CRYPTO_ALG_BULK flag

Quoted 16 lines · Baolin Wang

Hi Milan,

Quoted 13 lines · Milan Broz
Quoted 5 lines · Baolin Wang

Now some cipher hardware engines prefer to handle bulk block rather than one sector (512 bytes) created by dm-crypt, cause these cipher engines can handle the intermediate values (IV) by themselves in one bulk block. This means we can increase the size of the request by merging request rather than always 512 bytes and thus increase the hardware engine processing speed.

Hi,

could you please elaborate how exactly you are processing independently encrypted sectors? For example with XTS mode. Do you play internally with tweak calculation? Does this keep 512 bytes sector encryption blocks independent?

Quoted 7 lines
Quoted 3 lines

(If not, it is breaking compatibility everywhere and you are reinventing disk encryption logic here - just for performance reason for some hw not designed for this task... But that was said several times already.)

These are what the cipher hardware engine and engine driver should do, for software we just need send one initial IV and bulk data to crypto layer, which is enough.

Hi,

Thanks for answer.

So this is just doing some kind of batch processing optimization inside the driver? I still do not understand why it is not possible to "batch" these requests inside you driver (with async API) then and process them in one go without any changes to dmcrypt though. (But I am not familiar with these drivers.)

If I understand it correctly, subsequent IVs are calculated inside your driver just based on some initial value?
This can work only for sequential IVs (or, better said, for predictable IVs like plain64, implemented inside your hw/driver).

It cannot work for IVs/tweaks that are randomized or keyed (like ESSIV). Yes, I know that using ESSIV for XTS is considered overkill but I do not see you are checking this condition anywhere in code (we can definitely configure such a mapping).

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

So introduce 'CRYPTO_ALG_BULK' flag to indicate this cipher can support bulk mode.

What exactly skcipher will do if this flag is set?

I think that depends on how to implement the cipher engine driver.

I do not care about implementation details, I just would like to see some high-level description for the new API flag.
(Or at least read the code that implements it :)

Quoted 6 lines
Quoted 2 lines

Which drivers it should use? I do not see any posted patch that uses this flag yet. How we can test it?

Some cipher engine drivers which support bulk mode should use this flag. Yeah, we need upstream one cipher driver with this flag for testing.

I think if you introduce new flag, you should also post drivers that uses it. Otherwise it is just unused code for mainline.

And I definitely would like to test it somewhere and see real performance data (not just simple dd).

Thanks,
Milan

Quoted 57 lines
Quoted 54 lines

Milan

Quoted 49 lines
---
 include/crypto/skcipher.h |    7 +++++++
 include/linux/crypto.h    |    6 ++++++
 2 files changed, 13 insertions(+)
include/crypto/skcipher.h +7 -0
index 0f987f5..d89d29a 100644
@@ -519,5 +519,12 @@ static inline void skcipher_request_set_crypt(
      req->iv = iv;
 }
 
+static inline unsigned int skcipher_is_bulk_mode(struct crypto_skcipher *sk_tfm)
+{
+     struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
+
+     return crypto_tfm_alg_bulk(tfm);
+}
+
 #endif       /* _CRYPTO_SKCIPHER_H */
 
include/linux/crypto.h +6 -0
index 6e28c89..a315487 100644
@@ -63,6 +63,7 @@
 #define CRYPTO_ALG_DEAD                      0x00000020
 #define CRYPTO_ALG_DYING             0x00000040
 #define CRYPTO_ALG_ASYNC             0x00000080
+#define CRYPTO_ALG_BULK                      0x00000100
 
 /*
  * Set this bit if and only if the algorithm requires another algorithm of
@@ -623,6 +624,11 @@ static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
      return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
 }
 
+static inline unsigned int crypto_tfm_alg_bulk(struct crypto_tfm *tfm)
+{
+     return tfm->__crt_alg->cra_flags & CRYPTO_ALG_BULK;
+}
+
 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
 {
      return tfm->__crt_alg->cra_blocksize;
#8 lore raw

Re: [RFC 2/3] crypto: Introduce CRYPTO_ALG_BULK flag

Quoted 25 lines · Milan Broz
Quoted 16 lines · Baolin Wang

Hi Milan,

Quoted 13 lines · Milan Broz
Quoted 5 lines · Baolin Wang

Now some cipher hardware engines prefer to handle bulk block rather than one sector (512 bytes) created by dm-crypt, cause these cipher engines can handle the intermediate values (IV) by themselves in one bulk block. This means we can increase the size of the request by merging request rather than always 512 bytes and thus increase the hardware engine processing speed.

Hi,

could you please elaborate how exactly you are processing independently encrypted sectors? For example with XTS mode. Do you play internally with tweak calculation? Does this keep 512 bytes sector encryption blocks independent?

Quoted 7 lines
Quoted 3 lines

(If not, it is breaking compatibility everywhere and you are reinventing disk encryption logic here - just for performance reason for some hw not designed for this task... But that was said several times already.)

These are what the cipher hardware engine and engine driver should do, for software we just need send one initial IV and bulk data to crypto layer, which is enough.

Quoted 8 lines

Hi,

Thanks for answer.

So this is just doing some kind of batch processing optimization inside the driver? I still do not understand why it is not possible to "batch" these requests inside you driver (with async API) then and process them in one go without any changes to dmcrypt though. (But I am not familiar with these drivers.)

I think it is not only for the driver level, but also for cipher API. If one cipher engine can support bulk mode, then we should send bulk data to it, not only one sector, which need to be implemented at top API level, such as:

skcipher_request_set_crypt(req, sgt_in.sgl, sgt_out.sgl, total_bytes, iv); /* send sg table to crypt layer */

If move these optimization into driver, it means we need to merge requests from dm-crypt together to be one big request for engine driver, but it will be low efficiency and not help. Why we can not send one bulk request at first in dm-crypt? We just need to set the different parameters for skcipher_request_set_crypt() function according to different cipher mode in dm-crypt.

Quoted 3 lines

If I understand it correctly, subsequent IVs are calculated inside your driver just based on some initial value?

Yes, something like this.

Quoted 7 lines

This can work only for sequential IVs (or, better said, for predictable IVs like plain64, implemented inside your hw/driver).

It cannot work for IVs/tweaks that are randomized or keyed (like ESSIV). Yes, I know that using ESSIV for XTS is considered overkill but I do not see you are checking this condition anywhere in code (we can definitely configure such a mapping).

So if some ciphers (like: aes(cbc)) can not support bulk mode, then we should not add the CRYPTO_ALG_BULK flag for this cipher in the cipher driver.

Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

So introduce 'CRYPTO_ALG_BULK' flag to indicate this cipher can support bulk mode.

What exactly skcipher will do if this flag is set?

I think that depends on how to implement the cipher engine driver.

I do not care about implementation details, I just would like to see some high-level description for the new API flag.
(Or at least read the code that implements it :)

I think for high-level description, we can use sg table to map one bulk block and send to crypt layer by skcipher_request_set_crypt() function.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

Which drivers it should use? I do not see any posted patch that uses this flag yet. How we can test it?

Some cipher engine drivers which support bulk mode should use this flag. Yeah, we need upstream one cipher driver with this flag for testing.

I think if you introduce new flag, you should also post drivers that uses it. Otherwise it is just unused code for mainline.

Yeah, that's right.

Quoted 3 lines

And I definitely would like to test it somewhere and see real performance data (not just simple dd).

OK. Thanks.

Quoted 61 lines

Thanks,
Milan

Quoted 57 lines
Quoted 54 lines

Milan

Quoted 49 lines
---
 include/crypto/skcipher.h |    7 +++++++
 include/linux/crypto.h    |    6 ++++++
 2 files changed, 13 insertions(+)
include/crypto/skcipher.h +7 -0
index 0f987f5..d89d29a 100644
@@ -519,5 +519,12 @@ static inline void skcipher_request_set_crypt(
      req->iv = iv;
 }
 
+static inline unsigned int skcipher_is_bulk_mode(struct crypto_skcipher *sk_tfm)
+{
+     struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
+
+     return crypto_tfm_alg_bulk(tfm);
+}
+
 #endif       /* _CRYPTO_SKCIPHER_H */
 
include/linux/crypto.h +6 -0
index 6e28c89..a315487 100644
@@ -63,6 +63,7 @@
 #define CRYPTO_ALG_DEAD                      0x00000020
 #define CRYPTO_ALG_DYING             0x00000040
 #define CRYPTO_ALG_ASYNC             0x00000080
+#define CRYPTO_ALG_BULK                      0x00000100
 
 /*
  * Set this bit if and only if the algorithm requires another algorithm of
@@ -623,6 +624,11 @@ static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
      return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
 }
 
+static inline unsigned int crypto_tfm_alg_bulk(struct crypto_tfm *tfm)
+{
+     return tfm->__crt_alg->cra_flags & CRYPTO_ALG_BULK;
+}
+
 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
 {
      return tfm->__crt_alg->cra_blocksize;
Signature · 2 lines
Baolin.wang
Best Regards
#9 lore raw

RFC 3/3 md: dm-crypt: Introduce the bulk mode method when sending request

In reply to Baolin Wang

In now dm-crypt code, it is ineffective to map one segment (always one sector) of one bio with just only one scatterlist at one time for hardware crypto engine. Especially for some encryption mode (like ecb or xts mode) cooperating with the crypto engine, they just need one initial IV or null IV instead of different IV for each sector. In this situation We can consider to use multiple scatterlists to map the whole bio and send all scatterlists of one bio to crypto engine to encrypt or decrypt, which can improve the hardware engine's efficiency.

With this optimization, On my test setup (beaglebone black board) using 64KB I/Os on an eMMC storage device I saw about 60% improvement in throughput for encrypted writes, and about 100% improvement for encrypted reads. But this is not fit for other modes which need different IV for each sector.

---
 drivers/md/dm-crypt.c |  188 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 176 insertions(+), 12 deletions(-)
drivers/md/dm-crypt.c +176 -12
index 4f3cb35..1c86ea7 100644
@@ -33,6 +33,7 @@
 #include <linux/device-mapper.h>
 
 #define DM_MSG_PREFIX "crypt"
+#define DM_MAX_SG_LIST	1024
 
 /*
  * context holding the current state of a multi-part conversion
@@ -46,6 +47,8 @@ struct convert_context {
 	sector_t cc_sector;
 	atomic_t cc_pending;
 	struct skcipher_request *req;
+	struct sg_table sgt_in;
+	struct sg_table sgt_out;
 };
 
 /*
@@ -803,6 +806,108 @@ static struct crypt_iv_operations crypt_iv_tcw_ops = {
 	.post	   = crypt_iv_tcw_post
 };
 
+/*
+ * Check how many sg entry numbers are needed when map one bio
+ * with scatterlists in advance.
+ */
+static unsigned int crypt_sg_entry(struct bio *bio_t)
+{
+	struct request_queue *q = bdev_get_queue(bio_t->bi_bdev);
+	int cluster = blk_queue_cluster(q);
+	struct bio_vec bvec, bvprv = { NULL };
+	struct bvec_iter biter;
+	unsigned long nbytes = 0, sg_length = 0;
+	unsigned int sg_cnt = 0, first_bvec = 0;
+
+	if (bio_t->bi_rw & REQ_DISCARD) {
+		if (bio_t->bi_vcnt)
+			return 1;
+		return 0;
+	}
+
+	if (bio_t->bi_rw & REQ_WRITE_SAME)
+		return 1;
+
+	bio_for_each_segment(bvec, bio_t, biter) {
+		nbytes = bvec.bv_len;
+
+		if (!cluster) {
+			sg_cnt++;
+			continue;
+		}
+
+		if (!first_bvec) {
+			first_bvec = 1;
+			goto new_segment;
+		}
+
+		if (sg_length + nbytes > queue_max_segment_size(q))
+			goto new_segment;
+
+		if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec))
+			goto new_segment;
+
+		if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bvec))
+			goto new_segment;
+
+		sg_length += nbytes;
+		continue;
+
+new_segment:
+		memcpy(&bvprv, &bvec, sizeof(struct bio_vec));
+		sg_length = nbytes;
+		sg_cnt++;
+	}
+
+	return sg_cnt;
+}
+
+static int crypt_convert_alloc_table(struct crypt_config *cc,
+				     struct convert_context *ctx)
+{
+	struct bio *bio_in = ctx->bio_in;
+	struct bio *bio_out = ctx->bio_out;
+	unsigned int mode = skcipher_is_bulk_mode(any_tfm(cc));
+	unsigned int sg_in_max, sg_out_max;
+	int ret = 0;
+
+	if (!mode)
+		goto out2;
+
+	/*
+	 * Need to calculate how many sg entry need to be used
+	 * for this bio.
+	 */
+	sg_in_max = crypt_sg_entry(bio_in) + 1;
+	if (sg_in_max > DM_MAX_SG_LIST || sg_in_max <= 2)
+		goto out2;
+
+	ret = sg_alloc_table(&ctx->sgt_in, sg_in_max, GFP_KERNEL);
+	if (ret)
+		goto out2;
+
+	if (bio_data_dir(bio_in) == READ)
+		goto out1;
+
+	sg_out_max = crypt_sg_entry(bio_out) + 1;
+	if (sg_out_max > DM_MAX_SG_LIST || sg_out_max <= 2)
+		goto out3;
+
+	ret = sg_alloc_table(&ctx->sgt_out, sg_out_max, GFP_KERNEL);
+	if (ret)
+		goto out3;
+
+	return 0;
+
+out3:
+	sg_free_table(&ctx->sgt_in);
+out2:
+	ctx->sgt_in.orig_nents = 0;
+out1:
+	ctx->sgt_out.orig_nents = 0;
+	return ret;
+}
+
 static void crypt_convert_init(struct crypt_config *cc,
 			       struct convert_context *ctx,
 			       struct bio *bio_out, struct bio *bio_in,
@@ -843,7 +948,13 @@ static int crypt_convert_block(struct crypt_config *cc,
 {
 	struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
 	struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
+	unsigned int mode = skcipher_is_bulk_mode(any_tfm(cc));
+	struct bio *bio_in = ctx->bio_in;
+	struct bio *bio_out = ctx->bio_out;
+	unsigned int total_bytes = bio_in->bi_iter.bi_size;
 	struct dm_crypt_request *dmreq;
+	struct scatterlist *sg_in;
+	struct scatterlist *sg_out;
 	u8 *iv;
 	int r;
 
@@ -852,16 +963,6 @@ static int crypt_convert_block(struct crypt_config *cc,
 
 	dmreq->iv_sector = ctx->cc_sector;
 	dmreq->ctx = ctx;
-	sg_init_table(&dmreq->sg_in, 1);
-	sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
-		    bv_in.bv_offset);
-
-	sg_init_table(&dmreq->sg_out, 1);
-	sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
-		    bv_out.bv_offset);
-
-	bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
-	bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
 
 	if (cc->iv_gen_ops) {
 		r = cc->iv_gen_ops->generator(cc, iv, dmreq);
@@ -869,8 +970,63 @@ static int crypt_convert_block(struct crypt_config *cc,
 			return r;
 	}
 
-	skcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
-				   1 << SECTOR_SHIFT, iv);
+	if (mode && ctx->sgt_in.orig_nents > 0) {
+		struct scatterlist *sg = NULL;
+		unsigned int total_sg_in, total_sg_out;
+
+		total_sg_in = blk_bio_map_sg(bdev_get_queue(bio_in->bi_bdev),
+					     bio_in, ctx->sgt_in.sgl, &sg);
+		if ((total_sg_in <= 0) ||
+		    (total_sg_in > ctx->sgt_in.orig_nents)) {
+			DMERR("%s in sg map error %d, sg table nents[%d]\n",
+			      __func__, total_sg_in, ctx->sgt_in.orig_nents);
+			return -EINVAL;
+		}
+
+		if (sg)
+			sg_mark_end(sg);
+
+		ctx->iter_in.bi_size -= total_bytes;
+		sg_in = ctx->sgt_in.sgl;
+		sg_out = ctx->sgt_in.sgl;
+
+		if (bio_data_dir(bio_in) == READ)
+			goto set_crypt;
+
+		sg = NULL;
+		total_sg_out = blk_bio_map_sg(bdev_get_queue(bio_out->bi_bdev),
+					      bio_out, ctx->sgt_out.sgl, &sg);
+		if ((total_sg_out <= 0) ||
+		    (total_sg_out > ctx->sgt_out.orig_nents)) {
+			DMERR("%s out sg map error %d, sg table nents[%d]\n",
+			      __func__, total_sg_out, ctx->sgt_out.orig_nents);
+			return -EINVAL;
+		}
+
+		if (sg)
+			sg_mark_end(sg);
+
+		ctx->iter_out.bi_size -= total_bytes;
+		sg_out = ctx->sgt_out.sgl;
+	} else  {
+		sg_init_table(&dmreq->sg_in, 1);
+		sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
+			    bv_in.bv_offset);
+
+		sg_init_table(&dmreq->sg_out, 1);
+		sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
+			    bv_out.bv_offset);
+
+		bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
+		bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
+
+		sg_in = &dmreq->sg_in;
+		sg_out = &dmreq->sg_out;
+		total_bytes = 1 << SECTOR_SHIFT;
+	}
+
+set_crypt:
+	skcipher_request_set_crypt(req, sg_in, sg_out, total_bytes, iv);
 
 	if (bio_data_dir(ctx->bio_in) == WRITE)
 		r = crypto_skcipher_encrypt(req);
@@ -1081,6 +1237,8 @@ static void crypt_dec_pending(struct dm_crypt_io *io)
 	if (io->ctx.req)
 		crypt_free_req(cc, io->ctx.req, base_bio);
 
+	sg_free_table(&io->ctx.sgt_in);
+	sg_free_table(&io->ctx.sgt_out);
 	base_bio->bi_error = error;
 	bio_endio(base_bio);
 }
@@ -1312,6 +1470,9 @@ static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
 	io->ctx.iter_out = clone->bi_iter;
 
 	sector += bio_sectors(clone);
+	r = crypt_convert_alloc_table(cc, &io->ctx);
+	if (r < 0)
+		io->error = -EIO;
 
 	crypt_inc_pending(io);
 	r = crypt_convert(cc, &io->ctx);
@@ -1343,6 +1504,9 @@ static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
 
 	crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
 			   io->sector);
+	r = crypt_convert_alloc_table(cc, &io->ctx);
+	if (r < 0)
+		io->error = -EIO;
 
 	r = crypt_convert(cc, &io->ctx);
 	if (r < 0)
Signature · 1 line
1.7.9.5
#10 lore raw

Re: [RFC 3/3] md: dm-crypt: Introduce the bulk mode method when sending request

Comments inlined.

In general the most concerning bit is the need for memory allocation in the IO path (see comment/question below near call to sg_alloc_table). In DM targets we make heavy use of .ctr preallocated memory and/or per-bio-data to avoid memory allocations in the IO path.

Quoted 96 lines · Baolin Wang

In now dm-crypt code, it is ineffective to map one segment (always one sector) of one bio with just only one scatterlist at one time for hardware crypto engine. Especially for some encryption mode (like ecb or xts mode) cooperating with the crypto engine, they just need one initial IV or null IV instead of different IV for each sector. In this situation We can consider to use multiple scatterlists to map the whole bio and send all scatterlists of one bio to crypto engine to encrypt or decrypt, which can improve the hardware engine's efficiency.

With this optimization, On my test setup (beaglebone black board) using 64KB I/Os on an eMMC storage device I saw about 60% improvement in throughput for encrypted writes, and about 100% improvement for encrypted reads. But this is not fit for other modes which need different IV for each sector.

---
 drivers/md/dm-crypt.c |  188 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 176 insertions(+), 12 deletions(-)
drivers/md/dm-crypt.c +55 -0
index 4f3cb35..1c86ea7 100644
@@ -33,6 +33,7 @@
 #include <linux/device-mapper.h>
 
 #define DM_MSG_PREFIX "crypt"
+#define DM_MAX_SG_LIST	1024
 
 /*
  * context holding the current state of a multi-part conversion
@@ -46,6 +47,8 @@ struct convert_context {
 	sector_t cc_sector;
 	atomic_t cc_pending;
 	struct skcipher_request *req;
+	struct sg_table sgt_in;
+	struct sg_table sgt_out;
 };
 
 /*
@@ -803,6 +806,108 @@ static struct crypt_iv_operations crypt_iv_tcw_ops = {
 	.post	   = crypt_iv_tcw_post
 };
 
+/*
+ * Check how many sg entry numbers are needed when map one bio
+ * with scatterlists in advance.
+ */
+static unsigned int crypt_sg_entry(struct bio *bio_t)
+{
+	struct request_queue *q = bdev_get_queue(bio_t->bi_bdev);
+	int cluster = blk_queue_cluster(q);
+	struct bio_vec bvec, bvprv = { NULL };
+	struct bvec_iter biter;
+	unsigned long nbytes = 0, sg_length = 0;
+	unsigned int sg_cnt = 0, first_bvec = 0;
+
+	if (bio_t->bi_rw & REQ_DISCARD) {
+		if (bio_t->bi_vcnt)
+			return 1;
+		return 0;
+	}
+
+	if (bio_t->bi_rw & REQ_WRITE_SAME)
+		return 1;
+
+	bio_for_each_segment(bvec, bio_t, biter) {
+		nbytes = bvec.bv_len;
+
+		if (!cluster) {
+			sg_cnt++;
+			continue;
+		}
+
+		if (!first_bvec) {
+			first_bvec = 1;
+			goto new_segment;
+		}
+
+		if (sg_length + nbytes > queue_max_segment_size(q))
+			goto new_segment;
+
+		if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec))
+			goto new_segment;
+
+		if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bvec))
+			goto new_segment;
+
+		sg_length += nbytes;
+		continue;
+
+new_segment:
+		memcpy(&bvprv, &bvec, sizeof(struct bio_vec));
+		sg_length = nbytes;
+		sg_cnt++;
+	}

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 870

Quoted 10 lines
drivers/md/dm-crypt.c +10 -0
+
+	return sg_cnt;
+}
+
+static int crypt_convert_alloc_table(struct crypt_config *cc,
+				     struct convert_context *ctx)
+{
+	struct bio *bio_in = ctx->bio_in;
+	struct bio *bio_out = ctx->bio_out;
+	unsigned int mode = skcipher_is_bulk_mode(any_tfm(cc));

please use: bool bulk_mode = ...

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 875

Quoted 5 lines
drivers/md/dm-crypt.c +5 -0
+	unsigned int sg_in_max, sg_out_max;
+	int ret = 0;
+
+	if (!mode)
+		goto out2;

Please use more descriptive label names than out[1-3]

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 881

Quoted 6 lines
drivers/md/dm-crypt.c +6 -0
+
+	/*
+	 * Need to calculate how many sg entry need to be used
+	 * for this bio.
+	 */
+	sg_in_max = crypt_sg_entry(bio_in) + 1;

The return from crypt_sg_entry() is pretty awkward, given you just go on to add 1; as is the bounds checking.. the magic value of 2 needs to be be made clearer.

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 885

Quoted 4 lines
drivers/md/dm-crypt.c +4 -0
+	if (sg_in_max > DM_MAX_SG_LIST || sg_in_max <= 2)
+		goto out2;
+
+	ret = sg_alloc_table(&ctx->sgt_in, sg_in_max, GFP_KERNEL);

Is it safe to be using GFP_KERNEL here? AFAIK this is in the IO mapping path and we try to avoid memory allocations at all costs -- due to the risk of deadlock when issuing IO to stacked block devices (dm-crypt could be part of a much more elaborate IO stack).

Quoted 7 lines
drivers/md/dm-crypt.c +7 -0
+	if (ret)
+		goto out2;
+
+	if (bio_data_dir(bio_in) == READ)
+		goto out1;
+
+	sg_out_max = crypt_sg_entry(bio_out) + 1;

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 902

Quoted 10 lines
drivers/md/dm-crypt.c +10 -0
+	if (sg_out_max > DM_MAX_SG_LIST || sg_out_max <= 2)
+		goto out3;
+
+	ret = sg_alloc_table(&ctx->sgt_out, sg_out_max, GFP_KERNEL);
+	if (ret)
+		goto out3;
+
+	return 0;
+
+out3:

out_free_table?

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 904

Quoted 2 lines
drivers/md/dm-crypt.c +2 -0
+	sg_free_table(&ctx->sgt_in);
+out2:

out_skip_alloc?

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 906

Quoted 2 lines
drivers/md/dm-crypt.c +2 -0
+	ctx->sgt_in.orig_nents = 0;
+out1:

out_skip_write?

drivers/md/dm-crypt.c static int crypt_convert_block(struct crypt_config *cc, · line 951

Quoted 12 lines
drivers/md/dm-crypt.c +5 -0
+	ctx->sgt_out.orig_nents = 0;
+	return ret;
+}
+
 static void crypt_convert_init(struct crypt_config *cc,
 			       struct convert_context *ctx,
 			       struct bio *bio_out, struct bio *bio_in,
@@ -843,7 +948,13 @@ static int crypt_convert_block(struct crypt_config *cc,
 {
 	struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
 	struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
+	unsigned int mode = skcipher_is_bulk_mode(any_tfm(cc));

again please use: bool bulk_mode = ...

Quoted 79 lines
drivers/md/dm-crypt.c +52 -12
+	struct bio *bio_in = ctx->bio_in;
+	struct bio *bio_out = ctx->bio_out;
+	unsigned int total_bytes = bio_in->bi_iter.bi_size;
 	struct dm_crypt_request *dmreq;
+	struct scatterlist *sg_in;
+	struct scatterlist *sg_out;
 	u8 *iv;
 	int r;
 
@@ -852,16 +963,6 @@ static int crypt_convert_block(struct crypt_config *cc,
 
 	dmreq->iv_sector = ctx->cc_sector;
 	dmreq->ctx = ctx;
-	sg_init_table(&dmreq->sg_in, 1);
-	sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
-		    bv_in.bv_offset);
-
-	sg_init_table(&dmreq->sg_out, 1);
-	sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
-		    bv_out.bv_offset);
-
-	bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
-	bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
 
 	if (cc->iv_gen_ops) {
 		r = cc->iv_gen_ops->generator(cc, iv, dmreq);
@@ -869,8 +970,63 @@ static int crypt_convert_block(struct crypt_config *cc,
 			return r;
 	}
 
-	skcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
-				   1 << SECTOR_SHIFT, iv);
+	if (mode && ctx->sgt_in.orig_nents > 0) {
+		struct scatterlist *sg = NULL;
+		unsigned int total_sg_in, total_sg_out;
+
+		total_sg_in = blk_bio_map_sg(bdev_get_queue(bio_in->bi_bdev),
+					     bio_in, ctx->sgt_in.sgl, &sg);
+		if ((total_sg_in <= 0) ||
+		    (total_sg_in > ctx->sgt_in.orig_nents)) {
+			DMERR("%s in sg map error %d, sg table nents[%d]\n",
+			      __func__, total_sg_in, ctx->sgt_in.orig_nents);
+			return -EINVAL;
+		}
+
+		if (sg)
+			sg_mark_end(sg);
+
+		ctx->iter_in.bi_size -= total_bytes;
+		sg_in = ctx->sgt_in.sgl;
+		sg_out = ctx->sgt_in.sgl;
+
+		if (bio_data_dir(bio_in) == READ)
+			goto set_crypt;
+
+		sg = NULL;
+		total_sg_out = blk_bio_map_sg(bdev_get_queue(bio_out->bi_bdev),
+					      bio_out, ctx->sgt_out.sgl, &sg);
+		if ((total_sg_out <= 0) ||
+		    (total_sg_out > ctx->sgt_out.orig_nents)) {
+			DMERR("%s out sg map error %d, sg table nents[%d]\n",
+			      __func__, total_sg_out, ctx->sgt_out.orig_nents);
+			return -EINVAL;
+		}
+
+		if (sg)
+			sg_mark_end(sg);
+
+		ctx->iter_out.bi_size -= total_bytes;
+		sg_out = ctx->sgt_out.sgl;
+	} else  {
+		sg_init_table(&dmreq->sg_in, 1);
+		sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
+			    bv_in.bv_offset);
+
+		sg_init_table(&dmreq->sg_out, 1);
+		sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
+			    bv_out.bv_offset);
+

drivers/md/dm-crypt.c static int crypt_convert_block(struct crypt_config *cc, · line 1029

Quoted 10 lines
drivers/md/dm-crypt.c +10 -0
+		bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
+		bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
+
+		sg_in = &dmreq->sg_in;
+		sg_out = &dmreq->sg_out;
+		total_bytes = 1 << SECTOR_SHIFT;
+	}
+
+set_crypt:
+	skcipher_request_set_crypt(req, sg_in, sg_out, total_bytes, iv);

Given how long this code has gotten I'd prefer to see this factored out to a new setup method.

Quoted 38 lines
drivers/md/dm-crypt.c +8 -0
 	if (bio_data_dir(ctx->bio_in) == WRITE)
 		r = crypto_skcipher_encrypt(req);
@@ -1081,6 +1237,8 @@ static void crypt_dec_pending(struct dm_crypt_io *io)
 	if (io->ctx.req)
 		crypt_free_req(cc, io->ctx.req, base_bio);
 
+	sg_free_table(&io->ctx.sgt_in);
+	sg_free_table(&io->ctx.sgt_out);
 	base_bio->bi_error = error;
 	bio_endio(base_bio);
 }
@@ -1312,6 +1470,9 @@ static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
 	io->ctx.iter_out = clone->bi_iter;
 
 	sector += bio_sectors(clone);
+	r = crypt_convert_alloc_table(cc, &io->ctx);
+	if (r < 0)
+		io->error = -EIO;
 
 	crypt_inc_pending(io);
 	r = crypt_convert(cc, &io->ctx);
@@ -1343,6 +1504,9 @@ static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
 
 	crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
 			   io->sector);
+	r = crypt_convert_alloc_table(cc, &io->ctx);
+	if (r < 0)
+		io->error = -EIO;
 
 	r = crypt_convert(cc, &io->ctx);
 	if (r < 0)
Signature · 6 lines
1.7.9.5

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel
#11 lore raw

Re: [RFC 3/3] md: dm-crypt: Introduce the bulk mode method when sending request

Quoted 6 lines · Mike Snitzer

Comments inlined.

In general the most concerning bit is the need for memory allocation in the IO path (see comment/question below near call to sg_alloc_table). In DM targets we make heavy use of .ctr preallocated memory and/or per-bio-data to avoid memory allocations in the IO path.

Make sense.

Quoted 104 lines
Quoted 100 lines · Baolin Wang

In now dm-crypt code, it is ineffective to map one segment (always one sector) of one bio with just only one scatterlist at one time for hardware crypto engine. Especially for some encryption mode (like ecb or xts mode) cooperating with the crypto engine, they just need one initial IV or null IV instead of different IV for each sector. In this situation We can consider to use multiple scatterlists to map the whole bio and send all scatterlists of one bio to crypto engine to encrypt or decrypt, which can improve the hardware engine's efficiency.

With this optimization, On my test setup (beaglebone black board) using 64KB I/Os on an eMMC storage device I saw about 60% improvement in throughput for encrypted writes, and about 100% improvement for encrypted reads. But this is not fit for other modes which need different IV for each sector.

---
 drivers/md/dm-crypt.c |  188 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 176 insertions(+), 12 deletions(-)
drivers/md/dm-crypt.c +59 -0
index 4f3cb35..1c86ea7 100644
@@ -33,6 +33,7 @@
 #include <linux/device-mapper.h>
 
 #define DM_MSG_PREFIX "crypt"
+#define DM_MAX_SG_LIST       1024
 
 /*
  * context holding the current state of a multi-part conversion
@@ -46,6 +47,8 @@ struct convert_context {
      sector_t cc_sector;
      atomic_t cc_pending;
      struct skcipher_request *req;
+     struct sg_table sgt_in;
+     struct sg_table sgt_out;
 };
 
 /*
@@ -803,6 +806,108 @@ static struct crypt_iv_operations crypt_iv_tcw_ops = {
      .post      = crypt_iv_tcw_post
 };
 
+/*
+ * Check how many sg entry numbers are needed when map one bio
+ * with scatterlists in advance.
+ */
+static unsigned int crypt_sg_entry(struct bio *bio_t)
+{
+     struct request_queue *q = bdev_get_queue(bio_t->bi_bdev);
+     int cluster = blk_queue_cluster(q);
+     struct bio_vec bvec, bvprv = { NULL };
+     struct bvec_iter biter;
+     unsigned long nbytes = 0, sg_length = 0;
+     unsigned int sg_cnt = 0, first_bvec = 0;
+
+     if (bio_t->bi_rw & REQ_DISCARD) {
+             if (bio_t->bi_vcnt)
+                     return 1;
+             return 0;
+     }
+
+     if (bio_t->bi_rw & REQ_WRITE_SAME)
+             return 1;
+
+     bio_for_each_segment(bvec, bio_t, biter) {
+             nbytes = bvec.bv_len;
+
+             if (!cluster) {
+                     sg_cnt++;
+                     continue;
+             }
+
+             if (!first_bvec) {
+                     first_bvec = 1;
+                     goto new_segment;
+             }
+
+             if (sg_length + nbytes > queue_max_segment_size(q))
+                     goto new_segment;
+
+             if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bvec))
+                     goto new_segment;
+
+             if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bvec))
+                     goto new_segment;
+
+             sg_length += nbytes;
+             continue;
+
+new_segment:
+             memcpy(&bvprv, &bvec, sizeof(struct bio_vec));
+             sg_length = nbytes;
+             sg_cnt++;
+     }
+
+     return sg_cnt;
+}
+
Quoted 8 lines

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 870

Quoted 6 lines
drivers/md/dm-crypt.c +6 -0
+static int crypt_convert_alloc_table(struct crypt_config *cc,
+                                  struct convert_context *ctx)
+{
+     struct bio *bio_in = ctx->bio_in;
+     struct bio *bio_out = ctx->bio_out;
+     unsigned int mode = skcipher_is_bulk_mode(any_tfm(cc));

please use: bool bulk_mode = ...

OK.

Quoted 8 lines

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 875

Quoted 6 lines
drivers/md/dm-crypt.c +5 -0
+     unsigned int sg_in_max, sg_out_max;
+     int ret = 0;
+
+     if (!mode)
+             goto out2;

Please use more descriptive label names than out[1-3]

OK.

Quoted 11 lines

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 881

Quoted 7 lines
drivers/md/dm-crypt.c +6 -0
+
+     /*
+      * Need to calculate how many sg entry need to be used
+      * for this bio.
+      */
+     sg_in_max = crypt_sg_entry(bio_in) + 1;

The return from crypt_sg_entry() is pretty awkward, given you just go on to add 1; as is the bounds checking.. the magic value of 2 needs to be be made clearer.

I'll remove the crypt_sg_entry() function.

Quoted 10 lines

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 885

Quoted 5 lines
drivers/md/dm-crypt.c +4 -0
+     if (sg_in_max > DM_MAX_SG_LIST || sg_in_max <= 2)
+             goto out2;
+
+     ret = sg_alloc_table(&ctx->sgt_in, sg_in_max, GFP_KERNEL);

Is it safe to be using GFP_KERNEL here? AFAIK this is in the IO mapping path and we try to avoid memory allocations at all costs -- due to the risk of deadlock when issuing IO to stacked block devices (dm-crypt could be part of a much more elaborate IO stack).

OK. I'll move the sg table allocation to be preallocated in the .ctr function.

Quoted 31 lines
Quoted 8 lines
drivers/md/dm-crypt.c +7 -0
+     if (ret)
+             goto out2;
+
+     if (bio_data_dir(bio_in) == READ)
+             goto out1;
+
+     sg_out_max = crypt_sg_entry(bio_out) + 1;

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 902

Quoted 10 lines
drivers/md/dm-crypt.c +10 -0
+     if (sg_out_max > DM_MAX_SG_LIST || sg_out_max <= 2)
+             goto out3;
+
+     ret = sg_alloc_table(&ctx->sgt_out, sg_out_max, GFP_KERNEL);
+     if (ret)
+             goto out3;
+
+     return 0;
+
+out3:

out_free_table?

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 904

Quoted 2 lines
drivers/md/dm-crypt.c +2 -0
+     sg_free_table(&ctx->sgt_in);
+out2:

out_skip_alloc?

drivers/md/dm-crypt.c static struct crypt_iv_operations crypt_iv_tcw_ops = { · line 906

Quoted 2 lines
drivers/md/dm-crypt.c +2 -0
+     ctx->sgt_in.orig_nents = 0;
+out1:

out_skip_write?

Quoted 14 lines

drivers/md/dm-crypt.c static int crypt_convert_block(struct crypt_config *cc, · line 951

Quoted 12 lines
drivers/md/dm-crypt.c +5 -0
+     ctx->sgt_out.orig_nents = 0;
+     return ret;
+}
+
 static void crypt_convert_init(struct crypt_config *cc,
                             struct convert_context *ctx,
                             struct bio *bio_out, struct bio *bio_in,
@@ -843,7 +948,13 @@ static int crypt_convert_block(struct crypt_config *cc,
 {
      struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
      struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
+     unsigned int mode = skcipher_is_bulk_mode(any_tfm(cc));

again please use: bool bulk_mode = ...

OK.

Quoted 85 lines
Quoted 85 lines
drivers/md/dm-crypt.c +57 -12
+     struct bio *bio_in = ctx->bio_in;
+     struct bio *bio_out = ctx->bio_out;
+     unsigned int total_bytes = bio_in->bi_iter.bi_size;
      struct dm_crypt_request *dmreq;
+     struct scatterlist *sg_in;
+     struct scatterlist *sg_out;
      u8 *iv;
      int r;
 
@@ -852,16 +963,6 @@ static int crypt_convert_block(struct crypt_config *cc,
 
      dmreq->iv_sector = ctx->cc_sector;
      dmreq->ctx = ctx;
-     sg_init_table(&dmreq->sg_in, 1);
-     sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
-                 bv_in.bv_offset);
-
-     sg_init_table(&dmreq->sg_out, 1);
-     sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
-                 bv_out.bv_offset);
-
-     bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
-     bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
 
      if (cc->iv_gen_ops) {
              r = cc->iv_gen_ops->generator(cc, iv, dmreq);
@@ -869,8 +970,63 @@ static int crypt_convert_block(struct crypt_config *cc,
                      return r;
      }
 
-     skcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
-                                1 << SECTOR_SHIFT, iv);
+     if (mode && ctx->sgt_in.orig_nents > 0) {
+             struct scatterlist *sg = NULL;
+             unsigned int total_sg_in, total_sg_out;
+
+             total_sg_in = blk_bio_map_sg(bdev_get_queue(bio_in->bi_bdev),
+                                          bio_in, ctx->sgt_in.sgl, &sg);
+             if ((total_sg_in <= 0) ||
+                 (total_sg_in > ctx->sgt_in.orig_nents)) {
+                     DMERR("%s in sg map error %d, sg table nents[%d]\n",
+                           __func__, total_sg_in, ctx->sgt_in.orig_nents);
+                     return -EINVAL;
+             }
+
+             if (sg)
+                     sg_mark_end(sg);
+
+             ctx->iter_in.bi_size -= total_bytes;
+             sg_in = ctx->sgt_in.sgl;
+             sg_out = ctx->sgt_in.sgl;
+
+             if (bio_data_dir(bio_in) == READ)
+                     goto set_crypt;
+
+             sg = NULL;
+             total_sg_out = blk_bio_map_sg(bdev_get_queue(bio_out->bi_bdev),
+                                           bio_out, ctx->sgt_out.sgl, &sg);
+             if ((total_sg_out <= 0) ||
+                 (total_sg_out > ctx->sgt_out.orig_nents)) {
+                     DMERR("%s out sg map error %d, sg table nents[%d]\n",
+                           __func__, total_sg_out, ctx->sgt_out.orig_nents);
+                     return -EINVAL;
+             }
+
+             if (sg)
+                     sg_mark_end(sg);
+
+             ctx->iter_out.bi_size -= total_bytes;
+             sg_out = ctx->sgt_out.sgl;
+     } else  {
+             sg_init_table(&dmreq->sg_in, 1);
+             sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
+                         bv_in.bv_offset);
+
+             sg_init_table(&dmreq->sg_out, 1);
+             sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
+                         bv_out.bv_offset);
+
+             bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
+             bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
+
+             sg_in = &dmreq->sg_in;
+             sg_out = &dmreq->sg_out;
Quoted 8 lines

drivers/md/dm-crypt.c static int crypt_convert_block(struct crypt_config *cc, · line 1029

Quoted 5 lines
drivers/md/dm-crypt.c +5 -0
+             total_bytes = 1 << SECTOR_SHIFT;
+     }
+
+set_crypt:
+     skcipher_request_set_crypt(req, sg_in, sg_out, total_bytes, iv);

Given how long this code has gotten I'd prefer to see this factored out to a new setup method.

I'll refactor this long function. Thanks for your comments.

Quoted 39 lines
Quoted 39 lines
drivers/md/dm-crypt.c +8 -1
      if (bio_data_dir(ctx->bio_in) == WRITE)
              r = crypto_skcipher_encrypt(req);
@@ -1081,6 +1237,8 @@ static void crypt_dec_pending(struct dm_crypt_io *io)
      if (io->ctx.req)
              crypt_free_req(cc, io->ctx.req, base_bio);
 
+     sg_free_table(&io->ctx.sgt_in);
+     sg_free_table(&io->ctx.sgt_out);
      base_bio->bi_error = error;
      bio_endio(base_bio);
 }
@@ -1312,6 +1470,9 @@ static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
      io->ctx.iter_out = clone->bi_iter;
 
      sector += bio_sectors(clone);
+     r = crypt_convert_alloc_table(cc, &io->ctx);
+     if (r < 0)
+             io->error = -EIO;
 
      crypt_inc_pending(io);
      r = crypt_convert(cc, &io->ctx);
@@ -1343,6 +1504,9 @@ static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
 
      crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
                         io->sector);
+     r = crypt_convert_alloc_table(cc, &io->ctx);
+     if (r < 0)
+             io->error = -EIO;
 
      r = crypt_convert(cc, &io->ctx);
      if (r < 0)
--

1.7.9.5

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel

Signature · 2 lines
Baolin.wang
Best Regards