Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Harald Freudenberger and committed by
Heiko Carstens
989ed61e 8a883225

+29 -13
+29 -13
drivers/s390/crypto/zcrypt_ccamisc.c
··· 1126 1126 const u8 *clrkey, u8 *keybuf, u32 *keybufsize) 1127 1127 { 1128 1128 int rc; 1129 - u8 *token; 1129 + void *mem; 1130 1130 int tokensize; 1131 - u8 exorbuf[32]; 1131 + u8 *token, exorbuf[32]; 1132 1132 struct cipherkeytoken *t; 1133 + u32 xflags = 0; 1133 1134 1134 1135 /* fill exorbuf with random data */ 1135 1136 get_random_bytes(exorbuf, sizeof(exorbuf)); 1136 1137 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) 1140 1148 return -ENOMEM; 1141 1149 1142 1150 /* prepare the token with the key skeleton */ 1151 + token = (u8 *)mem; 1143 1152 tokensize = SIZEOF_SKELETON; 1144 1153 memcpy(token, aes_cipher_key_skeleton, tokensize); 1145 1154 ··· 1205 1196 *keybufsize = tokensize; 1206 1197 1207 1198 out: 1208 - kfree(token); 1199 + mempool_free(mem, cprb_mempool); 1209 1200 return rc; 1210 1201 } 1211 1202 EXPORT_SYMBOL(cca_clr2cipherkey); ··· 1637 1628 */ 1638 1629 int cca_get_info(u16 cardnr, u16 domain, struct cca_info *ci) 1639 1630 { 1631 + void *mem; 1640 1632 int rc, found = 0; 1641 1633 size_t rlen, vlen; 1642 - u8 *rarray, *varray, *pg; 1634 + u8 *rarray, *varray; 1643 1635 struct zcrypt_device_status_ext devstat; 1636 + u32 xflags = 0; 1644 1637 1645 1638 memset(ci, 0, sizeof(*ci)); 1646 1639 ··· 1652 1641 return rc; 1653 1642 ci->hwtype = devstat.hwtype; 1654 1643 1655 - /* prep page for rule array and var array use */ 1656 - pg = (u8 *)__get_free_page(GFP_KERNEL); 1657 - if (!pg) 1644 + /* 1645 + * Prep memory for rule array and var array use. 1646 + * Use the cprb mempool for this. 1647 + */ 1648 + mem = (xflags & ZCRYPT_XFLAG_NOMEMALLOC) ? 1649 + mempool_alloc_preallocated(cprb_mempool) : 1650 + mempool_alloc(cprb_mempool, GFP_KERNEL); 1651 + if (!mem) 1658 1652 return -ENOMEM; 1659 - rarray = pg; 1660 - varray = pg + PAGE_SIZE / 2; 1653 + rarray = (u8 *)mem; 1654 + varray = (u8 *)mem + PAGE_SIZE / 2; 1661 1655 rlen = vlen = PAGE_SIZE / 2; 1662 1656 1663 1657 /* QF for this card/domain */ ··· 1709 1693 } 1710 1694 1711 1695 out: 1712 - free_page((unsigned long)pg); 1696 + mempool_free(mem, cprb_mempool); 1713 1697 return found == 2 ? 0 : -ENOENT; 1714 1698 } 1715 1699 EXPORT_SYMBOL(cca_get_info);