From patchwork Wed Dec 14 19:40:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073369 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 6EA80C10F1E for ; Wed, 14 Dec 2022 19:47:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CABB78E0005; Wed, 14 Dec 2022 14:47:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C35408E0002; Wed, 14 Dec 2022 14:47:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAE878E0005; Wed, 14 Dec 2022 14:47:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 95FEB8E0002 for ; Wed, 14 Dec 2022 14:47:53 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5DE15140381 for ; Wed, 14 Dec 2022 19:47:53 +0000 (UTC) X-FDA: 80241947226.12.968EEE4 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf23.hostedemail.com (Postfix) with ESMTP id 70FC1140019 for ; Wed, 14 Dec 2022 19:47:50 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=koQk3jhM; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1671047270; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JUgAP9c6+ee2LMTX4sIVCW+FM9ZjLBQ77Cw5MM6nbMc=; b=c+zC+LYaSsBuLWtLIQluuDekDjs2nx3PwKRX2yi/Aj0G+DlRNPmtYaaM/aWsrbiE3mPVRj NGfJn7IsYWWrsBHUZHVV0MwYK39/2JC8CXTFupvZsReDnkS2/kJKL2bOVGNEoKaKUTbfrh P/eHab7UB8K7BYz2qexNXaOnQrmuPSg= ARC-Authentication-Results: i=2; imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=koQk3jhM; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf23.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1671047270; a=rsa-sha256; cv=pass; b=i7d5gkF4Xz3Y3fzqdNnjCjuPAGT0RG2unw9egAa/vqXrkkDoSN9pN3W8akB6x3VFAqtAsi rbSarz2mv4ASG+VA61INYHU3idRATf6ojIKb/XYShcFUHA/0vOP4btyoN5Wso2N0Jv9Wag nagTghxvrvJlFTwK3lgQRqzgHj8hJpU= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T7YSf7N09G1GFr+wEm8XDMZSXq1SJmIOlIzoDXh2iBV3hZp3JzuaIcy2A+NrvOo8ve53iTxqHMAlAerG+H0oPEytv0LaxNBq2SUCJX9yIGZDxt6Frv9u+JaicWCXmx2gRvurWQ5BsuDrt/0y1KW7etdy5qmrMCA4lr1shKZd+qGlgsZh6cOVg/BJLO+q3+9kElV2sRJGimCRq3cer8dY/mokpES/IfC8mH+HD3/5w3EMhbScTlfrNkPaVDQbWMz0N/FapYoj59IIWYW9ftGOD8Z7N1grnyxzuwnDzUu1s7v9xjU7l+I1EvJpr82rW4N5OuTYayOvXmV/dgINFp0U/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JUgAP9c6+ee2LMTX4sIVCW+FM9ZjLBQ77Cw5MM6nbMc=; b=UEFjwtdANyx+JLRGs9zA0QOrU5eLpAU2/Vg7FL43qSaQ0NYxsny79662fhdgDjpusa4YYLSJCbZwZlVvhTJtSsPGyZl8gF84CzQRW7omHQ6najr1VQIf/SAmy5Rq4Ivh7Y52pj7GXtE9cK0iWxrR7kn67LjneCv3c1kjApygb8lGGorJTfXyIY4dIcBnsW1zCJYFxVkgnEh169QKWZyvYQXNJ0Npbvtzu4nnqUIul2x1SH40CfdiBuPH42N7+C2cTc8oAK1JBBagkKDeK2vm9B5zoS51x1mq3sijPfLBbfOhsC20WbR/nF5iiyyaxTE2uYu2FB43cW3kWjwMMOuH6w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JUgAP9c6+ee2LMTX4sIVCW+FM9ZjLBQ77Cw5MM6nbMc=; b=koQk3jhMsrh919UaeAilJRpNP/IwnhEOykqsru/bH1o6yFsVW1PIA01XY6anes17LzqiilP/tWOanMU/uS3e9iCvsOUZUjQhNvaNTfbia2SmkcPyqbBhRiEAQbKGm5TsOaLbA5PeunmEuZe9sP+NMebCY3cAUCC/ULxCpdgGbfI= Received: from BL1PR13CA0117.namprd13.prod.outlook.com (2603:10b6:208:2b9::32) by SJ2PR12MB7894.namprd12.prod.outlook.com (2603:10b6:a03:4c6::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 19:47:47 +0000 Received: from BL02EPF0000EE3D.namprd05.prod.outlook.com (2603:10b6:208:2b9:cafe::52) by BL1PR13CA0117.outlook.office365.com (2603:10b6:208:2b9::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.11 via Frontend Transport; Wed, 14 Dec 2022 19:47:47 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BL02EPF0000EE3D.mail.protection.outlook.com (10.167.241.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5880.8 via Frontend Transport; Wed, 14 Dec 2022 19:47:47 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 13:47:46 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Nikunj A Dadhania Subject: [PATCH RFC v7 11/64] KVM: SEV: Support private pages in LAUNCH_UPDATE_DATA Date: Wed, 14 Dec 2022 13:40:03 -0600 Message-ID: <20221214194056.161492-12-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL02EPF0000EE3D:EE_|SJ2PR12MB7894:EE_ X-MS-Office365-Filtering-Correlation-Id: ebbfc3ad-dfe7-455f-9697-08dade0c136f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PLaeAO7TYsE9o2XaoIiKwk8zTmAl/zzqIh0eqH2LwPJoY++2mS/RAk8Q+Bx48m76okg5+ZS2B36u7fgp06Oqa6toOXDMODFXGLKhdNibUKSZfDJb3i4h+zT7vFV69eXeBUTQZfpDtZAAxFWS8LMJ2B8ZmTv+PQfUPNEY0WafPXmoDC7AwZMXP7Q0xMmgngHoLzo8vSOoFU3uSQvvLtBHIH+tSmJzbijkGSvBhHELxglTSwVcWR0t8gSLUhWORKnfLdSEdX92g5boVydYyCNvDIS8t4egMmC4lRZo6T+w79pL+Dm/T/31k9asqcnsa2UZrM+Oi19dJ9dLS564JeUjb3Y9k7p9+y8RRUE0nRjWtY1OYhSogAZL1u/FRxzv30j6Rhx6eQuyimjNODiRpFRIsUp/IDDoT7c3P3hW5Q5jlLdKNIVjGc985Ag9ewFTIcnFciItGrhCvrj3RWK6JlDl8FhGzZqmWvNRuzCWMkxAPlKHLyqLGDVNNoxSOMrMSkOHlwEwhpXES2j1aClhhGwB/fJn/eC9QwBGHguvL0q7J3LtlgMLC+QXOcLUU7aoChrRtmMjla9VARnpyBBb4M6Ha9hD7cYpSXWa2pKxanxbxjw7LSwjUk39AbdhSeZJqpp/a4/pxvENJTUQUOtkKdNkC+ZchpGroK7gDFAr+TA82GhdjDsyOmfcI7KxV3gxEmg6jccdvG4tZvdsTn+IPLKS45ooTAf7ZJzucDuoUwbbDNdvfhzdxUebPZfR2AngehlOe29DUfzPly9ZWenlfepTXA== X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(39860400002)(136003)(396003)(346002)(451199015)(40470700004)(46966006)(36840700001)(7406005)(7416002)(6916009)(54906003)(336012)(4326008)(70586007)(8676002)(5660300002)(41300700001)(6666004)(316002)(8936002)(86362001)(82310400005)(70206006)(26005)(83380400001)(426003)(47076005)(186003)(1076003)(16526019)(40460700003)(2616005)(36860700001)(478600001)(36756003)(2906002)(40480700001)(81166007)(44832011)(356005)(82740400003)(36900700001)(309714004);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 19:47:47.2775 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ebbfc3ad-dfe7-455f-9697-08dade0c136f X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0000EE3D.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB7894 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 70FC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AzIbE7KNf7fc2v0OD48oiO7qr9w== 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: From: Nikunj A Dadhania Pre-boot guest payload needs to be encrypted and VMM has copied it over to the private-fd. Add support to get the pfn from the memfile fd for encrypting the payload in-place. Signed-off-by: Nikunj A Dadhania Signed-off-by: Michael Roth --- arch/x86/kvm/svm/sev.c | 79 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index a7e4e3005786..ae4920aeb281 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -107,6 +107,11 @@ static inline bool is_mirroring_enc_context(struct kvm *kvm) return !!to_kvm_svm(kvm)->sev_info.enc_context_owner; } +static bool kvm_is_upm_enabled(struct kvm *kvm) +{ + return kvm->arch.upm_mode; +} + /* Must be called with the sev_bitmap_lock held */ static bool __sev_recycle_asids(int min_asid, int max_asid) { @@ -382,6 +387,38 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int sev_get_memfile_pfn_handler(struct kvm *kvm, struct kvm_gfn_range *range, void *data) +{ + struct kvm_memory_slot *memslot = range->slot; + struct page **pages = data; + int ret = 0, i = 0; + kvm_pfn_t pfn; + gfn_t gfn; + + for (gfn = range->start; gfn < range->end; gfn++) { + int order; + + ret = kvm_restricted_mem_get_pfn(memslot, gfn, &pfn, &order); + if (ret) + return ret; + + if (is_error_noslot_pfn(pfn)) + return -EFAULT; + + pages[i++] = pfn_to_page(pfn); + } + + return ret; +} + +static int sev_get_memfile_pfn(struct kvm *kvm, unsigned long addr, + unsigned long size, unsigned long npages, + struct page **pages) +{ + return kvm_vm_do_hva_range_op(kvm, addr, size, + sev_get_memfile_pfn_handler, pages); +} + static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, unsigned long ulen, unsigned long *n, int write) @@ -424,16 +461,25 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr, if (!pages) return ERR_PTR(-ENOMEM); - /* Pin the user virtual address. */ - npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages); - if (npinned != npages) { - pr_err("SEV: Failure locking %lu pages.\n", npages); - ret = -ENOMEM; - goto err; + if (kvm_is_upm_enabled(kvm)) { + /* Get the PFN from memfile */ + if (sev_get_memfile_pfn(kvm, uaddr, ulen, npages, pages)) { + pr_err("%s: ERROR: unable to find slot for uaddr %lx", __func__, uaddr); + ret = -ENOMEM; + goto err; + } + } else { + /* Pin the user virtual address. */ + npinned = pin_user_pages_fast(uaddr, npages, write ? FOLL_WRITE : 0, pages); + if (npinned != npages) { + pr_err("SEV: Failure locking %lu pages.\n", npages); + ret = -ENOMEM; + goto err; + } + sev->pages_locked = locked; } *n = npages; - sev->pages_locked = locked; return pages; @@ -514,6 +560,7 @@ static int sev_launch_update_shared_gfn_handler(struct kvm *kvm, size = (range->end - range->start) << PAGE_SHIFT; vaddr_end = vaddr + size; + WARN_ON(size < PAGE_SIZE); /* Lock the user memory. */ inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1); @@ -554,13 +601,16 @@ static int sev_launch_update_shared_gfn_handler(struct kvm *kvm, } e_unpin: - /* content of memory is updated, mark pages dirty */ - for (i = 0; i < npages; i++) { - set_page_dirty_lock(inpages[i]); - mark_page_accessed(inpages[i]); + if (!kvm_is_upm_enabled(kvm)) { + /* content of memory is updated, mark pages dirty */ + for (i = 0; i < npages; i++) { + set_page_dirty_lock(inpages[i]); + mark_page_accessed(inpages[i]); + } + /* unlock the user pages */ + sev_unpin_memory(kvm, inpages, npages); } - /* unlock the user pages */ - sev_unpin_memory(kvm, inpages, npages); + return ret; } @@ -609,9 +659,8 @@ static int sev_launch_update_priv_gfn_handler(struct kvm *kvm, goto e_ret; kvm_release_pfn_clean(pfn); } - kvm_vm_set_region_attr(kvm, range->start, range->end, - true /* priv_attr */); + kvm_vm_set_region_attr(kvm, range->start, range->end, KVM_MEMORY_ATTRIBUTE_PRIVATE); e_ret: return ret; }