2021-10-28 17:58:43

by Peter Gonda

[permalink] [raw]
Subject: [PATCH 3/4] crypto: ccp - Refactor out sev_fw_alloc()

Creates a helper function sev_fw_alloc() which can be used to allocate
aligned memory regions for use by the PSP firmware. Currently only used
for the SEV-ES TMR region but will be used for the SEV_INIT_EX NV memory
region.

Signed-off-by: Peter Gonda <[email protected]>
Acked-by: David Rientjes <[email protected]>
Cc: Tom Lendacky <[email protected]>
Cc: Brijesh Singh <[email protected]>
Cc: Marc Orr <[email protected]>
Cc: Joerg Roedel <[email protected]>
Cc: Herbert Xu <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: John Allen <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Paolo Bonzini <[email protected]> (
Cc: [email protected]
Cc: [email protected]
---
drivers/crypto/ccp/sev-dev.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
index e4bc833949a0..b568ae734857 100644
--- a/drivers/crypto/ccp/sev-dev.c
+++ b/drivers/crypto/ccp/sev-dev.c
@@ -141,6 +141,21 @@ static int sev_cmd_buffer_len(int cmd)
return 0;
}

+static void *sev_fw_alloc(unsigned long len)
+{
+ const int order = get_order(len);
+ struct page *page;
+
+ if (order > MAX_ORDER-1)
+ return NULL;
+
+ page = alloc_pages(GFP_KERNEL, order);
+ if (!page)
+ return NULL;
+
+ return page_address(page);
+}
+
static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
{
struct psp_device *psp = psp_master;
@@ -1076,7 +1091,6 @@ EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
void sev_pci_init(void)
{
struct sev_device *sev = psp_master->sev_data;
- struct page *tmr_page;
int error = 0, rc;

if (!sev)
@@ -1092,14 +1106,10 @@ void sev_pci_init(void)
sev_get_api_version();

/* Obtain the TMR memory area for SEV-ES use */
- tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE));
- if (tmr_page) {
- sev_es_tmr = page_address(tmr_page);
- } else {
- sev_es_tmr = NULL;
+ sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE);
+ if (!sev_es_tmr)
dev_warn(sev->dev,
"SEV: TMR allocation failed, SEV-ES support unavailable\n");
- }

/* Initialize the platform */
rc = sev_platform_init(&error);
--
2.33.1.1089.g2158813163f-goog


2021-10-29 13:48:54

by Tom Lendacky

[permalink] [raw]
Subject: Re: [PATCH 3/4] crypto: ccp - Refactor out sev_fw_alloc()

On 10/28/21 12:57 PM, Peter Gonda wrote:
> Creates a helper function sev_fw_alloc() which can be used to allocate
> aligned memory regions for use by the PSP firmware. Currently only used
> for the SEV-ES TMR region but will be used for the SEV_INIT_EX NV memory
> region.
>
> Signed-off-by: Peter Gonda <[email protected]>
> Acked-by: David Rientjes <[email protected]>
> Cc: Tom Lendacky <[email protected]>
> Cc: Brijesh Singh <[email protected]>
> Cc: Marc Orr <[email protected]>
> Cc: Joerg Roedel <[email protected]>
> Cc: Herbert Xu <[email protected]>
> Cc: David Rientjes <[email protected]>
> Cc: John Allen <[email protected]>
> Cc: "David S. Miller" <[email protected]>
> Cc: Paolo Bonzini <[email protected]> (
> Cc: [email protected]
> Cc: [email protected]
> ---
> drivers/crypto/ccp/sev-dev.c | 24 +++++++++++++++++-------
> 1 file changed, 17 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
> index e4bc833949a0..b568ae734857 100644
> --- a/drivers/crypto/ccp/sev-dev.c
> +++ b/drivers/crypto/ccp/sev-dev.c
> @@ -141,6 +141,21 @@ static int sev_cmd_buffer_len(int cmd)
> return 0;
> }
>
> +static void *sev_fw_alloc(unsigned long len)
> +{
> + const int order = get_order(len);

This should be an unsigned int to match the function definition, but is
probably not needed given the comment below.

> + struct page *page;
> +
> + if (order > MAX_ORDER-1)
> + return NULL;

I believe alloc_pages() already does this check (and provides a warning
unless requested not to), so this check isn't needed.

> +
> + page = alloc_pages(GFP_KERNEL, order);

Without the above check, you can just replace the 'order' variable with
'get_order(len)'.

Thanks,
Tom

> + if (!page)
> + return NULL;
> +
> + return page_address(page);
> +}
> +
> static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
> {
> struct psp_device *psp = psp_master;
> @@ -1076,7 +1091,6 @@ EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
> void sev_pci_init(void)
> {
> struct sev_device *sev = psp_master->sev_data;
> - struct page *tmr_page;
> int error = 0, rc;
>
> if (!sev)
> @@ -1092,14 +1106,10 @@ void sev_pci_init(void)
> sev_get_api_version();
>
> /* Obtain the TMR memory area for SEV-ES use */
> - tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE));
> - if (tmr_page) {
> - sev_es_tmr = page_address(tmr_page);
> - } else {
> - sev_es_tmr = NULL;
> + sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE);
> + if (!sev_es_tmr)
> dev_warn(sev->dev,
> "SEV: TMR allocation failed, SEV-ES support unavailable\n");
> - }
>
> /* Initialize the platform */
> rc = sev_platform_init(&error);
>

