Skip to content

Commit 6313e09

Browse files
committed
KVM: selftests: Zero-initialize entire test_result in memslot perf test
Zero-initialize the entire test_result structure used by memslot_perf_test instead of zeroing only the fields used to guard the pr_info() calls. gcc 13.2.0 is a bit overzealous and incorrectly thinks that rbestslottime's slot_runtime may be used uninitialized. In file included from memslot_perf_test.c:25: memslot_perf_test.c: In function ‘main’: include/test_util.h:31:22: error: ‘rbestslottime.slot_runtime.tv_nsec’ may be used uninitialized [-Werror=maybe-uninitialized] 31 | #define pr_info(...) printf(__VA_ARGS__) | ^~~~~~~~~~~~~~~~~~~ memslot_perf_test.c:1127:17: note: in expansion of macro ‘pr_info’ 1127 | pr_info("Best slot setup time for the whole test area was %ld.%.9lds\n", | ^~~~~~~ memslot_perf_test.c:1092:28: note: ‘rbestslottime.slot_runtime.tv_nsec’ was declared here 1092 | struct test_result rbestslottime; | ^~~~~~~~~~~~~ include/test_util.h:31:22: error: ‘rbestslottime.slot_runtime.tv_sec’ may be used uninitialized [-Werror=maybe-uninitialized] 31 | #define pr_info(...) printf(__VA_ARGS__) | ^~~~~~~~~~~~~~~~~~~ memslot_perf_test.c:1127:17: note: in expansion of macro ‘pr_info’ 1127 | pr_info("Best slot setup time for the whole test area was %ld.%.9lds\n", | ^~~~~~~ memslot_perf_test.c:1092:28: note: ‘rbestslottime.slot_runtime.tv_sec’ was declared here 1092 | struct test_result rbestslottime; | ^~~~~~~~~~~~~ That can't actually happen, at least not without the "result" structure in test_loop() also being used uninitialized, which gcc doesn't complain about, as writes to rbestslottime are all-or-nothing, i.e. slottimens can't be non-zero without slot_runtime being written. if (!data->mem_size && (!rbestslottime->slottimens || result.slottimens < rbestslottime->slottimens)) *rbestslottime = result; Zero-initialize the structures to make gcc happy even though this is likely a compiler bug. The cost to do so is negligible, both in terms of code and runtime overhead. The only downside is that the compiler won't warn about legitimate usage of "uninitialized" data, e.g. the test could end up consuming zeros instead of useful data. However, given that the test is quite mature and unlikely to see substantial changes, the odds of introducing such bugs are relatively low, whereas being able to compile KVM selftests with -Werror detects issues on a regular basis. Reviewed-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Link: https://lore.kernel.org/r/20231005002954.2887098-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 332c4d9 commit 6313e09

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

tools/testing/selftests/kvm/memslot_perf_test.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,9 +1033,8 @@ static bool test_loop(const struct test_data *data,
10331033
struct test_result *rbestruntime)
10341034
{
10351035
uint64_t maxslots;
1036-
struct test_result result;
1036+
struct test_result result = {};
10371037

1038-
result.nloops = 0;
10391038
if (!test_execute(targs->nslots, &maxslots, targs->seconds, data,
10401039
&result.nloops,
10411040
&result.slot_runtime, &result.guest_runtime)) {
@@ -1089,7 +1088,7 @@ int main(int argc, char *argv[])
10891088
.seconds = 5,
10901089
.runs = 1,
10911090
};
1092-
struct test_result rbestslottime;
1091+
struct test_result rbestslottime = {};
10931092
int tctr;
10941093

10951094
if (!check_memory_sizes())
@@ -1098,19 +1097,17 @@ int main(int argc, char *argv[])
10981097
if (!parse_args(argc, argv, &targs))
10991098
return -1;
11001099

1101-
rbestslottime.slottimens = 0;
11021100
for (tctr = targs.tfirst; tctr <= targs.tlast; tctr++) {
11031101
const struct test_data *data = &tests[tctr];
11041102
unsigned int runctr;
1105-
struct test_result rbestruntime;
1103+
struct test_result rbestruntime = {};
11061104

11071105
if (tctr > targs.tfirst)
11081106
pr_info("\n");
11091107

11101108
pr_info("Testing %s performance with %i runs, %d seconds each\n",
11111109
data->name, targs.runs, targs.seconds);
11121110

1113-
rbestruntime.runtimens = 0;
11141111
for (runctr = 0; runctr < targs.runs; runctr++)
11151112
if (!test_loop(data, &targs,
11161113
&rbestslottime, &rbestruntime))

0 commit comments

Comments
 (0)