From patchwork Thu Jan 19 21:25:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Gardon X-Patchwork-Id: 45980 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp555588wrn; Thu, 19 Jan 2023 13:34:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXt6N+4+3ysTQVeb93eXnF4Ttyqlvqq2f4J3NaMrBVs7CeQTq2uZ0TdOlj8fxMHyRjJgG7dB X-Received: by 2002:a17:90a:a618:b0:229:989:4b30 with SMTP id c24-20020a17090aa61800b0022909894b30mr12805411pjq.46.1674164075774; Thu, 19 Jan 2023 13:34:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674164075; cv=none; d=google.com; s=arc-20160816; b=wTkqDutWBgqk28DYQdzCRKMbtBCAK76VC9gpkrJM+coYsEPSH/Xlra4cnvDfGzmXqW i3iNtaMSoO95bQMN4lEJZXzNKHKG0Uv6EPwLXPTbGi+FIBPEE6yDX/YITUhve512dREU eZezCRIsVom9U/yrcZDGIb+1PHoS3MLl6So7N7FK4aGFV7XLXBQRp1SGyAy3RLYQVnK2 qdnF7I4DZIOB1HBkFqwQdv5sxZL2nnQkZv6GFjr7oKo+fS2B9CTuUh338rFil82RgY22 b5EvLyG96enGzq5QVDLem1ALKBQ/mtVntoJbYgIBj/1law27VDxsSL6/fQq6Yrt89Tyo /1ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=0npHJgLgboBXgYnUwk1Sul4f7aUtJiI3ey0Plz8dops=; b=Lt20tq38KIKgSP8nyPQ4uhPHFpOITG+usRxAFcpWqTDkzwov7kD7ULH70WVijs+ANK 6GIM2khHhgl4eUSCmuMz50EFOGo+zRMNYujT3qwSHXfmo9CU4oZ/YkFgjfl7tgCFbBU7 kmlKthx71nOrLvZjwDSsd1MCnUquQkqYnXXw0D0OvshHAZUSssjcZUDp1St5qyJs9agv jnVtX8DpuJ6FlpF5tjKgOXnu1bZTvG/ItBrVAMlQ05eRxD8DqqhXEIFT1046xhrvqwIS 20hpLFDq4m2SZ45AVGhNSX0rFrc70zbp9s8U9nH0NEqJnC98d2IrSifMDSwcNndPtt2X +2Wg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kZDagZGn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e14-20020a17090a9a8e00b0022979c98557si399087pjp.0.2023.01.19.13.34.23; Thu, 19 Jan 2023 13:34:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=kZDagZGn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229761AbjASVdJ (ORCPT + 99 others); Thu, 19 Jan 2023 16:33:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231209AbjASVbO (ORCPT ); Thu, 19 Jan 2023 16:31:14 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C251BA3149 for ; Thu, 19 Jan 2023 13:25:37 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-4fb212e68b7so25417017b3.0 for ; Thu, 19 Jan 2023 13:25:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=0npHJgLgboBXgYnUwk1Sul4f7aUtJiI3ey0Plz8dops=; b=kZDagZGnKaXzrzxBVzFJZj64QHAOlJfMB9vNYLghv3lYw75JfWR6q8/c5+XrUEmC/u 4MtjXQ1vvfwSGQAggOKTadc215KUhxoFZNKtG0T68AvsZenUFfIcjGYxmj51Wr2xE6wa tdstsHXNzIbUitPwNAI3e2o7pADTCkQE78us9ug0ZY750WO7VN1DImF9lw8swbutToe9 7OiRAqydyyYULCZPo5SW2TvIx2VwIFnj/ZbzgK9YYEUglQNl2bUhCSTHf5r5v7eAsrlq srK1dboeA9im6XfbEYAHQAmIUZJpFuXJ6R/Re1AOyVeqNx3fn7gtTIb8nHO3+nSrdjrw qCIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0npHJgLgboBXgYnUwk1Sul4f7aUtJiI3ey0Plz8dops=; b=kszVU/qjO3ixDl1OHat2jyBrc0aozh7dcBRAs/s2FAI2AYVr79wRXJrlGGIBYj0St5 7r0SqdFvARJsS6aIHdS9uXVuYODhuxyVnpcPYryz93BFx9tLs8g3NUzBkvzZEqknm/kW lJjl4wxyOwUQIJcYghtnBPkX8CBZ1Wv/2bWEAqjB6jIJe1iGPbxbzrfME2GamWSVI/M2 YjN/W98G9yrCsIyQTHghnRNVggHCvgnusl3BkJSakuXAjYoCPFdWd5K5DKeznnnAdJou XNRSPdGgUsroM+gDp/l1Llpeqjf3f7X8gsxCFvzYfrlJFYn7tjCwLOLdfhwdn0mOse6Y 46TA== X-Gm-Message-State: AFqh2kpAtvB0Ljgj0MzN5XkFOhtJMUvqA9HzAHCo/KBqgVx7kf5fmIdL MQlK3cz4V6yBqp9eibc05lOtEKtRDH2UjU+jxRRedRo2V6moqsFilhEOlCqayxBTgziDL0I+3Ov xOO7LYcdIXsaYUyD4kD04X9rwSJ+rlqV9FK+XCSldm606SWOeFewpnuCL1JWtGzy7maVal7mB X-Received: from sweer.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:e45]) (user=bgardon job=sendgmr) by 2002:a81:78cc:0:b0:364:2f5d:5eb6 with SMTP id t195-20020a8178cc000000b003642f5d5eb6mr1962468ywc.215.1674163515044; Thu, 19 Jan 2023 13:25:15 -0800 (PST) Date: Thu, 19 Jan 2023 21:25:09 +0000 In-Reply-To: <20230119212510.3938454-1-bgardon@google.com> Mime-Version: 1.0 References: <20230119212510.3938454-1-bgardon@google.com> X-Mailer: git-send-email 2.39.1.405.gd4c25cc71f-goog Message-ID: <20230119212510.3938454-2-bgardon@google.com> Subject: [PATCH 1/2] selftests: KVM: Move dirty logging functions to memstress.(c|h) From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , David Matlack , Vipin Sharma , Ricardo Koller , Ben Gardon X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755488269866503975?= X-GMAIL-MSGID: =?utf-8?q?1755488269866503975?= Move some helper functions from dirty_log_perf_test.c to the memstress library so that they can be used in a future commit which tests page splitting during dirty logging. Signed-off-by: Ben Gardon --- .../selftests/kvm/dirty_log_perf_test.c | 84 ++----------------- .../testing/selftests/kvm/include/memstress.h | 8 ++ tools/testing/selftests/kvm/lib/memstress.c | 72 ++++++++++++++++ 3 files changed, 87 insertions(+), 77 deletions(-) diff --git a/tools/testing/selftests/kvm/dirty_log_perf_test.c b/tools/testing/selftests/kvm/dirty_log_perf_test.c index e9d6d1aecf89c..416719e205183 100644 --- a/tools/testing/selftests/kvm/dirty_log_perf_test.c +++ b/tools/testing/selftests/kvm/dirty_log_perf_test.c @@ -136,77 +136,6 @@ struct test_params { bool random_access; }; -static void toggle_dirty_logging(struct kvm_vm *vm, int slots, bool enable) -{ - int i; - - for (i = 0; i < slots; i++) { - int slot = MEMSTRESS_MEM_SLOT_INDEX + i; - int flags = enable ? KVM_MEM_LOG_DIRTY_PAGES : 0; - - vm_mem_region_set_flags(vm, slot, flags); - } -} - -static inline void enable_dirty_logging(struct kvm_vm *vm, int slots) -{ - toggle_dirty_logging(vm, slots, true); -} - -static inline void disable_dirty_logging(struct kvm_vm *vm, int slots) -{ - toggle_dirty_logging(vm, slots, false); -} - -static void get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots) -{ - int i; - - for (i = 0; i < slots; i++) { - int slot = MEMSTRESS_MEM_SLOT_INDEX + i; - - kvm_vm_get_dirty_log(vm, slot, bitmaps[i]); - } -} - -static void clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], - int slots, uint64_t pages_per_slot) -{ - int i; - - for (i = 0; i < slots; i++) { - int slot = MEMSTRESS_MEM_SLOT_INDEX + i; - - kvm_vm_clear_dirty_log(vm, slot, bitmaps[i], 0, pages_per_slot); - } -} - -static unsigned long **alloc_bitmaps(int slots, uint64_t pages_per_slot) -{ - unsigned long **bitmaps; - int i; - - bitmaps = malloc(slots * sizeof(bitmaps[0])); - TEST_ASSERT(bitmaps, "Failed to allocate bitmaps array."); - - for (i = 0; i < slots; i++) { - bitmaps[i] = bitmap_zalloc(pages_per_slot); - TEST_ASSERT(bitmaps[i], "Failed to allocate slot bitmap."); - } - - return bitmaps; -} - -static void free_bitmaps(unsigned long *bitmaps[], int slots) -{ - int i; - - for (i = 0; i < slots; i++) - free(bitmaps[i]); - - free(bitmaps); -} - static void run_test(enum vm_guest_mode mode, void *arg) { struct test_params *p = arg; @@ -236,7 +165,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) host_num_pages = vm_num_host_pages(mode, guest_num_pages); pages_per_slot = host_num_pages / p->slots; - bitmaps = alloc_bitmaps(p->slots, pages_per_slot); + bitmaps = memstress_alloc_bitmaps(p->slots, pages_per_slot); if (dirty_log_manual_caps) vm_enable_cap(vm, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2, @@ -277,7 +206,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) /* Enable dirty logging */ clock_gettime(CLOCK_MONOTONIC, &start); - enable_dirty_logging(vm, p->slots); + memstress_enable_dirty_logging(vm, p->slots); ts_diff = timespec_elapsed(start); pr_info("Enabling dirty logging time: %ld.%.9lds\n\n", ts_diff.tv_sec, ts_diff.tv_nsec); @@ -306,7 +235,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) iteration, ts_diff.tv_sec, ts_diff.tv_nsec); clock_gettime(CLOCK_MONOTONIC, &start); - get_dirty_log(vm, bitmaps, p->slots); + memstress_get_dirty_log(vm, bitmaps, p->slots); ts_diff = timespec_elapsed(start); get_dirty_log_total = timespec_add(get_dirty_log_total, ts_diff); @@ -315,7 +244,8 @@ static void run_test(enum vm_guest_mode mode, void *arg) if (dirty_log_manual_caps) { clock_gettime(CLOCK_MONOTONIC, &start); - clear_dirty_log(vm, bitmaps, p->slots, pages_per_slot); + memstress_clear_dirty_log(vm, bitmaps, p->slots, + pages_per_slot); ts_diff = timespec_elapsed(start); clear_dirty_log_total = timespec_add(clear_dirty_log_total, ts_diff); @@ -334,7 +264,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) /* Disable dirty logging */ clock_gettime(CLOCK_MONOTONIC, &start); - disable_dirty_logging(vm, p->slots); + memstress_disable_dirty_logging(vm, p->slots); ts_diff = timespec_elapsed(start); pr_info("Disabling dirty logging time: %ld.%.9lds\n", ts_diff.tv_sec, ts_diff.tv_nsec); @@ -359,7 +289,7 @@ static void run_test(enum vm_guest_mode mode, void *arg) clear_dirty_log_total.tv_nsec, avg.tv_sec, avg.tv_nsec); } - free_bitmaps(bitmaps, p->slots); + memstress_free_bitmaps(bitmaps, p->slots); arch_cleanup_vm(vm); memstress_destroy_vm(vm); } diff --git a/tools/testing/selftests/kvm/include/memstress.h b/tools/testing/selftests/kvm/include/memstress.h index 72e3e358ef7bd..ce4e603050eaa 100644 --- a/tools/testing/selftests/kvm/include/memstress.h +++ b/tools/testing/selftests/kvm/include/memstress.h @@ -72,4 +72,12 @@ void memstress_guest_code(uint32_t vcpu_id); uint64_t memstress_nested_pages(int nr_vcpus); void memstress_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu *vcpus[]); +void memstress_enable_dirty_logging(struct kvm_vm *vm, int slots); +void memstress_disable_dirty_logging(struct kvm_vm *vm, int slots); +void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots); +void memstress_clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], + int slots, uint64_t pages_per_slot); +unsigned long **memstress_alloc_bitmaps(int slots, uint64_t pages_per_slot); +void memstress_free_bitmaps(unsigned long *bitmaps[], int slots); + #endif /* SELFTEST_KVM_MEMSTRESS_H */ diff --git a/tools/testing/selftests/kvm/lib/memstress.c b/tools/testing/selftests/kvm/lib/memstress.c index 5f1d3173c238c..3632956c6bcf5 100644 --- a/tools/testing/selftests/kvm/lib/memstress.c +++ b/tools/testing/selftests/kvm/lib/memstress.c @@ -5,6 +5,7 @@ #define _GNU_SOURCE #include +#include #include "kvm_util.h" #include "memstress.h" @@ -320,3 +321,74 @@ void memstress_join_vcpu_threads(int nr_vcpus) for (i = 0; i < nr_vcpus; i++) pthread_join(vcpu_threads[i].thread, NULL); } + +static void toggle_dirty_logging(struct kvm_vm *vm, int slots, bool enable) +{ + int i; + + for (i = 0; i < slots; i++) { + int slot = MEMSTRESS_MEM_SLOT_INDEX + i; + int flags = enable ? KVM_MEM_LOG_DIRTY_PAGES : 0; + + vm_mem_region_set_flags(vm, slot, flags); + } +} + +void memstress_enable_dirty_logging(struct kvm_vm *vm, int slots) +{ + toggle_dirty_logging(vm, slots, true); +} + +void memstress_disable_dirty_logging(struct kvm_vm *vm, int slots) +{ + toggle_dirty_logging(vm, slots, false); +} + +void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots) +{ + int i; + + for (i = 0; i < slots; i++) { + int slot = MEMSTRESS_MEM_SLOT_INDEX + i; + + kvm_vm_get_dirty_log(vm, slot, bitmaps[i]); + } +} + +void memstress_clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], + int slots, uint64_t pages_per_slot) +{ + int i; + + for (i = 0; i < slots; i++) { + int slot = MEMSTRESS_MEM_SLOT_INDEX + i; + + kvm_vm_clear_dirty_log(vm, slot, bitmaps[i], 0, pages_per_slot); + } +} + +unsigned long **memstress_alloc_bitmaps(int slots, uint64_t pages_per_slot) +{ + unsigned long **bitmaps; + int i; + + bitmaps = malloc(slots * sizeof(bitmaps[0])); + TEST_ASSERT(bitmaps, "Failed to allocate bitmaps array."); + + for (i = 0; i < slots; i++) { + bitmaps[i] = bitmap_zalloc(pages_per_slot); + TEST_ASSERT(bitmaps[i], "Failed to allocate slot bitmap."); + } + + return bitmaps; +} + +void memstress_free_bitmaps(unsigned long *bitmaps[], int slots) +{ + int i; + + for (i = 0; i < slots; i++) + free(bitmaps[i]); + + free(bitmaps); +} From patchwork Thu Jan 19 21:25:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Gardon X-Patchwork-Id: 45986 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp555774wrn; Thu, 19 Jan 2023 13:35:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXsmt0QLCzQCQby5p6NagWeUYbIJJiGLVj2HsAdA7H7UHeNB1UHm+LFb/mMMMpPQS+FRJzW4 X-Received: by 2002:a17:902:e548:b0:194:687c:e883 with SMTP id n8-20020a170902e54800b00194687ce883mr17951099plf.37.1674164107032; Thu, 19 Jan 2023 13:35:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674164107; cv=none; d=google.com; s=arc-20160816; b=MXceb8iTOJOFUBDRIS6P77tWTmjMqyeZYkf4Uz3pXJJmK7fakaX8ZUPBqfuVMdO8nv V+6ovHzURKMvB9fi+xTgglWZkrSRRsoc7Vm58aVX0/cWRldVehxH2DWMxXVkfCSkus7U ZbXgk88lBzyPsFWrl4pZo1ioF6eAwRFZp1kpleMupPXRQx7KJ4R5ObZOEQrMbWHMVpfg CBU/oW/TnXANDas2A5iNzCyoQ7nVgdEthFAVzweMCAaRjg4ZTMVs3f4ppoWKiLYm2nqZ q+1CLDv8jxPr03XG4m5y6oJpnqsH6aura0CJtFEueTywYfyh3lPgi+rIS++imh5ccloZ pRiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=DYdrgSlWXft+Tsg8pN9NCKsz/7KXm3iGPcuIiufq1QE=; b=f2R5VHV9wFTDllWw1H/E7YyFUWB4yDQ2blGaeUSrelmh1iu3wrGG/AsF2s4SPptk9f 9bIlMBMJPsVPlDyP0FYJ7Tb2ZHYHtIpA3BDYT19PA+3E2P93Ai7i1l5sPLCeqd4u1lWX OSdoLMzkf+u4wmf6LIySDJGP79Dsantsyke92GcRhMsGLWOng33iFp8gPDhOuHz4OkhE 86txlbPZ1Z/kHn1C9PHpk66j9jmW3PZw91zzpsbCD6XZww69m5AZMblXcoCzC6b1+0Is 7FiwQnEjCdUfUZEdbGCafZDqNJp6cLWMLa34bna3JEwPorGjTrzgFrLs7tfzGe5k+Voi QZfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=nk1Zy3yz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o9-20020a170902778900b00194516b2d94si15034427pll.531.2023.01.19.13.34.53; Thu, 19 Jan 2023 13:35:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=nk1Zy3yz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230287AbjASVdk (ORCPT + 99 others); Thu, 19 Jan 2023 16:33:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231220AbjASVbQ (ORCPT ); Thu, 19 Jan 2023 16:31:16 -0500 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 375894F870 for ; Thu, 19 Jan 2023 13:25:40 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id g31-20020a63111f000000b004bbc748ca63so1579780pgl.3 for ; Thu, 19 Jan 2023 13:25:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DYdrgSlWXft+Tsg8pN9NCKsz/7KXm3iGPcuIiufq1QE=; b=nk1Zy3yzFTo1ZuadOsMQ3wFIgaAuRxHBUCx3Rkat+n0jxtXXWLDNvHKZjQuu6i5OV7 LQjD6BexQrBCShQxxASv2roEFp5cwR4EBbFRI2o+8Ur2Ekjq/3EjJ8oTpl/eMbO4Xkn+ UtFolePP25hJMgesm5DKINUW4dNif2cdf+Bhc08NdJrQgkwnpDzYP8It2F/XrSapNYVN G16/JUOGu2H0jt23wnAay4s9YfZ13waZK/5xEJwTgdhv2FU3jcrP5/+zk7KAWqan74my G4SPmQaa2/dU0sZ+0gWUEoWtW+gPgMOB/XSM0t/w6r4VYn256yV9ZByLzLrWYlyIedZd CIvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DYdrgSlWXft+Tsg8pN9NCKsz/7KXm3iGPcuIiufq1QE=; b=2YC+0Mo+xEAcZCujJHYGdgX8fxfW7w5t0lLL3Ts5AseIieEUGvbEDBDaKACV4uGt+U BsbMFppDQJYIE0RMcQIpNwSrIDqcG1JM7iN6FjPtgFpkZRDxaAp/EWX2bUyt2xWPin+I GWaPn3SYZkou6GZQmahWhvRupq1C9hMKQKeKEcxoaBFq0CiW/Z0/yktoIOuo2qtVoQoE sUhgG9C2j/s3MoxQEa0kozso11uULF6+CsrWAbphVLqhC8NVD0CMAVdOS7VeIOJyv7qN J8FeBiPKfcTEw7+c/slFm4R1ecGfF02csUK4oLbLINh3c682F139h2725laZtfL9sLUr I1yw== X-Gm-Message-State: AFqh2kpyJW+J/h6/IyA/sg1Rfh0YJaX0RljT8kCEv8WbzZROeQ0XsueB sCVtpUfHIOugXJvWuZedtEBVhCZXTUpMoOATV+KUbDE70iti+Jsb7Vu8FLm6yTXpjLdNzbecMfJ udrNqzKZDezPtg5kELwSsqLnKJKkS+PUGEU8RDyE9icv14uZ4d6w2w8Gysa/VPI8xsQpqWpEM X-Received: from sweer.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:e45]) (user=bgardon job=sendgmr) by 2002:a62:18d5:0:b0:578:3528:b4d with SMTP id 204-20020a6218d5000000b0057835280b4dmr1081889pfy.80.1674163516710; Thu, 19 Jan 2023 13:25:16 -0800 (PST) Date: Thu, 19 Jan 2023 21:25:10 +0000 In-Reply-To: <20230119212510.3938454-1-bgardon@google.com> Mime-Version: 1.0 References: <20230119212510.3938454-1-bgardon@google.com> X-Mailer: git-send-email 2.39.1.405.gd4c25cc71f-goog Message-ID: <20230119212510.3938454-3-bgardon@google.com> Subject: [PATCH 2/2] selftests: KVM: Add page splitting test From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , David Matlack , Vipin Sharma , Ricardo Koller , Ben Gardon X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755488302747078903?= X-GMAIL-MSGID: =?utf-8?q?1755488302747078903?= Add a test for page splitting during dirty logging and for hugepage recovery after dirty logging. Page splitting represents non-trivial behavior, which is complicated by MANUAL_PROTECT mode, which causes pages to be split on the first clear, instead of when dirty logging is enabled. Add a test which makes asserions about page counts to help define the expected behavior of page splitting and to provid needed coverage of the behavior. This also helps ensure that a failure in eager page splitting is not covered up by splitting in the vCPU path. Tested by running the test on an Intel Haswell machine w/wo MANUAL_PROTECT. Signed-off-by: Ben Gardon --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/kvm_util_base.h | 2 + tools/testing/selftests/kvm/lib/kvm_util.c | 5 + .../kvm/x86_64/page_splitting_test.c | 314 ++++++++++++++++++ 4 files changed, 322 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/page_splitting_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 1750f91dd9362..057ebd709e77a 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -76,6 +76,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test TEST_GEN_PROGS_x86_64 += x86_64/monitor_mwait_test TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test +TEST_GEN_PROGS_x86_64 += x86_64/page_splitting_test TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index fbc2a79369b8b..4e38a771fa5d1 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -213,6 +213,7 @@ extern const struct vm_guest_mode_params vm_guest_mode_params[]; int open_path_or_exit(const char *path, int flags); int open_kvm_dev_path_or_exit(void); +bool get_kvm_param_bool(const char *param); bool get_kvm_intel_param_bool(const char *param); bool get_kvm_amd_param_bool(const char *param); @@ -402,6 +403,7 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) uint64_t data; __vm_get_stat(vm, stat_name, &data, 1); + return data; } diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 56d5ea949cbbe..fa6d69f731990 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -80,6 +80,11 @@ static bool get_module_param_bool(const char *module_name, const char *param) TEST_FAIL("Unrecognized value '%c' for boolean module param", value); } +bool get_kvm_param_bool(const char *param) +{ + return get_module_param_bool("kvm", param); +} + bool get_kvm_intel_param_bool(const char *param) { return get_module_param_bool("kvm_intel", param); diff --git a/tools/testing/selftests/kvm/x86_64/page_splitting_test.c b/tools/testing/selftests/kvm/x86_64/page_splitting_test.c new file mode 100644 index 0000000000000..3e2ee20adf036 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/page_splitting_test.c @@ -0,0 +1,314 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KVM dirty logging page splitting test + * + * Based on dirty_log_perf.c + * + * Copyright (C) 2018, Red Hat, Inc. + * Copyright (C) 2020, Google, Inc. + */ + +#include +#include +#include +#include +#include + +#include "kvm_util.h" +#include "test_util.h" +#include "memstress.h" +#include "guest_modes.h" + +/* How many host loops to run by default (one KVM_GET_DIRTY_LOG for each loop)*/ +#define TEST_HOST_LOOP_N 2UL + +static int NR_VCPUS = 2; +static int NR_SLOTS = 2; +static int NR_ITERATIONS = 2; +static uint64_t guest_percpu_mem_size = DEFAULT_PER_VCPU_MEM_SIZE; + +/* Host variables */ +static u64 dirty_log_manual_caps; +static bool host_quit; +static int iteration; +static int vcpu_last_completed_iteration[KVM_MAX_VCPUS]; + +struct kvm_page_stats { + uint64_t pages_4k; + uint64_t pages_2m; + uint64_t pages_1g; + uint64_t hugepages; +}; + +static void get_page_stats(struct kvm_vm *vm, struct kvm_page_stats *stats) +{ + stats->pages_4k = vm_get_stat(vm, "pages_4k"); + stats->pages_2m = vm_get_stat(vm, "pages_2m"); + stats->pages_1g = vm_get_stat(vm, "pages_1g"); + stats->hugepages = stats->pages_2m + stats->pages_1g; + + pr_debug("Page stats - 4K: %ld 2M: %ld 1G: %ld huge: %ld\n", + stats->pages_4k, stats->pages_2m, stats->pages_1g, + stats->hugepages); +} + +static void vcpu_worker(struct memstress_vcpu_args *vcpu_args) +{ + struct kvm_vcpu *vcpu = vcpu_args->vcpu; + int vcpu_idx = vcpu_args->vcpu_idx; + uint64_t pages_count = 0; + struct kvm_run *run; + int ret; + + run = vcpu->run; + + while (!READ_ONCE(host_quit)) { + int current_iteration = READ_ONCE(iteration); + + ret = _vcpu_run(vcpu); + + TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret); + TEST_ASSERT(get_ucall(vcpu, NULL) == UCALL_SYNC, + "Invalid guest sync status: exit_reason=%s\n", + exit_reason_str(run->exit_reason)); + + vcpu_last_completed_iteration[vcpu_idx] = current_iteration; + + if (current_iteration) + pages_count += vcpu_args->pages; + + /* Wait for the start of the next iteration to be signaled. */ + while (current_iteration == READ_ONCE(iteration) && + READ_ONCE(iteration) >= 0 && + !READ_ONCE(host_quit)) + ; + } +} + +struct test_params { + enum vm_mem_backing_src_type backing_src; +}; + +static void run_test(struct test_params *p) +{ + struct kvm_vm *vm; + unsigned long **bitmaps; + uint64_t guest_num_pages; + uint64_t host_num_pages; + uint64_t pages_per_slot; + int i; + uint64_t total_4k_pages; + struct kvm_page_stats stats_populated; + struct kvm_page_stats stats_dirty_logging_enabled; + struct kvm_page_stats stats_dirty_pass[NR_ITERATIONS]; + struct kvm_page_stats stats_clear_pass[NR_ITERATIONS]; + struct kvm_page_stats stats_dirty_logging_disabled; + struct kvm_page_stats stats_repopulated; + + vm = memstress_create_vm(VM_MODE_DEFAULT, NR_VCPUS, guest_percpu_mem_size, + NR_SLOTS, p->backing_src, false); + + guest_num_pages = (NR_VCPUS * guest_percpu_mem_size) >> vm->page_shift; + guest_num_pages = vm_adjust_num_guest_pages(VM_MODE_DEFAULT, guest_num_pages); + host_num_pages = vm_num_host_pages(VM_MODE_DEFAULT, guest_num_pages); + pages_per_slot = host_num_pages / NR_SLOTS; + + bitmaps = memstress_alloc_bitmaps(NR_SLOTS, pages_per_slot); + + if (dirty_log_manual_caps) + vm_enable_cap(vm, KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2, + dirty_log_manual_caps); + + /* Start the iterations */ + iteration = 0; + host_quit = false; + + for (i = 0; i < NR_VCPUS; i++) + vcpu_last_completed_iteration[i] = -1; + + memstress_start_vcpu_threads(NR_VCPUS, vcpu_worker); + + /* Allow the vCPUs to populate memory */ + for (i = 0; i < NR_VCPUS; i++) { + while (READ_ONCE(vcpu_last_completed_iteration[i]) != + iteration) + ; + } + + pr_debug("\nGetting stats after populating memory:\n"); + get_page_stats(vm, &stats_populated); + + /* Enable dirty logging */ + memstress_enable_dirty_logging(vm, NR_SLOTS); + + pr_debug("\nGetting stats after enabling dirty logging:\n"); + get_page_stats(vm, &stats_dirty_logging_enabled); + + while (iteration < NR_ITERATIONS) { + /* + * Incrementing the iteration number will start the vCPUs + * dirtying memory again. + */ + iteration++; + + for (i = 0; i < NR_VCPUS; i++) { + while (READ_ONCE(vcpu_last_completed_iteration[i]) + != iteration) + ; + } + + pr_debug("\nGetting stats after dirtying memory on pass %d:\n", iteration); + get_page_stats(vm, &stats_dirty_pass[iteration - 1]); + + memstress_get_dirty_log(vm, bitmaps, NR_SLOTS); + + if (dirty_log_manual_caps) { + memstress_clear_dirty_log(vm, bitmaps, NR_SLOTS, pages_per_slot); + + pr_debug("\nGetting stats after clearing dirty log pass %d:\n", iteration); + get_page_stats(vm, &stats_clear_pass[iteration - 1]); + } + } + + /* Disable dirty logging */ + memstress_disable_dirty_logging(vm, NR_SLOTS); + + pr_debug("\nGetting stats after disabling dirty logging:\n"); + get_page_stats(vm, &stats_dirty_logging_disabled); + + /* Run vCPUs again to fault pages back in. */ + iteration++; + for (i = 0; i < NR_VCPUS; i++) { + while (READ_ONCE(vcpu_last_completed_iteration[i]) != iteration) + ; + } + + pr_debug("\nGetting stats after repopulating memory:\n"); + get_page_stats(vm, &stats_repopulated); + + /* + * Tell the vCPU threads to quit. No need to manually check that vCPUs + * have stopped running after disabling dirty logging, the join will + * wait for them to exit. + */ + host_quit = true; + memstress_join_vcpu_threads(NR_VCPUS); + + memstress_free_bitmaps(bitmaps, NR_SLOTS); + memstress_destroy_vm(vm); + + /* Make assertions about the page counts. */ + total_4k_pages = stats_populated.pages_4k; + total_4k_pages += stats_populated.pages_2m * 512; + total_4k_pages += stats_populated.pages_1g * 512 * 512; + + /* + * Check that all huge pages were split. Since large pages can only + * exist in the data slot, and the vCPUs should have dirtied all pages + * in the data slot, there should be no huge pages left after splitting. + * Splitting happens at dirty log enable time without + * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 and after the first clear pass + * with that capability. + */ + if (dirty_log_manual_caps) { + TEST_ASSERT(stats_clear_pass[0].hugepages == 0, + "Unexpected huge page count after splitting. Expected 0, got %ld", + stats_clear_pass[0].hugepages); + TEST_ASSERT(stats_clear_pass[0].pages_4k == total_4k_pages, + "All memory should be mapped at 4k. Expected %ld 4k pages, got %ld", + total_4k_pages, stats_clear_pass[0].pages_4k); + } else { + TEST_ASSERT(stats_dirty_logging_enabled.hugepages == 0, + "Unexpected huge page count after splitting. Expected 0, got %ld", + stats_dirty_logging_enabled.hugepages); + TEST_ASSERT(stats_dirty_logging_enabled.pages_4k == total_4k_pages, + "All memory should be mapped at 4k. Expected %ld 4k pages, got %ld", + total_4k_pages, stats_dirty_logging_enabled.pages_4k); + } + + /* + * Once dirty logging is disabled and the vCPUs have touched all their + * memory again, the page counts should be the same as they were + * right after initial population of memory. + */ + TEST_ASSERT(stats_populated.pages_4k == stats_repopulated.pages_4k, + "4k page count did not return to its initial value after " + "dirty logging. Expected %ld, got %ld", + stats_populated.pages_4k, stats_repopulated.pages_4k); + TEST_ASSERT(stats_populated.pages_2m == stats_repopulated.pages_2m, + "2m page count did not return to its initial value after " + "dirty logging. Expected %ld, got %ld", + stats_populated.pages_2m, stats_repopulated.pages_2m); + TEST_ASSERT(stats_populated.pages_1g == stats_repopulated.pages_1g, + "1g page count did not return to its initial value after " + "dirty logging. Expected %ld, got %ld", + stats_populated.pages_1g, stats_repopulated.pages_1g); +} + +static void help(char *name) +{ + puts(""); + printf("usage: %s [-h] [-g] [-m mode] [-b vcpu bytes] [-s mem type]\n", + name); + puts(""); + printf(" -g: Do not enable KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2. This\n" + " makes KVM_GET_DIRTY_LOG clear the dirty log (i.e.\n" + " KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE is not enabled)\n" + " and writes will be tracked as soon as dirty logging is\n" + " enabled on the memslot (i.e. KVM_DIRTY_LOG_INITIALLY_SET\n" + " is not enabled).\n"); + printf(" -b: specify the size of the memory region which should be\n" + " dirtied by each vCPU. e.g. 10M or 3G.\n" + " (default: 1G)\n"); + backing_src_help("-s"); + puts(""); + exit(0); +} + +int main(int argc, char *argv[]) +{ + struct test_params p = { + .backing_src = VM_MEM_SRC_ANONYMOUS_HUGETLB, + }; + int opt; + + TEST_REQUIRE(get_kvm_param_bool("eager_page_split")); + TEST_REQUIRE(get_kvm_param_bool("tdp_mmu")); + + dirty_log_manual_caps = + kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2); + dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | + KVM_DIRTY_LOG_INITIALLY_SET); + + guest_modes_append_default(); + + while ((opt = getopt(argc, argv, "b:ghs:")) != -1) { + switch (opt) { + case 'b': + guest_percpu_mem_size = parse_size(optarg); + break; + case 'g': + dirty_log_manual_caps = 0; + break; + case 'h': + help(argv[0]); + break; + case 's': + p.backing_src = parse_backing_src_type(optarg); + break; + default: + help(argv[0]); + break; + } + } + + if (!is_backing_src_hugetlb(p.backing_src)) { + pr_info("This test will only work reliably with HugeTLB memory. " + "It can work with THP, but that is best effort."); + return KSFT_SKIP; + } + + run_test(&p); + + return 0; +}