Skip to content

Commit 45e4755

Browse files
committed
KVM: selftests: Add helpers to read integer module params
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. Tested-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Link: https://lore.kernel.org/r/20240109230250.424295-24-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent c85e986 commit 45e4755

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

tools/testing/selftests/kvm/include/kvm_util_base.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ bool get_kvm_param_bool(const char *param);
259259
bool get_kvm_intel_param_bool(const char *param);
260260
bool get_kvm_amd_param_bool(const char *param);
261261

262+
int get_kvm_param_integer(const char *param);
263+
int get_kvm_intel_param_integer(const char *param);
264+
int get_kvm_amd_param_integer(const char *param);
265+
262266
unsigned int kvm_check_cap(long cap);
263267

264268
static inline bool kvm_has_cap(long cap)

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ int open_kvm_dev_path_or_exit(void)
5151
return _open_kvm_dev_path_or_exit(O_RDONLY);
5252
}
5353

54-
static bool get_module_param_bool(const char *module_name, const char *param)
54+
static ssize_t get_module_param(const char *module_name, const char *param,
55+
void *buffer, size_t buffer_size)
5556
{
5657
const int path_size = 128;
5758
char path[path_size];
58-
char value;
59-
ssize_t r;
60-
int fd;
59+
ssize_t bytes_read;
60+
int fd, r;
6161

6262
r = snprintf(path, path_size, "/sys/module/%s/parameters/%s",
6363
module_name, param);
@@ -66,11 +66,46 @@ static bool get_module_param_bool(const char *module_name, const char *param)
6666

6767
fd = open_path_or_exit(path, O_RDONLY);
6868

69-
r = read(fd, &value, 1);
70-
TEST_ASSERT(r == 1, "read(%s) failed", path);
69+
bytes_read = read(fd, buffer, buffer_size);
70+
TEST_ASSERT(bytes_read > 0, "read(%s) returned %ld, wanted %ld bytes",
71+
path, bytes_read, buffer_size);
7172

7273
r = close(fd);
7374
TEST_ASSERT(!r, "close(%s) failed", path);
75+
return bytes_read;
76+
}
77+
78+
static int get_module_param_integer(const char *module_name, const char *param)
79+
{
80+
/*
81+
* 16 bytes to hold a 64-bit value (1 byte per char), 1 byte for the
82+
* NUL char, and 1 byte because the kernel sucks and inserts a newline
83+
* at the end.
84+
*/
85+
char value[16 + 1 + 1];
86+
ssize_t r;
87+
88+
memset(value, '\0', sizeof(value));
89+
90+
r = get_module_param(module_name, param, value, sizeof(value));
91+
TEST_ASSERT(value[r - 1] == '\n',
92+
"Expected trailing newline, got char '%c'", value[r - 1]);
93+
94+
/*
95+
* Squash the newline, otherwise atoi_paranoid() will complain about
96+
* trailing non-NUL characters in the string.
97+
*/
98+
value[r - 1] = '\0';
99+
return atoi_paranoid(value);
100+
}
101+
102+
static bool get_module_param_bool(const char *module_name, const char *param)
103+
{
104+
char value;
105+
ssize_t r;
106+
107+
r = get_module_param(module_name, param, &value, sizeof(value));
108+
TEST_ASSERT_EQ(r, 1);
74109

75110
if (value == 'Y')
76111
return true;
@@ -95,6 +130,21 @@ bool get_kvm_amd_param_bool(const char *param)
95130
return get_module_param_bool("kvm_amd", param);
96131
}
97132

133+
int get_kvm_param_integer(const char *param)
134+
{
135+
return get_module_param_integer("kvm", param);
136+
}
137+
138+
int get_kvm_intel_param_integer(const char *param)
139+
{
140+
return get_module_param_integer("kvm_intel", param);
141+
}
142+
143+
int get_kvm_amd_param_integer(const char *param)
144+
{
145+
return get_module_param_integer("kvm_amd", param);
146+
}
147+
98148
/*
99149
* Capability
100150
*

0 commit comments

Comments
 (0)