Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Convert vgic_init away from vm_create_default_with_vcpus()

Use a combination of vm_create(), vm_create_with_vcpus(), and
vm_vcpu_add() to convert vgic_init from vm_create_default_with_vcpus(),
and away from referncing vCPUs by ID.

Thus continues the march toward total annihilation of "default" helpers.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Sean Christopherson and committed by
Paolo Bonzini
45f56808 f3443bed

+51 -31
+51 -31
tools/testing/selftests/kvm/aarch64/vgic_init.c
··· 66 66 } 67 67 68 68 /* we don't want to assert on run execution, hence that helper */ 69 - static int run_vcpu(struct kvm_vm *vm, uint32_t vcpuid) 69 + static int run_vcpu(struct kvm_vcpu *vcpu) 70 70 { 71 - ucall_init(vm, NULL); 71 + ucall_init(vcpu->vm, NULL); 72 72 73 - return __vcpu_run(vm, vcpuid) ? -errno : 0; 73 + return __vcpu_run(vcpu->vm, vcpu->id) ? -errno : 0; 74 74 } 75 75 76 - static struct vm_gic vm_gic_create_with_vcpus(uint32_t gic_dev_type, uint32_t nr_vcpus) 76 + static struct vm_gic vm_gic_create_with_vcpus(uint32_t gic_dev_type, 77 + uint32_t nr_vcpus, 78 + struct kvm_vcpu *vcpus[]) 77 79 { 78 80 struct vm_gic v; 79 81 80 82 v.gic_dev_type = gic_dev_type; 81 - v.vm = vm_create_default_with_vcpus(nr_vcpus, 0, 0, guest_code, NULL); 83 + v.vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus); 82 84 v.gic_fd = kvm_create_device(v.vm, gic_dev_type); 83 85 84 86 return v; ··· 324 322 */ 325 323 static void test_vgic_then_vcpus(uint32_t gic_dev_type) 326 324 { 325 + struct kvm_vcpu *vcpus[NR_VCPUS]; 327 326 struct vm_gic v; 328 327 int ret, i; 329 328 330 - v = vm_gic_create_with_vcpus(gic_dev_type, 1); 329 + v = vm_gic_create_with_vcpus(gic_dev_type, 1, vcpus); 331 330 332 331 subtest_dist_rdist(&v); 333 332 334 333 /* Add the rest of the VCPUs */ 335 334 for (i = 1; i < NR_VCPUS; ++i) 336 - vm_vcpu_add(v.vm, i, guest_code); 335 + vcpus[i] = vm_vcpu_add(v.vm, i, guest_code); 337 336 338 - ret = run_vcpu(v.vm, 3); 337 + ret = run_vcpu(vcpus[3]); 339 338 TEST_ASSERT(ret == -EINVAL, "dist/rdist overlap detected on 1st vcpu run"); 340 339 341 340 vm_gic_destroy(&v); ··· 345 342 /* All the VCPUs are created before the VGIC KVM device gets initialized */ 346 343 static void test_vcpus_then_vgic(uint32_t gic_dev_type) 347 344 { 345 + struct kvm_vcpu *vcpus[NR_VCPUS]; 348 346 struct vm_gic v; 349 347 int ret; 350 348 351 - v = vm_gic_create_with_vcpus(gic_dev_type, NR_VCPUS); 349 + v = vm_gic_create_with_vcpus(gic_dev_type, NR_VCPUS, vcpus); 352 350 353 351 subtest_dist_rdist(&v); 354 352 355 - ret = run_vcpu(v.vm, 3); 353 + ret = run_vcpu(vcpus[3]); 356 354 TEST_ASSERT(ret == -EINVAL, "dist/rdist overlap detected on 1st vcpu run"); 357 355 358 356 vm_gic_destroy(&v); ··· 361 357 362 358 static void test_v3_new_redist_regions(void) 363 359 { 360 + struct kvm_vcpu *vcpus[NR_VCPUS]; 364 361 void *dummy = NULL; 365 362 struct vm_gic v; 366 363 uint64_t addr; 367 364 int ret; 368 365 369 - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS); 366 + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus); 370 367 subtest_v3_redist_regions(&v); 371 368 kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 372 369 KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); 373 370 374 - ret = run_vcpu(v.vm, 3); 371 + ret = run_vcpu(vcpus[3]); 375 372 TEST_ASSERT(ret == -ENXIO, "running without sufficient number of rdists"); 376 373 vm_gic_destroy(&v); 377 374 378 375 /* step2 */ 379 376 380 - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS); 377 + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus); 381 378 subtest_v3_redist_regions(&v); 382 379 383 380 addr = REDIST_REGION_ATTR_ADDR(1, 0x280000, 0, 2); 384 381 kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, 385 382 KVM_VGIC_V3_ADDR_TYPE_REDIST_REGION, &addr); 386 383 387 - ret = run_vcpu(v.vm, 3); 384 + ret = run_vcpu(vcpus[3]); 388 385 TEST_ASSERT(ret == -EBUSY, "running without vgic explicit init"); 389 386 390 387 vm_gic_destroy(&v); 391 388 392 389 /* step 3 */ 393 390 394 - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS); 391 + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus); 395 392 subtest_v3_redist_regions(&v); 396 393 397 394 ret = __kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_ADDR, ··· 407 402 kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 408 403 KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); 409 404 410 - ret = run_vcpu(v.vm, 3); 405 + ret = run_vcpu(vcpus[3]); 411 406 TEST_ASSERT(!ret, "vcpu run"); 412 407 413 408 vm_gic_destroy(&v); ··· 419 414 uint64_t addr; 420 415 int ret, i; 421 416 422 - v.vm = vm_create_default(0, 0, guest_code); 417 + v.vm = vm_create(DEFAULT_GUEST_PHY_PAGES); 418 + (void)vm_vcpu_add(v.vm, 0, guest_code); 423 419 424 420 v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3); 425 421 426 - vm_vcpu_add(v.vm, 3, guest_code); 422 + (void)vm_vcpu_add(v.vm, 3, guest_code); 427 423 428 424 v3_redist_reg_get_errno(v.gic_fd, 1, GICR_TYPER, EINVAL, 429 425 "attempting to read GICR_TYPER of non created vcpu"); 430 426 431 - vm_vcpu_add(v.vm, 1, guest_code); 427 + (void)vm_vcpu_add(v.vm, 1, guest_code); 432 428 433 429 v3_redist_reg_get_errno(v.gic_fd, 1, GICR_TYPER, EBUSY, 434 430 "read GICR_TYPER before GIC initialized"); 435 431 436 - vm_vcpu_add(v.vm, 2, guest_code); 432 + (void)vm_vcpu_add(v.vm, 2, guest_code); 437 433 438 434 kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 439 435 KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); ··· 473 467 vm_gic_destroy(&v); 474 468 } 475 469 470 + static struct vm_gic vm_gic_v3_create_with_vcpuids(int nr_vcpus, 471 + uint32_t vcpuids[]) 472 + { 473 + struct vm_gic v; 474 + int i; 475 + 476 + v.vm = vm_create(DEFAULT_GUEST_PHY_PAGES); 477 + for (i = 0; i < nr_vcpus; i++) 478 + vm_vcpu_add(v.vm, vcpuids[i], guest_code); 479 + 480 + v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3); 481 + 482 + return v; 483 + } 484 + 476 485 /** 477 486 * Test GICR_TYPER last bit with new redist regions 478 487 * rdist regions #1 and #2 are contiguous ··· 504 483 struct vm_gic v; 505 484 uint64_t addr; 506 485 507 - v.vm = vm_create_default_with_vcpus(6, 0, 0, guest_code, vcpuids); 508 - 509 - v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3); 486 + v = vm_gic_v3_create_with_vcpuids(ARRAY_SIZE(vcpuids), vcpuids); 510 487 511 488 kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 512 489 KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); ··· 538 519 struct vm_gic v; 539 520 uint64_t addr; 540 521 541 - v.vm = vm_create_default_with_vcpus(6, 0, 0, guest_code, vcpuids); 542 - 543 - v.gic_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_V3); 522 + v = vm_gic_v3_create_with_vcpuids(ARRAY_SIZE(vcpuids), vcpuids); 544 523 545 524 kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 546 525 KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); ··· 559 542 /* Uses the legacy REDIST region API. */ 560 543 static void test_v3_redist_ipa_range_check_at_vcpu_run(void) 561 544 { 545 + struct kvm_vcpu *vcpus[NR_VCPUS]; 562 546 struct vm_gic v; 563 547 int ret, i; 564 548 uint64_t addr; 565 549 566 - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, 1); 550 + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, 1, vcpus); 567 551 568 552 /* Set space for 3 redists, we have 1 vcpu, so this succeeds. */ 569 553 addr = max_phys_size - (3 * 2 * 0x10000); ··· 577 559 578 560 /* Add the rest of the VCPUs */ 579 561 for (i = 1; i < NR_VCPUS; ++i) 580 - vm_vcpu_add(v.vm, i, guest_code); 562 + vcpus[i] = vm_vcpu_add(v.vm, i, guest_code); 581 563 582 564 kvm_device_attr_set(v.gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, 583 565 KVM_DEV_ARM_VGIC_CTRL_INIT, NULL); 584 566 585 567 /* Attempt to run a vcpu without enough redist space. */ 586 - ret = run_vcpu(v.vm, 2); 568 + ret = run_vcpu(vcpus[2]); 587 569 TEST_ASSERT(ret && errno == EINVAL, 588 570 "redist base+size above PA range detected on 1st vcpu run"); 589 571 ··· 592 574 593 575 static void test_v3_its_region(void) 594 576 { 577 + struct kvm_vcpu *vcpus[NR_VCPUS]; 595 578 struct vm_gic v; 596 579 uint64_t addr; 597 580 int its_fd, ret; 598 581 599 - v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS); 582 + v = vm_gic_create_with_vcpus(KVM_DEV_TYPE_ARM_VGIC_V3, NR_VCPUS, vcpus); 600 583 its_fd = kvm_create_device(v.vm, KVM_DEV_TYPE_ARM_VGIC_ITS); 601 584 602 585 addr = 0x401000; ··· 637 618 */ 638 619 int test_kvm_device(uint32_t gic_dev_type) 639 620 { 621 + struct kvm_vcpu *vcpus[NR_VCPUS]; 640 622 struct vm_gic v; 641 623 uint32_t other; 642 624 int ret; 643 625 644 - v.vm = vm_create_default_with_vcpus(NR_VCPUS, 0, 0, guest_code, NULL); 626 + v.vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus); 645 627 646 628 /* try to create a non existing KVM device */ 647 629 ret = __kvm_test_create_device(v.vm, 0);