Received: by 2002:a05:6512:3d0e:0:0:0:0 with SMTP id d14csp49019lfv; Tue, 12 Apr 2022 16:49:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxajYzdeppDvfL+Vx1+QNE0rTITyS/o7Kh4kOUKy0n9DA97s8Zv3jKRKyp2DFreHayZaR7W X-Received: by 2002:a05:6402:1148:b0:416:a4fb:3c2e with SMTP id g8-20020a056402114800b00416a4fb3c2emr40922755edw.182.1649807354686; Tue, 12 Apr 2022 16:49:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649807354; cv=none; d=google.com; s=arc-20160816; b=LIQVJBgHFEbsVqsI9GW+QYaRHnFVJ/+0eHQgb9jYowjqpzB47igSumC3oEFdRv0fal 04WrPMCmoGK2K4SjQl6gfZUBXL5KcmPNjuP9V1wOVikJd02IrK9UPpVfZb93c2uaTez5 26uVHRin+Qjplp9H2tdB99dcN7MFhgcAibHuG31g4JnwDpxFvJuwX1Gb+oPHUa/QmWKm Y7mDnVe9j6JiJrTrh7G638Dh+O45V4+MfaMirXCdUr2d3pYDX9vtG5wVPvC2wmvSGPLZ KY8sExvjgxc8GcFLNOJ576Q2RlgvDH1DR2xVv60FmLfLmhray/2P2INZG5SWGyt81mcs be3w== 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=wH4O0dnTVGL+IJJ5WtWlvfWg3+EbrXvX3Kihn0J2PXo=; b=IF6K6/sEjDxewdLDLCVicy1sBJuc9mCYKqU1YbDRQWj4Av9Q4ihCTKYcm762z0v7Ik lWcdDSOjd5sBefFqG7peCvGhtbGaCITtXdA9/mJ/mS3/N2pBYWzQ8KoYTYejmXUwA2Gi 9tV2fjFuE8eVm8o+XvYTlBNbs75nhupJwyMfqP97XS4jKxN8iD3kkmFCsVBLmgtH5/Sk oKkobSMgY7lhXk++Pma8apuWd889IRqhGSGgY8o97GI9Hn7lCmBM/8jNMn65YxueOhCp hO+f55lnadSX0VMsriIGKJ0YJQbaMAYIFAfUqQgVWJl8Ao7cxBSc4XxWTjoxJjDg/Ngp cQkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=I5+hrPJV; 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 j21-20020a170906431500b006e89b49cb32si4017732ejm.180.2022.04.12.16.48.50; Tue, 12 Apr 2022 16:49:14 -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=I5+hrPJV; 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 S229939AbiDLX2d (ORCPT + 99 others); Tue, 12 Apr 2022 19:28:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230297AbiDLX1r (ORCPT ); Tue, 12 Apr 2022 19:27:47 -0400 Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D923D1133 for ; Tue, 12 Apr 2022 15:12:57 -0700 (PDT) Received: by mail-yb1-xb33.google.com with SMTP id z33so526907ybh.5 for ; Tue, 12 Apr 2022 15:12:57 -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=wH4O0dnTVGL+IJJ5WtWlvfWg3+EbrXvX3Kihn0J2PXo=; b=I5+hrPJVkQspvFYxISEhKos6XFff3LFzGkbpLmGszEe268qN4q+QCJVCmpkoRonzoE ktz5K4AfgFtEQPUt9/KfqNr0v/RuX3O8SpMICcZtbfV9IYCcE/EQhxlZFtRXYFK8g3js 8b99ki+jzImCibf7j2hfvYKdkBf6bTxusS1GxZ0JqHwZ3klRvGfaQtNRZYZ5HNbwQ0AC 0eCE6Nmm6+shnUqs6P8vTeipUNJeVsHhXSR008ZFyqf5sW3grlNU0Gfnz2sBC2ElWlqv yVvvVwSpBg4dTqa88urPeL44iMIu+IcpRKTX0UYbbZoapbXwkJw/ujMd5u7ERBcuFg+1 XyuA== 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=wH4O0dnTVGL+IJJ5WtWlvfWg3+EbrXvX3Kihn0J2PXo=; b=d6V4VGEi/6v++La5p9NqGk09WPHG7ISBbVCmu9I0UDaY/YSj4+gRF8I7R0VpOADIhi KytYE4NsDh/gK4j8aHY6EB8vtnwxbly8IzOrXDaKX9XutEgx0joJb29MzkdNtOAJMkTv EHl/mM8QRQa09+3nfbfy3dykFQx2FkOZOOy3SiPfmH/GrycRCr7yrbDKKmN1TpR6f7lT PK/K0d8M+qGXReBNmNT1VjaZfQHtl2GA2eUG7vDaoEmYDevloA67OwTO03COUp/bpM1R lzB5NktOazmyXIbeZHf1QBGUYcLujZqXU4+HStquX7euNV4Z11WDbMHuXfVT+1a4hi8a PG4Q== X-Gm-Message-State: AOAM532oQOtUMmFQcbtPPBeXqAB3CdZW+35uc6nVwJFPEkKMStgJ+NIe JbrrEcF3uqnnuKHctHhxJFfdrfVyFzxYusV351vMJA== X-Received: by 2002:a25:add6:0:b0:641:2562:4022 with SMTP id d22-20020a25add6000000b0064125624022mr11991204ybe.391.1649801576439; Tue, 12 Apr 2022 15:12:56 -0700 (PDT) MIME-Version: 1.0 References: <20220411211015.3091615-1-bgardon@google.com> <20220411211015.3091615-4-bgardon@google.com> In-Reply-To: From: Ben Gardon Date: Tue, 12 Apr 2022 15:12:45 -0700 Message-ID: Subject: Re: [PATCH v4 03/10] KVM: selftests: Read binary stats desc in lib To: Sean Christopherson Cc: Mingwei Zhang , LKML , kvm , Paolo Bonzini , Peter Xu , Peter Shier , David Dunn , Junaid Shahid , Jim Mattson , David Matlack , 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=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 On Tue, Apr 12, 2022 at 1:03 PM Sean Christopherson wrote: > > On Tue, Apr 12, 2022, Sean Christopherson wrote: > > On Tue, Apr 12, 2022, Ben Gardon wrote: > > > On Mon, Apr 11, 2022 at 5:55 PM Mingwei Zhang wrote: > > > > I was very confused on header->name_size. So this field means the > > > > maximum string size of a stats name, right? Can we update the comments > > > > in the kvm.h to specify that? By reading the comments, I don't really > > > > feel this is how we should use this field. > > > > > > I believe that's right. I agree the documentation on that was a little > > > confusing. > > > > Heh, a little. I got tripped up looking at this too. Give me a few minutes and > > I'll attach a cleanup patch to add comments and fix the myriad style issues. > > > > This whole file is painful to look at. Aside from violating preferred kernel > > style, it's horribly consistent with itself. Well, except for the 80 char limit, > > to which it has a fanatical devotion. > > If you send a new version of this series, can you add this on top? Will do. > > From: Sean Christopherson > Date: Tue, 12 Apr 2022 11:49:59 -0700 > Subject: [PATCH] KVM: selftests: Clean up coding style in binary stats test > > Fix a variety of code style violations and/or inconsistencies in the > binary stats test. The 80 char limit is a soft limit and can and should > be ignored/violated if doing so improves the overall code readability. > > Specifically, provide consistent indentation and don't split expressions > at arbitrary points just to honor the 80 char limit. > > Opportunistically expand/add comments to call out the more subtle aspects > of the code. > > Signed-off-by: Sean Christopherson > --- > .../selftests/kvm/kvm_binary_stats_test.c | 91 ++++++++++++------- > 1 file changed, 56 insertions(+), 35 deletions(-) > > diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c > index 97b180249ba0..944ed52e3f07 100644 > --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c > +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c > @@ -37,32 +37,42 @@ static void stats_test(int stats_fd) > /* Read kvm stats header */ > read_vm_stats_header(stats_fd, &header); > > + /* > + * The base size of the descriptor is defined by KVM's ABI, but the > + * size of the name field is variable as far as KVM's ABI is concerned. > + * But, the size of name is constant for a given instance of KVM and > + * is provided by KVM in the overall stats header. > + */ > size_desc = sizeof(*stats_desc) + header.name_size; > > /* Read kvm stats id string */ > id = malloc(header.name_size); > TEST_ASSERT(id, "Allocate memory for id string"); > + > ret = read(stats_fd, id, header.name_size); > TEST_ASSERT(ret == header.name_size, "Read id string"); > > /* Check id string, that should start with "kvm" */ > TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size, > - "Invalid KVM stats type, id: %s", id); > + "Invalid KVM stats type, id: %s", id); > > /* Sanity check for other fields in header */ > if (header.num_desc == 0) { > printf("No KVM stats defined!"); > return; > } > - /* Check overlap */ > - TEST_ASSERT(header.desc_offset > 0 && header.data_offset > 0 > - && header.desc_offset >= sizeof(header) > - && header.data_offset >= sizeof(header), > - "Invalid offset fields in header"); > + /* > + * The descriptor and data offsets must be valid, they must not overlap > + * the header, and the descriptor and data blocks must not overlap each > + * other. Note, the data block is rechecked after its size is known. > + */ > + TEST_ASSERT(header.desc_offset && header.desc_offset >= sizeof(header) && > + header.data_offset && header.data_offset >= sizeof(header), > + "Invalid offset fields in header"); > + > TEST_ASSERT(header.desc_offset > header.data_offset || > - (header.desc_offset + size_desc * header.num_desc <= > - header.data_offset), > - "Descriptor block is overlapped with data block"); > + (header.desc_offset + size_desc * header.num_desc <= header.data_offset), > + "Descriptor block is overlapped with data block"); > > /* Read kvm stats descriptors */ > stats_desc = alloc_vm_stats_desc(stats_fd, &header); > @@ -70,15 +80,22 @@ static void stats_test(int stats_fd) > > /* Sanity check for fields in descriptors */ > for (i = 0; i < header.num_desc; ++i) { > + /* > + * Note, size_desc includes the of the name field, which is > + * variable, i.e. this is NOT equivalent to &stats_desc[i]. > + */ > pdesc = (void *)stats_desc + i * size_desc; > - /* Check type,unit,base boundaries */ > - TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK) > - <= KVM_STATS_TYPE_MAX, "Unknown KVM stats type"); > - TEST_ASSERT((pdesc->flags & KVM_STATS_UNIT_MASK) > - <= KVM_STATS_UNIT_MAX, "Unknown KVM stats unit"); > - TEST_ASSERT((pdesc->flags & KVM_STATS_BASE_MASK) > - <= KVM_STATS_BASE_MAX, "Unknown KVM stats base"); > - /* Check exponent for stats unit > + > + /* Check type, unit, and base boundaries */ > + TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK) <= KVM_STATS_TYPE_MAX, > + "Unknown KVM stats type"); > + TEST_ASSERT((pdesc->flags & KVM_STATS_UNIT_MASK) <= KVM_STATS_UNIT_MAX, > + "Unknown KVM stats unit"); > + TEST_ASSERT((pdesc->flags & KVM_STATS_BASE_MASK) <= KVM_STATS_BASE_MAX, > + "Unknown KVM stats base"); > + > + /* > + * Check exponent for stats unit > * Exponent for counter should be greater than or equal to 0 > * Exponent for unit bytes should be greater than or equal to 0 > * Exponent for unit seconds should be less than or equal to 0 > @@ -89,47 +106,51 @@ static void stats_test(int stats_fd) > case KVM_STATS_UNIT_NONE: > case KVM_STATS_UNIT_BYTES: > case KVM_STATS_UNIT_CYCLES: > - TEST_ASSERT(pdesc->exponent >= 0, > - "Unsupported KVM stats unit"); > + TEST_ASSERT(pdesc->exponent >= 0, "Unsupported KVM stats unit"); > break; > case KVM_STATS_UNIT_SECONDS: > - TEST_ASSERT(pdesc->exponent <= 0, > - "Unsupported KVM stats unit"); > + TEST_ASSERT(pdesc->exponent <= 0, "Unsupported KVM stats unit"); > break; > } > /* Check name string */ > TEST_ASSERT(strlen(pdesc->name) < header.name_size, > - "KVM stats name(%s) too long", pdesc->name); > + "KVM stats name(%s) too long", pdesc->name); > /* Check size field, which should not be zero */ > - TEST_ASSERT(pdesc->size, "KVM descriptor(%s) with size of 0", > - pdesc->name); > + TEST_ASSERT(pdesc->size, > + "KVM descriptor(%s) with size of 0", pdesc->name); > /* Check bucket_size field */ > switch (pdesc->flags & KVM_STATS_TYPE_MASK) { > case KVM_STATS_TYPE_LINEAR_HIST: > TEST_ASSERT(pdesc->bucket_size, > - "Bucket size of Linear Histogram stats (%s) is zero", > - pdesc->name); > + "Bucket size of Linear Histogram stats (%s) is zero", > + pdesc->name); > break; > default: > TEST_ASSERT(!pdesc->bucket_size, > - "Bucket size of stats (%s) is not zero", > - pdesc->name); > + "Bucket size of stats (%s) is not zero", > + pdesc->name); > } > size_data += pdesc->size * sizeof(*stats_data); > } > - /* Check overlap */ > - TEST_ASSERT(header.data_offset >= header.desc_offset > - || header.data_offset + size_data <= header.desc_offset, > - "Data block is overlapped with Descriptor block"); > + > + /* > + * Now that the size of the data block is known, verify the data block > + * doesn't overlap the descriptor block. > + */ > + TEST_ASSERT(header.data_offset >= header.desc_offset || > + header.data_offset + size_data <= header.desc_offset, > + "Data block is overlapped with Descriptor block"); > + > /* Check validity of all stats data size */ > TEST_ASSERT(size_data >= header.num_desc * sizeof(*stats_data), > - "Data size is not correct"); > + "Data size is not correct"); > + > /* Check stats offset */ > for (i = 0; i < header.num_desc; ++i) { > pdesc = (void *)stats_desc + i * size_desc; > TEST_ASSERT(pdesc->offset < size_data, > - "Invalid offset (%u) for stats: %s", > - pdesc->offset, pdesc->name); > + "Invalid offset (%u) for stats: %s", > + pdesc->offset, pdesc->name); > } > > /* Read kvm stats data one by one */ > > base-commit: f9955a6aaa037a7a8198a817b9d272efcf10961a > -- >