From patchwork Fri Jan 10 16:58:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Isaac J. Manjarres" X-Patchwork-Id: 13935156 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 DA665E77188 for ; Fri, 10 Jan 2025 16:59:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 717958D0007; Fri, 10 Jan 2025 11:59:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C8C38D0003; Fri, 10 Jan 2025 11:59:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 592058D0007; Fri, 10 Jan 2025 11:59:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 3053D8D0003 for ; Fri, 10 Jan 2025 11:59:15 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D7B6644E81 for ; Fri, 10 Jan 2025 16:59:14 +0000 (UTC) X-FDA: 82992152628.26.8F4A57A Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf08.hostedemail.com (Postfix) with ESMTP id 1AF5F160019 for ; Fri, 10 Jan 2025 16:59:12 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=YqTzvsQL; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 331GBZw4KCMoy8qqs2q3zq77u8w44w1u.s421y3AD-220Bqs0.47w@flex--isaacmanjarres.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=331GBZw4KCMoy8qqs2q3zq77u8w44w1u.s421y3AD-220Bqs0.47w@flex--isaacmanjarres.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736528353; a=rsa-sha256; cv=none; b=6X4EfYlrYytE7HpPeFEv/TMT4yrrp8xW4FNj2TDepAHnBIPcK3VfDVj3yuC9Io9idLZyOK SZih8mGXqAlmCooZWpMBTXLYfHHFky9v543t2sQFTHGysWHZp/qPI/kd4ND9TJ73Z19qeS q+CUNpPmTDcOxnyTNJRSc5kCdH+ALiw= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=YqTzvsQL; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 331GBZw4KCMoy8qqs2q3zq77u8w44w1u.s421y3AD-220Bqs0.47w@flex--isaacmanjarres.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=331GBZw4KCMoy8qqs2q3zq77u8w44w1u.s421y3AD-220Bqs0.47w@flex--isaacmanjarres.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736528353; 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=zKt1DPL8dLAaFtUnRUzSqTKiJk4PTPPAGd+NFYdeSwg=; b=2QKBWwwGKDKnSr0mGao9yzfKH+yt3uxVDsD85C0PjsTBjbAEJrRCsKctZGqWm9Jla2qa2Z u6a5seSgY17CQb1mdP+mzICsZUQTEZ49+wzr6kgxvHo628PV9m3Ut+NmsXbVCgwRII0i4T Gj81SabfOeaU/Os6aRlfpaa19rzBL5g= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef9204f898so4091483a91.2 for ; Fri, 10 Jan 2025 08:59:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736528352; x=1737133152; 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=zKt1DPL8dLAaFtUnRUzSqTKiJk4PTPPAGd+NFYdeSwg=; b=YqTzvsQLFe/hjC95OLAo8sBa2wWQ5e4Kqhsn7lVxVYSoRs1NGCE0xcx7fw6fVNmsHI kdSO+JbNX2UpZRUI7ICvdlhXFBJ/XWq56GZJZJIZ7uSTyBGQufXoTTgnxYuyajKDB4TO S/nAY9PxGsMwyZxl/uX+KcDanE06u/WBb21ZVhM1384qOvk4yHr9nXWqoP1YE0Gaq/IK SdanKhzHGDUbyAbmMpJNqxgG4te3Q32ez54J6qfAzEN0spjdD/NYkzhDzYj71PsN4qNH BiilJHOjcgZe6uiIbixKIC7dLvR+PJeLVpTgrP2XZ7jvW0mVUbUVOr+IgMZ5rWJxB+y1 NS1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736528352; x=1737133152; 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=zKt1DPL8dLAaFtUnRUzSqTKiJk4PTPPAGd+NFYdeSwg=; b=Xo1eSUIC8W1impALjAKdt2uEVAgJGKfBF4L+YOAphomzOs9fTEZ9BYyiBvcLe+kj7K //AchW3Yavww20r4K4SAx0FXrSgFOXtj6FC2gF5SRu6YlwGj3DPXKyO/NbARCsLjosyz m/RlbC6t5o1dsU/3LvYntgXJIfOEDdnkCuorsRevNlD506zhrek/6dar4FsyHzBNPPw9 xyd3X354MqVdK2NWcKgGJDkevVCdfVlPWzkX0uAdNY8YKju7TLzoOSg7K9LtgzgHNENu TEWG7N116f0cYA3m/k3bDxhMwQQr0YmN4iu1e31hHsveRYw/wtFe0OrF/wZmKJlMJFG4 IDXg== X-Forwarded-Encrypted: i=1; AJvYcCXf/JyI5mXbldnwxgMyF5IhuE0d+jcnngLu4RTsEVJJjdaoXMgCKn5bCeQtvWBhsNKpRjOegNF5MA==@kvack.org X-Gm-Message-State: AOJu0Yy2DCRJYfNVhEWCJrz3wt8RMYWzYM09vnSploNgRjXGF7Z7YtRg oEjYypKJA8A1EcsZxf1EqloRJt7gV99lp4FynJuTRihGPOllPRcDC62w0EofdzM9sGamOL8tdxm P+QU0kzCKIVXS6A8v+gdvoEYhmx13lhQrwg== X-Google-Smtp-Source: AGHT+IH4zNEHH+SxAVpiiIGRgV/DdPoVtFRvZbNB2Vrw97PKwLA6EhCRn54go/tXkZHsIwhEFHnKyvwRUhcE1SM0KdlLzw== X-Received: from pjbqi14.prod.google.com ([2002:a17:90b:274e:b0:2ef:d283:5089]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:33d2:b0:2f1:4715:5987 with SMTP id 98e67ed59e1d1-2f548f32f7fmr17624178a91.9.1736528351924; Fri, 10 Jan 2025 08:59:11 -0800 (PST) Date: Fri, 10 Jan 2025 08:58:59 -0800 In-Reply-To: <20250110165904.3437374-1-isaacmanjarres@google.com> Mime-Version: 1.0 References: <20250110165904.3437374-1-isaacmanjarres@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250110165904.3437374-2-isaacmanjarres@google.com> Subject: [PATCH v4 1/2] mm/memfd: Refactor and cleanup the logic in memfd_create() From: "Isaac J. Manjarres" To: lorenzo.stoakes@oracle.com, Andrew Morton Cc: kaleshsingh@google.com, jstultz@google.com, aliceryhl@google.com, surenb@google.com, "Isaac J. Manjarres" , kernel-team@android.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1AF5F160019 X-Stat-Signature: awq8pkxdkoku35i1wasn9prazpw4mt41 X-Rspam-User: X-HE-Tag: 1736528352-129555 X-HE-Meta: U2FsdGVkX187oMcwa3ot5PMiBE4LhMiFJEZzj0jR9c9wD33852L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lZeypHBvoFtL8O6QH05tA7CTvT+XJJn6CWZRNraPyd3EmCXU9f9ctGbFnaOCrg0lVjX1ey+kF/PF07NpdTd81R9gyH8SinXmQlv44vleiaPJ04HgewpUuEkYUp5cDI11aOiHRlAsztdy8TFwzZVw1cwHjOaZmfLSt1RgLApz9+99LU3mcQahChboPINzp+HbxOHaqaNFA3qn12OsIcNG7XHjgBCZISZ+Ysjkp9w2FXdjs4Ix/XrytxSco8T/W8+D5NcnmIbCtNUuSXnnlIJhVPIz28oVvzvrx5qiDezfc3AYlwAGr6EzlEs3LtAYKebm1+5/3NjZOyps/JlYWdjgrOuB5fSM8kmClroCiAFxliPNlDsnH7bQf7RcnUFMkGHq0UfMugTXjHz4kpaPTqYtWwCHjfY0f5fp2Wb6ZJGcC8HI1lGBckQfoyhdTYKHWKZvfpoj1aI5vRVO6tYImnHP53Vs1C8J0OFV1vnGqNCubcLhEmeruzI7tfg3ybOlvLz+xXHp+5nLGIgiA3HaxesYpK87uCJAQsJLAo= 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: memfd_create() is a pretty busy function that could be easier to read if some of the logic was split out into helper functions. Therefore, split the flags sanitization, name allocation, and file structure allocation into their own helper functions. No functional change. Reviewed-by: Alice Ryhl Reviewed-by: Lorenzo Stoakes Signed-off-by: Isaac J. Manjarres --- mm/memfd.c | 81 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index 5f5a23c9051d..04d9e2a23df8 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -369,15 +369,9 @@ int memfd_check_seals_mmap(struct file *file, unsigned long *vm_flags_ptr) return err; } -SYSCALL_DEFINE2(memfd_create, - const char __user *, uname, - unsigned int, flags) +static int sanitize_flags(unsigned int *flags_ptr) { - unsigned int *file_seals; - struct file *file; - int fd, error; - char *name; - long len; + unsigned int flags = *flags_ptr; if (!(flags & MFD_HUGETLB)) { if (flags & ~(unsigned int)MFD_ALL_FLAGS) @@ -393,20 +387,25 @@ SYSCALL_DEFINE2(memfd_create, if ((flags & MFD_EXEC) && (flags & MFD_NOEXEC_SEAL)) return -EINVAL; - error = check_sysctl_memfd_noexec(&flags); - if (error < 0) - return error; + return check_sysctl_memfd_noexec(flags_ptr); +} + +static char *alloc_name(const char __user *uname) +{ + int error; + char *name; + long len; /* length includes terminating zero */ len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); if (len <= 0) - return -EFAULT; + return ERR_PTR(-EFAULT); if (len > MFD_NAME_MAX_LEN + 1) - return -EINVAL; + return ERR_PTR(-EINVAL); name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_KERNEL); if (!name) - return -ENOMEM; + return ERR_PTR(-ENOMEM); strcpy(name, MFD_NAME_PREFIX); if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) { @@ -420,23 +419,28 @@ SYSCALL_DEFINE2(memfd_create, goto err_name; } - fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0); - if (fd < 0) { - error = fd; - goto err_name; - } + return name; + +err_name: + kfree(name); + return ERR_PTR(error); +} + +static struct file *alloc_file(const char *name, unsigned int flags) +{ + unsigned int *file_seals; + struct file *file; if (flags & MFD_HUGETLB) { file = hugetlb_file_setup(name, 0, VM_NORESERVE, HUGETLB_ANONHUGE_INODE, (flags >> MFD_HUGE_SHIFT) & MFD_HUGE_MASK); - } else + } else { file = shmem_file_setup(name, 0, VM_NORESERVE); - if (IS_ERR(file)) { - error = PTR_ERR(file); - goto err_fd; } + if (IS_ERR(file)) + return file; file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; file->f_flags |= O_LARGEFILE; @@ -456,6 +460,37 @@ SYSCALL_DEFINE2(memfd_create, *file_seals &= ~F_SEAL_SEAL; } + return file; +} + +SYSCALL_DEFINE2(memfd_create, + const char __user *, uname, + unsigned int, flags) +{ + struct file *file; + int fd, error; + char *name; + + error = sanitize_flags(&flags); + if (error < 0) + return error; + + name = alloc_name(uname); + if (IS_ERR(name)) + return PTR_ERR(name); + + fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0); + if (fd < 0) { + error = fd; + goto err_name; + } + + file = alloc_file(name, flags); + if (IS_ERR(file)) { + error = PTR_ERR(file); + goto err_fd; + } + fd_install(fd, file); kfree(name); return fd;