Re: [PATCH] fs/netfs: fix reference leak
From: David Howells
Date: Wed Sep 24 2025 - 11:29:45 EST
Max Kellermann <max.kellermann@xxxxxxxxx> wrote:
> For my taste, the whole netfs code needs an overhaul to make reference
> counting easier to understand and less fragile & obscure. But to fix
> this bug here and now and produce a patch that is adequate for a
> stable backport, I tried a minimal approach that quickly frees the
> request object upon early failure.
I'm not entirely satisfied with the refcounting either, as it's tricky with
the asynchronicity requirements.
> I decided against adding a second netfs_put_request() each time because that
> would cause code duplication which obscures the code further. Instead, I
> added the function netfs_put_failed_request() which frees such a failed
> request synchronously under the assumption that the reference count is
> exactly 2 (as initially set by netfs_alloc_request() and never touched),
> verified by a WARN_ON_ONCE().
I like this.
> ... and frees the allocation (without the "call_rcu" indirection).
Unfortunately, this isn't good. The request has already been added to the
proc list and is removed in netfs_deinit_request() by netfs_proc_del_rreq() -
but that means that someone reading /proc/fs/netfs/requests can be looking at
it as you free it.
You still need the call_rcu() - or you have to call synchronize_rcu().
I can change netfs_put_failed_request() to do the call_rcu() rather than
mempool_free()/netfs_stat_d().
Another possibility could be to defer the addition to the proc list to right
before we start adding subrequests. Deleting from the proc list would be a
no-op if the thing isn't queued.
Thanks,
David