Skip to content

Commit 8a88322

Browse files
hfreudehcahca
authored andcommitted
s390/zcrypt: Rework ep11 findcard() implementation and callers
Rework the memory usage of the ep11 findcard() implementation: - findcard does not allocate memory for the list of apqns any more. - the callers are now responsible to provide an array of apqns to store the matching apqns into. 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-15-freude@linux.ibm.com Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
1 parent 95de56a commit 8a88322

3 files changed

Lines changed: 20 additions & 42 deletions

File tree

drivers/s390/crypto/pkey_ep11.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static int ep11_apqns4key(const u8 *key, u32 keylen, u32 flags,
7373
struct pkey_apqn *apqns, size_t *nr_apqns)
7474
{
7575
struct keytoken_header *hdr = (struct keytoken_header *)key;
76-
u32 _nr_apqns, *_apqns = NULL;
76+
u32 _apqns[MAXAPQNSINLIST], _nr_apqns = ARRAY_SIZE(_apqns);
7777
int rc;
7878

7979
if (!flags)
@@ -98,7 +98,7 @@ static int ep11_apqns4key(const u8 *key, u32 keylen, u32 flags,
9898
minhwtype = ZCRYPT_CEX7;
9999
api = ap_is_se_guest() ? EP11_API_V6 : EP11_API_V4;
100100
}
101-
rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
101+
rc = ep11_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
102102
minhwtype, api, kb->wkvp);
103103
if (rc)
104104
goto out;
@@ -115,7 +115,7 @@ static int ep11_apqns4key(const u8 *key, u32 keylen, u32 flags,
115115
minhwtype = ZCRYPT_CEX7;
116116
api = ap_is_se_guest() ? EP11_API_V6 : EP11_API_V4;
117117
}
118-
rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
118+
rc = ep11_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
119119
minhwtype, api, kb->wkvp);
120120
if (rc)
121121
goto out;
@@ -135,7 +135,6 @@ static int ep11_apqns4key(const u8 *key, u32 keylen, u32 flags,
135135
*nr_apqns = _nr_apqns;
136136

137137
out:
138-
kfree(_apqns);
139138
pr_debug("rc=%d\n", rc);
140139
return rc;
141140
}
@@ -144,7 +143,7 @@ static int ep11_apqns4type(enum pkey_key_type ktype,
144143
u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
145144
struct pkey_apqn *apqns, size_t *nr_apqns)
146145
{
147-
u32 _nr_apqns, *_apqns = NULL;
146+
u32 _apqns[MAXAPQNSINLIST], _nr_apqns = ARRAY_SIZE(_apqns);
148147
int rc;
149148

150149
zcrypt_wait_api_operational();
@@ -158,7 +157,7 @@ static int ep11_apqns4type(enum pkey_key_type ktype,
158157
if (flags & PKEY_FLAGS_MATCH_CUR_MKVP)
159158
wkvp = cur_mkvp;
160159
api = ap_is_se_guest() ? EP11_API_V6 : EP11_API_V4;
161-
rc = ep11_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
160+
rc = ep11_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
162161
ZCRYPT_CEX7, api, wkvp);
163162
if (rc)
164163
goto out;
@@ -178,7 +177,6 @@ static int ep11_apqns4type(enum pkey_key_type ktype,
178177
*nr_apqns = _nr_apqns;
179178

180179
out:
181-
kfree(_apqns);
182180
pr_debug("rc=%d\n", rc);
183181
return rc;
184182
}
@@ -423,7 +421,7 @@ static int ep11_verifykey(const u8 *key, u32 keylen,
423421
u32 *keytype, u32 *keybitsize, u32 *flags)
424422
{
425423
struct keytoken_header *hdr = (struct keytoken_header *)key;
426-
u32 nr_apqns, *apqns = NULL;
424+
u32 apqns[MAXAPQNSINLIST], nr_apqns = ARRAY_SIZE(apqns);
427425
int rc;
428426

429427
if (keylen < sizeof(*hdr))
@@ -443,7 +441,7 @@ static int ep11_verifykey(const u8 *key, u32 keylen,
443441
*keybitsize = kb->head.bitlen;
444442

445443
api = ap_is_se_guest() ? EP11_API_V6 : EP11_API_V4;
446-
rc = ep11_findcard2(&apqns, &nr_apqns, *card, *dom,
444+
rc = ep11_findcard2(apqns, &nr_apqns, *card, *dom,
447445
ZCRYPT_CEX7, api,
448446
ep11_kb_wkvp(key, keylen));
449447
if (rc)
@@ -467,7 +465,7 @@ static int ep11_verifykey(const u8 *key, u32 keylen,
467465
*keybitsize = kh->bitlen;
468466

469467
api = ap_is_se_guest() ? EP11_API_V6 : EP11_API_V4;
470-
rc = ep11_findcard2(&apqns, &nr_apqns, *card, *dom,
468+
rc = ep11_findcard2(apqns, &nr_apqns, *card, *dom,
471469
ZCRYPT_CEX7, api,
472470
ep11_kb_wkvp(key, keylen));
473471
if (rc)
@@ -484,7 +482,6 @@ static int ep11_verifykey(const u8 *key, u32 keylen,
484482
}
485483

486484
out:
487-
kfree(apqns);
488485
pr_debug("rc=%d\n", rc);
489486
return rc;
490487
}

drivers/s390/crypto/zcrypt_ep11misc.c

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,14 +1522,14 @@ int ep11_kblob2protkey(u16 card, u16 dom,
15221522
}
15231523
EXPORT_SYMBOL(ep11_kblob2protkey);
15241524

1525-
int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
1525+
int ep11_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
15261526
int minhwtype, int minapi, const u8 *wkvp)
15271527
{
15281528
struct zcrypt_device_status_ext *device_status;
1529-
u32 *_apqns = NULL, _nr_apqns = 0;
1530-
int i, card, dom, rc;
15311529
struct ep11_domain_info edi;
15321530
struct ep11_card_info eci;
1531+
u32 _nr_apqns = 0;
1532+
int i, card, dom;
15331533

15341534
/* occupy the device status memory */
15351535
mutex_lock(&dev_status_mem_mutex);
@@ -1541,13 +1541,6 @@ int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
15411541
ZCRYPT_DEV_STATUS_CARD_MAX,
15421542
ZCRYPT_DEV_STATUS_QUEUE_MAX);
15431543

1544-
/* allocate 1k space for up to 256 apqns */
1545-
_apqns = kmalloc_array(256, sizeof(u32), GFP_KERNEL);
1546-
if (!_apqns) {
1547-
rc = -ENOMEM;
1548-
goto out;
1549-
}
1550-
15511544
/* walk through all the crypto apqnss */
15521545
for (i = 0; i < ZCRYPT_DEV_STATUS_ENTRIES; i++) {
15531546
card = AP_QID_CARD(device_status[i].qid);
@@ -1584,25 +1577,15 @@ int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
15841577
continue;
15851578
}
15861579
/* apqn passed all filtering criterons, add to the array */
1587-
if (_nr_apqns < 256)
1588-
_apqns[_nr_apqns++] = (((u16)card) << 16) | ((u16)dom);
1580+
if (_nr_apqns < *nr_apqns)
1581+
apqns[_nr_apqns++] = (((u16)card) << 16) | ((u16)dom);
15891582
}
15901583

1591-
/* nothing found ? */
1592-
if (!_nr_apqns) {
1593-
kfree(_apqns);
1594-
rc = -ENODEV;
1595-
} else {
1596-
/* no re-allocation, simple return the _apqns array */
1597-
*apqns = _apqns;
1598-
*nr_apqns = _nr_apqns;
1599-
rc = 0;
1600-
}
1584+
*nr_apqns = _nr_apqns;
16011585

1602-
out:
16031586
mutex_unlock(&dev_status_mem_mutex);
16041587

1605-
return rc;
1588+
return _nr_apqns ? 0 : -ENODEV;
16061589
}
16071590
EXPORT_SYMBOL(ep11_findcard2);
16081591

drivers/s390/crypto/zcrypt_ep11misc.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,12 @@ int ep11_clr2keyblob(u16 cardnr, u16 domain, u32 keybitsize, u32 keygenflags,
136136
* key for this domain. When a wkvp is given there will always be a re-fetch
137137
* of the domain info for the potential apqn - so this triggers an request
138138
* reply to each apqn eligible.
139-
* The array of apqn entries is allocated with kmalloc and returned in *apqns;
140-
* the number of apqns stored into the list is returned in *nr_apqns. One apqn
141-
* entry is simple a 32 bit value with 16 bit cardnr and 16 bit domain nr and
142-
* may be casted to struct pkey_apqn. The return value is either 0 for success
143-
* or a negative errno value. If no apqn meeting the criteria is found,
144-
* -ENODEV is returned.
139+
* The caller should set *nr_apqns to the nr of elements available in *apqns.
140+
* On return *nr_apqns is then updated with the nr of apqns filled into *apqns.
141+
* The return value is either 0 for success or a negative errno value.
142+
* If no apqn meeting the criteria is found, -ENODEV is returned.
145143
*/
146-
int ep11_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
144+
int ep11_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
147145
int minhwtype, int minapi, const u8 *wkvp);
148146

149147
/*

0 commit comments

Comments
 (0)