FD_ADD() was built on FD_PREPARE() + fd_publish(). Give it its own small macro that allocates the descriptor so it stays an immediate one-shot for the hot descriptor-is-the-result paths (open, socket, accept, memfd etc.). Signed-off-by: Christian Brauner (Amutable) --- include/linux/file.h | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/include/linux/file.h b/include/linux/file.h index 04dd85cdd9bf..1ee7f058a882 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -259,24 +259,30 @@ static inline int class_fd_prepare_lock_err(const struct fd_prepare *fdf) take_fd(fdp->__fd); \ }) -/* Do not use directly. */ -#define __FD_ADD(_fdf, _fd_flags, _file_owned) \ - ({ \ - FD_PREPARE(_fdf, _fd_flags, _file_owned); \ - s32 ret = _fdf.err; \ - if (likely(!ret)) \ - ret = fd_publish(_fdf); \ - ret; \ - }) - /* - * FD_ADD - Allocate and install an fd and file in one step. + * FD_ADD - allocate a descriptor, build the file and install it in one step. * @_fd_flags: flags for get_unused_fd_flags() - * @_file_owned: struct file to take ownership of + * @_file_owned: struct file to take ownership of (can be an expression) * - * Returns the allocated fd number, or negative error code on failure. + * The file expression is evaluated only after the descriptor is allocated, so + * a full table does not run its side effects. Drops the file and returns a + * negative errno on failure. Installs immediately: for anything more than a + * bare install-and-return (reporting the number, configuring the file, + * installing several descriptors) use fd_prepare()/fd_stage(). */ -#define FD_ADD(_fd_flags, _file_owned) \ - __FD_ADD(__UNIQUE_ID(fd_prepare), _fd_flags, _file_owned) +#define FD_ADD(_fd_flags, _file_owned) \ +({ \ + int __fd = get_unused_fd_flags(_fd_flags); \ + if (likely(__fd >= 0)) { \ + struct file *__file = (_file_owned); \ + if (unlikely(IS_ERR_OR_NULL(__file))) { \ + put_unused_fd(__fd); \ + __fd = __file ? PTR_ERR(__file) : -ENOMEM; \ + } else { \ + fd_install(__fd, __file); \ + } \ + } \ + __fd; \ +}) #endif /* __LINUX_FILE_H */ -- 2.53.0