From patchwork Thu Mar 16 00:31:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13176858 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 860D9C76195 for ; Thu, 16 Mar 2023 00:31:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 39D886B008A; Wed, 15 Mar 2023 20:31:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3763D6B008C; Wed, 15 Mar 2023 20:31:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EDF06B0092; Wed, 15 Mar 2023 20:31:30 -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 10A616B008A for ; Wed, 15 Mar 2023 20:31:30 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CFA19C125B for ; Thu, 16 Mar 2023 00:31:29 +0000 (UTC) X-FDA: 80572882698.25.39CC28A Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf11.hostedemail.com (Postfix) with ESMTP id 15F424000C for ; Thu, 16 Mar 2023 00:31:27 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=I+hk1kHX; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3XmMSZAsKCJUz193GA3NIC55DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--ackerleytng.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3XmMSZAsKCJUz193GA3NIC55DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--ackerleytng.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678926688; 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=JBLPG3XthGG9faiSvk5U/ET6qjyhuWi/SwHOth1Ucac=; b=ZhMpTkhN/ZgUPrma7aX1nloLLBpCjHT835HX5zQu2vL2Yt9Lu7HPQUrxIMNf+PgL515Fe0 WUgINdoHppheeOzA0V/apiSsoxQCfMIHViX8yFzhfr26BKHjIz7mDdAn/FkAZg5RCZTphn DLaODvD/vSOsClfLQ/7oSz1+JyYsBVE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=I+hk1kHX; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3XmMSZAsKCJUz193GA3NIC55DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--ackerleytng.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3XmMSZAsKCJUz193GA3NIC55DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--ackerleytng.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678926688; a=rsa-sha256; cv=none; b=5963LAySW9itpaKBTKldUX25HGV7VoiADgpaV1s/bwJNhHKhujV5sj+3goQT5JhUs4XAnS 09PE3bDx2P3mlzQgnysGC/oQZ0xChSfxjJNKTu0sJFtuTzUL9fyJeONbLygFuZn6dThc8V LKrP7uv1P9UxYobPzOf6z1QZZC6k15U= Received: by mail-pj1-f73.google.com with SMTP id n33-20020a17090a5aa400b0023b4f444476so68447pji.3 for ; Wed, 15 Mar 2023 17:31:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678926687; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JBLPG3XthGG9faiSvk5U/ET6qjyhuWi/SwHOth1Ucac=; b=I+hk1kHXyTdyRHWR/E+Fe4D/KjC/hhFWZULnWPbpGXImZDU13NH2r0CvjRWyfCgLxX /nkIHP6zbtD+kRkLZz1i9msvu36P2j1F1ed2x92tbTloVbSMlV2L5X7YmVhhKmZ4DqvM XpXOnOkjDklzkljzqZGV/y4Nh+8aXvLV39ccn20yIQi1+cuwUBgQwtL3GYk3OKYOFzt5 fGTKHpxrFE/fRCl2mKHPSZMCdRAxw4bOce1hR/tDvln+tyvmSAorwJz0NO2G3/AQeBf+ ctLv6vLizdn+6uzwfijXtM/b902mBW+WM3hgf9E7bWyK12V6rTdbKghkmJDtmACEkOkZ v8Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678926687; 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=JBLPG3XthGG9faiSvk5U/ET6qjyhuWi/SwHOth1Ucac=; b=OJ+8HLq37D4S1VGKBlds+pax/Li2+94e9fEs9eZMIIz+JB9cCrJVkM0kgtlCtutFhl 6TX8/H4hm+G2IhhvYEegPrrlCWas2xez8u8yoVbsQQAs5DTuydy5peT8kOG+/gKtrYTs QFGrJOj5SbzDshFHIRQHnLHBZ781tRfCp+rnWEqXHV+qZcNCPSk6Xm5tODDouVYQSRz0 d/3z5mqy+8hk0VsbG8j3mwFAw4h0l7AxUry43TyghPHvONCzhQSkp2bsyMZG533fWwHX rnoA5N5nCCZdGxd5ahRx+RYz14491Ha1lKjruEu/5UtGLBMhM58dXOQLblZFXy0bVxx5 zMIA== X-Gm-Message-State: AO0yUKV+aZkfjlvaRKBojIspFyxmzkLGXzEHulHVlYSSq2ppSz+pEBYB hNYspiOtlx4qC9xGgILKZ/g9Ycx5+JhtcjQjrA== X-Google-Smtp-Source: AK7set8wyHBYS0yNjAY2Bu6NPZk7sDhPcWzuxoadwL3xHiIjGr+DZnA4k/ntT0jv/7rXx9SUD16F0j0DYIAvrg8IcQ== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a17:902:e749:b0:1a0:4aa3:3a9a with SMTP id p9-20020a170902e74900b001a04aa33a9amr581092plf.2.1678926686991; Wed, 15 Mar 2023 17:31:26 -0700 (PDT) Date: Thu, 16 Mar 2023 00:31:00 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <59ff32ad0aa9e3533a96064d1ae07aba11f55924.1678926164.git.ackerleytng@google.com> Subject: [RFC PATCH 07/10] KVM: selftests: Add vm_userspace_mem_region_add_with_restrictedmem 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-Rspamd-Queue-Id: 15F424000C X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: m48hwdtszmhharcdne15kpkna493nac5 X-HE-Tag: 1678926687-173905 X-HE-Meta: U2FsdGVkX184HvTza9Np2Ftvn8Wf0+4xxtOWax4N2qmNgwXj9CYaR8caJyoEL0nCyJllJjzrAcrwAb8okooTK3FD1TaqyTs6ckgiWWWYB6lz7Feq1LzAaFCC1FWKs9Ucc+3IRC706eshnzdMb6hpgxaUs9RIyV6puANGcmh7JzhwD/DFMakC6DIEl8a80+ll4fiJpDQ4e1xE91mzJHi/3QnmavVWaSKj3nZhqdxXJHWvCjlEpt1omMULMP6kUoyYyy4hJK+bHx4CGh0YJIUTKwXdmYLWEVT9kF5WBASYjNuKfj4C+uTwouuH4Y46qNzpHn1wXSzjmF4rMGNHh5d5Vg5yDVFS/Rpuqs15x+SnuZ8FDJKDo1khzZYfLgRiwYStI0fg+wQ775zxN3DH43nErJIVz1Ae3FvCcFnrWi5IIampBltakwUOEc1gsGOltNTDyUQ63txWEJ+Rr436QRP7NlTaTOYVHnaq6jdT/kiwVrUfw6QLbaqO6512xiqfix8XdJurqOtSpYct5tsEwhhXVPPjWDeYaJiZyIoz0AkKSheJ5ZyXMxZ/dVsmRuU4ICV8UAr51TMgsLzQjMzLIniflbdbEzbyV79h/ldTIXBXhZgOb/1fxbZbl//pZsViKL8T9zIB/fq9FHoxPJKpqFbZG50fpl/yXtJ+L+O8j/ENmKLvbqDNOFK2/T0+GzFsde0TC6wxYJZFFobaj5C2Ch/GAwRgydoGM5ctJZ6RoPv/kOyfMf18Y0duVIRzbfMABAW/mQjI7kxO59LscIxY8iExpg/6iZ3bMrX6qXcMqspxTQnZ5iZADrl8O1o5yWCTF6pEFFQvzmXFhqOJfxrz7dO5NZiqzZgMtDGZjGn4uh8Vxzxm7Zo7+xxWdpWGyJIB6yraDuXdMbXBCJPmCnbKWw+1zjLO16Rs9rTi/JD24dzysFltcxb2BbV2JbcoYLKZLGiKsOhKAi16igniLfgawii ND9Lx3jQ TUVC5DKBTxqKqhQdyBLDMDXtu0iikTZfKx7GDXNqj4v/5NOBaw9I5VFHCKguOr7lG4swmijxNm1pWVTgMd/vD8CDEAOZGHskNU5WQotIEJAxFP5BIUwI/F25gdNHSuzfiJAwts+3135qlyTv9bk51gcSDTl3ilFW3JemO4SwG9zf7V1mbcO3ewVMzRG4diZFwCR8RgjiT7wJl7uXAgvDzvZgDQpSFBGgoq2Q49l40ywgMnZRnQDeW7BBaStz05ETN2GcL6rp1bhFrbMc8NBOLIhI1oRJIuY0MUIT7F9Do2SnNTg1ZUA5pbp+xd7YQhSle9IgGlAS60Qs4C3h42Do3kq9hOsm9yKOZsuseezDjzlsGadTHg5MScvXNO6auT2QgcegSpiexI7fpkN+C/vb8eqcOLj43uxvhYJwFBGTGVPqRuToFla/YcVKMdNa7lj2ukUx9MlcKGNtDHtEBvP67kkxf8wF8mctWkE5CuuqnIyJVF28ZPRJ3xYR0aDyENuRSBnm8pWW0SfajQy1MwINVgGH1E1KTh9L83lKLBSaq4yELHiz7e0GIwAaqyvyNg73c3sMvLimhmHiozQyO47FnIgadC0NLBlx/h0d6CaHQ3VnIuGbfcHrav02p1ZJjape1u4wh0qyo+FYFXHi8qG4jwecaj/RU59/MCp7+H5wM2msnaq+n+Kkk7fs/f644MNYf9hP8IV4ucu3DUnE= 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: Provide new function to allow restrictedmem's fd and offset to be specified in selftests. No functional change intended to vm_userspace_mem_region_add. Signed-off-by: Ackerley Tng --- .../selftests/kvm/include/kvm_util_base.h | 4 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 46 +++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index b6531a4063bb..c1ac82332ca4 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -486,6 +486,10 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, uint32_t flags); +void vm_userspace_mem_region_add_with_restrictedmem(struct kvm_vm *vm, + enum vm_mem_backing_src_type src_type, + uint64_t guest_paddr, uint32_t slot, uint64_t npages, + uint32_t flags, int restrictedmem_fd, uint64_t restrictedmem_offset); void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags); void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index d0e6b10f140f..d6bfcfc5cdea 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -898,6 +898,43 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, uint32_t flags) +{ + int restrictedmem_fd; + + restrictedmem_fd = flags & KVM_MEM_PRIVATE ? memfd_restricted(0) : 0; + vm_userspace_mem_region_add_with_restrictedmem( + vm, src_type, guest_paddr, slot, npages, flags, + restrictedmem_fd, 0); +} + +/* + * VM Userspace Memory Region Add With restrictedmem + * + * Input Args: + * vm - Virtual Machine + * src_type - Storage source for this region. + * NULL to use anonymous memory. + * guest_paddr - Starting guest physical address + * slot - KVM region slot + * npages - Number of physical pages + * flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES) + * restrictedmem_fd - restrictedmem_fd for use with restrictedmem + * restrictedmem_offset - offset within restrictedmem_fd to be used + * + * Output Args: None + * + * Return: None + * + * Allocates a memory area of the number of pages specified by npages + * and maps it to the VM specified by vm, at a starting physical address + * given by guest_paddr. The region is created with a KVM region slot + * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM. The + * region is created with the flags given by flags. + */ +void vm_userspace_mem_region_add_with_restrictedmem(struct kvm_vm *vm, + enum vm_mem_backing_src_type src_type, + uint64_t guest_paddr, uint32_t slot, uint64_t npages, + uint32_t flags, int restrictedmem_fd, uint64_t restrictedmem_offset) { int ret; struct userspace_mem_region *region; @@ -1011,8 +1048,8 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, region->backing_src_type = src_type; if (flags & KVM_MEM_PRIVATE) { - region->region.restrictedmem_fd = memfd_restricted(0); - region->region.restrictedmem_offset = 0; + region->region.restrictedmem_fd = restrictedmem_fd; + region->region.restrictedmem_offset = restrictedmem_offset; TEST_ASSERT(region->region.restrictedmem_fd >= 0, "Failed to create restricted memfd"); @@ -1030,10 +1067,11 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n" " rc: %i errno: %i\n" " slot: %u flags: 0x%x\n" - " guest_phys_addr: 0x%lx size: 0x%lx restricted fd: %d\n", + " guest_phys_addr: 0x%lx size: 0x%lx\n" + " restricted fd: %d restricted_offset: 0x%llx\n", ret, errno, slot, flags, guest_paddr, (uint64_t) region->region.memory_size, - region->region.restrictedmem_fd); + region->region.restrictedmem_fd, region->region.restrictedmem_offset); /* Add to quick lookup data structures */ vm_userspace_mem_region_gpa_insert(&vm->regions.gpa_tree, region);