From 893d20c95ade6ae4b6a514bb003457ce8f2959fd Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sat, 29 Jan 2022 23:07:06 +0100 Subject: [PATCH] fd: move fd table sizing out of fdinit now it is placed with the rest of actual initialisation --- sys/kern/init_main.c | 2 +- sys/kern/kern_descrip.c | 36 +++++++++++------------------------- sys/kern/kern_fork.c | 4 ++-- sys/sys/filedesc.h | 2 +- 4 files changed, 15 insertions(+), 29 deletions(-) diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 5ab9eed6a114..2225c5ed95d5 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -568,7 +568,7 @@ proc0_init(void *dummy __unused) /* Create the file descriptor table. */ p->p_pd = pdinit(NULL, false); - p->p_fd = fdinit(NULL, false, NULL); + p->p_fd = fdinit(); p->p_fdtol = NULL; /* Create the limits structures. */ diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index bf4a3a3f20e5..1dc0f1e647d6 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -2150,16 +2150,11 @@ finstall(struct thread *td, struct file *fp, int *fd, int flags, * If fdp is not NULL, return with it shared locked. */ struct filedesc * -fdinit(struct filedesc *fdp, bool prepfiles, int *lastfile) +fdinit(void) { struct filedesc0 *newfdp0; struct filedesc *newfdp; - if (prepfiles) - MPASS(lastfile != NULL); - else - MPASS(lastfile == NULL); - newfdp0 = uma_zalloc(filedesc0_zone, M_WAITOK | M_ZERO); newfdp = &newfdp0->fd_fd; @@ -2171,24 +2166,6 @@ fdinit(struct filedesc *fdp, bool prepfiles, int *lastfile) newfdp->fd_files = (struct fdescenttbl *)&newfdp0->fd_dfiles; newfdp->fd_files->fdt_nfiles = NDFILE; - if (fdp == NULL) - return (newfdp); - - FILEDESC_SLOCK(fdp); - if (!prepfiles) { - FILEDESC_SUNLOCK(fdp); - return (newfdp); - } - - for (;;) { - *lastfile = fdlastfile(fdp); - if (*lastfile < newfdp->fd_nfiles) - break; - FILEDESC_SUNLOCK(fdp); - fdgrowtable(newfdp, *lastfile + 1); - FILEDESC_SLOCK(fdp); - } - return (newfdp); } @@ -2368,7 +2345,16 @@ fdcopy(struct filedesc *fdp) MPASS(fdp != NULL); - newfdp = fdinit(fdp, true, &lastfile); + newfdp = fdinit(); + FILEDESC_SLOCK(fdp); + for (;;) { + lastfile = fdlastfile(fdp); + if (lastfile < newfdp->fd_nfiles) + break; + FILEDESC_SUNLOCK(fdp); + fdgrowtable(newfdp, lastfile + 1); + FILEDESC_SLOCK(fdp); + } /* copy all passable descriptors (i.e. not kqueue) */ newfdp->fd_freefile = -1; for (i = 0; i <= lastfile; ++i) { diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index a6d83fc8049d..0062f7419ac0 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -340,7 +340,7 @@ fork_norfproc(struct thread *td, int flags) struct filedesc *fdtmp; struct pwddesc *pdtmp; pdtmp = pdinit(td->td_proc->p_pd, false); - fdtmp = fdinit(td->td_proc->p_fd, false, NULL); + fdtmp = fdinit(); pdescfree(td); fdescfree(td); p1->p_fd = fdtmp; @@ -418,7 +418,7 @@ do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread * */ if (fr->fr_flags & RFCFDG) { pd = pdinit(p1->p_pd, false); - fd = fdinit(p1->p_fd, false, NULL); + fd = fdinit(); fdtol = NULL; } else if (fr->fr_flags & RFFDG) { if (fr->fr_flags2 & FR2_SHARE_PATHS) diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h index 7d106adb756f..0560a517c160 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -254,7 +254,7 @@ void fdunshare(struct thread *td); void fdescfree(struct thread *td); int fdlastfile(struct filedesc *fdp); int fdlastfile_single(struct filedesc *fdp); -struct filedesc *fdinit(struct filedesc *fdp, bool prepfiles, int *lastfile); +struct filedesc *fdinit(void); struct filedesc *fdshare(struct filedesc *fdp); struct filedesc_to_leader * filedesc_to_leader_alloc(struct filedesc_to_leader *old,