From patchwork Tue Sep 6 18:09:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Koller X-Patchwork-Id: 12968005 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F097ECAAA1 for ; Tue, 6 Sep 2022 18:10:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229802AbiIFSKQ (ORCPT ); Tue, 6 Sep 2022 14:10:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229749AbiIFSKE (ORCPT ); Tue, 6 Sep 2022 14:10:04 -0400 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 18B5785FD5 for ; Tue, 6 Sep 2022 11:09:58 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-345482ec6adso41131307b3.18 for ; Tue, 06 Sep 2022 11:09:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=GXyzgnGlMnDyr6jDldAtcQlrUJka0cbmkJbuqj5LpV4=; b=p6fpXW5JHNNYawSyrwmiqzoEy0jsuFom0Q0HhVjw1McwKQqqD2uhVb7G8zF+ZCF32B rJIxYxleLHMiUVsIvHWOKYpqFX+COdtZSEiQoT+lRqt1jkckXeeaXxUnrQa6E8NmX7ML HI7ndYlM1IL2gtNZMWR1kVM9FWJw25XYsCWgDgOzDKNgOY4EVZx+rOMZqxalDSRjtGOF lkTFi1wWESNwYsa8PAbBaU50ZLrc3ndrxy4a6I7UN3EYuzqab9JJ1YlwksVu5rjQwnl9 x4WIfsBUuBNPu6PiaQ2fbyoi0KFu+JXVfINnvKV6m8lHQm+w6A2s3HQaDAfMheaPBtUg zzQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=GXyzgnGlMnDyr6jDldAtcQlrUJka0cbmkJbuqj5LpV4=; b=09IXMKshulLGydNK8DPs+FBdZN7QUw0HSLkh5Bp4WtXgyVgABpVgXRMss2fnt2Vs/D C5aCa/GAF+jvRDm4HxF6GFd5fwrdw76xXOp41/4Ugjqr4StBitMTZU0AzWqR6j8/GfDH gNrjiKN1garmD4SS7eTJ/nfEjMYPbS5sg6VfepdkhejunwWvqr1fCorcse/QhlEmUqje qWbaSW3YAoDbeXLC07IggLnPkMQfp+LyMQs/kB0BxaKliykajZjx590MWGp0S4Q3hN4a 3eJf7Jr6yORCEGa4EBL683jbnYTI/+ekuYu+Fdm5Zlf+w5+IOjVIVdWmHF7NG9KyooBe ETiQ== X-Gm-Message-State: ACgBeo2lnnklh124vqBdZ6xDqBa75g7V0BpPE4IA/q6YrRDk9eEKzI6H /VgB8A+lzEjGjmhFxQf3FNHPUnSm9GtSDJjOYYR3/x1CHMGnh9VHZw8wH4kutRzuguQEXflG8Nv 6DK+sHnYLC72FgniDLhNBa0wTvul2v7z7a1Bns1+yG9mISKU1FEaDkUJlbG034hw= X-Google-Smtp-Source: AA6agR6dt/m/DW6IyOB3sq8vNkk6/O99Psm2qDwJwW0NzDs6Kl+pyaeY8X+6TB4JBfhZWkEuXvx5RRp1BsXi/Q== X-Received: from ricarkol4.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1248]) (user=ricarkol job=sendgmr) by 2002:a25:37d2:0:b0:6a9:a7ad:a22 with SMTP id e201-20020a2537d2000000b006a9a7ad0a22mr2495732yba.185.1662487797263; Tue, 06 Sep 2022 11:09:57 -0700 (PDT) Date: Tue, 6 Sep 2022 18:09:28 +0000 In-Reply-To: <20220906180930.230218-1-ricarkol@google.com> Mime-Version: 1.0 References: <20220906180930.230218-1-ricarkol@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220906180930.230218-12-ricarkol@google.com> Subject: [PATCH v6 11/13] KVM: selftests: aarch64: Add dirty logging tests into page_fault_test From: Ricardo Koller To: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, andrew.jones@linux.dev Cc: pbonzini@redhat.com, maz@kernel.org, seanjc@google.com, alexandru.elisei@arm.com, eric.auger@redhat.com, oupton@google.com, reijiw@google.com, rananta@google.com, bgardon@google.com, dmatlack@google.com, axelrasmussen@google.com, Ricardo Koller Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add some dirty logging tests into page_fault_test. Mark the data and/or page-table memslots for dirty logging, perform some accesses, and check that the dirty log bits are set or clean when expected. Signed-off-by: Ricardo Koller --- .../selftests/kvm/aarch64/page_fault_test.c | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tools/testing/selftests/kvm/aarch64/page_fault_test.c b/tools/testing/selftests/kvm/aarch64/page_fault_test.c index 57466d213b95..481c96eec34a 100644 --- a/tools/testing/selftests/kvm/aarch64/page_fault_test.c +++ b/tools/testing/selftests/kvm/aarch64/page_fault_test.c @@ -31,6 +31,11 @@ static uint64_t *guest_test_memory = (uint64_t *)TEST_GVA; #define CMD_SKIP_TEST (1ULL << 1) #define CMD_HOLE_PT (1ULL << 2) #define CMD_HOLE_DATA (1ULL << 3) +#define CMD_CHECK_WRITE_IN_DIRTY_LOG (1ULL << 4) +#define CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG (1ULL << 5) +#define CMD_CHECK_NO_WRITE_IN_DIRTY_LOG (1ULL << 6) +#define CMD_CHECK_NO_S1PTW_WR_IN_DIRTY_LOG (1ULL << 7) +#define CMD_SET_PTE_AF (1ULL << 8) #define PREPARE_FN_NR 10 #define CHECK_FN_NR 10 @@ -213,6 +218,21 @@ static void guest_check_pte_af(void) GUEST_ASSERT_EQ(*((uint64_t *)TEST_PTE_GVA) & PTE_AF, PTE_AF); } +static void guest_check_write_in_dirty_log(void) +{ + GUEST_SYNC(CMD_CHECK_WRITE_IN_DIRTY_LOG); +} + +static void guest_check_no_write_in_dirty_log(void) +{ + GUEST_SYNC(CMD_CHECK_NO_WRITE_IN_DIRTY_LOG); +} + +static void guest_check_s1ptw_wr_in_dirty_log(void) +{ + GUEST_SYNC(CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG); +} + static void guest_exec(void) { int (*code)(void) = (int (*)(void))TEST_EXEC_GVA; @@ -398,6 +418,21 @@ static bool punch_hole_in_memslot(struct kvm_vm *vm, return true; } +static bool check_write_in_dirty_log(struct kvm_vm *vm, + struct userspace_mem_region *region, uint64_t host_pg_nr) +{ + unsigned long *bmap; + bool first_page_dirty; + uint64_t size = region->region.memory_size; + + /* getpage_size() is not always equal to vm->page_size */ + bmap = bitmap_zalloc(size / getpagesize()); + kvm_vm_get_dirty_log(vm, region->region.slot, bmap); + first_page_dirty = test_bit(host_pg_nr, bmap); + free(bmap); + return first_page_dirty; +} + /* Returns true to continue the test, and false if it should be skipped. */ static bool handle_cmd(struct kvm_vm *vm, int cmd) { @@ -414,6 +449,18 @@ static bool handle_cmd(struct kvm_vm *vm, int cmd) continue_test = punch_hole_in_memslot(vm, pt_region); if (cmd & CMD_HOLE_DATA) continue_test = punch_hole_in_memslot(vm, data_region); + if (cmd & CMD_CHECK_WRITE_IN_DIRTY_LOG) + TEST_ASSERT(check_write_in_dirty_log(vm, data_region, 0), + "Missing write in dirty log"); + if (cmd & CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG) + TEST_ASSERT(check_write_in_dirty_log(vm, pt_region, 0), + "Missing s1ptw write in dirty log"); + if (cmd & CMD_CHECK_NO_WRITE_IN_DIRTY_LOG) + TEST_ASSERT(!check_write_in_dirty_log(vm, data_region, 0), + "Unexpected write in dirty log"); + if (cmd & CMD_CHECK_NO_S1PTW_WR_IN_DIRTY_LOG) + TEST_ASSERT(!check_write_in_dirty_log(vm, pt_region, 0), + "Unexpected s1ptw write in dirty log"); return continue_test; } @@ -702,6 +749,19 @@ static void help(char *name) .expected_events = { .uffd_faults = _uffd_faults, }, \ } +#define TEST_DIRTY_LOG(_access, _with_af, _test_check) \ +{ \ + .name = SCAT3(dirty_log, _access, _with_af), \ + .data_memslot_flags = KVM_MEM_LOG_DIRTY_PAGES, \ + .pt_memslot_flags = KVM_MEM_LOG_DIRTY_PAGES, \ + .guest_prepare = { _PREPARE(_with_af), \ + _PREPARE(_access) }, \ + .guest_test = _access, \ + .guest_test_check = { _CHECK(_with_af), _test_check, \ + guest_check_s1ptw_wr_in_dirty_log}, \ + .expected_events = { 0 }, \ +} + static struct test_desc tests[] = { /* Check that HW is setting the Access Flag (AF) (sanity checks). */ @@ -755,6 +815,21 @@ static struct test_desc tests[] = { TEST_UFFD(guest_exec, with_af, CMD_HOLE_DATA | CMD_HOLE_PT, uffd_data_read_handler, uffd_pt_write_handler, 2), + /* + * Try accesses when the data and PT memslots are both tracked for + * dirty logging. + */ + TEST_DIRTY_LOG(guest_read64, with_af, guest_check_no_write_in_dirty_log), + /* no_af should also lead to a PT write. */ + TEST_DIRTY_LOG(guest_read64, no_af, guest_check_no_write_in_dirty_log), + TEST_DIRTY_LOG(guest_ld_preidx, with_af, guest_check_no_write_in_dirty_log), + TEST_DIRTY_LOG(guest_at, no_af, guest_check_no_write_in_dirty_log), + TEST_DIRTY_LOG(guest_exec, with_af, guest_check_no_write_in_dirty_log), + TEST_DIRTY_LOG(guest_write64, with_af, guest_check_write_in_dirty_log), + TEST_DIRTY_LOG(guest_cas, with_af, guest_check_write_in_dirty_log), + TEST_DIRTY_LOG(guest_dc_zva, with_af, guest_check_write_in_dirty_log), + TEST_DIRTY_LOG(guest_st_preidx, with_af, guest_check_write_in_dirty_log), + { 0 } };