devpts_pty_new() creates /dev/pts/N with d_alloc_name() and d_make_persistent() without holding the inode lock of the devpts root. A lookup of /dev/pts/N that misses the dcache holds that lock shared, gets an in-lookup dentry from d_alloc_parallel() and calls simple_lookup(), which hashes it as a negative dentry. Nothing serializes the two, so the pty can be created and hashed after the lookup missed it, and simple_lookup() then hashes its negative dentry for the same name right in front of it: lookup devpts_pty_new() inode_lock_shared(root) d_alloc_parallel(root, "N") miss d_alloc_name(root, "N") d_make_persistent() simple_lookup() d_splice_alias(NULL) inode_unlock_shared(root) Both dentries are now hashed under the same name and the negative one is found first. So stat() or open() of a freshly allocated pty fails with ENOENT until the negative dentry goes away. Cheng measured about 26k bogus ENOENTs in 6.5 million lookups while ptys were being created in a loop. The parent lock used to be held here but was dropped in 8ead9dd54716 ("devpts: more pty driver interface cleanups"). b4a76faf9d96 ("convert devpts") then switched mknod_ptmx() over to simple_start_creating() but left devpts_pty_new() alone. Do what mknod_ptmx() does and create the pty node with simple_start_creating() and simple_done_creating(). That takes the parent lock exclusive, so creation waits for lookups in flight, and it reuses the negative dentry a finished lookup might have left behind instead of hashing a second dentry next to it. devpts_pty_kill() doesn't need the same treatment. It only unhashes the dentry and a lookup racing with it either still finds the pty or misses and correctly reports ENOENT. The index of a pty is only freed in ->remove() after the master was closed and devpts_pty_kill() ran, so devpts_pty_new() never finds a positive dentry for the name it creates. Note that ptmx_open() calls devpts_pty_new() with the tty locked. We never take the tty lock under the inode lock of the devpts root: lookup_open() drops the parent lock before do_open() calls ->open() and devpts has no ->atomic_open(). Reported-by: Cheng Yongkang Closes: https://lore.kernel.org/20261115163620.225761-1-teel4res@gmail.com Fixes: 8ead9dd54716 ("devpts: more pty driver interface cleanups") Cc: stable@vger.kernel.org Signed-off-by: Christian Brauner (Amutable) --- fs/devpts/inode.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fs/devpts/inode.c b/fs/devpts/inode.c index bd1e8eb26edb7..65f6aee74fa45 100644 --- a/fs/devpts/inode.c +++ b/fs/devpts/inode.c @@ -521,15 +521,15 @@ struct dentry *devpts_pty_new(struct pts_fs_info *fsi, int index, void *priv) sprintf(s, "%d", index); - dentry = d_alloc_name(root, s); - if (!dentry) { + dentry = simple_start_creating(root, s); + if (IS_ERR(dentry)) { iput(inode); - return ERR_PTR(-ENOMEM); + return dentry; } dentry->d_fsdata = priv; d_make_persistent(dentry, inode); fsnotify_create(d_inode(root), dentry); - dput(dentry); + simple_done_creating(dentry); return dentry; // borrowed } --- base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4 change-id: 20261117-work-devpts-pty_new_locking-9b3d01e643c3