Re: [PATCH] cgroup: allocate the inode for cgroup_may_write() before cgroup_mutex

From: Shakeel Butt

Date: Fri Nov 20 2026 - 13:27:22 EST


On Fri, Nov 20, 2026 at 01:24:18AM -1000, Tejun Heo wrote:
> Hello, Shakeel.
>
> On Wed, Nov 18, 2026 at 03:03:45PM -0800, Shakeel Butt wrote:
> > + dst_cgrp = cgroup_kn_break_active(of->kn);
> > if (IS_ERR(dst_cgrp))
> > return PTR_ERR(dst_cgrp);
> > + new = new_inode(of->file->f_path.dentry->d_sb);
> > + cgroup_lock();
>
> I don't really like new_inode() being called on every write, and twice on
> every clone3(CLONE_INTO_CGROUP). The inode is often not cached but not
> mostly. It's not a lot of overhead, but doing it the right way isn't that
> complicated either. How about peeking at the inode cache before taking the
> locks, preallocating only when the peek misses, and then looking up again
> under cgroup_mutex? If the peek turns out wrong, it can drop the locks,
> allocate and retry, so that nothing is allocated under the lock.

Thanks for taking a look and yes, this makes sense.

> Can we
> apply the same shape in other places too?

Yes, I agree, though I will do that in followup patches. More specifically I
think I can do the same approach in __kernfs_fh_to_dentry() and
kernfs_get_parent_dentry() for inode. For kernfs_setattr(), I think I can use
the same approach for iattrs. Also I am working on allocating css for new cgroup
before cgroup_mutex in mkdir wich will use similar approach.

>
> > scoped_with_creds(of->file->f_cred)
> > - ret = cgroup_attach_permissions(src_cgrp, dst_cgrp,
> > - of->file->f_path.dentry->d_sb,
> > + ret = cgroup_attach_permissions(src_cgrp, dst_cgrp, &new,
> > threadgroup, ctx->ns);
>
> AFAICS, this changes behavior under Smack. Smack labels a new inode from the
> allocating task's creds, and the allocation used to be inside
> scoped_with_creds(). Now the inode gets the writer's label and the
> permission check runs with the opener's creds, which can fail when the two
> differ. Maybe the allocation can use the opener's creds too?

Thanks for catching this. I will make sure this remain the same in the v2.