s390/ap: Introduce ap message buffer pool

There is a need for a do-not-allocate-memory path through the AP bus
layer. The pkey layer may be triggered via the in-kernel interface
from a protected key crypto algorithm (namely PAES) to convert a
secure key into a protected key. This happens in a workqueue context,
so sleeping is allowed but memory allocations causing IO operations
are not permitted.

To accomplish this, an AP message memory pool with pre-allocated space
is established. When ap_init_apmsg() with use_mempool set to true is
called, instead of kmalloc() the ap message buffer is allocated from
the ap_msg_pool. This pool only holds a limited amount of buffers:
ap_msg_pool_min_items with the item size AP_DEFAULT_MAX_MSG_SIZE and
exactly one of these items (if available) is returned if
ap_init_apmsg() with the use_mempool arg set to true is called. When
this pool is exhausted and use_mempool is set true, ap_init_apmsg()
returns -ENOMEM without any attempt to allocate memory and the caller
has to deal with that.

Default values for this mempool of ap messages is:
 * Each buffer is 12KB (that is the default AP bus size
   and all the urgent messages should fit into this space).
 * Minimum items held in the pool is 8. This value is adjustable
   via module parameter ap.msgpool_min_items.

The zcrypt layer may use this flag to indicate to the ap bus that the
processing path for this message should not allocate memory but should
use pre-allocated memory buffer instead.  This is to prevent deadlocks
with crypto and io for example with encrypted swap volumes.

Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
Reviewed-by: Holger Dengler <dengler@linux.ibm.com>
Link: https://lore.kernel.org/r/20250424133619.16495-4-freude@linux.ibm.com
Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
This commit is contained in:
Harald Freudenberger
2025-04-24 15:35:57 +02:00
committed by Heiko Carstens
parent 57db62a130
commit dcc160b3a0
3 changed files with 58 additions and 9 deletions

View File

@@ -648,7 +648,7 @@ static long zcrypt_rsa_modexpo(struct ap_perms *perms,
trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
rc = ap_init_apmsg(&ap_msg);
rc = ap_init_apmsg(&ap_msg, false);
if (rc)
goto out;
@@ -753,7 +753,7 @@ static long zcrypt_rsa_crt(struct ap_perms *perms,
trace_s390_zcrypt_req(crt, TP_ICARSACRT);
rc = ap_init_apmsg(&ap_msg);
rc = ap_init_apmsg(&ap_msg, false);
if (rc)
goto out;
@@ -861,7 +861,7 @@ static long _zcrypt_send_cprb(bool userspace, struct ap_perms *perms,
xcrb->status = 0;
rc = ap_init_apmsg(&ap_msg);
rc = ap_init_apmsg(&ap_msg, false);
if (rc)
goto out;
@@ -1045,7 +1045,7 @@ static long _zcrypt_send_ep11_cprb(bool userspace, struct ap_perms *perms,
trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
rc = ap_init_apmsg(&ap_msg);
rc = ap_init_apmsg(&ap_msg, false);
if (rc)
goto out;
@@ -1219,7 +1219,7 @@ static long zcrypt_rng(char *buffer)
trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
rc = ap_init_apmsg(&ap_msg);
rc = ap_init_apmsg(&ap_msg, false);
if (rc)
goto out;
rc = prep_rng_ap_msg(&ap_msg, &func_code, &domain);