Skip to content

Commit 6fecab9

Browse files
hfreudehcahca
authored andcommitted
s390/zcrypt: Rework ep11 misc functions to use cprb mempool
There are two places in the ep11 misc code where a short term memory buffer is needed. Rework this code to use the cprb mempool to satisfy this ephemeral memory requirements. 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-19-freude@linux.ibm.com Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
1 parent e9f45ef commit 6fecab9

4 files changed

Lines changed: 76 additions & 49 deletions

File tree

drivers/s390/crypto/pkey_ep11.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ static int ep11_gen_key(const struct pkey_apqn *apqns, size_t nr_apqns,
280280
{
281281
struct pkey_apqn *local_apqns = NULL;
282282
int i, len, rc;
283+
const u32 xflags = 0;
283284

284285
/* check keytype, subtype, keybitsize */
285286
switch (keytype) {
@@ -328,7 +329,7 @@ static int ep11_gen_key(const struct pkey_apqn *apqns, size_t nr_apqns,
328329
for (rc = -ENODEV, i = 0; rc && i < nr_apqns; i++) {
329330
rc = ep11_genaeskey(apqns[i].card, apqns[i].domain,
330331
keybitsize, flags,
331-
keybuf, keybuflen, subtype);
332+
keybuf, keybuflen, subtype, xflags);
332333
}
333334

334335
out:

drivers/s390/crypto/zcrypt_cex4.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static ssize_t ep11_api_ordinalnr_show(struct device *dev,
214214

215215
memset(&ci, 0, sizeof(ci));
216216

217-
ep11_get_card_info(ac->id, &ci);
217+
ep11_get_card_info(ac->id, &ci, 0);
218218

219219
if (ci.API_ord_nr > 0)
220220
return sysfs_emit(buf, "%u\n", ci.API_ord_nr);
@@ -234,7 +234,7 @@ static ssize_t ep11_fw_version_show(struct device *dev,
234234

235235
memset(&ci, 0, sizeof(ci));
236236

237-
ep11_get_card_info(ac->id, &ci);
237+
ep11_get_card_info(ac->id, &ci, 0);
238238

239239
if (ci.FW_version > 0)
240240
return sysfs_emit(buf, "%d.%d\n",
@@ -256,7 +256,7 @@ static ssize_t ep11_serialnr_show(struct device *dev,
256256

257257
memset(&ci, 0, sizeof(ci));
258258

259-
ep11_get_card_info(ac->id, &ci);
259+
ep11_get_card_info(ac->id, &ci, 0);
260260

261261
if (ci.serial[0])
262262
return sysfs_emit(buf, "%16.16s\n", ci.serial);
@@ -293,7 +293,7 @@ static ssize_t ep11_card_op_modes_show(struct device *dev,
293293

294294
memset(&ci, 0, sizeof(ci));
295295

296-
ep11_get_card_info(ac->id, &ci);
296+
ep11_get_card_info(ac->id, &ci, 0);
297297

298298
for (i = 0; ep11_op_modes[i].mode_txt; i++) {
299299
if (ci.op_mode & (1ULL << ep11_op_modes[i].mode_bit)) {
@@ -343,7 +343,7 @@ static ssize_t ep11_mkvps_show(struct device *dev,
343343
if (zq->online)
344344
ep11_get_domain_info(AP_QID_CARD(zq->queue->qid),
345345
AP_QID_QUEUE(zq->queue->qid),
346-
&di);
346+
&di, 0);
347347

348348
if (di.cur_wk_state == '0') {
349349
n = sysfs_emit(buf, "WK CUR: %s -\n",
@@ -390,7 +390,7 @@ static ssize_t ep11_queue_op_modes_show(struct device *dev,
390390
if (zq->online)
391391
ep11_get_domain_info(AP_QID_CARD(zq->queue->qid),
392392
AP_QID_QUEUE(zq->queue->qid),
393-
&di);
393+
&di, 0);
394394

395395
for (i = 0; ep11_op_modes[i].mode_txt; i++) {
396396
if (di.op_mode & (1ULL << ep11_op_modes[i].mode_bit)) {

drivers/s390/crypto/zcrypt_ep11misc.c

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ static int check_reply_cprb(const struct ep11_cprb *rep, const char *func)
549549
* Helper function which does an ep11 query with given query type.
550550
*/
551551
static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
552-
size_t buflen, u8 *buf)
552+
size_t buflen, u8 *buf, u32 xflags)
553553
{
554554
struct ep11_info_req_pl {
555555
struct pl_head head;
@@ -573,7 +573,6 @@ static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
573573
struct ep11_target_dev target;
574574
struct ep11_urb urb;
575575
int api = EP11_API_V1, rc = -ENOMEM;
576-
const u32 xflags = 0;
577576

578577
/* request cprb and payload */
579578
req = alloc_cprbmem(sizeof(struct ep11_info_req_pl), xflags);
@@ -639,7 +638,7 @@ static int ep11_query_info(u16 cardnr, u16 domain, u32 query_type,
639638
/*
640639
* Provide information about an EP11 card.
641640
*/
642-
int ep11_get_card_info(u16 card, struct ep11_card_info *info)
641+
int ep11_get_card_info(u16 card, struct ep11_card_info *info, u32 xflags)
643642
{
644643
int rc;
645644
struct ep11_module_query_info {
@@ -669,12 +668,15 @@ int ep11_get_card_info(u16 card, struct ep11_card_info *info)
669668
u32 max_CP_index;
670669
} __packed * pmqi = NULL;
671670

672-
pmqi = kmalloc(sizeof(*pmqi), GFP_KERNEL);
671+
/* use the cprb mempool to satisfy this short term mem alloc */
672+
pmqi = (xflags & ZCRYPT_XFLAG_NOMEMALLOC) ?
673+
mempool_alloc_preallocated(cprb_mempool) :
674+
mempool_alloc(cprb_mempool, GFP_KERNEL);
673675
if (!pmqi)
674676
return -ENOMEM;
675677
rc = ep11_query_info(card, AUTOSEL_DOM,
676678
0x01 /* module info query */,
677-
sizeof(*pmqi), (u8 *)pmqi);
679+
sizeof(*pmqi), (u8 *)pmqi, xflags);
678680
if (rc)
679681
goto out;
680682

@@ -685,15 +687,16 @@ int ep11_get_card_info(u16 card, struct ep11_card_info *info)
685687
info->op_mode = pmqi->op_mode;
686688

687689
out:
688-
kfree(pmqi);
690+
mempool_free(pmqi, cprb_mempool);
689691
return rc;
690692
}
691693
EXPORT_SYMBOL(ep11_get_card_info);
692694

693695
/*
694696
* Provide information about a domain within an EP11 card.
695697
*/
696-
int ep11_get_domain_info(u16 card, u16 domain, struct ep11_domain_info *info)
698+
int ep11_get_domain_info(u16 card, u16 domain,
699+
struct ep11_domain_info *info, u32 xflags)
697700
{
698701
int rc;
699702
struct ep11_domain_query_info {
@@ -705,7 +708,8 @@ int ep11_get_domain_info(u16 card, u16 domain, struct ep11_domain_info *info)
705708
} __packed dom_query_info;
706709

707710
rc = ep11_query_info(card, domain, 0x03 /* domain info query */,
708-
sizeof(dom_query_info), (u8 *)&dom_query_info);
711+
sizeof(dom_query_info), (u8 *)&dom_query_info,
712+
xflags);
709713
if (rc)
710714
goto out;
711715

@@ -739,7 +743,7 @@ EXPORT_SYMBOL(ep11_get_domain_info);
739743

740744
static int _ep11_genaeskey(u16 card, u16 domain,
741745
u32 keybitsize, u32 keygenflags,
742-
u8 *keybuf, size_t *keybufsize)
746+
u8 *keybuf, size_t *keybufsize, u32 xflags)
743747
{
744748
struct keygen_req_pl {
745749
struct pl_head head;
@@ -777,7 +781,6 @@ static int _ep11_genaeskey(u16 card, u16 domain,
777781
struct ep11_urb urb;
778782
int api, rc = -ENOMEM;
779783
u8 *p;
780-
const u32 xflags = 0;
781784

782785
switch (keybitsize) {
783786
case 128:
@@ -880,7 +883,7 @@ static int _ep11_genaeskey(u16 card, u16 domain,
880883
}
881884

882885
int ep11_genaeskey(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
883-
u8 *keybuf, u32 *keybufsize, u32 keybufver)
886+
u8 *keybuf, u32 *keybufsize, u32 keybufver, u32 xflags)
884887
{
885888
struct ep11kblob_header *hdr;
886889
size_t hdr_size, pl_size;
@@ -901,7 +904,7 @@ int ep11_genaeskey(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
901904
return rc;
902905

903906
rc = _ep11_genaeskey(card, domain, keybitsize, keygenflags,
904-
pl, &pl_size);
907+
pl, &pl_size, xflags);
905908
if (rc)
906909
return rc;
907910

@@ -921,7 +924,8 @@ static int ep11_cryptsingle(u16 card, u16 domain,
921924
u16 mode, u32 mech, const u8 *iv,
922925
const u8 *key, size_t keysize,
923926
const u8 *inbuf, size_t inbufsize,
924-
u8 *outbuf, size_t *outbufsize)
927+
u8 *outbuf, size_t *outbufsize,
928+
u32 xflags)
925929
{
926930
struct crypt_req_pl {
927931
struct pl_head head;
@@ -952,7 +956,6 @@ static int ep11_cryptsingle(u16 card, u16 domain,
952956
size_t req_pl_size, rep_pl_size = 0;
953957
int n, api = EP11_API_V1, rc = -ENOMEM;
954958
u8 *p;
955-
const u32 xflags = 0;
956959

957960
/* the simple asn1 coding used has length limits */
958961
if (keysize > 0xFFFF || inbufsize > 0xFFFF)
@@ -1051,7 +1054,7 @@ static int _ep11_unwrapkey(u16 card, u16 domain,
10511054
const u8 *enckey, size_t enckeysize,
10521055
u32 mech, const u8 *iv,
10531056
u32 keybitsize, u32 keygenflags,
1054-
u8 *keybuf, size_t *keybufsize)
1057+
u8 *keybuf, size_t *keybufsize, u32 xflags)
10551058
{
10561059
struct uw_req_pl {
10571060
struct pl_head head;
@@ -1091,7 +1094,6 @@ static int _ep11_unwrapkey(u16 card, u16 domain,
10911094
struct ep11_urb urb;
10921095
int api, rc = -ENOMEM;
10931096
u8 *p;
1094-
const u32 xflags = 0;
10951097

10961098
/* request cprb and payload */
10971099
api = (!keygenflags || keygenflags & 0x00200000) ?
@@ -1199,7 +1201,7 @@ static int ep11_unwrapkey(u16 card, u16 domain,
11991201
u32 mech, const u8 *iv,
12001202
u32 keybitsize, u32 keygenflags,
12011203
u8 *keybuf, u32 *keybufsize,
1202-
u8 keybufver)
1204+
u8 keybufver, u32 xflags)
12031205
{
12041206
struct ep11kblob_header *hdr;
12051207
size_t hdr_size, pl_size;
@@ -1213,7 +1215,7 @@ static int ep11_unwrapkey(u16 card, u16 domain,
12131215

12141216
rc = _ep11_unwrapkey(card, domain, kek, keksize, enckey, enckeysize,
12151217
mech, iv, keybitsize, keygenflags,
1216-
pl, &pl_size);
1218+
pl, &pl_size, xflags);
12171219
if (rc)
12181220
return rc;
12191221

@@ -1232,7 +1234,7 @@ static int ep11_unwrapkey(u16 card, u16 domain,
12321234
static int _ep11_wrapkey(u16 card, u16 domain,
12331235
const u8 *key, size_t keysize,
12341236
u32 mech, const u8 *iv,
1235-
u8 *databuf, size_t *datasize)
1237+
u8 *databuf, size_t *datasize, u32 xflags)
12361238
{
12371239
struct wk_req_pl {
12381240
struct pl_head head;
@@ -1265,7 +1267,6 @@ static int _ep11_wrapkey(u16 card, u16 domain,
12651267
size_t req_pl_size;
12661268
int api, rc = -ENOMEM;
12671269
u8 *p;
1268-
const u32 xflags = 0;
12691270

12701271
/* request cprb and payload */
12711272
req_pl_size = sizeof(struct wk_req_pl) + (iv ? 16 : 0)
@@ -1355,8 +1356,10 @@ int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
13551356
u32 keytype)
13561357
{
13571358
int rc;
1358-
u8 encbuf[64], *kek = NULL;
1359+
void *mem;
1360+
u8 encbuf[64], *kek;
13591361
size_t clrkeylen, keklen, encbuflen = sizeof(encbuf);
1362+
const u32 xflags = 0;
13601363

13611364
if (keybitsize == 128 || keybitsize == 192 || keybitsize == 256) {
13621365
clrkeylen = keybitsize / 8;
@@ -1366,18 +1369,24 @@ int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
13661369
return -EINVAL;
13671370
}
13681371

1369-
/* allocate memory for the temp kek */
1372+
/*
1373+
* Allocate space for the temp kek.
1374+
* Also we only need up to MAXEP11AESKEYBLOBSIZE bytes for this
1375+
* we use the already existing cprb mempool to solve this
1376+
* short term memory requirement.
1377+
*/
1378+
mem = (xflags & ZCRYPT_XFLAG_NOMEMALLOC) ?
1379+
mempool_alloc_preallocated(cprb_mempool) :
1380+
mempool_alloc(cprb_mempool, GFP_KERNEL);
1381+
if (!mem)
1382+
return -ENOMEM;
1383+
kek = (u8 *)mem;
13701384
keklen = MAXEP11AESKEYBLOBSIZE;
1371-
kek = kmalloc(keklen, GFP_ATOMIC);
1372-
if (!kek) {
1373-
rc = -ENOMEM;
1374-
goto out;
1375-
}
13761385

13771386
/* Step 1: generate AES 256 bit random kek key */
13781387
rc = _ep11_genaeskey(card, domain, 256,
13791388
0x00006c00, /* EN/DECRYPT, WRAP/UNWRAP */
1380-
kek, &keklen);
1389+
kek, &keklen, xflags);
13811390
if (rc) {
13821391
ZCRYPT_DBF_ERR("%s generate kek key failed, rc=%d\n",
13831392
__func__, rc);
@@ -1386,7 +1395,7 @@ int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
13861395

13871396
/* Step 2: encrypt clear key value with the kek key */
13881397
rc = ep11_cryptsingle(card, domain, 0, 0, def_iv, kek, keklen,
1389-
clrkey, clrkeylen, encbuf, &encbuflen);
1398+
clrkey, clrkeylen, encbuf, &encbuflen, xflags);
13901399
if (rc) {
13911400
ZCRYPT_DBF_ERR("%s encrypting key value with kek key failed, rc=%d\n",
13921401
__func__, rc);
@@ -1396,15 +1405,15 @@ int ep11_clr2keyblob(u16 card, u16 domain, u32 keybitsize, u32 keygenflags,
13961405
/* Step 3: import the encrypted key value as a new key */
13971406
rc = ep11_unwrapkey(card, domain, kek, keklen,
13981407
encbuf, encbuflen, 0, def_iv,
1399-
keybitsize, 0, keybuf, keybufsize, keytype);
1408+
keybitsize, 0, keybuf, keybufsize, keytype, xflags);
14001409
if (rc) {
14011410
ZCRYPT_DBF_ERR("%s importing key value as new key failed, rc=%d\n",
14021411
__func__, rc);
14031412
goto out;
14041413
}
14051414

14061415
out:
1407-
kfree(kek);
1416+
mempool_free(mem, cprb_mempool);
14081417
return rc;
14091418
}
14101419
EXPORT_SYMBOL(ep11_clr2keyblob);
@@ -1427,6 +1436,7 @@ int ep11_kblob2protkey(u16 card, u16 dom,
14271436
} __packed * wki;
14281437
u8 *wkbuf = NULL;
14291438
int rc = -EIO;
1439+
const u32 xflags = 0;
14301440

14311441
if (ep11_kb_decode((u8 *)keyblob, keybloblen, &hdr, NULL, &key, &keylen))
14321442
return -EINVAL;
@@ -1437,15 +1447,29 @@ int ep11_kblob2protkey(u16 card, u16 dom,
14371447
}
14381448
/* !!! hdr is no longer a valid header !!! */
14391449

1440-
/* alloc temp working buffer */
1450+
/* need a temp working buffer */
14411451
wkbuflen = (keylen + AES_BLOCK_SIZE) & (~(AES_BLOCK_SIZE - 1));
1442-
wkbuf = kmalloc(wkbuflen, GFP_ATOMIC);
1443-
if (!wkbuf)
1444-
return -ENOMEM;
1452+
if (wkbuflen > CPRB_MEMPOOL_ITEM_SIZE) {
1453+
/* this should never happen */
1454+
rc = -ENOMEM;
1455+
ZCRYPT_DBF_WARN("%s wkbuflen %d > cprb mempool item size %d, rc=%d\n",
1456+
__func__, (int)wkbuflen, CPRB_MEMPOOL_ITEM_SIZE, rc);
1457+
return rc;
1458+
}
1459+
/* use the cprb mempool to satisfy this short term mem allocation */
1460+
wkbuf = (xflags & ZCRYPT_XFLAG_NOMEMALLOC) ?
1461+
mempool_alloc_preallocated(cprb_mempool) :
1462+
mempool_alloc(cprb_mempool, GFP_ATOMIC);
1463+
if (!wkbuf) {
1464+
rc = -ENOMEM;
1465+
ZCRYPT_DBF_WARN("%s allocating tmp buffer via cprb mempool failed, rc=%d\n",
1466+
__func__, rc);
1467+
return rc;
1468+
}
14451469

14461470
/* ep11 secure key -> protected key + info */
14471471
rc = _ep11_wrapkey(card, dom, (u8 *)key, keylen,
1448-
0, def_iv, wkbuf, &wkbuflen);
1472+
0, def_iv, wkbuf, &wkbuflen, xflags);
14491473
if (rc) {
14501474
ZCRYPT_DBF_ERR("%s rewrapping ep11 key to pkey failed, rc=%d\n",
14511475
__func__, rc);
@@ -1512,7 +1536,7 @@ int ep11_kblob2protkey(u16 card, u16 dom,
15121536
*protkeylen = wki->pkeysize;
15131537

15141538
out:
1515-
kfree(wkbuf);
1539+
mempool_free(wkbuf, cprb_mempool);
15161540
return rc;
15171541
}
15181542
EXPORT_SYMBOL(ep11_kblob2protkey);
@@ -1525,6 +1549,7 @@ int ep11_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
15251549
struct ep11_card_info eci;
15261550
u32 _nr_apqns = 0;
15271551
int i, card, dom;
1552+
const u32 xflags = 0;
15281553

15291554
/* occupy the device status memory */
15301555
mutex_lock(&dev_status_mem_mutex);
@@ -1557,14 +1582,14 @@ int ep11_findcard2(u32 *apqns, u32 *nr_apqns, u16 cardnr, u16 domain,
15571582
continue;
15581583
/* check min api version if given */
15591584
if (minapi > 0) {
1560-
if (ep11_get_card_info(card, &eci))
1585+
if (ep11_get_card_info(card, &eci, xflags))
15611586
continue;
15621587
if (minapi > eci.API_ord_nr)
15631588
continue;
15641589
}
15651590
/* check wkvp if given */
15661591
if (wkvp) {
1567-
if (ep11_get_domain_info(card, dom, &edi))
1592+
if (ep11_get_domain_info(card, dom, &edi, xflags))
15681593
continue;
15691594
if (edi.cur_wk_state != '1')
15701595
continue;

0 commit comments

Comments
 (0)