Skip to content

Commit 99c5c4c

Browse files
kirylbp3tk0v
authored andcommitted
x86/mm: Make x86_platform.guest.enc_status_change_*() return an error
TDX is going to have more than one reason to fail enc_status_change_prepare(). Change the callback to return errno instead of assuming -EIO. Change enc_status_change_finish() too to keep the interface symmetric. Signed-off-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Reviewed-by: Dave Hansen <[email protected]> Reviewed-by: Kai Huang <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Tested-by: Tao Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent de60613 commit 99c5c4c

File tree

6 files changed

+36
-34
lines changed

6 files changed

+36
-34
lines changed

arch/x86/coco/tdx/tdx.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -798,28 +798,30 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)
798798
return true;
799799
}
800800

801-
static bool tdx_enc_status_change_prepare(unsigned long vaddr, int numpages,
802-
bool enc)
801+
static int tdx_enc_status_change_prepare(unsigned long vaddr, int numpages,
802+
bool enc)
803803
{
804804
/*
805805
* Only handle shared->private conversion here.
806806
* See the comment in tdx_early_init().
807807
*/
808-
if (enc)
809-
return tdx_enc_status_changed(vaddr, numpages, enc);
810-
return true;
808+
if (enc && !tdx_enc_status_changed(vaddr, numpages, enc))
809+
return -EIO;
810+
811+
return 0;
811812
}
812813

813-
static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
814+
static int tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
814815
bool enc)
815816
{
816817
/*
817818
* Only handle private->shared conversion here.
818819
* See the comment in tdx_early_init().
819820
*/
820-
if (!enc)
821-
return tdx_enc_status_changed(vaddr, numpages, enc);
822-
return true;
821+
if (!enc && !tdx_enc_status_changed(vaddr, numpages, enc))
822+
return -EIO;
823+
824+
return 0;
823825
}
824826

825827
void __init tdx_early_init(void)

arch/x86/hyperv/ivm.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,9 @@ static int hv_mark_gpa_visibility(u16 count, const u64 pfn[],
523523
* transition is complete, hv_vtom_set_host_visibility() marks the pages
524524
* as "present" again.
525525
*/
526-
static bool hv_vtom_clear_present(unsigned long kbuffer, int pagecount, bool enc)
526+
static int hv_vtom_clear_present(unsigned long kbuffer, int pagecount, bool enc)
527527
{
528-
return !set_memory_np(kbuffer, pagecount);
528+
return set_memory_np(kbuffer, pagecount);
529529
}
530530

531531
/*
@@ -536,20 +536,19 @@ static bool hv_vtom_clear_present(unsigned long kbuffer, int pagecount, bool enc
536536
* with host. This function works as wrap of hv_mark_gpa_visibility()
537537
* with memory base and size.
538538
*/
539-
static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bool enc)
539+
static int hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bool enc)
540540
{
541541
enum hv_mem_host_visibility visibility = enc ?
542542
VMBUS_PAGE_NOT_VISIBLE : VMBUS_PAGE_VISIBLE_READ_WRITE;
543543
u64 *pfn_array;
544544
phys_addr_t paddr;
545+
int i, pfn, err;
545546
void *vaddr;
546547
int ret = 0;
547-
bool result = true;
548-
int i, pfn;
549548

550549
pfn_array = kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
551550
if (!pfn_array) {
552-
result = false;
551+
ret = -ENOMEM;
553552
goto err_set_memory_p;
554553
}
555554

@@ -568,10 +567,8 @@ static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bo
568567
if (pfn == HV_MAX_MODIFY_GPA_REP_COUNT || i == pagecount - 1) {
569568
ret = hv_mark_gpa_visibility(pfn, pfn_array,
570569
visibility);
571-
if (ret) {
572-
result = false;
570+
if (ret)
573571
goto err_free_pfn_array;
574-
}
575572
pfn = 0;
576573
}
577574
}
@@ -586,10 +583,11 @@ static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bo
586583
* order to avoid leaving the memory range in a "broken" state. Setting
587584
* the PRESENT bits shouldn't fail, but return an error if it does.
588585
*/
589-
if (set_memory_p(kbuffer, pagecount))
590-
result = false;
586+
err = set_memory_p(kbuffer, pagecount);
587+
if (err && !ret)
588+
ret = err;
591589

592-
return result;
590+
return ret;
593591
}
594592

