From patchwork Tue Jan 7 18:48:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaac Manjarres X-Patchwork-Id: 13929509 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 CB06FE77197 for ; Tue, 7 Jan 2025 18:48:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47E0F6B0083; Tue, 7 Jan 2025 13:48:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E1BA6B0089; Tue, 7 Jan 2025 13:48:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20AB46B008A; Tue, 7 Jan 2025 13:48:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id F11E56B0083 for ; Tue, 7 Jan 2025 13:48:16 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8439CAFF85 for ; Tue, 7 Jan 2025 18:48:16 +0000 (UTC) X-FDA: 82981540992.19.F58DA9F Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf28.hostedemail.com (Postfix) with ESMTP id B309DC000E for ; Tue, 7 Jan 2025 18:48:14 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Cse7asJL; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 37XZ9Zw4KCBQ2Cuuw6u73uBByC08805y.w86527EH-664Fuw4.8B0@flex--isaacmanjarres.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=37XZ9Zw4KCBQ2Cuuw6u73uBByC08805y.w86527EH-664Fuw4.8B0@flex--isaacmanjarres.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736275694; 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=rk8gGRBUBzYGA/wGbzmWKmNsJkZ0zUQC1Vm7cFkzsUg=; b=B1Q7ZIPLuBQ+HZf5JPB/QbcnoiijV96GjiC2k4GPvNUFyFeOfspOLIJJgVN9RPQJBab+tk 26cK2Y4bOPppwHO1uwuHJZ60pV+prWBqFUQ5QWcFKYihFDzn1zEEOYn0aVnsXMu/31HL/E TyjvayRX4vImm1eyk7gCMVx620dKkM4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736275694; a=rsa-sha256; cv=none; b=tH7LZXlhtVzKWtULY4ZR6J8kk8BO6f17Vh9jDuIhPubiinCIBZbnoD1XnNbmkhHLZfItRc XgBTjg/l2kYIFGdBK6Q9/dfO/JdmR69XdIpyxL7Np/KjFrsMMFEim+iWiXyaMCwU2RT8fj G+veml7Pb/zs5ZdigOY3mhtZ6ficKKI= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Cse7asJL; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of 37XZ9Zw4KCBQ2Cuuw6u73uBByC08805y.w86527EH-664Fuw4.8B0@flex--isaacmanjarres.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=37XZ9Zw4KCBQ2Cuuw6u73uBByC08805y.w86527EH-664Fuw4.8B0@flex--isaacmanjarres.bounces.google.com Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-21648ddd461so286592785ad.0 for ; Tue, 07 Jan 2025 10:48:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736275693; x=1736880493; 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=rk8gGRBUBzYGA/wGbzmWKmNsJkZ0zUQC1Vm7cFkzsUg=; b=Cse7asJL/JSFDgr+jyukzMFYqFSYUZYzL4vbBSvIH7R63idUllYc7Bb+mZb3FS08h0 ndEdgncz35EjOeMegqrLM5SQSP9wmCNZ75AKOXqbH03qE4DvJvDaB4fkG8SDi+LPQl5M 3QOs5+ggWD5QUYJT5eOABRivePa4AQu0+qdP7TnkcWXBwzWafXTekfGtKy8FMk9fOm/Y 9Nfjs9mo/Ua6jUbiZ5LOzLjgyNNlrgmdH2u2Tl6Zmqv1Iab+3D0qh1WMm56AyMODHDSJ W8lWHxs922+lNTh1B2dwBM1mvtM0n2H4+eHZRjPcwAoR9ZAkD3IuVDbbC6KcWup9VzaK zlCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736275693; x=1736880493; 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=rk8gGRBUBzYGA/wGbzmWKmNsJkZ0zUQC1Vm7cFkzsUg=; b=IzfgW6gKl6YmBMbe+ImJIhVD3dMPkG/n3+O7Tm5jAXhu2qNDmMdUcXGRseyzsIWLmt EB7qGfBSjaFTCM5Les00+k8gW+MJArVoet8Hvxo/8Arzn/mfibnlpKIIzHPtz5EnFv3+ UfcYjO41yb6+ACKmtQ0vazGMemTbXm3VFd8l2KyvA1Fq8aNDtHHXGLsrs4NUSp+k5h0+ kOCi83+nX1pV+n0/bmYwIMh4qhMXsI90rrulpXqMWyQJPZdSxFcvOuYY+ApUdsO2a/21 zlVWM3GTLAFIpiln2hIG+CINENeVUPTdHh+JYE6d/2ErgFb49ThO3LgCQhe2UCMK/l+5 aKIQ== X-Forwarded-Encrypted: i=1; AJvYcCWtz2WapXh8M6ZfwnaunDAkt0MLBwmH5sHztj6s8vMe+7Tpc+OqcngNFk1fUgXFHkCcGr7s5AOCXg==@kvack.org X-Gm-Message-State: AOJu0Yz6v/+xLpuUUrat+656tsbBQ2yvxkNnLkObIYEw539+jOiT1Wxp qpTxLZvs0WhTBfqcRnQl7oSEpeo2Y5TXEi/itW74gJ335b3Bu69gj3ue1z2AkpFLBlweI3HKunS O38ElqTKq6veug0eAd1dPlztd5+UIzssdfA== X-Google-Smtp-Source: AGHT+IFdAzLXbO2JkFr6H9h5q7HlYAi/wupYUCRA9P6KEwg4AV1/VAJRWO1ih6khzEfbAr8w3y0I1JH1Peh32phJwTXDJQ== X-Received: from pfxa5.prod.google.com ([2002:a05:6a00:1d05:b0:724:e19a:dfd1]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:8cc4:b0:1e2:5c9:65e0 with SMTP id adf61e73a8af0-1e88d2ec106mr351161637.32.1736275693607; Tue, 07 Jan 2025 10:48:13 -0800 (PST) Date: Tue, 7 Jan 2025 10:48:01 -0800 In-Reply-To: <20250107184804.4074147-1-isaacmanjarres@google.com> Mime-Version: 1.0 References: <20250107184804.4074147-1-isaacmanjarres@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250107184804.4074147-2-isaacmanjarres@google.com> Subject: [PATCH v2 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: om7mpttb6yj76hdncectr5h4dnscpmot X-Rspamd-Queue-Id: B309DC000E X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1736275694-572638 X-HE-Meta: U2FsdGVkX18K4ZKaHbkQZt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0cxOF7xjEfGS0VCtumR+Gh3WNC0yLsNlc0Ysyj99bva++c50itos1JFNKaHC5FHsgMRhb3XXS4+m3DsHoZ/fo0cvHGhMa7HTe+VMDnREZ6JHQTcFJH+1RPPXVkQ0kE26y7hD6GLpZdpnnKNAxRvNVbqisECPilEyEXGEkI3TlJAYogA2VfYdvCAUGdVOvlDLBsMaFjNk+QyMe2laG2amLMWkVLFvN0SErTQXUNGUmwYsmKOG9V1sJnHe3ATtVOmp+0sq1M4NvoWG4p2tJpIB8CacXBMyITBMw6hRgx7l9BN5r3K/U+dEiFHhdNYQi/8O70sYOugBrxP1phrplZLAxJ76cNdFnabCtj7nrZbnj2tNB7yr/9FvZtcQchXM6b4+bZnp6F0N014xUXPY/Rmwcdj6z9Zp5VcRrdSAr5jxulh9jtffPIvjFLkKorkkWtY99D03/LAkyrSD8Y2Besrhz/cIbjjBgGKw6JfOTzR/jaNOSSCNhveC5U+KAttmm5TnYjY/wggVbtIgKzekjM8syouiJCgnpZx8uHtNHCLLoPaSOns1SsvjNsQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.011821, 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 check, name creation, and file creation into their own helper functions, and create the file structure before creating the memfd. This allows for simplifying the error handling path in memfd_create(). No functional change. Signed-off-by: Isaac J. Manjarres --- mm/memfd.c | 87 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 31 deletions(-) diff --git a/mm/memfd.c b/mm/memfd.c index 5f5a23c9051d..a9430090bb20 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -369,16 +369,8 @@ 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 memfd_validate_flags(unsigned int flags) { - unsigned int *file_seals; - struct file *file; - int fd, error; - char *name; - long len; - if (!(flags & MFD_HUGETLB)) { if (flags & ~(unsigned int)MFD_ALL_FLAGS) return -EINVAL; @@ -393,20 +385,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); +} + +static char *memfd_create_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,11 +417,22 @@ 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 *memfd_file_create(const char *name, unsigned int flags) +{ + unsigned int *file_seals; + struct file *file; + int error; + + error = memfd_validate_flags(flags); + if (error < 0) + return ERR_PTR(error); if (flags & MFD_HUGETLB) { file = hugetlb_file_setup(name, 0, VM_NORESERVE, @@ -433,10 +441,8 @@ SYSCALL_DEFINE2(memfd_create, MFD_HUGE_MASK); } 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,13 +462,32 @@ SYSCALL_DEFINE2(memfd_create, *file_seals &= ~F_SEAL_SEAL; } - fd_install(fd, file); - kfree(name); - return fd; + return file; +} -err_fd: - put_unused_fd(fd); -err_name: +SYSCALL_DEFINE2(memfd_create, + const char __user *, uname, + unsigned int, flags) +{ + struct file *file; + int fd; + char *name; + + name = memfd_create_name(uname); + if (IS_ERR(name)) + return PTR_ERR(name); + + file = memfd_file_create(name, flags); + /* name is not needed beyond this point. */ kfree(name); - return error; + if (IS_ERR(file)) + return PTR_ERR(file); + + fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0); + if (fd >= 0) + fd_install(fd, file); + else + fput(file); + + return fd; }