Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp1572832pxb; Thu, 14 Apr 2022 08:58:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz5a5JbAP9FH4dIvMBv4HseLz8hOqUl830x9iWuvMA/N9UcVZ4ifbCqUDtSRwA+OwceVXuD X-Received: by 2002:a17:902:8506:b0:154:8692:a7ac with SMTP id bj6-20020a170902850600b001548692a7acmr48375780plb.10.1649951906237; Thu, 14 Apr 2022 08:58:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649951906; cv=none; d=google.com; s=arc-20160816; b=ZeJB51XohujKjQNmcSoMEaLbEJB0j0qq9ntoap+VzL+KEOxLCGUeARvjPSjEoHsydD t33yOfEKh/BADG4+onx7LUJEJvDxbmToNNqkWY+/bWlLo3tXhkTZRZkOLaSg/imVQbp5 0ycXXEY2ENivVQsf1cAIycpkdcd8Ocm5K6eE3peR+1EZlrLEvzK1WjkJFhLI1xHyGi0J mxonF/SUocpJ4DB7XSg2Ig4vEZobdwZ2ZBwCD/vEMWscOkMnLj/FGt63g2RyEdgPuT9S kDB6h8QGlgubTm6qOuQXgMWX2Z67aDFf0xIGAyiWBeVFKVdvEfOan12giduG0q7qoaxM AOVw== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=dGdHT2bPLXOJ2n8aFuh4xhdm9dOQMBhMBknnyhFlP5I=; b=LFc7K4ulZzxunzEAsmGXMgjwRXFgdFUDeK2EsO2/BefWPPU4H8PdTPMWAJmvIx+k8M Lx5Hs/Pwd5TDFMnd1kovL+jNWwiKwaPNVQLbP83d1oxCWjmOQvmg+S2GgTC0i55Z8b24 KMYOb8YZ7pf3bec5nSW+m1XAqI0pXI3n0RKL6yC+R248yuMg4mf6TCwoTQRfBKay0znS pmVUyt5zBIcFjkIOJI+A8vtq1saWv+L1ScO7g/52v129ygN620xeFHrZvyGyzGfiHJn/ w+DHw0KfDkZiErVnngAAhnPmwtpk1ZkZ6cWN6zOCLoSnJTRvDu54xtl01D9faGqrLKnu JdqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=jBAwjxin; 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 u12-20020a63600c000000b003a2211d25fcsi611186pgb.598.2022.04.14.08.58.10; Thu, 14 Apr 2022 08:58:26 -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=jBAwjxin; 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 S237595AbiDMSCZ (ORCPT + 99 others); Wed, 13 Apr 2022 14:02:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237567AbiDMSCS (ORCPT ); Wed, 13 Apr 2022 14:02:18 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18CAF3FBD2 for ; Wed, 13 Apr 2022 10:59:55 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id oo16-20020a17090b1c9000b001c6d21e8c04so4097064pjb.4 for ; Wed, 13 Apr 2022 10:59:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=dGdHT2bPLXOJ2n8aFuh4xhdm9dOQMBhMBknnyhFlP5I=; b=jBAwjxink+IFXlbU98ZjIOs4uulu5gO8kRsE7u4MXqpB8RzpFew92LEOwx4Xu9X8k6 xgoBr1hxRAUDGQ4kJ/MnRo04q0jTuZTTcosAtfJhwItYkFSokhBaSr58g6X42k1DPPVa 2Kqr6dmOtE+hUey8r9q5rFiUOv0nlW8R2sdtAD64IdlTe4So1NjCYEQwjwFN4tochF9P AWaA1CH7FDYgCSDTspVLWbL7yqNDxiXZ3xu3IsNz+qR+Nk0/qqtJYnCAfmOnkRgATAg3 v8L9TysbeR0jl5BKqY5Znlh64uGAV+JGCdG6puNyFcHiVje3A5beJKeI4YFR1g7st/Nz qZng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=dGdHT2bPLXOJ2n8aFuh4xhdm9dOQMBhMBknnyhFlP5I=; b=3Jqh0iKSMOeERPexQ3vYI2vca2R9DNCrIw0d4h9d7mLj3lMtC3W0l/ErZGSfmmwSKJ 29k8OubIoVL40MqVL+JGpP0Bt0LzTJUKBxQsbsz8ow5scLKgh5LFdx0L9SUtWJkyxvpD eIBlXWg8rIRdzmcCY/XjoFeFAcQtrzzll55gbRT7ATiJW4tNIs3xCDTbFgmti61F0U91 XTBtuUSt5DuBtplaI3B8uVyjZXcT04qz3mU8rs1huV/6vCyFs89bEJlagcIFrbImHSpP zhYa3rpXPYme7OZfI/m+r7rwPIJqRgI3mw81r7yCCCVjofLjkSl7/dflGq+l2S3j1jof RmmA== X-Gm-Message-State: AOAM531DrZH1pOaEIMDPQuMdZyvUNOBA8NbrHj4dEIyyxFd5kaBV94wP 3YLUekkLAbGGyQX3GeRGgfh9ESUZ5G6OgFfknT6Puq0qMgkW6S6PVE5dlrqBwsxGaM4MqvlR0V5 8b0eOL3CSFPl6hrYQ2+HKfj112ifEhZUSw7yEgVJifR7THQDJr/A3WTeDIzOyrn8tay201Gnu X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:c087:f2f2:f5f0:f73]) (user=bgardon job=sendgmr) by 2002:a65:6753:0:b0:385:fa8a:188f with SMTP id c19-20020a656753000000b00385fa8a188fmr36502519pgu.499.1649872794452; Wed, 13 Apr 2022 10:59:54 -0700 (PDT) Date: Wed, 13 Apr 2022 10:59:38 -0700 In-Reply-To: <20220413175944.71705-1-bgardon@google.com> Message-Id: <20220413175944.71705-5-bgardon@google.com> Mime-Version: 1.0 References: <20220413175944.71705-1-bgardon@google.com> X-Mailer: git-send-email 2.35.1.1178.g4f1659d476-goog Subject: [PATCH v5 04/10] KVM: selftests: Clean up coding style in binary stats test From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , David Matlack , Jim Mattson , David Dunn , Jing Zhang , Junaid Shahid , Ben Gardon Content-Type: text/plain; charset="UTF-8" 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,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_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 From: Sean Christopherson 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 Signed-off-by: Ben Gardon --- .../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 b49fae45db1e..8b31f8fc7e08 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -35,47 +35,64 @@ static void stats_test(int stats_fd) /* Read kvm stats header */ read_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 = read_stats_desc(stats_fd, &header); /* 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 @@ -86,47 +103,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); } /* Allocate memory for stats data */ -- 2.35.1.1178.g4f1659d476-goog