mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tom Lendacky <thomas.lendacky@amd.com>
To: Ashish Kalra <Ashish.Kalra@amd.com>,
	tglx@linutronix.de, mingo@redhat.com, bp@alien8.de,
	dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com,
	seanjc@google.com, pbonzini@redhat.com,
	herbert@gondor.apana.org.au
Cc: nikunj@amd.com, davem@davemloft.net, aik@amd.com,
	ardb@kernel.org, john.allen@amd.com, michael.roth@amd.com,
	Neeraj.Upadhyay@amd.com, linux-kernel@vger.kernel.org,
	kvm@vger.kernel.org, linux-crypto@vger.kernel.org
Subject: Re: [PATCH v4 2/3] crypto: ccp - Add new HV-Fixed page allocation/free API.
Date: Fri, 12 Sep 2025 09:57:37 -0500	[thread overview]
Message-ID: <c36a12f6-33bd-4430-92b2-5fd2939d9b24@amd.com> (raw)
In-Reply-To: <e7807012187bdda8d76ab408b87f15631461993d.1757543774.git.ashish.kalra@amd.com>

On 9/10/25 17:55, Ashish Kalra wrote:
> From: Ashish Kalra <ashish.kalra@amd.com>
> 
> When SEV-SNP is active, the TEE extended command header page and
> all output buffers for TEE extended commands (such as used by Seamless
> Firmware servicing support) must be in hypervisor-fixed state,
> assigned to the hypervisor and marked immutable in the RMP entrie(s).
> 
> Add a new generic SEV API interface to allocate/free hypervisor fixed
> pages which abstracts hypervisor fixed page allocation/free for PSP
> sub devices. The API internally uses SNP_INIT_EX to transition pages
> to HV-Fixed page state.
> 
> If SNP is not enabled then the allocator is simply a wrapper over
> alloc_pages() and __free_pages().
> 
> When the sub device free the pages, they are put on a free list
> and future allocation requests will try to re-use the freed pages from
> this list. But this list is not preserved across PSP driver load/unload
> hence this free/reuse support is only supported while PSP driver is
> loaded. As HV_FIXED page state is only changed at reboot, these pages
> are leaked as they cannot be returned back to the page allocator and
> then potentially allocated to guests, which will cause SEV-SNP guests
> to fail to start or terminate when accessing the HV_FIXED page.
> 
> Suggested-by: Thomas Lendacky <Thomas.Lendacky@amd.com>
> Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>

Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>