595593
static bool hv_vtom_tlb_flush_required(bool private)

arch/x86/include/asm/x86_init.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ struct x86_init_acpi {
151151
* @enc_cache_flush_required Returns true if a cache flush is needed before changing page encryption status
152152
*/
153153
struct x86_guest {
154-
bool (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool enc);
155-
bool (*enc_status_change_finish)(unsigned long vaddr, int npages, bool enc);
154+
int (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool enc);
155+
int (*enc_status_change_finish)(unsigned long vaddr, int npages, bool enc);
156156
bool (*enc_tlb_flush_required)(bool enc);
157157
bool (*enc_cache_flush_required)(void);
158158
};

arch/x86/kernel/x86_init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ struct x86_cpuinit_ops x86_cpuinit = {
134134

135135
static void default_nmi_init(void) { };
136136

137-
static bool enc_status_change_prepare_noop(unsigned long vaddr, int npages, bool enc) { return true; }
138-
static bool enc_status_change_finish_noop(unsigned long vaddr, int npages, bool enc) { return true; }
137+
static int enc_status_change_prepare_noop(unsigned long vaddr, int npages, bool enc) { return 0; }
138+
static int enc_status_change_finish_noop(unsigned long vaddr, int npages, bool enc) { return 0; }
139139
static bool enc_tlb_flush_required_noop(bool enc) { return false; }
140140
static bool enc_cache_flush_required_noop(void) { return false; }
141141
static bool is_private_mmio_noop(u64 addr) {return false; }

arch/x86/mm/mem_encrypt_amd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ static void enc_dec_hypercall(unsigned long vaddr, unsigned long size, bool enc)
283283
#endif
284284
}
285285

286-
static bool amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc)
286+
static int amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc)
287287
{
288288
/*
289289
* To maintain the security guarantees of SEV-SNP guests, make sure
@@ -292,11 +292,11 @@ static bool amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool
292292
if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && !enc)
293293
snp_set_memory_shared(vaddr, npages);
294294

295-
return true;
295+
return 0;
296296
}
297297

298298
/* Return true unconditionally: return value doesn't matter for the SEV side */
299-
static bool amd_enc_status_change_finish(unsigned long vaddr, int npages, bool enc)
299+
static int amd_enc_status_change_finish(unsigned long vaddr, int npages, bool enc)
300300
{
301301
/*
302302
* After memory is mapped encrypted in the page table, validate it
@@ -308,7 +308,7 @@ static bool amd_enc_status_change_finish(unsigned long vaddr, int npages, bool e
308308
if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
309309
enc_dec_hypercall(vaddr, npages << PAGE_SHIFT, enc);
310310

311-
return true;
311+
return 0;
312312
}
313313

314314
static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc)

arch/x86/mm/pat/set_memory.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,8 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc)
21962196
cpa_flush(&cpa, x86_platform.guest.enc_cache_flush_required());
21972197

21982198
/* Notify hypervisor that we are about to set/clr encryption attribute. */
2199-
if (!x86_platform.guest.enc_status_change_prepare(addr, numpages, enc))
2199+
ret = x86_platform.guest.enc_status_change_prepare(addr, numpages, enc);
2200+
if (ret)
22002201
goto vmm_fail;
22012202

22022203
ret = __change_page_attr_set_clr(&cpa, 1);
@@ -2214,16 +2215,17 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc)
22142215
return ret;
22152216

22162217
/* Notify hypervisor that we have successfully set/clr encryption attribute. */
2217-
if (!x86_platform.guest.enc_status_change_finish(addr, numpages, enc))
2218+
ret = x86_platform.guest.enc_status_change_finish(addr, numpages, enc);
2219+
if (ret)
22182220
goto vmm_fail;
22192221

22202222
return 0;
22212223

22222224
vmm_fail:
2223-
WARN_ONCE(1, "CPA VMM failure to convert memory (addr=%p, numpages=%d) to %s.\n",
2224-
(void *)addr, numpages, enc ? "private" : "shared");
2225+
WARN_ONCE(1, "CPA VMM failure to convert memory (addr=%p, numpages=%d) to %s: %d\n",
2226+
(void *)addr, numpages, enc ? "private" : "shared", ret);
22252227

2226-
return -EIO;
2228+
return ret;
22272229
}
22282230

22292231
static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)

0 commit comments

Comments
 (0)