]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
crypto: ccp - Fix locking on alloc failure handling
authorAlexey Kardashevskiy <aik@amd.com>
Tue, 17 Jun 2025 09:43:54 +0000 (19:43 +1000)
committerHerbert Xu <herbert@gondor.apana.org.au>
Mon, 7 Jul 2025 03:27:04 +0000 (15:27 +1200)
The __snp_alloc_firmware_pages() helper allocates pages in the firmware
state (alloc + rmpupdate). In case of failed rmpupdate, it tries
reclaiming pages with already changed state. This requires calling
the PSP firmware and since there is sev_cmd_mutex to guard such calls,
the helper takes a "locked" parameter so specify if the lock needs to
be held.

Most calls happen from snp_alloc_firmware_page() which executes without
the lock. However

commit 24512afa4336 ("crypto: ccp: Handle the legacy TMR allocation when SNP is enabled")

switched sev_fw_alloc() from alloc_pages() (which does not call the PSP) to
__snp_alloc_firmware_pages() (which does) but did not account for the fact
that sev_fw_alloc() is called from __sev_platform_init_locked()
(via __sev_platform_init_handle_tmr()) and executes with the lock held.

Add a "locked" parameter to __snp_alloc_firmware_pages().
Make sev_fw_alloc() use the new parameter to prevent potential deadlock in
rmp_mark_pages_firmware() if rmpupdate() failed.

Fixes: 24512afa4336 ("crypto: ccp: Handle the legacy TMR allocation when SNP is enabled")
Signed-off-by: Alexey Kardashevskiy <aik@amd.com>
Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
Reviewed-by: Pratik R. Sampat <prsampat@amd.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
drivers/crypto/ccp/sev-dev.c

index 17edc6bf56224f194f79b209313c62cd3113dd8f..e058ba0277929620c14618e43ba7e89e05f981d2 100644 (file)
@@ -434,7 +434,7 @@ cleanup:
        return rc;
 }
 
-static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order)
+static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked)
 {
        unsigned long npages = 1ul << order, paddr;
        struct sev_device *sev;
@@ -453,7 +453,7 @@ static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order)
                return page;
 
        paddr = __pa((unsigned long)page_address(page));
-       if (rmp_mark_pages_firmware(paddr, npages, false))
+       if (rmp_mark_pages_firmware(paddr, npages, locked))
                return NULL;
 
        return page;
@@ -463,7 +463,7 @@ void *snp_alloc_firmware_page(gfp_t gfp_mask)
 {
        struct page *page;
 
-       page = __snp_alloc_firmware_pages(gfp_mask, 0);
+       page = __snp_alloc_firmware_pages(gfp_mask, 0, false);
 
        return page ? page_address(page) : NULL;
 }
@@ -498,7 +498,7 @@ static void *sev_fw_alloc(unsigned long len)
 {
        struct page *page;
 
-       page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(len));
+       page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(len), true);
        if (!page)
                return NULL;