Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp819262rdf; Fri, 3 Nov 2023 17:04:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHqFE3QSq1m1oX1B1rqrq80mjEYOEfvbpe6mUl3nk9gQ8eNBwtMBfFoWzoGSZKn8QUFyRLh X-Received: by 2002:a17:902:ec82:b0:1cc:6acc:8fa4 with SMTP id x2-20020a170902ec8200b001cc6acc8fa4mr12678092plg.32.1699056254619; Fri, 03 Nov 2023 17:04:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699056254; cv=none; d=google.com; s=arc-20160816; b=OlXRLxJHi0oFmVJgeW5kFntP7vLbx+Lmx8PzH99dDrLTkNF68Huj7mouhgTwSpv1aG 8SRp1wKrpjsY8anAx0ufjmf4r4XE0mjop55V6KP7O62GcvV8DhiNsH73eiQ0wRSrnraw xE7dk/ChIAAVj+35hNl1Vs+oALZksIij+eqYqdcIZQfckBz3VhHxzgxGOb5IioGQhfqJ HIYvsOoN1hWoAGIXeG9EeQGnkE6wZpswBwz5/a7BL9HWUln7N4ygIEp0YTH1WUJth7X8 r2QHf8LurnE6L3Zt+EdtDAU07b4WcAX0r3t1WuEdX0jtgXmniACJKPhojN6m1Tn1DPhm uoAg== 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:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=QsiXyox+rGQhiB/Za0Yz/FRQCQ9gMcg4U4njvPOlEGQ=; fh=z/UQYx9FUD9Bz9JyYrmi2ijAScp29gktKr8lejSUuXU=; b=BUXtCF6jMZdK550GN/RzQMpZg5ZsHtIInfBOQ5JrYZEfL87xg7DkCy16NeigtRpvbb uqBM+S5nvTHH1VQGNrID4BWyX+1JeorNrT3t+epIW8ZT0TyLGSBL/uGt95ZHfjWH/XMS jVWBLxGnzYrHd1uKSIz2pinpGM9ZCp3ojCEi6D/waaN4eZneE6AdHIvI/qIuxeIklwnY QuA0Hc1RXSYduhZKlz0yzGw6Y/br0GMug+dJacMnaWVfOQ4ey3376CNHvbDHMH3K+dBo W1BhGiWcfK6JmkDDUsAbi7a8VI0lz67Kak1425bhzOaQKx3NsPZD0fzLJe9p8GKMbTeG 7TKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gxwbg3Vi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id v17-20020a170902d09100b001cc649af5e7si2376513plv.243.2023.11.03.17.04.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 17:04:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gxwbg3Vi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 91D9681BBA29; Fri, 3 Nov 2023 17:04:13 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230453AbjKDAEK (ORCPT + 99 others); Fri, 3 Nov 2023 20:04:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231548AbjKDADj (ORCPT ); Fri, 3 Nov 2023 20:03:39 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72EC710E6 for ; Fri, 3 Nov 2023 17:03:14 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a9012ab0adso37085937b3.1 for ; Fri, 03 Nov 2023 17:03:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699056193; x=1699660993; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=QsiXyox+rGQhiB/Za0Yz/FRQCQ9gMcg4U4njvPOlEGQ=; b=gxwbg3ViatDEwtW2lzlL+SOzbs6wTJK9rxU2fjCkHCht9udbHlNFTLX1VVcN7QclCd /JLEMm9b1P8Gaj/qn1te0rJS6P5qlYYcuHHZqupCbll+BEfYL9NvrgRrwjVUv/grY+NK LOSN60MmpPMwTW1SDuuQrqDPzinzOE1WPUYc4PM/BZNIN2737NirQxz0gQkAxJWBPupq +aeiQw28PZT74af2K9cjpZ5ybeDfpeKO7o2fVDNHnXjXwUgR37vbDkxq70y66uAuxxPm O2Y8BWi+Nubs7EpbmJKOo+2gYr6lyyN4/UcrutBlDXXRw5rvhp+lAAGDymBrv+R8nvXE 2YJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699056193; x=1699660993; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QsiXyox+rGQhiB/Za0Yz/FRQCQ9gMcg4U4njvPOlEGQ=; b=oGvdxy2ngp7FMYA1r6ZRZVTUFQix45GutY0uauJIfTij7r6LhzMJJTNVKAJdlbVtIm b1G9jxZ2x7RWNlwt9cV1uAVb8VMBQRrB6RmKGVNUkqPMBGrNKjx6DRuFlnRo1lUSMu1W zWvojeOoETDxKvxi0tbJAOVKrs8AsuBtl24mw8IkEddWT0U9lAfuHpBC1ScthrDMqR8i NHesFwmU4gWNCydwjEliFhpVxFY8tABEm8rUD+JPJ6rHXD6c9amcDktjndpEU4h6nslm tjkB60TdN8iFbqwlr+kIu5rq9ARbPtpt9457El6DE5WHW0y3eeKSU1uTRwbradmWNBWm JWPQ== X-Gm-Message-State: AOJu0Yw894aLjw+Inpwv9In+nZu3sUqnhdM25EtdOEH4S/JEFi+bM+UB Wt1ZZ0hzbQby2lIRLhDO+DRgGFfIcVQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:84d1:0:b0:da0:cbe9:6bb5 with SMTP id x17-20020a2584d1000000b00da0cbe96bb5mr442124ybm.11.1699056193328; Fri, 03 Nov 2023 17:03:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 3 Nov 2023 17:02:35 -0700 In-Reply-To: <20231104000239.367005-1-seanjc@google.com> Mime-Version: 1.0 References: <20231104000239.367005-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231104000239.367005-18-seanjc@google.com> Subject: [PATCH v6 17/20] KVM: selftests: Add helpers to read integer module params From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Dapeng Mi , Jinrong Liang , Like Xu , Jim Mattson , Aaron Lewis 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_BLOCKED,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 03 Nov 2023 17:04:13 -0700 (PDT) Add helpers to read integer module params, which is painfully non-trivial because the pain of dealing with strings in C is exacerbated by the kernel inserting a newline. Don't bother differentiating between int, uint, short, etc. They all fit in an int, and KVM (thankfully) doesn't have any integer params larger than an int. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/kvm_util_base.h | 4 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 62 +++++++++++++++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index a18db6a7b3cf..46b71241216e 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -238,6 +238,10 @@ bool get_kvm_param_bool(const char *param); bool get_kvm_intel_param_bool(const char *param); bool get_kvm_amd_param_bool(const char *param); +int get_kvm_param_integer(const char *param); +int get_kvm_intel_param_integer(const char *param); +int get_kvm_amd_param_integer(const char *param); + unsigned int kvm_check_cap(long cap); static inline bool kvm_has_cap(long cap) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 7a8af1821f5d..65101c7d1a1a 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -51,13 +51,13 @@ int open_kvm_dev_path_or_exit(void) return _open_kvm_dev_path_or_exit(O_RDONLY); } -static bool get_module_param_bool(const char *module_name, const char *param) +static ssize_t get_module_param(const char *module_name, const char *param, + void *buffer, size_t buffer_size) { const int path_size = 128; char path[path_size]; - char value; - ssize_t r; - int fd; + ssize_t bytes_read; + int fd, r; r = snprintf(path, path_size, "/sys/module/%s/parameters/%s", module_name, param); @@ -66,11 +66,46 @@ static bool get_module_param_bool(const char *module_name, const char *param) fd = open_path_or_exit(path, O_RDONLY); - r = read(fd, &value, 1); - TEST_ASSERT(r == 1, "read(%s) failed", path); + bytes_read = read(fd, buffer, buffer_size); + TEST_ASSERT(bytes_read > 0, "read(%s) returned %ld, wanted %ld bytes", + path, bytes_read, buffer_size); r = close(fd); TEST_ASSERT(!r, "close(%s) failed", path); + return bytes_read; +} + +static int get_module_param_integer(const char *module_name, const char *param) +{ + /* + * 16 bytes to hold a 64-bit value (1 byte per char), 1 byte for the + * NUL char, and 1 byte because the kernel sucks and inserts a newline + * at the end. + */ + char value[16 + 1 + 1]; + ssize_t r; + + memset(value, '\0', sizeof(value)); + + r = get_module_param(module_name, param, value, sizeof(value)); + TEST_ASSERT(value[r - 1] == '\n', + "Expected trailing newline, got char '%c'", value[r - 1]); + + /* + * Squash the newline, otherwise atoi_paranoid() will complain about + * trailing non-NUL characters in the string. + */ + value[r - 1] = '\0'; + return atoi_paranoid(value); +} + +static bool get_module_param_bool(const char *module_name, const char *param) +{ + char value; + ssize_t r; + + r = get_module_param(module_name, param, &value, sizeof(value)); + TEST_ASSERT_EQ(r, 1); if (value == 'Y') return true; @@ -95,6 +130,21 @@ bool get_kvm_amd_param_bool(const char *param) return get_module_param_bool("kvm_amd", param); } +int get_kvm_param_integer(const char *param) +{ + return get_module_param_integer("kvm", param); +} + +int get_kvm_intel_param_integer(const char *param) +{ + return get_module_param_integer("kvm_intel", param); +} + +int get_kvm_amd_param_integer(const char *param) +{ + return get_module_param_integer("kvm_amd", param); +} + /* * Capability * -- 2.42.0.869.gea05f2083d-goog