Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: ccp - Fix locking on alloc failure handling

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>

authored by

Alexey Kardashevskiy and committed by
Herbert Xu
b4abeccb a71d3e1b

+4 -4
+4 -4
drivers/crypto/ccp/sev-dev.c
··· 434 434 return rc; 435 435 } 436 436 437 - static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order) 437 + static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order, bool locked) 438 438 { 439 439 unsigned long npages = 1ul << order, paddr; 440 440 struct sev_device *sev; ··· 453 453 return page; 454 454 455 455 paddr = __pa((unsigned long)page_address(page)); 456 - if (rmp_mark_pages_firmware(paddr, npages, false)) 456 + if (rmp_mark_pages_firmware(paddr, npages, locked)) 457 457 return NULL; 458 458 459 459 return page; ··· 463 463 { 464 464 struct page *page; 465 465 466 - page = __snp_alloc_firmware_pages(gfp_mask, 0); 466 + page = __snp_alloc_firmware_pages(gfp_mask, 0, false); 467 467 468 468 return page ? page_address(page) : NULL; 469 469 } ··· 498 498 { 499 499 struct page *page; 500 500 501 - page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(len)); 501 + page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(len), true); 502 502 if (!page) 503 503 return NULL; 504 504