mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Kalra, Ashish" <ashish.kalra@amd.com>
To: Sean Christopherson <seanjc@google.com>
Cc: pbonzini@redhat.com, tglx@linutronix.de, mingo@redhat.com,
	bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org,
	hpa@zytor.com, thomas.lendacky@amd.com, john.allen@amd.com,
	herbert@gondor.apana.org.au, davem@davemloft.net,
	joro@8bytes.org, suravee.suthikulpanit@amd.com, will@kernel.org,
	robin.murphy@arm.com, michael.roth@amd.com,
	dionnaglaze@google.com, nikunj@amd.com, ardb@kernel.org,
	kevinloughlin@google.com, Neeraj.Upadhyay@amd.com,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-crypto@vger.kernel.org, linux-coco@lists.linux.dev,
	iommu@lists.linux.dev
Subject: Re: [PATCH v2 4/4] iommu/amd: Enable Host SNP support after enabling IOMMU SNP support
Date: Fri, 31 Jan 2025 16:53:07 -0600	[thread overview]
Message-ID: <9b338481-39c6-4102-9c4c-19793477242e@amd.com> (raw)
In-Reply-To: <Z5wr5h03oLEA5WBn@google.com>

Hello Sean,

On 1/30/2025 7:48 PM, Sean Christopherson wrote:
> On Fri, Jan 31, 2025, Ashish Kalra wrote:
>> From: Sean Christopherson <seanjc@google.com>
>>
>> This patch fixes the current SNP host enabling code and effectively SNP
>   ^^^^^^^^^^
>> ---
>>  drivers/iommu/amd/init.c | 18 ++++++++++++++----
>>  1 file changed, 14 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c
>> index c5cd92edada0..ee887aa4442f 100644
>> --- a/drivers/iommu/amd/init.c
>> +++ b/drivers/iommu/amd/init.c
>> @@ -3194,7 +3194,7 @@ static bool __init detect_ivrs(void)
>>  	return true;
>>  }
>>  
>> -static void iommu_snp_enable(void)
>> +static __init void iommu_snp_enable(void)
> 
> If you're feeling nitpicky, adding "__init" could be done in a separate patch.
> 
>>  {
>>  #ifdef CONFIG_KVM_AMD_SEV
>>  	if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
>> @@ -3219,6 +3219,11 @@ static void iommu_snp_enable(void)
>>  		goto disable_snp;
>>  	}
>>  
>> +	if (snp_rmptable_init()) {
>> +		pr_warn("SNP: RMP initialization failed, SNP cannot be supported.\n");
>> +		goto disable_snp;
>> +	}
>> +
>>  	pr_info("IOMMU SNP support enabled.\n");
>>  	return;
>>  
>> @@ -3426,18 +3431,23 @@ void __init amd_iommu_detect(void)
>>  	int ret;
>>  
>>  	if (no_iommu || (iommu_detected && !gart_iommu_aperture))
>> -		return;
>> +		goto disable_snp;
>>  
>>  	if (!amd_iommu_sme_check())
>> -		return;
>> +		goto disable_snp;
>>  
>>  	ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
>>  	if (ret)
>> -		return;
>> +		goto disable_snp;
> 
> This handles initial failure, but it won't handle the case where amd_iommu_prepare()
> fails, as the iommu_go_to_state() call from amd_iommu_enable() will get
> short-circuited.  I don't see any pleasant options.  Maybe this?
> 
> diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c
> index c5cd92edada0..436e47f13f8f 100644
> --- a/drivers/iommu/amd/init.c
> +++ b/drivers/iommu/amd/init.c
> @@ -3318,6 +3318,8 @@ static int __init iommu_go_to_state(enum iommu_init_state state)
>                 ret = state_next();
>         }
>  
> +       if (ret && !amd_iommu_snp_en && cc_platform_has(CC_ATTR_HOST_SEV_SNP))
> +               cc_platform_clear(CC_ATTR_HOST_SEV_SNP);
>         return ret;
>  }
>  

