Skip to content

Commit 3439643

Browse files
Gavin ShanMarc Zyngier
authored andcommitted
KVM: selftests: memslot_perf_test: Probe memory slots for once
prepare_vm() is called in every iteration and run. The allowed memory slots (KVM_CAP_NR_MEMSLOTS) are probed for multiple times. It's not free and unnecessary. Move the probing logic for the allowed memory slots to parse_args() for once, which is upper layer of prepare_vm(). No functional change intended. Signed-off-by: Gavin Shan <[email protected]> Reviewed-by: Maciej S. Szmigiero <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 2aae5e6 commit 3439643

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

tools/testing/selftests/kvm/memslot_perf_test.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -245,27 +245,17 @@ static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots,
245245
void *guest_code, uint64_t mempages,
246246
struct timespec *slot_runtime)
247247
{
248-
uint32_t max_mem_slots;
249248
uint64_t rempages;
250249
uint64_t guest_addr;
251250
uint32_t slot;
252251
struct timespec tstart;
253252
struct sync_area *sync;
254253

255-
max_mem_slots = kvm_check_cap(KVM_CAP_NR_MEMSLOTS);
256-
TEST_ASSERT(max_mem_slots > 1,
257-
"KVM_CAP_NR_MEMSLOTS should be greater than 1");
258-
TEST_ASSERT(nslots > 1 || nslots == -1,
259-
"Slot count cap should be greater than 1");
260-
if (nslots != -1)
261-
max_mem_slots = min(max_mem_slots, (uint32_t)nslots);
262-
pr_info_v("Allowed number of memory slots: %"PRIu32"\n", max_mem_slots);
263-
264254
TEST_ASSERT(mempages > 1,
265255
"Can't test without any memory");
266256

267257
data->npages = mempages;
268-
data->nslots = max_mem_slots - 1;
258+
data->nslots = nslots;
269259
data->pages_per_slot = mempages / data->nslots;
270260
if (!data->pages_per_slot) {
271261
*maxslots = mempages + 1;
@@ -869,6 +859,7 @@ static void help(char *name, struct test_args *targs)
869859
static bool parse_args(int argc, char *argv[],
870860
struct test_args *targs)
871861
{
862+
uint32_t max_mem_slots;
872863
int opt;
873864

874865
while ((opt = getopt(argc, argv, "hvds:f:e:l:r:")) != -1) {
@@ -885,8 +876,8 @@ static bool parse_args(int argc, char *argv[],
885876
break;
886877
case 's':
887878
targs->nslots = atoi(optarg);
888-
if (targs->nslots <= 0 && targs->nslots != -1) {
889-
pr_info("Slot count cap has to be positive or -1 for no cap\n");
879+
if (targs->nslots <= 1 && targs->nslots != -1) {
880+
pr_info("Slot count cap must be larger than 1 or -1 for no cap\n");
890881
return false;
891882
}
892883
break;
@@ -932,6 +923,21 @@ static bool parse_args(int argc, char *argv[],
932923
return false;
933924
}
934925

926+
max_mem_slots = kvm_check_cap(KVM_CAP_NR_MEMSLOTS);
927+
if (max_mem_slots <= 1) {
928+
pr_info("KVM_CAP_NR_MEMSLOTS should be greater than 1\n");
929+
return false;
930+
}
931+
932+
/* Memory slot 0 is reserved */
933+
if (targs->nslots == -1)
934+
targs->nslots = max_mem_slots - 1;
935+
else
936+
targs->nslots = min_t(int, targs->nslots, max_mem_slots) - 1;
937+
938+
pr_info_v("Allowed Number of memory slots: %"PRIu32"\n",
939+
targs->nslots + 1);
940+
935941
return true;
936942
}
937943

0 commit comments

Comments
 (0)