From patchwork Thu Nov 30 12:49:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13474358 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20939168B0 for ; Thu, 30 Nov 2023 12:49:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DVJcARuz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0ABDFC433C7; Thu, 30 Nov 2023 12:49:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701348569; bh=Ld6OoEPx+dhBIvzinSbEgyOzoS2jFWZHZ5jQMYDnSG0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DVJcARuzhjWkMTe67xdkJP3PYSnAdn+s9Ofb2SM6hItSG1B1HMYglTd7jU+0Cszij xMh6qSLwRKaF/3P2VkV8lhsOMDNUmJoQjrUeMs7g9R84CXUtoBbZBGnDa23eaDhek8 sP6aQNZ/ybpSAiEMyLue58Do2vBAEesMvvPu8hWg64vFdKropz8ZRSmohslrpmgzWN 8nLeQgAQdQJN2CW8PvLPNKckF0kKAnDWEDQ1fp7WUgd0yE8WtWULl9IRAiYUKfCgh3 4DrwBOqYwr+6dnVnQvFiMzS1+RUmumDmAGlcAUrFYXpUJ2GDxcn3IYur9ALcxIW364 QRthb2y45Zueg== From: Christian Brauner Date: Thu, 30 Nov 2023 13:49:07 +0100 Subject: [PATCH RFC 1/5] file: s/close_fd_get_file()/file_close_fd()/g Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231130-vfs-files-fixes-v1-1-e73ca6f4ea83@kernel.org> References: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> In-Reply-To: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Jan Kara , Jens Axboe , Carlos Llamas , Alexander Viro , Linus Torvalds , Christian Brauner X-Mailer: b4 0.13-dev-7edf1 X-Developer-Signature: v=1; a=openpgp-sha256; l=3103; i=brauner@kernel.org; h=from:subject:message-id; bh=Ld6OoEPx+dhBIvzinSbEgyOzoS2jFWZHZ5jQMYDnSG0=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaRmtFydu8xk/4lPJs9ESg7OjeDOry3U+jWj9qaKp8elr nTLja1LOkpZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACZyaAMjw2GGbPelVttZJh4M 5aoNlJ8yS/ODvtDtYOk9eQHqiapVTxkZDt7W/PNPeJFt9Zo58ocrRM5/8t1+6UdUGPec51EtqX3 MXAA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 That really shouldn't have "get" in there as that implies we're bumping the reference count which we don't do at all. We used to but not anmore. Now we're just closing the fd and pick that file from the fdtable without bumping the reference count. Update the wrong documentation while at it. Signed-off-by: Christian Brauner Reviewed-by: Jan Kara --- drivers/android/binder.c | 2 +- fs/file.c | 14 +++++++++----- fs/open.c | 2 +- include/linux/fdtable.h | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/drivers/android/binder.c b/drivers/android/binder.c index 92128aae2d06..7658103ba760 100644 --- a/drivers/android/binder.c +++ b/drivers/android/binder.c @@ -1921,7 +1921,7 @@ static void binder_deferred_fd_close(int fd) if (!twcb) return; init_task_work(&twcb->twork, binder_do_fd_close); - twcb->file = close_fd_get_file(fd); + twcb->file = file_close_fd(fd); if (twcb->file) { // pin it until binder_do_fd_close(); see comments there get_file(twcb->file); diff --git a/fs/file.c b/fs/file.c index 50df31e104a5..66f04442a384 100644 --- a/fs/file.c +++ b/fs/file.c @@ -796,7 +796,7 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags) } /* - * See close_fd_get_file() below, this variant assumes current->files->file_lock + * See file_close_fd() below, this variant assumes current->files->file_lock * is held. */ struct file *__close_fd_get_file(unsigned int fd) @@ -804,11 +804,15 @@ struct file *__close_fd_get_file(unsigned int fd) return pick_file(current->files, fd); } -/* - * variant of close_fd that gets a ref on the file for later fput. - * The caller must ensure that filp_close() called on the file. +/** + * file_close_fd - return file associated with fd + * @fd: file descriptor to retrieve file for + * + * Doesn't take a separate reference count. + * + * Returns: The file associated with @fd (NULL if @fd is not open) */ -struct file *close_fd_get_file(unsigned int fd) +struct file *file_close_fd(unsigned int fd) { struct files_struct *files = current->files; struct file *file; diff --git a/fs/open.c b/fs/open.c index 0bd7fce21cbf..328dc6ef1883 100644 --- a/fs/open.c +++ b/fs/open.c @@ -1578,7 +1578,7 @@ SYSCALL_DEFINE1(close, unsigned int, fd) int retval; struct file *file; - file = close_fd_get_file(fd); + file = file_close_fd(fd); if (!file) return -EBADF; diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h index 80bd7789bab1..78c8326d74ae 100644 --- a/include/linux/fdtable.h +++ b/include/linux/fdtable.h @@ -119,7 +119,7 @@ int iterate_fd(struct files_struct *, unsigned, extern int close_fd(unsigned int fd); extern int __close_range(unsigned int fd, unsigned int max_fd, unsigned int flags); -extern struct file *close_fd_get_file(unsigned int fd); +extern struct file *file_close_fd(unsigned int fd); extern int unshare_fd(unsigned long unshare_flags, unsigned int max_fds, struct files_struct **new_fdp); From patchwork Thu Nov 30 12:49:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13474359 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EE7E538FB1 for ; Thu, 30 Nov 2023 12:49:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="lJ6h0bYz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 301F2C433CA; Thu, 30 Nov 2023 12:49:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701348571; bh=y5oee8d+LygiNr44hXs874ugh7uQlyFrHx6WyxL7MCI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=lJ6h0bYz6CQpNDohEp29rIYEg2DSYrVlQghOo37eYbmaTV0ZNyS75TwhyAG0cBB7H CIkL2mg9h3lrwdkrTKXiBuXN0L/DWpyz8/2atTaBfQCoEDZNxbWv1Nf+6W4DwG//PH /4VNmxyOgOY51U4a80qJlS9d5tecpz3clhsxkGdooaeG8+C/eVWHU1AmWb+wTbgGeD srEkB7mPgpTk6MbIxXj1CMMHMX7ECrSn3VgzqUXZGyWYAJAO9SsYuB0GcH7Z0/8X5O pJtTJ7hC9a/pNCYVdsKoQ9/rdfZaEpGV50iA0aasG+SFFoseHAa/wEntZYRWeCPBWa krdr4ptY09hCg== From: Christian Brauner Date: Thu, 30 Nov 2023 13:49:08 +0100 Subject: [PATCH RFC 2/5] file: remove pointless wrapper Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231130-vfs-files-fixes-v1-2-e73ca6f4ea83@kernel.org> References: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> In-Reply-To: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Jan Kara , Jens Axboe , Carlos Llamas , Alexander Viro , Linus Torvalds , Christian Brauner X-Mailer: b4 0.13-dev-7edf1 X-Developer-Signature: v=1; a=openpgp-sha256; l=3834; i=brauner@kernel.org; h=from:subject:message-id; bh=y5oee8d+LygiNr44hXs874ugh7uQlyFrHx6WyxL7MCI=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaRmtFztSi0y3XAtMaTYkvmll66Ww5MjjVk/Im+8kuVKP lxT99mmo5SFQYyLQVZMkcWh3SRcbjlPxWajTA2YOaxMIEMYuDgFYCIOpxn+V6xI6Lm5+gbTd6Oa N5J/HE8Hve54L1Wq0H4kkXW3wM9II0aGff4FF9T4DP/91KxREap89enzVvaU6Cjzlc3v4j99/Zj OBwA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Only io_uring uses __close_fd_get_file(). All it does is hide current->files but io_uring accesses files_struct directly right now anyway so it's a bit pointless. Just rename pick_file() to file_close_fd_locked() and let io_uring use it. Add a lockdep assert in there that we expect the caller to hold file_lock while we're at it. Signed-off-by: Christian Brauner Reviewed-by: Jens Axboe Reviewed-by: Jan Kara --- fs/file.c | 23 +++++++++-------------- fs/internal.h | 2 +- io_uring/openclose.c | 2 +- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/fs/file.c b/fs/file.c index 66f04442a384..c8eaa0b29a08 100644 --- a/fs/file.c +++ b/fs/file.c @@ -629,19 +629,23 @@ void fd_install(unsigned int fd, struct file *file) EXPORT_SYMBOL(fd_install); /** - * pick_file - return file associatd with fd + * file_close_fd_locked - return file associated with fd * @files: file struct to retrieve file from * @fd: file descriptor to retrieve file for * + * Doesn't take a separate reference count. + * * Context: files_lock must be held. * * Returns: The file associated with @fd (NULL if @fd is not open) */ -static struct file *pick_file(struct files_struct *files, unsigned fd) +struct file *file_close_fd_locked(struct files_struct *files, unsigned fd) { struct fdtable *fdt = files_fdtable(files); struct file *file; + lockdep_assert_held(&files->file_lock); + if (fd >= fdt->max_fds) return NULL; @@ -660,7 +664,7 @@ int close_fd(unsigned fd) struct file *file; spin_lock(&files->file_lock); - file = pick_file(files, fd); + file = file_close_fd_locked(files, fd); spin_unlock(&files->file_lock); if (!file) return -EBADF; @@ -707,7 +711,7 @@ static inline void __range_close(struct files_struct *files, unsigned int fd, max_fd = min(max_fd, n); for (; fd <= max_fd; fd++) { - file = pick_file(files, fd); + file = file_close_fd_locked(files, fd); if (file) { spin_unlock(&files->file_lock); filp_close(file, files); @@ -795,15 +799,6 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags) return 0; } -/* - * See file_close_fd() below, this variant assumes current->files->file_lock - * is held. - */ -struct file *__close_fd_get_file(unsigned int fd) -{ - return pick_file(current->files, fd); -} - /** * file_close_fd - return file associated with fd * @fd: file descriptor to retrieve file for @@ -818,7 +813,7 @@ struct file *file_close_fd(unsigned int fd) struct file *file; spin_lock(&files->file_lock); - file = pick_file(files, fd); + file = file_close_fd_locked(files, fd); spin_unlock(&files->file_lock); return file; diff --git a/fs/internal.h b/fs/internal.h index 273e6fd40d1b..a7469ddba9b6 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -179,7 +179,7 @@ extern struct file *do_file_open_root(const struct path *, const char *, const struct open_flags *); extern struct open_how build_open_how(int flags, umode_t mode); extern int build_open_flags(const struct open_how *how, struct open_flags *op); -extern struct file *__close_fd_get_file(unsigned int fd); +struct file *file_close_fd_locked(struct files_struct *files, unsigned fd); long do_sys_ftruncate(unsigned int fd, loff_t length, int small); int chmod_common(const struct path *path, umode_t mode); diff --git a/io_uring/openclose.c b/io_uring/openclose.c index fb73adb89067..74fc22461f48 100644 --- a/io_uring/openclose.c +++ b/io_uring/openclose.c @@ -241,7 +241,7 @@ int io_close(struct io_kiocb *req, unsigned int issue_flags) return -EAGAIN; } - file = __close_fd_get_file(close->fd); + file = file_close_fd_locked(files, close->fd); spin_unlock(&files->file_lock); if (!file) goto err; From patchwork Thu Nov 30 12:49:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13474360 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DD653B28A for ; Thu, 30 Nov 2023 12:49:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qyphNFlc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E6C2C433C9; Thu, 30 Nov 2023 12:49:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701348573; bh=ZvwItoXx0k/brQZ8mZ58tI0k5W8jRqXALE+vqboThis=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qyphNFlc4v4QercXeJa0gAEERPEoBc0V0esDMnAhK7pfY8ie1pjJKgyZUBMAOzotf ViX1FdzIveD53iL9IWE1XeOIHN/MASYZ4OinvqiLzQYwI94Yads5aMtR5q9El7wbb9 r6th2B2GqgqCPvxrOM0dIQMLNM++gmXRwxFw3Q0uR4V8ll05io+VvYl/kplaJYQGmo BPRAL5MTSG62LMTwn7S6yRE8vKmoTbohbQ1+01G/gfXMX+Xi3LNBdQ8O7KfhUsDz3C UH7dF7G4gYg0+EZ24BaS+Ubusrp89F6UQdvHUPm1HDjc6V+IWbhwuVRTgzFf1busop CpMg10l/JOnOg== From: Christian Brauner Date: Thu, 30 Nov 2023 13:49:09 +0100 Subject: [PATCH RFC 3/5] fs: replace f_rcuhead with f_tw Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231130-vfs-files-fixes-v1-3-e73ca6f4ea83@kernel.org> References: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> In-Reply-To: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Jan Kara , Jens Axboe , Carlos Llamas , Alexander Viro , Linus Torvalds , Christian Brauner X-Mailer: b4 0.13-dev-7edf1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1567; i=brauner@kernel.org; h=from:subject:message-id; bh=ZvwItoXx0k/brQZ8mZ58tI0k5W8jRqXALE+vqboThis=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaRmtFz1u+fKcqPiCuOJMsc/LTsnPVu18eCZt9uzplTxC 8ltvT1Nv6OUhUGMi0FWTJHFod0kXG45T8Vmo0wNmDmsTCBDGLg4BWAij9gY/odNDphZ5sXYvkQz lufL6SsnTbYs/BzbsUVglsnHjbsXZnxnZPi3+zzrstkOGSl/a7/7nSuxv1bgInXSyXfGUzPByR7 8PXwA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 The naming is actively misleading since we switched to SLAB_TYPESAFE_BY_RCU. rcu_head is #define callback_head. Use callback_head directly and rename f_rcuhead to f_tw. Signed-off-by: Christian Brauner Reviewed-by: Jan Kara --- fs/file_table.c | 6 +++--- include/linux/fs.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/file_table.c b/fs/file_table.c index 6deac386486d..78614204ef2c 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -407,7 +407,7 @@ static void delayed_fput(struct work_struct *unused) static void ____fput(struct callback_head *work) { - __fput(container_of(work, struct file, f_rcuhead)); + __fput(container_of(work, struct file, f_tw)); } /* @@ -438,8 +438,8 @@ void fput(struct file *file) return; } if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) { - init_task_work(&file->f_rcuhead, ____fput); - if (!task_work_add(task, &file->f_rcuhead, TWA_RESUME)) + init_task_work(&file->f_tw, ____fput); + if (!task_work_add(task, &file->f_tw, TWA_RESUME)) return; /* * After this task has run exit_task_work(), diff --git a/include/linux/fs.h b/include/linux/fs.h index f171505940ff..d23a886df8fa 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -992,7 +992,7 @@ static inline int ra_has_index(struct file_ra_state *ra, pgoff_t index) struct file { union { struct llist_node f_llist; - struct rcu_head f_rcuhead; + struct callback_head f_tw; unsigned int f_iocb_flags; }; From patchwork Thu Nov 30 12:49:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13474361 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7287C2B9DD for ; Thu, 30 Nov 2023 12:49:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NlROJCmT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 656B9C433C8; Thu, 30 Nov 2023 12:49:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701348576; bh=ZlCxoHM6aTXEK+7SoLSKlNAfXLIHJkuaGDjxE+FfjAc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=NlROJCmTJs1DzaYNrxcVW5TNFvjnRcBA2lPDUXZwgg5Fyx2oEuoUwjpCb1PWBIqNP pbnk31MzsfhE8FLIbl6e6HH1Pf4rNcM2SQm3NaHilx428hHcpMOcquGTGN3WhsFgCb KtJGv7wnxEdpUTl/6Ol1jpWOUhRhmHGoytFzTB6R1E58QO4Vi2trIqlEzAkk+B36vE EqR5LAAUEPxrrBN1C6We5kuQkzEk4NrS0iLN0JCBRMgURdL1REqgH2+vnnX9RGcXaI NO3E5gMAb05xsGLNfzr6Zs8r5y+w3brtLdcA342bfmc8PO8y7lnU9uOdal5ry9Vahh K0H7/9MdwpQ4A== From: Christian Brauner Date: Thu, 30 Nov 2023 13:49:10 +0100 Subject: [PATCH RFC 4/5] file: stop exposing receive_fd_user() Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231130-vfs-files-fixes-v1-4-e73ca6f4ea83@kernel.org> References: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> In-Reply-To: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Jan Kara , Jens Axboe , Carlos Llamas , Alexander Viro , Linus Torvalds , Christian Brauner X-Mailer: b4 0.13-dev-7edf1 X-Developer-Signature: v=1; a=openpgp-sha256; l=2546; i=brauner@kernel.org; h=from:subject:message-id; bh=ZlCxoHM6aTXEK+7SoLSKlNAfXLIHJkuaGDjxE+FfjAc=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaRmtFy1johwarV7PWPfCUmjxTVdyw+by1jbBAVa2O/d3 DJlBr9QRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwERscxkZ3rkVvznAcSzAb4Vx 4hKRx/O++T+9Fu7884Bh3uYfscaFvYwMS7MfXRIMOZGmUxNyhC+O3f1Xo5KHuMsB+buscYcYmfX 5AA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Not every subsystem needs to have their own specialized helper. Just us the __receive_fd() helper. Signed-off-by: Christian Brauner Reviewed-by: Jan Kara --- include/linux/file.h | 7 ------- include/net/scm.h | 9 +++++++++ net/compat.c | 2 +- net/core/scm.c | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/linux/file.h b/include/linux/file.h index 6e9099d29343..c0d5219c2852 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -101,13 +101,6 @@ extern int __receive_fd(struct file *file, int __user *ufd, extern int receive_fd(struct file *file, unsigned int o_flags); -static inline int receive_fd_user(struct file *file, int __user *ufd, - unsigned int o_flags) -{ - if (ufd == NULL) - return -EFAULT; - return __receive_fd(file, ufd, o_flags); -} int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags); extern void flush_delayed_fput(void); diff --git a/include/net/scm.h b/include/net/scm.h index e8c76b4be2fe..8aae2468bae0 100644 --- a/include/net/scm.h +++ b/include/net/scm.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -208,5 +209,13 @@ static inline void scm_recv_unix(struct socket *sock, struct msghdr *msg, scm_destroy_cred(scm); } +static inline int scm_recv_one_fd(struct file *f, int __user *ufd, + unsigned int flags) +{ + if (!ufd) + return -EFAULT; + return __receive_fd(f, ufd, flags); +} + #endif /* __LINUX_NET_SCM_H */ diff --git a/net/compat.c b/net/compat.c index 6564720f32b7..485db8ee9b28 100644 --- a/net/compat.c +++ b/net/compat.c @@ -297,7 +297,7 @@ void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm) int err = 0, i; for (i = 0; i < fdmax; i++) { - err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags); + err = scm_recv_one_fd(scm->fp->fp[i], cmsg_data + i, o_flags); if (err < 0) break; } diff --git a/net/core/scm.c b/net/core/scm.c index 880027ecf516..eec78e312550 100644 --- a/net/core/scm.c +++ b/net/core/scm.c @@ -319,7 +319,7 @@ void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm) } for (i = 0; i < fdmax; i++) { - err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags); + err = scm_recv_one_fd(scm->fp->fp[i], cmsg_data + i, o_flags); if (err < 0) break; } From patchwork Thu Nov 30 12:49:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13474362 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5ED0B2B9DD for ; Thu, 30 Nov 2023 12:49:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rbiuyWLr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9EF09C433C9; Thu, 30 Nov 2023 12:49:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701348578; bh=v8xFV2oXorsf2WY8P2pU/42bWbrkyQrwPNydx5WgnfI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rbiuyWLr/uDEqVeyaiTpR4FpKQlGPmnamnx65+cxjxEzf2WqWm2BoSxwSJ9KuA7P3 DbYwVLT7+hHk52KI9ADpn47fdwcF0q1EdBXivvfd+huDq3RebOWVL3mFktjvv5XfXr r4QhBR4xJpJVGV2KQWKF+qVhM4+zLB4CmOTBoURQcZFbZx/zZu6r7nMinD2NqKs++D Whd2RS/eW00mFAET96HR4mjtRcpJutU2QIT7LO7+8pGAKaM1MYcrZ2Qj5su/rApLnv r2Gabf4mAWk2H409fk75fsVRac5jkh1LMhE6Mha64DeasicRcvyahVXtFWs51hx/WH zS2a55iUv1KPw== From: Christian Brauner Date: Thu, 30 Nov 2023 13:49:11 +0100 Subject: [PATCH RFC 5/5] file: remove __receive_fd() Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20231130-vfs-files-fixes-v1-5-e73ca6f4ea83@kernel.org> References: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> In-Reply-To: <20231130-vfs-files-fixes-v1-0-e73ca6f4ea83@kernel.org> To: linux-fsdevel@vger.kernel.org Cc: Jan Kara , Jens Axboe , Carlos Llamas , Alexander Viro , Linus Torvalds , Christian Brauner X-Mailer: b4 0.13-dev-7edf1 X-Developer-Signature: v=1; a=openpgp-sha256; l=4483; i=brauner@kernel.org; h=from:subject:message-id; bh=v8xFV2oXorsf2WY8P2pU/42bWbrkyQrwPNydx5WgnfI=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaRmtFzdXlnuJ9lwrIbth4p64jxu/RU67kuDlvpfVgwz2 2MX8M6oo5SFQYyLQVZMkcWh3SRcbjlPxWajTA2YOaxMIEMYuDgFYCKK7IwMC9fc11ty8tqtmHWf NG8us+k4eLujbdWa05FRjpuP7552ZQYjw4IPOo/efl8pdMLx/ssL7oZrxGuK6k4ofPbQ+Hzl5qe kEDYA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Honestly, there's little value in having a helper with and without that int __user *ufd argument. It's just messy and doesn't really give us anything. Just expose receive_fd() with that argument and get rid of that helper. Signed-off-by: Christian Brauner Reviewed-by: Jan Kara --- drivers/vdpa/vdpa_user/vduse_dev.c | 2 +- fs/file.c | 11 +++-------- include/linux/file.h | 5 +---- include/net/scm.h | 2 +- kernel/pid.c | 2 +- kernel/seccomp.c | 2 +- 6 files changed, 8 insertions(+), 16 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vduse_dev.c index 6cb5ce4a8b9a..1d24da79c399 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -1157,7 +1157,7 @@ static long vduse_dev_ioctl(struct file *file, unsigned int cmd, fput(f); break; } - ret = receive_fd(f, perm_to_file_flags(entry.perm)); + ret = receive_fd(f, NULL, perm_to_file_flags(entry.perm)); fput(f); break; } diff --git a/fs/file.c b/fs/file.c index c8eaa0b29a08..3b683b9101d8 100644 --- a/fs/file.c +++ b/fs/file.c @@ -1296,7 +1296,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) } /** - * __receive_fd() - Install received file into file descriptor table + * receive_fd() - Install received file into file descriptor table * @file: struct file that was received from another process * @ufd: __user pointer to write new fd number to * @o_flags: the O_* flags to apply to the new fd entry @@ -1310,7 +1310,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) * * Returns newly install fd or -ve on error. */ -int __receive_fd(struct file *file, int __user *ufd, unsigned int o_flags) +int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags) { int new_fd; int error; @@ -1335,6 +1335,7 @@ int __receive_fd(struct file *file, int __user *ufd, unsigned int o_flags) __receive_sock(file); return new_fd; } +EXPORT_SYMBOL_GPL(receive_fd); int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags) { @@ -1350,12 +1351,6 @@ int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags) return new_fd; } -int receive_fd(struct file *file, unsigned int o_flags) -{ - return __receive_fd(file, NULL, o_flags); -} -EXPORT_SYMBOL_GPL(receive_fd); - static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags) { int err = -EBADF; diff --git a/include/linux/file.h b/include/linux/file.h index c0d5219c2852..a50545ef1197 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -96,10 +96,7 @@ DEFINE_CLASS(get_unused_fd, int, if (_T >= 0) put_unused_fd(_T), extern void fd_install(unsigned int fd, struct file *file); -extern int __receive_fd(struct file *file, int __user *ufd, - unsigned int o_flags); - -extern int receive_fd(struct file *file, unsigned int o_flags); +extern int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags); int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags); diff --git a/include/net/scm.h b/include/net/scm.h index 8aae2468bae0..cf68acec4d70 100644 --- a/include/net/scm.h +++ b/include/net/scm.h @@ -214,7 +214,7 @@ static inline int scm_recv_one_fd(struct file *f, int __user *ufd, { if (!ufd) return -EFAULT; - return __receive_fd(f, ufd, flags); + return receive_fd(f, ufd, flags); } #endif /* __LINUX_NET_SCM_H */ diff --git a/kernel/pid.c b/kernel/pid.c index 6500ef956f2f..b52b10865454 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -700,7 +700,7 @@ static int pidfd_getfd(struct pid *pid, int fd) if (IS_ERR(file)) return PTR_ERR(file); - ret = receive_fd(file, O_CLOEXEC); + ret = receive_fd(file, NULL, O_CLOEXEC); fput(file); return ret; diff --git a/kernel/seccomp.c b/kernel/seccomp.c index 255999ba9190..aca7b437882e 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c @@ -1072,7 +1072,7 @@ static void seccomp_handle_addfd(struct seccomp_kaddfd *addfd, struct seccomp_kn */ list_del_init(&addfd->list); if (!addfd->setfd) - fd = receive_fd(addfd->file, addfd->flags); + fd = receive_fd(addfd->file, NULL, addfd->flags); else fd = receive_fd_replace(addfd->fd, addfd->file, addfd->flags); addfd->ret = fd;