Re: [PATCH] kernfs: don't take a failed id allocation for a wrap

From: Alexander Mikhalitsyn

Date: Fri Dec 04 2026 - 04:42:51 EST


On Wed, 2026-12-02 at 14:40 -0500, Josef Bacik wrote:
> After one failed id allocation every new node of that kernfs root
> gets an
> inode number above 32 bits on a 64 bit kernel, and the 32 bit stat()
> and
> getdents() fail on it with EOVERFLOW until the root goes away.
>
> __kernfs_new_node() stores the result of idr_alloc_cyclic() in the
> u32
> last_id_lowbits even when it is an errno. The next id is below that
> value, so id_highbits is incremented as if the low bits had wrapped.
>
> Reproduced with failslab on radix_tree_node while making cgroups, the
> inode number of the first cgroup made after the ENOMEM:
>
>     before: 0x1000002c0
>     after:  0x2c0
>
> Getting the last id once more is a wrap as well, and the stored errno
> covered for that after an ENOSPC.
>
> Fix by only recording ids that were allocated, and by counting an id
> equal to the last one as a wrap.
>
> Fixes: e23f568aa63f ("kernfs: fix ino wrap-around detection")
> Signed-off-by: Josef Bacik <josef@xxxxxxxx>

LGTM

Please, feel free to add:

Reviewed-by: Alexander Mikhalitsyn <alex@xxxxxxxxxxxx>

> ---
>  fs/kernfs/dir.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
> index 800af10d9511e..95563c73aed78 100644
> --- a/fs/kernfs/dir.c
> +++ b/fs/kernfs/dir.c
> @@ -671,10 +671,12 @@ static struct kernfs_node
> *__kernfs_new_node(struct kernfs_root *root,
>   idr_preload(GFP_KERNEL);
>   spin_lock(&root->kernfs_idr_lock);
>   ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0,
> GFP_ATOMIC);
> - if (ret >= 0 && ret < root->last_id_lowbits)
> - root->id_highbits++;
> + if (ret >= 0) {
> + if (ret <= root->last_id_lowbits)
> + root->id_highbits++;
> + root->last_id_lowbits = ret;
> + }
>   id_highbits = root->id_highbits;
> - root->last_id_lowbits = ret;
>   spin_unlock(&root->kernfs_idr_lock);
>   idr_preload_end();
>   if (ret < 0)
> ---
> base-commit: b8e026b973764a52b92c0cdc1d3cf96f2abc1058
> change-id: 20261202-b4-kernfs-id-wrap-99d9a77c1300