Merge tag 'nvme-6.2-2023-02-15' of git://git.infradead.org/nvme into block-6.2
[linux-block.git] / fs / file.c
index 5f9c802a5d8d34454f2382196ef9d81b43e76bd9..c942c89ca4cda90b94eaf724a2dabd63853bb52e 100644 (file)
--- a/fs/file.c
+++ b/fs/file.c
@@ -1003,7 +1003,16 @@ static unsigned long __fget_light(unsigned int fd, fmode_t mask)
        struct files_struct *files = current->files;
        struct file *file;
 
-       if (atomic_read(&files->count) == 1) {
+       /*
+        * If another thread is concurrently calling close_fd() followed
+        * by put_files_struct(), we must not observe the old table
+        * entry combined with the new refcount - otherwise we could
+        * return a file that is concurrently being freed.
+        *
+        * atomic_read_acquire() pairs with atomic_dec_and_test() in
+        * put_files_struct().
+        */
+       if (atomic_read_acquire(&files->count) == 1) {
                file = files_lookup_fd_raw(files, fd);
                if (!file || unlikely(file->f_mode & mask))
                        return 0;