[v5,19/25] powerpc/pseries: Make caller pass buffer to plpks_read_var()

Message ID 20230131063928.388035-20-ajd@linux.ibm.com
State New
Headers
Series pSeries dynamic secure boot secvar interface + platform keyring loading |

Commit Message

Andrew Donnellan Jan. 31, 2023, 6:39 a.m. UTC
  Currently, plpks_read_var() allocates a buffer to pass to the
H_PKS_READ_OBJECT hcall, then allocates another buffer, of the caller's
preferred size if necessary, into which the data is copied, and returns
that buffer to the caller.

This is a bit over the top - while we probably still want to allocate a
separate buffer to pass to the hypervisor in the hcall, we can let the
caller allocate the final buffer and specify the size.

Don't allocate var->data in plpks_read_var(), instead expect the caller to
allocate it. If the caller needs to discover the size, it can set
var->data to NULL and var->datalen will be populated. Update header file
to document this.

Suggested-by: Michael Ellerman <mpe@ellerman.id.au>
Signed-off-by: Andrew Donnellan <ajd@linux.ibm.com>
Signed-off-by: Russell Currey <ruscur@russell.cc>

---

v3: New patch (mpe)
---
 arch/powerpc/include/asm/plpks.h       | 12 ++++++++++++
 arch/powerpc/platforms/pseries/plpks.c | 11 ++++-------
 2 files changed, 16 insertions(+), 7 deletions(-)
  

Comments

Stefan Berger Jan. 31, 2023, 4:38 p.m. UTC | #1
On 1/31/23 01:39, Andrew Donnellan wrote:
> Currently, plpks_read_var() allocates a buffer to pass to the
> H_PKS_READ_OBJECT hcall, then allocates another buffer, of the caller's


-> buffer of the (no comma)

> preferred size if necessary, into which the data is copied, and returns
> that buffer to the caller.
> 
> This is a bit over the top - while we probably still want to allocate a
> separate buffer to pass to the hypervisor in the hcall, we can let the
> caller allocate the final buffer and specify the size.
> 
> Don't allocate var->data in plpks_read_var(), instead expect the caller to
> allocate it. If the caller needs to discover the size, it can set
> var->data to NULL and var->datalen will be populated. Update header file
> to document this.

It looks like there are no callers yet that would need to be adapted...

Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>

> 
> Suggested-by: Michael Ellerman <mpe@ellerman.id.au>
> Signed-off-by: Andrew Donnellan <ajd@linux.ibm.com>
> Signed-off-by: Russell Currey <ruscur@russell.cc>
> 
> ---
> 
> v3: New patch (mpe)
> ---
>   arch/powerpc/include/asm/plpks.h       | 12 ++++++++++++
>   arch/powerpc/platforms/pseries/plpks.c | 11 ++++-------
>   2 files changed, 16 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/powerpc/include/asm/plpks.h b/arch/powerpc/include/asm/plpks.h
> index e7204e6c0ca4..0c49969b0864 100644
> --- a/arch/powerpc/include/asm/plpks.h
> +++ b/arch/powerpc/include/asm/plpks.h
> @@ -88,16 +88,28 @@ int plpks_remove_var(char *component, u8 varos,
>   
>   /**
>    * Returns the data for the specified os variable.
> + *
> + * Caller must allocate a buffer in var->data with length in var->datalen.
> + * If no buffer is provided, var->datalen will be populated with the object's
> + * size.
>    */
>   int plpks_read_os_var(struct plpks_var *var);
>   
>   /**
>    * Returns the data for the specified firmware variable.
> + *
> + * Caller must allocate a buffer in var->data with length in var->datalen.
> + * If no buffer is provided, var->datalen will be populated with the object's
> + * size.
>    */
>   int plpks_read_fw_var(struct plpks_var *var);
>   
>   /**
>    * Returns the data for the specified bootloader variable.
> + *
> + * Caller must allocate a buffer in var->data with length in var->datalen.
> + * If no buffer is provided, var->datalen will be populated with the object's
> + * size.
>    */
>   int plpks_read_bootloader_var(struct plpks_var *var);
>   
> diff --git a/arch/powerpc/platforms/pseries/plpks.c b/arch/powerpc/platforms/pseries/plpks.c
> index e5755443d4a4..926b6a927326 100644
> --- a/arch/powerpc/platforms/pseries/plpks.c
> +++ b/arch/powerpc/platforms/pseries/plpks.c
> @@ -581,17 +581,14 @@ static int plpks_read_var(u8 consumer, struct plpks_var *var)
>   		goto out_free_output;
>   	}
>   
> -	if (var->datalen == 0 || var->datalen > retbuf[0])
> +	if (!var->data || var->datalen > retbuf[0])
>   		var->datalen = retbuf[0];
>   
> -	var->data = kzalloc(var->datalen, GFP_KERNEL);
> -	if (!var->data) {
> -		rc = -ENOMEM;
> -		goto out_free_output;
> -	}
>   	var->policy = retbuf[1];
>   
> -	memcpy(var->data, output, var->datalen);
> +	if (var->data)
> +		memcpy(var->data, output, var->datalen);
> +
>   	rc = 0;
>   
>   out_free_output:
  
