Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1406110rdb; Fri, 1 Dec 2023 16:07:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IEuXIGLY8flkxgWnYG/gOHFpJE1OviUz73LrmiTrUWT9fLbThrrcWc9sVZmF64rXGw7QWXN X-Received: by 2002:a05:6870:7e12:b0:1fa:bd0d:14f7 with SMTP id wx18-20020a0568707e1200b001fabd0d14f7mr549087oab.39.1701475633518; Fri, 01 Dec 2023 16:07:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701475633; cv=none; d=google.com; s=arc-20160816; b=GxfwW4f6pz6hXFrz8QJgPpBAWpGW7OHfhqaO/7gOTLG38hZOkPFLLOB0fU8hH+BAbn Fslk07Nm/ny+72uAdh9XR5t1moI37NXoEO6RKwWQc43UxlSW3RRZwTiw291aD0dlffxU HtlUywIYs7PA5jIbMk5KuqusTfC58Ioumes33dT964g2Rox7D0Ew3wMpixX2pZFcvJIC qoqYV1GgHVDmtvEFSjgNTLFS/1SwfSF9RJtjMkDcLDwwkHCV31kvXILEQMr2YeI9w0wL dd03lbGW/Ak9P81D/3U1baMtO04TCN/RWohjKbzLnHKRD5ArVeBjzc7f3Ik348khdSRE fNbQ== 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=o4Hn9d5ofhE1GJj3G12PpMWX7+kcX8n0WNzzYeEBc4A=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=pYrT8iFAf8LOkmzhV1vYH/jDyv9h4GQU7qtTQns6tmZFVL1PbpKylw48AYzP9qZCP6 VHK4vmdaooGG/MaF3yid5k6qI8Sw/CpiCE7fnfxGK58nsFMEpMW2GiV7mfMn/Fx9uvzy bmA1SD2w/3KZ1CV6duGeUCoxzY2UPqRKUgzbstv9EfCfDvxvcWQdM2OxmPyvXBNbP7gv yviRb89EBaDs/b0Eua5sYU6NTonMeJ5HS+/vJ1yXFISCRvhz2TxEnULw7UOzdC6uZ+nR Czg42zeI69LDOvThOXrs6EHgHxm6aXuUC+8mm+5auXDA1bw6jL3d7o4uh9FrYkbegLPp AmmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=a0yc4Rex; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id k4-20020a17090a3e8400b00279866aa14csi4064563pjc.16.2023.12.01.16.07.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 16:07:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=a0yc4Rex; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id 8C95C80E9964; Fri, 1 Dec 2023 16:06:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442090AbjLBAGM (ORCPT + 99 others); Fri, 1 Dec 2023 19:06:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442092AbjLBAFY (ORCPT ); Fri, 1 Dec 2023 19:05:24 -0500 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 ADE3F1996 for ; Fri, 1 Dec 2023 16:05:03 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d351694be7so42037657b3.3 for ; Fri, 01 Dec 2023 16:05:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701475503; x=1702080303; 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=o4Hn9d5ofhE1GJj3G12PpMWX7+kcX8n0WNzzYeEBc4A=; b=a0yc4Rex3MoouOFIWOZKCcYuyd/kTZjnFweVpGCdVLO3DFs3lJA/0j7EtoPQc4aNoU mNO/ISX6b2dXzXjaxZImfkxF87+vcwrzeYQYQNFe0qzy93uAC76LLIEcDO8hui0ktkDX Hs/LrlIN9T9aynAkhHfrWfl+0YtaK5ew0xFJ9LPevm2d18p8Rbl0NgAVicMnkS+0D16v VIoEWC5umWUUAb5sofokEMO/KIgLRXcipArwLDvh3Fhm9f6oS4WDcQT9LSQAw8IPMFfp mySjVlrA6c3oMzhT7Yr+wkYaIGDq8pmbatcZFEfLvJQzN59A9urDZWxLeUmJn3q1zxcT PjqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701475503; x=1702080303; 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=o4Hn9d5ofhE1GJj3G12PpMWX7+kcX8n0WNzzYeEBc4A=; b=irc0C+agqlkgZuihDDDtMQaRgEbIopRr1z+EzEo5f/OZhqCBgQL6URdxngHGfgV/Jg WN6gy/0uulOpwig9N7lKOzU4dhcnUUfc3d8/3MglB57AfEnsETgmuPog5tT2xzdFwleR D6UHK3qS9z1c0zXcRYyiDL7kYy8UWLMhuouuu0fJoAs+PH2F0mjG7rph6jFCtVQ1g9oY djFsnAJOAbYDko//SAj4ysDqIq4+j3cAmufqfjY/S7EOJHBz3N/Chtw+3vedFWQhLOMk O3sh/XLp+D1/X6vdLpCzHpP0BHw4xPytzjMaB0gfsvw7UP/UMBxP7ss89bhR81Tj4mFu l35Q== X-Gm-Message-State: AOJu0YzrhvFyb/D9omwqNin038QrtqIXt1enuy3x9Oi61FWs/nHX0ddf b2NvWX+KIUO4qFXq01LwYbypBsialEs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:57d4:0:b0:5d3:a348:b0bd with SMTP id l203-20020a8157d4000000b005d3a348b0bdmr197529ywb.6.1701475502927; Fri, 01 Dec 2023 16:05:02 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 1 Dec 2023 16:04:11 -0800 In-Reply-To: <20231202000417.922113-1-seanjc@google.com> Mime-Version: 1.0 References: <20231202000417.922113-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231202000417.922113-23-seanjc@google.com> Subject: [PATCH v9 22/28] 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 , Jim Mattson , Jinrong Liang , Aaron Lewis , Like Xu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 pete.vger.email 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 (pete.vger.email [0.0.0.0]); Fri, 01 Dec 2023 16:06:56 -0800 (PST) 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 e0da036a13ae..4293af2cb28c 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -257,6 +257,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 17a978b8a2c4..878bdbc8e618 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.43.0.rc2.451.g8631bc7472-goog