From patchwork Tue Jan 16 02:20:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Kelley X-Patchwork-Id: 13520331 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61D5DC47422 for ; Tue, 16 Jan 2024 02:20:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09D5D6B0075; Mon, 15 Jan 2024 21:20:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 050736B007B; Mon, 15 Jan 2024 21:20:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C0E556B0078; Mon, 15 Jan 2024 21:20:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id AA8986B0075 for ; Mon, 15 Jan 2024 21:20:33 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7ABCBC01A2 for ; Tue, 16 Jan 2024 02:20:33 +0000 (UTC) X-FDA: 81683570346.17.41FFB90 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf06.hostedemail.com (Postfix) with ESMTP id 90D7D180007 for ; Tue, 16 Jan 2024 02:20:31 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AWwP+PGK; spf=pass (imf06.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=mhkelley58@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705371631; a=rsa-sha256; cv=none; b=X71AUkFk83msbMc/PaFFW1oTIgMO0PfcsYa7lfTV6xgxZEc9LXDChRl2c9U5p4PbrtryFh S8Z9Mtn0TfIJ4E8qw6lzyrMx7sGAbOwLHAKtzcR1zeAXKczsjja4HzkXr7t8rz4DvhLfQQ vRqVZNrbgaQmBGDUSuwBZByI5ddz6F4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AWwP+PGK; spf=pass (imf06.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=mhkelley58@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705371631; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WMHKxJ/T5Ct44oYlSE+LYrzOvXToXwBp+RoVB/ApQnM=; b=afns1U1TahlzvPugmHNejrIhe1ZAukdg8FKEXkK2Tc63XXR7p6SbnUy/xBlatMUGuOsWnv 1qtpKEYMGr2I75ILydylFuYG9P+N9LDAI9PM2fqZ9FgMrkl92AKH7wCuHAV09ngsHLDwPb N2IPhWvMgx7VyMc9dslA2jnNo0j68Zo= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1d3f29fea66so51625425ad.3 for ; Mon, 15 Jan 2024 18:20:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705371630; x=1705976430; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WMHKxJ/T5Ct44oYlSE+LYrzOvXToXwBp+RoVB/ApQnM=; b=AWwP+PGKD6idvzBPJF9D2gsIQTKNMUFvQFzk4P+ExbCPd99IE97DKPCg1L6RlEgCNH /PhxeTtJ80yVokxqd1AFXu19zZ/iR/tTpiliSxSw6MShy6LXLRdZheD+byl48O1EsjRd UNLupCELWSuEuBhONf8umfHr4adISKPIMBR6xUoYMUwmX8DA9dYqixI2ivKkKuCLWpiv 8Q7VDjFr5vPGdA7Pe49FVMO3h9ommiAzhkxs8MqA/UwpRUDBj8bhhtSmiv65psQTRGTc kaR61LP6rOtGNOZUjzl9w9gprQuWV8WQlTxIz8x4X4yNhdzudydmta8izkbnvM5iP7WO qA2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705371630; x=1705976430; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:to:from:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=WMHKxJ/T5Ct44oYlSE+LYrzOvXToXwBp+RoVB/ApQnM=; b=n6V8oGgtdgTL1UXWWXK5VDQ8xeUgRNiYjw4UTJ+6SejW9+JAV3VtZjhgMjYdMtG3xD pa4COHUGG0ajNdho7gMQC5O1zSrEIewmMVi3ualkIIpArMzdch5KA4D94ade24tNlBN3 hLoJ3QwhAcJ3Rryz4OvhgHJtTYHTvYA48Bq5S44AV8vJoHu7XE9yWLDGkB07wG1O35HF DDWrdavduMDFum0bcilU0FND+5YbUAwoZ23M5Q1cSIgUiGcDy0UQfeeOoKY+qmCzZuKt y8hGaXyE9bol5PaQN5Ujd9qsnbx4BwBXulxjYqy0qCFB0vfCwVuKFRBkFm5U14U/i0sB TgNw== X-Gm-Message-State: AOJu0YyhOjMPvYUpPXR17UzFgoL2VJLZ05aOvhzSrbuMuMiPrFuUzPaJ 3jjnYL3jW7QXu1JuLcGWhmA= X-Google-Smtp-Source: AGHT+IGKhIpxJK9risAw80iB9x8SFXIfFEzqHrHFHgbziciGN/6OZbZls+x7EV8Jy+UwtVv+JbHLlQ== X-Received: by 2002:a17:903:2808:b0:1d4:75c6:9560 with SMTP id kp8-20020a170903280800b001d475c69560mr2742635plb.59.1705371630377; Mon, 15 Jan 2024 18:20:30 -0800 (PST) Received: from localhost.localdomain (c-73-254-87-52.hsd1.wa.comcast.net. [73.254.87.52]) by smtp.gmail.com with ESMTPSA id kn14-20020a170903078e00b001d1d1ef8be5sm8193379plb.173.2024.01.15.18.20.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 18:20:30 -0800 (PST) From: mhkelley58@gmail.com X-Google-Original-From: mhklinux@outlook.com To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, kirill.shutemov@linux.intel.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, luto@kernel.org, peterz@infradead.org, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com, thomas.lendacky@amd.com, ardb@kernel.org, jroedel@suse.de, seanjc@google.com, rick.p.edgecombe@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, linux-hyperv@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 1/3] x86/hyperv: Use slow_virt_to_phys() in page transition hypervisor callback Date: Mon, 15 Jan 2024 18:20:06 -0800 Message-Id: <20240116022008.1023398-2-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240116022008.1023398-1-mhklinux@outlook.com> References: <20240116022008.1023398-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 90D7D180007 X-Stat-Signature: odqbw4oybmz63eunsnpwyfk14xbfamue X-Rspam-User: X-HE-Tag: 1705371631-978175 X-HE-Meta: U2FsdGVkX1/0M/AzSq3tM+E0kMzhcujR/KWH8ggF0zwSwjOMzJYIjyIVumNX6ScrstakpMBeBvoWF0TiNcQ5qiw4p5skzp1I/56+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LPcaIz5GmLNv3E10/UtgbdQx0F42hCX79wmatJVGDjE4u/AgOs3wNkVUZ84tAH/v+gx+J5TXGIY/ZHTlGZMc84P15ChlYC3Xj3nSpc7ml5Ax+oR8ER10MojCT3OciiAULtn2NyH6qQHbfTdVbq9YYnzcB5E0wx0R5sKkyNVf+t7ZDCxjXslUMV8zIJoIYzcpmpCOwF5NmnOrbSnPWv5LkY3e90Me/Y7s21UaN4x+vRrEwDXO8j1SNQfUgD+H087kZ2tii38aQKJ9sOQFC5A+FCGmafU43bHPo7prK1mEEoVty9wz907z277D+zStEegosujjazDptpyNaCmD//TBxIRp8Qjc3MRp4/jeLE34GIYqpmoQGU2WXyxgspFF6bJy03qiB2q7xG6agM/hpIYXs/uLHMGNHL+uncUm+mbPPVA9oVBjDFhlbadLMZAmwKzyeeMjRzvcYoA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Michael Kelley In preparation for temporarily marking pages not present during a transition between encrypted and decrypted, use slow_virt_to_phys() in the hypervisor callback. As long as the PFN is correct, slow_virt_to_phys() works even if the leaf PTE is not present. The existing functions that depend on vmalloc_to_page() all require that the leaf PTE be marked present, so they don't work. Update the comments for slow_virt_to_phys() to note this broader usage and the requirement to work even if the PTE is not marked present. Signed-off-by: Michael Kelley Reviewed-by: Rick Edgecombe Acked-by: Kirill A. Shutemov --- arch/x86/hyperv/ivm.c | 12 +++++++++++- arch/x86/mm/pat/set_memory.c | 12 ++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 02e55237d919..851107c77f4d 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -515,6 +515,8 @@ static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bo enum hv_mem_host_visibility visibility = enc ? VMBUS_PAGE_NOT_VISIBLE : VMBUS_PAGE_VISIBLE_READ_WRITE; u64 *pfn_array; + phys_addr_t paddr; + void *vaddr; int ret = 0; bool result = true; int i, pfn; @@ -524,7 +526,15 @@ static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bo return false; for (i = 0, pfn = 0; i < pagecount; i++) { - pfn_array[pfn] = virt_to_hvpfn((void *)kbuffer + i * HV_HYP_PAGE_SIZE); + /* + * Use slow_virt_to_phys() because the PRESENT bit has been + * temporarily cleared in the PTEs. slow_virt_to_phys() works + * without the PRESENT bit while virt_to_hvpfn() or similar + * does not. + */ + vaddr = (void *)kbuffer + (i * HV_HYP_PAGE_SIZE); + paddr = slow_virt_to_phys(vaddr); + pfn_array[pfn] = paddr >> HV_HYP_PAGE_SHIFT; pfn++; if (pfn == HV_MAX_MODIFY_GPA_REP_COUNT || i == pagecount - 1) { diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index bda9f129835e..e76ac64b516e 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -755,10 +755,14 @@ pmd_t *lookup_pmd_address(unsigned long address) * areas on 32-bit NUMA systems. The percpu areas can * end up in this kind of memory, for instance. * - * This could be optimized, but it is only intended to be - * used at initialization time, and keeping it - * unoptimized should increase the testing coverage for - * the more obscure platforms. + * Note that as long as the PTEs are well-formed with correct PFNs, this + * works without checking the PRESENT bit in the leaf PTE. This is unlike + * the similar vmalloc_to_page() and derivatives. Callers may depend on + * this behavior. + * + * This could be optimized, but it is only used in paths that are not perf + * sensitive, and keeping it unoptimized should increase the testing coverage + * for the more obscure platforms. */ phys_addr_t slow_virt_to_phys(void *__virt_addr) { From patchwork Tue Jan 16 02:20:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Kelley X-Patchwork-Id: 13520332 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94528C3DA79 for ; Tue, 16 Jan 2024 02:20:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EC516B0078; Mon, 15 Jan 2024 21:20:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 094266B007B; Mon, 15 Jan 2024 21:20:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E08AE6B007D; Mon, 15 Jan 2024 21:20:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CCD436B0078 for ; Mon, 15 Jan 2024 21:20:34 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9DE3F1C0D6C for ; Tue, 16 Jan 2024 02:20:34 +0000 (UTC) X-FDA: 81683570388.10.3B92DAC Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf04.hostedemail.com (Postfix) with ESMTP id A45B94001F for ; Tue, 16 Jan 2024 02:20:32 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=MoFiggBY; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=mhkelley58@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705371632; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=c4vfRMdoaViI6HxDtxXy2GwJGspyIYSCzcn+j2z7kh4=; b=0nStZaTfHiciRneMhrk7zZIPdmkJ5NSqlV/BybnVm4fNWUYrIVqPvI1bVJXINeOHk41OmQ eGj895nxfvuWIvENQuoktwgtL7v3Z1WsXZENryYo0amus/GoaH2CslL6nn93AJTcvu1/V0 aD9wfWHhS2FcEUowzc7PuVNFoUUGJyY= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=MoFiggBY; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=mhkelley58@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705371632; a=rsa-sha256; cv=none; b=i1c+Ndyud/9ydMPILfG9p2wnK1c1jFFQ7+FCSEKiDcYWVuzRFnyOBB8nqGRHEsLzWiwExP f3YQyZ4zwu1QrjkiUN7Q2bheaRe4jEHRXQnyN4TzdeY87Xg+8GrzK6XU4N3Ac2Osi8q9hi JOYYmCXi6OnQdc7plqi6KBUTPNwVfqE= Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1d41bb4da91so50984275ad.0 for ; Mon, 15 Jan 2024 18:20:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705371631; x=1705976431; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c4vfRMdoaViI6HxDtxXy2GwJGspyIYSCzcn+j2z7kh4=; b=MoFiggBYyDwhzGB9O4tyaINlBJO0E5ASlqM2iA+agy1eMmwMqtbW2FkKEUhk/RpG8A anN27JNilm+oWexw+5JpA6svUFwYVv6cTb3uKr50vjlArXRirbRsbf0xS5S5XqVg6AQM p9ZGwjwGD0fKwRV8Vgw0/dornllawci11izbbPoAQebkWG9GlsDGLIMod4oNhQprqPm6 ZWjgPrz3e1pQZbQDMqiEvG1k6iEghA/snPcb2ELOmtcJD8zfjJwV9XsTU6lnTrJ6hoyi npf5kreBcNBwKFx4a98gY8EP7XiIvR2/49LelWH/Xz7ulNpZq4LFaDvT/kHep5cV8bQA Hl+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705371631; x=1705976431; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:to:from:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=c4vfRMdoaViI6HxDtxXy2GwJGspyIYSCzcn+j2z7kh4=; b=fLfkOjS4D7FlBIECvOIGZm5YfNQUfVTSyG7IswA7zJ4C0m7Vx6PzGjf7O+PnZBNxxU mBCunpjLTzIhlPZq3Mnh6iCRgpNJQl4nZKz3P+bEvl7NJFdoz9dymtiXAB+ZNkBr4qQv sv7fQVM2EbyGjoJ8U0s1XOHts0Xh51Rb+4/Ly4eG+3G12Nuh4lWPHJfnkYLKkadRdfhm Xpstc/u685qxZZsHzUpqPmbmx0PWwrgIfMfi9JqX1Yl+FF7IokSa+IIIl1+4LbefdW/H SPPzLiImFO7YzuUVYQqcfJ1LujUUHpWIV8qakWt+k3o2/ZRIVZru4wiOoeguu6HJtMsU n1ow== X-Gm-Message-State: AOJu0YwT6t4CIkZ3PFDeTezgd5Bzg+LumYkPqAAIowLlUW5ayhApE3y6 PaIv6ehbCTrcDCj0e81SK/7EPw9VZbk= X-Google-Smtp-Source: AGHT+IFRVMIEFLp+rPfL0OtTdnRiBOnM5nFlc0pFAGcjr3KeEtvte2JqdiXRGVdv5FquDNd/HY+qHQ== X-Received: by 2002:a17:902:cec8:b0:1d5:c558:164d with SMTP id d8-20020a170902cec800b001d5c558164dmr1779308plg.74.1705371631546; Mon, 15 Jan 2024 18:20:31 -0800 (PST) Received: from localhost.localdomain (c-73-254-87-52.hsd1.wa.comcast.net. [73.254.87.52]) by smtp.gmail.com with ESMTPSA id kn14-20020a170903078e00b001d1d1ef8be5sm8193379plb.173.2024.01.15.18.20.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 18:20:31 -0800 (PST) From: mhkelley58@gmail.com X-Google-Original-From: mhklinux@outlook.com To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, kirill.shutemov@linux.intel.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, luto@kernel.org, peterz@infradead.org, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com, thomas.lendacky@amd.com, ardb@kernel.org, jroedel@suse.de, seanjc@google.com, rick.p.edgecombe@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, linux-hyperv@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 2/3] x86/mm: Regularize set_memory_p() parameters and make non-static Date: Mon, 15 Jan 2024 18:20:07 -0800 Message-Id: <20240116022008.1023398-3-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240116022008.1023398-1-mhklinux@outlook.com> References: <20240116022008.1023398-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: fh841kjdigschut6e7xqfxw9zuff46ya X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A45B94001F X-HE-Tag: 1705371632-553845 X-HE-Meta: U2FsdGVkX1/elwMa7EFogdvIOsPJjYJ7sZIyqFrhLarwUjLJuQ21j/13/j1YsuFjq0MXNVLrxnB8N+bTdPSRqp8bA5j24WDP5YNZnHjyifWIFCGhTIeiNUSp9uwULhgbbykCraYqfTkQqPc4y2C4gwikmHVUuHKyaUuM76Xj684Ll9T1Wqk1KEQu8BvbBU5NPpV3rv1xGijoiirflVWoQvHWpmPEunZlB5gMR2ZIObIjuCt9nJou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wYnL1xQgZdLkLiI0gxzJS40yIjwBVL2YxhYpnaSAO4xgfrIkur4FAo64eCFw6wQvD85FBp2MynvoJL7GD8nCMAlYcemy6La/035r/eQAslMXv89SI4ZAM8HmS3R3+55AQOQ4FNgjz96YYofJ94XigdKqGRFhi4mzFu0cDiaSwUx3S/eZPtm0lOY3nEJXWWx/Fu8A5M3M5f9HZZVrZc7sm6ph98rJ4TV5NnJcwEPFsI0Er+Y61425ZkfWvWbClUB+fidfd/FoUIRz/q7ZhipF0FXAC4ByD8CDZRhhxU4Tywy6brUmEnomsdMM7s0jxW7X8lSoEqVmQg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Michael Kelley set_memory_p() is currently static. It has parameters that don't match set_memory_p() under arch/powerpc and that aren't congruent with the other set_memory_* functions. There's no good reason for the difference. Fix this by making the parameters consistent, and update the one existing call site. Make the function non-static and add it to include/asm/set_memory.h so that it is completely parallel to set_memory_np() and is usable in other modules. No functional change. Signed-off-by: Michael Kelley Reviewed-by: Rick Edgecombe Acked-by: Kirill A. Shutemov --- arch/x86/include/asm/set_memory.h | 1 + arch/x86/mm/pat/set_memory.c | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index a5e89641bd2d..9aee31862b4a 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -47,6 +47,7 @@ int set_memory_uc(unsigned long addr, int numpages); int set_memory_wc(unsigned long addr, int numpages); int set_memory_wb(unsigned long addr, int numpages); int set_memory_np(unsigned long addr, int numpages); +int set_memory_p(unsigned long addr, int numpages); int set_memory_4k(unsigned long addr, int numpages); int set_memory_encrypted(unsigned long addr, int numpages); int set_memory_decrypted(unsigned long addr, int numpages); diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index e76ac64b516e..164d32029424 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2045,17 +2045,12 @@ int set_mce_nospec(unsigned long pfn) return rc; } -static int set_memory_p(unsigned long *addr, int numpages) -{ - return change_page_attr_set(addr, numpages, __pgprot(_PAGE_PRESENT), 0); -} - /* Restore full speculative operation to the pfn. */ int clear_mce_nospec(unsigned long pfn) { unsigned long addr = (unsigned long) pfn_to_kaddr(pfn); - return set_memory_p(&addr, 1); + return set_memory_p(addr, 1); } EXPORT_SYMBOL_GPL(clear_mce_nospec); #endif /* CONFIG_X86_64 */ @@ -2108,6 +2103,11 @@ int set_memory_np_noalias(unsigned long addr, int numpages) CPA_NO_CHECK_ALIAS, NULL); } +int set_memory_p(unsigned long addr, int numpages) +{ + return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT), 0); +} + int set_memory_4k(unsigned long addr, int numpages) { return change_page_attr_set_clr(&addr, numpages, __pgprot(0), From patchwork Tue Jan 16 02:20:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Kelley X-Patchwork-Id: 13520333 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF85BC4707B for ; Tue, 16 Jan 2024 02:20:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 310656B007D; Mon, 15 Jan 2024 21:20:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 299576B0080; Mon, 15 Jan 2024 21:20:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F1BA06B0081; Mon, 15 Jan 2024 21:20:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id DBF3C6B007D for ; Mon, 15 Jan 2024 21:20:35 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B1D1D140468 for ; Tue, 16 Jan 2024 02:20:35 +0000 (UTC) X-FDA: 81683570430.30.D999861 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf01.hostedemail.com (Postfix) with ESMTP id BE7AD40005 for ; Tue, 16 Jan 2024 02:20:33 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mDi6lq+j; spf=pass (imf01.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=mhkelley58@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705371633; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bse0/qXZQoiuVJq8kHe7VoVGITL3RsI555OnNeb2YB0=; b=EKKF+ScKVWLG5Lu+LTilA8+SNyEmp5ENRSOCKTJACAlj7BUh86zRWEHa4rEAprSE6XwuSL 7spAzVYf1FuFieqiaAG78G5HxZztzXTVZYlL+OsKEOVHBg2KKlrk8q7O0l/A17yY7qpgFJ hbDt8453Nj4dKJtIMjftzrhdjKf9zsI= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mDi6lq+j; spf=pass (imf01.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=mhkelley58@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705371633; a=rsa-sha256; cv=none; b=LZ+JsrW1aepEGqIHoRw5gM+s/1LFB+dBRCkoMlGVQ7XjdPf0RpwP/GmgueZIpod30JkIcT /YpNvMRCSWWI3REoVh1H9BEo3hZbDZASizsZ6sz0paK7DB5zO9crdvYEBQFhWY/6IPnbFd 2/CKYjwypIWP8coBG8c70Z6y+RJPH/k= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6d9b050e88cso4999336b3a.0 for ; Mon, 15 Jan 2024 18:20:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705371633; x=1705976433; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bse0/qXZQoiuVJq8kHe7VoVGITL3RsI555OnNeb2YB0=; b=mDi6lq+jNBOqyviqSP68LkcAZNQNCHU4kGSRM8ZnS+DtmXFnkhBzeJDdwXSW0xlz24 FoMZ6Eurz5QwPCqMjVEDfJMZYo+gf/Z41qNkFDWj27tCGGB4kT4M2AboOJ3UcizBzMsj kk16S8xtwlmfSaVsSR9KPD9MDsGMbXCVFhlwwJZLIf2Q01GS97Fd+g2YrpWM6AtM6E6l tkxIyS6eWx2RUJTcw2Wc1v9tFvem56o2eawp4i1Jv1IJMLY5Udw6ahgn6h4oItMhQc17 /WSvnYCeyQmrvL3j2YjGXTnw/jRkUWqey1dEL1R8oUW06OxVxXI4q5rxK5YSe90V76Dl nNoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705371633; x=1705976433; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:to:from:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=bse0/qXZQoiuVJq8kHe7VoVGITL3RsI555OnNeb2YB0=; b=YgeezzU2CmogZCSkmjJSxm6OpNQYoNtUJ67z+R77ahF/ix6bEdJlUPXy3g3FsGBHrk rsNvAeVFqHOmsJ1ye6Bfs8O9VnTenNoNhd8zMnlNH5lS/hnEmFBok0knuMFSsviy90W5 68/oXiW/+qfOQ8HEPxk9e2XGKgizkLOaIw1b8x/866DVpSPsGqc/q3/MMEyCqKzzmH36 k4x1gbjeSaXzES6cNHC8OsIg4gAxbgb7TwoCeg4r9yJuzbGA4UgoTjW2i/nD31JZ6EWu KRCjK6dcI4bFix0ajdY5zE0Wub3LU1nDJEkQlk8Fm9dWKctKaWC9Je5YD+V4Z8pKQuvE xiAQ== X-Gm-Message-State: AOJu0YzhzW61PxRNJpxOvXRg784WpwsfrV4ISzYKdgJIdhaiuHtQPZA8 49Hk9bwstG8cxkFcbiy6kDc= X-Google-Smtp-Source: AGHT+IEr2+CDYGS0V8/X7I1MGszR2ZNlclrOwLdx0/MeFP/gOuYxw/HddIq+C+XRl375qrBivD1cYQ== X-Received: by 2002:a05:6a20:e618:b0:19b:1da3:bafc with SMTP id my24-20020a056a20e61800b0019b1da3bafcmr760682pzb.4.1705371632710; Mon, 15 Jan 2024 18:20:32 -0800 (PST) Received: from localhost.localdomain (c-73-254-87-52.hsd1.wa.comcast.net. [73.254.87.52]) by smtp.gmail.com with ESMTPSA id kn14-20020a170903078e00b001d1d1ef8be5sm8193379plb.173.2024.01.15.18.20.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 18:20:32 -0800 (PST) From: mhkelley58@gmail.com X-Google-Original-From: mhklinux@outlook.com To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, kirill.shutemov@linux.intel.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, luto@kernel.org, peterz@infradead.org, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com, thomas.lendacky@amd.com, ardb@kernel.org, jroedel@suse.de, seanjc@google.com, rick.p.edgecombe@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, linux-hyperv@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 3/3] x86/hyperv: Make encrypted/decrypted changes safe for load_unaligned_zeropad() Date: Mon, 15 Jan 2024 18:20:08 -0800 Message-Id: <20240116022008.1023398-4-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240116022008.1023398-1-mhklinux@outlook.com> References: <20240116022008.1023398-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com MIME-Version: 1.0 X-Rspamd-Queue-Id: BE7AD40005 X-Rspam-User: X-Stat-Signature: xr9nany1zrr7qhmpnw5scdx8zqb3bm3c X-Rspamd-Server: rspam01 X-HE-Tag: 1705371633-809390 X-HE-Meta: U2FsdGVkX187ewGZtn+Q2d5FEb6ZN5jGLBKJi5R5w7FXvdhlXChGv/Fkl//fbz4doX78gO1N77OSAMq8dSZEUGIf5gcyNZYi3MBXxkDfhMdTTpmSNAiis+m14JELx58w0qKmIic5bYbwe9UJNlIk+Nms/aXJ+ISvtJ97FTUQua7xz3tZMXfuxJQe0mIAU5JVtz5m++76Wl58VJcWPp+9S1SGyOEDiC85Atz7osrdgbMMR9SNxRy6xiBcjkWg8tHcvtzBqZR47jo32fkBEzn0BPg/1HoreKVmHxeV/H5V2P4ZIaQCih3dvycBf3FvZQQseC1kqHyY3K8C3ADSWq2bsn1bjQm3nEAG2O77a0sAt5meQyJ6dbZuFMSJojtsJyaBElFooCfy191WXF+o/LDRxRaaSxMlTmxgfDK9FXTx+xxxP0vwbK8s+ej3LqLihS5SbqcpekhfQX/HXMO8LJz24E8WVcO3zSvm0PQSgiJPZjpx9iKIHco4OgFDganeUX7iRWbjo3ekBmVBA+HLvSAC0Jil98EcCK7uzOtSnb5PuwvrKlCiaXhsJJXycxZPLR1rlmHcNr7TnvcwYdtrbQqeqtjaMSzIS3kju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=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Michael Kelley In a CoCo VM, when transitioning memory from encrypted to decrypted, or vice versa, the caller of set_memory_encrypted() or set_memory_decrypted() is responsible for ensuring the memory isn't in use and isn't referenced while the transition is in progress. The transition has multiple steps, and the memory is in an inconsistent state until all steps are complete. A reference while the state is inconsistent could result in an exception that can't be cleanly fixed up. However, the kernel load_unaligned_zeropad() mechanism could cause a stray reference that can't be prevented by the caller of set_memory_encrypted() or set_memory_decrypted(), so there's specific code to handle this case. But a CoCo VM running on Hyper-V may be configured to run with a paravisor, with the #VC or #VE exception routed to the paravisor. There's no architectural way to forward the exceptions back to the guest kernel, and in such a case, the load_unaligned_zeropad() specific code doesn't work. To avoid this problem, mark pages as "not present" while a transition is in progress. If load_unaligned_zeropad() causes a stray reference, a normal page fault is generated instead of #VC or #VE, and the page-fault-based fixup handlers for load_unaligned_zeropad() resolve the reference. When the encrypted/decrypted transition is complete, mark the pages as "present" again. Signed-off-by: Michael Kelley Reviewed-by: Kuppuswamy Sathyanarayanan --- arch/x86/hyperv/ivm.c | 53 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 851107c77f4d..95036feb95e7 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -502,6 +503,31 @@ static int hv_mark_gpa_visibility(u16 count, const u64 pfn[], return -EFAULT; } +/* + * When transitioning memory between encrypted and decrypted, the caller + * of set_memory_encrypted() or set_memory_decrypted() is responsible for + * ensuring that the memory isn't in use and isn't referenced while the + * transition is in progress. The transition has multiple steps, and the + * memory is in an inconsistent state until all steps are complete. A + * reference while the state is inconsistent could result in an exception + * that can't be cleanly fixed up. + * + * But the Linux kernel load_unaligned_zeropad() mechanism could cause a + * stray reference that can't be prevented by the caller, so Linux has + * specific code to handle this case. But when the #VC and #VE exceptions + * routed to a paravisor, the specific code doesn't work. To avoid this + * problem, mark the pages as "not present" while the transition is in + * progress. If load_unaligned_zeropad() causes a stray reference, a normal + * page fault is generated instead of #VC or #VE, and the page-fault-based + * handlers for load_unaligned_zeropad() resolve the reference. When the + * 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, bool enc) +{ + return !set_memory_np(kbuffer, pagecount); +} + /* * hv_vtom_set_host_visibility - Set specified memory visible to host. * @@ -522,8 +548,10 @@ static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bo int i, pfn; pfn_array = kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL); - if (!pfn_array) - return false; + if (!pfn_array) { + result = false; + goto err_set_memory_p; + } for (i = 0, pfn = 0; i < pagecount; i++) { /* @@ -548,14 +576,30 @@ static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bo } } - err_free_pfn_array: +err_free_pfn_array: kfree(pfn_array); + +err_set_memory_p: + /* + * Set the PTE PRESENT bits again to revert what hv_vtom_clear_present() + * did. Do this even if there is an error earlier in this function in + * 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 = false; + return result; } static bool hv_vtom_tlb_flush_required(bool private) { - return true; + /* + * Since hv_vtom_clear_present() marks the PTEs as "not present" + * and flushes the TLB, they can't be in the TLB. That makes the + * flush controlled by this function redundant, so return "false". + */ + return false; } static bool hv_vtom_cache_flush_required(void) @@ -618,6 +662,7 @@ void __init hv_vtom_init(void) x86_platform.hyper.is_private_mmio = hv_is_private_mmio; x86_platform.guest.enc_cache_flush_required = hv_vtom_cache_flush_required; x86_platform.guest.enc_tlb_flush_required = hv_vtom_tlb_flush_required; + x86_platform.guest.enc_status_change_prepare = hv_vtom_clear_present; x86_platform.guest.enc_status_change_finish = hv_vtom_set_host_visibility; /* Set WB as the default cache mode. */