> ---
>  drivers/crypto/ccp/sev-dev.c | 182 +++++++++++++++++++++++++++++++++++
>  drivers/crypto/ccp/sev-dev.h |   3 +
>  2 files changed, 185 insertions(+)
> 
> diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
> index 9e797cbdf038..2300673c6683 100644
> --- a/drivers/crypto/ccp/sev-dev.c
> +++ b/drivers/crypto/ccp/sev-dev.c
> @@ -83,6 +83,21 @@ MODULE_FIRMWARE("amd/amd_sev_fam19h_model1xh.sbin"); /* 4th gen EPYC */
>  static bool psp_dead;
>  static int psp_timeout;
>  
> +enum snp_hv_fixed_pages_state {
> +	ALLOCATED,
> +	HV_FIXED,
> +};
> +
> +struct snp_hv_fixed_pages_entry {
> +	struct list_head list;
> +	struct page *page;
> +	unsigned int order;
> +	bool free;
> +	enum snp_hv_fixed_pages_state page_state;
> +};
> +
> +static LIST_HEAD(snp_hv_fixed_pages);
> +
>  /* Trusted Memory Region (TMR):
>   *   The TMR is a 1MB area that must be 1MB aligned.  Use the page allocator
>   *   to allocate the memory, which will return aligned memory for the specified
> @@ -1158,6 +1173,165 @@ static int snp_get_platform_data(struct sev_device *sev, int *error)
>  	return rc;
>  }
>  
> +/* Hypervisor Fixed pages API interface */
> +static void snp_hv_fixed_pages_state_update(struct sev_device *sev,
> +					    enum snp_hv_fixed_pages_state page_state)
> +{
> +	struct snp_hv_fixed_pages_entry *entry;
> +
> +	/* List is protected by sev_cmd_mutex */
> +	lockdep_assert_held(&sev_cmd_mutex);
> +
> +	if (list_empty(&snp_hv_fixed_pages))
> +		return;
> +
> +	list_for_each_entry(entry, &snp_hv_fixed_pages, list)
> +		entry->page_state = page_state;
> +}
> +
> +/*
> + * Allocate HV_FIXED pages in 2MB aligned sizes to ensure the whole
> + * 2MB pages are marked as HV_FIXED.
> + */
> +struct page *snp_alloc_hv_fixed_pages(unsigned int num_2mb_pages)
> +{
> +	struct psp_device *psp_master = psp_get_master_device();
> +	struct snp_hv_fixed_pages_entry *entry;
> +	struct sev_device *sev;
> +	unsigned int order;
> +	struct page *page;
> +
> +	if (!psp_master || !psp_master->sev_data)
> +		return NULL;
> +
> +	sev = psp_master->sev_data;
> +
> +	order = get_order(PMD_SIZE * num_2mb_pages);
> +
> +	/*
> +	 * SNP_INIT_EX is protected by sev_cmd_mutex, therefore this list
> +	 * also needs to be protected using the same mutex.
> +	 */
> +	guard(mutex)(&sev_cmd_mutex);
> +
> +	/*
> +	 * This API uses SNP_INIT_EX to transition allocated pages to HV_Fixed
> +	 * page state, fail if SNP is already initialized.
> +	 */
> +	if (sev->snp_initialized)
> +		return NULL;
> +
> +	/* Re-use freed pages that match the request */
> +	list_for_each_entry(entry, &snp_hv_fixed_pages, list) {
> +		/* Hypervisor fixed page allocator implements exact fit policy */
> +		if (entry->order == order && entry->free) {
> +			entry->free = false;
> +			memset(page_address(entry->page), 0,
> +			       (1 << entry->order) * PAGE_SIZE);
> +			return entry->page;
> +		}
> +	}
> +
> +	page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
> +	if (!page)
> +		return NULL;
> +
> +	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
> +	if (!entry) {
> +		__free_pages(page, order);
> +		return NULL;
> +	}
> +
> +	entry->page = page;
> +	entry->order = order;
> +	list_add_tail(&entry->list, &snp_hv_fixed_pages);
> +
> +	return page;
> +}
> +
> +void snp_free_hv_fixed_pages(struct page *page)
> +{
> +	struct psp_device *psp_master = psp_get_master_device();
> +	struct snp_hv_fixed_pages_entry *entry, *nentry;
> +
> +	if (!psp_master || !psp_master->sev_data)
> +		return;
> +
> +	/*
> +	 * SNP_INIT_EX is protected by sev_cmd_mutex, therefore this list
> +	 * also needs to be protected using the same mutex.
> +	 */
> +	guard(mutex)(&sev_cmd_mutex);
> +
> +	list_for_each_entry_safe(entry, nentry, &snp_hv_fixed_pages, list) {
> +		if (entry->page != page)
> +			continue;
> +
> +		/*
> +		 * HV_FIXED page state cannot be changed until reboot
> +		 * and they cannot be used by an SNP guest, so they cannot
> +		 * be returned back to the page allocator.
> +		 * Mark the pages as free internally to allow possible re-use.
> +		 */
> +		if (entry->page_state == HV_FIXED) {
> +			entry->free = true;
> +		} else {
> +			__free_pages(page, entry->order);
> +			list_del(&entry->list);
> +			kfree(entry);
> +		}
> +		return;
> +	}
> +}
> +
> +static void snp_add_hv_fixed_pages(struct sev_device *sev, struct sev_data_range_list *range_list)
> +{
> +	struct snp_hv_fixed_pages_entry *entry;
> +	struct sev_data_range *range;
> +	int num_elements;
> +
> +	lockdep_assert_held(&sev_cmd_mutex);
> +
> +	if (list_empty(&snp_hv_fixed_pages))
> +		return;
> +
> +	num_elements = list_count_nodes(&snp_hv_fixed_pages) +
> +		       range_list->num_elements;
> +
> +	/*
> +	 * Ensure the list of HV_FIXED pages that will be passed to firmware
> +	 * do not exceed the page-sized argument buffer.
> +	 */
> +	if (num_elements * sizeof(*range) + sizeof(*range_list) > PAGE_SIZE) {
> +		dev_warn(sev->dev, "Additional HV_Fixed pages cannot be accommodated, omitting\n");
> +		return;
> +	}
> +
> +	range = &range_list->ranges[range_list->num_elements];
> +	list_for_each_entry(entry, &snp_hv_fixed_pages, list) {
> +		range->base = page_to_pfn(entry->page) << PAGE_SHIFT;
> +		range->page_count = 1 << entry->order;
> +		range++;
> +	}
> +	range_list->num_elements = num_elements;
> +}
> +
> +static void snp_leak_hv_fixed_pages(void)
> +{
> +	struct snp_hv_fixed_pages_entry *entry;
> +
> +	/* List is protected by sev_cmd_mutex */
> +	lockdep_assert_held(&sev_cmd_mutex);
> +
> +	if (list_empty(&snp_hv_fixed_pages))
> +		return;
> +
> +	list_for_each_entry(entry, &snp_hv_fixed_pages, list)
> +		if (entry->page_state == HV_FIXED)
> +			__snp_leak_pages(page_to_pfn(entry->page),
> +					 1 << entry->order, false);
> +}
> +
>  static int snp_filter_reserved_mem_regions(struct resource *rs, void *arg)
>  {
>  	struct sev_data_range_list *range_list = arg;
> @@ -1248,6 +1422,12 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)
>  			return rc;
>  		}
>  
> +		/*
> +		 * Add HV_Fixed pages from other PSP sub-devices, such as SFS to the
> +		 * HV_Fixed page list.
> +		 */
> +		snp_add_hv_fixed_pages(sev, snp_range_list);
> +
>  		memset(&data, 0, sizeof(data));
>  
>  		if (max_snp_asid) {
> @@ -1293,6 +1473,7 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)
>  		return rc;
>  	}
>  
> +	snp_hv_fixed_pages_state_update(sev, HV_FIXED);
>  	sev->snp_initialized = true;
>  	dev_dbg(sev->dev, "SEV-SNP firmware initialized\n");
>  
> @@ -1896,6 +2077,7 @@ static int __sev_snp_shutdown_locked(int *error, bool panic)
>  		return ret;
>  	}
>  
> +	snp_leak_hv_fixed_pages();
>  	sev->snp_initialized = false;
>  	dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n");
>  
> diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h
> index 5aed2595c9ae..ac03bd0848f7 100644
> --- a/drivers/crypto/ccp/sev-dev.h
> +++ b/drivers/crypto/ccp/sev-dev.h
> @@ -69,4 +69,7 @@ void sev_dev_destroy(struct psp_device *psp);
>  void sev_pci_init(void);
>  void sev_pci_exit(void);
>  
> +struct page *snp_alloc_hv_fixed_pages(unsigned int num_2mb_pages);
> +void snp_free_hv_fixed_pages(struct page *page);
> +
>  #endif /* __SEV_DEV_H */


  reply	other threads:[~2025-09-12 14:57 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-10 22:55 [PATCH v4 0/3] crypto: ccp - Add AMD Seamless Firmware Servicing (SFS) driver Ashish Kalra
2025-09-10 22:55 ` [PATCH v4 1/3] x86/sev: Add new dump_rmp parameter to snp_leak_pages() API Ashish Kalra
2025-09-12 14:52   ` Tom Lendacky
2025-09-12 15:58   ` Borislav Petkov
2025-09-12 18:34     ` Sean Christopherson
2025-09-13 10:55       ` Borislav Petkov
2025-09-16  3:51         ` Herbert Xu
2025-09-16 10:27           ` Borislav Petkov
2025-09-12 18:34   ` Sean Christopherson
2025-09-12 19:37     ` Tom Lendacky
2025-09-12 19:41       ` Sean Christopherson
2025-09-12 19:52         ` Tom Lendacky
2025-09-10 22:55 ` [PATCH v4 2/3] crypto: ccp - Add new HV-Fixed page allocation/free API Ashish Kalra
2025-09-12 14:57   ` Tom Lendacky [this message]
2025-09-10 22:55 ` [PATCH v4 3/3] crypto: ccp - Add AMD Seamless Firmware Servicing (SFS) driver Ashish Kalra
2025-09-12 15:49   ` 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=c36a12f6-33bd-4430-92b2-5fd2939d9b24@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=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=hpa@zytor.com \
    --cc=john.allen@amd.com \
    --cc=kvm@vger.kernel.org \
    --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

all inboxes | Powered by JetHome®