From patchwork Wed Feb 21 19:51:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13566265 Received: from mx0a-002c1b01.pphosted.com (mx0a-002c1b01.pphosted.com [148.163.151.68]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6998D86AC6 for ; Wed, 21 Feb 2024 19:53:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=148.163.151.68 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708545196; cv=fail; b=sE92gpnVrNG5aCml4G45eZji7BQ8mebAQPwhQosxLXBziHOcoBqfnhpzBQyOR1VRf/Kk5xitaPEoCPv2JAAmgNGrM/oF3ko/h2WE6q65e5DXRvrt+Fwx4dGuJWkpf/yhFZ5cI2+z6aHcR0RoxYFbzRsQO1xMpvrsRsfpKgWK8gw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708545196; c=relaxed/simple; bh=F5kvKAvMlGsJmX1Qan7cUg77gztanuVs8iBZWsTqu/Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=My3tZ/xc+6OegX1oUBQg1aA+Jyj2744vxKqKpbvcCyGbB39IQcUstmaLN84e8bNlDarnB/elhE+jLMuSH8Zxn49i/DX7CCjNpGsw9thIcdUQWEIonvfO7BC5zXuajQcddHwd3ddj3UCos8+1Pt2MYMvAKLpVcdLw5CtZU1P/Qkc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nutanix.com; spf=pass smtp.mailfrom=nutanix.com; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b=LTFs33I1; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b=B+pK0exf; arc=fail smtp.client-ip=148.163.151.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nutanix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nutanix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b="LTFs33I1"; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b="B+pK0exf" Received: from pps.filterd (m0127839.ppops.net [127.0.0.1]) by mx0a-002c1b01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 41LJdecC020263; Wed, 21 Feb 2024 11:52:58 -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=wgC7itBopkgKNKAGevMzBQ5ty6joewVHUVuORP9/V 6s=; b=LTFs33I17U6KG+4lt/QMgSB2fLEv1W1PSDDs/qSwhNxemLeS9e1WdSjfX AvvWuwo3PAuZK9Wn2FBWabcdtcgK7K0GqEJ2wjpLreOg6kLNmLNjM1BRymCzBIkT YrlnSdi4xK5yvD4lB+zLQbv16VfHfBD8advIOWwGDd8z5dOkXsbwnvKtYJgVUS8m ZnHzzfpiJ0kXviEqNpS5x6QWpRXxILUvndbsk5Ow1GPRfIp4o4kiRc9rDCHaq6tL 1MDWFT8kO3W68hOfqBlQJfHq6BbUD92zyXkiGHq9d+lYcqn81dpl9fMOtjW+lHRX WgG5WFIEGStfSq5QtsB4kw7MSMwDw== Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2041.outbound.protection.outlook.com [104.47.66.41]) by mx0a-002c1b01.pphosted.com (PPS) with ESMTPS id 3wd21wu6sd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 21 Feb 2024 11:52:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jxMYQv0k2l8SEDZvAeSQMXKM/U4ltNRb9yQZA4dAmNhkEwv+oa/60sgAqj+l8CUvBYQd14e3NkyoaLlLZZ3hj2U/ha7n47lqNq9VVtF1595M4OC0GyaV8nO2z29QFaK9YW4nV1ElNMg+GBdRO5O/K9Yb7GTSCdPw+nYSdHC4BJNKjGgiBXXZQsbgHksuimcSsTq0CK+5wuvnj+lokOZ7a52LgPDcbVU06H4N+PvWjjFRj7XtVnMMAseESLXfVQmDaI4m7uMFonX0veuKln4trmNWKOEGbTgbxHb00heILlgTLQnz5K23RoMSnjjwBwBFurkS4CO2ojYACiRdKGx49g== 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=wgC7itBopkgKNKAGevMzBQ5ty6joewVHUVuORP9/V6s=; b=LCSZHuj1d/9n1Z+weMQylq/D8xwOQCz8hZP3Vg7obl333WuRDG2tcfHyBDskJVWX/bhusboxiRVXdpqJysPLO3nMNg5/L1NkwJX7tXO/+3TP2620emPtmdJi4z0G9m0D1aCoq8EJ6va5wOZZzvR3IvGb6B4VVdh9e5N5w27SmcORb9g8qXe3teDbFDZI8uD0bW/QfXxVMdL1igJCEuH9jzriR19b/EDnr6eh0Pz3Kqf48fAy7PZhUkeUtmIZA7TaujXrxuAm0bo7Zl04UKbGHp8tMsanRwQA547RtLfhKEDAzahy7yJ3bTWhs1OzPviXreNB1FopUwKvJwpXyLt1nw== 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=wgC7itBopkgKNKAGevMzBQ5ty6joewVHUVuORP9/V6s=; b=B+pK0exfvOpuOMmByvJnL2pIkop8hRxfMBk7+g+bkaBHjFuJBPdNosylIkfv7u2faMGCV0HlX/5CtrJ1xyJpKJo10JQP0n5xxpUEgHFBWchTRYpKSAWIhmMozQNCHSxgp7OIvdGMU+CDdztmQwxpMQN1Y0jVqbfE68yEYXTZyHFWGFSnojRXL7x7jjc9Ud0HqkdAmX6F00BeM+YQx9a7LiX1g04patHxoRcZh4gq2mYH61mujfGAPsjgyq3O+RPYBkQJ3batndB5LM3UfGbwCgZ0ty3eEoG/u5XluDeMNuPTsyWx+LCD4Ymxhd+1OK7m4qPvzhaiadL/KQGqrFdIdg== Received: from CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) by LV8PR02MB10213.namprd02.prod.outlook.com (2603:10b6:408:207::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.21; Wed, 21 Feb 2024 19:52:56 +0000 Received: from CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::4a9b:daa2:2dff:4a60]) by CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::4a9b:daa2:2dff:4a60%3]) with mapi id 15.20.7292.036; Wed, 21 Feb 2024 19:52:56 +0000 From: Shivam Kumar To: maz@kernel.org, pbonzini@redhat.com, seanjc@google.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, catalin.marinas@arm.com, aravind.retnakaran@nutanix.com, carl.waldspurger@nutanix.com, david.vrabel@nutanix.com, david@redhat.com, will@kernel.org Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v10 1/3] KVM: Implement dirty quota-based throttling of vcpus Date: Wed, 21 Feb 2024 19:51:24 +0000 Message-Id: <20240221195125.102479-2-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20240221195125.102479-1-shivam.kumar1@nutanix.com> References: <20240221195125.102479-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: PH8PR20CA0013.namprd20.prod.outlook.com (2603:10b6:510:23c::27) To CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO6PR02MB7555:EE_|LV8PR02MB10213:EE_ X-MS-Office365-Filtering-Correlation-Id: 2d20bb43-251a-4dab-a5cd-08dc3316b2b2 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: k5wB2gJGMlzCmqB7XvvxPh8OM8VMMpnAmeiPvUMogdNW2F3+e0NbqfUamjxrfX4+EBGEJWM6YJPN1K1bd88bDkACyrOa1s583JwmgL9yz9KcQcHQbVOM9natN/yvMb3SO0pzaJC1yxlL3i0p7yJBL/RsQhspIYKM1e2nGXCFiVAwMcb103a+ciP/qIVeJHeiIYxjaxvKGgcKdKsv5+Gs1gbVKhzvE3aHgROTXYqie+/0C6gPI1M3mfJc/H3ngtD7WkDAh/HyX38wVvujq8ZjlUgCIwdYW66j/ZwwudKRouC1HPQ9sJsQyMewtQUVss9ssaj8i/YaqjS6Nnu6CtHHyoGOalrb684Cgcy9CPShDGsGdtVhOKsZ7z5M8cS2Z+l2SMW+Hcv2Y/9/5JTvQmGKKRQO0ifooiU2XAATKTZHc4iPEqYtFuVx7addd9MUVf5hLigVlCaClwipAME0V8Bqpnba2kloVMfuXeWBSrXe9cI5KG7MsJPZrn5bhXVejBGHwlkzfddix3AjjuIO9WzJvIuA/W7kvFw8WxIMIsqr4GoY5RJbW/umYV4wmch+W9RmLSng17vjG9sAT7+7272grwrdDnXjBjuau3UwdJ6Rbw1fnmBJSKPjH29sKrAjcE8PLBGyzj0v3f/NVbUx/etYyA== 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:(13230031)(38350700005)(921011);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: B4304hL1NlfpuoX2dRuEN9E1kspDsUg1keAOyIbUuk/efM9jNNp04fSmBxcaJUvCF028YNvV0Al7WVEoGUT6SwsCb+HawD0BN/vUOC8VZqwLBoP1L+H9oeszwaGb8b1dcIWrjL1XNTYaLw2SoElubcZ26jTl6FGbSMkfpw8E8+efXhBsTVZYMh7o0Em2eWn8aXXN+WP/AFTFLYUhyl/LjIOdSrl7weh/WhkeMAmH1PhP53lVWZQm9EJgDEKOq2b/gfYj5BSQ3ILpMBZR26CvQ5y6+XSnAKFLkoSQfSB8U++CI1e27XcbtkTGVmVlNF74Xsx7fyk5Ph86JS3OyGsXDL7K1vax0WNaOH25DKSj20DZHbk6UOJFw/oLJjpXjQiFAJb1gpfOQ/h/VF3z61hidrfPUI8nzk94kduVUfbi1pNsCTzZhhk5Dcse0U7foOTOVHxTNfRA9z6Deow2na49sfa+6z37JoG+GKkRRlvZ5t3aNW2bpyf8WbnSORy7S43+EvdeAbPTLu4/uG8WH3K/wd9WfVNdVy85Ne7/D4y/x0WmVCCEGDEbAzZvPxWmNWlc/yXnXGRF5zWNWWm6fS5UXl/qOHlY4O/E2At2t/LX/gigYQfl6VS3KcIsNfBsr+vcp1njnpgIQ/cHEvlnh14v9gEbKCw0xNuGi3jSPLrN+Rtn2QoUM+RSUaaSErPA8AKgBH6qhpg5y1VStkisPJ1fmdznntgZ609zWM6Rl1KrObUOaOfMZNKy3kB/BVJLFcNGbyItquRmBD2rCtBohGHMaJfUXmGSZxW9BCpjrmfphBAiAy3ZhuDp2DbqBpGQP67yDQKe0JW0aeTQQr5K5Fa6S0Vcc2kUN2RNcsBKRJLlGeDSHXUqUhxND8eqwl2gesDMQCed63Ogr56aBnw5Qk98YgqGmrxf9KwG9PUhvv/JnnODg0aAId/0TxJYyPytOA7bdYWipVcGwf+prBc4iNYM0C20kOCeQp7JzppgfLH1HjOTvUcEhTfIL8cXh/KZOAbctTLiV6UPmfYxjkzKIBuATNU1PuSeRqgCamt9IyOlaI3n/FT4yeIsvXLexorn1Jv0KVjYFUtiNN9e0tHlhTFy6HrBY8uW221EdlapXYh1j5TFIhP/g6uC6D/LdqL9ymIxIMQluflwDP4dSU5lbJWzp01fku9OCaKQPBMAO1gEJXcDO4aWSa59siF7Kcl7IWZPhf4eKAEb0JAIhe8fyJ4LxoMyrb9l5z8Bs172kIjq7Y83njrSb0IQF+GqdFx/l3soRIlDbuQXaag7qVQWm37zFm84pxw3eEW4KubCEr8EE37e4SYBqwymdUakI40aurGl5Rb4+RfH/Xwf74NjystPc3I8dyS3J7s9NU9OIrqfTI0hjzanU84xuzB1ixgEykf6gT1QS6lQJjYw/B1ctfQIGpyFI8fOpHfSu+cwldGQRoQCWPRRG/2K72T/sIDxDtXfpEJilCBjpC1inpu5s8f98Hb8M6tutA6r7a71Euek+l5S0yFTuxLGZslxmUqqO4jLgS3k0XKHNMk4WCylLl86Y3egku9l1A8tLc2repyTJy8F52F6ovo72VY5s7AKffJcqbid5I5HhGZvBzP++Db20A== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2d20bb43-251a-4dab-a5cd-08dc3316b2b2 X-MS-Exchange-CrossTenant-AuthSource: CO6PR02MB7555.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Feb 2024 19:52:56.1105 (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: xjivjzR6xwtTL2x17wZkhpOMHRZDw3sulVpH1qHgc76PCuLF5WNnk4MRGzNV8zYrfaxfN1Td+/hnW7HVPkq44f7ZH1rzNHfOfaPaiRzFBio= X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR02MB10213 X-Proofpoint-GUID: DV_S6r4EcsjlBzwnVyasQwLxFJ7PbmbC X-Proofpoint-ORIG-GUID: DV_S6r4EcsjlBzwnVyasQwLxFJ7PbmbC X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-21_07,2024-02-21_02,2023-05-22_02 X-Proofpoint-Spam-Reason: safe 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 | 9 +++++++++ include/uapi/linux/kvm.h | 8 ++++++++ tools/include/uapi/linux/kvm.h | 1 + virt/kvm/Kconfig | 3 +++ virt/kvm/kvm_main.c | 27 +++++++++++++++++++++++++++ 6 files changed, 65 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 3ec0b7a455a0..1858db8b0698 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7031,6 +7031,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, e.g. when PML is enabled. + +:: + }; + + 6. Capabilities that can be enabled on vCPUs ============================================ diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7e7fd25b09b3..994ecc4e5194 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 /* @@ -831,6 +832,7 @@ struct kvm { bool dirty_ring_with_bitmap; bool vm_bugged; bool vm_dead; + bool dirty_quota_enabled; #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER struct notifier_block pm_notifier; @@ -1291,6 +1293,13 @@ 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); +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA +void update_dirty_quota(struct kvm *kvm, unsigned long page_size_bytes); +#else +static inline void update_dirty_quota(struct kvm *kvm, unsigned long page_size_bytes) +{ +} +#endif 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 c3308536482b..217f19100003 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -210,6 +210,7 @@ struct kvm_xen_exit { #define KVM_EXIT_NOTIFY 37 #define KVM_EXIT_LOONGARCH_IOCSR 38 #define KVM_EXIT_MEMORY_FAULT 39 +#define KVM_EXIT_DIRTY_QUOTA_EXHAUSTED 40 /* For KVM_EXIT_INTERNAL_ERROR */ /* Emulate instruction failed. */ @@ -491,6 +492,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 */ @@ -1155,6 +1162,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_MEMORY_ATTRIBUTES 233 #define KVM_CAP_GUEST_MEMFD 234 #define KVM_CAP_VM_TYPES 235 +#define KVM_CAP_DIRTY_QUOTA 236 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h index c3308536482b..cf880e26f55f 100644 --- a/tools/include/uapi/linux/kvm.h +++ b/tools/include/uapi/linux/kvm.h @@ -1155,6 +1155,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_MEMORY_ATTRIBUTES 233 #define KVM_CAP_GUEST_MEMFD 234 #define KVM_CAP_VM_TYPES 235 +#define KVM_CAP_DIRTY_QUOTA 236 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 184dab4ee871..c4071cb14d15 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -22,6 +22,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 10bfc88a69f7..9a1e67187735 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3626,6 +3626,19 @@ 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); +} +EXPORT_SYMBOL_GPL(update_dirty_quota); + void mark_page_dirty_in_slot(struct kvm *kvm, const struct kvm_memory_slot *memslot, gfn_t gfn) @@ -3656,6 +3669,7 @@ void mark_page_dirty(struct kvm *kvm, gfn_t gfn) struct kvm_memory_slot *memslot; memslot = gfn_to_memslot(kvm, gfn); + update_dirty_quota(kvm, PAGE_SIZE); mark_page_dirty_in_slot(kvm, memslot, gfn); } EXPORT_SYMBOL_GPL(mark_page_dirty); @@ -3665,6 +3679,7 @@ 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); + update_dirty_quota(vcpu->kvm, PAGE_SIZE); mark_page_dirty_in_slot(vcpu->kvm, memslot, gfn); } EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty); @@ -4877,6 +4892,8 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) case KVM_CAP_GUEST_MEMFD: return !kvm || kvm_arch_has_private_mem(kvm); #endif + case KVM_CAP_DIRTY_QUOTA: + return !!IS_ENABLED(CONFIG_HAVE_KVM_DIRTY_QUOTA); default: break; } @@ -5027,6 +5044,16 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm, return r; } + case KVM_CAP_DIRTY_QUOTA: { + int r = -EINVAL; + + if (IS_ENABLED(CONFIG_HAVE_KVM_DIRTY_QUOTA)) { + WRITE_ONCE(kvm->dirty_quota_enabled, cap->args[0]); + r = 0; + } + + return r; + } default: return kvm_vm_ioctl_enable_cap(kvm, cap); } From patchwork Wed Feb 21 19:51:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13566275 Received: from mx0b-002c1b01.pphosted.com (mx0b-002c1b01.pphosted.com [148.163.155.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB3CB83CB2 for ; Wed, 21 Feb 2024 20:12:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=148.163.155.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708546335; cv=fail; b=p3aowXRYMS+BVpuhcIAob3fdnGvhxvv3xJYB+kpQ6mE7I2B6jmn089b0Rp2ZUsQkoeAgiohbdKVlS0Y9EMwolyPoUHoHcHK+ijadBTuCldaKgSDOzLQ3FiP9TL1WUycGRfZyYR9QgVSz/7/svPy7fRSTDFFIxk2QGOtSDTFuj94= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708546335; c=relaxed/simple; bh=ovKy9DHo0Yx2y8FJ+BXVlEE4JYVGIjUJFF3k2KWZiTs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=DukpogP2JQ4zC0vDc5iBqJCDcdBAfqD6mac78Nua3582qVMbdhA14ieFehfy7xuFfKpvWORmIehADQylmWw0ei+5P7w5x9lISEtVLS5R1IqSk82PAIUhQiYWALWMI6FVy7pvHkjePURnqSXDICrOm5U4eAzFgckHGcyrysvRnpY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nutanix.com; spf=pass smtp.mailfrom=nutanix.com; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b=UiUYK64C; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b=PuM0B4vn; arc=fail smtp.client-ip=148.163.155.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nutanix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nutanix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b="UiUYK64C"; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b="PuM0B4vn" Received: from pps.filterd (m0127843.ppops.net [127.0.0.1]) by mx0b-002c1b01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 41LJdcfI003503; Wed, 21 Feb 2024 11:53:20 -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=ljnWk6llReTs/Hvun7QWlEoRv4h7EPOFlu/ENa1FL ao=; b=UiUYK64Cy6M96CRoB1fC+kz8OJAai5vK2KnCl8XjIYTqrKcnJJVSNmps+ 1NPuR+EiLR+wrWSR0W9utoOBiLYYxFi6Wu523nMYlwsZSiJV4BbJF4Hkhd+PBEJv 3Kja0Dqkd7MU6jiG7a8cj4Le3mqSzAFglWVDC8HjqpI6oUoxTwebclG7z6GzsmvT ygdWJtEayIhb3PvXwUOyEHKrj0P+lS4EhPebxo+1GSVn8jyCs2hLeguuEe1PFo8y pwykR4OVa+x7FAS4UakQ2N0rvFRWjDm03C75dcsB54FaRdULZOUmi/2028pL1DpE j3h2/xKK4VjINiyPbnlVZV/8DupRg== Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2040.outbound.protection.outlook.com [104.47.66.40]) by mx0b-002c1b01.pphosted.com (PPS) with ESMTPS id 3wd21839v2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 21 Feb 2024 11:53:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=egd0h2d42lYvfdGqRGb4ZVA1dXYHpecyCVjnVtBpejVSYfyM+QtXnDe2s+qRVqqhVlw2TtiCzzgX3IBXA/g9cQZf/xj/veLsOd42R5WI1qt52wijkZbpChRk3DvyQZdnvbmDWfBRJ+P/0xJW5NkyGNeGiyeI/34aMH4mSSRgR0O04ZIuYECC+2L1G4CdJywYmoPLhZOFlY2ccYfVFyHbZ6/aKYqmj4kxXPt8s/QieTdvi3XANA9Pp5HkROJVnaagPr7smwA/C92JFfpHwUHRP+8yhkDqCbdHo/89ly8G8dI218E+0D/0W36X/gZVHv7ny8vMMQAN4zY5m9YZXuxRHg== 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=ljnWk6llReTs/Hvun7QWlEoRv4h7EPOFlu/ENa1FLao=; b=IFCRpJ5EZvOTcHC1WTz0bwdxFMJ47J8GQU46J33Udfpx0Say+SPX0dboHK79PMuN93PCnnNLhFRg/znlSuQnP8oXaRAdKXeQT5vIMNR1Wy00ZIuLF25P4/K4vHmzSsjGq+UI9yvMDy+v2Xc4otVStQC8rMsdQM+1TICL9bvOjITkGqmjDOhhE16w36bSoshtLK58zFVgez9gJrFePXdPniV23soc8lON3gZ4S9gh7aAWInV/R2iDyfZm2J7Zh/wk0H3Gf1t5Up1NEEVF/cFD6hQfxvpuW2g+hKrkXqLobcQ4/I8SPcSsqfIaW7Vg9ZilXxszGL2FoxOXq9XMPTsy7A== 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=ljnWk6llReTs/Hvun7QWlEoRv4h7EPOFlu/ENa1FLao=; b=PuM0B4vnDsgFB3rOow3gzhqivLtb4RfxYvtE+PGvVcvg4mkMxcYgHxTR7dcFCixM6yrKOwR7ag8va9PkgBI9xQbulPwa4rjcHjL2iXrXb3HOCwZ9WgcbIWaVWo+2syocRrBCO0PlU2qOuHVz3AM9+bmHpoGHWP6veD1AbV6DDbw53GjS8JlcsvLtdg6TzuDC7Y8ys3k52evtPqBEx4hEvFHJxt5QsNb0JBvZ35V1WxOhP6LZdShabpC1EbRDBSi7y8nl3ApNWE+0djVcSqKwSz+DjYWDf8u4wk2k1rTmbenIMtzsI8OkKU4Ad0mLPO1Io0y3VwFKXBqXoM3dlabjQQ== Received: from CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) by LV8PR02MB10213.namprd02.prod.outlook.com (2603:10b6:408:207::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.21; Wed, 21 Feb 2024 19:53:17 +0000 Received: from CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::4a9b:daa2:2dff:4a60]) by CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::4a9b:daa2:2dff:4a60%3]) with mapi id 15.20.7292.036; Wed, 21 Feb 2024 19:53:17 +0000 From: Shivam Kumar To: maz@kernel.org, pbonzini@redhat.com, seanjc@google.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, catalin.marinas@arm.com, aravind.retnakaran@nutanix.com, carl.waldspurger@nutanix.com, david.vrabel@nutanix.com, david@redhat.com, will@kernel.org Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v10 2/3] KVM: x86: Dirty quota-based throttling of vcpus Date: Wed, 21 Feb 2024 19:51:26 +0000 Message-Id: <20240221195125.102479-3-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20240221195125.102479-1-shivam.kumar1@nutanix.com> References: <20240221195125.102479-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: PH8PR20CA0013.namprd20.prod.outlook.com (2603:10b6:510:23c::27) To CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO6PR02MB7555:EE_|LV8PR02MB10213:EE_ X-MS-Office365-Filtering-Correlation-Id: 83062884-eeb0-4b52-9ea3-08dc3316bf79 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: lffNAEGl2MumGm+BkubKeZFVCnbG3CHq7fM3Zdk99gBVtIV86fHVG71315o82HT0HnwkhI6u3RF+xgO66s8FeuyfOx8jlGJ84PVJQWqWQnLqOSSXeGA+kpm93/Kc2B9IB6KAVlGJiHru6NyMUhb4sZ9SFTJH7h97oNfmJkmcVGEVK+ZbRRVpi9Xj62YOhcjbIYt92suRcCpb6ZDszmlehpW8ijA9a8EclwKDNLQwnIvnOc4KlXlUqyXSxgsndw6r/WMesUhei/fXwdPa1aEDu2bXw1gHv84CiIMmjG9lL2CVvnVYWmrN3cLII7S6uePug37Xw9Ed7mCKkEVQ/55b8K4W50VtJyaSMdPTbJRIfftjnGXPAUi3y+4HxYudzKnOcI/OH1mQO3DTBeuFJJJ83FeohEZ/0pbAINlcVndLKLK6dvrpr4YbGnzdg8dhvI4+E8qquTi8GjdWACccIB08Ui5+pCqFGJxolC2Co2W1eLiXRMHF5ePJXYZb8ewzk+HcsRqb/XB6qD7S+9oYYvJM0n0Zjd20GN4yJqygo1FvJ/mesYozLwCn/JRar8iu45HFDYy8l7Ltp7VcfhH7Nr6L2ffTn7d93M/KcjS6JJw9wCnyX4bg9lZ9oQOD8EBIsbX0TCamfVZCgS9+0rsB1o0s+Q== 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:(13230031)(38350700005)(921011);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UJGd1iC2lsIuGSFfldx6CkDa8ldzfFnUOBJ3Xl/ZhVAHoVG9C2aGxRJaD7CTaS7pbuSpzREE4e9aNcqap3Qu30vrTD5+WBLXPcy+M/07Ynfzy6v4q3uR1AqfwHbXxfgHiaewUOR0JzBs/DomgP6LqRNdO24aCJxDi/rzFVrcde9ZVi36yj+Fya88cT0i146Dsvb9tCAePWUik6uj6ogpyqO+cF/LEDmemgNY3rGiwdCxiAEY1Uh92axZDHyQ1YEOxrJLrwTTaV6mtxUBCBjqgoRCWsmI+Vk7CN40jkELU/x7BaUk8MHcPQ/IYVBw2PSGzKZGztORJcRkM2k+1H9sdjlA45NjBBAfBVSs9jfqhjsdsIMWhvf21ZekCp6JCmpfcay2UwC8isqGiIdzTH2IAS/CYJ+gOtt+DtZ76LI4k+zNjqrj+SKF2lC+DhgNeLUGSV4ISVsV8GoCCR40OLyE4MT8kLZreyr/zyEIt0unEDJasXvWTVHKZ4pEGwPKC9E2dr0EfLSo21X8/rKYK5yPIOcR2tiEGw7gR0hFbLCIV7fEsTw1qq8uPSl0XQRmRh2W+Qku7+rU3RvKU7liPxeMXX9ay1Ew+colHav/J3Rwmvm8+g78WEajavPeITjfsBZkxQiBoMWzn9KZXG1qHjcGpy1zj5kMQp1Fzlk0zM9/IYxPG16Ta3RY3cFvt0/M4JrIASQI4sMGmLCBvewexUiUIJTD4irp6YFYV5xaA7g2NovCacNKYXQ9O75ogXPnTdLkTDK158o/gv36HvMJyMwllXpc+EmlA8i++7RyaxrP06sv9OS6dsWqXrCAeKZLrgw1vvMJ/l1Ll2PyIUz5l7lazyK3d/FwT5PCdjMgz/6Dj6KFrkd3+5AQuga9QU7VmGmTpaXUQom6dCFRQJzoPhc3HimiX1eSvEnu/nFSE07vRDW8ke4PZ6uDTjAn/yAMfDEVqAjkPuFy5NO7m2aAz5F8R09NnvHVEYhIVlWDaMpSq+dv7FtmE5iNi08Z48CeuKuV7MwCEmu4Z5tOWE74Xq7xMlYq2QURLXWnB8hrogUSrbFfpcFJwmnfnW1ggx1PLu+ILY/ZVuvhQz5tPEb6ehMh4lBakM4iMPn01tQpSAhHMGx/JEdkBiSG+ZJRPig9nDZvaqy+vYKAHTLLD+Nlu2Sg69aDcotJnewdAW/vnMvJcpeHB4PlAlNYTxXSqVRTdNbCxm7vwV3a7hgMBNUt9Qmmu8V6ue7RM++bQ7vHV0imHFALgaYDUwE/pgq7gdaIhV4hwVi0VkXe2Y90SVnyKktk59acVvwLimN14/RztbOTQ2iBZ6S3haLGiuzZv7moUINLd0Se+GtrIeiOP78hnCZhrCxt4XQfLvYqW7LWo5Q35xbyOCKmb4V/+BvatG4JuVkKq6GARTMrSGPXq8JCt3SYNjmToY8r7orbCgtZeYMbN3/64XCpwPG+1vIrifzi4gXwo9xUuXCjoEPcQ5A21/lwKWJFCvS1Hf5gF8VxMxXhzpbn7CsNsvI0b2spUn0rPe6roN35yzUNEsYnUXNgYq+xNGhJhrrZPaHtstRtm+UXaYRHNHZynEXS84DqA+ilKx3SZ5EUa/K+I55sqTa6kH0QWg== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 83062884-eeb0-4b52-9ea3-08dc3316bf79 X-MS-Exchange-CrossTenant-AuthSource: CO6PR02MB7555.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Feb 2024 19:53:17.5375 (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: F00JR3Lfem191N/Uvyv/PvZOpXxzZDYZdIdDnjvGBL002M4fZ5jClOi4OJIUbR08BmLgGYwiYQXbib5cvEqLEkZUSLj4FtxI00JDE8Br7zg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR02MB10213 X-Proofpoint-ORIG-GUID: Hn8zRy3jxy-kTHRU28Ga0AdjY2RNnc_O X-Proofpoint-GUID: Hn8zRy3jxy-kTHRU28Ga0AdjY2RNnc_O X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-21_07,2024-02-21_02,2023-05-22_02 X-Proofpoint-Spam-Reason: safe 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 | 6 +++++- arch/x86/kvm/mmu/spte.c | 1 + arch/x86/kvm/vmx/vmx.c | 3 +++ arch/x86/kvm/x86.c | 6 +++++- 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 87e3da7b0439..791456233f28 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -44,6 +44,7 @@ config KVM select KVM_XFER_TO_GUEST_WORK select KVM_GENERIC_DIRTYLOG_READ_PROTECT select KVM_VFIO + 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 2d6cdeab1f8a..fa0b3853ee31 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3397,8 +3397,12 @@ static bool fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, 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)) { + struct kvm_mmu_page *sp = sptep_to_sp(sptep); + + update_dirty_quota(vcpu->kvm, (1L << SPTE_LEVEL_SHIFT(sp->role.level))); 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 4a599130e9c9..550f9c1d03af 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -241,6 +241,7 @@ 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_ONCE(level > PG_LEVEL_4K); + update_dirty_quota(vcpu->kvm, (1L << SPTE_LEVEL_SHIFT(level))); mark_page_dirty_in_slot(vcpu->kvm, slot, gfn); } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 1111d9d08903..e2f8764c16ff 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5864,6 +5864,9 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) */ if (__xfer_to_guest_mode_work_pending()) return 1; + + if (kvm_test_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu)) + return 1; } return 1; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 48a61d283406..4f36c0efb542 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10829,7 +10829,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) r = 0; goto out; } - + if (kvm_check_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu)) { + vcpu->run->exit_reason = KVM_EXIT_DIRTY_QUOTA_EXHAUSTED; + r = 0; + goto out; + } /* * KVM_REQ_HV_STIMER has to be processed after * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers From patchwork Wed Feb 21 19:51:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13566266 Received: from mx0b-002c1b01.pphosted.com (mx0b-002c1b01.pphosted.com [148.163.155.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D544126F35 for ; Wed, 21 Feb 2024 19:54:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=148.163.155.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708545247; cv=fail; b=Gfe6DwProY7JxqlIF9d7UMmea3lVPOh7i7yfT9H+GoquVJ22PEBLT90BSoYeh+NOuxTwjsJnYEZ4JiiF5bUXvZgJMRTxzvmJx4V1uO5S9uQvLUHzg+lE0b0Dmljx8FhjeyymivcgmTtFakIeezvrv4sPPbHu6sNSA6KIGwQunlY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708545247; c=relaxed/simple; bh=Tzscmp/jiuDKJ433jbtzU0UCznp84mbfnBUExJDZY7M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: Content-Type:MIME-Version; b=XMgGEHW6cDIjWSjt0MjbaHbk7HC2p8f+NRk517oT7aUxntybxnZ/tF7PV3KNW7MN/eAahw4OTbOJFHqqp6nKv1XuqcM3+4MmOZaUaXyx75JW6AbNQavISeTQMv3YSjoK09B/y9DgTIA9xl4eWAeiJjD7zTF/xQbI4vrYXB2a9Cc= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nutanix.com; spf=pass smtp.mailfrom=nutanix.com; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b=lLRH70qj; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b=Ta7gf/1Q; arc=fail smtp.client-ip=148.163.155.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=nutanix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nutanix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b="lLRH70qj"; dkim=pass (2048-bit key) header.d=nutanix.com header.i=@nutanix.com header.b="Ta7gf/1Q" Received: from pps.filterd (m0127843.ppops.net [127.0.0.1]) by mx0b-002c1b01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 41LJdqpl003996; Wed, 21 Feb 2024 11:53:46 -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=MqUNWpX5nuJ3db/znXJ1ZzQQ2f8fN1kr4Gk8v/1Ty kM=; b=lLRH70qjk9rQlYYehFyNDAQAae0bkzFw8s/gCuZkf2QTK8tFDVI1fW/RV /OUjzgxcG4qZwekA7ss0u9/jg2rf1x7wlvA0NdJa7v+l1DvzVnYW3soLv4vJw74K VQOOgQDfZTSSBKXwUrH6m9dKFdui5LWLzPZSBhsRj8er5nHtRRQ36Dyky1n2+riW hns1s1YEvRsAOnOR8tI7/FFQMFecEdYj5RViKcnCOPDDVPxd53xXWepTU1mo7Hb8 UZnQSZDBwSWCTva/bOMtfoP4RiNcRS2XZlYHDfMsgprveV40UMECNnxA4cl1/oh/ yuVyylEK1byPDU1TanAoW8o0lLD5g== Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2040.outbound.protection.outlook.com [104.47.66.40]) by mx0b-002c1b01.pphosted.com (PPS) with ESMTPS id 3wd21839vv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 21 Feb 2024 11:53:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kksOemrWd6UXhreR0d8lmiHfxAGDcCTVDMHDT9KZn2rYeFCJvMZi56lv48neJF7PL4ax+6fWPjGMxRAWc73FPD2mL6hLNx7MQNycUGu8vZsHIh/+KYBexrLboDHwirDgL8ou3Y2KfteNyU0agZO8y7EEcQH2vyhs6MWbmCXMwXvDcRSU51H4NrhYkjvTpvfPH6gaWgeaRfWwq6wHRHBlTMloYfNgvyMQBZn/8CS8DeW1d6oagM+pUGSvacOKH/sNOIxRs6z1UbscCzCJWSWXLBBDuFCB6wDDOva06UzJRugUIYAzF+TXDf4ByQZcFcfVieVUASQjpFlf3Dri4YYZig== 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=MqUNWpX5nuJ3db/znXJ1ZzQQ2f8fN1kr4Gk8v/1TykM=; b=Gt5005TGeluvr2WpInQyHbkLvYrT2+zgo5r913jmguFJTc9pgorHfOQM2Cah7i10I5Vxz6PPZ0qKOHQaiUSirZjWbr8MnplwZ69F0wtE8YT5m8ig1TM0pmgusEPlR/uuUg7NHppbVgbteAHNPQgtKoLz9MBOJkjddKdlnorvmtLVWlWuud5bM+ZRfpE3HKj5bmYZuYUIA12QDtAHb2NRCWjAgvE0hl2Lw55Ume9XtZa18mFlY5Zr7ivcReuQnZ3lZc4wiLcpCT04d8c7gVbc2jHGJQ/jP3yCse05mfE0U8sc/ooxd8yrZCfOKRZAoxioYx5V/stt4bgsTe7x+L28zw== 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=MqUNWpX5nuJ3db/znXJ1ZzQQ2f8fN1kr4Gk8v/1TykM=; b=Ta7gf/1QlAA1D2Q3mnsKoidB1S5nAQRZluS7wgfxQ+NURq7hNNGlQHHBL9zNajMSZIyV9fbuX1qF8PI0Zfl0Wmd5du6FACakZJQv9ZiK4itrKRVGIzFRMdSL8Qx4fV8ixezSwnkUhYyNFytH/atTVLpHPotn/RVX9evvR+4xyI+k9ZZHir2bOkPUPVrTXiWlsdJINnY/v8X5r/F3v/SStyo2GbCO4X6Xf8ovureoOyPT2Gb+lB0DWuBta0MSvcjO1WAINFzzLDKNsBumQuqvDWJEDPpGz3ekC6EwVCzl4DnSbxT5eKdX3REdz4pa8PMHQx8qg93EZrzl7XBL9fbrxw== Received: from CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) by LV8PR02MB10213.namprd02.prod.outlook.com (2603:10b6:408:207::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.21; Wed, 21 Feb 2024 19:53:43 +0000 Received: from CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::4a9b:daa2:2dff:4a60]) by CO6PR02MB7555.namprd02.prod.outlook.com ([fe80::4a9b:daa2:2dff:4a60%3]) with mapi id 15.20.7292.036; Wed, 21 Feb 2024 19:53:43 +0000 From: Shivam Kumar To: maz@kernel.org, pbonzini@redhat.com, seanjc@google.com, james.morse@arm.com, suzuki.poulose@arm.com, oliver.upton@linux.dev, yuzenghui@huawei.com, catalin.marinas@arm.com, aravind.retnakaran@nutanix.com, carl.waldspurger@nutanix.com, david.vrabel@nutanix.com, david@redhat.com, will@kernel.org Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v10 3/3] KVM: arm64: Dirty quota-based throttling of vcpus Date: Wed, 21 Feb 2024 19:51:28 +0000 Message-Id: <20240221195125.102479-4-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20240221195125.102479-1-shivam.kumar1@nutanix.com> References: <20240221195125.102479-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: PH8PR20CA0013.namprd20.prod.outlook.com (2603:10b6:510:23c::27) To CO6PR02MB7555.namprd02.prod.outlook.com (2603:10b6:303:b3::20) Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO6PR02MB7555:EE_|LV8PR02MB10213:EE_ X-MS-Office365-Filtering-Correlation-Id: 31870762-76f0-496f-766f-08dc3316cf0f 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: pp20zQLWlyAlTQnrbUylEM/niWGZWSuCt4TqG0ArwYNvNcIoFQs+rbGGTU+7LeCXvyVjJIf9l7oQFu0QKa7AEyR22KhiEcd70DerLsMsWiRbLz7WhNV6ZfwVygziSBfczLPjj+OsHSHp1ff1cEE2kA+Sw/1HeypUXCUp5QbemitVZlJOTWNVmwBhCvZ+KI2CjvxQajC+ZG3rWOt8G3P3leXJ48Q4iJdpoxkdv8STxVkXasUs/x6pQ1VhcJx+Zuwm/ifA+PbUE6HA7wOGIguGez/1Zmyc5sw9orP2z9Lb2dXnrUMaV3jvirwMU7ooGEg5jWbShMPPzonij7aRF0PoYcHHTfPL2sah9smACia6SkDAWhRABrOXhviHvIR55XgSzL73MXiH5qkgOdWOjV3pXg2uHoLnAp+1mOsd8P6SktxKkL2JG8FF1ebxAXzkSbv642kWSq1T33Wxn7cWnts1o5h5oYM8LoxCOc8d8ZfEjOIzZTWP+fCYLslkkvqJtFUJAiC+Gm4PrCiKOyndcyEviOLiB4j5mpPyBa+SJFnoX4+vwkELoqFZw8NkRmHoEWcrs+JzFW0jW57v18CZ30ouMvdkpPWFpylLlOgYJlDO+LIaqOOZ8M+n5AAQEWqdh768UihXjE/6rZqJufGDWZxFIQ== 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:(13230031)(38350700005)(921011);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9TIPJVaNLihv2JaSCfQeCGBvDuxcIXpR8fpcVt43187POE7V8pwKy+VCxTPMTES+CAPaGRA3ikJIqonGpMOxw8a1/0N0Sa+zPVuVWUo+NP4PSVVYF+GJaK0RnU7Ma+UJH7bBPELqcy89pAvOcL67GIFBeMwBM4vQXqIJOdxPo1zvC1RQR3Us8GPkuuD6uydrHw/qhefri50RK9y0Psk79rjVVYk59Qaw7q3URjNkHOzOIjl+Os5sdhKB5BAepNDI1tXi8ttwJ2QKico/6dzEt+hIpuJhcsStNoiFdBqjAoB0h1T51VLJAZrJmeviRhcKoLqSuXXrJl3/y+WOpMsL/oqYGCUGS8f1g5fXG+u8x5I3M9WzCG/yW9xwqQH/KK7UI1FPCIoccSHS4e060CeDShfaKoZsb7CmfYfk+COZ+G2vIsclYO8WMdpSvY3J3XrIwxfrljqlJGhfjFNB1COXQOBh5mudKEmLqu7aai4wlmxLbE2DaTybSBDQD0ADV6geAgPvkjTXRH075KwnQcU+tC7bC3F6wQZdahEpPWqZbPgNTIW/lAvm97rNktXjXqlHxwK6Opv97cezxxd0MdI7/OdyLRgZQO1VqFod4VAAJfqy0XzmpVJrB4bnU8vJyZ/MN+VAvYkf9P80iMQbKNFjZ5rk94fTbHStWmsN8yK3rG8xibOsOej5rK5zgv+QBAuEvCLXs3Io8BFFQiopNd+bTgT7PAwyDZgboP5/d6lPBiKwRST7Hha0EdFJkroVrn/mqWQGKgFG91dB+OtrOgsofb2h1Ip0eDV01NQ8s1iW6ajZSeSbnYAB+FmqV/fjLslNJlMCXyv1KTMMwHMOt7PegzesU0PHPJbSTPgynS5SYmXJioSu2r+/JCEv01yiyMaugFQkB/lx6vBciTCc7QlkRkKDlFNvHpSeLaVZ94TLYZksANNYx/kx01yHEVDFXqGMWhVTKsdlbcuqOANoKYClpoV+g8856uEIXk8CWaQ0RVFPF/P9vMMSYyZ2v8fT/A/V9QOSBUuoV3Ut+Ggtzt3NYcVqDh3TFo64re4babIN6fRhb1wD0lvDJNpxmPUteWkoSPEY/bDlVukhvFedXcT6ijfuaWzWoWvTMR6oPrsksHPHdhiMQlF+jeatSfkfgV1nFCnW/j0WHyXvvNCbsqngP6dRWjd/ndu2WuMJGbMJD8mzw9VI9NLiY/dCBbbQd5x39M12z0+ugAGHKIrXsJy3RhC1+qczfQLmSz5C0gZh/QcNN6xw86jB8PaWiKbeUb9xRjKL7KQzXSC6ekOO8Evwr41/6he6gjYP//As8i6BmWyzHtwRUgra8erydD08ZeOgffcRw/H+5afLOG+B+6cRh8GAfpYrf3SEolsx1L17tXnaYQtvEj1fObHSq1K6nRSihSlLFR9EYdW76vxcEpDtSBICDfvwBOhLo9ON0pD/5cZfxqua9LTU/3OHJ0NVA+vTnC35NJdgYD1707xzqu0Pitu3rDnjl0bE9l3tLR1hZJ2II3tTXqV+ed0PauVWv9z4Y8qyQpHY2DwetPPQDZHHP5C4raaedvtWJaLEidWUy7exgy2vykUZIbUdNlvHd5GhI0vZamkTKN+4aq0zO+/TNQ== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 31870762-76f0-496f-766f-08dc3316cf0f X-MS-Exchange-CrossTenant-AuthSource: CO6PR02MB7555.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Feb 2024 19:53:43.7277 (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: f31+9wR+AVQIg/CfM2vKIcNTsZ35Bx49E4xDgxOmFgaE2CgFwi9PNnVYzjYZZT/b7VB/T9hGMa7KoOwCeq121/0KHMJNeIWV2yOZCr1/eDY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR02MB10213 X-Proofpoint-ORIG-GUID: e3dFhFhhqFj_hGoROAiaTq44dLiU4du2 X-Proofpoint-GUID: e3dFhFhhqFj_hGoROAiaTq44dLiU4du2 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-21_07,2024-02-21_02,2023-05-22_02 X-Proofpoint-Spam-Reason: safe 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 | 5 +++++ arch/arm64/kvm/mmu.c | 1 + 3 files changed, 7 insertions(+) diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 27ca89b628a0..f66d872d0830 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -39,6 +39,7 @@ menuconfig KVM select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS select XARRAY_MULTI + 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 a25265aca432..dde02c372551 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -872,6 +872,11 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu) if (kvm_dirty_ring_check_request(vcpu)) return 0; + + if (kvm_check_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu)) { + vcpu->run->exit_reason = KVM_EXIT_DIRTY_QUOTA_EXHAUSTED; + return 0; + } } return 1; diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index d14504821b79..77088bf9a502 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1579,6 +1579,7 @@ 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); + update_dirty_quota(kvm, vma_pagesize); mark_page_dirty_in_slot(kvm, memslot, gfn); }