Re: [PATCH] cgroup: allocate the inode for cgroup_may_write() before cgroup_mutex
From: Tejun Heo
Date: Fri Nov 20 2026 - 06:24:29 EST
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. Can we
apply the same shape in other places too?
> 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.
--
tejun