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: 73051 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2000629wrt; Tue, 21 Mar 2023 13:45:23 -0700 (PDT) X-Google-Smtp-Source: AK7set+6/Jn7MxTfhJsPBI7iulgx9c74SDW8o9MKvKJ3HXMA36BJuKdZDzhmeIuyM86rZWwYzWj8 X-Received: by 2002:a17:90a:598b:b0:237:161d:f5ac with SMTP id l11-20020a17090a598b00b00237161df5acmr977315pji.36.1679431523520; Tue, 21 Mar 2023 13:45:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679431523; cv=none; d=google.com; s=arc-20160816; b=TpFIEN5Y3Qe2gm1Og3Wg1O0t2+3tDOOPUY2AAKFiPrEzBPfzWRS/PDm2roNPKME2Nl CIqDxAVJcHztu8uFk5Xy8l22hVAqr5Fb2Dxg5ouX3VXbVaxFg+IpcysUF1F7f8llHS+S Dh7yGtabuWEkkXFNDWkhRVPFSJm/nh4pHwiMvPbL9vMAsC3h9l7T4N0QLcQTF6WBpv+R co9G6ZHgW4+TGh7F5rRWlCwoWLpxD6las65A+U7UjiCPcOPGon2DLmaIINJ/NtzaRnDb igfsFY5kFdL32LfH8z64SK4wm+0Sk0SGDxvvxECtyZHEl3u55JpBqxIAL3k1TYi0IQ4A 8qtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=SASYwsYhAQSqNIE6HUln5R808O00UDaM+ugmJxC1i6Y=; b=0AnlLX7+xyrpM6nxcAapa7WaZ4UmjeDfFpqcILuAYXftJJE9rPa9EswxCERaVwjZbP MRGmilyokFRpM1+19rFTKctF6qOM5IuooDJw3mk+2coGVRjbbq73dmFWyAuJnSxaXb1/ qW0AIKo2+eCEfBpo3OG/RvLgPVnuszFOFKkzU3H413R/dF7R8Ec3Nu2zR7ywgeRWgxh7 gNNWs6wdvTrLUK7FEiuVNO/cK/48pdUebXooKThLmmjuwpXo8NAZHENyqSqvHcAw2hQP D0Z4283hZI0wCoEde9MqbyLWIBdLnDI3hJ3KjDhPQ1a5BRlt8+jm0JxrPez6EPJ5uIUb tv1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=nkDLVeFO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w13-20020a170902a70d00b00194afb5a3f9si14058611plq.164.2023.03.21.13.45.07; Tue, 21 Mar 2023 13:45:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=nkDLVeFO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230261AbjCUUQj (ORCPT + 99 others); Tue, 21 Mar 2023 16:16:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230257AbjCUUQd (ORCPT ); Tue, 21 Mar 2023 16:16:33 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E17944FCF7 for ; Tue, 21 Mar 2023 13:15:48 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id c8-20020a170902d48800b001a1e0fd4085so2022359plg.20 for ; Tue, 21 Mar 2023 13:15:48 -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=XTySm9hyje68Nwef7ABjYvKW2TjsuLNuXlrwCpkimvrLQJOcxSk4tJty/k/B21qvfd dgKJxOym0+HFovBJhQUxNn3hxrENGWlwcyXsN6BF7qsiQ3VuGHjCdORYLkaBh9igFLb+ CL297Qun/M2lJrUbMyC0C1nda+hQwC7gU1qSpiXqPjTJK2rqgQtZPXp55CXMk39UVf/H uerPtzD7NbOzaUgwswfKGz9vEYiEoNV6f+A5hwmj6274MjdoYCcSxavT7TzIb1S0iLmC rl059LjS9wbN16kuMKs/bpbYzWyhrzwwaE65QqvQAZlklT9CO7LAnYQTeoQ+9Z7v2TUT B0Dg== X-Gm-Message-State: AO0yUKWnpdhQlp0akbzGZJBAfvAXVBKYZxOd/Mcq43eoOsLcDDa22pTA bkyJN2zGNk+1tpyJBI9PoIXqmg9L1b9yhyIrpg== 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-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761011589069625488?= X-GMAIL-MSGID: =?utf-8?q?1761011589069625488?= 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) {