From patchwork Sat Feb 25 20:47:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13152262 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 636BFC7EE23 for ; Sat, 25 Feb 2023 20:49:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229671AbjBYUtB (ORCPT ); Sat, 25 Feb 2023 15:49:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229507AbjBYUs7 (ORCPT ); Sat, 25 Feb 2023 15:48:59 -0500 Received: from mx0a-002c1b01.pphosted.com (mx0a-002c1b01.pphosted.com [148.163.151.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82084DBC8 for ; Sat, 25 Feb 2023 12:48:58 -0800 (PST) Received: from pps.filterd (m0127837.ppops.net [127.0.0.1]) by mx0a-002c1b01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31PJf6BR020594; Sat, 25 Feb 2023 12:48:38 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=proofpoint20171006; bh=9FVJ0v+ZSpT1rAFY42LiJYewV8FsI0kOzcvvAEqnSJA=; b=gALypOusr3vRW4PvMdNVVAdxPWgK2+7fLM87hqBrHm4Uefm/fQLf9fPWbMYv2mtCH/BG XhscXc3VzWjcUDqKHzLuZpFC5aEnGCZkp6qOIn8VShRJ8fYZ6pbWnvy/pz11MBP5sXIg qpDW6He2tYO1oc0olwjjJz7aEyljBQ+8Ksuc+6k9ONTeKj2VKFp0im3i1VioO58yd1/V glV0xdnp6aLilnuy/KTMlg3qV/PXHHex6/TTJgAO9UUPwZJU9JX0c/lS0xPiqfM1THvc rSDWPXTjrGqEHjQtCWb3Cp/xTFJA+5tnFEAWQoUxPE2y3k/M29cz18LRNOcT2iAUZvq8 WA== Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1nam02lp2046.outbound.protection.outlook.com [104.47.57.46]) by mx0a-002c1b01.pphosted.com (PPS) with ESMTPS id 3nyf4egugv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 25 Feb 2023 12:48:38 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Du++PGYivKJIdMH4S7kZUIZRDBy+UNsNEBvDyvRnVz+REuIaXZS/KterPIN9mdva0BBI+BZBh813JRokfEsvFQjVPM9E4xtzysbfh2yZw9bWV4mfsxDSFlwJue45ovbYFhHZ6jrupvHCzQqB2avQHzyUgR5u6LhhfRhhtFN0XXyL2k2sQn4N5d9PkaiQHZtaLkDSZ9UblfbLNXveNcd3W8p1ROC6+U8DrptPkAAdzp928xcmrXLl5FKRpDnaF467Vg50ycJv6Xl+ltHdO85aYovQwwyio1Cv/42aP/m9R4ynSEARZyJMnBaMMyk4rlpEpD3rG2B5Di4m0zcIDYZy6w== 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=9FVJ0v+ZSpT1rAFY42LiJYewV8FsI0kOzcvvAEqnSJA=; b=k8Uh+f91LsYiVGvatz4qfdKYvcDjlaosyqur5nBvEmBI4NwyKAMMiC4StoljrueeFTuMqSCDyy+pmnl5TPA+IhZug9FRXOA6hEBdilLptw7AERArpNaEdbQpKOwguW1Pl1OyUbRkSAkK1BBXjQq/fMyktDHAiy5/q46jL34hC7V7cgq0Ffz7gHOxjLXussxJVIHjuKoedJJGY+m4f5DZB4gGZsA+NTJA1gh++hoH2p/Fle1bZ4r2Lac/7VhrDWKjbzesOo8bbO7mEcPEV8FVOiy7PlM2z/ZWMJbfFz3aKQH/pVGOOdYGx2tYZ4EAzXvym1b538KaersZFtIM+BIXyA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nutanix.com; dmarc=pass action=none header.from=nutanix.com; dkim=pass header.d=nutanix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=9FVJ0v+ZSpT1rAFY42LiJYewV8FsI0kOzcvvAEqnSJA=; b=DesIG1TWr2165yd44E72ALEvYdlOFJYqdCnLpYXpv21FPHsF/nIgRfO7fccx5JjMGiE2u2P4VQrImOTJ0Ga1wA08QyBCOjPf9WJiVWG2He8RcCXK2zRKPGIQY1eWoIIza344HnRMQgK4XBiLBwobBVUXhcG6IWP/O/x6OglpNX1BFNHG90fGiYZyK6Fr7TAgdRV2Z+w4iKr9bgfAup9gmbDkHC5/qI4WORnK6y2ep3meLgV0g+v3rr4zpHMtryyUdNIM+yxZFA8J2G6tsD+lQnKAimOcE5dv/BGmMopIQtvD1g0XWpgbUnOb6dh4KVNUZvKiwsZ9hg7e9Y9mqLhqYg== Received: from CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) by BN0PR02MB8175.namprd02.prod.outlook.com (2603:10b6:408:163::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.25; Sat, 25 Feb 2023 20:48:36 +0000 Received: from CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::aeca:cda9:6785:bce0]) by CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::aeca:cda9:6785:bce0%9]) with mapi id 15.20.6134.025; Sat, 25 Feb 2023 20:48:36 +0000 From: Shivam Kumar To: pbonzini@redhat.com, seanjc@google.com, maz@kernel.org, james.morse@arm.com, borntraeger@linux.ibm.com, david@redhat.com, aravind.retnakaran@nutanix.com Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v8 1/3] KVM: Implement dirty quota-based throttling of vcpus Date: Sat, 25 Feb 2023 20:47:57 +0000 Message-Id: <20230225204758.17726-2-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20230225204758.17726-1-shivam.kumar1@nutanix.com> References: <20230225204758.17726-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: BY3PR03CA0009.namprd03.prod.outlook.com (2603:10b6:a03:39a::14) To CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO6PR02MB7555:EE_|BN0PR02MB8175:EE_ X-MS-Office365-Filtering-Correlation-Id: d0cbc2b4-ab3f-458d-07c8-08db1771aa5c x-proofpoint-crosstenant: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fqlJ52KGoF3kk8XF/6i9hPR9fqX1zSMeSPHhdEQ1yvkTy0c4fQnbN9VSL5tDTGEoezmhiCMsZxF6Pz3vzlivyhr3ZWrfB+rjzJI/gKf1lyAqjJ+CpKKWiSMx92xn3lxsDMPK97siZFXtSC6QbSpqEgsOm8EVpYwvFfwY1LJi99RnEdK1aJ8SHNvPMZ6VmEViopdjFMvXg3gCw/jFdZ6KAuKuNYYpGOnYv2CtD8NxxP6Y6cmc03MkAP1YSIJK2wD963Pqaji0fqtNhu+WTvEB04D4MFTN0WbhxlTJAQnkhLA5lYtN/nc7AUHwTvUrfcNE+Dp7bBnAltxDPF60cEe2Bwlo6q5N1l7NPqcEqOUgVWipyT+AYbtX2x9fV3lJge7xuXijTUr+dnFswfy5YFKmQRZ4DQsxnJQ5EPSJANLOFNs+Hy+0rVSoSWJ1MoLXtPsdS84BVzNNnymZz7bH8ZxYjvRFkvY0izIQ6OaAcN9fn2KGMsp+QodPkluqxXVrn4E5B1MQ71Q+Jn2P3UqI02bBylTXjKxs4zCI9lKqeuNdyGkWzVL8kFHz4kvR/+1A4K4bYlk5AVsiLOIb8SJXvZDkI27w1O2kBjIxjnOcfLGfMCDXzdhj+jgGeRo82tDb8aWIWCmilMcObnKv9fhqr7HPhQNo/5cYONUr+EIi0HAVAZFpGGTK0FFzIugMxuDkwpFRcX2mIdrb5vmkc3JxmxNrcuBcOBPRw9IV4pt+keUt6yV9DQtmCmKo6xlIqa223r21 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO6PR02MB7555.namprd02.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(366004)(39860400002)(346002)(396003)(376002)(136003)(451199018)(36756003)(86362001)(41300700001)(66556008)(66476007)(8676002)(66946007)(4326008)(8936002)(478600001)(6636002)(54906003)(5660300002)(316002)(15650500001)(2906002)(52116002)(83380400001)(38100700002)(38350700002)(6506007)(107886003)(6666004)(6512007)(186003)(26005)(1076003)(6486002)(2616005)(14143004)(309714004);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: cSKZkm2oKhlnU0oKx7e3LlP2iUVtsKRT6I63WdcIvN53FRaMehXjdJeJqrPCPJUWPfwIQWjsPMWWSvPkNgWHgl/cM9XvwQGN9vXuUoXsHGDDKnhlGPEO2DSDPaRt8+xRHiHxxV6Ro+uKO7TwKnEdrFbEkMXAmZD/jtl2NlJy5H9f4WxOjqlaSVn1qWCWjh7J8gESmgsNgtfjAUU4a/Z9cv3Tpno29xcxX238dS1Qgtjt+hDSVw2+75rbqh27QpvzgbBrvGL1PPTRVuNeD+LT/B4auCIfCT82d/6gUiVEjD9O0XKUbsePRv6YlQWOy6zdtCXHqVCLPiDhTkeyGvp2BSnfKs5XL99BdEYsm+ERa86V8eR0SZBzZjOU4vDViXkFn9QUMlLyM1MX6MaPnfUWKV/TaxcUrl6UL940P//1gfFllYBR6IeWpnTFyR3xMbdPa+6pcezOHm/tV+aOMzMVxHjBh7Rg3K5Jskn3CpVfPm99dXuEtgNOkgF8t6VgwjBnefgi8MEXGxuXnC0zNB/mOB2Xp5awnsbUsnG1Eor4SkiQ76Q9v5SePvv0tBJ6whnDP3p9KpqIpe2svTnosL8beF5grnWEYcPs3RKq5QoBDbUynmhKnPwKNm7xu0wlz7/L53MmUiM5HVEAzjCJkFqS0oofAiFFL0HoYHD95I5AnaylPleMZTSljO0dJQRdxQb8tipnwzUd/8laSzoiOLWgiVjVSq8dROTywzuFPyKgdTV+ST6/cUy5OVHqomUfMOtx+qbJE9tgyWVWdD0n6mIjm92VJZ9PITKgWtYHAl9/b8mS0PX4CQCSLwmbuh0Y5cPUhOvom6K8sTNdPQwsJhmDT0LOKxzM+gIs5+hI1vgYz5cbhOLOb6QeT/+jXD12bYSymIh/whCuP5qK2SFRTDTk49iki7Dd95cmgqW7sXdS8PI5Z4oCRjs/A3ecY8LuAuFaKtq97VR2wBQobpseffqyBcsbh1CCI86gTKIk3u5B7amyKBx2hRLcOeP2OQJlWJbFwZNUktArH2R8SAf55QgRFxcrekgtC8CUslrkhGUErQMdsr8KoZCM0LDMfzfrEQXRkdj+QT9ZO7XrdTuyTz4s65jQ5xl9UXzvdXo+SS4P55admAbh5MnbARgYM1CsO9SVQGyj6pPQT8jA+lD1i2ikCoDr2uKTlSiJKE4sLdiyrFBWqDN38deceDA4j7LBO6DdnrEqJ6xlIIRejSaOgAFWBslEy+JQDnWKKh6BL2ueXkVrIUMdhK5UPC9csB48qqUk4ltJK/sqbm9c2gjEnEPvhZ99k4Es1Crg1n/V644Vs3q7yEKdnfoUVsgmuVUizYeUgbo511g77qVXJywcJljbVH3XSDzpse9IUW0wmgzWsAbqqN4KtebCoLVDyjc9zv+FYuvf98x44iW//bN9QuKYVP0YgwlUCFvsPgWrjqxIOolwaZxNiY3mze3VAvJXiPUOcDOeI6KI8lU4xC/hyJTh7QBp0tHo7O+V2zZddYtI9cyOCqTEb9tf7kEDzPzuBNipWdT4A/I+4IIEbzBcDluX5OvBw/46fGGr1ocIf7Nq4CUaUPp1WrQgRY3iX6dkd4QRyj+3ozMWNHRAOq5xFRl+7g== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: d0cbc2b4-ab3f-458d-07c8-08db1771aa5c X-MS-Exchange-CrossTenant-AuthSource: CO6PR02MB7555.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 20:48:36.1520 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bb047546-786f-4de1-bd75-24e5b6f79043 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qT5B9NtvF3HpiWAGQg0zT6pX6A+2s4eaml5Od8L5a7NjAlMQdAuuB7su2HPTi3MjMSN4zl5kyb+l1eQwfGEVXRerx738ygB6qEy3UkuaHJA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN0PR02MB8175 X-Proofpoint-ORIG-GUID: GvTLQJazGrkdBeNcCn8bDQPMUoiC-E_W X-Proofpoint-GUID: GvTLQJazGrkdBeNcCn8bDQPMUoiC-E_W X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.170.22 definitions=2023-02-25_12,2023-02-24_01,2023-02-09_01 X-Proofpoint-Spam-Reason: safe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Define dirty_quota_bytes variable to track and throttle memory dirtying for every vcpu. This variable stores the number of bytes the vcpu is allowed to dirty. To dirty more, the vcpu needs to request more quota by exiting to userspace. Implement update_dirty_quota function which i) Decreases dirty_quota_bytes by arch-specific page size whenever a page is dirtied. ii) Raises a KVM request KVM_REQ_DIRTY_QUOTA_EXIT whenever the dirty quota is exhausted (i.e. dirty_quota_bytes <= 0). Suggested-by: Shaju Abraham Suggested-by: Manish Mishra Co-developed-by: Anurag Madnawat Signed-off-by: Anurag Madnawat Signed-off-by: Shivam Kumar --- Documentation/virt/kvm/api.rst | 17 +++++++++++++++++ include/linux/kvm_host.h | 5 +++++ include/uapi/linux/kvm.h | 8 ++++++++ tools/include/uapi/linux/kvm.h | 1 + virt/kvm/Kconfig | 3 +++ virt/kvm/kvm_main.c | 31 +++++++++++++++++++++++++++++++ 6 files changed, 65 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 62de0768d6aa..3a283fe212d8 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6688,6 +6688,23 @@ Please note that the kernel is allowed to use the kvm_run structure as the primary storage for certain register types. Therefore, the kernel may use the values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. +:: + + /* + * Number of bytes the vCPU is allowed to dirty if KVM_CAP_DIRTY_QUOTA is + * enabled. KVM_RUN exits with KVM_EXIT_DIRTY_QUOTA_EXHAUSTED if this quota + * is exhausted, i.e. dirty_quota_bytes <= 0. + */ + long dirty_quota_bytes; + +Please note that enforcing the quota is best effort. Dirty quota is reduced by +arch-specific page size when any guest page is dirtied. Also, the guest may dirty +multiple pages before KVM can recheck the quota. + +:: + }; + + 6. Capabilities that can be enabled on vCPUs ============================================ diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 8ada23756b0e..f5ce343c64f2 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -167,6 +167,7 @@ static inline bool is_error_page(struct page *page) #define KVM_REQ_VM_DEAD (1 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_UNBLOCK 2 #define KVM_REQ_DIRTY_RING_SOFT_FULL 3 +#define KVM_REQ_DIRTY_QUOTA_EXIT 4 #define KVM_REQUEST_ARCH_BASE 8 /* @@ -800,6 +801,9 @@ struct kvm { bool dirty_ring_with_bitmap; bool vm_bugged; bool vm_dead; +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + bool dirty_quota_enabled; +#endif #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; @@ -1235,6 +1239,7 @@ struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn); bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn); bool kvm_vcpu_is_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn); unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn); +void update_dirty_quota(struct kvm *kvm, unsigned long page_size_bytes); void mark_page_dirty_in_slot(struct kvm *kvm, const struct kvm_memory_slot *memslot, gfn_t gfn); void mark_page_dirty(struct kvm *kvm, gfn_t gfn); diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index d77aef872a0a..ddb9d3d797c4 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -264,6 +264,7 @@ struct kvm_xen_exit { #define KVM_EXIT_RISCV_SBI 35 #define KVM_EXIT_RISCV_CSR 36 #define KVM_EXIT_NOTIFY 37 +#define KVM_EXIT_DIRTY_QUOTA_EXHAUSTED 38 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -526,6 +527,12 @@ struct kvm_run { struct kvm_sync_regs regs; char padding[SYNC_REGS_SIZE_BYTES]; } s; + /* + * Number of bytes the vCPU is allowed to dirty if KVM_CAP_DIRTY_QUOTA is + * enabled. KVM_RUN exits with KVM_EXIT_DIRTY_QUOTA_EXHAUSTED if this quota + * is exhausted, i.e. dirty_quota_bytes <= 0. + */ + long dirty_quota_bytes; }; /* for KVM_REGISTER_COALESCED_MMIO / KVM_UNREGISTER_COALESCED_MMIO */ @@ -1184,6 +1191,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224 #define KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP 225 #define KVM_CAP_PMU_EVENT_MASKED_EVENTS 226 +#define KVM_CAP_DIRTY_QUOTA 227 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h index 55155e262646..48f236e2b836 100644 --- a/tools/include/uapi/linux/kvm.h +++ b/tools/include/uapi/linux/kvm.h @@ -1175,6 +1175,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_DIRTY_LOG_RING_ACQ_REL 223 #define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224 #define KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP 225 +#define KVM_CAP_DIRTY_QUOTA 227 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index b74916de5183..ccaa332d88f9 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -19,6 +19,9 @@ config HAVE_KVM_IRQ_ROUTING config HAVE_KVM_DIRTY_RING bool +config HAVE_KVM_DIRTY_QUOTA + bool + # Only strongly ordered architectures can select this, as it doesn't # put any explicit constraint on userspace ordering. They can also # select the _ACQ_REL version. diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d255964ec331..744b955514ce 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3096,6 +3096,9 @@ static int __kvm_write_guest_page(struct kvm *kvm, r = __copy_to_user((void __user *)addr + offset, data, len); if (r) return -EFAULT; +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(kvm, memslot, gfn); return 0; } @@ -3234,6 +3237,9 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, r = __copy_to_user((void __user *)ghc->hva + offset, data, len); if (r) return -EFAULT; +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(kvm, ghc->memslot, gpa >> PAGE_SHIFT); return 0; @@ -3304,6 +3310,18 @@ int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) } EXPORT_SYMBOL_GPL(kvm_clear_guest); +void update_dirty_quota(struct kvm *kvm, unsigned long page_size_bytes) +{ + struct kvm_vcpu *vcpu = kvm_get_running_vcpu(); + + if (!vcpu || (vcpu->kvm != kvm) || !READ_ONCE(kvm->dirty_quota_enabled)) + return; + + vcpu->run->dirty_quota_bytes -= page_size_bytes; + if (vcpu->run->dirty_quota_bytes <= 0) + kvm_make_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu); +} + void mark_page_dirty_in_slot(struct kvm *kvm, const struct kvm_memory_slot *memslot, gfn_t gfn) @@ -3334,6 +3352,9 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn) struct kvm_memory_slot *memslot; memslot = gfn_to_memslot(kvm, gfn); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(kvm, memslot, gfn); } EXPORT_SYMBOL_GPL(mark_page_dirty); @@ -3343,6 +3364,9 @@ void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn) struct kvm_memory_slot *memslot; memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(vcpu->kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(vcpu->kvm, memslot, gfn); } EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty); @@ -4524,6 +4548,8 @@ static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) case KVM_CAP_BINARY_STATS_FD: case KVM_CAP_SYSTEM_EVENT_DATA: return 1; + case KVM_CAP_DIRTY_QUOTA: + return !!IS_ENABLED(CONFIG_HAVE_KVM_DIRTY_QUOTA); default: break; } @@ -4673,6 +4699,11 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, return r; } +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + case KVM_CAP_DIRTY_QUOTA: + WRITE_ONCE(kvm->dirty_quota_enabled, cap->args[0]); + return 0; +#endif default: return kvm_vm_ioctl_enable_cap(kvm, cap); } From patchwork Sat Feb 25 20:47:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13152263 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED550C7EE23 for ; Sat, 25 Feb 2023 20:49:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229682AbjBYUtJ (ORCPT ); Sat, 25 Feb 2023 15:49:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229677AbjBYUtI (ORCPT ); Sat, 25 Feb 2023 15:49:08 -0500 Received: from mx0b-002c1b01.pphosted.com (mx0b-002c1b01.pphosted.com [148.163.155.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2EB66D53D for ; Sat, 25 Feb 2023 12:49:07 -0800 (PST) Received: from pps.filterd (m0127842.ppops.net [127.0.0.1]) by mx0b-002c1b01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31PJO1Wn017048; Sat, 25 Feb 2023 12:48:56 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=proofpoint20171006; bh=qX3mLvPfEbTiXd8MXXCCVIucbJSUkZK7mCExEfWGeYM=; b=wYIE/N+fsFe2EiuKkYt9snF5r2CEzoB5tFTB54xw4YtF2GT3L22ZXzGFAi1dlm6R+3Q9 pCfX6RYlCp7Wht84HlZdatsSdo2avGyDn+vYz5LCsSkPBI8D3oNJ1p/thXQYFY93bD8V /jEPebcXBf+MApvsCzk/zLDnGSDGK9pWw2CDLOq9+IR1illGhnT4nHtSUaj4Z+MICtUC qqpwXvzCYkKyqPxYGQMMUf1vZT+qkAwEThiXaadrhqXoaw3kEo8HmTbeYdjWWWC5Xjb2 vu+TESCjIxJRhGpBVkqQ1WhJdgR3+KjYAYiX9HTAGDYa2bR81O4GZZ8wkBhoLeUF4yK5 AA== Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1nam02lp2045.outbound.protection.outlook.com [104.47.57.45]) by mx0b-002c1b01.pphosted.com (PPS) with ESMTPS id 3nyjhn0fv9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 25 Feb 2023 12:48:56 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KJ8pf8LueoElUIq6RPfATh8HHsmvmBp/bkQafDf0HTr/xQcGaFXu2SnQuP0kchpXOi5ulimn4K2SOPMmKmuVv8iLJv+LGTg9f6rG1H67tUp3ESFyzaJ5vRsYPSjJJy6BbD+Lqpe9QEY0U4TT67skM4+6O4ybv9n/MTHEv1wE+YZS4jiTWwpeSovxhUnB9foXOoBzNzPta0+yN8iOYFgLvHH4CWvWxMBgS+PKZpi5gBG1F4lDT1mkvz4uFHBxKgKD43GSgeAZsodQwdsfc3+F02u3H33h3Ee5L4rtf1uYs8abRnAEiMTrZ6CxbLjKh/Zm0DaItJNLkca96LNn9JLKFw== 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=qX3mLvPfEbTiXd8MXXCCVIucbJSUkZK7mCExEfWGeYM=; b=joK59SRu6xO2wDhr/JL+n2QkKqXUz2YttTsUmVTwh6eaXHJxMvZ2klXBDrgrS7oZdktLh4Q7p1JVSJiiVzzIW7PBangZQfftbmykrsFHqglcGWd5c4eTeufSbpWsk6IiU2/uu5RBoTqvB0kPMJ9Z5ZpsvC1aaqP04IOKt+1GmAXJip1ejoGZOQOwLoZmLcGQ0RDe1euOb25lSt8MCfapOtUX6NZwItvI4kuikYcK1xAcKQFVtjR9BhuoPq+x1FqATEBdUjHv8lYqBdAzXInd/3bjEarv5leO4FH1VBq9hNsykqwiaCuf1MdY5uK0hjP4e8up26v7XFYapqIhsfQY/g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nutanix.com; dmarc=pass action=none header.from=nutanix.com; dkim=pass header.d=nutanix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qX3mLvPfEbTiXd8MXXCCVIucbJSUkZK7mCExEfWGeYM=; b=dwRZ/+Vl+bJj2uaf3xV3n+lb3VChb3pcdt8YWbb1CKLRS/6lgEKwQdbe85nDg2Cei7KIfyCIsWeXWQXQjK6770rQjU4B37PPkEQcgu6GuwSppnaokQF2QoSdqOwWqf3pNJfy9c59DxNpmRhrGHQ+Uhj/BavT2IJsZaR0pObkAxvlPYNhrWnMdcXNtoJm9lPh5bePz8ZePIPdlIv6uGKkidmhhwrVpdjpISVu6GE2rqzRkNpYU3LiTzdL5FhNr3syftCKcYr9p2VsF7fT1cxk0t66shlh3ejqPeMsKcuAy+gXhVFqiQF0L7nHN+UBgaAaxdCxntM3iElBwv9kiIPnyA== Received: from CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) by BN0PR02MB8175.namprd02.prod.outlook.com (2603:10b6:408:163::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.25; Sat, 25 Feb 2023 20:48:55 +0000 Received: from CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::aeca:cda9:6785:bce0]) by CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::aeca:cda9:6785:bce0%9]) with mapi id 15.20.6134.025; Sat, 25 Feb 2023 20:48:54 +0000 From: Shivam Kumar To: pbonzini@redhat.com, seanjc@google.com, maz@kernel.org, james.morse@arm.com, borntraeger@linux.ibm.com, david@redhat.com, aravind.retnakaran@nutanix.com Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v8 2/3] KVM: x86: Dirty quota-based throttling of vcpus Date: Sat, 25 Feb 2023 20:47:59 +0000 Message-Id: <20230225204758.17726-3-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20230225204758.17726-1-shivam.kumar1@nutanix.com> References: <20230225204758.17726-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: BY3PR03CA0009.namprd03.prod.outlook.com (2603:10b6:a03:39a::14) To CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO6PR02MB7555:EE_|BN0PR02MB8175:EE_ X-MS-Office365-Filtering-Correlation-Id: 6b20936d-c35a-4ba0-25c7-08db1771b57e x-proofpoint-crosstenant: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2n+YT9p0vrS87V2fZ4n63SxS85uhr9hE1GO5kedA6PIsRUlSaPBy9e6oaDtRzt7s28XZ7IFEDZ3Jt7y4rsV4yWVFIQrp0KWZpm5MeHAOV7T+IqE97aZaoe8O4KNlssFyvMB3zdPmaRe1y76u6iatUVRiYLIiV5DVaoZhhlEf/IGbX91ONdaOr+OykIV3AVnaYknGTrvdEB2QLT3EryqU5v331sOA/3He9rnLLofGAIep9VIqiEBcBBZDNkMStQw0sSWfsrjoR9ZpxlkZujkg4UAsMetWUSaPdgLWYrtEtkK3OrGm48jpKZePDlP3b9V/rJONj4Fz15desrMeEgK8pau9Ar55tNGB4vEQHAyNWHM1VdZHHrpq1ZxD9hPnBWSFS3U0ICQG1D1dI3u6FzTY1m/CeAsqWY1oznTrtU9RULNX3miIRdP/GLp1wuYMFbKW1SWM2RDXfCsF9hwhSNx49s/ZtQArkIdCft/ND6Gvg0yKHXAYuHGbkQ4vz3vudFqlv+irF7I2vbtRoPe7PG5ItpLMbuIiSQahBOv6U+mUi+2gC1BqxsMK6SdUkmYj7OCPeAOjckK/pVyd+KU4emecjmOf34RFx81tW8Z0EcUx7f8uDrGxHyO2mIivnC5ZkfwGXTOaS6WA0L5qpbrTrUHNA0kyDP/7iGee1BDQyix/lc1KpMDZtgN3MafXZdNXfHOEC4b/pwltCO3lUK3cHeb2t4DwebA6Bp/s1ZU/PLuBO6Q= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO6PR02MB7555.namprd02.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(366004)(39860400002)(346002)(396003)(376002)(136003)(451199018)(36756003)(86362001)(41300700001)(66556008)(66476007)(8676002)(66946007)(4326008)(8936002)(478600001)(6636002)(54906003)(5660300002)(316002)(15650500001)(2906002)(52116002)(83380400001)(38100700002)(38350700002)(6506007)(107886003)(6512007)(186003)(26005)(1076003)(6486002)(2616005)(14143004);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jfIopU3jvpPcb+E/pnpoqn7916cYHUQrpso7YFD7RrjGhS2o39OCMrKyquIxyoM3YMPiZbv0E3TJbD/8t9zQP+/PbuHHelm6XRiW7VoykSAtnUljwk5EfYjIN3Y+f+sZTnE7D0M7s1B4QJ/GTINfgGqfIIzecdSRpd9YlLevVcHaU62Ih4eLwgbGuIi/Ikm8lyLa1LEzkp0OkLgJub4XSCTG7EPXZz1oIfSKFbVuwcNzRzRrA9kpVYT8M5Vq+imKdRyKdNplnBMDUYTpGfG3TjGtBytKQIiXPypj/T1ju4JvP3pHv+UfmtOPnQOuPr7tMQD/n7/OVs14XEJ8J+NKQ7cT3unAjL4zanvLr/K2GDSutciJf59j0CdZdnF7f+tnns0CItYYEfs2KUiGb7yzB8Ddq2j8hZ7TiFIXYkSVzHxKvkXS9gcL5I4x6lE7quD/E/g0QfmAFWQsWTCip2dFq9OxZeyf8BIkY3DF7PZilRGejfmb7H84ghMndaNU/qyA+kC9g6mV3ACAt3ZVFZXem9gdQd34KWcQpkNRZ+GTBjPuQyX7ZdaTd/kJOHpW+zBjTPHK7ePe8bNm5CC3AXgSFY4hVdCqAhWq21uLCJzt3jJlSMqZBFo7JKS/vcH5rDNaSP8NBGvYjPAshS505/H4HAEbp+j4nu/CQqakud4agiCiT1V3rbhMValX1Vv28OfUwY7kxH2OtTMymtHJIvWloAW8sycyIPFr3a9zX7YdlbaneeDDBhZDKzpocEmHmH2ae2AFLSK/gaqC14c7eBdwZp+Kmtygt6eNu48jspNUFF6QErxXPHhchc/V5V21QWD7G/CsYeok7HIYy5VyORvXoe2BxD1/W3rihK3oCYvZ7WWbobPa0mb+TFpno4eVNvcSrOzvr6gqPL8M9DmDZ8u3UOMDvAvGd8BrT0RVFlsg8gseP20n4wbgLpPaH0aBeIqEdIBzzz6nu7wLg+4gLXhYPbjHxjT3c7THVT7sTRWaUbYrz0MyKUZJmIzV3rflaRspxEtAOnIqITFanz4edIDQQQDsC9c+95eva1Bk/ouenwT0q26ixcWWjwzADJTvdYyMxowHZEmsZ9jfq08GzzGtex3MgE/2+MsFn/z1qMVDVFQagZu8xT02marsOUdlWAlqy+/ZvMLhmdI5sHG7eRevBLB3yT0bACj1YGe1t6vkGAs+O1cx7p4t0zKlpWS/s36FaJF51Tq0WShOTL2FutBAPqEAPZpkX+q2V4E5Z0YPC5/9vhFw04EhPaKKjse8Pp2gstZ3hxN5rPWbGlNPmgNon8pqo1Gm9/86QHPyWYxve9VXHAMIAtBh8QEbYNtEGNbw4CJIFYSp+zYm8eoVdmwsfwxGwAcrLJM+ZmFdyVoKCo3FzVQQ6rHvkrhk4zk39zbxSTjUIdK8cKSp42jC6V3EZzwa5WB2AssOg0UMbXYc0md1lx6grHu5CqmHaPBemsbRrEK9iCIGpYdfr+I/XICEFl3zbFhmPY00d3dxsE1s4IqvOTM7AgUk7qbS8ztfq5uFyIaespwlzgofxOxyNmquMa2/K9mp7ybgarvAGzaAJRoMS72DBc/kj6Gc+CyrOjNO6XUvJ102gGH6X0VnUh8Erg== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6b20936d-c35a-4ba0-25c7-08db1771b57e X-MS-Exchange-CrossTenant-AuthSource: CO6PR02MB7555.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 20:48:54.8305 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bb047546-786f-4de1-bd75-24e5b6f79043 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: QamD2utSFTe6O9JJ3/Rae6KxV6ULSxN/a8CFNDFXPuaI0y8j0LI+mfXa+itXz4kL6aNp9jKexCXfdC+/coB6OISBwCOmfHLLpobL4Y+DdDU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN0PR02MB8175 X-Proofpoint-GUID: pI9xQFsESxSoAUitrAL-xCEjFJV17gi1 X-Proofpoint-ORIG-GUID: pI9xQFsESxSoAUitrAL-xCEjFJV17gi1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.170.22 definitions=2023-02-25_12,2023-02-24_01,2023-02-09_01 X-Proofpoint-Spam-Reason: safe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Call update_dirty_quota whenever a page is marked dirty with appropriate arch-specific page size. Process the KVM request KVM_REQ_DIRTY_QUOTA_EXIT (raised by update_dirty_quota) to exit to userspace with exit reason KVM_EXIT_DIRTY_QUOTA_EXHAUSTED. Suggested-by: Shaju Abraham Suggested-by: Manish Mishra Co-developed-by: Anurag Madnawat Signed-off-by: Anurag Madnawat Signed-off-by: Shivam Kumar --- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu/mmu.c | 8 +++++++- arch/x86/kvm/mmu/spte.c | 3 +++ arch/x86/kvm/mmu/tdp_mmu.c | 3 +++ arch/x86/kvm/vmx/vmx.c | 5 +++++ arch/x86/kvm/x86.c | 16 ++++++++++++++++ arch/x86/kvm/xen.c | 12 +++++++++++- 7 files changed, 46 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 8e578311ca9d..8621a9512572 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -48,6 +48,7 @@ config KVM select KVM_VFIO select SRCU select INTERVAL_TREE + select HAVE_KVM_DIRTY_QUOTA select HAVE_KVM_PM_NOTIFIER if PM select KVM_GENERIC_HARDWARE_ENABLING help diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c8ebe542c565..e0c8348ecdf1 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3323,8 +3323,14 @@ fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, if (!try_cmpxchg64(sptep, &old_spte, new_spte)) return false; - if (is_writable_pte(new_spte) && !is_writable_pte(old_spte)) + if (is_writable_pte(new_spte) && !is_writable_pte(old_spte)) { +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + struct kvm_mmu_page *sp = sptep_to_sp(sptep); + + update_dirty_quota(vcpu->kvm, (1L << SPTE_LEVEL_SHIFT(sp->role.level))); +#endif mark_page_dirty_in_slot(vcpu->kvm, fault->slot, fault->gfn); + } return true; } diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index c15bfca3ed15..15f4f1d97ce9 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -243,6 +243,9 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, if ((spte & PT_WRITABLE_MASK) && kvm_slot_dirty_track_enabled(slot)) { /* Enforced by kvm_mmu_hugepage_adjust. */ WARN_ON(level > PG_LEVEL_4K); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(vcpu->kvm, (1L << SPTE_LEVEL_SHIFT(level))); +#endif mark_page_dirty_in_slot(vcpu->kvm, slot, gfn); } diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 7c25dbf32ecc..4bf98e96343d 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -358,6 +358,9 @@ static void handle_changed_spte_dirty_log(struct kvm *kvm, int as_id, gfn_t gfn, if ((!is_writable_pte(old_spte) || pfn_changed) && is_writable_pte(new_spte)) { +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(kvm, (1L << SPTE_LEVEL_SHIFT(level))); +#endif slot = __gfn_to_memslot(__kvm_memslots(kvm, as_id), gfn); mark_page_dirty_in_slot(kvm, slot, gfn); } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index bcac3efcde41..da4c6342a647 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5861,6 +5861,11 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) */ if (__xfer_to_guest_mode_work_pending()) return 1; + +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + if (kvm_test_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu)) + return 1; +#endif } return 1; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7713420abab0..1733be829197 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3092,6 +3092,9 @@ static void kvm_setup_guest_pvclock(struct kvm_vcpu *v, guest_hv_clock->version = ++vcpu->hv_clock.version; +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(v->kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); read_unlock_irqrestore(&gpc->lock, flags); @@ -3566,6 +3569,9 @@ static void record_steal_time(struct kvm_vcpu *vcpu) out: user_access_end(); dirty: +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(vcpu->kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); } @@ -4815,6 +4821,9 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(vcpu->kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); } @@ -10514,6 +10523,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) r = 0; goto out; } +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + if (kvm_check_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu)) { + vcpu->run->exit_reason = KVM_EXIT_DIRTY_QUOTA_EXHAUSTED; + r = 0; + goto out; + } +#endif /* * KVM_REQ_HV_STIMER has to be processed after diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 40edf4d1974c..00a3ac438539 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -435,9 +435,16 @@ static void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, bool atomic) read_unlock_irqrestore(&gpc1->lock, flags); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(v->kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(v->kvm, gpc1->memslot, gpc1->gpa >> PAGE_SHIFT); - if (user_len2) + if (user_len2) { +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(v->kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(v->kvm, gpc2->memslot, gpc2->gpa >> PAGE_SHIFT); + } } void kvm_xen_update_runstate(struct kvm_vcpu *v, int state) @@ -549,6 +556,9 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) if (v->arch.xen.upcall_vector) kvm_xen_inject_vcpu_vector(v); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(v->kvm, PAGE_SIZE); +#endif mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); } From patchwork Sat Feb 25 20:48:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13152264 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE817C7EE23 for ; Sat, 25 Feb 2023 20:49:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229693AbjBYUte (ORCPT ); Sat, 25 Feb 2023 15:49:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229675AbjBYUtd (ORCPT ); Sat, 25 Feb 2023 15:49:33 -0500 Received: from mx0a-002c1b01.pphosted.com (mx0a-002c1b01.pphosted.com [148.163.151.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3650E5272 for ; Sat, 25 Feb 2023 12:49:31 -0800 (PST) Received: from pps.filterd (m0127837.ppops.net [127.0.0.1]) by mx0a-002c1b01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 31PJf6BU020594; Sat, 25 Feb 2023 12:49:17 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=proofpoint20171006; bh=A+Sq24P5ZmnRWMrLZdXXUCh5HPjTk17p1yskimpbq+I=; b=vqc+wvcnBX7ucibIrUSRyeUkb9+C8n5t8WpaS7qv5ybyMjdNKBJVKsKPbwU1Lq+8BIOz zTAy9N9PmqRRA44JdyTJ5ciY3bqvTcb5Vz7AqnReNV4+TOnRsLF4jQgfq5bzVR3Y5g0d hC50KokPG7i8o8Bu6/ozm+FIHbnliU8f++VfMZNyUggmvXNUWlMNZzvyCtUC2+zYLTav 20MdNMNp9rPX01O8Sy/iM1YCTKyLFnREwf7OB5Cg/GgozCNl5gex9FRFzUcfyXlxuEbk P+sb17BmWlvVO9aR27cXjXYK4UB3t246Q/OKlsyVU+G23rurA3RUeG/ElgSrNs4t1rFl cg== Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2177.outbound.protection.outlook.com [104.47.55.177]) by mx0a-002c1b01.pphosted.com (PPS) with ESMTPS id 3nyf4eguhp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 25 Feb 2023 12:49:17 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WBFeDokiCoq6hJbcqVrJCvP+wZHJsAs8mVFvRhOcqyR8ofRZR5UTMBbtmCl3r9TDVIG0UvtVOSqBV18zyFsT9PzjhfwKG01qvuRFSP8BZIN4z2sAZh4C9xwkZbl9YILwBRkzVbspyPUp69S6TvEVgUs+oGb8QeUkDjZJJy5Wj5kCX0LvrbWjyLi25MuVprGwT/sPbmSbSf/5ov+ZtEFEviyyOLpfKbA2cvCgWmy4VIhOqzagfQeTrk56djNmDWaoSdso5kMJszLubxZZOltMmFSsU2Y75FSr4VdZC8iJNHHu1VPM5bqvMWMHrScy3KjMpGZA1ufmsDp/pSe/YeZOVw== 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=A+Sq24P5ZmnRWMrLZdXXUCh5HPjTk17p1yskimpbq+I=; b=QZjbA5ECOsMZhIUEQldYooGYHuDHzx56M48zlpxBbj9qDFQzMlBA7lYyIRQs6cajaPRCX+A/xejz1FnDlgTY4tDVLu4gNysRxJaKawzuktH+AxIf0m1TN7pGVGXKjZeNOQoY6/EnpXplf2/Uh41fzOhGUyImfgZTd38CKC3YF4vCw4bNafqxEAmZeuMb1t8+TpMvoT/uo1mOaJfmHLFgppdP/4fUpicGCQQ/ObqnPFsZXQMCZz1fOWQNxkPrcBxBKzb7zV7naywbuuh3MRioEuo7gZJJVyT6fodRmWwHD8oWrBMcSD4JvEzcoSWYi9Ml6HdNNKJ2M+EJmCg4LEl1ag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nutanix.com; dmarc=pass action=none header.from=nutanix.com; dkim=pass header.d=nutanix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nutanix.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=A+Sq24P5ZmnRWMrLZdXXUCh5HPjTk17p1yskimpbq+I=; b=nSu/1RY6bCQZ8cdR/UUrgo392JAbKMJq5eN8RmRBkBxRdgLwtfah4XJoc4sTU1vHZtv94e1AqjUxcxOn7UEJetVP9IdcdqQ3YMw9cnvDsqvdkn/GeMafB7g++KqULQ8QPl62RzDzvyKbmMRAtuw64UiyYRa1Ab1Amw802N1rmOAGmpzI8E26Jr4UEzDFPp8hqJwADqCG7VowBHJIS58ESBb8CK/pfo2tqV10W5BBPADL8uP+AGa0W0TGXMOpfnx8DC9pH3cPPwAlQzaycpk0sHe4nIbi4Gqv1SLnyNfQLOVJhrkv7XyO2lcGupkkXlfmY4bC0jdZo3ffvTrW/2IVXA== Received: from CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) by BN0PR02MB8175.namprd02.prod.outlook.com (2603:10b6:408:163::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6134.25; Sat, 25 Feb 2023 20:49:15 +0000 Received: from CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::aeca:cda9:6785:bce0]) by CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::aeca:cda9:6785:bce0%9]) with mapi id 15.20.6134.025; Sat, 25 Feb 2023 20:49:15 +0000 From: Shivam Kumar To: pbonzini@redhat.com, seanjc@google.com, maz@kernel.org, james.morse@arm.com, borntraeger@linux.ibm.com, david@redhat.com, aravind.retnakaran@nutanix.com Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v8 3/3] KVM: arm64: Dirty quota-based throttling of vcpus Date: Sat, 25 Feb 2023 20:48:01 +0000 Message-Id: <20230225204758.17726-4-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20230225204758.17726-1-shivam.kumar1@nutanix.com> References: <20230225204758.17726-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: BY3PR03CA0009.namprd03.prod.outlook.com (2603:10b6:a03:39a::14) To CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO6PR02MB7555:EE_|BN0PR02MB8175:EE_ X-MS-Office365-Filtering-Correlation-Id: 76348903-cfe6-45ae-fde6-08db1771c1c9 x-proofpoint-crosstenant: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TaA8fbdl0gdBy/hDA/gc8see5Qbkf9P63B+2yYPtPBJG7sfGXokW6BKmsjQKd5xIrJigzD7nfw1wDTIZfRCE5OqRuKw+I/m6kbKzXOuSsEVdYBVSru3/q7Unl1taMByx2Y9zO6gKTI58QbgbPDbs0OpbvC+XY1d3YNwxTrsMy7QAJ03r0rgXzQqxQ+Lj7pa9ZpHPfKwvSZHq897pc4OGtJLcGovMZbGuJwBZO4rJ6L55Hk27eer5FuIY7TI36A0mKYmGKAgurqYOIiZPjyjtYrtYzHDkUWTvJ+4LDzMrgyh2na6qqpPyFh76YbvChHg5OTdZCOM3AVkUrN96CZE1eb+gmtI8TAkDKBwv9qtZ/ZcgvxZCc/vLH1EduRuijvFISKHO9CbFOpfvEuMazzG3okm5ZySj8V/RVZoHBC4KliLrEKtT0jwOGOrYrKcL8lKuCguBL7fekx9nayDFDMEDjFKo376Omscmgq3AyGRQ3OtDjsunLMEdY9f9PwhH2kTa8rHRrfMvuLxbr7eCCjrEEe+rCqhUeZKRdiJ3NPmQdGCgWqqWNc2GscVWTLNPg5R3dvOp/spM/yjk1VnGiR8Ahr8NEl3Ff49B/spconydLX4bnWm7aUck6wodhJLq86nwiBUPwaFMcIHaAEyotVUkGy10QpG2JWX3PGBbt92GRPO00O3wOpPy/uJG1oK4mfsAhhOYABVj3rjaIdWQDNBG9Sf+KMV+rwBiKdNhlnsNUqg= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO6PR02MB7555.namprd02.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(366004)(39860400002)(346002)(396003)(376002)(136003)(451199018)(36756003)(86362001)(41300700001)(66556008)(66476007)(8676002)(66946007)(4326008)(8936002)(478600001)(6636002)(54906003)(5660300002)(316002)(15650500001)(2906002)(52116002)(83380400001)(38100700002)(38350700002)(6506007)(107886003)(6666004)(6512007)(186003)(26005)(1076003)(6486002)(2616005)(14143004);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tAv7WG7tsNfFYVdzfp8/wx1df9qBbPGVkaxr7SiW+x1g+hISUtA+f+UOinOABx9RRrGkjjk0GsuENt7G/GgrsLSLT+Xd82GiEmU6jsGn8fMNlMOuiawczYHlPYdE1FfTe55PZbudzXP3gfxYrIr5LXmCUxNPPDQ900kdZnfGprYkfMyn2xa5f4HeKq3mZhPgKJwafTd8lazdUwZw9c4arxIQPgtHPVsFbmM5bDRgebT8NCrnCwtAlR02vPEY0m/uN3k4DqNKO9bFLdhXH19nF/4bLyiRlA+8qtipuc9haLL4M/F88wlippEGgbNAZbIxDUBPtquMw8wDtgp94CA/zHbzhslJKIJPge0fyw+cSQpFgYuCMwpLsyCe5P+VdZgeqcem/jBsFpOIXGKX/Xp2Z82cfxjOiAiYa3hhB0jxkfUq9l1IWDLjDm4XLa3TBmc/yA+qdBHEB0sCdE0qufqrzC40zG4/zkrLAj6UJEWXmQahZm38Xu3cSiST+Fl7iPdW80u50f3nrE+K5N3zmdnnN2cGU/F92fyaDlCRpRsKhgMxMep6amMaEmyg6SInj6U1FTNEoK8uLfqa0DSZfaqtgKUgPk/0pU+5FkHogF99F/qoaycg5cJXviSBKWh9Lo35DDepaf0Yiig0KomrYtopdGqeq5a5x1UvhhU2mofOQgn6a0JyfXl+n7vEWiF3C6jBgRAJBUUIlL6bILbD/NpCKJyO3s5pQT83UMEPatd7QR04t/bW2Zkq/FVwLAORev25A9nVqKj5qYk95yEwyzVqk/cLCs6gA7KEQjbpPQADNCXFzpW4QEXVqSDUVga4CCIy0gJ85HBrLNB08lyg7WKm7GWABPArZ0hdvIntc01GK7PlVj6C/LpL1aQ4lnA7l/Zg7GbwQO8mjHhlFnwawaOmM34P0Tqx2zCSfIrC23VhUsw7eJ6qz3VsUravzgEvMn7TmAqGY8xqa9pvTlvNXw7v7nk0VIYpnK4sNVLjzo58LU/mJVpK616pqDXgsnqDe1Jg4/khihnr8AsxRAZY4Fh7pDKDd7jGoRs3W1UdgabHhebWM1/GedeGBnJGKLPy6cTW9KbiPy5TTl426uLfNJ7FDkWCLNLRvaZyFNiFs3NozI0be7iDSv9KVJpNi1bRErBf6Wb2nzVfrFL0WJakzwmrnS9zMdqWZU2GB0BKqDk9ApQaWvXrN78QbT4TgK+liHS7vHDG9WB0akfQkIH43K6EWI59xcYISNv1pnwBKj9cZuwBHp1/zdwN5mxslkVoH/mkwz/CGaVlsAgfGR34gKfJ/Gs3F5BsXyZIewl17KfEsC3ddBVVfOnQwOIhFJThz1CYGE1K3edooTzEb2qaLE6bB09xj5kAZaTzDPpv6jpmkGrHlzgWVXJz5khbPCdyigWrWWX7DbyNDcNAY8+DRWi6qrFosi/TJtwKz0tTAnf2OrgYKKJhwR+FmIS79oNxiW3zY+9BtDikTt+3OzqcI3a+ZmX/ppRGMk+3lnbmJpNBckPo+MmDIB9ELoc0AoZvKGHephBaPlKVkRVjh/lV4v9oj0OI3OKh7I2/HiFKJKz8gEXMQqYETZbiBoRaDdlK+Ga4K4nmWUftNHVEtu+649sShg== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 76348903-cfe6-45ae-fde6-08db1771c1c9 X-MS-Exchange-CrossTenant-AuthSource: CO6PR02MB7555.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Feb 2023 20:49:15.4759 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bb047546-786f-4de1-bd75-24e5b6f79043 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qOmdjCG0LJgvfnA22B09f+KAmWTwMTuqa928vJzF5YtfWPJndB6u/DgzyZtaWwQmZfv+A2s4+7aGmB3D9x9NCSPyrms3IPNSkDmWd0NBn5s= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN0PR02MB8175 X-Proofpoint-ORIG-GUID: cz9SOLH2F0-VyeYyfnaYC5svpUPdo5I7 X-Proofpoint-GUID: cz9SOLH2F0-VyeYyfnaYC5svpUPdo5I7 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.930,Hydra:6.0.562,FMLib:17.11.170.22 definitions=2023-02-25_12,2023-02-24_01,2023-02-09_01 X-Proofpoint-Spam-Reason: safe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Call update_dirty_quota whenever a page is marked dirty with appropriate arch-specific page size. Process the KVM request KVM_REQ_DIRTY_QUOTA_EXIT (raised by update_dirty_quota) to exit to userspace with exit reason KVM_EXIT_DIRTY_QUOTA_EXHAUSTED. Suggested-by: Shaju Abraham Suggested-by: Manish Mishra Co-developed-by: Anurag Madnawat Signed-off-by: Anurag Madnawat Signed-off-by: Shivam Kumar --- arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/arm.c | 7 +++++++ arch/arm64/kvm/mmu.c | 3 +++ 3 files changed, 11 insertions(+) diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index ca6eadeb7d1a..8e7dea2c3a9f 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -44,6 +44,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select INTERVAL_TREE + select HAVE_KVM_DIRTY_QUOTA help Support hosting virtualized guest machines. diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 3bd732eaf087..5162b2fc46a1 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -757,6 +757,13 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu) if (kvm_dirty_ring_check_request(vcpu)) return 0; + +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + if (kvm_check_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu)) { + vcpu->run->exit_reason = KVM_EXIT_DIRTY_QUOTA_EXHAUSTED; + return 0; + } +#endif } return 1; diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 7113587222ff..baf416046f46 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1390,6 +1390,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, /* Mark the page dirty only if the fault is handled successfully */ if (writable && !ret) { kvm_set_pfn_dirty(pfn); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + update_dirty_quota(kvm, fault_granule); +#endif mark_page_dirty_in_slot(kvm, memslot, gfn); }