public inbox for linux-s390@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/1] Remove the need to alloc memory in uv.c
@ 2025-04-01 13:43 Harald Freudenberger
  2025-04-01 13:43 ` [PATCH v3 1/1] s390/uv: Rename find_secret() to uv_find_secret() and publish Harald Freudenberger
  0 siblings, 1 reply; 3+ messages in thread
From: Harald Freudenberger @ 2025-04-01 13:43 UTC (permalink / raw)
  To: seiden, borntraeger, frankja, imbrenda, hca; +Cc: linux-s390

The pkey uv handler may be called in a do-not-allocate memory
situation where sleeping is allowed but allocating memory which
may cause IO operations is not. For example when an encrypted
swap file is used and the encryption is done via UV retrievable
secrets with protected keys.

The UV API function uv_get_secret_metadata() allocates memory
and then calls the find_secret() function. By exposing the
find_secret() function as a new UV API function uv_find_secret()
it is possible to retrieve UV secret meta data without any
memory allocations from the UV when the caller offers space
for one struct uv_secret_list.

Harald Freudenberger (1):
  s390/uv: Rename find_secret() to uv_find_secret() and publish

 arch/s390/include/asm/uv.h |  3 +++
 arch/s390/kernel/uv.c      | 15 +++++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v3 1/1] s390/uv: Rename find_secret() to uv_find_secret() and publish
  2025-04-01 13:43 [PATCH v3 0/1] Remove the need to alloc memory in uv.c Harald Freudenberger
@ 2025-04-01 13:43 ` Harald Freudenberger
  2025-04-01 14:58   ` Heiko Carstens
  0 siblings, 1 reply; 3+ messages in thread
From: Harald Freudenberger @ 2025-04-01 13:43 UTC (permalink / raw)
  To: seiden, borntraeger, frankja, imbrenda, hca; +Cc: linux-s390

Rename the internal UV function find_secret() to uv_find_secret()
and publish it as new UV API in-kernel function.

The pkey uv handler may be called in a do-not-allocate memory
situation where sleeping is allowed but allocating memory which
may cause IO operations is not. For example when an encrypted
swap file is used and the encryption is done via UV retrievable
secrets with protected keys.

The UV API function uv_get_secret_metadata() allocates memory
and then calls the find_secret() function. By exposing the
find_secret() function as a new UV API function uv_find_secret()
it is possible to retrieve UV secret meta data without any
memory allocations from the UV when the caller offers space
for one struct uv_secret_list.

Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
---
 arch/s390/include/asm/uv.h |  3 +++
 arch/s390/kernel/uv.c      | 15 +++++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/arch/s390/include/asm/uv.h b/arch/s390/include/asm/uv.h
index b11f5b6d0bd1..8d7e00fa2bf0 100644
--- a/arch/s390/include/asm/uv.h
+++ b/arch/s390/include/asm/uv.h
@@ -616,6 +616,9 @@ static inline int uv_remove_shared(unsigned long addr)
 	return share(addr, UVC_CMD_REMOVE_SHARED_ACCESS);
 }
 
+int uv_find_secret(const u8 secret_id[UV_SECRET_ID_LEN],
+		   struct uv_secret_list *list,
+		   struct uv_secret_list_item_hdr *secret);
 int uv_get_secret_metadata(const u8 secret_id[UV_SECRET_ID_LEN],
 			   struct uv_secret_list_item_hdr *secret);
 int uv_retrieve_secret(u16 secret_idx, u8 *buf, size_t buf_size);
diff --git a/arch/s390/kernel/uv.c b/arch/s390/kernel/uv.c
index 9f05df2da2f7..12da637f23f2 100644
--- a/arch/s390/kernel/uv.c
+++ b/arch/s390/kernel/uv.c
@@ -681,17 +681,23 @@ static int find_secret_in_page(const u8 secret_id[UV_SECRET_ID_LEN],
 
 /*
  * Do the actual search for `uv_get_secret_metadata`.
+ * @secret_id: search pattern.
+ * @list: ephemeral buffer space
+ * @secret: output data, containing the secret's metadata.
  *
  * Context: might sleep.
  */
-static int find_secret(const u8 secret_id[UV_SECRET_ID_LEN],
-		       struct uv_secret_list *list,
-		       struct uv_secret_list_item_hdr *secret)
+int uv_find_secret(const u8 secret_id[UV_SECRET_ID_LEN],
+		   struct uv_secret_list *list,
+		   struct uv_secret_list_item_hdr *secret)
 {
 	u16 start_idx = 0;
 	u16 list_rc;
 	int ret;
 
+	if (!list)
+		return -ENOMEM;
+
 	do {
 		uv_list_secrets(list, start_idx, &list_rc, NULL);
 		if (list_rc != UVC_RC_EXECUTED && list_rc != UVC_RC_MORE_DATA) {
@@ -708,6 +714,7 @@ static int find_secret(const u8 secret_id[UV_SECRET_ID_LEN],
 
 	return -ENOENT;
 }
+EXPORT_SYMBOL_GPL(uv_find_secret);
 
 /**
  * uv_get_secret_metadata() - get secret metadata for a given secret id.
@@ -733,7 +740,7 @@ int uv_get_secret_metadata(const u8 secret_id[UV_SECRET_ID_LEN],
 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
 	if (!buf)
 		return -ENOMEM;
-	rc = find_secret(secret_id, buf, secret);
+	rc = uv_find_secret(secret_id, buf, secret);
 	kfree(buf);
 	return rc;
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v3 1/1] s390/uv: Rename find_secret() to uv_find_secret() and publish
  2025-04-01 13:43 ` [PATCH v3 1/1] s390/uv: Rename find_secret() to uv_find_secret() and publish Harald Freudenberger
@ 2025-04-01 14:58   ` Heiko Carstens
  0 siblings, 0 replies; 3+ messages in thread
From: Heiko Carstens @ 2025-04-01 14:58 UTC (permalink / raw)
  To: Harald Freudenberger; +Cc: seiden, borntraeger, frankja, imbrenda, linux-s390

On Tue, Apr 01, 2025 at 03:43:31PM +0200, Harald Freudenberger wrote:
> -static int find_secret(const u8 secret_id[UV_SECRET_ID_LEN],
> -		       struct uv_secret_list *list,
> -		       struct uv_secret_list_item_hdr *secret)
> +int uv_find_secret(const u8 secret_id[UV_SECRET_ID_LEN],
> +		   struct uv_secret_list *list,
> +		   struct uv_secret_list_item_hdr *secret)
>  {
>  	u16 start_idx = 0;
>  	u16 list_rc;
>  	int ret;
>  
> +	if (!list)
> +		return -ENOMEM;
> +

Without this sanity check, please. Callers really should know what
they are doing. Besides that looks good to me.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-04-01 14:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-01 13:43 [PATCH v3 0/1] Remove the need to alloc memory in uv.c Harald Freudenberger
2025-04-01 13:43 ` [PATCH v3 1/1] s390/uv: Rename find_secret() to uv_find_secret() and publish Harald Freudenberger
2025-04-01 14:58   ` Heiko Carstens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox