mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jori Koolstra <jkoolstra@xs4all.nl>
To: Christian Brauner <brauner@kernel.org>
Cc: viro@zeniv.linux.org.uk, jack@suse.cz, neil@brown.name,
	cyphar@cyphar.com, jlayton@kernel.org, amir73il@gmail.com,
	shakeel.butt@linux.dev, gregkh@linuxfoundation.org,
	tj@kernel.org, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v8 00/12] vfs: add O_CREAT|O_DIRECTORY to open*(2)
Date: Wed, 2 Dec 2026 14:57:09 +0100 (CET)	[thread overview]
Message-ID: <1061093790.3487073.1796219829053@kpc.webmail.kpnmail.nl> (raw)


> Op 30-11-2026 15:53 CET schreef Christian Brauner <brauner@kernel.org>:
> 
>  
> On Sun, Nov 22, 2026 at 10:20:23PM +0100, Jori Koolstra wrote:
> > However, I am not sure that relying on holding the parent's i_rwsem is
> > sufficient for the atomicity that we are trying to provide. Afaict nothing
> > in the kernfs layer prevents you from removing a grandparent and a recursive
> > kernfs_remove() of an ancestor never touches the immediate parent's i_rwsem.
> 
> cgroup cannot hit it because cgroup_destroy_locked() refuses to remove a
> group with online children. The only other case is resctrl.
> 
> While an rmdir() of a control group removes mon_groups/<new> without
> ever taking the i_rwsem of mon_groups it still can't hand out the wrong
> directory.
> 
> The lookup in kernfs_iop_mkdir() runs under kernfs_rwsem and either
> finds nothing or finds an inactive node that kernfs_get_inode_locked()
> refuses. And both end in -ESTALE.

Hmm, yes, the draining may drop the kernfs_rwsem, but at that point all the kn
descendants will have been deactivated under kernfs_rwsem and a racing mkdir
will result in -ESTALE. Or mkdir reaches the lookup first and grabs the rwsem,
and then the rmdir is as if it never raced but just happened after the mkdir,
and you end up with an fd pointing to a removed dir. This is fine because it
is still the dir the process created (although not much can be done with it,
I think?)

So if I am correct then, none of this is protected by the mutexes of cgroup and
resctrl, and also not by the inode rwsem. The guarantees run of the kernfs
locking itself. If that is true, I think the comment:

	/*
	 * Find the node that ->mkdir() created.  The parent's i_rwsem is
	 * held, so only a removal from inside the kernel can make it go
	 * away again, and then the open fails like one of a removed
	 * directory.
	 */

may be a bit misleading then. Userspace can trigger removal and race protection
does not come from the i_rwsem at all.

Can you clarify this? I must admit that I find the locking here not easy to
grasp. I am just making sure I am on the same page.


> 
> Afaict, nothign in between can grab that name:
> 
> * kernfs_add_one() refuses a parent that is being removed
> * a new node of that name under a live parent needs the i_rwsem we're holding
> 
> So either you get the directory you created or the open fails.
> 
> > Also, returning -ESTALE, if you set LOOKUP_REVAL, can make open(O_CREAT|O_DIRECTORY)
> > return -ENOENT. Not sure if that is what you want.
> 
> I think that's what we want. The retry runs after the removal so it
> returns what you'd get if the rmdir() had won outright.

OK, the point I wanted to make is stupid because this also happens when any part of
the mkdir path does not exist. But, now that I reread all the code, I guess that
removing a grandfather in resctrl might end you up with -ENODEV. I think we should
get this in line with the -ESTALE return you've added. But this is another patch.
Agree?

Best,
Jori.

             reply	other threads:[~2026-12-02 13:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 13:57 Jori Koolstra [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-10-24 21:35 Jori Koolstra
2026-11-04 23:44 ` John Ericson
2026-11-06 14:33 ` Christian Brauner
2026-11-18 16:30 ` Christian Brauner
2026-11-18 16:42   ` Shakeel Butt
2026-11-20  8:59     ` Christian Brauner
2026-11-22 21:20   ` Jori Koolstra
2026-11-30 14:53     ` Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1061093790.3487073.1796219829053@kpc.webmail.kpnmail.nl \
    --to=jkoolstra@xs4all.nl \
    --cc=amir73il@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cyphar@cyphar.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jack@suse.cz \
    --cc=jlayton@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=neil@brown.name \
    --cc=shakeel.butt@linux.dev \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®