Re: [PATCH] kernfs: allocate the open node outside the open file mutex
From: Tejun Heo
Date: Thu Sep 10 2026 - 17:15:04 EST
On Wed, Sep 09, 2026 at 09:54:33PM -0700, Shakeel Butt wrote:
> kernfs_get_open_node() allocates the kernfs_open_node while holding one
> of the hashed kernfs_open_file mutexes, so opening a file nobody has open
> yet can enter reclaim with that mutex held. The mutex is shared by every
> node that hashes to it, so unrelated opens, closes and xattr updates in
> the same bucket wait.
>
> It is the most contended lock in kernfs on our fleet: 2.19M waiters over
> 29 days, more than two hundred times the waiters on kernfs_rwsem, and the
> highest median hold of any kernfs lock at 77ms. Most of it is monitoring
> daemons opening cgroup control files, and the open node is freed once the
> last descriptor closes, so an open-read-close loop allocates every time.
>
> Look at kn->attr.open before taking the mutex, and allocate then if
> nothing has the file open. Should the peek be wrong, which needs the
> last descriptor to close inside the window, fall back to allocating under
> the mutex as before.
>
> Eight tasks opening and closing four cgroup files 20000 times each, with
> lock_stat on the hashed mutex:
>
> contentions hold total hold avg
> before 198 4.19 s 8.67 us
> after 119 2.31 s 4.78 us
>
> and the same number of acquisitions either way.
>
> This does not empty the bucket. kernfs_fop_release() and
> kernfs_xattr_set() still sleep under the same mutex. It stops opens
> doing it.
>
> Assisted-by: LLM
> Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
Acked-by: Tejun Heo <tj@xxxxxxxxxx>
Thanks.
--
tejun