2021-10-29 15:14:44

by Peter Gonda

[permalink] [raw]
Subject: Re: [PATCH 3/4] crypto: ccp - Refactor out sev_fw_alloc()

On Fri, Oct 29, 2021 at 7:48 AM Tom Lendacky <[email protected]> wrote:
>
> On 10/28/21 12:57 PM, Peter Gonda wrote:
> > Creates a helper function sev_fw_alloc() which can be used to allocate
> > aligned memory regions for use by the PSP firmware. Currently only used
> > for the SEV-ES TMR region but will be used for the SEV_INIT_EX NV memory
> > region.
> >
> > Signed-off-by: Peter Gonda <[email protected]>
> > Acked-by: David Rientjes <[email protected]>
> > Cc: Tom Lendacky <[email protected]>
> > Cc: Brijesh Singh <[email protected]>
> > Cc: Marc Orr <[email protected]>
> > Cc: Joerg Roedel <[email protected]>
> > Cc: Herbert Xu <[email protected]>
> > Cc: David Rientjes <[email protected]>
> > Cc: John Allen <[email protected]>
> > Cc: "David S. Miller" <[email protected]>
> > Cc: Paolo Bonzini <[email protected]> (
> > Cc: [email protected]
> > Cc: [email protected]
> > ---
> > drivers/crypto/ccp/sev-dev.c | 24 +++++++++++++++++-------
> > 1 file changed, 17 insertions(+), 7 deletions(-)
> >
> > diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
> > index e4bc833949a0..b568ae734857 100644
> > --- a/drivers/crypto/ccp/sev-dev.c
> > +++ b/drivers/crypto/ccp/sev-dev.c
> > @@ -141,6 +141,21 @@ static int sev_cmd_buffer_len(int cmd)
> > return 0;
> > }
> >
> > +static void *sev_fw_alloc(unsigned long len)
> > +{
> > + const int order = get_order(len);
>
> This should be an unsigned int to match the function definition, but is
> probably not needed given the comment below.
>
> > + struct page *page;
> > +
> > + if (order > MAX_ORDER-1)
> > + return NULL;
>
> I believe alloc_pages() already does this check (and provides a warning
> unless requested not to), so this check isn't needed.

Oh I missed that. Removed.

>
> > +
> > + page = alloc_pages(GFP_KERNEL, order);
>
> Without the above check, you can just replace the 'order' variable with
> 'get_order(len)'.

Moved the get_order() inline here as suggested.

>
> Thanks,
> Tom
>
> > + if (!page)
> > + return NULL;
> > +
> > + return page_address(page);
> > +}
> > +
> > static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
> > {
> > struct psp_device *psp = psp_master;
> > @@ -1076,7 +1091,6 @@ EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
> > void sev_pci_init(void)
> > {
> > struct sev_device *sev = psp_master->sev_data;
> > - struct page *tmr_page;
> > int error = 0, rc;
> >
> > if (!sev)
> > @@ -1092,14 +1106,10 @@ void sev_pci_init(void)
> > sev_get_api_version();
> >
> > /* Obtain the TMR memory area for SEV-ES use */
> > - tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE));
> > - if (tmr_page) {
> > - sev_es_tmr = page_address(tmr_page);
> > - } else {
> > - sev_es_tmr = NULL;
> > + sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE);
> > + if (!sev_es_tmr)
> > dev_warn(sev->dev,
> > "SEV: TMR allocation failed, SEV-ES support unavailable\n");
> > - }
> >
> > /* Initialize the platform */
> > rc = sev_platform_init(&error);
> >

