Skip to contentSkip to conversation

devpts: lookup racing with devpts_pty_new() can return ENOENT

BUG Cheng Yongkang 1 participant 1 message Nov 15 VFS

lore
Share & tools
lore
#0 lore 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