From patchwork Tue Jul 19 14:31:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12922619 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59D07C43334 for ; Tue, 19 Jul 2022 14:37:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238563AbiGSOhy (ORCPT ); Tue, 19 Jul 2022 10:37:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239153AbiGSOhc (ORCPT ); Tue, 19 Jul 2022 10:37:32 -0400 Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 448831176 for ; Tue, 19 Jul 2022 07:31:48 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1658241106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=O3n2AO7hJDNpmrDB/OBV6bnT5N4ksN+mLBmsqOqb48I=; b=cZ4BDOUps9V6Qv/PI3nsolJxJSAo8PuSobvJLz0aZA7cTmi6K7NF6/vyersEXXsaP3isP6 odmYUAQ+yJ0mlgp+wJ2+pYNAn/0ebnyTDsXoEFxM0gWZKi7PUAhmQ580Yzscy/l8E7910k cTF+gUXFRJEzUZ9mmUKN05+dezazDeU= From: Oliver Upton To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, Oliver Upton Subject: [PATCH 1/3] selftests: KVM: Check stat name before other fields Date: Tue, 19 Jul 2022 14:31:32 +0000 Message-Id: <20220719143134.3246798-2-oliver.upton@linux.dev> In-Reply-To: <20220719143134.3246798-1-oliver.upton@linux.dev> References: <20220719143134.3246798-1-oliver.upton@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Oliver Upton In order to provide more useful test assertions that describe the broken stats descriptor, perform sanity check on the stat name before any other descriptor field. While at it, avoid dereferencing the name field if the sanity check fails as it is more likely to contain garbage. Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/kvm_binary_stats_test.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c index b01e8b0851e7..40227ad9ba0c 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -73,6 +73,10 @@ static void stats_test(int stats_fd) for (i = 0; i < header.num_desc; ++i) { pdesc = get_stats_descriptor(stats_desc, i, &header); + /* Check name string */ + TEST_ASSERT(strlen(pdesc->name) < header.name_size, + "KVM stats name (index: %d) too long", i); + /* Check type,unit,base boundaries */ TEST_ASSERT((pdesc->flags & KVM_STATS_TYPE_MASK) <= KVM_STATS_TYPE_MAX, "Unknown KVM stats type"); @@ -99,9 +103,7 @@ static void stats_test(int stats_fd) 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); + /* Check size field, which should not be zero */ TEST_ASSERT(pdesc->size, "KVM descriptor(%s) with size of 0", pdesc->name); From patchwork Tue Jul 19 14:31:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12922617 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00A8EC43334 for ; Tue, 19 Jul 2022 14:37:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239288AbiGSOhs (ORCPT ); Tue, 19 Jul 2022 10:37:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239203AbiGSOhc (ORCPT ); Tue, 19 Jul 2022 10:37:32 -0400 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7B61240 for ; Tue, 19 Jul 2022 07:31:49 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1658241108; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IAgwA5oED9+OeZsGmvKgcD/KtCqiLCsBPLGVNza1h7g=; b=ch7XHr08P0o9h3SS+yUtF1HieNf6oeaqCDnx+1gJiMLRQmKzQ3O7sRvnTJI145kLabq4y4 a4xZ7eogqx/2c8kO+vDF0QNDg04u4J3Xmwitadb+kG7RSw9hPQ4PJWPd/D35jwA907zzTd zRTtnUQ8HVgjKwy5eln+IG2OGq6xUyM= From: Oliver Upton To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, Oliver Upton Subject: [PATCH 2/3] selftests: KVM: Provide descriptive assertions in kvm_binary_stats_test Date: Tue, 19 Jul 2022 14:31:33 +0000 Message-Id: <20220719143134.3246798-3-oliver.upton@linux.dev> In-Reply-To: <20220719143134.3246798-1-oliver.upton@linux.dev> References: <20220719143134.3246798-1-oliver.upton@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Oliver Upton As it turns out, tests sometimes fail. When that is the case, packing the test assertion with as much relevant information helps track down the problem more quickly. Sharpen up the stat descriptor assertions in kvm_binary_stats_test to more precisely describe the reason for the test assertion and which stat is to blame. Signed-off-by: Oliver Upton --- .../selftests/kvm/kvm_binary_stats_test.c | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c index 40227ad9ba0c..3237c7c94bf0 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -31,6 +31,7 @@ static void stats_test(int stats_fd) struct kvm_stats_desc *stats_desc; u64 *stats_data; struct kvm_stats_desc *pdesc; + u32 type, unit, base; /* Read kvm stats header */ read_stats_header(stats_fd, &header); @@ -72,18 +73,21 @@ 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); + type = pdesc->flags & KVM_STATS_TYPE_MASK; + unit = pdesc->flags & KVM_STATS_UNIT_MASK; + base = pdesc->flags & KVM_STATS_BASE_MASK; /* Check name string */ TEST_ASSERT(strlen(pdesc->name) < header.name_size, "KVM stats name (index: %d) too long", i); /* 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"); + TEST_ASSERT(type <= KVM_STATS_TYPE_MAX, + "Unknown KVM stats (%s) type: %u", pdesc->name, type); + TEST_ASSERT(unit <= KVM_STATS_UNIT_MAX, + "Unknown KVM stats (%s) unit: %u", pdesc->name, unit); + TEST_ASSERT(base <= KVM_STATS_BASE_MAX, + "Unknown KVM stats (%s) base: %u", pdesc->name, base); /* * Check exponent for stats unit @@ -97,10 +101,14 @@ 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 (%s) exponent: %i", + pdesc->name, pdesc->exponent); break; case KVM_STATS_UNIT_SECONDS: - TEST_ASSERT(pdesc->exponent <= 0, "Unsupported KVM stats unit"); + TEST_ASSERT(pdesc->exponent <= 0, + "Unsupported KVM stats (%s) exponent: %i", + pdesc->name, pdesc->exponent); break; } From patchwork Tue Jul 19 14:31:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12922618 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CF14C43334 for ; Tue, 19 Jul 2022 14:37:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239234AbiGSOhv (ORCPT ); Tue, 19 Jul 2022 10:37:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239208AbiGSOhc (ORCPT ); Tue, 19 Jul 2022 10:37:32 -0400 Received: from out2.migadu.com (out2.migadu.com [IPv6:2001:41d0:2:aacc::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAD7FC39 for ; Tue, 19 Jul 2022 07:31:50 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1658241109; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9Y+TsnEqdea4fx7oRubu4Fn88iDoXdM2Un0sc0IWSD4=; b=R5rUBU0I2hOmGECDP1AD2fwSA06w7jPHsloi+9QZg56usCVOLh48cWM2rhWX5D7Ue3OqQf nZBk5ktMIU5qAABlj5mBu3FevK4TL3xWO4fW8jQjU+09mdJE+FMV3paG6i94BbtAY1LQzW eOlWcDH5XsjUZWKriA3+U5BzH02VoN0= From: Oliver Upton To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, Oliver Upton Subject: [PATCH 3/3] selftests: KVM: Add exponent check for boolean stats Date: Tue, 19 Jul 2022 14:31:34 +0000 Message-Id: <20220719143134.3246798-4-oliver.upton@linux.dev> In-Reply-To: <20220719143134.3246798-1-oliver.upton@linux.dev> References: <20220719143134.3246798-1-oliver.upton@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Oliver Upton The only sensible exponent for a boolean stat is 0. Add a test assertion requiring all boolean statistics to have an exponent of 0. Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/kvm_binary_stats_test.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c index 3237c7c94bf0..0b45ac593387 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -96,6 +96,7 @@ static void stats_test(int stats_fd) * Exponent for unit seconds should be less than or equal to 0 * Exponent for unit clock cycles should be greater than or * equal to 0 + * Exponent for unit boolean should be 0 */ switch (pdesc->flags & KVM_STATS_UNIT_MASK) { case KVM_STATS_UNIT_NONE: @@ -110,6 +111,11 @@ static void stats_test(int stats_fd) "Unsupported KVM stats (%s) exponent: %i", pdesc->name, pdesc->exponent); break; + case KVM_STATS_UNIT_BOOLEAN: + TEST_ASSERT(pdesc->exponent == 0, + "Unsupported KVM stats (%s) exponent: %d", + pdesc->name, pdesc->exponent); + break; } /* Check size field, which should not be zero */