lkml.org 
[lkml]   [2023]   [May]   [3]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH v2] mm: fix zswap writeback race condition
Hi Domenico,

On Wed, May 03, 2023 at 05:12:00PM +0200, Domenico Cerasuolo wrote:
> 1. a page with data A and swap offset X is stored in zswap
> 2. page A is removed off the LRU by zpool driver for writeback in
> zswap-shrink work, data for A is mapped by zpool driver
> 3. user space program faults and invalidates page entry A, offset X is
> considered free
> 4. kswapd stores page B at offset X in zswap (zswap could also be full,
> if so, page B would then be IOed to X, then skip step 5.)
> 5. entry A is replaced by B in tree->rbroot, this doesn't affect the
> local reference held by zswap-shrink work
> 6. zswap-shrink work writes back A at X, and frees zswap entry A
> 7. swapin of slot X brings A in memory instead of B

Thanks for the interesting discovery.

> V2:
> - updated comment with better explaination of the situation being
> addressed in the check
>
> Signed-off-by: Domenico Cerasuolo <cerasuolodomenico@gmail.com>
> Acked-by: Johannes Weiner <hannes@cmpxchg.org>
> ---
> mm/zswap.c | 16 ++++++++++++++++
> 1 file changed, 16 insertions(+)
>
> diff --git a/mm/zswap.c b/mm/zswap.c
> index f6c89049cf70..5d5977c9ea45 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
> @@ -995,6 +995,22 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
> goto fail;
>
> case ZSWAP_SWAPCACHE_NEW: /* page is locked */
> + /*
> + * Having a local reference to the zswap entry doesn't exclude
> + * swapping from invalidating and recycling the swap slot. Once
> + * the swapcache is secured against concurrent swapping to and
> + * from the slot, recheck that the entry is still current before
> + * writing.
> + */
> + spin_lock(&tree->lock);
> + if (zswap_rb_search(&tree->rbroot, entry->offset) != entry) {
> + spin_unlock(&tree->lock);
> + delete_from_swap_cache(page_folio(page));
> + ret = -ENOMEM;
> + goto fail;
> + }
> + spin_unlock(&tree->lock);
> +

The race condition is still there, just making it much harder to hit.
What happens after you perform the rb tree search, release tree lock.
Then the entry gets invalid and recycled right here before the decompress
step?

> /* decompress */
> acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
> dlen = PAGE_SIZE;
>

Chris


\
 
 \ /
  Last update: 2023-05-04 00:00    [W:0.058 / U:0.436 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site