public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Tom Lendacky <thomas.lendacky@amd.com>
To: Ashish Kalra <Ashish.Kalra@amd.com>,
	seanjc@google.com, pbonzini@redhat.com, tglx@linutronix.de,
	mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
	x86@kernel.org, hpa@zytor.com, john.allen@amd.com,
	herbert@gondor.apana.org.au
Cc: michael.roth@amd.com, dionnaglaze@google.com, nikunj@amd.com,
	ardb@kernel.org, kevinloughlin@google.com,
	Neeraj.Upadhyay@amd.com, aik@amd.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-coco@lists.linux.dev
Subject: Re: [PATCH v5 2/7] crypto: ccp: Ensure implicit SEV/SNP init and shutdown in ioctls
Date: Mon, 3 Mar 2025 08:50:56 -0600	[thread overview]
Message-ID: <ba92cd1e-a68f-9d49-ac3d-16368bb1f9df@amd.com> (raw)
In-Reply-To: <1d7b31af0eb36d860907c1e89e553e642f3882e0.1740512583.git.ashish.kalra@amd.com>

On 2/25/25 15:00, Ashish Kalra wrote:
> From: Ashish Kalra <ashish.kalra@amd.com>
> 
> Modify the behavior of implicit SEV initialization in some of the
> SEV ioctls to do both SEV initialization and shutdown and add
> implicit SNP initialization and shutdown to some of the SNP ioctls
> so that the change of SEV/SNP platform initialization not being
> done during PSP driver probe time does not break userspace tools
> such as sevtool, etc.
> 
> Prior to this patch, SEV has always been initialized before these
> ioctls as SEV initialization is done as part of PSP module probe,
> but now with SEV initialization being moved to KVM module load instead
> of PSP driver probe, the implied SEV INIT actually makes sense and gets
> used and additionally to maintain SEV platform state consistency
> before and after the ioctl SEV shutdown needs to be done after the
> firmware call.
> 
> It is important to do SEV Shutdown here with the SEV/SNP initialization
> moving to KVM, an implicit SEV INIT here as part of the SEV ioctls not
> followed with SEV Shutdown will cause SEV to remain in INIT state and
> then a future SNP INIT in KVM module load will fail.
> 
> Similarly, prior to this patch, SNP has always been initialized before
> these ioctls as SNP initialization is done as part of PSP module probe,
> therefore, to keep a consistent behavior, SNP init needs to be done
> here implicitly as part of these ioctls followed with SNP shutdown
> before returning from the ioctl to maintain the consistent platform
> state before and after the ioctl.
> 
> Suggested-by: Tom Lendacky <thomas.lendacky@amd.com>
> Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>
> ---
>  drivers/crypto/ccp/sev-dev.c | 145 +++++++++++++++++++++++++++--------
>  1 file changed, 115 insertions(+), 30 deletions(-)
> 
> diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
> index 8962a0dbc66f..14847f1c05fc 100644
> --- a/drivers/crypto/ccp/sev-dev.c
> +++ b/drivers/crypto/ccp/sev-dev.c
> @@ -1459,28 +1459,38 @@ static int sev_ioctl_do_platform_status(struct sev_issue_cmd *argp)
>  static int sev_ioctl_do_pek_pdh_gen(int cmd, struct sev_issue_cmd *argp, bool writable)
>  {
>  	struct sev_device *sev = psp_master->sev_data;
> -	int rc;
> +	bool shutdown_required = false;
> +	int rc, error;
>  
>  	if (!writable)
>  		return -EPERM;
>  
>  	if (sev->state == SEV_STATE_UNINIT) {
> -		rc = __sev_platform_init_locked(&argp->error);
> -		if (rc)
> +		rc = __sev_platform_init_locked(&error);
> +		if (rc) {
> +			argp->error = SEV_RET_INVALID_PLATFORM_STATE;
>  			return rc;
> +		}
> +		shutdown_required = true;
>  	}

I see this block of code multiple times throughout this patch, both for
SEV and SNP. Can this be consolidated into one or two functions that get
called? Maybe something like:

	rc = sev_move_to_cmd_state(argp, &shutdown_required);

Thanks,
Tom

>  
> -	return __sev_do_cmd_locked(cmd, NULL, &argp->error);
> +	rc = __sev_do_cmd_locked(cmd, NULL, &argp->error);
> +
> +	if (shutdown_required)
> +		__sev_platform_shutdown_locked(&error);
> +
> +	return rc;
>  }
>  
>  static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
>  {
>  	struct sev_device *sev = psp_master->sev_data;
>  	struct sev_user_data_pek_csr input;
> +	bool shutdown_required = false;
>  	struct sev_data_pek_csr data;
>  	void __user *input_address;
>  	void *blob = NULL;
> -	int ret;
> +	int ret, error;
>  
>  	if (!writable)
>  		return -EPERM;
> @@ -1508,9 +1518,12 @@ static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
>  
>  cmd:
>  	if (sev->state == SEV_STATE_UNINIT) {
> -		ret = __sev_platform_init_locked(&argp->error);
> -		if (ret)
> +		ret = __sev_platform_init_locked(&error);
> +		if (ret) {
> +			argp->error = SEV_RET_INVALID_PLATFORM_STATE;
>  			goto e_free_blob;
> +		}
> +		shutdown_required = true;
>  	}
>  
>  	ret = __sev_do_cmd_locked(SEV_CMD_PEK_CSR, &data, &argp->error);
> @@ -1529,6 +1542,9 @@ static int sev_ioctl_do_pek_csr(struct sev_issue_cmd *argp, bool writable)
>  	}
>  
>  e_free_blob:
> +	if (shutdown_required)
> +		__sev_platform_shutdown_locked(&error);
> +
>  	kfree(blob);
>  	return ret;
>  }
> @@ -1746,8 +1762,9 @@ static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
>  	struct sev_device *sev = psp_master->sev_data;
>  	struct sev_user_data_pek_cert_import input;
>  	struct sev_data_pek_cert_import data;
> +	bool shutdown_required = false;
>  	void *pek_blob, *oca_blob;
> -	int ret;
> +	int ret, error;
>  
>  	if (!writable)
>  		return -EPERM;
> @@ -1776,14 +1793,20 @@ static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable)
>  
>  	/* If platform is not in INIT state then transition it to INIT */
>  	if (sev->state != SEV_STATE_INIT) {
> -		ret = __sev_platform_init_locked(&argp->error);
> -		if (ret)
> +		ret = __sev_platform_init_locked(&error);
> +		if (ret) {
> +			argp->error = SEV_RET_INVALID_PLATFORM_STATE;
>  			goto e_free_oca;
> +		}
> +		shutdown_required = true;
>  	}
>  
>  	ret = __sev_do_cmd_locked(SEV_CMD_PEK_CERT_IMPORT, &data, &argp->error);
>  
>  e_free_oca:
> +	if (shutdown_required)
> +		__sev_platform_shutdown_locked(&error);
> +
>  	kfree(oca_blob);
>  e_free_pek:
>  	kfree(pek_blob);
> @@ -1900,17 +1923,8 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
>  	struct sev_data_pdh_cert_export data;
>  	void __user *input_cert_chain_address;
>  	void __user *input_pdh_cert_address;
> -	int ret;
> -
> -	/* If platform is not in INIT state then transition it to INIT. */
> -	if (sev->state != SEV_STATE_INIT) {
> -		if (!writable)
> -			return -EPERM;
> -
> -		ret = __sev_platform_init_locked(&argp->error);
> -		if (ret)
> -			return ret;
> -	}
> +	bool shutdown_required = false;
> +	int ret, error;
>  
>  	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
>  		return -EFAULT;
> @@ -1951,6 +1965,18 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
>  	data.cert_chain_len = input.cert_chain_len;
>  
>  cmd:
> +	/* If platform is not in INIT state then transition it to INIT. */
> +	if (sev->state != SEV_STATE_INIT) {
> +		if (!writable)
> +			goto e_free_cert;
> +		ret = __sev_platform_init_locked(&error);
> +		if (ret) {
> +			argp->error = SEV_RET_INVALID_PLATFORM_STATE;
> +			goto e_free_cert;
> +		}
> +		shutdown_required = true;
> +	}
> +
>  	ret = __sev_do_cmd_locked(SEV_CMD_PDH_CERT_EXPORT, &data, &argp->error);
>  
>  	/* If we query the length, FW responded with expected data. */
> @@ -1977,6 +2003,9 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
>  	}
>  
>  e_free_cert:
> +	if (shutdown_required)
> +		__sev_platform_shutdown_locked(&error);
> +
>  	kfree(cert_blob);
>  e_free_pdh:
>  	kfree(pdh_blob);
> @@ -1986,12 +2015,13 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)
>  static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp)
>  {
>  	struct sev_device *sev = psp_master->sev_data;
> +	bool shutdown_required = false;
>  	struct sev_data_snp_addr buf;
>  	struct page *status_page;
> +	int ret, error;
>  	void *data;
> -	int ret;
>  
> -	if (!sev->snp_initialized || !argp->data)
> +	if (!argp->data)
>  		return -EINVAL;
>  
>  	status_page = alloc_page(GFP_KERNEL_ACCOUNT);
> @@ -2000,6 +2030,15 @@ static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp)
>  
>  	data = page_address(status_page);
>  
> +	if (!sev->snp_initialized) {
> +		ret = __sev_snp_init_locked(&error);
> +		if (ret) {
> +			argp->error = SEV_RET_INVALID_PLATFORM_STATE;
> +			goto cleanup;
> +		}
> +		shutdown_required = true;
> +	}
> +
>  	/*
>  	 * Firmware expects status page to be in firmware-owned state, otherwise
>  	 * it will report firmware error code INVALID_PAGE_STATE (0x1A).
> @@ -2028,6 +2067,9 @@ static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp)
>  		ret = -EFAULT;
>  
>  cleanup:
> +	if (shutdown_required)
> +		__sev_snp_shutdown_locked(&error, false);
> +
>  	__free_pages(status_page, 0);
>  	return ret;
>  }
> @@ -2036,21 +2078,36 @@ static int sev_ioctl_do_snp_commit(struct sev_issue_cmd *argp)
>  {
>  	struct sev_device *sev = psp_master->sev_data;
>  	struct sev_data_snp_commit buf;
> +	bool shutdown_required = false;
> +	int ret, error;
>  
> -	if (!sev->snp_initialized)
> -		return -EINVAL;
> +	if (!sev->snp_initialized) {
> +		ret = __sev_snp_init_locked(&error);
> +		if (ret) {
> +			argp->error = SEV_RET_INVALID_PLATFORM_STATE;
> +			return ret;
> +		}
> +		shutdown_required = true;
> +	}
>  
>  	buf.len = sizeof(buf);
>  
> -	return __sev_do_cmd_locked(SEV_CMD_SNP_COMMIT, &buf, &argp->error);
> +	ret = __sev_do_cmd_locked(SEV_CMD_SNP_COMMIT, &buf, &argp->error);
> +
> +	if (shutdown_required)
> +		__sev_snp_shutdown_locked(&error, false);
> +
> +	return ret;
>  }
>  
>  static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable)
>  {
>  	struct sev_device *sev = psp_master->sev_data;
>  	struct sev_user_data_snp_config config;
> +	bool shutdown_required = false;
> +	int ret, error;
>  
> -	if (!sev->snp_initialized || !argp->data)
> +	if (!argp->data)
>  		return -EINVAL;
>  
>  	if (!writable)
> @@ -2059,17 +2116,32 @@ static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable
>  	if (copy_from_user(&config, (void __user *)argp->data, sizeof(config)))
>  		return -EFAULT;
>  
> -	return __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error);
> +	if (!sev->snp_initialized) {
> +		ret = __sev_snp_init_locked(&error);
> +		if (ret) {
> +			argp->error = SEV_RET_INVALID_PLATFORM_STATE;
> +			return ret;
> +		}
> +		shutdown_required = true;
> +	}
> +
> +	ret = __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error);
> +
> +	if (shutdown_required)
> +		__sev_snp_shutdown_locked(&error, false);
> +
> +	return ret;
>  }
>  
>  static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
>  {
>  	struct sev_device *sev = psp_master->sev_data;
>  	struct sev_user_data_snp_vlek_load input;
> +	bool shutdown_required = false;
> +	int ret, error;
>  	void *blob;
> -	int ret;
>  
> -	if (!sev->snp_initialized || !argp->data)
> +	if (!argp->data)
>  		return -EINVAL;
>  
>  	if (!writable)
> @@ -2088,8 +2160,21 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
>  
>  	input.vlek_wrapped_address = __psp_pa(blob);
>  
> +	if (!sev->snp_initialized) {
> +		ret = __sev_snp_init_locked(&error);
> +		if (ret) {
> +			argp->error = SEV_RET_INVALID_PLATFORM_STATE;
> +			goto cleanup;
> +		}
> +		shutdown_required = true;
> +	}
> +
>  	ret = __sev_do_cmd_locked(SEV_CMD_SNP_VLEK_LOAD, &input, &argp->error);
>  
> +	if (shutdown_required)
> +		__sev_snp_shutdown_locked(&error, false);
> +
> +cleanup:
>  	kfree(blob);
>  
>  	return ret;

  parent reply	other threads:[~2025-03-03 14:51 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-25 20:59 [PATCH v5 0/7] Move initializing SEV/SNP functionality to KVM Ashish Kalra
2025-02-25 20:59 ` [PATCH v5 1/7] crypto: ccp: Move dev_info/err messages for SEV/SNP init and shutdown Ashish Kalra
2025-03-03 14:38   ` Tom Lendacky
2025-02-25 21:00 ` [PATCH v5 2/7] crypto: ccp: Ensure implicit SEV/SNP init and shutdown in ioctls Ashish Kalra
2025-02-27 18:57   ` kernel test robot
2025-03-03 14:50   ` Tom Lendacky [this message]
2025-02-25 21:00 ` [PATCH v5 3/7] crypto: ccp: Reset TMR size at SNP Shutdown Ashish Kalra
2025-02-25 21:00 ` [PATCH v5 4/7] crypto: ccp: Register SNP panic notifier only if SNP is enabled Ashish Kalra
2025-03-03 14:56   ` Tom Lendacky
2025-02-25 21:01 ` [PATCH v5 5/7] crypto: ccp: Add new SEV/SNP platform shutdown API Ashish Kalra
2025-02-25 21:01 ` [PATCH v5 6/7] KVM: SVM: Add support to initialize SEV/SNP functionality in KVM Ashish Kalra
2025-02-28 18:31   ` Sean Christopherson
2025-02-28 20:38     ` Kalra, Ashish
2025-02-28 22:32       ` Sean Christopherson
2025-03-03 18:50         ` Kalra, Ashish
2025-03-03 20:49           ` Sean Christopherson
2025-03-03 21:39             ` Kalra, Ashish
2025-03-04 21:58               ` Sean Christopherson
2025-03-05  1:58                 ` Kalra, Ashish
2025-03-05 22:17                   ` Kalra, Ashish
2025-03-12 23:02                 ` Kalra, Ashish
2025-02-25 21:02 ` [PATCH v5 7/7] crypto: ccp: Move SEV/SNP Platform initialization to KVM Ashish Kalra
2025-03-03 15:35   ` Tom Lendacky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ba92cd1e-a68f-9d49-ac3d-16368bb1f9df@amd.com \
    --to=thomas.lendacky@amd.com \
    --cc=Ashish.Kalra@amd.com \
    --cc=Neeraj.Upadhyay@amd.com \
    --cc=aik@amd.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=dionnaglaze@google.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=hpa@zytor.com \
    --cc=john.allen@amd.com \
    --cc=kevinloughlin@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.roth@amd.com \
    --cc=mingo@redhat.com \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox