Re: [PATCH 1/2] fs, close_range: add flag CLOSE_RANGE_CLOEXEC

From: Rasmus Villemoes
Date: Wed Oct 14 2020 - 05:20:58 EST


On 13/10/2020 23.09, Al Viro wrote:
> On Tue, Oct 13, 2020 at 04:06:08PM +0200, Giuseppe Scrivano wrote:
>> + spin_lock(&cur_fds->file_lock);
>> + fdt = files_fdtable(cur_fds);
>> + cur_max = fdt->max_fds - 1;
>> + max_fd = min(max_fd, cur_max);
>> + while (fd <= max_fd)
>> + __set_close_on_exec(fd++, fdt);
>> + spin_unlock(&cur_fds->file_lock);
>
> First of all, this is an atrocious way to set all bits
> in a range. What's more, you don't want to set it for *all*
> bits - only for the ones present in open bitmap. It's probably
> harmless at the moment, but let's not create interesting surprises
> for the future.

Eh, why not? They can already be set for unallocated slots:

commit 5297908270549b734c7c2556745e2385b6d4941d
Author: Mateusz Guzik <mguzik@xxxxxxxxxx>
Date: Tue Oct 3 12:58:14 2017 +0200

vfs: stop clearing close on exec when closing a fd

Codepaths allocating a fd always make sure the bit is set/unset
depending on flags, thus clearing on close is redundant.

And while we're on that subject, yours truly suggested exactly that two
years prior [1], with a follow-up [2] in 2018 to do what wasn't done in
5297908, but (still) seems like obvious micro-optimizations, given that
the close_on_exec bitmap is not maintained as a subset of the open
bitmap. Mind taking a look at [2]?

[1]
https://lore.kernel.org/lkml/1446543679-28849-1-git-send-email-linux@xxxxxxxxxxxxxxxxxx/t/#u
[2]
https://lore.kernel.org/lkml/20181024160159.25884-1-linux@xxxxxxxxxxxxxxxxxx/

Rasmus