mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cheng Yongkang <teel4res@gmail.com>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [BUG] devpts: lookup racing with devpts_pty_new() can return ENOENT
Date: Sun, 15 Nov 2026 08:36:20 -0800	[thread overview]
Message-ID: <20261115163620.225761-1-teel4res@gmail.com> (raw)

Hi Al, Christian,

I hit a narrow race in devpts. It needs a lookup of /dev/pts/N at the
same moment pty N is created, and the result is a short-lived ENOENT,
so it may not matter much in practice.

bacdf1d70bbe ("primitives for maintaining persisitency") says that
d_make_persistent() "is always called in strong locking environment
(parent held exclusive, or, in some cases, dentry coming from
d_alloc_name())". devpts_pty_new() is the d_alloc_name() case, but it
does not hold the parent, and the d_alloc_name() dentry never goes
through the in-lookup hash or i_dir_seq. A lookup of the same name
that missed the dcache just before can then finish in simple_lookup()
and add a negative dentry:

  lookup                           devpts_pty_new()
  d_alloc_parallel(root, "N")
    miss
                                   d_alloc_name(root, "N")
                                   d_make_persistent()  -> positive
  simple_lookup()
    d_splice_alias(NULL)           -> negative, same name

Both dentries are hashed under the same name, the negative one first,
so stat() or open() of the new pty returns ENOENT until that lookup
drops it. As far as I can tell, the old d_add() code before
b4a76faf9d96 ("convert devpts") had the same window.

On v7.3 I ran one thread that opens ptmx and immediately stats the new
/dev/pts/N, and 16 threads that stat the same path. In 120 seconds the
opening thread got ENOENT for its own pty 13 times, and the other
threads saw ENOENT for a pty that existed before and after the call
26,372 times out of 6.5 million lookups. __d_add() and
d_alloc_parallel() have changed since v7.3, but the same window is
still there in next-20261113.

The same commit message says what d_make_persistent() would need if
it were ever used with the parent held shared and a dentry from
d_alloc_parallel(): the in-lookup logic of __d_add(). I tried that
direction for devpts: devpts_pty_new() takes its dentry from
d_alloc_parallel(), and d_make_persistent() does start_dir_add(),
__d_lookup_unhash(), end_dir_add() and wakes the waiters when the
dentry is in-lookup. It takes no parent lock at all, which is weaker
than what the commit message assumes. With it both counts went to
zero, and basic pty tests still passed.

You may prefer a different fix, for example creating the dentry with
simple_start_creating() as mknod_ptmx() does, which I have not tried.
I can send my version as a patch if you want it.

Thanks,
Cheng Yongkang

                 reply	other threads:[~2026-11-15 16:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261115163620.225761-1-teel4res@gmail.com \
    --to=teel4res@gmail.com \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.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®