From patchwork Mon Feb 20 18:37:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13146905 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 216C5C6379F for ; Mon, 20 Feb 2023 18:51:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AFD466B0073; Mon, 20 Feb 2023 13:51:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AAD876B0075; Mon, 20 Feb 2023 13:51:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 929396B0078; Mon, 20 Feb 2023 13:51:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 845B86B0073 for ; Mon, 20 Feb 2023 13:51:41 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 50816C0701 for ; Mon, 20 Feb 2023 18:51:41 +0000 (UTC) X-FDA: 80488564002.10.357A03C Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2082.outbound.protection.outlook.com [40.107.92.82]) by imf26.hostedemail.com (Postfix) with ESMTP id 5682D140011 for ; Mon, 20 Feb 2023 18:51:38 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="aTeGgX/2"; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.82 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676919098; 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=3nl9ReAu52r8NoHSvqQtO/vhnozQrmzY3lpXAFSgGIA=; b=uypzxq+XGadqlmdILDtMXQtQjjM+G3T6qeD0O23blfPGgCV2ler1D5ragShOIlfs3B9VLf iApgaCo7/dxJPOntsqJehZsZ+u7rS2tdmER3/KxXxfUwdXc+f/vI8k67Oa4sHZR9ArneQV FBWQsJz/fDVO9E0px/EtFwjW4Q+qSLk= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="aTeGgX/2"; spf=pass (imf26.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.92.82 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1676919098; a=rsa-sha256; cv=pass; b=VJhviTFXxlV/syqj75yTIDdJbLAVVxnAYGiJEXKw4MH/w8ps45bWKQgq5PhkBO1zSbP840 n0fwMfR3kwyB2DGSj9RFGNuUzt/N2X0BVgHhq7KtWHgA5Jp6gUg5v8575/AX8nPbavE+gZ kkKyau14L6aEo+HnkjScmF8lHySA3gw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SBaFVvxGVzAxIuGvMR7RPhwVAi4M+8L+o6DsaKo5Wt+2sBwUUOeLOsfwm/uXVpCYcJiWOirpfm/F+X343772CAPMdpmG6jYR/EEOld423YP4znDXQ6J7HWlSpjfQSZVVa4BBiERkJ2iM/N/gKLCntCmWHbNMNVBrIgT945PWxWht3DppBLjSbuB2YFg+UYyOi/fy5F9IWQ0kvY2buFuJP1Pg7CmSo7nbU3jyoB38aAYgryTRKoAtNUOLy9villAo10EJpTRLM/2NITfh1FKBYiDD6VufeIS18DQB2y+CHj6Hr1VGJnzd0/r3Hg6q+uH/MJJ5WBqKFWyn0eqQxKZWXw== 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=3nl9ReAu52r8NoHSvqQtO/vhnozQrmzY3lpXAFSgGIA=; b=A4ICQ2Egw4E9h1FsFNSL85Xt2SOf0jSeUaWmzWMti0BhKtbbwi9SVtGBTpcVSs/8wgfLJItow86W2S+F3tIdNe7i/iEPGcjr3D7/CgvPtidItjacY7fYGYH7vgTyYhehgLG/idsII4RsM/mOixSUflL1TlNOt4k6JALkSb4hwJ/0LrP8DaMAsINh7tjcKZGUbdpSqoHlqcsXjMnLWHuSC831SVwUtLtZ5eSJMclHxBDYJ78o5upnLDe7hMNk0mf9r9Fnnja1FU/By/5ixUYQ1aG7XzAgDKuPKs+jxoKRTN5tbc5ML7rcRdjlAR2ioTqWcOlEoKUsDLC01zbYfx/rsw== 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=3nl9ReAu52r8NoHSvqQtO/vhnozQrmzY3lpXAFSgGIA=; b=aTeGgX/2/DFuWExk1bptM3xwtYiFj6A73o1BzlLo0jFSHja/si0L/kBUQQZJjzEp1p3F8m6Qj0YzTjpvXO7+hQVs8Lr+fO+T0jTGUYYcCVYrsK+zaHkq9W1htYSpylyxbtrJbfPd9dbplahT+xPTf2gP69ouFV8wzsyY79iuCYQ= Received: from DM5PR07CA0064.namprd07.prod.outlook.com (2603:10b6:4:ad::29) by BL1PR12MB5190.namprd12.prod.outlook.com (2603:10b6:208:31c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.38; Mon, 20 Feb 2023 18:51:35 +0000 Received: from DM6NAM11FT014.eop-nam11.prod.protection.outlook.com (2603:10b6:4:ad:cafe::db) by DM5PR07CA0064.outlook.office365.com (2603:10b6:4:ad::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.20 via Frontend Transport; Mon, 20 Feb 2023 18:51:35 +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 DM6NAM11FT014.mail.protection.outlook.com (10.13.173.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6111.19 via Frontend Transport; Mon, 20 Feb 2023 18:51:35 +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; Mon, 20 Feb 2023 12:51:34 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH RFC v8 02/56] KVM: x86: Add 'update_mem_attr' x86 op Date: Mon, 20 Feb 2023 12:37:53 -0600 Message-ID: <20230220183847.59159-3-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230220183847.59159-1-michael.roth@amd.com> References: <20230220183847.59159-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: DM6NAM11FT014:EE_|BL1PR12MB5190:EE_ X-MS-Office365-Filtering-Correlation-Id: 829f19a8-d636-4e6a-cf32-08db13737db1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4sTWKOgJR4B/cQaq4zDslFBI0FW/VH/CBEeNBvUhe78h8cSEVm+KP+U2KnFQ0wURfOp5ivivfDU2eoAOdyY9hUCC4nlrJydjmxQwGAl0af36spOfJcawVmlki0V/IKU1K2N4ponknT7yJM4RXAv5jbPIASyDkJ6kNPk1ZND0zcLsMDirMqvh18V517BPGk9NhodxnxrTDo+Dk7/U5iBlH3deTRQ0l/O+OA9dJ5riRDiFb8WsbA82BFmJ9nkQLCOv9qaFQIqNXiVH+a4OGtIfvKJ/B+/UNT6kucfBx9gXXQFXPRxXd/pRGHhE7MVeMcy1+d/FmFJNjGxBGmnU92m3VbqZHLVq5EKaiGfD2Ih94m4Klbg42mBgwU4XTFQNFBbOZa9gVnUtMcUYHVn3m8LOlQc2g/9C+tB04Ej/ZwTUfrnsBijEg8AS+rpAvJrkpNwonyBMYV1pimtNAPU4s5coHsFUcZwA9d3Pn0nzxgjcLTi1wkgFV6GJhaZn4XQCvD3u0Zkfr4I7iQdNeX1bOamWuuOSjtBo3Z/1xUmASw+EBr0VKdzjRBsd0o9OXdpbgJyNH2Gs6Zol8h6lAhuci8nbPsh0jASMXms+AR10wFQUbeV5LbuR+ygjIGyT+y3G8/yhQA2B8CA/rQo1V+P7sPnUZ5tuUY9LjWKeyET/YtLC7TzDum2Di0hR08itnh0iTSO9WWruLy3MTcBYwqRnbQKksmCYpYZBCW0joQEhOJADLOc= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230025)(4636009)(376002)(39860400002)(396003)(136003)(346002)(451199018)(46966006)(40470700004)(36840700001)(8936002)(41300700001)(7406005)(70586007)(86362001)(5660300002)(36860700001)(7416002)(316002)(356005)(70206006)(8676002)(4326008)(36756003)(44832011)(6916009)(186003)(81166007)(336012)(82740400003)(40460700003)(2906002)(16526019)(2616005)(6666004)(26005)(40480700001)(426003)(82310400005)(47076005)(15650500001)(54906003)(1076003)(83380400001)(478600001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2023 18:51:35.3247 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 829f19a8-d636-4e6a-cf32-08db13737db1 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: DM6NAM11FT014.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5190 X-Rspamd-Queue-Id: 5682D140011 X-Stat-Signature: qiqx41imkff581ygggstxj4sss6eb5ek X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1676919098-383419 X-HE-Meta: U2FsdGVk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KDGaJY9QE9FGU/b0djndX4f4pxRPJaPBPeL0JvC3S1RfcOdAv/DN6GgYMn7qqUEbYVhAkYmGhVT/UAAVK9vSr4j/JrHH+BFV9DqwPE6nkDV9LFZdkxvzgLj/COy0jtdBwNwCiLj440MDzN77+4qQ0Db7BNk/d+QdfrnYpEuRDkU9A4UkYe4XfUObRqt60AR2KhasHuf7CqT0GXf8eF/yXOEKby1NkNF5Ibf9+ffrrKXTt8tJHQhDR0DDQ== 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: This callback will do any platform-specific handling needed for converting pages between shared/private. Signed-off-by: Michael Roth Signed-off-by: Isaku Yamahata --- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 13 +++++++++++++ include/linux/kvm_host.h | 4 ++++ virt/kvm/kvm_main.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 72183da010b8..a8aaf532c2ab 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -132,6 +132,7 @@ KVM_X86_OP(complete_emulated_msr) KVM_X86_OP(vcpu_deliver_sipi_vector) KVM_X86_OP_OPTIONAL_RET0(vcpu_get_apicv_inhibit_reasons); KVM_X86_OP_OPTIONAL_RET0(fault_is_private); +KVM_X86_OP_OPTIONAL_RET0(update_mem_attr) #undef KVM_X86_OP #undef KVM_X86_OP_OPTIONAL diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index f856d689dda0..2da3fb2d5d1b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1644,6 +1644,8 @@ struct kvm_x86_ops { void (*load_mmu_pgd)(struct kvm_vcpu *vcpu, hpa_t root_hpa, int root_level); bool (*fault_is_private)(struct kvm *kvm, gpa_t gpa, u64 error_code, bool *private_fault); + int (*update_mem_attr)(struct kvm_memory_slot *slot, unsigned int attr, + gfn_t start, gfn_t end); bool (*has_wbinvd_exit)(void); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index fb3f34b7391c..053bd77bbf52 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7251,4 +7251,17 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, linfo_update_mixed(gfn, slot, level, mixed); } } + +void kvm_arch_post_set_memory_attributes(struct kvm *kvm, + struct kvm_memory_slot *slot, + unsigned long attrs, + gfn_t start, gfn_t end) +{ + int ret; + + ret = static_call(kvm_x86_update_mem_attr)(slot, attrs, start, end); + if (ret) + pr_warn_ratelimited("Failed to update GFN range 0x%llx-0x%llx with attributes 0x%lx. Ret: %d\n", + start, end, attrs, ret); +} #endif diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index fdc59479b3e2..d200b8f45583 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2330,6 +2330,10 @@ void kvm_arch_set_memory_attributes(struct kvm *kvm, struct kvm_memory_slot *slot, unsigned long attrs, gfn_t start, gfn_t end); +void kvm_arch_post_set_memory_attributes(struct kvm *kvm, + struct kvm_memory_slot *slot, + unsigned long attrs, + gfn_t start, gfn_t end); static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index b68574ff6c30..8ec985f1c57d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2561,6 +2561,32 @@ static void kvm_mem_attrs_changed(struct kvm *kvm, unsigned long attrs, kvm_flush_remote_tlbs(kvm); } +static void kvm_post_mem_attrs_changed(struct kvm *kvm, unsigned long attrs, + gfn_t start_orig, gfn_t end_orig) +{ + struct kvm_memory_slot *slot; + struct kvm_memslots *slots; + struct kvm_memslot_iter iter; + int i; + + for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) { + slots = __kvm_memslots(kvm, i); + + kvm_for_each_memslot_in_gfn_range(&iter, slots, start_orig, end_orig) { + gfn_t start, end; + + slot = iter.slot; + start = max(start_orig, slot->base_gfn); + end = min(end_orig, slot->base_gfn + slot->npages); + + if (start >= end) + continue; + + kvm_arch_post_set_memory_attributes(kvm, slot, attrs, start, end); + } + } +} + static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, struct kvm_memory_attributes *attrs) { @@ -2602,6 +2628,9 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, kvm_mmu_invalidate_end(kvm); KVM_MMU_UNLOCK(kvm); + if (i > start) + kvm_post_mem_attrs_changed(kvm, attrs->attributes, start, i); + mutex_unlock(&kvm->slots_lock); attrs->address = i << PAGE_SHIFT;