Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp831139lfv; Tue, 12 Apr 2022 06:29:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwsSg02fUcI8O82Dyg0fM2Dsv6d07TEKZR659F+g1YLYgTIB+H7KriD4Hb4I6qJGwfH2Oel X-Received: by 2002:a05:6870:b3a8:b0:d9:a7d5:f00d with SMTP id w40-20020a056870b3a800b000d9a7d5f00dmr2009982oap.38.1649770148392; Tue, 12 Apr 2022 06:29:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649770148; cv=none; d=google.com; s=arc-20160816; b=VW1HLk8LMJqsVWPgHv+ntf5kFPxP3nKXEJjixTaE2aRoWWYIuQpBGUKss62y8/z2G9 BnnPxaXy8aTO+wGSAcnOdbCdlvWi4sGrTaLBNAD/Ovd6Q6K+xLWz0v5byvP8I3tjk15T JXd9LR7uI1kSkP388D7lDaGKfSaAr4/5VVewi8YyaPlnzge8719LTrZPY8eOd4xaIKFT 0L4MPf/z5iR0WVtaItf/IZgwX1pP8wKCRewBagKxjynzeWjOwDKfj89ERc47kTNI9X2t V7aYadLsnsZjjFF9HmxwzOHMK0HpHx08qlbQBbrdXLOEBJCZZ7uDnh7uV4fttHgzeBgS X0qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=ZNp2x4iSYzfok2hpXbrlRX9Pkk/fC9iE5eZ1/zNFkm8=; b=ANf8B0zQsanmuyO8BA+ei/nrvIXnISJ+TtvX9v/0a76xaU0PQyMA/fbouWUt6rKcQg +n5F2wS2KRwML0QWmJB1/Ep40W55iPW8mpWpiiD9cQpZEldBI/pOsRBYjldAlSxqQNYM OpT0z1eKNW/SigDWpnH31+zmg1axG1LeKJHkroaiydcRdtilMgO7T971nfLxZYfKHD7a /hOoqlO+iILAnuD40g+Coi/3Yqabcoqwe56wse1y5UWRORpWeASDGkNi7NcOnFMERFo7 kMFGRCLBmTkNEmTFP/oKX2C/8IeuLI3M6m+M5QtqyupYPp2rC7B/rnni7D2LGxoqOA5W n81g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="tCfax1/A"; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m38-20020a05687088a600b000e20969c33bsi5687366oam.111.2022.04.12.06.28.53; Tue, 12 Apr 2022 06:29:08 -0700 (PDT) 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="tCfax1/A"; 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 S235620AbiDKWE5 (ORCPT + 99 others); Mon, 11 Apr 2022 18:04:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234958AbiDKWEs (ORCPT ); Mon, 11 Apr 2022 18:04:48 -0400 Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CAC7233E37 for ; Mon, 11 Apr 2022 15:01:43 -0700 (PDT) Received: by mail-lf1-x134.google.com with SMTP id x17so19915936lfa.10 for ; Mon, 11 Apr 2022 15:01:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ZNp2x4iSYzfok2hpXbrlRX9Pkk/fC9iE5eZ1/zNFkm8=; b=tCfax1/AYzD7Ti4qMWcS1kyB+xX3oV5rNPQ/YOCcHEKW3JgEnhCdscZ0+8E7cSlvGx E8u6ytmc9BqTl5rGhDdk5GlwBiog39gUu8SYkYtNLl6mvYYlbye2jJc7qzG5ZXezdbjC WKTneZgzdO16Psvm1hr8FgHjYCGSyuXIyFeSuXsHw5bPXg8wN1vymxJrQDVlWdvIBeYY 3Gsg2NpFxuS0GWF9QArO5UwK95eYSKhGVEWwuDLQ+5FobVg7RR1dpadylVfxnO4X3nAQ cQODHMSD32D2giK1USbkj9lWi/SBg4mUZNblMgDK5IDE2oFrvwqOM/X+gGgwP92kMxfG iHyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ZNp2x4iSYzfok2hpXbrlRX9Pkk/fC9iE5eZ1/zNFkm8=; b=yUwyNFBHq0StltxEeXdYomO1XvUwIajdaPQoR+N6R1tN5zXsY3ZJB0F1PBWzM0kDIG i50BKxU7sXeAl534HizYO0X/IrXCSSH3PIFJ2CkEHcjoO7C4WTfAWuH3DwyYJ2Dr3MIK DKSaybfHr2xO1AnhIbJhbSx1bH5wITJ0id7wJ51fbAEwNn/E2BiPijqP96MdKHB/j/bh 635A99mfd43YAHW1+8xaM4+qbIUjdtiu/GU8yLuEOokoUAl5HQQTnahBN5AWPBXSF5Eg voCXFKE2rOotPevvaFWHRyar5uKR6vDgDVyXdgQhKgR0RvkoQeRtnT//5a0eVFUxnb/6 sq6w== X-Gm-Message-State: AOAM5307j6iojqHRodsJEqvSqnmlknB8omL/GuIUhf2BWEEu0Gki2K2V IsIItrLm0sAt5wyzdcJfSxxufbHODfvh0G7zr8el/A== X-Received: by 2002:a19:674c:0:b0:448:3f49:e6d5 with SMTP id e12-20020a19674c000000b004483f49e6d5mr23577490lfj.518.1649714501680; Mon, 11 Apr 2022 15:01:41 -0700 (PDT) MIME-Version: 1.0 References: <20220411211015.3091615-1-bgardon@google.com> <20220411211015.3091615-4-bgardon@google.com> In-Reply-To: <20220411211015.3091615-4-bgardon@google.com> From: David Matlack Date: Mon, 11 Apr 2022 15:01:15 -0700 Message-ID: Subject: Re: [PATCH v4 03/10] KVM: selftests: Read binary stats desc in lib To: Ben Gardon Cc: LKML , kvm list , Paolo Bonzini , Peter Xu , Sean Christopherson , Peter Shier , David Dunn , Junaid Shahid , Jim Mattson , Mingwei Zhang , Jing Zhang Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 On Mon, Apr 11, 2022 at 2:10 PM Ben Gardon wrote: > > Move the code to read the binary stats descriptors to the KVM selftests > library. It will be re-used by other tests to check KVM behavior. > > No functional change intended. > > Signed-off-by: Ben Gardon > --- > .../selftests/kvm/include/kvm_util_base.h | 4 +++ > .../selftests/kvm/kvm_binary_stats_test.c | 9 ++---- > tools/testing/selftests/kvm/lib/kvm_util.c | 29 +++++++++++++++++++ > 3 files changed, 35 insertions(+), 7 deletions(-) > > diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h > index 5ba3132f3110..c5f34551ff76 100644 > --- a/tools/testing/selftests/kvm/include/kvm_util_base.h > +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h > @@ -401,6 +401,10 @@ void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid); > int vm_get_stats_fd(struct kvm_vm *vm); > int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid); > void read_vm_stats_header(int stats_fd, struct kvm_stats_header *header); > +struct kvm_stats_desc *alloc_vm_stats_desc(int stats_fd, > + struct kvm_stats_header *header); > +void read_vm_stats_desc(int stats_fd, struct kvm_stats_header *header, > + struct kvm_stats_desc *stats_desc); Same feedback here as the previous patch about getting rid of "vm_" in the function names. > > uint32_t guest_get_vcpuid(void); > > diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c > index 22c22a90f15a..e4795bad7db6 100644 > --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c > +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c > @@ -62,14 +62,9 @@ static void stats_test(int stats_fd) > header.data_offset), > "Descriptor block is overlapped with data block"); > > - /* Allocate memory for stats descriptors */ > - stats_desc = calloc(header.num_desc, size_desc); > - TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors"); > /* Read kvm stats descriptors */ > - ret = pread(stats_fd, stats_desc, > - size_desc * header.num_desc, header.desc_offset); > - TEST_ASSERT(ret == size_desc * header.num_desc, > - "Read KVM stats descriptors"); > + stats_desc = alloc_vm_stats_desc(stats_fd, &header); > + read_vm_stats_desc(stats_fd, &header, stats_desc); > > /* Sanity check for fields in descriptors */ > for (i = 0; i < header.num_desc; ++i) { > diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c > index 0caf28e324ed..e3ae26fbef03 100644 > --- a/tools/testing/selftests/kvm/lib/kvm_util.c > +++ b/tools/testing/selftests/kvm/lib/kvm_util.c > @@ -2564,3 +2564,32 @@ void read_vm_stats_header(int stats_fd, struct kvm_stats_header *header) > ret = read(stats_fd, header, sizeof(*header)); > TEST_ASSERT(ret == sizeof(*header), "Read stats header"); > } > + > +static ssize_t stats_descs_size(struct kvm_stats_header *header) > +{ > + return header->num_desc * > + (sizeof(struct kvm_stats_desc) + header->name_size); > +} > + > +/* Caller is responsible for freeing the returned kvm_stats_desc. */ > +struct kvm_stats_desc *alloc_vm_stats_desc(int stats_fd, > + struct kvm_stats_header *header) > +{ > + struct kvm_stats_desc *stats_desc; > + > + stats_desc = malloc(stats_descs_size(header)); > + TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors"); > + > + return stats_desc; > +} > + > +void read_vm_stats_desc(int stats_fd, struct kvm_stats_header *header, > + struct kvm_stats_desc *stats_desc) Reading the descriptors only needs to be done once per fd. So from an API point of view I don't think there's value in creating separate functions for the allocation and the read; just combine them in one. > +{ > + ssize_t ret; > + > + ret = pread(stats_fd, stats_desc, stats_descs_size(header), > + header->desc_offset); > + TEST_ASSERT(ret == stats_descs_size(header), > + "Read KVM stats descriptors"); > +} > -- > 2.35.1.1178.g4f1659d476-goog >