From patchwork Thu Oct 10 08:59:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829778 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 6DB32CF07DB for ; Thu, 10 Oct 2024 08:59:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 036BC6B009A; Thu, 10 Oct 2024 04:59:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F29406B009C; Thu, 10 Oct 2024 04:59:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA2496B009D; Thu, 10 Oct 2024 04:59:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B281C6B009A for ; Thu, 10 Oct 2024 04:59:53 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0A276AC2DD for ; Thu, 10 Oct 2024 08:59:47 +0000 (UTC) X-FDA: 82657095066.20.FA8FF50 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf13.hostedemail.com (Postfix) with ESMTP id 59DCF20002 for ; Thu, 10 Oct 2024 08:59:50 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=I+d9iPd+; spf=pass (imf13.hostedemail.com: domain of 3hpcHZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3hpcHZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@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=1728550654; 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=zKaf8nivAag4/4uIL21ZmJPpCRWLdv9h6z/9SiSCBvk=; b=ZqDVUTJamxx2yntOSO/kuYMcF/efNcaHlYMLM16JVIjhrfcxVZODbbbzGETy3icfEIAhle OlkuVtc41HJ0ziQ7My4sCVbgnkVoJ8KHpLpjWiIemyUFSVkZn1ivI/6nJy3rNdZus4pfLy x+2P+30j7iPz/eXQ9S7xwpG4oysH0UI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550654; a=rsa-sha256; cv=none; b=EpIYDizCJEw3CjIpJ5ATUcFd1swE48aRij6b7kA6LfgqNLJdF2lhlL8WacTo56R0tFfZbN OOFDfacoYdOtZS1jSSS4zzkGAtiSzimT/rKC9Tl88asYgl291Hke+yJaEV0RXi1hlAErIV WY745mCLPQZeb0BS/L47M1DhObIOqDQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=I+d9iPd+; spf=pass (imf13.hostedemail.com: domain of 3hpcHZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3hpcHZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e1159fb161fso1191775276.1 for ; Thu, 10 Oct 2024 01:59:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550791; x=1729155591; 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=zKaf8nivAag4/4uIL21ZmJPpCRWLdv9h6z/9SiSCBvk=; b=I+d9iPd++IwTFGEcPKQ7NiCf6QbNbfcoHbxg54Z9tPO78EBNv4J4I9KlXS+NL7dTI8 sbrdH9PoLbVlZkRAmNULGp+Amae/42OtqeGHb5HTE+zQA9yMAsvRjjb4ij89eek0+M1V h7HO4qCny4a61DCyD5ADPRkOvHuGM5LDEWkUYxsnLVFL5jsjPu8yuq7JLcqYZsFmq6SH szOmryU6F27xuxe/JH1gzYkoeyHyS97tzQ4/DMUNBsHIKqjzdoCLQP3U2D1MrS2iLbh6 HTcU0OPC4dy/jNo1h2B5CbhB2tfGJ0qYi27KkB+gig9CmxxwAkF80hcOgDhgacJaRF7x Kw4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550791; x=1729155591; 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=zKaf8nivAag4/4uIL21ZmJPpCRWLdv9h6z/9SiSCBvk=; b=okm9gfUy2ssB5PRLxuM6GEsrwbfdJkF7Phu5alqIyQ6ufOANs14E1TAuOazjy+W6r9 GqUToD/Ket5f5n1EK4O+WCtKSwjLuKzc0jXV7/5OVz9sQV/aSHoOIXMka1ero67kgVs0 lAEdaLEsocrqy60DgIU8IuOZ3awoyjmrzu6PziHz+Of8IrfdHs8Ok/9xxxJs3/x78GKN TppnahRGFYRlvDXr7cry1/nK1vJzKUYDW/NpqwwCezp525/jWzE9U/uWmIRjvmzjp1vM R9ROW1BOFasIXatPHWrUz8bVw9lki3jv67RL+STtDWoyivtsFA/GYow20EwhKcIQ2ERt 6qXw== X-Forwarded-Encrypted: i=1; AJvYcCWr2dC70altRoDp9V1CCYJaVzyy3vd6w5iAQliNbOGd5FvfJt1s1IFyK2DMX9rPmEirY+gVizdC3g==@kvack.org X-Gm-Message-State: AOJu0YzuidJZKtpg3malTKKakOXIgIRjnZDdT9n2ww22TJm7dAUVUxtx gVcsYuK7345Hv4bEaHD+qVi75NSTU+clfY8WS9sjT0kOVrUNdzNx2lPd2M+mkczfqJjt4/Fqig= = X-Google-Smtp-Source: AGHT+IFyezTuDqo1giJ/p5guQQYFu/xIk5SZEqJH7PMzqSsZJQW4O7K2L6rTa8b012uIruIWq11czsKArw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a25:c2c1:0:b0:e22:5bdf:39c1 with SMTP id 3f1490d57ef6-e28fe45b504mr3520276.10.1728550790172; Thu, 10 Oct 2024 01:59:50 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:26 +0100 In-Reply-To: <20241010085930.1546800-1-tabba@google.com> Mime-Version: 1.0 References: <20241010085930.1546800-1-tabba@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241010085930.1546800-8-tabba@google.com> Subject: [PATCH v3 07/11] KVM: guest_memfd: Add a guest_memfd() flag to initialize it as mappable 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, yu.c.zhang@linux.intel.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, tabba@google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 59DCF20002 X-Stat-Signature: m3qbhfctwztxcrca8gogt8q8d58ieqxy X-HE-Tag: 1728550790-55154 X-HE-Meta: U2FsdGVkX18g44h+HDQCQ3rV2QA1uPiOBc32eqUn6OJdZuo3V4YBBdooRri6Keyq9xycHrk8MW9UdJAPeil5RLAdsUprQzVWmkxhtL0WsDO+9e3sU8WuLEBlOhAOT1ZFBTiHhZFFyUTIrAq7OTYU9lR11UCbYVwyKIHZtr21V+nNTwtj/AchsgbFauRh3S6GLr/bQ3+DRU8+nvMRNMOvoNBX+iAI0Nwb8OMw+DzmFpC3sDymqI6yhIi2Qu/vRnxETCN+jcn3Zc2JPWfMcxY19X/e5AjDZJu94VAgJNEuYe4eqC0EH/3Hc76+n/ECcvGUvj973woHLL76Ou58ZY89Cf5wi9opm/qiwIU8ECkorzUGvsDMRECkHN9B+eNa92VW1FZXqJB7YSU0+ec7j3Oa0WvEKEfkO5VeRPf9lci6zkbvbanice3lZ1WPhk18FR94JB7pr1vNkt7cODlKaQzImPxE/Ei7mrbBW2yod+8jebIrGEXYeSzIptw9TEYJBm3BwFRc4HCxcOE6xEfFOrqzdi2a/BeQ2OCNpkUS10XzamlgwtoFvXOT9t1vfmPHj3mhxF2W5QfZWQlCVqlkm6hM0G2BXKG+KGgR5QuOX5vVL1tRj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ogofilter, 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 mappable by the host when first created. Add a new flag, GUEST_MEMFD_FLAG_INIT_MAPPABLE, which when set on KVM_CREATE_GUEST_MEMFD initializes the memory as mappable by the host. Otherwise, memory is private until shared by the guest with the host. Signed-off-by: Fuad Tabba --- Documentation/virt/kvm/api.rst | 4 ++++ include/uapi/linux/kvm.h | 1 + virt/kvm/guest_memfd.c | 6 +++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index e32471977d0a..c503f9443335 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6380,6 +6380,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_GUEST_MEMFD_MAPPABLE is supported, then the flags +field supports GUEST_MEMFD_FLAG_INIT_MAPPABLE, which initializes the memory +as mappable by the host. + 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 2c6057bab71c..751f167d0f33 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1558,6 +1558,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_MAPPABLE BIT(0) struct kvm_create_guest_memfd { __u64 size; diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index df3a6f05a16e..9080fa29cd8c 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -734,7 +734,8 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_gmem; } - if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE)) { + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && + (flags & GUEST_MEMFD_FLAG_INIT_MAPPABLE)) { err = gmem_set_mappable(file_inode(file), 0, size >> PAGE_SHIFT); if (err) { fput(file); @@ -763,6 +764,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_MAPPABLE)) + valid_flags |= GUEST_MEMFD_FLAG_INIT_MAPPABLE; + if (flags & ~valid_flags) return -EINVAL;