Re: [PATCH] kernfs: allocate in open before taking the active reference

From: Tejun Heo

Date: Fri Nov 20 2026 - 02:12:02 EST


On Thu, Nov 19, 2026 at 11:19:22AM -0800, Shakeel Butt wrote:
> kernfs_fop_open() allocates the open file, its seq_file and the open
> node while holding the file's active reference. The seq_file is
> charged to the opener's memory cgroup, so in a cgroup at its limit the
> allocation reclaims. Removing the file waits for active references.
> cgroup rmdir does that with cgroup_mutex held, so every cgroup
> operation on the host waits for that reclaim. Deleting a netdev waits
> the same way with rtnl_lock held.
>
> Allocate them before taking the active reference. kn->flags already
> says whether the file has ->seq_show, which is all seq_open() needs.
> If the file is removed in the meantime, open fails with -ENODEV as
> before. Only the prealloc buffer, whose size comes from the ops, and
> ->open() still allocate under the active reference.
>
> In a VM, eight tasks in a 256 MB memory cgroup at its limit, with swap
> writes taking 500 ms, open and read small files of a cgroup that
> another task removes and recreates every 20 ms (60 s, worst of four
> runs):
>
> before after
> reclaim with an active reference held 5.0 s 0
> rmdir waiting for it, cgroup_mutex held 377 ms 6.3 ms
> mkdir/rmdir of other cgroups 382 ms 125 ms
>
> The rest of the mkdir/rmdir time is waiting for kernfs_rwsem. Opening
> and closing cgroup files in a loop is as fast as before.
>
> Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>

Acked-by: Tejun Heo <tj@xxxxxxxxxx>

Thanks.

--
tejun