Re: [PATCH 1/2] fs, close_range: add flag CLOSE_RANGE_CLOEXEC
From: Christian Brauner
Date: Tue Oct 13 2020 - 17:22:38 EST
On Tue, Oct 13, 2020 at 11:04:21PM +0200, Rasmus Villemoes wrote:
> On 13/10/2020 22.54, Christian Brauner wrote:
> > On Tue, Oct 13, 2020 at 04:06:08PM +0200, Giuseppe Scrivano wrote:
> >
> > Hey Guiseppe,
> >
> > Thanks for the patch!
> >
> >> When the flag CLOSE_RANGE_CLOEXEC is set, close_range doesn't
> >> immediately close the files but it sets the close-on-exec bit.
> >
> > Hm, please expand on the use-cases a little here so people know where
> > and how this is useful. Keeping the rationale for a change in the commit
> > log is really important.
> >
>
> > I think I don't have quarrels with this patch in principle but I wonder
> > if something like the following wouldn't be easier to follow:
> >
> > diff --git a/fs/file.c b/fs/file.c
> > index 21c0893f2f1d..872a4098c3be 100644
> > --- a/fs/file.c
> > +++ b/fs/file.c
> > @@ -672,6 +672,32 @@ int __close_fd(struct files_struct *files, unsigned fd)
> > }
> > EXPORT_SYMBOL(__close_fd); /* for ksys_close() */
> >
> > +static inline void __range_cloexec(struct files_struct *cur_fds,
> > + unsigned int fd, unsigned max_fd)
> > +{
> > + struct fdtable *fdt;
> > + spin_lock(&cur_fds->file_lock);
> > + fdt = files_fdtable(cur_fds);
> > + while (fd <= max_fd)
> > + __set_close_on_exec(fd++, fdt);
>
(I should've warned that I just proposed this as a completely untested
brainstorm.)
> Doesn't that want to be
>
> bitmap_set(fdt->close_on_exec, fd, max_fd - fd + 1)
>
> to do word-at-a-time? I assume this would mostly be called with (3, ~0U)
> as arguments or something like that.
Yes, that is the common case.
Thanks Rasmus, I was unaware we had that function.
In that case I think we'd actually need sm like:
spin_lock(&cur_fds->file_lock);
fdt = files_fdtable(cur_fds);
cur_max = files_fdtable(cur_fds)->max_fds - 1;
max_fd = min(max_fd, cur_max);
bitmap_set(fdt->close_on_exec, fd, max_fd - fd + 1)
so we retrieve max_fd with the spinlock held, I think.
Christian