Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp816074ioo; Thu, 26 May 2022 15:55:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjpPsyuWVfVTl2x3qmhZPRGzgLJqvwzA15Xzt8yWvQ0m6+hryKovcBe5g1QWhDNYshLl1m X-Received: by 2002:a17:902:c942:b0:161:e3aa:26fd with SMTP id i2-20020a170902c94200b00161e3aa26fdmr36780566pla.103.1653605725176; Thu, 26 May 2022 15:55:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653605725; cv=none; d=google.com; s=arc-20160816; b=ZCPR+4WUD+1X+7M2wLUbDhskTYJuB3QnKShogYBhDMPFmt4rcxdYM49mV3Hw5ccH91 h2BefMvkkM9kLTJDSVnzDsw1xbkRmVtXjufbFoG232qkvKv121Wi6YXMe1T/EBchy8oc ymvMdiC8NgEK/bSkVw4018WtI767uhgjsntcZa7QzJVaMSYBlRDmq00mZX2vzYgeua7d JmczoFS2oasyQshnCR0yb8w0QXJ/U4FPExcFHxJNVe6KiJL1dMjyr+ZOw7FAsTPZb9sE wXGCfjyVcJAOa7LDdBZCGpPD+MaTWPOOfbS1hyyfhfoVsKhm5YacT7VjrlmOXPbcBLU8 38Jg== 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=falxtzP4l1rM8q3vaJiFDhXAWycm4MI2aAJ4+6pFDbo=; b=Gz4KBgIcvpOMDKsQJXowgSrLvWL76GBCuZ2+/HWvMTaR4x0ZwP+Yas2tBFrWVIU5H+ fLYNqPEa2D2Yqo+uN7yT8tok3bIMeXhbXxyYi9FQEHV6OgHZGEqnoKZCZZImKByL319F OVGb3UWHfTTNWCf526Q5SCy8PeYZuJ7u25pQvvxM7F9a9aO48F/asQ+fzDkfdK5ksNas cyTtL7dMVXtfJ1SOgnJyoO2Zp9RI6TSq00JjkT7MV6RN16GiknPyVoeeFdGffuwmKGjK G7StH1r2LInBUECm0Ev5msOnm3ONRb9hzsPIcki1hqerddnKaA2VqDI2hJKBIjKXA+0k iTtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Iy7Dxc3i; 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 z12-20020a056a001d8c00b00518743649a9si2836241pfw.366.2022.05.26.15.55.13; Thu, 26 May 2022 15:55:25 -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=Iy7Dxc3i; 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 S1348376AbiEZRyc (ORCPT + 99 others); Thu, 26 May 2022 13:54:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347617AbiEZRyX (ORCPT ); Thu, 26 May 2022 13:54:23 -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 55271A7E33 for ; Thu, 26 May 2022 10:54:19 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id m5-20020a17090a4d8500b001e0cfe135c7so1458604pjh.3 for ; Thu, 26 May 2022 10:54:19 -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=falxtzP4l1rM8q3vaJiFDhXAWycm4MI2aAJ4+6pFDbo=; b=Iy7Dxc3igXT8IaYfpRfrevtHrWY0aG5RMOHxZFj1X3DSUrAa9qJVreThMq7/xLkq4a fZMvR3ZTjvrg7dRmTnDc6HYXsd3dFiIB2XuVHIsu3TFrv0WqNcPIdK/aNOaUdcK3qEva G/jlnDADPDyj7xMTwBNtDLfeUGtEI5Q9/OYBpirxN2g6EqjaeSaklU/jDfLKFdnmz+w1 ICfl4elBVBMQ5pa6z//x1FCkMUBEmpaOWAMGBLdBtI+ReYvwByWhsyM7dEVRfUZ+9K0g 7a38FAhAHvfbCmszrVQSNb/gt1u7XhyrkeyFmLjMHldrtgL2HndL4D78Sbk5q0jfGFVX /pQQ== 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=falxtzP4l1rM8q3vaJiFDhXAWycm4MI2aAJ4+6pFDbo=; b=5lBEvmGHLxi7oYEJ5xGoFB5IY/NsEYRtVcGdJzSrzu4UHY0mqjLjDWCqYzRwcCCVQr FseL0XzmDzVIYR8JLgKJ7Tdg2ZjIk57mULkt78waDqTiMo8+FYMXBsA0cZdKHIeTieE0 gGiudgNjBDUf+V9UYDUf//fWbSdXTfZ5PaTPtN8MJlH0RMHJW/hTOgo9/im4MZkZ2uLx 8AORQWnDz/UCH5bw5KbnUtvOLNXZZhBmhKOR/yAXFVM/eij/bUlnP39Jr1luJ758Q8/P t/A3TFkbWhuieLT31YPbpFfKIaxDEuI5VY4jsaiwuhbVPjHC+txhNJzaGDDh1xSDAZyH ctyQ== X-Gm-Message-State: AOAM533V9CpAE2ZkET+gG9y2deiZ7SGhKfSa2p8/kp9pKQNS40Mgepe4 tJXY3v6+d7Gz7Sq+EZ4CILFrWmlh/6Fj X-Received: from sweer.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:e45]) (user=bgardon job=sendgmr) by 2002:a05:6a00:1a44:b0:510:a043:3b09 with SMTP id h4-20020a056a001a4400b00510a0433b09mr39441711pfv.62.1653587658635; Thu, 26 May 2022 10:54:18 -0700 (PDT) Date: Thu, 26 May 2022 17:54:01 +0000 In-Reply-To: <20220526175408.399718-1-bgardon@google.com> Message-Id: <20220526175408.399718-5-bgardon@google.com> Mime-Version: 1.0 References: <20220526175408.399718-1-bgardon@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [PATCH v8 04/11] KVM: selftests: Clean up coding style in binary stats test From: Ben Gardon To: kvm@vger.kernel.org, Paolo Bonzini Cc: linux-kernel@vger.kernel.org, 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=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 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 Reviewed-by: David Matlack Signed-off-by: Ben Gardon --- .../selftests/kvm/kvm_binary_stats_test.c | 79 +++++++++++-------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c index a5198b9a19c1..956dc40d9bc7 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -40,27 +40,31 @@ static void stats_test(int stats_fd) /* 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_descriptors(stats_fd, &header); @@ -68,14 +72,17 @@ static void stats_test(int stats_fd) /* Sanity check for fields in descriptors */ for (i = 0; i < header.num_desc; ++i) { pdesc = get_stats_descriptor(stats_desc, i, &header); + /* 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 + 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 +93,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 = get_stats_descriptor(stats_desc, i, &header); 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.36.1.124.g0e6072fb45-goog