From patchwork Fri Jan 5 18:30:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Kelley X-Patchwork-Id: 13512438 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 C5397C4707B for ; Fri, 5 Jan 2024 18:30:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 012E16B0246; Fri, 5 Jan 2024 13:30:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F228A6B025F; Fri, 5 Jan 2024 13:30:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEA176B026A; Fri, 5 Jan 2024 13:30:44 -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 CE6B26B0258 for ; Fri, 5 Jan 2024 13:30:44 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E496940AEA for ; Fri, 5 Jan 2024 18:30:43 +0000 (UTC) X-FDA: 81646098366.02.AA83E5D Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf14.hostedemail.com (Postfix) with ESMTP id F2DB1100013 for ; Fri, 5 Jan 2024 18:30:41 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=J8HpHhMc; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.215.178 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=1704479442; 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=3sjtnuQv0aW8e3tX/O8I3CPvFIKJ9WnNTakGmnE/PQ4=; b=m/kWvQs1eFB1xaB/TMYW4XfWnSbZu3gjVNfnGHt6ENk9biITg2Pij3/wT4rmdCND+cgyFh O13AsnKXAbCnYvg70Fv7jiYK4J7bTKOCrxvj937DeDm/ZIY+6PGapv28Bq8xsjteciosXb 4YdKJFgCBjzv6mXM8ndXYEgvR2D8bs8= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=J8HpHhMc; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=mhkelley58@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704479442; a=rsa-sha256; cv=none; b=T/Aw77/6fm7CLCDZAuPQBMVzyJFc1s9iUu2FUacOMF806wMCFvEGWkiZ6aMgoWB16bqDlg rLDHnm3BYqzpNVDS+zCrmmtccYH3hCMvPXHxUQj+qJtrrs2/998DChTGi8nwyZKJIoOjKX YaXG0SsDk/YOB2LXeAfZp02R3nNIAt0= Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-5ce2aada130so1111745a12.1 for ; Fri, 05 Jan 2024 10:30:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704479441; x=1705084241; 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=3sjtnuQv0aW8e3tX/O8I3CPvFIKJ9WnNTakGmnE/PQ4=; b=J8HpHhMcpAYDn74MlcwHuBJzMLKfKAUHGPrPLnYQuUDk23xoXjrtlLs9ODDc6ByqJv G08JeWmHNt67ufVhstpwpOwSeiI9nGDmuNMuOx/HDV//hErskx6VKp2D5eVLzzlSZXOu MMMdW1P7hrlHxyQGqsV698lScvkQvLeezcq+Duwuk8MtxjpFU0fulYC3ZXg/Czrj1Xcz eCNVSY1zRhhoG2v1UrNjggU62uFOrK+q6sKAxIGPPU4lfPO+fA98IEK12M4jtJh/Lbtx N/AswrZqPI1BiF0Qw620N9/FmxL+EaQpfdxl/3yBdjJhViM0/zX8I6UcJoAwybf+BsoY OTbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704479441; x=1705084241; 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=3sjtnuQv0aW8e3tX/O8I3CPvFIKJ9WnNTakGmnE/PQ4=; b=NrrrXIhhyyXjYvd9kMHPoHhPYrX08B1gZUnRPd+PHH5XjvmBE1L48HRk7JWwAnoDGh qbB7Od/HUynaoa2ik7EhK4yfepfGIrKkY2TXo/lTjUUvOzWTKNmnayc5gi/tPta2wnXF g7Ymh65EiOGPtXbVeI30AC3qZlx7mfqR3H0NjE7Q3YGAz4TgovSrMdaQC4GL0AfRrK7E 0Di6zPBIoaWl2NHv7iU1enWpAyjz3f8H+fif2CV2EvVX6dsR12zCwRGaYJOFtt+kXLD4 2IXLR+VaQPrF30tNMdvjbn+IMhPOpTZh/Cz+U5kAVFrlPUYz//MiTpfbV8/HY9Aujekk cRAA== X-Gm-Message-State: AOJu0YzTrgQf3YO3CkqkQOSqMYDI1wioRcOSISLMg3gDSVhNxooHyM8i kmzRRpNJPmkm1JIT5Pfep3A= X-Google-Smtp-Source: AGHT+IGZaFoZdBTf7Hf3s9plZg+SJexjwJwB03dmWIM7+favexb3cAjsi05kOu2bP0EG5HtVpfK9Dg== X-Received: by 2002:a17:90b:1d0d:b0:28c:4a67:eb7d with SMTP id on13-20020a17090b1d0d00b0028c4a67eb7dmr2069534pjb.48.1704479440839; Fri, 05 Jan 2024 10:30:40 -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 23-20020a17090a195700b002868abc0e6dsm1687293pjh.11.2024.01.05.10.30.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:30:40 -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 v3 1/3] x86/hyperv: Use slow_virt_to_phys() in page transition hypervisor callback Date: Fri, 5 Jan 2024 10:30:23 -0800 Message-Id: <20240105183025.225972-2-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240105183025.225972-1-mhklinux@outlook.com> References: <20240105183025.225972-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com MIME-Version: 1.0 X-Rspamd-Queue-Id: F2DB1100013 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: g954racdtra13aa8tai3tdn9zrt5tqiw X-HE-Tag: 1704479441-184957 X-HE-Meta: U2FsdGVkX19hnoRmIrnaUfUuKRjLDHS1wXa9Uoj+8eZqUSv+GhPX58Q8ZtR1YUNEY30GTNhkT3chC4zdCG9LNnYCNq1uMW6yA/CRkDghQLY5WLlVGOMB+M4WlQSXLWl+jY9oOz+NmUqTgKTqj3lr5P9LqnSAoVdFWQoD5a4APZ8ggRwJbP7gcKUo9JM+o4RUKlIgWUiei7cmBI188Jlj7FQdWOo7FymOCUsCUmZXWSDm2xjA47QpMmU8vX1UI8FuzN9Nb2eYcGdHRPnTJrU60Mbi7Pd2RDMN4YDmy8ZxhIMPA/DP7LxyJHSbR91bQDSDxmqasrLurpYdgFMWy14zrD/2P19HclMWlGTxAsbLHbwwOBT3khFDgKsMSnpMbgUROqkJoRjH7pxdOyzLvD1ZqY4HlgsqvsPVnMM5hpfWp1GdB958SEJTB8qMTO4BQVFFGcV928vvbk/2DTsetHRdG+djbBLAJXVBKwuH4JqO0E7V4oqlSXGTM34tSwtJjZVKBeDVXs4ta6HyaZjALx5WircQAAU3JiQtOOfd9BFUgQpQF/juGnPUTluHvlr9mXIdY1jvSz9QMGqzE3EgaCc98glIPn3l65kmsc9m/QK2Ujg+tJgG73xOkd19vQsv3w4s6P46B6fUVvARMc5CHpOoMGGdSxzYfmw03sbYMQKCfEGbvMEVWMqPszx2SGMZHq+Tg8eA8a2WtmAevBnwEOp5WUIFPkEpgsTj/66+GpDOjBlGRvY+AMgpflwFNdxtotbPRlRt+OvDCNk/FLoNq5MqHQVIyQT9glRLzxUdtLRWgebtzXsZId6kE3DcjvL+A/gvfWKaK69tWEfTsXV73HF/IrgRiwf3qIETwNkYrFV9O/oDNhPVpQYhVzNpzPf7cVdqdAHwWWodjCiYPvi656B7XjanPmQCXkSi6OaimkEKGWY+cDsPhAW2HM2jOtG+rZRbL5Owzn4BYTb91psyhXN Od4EYy05 tKGCRJ0wCk3csXPK4FvCJ4oQPim58jVd1SrdpidLD3BPb1qH2MChOdfC3TPH+68RHq7YFKCexJmqChvAdwVNddTpjFHfsCB+B1MQkD/RU36c88Dp0Gh2IQ9CznZBg3YvDcSeUnAJK6fMvgLjnaktPpPl+fEZ1bvnCpYKuaTSCyrgx6GRbD5Vu4Prhb0kMd7rVjFaMLbQa/GmD+Ed0kYnBsL/cDPAAPJ/u4VRMixyLPKebl8t69jcuYdvdZjHWPNIbABgOso5F8WR5YZs5jadwYU+OPSE3pPKFlGij69PtOPHB/shQ6w2GicLq/iaS4o1xhW43OZ28h5Kyel350ewAeTTy/LFRbJEwbpn7f6Pm9UaA2hDn1Cun2uLs5C3C7+zx7uBouSySMGt4Xd943BeQDcfDj8XzykcOl5cvydz4oO6+C+n4l/t0Ty7+678sBRztIHEr2q0IcO776/0wvcdCG4bOGddcpYiQUu/FeNspOr+M9gM7aUuls8QEi4ToIbVASbnuMmV9uqn4IWI6u6QzsKB3Qg842MK9G+40pP69BLd9X84oQB/8G7bsivFeYdhP/A3OOszwQcBPX2LcJkO64vVuuhXQV9Ew14yebBqCJTNkUQr5uL+Sl2+DNA== 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 --- arch/x86/hyperv/ivm.c | 9 ++++++++- arch/x86/mm/pat/set_memory.c | 13 +++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 02e55237d919..8ba18635e338 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -524,7 +524,14 @@ 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. + */ + pfn_array[pfn] = slow_virt_to_phys((void *)kbuffer + + i * HV_HYP_PAGE_SIZE) >> 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..8e19796e7ce5 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -755,10 +755,15 @@ 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. + * It is also used in callbacks for CoCo VM page transitions between private + * and shared because it works when the PRESENT bit is not set in the leaf + * PTE. In such cases, the state of the PTEs, including the PFN, is otherwise + * known to be valid, so the returned physical address is correct. The similar + * function vmalloc_to_pfn() can't be used because it requires the PRESENT bit. + * + * 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 Fri Jan 5 18:30:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Kelley X-Patchwork-Id: 13512439 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 6D912C3DA6E for ; Fri, 5 Jan 2024 18:30:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D51036B024E; Fri, 5 Jan 2024 13:30:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CFF596B0258; Fri, 5 Jan 2024 13:30:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B7A266B025F; Fri, 5 Jan 2024 13:30:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9BC736B024E for ; Fri, 5 Jan 2024 13:30:45 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 66212C09BB for ; Fri, 5 Jan 2024 18:30:45 +0000 (UTC) X-FDA: 81646098450.15.0554DEF Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf04.hostedemail.com (Postfix) with ESMTP id 3914A40022 for ; Fri, 5 Jan 2024 18:30:42 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Uij1az9e; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.216.43 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=1704479443; 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=lydFpi0OZX3PRN1Ii8TvNVEIYJGkasxvD7FkCrzWjvc=; b=t1XrhhJ9i7LrORVDTglhAKEO6zceR/X8QMzlxb4uvWuitFFUCzh82EdWRLIK6jHhPGH0hl s3lOozXC4zXvzqS1AhSXUSXsRT0lrxbJT2FrCNxgRlnngOJ+LeStzxDDkLPDc+JnHRmpYM S+3TlhUTahGQRzz6OwuV/Lt2BJAuIOs= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Uij1az9e; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=mhkelley58@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704479443; a=rsa-sha256; cv=none; b=hgGE83eamjDbFtBt6ep2ZIYUFZ2kpPZl2msKjq0hEv5pEUpGFeA9e44Jp3AXqhP8JcJnax B3NRi0Bn7IMOK5weFn04bftwt5/8ZyNz9dsrzMoQC4uPP6IE00UmtGoO8NXpiEHlOU3ZJG FiR2qjXtM0Gqm9lSphmcF0vmQ8zS8RE= Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-28cba988d6bso1331241a91.2 for ; Fri, 05 Jan 2024 10:30:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704479442; x=1705084242; 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=lydFpi0OZX3PRN1Ii8TvNVEIYJGkasxvD7FkCrzWjvc=; b=Uij1az9erj5pQy32UFaezEVgq7mrHai7hdv7UTmxKAG0FgTdzZBAOQFVPhMk42tBZl zDUtXJ6ijJhP/DtCMfQHT4aZXbdNBOjq+nmJr95iFttQFUCuo3wmSG/XHt7J0pUX0Hh+ BLieHtrJYMsRP4YfggpeLn/OJUaOqRe+u7c8piKCSxXUUWc7GyIhVsgaMY1AJRuPdEiY zevDNkYbkU/O3NbKGkhUuw1jPMp9sxJqVRFaPfKn7Tjmfl8f3JKf03vj1293A1CJWi8B CazxQ9cLZZEDEpCy9Lmjzt2F0U1g2PA9bNQE5L159E5r7atT3tywLVhZM8HX78UHTkwC CYUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704479442; x=1705084242; 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=lydFpi0OZX3PRN1Ii8TvNVEIYJGkasxvD7FkCrzWjvc=; b=VO+k16mssF4QkAynH2Kf+2sOcK+hv2xm/iaL1aEltCugWFgPukBgNbMwn6WJGXz17m CN3D+pUhpTJOcJs67G8+pZn5gvarQIy7WPt2XYcvYicfSb9WsWmj+IMzUyZL9+zIMzP8 tm+s3bIubg9Tu0pi2B1IfSAHO1zlr4FGzoJ29slcz7b2BiKMKWLrqt8av4HprSs7kegU YYaoxKSwSLw0en0nEUXZkrPETh297OC3/PVr1Pb69eLyKqO5DJ4RMFqafYZhNe1XrirF JIryci7PGFtkVBcoUDY11TPJNIbWt/lyNormd6UoCdotkUsZ0iSBEmpAquPRQrlbcliC mDbQ== X-Gm-Message-State: AOJu0YwU7Bk7+iJdZD5AbFGlG9zMVatF2aIZVhzd8DbDECpyqG2d7dVl 6/rD4GCH3szL8B3/BRHX1Ik= X-Google-Smtp-Source: AGHT+IG19yTmGDm43gGH0Te1ADwX3xmhZ2HI5+b+LjzFdjx7tfO595u+C0rtiQTuBnjZcxP5J7fOYQ== X-Received: by 2002:a17:90b:24b:b0:28b:c9fb:e328 with SMTP id fz11-20020a17090b024b00b0028bc9fbe328mr2079013pjb.51.1704479442131; Fri, 05 Jan 2024 10:30:42 -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 23-20020a17090a195700b002868abc0e6dsm1687293pjh.11.2024.01.05.10.30.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:30:41 -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 v3 2/3] x86/mm: Regularize set_memory_p() parameters and make non-static Date: Fri, 5 Jan 2024 10:30:24 -0800 Message-Id: <20240105183025.225972-3-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240105183025.225972-1-mhklinux@outlook.com> References: <20240105183025.225972-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 3914A40022 X-Stat-Signature: ww7psts8wsxby1zqdzauthshtquk3bdf X-HE-Tag: 1704479442-953059 X-HE-Meta: U2FsdGVkX19dVybIvL7AjP1I8MqU9Co4dnaEXIZKmJdnpSPSi58iUeZUuRkEFEYNLV+AAF9mWOtbSaNlrHrJG4ck6SiJAppAN6EzWEsbm2ZOZzuCDT2CduKdvyrlRYOas4StzFHZ2zgaGW1H+Po97bIzI5BZ3aHtzB4SexD4jy/VjrRcfQv3JeM/WuZWTlhpR6YRt5gPvhe3Au1OyGXyaJ0IiAIKdxgcwods2i0arWsCzFmA3SjzNE+pJogTzIJaFU7FmQcatZpOLEzlppczIRS5Avvoap7xhocKpcnvw1QHU3MJKR/IAf+ZCkV+3n7uvfr6e5Xr7psVxQ20MDZvv9CbRr3uec8OV7lPLjB6W7Uj11ng2RWYTUGf5RE2MNpyitwErLdVg0MDGRwxqlWvFsMn+zjLUne7FPRahkvaERTVAybua1s0r/U0dpbIu8nPKr8p6MLvgoCVU8d6XScOwDtMgYP6m+C3KdufuT8E3t8bHl0YybtZjkvEsYWBUpb9o0nZzH2A2IBhEbqkU2/nG4FKNH/xDU+OhH4DB0+YfoLTrbsgPfmfKV5Jb+vIBFMyUGjcgcQgO/bypz42YCpxANHYTzK5WThD4r6c/Fbfwqoi87PdMTpcJWD990OG/SyBhzMYg9Bjt3Ft/o4p5dXVoENyhof+l4oZ0Ci/sjVhkSnkRWnYQjFh12jXodeTD3yvTTsWUGdbVnNZWQuHEVh4Bf93DtqwBw/T8EcjU+b9bz3Pm5l9F71HdT2mWuYvtCDgwRisxAePUK65F7prXkc+SUKkAoyx+NrsmZgSgNeLdmXYI8hIOcYe7aoWWK+36FeF0eC2/mUBMx2apRN1hbPbZsRL1IQduvBbfSTGQloqmoAWPXwRmSOquuIFqHMaVJa8zWkIWVQhsKZUfUbhKAhCD9lkFaWJRC5eSyHTsr/afoN4n5Y5jSQ865XJ3O75pmOvOIMuCpAnKzqHVFXvp4C h8X7DXJU PWDaads+RHBE9qGQokjHkfnDtq/eWHW31v/qaLEZoB8QCgozo796yT9TtCl/ZV4k9YRltB1ZKxZh2Wwy1hZowEoj4ScaNQNdwFD8I1mdEL3Ou6upvd8iTqC0yJ/0LO7LeRbDZeFwV44Y3oapCMWyJD16Zq/IZSiOMupfwVCVm2vgfjEPWLXYKJ+jPJkNRxXmkbfBBQqI00snCK/b1iXhfIX/gUycmYGa8m2olk11WxZKG18i/f773ww45PTq9dvhWUiUmBpgDBWiP2X2c7Ktch0tBPzbQfqnUTD6TDqgMvzfs7L7DwVEUKHQXGrzF/MRSFd50ijV/B2e2n4dagVT0hp+HziI025n+Vpd2r6OUr/R74LENOs5t8PYV5FV2+xzo2qfACBd041/sfTqF1KOO+AG8DP7InoHILvZ3fW4IdhfgpahxGuYfRBRoEK3P41s5S5OmeQnXHeigCGKyjyWKA9UTPd/xcDxqld16sDwXOD9lkASK5w124SuNY2BkbUF7rhhAjwmK7Go4+UAYihmFOrY04FhlV6tlx42VB3AEyc4eI0rm5l71FJRipHjqLMp15GSjVB7Mwl6NPvGXwmN4UDp7g8rp7w1oyAt5RaBLaegJr3R8BF5JGUThpQ== 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 Acked-by: Kirill A. Shutemov Reviewed-by: Rick Edgecombe --- 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 8e19796e7ce5..05d42395a462 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2046,17 +2046,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 */ @@ -2109,6 +2104,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 Fri Jan 5 18:30:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Kelley X-Patchwork-Id: 13512440 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 76FA8C47077 for ; Fri, 5 Jan 2024 18:30:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9E826B0258; Fri, 5 Jan 2024 13:30:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D76F26B0284; Fri, 5 Jan 2024 13:30:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA00C6B0285; Fri, 5 Jan 2024 13:30:46 -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 A73EF6B0258 for ; Fri, 5 Jan 2024 13:30:46 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 70B2116076F for ; Fri, 5 Jan 2024 18:30:46 +0000 (UTC) X-FDA: 81646098492.02.97AA222 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf30.hostedemail.com (Postfix) with ESMTP id 8A5688001E for ; Fri, 5 Jan 2024 18:30:44 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=DKmoTgPv; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.216.43 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=1704479444; 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=/DDBxixv8I/6JBD0uZ/FnCePGFgbAZpyF75SjVpOH3s=; b=maj11rRn3AMcsc0Ccd8ybmfV9L4wvyr2SMDwFCPy7WcI/Qy442GXiseGrn7Yta1COueue2 RBlAMTOOAbSjj7Ocvt0VM332NbErtQulfZlkjQIk8OwRXAcMUvfGoh2DU8PlFMgPP5Edkw 7Th++bFTLCaB9imvHOr5EVS5j5w65Sk= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=DKmoTgPv; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of mhkelley58@gmail.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=mhkelley58@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704479444; a=rsa-sha256; cv=none; b=bJLttz7rD6maYA/Iqmq5FE28VZ4byaEc4X7Sb2CqSMezSQMJ/HsVQT4uswu5l0xpAHRxX0 M8bYTprYiK2ReHY1m6FT2tZSZBMSOXkkIoZ8CuXKev89wM9/vp2zcHbpvLL0tt7ou7/AqF q8lrDYVdISyr6K5npuD4QiooD3NL5Ak= Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-28cb3bc3fe7so589492a91.1 for ; Fri, 05 Jan 2024 10:30:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704479443; x=1705084243; 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=/DDBxixv8I/6JBD0uZ/FnCePGFgbAZpyF75SjVpOH3s=; b=DKmoTgPvjXF/8oA/4R0l9jiZpPrhRARoJFDVf8EyaWHFLGkqnJAYSl2nAls3OR1np4 eSNskbToO9ugmJXuaR7T/Lxg7MAKsifylA2zG+oIGt1/bveT/stTk0MfyloMH1ze84Vk 6AheZsDWRYODt6NY2ol9tiV+6uMds+8S6/nvxjvM4EbmFQvgwv0li6HpZlTmPc3KkVuR 7pp7pXxycsyaQc9BuuFm5iXy4pFx2OUDhaWEqmA67BaoieZ52ACALyOy61f2jVUsGFuQ eKGZC0OtE4st4UXwJZ9j4Se2bM2NYIVMaumpA136dBMq77tk/hBapObF4XTrT+0gR+VC 5prg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704479443; x=1705084243; 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=/DDBxixv8I/6JBD0uZ/FnCePGFgbAZpyF75SjVpOH3s=; b=WVj1XmEtzwFxBXrisas14N8DYNU7JQQgVQYKBPImE7gDl8zzyX1fM9L7/h1a2EGoeg MQt9a5yPi8B85FQTf3B70YlCvVCeahe+c1OBv71fs3KEs4ycbePJtFsKOeqTimwctlDq e7BCk/riQQZBM+VGExvhg+o0mmSB+zYLQSUug1ptIVNChFdk6s9+gAsgHL6dyC2AjSGO IG33ZO+bKBKN5TQFeqUNVYcIH2VB/5VIKyZjfMPemDbQS+SvnWdiG+MGVaTa0r/pAd6M oMrJbNrsP90SgrM51UsdNachJ6p3MWMkGyO7Cquk5o3gxV9+dwFTb1fdVl+25+HJDlji sfPw== X-Gm-Message-State: AOJu0YxHd27xbPRy8Qbe1QwOUdDaxK9cVz8c9RayDtXxXskioOModHoe Uhe+Bl9ZlSfz+hJovineEVo= X-Google-Smtp-Source: AGHT+IHZB8hxoZEO9XFseOS+q+ucI/+i6AVwNZFHhru24uO3VXpK5t8sRYDrNHcHS8sHO6AGVonlVw== X-Received: by 2002:a17:90a:de11:b0:28c:3620:b5ee with SMTP id m17-20020a17090ade1100b0028c3620b5eemr3251196pjv.28.1704479443490; Fri, 05 Jan 2024 10:30:43 -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 23-20020a17090a195700b002868abc0e6dsm1687293pjh.11.2024.01.05.10.30.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:30:43 -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 v3 3/3] x86/hyperv: Make encrypted/decrypted changes safe for load_unaligned_zeropad() Date: Fri, 5 Jan 2024 10:30:25 -0800 Message-Id: <20240105183025.225972-4-mhklinux@outlook.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240105183025.225972-1-mhklinux@outlook.com> References: <20240105183025.225972-1-mhklinux@outlook.com> Reply-To: mhklinux@outlook.com MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 9b5adkobq5gcicbz5f31xd1rhd3gs4ib X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8A5688001E X-HE-Tag: 1704479444-90321 X-HE-Meta: U2FsdGVkX1/CYJOIx1ycf/i8Svjk34qQF/agdCre82zgJgv4dZkjLM3abs/ZRuA4I+Y2ugx3Df2Jh9ybXu3wdmrEsinjGBG0kbgmxczDJXAzGHjXsTp+Pqsr57hZ8oTa78Jweh6XYeL3ZkuOMaSlhP8XPGH9QVjaDNTUWsXtngZNGv/yUl4oEOYuIV4GLLlu879PQi5H0GiyvSOS44YX/Ixc7eXc+3+hGfRnr21u5ONj2VW2puUGUbvWpaWEXKcRV3nY/vEzLMMW4m/xbNoFTEXZAOAqR0sdvbiD8wpvge3tOE5qJrLy2wIzYY1lf7kmsnZ8+n4cUvxDkQcttWxAvg41gF+chFptoe5NvTmuaXL15Uq0GfBPicg5RRgc0z7jmkekfFOebh9GFO02rTosIoEjag4QR2kVrGEWPMnNQJSB5Ojzl5MfR5xgNfWSyn0mMg6DNA4dlcUxZ9yuKWDnEhOji/ph4wCqlao5cLHZQzqUeJX2VMlwJB6KCN0tz1i7Tbwknoub8yqXIO2JIZnD7LeZzyCBIJI24RSAAFla/sRlO8glaogMwSYD4GyUPq91Or5M/0eP+Asu5XLp8MATT7WiyLqUArisCvszmGpexDLizclx0ica8Pzhf2lDbmyCjdhMr+fu74QdnnVpqdd4ELHaeWdamXp9E94uxy8lt4/HJqDiJsZoFdMD9rKKoHnS5D5lQx00WA7jd8PXJ4fv0lwn5FFKAFePBfzCjz24vZxKlRg+XWGY+YgSjhE841JWZZ5C9Pml47uDfVucL5gmXdjv3GKW1NYHDVfSbxMbk1RlTWsnqDoVta+w+Zj3cDuoMEZnXt4oODiDLkhKaoX578NrwXOdA/PNaFeqnq9OvXMCNtBen2Q5it7BoDbD0Y39a2JqkUMu1Pje15ZmS0s6X+Tkts/a3BExdWgNGvU+b5dJ0OPx3x5eh/TZ08HsxxjCXBNMYYs6gSpgCZ/SKuS 87UzROjs vN8oN8G+bXb/EzsnS9IQBFPYvDO3H3z+Ysf642h0yUwHUNrSo3kRztBtAfE0bUYxwygN72SF7fqVpzsOvecFmyh0Dp4NKHeoAbFq9naLAq3Ho1DbrqYuH3fSPXenPU9+CGlgxxYIruSV8FmhHWSGz5xoooqa0FMQTxLoAx3rC6oIBN4UGmkBAb8Y71xrptnRmejqMW+o2eRY/fHqzi2phVqWZZQhNM0RrKFj5qEY9FYQ4BdhDaFBvyMPlDF7i17rTNj6BLE1WmtsOfuTL/8gGSLa9N4WhMmuCYTN9f5Pfc+BDwabXYgnyC/6htEmXG0r/QQJayWTUR1OD74W+zN4z+97x/u1SDnYBJyZVDttEOFFWv8L3ZI9KvTOfq8vEW3+l3bfiKMFb+OzuLmCy/pVmT9rjw2zoWFzgjG+F9GXFrcaE8x6RA3if5D42LG+ljsInwnjluxx3a1GPVOkxHUBcQRMSGvNIKhujVj7GNp9iTwQzZ5nYOZjNYLFKC6I/UQfdDVREegv4fBVEkWDwIMhs5VXj+0lv1Eldoujbr5vUUZI7Sex9FFXFRu6caiDUPR96u5s687D9ejXb9Z//oVEPtgLfz65Sb4bSAdEY84cy8eJKOMD1zwlsIprYLQ== 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 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 | 49 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 8ba18635e338..5ad39256a5d2 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. * @@ -521,7 +547,7 @@ static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bo pfn_array = kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL); if (!pfn_array) - return false; + goto err_set_memory_p; for (i = 0, pfn = 0; i < pagecount; i++) { /* @@ -545,14 +571,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) @@ -615,6 +657,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. */