Skip to content

Commit 989ed61

Browse files
hfreudehcahca
authored andcommitted
s390/zcrypt: Rework cca misc functions kmallocs to use the cprb mempool
Rework two places in the zcrypt cca misc code using kmalloc() for ephemeral memory allocation. As there is anyway now a cprb mempool let's use this pool instead to satisfy these short term memory allocations. 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-16-freude@linux.ibm.com Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
1 parent 8a88322 commit 989ed61

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

drivers/s390/crypto/zcrypt_ccamisc.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,20 +1126,29 @@ int cca_clr2cipherkey(u16 card, u16 dom, u32 keybitsize, u32 keygenflags,
11261126
const u8 *clrkey, u8 *keybuf, u32 *keybufsize)
11271127
{
11281128
int rc;
1129-
u8 *token;
1129+
void *mem;
11301130
int tokensize;
1131-
u8 exorbuf[32];
1131+
u8 *token, exorbuf[32];
11321132
struct cipherkeytoken *t;
1133+
u32 xflags = 0;
11331134

11341135
/* fill exorbuf with random data */
11351136
get_random_bytes(exorbuf, sizeof(exorbuf));
11361137

1137-
/* allocate space for the key token to build */
1138-
token = kmalloc(MAXCCAVLSCTOKENSIZE, GFP_KERNEL);
1139-
if (!token)
1138+
/*
1139+
* Allocate space for the key token to build.
1140+
* Also we only need up to MAXCCAVLSCTOKENSIZE bytes for this
1141+
* we use the already existing cprb mempool to solve this
1142+
* short term memory requirement.
1143+
*/
1144+
mem = (xflags & ZCRYPT_XFLAG_NOMEMALLOC) ?
1145+
mempool_alloc_preallocated(cprb_mempool) :
1146+
mempool_alloc(cprb_mempool, GFP_KERNEL);
1147+
if (!mem)
11401148
return -ENOMEM;
11411149

11421150
/* prepare the token with the key skeleton */
1151+
token = (u8 *)mem;
11431152
tokensize = SIZEOF_SKELETON;
11441153
memcpy(token, aes_cipher_key_skeleton, tokensize);
11451154

@@ -1196,7 +1205,7 @@ int cca_clr2cipherkey(u16 card, u16 dom, u32 keybitsize, u32 keygenflags,
11961205
*keybufsize = tokensize;
11971206

11981207
out:
1199-
kfree(token);
1208+
mempool_free(mem, cprb_mempool);
12001209
return rc;
12011210
}
12021211
EXPORT_SYMBOL(cca_clr2cipherkey);
@@ -1628,10 +1637,12 @@ EXPORT_SYMBOL(cca_query_crypto_facility);
16281637
*/
16291638
int cca_get_info(u16 cardnr, u16 domain, struct cca_info *ci)
16301639
{
1640+
void *mem;
16311641
int rc, found = 0;
16321642
size_t rlen, vlen;
1633-
u8 *rarray, *varray, *pg;
1643+
u8 *rarray, *varray;
16341644
struct zcrypt_device_status_ext devstat;
1645+
u32 xflags = 0;
16351646

16361647
memset(ci, 0, sizeof(*ci));
16371648

@@ -1641,12 +1652,17 @@ int cca_get_info(u16 cardnr, u16 domain, struct cca_info *ci)
16411652
return rc;
16421653
ci->hwtype = devstat.hwtype;
16431654

1644-
/* prep page for rule array and var array use */
1645-
pg = (u8 *)__get_free_page(GFP_KERNEL);
1646-
if (!pg)
1655+
/*
1656+
* Prep memory for rule array and var array use.
1657+
* Use the cprb mempool for this.
1658+
*/
1659+
mem = (xflags & ZCRYPT_XFLAG_NOMEMALLOC) ?
1660+
mempool_alloc_preallocated(cprb_mempool) :
1661+
mempool_alloc(cprb_mempool, GFP_KERNEL);
1662+
if (!mem)
16471663
return -ENOMEM;
1648-
rarray = pg;
1649-
varray = pg + PAGE_SIZE / 2;
1664+
rarray = (u8 *)mem;
1665+
varray = (u8 *)mem + PAGE_SIZE / 2;
16501666
rlen = vlen = PAGE_SIZE / 2;
16511667

16521668
/* QF for this card/domain */
@@ -1693,7 +1709,7 @@ int cca_get_info(u16 cardnr, u16 domain, struct cca_info *ci)
16931709
}
16941710

16951711
out:
1696-
free_page((unsigned long)pg);
1712+
mempool_free(mem, cprb_mempool);
16971713
return found == 2 ? 0 : -ENOENT;
16981714
}
16991715
EXPORT_SYMBOL(cca_get_info);

0 commit comments

Comments
 (0)