Skip to content

Commit 2505a98

Browse files
kerneltoastakpm00
authored andcommitted
zsmalloc: fix races between asynchronous zspage free and page migration
The asynchronous zspage free worker tries to lock a zspage's entire page list without defending against page migration. Since pages which haven't yet been locked can concurrently migrate off the zspage page list while lock_zspage() churns away, lock_zspage() can suffer from a few different lethal races. It can lock a page which no longer belongs to the zspage and unsafely dereference page_private(), it can unsafely dereference a torn pointer to the next page (since there's a data race), and it can observe a spurious NULL pointer to the next page and thus not lock all of the zspage's pages (since a single page migration will reconstruct the entire page list, and create_page_chain() unconditionally zeroes out each list pointer in the process). Fix the races by using migrate_read_lock() in lock_zspage() to synchronize with page migration. Link: https://lkml.kernel.org/r/20220509024703.243847-1-sultan@kerneltoast.com Fixes: 77ff465 ("zsmalloc: zs_page_migrate: skip unnecessary loops but not return -EBUSY if zspage is not inuse") Signed-off-by: Sultan Alsawaf <sultan@kerneltoast.com> Acked-by: Minchan Kim <minchan@kernel.org> Cc: Nitin Gupta <ngupta@vflare.org> Cc: Sergey Senozhatsky <senozhatsky@chromium.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 60a60e3 commit 2505a98

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

mm/zsmalloc.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,11 +1718,40 @@ static enum fullness_group putback_zspage(struct size_class *class,
17181718
*/
17191719
static void lock_zspage(struct zspage *zspage)
17201720
{
1721-
struct page *page = get_first_page(zspage);
1721+
struct page *curr_page, *page;
17221722

1723-
do {
1724-
lock_page(page);
1725-
} while ((page = get_next_page(page)) != NULL);
1723+
/*
1724+
* Pages we haven't locked yet can be migrated off the list while we're
1725+
* trying to lock them, so we need to be careful and only attempt to
1726+
* lock each page under migrate_read_lock(). Otherwise, the page we lock
1727+
* may no longer belong to the zspage. This means that we may wait for
1728+
* the wrong page to unlock, so we must take a reference to the page
1729+
* prior to waiting for it to unlock outside migrate_read_lock().
1730+
*/
1731+
while (1) {
1732+
migrate_read_lock(zspage);
1733+
page = get_first_page(zspage);
1734+
if (trylock_page(page))
1735+
break;
1736+
get_page(page);
1737+
migrate_read_unlock(zspage);
1738+
wait_on_page_locked(page);
1739+
put_page(page);
1740+
}
1741+
1742+
curr_page = page;
1743+
while ((page = get_next_page(curr_page))) {
1744+
if (trylock_page(page)) {
1745+
curr_page = page;
1746+
} else {
1747+
get_page(page);
1748+
migrate_read_unlock(zspage);
1749+
wait_on_page_locked(page);
1750+
put_page(page);
1751+
migrate_read_lock(zspage);
1752+
}
1753+
}
1754+
migrate_read_unlock(zspage);
17261755
}
17271756

17281757
static int zs_init_fs_context(struct fs_context *fc)

0 commit comments

Comments
 (0)