Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp329532iob; Tue, 3 May 2022 18:59:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxHpdbZSfFASRW5P9Ve2d5POh1ZWxZiWRL9rmwSvBStDBGqrs3REm+k+GIrDVXZAdBw9aDo X-Received: by 2002:a17:902:6bc9:b0:158:a0d3:d080 with SMTP id m9-20020a1709026bc900b00158a0d3d080mr19791848plt.24.1651629579463; Tue, 03 May 2022 18:59:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651629579; cv=none; d=google.com; s=arc-20160816; b=tick4FFRrsIb42GI0oqLOFKT0rELujMpA9qdmtddesqUJ+t0dZWCHLlNSToLA5P2MU 1wdQuuHXVM0pz6kXsx7czAyN7LkOg7FsVM2EXkKuBIontqbAHDNkRrYjqxSE6IrP2yra cg75P+9YdtiI6Lu7vDKnbVw7fmuIXz1Ee1tJR3WWU5ebfDGyW7U/kCionQZN9+Ubl4G5 mSAe4+ymUtljZPwxIjDX/C/m4juIT8krb60o9cDEyjr3kidyuP64EHMx9EJyxZuLZVXD zbqhuUa7bgqSzY+p+TSN3RCEjesLhNa/kLWroVG7FkrP90swjVunhNRk8d1V9AuuxpI0 wbTQ== 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=ZcTa31/SHnSC3s49aeO+K0dpZ+qhe9XO6pQANg17BXs=; b=KojJKeIUXPj0+1Kmtc9AJ1NIr4QH5i6/lpjtFCnLOJuAJNgTmFCc3ynjGug27yjctX CBVQ6gMzWPQFVvVhe+w/n5yD9CqWsTIcmpExy1saHuZHPItJ0ufimx6VzyTuNlpXfmog RaXoYzbF52RcqdNDdhBm1sKtri8AFDheVkXnPBeBNW71nogkJDoJqazCqNKrisAzaS+y oLzKxZRVtwRWFYdGMCwXP69jMvDaduiRmm+UUxEoJEgVVY22wxhF3ax+1J1XKQ9rOU2n oWz1mJldlNIzB3n95K3+wXavNs/WpgobBfVcDGIaOUqC02/Il6MT2+yLvDLIcR1Ah3h7 pFag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="a/uhsK4R"; 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 o9-20020a170902d4c900b0015e8509c44dsi5557326plg.171.2022.05.03.18.59.24; Tue, 03 May 2022 18:59:39 -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="a/uhsK4R"; 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 S241428AbiECSer (ORCPT + 99 others); Tue, 3 May 2022 14:34:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241454AbiECSe3 (ORCPT ); Tue, 3 May 2022 14:34:29 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF6D81EACB for ; Tue, 3 May 2022 11:30:55 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id m11-20020a170902f64b00b0015820f8038fso8220553plg.23 for ; Tue, 03 May 2022 11:30: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=ZcTa31/SHnSC3s49aeO+K0dpZ+qhe9XO6pQANg17BXs=; b=a/uhsK4RxHQP14ztHfIBSg/ZakJ/fA1TktE7S4+NmjsAWXMvT7CHxbvRcXbpPxTnI4 FJ7DNFpnR2rgysgV3lab22Gx49Y8jYUzy/PyiIfY2+lC9hkVtENJiHwOzbtSZSC5h1Jn TVGZ4o3FY5VoGf1OBlTVKhdZusATR7tfAKQXdUmslb9QWP5LjXzYNmQauFLTPDIWGJzl VeMF3MPtyRQw1DOx+azwWeB6ffJSJelEpeH7m65S/xoY9ziCjS866cesm5Uydxousnvz Q4ypxTqYWCcii2ghtZMepBSSe9GlGH+4dc5x9x9sKzkWXPRZE6pauzBo5e8+tWP381u3 kxqg== 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=ZcTa31/SHnSC3s49aeO+K0dpZ+qhe9XO6pQANg17BXs=; b=Yfaqpe7/xIKxrmND7ErmF6L4UnEaBHS8UpVM0wcM2uKF/Cfeoc/yMS6mcNF+Xa+NGX O7w8OQ4CijuECQlFWNQRbNWW6qzi7BI9uK9kzvKEa8cz8ud8NC8iCqTvqXx669jjuDLr 0haRoHFc4jW4xOIL3apPtkUJaG5jHQOvawmPKe31h2dPIZdjOuO96YT0Jt5Klmj+5tHH RhwAE886yi57JO/+4ftTv5LBkGM1yzpB84KrXtILvXmuusdpj6u4VIpwYB5hWHsim0MS qdBF8B2FQ/I5sUWW36nHVicJkU5MdGwY8ALqWrg1EuH3IfE4nJbBnQxAhy4368i7AAgr PxGQ== X-Gm-Message-State: AOAM530hPhzoijrLCqWAa/mdDrbqLf5JLPnrN9Fi4topvJDNIXM19qIV cx3AJd5FukU4POYs3AHU9Ai6cZYbEw1KSqY/rSCm4J2v1GB7nnj5bFNls3gmyKl2vkCC8AT3Nz9 aV2vB7/14s7vAbii7O7KShHsoUR9vLi2QnD/GrAHhUm289wxYngWuNF0AbUROQgbXNFn+nxFy X-Received: from sweer.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:e45]) (user=bgardon job=sendgmr) by 2002:a17:902:8644:b0:15a:3b4a:538a with SMTP id y4-20020a170902864400b0015a3b4a538amr17965247plt.146.1651602655150; Tue, 03 May 2022 11:30:55 -0700 (PDT) Date: Tue, 3 May 2022 18:30:38 +0000 In-Reply-To: <20220503183045.978509-1-bgardon@google.com> Message-Id: <20220503183045.978509-5-bgardon@google.com> Mime-Version: 1.0 References: <20220503183045.978509-1-bgardon@google.com> X-Mailer: git-send-email 2.36.0.464.gb9c8b46e94-goog Subject: [PATCH v7 04/11] 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=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 | 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.36.0.464.gb9c8b46e94-goog