Skip to contentSkip to conversation

Hole punch races in GFS2

DISCUSSION Jan Kara 3 participants 5 messages Apr 22, 2021 – Apr 29, 2021 1 message not in the archive VFS

lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

Hello,

I am looking into how GFS2 protects against races between hole punching and things like page fault or readahead and AFAICT it seems it does not. In particular is there anything that protects against a race like:

CPU1 CPU2
gfs2_fallocate()
__gfs2_punch_hole()
truncate_pagecache_range()
gfs2_fault()
- faults in old data into page cache
punch_hole()

And now we have stale data in the page cache (data corruption). If gfs2_page_mkwrite() sneaked in that window as well, we might be even racing with writeback and are possibly corrupting the filesystem on disk. Is there anything I'm missing?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR

A message that is not in the archive (sent off-list or not yet received): CAHc6FU7kFWtQDAAU16h3xkWkSV2dm9WWb-RbjXBusJ9+118GRw@mail.gmail.com

author reviewer
#2 lore raw
Quoted 21 lines · Andreas Gruenbacher

Hi Jan,

Quoted 13 lines · Jan Kara

I am looking into how GFS2 protects against races between hole punching and things like page fault or readahead and AFAICT it seems it does not. In particular is there anything that protects against a race like:

CPU1 CPU2
gfs2_fallocate()
__gfs2_punch_hole()
truncate_pagecache_range()
gfs2_fault()
- faults in old data into page cache
punch_hole()

Quoted 4 lines

And now we have stale data in the page cache (data corruption). If gfs2_page_mkwrite() sneaked in that window as well, we might be even racing with writeback and are possibly corrupting the filesystem on disk. Is there anything I'm missing?

Quoted 7 lines

This is controlled by the inode glock (ip->i_gl). Readers are holding a shared lock while writers are holding an exclusive lock, similar to an rwlock. Those locks take effect cluster wide (via DLM locks) and down to individual tasks locally. The only exception are resource group glocks, which use the LM_FLAG_NODE_SCOPE flag to indicate that exclusive locks should be shared locally. In that case, rgd->rd_mutex provides the exclusion among local tasks.

OK, thanks for explanation! I missed that GFS2 glocks are task local. But then I have another question. We have the following:

gfs2_file_read_iter()
grabs inode glock in shared mode
generic_file_read_iter()

    filemap_read()
      copy_page_to_iter()
        <page fault>
        grabs mmap_sem
          gfs2_fault()
            - tries to grab inode glock again -> possible recursive locking

And even if the locking isn't recursive, you have glock->mmap_sem and mmap_sem->glock lock orderings so ABBA deadlocks are possible AFAICT.

And there's a similar problem with the write path as well, just the lock is grabbed exclusively.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw

----- Original Message -----

Quoted 16 lines

OK, thanks for explanation! I missed that GFS2 glocks are task local. But then I have another question. We have the following:

gfs2_file_read_iter()
grabs inode glock in shared mode
generic_file_read_iter()

    filemap_read()
      copy_page_to_iter()
        <page fault>
        grabs mmap_sem
          gfs2_fault()
            - tries to grab inode glock again -> possible recursive locking

And even if the locking isn't recursive, you have glock->mmap_sem and mmap_sem->glock lock orderings so ABBA deadlocks are possible AFAICT.

Quoted 7 lines

And there's a similar problem with the write path as well, just the lock is grabbed exclusively.

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR

Hi Jan,

IIRC, gfs2 tries to prevent the page fault by having gfs2_file_read_iter call into generic_file_read_iter twice:

The first time without the glock held, but specifying IOCB_NOIO, which brings the pages in and returns -EAGAIN. Then it acquires the glock, then calls into generic_file_read_iter a second time.

I'm not sure if that prevents the deadlock or just makes it less likely. I still need to investigate that.

Regards,

Bob Peterson
Red Hat File Systems

author reviewer
#4 lore raw
Quoted 30 lines · Bob Peterson

----- Original Message -----

Quoted 16 lines

OK, thanks for explanation! I missed that GFS2 glocks are task local. But then I have another question. We have the following:

gfs2_file_read_iter()
grabs inode glock in shared mode
generic_file_read_iter()

    filemap_read()
      copy_page_to_iter()
        <page fault>
        grabs mmap_sem
          gfs2_fault()
            - tries to grab inode glock again -> possible recursive locking

And even if the locking isn't recursive, you have glock->mmap_sem and mmap_sem->glock lock orderings so ABBA deadlocks are possible AFAICT.

Quoted 7 lines

And there's a similar problem with the write path as well, just the lock is grabbed exclusively.

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR

Hi Jan,

IIRC, gfs2 tries to prevent the page fault by having gfs2_file_read_iter call into generic_file_read_iter twice:

Quoted 6 lines

The first time without the glock held, but specifying IOCB_NOIO, which brings the pages in and returns -EAGAIN. Then it acquires the glock, then calls into generic_file_read_iter a second time.

I'm not sure if that prevents the deadlock or just makes it less likely. I still need to investigate that.

Aha, I missed that trick. Thanks for pointing that out. But indeed this just makes the race harder to hit. If already the first page is not in the page cache and needs to be fetched from the disk, nothing is faulted in and you have to do everything in the call with glock held. And even if you prefaulted the whole buffer, there's no guarantee it doesn't get reclaimed again before you reach copy_page_to_iter().

Also the write path doesn't seem to play any tricks like this and is prone to the same kind of problem:

iomap_file_buffered_write()
iomap_apply()
gfs2_iomap_begin() - takes glock
iomap_write_actor()
iov_iter_fault_in_readable()
<fault>

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw
In reply to Bob Peterson
Quoted 25 lines · Bob Peterson

----- Original Message -----

Quoted 16 lines

OK, thanks for explanation! I missed that GFS2 glocks are task local. But then I have another question. We have the following:

gfs2_file_read_iter()
grabs inode glock in shared mode
generic_file_read_iter()

    filemap_read()
      copy_page_to_iter()
        <page fault>
        grabs mmap_sem
          gfs2_fault()
            - tries to grab inode glock again -> possible recursive locking

And even if the locking isn't recursive, you have glock->mmap_sem and mmap_sem->glock lock orderings so ABBA deadlocks are possible AFAICT.

Quoted 7 lines

And there's a similar problem with the write path as well, just the lock is grabbed exclusively.

Honza
--
Jan Kara <[email protected]>
SUSE Labs, CR

Quoted 8 lines

Hi Jan,

IIRC, gfs2 tries to prevent the page fault by having gfs2_file_read_iter call into generic_file_read_iter twice:

The first time without the glock held, but specifying IOCB_NOIO, which brings the pages in and returns -EAGAIN. Then it acquires the glock, then calls into generic_file_read_iter a second time.

The IOCB_NOIO generic_file_read_iter call is there for lockless reads, not there for faulting pages in at all. See commit 20f829999c38 ("gfs2: Rework read and page fault locking").

Andreas