Andrew Donnellan Feb. 7, 2023, 5:25 a.m. UTC | #2
On Tue, 2023-01-31 at 11:38 -0500, Stefan Berger wrote:
> 
> 
> On 1/31/23 01:39, Andrew Donnellan wrote:
> > Currently, plpks_read_var() allocates a buffer to pass to the
> > H_PKS_READ_OBJECT hcall, then allocates another buffer, of the
> > caller's
> 
> 
> -> buffer of the (no comma)

I'll just remove that clause entirely, it's not really necessary
  

Patch

diff --git a/arch/powerpc/include/asm/plpks.h b/arch/powerpc/include/asm/plpks.h
index e7204e6c0ca4..0c49969b0864 100644
--- a/arch/powerpc/include/asm/plpks.h
+++ b/arch/powerpc/include/asm/plpks.h
@@ -88,16 +88,28 @@  int plpks_remove_var(char *component, u8 varos,
 
 /**
  * Returns the data for the specified os variable.
+ *
+ * Caller must allocate a buffer in var->data with length in var->datalen.
+ * If no buffer is provided, var->datalen will be populated with the object's
+ * size.
  */
 int plpks_read_os_var(struct plpks_var *var);
 
 /**
  * Returns the data for the specified firmware variable.
+ *
+ * Caller must allocate a buffer in var->data with length in var->datalen.
+ * If no buffer is provided, var->datalen will be populated with the object's
+ * size.
  */
 int plpks_read_fw_var(struct plpks_var *var);
 
 /**
  * Returns the data for the specified bootloader variable.
+ *
+ * Caller must allocate a buffer in var->data with length in var->datalen.
+ * If no buffer is provided, var->datalen will be populated with the object's
+ * size.
  */
 int plpks_read_bootloader_var(struct plpks_var *var);
 
diff --git a/arch/powerpc/platforms/pseries/plpks.c b/arch/powerpc/platforms/pseries/plpks.c
index e5755443d4a4..926b6a927326 100644
--- a/arch/powerpc/platforms/pseries/plpks.c
+++ b/arch/powerpc/platforms/pseries/plpks.c
@@ -581,17 +581,14 @@  static int plpks_read_var(u8 consumer, struct plpks_var *var)
 		goto out_free_output;
 	}
 
-	if (var->datalen == 0 || var->datalen > retbuf[0])
+	if (!var->data || var->datalen > retbuf[0])
 		var->datalen = retbuf[0];
 
-	var->data = kzalloc(var->datalen, GFP_KERNEL);
-	if (!var->data) {
-		rc = -ENOMEM;
-		goto out_free_output;
-	}
 	var->policy = retbuf[1];
 
-	memcpy(var->data, output, var->datalen);
+	if (var->data)
+		memcpy(var->data, output, var->datalen);
+
 	rc = 0;
 
 out_free_output: