Skip to content

Commit 1bd4793

Browse files
hfreudehcahca
authored andcommitted
s390/pkey: Use preallocated memory for retrieve of UV secret metadata
The pkey uv functions may be called in a situation where memory allocations which trigger IO operations are not allowed. An example: decryption of the swap partition with protected key (PAES). The pkey uv code takes care of this by holding one preallocated struct uv_secret_list to be used with the new UV function uv_find_secret(). The older function uv_get_secret_metadata() used before always allocates/frees an ephemeral memory buffer. The preallocated struct is concurrency protected by a mutex. Signed-off-by: Harald Freudenberger <freude@linux.ibm.com> Reviewed-by: Holger Dengler <dengler@linux.ibm.com> Reviewed-by: Steffen Eiden <seiden@linux.ibm.com> Link: https://lore.kernel.org/r/20250424133619.16495-23-freude@linux.ibm.com Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
1 parent 933dd21 commit 1bd4793

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

drivers/s390/crypto/pkey_uv.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ MODULE_LICENSE("GPL");
2020
MODULE_AUTHOR("IBM Corporation");
2121
MODULE_DESCRIPTION("s390 protected key UV handler");
2222

23+
/*
24+
* One pre-allocated uv_secret_list for use with uv_find_secret()
25+
*/
26+
static struct uv_secret_list *uv_list;
27+
static DEFINE_MUTEX(uv_list_mutex);
28+
2329
/*
2430
* UV secret token struct and defines.
2531
*/
@@ -85,13 +91,26 @@ static bool is_uv_keytype(enum pkey_key_type keytype)
8591
}
8692
}
8793

94+
static int get_secret_metadata(const u8 secret_id[UV_SECRET_ID_LEN],
95+
struct uv_secret_list_item_hdr *secret)
96+
{
97+
int rc;
98+
99+
mutex_lock(&uv_list_mutex);
100+
memset(uv_list, 0, sizeof(*uv_list));
101+
rc = uv_find_secret(secret_id, uv_list, secret);
102+
mutex_unlock(&uv_list_mutex);
103+
104+
return rc;
105+
}
106+
88107
static int retrieve_secret(const u8 secret_id[UV_SECRET_ID_LEN],
89108
u16 *secret_type, u8 *buf, u32 *buflen)
90109
{
91110
struct uv_secret_list_item_hdr secret_meta_data;
92111
int rc;
93112

94-
rc = uv_get_secret_metadata(secret_id, &secret_meta_data);
113+
rc = get_secret_metadata(secret_id, &secret_meta_data);
95114
if (rc)
96115
return rc;
97116

@@ -225,7 +244,7 @@ static int uv_verifykey(const u8 *key, u32 keylen,
225244
if (rc)
226245
goto out;
227246

228-
rc = uv_get_secret_metadata(t->secret_id, &secret_meta_data);
247+
rc = get_secret_metadata(t->secret_id, &secret_meta_data);
229248
if (rc)
230249
goto out;
231250

@@ -263,13 +282,23 @@ static struct pkey_handler uv_handler = {
263282
*/
264283
static int __init pkey_uv_init(void)
265284
{
285+
int rc;
286+
266287
if (!is_prot_virt_guest())
267288
return -ENODEV;
268289

269290
if (!test_bit_inv(BIT_UVC_CMD_RETR_SECRET, uv_info.inst_calls_list))
270291
return -ENODEV;
271292

272-
return pkey_handler_register(&uv_handler);
293+
uv_list = kmalloc(sizeof(*uv_list), GFP_KERNEL);
294+
if (!uv_list)
295+
return -ENOMEM;
296+
297+
rc = pkey_handler_register(&uv_handler);
298+
if (rc)
299+
kfree(uv_list);
300+
301+
return rc;
273302
}
274303

275304
/*
@@ -278,6 +307,9 @@ static int __init pkey_uv_init(void)
278307
static void __exit pkey_uv_exit(void)
279308
{
280309
pkey_handler_unregister(&uv_handler);
310+
mutex_lock(&uv_list_mutex);
311+
kvfree(uv_list);
312+
mutex_unlock(&uv_list_mutex);
281313
}
282314

283315
module_cpu_feature_match(S390_CPU_FEATURE_UV, pkey_uv_init);

0 commit comments

Comments
 (0)