Add a way to get an empty descriptor table with one reference. Let dup_fd() use that helper. The coredump code will use it too in a bit. No functional changes. Signed-off-by: Christian Brauner (Amutable) --- fs/file.c | 49 ++++++++++++++++++++++++++++++++++++++----------- include/linux/fdtable.h | 1 + 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/fs/file.c b/fs/file.c index 608e6a1fa69b..ccbaa71dfbe4 100644 --- a/fs/file.c +++ b/fs/file.c @@ -375,21 +375,15 @@ static unsigned int sane_fdtable_size(struct fdtable *fdt, struct fd_range *punc return ALIGN(last + 1, BITS_PER_LONG); } -/* - * Allocate a new descriptor table and copy contents from the passed in - * instance. Returns a pointer to cloned table on success, ERR_PTR() - * on failure. For 'punch_hole' see sane_fdtable_size(). - */ -struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_hole) +/* A table with one reference and the embedded fdtable, nothing copied yet. */ +static struct files_struct *alloc_files(gfp_t gfp) { struct files_struct *newf; - struct file **old_fds, **new_fds; - unsigned int open_files, i; - struct fdtable *old_fdt, *new_fdt; + struct fdtable *new_fdt; - newf = kmem_cache_alloc(files_cachep, GFP_KERNEL); + newf = kmem_cache_alloc(files_cachep, gfp); if (!newf) - return ERR_PTR(-ENOMEM); + return NULL; atomic_set(&newf->count, 1); @@ -404,6 +398,39 @@ struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_ho new_fdt->full_fds_bits = newf->full_fds_bits_init; new_fdt->fd = &newf->fd_array[0]; + return newf; +} + +/* An empty descriptor table with one reference. */ +struct files_struct *alloc_files_struct(void) +{ + struct files_struct *newf; + + newf = alloc_files(GFP_KERNEL | __GFP_ZERO); + if (!newf) + return NULL; + + rcu_assign_pointer(newf->fdt, &newf->fdtab); + return newf; +} + +/* + * Allocate a new descriptor table and copy contents from the passed in + * instance. Returns a pointer to cloned table on success, ERR_PTR() + * on failure. For 'punch_hole' see sane_fdtable_size(). + */ +struct files_struct *dup_fd(struct files_struct *oldf, struct fd_range *punch_hole) +{ + struct files_struct *newf; + struct file **old_fds, **new_fds; + unsigned int open_files, i; + struct fdtable *old_fdt, *new_fdt; + + newf = alloc_files(GFP_KERNEL); + if (!newf) + return ERR_PTR(-ENOMEM); + new_fdt = &newf->fdtab; + spin_lock(&oldf->file_lock); old_fdt = files_fdtable(oldf); open_files = sane_fdtable_size(old_fdt, punch_hole); diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h index 15ee6292e0e3..811994b16bed 100644 --- a/include/linux/fdtable.h +++ b/include/linux/fdtable.h @@ -100,6 +100,7 @@ static inline bool close_on_exec(unsigned int fd, const struct files_struct *fil struct task_struct; void put_files_struct(struct files_struct *fs); +struct files_struct *alloc_files_struct(void); struct files_struct *switch_files_struct(struct task_struct *tsk, struct files_struct *files); int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp); -- 2.53.0