Re: [PATCH v3 05/21] s390/zcrypt: Introduce cprb mempool for cca misc functions

Heiko Carstens
2025-03-31T11:21:01Z

On Thu, Mar 27, 2025 at 03:39:25PM +0100, Harald Freudenberger wrote:
> Introduce a new module parameter "zcrypt_mempool_threshold"
> for the zcrypt module. This parameter controls the minimal
> amount of mempool items which are pre-allocated for urgent
> requests/replies and will be used with the support for the
> new xflag ZCRYPT_XFLAG_NOMEMALLOC. The default value of 5
> shall provide enough memory items to support up to 5 requests
> (and their associated reply) in parallel.
>
> For CCA each mempool item is 16KB, as a CCA CPRB needs to
> hold the request and the reply. The pool items only support
> requests/replies with a limit of about 8KB.
> So by default the CCA mempool consumes
> 5 * 16KB = 80KB
>
> This is only part of an rework to support a new xflag
> ZCRYPT_XFLAG_NOMEMALLOC but not yet complete.
>
> Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
> Reviewed-by: Holger Dengler <dengler@linux.ibm.com>
> ---
> drivers/s390/crypto/zcrypt_api.c | 13 ++-
> drivers/s390/crypto/zcrypt_api.h | 2 +
> drivers/s390/crypto/zcrypt_ccamisc.c | 117 ++++++++++++++++++++-------
> drivers/s390/crypto/zcrypt_ccamisc.h | 1 +
> 4 files changed, 102 insertions(+), 31 deletions(-)

...

> @@ -229,7 +241,16 @@ static int alloc_and_prep_cprbmem(size_t paramblen,
> * allocate consecutive memory for request CPRB, request param
> * block, reply CPRB and reply param block
> */
> - cprbmem = kcalloc(2, cprbplusparamblen, GFP_KERNEL);
> + if (xflags & ZCRYPT_XFLAG_NOMEMALLOC) {
> + size_t len = 2 * cprbplusparamblen;
> +
> + if (cprb_mempool && len <= CPRB_MEMPOOL_ITEM_SIZE) {
> + cprbmem = mempool_alloc_preallocated(cprb_mempool);
> + memset(cprbmem, 0, len);
> + }
> + } else {
> + cprbmem = kcalloc(2, cprbplusparamblen, GFP_KERNEL);
> + }
> if (!cprbmem)
> return -ENOMEM;

What happens on the ZCRYPT_XFLAG_NOMEMALLOC path if the cprb_mempool
is empty and the allocation fails?

> +int __init zcrypt_ccamisc_init(void)
> +{
> + /* Pre-allocate a small memory pool for cca cprbs. */
> + cprb_mempool = mempool_create_kmalloc_pool(zcrypt_mempool_threshold,
> + CPRB_MEMPOOL_ITEM_SIZE);
> + if (IS_ERR(cprb_mempool)) {
> + ZCRYPT_DBF_ERR("%s mempool_create(%d,%d) failed: %ld\n",
> + __func__, zcrypt_mempool_threshold,
> + CPRB_MEMPOOL_ITEM_SIZE, PTR_ERR(cprb_mempool));
> + cprb_mempool = NULL;
> + return -ENOMEM;
> + }
> + return 0;
> +}

Like before: mempool_create_kmalloc_pool() returns NULL on error. Also
please don't use ZCRYPT_DBF_ERR for such allocation failures. There
will be a very visible message on the console for this case.