From patchwork Fri Mar 28 15:31:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 14032178 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 F3490C36011 for ; Fri, 28 Mar 2025 15:32:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4E76D280155; Fri, 28 Mar 2025 11:31:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 46FFB280152; Fri, 28 Mar 2025 11:31:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 22857280156; Fri, 28 Mar 2025 11:31:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F0793280155 for ; Fri, 28 Mar 2025 11:31:50 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D6B151C7ECC for ; Fri, 28 Mar 2025 15:31:51 +0000 (UTC) X-FDA: 83271350022.14.869BEAA Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf30.hostedemail.com (Postfix) with ESMTP id C1EEA80012 for ; Fri, 28 Mar 2025 15:31:49 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nPxs8yQk; spf=pass (imf30.hostedemail.com: domain of 35MDmZwUKCEc2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=35MDmZwUKCEc2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.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=1743175909; 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=Mv/1E0AU+NyqHw7xnz4KzldcKMV/7tB5wdC54OSV0gs=; b=id8PkCIKzfFtNBFBTtM0AHP7sO7rYlfVoZab7jcFG5noyC7PvEQGd0yKDy4LD4Y0L1rLc1 MwVzTp37yymz5S+PovU5Fhm/ycnwz5UOQHznOViqRXx+9J4mtJhxATw+RFu/BO4rfmGnyv W49DVpJ0REBLMtD8eZdYZ4+Ac827SVA= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=nPxs8yQk; spf=pass (imf30.hostedemail.com: domain of 35MDmZwUKCEc2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=35MDmZwUKCEc2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743175909; a=rsa-sha256; cv=none; b=oG3/urjF76q/6SmNSeec/USvK3VbPRHOz5pSFKF+w8yy+WyAaShg36mpXwnluWvdH2H5Ai cdQdr84cuD/06xX0LVcH/sZDir//jeOfTTGfuf/AgPe96Y7O3ExIIH11OtwCfRYqIE7nYK IKiEwbpRn1yl+tHSB9/PFHprWVQ8qSk= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-3912d9848a7so1769614f8f.0 for ; Fri, 28 Mar 2025 08:31:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743175908; x=1743780708; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Mv/1E0AU+NyqHw7xnz4KzldcKMV/7tB5wdC54OSV0gs=; b=nPxs8yQkeQr0SWUO/ln9SqNugbSJP8GAUqNjZL76tLRxTBAi8FZZEMrsphanQcp1Vz A/EUzmVeHktkC0YY4iC3dNKSHtPNf92TJ0ahfz6FVAy5f8t6d+IRjOBIBu96sue6Lo7O p7G3FhvIK19X+8+hyOaJKBiEGZOFx3hVWHEVslykGmCHOfbJlH9Net94phbsNx8UBua3 yGXJeII5iF+fwo+CsY6Ah0bzn1P+5VpqLnEXqHvYf3TqegxFJ66m1hc641/rUSYnpNem jQ+F14vHPGzotltk0kksEDf44eTNSuehAvcBnssCkA5ktMYYLGpe3l96Kangs6yyEM8f 59ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743175908; x=1743780708; 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=Mv/1E0AU+NyqHw7xnz4KzldcKMV/7tB5wdC54OSV0gs=; b=t+sGop0NZhzWyxQxj/mCbqq/yX3giTQJjMzAfcduFKRlAM3PGp7p6nQvEbmSew5vHe IV5mmGBc9H5B64Ddo1ZdUVmuGOf/2jLA/8IgifBLfeOqeUFagl/MXD1eBGEHBh2QBO7V AkJGALiiANE83iHakuLS3LqseaC+fnqEkm+eqng2LAdrAn06GTbf6I5AGtvLD/yiwTDS GQQrD7UjWOEUMMucKtv3hlJbaHy9nKeHiHHMm0aKTt+YZSgkrYXvbUolE9JF+9DnTZzj 9WRvkNHw7jidYC7n7hIpqq28olEXqcRt+B6GxqdcWaLcT4CHqvsnDFG7ZmCjPP4ao235 vHDw== X-Forwarded-Encrypted: i=1; AJvYcCX5EZUCA4dWaOxD+TnLDuXola+DlLYBpKORnEIPhKxPwPmxnplKXHppeB2wSEThGW3jKtMtQWn8fw==@kvack.org X-Gm-Message-State: AOJu0YzcuxORjLSTsoeE5kojgnNyZKE85O6AgVnyFOSOhc1LTKwHAK9B Wdt0mRVW0Gim53IGx1MjJhyFjziPfJob87KeeWLsq7NKrCxB0BThgXHcTu9LuFRNGH1smYUh1g= = X-Google-Smtp-Source: AGHT+IFlQ1UrYxh5xtzAR3ixBktqgMp71GraHERqKS0AjfxoTOcZj42vAVyGIN1u0tDDWZHdqWBm4jW8+Q== X-Received: from wmbje14.prod.google.com ([2002:a05:600c:1f8e:b0:43d:4038:9229]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a5d:6d02:0:b0:391:a74:d7e2 with SMTP id ffacd0b85a97d-39c0c13b8bamr3105305f8f.26.1743175908326; Fri, 28 Mar 2025 08:31:48 -0700 (PDT) Date: Fri, 28 Mar 2025 15:31:33 +0000 In-Reply-To: <20250328153133.3504118-1-tabba@google.com> Mime-Version: 1.0 References: <20250328153133.3504118-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.472.ge94155a9ec-goog Message-ID: <20250328153133.3504118-8-tabba@google.com> Subject: [PATCH v7 7/7] KVM: guest_memfd: Add a guest_memfd() flag to initialize it as shared From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, peterx@redhat.com, pankaj.gupta@amd.com, tabba@google.com X-Rspamd-Queue-Id: C1EEA80012 X-Rspamd-Server: rspam05 X-Rspam-User: X-Stat-Signature: qrmor4rtfir5uz5bxdhpkkxq8rcmrhm9 X-HE-Tag: 1743175909-208440 X-HE-Meta: U2FsdGVkX19VJTNQE1SarNYw9DiWQ7j9rTPMeOmHfiKvH8rZ041kvDNhfrroKhYVT3lJrVimhP7cCWje2/SVYVIl0McQHI/iY4UFhdQyjF8i4QxNf96fR5xTSrQvGAAcYcYl2+zNirwa9WAyCsa0THXq/irm/mKLhjC6eIArX2DjHuO3m35sZyKiVdn8P7JMp9JRyJtUMI0EaKE9tqt6z/zl9+0Hvh7qb7IX7/xooUUk8XZCUPiChg1XsRjuWbQPnDkDe2ARyl+yjJ9wLX9rRKiIZVCOgyt59vKlXwhZK9zqDcFytmT5uBwRBSEVK6xD/LWAYmp14rS9UqZRjkAMcbfWmMvq0o9YP1qp0Ir1QvF0g4MgBh+SpofgNq0NXXBJOTvZbLwUg0DAL68s8awkbRhnl2FkInRzVi6ECE3zj0zbuI+EJtQCeTJJjMLAhZip8i9ajCXhOf/fxQamYyKGyOpaKA2tc1RzFJyLnQLsGykqJfzFOPHdGHUv7qMK1k1kSNAWueMQU9mot3x372aTqkswyHGA2isFqR+iHGONcU8uzjxpInH6Yo3NDGb9CNJ2664pDE4uq+g8cSxAP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PMJJFCIrz2VQZAfiQpey06smVGKnCZzQatg== 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: List-Subscribe: List-Unsubscribe: Not all use cases require guest_memfd() to be shared with the host when first created. Add a new flag, GUEST_MEMFD_FLAG_INIT_SHARED, which when set on KVM_CREATE_GUEST_MEMFD initializes the memory as shared with the host, and therefore mappable by it. Otherwise, memory is private until explicitly shared by the guest with the host. Signed-off-by: Fuad Tabba --- Documentation/virt/kvm/api.rst | 4 ++++ include/uapi/linux/kvm.h | 1 + tools/testing/selftests/kvm/guest_memfd_test.c | 7 +++++-- virt/kvm/guest_memfd.c | 12 ++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 2b52eb77e29c..a5496d7d323b 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6386,6 +6386,10 @@ most one mapping per page, i.e. binding multiple memory regions to a single guest_memfd range is not allowed (any number of memory regions can be bound to a single guest_memfd file, but the bound ranges must not overlap). +If the capability KVM_CAP_GMEM_SHARED_MEM is supported, then the flags field +supports GUEST_MEMFD_FLAG_INIT_SHARED, which initializes the memory as shared +with the host, and thereby, mappable by it. + See KVM_SET_USER_MEMORY_REGION2 for additional details. 4.143 KVM_PRE_FAULT_MEMORY diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 117937a895da..22d7e33bf09c 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1566,6 +1566,7 @@ struct kvm_memory_attributes { #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) +#define GUEST_MEMFD_FLAG_INIT_SHARED (1UL << 0) struct kvm_create_guest_memfd { __u64 size; diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index 38c501e49e0e..4a7fcd6aa372 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -159,7 +159,7 @@ static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size) static void test_create_guest_memfd_invalid(struct kvm_vm *vm) { size_t page_size = getpagesize(); - uint64_t flag; + uint64_t flag = BIT(0); size_t size; int fd; @@ -170,7 +170,10 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm) size); } - for (flag = BIT(0); flag; flag <<= 1) { + if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) + flag = GUEST_MEMFD_FLAG_INIT_SHARED << 1; + + for (; flag; flag <<= 1) { fd = __vm_create_guest_memfd(vm, page_size, flag); TEST_ASSERT(fd == -1 && errno == EINVAL, "guest_memfd() with flag '0x%lx' should fail with EINVAL", diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index eec9d5e09f09..32e149478b04 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -1069,6 +1069,15 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_gmem; } + if (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) && + (flags & GUEST_MEMFD_FLAG_INIT_SHARED)) { + err = kvm_gmem_offset_range_set_shared(file_inode(file), 0, size >> PAGE_SHIFT); + if (err) { + fput(file); + goto err_gmem; + } + } + kvm_get_kvm(kvm); gmem->kvm = kvm; xa_init(&gmem->bindings); @@ -1090,6 +1099,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) u64 flags = args->flags; u64 valid_flags = 0; + if (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)) + valid_flags |= GUEST_MEMFD_FLAG_INIT_SHARED; + if (flags & ~valid_flags) return -EINVAL;