From nobody Mon Feb 9 04:46:46 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D459316DEB9; Tue, 28 May 2024 09:55:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716890159; cv=none; b=hYe+Iqsz8fj+ZunbSrKqCJMjW+QPje1GrcxibnC4sKjvXCGTFbGtQSPblC+HEZT5K45YANHLyfdvyfkjYcV9JPX5QTO/mxtxpJzRn1HQWvSJbuEQuQ4YZ9zoPNyuZ+rr7i8gsi0WBXgXlMxp0Wrg5ZGrn5Oi6C7obZ/1Y6DXpUw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716890159; c=relaxed/simple; bh=ECdX67CT706dj0mlzCOMRqGiltd61ccVv16An5RaII4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ur3Lsnd0s9+O3U1Nz5uCGa8qDqy+o1SzQColRzNqNRRSgPg+c6q1aQw77C7J+UogW6WvvDUmcejpWN+cQbcOjOxxY2B+HmREaRe/FULnSGr43uKa4dhwNbGbtNS6CI94fPFlXM++xbnCXr/8T4dTVC3/a2e6de6/cVMHs1FSgCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.helo=mgamail.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=dUDkn9ro; arc=none smtp.client-ip=198.175.65.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.helo=mgamail.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="dUDkn9ro" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1716890157; x=1748426157; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ECdX67CT706dj0mlzCOMRqGiltd61ccVv16An5RaII4=; b=dUDkn9rowcu+61xi9svNIkFuP5zzUbqZW9TcN/pKMCtVzXTrbTzzExcd fYmVbCDDWmm5W4kR7ba2J88V0rhAbvdkapoe6oCufqTGDp9zTjK2Fxg7u ahakaoQFVmGeuIn98nKHEZNY86bOVpR8kLPaWe7IQ9QPh02HLr6nsbS35 p69xDiPb7iAFpP1AYtwbcoprx7TH3Zps6fvUW/L8x1oH1aS+BAzYbmGSs vvVxj7qXOKlgCXMhFYkzzTd6/6zHWhZ1yeGVYaEl420x111w1g8Hf4SPI NxpHcvAprIf/8utX5u2OJl/XNznFrGXAJKoITbuhuz8g2k+nVvn/tQv+/ Q==; X-CSE-ConnectionGUID: gdx7tdD/T8KOw2ua7yrTiA== X-CSE-MsgGUID: 23gh/B6NQrq/PEFxuxBfag== X-IronPort-AV: E=McAfee;i="6600,9927,11085"; a="13172209" X-IronPort-AV: E=Sophos;i="6.08,194,1712646000"; d="scan'208";a="13172209" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa113.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2024 02:55:41 -0700 X-CSE-ConnectionGUID: yBhy+KvbRWy8mZbcDepX4A== X-CSE-MsgGUID: 9nRbDhTcQm+Cu80VC7v3pg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,194,1712646000"; d="scan'208";a="39984755" Received: from black.fi.intel.com ([10.237.72.28]) by orviesa004.jf.intel.com with ESMTP; 28 May 2024 02:55:34 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 8F0826B7; Tue, 28 May 2024 12:55:26 +0300 (EEST) From: "Kirill A. Shutemov" To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org Cc: "Rafael J. Wysocki" , Peter Zijlstra , Adrian Hunter , Kuppuswamy Sathyanarayanan , Elena Reshetova , Jun Nakajima , Rick Edgecombe , Tom Lendacky , "Kalra, Ashish" , Sean Christopherson , "Huang, Kai" , Ard Biesheuvel , Baoquan He , "H. Peter Anvin" , "Kirill A. Shutemov" , "K. Y. Srinivasan" , Haiyang Zhang , kexec@lists.infradead.org, linux-hyperv@vger.kernel.org, linux-acpi@vger.kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Dave Hansen , Tao Liu , Michael Kelley Subject: [PATCHv11 07/19] x86/mm: Make x86_platform.guest.enc_status_change_*() return errno Date: Tue, 28 May 2024 12:55:10 +0300 Message-ID: <20240528095522.509667-8-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240528095522.509667-1-kirill.shutemov@linux.intel.com> References: <20240528095522.509667-1-kirill.shutemov@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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; enc_status_change_finish() changed too to keep the interface symmetric. Signed-off-by: Kirill A. Shutemov Reviewed-by: Dave Hansen Reviewed-by: Kai Huang Tested-by: Tao Liu Reviewed-by: Michael Kelley --- arch/x86/coco/tdx/tdx.c | 20 +++++++++++--------- arch/x86/hyperv/ivm.c | 22 ++++++++++------------ arch/x86/include/asm/x86_init.h | 4 ++-- arch/x86/kernel/x86_init.c | 4 ++-- arch/x86/mm/mem_encrypt_amd.c | 8 ++++---- arch/x86/mm/pat/set_memory.c | 12 +++++++----- 6 files changed, 36 insertions(+), 34 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index c1cb90369915..26fa47db5782 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -798,28 +798,30 @@ static bool tdx_enc_status_changed(unsigned long vadd= r, int numpages, bool enc) return true; } =20 -static bool tdx_enc_status_change_prepare(unsigned long vaddr, int numpage= s, - bool enc) +static int tdx_enc_status_change_prepare(unsigned long vaddr, int numpages, + bool enc) { /* * Only handle shared->private conversion here. * See the comment in tdx_early_init(). */ - if (enc) - return tdx_enc_status_changed(vaddr, numpages, enc); - return true; + if (enc && !tdx_enc_status_changed(vaddr, numpages, enc)) + return -EIO; + + return 0; } =20 -static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages, +static int tdx_enc_status_change_finish(unsigned long vaddr, int numpages, bool enc) { /* * Only handle private->shared conversion here. * See the comment in tdx_early_init(). */ - if (!enc) - return tdx_enc_status_changed(vaddr, numpages, enc); - return true; + if (!enc && !tdx_enc_status_changed(vaddr, numpages, enc)) + return -EIO; + + return 0; } =20 void __init tdx_early_init(void) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 768d73de0d09..b4a851d27c7c 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -523,9 +523,9 @@ static int hv_mark_gpa_visibility(u16 count, const u64 = pfn[], * transition is complete, hv_vtom_set_host_visibility() marks the pages * as "present" again. */ -static bool hv_vtom_clear_present(unsigned long kbuffer, int pagecount, bo= ol enc) +static int hv_vtom_clear_present(unsigned long kbuffer, int pagecount, boo= l enc) { - return !set_memory_np(kbuffer, pagecount); + return set_memory_np(kbuffer, pagecount); } =20 /* @@ -536,20 +536,19 @@ static bool hv_vtom_clear_present(unsigned long kbuff= er, int pagecount, bool enc * with host. This function works as wrap of hv_mark_gpa_visibility() * with memory base and size. */ -static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecou= nt, bool enc) +static int hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecoun= t, bool enc) { enum hv_mem_host_visibility visibility =3D enc ? VMBUS_PAGE_NOT_VISIBLE : VMBUS_PAGE_VISIBLE_READ_WRITE; u64 *pfn_array; phys_addr_t paddr; + int i, pfn, err; void *vaddr; int ret =3D 0; - bool result =3D true; - int i, pfn; =20 pfn_array =3D kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL); if (!pfn_array) { - result =3D false; + ret =3D -ENOMEM; goto err_set_memory_p; } =20 @@ -568,10 +567,8 @@ static bool hv_vtom_set_host_visibility(unsigned long = kbuffer, int pagecount, bo if (pfn =3D=3D HV_MAX_MODIFY_GPA_REP_COUNT || i =3D=3D pagecount - 1) { ret =3D hv_mark_gpa_visibility(pfn, pfn_array, visibility); - if (ret) { - result =3D false; + if (ret) goto err_free_pfn_array; - } pfn =3D 0; } } @@ -586,10 +583,11 @@ static bool hv_vtom_set_host_visibility(unsigned long= kbuffer, int pagecount, bo * order to avoid leaving the memory range in a "broken" state. Setting * the PRESENT bits shouldn't fail, but return an error if it does. */ - if (set_memory_p(kbuffer, pagecount)) - result =3D false; + err =3D set_memory_p(kbuffer, pagecount); + if (err && !ret) + ret =3D err; =20 - return result; + return ret; } =20 static bool hv_vtom_tlb_flush_required(bool private) diff --git a/arch/x86/include/asm/x86_init.h b/arch/x86/include/asm/x86_ini= t.h index 6149eabe200f..28ac3cb9b987 100644 --- a/arch/x86/include/asm/x86_init.h +++ b/arch/x86/include/asm/x86_init.h @@ -151,8 +151,8 @@ struct x86_init_acpi { * @enc_cache_flush_required Returns true if a cache flush is needed befor= e changing page encryption status */ struct x86_guest { - bool (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool e= nc); - bool (*enc_status_change_finish)(unsigned long vaddr, int npages, bool en= c); + int (*enc_status_change_prepare)(unsigned long vaddr, int npages, bool en= c); + int (*enc_status_change_finish)(unsigned long vaddr, int npages, bool enc= ); bool (*enc_tlb_flush_required)(bool enc); bool (*enc_cache_flush_required)(void); }; diff --git a/arch/x86/kernel/x86_init.c b/arch/x86/kernel/x86_init.c index d5dc5a92635a..a7143bb7dd93 100644 --- a/arch/x86/kernel/x86_init.c +++ b/arch/x86/kernel/x86_init.c @@ -134,8 +134,8 @@ struct x86_cpuinit_ops x86_cpuinit =3D { =20 static void default_nmi_init(void) { }; =20 -static bool enc_status_change_prepare_noop(unsigned long vaddr, int npages= , bool enc) { return true; } -static bool enc_status_change_finish_noop(unsigned long vaddr, int npages,= bool enc) { return true; } +static int enc_status_change_prepare_noop(unsigned long vaddr, int npages,= bool enc) { return 0; } +static int enc_status_change_finish_noop(unsigned long vaddr, int npages, = bool enc) { return 0; } static bool enc_tlb_flush_required_noop(bool enc) { return false; } static bool enc_cache_flush_required_noop(void) { return false; } static bool is_private_mmio_noop(u64 addr) {return false; } diff --git a/arch/x86/mm/mem_encrypt_amd.c b/arch/x86/mm/mem_encrypt_amd.c index 422602f6039b..e7b67519ddb5 100644 --- a/arch/x86/mm/mem_encrypt_amd.c +++ b/arch/x86/mm/mem_encrypt_amd.c @@ -283,7 +283,7 @@ static void enc_dec_hypercall(unsigned long vaddr, unsi= gned long size, bool enc) #endif } =20 -static bool amd_enc_status_change_prepare(unsigned long vaddr, int npages,= bool enc) +static int amd_enc_status_change_prepare(unsigned long vaddr, int npages, = bool enc) { /* * To maintain the security guarantees of SEV-SNP guests, make sure @@ -292,11 +292,11 @@ static bool amd_enc_status_change_prepare(unsigned lo= ng vaddr, int npages, bool if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && !enc) snp_set_memory_shared(vaddr, npages); =20 - return true; + return 0; } =20 /* Return true unconditionally: return value doesn't matter for the SEV si= de */ -static bool amd_enc_status_change_finish(unsigned long vaddr, int npages, = bool enc) +static int amd_enc_status_change_finish(unsigned long vaddr, int npages, b= ool enc) { /* * 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 if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) enc_dec_hypercall(vaddr, npages << PAGE_SHIFT, enc); =20 - return true; + return 0; } =20 static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 19fdfbb171ed..498812f067cd 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2196,7 +2196,8 @@ static int __set_memory_enc_pgtable(unsigned long add= r, int numpages, bool enc) cpa_flush(&cpa, x86_platform.guest.enc_cache_flush_required()); =20 /* Notify hypervisor that we are about to set/clr encryption attribute. */ - if (!x86_platform.guest.enc_status_change_prepare(addr, numpages, enc)) + ret =3D x86_platform.guest.enc_status_change_prepare(addr, numpages, enc); + if (ret) goto vmm_fail; =20 ret =3D __change_page_attr_set_clr(&cpa, 1); @@ -2214,16 +2215,17 @@ static int __set_memory_enc_pgtable(unsigned long a= ddr, int numpages, bool enc) return ret; =20 /* Notify hypervisor that we have successfully set/clr encryption attribu= te. */ - if (!x86_platform.guest.enc_status_change_finish(addr, numpages, enc)) + ret =3D x86_platform.guest.enc_status_change_finish(addr, numpages, enc); + if (ret) goto vmm_fail; =20 return 0; =20 vmm_fail: - WARN_ONCE(1, "CPA VMM failure to convert memory (addr=3D%p, numpages=3D%d= ) to %s.\n", - (void *)addr, numpages, enc ? "private" : "shared"); + WARN_ONCE(1, "CPA VMM failure to convert memory (addr=3D%p, numpages=3D%d= ) to %s: %d\n", + (void *)addr, numpages, enc ? "private" : "shared", ret); =20 - return -EIO; + return ret; } =20 static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) --=20 2.43.0