From patchwork Fri Jan 17 16:29:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943557 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 83BDFC02183 for ; Fri, 17 Jan 2025 16:30:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1681B6B009F; Fri, 17 Jan 2025 11:30:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0EE606B00A1; Fri, 17 Jan 2025 11:30:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED2C66B00A2; Fri, 17 Jan 2025 11:30:28 -0500 (EST) 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 B20B26B009F for ; Fri, 17 Jan 2025 11:30:28 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 71825B05A9 for ; Fri, 17 Jan 2025 16:30:28 +0000 (UTC) X-FDA: 83017481736.21.151B97B Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf25.hostedemail.com (Postfix) with ESMTP id 7AAE4A001E for ; Fri, 17 Jan 2025 16:30:26 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=irTyNm3g; spf=pass (imf25.hostedemail.com: domain of 3oYWKZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3oYWKZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@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=1737131426; 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=Xo0QGcWhFZe9U6hSQiSPOH6tARFYvfNnZ2E+qSQ4HQk=; b=pe4tbVBOrPrKU7VyINwpjJi2JZ+sY3g5E1bnF5ZfoiJwAjXSKtndmIXpw5RHThSoxScbBD TnMq9VewrH6HtGvlPll5J3N/KR7GuxeFV02Q5UoM2fPIAPN0DgIX/fAkqVQ/BncuiyewQv WR3SO4+/pxmSJJQ0IRZNIVCmQF+aaoU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131426; a=rsa-sha256; cv=none; b=RF71ZEssHaaZN3z2vACZ/GCOe1iQPBqSiK7Yyr4np1qHnlrniuFzzsiESZeinAc5yrbovR 4KeMOyrClEf16vWAPYoPLH2cc4HE5UyGnORGQclDIRwP7+Xteb/U+pniMZUVVlZcgokIWf OH12iTf0lLhY+9TesDlYQ4mwp99B3HU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=irTyNm3g; spf=pass (imf25.hostedemail.com: domain of 3oYWKZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3oYWKZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361efc9dc6so11805875e9.3 for ; Fri, 17 Jan 2025 08:30:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131425; x=1737736225; 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=Xo0QGcWhFZe9U6hSQiSPOH6tARFYvfNnZ2E+qSQ4HQk=; b=irTyNm3gNTt0cHVIU85E4rQfXQzZPRQrnJfs0ZouhdGmVTlNAkcyd3YffWmIoEg2a+ qutaogtiYhihQO6xJx2PbnxVYsMABRiDSpAhuuOg3w/chYMcvzHi1h1C5296mH8XnDuQ NFmuE3Fj9/kbmEUikcufvNT6XJRKxFcisj9L4IqgqxJCPyRM5lnaRw6v+XjLEDIP/x1I agm4T+gzcfCF7JBXNkXRFy3Pu7Lo1U7vPSVhaUZMbzTQEUSKmQMPHeyJT0Mx4cRpneIt 9OuvjGa4qDJz93L0Mxzj2nIPhVDUPrOWXzCvrW6Xx/UzOt8Zx6L8Na+Rkerabad/TcYZ 8GeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131425; x=1737736225; 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=Xo0QGcWhFZe9U6hSQiSPOH6tARFYvfNnZ2E+qSQ4HQk=; b=QQg0awuWyk4gEQ2c9aEpZL2+9aoXuhEwZtv27O43dk/bjc3hFYSLoj45SLMS0hW1EB 6oteow+Ts9/fmeMlXvV1/zz/MNevqkcQ57PnYlv78Vx2V2bM+kTE6Zg7rtWI6FG3KPlI TbcQHsvQMbmWRiTCLlvW87t1aRoiRijo2OHIH7XE62JnzPC2ggTtt8mUxd9JYp1EFst/ cxjnTCir/7SCbIU95QJKvg59H3wxIk4i15VP/+WN8pXqsgK68L6xf5POIjFEnnEO8r3F FfiliwKXbUiMp3cLrskGUpYFnRC3jq+BSNhkLLEL5+QvGQM1Gy7jewvIiC+vIeLI6Nlw Xbdw== X-Forwarded-Encrypted: i=1; AJvYcCVrtHpEzKt3qdP86z0vm1ylCwzY39Ize2aYA4WQtwQgGS6G7hbrJUVmfflZiuGDNItxZT2/fQoK2A==@kvack.org X-Gm-Message-State: AOJu0YzWawpAQe/XTVYJSWuNc2ZDILtYnzkT1T4xqeUPqYjJcXnUe3x/ yySSCXOONFG9Uonpq6I8k5eWOZNZi5wjglclernMUyaNpOGL1H0knrTzefVcbqUs07kQeqb/5Q= = X-Google-Smtp-Source: AGHT+IEr5gHEVjnL0bNACXR5FJY9vyiyHwTa1xX0TfEkZNy7DAbAsx+X4Pqj72+4G5i8n4A/x2j0nCfypw== X-Received: from wmsn40.prod.google.com ([2002:a05:600c:3ba8:b0:434:a1af:5d39]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1f82:b0:436:fbe0:cebe with SMTP id 5b1f17b1804b1-43891452f6emr37291625e9.30.1737131425133; Fri, 17 Jan 2025 08:30:25 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:56 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-11-tabba@google.com> Subject: [RFC PATCH v5 10/15] 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-Rspamd-Queue-Id: 7AAE4A001E X-Stat-Signature: opjr9xh1i3wed4fyhxik9nhqdup714xb X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737131426-913978 X-HE-Meta: U2FsdGVkX1/5fa+qMPebho9M1CR7fQKMkFruw3yZOHOwowNqQQxx14n0yMW1zNmLMj7aQonmhNJ1rtIaKGw44zAQlNPkWnuge4ai/nHDNtqTMcXFPeg8Sm0Mn/wjbO+zYddWntwLAaWQcZ8f0zASgdnD+CDHXDj5r/+C2AqQKuYl0D1Ay6mL/kflRmwJeM3jTKLLGRS9K9Mr2sb/qE4tYR2JBJHetk7k+MWhu3NgRusGWh611LW52kMH0fS2TMPwLQOzEn/f7Rnu8xW0hC30i8g7gri04UkUK1ByapFrLG2Ym/7SbNAyb2JfQ0y3ADGciT1x6Gh2Nm3wYWK90Yeh9QRX/yVqFIKfzZibwXlGGVhYO2qynqhn3IC1AKKqvWcxi/m5EoYLXdh30BwsX4JiMQUrPILSKKSJ6Kia2NlQC5JEsEVEFqCOHotUbXm4x1PoNQm4h5vHTIg3/l2cFCNC7T1JC7m9zhQrqD/TEm9MQNoNYCblxNj5l9Q7iizCzj7bEQon9LykK4dfoA5TA2JiXqenQ8OIgtZkirPV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owvJsqNXfw8cEhR6JELeZvpZMxfzDcJ4PpMasrZybwSPHsvfYEgDg0jnwunXjioGA+/VrXPhBV5uw9p+DGyzYdow== 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 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 + tools/testing/selftests/kvm/guest_memfd_test.c | 7 +++++-- virt/kvm/guest_memfd.c | 6 +++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index f15b61317aad..2a8571b1629f 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6383,6 +6383,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 021f8ef9979b..b34aed04ffa5 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_MAPPABLE (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 ce687f8d248f..04b4111b7190 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -123,7 +123,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; @@ -134,7 +134,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_GUEST_MEMFD_MAPPABLE)) + flag = GUEST_MEMFD_FLAG_INIT_MAPPABLE << 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 159ffa17f562..932c23f6b2e5 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -939,7 +939,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); @@ -968,6 +969,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;