Add struct crypto_alg->cra_check_optimized for crc32c_intel
Subsystems (2)
cover Add struct crypto_alg->cra_check_optimized for crc32c_intel
From: Nicholas Bellinger <[email protected]>
Hi Herbert and Co,
This series adds a struct crypto_alg->cra_check_optimized() that can be optionally used by crc32c.ko (and other algorithms) in order to make a request_module() call to load architecture dependent offload code for a libcrypto base algorithm.
This patch is a result of feedback during review for the iscsi_target_mod fabric module here:
Re: [RFC 12/12] iscsi-target: Add Makefile/Kconfig and update TCM
http://marc.info/?l=linux-scsi&m=129974455726437&w=2
Please review and comment,
Thanks!
Nicholas Bellinger (2):
crypto: Add struct crypto_alg->cra_check_optimized crypto/crc32c: Add crc32c_cra_check_optimized for crc32c_intel
crypto/api.c | 6 +++++- crypto/crc32c.c | 23 +++++++++++++++++++++++ include/linux/crypto.h | 1 + 3 files changed, 29 insertions(+), 1 deletions(-)
PATCH 1/2 crypto: Add struct crypto_alg->cra_check_optimized
From: Nicholas Bellinger <[email protected]>
This patch adds an optional struct crypto_alg->cra_check_optimized() caller that can be used by libcrypto algorithms in order to load an architecture dependent optimized / HW offload module.
This includes adding the call to alg->cra_check_optimized() inside of crypto_larval_lookup() once the initial generic algorithm has been loaded via request_module().
--- crypto/api.c | 6 +++++- include/linux/crypto.h | 1 + 2 files changed, 6 insertions(+), 1 deletions(-)
crypto/api.c +5 -1
@@ -226,8 +226,12 @@ struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask) alg =crypto_alg_lookup (name, type, mask); } -if (alg) +if (alg) { +if (alg->cra_check_optimized) + alg->cra_check_optimized (alg); +return crypto_is_larval (alg) ?crypto_larval_wait (alg) : alg; + }return crypto_larval_add (name, type, mask); }
include/linux/crypto.h +1 -0
@@ -295,6 +295,7 @@ struct crypto_alg {int (*cra_init)(struct crypto_tfm *tfm);void (*cra_exit)(struct crypto_tfm *tfm); +void (*cra_check_optimized)(struct crypto_alg *alg);void (*cra_destroy)(struct crypto_alg *alg);struct module *cra_module;
Signature · 1 line
1.5.6.5
PATCH 2/2 crypto/crc32c: Add crc32c_cra_check_optimized for crc32c_intel
In reply to Nicholas A. BellingerFrom: Nicholas Bellinger <[email protected]>
This patch adds crc32c_cra_check_optimized() for use by the new struct crypto_alg->cra_check_optimized() API caller in order to call request_module() to load crc32c_intel.ko for CRC32C instruction offload when available on CONFIG_X86 w/ SSE v4.2 compatiable hardware.
--- crypto/crc32c.c | 23 +++++++++++++++++++++++ 1 files changed, 23 insertions(+), 0 deletions(-)
crypto/crc32c.c +23 -0
@@ -44,6 +44,8 @@#define CHKSUM_BLOCK_SIZE 1#define CHKSUM_DIGEST_SIZE 4 +static int crc32c_offload_enabled; +struct chksum_ctx {u32 key; };
@@ -221,6 +223,24 @@ static int crc32c_cra_init(struct crypto_tfm *tfm)return 0 ; } +static void crc32c_cra_check_optimized (struct crypto_alg *alg) +{ +if (crc32c_offload_enabled) +return ; +#ifdef CONFIG_X86 +/* +* For cpu_has_xmm4_2 go ahead and load crc32c_intel.ko in order so +* crypto_alloc_hash("crc32c", ...) will automatically use SSE v4.2 +* CRC32C instruction offload. +*/ +if (cpu_has_xmm4_2) { +int rc =request_module ("crc32c_intel" ); +if (rc ==0 ) + crc32c_offload_enabled =1 ; + } +#endif +} +static struct shash_alg alg = { .digestsize =CHKSUM_DIGEST_SIZE , .setkey = chksum_setkey,
@@ -239,11 +259,14 @@ static struct shash_alg alg = { .cra_ctxsize = sizeof(struct chksum_ctx), .cra_module = THIS_MODULE, .cra_init = crc32c_cra_init, + .cra_check_optimized = crc32c_cra_check_optimized, } };static int __init crc32c_mod_init (void ) { + crc32c_offload_enabled =0 ; +return crypto_register_shash (&alg); }
Signature · 1 line
1.5.6.5
Quoted 4 lines · Nicholas A. Bellinger
Nicholas Bellinger (2):
crypto: Add structcrypto_alg->cra_check_optimizedcrypto/crc32c: Addcrc32c_cra_check_optimizedforcrc32c_intel
I think you misunderstood my suggestion. The reason I asked for this to be done in the crypto API is because it is not specific to crc32c_intel.
So sending me a patch that modifies crc32c clearly misses the point :)
This should be done in the core API, i.e., api.c/algapi.c.
Thanks,
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
Quoted 6 lines · Herbert Xu
Quoted 4 lines · Nicholas A. Bellinger
Nicholas Bellinger (2):
crypto: Add structcrypto_alg->cra_check_optimizedcrypto/crc32c: Addcrc32c_cra_check_optimizedforcrc32c_intel
Quoted 9 lines
I think you misunderstood my suggestion. The reason I asked for this to be done in the crypto API is because it is not specific to
crc32c_intel.So sending me a patch that modifies crc32c clearly misses the point :)
This should be done in the core API, i.e.,
api.c/algapi.c.
OK, so you mean each struct crypto_alg should define something like a 'cra_optimized_name' for which request_module(alg->cra_optimized_name) is called somewhere in libcrypto code..?
Would you mind being a bit more specific where this should be happening in api.c/algapi.c..?
Thanks,
--nab
Quoted 4 lines · Nicholas A. Bellinger
OK, so you mean each struct
crypto_algshould define something like a 'cra_optimized_name' for whichrequest_module(alg->cra_optimized_name)is called somewhere in libcrypto code..?
No, what I mean is that whenever we look up an algorithm through crypto_alg_mod_lookup, we should conditionally call modprobe if we havn't done so already.
So you just need to record one bit of info in each crypto_alg object to indicate whether we have invoked modprobe. I suggest adding a CRYPTO_ALG_* bit.
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
Quoted 6 lines · Herbert Xu
Quoted 4 lines · Nicholas A. Bellinger
OK, so you mean each struct
crypto_algshould define something like a 'cra_optimized_name' for whichrequest_module(alg->cra_optimized_name)is called somewhere in libcrypto code..?
Quoted 8 lines
No, what I mean is that whenever we look up an algorithm through
crypto_alg_mod_lookup, we should conditionally call modprobe if we havn't done so already.So you just need to record one bit of info in each
crypto_algobject to indicate whether we have invoked modprobe. I suggest adding a CRYPTO_ALG_* bit.
Mmmm, now I am really confused, and please let me apologize in advance for my lack of experience with libcrypto internals.. ;)
I thought the problem was that CONFIG_CRYPTO_ALGFOO=y and CONFIG_CRYPTO_ALGFOO_ARCH_HW_OFFLOAD=m would cause the latter to not explictly call request_module() for this HW offload case..
So what I don't understand how adding a request_module() call to a list of known modules works when crc32c_intel.ko has not been loaded yet..?
Am I missing something obvious wrt to how crc32c.ko can tell libcrypto about which architecture dependent optimized modules it should load..?
Best Regards,
--nab
Quoted 23 lines · Nicholas A. Bellinger
Quoted 6 lines · Herbert Xu
Quoted 4 lines · Nicholas A. Bellinger
OK, so you mean each struct
crypto_algshould define something like a 'cra_optimized_name' for whichrequest_module(alg->cra_optimized_name)is called somewhere in libcrypto code..?Quoted 8 lines
No, what I mean is that whenever we look up an algorithm through
crypto_alg_mod_lookup, we should conditionally call modprobe if we havn't done so already.So you just need to record one bit of info in each
crypto_algobject to indicate whether we have invoked modprobe. I suggest adding a CRYPTO_ALG_* bit.Mmmm, now I am really confused, and please let me apologize in advance for my lack of experience with libcrypto internals.. ;)
I thought the problem was that
CONFIG_CRYPTO_ALGFOO=yandCONFIG_CRYPTO_ALGFOO_ARCH_HW_OFFLOAD=mwould cause the latter to not explictly callrequest_module()for this HW offload case..
Quoted 6 lines
So what I don't understand how adding a
request_module()call to a list of known modules works when crc32c_intel.ko has not been loaded yet..?Am I missing something obvious wrt to how crc32c.ko can tell libcrypto about which architecture dependent optimized modules it should load..?
Just to clarify a bit on my previous comments..
We are still expecting the libcrypto consumer (iscsi_target_mod.ko) to call the arch independent crypto_alloc_hash("crc32c", ...) in order to have libcrypto backend logic perform a request_module() upon architecture dependent offload modules (like crc32c_intel.ko) that libcrypto consumers are not (and should not) be calling directly via crypto_alloc_host("crc32c_intel", ...), correct..?
Where I am getting confused is wrt to a new crypto_alg_mod_lookup() -> request_module() call for a struct shash_alg that has not yet be loaded via arch/x86/crypto/crc32c-intel.c:crc32c_intel_mod_init() -> crypto_register_shash().
Thanks,
--nab
Quoted 7 lines · Nicholas A. Bellinger
We are still expecting the libcrypto consumer (iscsi_target_mod.ko) to call the arch independent
crypto_alloc_hash("crc32c", ...)in order to have libcrypto backend logic perform arequest_module()upon architecture dependent offload modules (like crc32c_intel.ko) that libcrypto consumers are not (and should not) be calling directly viacrypto_alloc_host("crc32c_intel", ...), correct..?
Right.
Quoted 4 lines
Where I am getting confused is wrt to a new
crypto_alg_mod_lookup()->request_module()call for a structshash_algthat has not yet be loaded via arch/x86/crypto/crc32c-intel.c:crc32c_intel_mod_init() ->crypto_register_shash().
If you look at crypto_alg_mod_lookup, basically there are two paths. Either we already have a registered algorithm of the requested name, or we don't.
In the first case, we won't invoke request_module and in the second case we will.
So what I'm suggesting is that in the first case we also invoke request_module conditionally. Now exactly what that condition is is the tricky bit.
The easiest is to flip a bit in the algorithm we just found. This isn't optimal as it'll mean that for each unregistered algorithm we'll end up modprobing twice, but that shouldn't be too bad I think.
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
Quoted 23 lines · Herbert Xu
Quoted 7 lines · Nicholas A. Bellinger
We are still expecting the libcrypto consumer (iscsi_target_mod.ko) to call the arch independent
crypto_alloc_hash("crc32c", ...)in order to have libcrypto backend logic perform arequest_module()upon architecture dependent offload modules (like crc32c_intel.ko) that libcrypto consumers are not (and should not) be calling directly viacrypto_alloc_host("crc32c_intel", ...), correct..?Right.
Quoted 4 lines
Where I am getting confused is wrt to a new
crypto_alg_mod_lookup()->request_module()call for a structshash_algthat has not yet be loaded via arch/x86/crypto/crc32c-intel.c:crc32c_intel_mod_init() ->crypto_register_shash().If you look at
crypto_alg_mod_lookup, basically there are two paths. Either we already have a registered algorithm of the requested name, or we don't.In the first case, we won't invoke
request_moduleand in the second case we will.
Quoted 9 lines
So what I'm suggesting is that in the first case we also invoke
request_moduleconditionally. Now exactly what that condition is is the tricky bit.The easiest is to flip a bit in the algorithm we just found. This isn't optimal as it'll mean that for each unregistered algorithm we'll end up modprobing twice, but that shouldn't be too bad I think.
Hi Herbert,
Thanks for your feedback on this issue, and again my apologies for the lack of experience beyond the external libcrypto API. I will take another look this week and see if something can be produced more along the lines of what you had in mind to resolve this special case. In the mean time the extra crypto_alloc_hash("crc32c-intel", ...) calls have been removed from RFC-v2 iscsi-target code posted earlier this morning.
Please feel free to add any other pointers and I will have a look.
Best Regards,
--nab