From patchwork Tue Mar 21 20:15:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13183218 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 8C5D1C76195 for ; Tue, 21 Mar 2023 20:15:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C2B066B007D; Tue, 21 Mar 2023 16:15:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BB2B86B007B; Tue, 21 Mar 2023 16:15:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7B9B6B007D; Tue, 21 Mar 2023 16:15:48 -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 9936C6B0078 for ; Tue, 21 Mar 2023 16:15:48 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6A058C0192 for ; Tue, 21 Mar 2023 20:15:48 +0000 (UTC) X-FDA: 80594011176.29.F4E2FF5 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf17.hostedemail.com (Postfix) with ESMTP id A8C9E40008 for ; Tue, 21 Mar 2023 20:15:45 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=nkDLVeFO; spf=pass (imf17.hostedemail.com: domain of 3cBAaZAsKCCE79HBOIBVQKDDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--ackerleytng.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3cBAaZAsKCCE79HBOIBVQKDDLLDIB.9LJIFKRU-JJHS79H.LOD@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=1679429745; 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=SASYwsYhAQSqNIE6HUln5R808O00UDaM+ugmJxC1i6Y=; b=SCVN6i0D4EUHxxGRSpMAO+M5/UaRU3NzX55Y7H7wCyQXXnUyhWXfrMSz2ic9hhQwUUFjxN 3sJlafbv9jciu6weu86kazgX/9c6Ir/oLF+3QSNuQ6egrF8uWmj5DaOBuLe1NEGfXdkgHk 44C2zUScKzn0nJD4ra/mstwYT5vmjq4= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=nkDLVeFO; spf=pass (imf17.hostedemail.com: domain of 3cBAaZAsKCCE79HBOIBVQKDDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--ackerleytng.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3cBAaZAsKCCE79HBOIBVQKDDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679429745; a=rsa-sha256; cv=none; b=7mXkN/7c2VHRp1y/o4e7WuMANPCfyYghDd/txSXDSHAaAORGt7DPJ1ArnqNzzFYUKNqpOc mGnCL6j4duOwrLVZnQro5Kdf/oqTaRUbqhbrvUPl9t1HF1JZxtHnV0C20JNNzL03a0CLPM WkcE8gMKKogHvo/ZfKz+Tmo9csvCbv0= Received: by mail-pl1-f202.google.com with SMTP id p9-20020a170902e74900b001a1c7b2e7afso4745069plf.0 for ; Tue, 21 Mar 2023 13:15:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1679429744; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=SASYwsYhAQSqNIE6HUln5R808O00UDaM+ugmJxC1i6Y=; b=nkDLVeFOEtNHIk2/9xYa/CQ5Y+2ViSmqdwKS37RKpN9fN1MXmDMz1OU9puFW4g8bxR vii0vfViksMKRckdTjgGuQRIZo78lhmcmIzghCZBBHxvW/ndiQpVU4djvbvM+g/7wUMs +h4BMeKpfniUSkYQ2+iXYK1OzQ6Zixd18XxuCFovZYEJjKkL+JZVoFx8wgMxsnNIhhrW cwAnxft2O57Rn97HBOHm6iE115uNcFbAUa0j/WlnnPQnSn42zEaUa3fkNCQ6d75oG0jY i+hqkZSaarDOufSFczQvwQKpFfhRDRSFIn/93xiAf3T7ZrsK4NycMXJEfB2VVpheLbup 9TAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679429744; 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=SASYwsYhAQSqNIE6HUln5R808O00UDaM+ugmJxC1i6Y=; b=0+YGn5ZikpAFzl1U5vU4fVUeR5I0eWww9lh31JivTHgNsH2KzdoOgr5gHJSxH4EMLP QMGHnTyE5eO/U/Yvd17E5R1wdglE+xjt+91rxq0aureP6zJ0BNvh1UhExeZwZXNhEhji KDqBucBMT1ERWaJuP1lsyIwAlYn7380QnIhvb/iDMWYAQ8S9X32/CtH9ExsI63gybD7t GM3e4kZIXv6U46/XCB4gUGVEho9Vf/+6GWQUYz/2TpyDYrQ2byWtmGPeXkcDopCw67Cv uXNYxjhGNIiNGeA5VzcoThywjF5aJ0gU9N4p/JFCYClde3S3r1kuyf9dXWg6lVXcJO2f e+Sw== X-Gm-Message-State: AO0yUKWFMrwNzkCRsgszR1cKVRs8s4WEF23DAhNxaqhLScjBbIS57+qT oE9L1R4fJ9PlzcFgCCWpLjGmPTzMNtdTvSv60g== X-Google-Smtp-Source: AK7set/HvJzxMftd1uCLemfdr7EZh/avv/uPbf4VraKazWicqnhqmpj6db9AHljzqSRdUL4ivIO6IsbGNBTLA3WbfA== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a17:902:b612:b0:1a0:51f7:7f5a with SMTP id b18-20020a170902b61200b001a051f77f5amr137446pls.13.1679429744410; Tue, 21 Mar 2023 13:15:44 -0700 (PDT) Date: Tue, 21 Mar 2023 20:15:32 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.rc2.332.ga46443480c-goog Message-ID: <6e800e069c7fc400841b75ea49d1227bd101c1cf.1679428901.git.ackerleytng@google.com> Subject: [RFC PATCH v2 1/2] mm: restrictedmem: Allow userspace to specify mount for memfd_restricted 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-Server: rspam07 X-Rspamd-Queue-Id: A8C9E40008 X-Rspam-User: X-Stat-Signature: ndm8ak4watocb75s8ff46ncnxx3hxbmh X-HE-Tag: 1679429745-614087 X-HE-Meta: U2FsdGVkX18GPOxnUNc2s0ZW01r6fYG6Z74A6nXMPB+c4DG/eYLwvLFYbazxQRuonJuW0IaJfRKmxIuEero6QKu1Prf7C/fEtaLdfFNm0vlWxZw/5mYGVYsNCbg+9K9TVdoUPC4ZPE1I8sxLInvFGAOw4h5suYraJArJ/wZeMK67uwKP7QYg2u8HTd/zgD4u+qLC6BxZ5ak9T3ajAEMvF9v1tzlFP6ncl8/6ghmHiwUHJpY6rkn0urtnq4y6UrYHyk7o13vhd7oZJ4m1HqVaCvPwm3X5wwn/Th8/4UPyEhkB4OZdJP6ze15PGAV88qLtwbghjawlhTF6vIvRqa5JnTMIi13QRjTDOhQH4NdoQAv6SIbmJTn8zzPrnY5KXxB+/nJxF1dqidMN6FAocQx+W0gaUWJWabVeM943Aw4F6MQngFFfQrjF3Wx8UPjdBur/3QxfpmZVZUYDzIi+FkmDKh91VmqbVR4o0PcZksgl2iYnnVxyhrOxrz/AnAVPzNGY+rPDgxzGSqHrIPip9Pd8cxWKA/hd7UYUlvack3y6GhVUdL37PAfT2cyFTIm3aOdIhTaQpxcI637EVTA12mXD9aOaQHt2/Q/AhjhCHmssbIJIitMXT+yMCFxy7RkPsVIB+EJ+NNtfTIClEc+MyjVV4gZhcU8hRkCI1yMe53D+9gte5295cMT4HmJPqxGalaYj8LLzWO23rjrtbwOCiqGTflcBoDJH8mNP8kRkVgtPKp3fRcGxnDOfEJ/4oM1BO7Nq6qzWWccYJiE56f2/oGSza1Z68RONURGvpJHQ//wCjJZOMG8LD1XW/TKmoCHVepRVFUUdtwWeeXXVqcUsysXRNZ0xIZDqHKuJ5C7FZpBdFZfU6rxHtKDS1ymE3EpFQ4VHtG3KYsceUiBQUfVlVbt2TBvw6m3ssPRZZrpEGWB/mQl9RoDsvsmzITISR01BOHlHhTApFaMAlTGuTnlrqWf NeC0FVGs bAsEzB8Xb05h9DaCEebRCvBsdXrfRl5ugDWbXHvLI7B8PSg1IZlH9VH+P+m3iQpywFebbFjiCuIcgEW8AX+EEnNSdgrtwHI5VzyTgAApbrK8TR3++EdOcZbgU8d3exDbHkW3pygpD9TdZkB10NXMjTYgd5S2LFDnjUFsQhecRYC013a4Fg8OXb9Th1iVSEhk+Da/Pv89V91nsh7ymSTTm4spkpobIZk7bfL7RyKDxFi6hesXsB1OD0CYiSbkacH2BU90dWcCsVnYbXFh1TYW5iQd32L018XfobSnctAbE4iX+C+q1ATN5k5sHp6X0DE4AVA0t+IVxRP6VWzv9Rm8W6uA28Hqoxo9z0wT8oGKMHscjqm8VoCU+1r8vaS2/TYuTr2MSVt6aNzWt5Vw3WDytSLY6nIv+2hyV7KTBXyh4r/ILsQc2QN7TiPMLi79HWJbalSHhnACNIMDi3160Oj5sQQXOD2MMBSzf0iJS6Z9QJaIiR0cGX94V5DW3FMyQs3u6ipCH+AWI5Es6OcjL7ShAzL/tMnGjk/DTMcPOAQkQJrgQCTHNCw73Jp8ftRzvkrDsecu/V0HPpnfS5WutG93vMCes++3U/YL0CYuuDvApmnRCilaIHnoYpIDQ2L2UqzuqPJzTOavR1gs5u+PM6UaOO6u9hfoMnef+KClwhV7SC+fefVmVL1azwN4cY930f+1PItylpz9MNgK6xuiZlbO5BE5asw== 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: By default, the backing shmem file for a restrictedmem fd is created on shmem's kernel space mount. With this patch, an optional tmpfs mount can be specified via an fd, which will be used as the mountpoint for backing the shmem file associated with a restrictedmem fd. This change is modeled after how sys_open() can create an unnamed temporary file in a given directory with O_TMPFILE. This will help restrictedmem fds inherit the properties of the provided tmpfs mounts, for example, hugepage allocation hints, NUMA binding hints, etc. Signed-off-by: Ackerley Tng --- include/linux/syscalls.h | 2 +- include/uapi/linux/restrictedmem.h | 8 ++++ mm/restrictedmem.c | 63 +++++++++++++++++++++++++++--- 3 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 include/uapi/linux/restrictedmem.h diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index f9e9e0c820c5..a23c4c385cd3 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -1056,7 +1056,7 @@ asmlinkage long sys_memfd_secret(unsigned int flags); asmlinkage long sys_set_mempolicy_home_node(unsigned long start, unsigned long len, unsigned long home_node, unsigned long flags); -asmlinkage long sys_memfd_restricted(unsigned int flags); +asmlinkage long sys_memfd_restricted(unsigned int flags, int mount_fd); /* * Architecture-specific system calls diff --git a/include/uapi/linux/restrictedmem.h b/include/uapi/linux/restrictedmem.h new file mode 100644 index 000000000000..9f108dd1ac4c --- /dev/null +++ b/include/uapi/linux/restrictedmem.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_LINUX_RESTRICTEDMEM_H +#define _UAPI_LINUX_RESTRICTEDMEM_H + +/* flags for memfd_restricted */ +#define RMFD_TMPFILE 0x0001U + +#endif /* _UAPI_LINUX_RESTRICTEDMEM_H */ diff --git a/mm/restrictedmem.c b/mm/restrictedmem.c index c5d869d8c2d8..4d83b949d84e 100644 --- a/mm/restrictedmem.c +++ b/mm/restrictedmem.c @@ -1,11 +1,12 @@ // SPDX-License-Identifier: GPL-2.0 -#include "linux/sbitmap.h" +#include #include #include #include #include #include #include +#include #include struct restrictedmem { @@ -189,19 +190,20 @@ static struct file *restrictedmem_file_create(struct file *memfd) return file; } -SYSCALL_DEFINE1(memfd_restricted, unsigned int, flags) +static int restrictedmem_create(struct vfsmount *mount) { struct file *file, *restricted_file; int fd, err; - if (flags) - return -EINVAL; - fd = get_unused_fd_flags(0); if (fd < 0) return fd; - file = shmem_file_setup("memfd:restrictedmem", 0, VM_NORESERVE); + if (mount) + file = shmem_file_setup_with_mnt(mount, "memfd:restrictedmem", 0, VM_NORESERVE); + else + file = shmem_file_setup("memfd:restrictedmem", 0, VM_NORESERVE); + if (IS_ERR(file)) { err = PTR_ERR(file); goto err_fd; @@ -223,6 +225,55 @@ SYSCALL_DEFINE1(memfd_restricted, unsigned int, flags) return err; } +static bool is_shmem_mount(struct vfsmount *mnt) +{ + return mnt && mnt->mnt_sb && mnt->mnt_sb->s_magic == TMPFS_MAGIC; +} + +static int restrictedmem_create_from_file(int mount_fd) +{ + int ret; + struct fd f; + struct vfsmount *mnt; + + f = fdget_raw(mount_fd); + if (!f.file) + return -EBADF; + + mnt = f.file->f_path.mnt; + if (!is_shmem_mount(mnt)) { + ret = -EINVAL; + goto out; + } + + ret = mnt_want_write(mnt); + if (unlikely(ret)) + goto out; + + ret = restrictedmem_create(mnt); + + mnt_drop_write(mnt); +out: + fdput(f); + + return ret; +} + +SYSCALL_DEFINE2(memfd_restricted, unsigned int, flags, int, mount_fd) +{ + if (flags & ~RMFD_TMPFILE) + return -EINVAL; + + if (flags == RMFD_TMPFILE) { + if (mount_fd < 0) + return -EINVAL; + + return restrictedmem_create_from_file(mount_fd); + } else { + return restrictedmem_create(NULL); + } +} + int restrictedmem_bind(struct file *file, pgoff_t start, pgoff_t end, struct restrictedmem_notifier *notifier, bool exclusive) {