Skip to content

Commit ef800db

Browse files
hfreudehcahca
authored andcommitted
s390/zcrypt: Introduce pre-allocated device status array for cca misc
Introduce a pre-allocated device status array memory together with a mutex controlling the occupation to be used by the findcard2() function. Limit the device status array to max 128 cards and max 128 domains to reduce the size of this pre-allocated memory to 64 KB. 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-10-freude@linux.ibm.com Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
1 parent a01e748 commit ef800db

1 file changed

Lines changed: 37 additions & 10 deletions

File tree

drivers/s390/crypto/zcrypt_ccamisc.c

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ static DEFINE_SPINLOCK(cca_info_list_lock);
5151
#define CPRB_MEMPOOL_ITEM_SIZE (16 * 1024)
5252
static mempool_t *cprb_mempool;
5353

54+
/*
55+
* This is a pre-allocated memory for the device status array
56+
* used within the findcard() functions. It is currently
57+
* 128 * 128 * 4 bytes = 64 KB big. Usage of this memory is
58+
* controlled via dev_status_mem_mutex. Needs adaption if more
59+
* than 128 cards or domains to be are supported.
60+
*/
61+
#define ZCRYPT_DEV_STATUS_CARD_MAX 128
62+
#define ZCRYPT_DEV_STATUS_QUEUE_MAX 128
63+
#define ZCRYPT_DEV_STATUS_ENTRIES (ZCRYPT_DEV_STATUS_CARD_MAX * \
64+
ZCRYPT_DEV_STATUS_QUEUE_MAX)
65+
#define ZCRYPT_DEV_STATUS_EXT_SIZE (ZCRYPT_DEV_STATUS_ENTRIES * \
66+
sizeof(struct zcrypt_device_status_ext))
67+
static void *dev_status_mem;
68+
static DEFINE_MUTEX(dev_status_mem_mutex);
69+
5470
/*
5571
* Simple check if the token is a valid CCA secure AES data key
5672
* token. If keybitsize is given, the bitsize of the key is
@@ -1919,16 +1935,15 @@ int cca_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
19191935
int i, card, dom, curmatch, oldmatch, rc;
19201936
struct cca_info ci;
19211937

1922-
/* fetch status of all crypto cards */
1923-
device_status = kvcalloc(MAX_ZDEV_ENTRIES_EXT,
1924-
sizeof(struct zcrypt_device_status_ext),
1925-
GFP_KERNEL);
1926-
if (!device_status)
1927-
return -ENOMEM;
1938+
/* occupy the device status memory */
1939+
mutex_lock(&dev_status_mem_mutex);
1940+
memset(dev_status_mem, 0, ZCRYPT_DEV_STATUS_EXT_SIZE);
1941+
device_status = (struct zcrypt_device_status_ext *)dev_status_mem;
19281942

1943+
/* fetch crypto device status into this struct */
19291944
zcrypt_device_status_mask_ext(device_status,
1930-
MAX_ZDEV_CARDIDS_EXT,
1931-
MAX_ZDEV_DOMAINS_EXT);
1945+
ZCRYPT_DEV_STATUS_CARD_MAX,
1946+
ZCRYPT_DEV_STATUS_QUEUE_MAX);
19321947

19331948
/* allocate 1k space for up to 256 apqns */
19341949
_apqns = kmalloc_array(256, sizeof(u32), GFP_KERNEL);
@@ -1938,7 +1953,7 @@ int cca_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
19381953
}
19391954

19401955
/* walk through all the crypto apqnss */
1941-
for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
1956+
for (i = 0; i < ZCRYPT_DEV_STATUS_ENTRIES; i++) {
19421957
card = AP_QID_CARD(device_status[i].qid);
19431958
dom = AP_QID_QUEUE(device_status[i].qid);
19441959
/* check online state */
@@ -2000,7 +2015,9 @@ int cca_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
20002015
}
20012016

20022017
out:
2003-
kvfree(device_status);
2018+
/* release the device status memory */
2019+
mutex_unlock(&dev_status_mem_mutex);
2020+
20042021
return rc;
20052022
}
20062023
EXPORT_SYMBOL(cca_findcard2);
@@ -2013,11 +2030,21 @@ int __init zcrypt_ccamisc_init(void)
20132030
if (!cprb_mempool)
20142031
return -ENOMEM;
20152032

2033+
/* Pre-allocate one crypto status card struct used in findcard() */
2034+
dev_status_mem = kvmalloc(ZCRYPT_DEV_STATUS_EXT_SIZE, GFP_KERNEL);
2035+
if (!dev_status_mem) {
2036+
mempool_destroy(cprb_mempool);
2037+
return -ENOMEM;
2038+
}
2039+
20162040
return 0;
20172041
}
20182042

20192043
void zcrypt_ccamisc_exit(void)
20202044
{
20212045
mkvp_cache_free();
2046+
mutex_lock(&dev_status_mem_mutex);
2047+
kvfree(dev_status_mem);
2048+
mutex_unlock(&dev_status_mem_mutex);
20222049
mempool_destroy(cprb_mempool);
20232050
}

0 commit comments

Comments
 (0)