From patchwork Thu Mar 16 00:30:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 1757652 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20210112 header.b=DFkZM4Wx; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PcSrP3l9vz247P for ; Thu, 16 Mar 2023 11:32:04 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pcbWg-00042M-Lo; Wed, 15 Mar 2023 20:31:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3WWMSZAsKCpAuw4yB5yID7008805y.w86Ay6E-xyFy578707E.8B0@flex--ackerleytng.bounces.google.com>) id 1pcbWf-00040Y-Cn for qemu-devel@nongnu.org; Wed, 15 Mar 2023 20:31:25 -0400 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3WWMSZAsKCpAuw4yB5yID7008805y.w86Ay6E-xyFy578707E.8B0@flex--ackerleytng.bounces.google.com>) id 1pcbWd-0007eq-QJ for qemu-devel@nongnu.org; Wed, 15 Mar 2023 20:31:25 -0400 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5417f156cb9so225217b3.8 for ; Wed, 15 Mar 2023 17:31:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926682; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/WhAZz5TZYbz5eACM2xmX0O35zR+4WA+K6fPnqmTdp0=; b=DFkZM4WxAkVVNIJBuBzB0nwyIoWMqXfdjlRk3WXV6Jxw5x54jqdNlx617tRGFPhNbf FSFEhLgx9YrnXMD2N9F3ND0dGS2wEQLQ6W2DinnGY36gb1WqdxpuV00Tng2C80Dv85bd GGCXZZnPgvalLdYHdJ5gJafblCxeiqDbyy5BB57SEGPtzpj8zUdhi4l6LoIgVVGg05nZ 6Mo31eLAizgeZ7625k2PZoVDucHfhEkPNcn7OmHS1vFJrTg1pI8T6QFyePo8eh9L2ANf ioVmITbi3yeESUanrzgTRmSVNxGVFuNzX/w7x5gA4sylAMhIN1UM2xnxuIntZGawA78b lh/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926682; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/WhAZz5TZYbz5eACM2xmX0O35zR+4WA+K6fPnqmTdp0=; b=flTDaiPa8baH8XJF/9NCLXXaDPkKNl85zQcf4bz8FIgfKqRmFKPXY/csDKVv3pDh4k vSp4n9OIlWHbcksfDRRHBg4vDyKejHX4Uzv3sTvAEOn2DQvsRBitiPnTSGdjNVUumuqW X2t/ih/4ZRwu7t09B6taVYf5iNqqaVIQUhmKL4/ecXBYulN8CVic6RZd6PEJU8zJ4oOi nSl1SHy4KR3TE5H+++vXWOSbdNwxntNOvnz6EJu23gyxxgbo/yFKBpzNGqkLhufD2U1P wKy08wGkH/zu1aISc/eCWb2v3wq0SJ6fXT6ZvHBk4/xhh0lIMFG3esVEY4masUcy8q8u UGLA== X-Gm-Message-State: AO0yUKWiwc6V65OYkVIZOnRndUN/hPOnYOqv6M6VEqqf6iaTxe5L+6xF 9AoXw/8SwUe/WiGHjNNa+Cc8asDw7gcTRSDgZA== X-Google-Smtp-Source: AK7set9tsl6JIRx5mbMVZPA/DONTxMkHWtMyPLSYdDprwtuWdlOsvzYeVaLj2tQwnFWT9YsS5B+6yQbk5CSUogkZdg== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a25:8b08:0:b0:b14:91e:4d19 with SMTP id i8-20020a258b08000000b00b14091e4d19mr20688255ybl.7.1678926681818; Wed, 15 Mar 2023 17:31:21 -0700 (PDT) Date: Thu, 16 Mar 2023 00:30:57 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: Subject: [RFC PATCH 04/10] KVM: selftests: Exercise restrictedmem allocation and truncation code after KVM invalidation code has been unbound From: Ackerley Tng To: kvm@vger.kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, qemu-devel@nongnu.org Cc: aarcange@redhat.com, ak@linux.intel.com, akpm@linux-foundation.org, arnd@arndb.de, bfields@fieldses.org, bp@alien8.de, chao.p.peng@linux.intel.com, corbet@lwn.net, dave.hansen@intel.com, david@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, hpa@zytor.com, hughd@google.com, jlayton@kernel.org, jmattson@google.com, joro@8bytes.org, jun.nakajima@intel.com, kirill.shutemov@linux.intel.com, linmiaohe@huawei.com, luto@kernel.org, mail@maciej.szmigiero.name, mhocko@suse.com, michael.roth@amd.com, mingo@redhat.com, naoya.horiguchi@nec.com, pbonzini@redhat.com, qperret@google.com, rppt@kernel.org, seanjc@google.com, shuah@kernel.org, steven.price@arm.com, tabba@google.com, tglx@linutronix.de, vannapurve@google.com, vbabka@suse.cz, vkuznets@redhat.com, wanpengli@tencent.com, wei.w.wang@intel.com, x86@kernel.org, yu.c.zhang@linux.intel.com, Ackerley Tng Received-SPF: pass client-ip=2607:f8b0:4864:20::1149; envelope-from=3WWMSZAsKCpAuw4yB5yID7008805y.w86Ay6E-xyFy578707E.8B0@flex--ackerleytng.bounces.google.com; helo=mail-yw1-x1149.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The kernel interfaces restrictedmem_bind and restrictedmem_unbind are used by KVM to bind/unbind kvm functions to restrictedmem's invalidate_start and invalidate_end callbacks. After the KVM VM is freed, the KVM functions should have been unbound from the restrictedmem_fd's callbacks. In this test, we exercise fallocate to back and unback memory using the restrictedmem fd, and we expect no problems (crashes) after the KVM functions have been unbound. Signed-off-by: Ackerley Tng --- .../kvm/x86_64/private_mem_conversions_test.c | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c index f2c1e4450b0e..7741916818db 100644 --- a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c +++ b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c @@ -203,6 +203,30 @@ static void handle_exit_hypercall(struct kvm_vcpu *vcpu) run->hypercall.ret = 0; } +static void test_invalidation_code_unbound(struct kvm_vm *vm) +{ + uint32_t fd; + uint64_t offset; + struct userspace_mem_region *region; + + region = memslot2region(vm, DATA_SLOT); + fd = region->region.restrictedmem_fd; + offset = region->region.restrictedmem_offset; + + kvm_vm_free(vm); + + /* + * At this point the KVM invalidation code should have been unbound from + * the vm. We do allocation and truncation to exercise the restrictedmem + * code. There should be no issues after the unbinding happens. + */ + if (fallocate(fd, 0, offset, DATA_SIZE)) + TEST_FAIL("Unexpected error in fallocate"); + if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + offset, DATA_SIZE)) + TEST_FAIL("Unexpected error in fallocate"); +} + static void test_mem_conversions(enum vm_mem_backing_src_type src_type) { struct kvm_vcpu *vcpu; @@ -270,7 +294,7 @@ static void test_mem_conversions(enum vm_mem_backing_src_type src_type) } done: - kvm_vm_free(vm); + test_invalidation_code_unbound(vm); } int main(int argc, char *argv[])