Re: [PATCH v2] vfs: shave work on failed file open
From: Linus Torvalds
Date: Tue Sep 26 2023 - 15:00:30 EST
On Tue, 26 Sept 2023 at 09:22, Mateusz Guzik <mjguzik@xxxxxxxxx> wrote:
>
> +void fput_badopen(struct file *file)
> +{
> + if (unlikely(file->f_mode & (FMODE_BACKING | FMODE_OPENED))) {
> + fput(file);
> + return;
> + }
I don't understand.
Why the FMODE_BACKING test?
The only thing that sets FMODE_BACKING is alloc_empty_backing_file(),
and we know that isn't involved, because the file that is free'd is
file = alloc_empty_file(op->open_flag, current_cred());
so that test makes no sense.
It might make sense as another WARN_ON_ONCE(), but honestly, why even
that? Why worry about FMODE_BACKING?
Now, the FMODE_OPENED check makes sense to me, in that it most
definitely can be set, and means we need to call the ->release()
callback and a lot more. Although I get the feeling that this test
would make more sense in the caller, since path_openat() _already_
checks for FMODE_OPENED in the non-error path too.
> + if (WARN_ON_ONCE(atomic_long_cmpxchg(&file->f_count, 1, 0) != 1)) {
> + fput(file);
> + return;
> + }
Ok, I kind of see why you'd want this safety check. I don't see how
f_count could be validly anything else, but that's what the
WARN_ON_ONCE is all about.
Anyway, I think I'd be happier about this if it was more of a "just
the reverse of alloc_empty_file()", and path_openat() literally did
just
if (likely(file->f_mode & FMODE_OPENED))
release_empty_file(file);
else
fput(file);
instead of having this fput_badopen() helper that feels like it needs
to care about other cases than alloc_empty_file().
Don't take this email as a NAK, though. I don't hate the patch. I just
feel it could be more targeted, and more clearly "this is explicitly
avoiding the cost of 'fput()' in just path_openat() if we never
actually filled things in".
Linus