devpts: create pty nodes under the parent lock
Subsystems (2)
Christian Brauner
maintainerPATCH 1/1 devpts: create pty nodes under the parent lock
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().
--- fs/devpts/inode.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
fs/devpts/inode.c +4 -4
@@ -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
Quoted 56 lines · Christian Brauner
devpts_pty_new()creates/dev/pts/Nwithd_alloc_name()andd_make_persistent()without holding the inode lock of the devpts root.
A lookup of/dev/pts/Nthat misses the dcache holds that lock shared, gets an in-lookup dentry fromd_alloc_parallel()and callssimple_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, andsimple_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()oropen()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 switchedmknod_ptmx()over tosimple_start_creating()but
leftdevpts_pty_new()alone.Do what
mknod_ptmx()does and create the pty node withsimple_start_creating()andsimple_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 anddevpts_pty_kill()ran, sodevpts_pty_new()never finds a positive dentry for the name it creates.Note that
ptmx_open()callsdevpts_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 beforedo_open()calls ->open() and
devpts has no ->atomic_open().
Quoted 5 lines
Reported-by Cheng Yongkang <[email protected]>Closes:
https://lore.kernel.org/[email protected]Fixes 8ead9dd54716 ("devpts: more pty driver interface cleanups")
Quoted 34 lines
Signed-off-by Christian Brauner (Amutable) <[email protected]>---
fs/devpts/inode.c | 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)fs/devpts/inode.c +0
-0@@ -521,15 +521,15 @@ struct dentry *devpts_pty_new(struct pts_fs_info *fsi, int index, void *priv)
sprintf(s, "%d", index);fs/devpts/inode.c +2
-2- dentry =d_alloc_name (root, s); -if (!dentry) { + dentry =simple_start_creating (root, s); +if (IS_ERR (dentry)) {iput(inode);
fs/devpts/inode.c +1
-1-return ERR_PTR (-ENOMEM ); +return dentry;}
dentry->d_fsdata= priv;
d_make_persistent(dentry, inode);
fsnotify_create(d_inode(root), dentry);fs/devpts/inode.c +1
-1-dput (dentry); +simple_done_creating (dentry);return dentry; // borrowed
}---
base-commit:666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261117-work-devpts-pty_new_locking-9b3d01e643c3