2021-11-01 16:30:04

by Marc Orr

[permalink] [raw]
Subject: Re: [PATCH 3/4] crypto: ccp - Refactor out sev_fw_alloc()

On Thu, Oct 28, 2021 at 10:58 AM Peter Gonda <[email protected]> wrote:
>
> Creates a helper function sev_fw_alloc() which can be used to allocate
> aligned memory regions for use by the PSP firmware. Currently only used
> for the SEV-ES TMR region but will be used for the SEV_INIT_EX NV memory
> region.
>
> Signed-off-by: Peter Gonda <[email protected]>
> Acked-by: David Rientjes <[email protected]>
> Cc: Tom Lendacky <[email protected]>
> Cc: Brijesh Singh <[email protected]>
> Cc: Marc Orr <[email protected]>
> Cc: Joerg Roedel <[email protected]>
> Cc: Herbert Xu <[email protected]>
> Cc: David Rientjes <[email protected]>
> Cc: John Allen <[email protected]>
> Cc: "David S. Miller" <[email protected]>
> Cc: Paolo Bonzini <[email protected]> (
> Cc: [email protected]
> Cc: [email protected]
> ---
> drivers/crypto/ccp/sev-dev.c | 24 +++++++++++++++++-------
> 1 file changed, 17 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
> index e4bc833949a0..b568ae734857 100644
> --- a/drivers/crypto/ccp/sev-dev.c
> +++ b/drivers/crypto/ccp/sev-dev.c
> @@ -141,6 +141,21 @@ static int sev_cmd_buffer_len(int cmd)
> return 0;
> }
>
> +static void *sev_fw_alloc(unsigned long len)
> +{
> + const int order = get_order(len);
> + struct page *page;
> +
> + if (order > MAX_ORDER-1)
> + return NULL;
> +
> + page = alloc_pages(GFP_KERNEL, order);
> + if (!page)
> + return NULL;
> +
> + return page_address(page);
> +}
> +
> static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)
> {
> struct psp_device *psp = psp_master;
> @@ -1076,7 +1091,6 @@ EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user);
> void sev_pci_init(void)
> {
> struct sev_device *sev = psp_master->sev_data;
> - struct page *tmr_page;
> int error = 0, rc;
>
> if (!sev)
> @@ -1092,14 +1106,10 @@ void sev_pci_init(void)
> sev_get_api_version();
>
> /* Obtain the TMR memory area for SEV-ES use */
> - tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE));
> - if (tmr_page) {
> - sev_es_tmr = page_address(tmr_page);
> - } else {
> - sev_es_tmr = NULL;
> + sev_es_tmr = sev_fw_alloc(SEV_ES_TMR_SIZE);
> + if (!sev_es_tmr)
> dev_warn(sev->dev,
> "SEV: TMR allocation failed, SEV-ES support unavailable\n");
> - }
>
> /* Initialize the platform */
> rc = sev_platform_init(&error);
> --
> 2.33.1.1089.g2158813163f-goog
>

Reviewed-by: Marc Orr <[email protected]>