Skip to contentSkip to conversation

crypto: add asynchronous compression support

PATCH In review Weigang Li 4 participants 10 messages Oct 16, 2015 – Nov 20, 2015 5 messages not in the archive Crypto API

lore
Share & tools
lore

Weigang Li

author
#0 lore raw

PATCH 1/1 crypto: add asynchronous compression support

This patch set introduces Asynchronous Compression API.
What is proposed is a new crypto type called crypto_acomp_type, plus new struct acomp_alg and struct crypto_acomp, together with number of helper functions to register acomp type algorithms and allocate tfm instances. This is to make it similar to how the existing crypto API works for the ablkcipher, and akcipher types.
The operations the new interface will provide are:

	int (*compress)(struct acompress_request *req);
	int (*decompress)(struct acompress_request *req);

The benefits it gives interface are:
- the new interface allows for asynchronous implementations and scatterlist buffer that can use hardware to offload the compression operations, the new asynchronous API can be called by the linux kernel components (i.e., btrfs) who want to use hardware acceleration for data compression.

New helper functions have been added to allocate crypto_acomp instances and invoke the operations to make it easier to use.

---
 crypto/Kconfig                      |    5 +
 crypto/Makefile                     |    1 +
 crypto/acompress.c                  |  116 ++++++++++++++++
 crypto/crypto_user.c                |   21 +++
 include/crypto/acompress.h          |  251 +++++++++++++++++++++++++++++++++++
 include/crypto/internal/acompress.h |   62 +++++++++
 include/linux/crypto.h              |    1 +
 include/uapi/linux/cryptouser.h     |    5 +
 8 files changed, 462 insertions(+), 0 deletions(-)
 create mode 100644 crypto/acompress.c
 create mode 100644 include/crypto/acompress.h
 create mode 100644 include/crypto/internal/acompress.h
crypto/Kconfig +5 -0
index 7240821..8b8d2d6 100644
@@ -93,6 +93,10 @@ config CRYPTO_PCOMP2
 	tristate
 	select CRYPTO_ALGAPI2
 
+config CRYPTO_ACOMP2
+        tristate
+        select CRYPTO_ALGAPI2
+
 config CRYPTO_AKCIPHER2
 	tristate
 	select CRYPTO_ALGAPI2
@@ -123,6 +127,7 @@ config CRYPTO_MANAGER2
 	select CRYPTO_HASH2
 	select CRYPTO_BLKCIPHER2
 	select CRYPTO_PCOMP2
+	select CRYPTO_ACOMP2
 	select CRYPTO_AKCIPHER2
 
 config CRYPTO_USER
crypto/Makefile +1 -0
index f7aba92..6ad718f 100644
@@ -29,6 +29,7 @@ crypto_hash-y += shash.o
 obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o
 
 obj-$(CONFIG_CRYPTO_PCOMP2) += pcompress.o
+obj-$(CONFIG_CRYPTO_ACOMP2) += acompress.o
 obj-$(CONFIG_CRYPTO_AKCIPHER2) += akcipher.o
 
 $(obj)/rsapubkey-asn1.o: $(obj)/rsapubkey-asn1.c $(obj)/rsapubkey-asn1.h
crypto/acompress.c new file +116 -0

116 lines not on this page · raw message

crypto/crypto_user.c +21 -0
index d94d99f..f8a3729 100644
@@ -111,6 +111,21 @@ nla_put_failure:
 	return -EMSGSIZE;
 }
 
