mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-12 13:55:32 +00:00
vfs: fix page locking deadlocks when deduping files
[ Upstream commitedc58dd012
] When dedupe wants to use the page cache to compare parts of two files for dedupe, we must be very careful to handle locking correctly. The current code doesn't do this. It must lock and unlock the page only once if the two pages are the same, since the overlapping range check doesn't catch this when blocksize < pagesize. If the pages are distinct but from the same file, we must observe page locking order and lock them in order of increasing offset to avoid clashing with writeback locking. Fixes:876bec6f9b
("vfs: refactor clone/dedupe_file_range common functions") Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Reviewed-by: Bill O'Donnell <billodo@redhat.com> Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
parent
c62760f0c6
commit
1aca2008e2
1 changed files with 41 additions and 8 deletions
|
@ -1882,10 +1882,7 @@ int vfs_clone_file_range(struct file *file_in, loff_t pos_in,
|
|||
}
|
||||
EXPORT_SYMBOL(vfs_clone_file_range);
|
||||
|
||||
/*
|
||||
* Read a page's worth of file data into the page cache. Return the page
|
||||
* locked.
|
||||
*/
|
||||
/* Read a page's worth of file data into the page cache. */
|
||||
static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset)
|
||||
{
|
||||
struct address_space *mapping;
|
||||
|
@ -1901,10 +1898,32 @@ static struct page *vfs_dedupe_get_page(struct inode *inode, loff_t offset)
|
|||
put_page(page);
|
||||
return ERR_PTR(-EIO);
|
||||
}
|
||||
lock_page(page);
|
||||
return page;
|
||||
}
|
||||
|
||||
/*
|
||||
* Lock two pages, ensuring that we lock in offset order if the pages are from
|
||||
* the same file.
|
||||
*/
|
||||
static void vfs_lock_two_pages(struct page *page1, struct page *page2)
|
||||
{
|
||||
/* Always lock in order of increasing index. */
|
||||
if (page1->index > page2->index)
|
||||
swap(page1, page2);
|
||||
|
||||
lock_page(page1);
|
||||
if (page1 != page2)
|
||||
lock_page(page2);
|
||||
}
|
||||
|
||||
/* Unlock two pages, being careful not to unlock the same page twice. */
|
||||
static void vfs_unlock_two_pages(struct page *page1, struct page *page2)
|
||||
{
|
||||
unlock_page(page1);
|
||||
if (page1 != page2)
|
||||
unlock_page(page2);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare extents of two files to see if they are the same.
|
||||
* Caller must have locked both inodes to prevent write races.
|
||||
|
@ -1942,10 +1961,24 @@ int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
|
|||
dest_page = vfs_dedupe_get_page(dest, destoff);
|
||||
if (IS_ERR(dest_page)) {
|
||||
error = PTR_ERR(dest_page);
|
||||
unlock_page(src_page);
|
||||
put_page(src_page);
|
||||
goto out_error;
|
||||
}
|
||||
|
||||
vfs_lock_two_pages(src_page, dest_page);
|
||||
|
||||
/*
|
||||
* Now that we've locked both pages, make sure they're still
|
||||
* mapped to the file data we're interested in. If not,
|
||||
* someone is invalidating pages on us and we lose.
|
||||
*/
|
||||
if (!PageUptodate(src_page) || !PageUptodate(dest_page) ||
|
||||
src_page->mapping != src->i_mapping ||
|
||||
dest_page->mapping != dest->i_mapping) {
|
||||
same = false;
|
||||
goto unlock;
|
||||
}
|
||||
|
||||
src_addr = kmap_atomic(src_page);
|
||||
dest_addr = kmap_atomic(dest_page);
|
||||
|
||||
|
@ -1957,8 +1990,8 @@ int vfs_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
|
|||
|
||||
kunmap_atomic(dest_addr);
|
||||
kunmap_atomic(src_addr);
|
||||
unlock_page(dest_page);
|
||||
unlock_page(src_page);
|
||||
unlock:
|
||||
vfs_unlock_two_pages(src_page, dest_page);
|
||||
put_page(dest_page);
|
||||
put_page(src_page);
|
||||
|
||||
|
|
Loading…
Reference in a new issue