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: 13176855 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BAF56C7618B for ; Thu, 16 Mar 2023 00:31:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D62216B0082; Wed, 15 Mar 2023 20:31:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C9B816B0083; Wed, 15 Mar 2023 20:31:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC8526B0085; Wed, 15 Mar 2023 20:31:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9A7736B0082 for ; Wed, 15 Mar 2023 20:31:24 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6E74A120AF2 for ; Thu, 16 Mar 2023 00:31:24 +0000 (UTC) X-FDA: 80572882488.09.A08C062 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf05.hostedemail.com (Postfix) with ESMTP id ABF81100007 for ; Thu, 16 Mar 2023 00:31:22 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DFkZM4Wx; spf=pass (imf05.hostedemail.com: domain of 3WWMSZAsKCJAuw4yB5yID7008805y.w86527EH-664Fuw4.8B0@flex--ackerleytng.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WWMSZAsKCJAuw4yB5yID7008805y.w86527EH-664Fuw4.8B0@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678926682; a=rsa-sha256; cv=none; b=08ViQWfxaLMcE4bnD2SfdIaeBcN0nYWZPTG5ACGGCAVMfKzq6hnDtRZMTTcmzQyuGf4rQ7 PS1QfHivJo1Gwy7v+iZbBpdvfutYATqjnttinawMeRPCEaNOHY9bz2RXSOwSDZZOMx5cqW lg2r3+xMF1ifumudlJuifQ6VxjmDAuw= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=DFkZM4Wx; spf=pass (imf05.hostedemail.com: domain of 3WWMSZAsKCJAuw4yB5yID7008805y.w86527EH-664Fuw4.8B0@flex--ackerleytng.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3WWMSZAsKCJAuw4yB5yID7008805y.w86527EH-664Fuw4.8B0@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678926682; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/WhAZz5TZYbz5eACM2xmX0O35zR+4WA+K6fPnqmTdp0=; b=3Ji0N//Y00NhFWsV+CX6dYOGHhEEtJogQ3/3RKKqk4MV367RT/5oT3lKACtPwOR97+Q5+i qJmNZBpx4hrmNEmDl4iTVJMNTTJi/hh+bFiNjzANNyKTD8t9E2VZXIevCGj6GVQea2ED3/ aj2uEclNo1TOUx1u6ddwn1M6xQLJPgs= Received: by mail-yb1-f202.google.com with SMTP id m6-20020a056902118600b00aeb1e3dbd1bso110753ybu.9 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=61prnM49cF28ieZ5bFCD70SvaO5LxE6O2EQoOyp6ugVJKwXCX9EuOvvz1MgLyCukGj E04Dgs8/ZG4U+2FEDlGsbcBaqq5VO4q8BEabmq2lZu4Sf9HC6H42dvgtQOVsIPMc8rN6 9uK/AjmyR1HeVelQXwFqvwfgmroDLlkHBc0Mpr7RHLpMfm+YyQntbVQutNa7TTpwoLht PJZAHi7ntzItdAxpO9aNMqo3gG3cPrVR9XATzwla5dMsQr3zJyTMOrlDaOMrvqqQfkZk e08zdH/S6PLt3vmFE476t95nCwJqf18jalQ4JVNwesMY4F0UNL1i1MS9U4az1H9Os4qN KKZA== X-Gm-Message-State: AO0yUKW3/SQcrPupEGOTri6Oy45YndbWZ1Rcn82uLrWBeHuIReSMMZxM iiFNxPOMw070hJXLdgfVyhUlkG9kxpe6A8jb/A== 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 X-Rspam-User: X-Rspamd-Queue-Id: ABF81100007 X-Rspamd-Server: rspam01 X-Stat-Signature: 3fjbsugbr6pmeqoscro8d34q5thcsytq X-HE-Tag: 1678926682-565459 X-HE-Meta: U2FsdGVkX1+8XAuZJ7x0s/zDNtlMlAWy5ss3yj6kX2VM7a2UkR1D1XAiXySfvkx51oUN8AY87ZNUFvVPg50x6JirylSEB+QVrGY4GJ/PFma2mkgg+gGbv8F2x7V8vIf4MIqetcGGdW+MgRIeJ3WcN1AbrCxe35BGt8hfkF+OlMdHIxx7YKCjeYNI7ZSstEQNOqR1kDuOtu14fChVZLKIn3zXLpWe5CnU5XgYx4kVtD7fGh3IZjae+OFLgJeMAq2U0xJcr28/IN4Crk6VLxyVzpBfo8v04SbgkSkHL3BEYFHInIiPust703KAsCRisRddLEOejMRsXGesLQN9F+OroFHOtBCFesYcGe5LWWZHNiPHbhreXdy4onbl17pNI/TJHu0swGMEiw0H5mvCScZUVwgh46eWhi+brAvKJHAJ5SMZJpI3FZvUmDp0Ccgok8JyCkuBNG7lsXXiDqaW5aDUgLQOeNFOy+Wf0TCIi7Xly1gXuUXfhxP1GUHOsVxmxuCbsOciVkdKQ8l8sMalCtA98QRKkVK0WFlDFya48XwjWnDZS5xH7AnpjSIvf/Xs7CRbs9Hzhg937soI7F14ZSucDvcln3yRyicb6QJc5sUtNHgByNP9W1c+/6MEZqFLIeNmn6eLWPQWOr1+N6DGt8xsPWYWMnA35jyOnV6ADjaIgHghMvGtCygDk1PHfdig5dPoXtlREjx6zYb3lWVwrDFCZo6vp5h2Q4hSPs/Qm0aPTypVCbUU2HsiES6+NRP4d97TY9HItHH21OqKGGmQNkwOubf/6Ssw+/5AABlTgTuW9JZtMYTxNP8AXEJ5f1XWhK+kp68/cKmrCPEKBOXILvSmsTvVpu5r3tB+uCWbzSK0v+bmcB5FU1NofksxoymFtHTQHIInYR13AUWRjoPrVNZ+YLs3J7/UTWOORoXRbXk0F3kCsJWBl9US/fAv8NGXMh9Ymtf+W8oED+NTly6Cd0e Fyee87yT idbEFjVK/Ja7VTMTw5LlrpoJdntA4Hw5nqMmEygE3etIQqlN6xz+qwvVo3AyrOBGSGfrB2cwqTdtg6Re0VhfFAb5y9gvRZYVrvM71dZl0OtJItwXolZcmUPEHTe144wd4H0s+xK/7UmbR0OCiPHLn3BhyxM7Sa2/hRN8QQVGKkIEs1k0YNhStxEUCcNuMZ4Nho7Ngf69e8iOaIeIwtbM2uxmzmeRSpoRbgfLaK8LvW/5IW7yzGsrMVbiVBq7wtvLqS14AObSneLuNbUdlNdHianQqJh6cXtgL7NP/+OjchVh8mST1+Q/p/FfFnJvI5o9DUJmwnhRoIR8DO1aPKKwGp2vxES/bCd0AQORO6NbFF+VhFqexXBZnnjT54o6Lv3AuvJsVhZt7rBvTTCz/hVTLf90zVKCqXHTnJxvmcH5E15u/+PMHSoTrnOckY4ymY6km+ea2r92KkUrz6w0YO9kWx2/fkNTPrJx29a2unv8qK62zUPFf9iRQoZ7Jdfc6XM15mgaxU4uG2cuohGL+kP+8spI4clw2BebOnjHpA0ysLZt93w4Ne0hIGU7slGkkbJpg0fvR7adrTpv46GydS1GQKYIzesPF5WD1Nc1psgYZQVHmokQ+6Rkv+PPAMi6mGHFiNdwwOrJC9N3DPMIjLlfLOYgeJQwl6lNVu9+I X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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[])