+static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg)
+{
+	struct crypto_report_acomp racomp;
+
+	strncpy(racomp.type, "acompression", sizeof(racomp.type));
+
+	if (nla_put(skb, CRYPTOCFGA_REPORT_ACOMPRESS,
+		    sizeof(struct crypto_report_acomp), &racomp))
+		goto nla_put_failure;
+	return 0;
+
+nla_put_failure:
+	return -EMSGSIZE;
+}
+
 static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
 {
 	struct crypto_report_akcipher rakcipher;
@@ -171,6 +186,12 @@ static int crypto_report_one(struct crypto_alg *alg,
 
 		break;
 
+	case CRYPTO_ALG_TYPE_ACOMPRESS:
+		if (crypto_report_acomp(skb, alg))
+			goto nla_put_failure;
+
+		break;
+
 	case CRYPTO_ALG_TYPE_AKCIPHER:
 		if (crypto_report_akcipher(skb, alg))
 			goto nla_put_failure;
include/crypto/acompress.h new file +251 -0

251 lines not on this page · raw message

include/crypto/internal/acompress.h new file +62 -0

62 lines not on this page · raw message

include/linux/crypto.h +1 -0
index e71cb70..f051dac 100644
@@ -54,6 +54,7 @@
 #define CRYPTO_ALG_TYPE_AHASH		0x0000000a
 #define CRYPTO_ALG_TYPE_RNG		0x0000000c
 #define CRYPTO_ALG_TYPE_AKCIPHER	0x0000000d
+#define CRYPTO_ALG_TYPE_ACOMPRESS	0x0000000e
 #define CRYPTO_ALG_TYPE_PCOMPRESS	0x0000000f
 
 #define CRYPTO_ALG_TYPE_HASH_MASK	0x0000000e
include/uapi/linux/cryptouser.h +5 -0
index 2e67bb6..7cf256e 100644
@@ -42,6 +42,7 @@ enum crypto_attr_type_t {
 	CRYPTOCFGA_REPORT_BLKCIPHER,	/* struct crypto_report_blkcipher */
 	CRYPTOCFGA_REPORT_AEAD,		/* struct crypto_report_aead */
 	CRYPTOCFGA_REPORT_COMPRESS,	/* struct crypto_report_comp */
+	CRYPTOCFGA_REPORT_ACOMPRESS,	/* struct crypto_report_acomp */
 	CRYPTOCFGA_REPORT_RNG,		/* struct crypto_report_rng */
 	CRYPTOCFGA_REPORT_CIPHER,	/* struct crypto_report_cipher */
 	CRYPTOCFGA_REPORT_AKCIPHER,	/* struct crypto_report_akcipher */
@@ -98,6 +99,10 @@ struct crypto_report_comp {
 	char type[CRYPTO_MAX_NAME];
 };
 
+struct crypto_report_acomp {
+	char type[CRYPTO_MAX_NAME];
+};
+
 struct crypto_report_rng {
 	char type[CRYPTO_MAX_NAME];
 	unsigned int seedsize;
Signature · 1 line
1.7.7.6
maintainer
#1 lore raw
Quoted 18 lines · Weigang Li

This patch set introduces Asynchronous Compression API.
What is proposed is a new crypto type called crypto_acomp_type, plus new struct acomp_alg and struct crypto_acomp, together with number of helper functions to register acomp type algorithms and allocate tfm instances. This is to make it similar to how the existing crypto API works for the ablkcipher, and akcipher types.
The operations the new interface will provide are:

	int (*compress)(struct acompress_request *req);
	int (*decompress)(struct acompress_request *req);

The benefits it gives interface are:
- the new interface allows for asynchronous implementations and scatterlist buffer that can use hardware to offload the compression operations, the new asynchronous API can be called by the linux kernel components (i.e., btrfs) who want to use hardware acceleration for data compression.

Quoted 4 lines

New helper functions have been added to allocate crypto_acomp instances and invoke the operations to make it easier to use.

Thanks for the patch! Joonsoo Kim is also working on the compression interface for zram. Could you two collaborate and come up with one interface rather than two?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

A message that is not in the archive (sent off-list or not yet received): 929511EA6367314D8E32364A24D45FA612FE85D3@shsmsx102.ccr.corp.intel.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 20151019084905.GE963@bbox

A message that is not in the archive (sent off-list or not yet received): 20151021073322.GA31901@swordfish

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw
Quoted 15 lines

-----Original Message-----
From: Herbert Xu [mailto:[email protected]]
Sent: Wednesday, October 21, 2015 3:34 PM
To: Sergey Senozhatsky
Cc: Minchan Kim; Joonsoo Kim; Dan Streetman; Seth Jennings; Li, Weigang; Struk, Tadeusz
Subject: Re: [PATCH] crypto: add asynchronous compression support

Quoted 5 lines · Sergey Senozhatsky

the thing is -- I still want to have/use SW compressors; and they [the existing S/W algorithms] want virtual addresses. so we need to kmap_atomic pages in every SW algorithm? isn't it simpler to keep kmap_atomic_to_page around?

Quoted 8 lines

The Crypto API will do it for you. Have a look at how we handle it for aead and ahash for example.

Cheers,
--
Email: Herbert Xu <[email protected]> Home Page:
http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

Adding the "linux-crypto" list back to the latest discussion.

#8 lore raw
Quoted 18 lines · Weigang
Quoted 18 lines

-----Original Message-----
From: Herbert Xu [mailto:[email protected]]
Sent: Wednesday, October 21, 2015 3:34 PM
To: Sergey Senozhatsky
Cc: Minchan Kim; Joonsoo Kim; Dan Streetman; Seth Jennings; Li, Weigang; Struk, Tadeusz
Subject: Re: [PATCH] crypto: add asynchronous compression support

Quoted 5 lines · Sergey Senozhatsky

the thing is -- I still want to have/use SW compressors; and they [the existing S/W algorithms] want virtual addresses. so we need to kmap_atomic pages in every SW algorithm? isn't it simpler to keep kmap_atomic_to_page around?

The Crypto API will do it for you. Have a look at how we handle it for aead and ahash for example.

Quoted 7 lines
Quoted 5 lines

Cheers,
--
Email: Herbert Xu <[email protected]> Home Page:
http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

Adding the "linux-crypto" list back to the latest discussion.

re: zswap, I suspect it won't be able to use async compression, because when its store function returns the page must be available for reallocation. Of course, async compression can be useful elsewhere.

#9 lore raw
In reply to Herbert Xu

After sync with Joonsoo Kim offline, he agreed to merge this acomp patch with his ccomp patch, thanks Joonsoo!

-----Original Message-----
From: Herbert Xu [mailto:[email protected]]
Sent: Friday, October 16, 2015 11:14 PM
To: Li, Weigang
Cc: [email protected]; Struk, Tadeusz; Joonsoo Kim; Sergey Senozhatsky
Subject: Re: [PATCH] crypto: add asynchronous compression support

Quoted 18 lines · Weigang Li

This patch set introduces Asynchronous Compression API. What is proposed is a new crypto type called crypto_acomp_type, plus new struct acomp_alg and struct crypto_acomp, together with number of helper functions to register acomp type algorithms and allocate tfm instances. This is to make it similar to how the existing crypto API works for the ablkcipher, and akcipher types.
The operations the new interface will provide are:

	int (*compress)(struct acompress_request *req);
	int (*decompress)(struct acompress_request *req);

The benefits it gives interface are:
- the new interface allows for asynchronous implementations and scatterlist buffer that can use hardware to offload the compression operations, the new asynchronous API can be called by the linux kernel components (i.e., btrfs) who want to use hardware acceleration for data compression.

Quoted 4 lines

New helper functions have been added to allocate crypto_acomp instances and invoke the operations to make it easier to use.

Thanks for the patch! Joonsoo Kim is also working on the compression interface for zram. Could you two collaborate and come up with one interface rather than two?

Cheers,
--
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

#10 lore raw
In reply to Herbert Xu
Quoted 22 lines · Herbert Xu
Quoted 21 lines · Weigang Li

This patch set introduces Asynchronous Compression API.
What is proposed is a new crypto type called crypto_acomp_type, plus new struct acomp_alg and struct crypto_acomp, together with number of helper functions to register acomp type algorithms and allocate tfm instances. This is to make it similar to how the existing crypto API works for the ablkcipher, and akcipher types.
The operations the new interface will provide are:

	int (*compress)(struct acompress_request *req);
	int (*decompress)(struct acompress_request *req);

The benefits it gives interface are:
- the new interface allows for asynchronous implementations and scatterlist buffer that can use hardware to offload the compression operations, the new asynchronous API can be called by the linux kernel components (i.e., btrfs) who want to use hardware acceleration for data compression.

New helper functions have been added to allocate crypto_acomp instances and invoke the operations to make it easier to use.

Quoted 8 lines
Quoted 1 line

Thanks for the patch! Joonsoo Kim is also working on the compression interface for zram. Could you two collaborate and come up with one interface rather than two?

Cheers,

Hello Herbert,

After sync-up with Joonsoo Kim, we think it may be not feasible for a s/w implementation of the sg-list based asynchronous interface, we propose separate interfaces (patches) for acomp & ccomp. The reasons are:
1. to support sg-list in the ccomp (like what shash/ahash did), the partial update is required, some algorithms do not support partial update (i.e., lzo), that means:
2. the format of output buffer (sg-list) will be different, e.g., the lzo need contain the "length" info for each block in the output sg-list in order to de-compression, while zlib doesn't need, then it is difficult to have a single async sg-list i/f.
3. to compress a sg-list buffer, the lzo also requires an intermediate buffer to save the output of a block, and copy it back to the sg-list output buffer, it will introduce the complexity and cost, we don't see value for sg-list support in a s/w compression.

Any suggestions?

maintainer
#11 lore raw
Quoted 3 lines · Weigang

After sync-up with Joonsoo Kim, we think it may be not feasible for a s/w implementation of the sg-list based asynchronous interface, we propose separate interfaces (patches) for acomp & ccomp. The reasons are:
1. to support sg-list in the ccomp (like what shash/ahash did), the partial update is required, some algorithms do not support partial update (i.e., lzo), that means:

No this is not true. For the ones that don't support partial updates you can always linearise the input and then feed it in as one chunk. Because the overall interface you're proposing does not allow partial updates the underlying implementation doesn't need to do it either. Only linearisation is necessary.

Quoted 1 line

2. the format of output buffer (sg-list) will be different, e.g., the lzo need contain the "length" info for each block in the output sg-list in order to de-compression, while zlib doesn't need, then it is difficult to have a single async sg-list i/f.

I have no idea what you mean here. Please explain.

Quoted 1 line

3. to compress a sg-list buffer, the lzo also requires an intermediate buffer to save the output of a block, and copy it back to the sg-list output buffer, it will introduce the complexity and cost, we don't see value for sg-list support in a s/w compression.

Such an intermediate buffer would only be needed if the SG list is actually non-linear. So I don't see this as an issue.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#12 lore raw

Hello, Herbert.

Quoted 14 lines

-----Original Message-----
From: Herbert Xu [mailto:[email protected]]
Sent: Thursday, November 19, 2015 6:43 PM
To: Li, Weigang
Cc: [email protected]; Struk, Tadeusz; Joonsoo Kim; Sergey Senozhatsky
Subject: Re: [PATCH] crypto: add asynchronous compression support

Quoted 2 lines · Weigang

After sync-up with Joonsoo Kim, we think it may be not feasible for a

s/w implementation of the sg-list based asynchronous interface, we propose separate interfaces (patches) for acomp & ccomp. The reasons are:

Quoted 1 line

1. to support sg-list in the ccomp (like what shash/ahash did), the

Quoted 8 lines

partial update is required, some algorithms do not support partial update (i.e., lzo), that means:

No this is not true. For the ones that don't support partial updates you can always linearise the input and then feed it in as one chunk. Because the overall interface you're proposing does not allow partial updates the underlying implementation doesn't need to do it either. Only linearisation is necessary.

Linearization would be enough to use sg-list but it has a problem. Linearization needs sleepable function such as vmap() and it makes sync (de)compression in atomic context impossible. Currently, zram did sync (de)compression in atomic context. It uses map_vm_area() which isn't sleepable function to linearize two separate pages. This is possible because zram already knows that maximum number of spread pages is just two and have allocated vm area in advance. But, if we implement linearization in general API, we can't be sure of request input size so we need sleepable function, vmap().

And, this sleep could degrade performance.

Quoted 7 lines
Quoted 1 line

2. the format of output buffer (sg-list) will be different, e.g., the

lzo need contain the "length" info for each block in the output sg-list in order to de-compression, while zlib doesn't need, then it is difficult to have a single async sg-list i/f.

I have no idea what you mean here. Please explain.

Quoted 7 lines
Quoted 1 line

3. to compress a sg-list buffer, the lzo also requires an intermediate

buffer to save the output of a block, and copy it back to the sg-list output buffer, it will introduce the complexity and cost, we don't see value for sg-list support in a s/w compression.

Such an intermediate buffer would only be needed if the SG list is actually non-linear. So I don't see this as an issue.

Intermediate buffer size could vary greatly so it would be allocated and Freed whenever requested. This could affect performance.

I think that supporting unified API has more loss than gain. I'm not expert on this area so please let me know what I missed.

Thanks.

maintainer
#13 lore raw
Quoted 11 lines · Joonsoo Kim

Linearization would be enough to use sg-list but it has a problem. Linearization needs sleepable function such as vmap() and it makes sync (de)compression in atomic context impossible. Currently, zram did sync (de)compression in atomic context. It uses map_vm_area() which isn't sleepable function to linearize two separate pages. This is possible because zram already knows that maximum number of spread pages is just two and have allocated vm area in advance. But, if we implement linearization in general API, we can't be sure of request input size so we need sleepable function, vmap().

Quoted 1 line

And, this sleep could degrade performance.

Obviously you would only perform linearisation where it's needed. And if you are in an atomic context, then clearly linearisation can only be attempted using kmalloc/alloc_pages with GFP_ATOMIC.

I don't understand your concern with zram because either zram is already feeding us linear buffers in which case linearisation is never needed. Or it can only be used with algorithms that support SG lists or partial updates, which we can easily mark using a flag.

Quoted 2 lines

Intermediate buffer size could vary greatly so it would be allocated and Freed whenever requested. This could affect performance.

That's for the crypto user to figure out. Either they should supply a completely linear buffer if they want to be able to support every algorithm in an efficient manner, or they will either have to eat the cost of linearisation or only use algorithms that can deal with SG lists efficiently.

We have the same problem with network drivers and it's dealt with in exactly the same way. An skb can be an SG list and will be linearised when necessary.

Quoted 1 line

I think that supporting unified API has more loss than gain.

I disagree. I have seen no valid reason so far for adding two compression interfaces.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#14 lore raw
Quoted 36 lines · Herbert Xu
Quoted 11 lines · Joonsoo Kim

Linearization would be enough to use sg-list but it has a problem. Linearization needs sleepable function such as vmap() and it makes sync (de)compression in atomic context impossible. Currently, zram did sync (de)compression in atomic context. It uses map_vm_area() which isn't sleepable function to linearize two separate pages. This is possible because zram already knows that maximum number of spread pages is just two and have allocated vm area in advance. But, if we implement linearization in general API, we can't be sure of request input size so we need sleepable function, vmap().

Quoted 1 line

And, this sleep could degrade performance.

Obviously you would only perform linearisation where it's needed. And if you are in an atomic context, then clearly linearisation can only be attempted using kmalloc/alloc_pages with GFP_ATOMIC.

I don't understand your concern with zram because either zram is already feeding us linear buffers in which case linearisation is never needed. Or it can only be used with algorithms that support SG lists or partial updates, which we can easily mark using a flag.

Quoted 2 lines

Intermediate buffer size could vary greatly so it would be allocated and Freed whenever requested. This could affect performance.

That's for the crypto user to figure out. Either they should supply a completely linear buffer if they want to be able to support every algorithm in an efficient manner, or they will either have to eat the cost of linearisation or only use algorithms that can deal with SG lists efficiently.

We have the same problem with network drivers and it's dealt with in exactly the same way. An skb can be an SG list and will be linearised when necessary.

Quoted 7 lines
Quoted 1 line

I think that supporting unified API has more loss than gain.

I disagree. I have seen no valid reason so far for adding two compression interfaces.

Cheers,

Thanks Herbert.

If we assume the sg-list can be linearized - no "holes" in the sg-list, all chunks in middle of the list are of PAGE_SIZE, it seems ok to support sg-list in the s/w implementation, linearize the sg-list and compress it as one chunk.