From patchwork Thu Jan 2 23:06:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaac Manjarres X-Patchwork-Id: 13924999 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 637A4E77188 for ; Thu, 2 Jan 2025 23:07:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E7AA06B0083; Thu, 2 Jan 2025 18:07:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E03DF6B0085; Thu, 2 Jan 2025 18:07:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA5576B0088; Thu, 2 Jan 2025 18:07:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id A97BC6B0083 for ; Thu, 2 Jan 2025 18:07:11 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2D3DBC04E1 for ; Thu, 2 Jan 2025 23:07:11 +0000 (UTC) X-FDA: 82964047866.10.DEFF72D Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf04.hostedemail.com (Postfix) with ESMTP id 9661140014 for ; Thu, 2 Jan 2025 23:06:16 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1jj7CVer; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3Gxx3Zw4KCHIYiQQScQdZQhhUiWeeWbU.SecbYdkn-ccalQSa.ehW@flex--isaacmanjarres.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3Gxx3Zw4KCHIYiQQScQdZQhhUiWeeWbU.SecbYdkn-ccalQSa.ehW@flex--isaacmanjarres.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735859205; a=rsa-sha256; cv=none; b=RGY4hT4mPMS/LlrXVt2PzD8Wrzr0JsKMih5+ghzbB6oi10ElTAhBPiXCKbRXps/EufG3i4 IzyWO3wVG/r5G0w8adxVR9Z7LvXO4/EsIKqn2ouXg5tHv1xB9BvzFPxFinZStAgkPUsQoy dNoHv+2u95jSvtjfQt9lRoVzKzPSyKg= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1jj7CVer; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3Gxx3Zw4KCHIYiQQScQdZQhhUiWeeWbU.SecbYdkn-ccalQSa.ehW@flex--isaacmanjarres.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=3Gxx3Zw4KCHIYiQQScQdZQhhUiWeeWbU.SecbYdkn-ccalQSa.ehW@flex--isaacmanjarres.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1735859205; 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=oukJOp8a/TMok9WYKtZiYjZTJSlSkLa4mKjkY8cRWFM=; b=bAk529qEn8cJJXSRdeAWwfj39YGmDHjTrcKi5+gFTqM0kq6CB/h5sMPJtpprDdk2rW4ZuO 1jqHprkWm3J1muA6PqlAKeQpnrfQa4hztc8kiLa5i1z7HjkwZ5AZYOBp5BXizkD0luzv1x ZRldz98D4GdXLRtSpQURSnNXOjkepHs= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2162f80040aso148954975ad.1 for ; Thu, 02 Jan 2025 15:07:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735859228; x=1736464028; 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=oukJOp8a/TMok9WYKtZiYjZTJSlSkLa4mKjkY8cRWFM=; b=1jj7CVern8aQqpC2ZF+MTI0gsDM2z67kOQtixWN5ieAiNbZzgWmRHcXNWXrtxxLPA2 Op54Yp2tPQI7ul1L4x74xHhc2N6fscdDKN6hR4S3XPB7RQLcSGCyNxLlELwo1uTjIWSL CJzMHNj8wq6+UX+0BBght01r9Uv/XaNHqfGfkm+TKJmvuo31v0Dph4O7jzLwtJrUmIGn g077LVfNhqFqjZXqhJCaUZYkQiJjdnRwx3tbsc2+nDwZFLUs+EF0J5x2S+t+/5xDw5dw /DmhvlAFdlYEWWvNrZrb4cPkPqeJjYgjCCzt9YehDbdw45OZAxuCo8KSSFQwH+ayAIFi 2sFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735859228; x=1736464028; 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=oukJOp8a/TMok9WYKtZiYjZTJSlSkLa4mKjkY8cRWFM=; b=IGWhhL5N3NLMOQ1g0GrVEZXJENBoEhiNDShHuwCj1EGqQu369Uh5LAMt/347OHk3gQ D/ZQr2WIptUf9iEmkMYdfgsNzZocANjZMGRTfRarwJnNA7ggWX/Lydv48hR9MhP48sGl IxzxINmtL9/2qwYS8ivcLgPavxEqn28ZK32eT9lVONlHFK6EIK5exvjfQuf2QTQQLHDK cEf1zPc6wDlu/2cJAeNRO59rR23wxeaJ8+Vo3bN5DaUN8u9ZJ8P1OzIhku/nXaExQWTE sKXkPjldzDlhlVCD/RoHaNsyMz1uxSAbdtzWd6d7D32BmtIkyBDn9lRSFfSZPf8OrPfp PsBA== X-Forwarded-Encrypted: i=1; AJvYcCX+5//tgjxVzcB/7Cq/caf/qPgrMykbmkxshFtJpAWdaIgmYq7A1fv1cp+5GhqmSsMuaTddKCxSaw==@kvack.org X-Gm-Message-State: AOJu0YxQqiBAGWQYfhWhG1gzitr8RaCieF1EgSouvbdLn7OeWWc5PazT cjmpU4yj/gPszImCZbW9SnK/G66xaYzWu6b+D25jBE37QJsM/mY6s+5TDDrBrNjEcpTufmQ0XGg 0HgCtiUaALtaebhi7DoR2Xa0dJnwtLsw/CQ== X-Google-Smtp-Source: AGHT+IHuF92dOUZi8JHInVoyGiPpTf/4WW1PFofvYbD/uxaxSwdrHBgggo7bixp9tF8fN4tBvaLn+HpwurCruzC8y+eL8g== X-Received: from pfbeg22.prod.google.com ([2002:a05:6a00:8016:b0:725:d033:af87]) (user=isaacmanjarres job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:a103:b0:1e1:ae68:d900 with SMTP id adf61e73a8af0-1e5e04a2e64mr88252525637.22.1735859227902; Thu, 02 Jan 2025 15:07:07 -0800 (PST) Date: Thu, 2 Jan 2025 15:06:54 -0800 In-Reply-To: <20250102230658.1112261-1-isaacmanjarres@google.com> Mime-Version: 1.0 References: <20250102230658.1112261-1-isaacmanjarres@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250102230658.1112261-2-isaacmanjarres@google.com> Subject: [PATCH v1 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-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 9661140014 X-Stat-Signature: y8s744riam8puhodyguxi3jkn88aggyg X-HE-Tag: 1735859176-698431 X-HE-Meta: U2FsdGVkX19K559qyGKwznW++Y1nFoND3trlUurU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RUFhvMgj+n6gbSDGNR5Ulh22plM6+s2FoMjdBXc3Os+gLY2QzubqSfcTNwVofAIqw6mm7ljglWrp3fBTzZyPQ6R+Lr15XSA8q0Vnwb7QKEvHlZVzHrpu8DF4iOl9/2kRgVxiNTp+NaaExoK1qBmApTgYApX3paZ8poMXBERDq5hU/n4XPQFQAtd7wmWD5LiBR1Oft+vJxj6jW2MC0s5lEODEIdzxheusKi2Nlej8M6OukJaWE8vZv55PLxHTu4/UupAWGMVjqyx5UFjHenFNWXrjKwVPjlZPQBZof/22oeCd3ymkac3QgwlCbyvx7pXgUfDL5l8wSGOz7uo1c2fHhJEoRZtIPcg7dx6hTnJtHt8RqR8hEU7EfuLzHRskqol9pAjKXerPzOp+lLO41JNo2vaoML93h5WT8WOddhgupT154WSEYh/uoK3hrX0HacBSDSG/GhCxCJOlCYmYaNLEjsxzVWnjwcC7i2vswcCCG11Te/256qmVGKnEF8LJy1qWX10RmFTdccmW1x/nq5ZzBc0+Ed2g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000089, 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 c17c3ea701a1..2372b9b1dc18 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -327,16 +327,8 @@ static int check_sysctl_memfd_noexec(unsigned int *flags) return 0; } -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; @@ -351,20 +343,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)) { @@ -378,11 +375,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, @@ -391,10 +399,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; @@ -414,13 +420,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; }