Skip to content

Commit d2fd25b

Browse files
hfreudehcahca
authored andcommitted
s390/zcrypt: Introduce pre-allocated device status array for ep11 misc
Introduce a pre-allocated device status array memory together with a mutex controlling the occupation to be used by the findcard() 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-11-freude@linux.ibm.com Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
1 parent ef800db commit d2fd25b

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

drivers/s390/crypto/zcrypt_ep11misc.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ static const u8 def_iv[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
3939
#define CPRB_MEMPOOL_ITEM_SIZE (8 * 1024)
4040
static mempool_t *cprb_mempool;
4141

42+
/*
43+
* This is a pre-allocated memory for the device status array
44+
* used within the ep11_findcard2() function. It is currently
45+
* 128 * 128 * 4 bytes = 64 KB big. Usage of this memory is
46+
* controlled via dev_status_mem_mutex. Needs adaption if more
47+
* than 128 cards or domains to be are supported.
48+
*/
49+
#define ZCRYPT_DEV_STATUS_CARD_MAX 128
50+
#define ZCRYPT_DEV_STATUS_QUEUE_MAX 128
51+
#define ZCRYPT_DEV_STATUS_ENTRIES (ZCRYPT_DEV_STATUS_CARD_MAX * \
52+
ZCRYPT_DEV_STATUS_QUEUE_MAX)
53+
#define ZCRYPT_DEV_STATUS_EXT_SIZE (ZCRYPT_DEV_STATUS_ENTRIES * \
54+
sizeof(struct zcrypt_device_status_ext))
55+
static void *dev_status_mem;
56+
static DEFINE_MUTEX(dev_status_mem_mutex);
57+
4258
/* ep11 card info cache */
4359
struct card_list_entry {
4460
struct list_head list;
@@ -1602,16 +1618,15 @@ int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
16021618
struct ep11_domain_info edi;
16031619
struct ep11_card_info eci;
16041620

1605-
/* fetch status of all crypto cards */
1606-
device_status = kvcalloc(MAX_ZDEV_ENTRIES_EXT,
1607-
sizeof(struct zcrypt_device_status_ext),
1608-
GFP_KERNEL);
1609-
if (!device_status)
1610-
return -ENOMEM;
1621+
/* occupy the device status memory */
1622+
mutex_lock(&dev_status_mem_mutex);
1623+
memset(dev_status_mem, 0, ZCRYPT_DEV_STATUS_EXT_SIZE);
1624+
device_status = (struct zcrypt_device_status_ext *)dev_status_mem;
16111625

1626+
/* fetch crypto device status into this struct */
16121627
zcrypt_device_status_mask_ext(device_status,
1613-
MAX_ZDEV_CARDIDS_EXT,
1614-
MAX_ZDEV_DOMAINS_EXT);
1628+
ZCRYPT_DEV_STATUS_CARD_MAX,
1629+
ZCRYPT_DEV_STATUS_QUEUE_MAX);
16151630

16161631
/* allocate 1k space for up to 256 apqns */
16171632
_apqns = kmalloc_array(256, sizeof(u32), GFP_KERNEL);
@@ -1621,7 +1636,7 @@ int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
16211636
}
16221637

16231638
/* walk through all the crypto apqnss */
1624-
for (i = 0; i < MAX_ZDEV_ENTRIES_EXT; i++) {
1639+
for (i = 0; i < ZCRYPT_DEV_STATUS_ENTRIES; i++) {
16251640
card = AP_QID_CARD(device_status[i].qid);
16261641
dom = AP_QID_QUEUE(device_status[i].qid);
16271642
/* check online state */
@@ -1672,7 +1687,8 @@ int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
16721687
}
16731688

16741689
out:
1675-
kvfree(device_status);
1690+
mutex_unlock(&dev_status_mem_mutex);
1691+
16761692
return rc;
16771693
}
16781694
EXPORT_SYMBOL(ep11_findcard2);
@@ -1685,11 +1701,21 @@ int __init zcrypt_ep11misc_init(void)
16851701
if (!cprb_mempool)
16861702
return -ENOMEM;
16871703

1704+
/* Pre-allocate one crypto status card struct used in ep11_findcard2() */
1705+
dev_status_mem = kvmalloc(ZCRYPT_DEV_STATUS_EXT_SIZE, GFP_KERNEL);
1706+
if (!dev_status_mem) {
1707+
mempool_destroy(cprb_mempool);
1708+
return -ENOMEM;
1709+
}
1710+
16881711
return 0;
16891712
}
16901713

16911714
void zcrypt_ep11misc_exit(void)
16921715
{
16931716
card_cache_free();
1717+
mutex_lock(&dev_status_mem_mutex);
1718+
kvfree(dev_status_mem);
1719+
mutex_unlock(&dev_status_mem_mutex);
16941720
mempool_destroy(cprb_mempool);
16951721
}

0 commit comments

Comments
 (0)