From patchwork Sun Nov 13 17:05:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13041669 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 413EBC4332F for ; Sun, 13 Nov 2022 17:06:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235378AbiKMRG0 (ORCPT ); Sun, 13 Nov 2022 12:06:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235376AbiKMRGY (ORCPT ); Sun, 13 Nov 2022 12:06:24 -0500 Received: from mx0a-002c1b01.pphosted.com (mx0a-002c1b01.pphosted.com [148.163.151.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4F10DF07 for ; Sun, 13 Nov 2022 09:06:23 -0800 (PST) Received: from pps.filterd (m0127839.ppops.net [127.0.0.1]) by mx0a-002c1b01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2AD3QGFQ004849; Sun, 13 Nov 2022 09:06:05 -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=A56c02VdrH3MfGj5sJZpfMXsfALRWcPUo0k2Bj8Asu8=; b=yDHSjRMuXfUiF09YwSV0Hh3sExNASjJzLVYf4C2TSfd2I/hEJnuz/OQ9FjABpVD82Nlr mHHN85r8zHDRgRyJiLUpFKxRhMxLbLU5kIKwbxyl1Dsx9RqMJQDiMluk/IQcH34vCYvC kVe7yZ2wYZmFKGCXvPa0FkP6dWCdehsBuDEuk3MzrD1CfKVu3ed1bbR3UKYsh3L32jTW pj1y2SCjeyV4Lv7vtVrjv5Omgj2eNGpMdsD2rgZdc72SrGYg/EbvJQ2H+gRk3b1dZlCT tJ/TPq5BA1r35PHBJ98SyQ7K7iDbmOpkll+4v+tqmzjq++dfi6TqcraIVijPj1MHFgJc 0A== 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 3kthd0sk2d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 13 Nov 2022 09:06:05 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jtD86kLGxYPetao/jyGbFhswA7fqPBZgBpw+q9FTuEjIgsLEDhNhhQr4AZcHvuPENsVem4E08659gvHkKc/VvHq8f2iNAdzRD3x52zIGPRtuz7mE15lak+z9Ir1KK64oN40OSaMMLfqak0zMjCfiUeyFyfwsN0kAwZ3Ou6hkhAe/plXI8+ijkeSLdypQIlcgIjAPJaHBXcHh4sm4yPbwirHc1VIgVJZBhpDV44XXPsYjLi6p3bXlMHBSfW2H/w/6a1/QJVURN3+jJOVUzPbbv7Lv7LEYU3TWpZsdJ30c2xH21Vq+z7ef2UeqkG9Xxv8+xuP9VTl4SUximGSdLMMnSA== 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=A56c02VdrH3MfGj5sJZpfMXsfALRWcPUo0k2Bj8Asu8=; b=dc0WmhXv+gE87X0Mhcf5O0k2VrbHiREDIP+iH2YW74RVXi+JDeYRe04+FDSUL6EksgKbquBX9HBX0TaixkrwGSrVl1PUe8DrwHGi/VFTzcVlPlj9vVJ1quXFfvBMbhEmBL2rmMomnFa5GICuZTN57elXrTHQVGm+Lsy8wIVG3eLiSc35pdvC23Dw6ndXJWSBlmSvKEHH2KkXqvTCLnTTprrue+obItKFkCUT411Jx3vY2nwiRPMcZ4ra5hJ5E5SW8ZPY+j4O/rT3NDeU8TQik1MBbizB8+IRf7zFqEymC3Gsf2CCIA1rK1vrrHAgSMKdJ/lcYceZHQFpR93tPzqVww== 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=A56c02VdrH3MfGj5sJZpfMXsfALRWcPUo0k2Bj8Asu8=; b=MtCuDrYvyV59bQwavae/tmPNF8XPLrP8h6pi4RmFHd+f7t1HK7yWzt0nxYHU+PGrLxI5Nw4DPydzf+7pk4jGd/y2yldPYka0stR5eKKfbWrjki3BaD06IBd49Cb9vreWwRR7El+46ywUR/LRpBzUueNxn8IYWP1+cdjIN76sxz2XOXRRUU4e/dwl0TDwkAhKjuFMQ7g8ZBkFx2G5KU1hr81zyj7nYxTh41v630Nz7H1l5UV0hs8v9ZJMK8z1APepRUioLp9NGRxjvJhZmkytHmQ7CvF8bpJBp6TVhtlUr1u2SAlszdIDHV8/co0ZsAwqrXY4F00dAy0/V5++PKrscw== Received: from SA2PR02MB7564.namprd02.prod.outlook.com (2603:10b6:806:146::23) by DM6PR02MB7004.namprd02.prod.outlook.com (2603:10b6:5:22e::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.16; Sun, 13 Nov 2022 17:06:03 +0000 Received: from SA2PR02MB7564.namprd02.prod.outlook.com ([fe80::74fe:5557:d2d:1f5c]) by SA2PR02MB7564.namprd02.prod.outlook.com ([fe80::74fe:5557:d2d:1f5c%9]) with mapi id 15.20.5813.017; Sun, 13 Nov 2022 17:06:03 +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 Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v7 1/4] KVM: Implement dirty quota-based throttling of vcpus Date: Sun, 13 Nov 2022 17:05:06 +0000 Message-Id: <20221113170507.208810-2-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20221113170507.208810-1-shivam.kumar1@nutanix.com> References: <20221113170507.208810-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: BY3PR05CA0045.namprd05.prod.outlook.com (2603:10b6:a03:39b::20) To SA2PR02MB7564.namprd02.prod.outlook.com (2603:10b6:806:146::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PR02MB7564:EE_|DM6PR02MB7004:EE_ X-MS-Office365-Filtering-Correlation-Id: ad88ede5-0588-4916-ea84-08dac5995875 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: Myd/zgYOBk1Mb9dhLNAO4PRYFSaQMZAu34+aSKXjL8IQF2e6kgMAnZqAXFEbXmmLEUYYoZTHqwVDn/UCJxDzATCtIrIE3wvjI+NE9EgigesfLZ3BNiGlsOfcqe2O7m4BfKFpz/xeFqeTaQmBnMtPGdM8dsGcvpdkPKp7PbnTma/PcMUGTNlifeY3zi5sIhR/DIeDi1LIZu+tq2JqukgoJmMoocL42uI7/hM8K5mXkwy9rYkIsO50mX3xXfNHPUJzqZTxE9jKUdQa107AInTYrF3AnNYKkn6XaPxVFbUEGYJ6Ncw0kl9h3EnHbyR1iEkwn5eYByzBl1eViARGgtmYNacxG2rGhS8TKoUsSY6MvGJlgA1WlKqyKpKStU2XZUpsZdF7ORVFTWqkLpSxKgVlZzgttseFX3Oj84isQ/zsGAwSZeXyY5AmBfHXUjTNulZ8XEOfhYuU4FLXcHQ3umvJZKmVuKqgpt5zGuzvxl369u3vjKoeb8G84tu/5sLriP4O1+GaXzty38RNwpV431Yzuh9lqv5f8MK5/AO9iK0n3HP9B9w9hsLKUD9TZxsDBOJl1hP13/auEFdSYAsmiY6K018EXeq4zzbeFVjdJDOqY2WdpinB/kExZ/XMQYEAs7ILof2pJML9PXCjTXRc86Q9EAAtsvd3qzAr+tkjB3OgfghjvnRrfPHP1WdgbTt3PTzhli46I+P5a/9zmegT+UwMQoYzYMcfeZ+m6x1Xm4aB4ani096I4kt18d+21DXdpeG39oyrtIChs0ZKr7oHgi2EJj+f3dmqzuSp13cvvk/hybeKtYeQBZz+EkKdkqC6ZaFH93jdJgaMDCdj9Pox8FHw0Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR02MB7564.namprd02.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(39850400004)(366004)(346002)(376002)(396003)(136003)(451199015)(186003)(83380400001)(2616005)(1076003)(86362001)(6486002)(478600001)(966005)(52116002)(6512007)(6506007)(107886003)(38100700002)(38350700002)(26005)(41300700001)(4326008)(316002)(66946007)(66556008)(66476007)(8676002)(5660300002)(15650500001)(54906003)(2906002)(8936002)(36756003)(14143004)(309714004);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xO49JTcaS7YdSkAK9BgSwOHirpRsyZRBEIIA40kUsaMiBGbkgusILTdlzuMI+kR/U9MWcoNAuR+xl1aP/qGQ2zFaTGsrQ+NKe2HIOai7aU+JJDMu31t4Wi6+bGYkbhMhS7Sd83Z3/TSaKwOuhXbqAa6NFKhJpdGOYnPpivBcw9rKEDmjkUeUlNKoR0ZlYYv+N9lBJ4+uoTyyTgQ046TggrcIzke7pZDpUvX9EI4j6Y1zesQYRLlEDOECs8edCuamkzUignedJxT3YR97uv0tv6rI5hNtC2hNrIpwu8T4gVbmfMSLcTete/dKvt09Krwg0yxT6F0AhvJ2gTFmYtvSXI9yoZRuBMaHi0E0NVcbQ/ZIVcJIVfXaHkl8lLcTxSdIgEy3s+kFrsu/DF7IKaFEN1P5Nc7KOtN0Uekoh2Xx0yQPm/Tm76y1ttsaNGmwIQAkGJN7RSmoDq3SLJnF9J0NH5YAG5VluEEjnAWxjpZRxIJ7o3kF3NHc9hhGiwT6Hv43BcOBJ+uvA8VTWvxZ7504FBjpQo8oMoP53vynSKQSSlb4lKlS8eheuQ2gDYcHPZPEwA/8nqEMhdDe5F/3kXgQurmkuJ4ZaIbknuBZCcsSHqpEDfaYS+WDlNfKv7rqzKQSaipg4n/QEjEfcasyJU/69aUd5J5DTd5g7Sg0DqVIsKqZg+OVKSwLbNTkVi8Hixye8CWMlunOIb2NuRgbg12BmwCcdIof6HEy+uXwyq+VakwHIsxVOya9D1QaE9Iqg9Eeuf06HvrRpiAVpsW63ogl9L54PNW1WQXqctZq7JyeNIvwe8Cuqms+cmnVrOt3Qd1mJ0p0dNY4PtPlkRI8n7d4A6bttFI32WoUcVD1rFpSdBGPvpA6QPC8jDxGn260F1SgeveCxjcPo12Qtl9yVXKE/hEi3C/dtX9B58QxqLvXRprwUlsywIzfOyU5qDwz7BnA7t5Pyu27UR726HIbMKaH161tx8+SvXD9lCnnQSI+0Vfst95IpJSOz9VI5w0pg81h29fCwuaTtSvctCBSgdMZjQBcbPp5B4ILQBNuej8E5Z2EaVK9rDpc0on/xe8TMi2ms3+wnGsAXng9cIZercxbEWbhOg0AJHFxOKrhe4ULfts5nhkH1Q4vnSIHwiqT9hQOGTS+kWcmrDqYgmEjhrYtS2pwsKEvhDzF9WaVnL7trDVPLTyfU7Z4Ep1m3KXngcgaJ3ueY8Pog1h5kTfIcCRlKTwQjRS8IT6dNGDUpzxNZweIMSrSi8DE7Lo96Uh90cT9a53qXWiW4wjp/xlTukMI0Boh3mLzdkHOa78jKd2cCv5KICeARBX65TcHBBNuthlV/U7xqY6v5y6YZH7sH4o9PaosKwQNB6G/o+ptCfnl127oYwPSusFndTvng69dlIwSZOOl0Z1RhRZbtg/1FrOuoAiSw/u5dXYHRwJ6PvHhVeq75lH3ZY9gEhFQT6gaQbgWzrKdOcUxZ+XTg9ssOmD8UkRhoWpHCVBAxZs81/FyHGAfNixBWuXUBtDjFtd16GYq04lK7DBGqLBzlY9ML4J6qg89XqDxZAwog4VRbWYpyL0ZBkLO3Sjkl/b9gXoVtd333ceKN3VISR8h37BoZHP2SA== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: ad88ede5-0588-4916-ea84-08dac5995875 X-MS-Exchange-CrossTenant-AuthSource: SA2PR02MB7564.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Nov 2022 17:06:03.2543 (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: 2QguPRb8H6l1FqAHpzZTw7mqCJJKeei1FZe6jfGi1aJzw3WOsfwdnGkV/B0BVFuILZzBj52QPzTQloKZZ3yqwzmAecTBCLYKX2/+8i3rrCc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR02MB7004 X-Proofpoint-GUID: u6W_RVc2OgynMn7JaceURAK1V-NrlBH_ X-Proofpoint-ORIG-GUID: u6W_RVc2OgynMn7JaceURAK1V-NrlBH_ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-13_11,2022-11-11_01,2022-06-22_01 X-Proofpoint-Spam-Reason: safe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Define variables to track and throttle memory dirtying for every vcpu. dirty_count: Number of pages the vcpu has dirtied since its creation, while dirty logging is enabled. dirty_quota: Number of pages the vcpu is allowed to dirty. To dirty more, it needs to request more quota by exiting to userspace. Implement the flow for throttling based on dirty quota. i) Increment dirty_count for the vcpu whenever it dirties a page. ii) Exit to userspace whenever the dirty quota is exhausted (i.e. dirty count equals/exceeds dirty quota) to request more dirty quota. 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 | 35 ++++++++++++++++++++++++++++++++++ arch/x86/kvm/Kconfig | 1 + include/linux/kvm_host.h | 5 ++++- include/linux/kvm_types.h | 1 + include/uapi/linux/kvm.h | 13 +++++++++++++ tools/include/uapi/linux/kvm.h | 1 + virt/kvm/Kconfig | 4 ++++ virt/kvm/kvm_main.c | 25 +++++++++++++++++++++--- 8 files changed, 81 insertions(+), 4 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index eee9f857a986..4568faa33c6d 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6513,6 +6513,26 @@ array field represents return values. The userspace should update the return values of SBI call before resuming the VCPU. For more details on RISC-V SBI spec refer, https://github.com/riscv/riscv-sbi-doc. +:: + + /* KVM_EXIT_DIRTY_QUOTA_EXHAUSTED */ + struct { + __u64 count; + __u64 quota; + } dirty_quota_exit; + +If exit reason is KVM_EXIT_DIRTY_QUOTA_EXHAUSTED, it indicates that the VCPU has +exhausted its dirty quota. The 'dirty_quota_exit' member of kvm_run structure +makes the following information available to the userspace: + count: the current count of pages dirtied by the VCPU, can be + skewed based on the size of the pages accessed by each vCPU. + quota: the observed dirty quota just before the exit to userspace. + +The userspace can design a strategy to allocate the overall scope of dirtying +for the VM among the vcpus. Based on the strategy and the current state of dirty +quota throttling, the userspace can make a decision to either update (increase) +the quota or to put the VCPU to sleep for some time. + :: /* KVM_EXIT_NOTIFY */ @@ -6567,6 +6587,21 @@ values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. :: + /* + * Number of pages the vCPU is allowed to have dirtied over its entire + * lifetime. KVM_RUN exits with KVM_EXIT_DIRTY_QUOTA_EXHAUSTED if the quota + * is reached/exceeded. + */ + __u64 dirty_quota; + +Please note that enforcing the quota is best effort, as the guest may dirty +multiple pages before KVM can recheck the quota. However, unless KVM is using +a hardware-based dirty ring buffer, e.g. Intel's Page Modification Logging, +KVM will detect quota exhaustion within a handful of dirtied pages. If a +hardware ring buffer is used, the overrun is bounded by the size of the buffer +(512 entries for PML). + +:: }; diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 67be7f217e37..bdbd36321d52 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 help Support hosting fully virtualized guest machines using hardware diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 18592bdf4c1b..0b9b5c251a04 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -151,11 +151,12 @@ static inline bool is_error_page(struct page *page) #define KVM_REQUEST_NO_ACTION BIT(10) /* * Architecture-independent vcpu->requests bit members - * Bits 3-7 are reserved for more arch-independent bits. + * Bits 5-7 are reserved for more arch-independent bits. */ #define KVM_REQ_TLB_FLUSH (0 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_VM_DEAD (1 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_UNBLOCK 2 +#define KVM_REQ_DIRTY_QUOTA_EXIT 4 #define KVM_REQUEST_ARCH_BASE 8 /* @@ -379,6 +380,8 @@ struct kvm_vcpu { */ struct kvm_memory_slot *last_used_slot; u64 last_used_slot_gen; + + u64 dirty_quota; }; /* diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index 3ca3db020e0e..263a588f3cd3 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -118,6 +118,7 @@ struct kvm_vcpu_stat_generic { u64 halt_poll_fail_hist[HALT_POLL_HIST_COUNT]; u64 halt_wait_hist[HALT_POLL_HIST_COUNT]; u64 blocking; + u64 pages_dirtied; }; #define KVM_STATS_NAME_SIZE 48 diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 0d5d4419139a..5acb8991f872 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -272,6 +272,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. */ @@ -510,6 +511,11 @@ struct kvm_run { #define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) __u32 flags; } notify; + /* KVM_EXIT_DIRTY_QUOTA_EXHAUSTED */ + struct { + __u64 count; + __u64 quota; + } dirty_quota_exit; /* Fix the size of the union. */ char padding[256]; }; @@ -531,6 +537,12 @@ struct kvm_run { struct kvm_sync_regs regs; char padding[SYNC_REGS_SIZE_BYTES]; } s; + /* + * Number of pages the vCPU is allowed to have dirtied over its entire + * lifetime. KVM_RUN exits with KVM_EXIT_DIRTY_QUOTA_EXHAUSTED if the + * quota is reached/exceeded. + */ + __u64 dirty_quota; }; /* for KVM_REGISTER_COALESCED_MMIO / KVM_UNREGISTER_COALESCED_MMIO */ @@ -1178,6 +1190,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_S390_ZPCI_OP 221 #define KVM_CAP_S390_CPU_TOPOLOGY 222 #define KVM_CAP_DIRTY_LOG_RING_ACQ_REL 223 +#define KVM_CAP_DIRTY_QUOTA 224 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h index 0d5d4419139a..c8f811572670 100644 --- a/tools/include/uapi/linux/kvm.h +++ b/tools/include/uapi/linux/kvm.h @@ -1178,6 +1178,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_S390_ZPCI_OP 221 #define KVM_CAP_S390_CPU_TOPOLOGY 222 #define KVM_CAP_DIRTY_LOG_RING_ACQ_REL 223 +#define KVM_CAP_DIRTY_QUOTA 224 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 800f9470e36b..b6418a578c0a 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. @@ -86,3 +89,4 @@ config KVM_XFER_TO_GUEST_WORK config HAVE_KVM_PM_NOTIFIER bool + diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 25d7872b29c1..7a54438b4d49 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3298,18 +3298,32 @@ int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len) } EXPORT_SYMBOL_GPL(kvm_clear_guest); +static bool kvm_vcpu_is_dirty_quota_exhausted(struct kvm_vcpu *vcpu) +{ +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + u64 dirty_quota = READ_ONCE(vcpu->run->dirty_quota); + + return dirty_quota && (vcpu->stat.generic.pages_dirtied >= dirty_quota); +#else + return false; +#endif +} + void mark_page_dirty_in_slot(struct kvm *kvm, const struct kvm_memory_slot *memslot, gfn_t gfn) { struct kvm_vcpu *vcpu = kvm_get_running_vcpu(); -#ifdef CONFIG_HAVE_KVM_DIRTY_RING if (WARN_ON_ONCE(!vcpu) || WARN_ON_ONCE(vcpu->kvm != kvm)) return; -#endif - if (memslot && kvm_slot_dirty_track_enabled(memslot)) { + if (!memslot) + return; + + WARN_ON_ONCE(!vcpu->stat.generic.pages_dirtied++); + + if (kvm_slot_dirty_track_enabled(memslot)) { unsigned long rel_gfn = gfn - memslot->base_gfn; u32 slot = (memslot->as_id << 16) | memslot->id; @@ -3318,6 +3332,9 @@ void mark_page_dirty_in_slot(struct kvm *kvm, slot, rel_gfn); else set_bit_le(rel_gfn, memslot->dirty_bitmap); + + if (kvm_vcpu_is_dirty_quota_exhausted(vcpu)) + kvm_make_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu); } } EXPORT_SYMBOL_GPL(mark_page_dirty_in_slot); @@ -4487,6 +4504,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; } From patchwork Sun Nov 13 17:05:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13041670 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 0857DC4332F for ; Sun, 13 Nov 2022 17:06:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235391AbiKMRGl (ORCPT ); Sun, 13 Nov 2022 12:06:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235376AbiKMRGj (ORCPT ); Sun, 13 Nov 2022 12:06:39 -0500 Received: from mx0a-002c1b01.pphosted.com (mx0a-002c1b01.pphosted.com [148.163.151.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12EBB12A98 for ; Sun, 13 Nov 2022 09:06:38 -0800 (PST) Received: from pps.filterd (m0127839.ppops.net [127.0.0.1]) by mx0a-002c1b01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2AD5kjr5007869; Sun, 13 Nov 2022 09:06:28 -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=mLjmbP86RyBhgt4U1M0zUaOa9t+0w/5410/u/tA2QUY=; b=0OpLMBWuWsKyfvLRNUZEGvGlEjDL9IYEqoDptYgkC/9ylfUByogCMM8lQELzHsN5/qcu Alm99EVUDjkPUqedNU1pVARFpB6A28iMTzgzgMkwbgLiirGd4dB6TAxEpR5Xpa3wG82/ ygh5Kgu7J2TvB/3ynt3AhSCpBiLXCby6joPDpSLjjnCa7uSzHVbgu/XnlJrLiKmmUR7d pn5q2cpcPts3qPMpCwqpU1DxSHvsPCCIfVXegU2gk9WCc4iyx0CBb7SfJEVB61Rs1B0F 8xFYjhFGCrE7i+tnPospCz9bc57WB7y0GgS+RwpLv8G3HPy14tljhytn0wnYTZ1kSZ7E gg== Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2169.outbound.protection.outlook.com [104.47.57.169]) by mx0a-002c1b01.pphosted.com (PPS) with ESMTPS id 3kthd0sk31-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 13 Nov 2022 09:06:28 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=edjTyhkUcIZdkWQWlSG/84kTmwPUaEMPGXHiNUUkw+d8BaiK1LiKyZaGeaR+Py/aKn7Ly7yADwPoXURRCwab2IEeQIMl1SXb15/gogloRXAB6Q7/IshfTesATVTtd75TcNh9CPm3755r8sukU3pqMrajwB1nxMJamdLIXRjHaHHaXU6tQp2Nu18jTU9wcZDC+2Zc7C2YlLXkgMj51iBwtXVKBZenG+iOtyNh8dNNvsFaQlQ0iV08WpmIplST8HKBn8gN7IXsccdrOpdq/9/dyP/xq4bL0BjtZb/s+Wx1ewVZQsctEpwdcR9wz5AzJuIHbuXsyqN+qc/Br2QGdz+ofg== 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=mLjmbP86RyBhgt4U1M0zUaOa9t+0w/5410/u/tA2QUY=; b=QQ8qyws3pxxCqiNAbwwk43ZYa1VswIswFlX2OiK26y2XC5e+2DugUipv+W1b4NvX9aO/aLtz0gUrdObo8zY3bXd4KgQsYF9a9i7CVPqjG3I7D5gW4CtyeC6j/UbUk9LN30+FzxQCJ/9hT4RNgt2oA4yYo8fXogI3sUfP1vYXSGGRX/YL8xsb3QUy86/fYyZ21ugaNykCIXHst4xCeRWNEf9nT+OikLgNT7IkBerpMOFN540FfoVyiTCHtL3zBfjkTSbR7HB3+udt2e+S3FynJRAIeXa+O02qqsYIkBf8/V5K7fRA53LqUcLN5y38lsUabBmG3BZD5+zgfBnj6uXWVA== 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=mLjmbP86RyBhgt4U1M0zUaOa9t+0w/5410/u/tA2QUY=; b=gZKvF4apAX0wSsxoqsUEsb+XtHUuLNCTg+tMJ8Ah20kwUx62ZZ1y3MYLkj5P1MUQBJsyRN67nTwcpmLSSvMkwI8aQHS5m3fPdxMohajBta3c+u2MgAarmR2x/hi155SpSONFmu5Wj/LQh+CTBQjq1QvE8WkIkHLy7nIEybywaNGooXgQjSfDupIdsHB5VGFaS1KP5WaercqVUgmAT/81U6BSp3A4MzpHAmKt145U3gAC+FqZFzUcdoy7BcZzH+OlzXRZeZRKvJERw0u4KFtpr6tKqim6bQTSfAVy8sMTjZO/3brDQwkovzMISGUGXO7M3FZfbY5392xyHzOSWyaItA== Received: from SA2PR02MB7564.namprd02.prod.outlook.com (2603:10b6:806:146::23) by BY5PR02MB6723.namprd02.prod.outlook.com (2603:10b6:a03:213::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.16; Sun, 13 Nov 2022 17:06:26 +0000 Received: from SA2PR02MB7564.namprd02.prod.outlook.com ([fe80::74fe:5557:d2d:1f5c]) by SA2PR02MB7564.namprd02.prod.outlook.com ([fe80::74fe:5557:d2d:1f5c%9]) with mapi id 15.20.5813.017; Sun, 13 Nov 2022 17:06:25 +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 Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v7 2/4] KVM: x86: Dirty quota-based throttling of vcpus Date: Sun, 13 Nov 2022 17:05:08 +0000 Message-Id: <20221113170507.208810-3-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20221113170507.208810-1-shivam.kumar1@nutanix.com> References: <20221113170507.208810-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: BY3PR05CA0045.namprd05.prod.outlook.com (2603:10b6:a03:39b::20) To SA2PR02MB7564.namprd02.prod.outlook.com (2603:10b6:806:146::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PR02MB7564:EE_|BY5PR02MB6723:EE_ X-MS-Office365-Filtering-Correlation-Id: 64d7df9c-03e5-4c7c-4e65-08dac59965d6 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: +fWtjAXEUHleLgve61d6egFo+YfDrEApsFWz/q94zCMEcdgMemE85Tw92FH6mJNVGxbirQqg/d7LIKyhiWRvF7hHhEBIRv1WjITjfNe7nO/5lyN67lwbftqWj0sV9r6UqJh7w1VjNgXTA1OGVsyVwRkzcitWqXyA3hf4ixFYSF+Y/HH8+CZz6iWnKM6z0DNXPOBt7erEs42m46nXBLOn6pYiUr+JFSKylrzEGaxEUMtPZ3V9R7M4MLlbvfF5Z5/kd5akZaL1X4EVxMoDBSB0SHbsummgxklETlMVcFDT7kPSXE+F7SvvjYlcGg1P9mPXJ9FyCls4QYYEonw5UQqgWftLAwSRSz2xM/7o6Iw0N3Lmj45VMB+pBVdwyUOfEy2QiuUsKrqgsnHaj121msfC2q7vXRB88tic1RKSdPONkanrwonZ9Kco9JRzdgZInAkKTq+cPGaP9DiydQ69VdUbRoP5y2FAxuk2DosV6GyEJao0XLSedQfJDlFG9JajTPt5aoe6CqmLVNFHP9CxDFNu/0uNOeif+J7bANkFUqozx9csG0OHu8n9joMrAW7WTfPOtnL502Icy0KH+cSlwnJkgSrgNBDwQ4y0xvvkIHjYh5rHCChXII1q+NWr9wizrN5TNrDWsdauNXNIVbrEs57kYbfBbIX5fbn5oQ7eidYbR4Dee2BmcxU4cx0P3PKKXqqEJXFd9sV7cFMJXWqKT9PndiPVR3JC8tZhTRBsqXM/YUFzYI7etx+56aeU8Ec4SBia4K5cyReiAUAVJ8rTJBc+QQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR02MB7564.namprd02.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(396003)(376002)(39840400004)(136003)(346002)(366004)(451199015)(186003)(2906002)(15650500001)(38100700002)(38350700002)(86362001)(83380400001)(6486002)(26005)(2616005)(6666004)(6512007)(107886003)(478600001)(6506007)(52116002)(41300700001)(4326008)(5660300002)(8676002)(66476007)(66556008)(66946007)(1076003)(8936002)(54906003)(316002)(36756003)(14143004);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: fOXrNP0ux2URx6dNOv98+VSZ9RbYEtFvGhzUKxlgA2H5IVsIp0epqwYydrtBKLQUxio7RUWKXZb3ZMgE2P/98acXyqOMFBMdDBd9M/O2cU8gbHkXIPL+sjTLRc5tHBo5x3zqgUfwAuLLRwPvZC0JFDWYC/JOPTGqH/lEsmiW+VWtnUNFXpr1tPrxdDGMG8Wa9xT3bJMetLKsLSPkGoEUwhpXMucbkfJanchO+6SjElT+enAJyeTmznuS4qslHPR69hA+ZQ0HSDJA0sd/m1r4Y0KZc9VzUa/pxQv7s109QVm96/j8g0VpMKXlqHyh0zBhC44DzhA85umDvnZdRl310xRDINTLivnrpezsiI1NO8+GzmqyNfkVIkTD2EBofEDwx3tFWQJ2949T25oai60Mc0t+0qn9SgB5APDg/LVpmCfHHsNUiKjwDEdodb5j7xyz+iqvRsvTTxY1+jbb+K+xUNcpKSKdvlS3XJgGwGCiIlDEHIkMkDMukBJiGAkS44YRM+YFgZkm4ZUDTsFlspnvO+axE6CJ7n0YUhDYuWxef//uwqkqetKebvBwcMd8Djq76t7RzKO/j3KwXFYZDsdtLbNkCqZWR07T3tJLX/zI3hEzAL0aAN4/0asmCxA7RcEbWeh8a8/dJ/VmYm/Xauhz8eJ9ZgBXHnGdaYTX+3GvuVwfXe48hRq462RQD6Co9MmVcDYiN4JsoVpa+SDTbDcMr3lxeZ8WTEG1epBV9UM+0d39J8SWPQiW0v0sYYHxKRFmH78IIzTc4z1PD1mBz83HZctkMq9C6iMfPcXyKz1sV9zOw02ZgMHn0auabbQxdpWwLTMCrfvNjwD46Irq57OcWV/K4iGpoOaZ621IZLHublSeEkgpjNl0HxTfzJ8koSC3j9eclFqAG+8mlcH9mI1Wu1T/ylN89kiGyzwKYvNeSRp7ZOAi6O73TrKKIp6JAmqq7pwOMoelc0d9wEqZDL6uZE8keExZ2dJip4NrfAvDFdb5O33vF/3Z3T5360mo6EBXwSqRFtVi5ScsutYX1Z7I0cHqxBqdWKG43KneBCRv/VoSA461owIk/+apb9imPDPcWAkTbmUhi8k4BR8MWZt3mkHdOIpNrjkJMlJJpkEMIjSIFo/PcA5+DLscNB/zpNz6upx87UrpbSMz3RlsOFlcQ3KvrwhgBc+uw0/05Rrp7eWXwHkf67AukbuxR/Ns9Jt56eDlCnaIPMK2+/90eRw8sL0eswJFRrVmzDUNVF9/jC0/v9CoU2McbSZ0NFYLL+dSRgIyhOLL3VYM0UUYNzeFWpzI2QyS3qTSBkOaqn+9VNucsP1MVdJyhbHZrb4ZfaSBv4LVkWJVYLgU+TIR6Hgrecf4SGfL6ePsLrH6BWKbQaOfBjT7efi3izIRL7eWZYKcIYz7ck7dYa2ZNlSNngpVXCqVTRLkgeShPs5R36gIiUqPFSNZVlnR0H2Mp6G79sscDyX79bENIPihXXt+Dsuxvp4sdbBqrm9TZDFKwMMJKA5NHOfkHR/+/mVQAcA15kO8DsG9vhy0QK2RPAX2a3figKgh5TSSmXkJyU4fxlD/djkj49qtBn4Hg+KhdUY6TRylAiqGDOTjyguka2KPFa0bpg== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 64d7df9c-03e5-4c7c-4e65-08dac59965d6 X-MS-Exchange-CrossTenant-AuthSource: SA2PR02MB7564.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Nov 2022 17:06:25.6651 (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: RXj+Pa7UBN/kI1qJB3iawanGJqj/zB3crVxYwAMeqaM1GQ3y4GdPN5eMxzY2mBChnzhLH91M9ArDo+m6jVtIfJYWVQ2VS8Obi82un1AtkUc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR02MB6723 X-Proofpoint-GUID: XmLMMn8lGLt-cYWvuFybXKsU-LU0IjJ5 X-Proofpoint-ORIG-GUID: XmLMMn8lGLt-cYWvuFybXKsU-LU0IjJ5 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-13_11,2022-11-11_01,2022-06-22_01 X-Proofpoint-Spam-Reason: safe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Exit to userspace whenever the dirty quota is exhausted (i.e. dirty count equals/exceeds dirty quota) to request more dirty quota. 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/mmu/spte.c | 4 ++-- arch/x86/kvm/vmx/vmx.c | 3 +++ arch/x86/kvm/x86.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index 2e08b2a45361..c0ed35abbf2d 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -228,9 +228,9 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, "spte = 0x%llx, level = %d, rsvd bits = 0x%llx", spte, level, get_rsvd_bits(&vcpu->arch.mmu->shadow_zero_check, spte, level)); - if ((spte & PT_WRITABLE_MASK) && kvm_slot_dirty_track_enabled(slot)) { + if (spte & PT_WRITABLE_MASK) { /* Enforced by kvm_mmu_hugepage_adjust. */ - WARN_ON(level > PG_LEVEL_4K); + WARN_ON(level > PG_LEVEL_4K && kvm_slot_dirty_track_enabled(slot)); 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 63247c57c72c..cc130999eddf 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5745,6 +5745,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 ecea83f0da49..1a960fbb51f4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10494,6 +10494,30 @@ void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); +static inline bool kvm_check_dirty_quota_request(struct kvm_vcpu *vcpu) +{ +#ifdef CONFIG_HAVE_KVM_DIRTY_QUOTA + struct kvm_run *run; + + if (kvm_check_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu)) { + run = vcpu->run; + run->exit_reason = KVM_EXIT_DIRTY_QUOTA_EXHAUSTED; + run->dirty_quota_exit.count = vcpu->stat.generic.pages_dirtied; + run->dirty_quota_exit.quota = READ_ONCE(run->dirty_quota); + + /* + * Re-check the quota and exit if and only if the vCPU still + * exceeds its quota. If userspace increases (or disables + * entirely) the quota, then no exit is required as KVM is + * still honoring its ABI, e.g. userspace won't even be aware + * that KVM temporarily detected an exhausted quota. + */ + return run->dirty_quota_exit.count >= run->dirty_quota_exit.quota; + } +#endif + return false; +} + /* * Called within kvm->srcu read side. * Returns 1 to let vcpu_run() continue the guest execution loop without @@ -10625,6 +10649,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) r = 0; goto out; } + if (kvm_check_dirty_quota_request(vcpu)) { + r = 0; + goto out; + } /* * KVM_REQ_HV_STIMER has to be processed after From patchwork Sun Nov 13 17:05:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13041671 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 30546C433FE for ; Sun, 13 Nov 2022 17:07:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235400AbiKMRHA (ORCPT ); Sun, 13 Nov 2022 12:07:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235321AbiKMRG6 (ORCPT ); Sun, 13 Nov 2022 12:06:58 -0500 Received: from mx0a-002c1b01.pphosted.com (mx0a-002c1b01.pphosted.com [148.163.151.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9E6BDEE6 for ; Sun, 13 Nov 2022 09:06:57 -0800 (PST) Received: from pps.filterd (m0127840.ppops.net [127.0.0.1]) by mx0a-002c1b01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2AD405tF022082; Sun, 13 Nov 2022 09:06: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=5/+kEw+DHFL7XWMa6YRMq7RkZZbMdOAJ3VSvuUzTyjQ=; b=pVUs50OaVr25Lclz4r/RxNcZx+D4H9tCQTMe3ryLBA0xTSnaaFOFfsy82Z+nwAobXoqA zG8H5Ir/u7h1oHGgG1tChMYWOogvjtipKDTnBRZHz+kKAIA+k/Zzx4JThFFM2p3KGxIh c6/G9YSSMTjvwObcC/cL7qKiX1LKXBLPMDY9pI7kqONzEvE1cDr/B3ggukFRnKAXZdwV tYAmY6MN3oD8e7br7akKfWqtugWI9ppGC7SDqBpEVbOfVfWMjKCjZDAR0DZ7eMoKiNbU v8d3GxpQX0+q6yxQM3YyzQY6vGAiWZfK0nHSZczzWnIgT2SjjuuWJ47W9YPjZn9fBc48 gg== Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2172.outbound.protection.outlook.com [104.47.57.172]) by mx0a-002c1b01.pphosted.com (PPS) with ESMTPS id 3ktpm995th-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 13 Nov 2022 09:06:46 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qwdvuf7Rs5PdKXSGjswA2ebSbIjRqz7qL+nQ+IotNeL3qjnJeuDyrkiGsd56PiD8P5G/BBqNGvteAsov33W/Y4XNxkwQD07tTXxT2kVtBaXAdpMx3fm6j+dJ1dtJ1h/6czvuGAZl/gvW4LmxUyL2eHwAkP6pBMU1T8VyDrI3ZFSyvkgpQbilEj6FUL/8geYKnI1ktzDbY9wUKPExTs2DFPPtPgZwIjGzeyySp144zaFY75dJoMsIvmI+0pyHNC2sf8/Hseyn/wUZ++zuYyYPuvgFYHiRu/aE8POQ4d8vOOQswvzH0Xh+OI5y+W7/xHzJJmewVTuDzpi2xUj5X4n3vg== 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=5/+kEw+DHFL7XWMa6YRMq7RkZZbMdOAJ3VSvuUzTyjQ=; b=JUMq8cCqV8TKnXevpY77V8lCmHdegtsjUwNHgFbhB5XbQ8RRIQDtLS0ucvNGTAuxj6gIB0a8WW9Gwvle7H/Vl72QnTyZxG2xvV1FCKdzh6onurBdy8wIuBpGdwhDLRjENjJHJMfNzu1dbBL2tvODFJ/nQLPtx4jUocBv4VLPRyo8VErF5e9keIabMZzvPXDLkJSVZiYzF+WZkbc3jU2sfc54vZmf8ofOs5Izr/ER279ujGeaDOeHo8YH8x7zcA/qyXk5BfGTHpNLBD6N0U4EcHDJbampGgtXX6XecfNF2Hp350cxq5k+mYxLydMM8RPv4DmAacaPu3BIgxjhLvb34w== 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=5/+kEw+DHFL7XWMa6YRMq7RkZZbMdOAJ3VSvuUzTyjQ=; b=Cix/21Mo0F5tXUeqRq1fgUELWjdxOAkB3HsEPH2jKYDiL5SwoS/cf+4MHjYakIaH/LTBzXxFz7UrzUIAsUf7Zyidtc/Jp6NFVbgsamx7IFdX5uFObCcVOUHuuoGcM5u9LA3Zr/6ctGpzGFyVVO9GZD8jD4ZhneImDKKMeAQ4J0sLAdEWIxsu2dAxEX30p8JCRQr6ODxf/yyJDZFycAB/ZL8BJVOHZSYHcUKZA/4wFEhSxblcTvHiuf+E19zoE49z99pQQ3PJUUcUYyvNShnoSlxBVLkhYArhmB1Ts80bLlWquVRLJl0dmZ+bkmBW8piDBUXE+E5S998/uI6P/TRbHw== Received: from SA2PR02MB7564.namprd02.prod.outlook.com (2603:10b6:806:146::23) by BY5PR02MB6723.namprd02.prod.outlook.com (2603:10b6:a03:213::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.16; Sun, 13 Nov 2022 17:06:44 +0000 Received: from SA2PR02MB7564.namprd02.prod.outlook.com ([fe80::74fe:5557:d2d:1f5c]) by SA2PR02MB7564.namprd02.prod.outlook.com ([fe80::74fe:5557:d2d:1f5c%9]) with mapi id 15.20.5813.017; Sun, 13 Nov 2022 17:06:44 +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 Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v7 3/4] KVM: arm64: Dirty quota-based throttling of vcpus Date: Sun, 13 Nov 2022 17:05:10 +0000 Message-Id: <20221113170507.208810-4-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20221113170507.208810-1-shivam.kumar1@nutanix.com> References: <20221113170507.208810-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: BY3PR05CA0045.namprd05.prod.outlook.com (2603:10b6:a03:39b::20) To SA2PR02MB7564.namprd02.prod.outlook.com (2603:10b6:806:146::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PR02MB7564:EE_|BY5PR02MB6723:EE_ X-MS-Office365-Filtering-Correlation-Id: 3ebb8c07-ba81-4bdd-64f0-08dac59970f2 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: xQzxC6QHUVQDc2urN9KjM8qFx57JFUPmOU1Kr/d5+O2P8Dt/VbY9RI4IzQ8n1xl3TXSnS1bnOKA6aKL3yLjvkKMGw+Abdc6sOEEvgyJNOXRkoVpsfiUR9e6n7XSi2maaraxUQOTWrFZgvz2IfnzNxsn1ctXPMHOWcCgcr7r7hyK2pgiMp/JvOhcQ1c7o3T2x47dpaPcGvAzlkwcu39pTjbet1dbshp9NyFm4oTQhEveTEE17lTJsXsHpQlYVHVm0VcZfQuXD8bAPbfjrAViAm/9XvFd+yeZjTT7PmkS+uicLIJ3BeGsPnJkF8oEL63d/0mM5QeTVHIs3vVcyrE/t0GJNA4TCFJFnhxpaS085IljS4R1tjGnAE04T+vzL2AZBIDlNFUyJixtAwmdbT9jhD/2HnQd9rULffOkDn3GPUS3QKngEUgw1wtpVLLlnaS1RULy1CVJPpjhtSMSxr5DTSjH9HTkEFaTKQLj3/fUcTpD9p00njA0JcoHlXjfeGYJIAoOIJv1w59GpYGgLsXjlgSUDDuyBfmzCze8fWfxkHNUo5osqI0dGfLe0o/UP6TXTR75frmCTJjpdWh2EoElgxS8agAnN+wENI3vLPm872G+9KhlbfdtBhP5/4qvuFnbuhj8QER2xmDiKO0UTXwjoL1x1RlN8unit1FcWexCibxfs/uF73Vcq8zwMgYSbeP7nzqiwWe2soK/tMdcKaJz0D8/21J4SMdtOxPV0+g6CflPVuVSP6hwksLO8F7dXoq83o4PKVp+tXhhmtVlazQCUOv/MlX7LC3FJ2pfNV2bLIUw= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR02MB7564.namprd02.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(396003)(376002)(39840400004)(136003)(346002)(366004)(451199015)(186003)(2906002)(15650500001)(38100700002)(38350700002)(86362001)(83380400001)(6486002)(26005)(2616005)(6666004)(6512007)(107886003)(478600001)(6506007)(52116002)(41300700001)(4326008)(5660300002)(8676002)(66476007)(66556008)(66946007)(1076003)(8936002)(54906003)(316002)(36756003)(14143004);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dl39fvn9Ywb26Lv7D1cGUqIuRzCPx7WvzW1pfHZBfbGMh0Doq+s/OlcTG9P/N2Iz0TvGAMjKbhjyTjxVNxms7qEozrifMZpWrUsACK+uQS8Bj6XuFbyhVlAQFEvdBS/OlSKbCuSK+v6MVlZUUiS0aN0lFtlKg+CeD5hcRHiVqaDjdtlcyhfFKffjf9szuxizWxsx3jlu+NkTpSvUUc/yMEY7bPQQt0+SPZghxDS6AyQUZcufURmviE5gfIsmgFMe7gIauXMRRJrTjZEKfschUNqyk2s/FOMPW0QkJWGBrM5u2ONOwlFCX4B2S4yft1bv2Qu4rdDkORShJKpbdPuWD7xvSlc8WThAzs3VdfZv0UVd0xtx3ORlI0y+UUH6KSiCLN0QZahgP3XPjdO5E69KGukxwyxSpUg6Nx+P14uf1KyLBF9r8IzqvcPNLlKCxLB4h3lLzbOZc4YuV0WhfW3Og7vnNxRHD1mHpX34t67dmfgaQZRkw9RCNECnDy1YRUKN7yC4hw+IlI7q2rhMDs9xQvC7XI+Zii4imgJIBPUfGqurtD85VpGrGxx4oAju7IHF/ZtwPoIlfkg5cIsmNN/M+fzw5KVfNHLoKJtvs2aUc3XzGrv1RhCcjzRP77dK3/jDWnwzyuvnGl3mz4W8RfnEpighAclU1oqM7I0Ff43kGHR9hn8Vt4UK5lGHP6EHyRkLGA54Fhflcet8r/QAPiKbBEXmnovvIZ7Va8v9p3kkR95dzuVedkggbBWz6Ey9y42hHJAgTOBBczA6maeVeY5gu1KdEdUAPI+lYCaiC1l4wcM1Y3IzWk7MzJn0u718wdMHZybfC/aTJ5LbNUCxmGT7mtBR1Iq7L2OxUoAw8sNWv5Q0qomThsHDLbFmI8TyMAjTNNVtWcBs3EVYl1+w2yDqDBpamDyHMhKlwzlTl4kjYf2di95Nott3ZkOPQMZmPdnDLPsQ1o4PeYmT/uDYaPmcsFI6QlAa5szNP8XnE7KQnj7PR6cCPaNGj+PZcBrWzBjLUK5P37zySJ5DkhM7yrqJ2Kudk5CIsyshpVK5VgcgCUG9Eem/vnJUtV3/ZQ5HfKwNrl/8eiuADTQlo/qZFSbatrnroB8/hQw+zSOBS25YdVL6U0L5HG5VWyeHCfxbhvWqD6QaeKfAfGcQucGd3sKmrnvqX2TaKn23LliGJ7Hiz/1DZyCrCZYXulY7QDnajz/lFsmCo2mtNQsL4EMxFVTGuHHcR46leekPrLlyfJVB/K8XkM8U5OaTJijRi5EZ9MVAhhcXAazsOaYTDq5oZFbXfKwfw3/leiZNa9zizeeNWGMPA7qW4jXe2fC036hVkFcwZTWINsNDSinXxpp3WRHiDWtBwzrp/L8nNi71IbK+y67IUYdy+h9w/kry6CxWc2NfgXg+gRMaLEt/xcICsHFgCJjtZiFkTBNEczif8dFajV/iwsGu3ra7gjPVYBezoL9UDas9D/jc7JFv7fUPpgowKBAm3JCB79PopMqLgC9Q28e9iAOeoUFd9pP2NPpdckgCdzGB7Jj81vNV9uYe9+LcyCYojpGFE/Fnpv9dtnlvzwb+MxDTtjj8fjmCxH8VupLddK9gIxzZSw3MySpfEhW6pw== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3ebb8c07-ba81-4bdd-64f0-08dac59970f2 X-MS-Exchange-CrossTenant-AuthSource: SA2PR02MB7564.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Nov 2022 17:06:44.3666 (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: 4Y8wZKo79lseNVSqoOiTOTWrZfWs0Q79HgyMoqFYiUfEJWiLDrseM8qX4CvGgifimvWyxpQBbPk9VC5EoKeHivEb7r7w4+IkH3fywo+QVYk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR02MB6723 X-Proofpoint-GUID: RFSQwBq24r-TBrptwcpQB9VY8GfAEp2f X-Proofpoint-ORIG-GUID: RFSQwBq24r-TBrptwcpQB9VY8GfAEp2f X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-13_11,2022-11-11_01,2022-06-22_01 X-Proofpoint-Spam-Reason: safe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Exit to userspace whenever the dirty quota is exhausted (i.e. dirty count equals/exceeds dirty quota) to request more dirty quota. 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/arm.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 94d33e296e10..850024982dd9 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -746,6 +746,15 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_SUSPEND, vcpu)) return kvm_vcpu_suspend(vcpu); + + if (kvm_check_request(KVM_REQ_DIRTY_QUOTA_EXIT, vcpu)) { + struct kvm_run *run = vcpu->run; + + run->exit_reason = KVM_EXIT_DIRTY_QUOTA_EXHAUSTED; + run->dirty_quota_exit.count = vcpu->stat.generic.pages_dirtied; + run->dirty_quota_exit.quota = vcpu->dirty_quota; + return 0; + } } return 1; From patchwork Sun Nov 13 17:05:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shivam Kumar X-Patchwork-Id: 13041672 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 8C051C4332F for ; Sun, 13 Nov 2022 17:07:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235402AbiKMRHW (ORCPT ); Sun, 13 Nov 2022 12:07:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235317AbiKMRHV (ORCPT ); Sun, 13 Nov 2022 12:07:21 -0500 Received: from mx0b-002c1b01.pphosted.com (mx0b-002c1b01.pphosted.com [148.163.155.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66E4ADEE6 for ; Sun, 13 Nov 2022 09:07:20 -0800 (PST) Received: from pps.filterd (m0127841.ppops.net [127.0.0.1]) by mx0b-002c1b01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2ADH37BU023444; Sun, 13 Nov 2022 09:07:10 -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=Pvvx0nte5vYUH5z6zmOXZp5jM66q/QE4kRQOttYkTes=; b=oOfxqSnYhOOUDHOVPxLkOtJI244RgwS2F5HnUBQtZ+LEFYhBXlx+cSF5ZpKiTg/C9kdG eue3tXFrfRb5i8ow8PsD9oKgKGrCpBsOXL/6EzL2ld0Ur+XWvcYWn5148fRni5fjG/fL CZ4b3ubVDgL9dC0WMMMC8BO4OsEQ5oaa7hacTAhplWPmUI5Ft9lSl64/5rUCEpdJv2yx 8Ca4hkOqlcfL2SgqHnCF6citYpMJIpg2jAbg9e5EehcEYzNFYvwcjnwANa2mQNKoNsag RgB09i+/BEqxQrqMTHOvA8EYgSwZyLaYPu/BycAarRGAXTLZtRGrQ9a8x4b7fkLjlT2V pw== Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2170.outbound.protection.outlook.com [104.47.57.170]) by mx0b-002c1b01.pphosted.com (PPS) with ESMTPS id 3kt9wdt6tg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 13 Nov 2022 09:07:09 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZZCNsqSYbo7hiwcqTEvrf8e/R/6/6fyfP/ZkOEEFzxwJIQiZBpKk7AzRjfS4S5cIohlTNr3TjHtmgJOWkfeYpA7VKvufzGRGB0GqD5KDxIAecnGZ7Nrxj663m2JNh2ecEkd7oWXbi7YkZ/OwvRP0NyJ8HCjcMI/4u6MABhKPkVhz8NVsybt9PjGz+G/EA2X0cwjyyHGaCKYOn7Lb0bV2liygf00JUrK4y0RV7hFAo7r8IlATV3A6pbPhIwLl9a0OewwjyN2USs79BKHJLiG7Fw9Eu1RaBKTV5j7h6t/pC8OXhXs3z9N75OYotEH3J12X6CylK/qJuDK57ccZvKas4A== 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=Pvvx0nte5vYUH5z6zmOXZp5jM66q/QE4kRQOttYkTes=; b=G7ySgWE2brwp69b95dgw81hgQxYKNditCB/sJyRZ17Byb/qZ01gzncEsOg3JsQFwPTd4RfeE2B9DfDjSuOyZWVkyRGxgu5+/akwYrP0ASdgtLhLsZSi8vS7Od2P7IdnjluJ7xiMbkxoCmb3/LZuT1CtEyWNlxIn+BYB4mnQaR+dqJDLaRlZQpvgHc3etHTWlXgcvssDq7QJ8RgLz/xqvew95LI4qpX9k27+FTiyCu2Oy81EucX31p9AAKNYV+IPHAYM7yLRBGOQy3HjZB/ura0idlFepu2DWAlW5mYA+pN6aVXkk3FHq2QVS4BQVxxxYLF/nj3r1nLRGY48qPg0bcQ== 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=Pvvx0nte5vYUH5z6zmOXZp5jM66q/QE4kRQOttYkTes=; b=yjjFLONW08dQXLroCAoxNcLjlQRGt1v5ECjTLSFQ5gFpCWSpncS8r2bFQGgWaik06Dsw/OQwOa+P7uVqtC8V03cWv6BNRDId0Uz2KSrOjhyrgU9KDAUdwrImwC0NFjp6oQwuXfOJQVbghHh/oHmMpmmYOs2iQc7a3g/qFcUpTRyldJNcQGhMbhWcAj1Wm+NR4tSZjELb3+zdDZMI9h5++o1xwTjWKcL8FPG1VtmkYc7QO8MC6eoICDf6SbakeZ0Kex8lQiVks3t0KM1WOmTtcl+1eZcB/lk420uxcpjxaCP2Vs6eIV7cgN7+DGCplBPi890Ohvo29RgDALG0icxBqg== Received: from SA2PR02MB7564.namprd02.prod.outlook.com (2603:10b6:806:146::23) by BY5PR02MB6723.namprd02.prod.outlook.com (2603:10b6:a03:213::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.16; Sun, 13 Nov 2022 17:07:08 +0000 Received: from SA2PR02MB7564.namprd02.prod.outlook.com ([fe80::74fe:5557:d2d:1f5c]) by SA2PR02MB7564.namprd02.prod.outlook.com ([fe80::74fe:5557:d2d:1f5c%9]) with mapi id 15.20.5813.017; Sun, 13 Nov 2022 17:07:08 +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 Cc: kvm@vger.kernel.org, Shivam Kumar , Shaju Abraham , Manish Mishra , Anurag Madnawat Subject: [PATCH v7 4/4] KVM: selftests: Add selftests for dirty quota throttling Date: Sun, 13 Nov 2022 17:05:12 +0000 Message-Id: <20221113170507.208810-5-shivam.kumar1@nutanix.com> X-Mailer: git-send-email 2.22.3 In-Reply-To: <20221113170507.208810-1-shivam.kumar1@nutanix.com> References: <20221113170507.208810-1-shivam.kumar1@nutanix.com> X-ClientProxiedBy: BY3PR05CA0045.namprd05.prod.outlook.com (2603:10b6:a03:39b::20) To SA2PR02MB7564.namprd02.prod.outlook.com (2603:10b6:806:146::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PR02MB7564:EE_|BY5PR02MB6723:EE_ X-MS-Office365-Filtering-Correlation-Id: 562f6f75-fca4-48d7-978f-08dac5997f14 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: MLIFKy2jTk+a0wFkH5ZZ7MWtP7WGEeKCpIHcq0HQZRrxRVfuLs8kUX2G2wJsMPG5FqM622RnCeZGquGvAa7gSFrFgtTWxSaNB1VNzkm8mfXT44dmWKBOGEA6pgfzQh9OPpF33mptcn9bAQfgk05e5atAziucazpDOPMYLBxBt3eVxkZCTb9pxn9AddkVHihkRI8LPWrIk9SuYcqjkSdnGwoohtN8y/LUkPyztpU0jagxbkGdX6Xmh4r908xIJjA6BuMt7OyxyWhfnGmmTnakZSWybEON9V4qAgYZoi3SvcwNwKm4h2rvQDlNNi6Zih1kyBas1hD2n57SGdg1M38zIpdwCKfEHEpez9g1cO0Q8QnjRwTYKsVmPWJd+RhmxhUbnq3GY5qQo8drYBARCQ7HKzlHWXjczG3GTrfC4FwDo2wGgaFrN+oNzU1pkA91GMiSt3aH3+2kbjUy+9pm5FFvWXUB+SEBmNNVmPhPNERYA0upRNFkrGE4qhOCwRrM4vaTtE03cRm0HomXOd57qRhJXprJw9g7FwPSYS5sTTGmKE12UQo2GR7bw5m7Ha+xgOugpBL2BI7wsoBjKSieL0/qrt5v1s4ZK9kfu5tcLcQfhxGxlmKZOraKtnMGlxToeSCKVZsUak3+cem/Ng5gyAgjZJ6xRJMNZiCjC19izJDcxB2Bl6mFE2sx60Xw96YVO1qXVepUB9dl0A3DReDHIROoRadN6j7prq18Z9B/mpqkPesKnSmq+X/QOGOZoZqhsKfa1fGKjim1DE35eOZAGf9iS/NMRVHoGnyru+9R9t4NHkQ= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR02MB7564.namprd02.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(396003)(376002)(39840400004)(136003)(346002)(366004)(451199015)(186003)(2906002)(15650500001)(38100700002)(38350700002)(86362001)(83380400001)(45080400002)(6486002)(26005)(2616005)(6666004)(6512007)(107886003)(478600001)(6506007)(52116002)(41300700001)(4326008)(5660300002)(8676002)(66476007)(66556008)(66946007)(1076003)(8936002)(54906003)(316002)(36756003)(14143004);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9ZjpDx3ihK/J6nrcwyumZOFLOjhTAoNAU7GxdQaosur1f6UJDnVNaJKmswbr9CGNLvoi3UpkL0Is/cR4As9O6yNXMT4Gb9gEEl0ufdyQmePylsmwB//q7AlvxgUK/0tK4MKG+eAT1zHI+ZCL1u0HT/JfMhsRoFqIY6oZqiwXq/uQSpHvDTN0TSbBbxRsIuVoyayy5rLqpGgZaOR/yeBalL92A6y4d/eTPQrobbocYB6JkWFAeh3DtB+ikU5fqaQ94UtGvk7DpU332zVSepUkG5qQwN+ft23EHHwDUIRULL9Tufdwdt4vxsns859ocf/AcBmD9MJE+E2VoXfTc+6S8LU/WOCUzcskqub4ZbyvEwWqxHVwmqwoteNFS2xd+Y0QJo519LAgHA3MKgT/XX6s6rP/Kh8sQQkh89BILK4oxxtzDtDmwADurt+DlP3EbiWuW5NUXc/cHzN/RrZyy+fGCgYEOQDyC/ira6Tyu6z68Hab+Np7IXaegKJ9QX9Xb/6jwEBEThZkV5/RyP2UKahTOymi4GcKDqDojSY12doFEXhiAgt1UnOxNWtVRJBKklYB0TCKmY6N1h4ONlKyoW1HXrObQhbAiZ065nt6RqWWouuFXUADlZTsBpuYumhyehDu9jN4ZFdIRB09Ym0NTh2bo0yNo9L6InfsZoaWbr79kMt1mJ0RPl1SoD74oVm1FdwfQ58K7Yv+k6jt1AIP5iCJGBpMt0IfPZjLW8/y+8m6+Ox1eMaXHGOMYu1gkjWtVnuFDGOaLOn2z7pn/V8EjmgB4auizUow58FBoIQta3hKyhwEVahXlMA9CYvYkuR2AYPvre7aSQTH/xYMig2jJYAfLxMFm0PQyr+/RQARTTz/komTUaNxohrJ4KsutpQzZtyer2reKwYaNEkGf38kxH42RaEvayFjUlZ7CLxVuLzMrnoz0E478nIPF7LPLOYhzBPn9oVnFjpyr/Kw3FudukYSZ3TGf5OvD6t5KcZ1kINpz8ksjk9DMU/t2KF0tCpzMQE0PWU7dmgJ09RXs8hGIIArPpU0xyXqv7x1MOFA/IndBFqPcOJ6J+le5xCoLk8w1T9CQmiFOLpOiZqrl9Pmo/D0MPWc1DuNwG5M1/WcTdM7XaDCXGjjOeiuU6r1H2oqg81ELHyOmxrDC0DLf1W719+dTX2jijebhlDA9kfOuoZI4jM8zY8FVvB2Bqu86cN4r3x/0wiHZO2c01hyZCneLgf4dmcV0qTfIDL4VEXa2sfHPsdq2ou4McPUQuOzu/bR8jnYhXNcN/c9xGz4g4ymbE8qeI49y2Biyq+e3+1KB6a/Cvpi3IjqBmYK2HVp0G5Jqt/wup9rVVB8hqQXkvDuUiQZ8K3ZYjVzzMC/mNM+jY96QC776jUw7+le2nI9GSvNdLySNSphioAcEy1ETyJunsauj4xbioCtOOsQDhQugm3dS8fJJNwrZwinFLRaX+YrWcx+b1u6PS+oTyiTmc39uLsLT8cgHXGxKpBvqxBZaz2pQvRnD7fZOlNbp9GKswVB5oZI1DzUx3OSnUnWn7t+aba2ZoHqf7Z0D7pLOKu56fOyvsKsU3f+M78gOCLku1qFEud5nOsCoO8Kbry0+BHLl4WTrw== X-OriginatorOrg: nutanix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 562f6f75-fca4-48d7-978f-08dac5997f14 X-MS-Exchange-CrossTenant-AuthSource: SA2PR02MB7564.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Nov 2022 17:07:08.0320 (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: QXyaK3fSNwRr/cfpVJ9+GJqtyjSdzTToprQ935G8px2xsHbX/rzxIFG0898N441mORPW2OxX71PowqUBHUUOZk3UdWKi0lX3PAb68zwpxKY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR02MB6723 X-Proofpoint-GUID: W4SKAKVvTp4WjXnXnlakcpPWGUQNlV23 X-Proofpoint-ORIG-GUID: W4SKAKVvTp4WjXnXnlakcpPWGUQNlV23 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-13_11,2022-11-11_01,2022-06-22_01 X-Proofpoint-Spam-Reason: safe Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add selftests for dirty quota throttling with an optional -d parameter to configure by what value dirty quota should be incremented after each dirty quota exit. With very small intervals, a smaller value of dirty quota can ensure that the dirty quota exit code is tested. A zero value disables dirty quota throttling and thus dirty logging, without dirty quota throttling, can be tested. Suggested-by: Shaju Abraham Suggested-by: Manish Mishra Co-developed-by: Anurag Madnawat Signed-off-by: Anurag Madnawat Signed-off-by: Shivam Kumar --- tools/testing/selftests/kvm/dirty_log_test.c | 33 +++++++++++- .../selftests/kvm/include/kvm_util_base.h | 4 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 53 +++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_test.c b/tools/testing/selftests/kvm/dirty_log_test.c index b5234d6efbe1..a85ca6554d17 100644 --- a/tools/testing/selftests/kvm/dirty_log_test.c +++ b/tools/testing/selftests/kvm/dirty_log_test.c @@ -64,6 +64,8 @@ #define SIG_IPI SIGUSR1 +#define TEST_DIRTY_QUOTA_INCREMENT 8 + /* * Guest/Host shared variables. Ensure addr_gva2hva() and/or * sync_global_to/from_guest() are used when accessing from @@ -190,6 +192,7 @@ static enum log_mode_t host_log_mode_option = LOG_MODE_ALL; static enum log_mode_t host_log_mode; static pthread_t vcpu_thread; static uint32_t test_dirty_ring_count = TEST_DIRTY_RING_COUNT; +static uint64_t test_dirty_quota_increment = TEST_DIRTY_QUOTA_INCREMENT; static void vcpu_kick(void) { @@ -209,6 +212,13 @@ static void sem_wait_until(sem_t *sem) while (ret == -1 && errno == EINTR); } +static void set_dirty_quota(struct kvm_vm *vm, uint64_t dirty_quota) +{ + struct kvm_run *run = vcpu_state(vm, VCPU_ID); + + vcpu_set_dirty_quota(run, dirty_quota); +} + static bool clear_log_supported(void) { return kvm_has_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); @@ -256,7 +266,11 @@ static void default_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err) TEST_ASSERT(ret == 0 || (ret == -1 && err == EINTR), "vcpu run failed: errno=%d", err); - TEST_ASSERT(get_ucall(vcpu, NULL) == UCALL_SYNC, + if (test_dirty_quota_increment && + run->exit_reason == KVM_EXIT_DIRTY_QUOTA_EXHAUSTED) + vcpu_handle_dirty_quota_exit(run, test_dirty_quota_increment); + else + TEST_ASSERT(get_ucall(vcpu, NULL) == UCALL_SYNC, "Invalid guest sync status: exit_reason=%s\n", exit_reason_str(run->exit_reason)); @@ -374,6 +388,9 @@ static void dirty_ring_after_vcpu_run(struct kvm_vcpu *vcpu, int ret, int err) if (get_ucall(vcpu, NULL) == UCALL_SYNC) { /* We should allow this to continue */ ; + } else if (test_dirty_quota_increment && + run->exit_reason == KVM_EXIT_DIRTY_QUOTA_EXHAUSTED) { + vcpu_handle_dirty_quota_exit(run, test_dirty_quota_increment); } else if (run->exit_reason == KVM_EXIT_DIRTY_RING_FULL || (ret == -1 && err == EINTR)) { /* Update the flag first before pause */ @@ -764,6 +781,10 @@ static void run_test(enum vm_guest_mode mode, void *arg) sync_global_to_guest(vm, guest_test_virt_mem); sync_global_to_guest(vm, guest_num_pages); + /* Initialise dirty quota */ + if (test_dirty_quota_increment) + set_dirty_quota(vm, test_dirty_quota_increment); + /* Start the iterations */ iteration = 1; sync_global_to_guest(vm, iteration); @@ -805,6 +826,9 @@ static void run_test(enum vm_guest_mode mode, void *arg) /* Tell the vcpu thread to quit */ host_quit = true; log_mode_before_vcpu_join(); + /* Terminate dirty quota throttling */ + if (test_dirty_quota_increment) + set_dirty_quota(vm, 0); pthread_join(vcpu_thread, NULL); pr_info("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), " @@ -826,6 +850,8 @@ static void help(char *name) printf(" -c: specify dirty ring size, in number of entries\n"); printf(" (only useful for dirty-ring test; default: %"PRIu32")\n", TEST_DIRTY_RING_COUNT); + printf(" -q: specify incemental dirty quota (default: %"PRIu32")\n", + TEST_DIRTY_QUOTA_INCREMENT); printf(" -i: specify iteration counts (default: %"PRIu64")\n", TEST_HOST_LOOP_N); printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n", @@ -854,11 +880,14 @@ int main(int argc, char *argv[]) guest_modes_append_default(); - while ((opt = getopt(argc, argv, "c:hi:I:p:m:M:")) != -1) { + while ((opt = getopt(argc, argv, "c:q:hi:I:p:m:M:")) != -1) { switch (opt) { case 'c': test_dirty_ring_count = strtol(optarg, NULL, 10); break; + case 'q': + test_dirty_quota_increment = strtol(optarg, NULL, 10); + break; case 'i': p.iterations = strtol(optarg, NULL, 10); break; diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index e42a09cd24a0..d8eee61a9a7c 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -838,4 +838,8 @@ static inline int __vm_disable_nx_huge_pages(struct kvm_vm *vm) return __vm_enable_cap(vm, KVM_CAP_VM_DISABLE_NX_HUGE_PAGES, 0); } +void vcpu_set_dirty_quota(struct kvm_run *run, uint64_t dirty_quota); +void vcpu_handle_dirty_quota_exit(struct kvm_run *run, + uint64_t test_dirty_quota_increment); + #endif /* SELFTEST_KVM_UTIL_BASE_H */ diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index f1cb1627161f..2a60c7bdc778 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -18,6 +18,7 @@ #include #define KVM_UTIL_MIN_PFN 2 +#define PML_BUFFER_SIZE 512 static int vcpu_mmap_sz(void); @@ -1745,6 +1746,7 @@ static struct exit_reason { {KVM_EXIT_X86_RDMSR, "RDMSR"}, {KVM_EXIT_X86_WRMSR, "WRMSR"}, {KVM_EXIT_XEN, "XEN"}, + {KVM_EXIT_DIRTY_QUOTA_EXHAUSTED, "DIRTY_QUOTA_EXHAUSTED"}, #ifdef KVM_EXIT_MEMORY_NOT_PRESENT {KVM_EXIT_MEMORY_NOT_PRESENT, "MEMORY_NOT_PRESENT"}, #endif @@ -2021,3 +2023,54 @@ void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data, break; } } + +bool kvm_is_pml_enabled(void) +{ + return is_intel_cpu() && get_kvm_intel_param_bool("pml"); +} + +void vcpu_set_dirty_quota(struct kvm_run *run, uint64_t dirty_quota) +{ + run->dirty_quota = dirty_quota; + + if (dirty_quota) + pr_info("Dirty quota throttling enabled with initial quota %lu\n", + dirty_quota); + else + pr_info("Dirty quota throttling disabled\n"); +} + +void vcpu_handle_dirty_quota_exit(struct kvm_run *run, + uint64_t test_dirty_quota_increment) +{ + uint64_t quota = run->dirty_quota_exit.quota; + uint64_t count = run->dirty_quota_exit.count; + + /* + * Allow certain pages of overrun, KVM is allowed to dirty multiple + * pages before exiting to userspace, e.g. when emulating an + * instruction that performs multiple memory accesses. + */ + uint64_t buffer; + + /* + * When Intel's Page-Modification Logging (PML) is enabled, the CPU may + * dirty up to 512 pages (number of entries in the PML buffer) without + * exiting, thus KVM may effectively dirty that many pages before + * enforcing the dirty quota. + */ +#ifdef __x86_64__ + if (kvm_is_pml_enabled(void)) + buffer = PML_BUFFER_SIZE; +#endif + + TEST_ASSERT(count <= (quota + buffer), + "KVM dirtied too many pages: count=%lu, quota=%lu, buffer=%lu\n", + count, quota, buffer); + + TEST_ASSERT(count >= quota, + "Dirty quota exit happened with quota yet to be exhausted: count=%lu, quota=%lu\n", + count, quota); + + run->dirty_quota = count + test_dirty_quota_increment; +}