From patchwork Thu Jan 9 18:59:04 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: 13933154 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 DCDF2E77197 for ; Thu, 9 Jan 2025 18:59:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6FB246B00BA; Thu, 9 Jan 2025 13:59:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 686196B00BB; Thu, 9 Jan 2025 13:59:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4FFBF6B00BD; Thu, 9 Jan 2025 13:59:20 -0500 (EST) 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 2F9AB6B00BA for ; Thu, 9 Jan 2025 13:59:20 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CB6381203B8 for ; Thu, 9 Jan 2025 18:59:19 +0000 (UTC) X-FDA: 82988826438.28.6822707 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 0B83618000C for ; Thu, 9 Jan 2025 18:59:17 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uHs2Ez5h; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3hByAZw4KCAEjtbbdnbokbssfthpphmf.dpnmjovy-nnlwbdl.psh@flex--isaacmanjarres.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3hByAZw4KCAEjtbbdnbokbssfthpphmf.dpnmjovy-nnlwbdl.psh@flex--isaacmanjarres.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736449158; a=rsa-sha256; cv=none; b=EW0h/+TEYZcV5MIUCd427mwUg7kI/pzp4kj1nt2CYiWWUuQhJLbVoih0ydMLrYP0B0v4TU Umo8ldob2rqa8hZC6NrnhZ0/NQ+HehvCNIYFIgEp1rx1ZEO2Xkdq26MF3SRO/dhUlDSqzU nZsvO035yOhjCGeM5E79XWs+FQNLX50= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=uHs2Ez5h; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3hByAZw4KCAEjtbbdnbokbssfthpphmf.dpnmjovy-nnlwbdl.psh@flex--isaacmanjarres.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3hByAZw4KCAEjtbbdnbokbssfthpphmf.dpnmjovy-nnlwbdl.psh@flex--isaacmanjarres.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736449158; 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=9BM99KssmhmTboJoX+NifvNHQdZn7EvgZFYAg3A4crk=; b=PchaL3nBUrqPvxFNphd79KXQPYBeRfECCSVVrFAC+xPQqBdA2i2umVseoSwKMGMOHOkJWs LrWmuswxnresS0Iv1OaPb+TvWA2MORQZRXcx3MLvi7SxsTGHJ44IDTA3GwStt69q0F1cHz 9nBNKx3fe6UBKqgAQv8PtVzF29Drd40= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-216405eea1fso21448505ad.0 for ; Thu, 09 Jan 2025 10:59:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736449157; x=1737053957; 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=9BM99KssmhmTboJoX+NifvNHQdZn7EvgZFYAg3A4crk=; b=uHs2Ez5hLN0KfNafsVCDzSWx9X4qzIBzcutnb0byQuUQjuJW77cNHsyx5teQ9QQ2bz i6KulJ3+bLrF565UvgiYEAhs1itRMIdQDnLqJtMVlnwl8DyJBLoqD2nsIuKagUiJdQgQ B07nxxYayoWi9JHrtWsA6u6tyh1mx7V3532alA5ave+Um8YnzOflMlfHLQwUC7eLARtj zOh+ZJbo2cOT+XOfNpk/C4kL91udU8IIwaRP3Gx/fsyKaoaE4QPe6m0KhzqrIjEyfIq7 wgKwFgyPua53C3WmJV+KFnh+MbRWeeGFiAvmv21HcmzKxYak1HU4IpWO4Qb2B1V876De k2DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736449157; x=1737053957; 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=9BM99KssmhmTboJoX+NifvNHQdZn7EvgZFYAg3A4crk=; b=umBycQ/OuIU2WWMIsMFF6a3CSYkpIjid2MRS3HdPCF2Cp6D9glKLUAp2Q2BEtq315V aJmVyroeK9jIqJ82eIimlgSNe/uU1yajTApP4Thl55O5KhixfEUHYR3N8z4atANdLA9B h862rnRovgNkKn4JwMbx61aSKcJqVAJpOutP97ubOJMz8Ox1+zO2pW3s52jp9IVf8dzk CPUmKTKfPlooME3Zz5XpEXYRTeBEVGv6soG7Z6y5o5T0RUvnerciJhO2NF3aDQ0ox7qs h19En8rARD2Z9eXyvRNpDNVQaPBWYQu0w2yrIUUFWliPv3EtLKxCt1XO2GWlWuQd3cwC YsgQ== X-Forwarded-Encrypted: i=1; AJvYcCVhQmUoFxw0xQXiNf9g5JJ0rGS8SPPw46Y6YQqZH5ncCJ2dmQRSLGoc9yWRU+P5LE3VVQxKUbfF+g==@kvack.org X-Gm-Message-State: AOJu0YyNe0zV2a7W3AilwuPuOEBprOd5sdut4uf1Fs9uK8hFHxzEyMnD wxCOM/OhMnLZGqJjxOnSsYEYoFndStuLs6VpeDOSwnXRXxC2x8d5M47xj8qqRSoXPKFXrZCbvcY y4wzmQMgdyMLNw5fp8998NZX+CVKcX85bvw== X-Google-Smtp-Source: AGHT+IHA+ue8ssbg4T8El5ZJqPpI8Z5g1BzSDPCiGMlQKNfCBhMyjgYhHWpnabOxoBs97nTzEBWzGU1z3a8OuNT70fFeBA== X-Received: from pgbdr5.prod.google.com ([2002:a05:6a02:fc5:b0:7fd:3282:aa47]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:cecd:b0:215:6995:1ef3 with SMTP id d9443c01a7336-21a83f3469cmr96318765ad.3.1736449156806; Thu, 09 Jan 2025 10:59:16 -0800 (PST) Date: Thu, 9 Jan 2025 10:59:04 -0800 In-Reply-To: <20250109185908.1006310-1-isaacmanjarres@google.com> Mime-Version: 1.0 References: <20250109185908.1006310-1-isaacmanjarres@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109185908.1006310-2-isaacmanjarres@google.com> Subject: [PATCH v3 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-Stat-Signature: 7ze86sm7xtfgq334xnwa1zsek9ukpewo X-Rspam-User: X-Rspamd-Queue-Id: 0B83618000C X-Rspamd-Server: rspam08 X-HE-Tag: 1736449157-604499 X-HE-Meta: U2FsdGVkX1+P0J+ypSWTgHQn+vCLYNYGqVAS7w49NWCXytIHyDr9BCDbcF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AJ/nV7QVL6N4fVIUNRBSJKP9jgnfO4xkDtyszWH2NJiXrkzf+H0qeHYIf2vo9uApPndvik0D+QTD0De/8Nho6IQ+rIIiilBnODZeNApBDeUMDqw6awRYa55/JK4r0mwNbj31P2NsmCSO9SXqcEovx6leRJ0Kqv/d6uzQk/tYVLcnk/fAxXqLLtBXkf1iGUBgZhpB+Kaas2niYWdD14Zqh4LNMi7EwKy7MUjwSc9FcHNaO+Jq4rt3uLN78RTybXuZjfwdUltM2zsF+guvT4Jp+QZ9MABdDKTdiYQk6AVmf1rRIwDvxoVbMqt9xIabRJ0T3Qp6DXeOHcOT86IR9UJsaVvyf5fFatxhncLLX+dNpNlU5sZwym9zRVR37aj/QTep+lQgPmTnt3ab0k425pnCRFtr4KoYbo0R1zFUJiS+kj4NZ27NQW9jJtqKzjkFAQ/3/MYP3IRYz9BIymnXLNRmf+q3jz3y6q3U2tbf1EIMEB+2k63myPZAsgJLWg++QVlPxddw25yg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000016, 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. Signed-off-by: Isaac J. Manjarres Reviewed-by: Alice Ryhl --- mm/memfd.c | 82 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 23 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index 5f5a23c9051d..bf0c2d97b940 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,7 +460,39 @@ 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); + /* name is not needed beyond this point. */ kfree(name); return fd;