Skip to content

Commit 95de56a

Browse files
hfreudehcahca
authored andcommitted
s390/zcrypt: Rework cca findcard() implementation and callers
Rework the memory usage of the cca 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-14-freude@linux.ibm.com Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
1 parent 2845ad7 commit 95de56a

3 files changed

Lines changed: 25 additions & 45 deletions

File tree

drivers/s390/crypto/pkey_cca.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static int cca_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)
@@ -107,7 +107,7 @@ static int cca_apqns4key(const u8 *key, u32 keylen, u32 flags,
107107
/* unknown CCA internal token type */
108108
return -EINVAL;
109109
}
110-
rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
110+
rc = cca_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
111111
minhwtype, AES_MK_SET,
112112
cur_mkvp, old_mkvp);
113113
if (rc)
@@ -126,7 +126,7 @@ static int cca_apqns4key(const u8 *key, u32 keylen, u32 flags,
126126
/* unknown CCA internal 2 token type */
127127
return -EINVAL;
128128
}
129-
rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
129+
rc = cca_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
130130
ZCRYPT_CEX7, APKA_MK_SET,
131131
cur_mkvp, old_mkvp);
132132
if (rc)
@@ -147,7 +147,6 @@ static int cca_apqns4key(const u8 *key, u32 keylen, u32 flags,
147147
*nr_apqns = _nr_apqns;
148148

149149
out:
150-
kfree(_apqns);
151150
pr_debug("rc=%d\n", rc);
152151
return rc;
153152
}
@@ -156,7 +155,7 @@ static int cca_apqns4type(enum pkey_key_type ktype,
156155
u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
157156
struct pkey_apqn *apqns, size_t *nr_apqns)
158157
{
159-
u32 _nr_apqns, *_apqns = NULL;
158+
u32 _apqns[MAXAPQNSINLIST], _nr_apqns = ARRAY_SIZE(_apqns);
160159
int rc;
161160

162161
zcrypt_wait_api_operational();
@@ -171,7 +170,7 @@ static int cca_apqns4type(enum pkey_key_type ktype,
171170
old_mkvp = *((u64 *)alt_mkvp);
172171
if (ktype == PKEY_TYPE_CCA_CIPHER)
173172
minhwtype = ZCRYPT_CEX6;
174-
rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
173+
rc = cca_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
175174
minhwtype, AES_MK_SET,
176175
cur_mkvp, old_mkvp);
177176
if (rc)
@@ -184,7 +183,7 @@ static int cca_apqns4type(enum pkey_key_type ktype,
184183
cur_mkvp = *((u64 *)cur_mkvp);
185184
if (flags & PKEY_FLAGS_MATCH_ALT_MKVP)
186185
old_mkvp = *((u64 *)alt_mkvp);
187-
rc = cca_findcard2(&_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
186+
rc = cca_findcard2(_apqns, &_nr_apqns, 0xFFFF, 0xFFFF,
188187
ZCRYPT_CEX7, APKA_MK_SET,
189188
cur_mkvp, old_mkvp);
190189
if (rc)
@@ -205,7 +204,6 @@ static int cca_apqns4type(enum pkey_key_type ktype,
205204
*nr_apqns = _nr_apqns;
206205

207206
out:
208-
kfree(_apqns);
209207
pr_debug("rc=%d\n", rc);
210208
return rc;
211209
}
@@ -461,7 +459,7 @@ static int cca_verifykey(const u8 *key, u32 keylen,
461459
u32 *keytype, u32 *keybitsize, u32 *flags)
462460
{
463461
struct keytoken_header *hdr = (struct keytoken_header *)key;
464-
u32 nr_apqns, *apqns = NULL;
462+
u32 apqns[MAXAPQNSINLIST], nr_apqns = ARRAY_SIZE(apqns);
465463
int rc;
466464

467465
if (keylen < sizeof(*hdr))
@@ -478,13 +476,14 @@ static int cca_verifykey(const u8 *key, u32 keylen,
478476
goto out;
479477
*keytype = PKEY_TYPE_CCA_DATA;
480478
*keybitsize = t->bitsize;
481-
rc = cca_findcard2(&apqns, &nr_apqns, *card, *dom,
479+
rc = cca_findcard2(apqns, &nr_apqns, *card, *dom,
482480
ZCRYPT_CEX3C, AES_MK_SET,
483481
t->mkvp, 0);
484482
if (!rc)
485483
*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
486484
if (rc == -ENODEV) {
487-
rc = cca_findcard2(&apqns, &nr_apqns, *card, *dom,
485+
nr_apqns = ARRAY_SIZE(apqns);
486+
rc = cca_findcard2(apqns, &nr_apqns, *card, *dom,
488487
ZCRYPT_CEX3C, AES_MK_SET,
489488
0, t->mkvp);
490489
if (!rc)
@@ -511,13 +510,14 @@ static int cca_verifykey(const u8 *key, u32 keylen,
511510
*keybitsize = PKEY_SIZE_AES_192;
512511
else if (!t->plfver && t->wpllen == 640)
513512
*keybitsize = PKEY_SIZE_AES_256;
514-
rc = cca_findcard2(&apqns, &nr_apqns, *card, *dom,
513+
rc = cca_findcard2(apqns, &nr_apqns, *card, *dom,
515514
ZCRYPT_CEX6, AES_MK_SET,
516515
t->mkvp0, 0);
517516
if (!rc)
518517
*flags = PKEY_FLAGS_MATCH_CUR_MKVP;
519518
if (rc == -ENODEV) {
520-
rc = cca_findcard2(&apqns, &nr_apqns, *card, *dom,
519+
nr_apqns = ARRAY_SIZE(apqns);
520+
rc = cca_findcard2(apqns, &nr_apqns, *card, *dom,
521521
ZCRYPT_CEX6, AES_MK_SET,
522522
0, t->mkvp0);
523523
if (!rc)
@@ -535,7 +535,6 @@ static int cca_verifykey(const u8 *key, u32 keylen,
535535
}
536536

537537
out:
538-
kfree(apqns);
539538
pr_debug("rc=%d\n", rc);
540539
return rc;
541540
}

drivers/s390/crypto/zcrypt_ccamisc.c

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,13 +1698,13 @@ int cca_get_info(u16 cardnr, u16 domain, struct cca_info *ci)
16981698
}
16991699
EXPORT_SYMBOL(cca_get_info);
17001700

1701-
int cca_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
1701+
int cca_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
17021702
int minhwtype, int mktype, u64 cur_mkvp, u64 old_mkvp)
17031703
{
17041704
struct zcrypt_device_status_ext *device_status;
1705-
u32 *_apqns = NULL, _nr_apqns = 0;
1706-
int i, card, dom, curmatch, oldmatch, rc;
1705+
int i, card, dom, curmatch, oldmatch;
17071706
struct cca_info ci;
1707+
u32 _nr_apqns = 0;
17081708

17091709
/* occupy the device status memory */
17101710
mutex_lock(&dev_status_mem_mutex);
@@ -1716,13 +1716,6 @@ int cca_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
17161716
ZCRYPT_DEV_STATUS_CARD_MAX,
17171717
ZCRYPT_DEV_STATUS_QUEUE_MAX);
17181718

1719-
/* allocate 1k space for up to 256 apqns */
1720-
_apqns = kmalloc_array(256, sizeof(u32), GFP_KERNEL);
1721-
if (!_apqns) {
1722-
rc = -ENOMEM;
1723-
goto out;
1724-
}
1725-
17261719
/* walk through all the crypto apqnss */
17271720
for (i = 0; i < ZCRYPT_DEV_STATUS_ENTRIES; i++) {
17281721
card = AP_QID_CARD(device_status[i].qid);
@@ -1770,26 +1763,16 @@ int cca_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
17701763
continue;
17711764
}
17721765
/* apqn passed all filtering criterons, add to the array */
1773-
if (_nr_apqns < 256)
1774-
_apqns[_nr_apqns++] = (((u16)card) << 16) | ((u16)dom);
1766+
if (_nr_apqns < *nr_apqns)
1767+
apqns[_nr_apqns++] = (((u16)card) << 16) | ((u16)dom);
17751768
}
17761769

1777-
/* nothing found ? */
1778-
if (!_nr_apqns) {
1779-
kfree(_apqns);
1780-
rc = -ENODEV;
1781-
} else {
1782-
/* no re-allocation, simple return the _apqns array */
1783-
*apqns = _apqns;
1784-
*nr_apqns = _nr_apqns;
1785-
rc = 0;
1786-
}
1770+
*nr_apqns = _nr_apqns;
17871771

1788-
out:
17891772
/* release the device status memory */
17901773
mutex_unlock(&dev_status_mem_mutex);
17911774

1792-
return rc;
1775+
return _nr_apqns ? 0 : -ENODEV;
17931776
}
17941777
EXPORT_SYMBOL(cca_findcard2);
17951778

drivers/s390/crypto/zcrypt_ccamisc.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,12 @@ int cca_query_crypto_facility(u16 cardnr, u16 domain,
217217
* - if old_mkvp != 0 only apqns where old_mkvp == mkvp
218218
* The mktype determines which set of master keys to use:
219219
* 0 = AES_MK_SET - AES MK set, 1 = APKA MK_SET - APKA MK set
220-
* The array of apqn entries is allocated with kmalloc and returned in *apqns;
221-
* the number of apqns stored into the list is returned in *nr_apqns. One apqn
222-
* entry is simple a 32 bit value with 16 bit cardnr and 16 bit domain nr and
223-
* may be casted to struct pkey_apqn. The return value is either 0 for success
224-
* or a negative errno value. If no apqn meeting the criteria is found,
225-
* -ENODEV is returned.
220+
* The caller should set *nr_apqns to the nr of elements available in *apqns.
221+
* On return *nr_apqns is then updated with the nr of apqns filled into *apqns.
222+
* The return value is either 0 for success or a negative errno value.
223+
* If no apqn meeting the criteria is found, -ENODEV is returned.
226224
*/
227-
int cca_findcard2(u32 **apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
225+
int cca_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
228226
int minhwtype, int mktype, u64 cur_mkvp, u64 old_mkvp);
229227

230228
#define AES_MK_SET 0

0 commit comments

Comments
 (0)