This is surely hackish but it will work.

Though to add here, as mentioned in the patch commit logs, IOMMU needs to enabled for SNP_INIT to succeed. 

If IOMMU initialization fails and iommu_snp_enable() is never reached, CC_ATTR_HOST_SEV_SNP will be
left set but that will cause PSP driver's SNP_INIT to fail with invalid configuration error as 
IOMMU SNP sanity checks in SNP firmware will fail (if IOMMUs are not enabled) as below: 

[    9.723114] ccp 0000:23:00.1: sev enabled
[    9.727602] ccp 0000:23:00.1: psp enabled
[    9.732527] ccp 0000:a2:00.1: enabling device (0000 -> 0002)
[    9.739098] ccp 0000:a2:00.1: no command queues available
[    9.745167] ccp 0000:a2:00.1: psp enabled
[    9.805337] ccp 0000:23:00.1: SEV-SNP: failed to INIT rc -5, error 0x3
[    9.866426] ccp 0000:23:00.1: SEV API:1.53 build:5
...
and that will cause CC_ATTR_HOST_SEV_SNP flag to be cleared. 

> 
> Somewhat of a side topic, what happens if the RMP is fully configured and _then_
> IOMMU initialization fails?  I.e. if amd_iommu_init_pci() or amd_iommu_enable_interrupts()
> fails?  Is that even survivable?

Actually, SNP firmware initializes the IOMMU to perform RMP enforcement during SNP_INIT,
but as i mentioned above if IOMMU is not enabled then SNP_INIT will fail. 

Thanks,
Ashish

> 
>>  
>>  	amd_iommu_detected = true;
>>  	iommu_detected = 1;
>>  	x86_init.iommu.iommu_init = amd_iommu_init;
>> +	return;
>> +
>> +disable_snp:
>> +	if (cc_platform_has(CC_ATTR_HOST_SEV_SNP))
>> +		cc_platform_clear(CC_ATTR_HOST_SEV_SNP);
>>  }
>>  
>>  /****************************************************************************
>> -- 
>> 2.34.1
>>


  reply	other threads:[~2025-01-31 22:53 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-31  1:08 [PATCH v2 0/4] Fix broken SNP support with KVM module built-in Ashish Kalra
2025-01-31  1:10 ` [PATCH v2 1/4] crypto: ccp: Add external API interface for PSP module initialization Ashish Kalra
2025-01-31  1:11 ` [PATCH v2 2/4] KVM: SVM: Ensure PSP module is initialized if KVM module is built-in Ashish Kalra
2025-01-31  1:11 ` [PATCH v2 3/4] x86/sev: Fix broken SNP support with KVM module built-in Ashish Kalra
2025-01-31  1:41   ` Sean Christopherson
2025-01-31  3:18     ` Kalra, Ashish
2025-01-31 18:34       ` Sean Christopherson
2025-01-31  1:11 ` [PATCH v2 4/4] iommu/amd: Enable Host SNP support after enabling IOMMU SNP support Ashish Kalra
2025-01-31  1:48   ` Sean Christopherson
2025-01-31 22:53     ` Kalra, Ashish [this message]
2025-02-05  9:46     ` Vasant Hegde
2025-02-05 15:15       ` Sean Christopherson

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=9b338481-39c6-4102-9c4c-19793477242e@amd.com \
    --to=ashish.kalra@amd.com \
    --cc=Neeraj.Upadhyay@amd.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=davem@davemloft.net \
    --cc=dionnaglaze@google.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=hpa@zytor.com \
    --cc=iommu@lists.linux.dev \
    --cc=john.allen@amd.com \
    --cc=joro@8bytes.org \
    --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=robin.murphy@arm.com \
    --cc=seanjc@google.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=will@kernel.org \
    --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

all inboxes | Powered by JetHome®