Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp892076rwr; Wed, 3 May 2023 07:33:02 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4J/Bz6l8h1GLW6Pw2x9oWe9NUu3cRzuj43/coNJzMMuJZP5UE/cE+azQt0v79RvpHVOh6N X-Received: by 2002:a17:90a:346:b0:247:2baf:d6f with SMTP id 6-20020a17090a034600b002472baf0d6fmr5526764pjf.0.1683124382372; Wed, 03 May 2023 07:33:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683124382; cv=none; d=google.com; s=arc-20160816; b=SfrQNkqffVM+2P1fzTgfLF/kSBy6IG7JjFlt8tsf+WUjIltxw2DttTEOrxsFD5Z30Y mjmLEuO3bUndSjhghj27Wnnl/LI8zbNyjQkg7Tk0RsHobqA4NIQ6biYUS5ZcT+tx9x5I HdxPIkPY3lcslXa+G/DbDyz13EraIIDeVSiKPNwWqa7d7B5wLHPyG/8SakihALVrG3GW +oo6eItVcLvIX2JCj8vkyaxMBz1w/fXz+W326wXUd2d/cWZknn4Ef0oV+SjBV4xjw++z Rh735IU6A2M9O+51xei0TjfZnHlCQKN5ppyEvBygg+DmsKncOWsqojT5+B1X7M4jqSmh gVAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=qmZ4DsDKrMe1S2UiM4lF3iPDDNQLVdB3fN/nPm0O+pI=; b=KsDVyD1tw9V20NL458v1AVOCxh7O129FiKpc4+rn2FFoqeOhznoLy7jRJ+N0is9Eho mGAX5i60TW3albg/WfY8ADgNrN44BA6FGNjmJdHa4Cu72KD6sgbt/uofD4NC7gWqYwd+ MWZ9uhuSuGv7X3tqp8ibTBk8ONjTA+FmOFqjD3IaJSDoPIhsL8lEKA+Fy7+6/3jyOy2m wUDIA9rAi20PIFxx7Ga5Hm0/3ZvNqlwF86Z+2tSE/lw9NbMtkqlabkngOx5WOxeJZbn9 6DmN7lnT62u8qTLL5mDGlOh9hJTdJhP5/SndTCzJ7geDkkBF5OfDrjQGJCEcIikW4QCc Sfcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20221208.gappssmtp.com header.s=20221208 header.b=xOFe0H29; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o6-20020a17090ac70600b0024bb7190b08si13567222pjt.153.2023.05.03.07.32.50; Wed, 03 May 2023 07:33:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@cmpxchg-org.20221208.gappssmtp.com header.s=20221208 header.b=xOFe0H29; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230297AbjECO2x (ORCPT + 99 others); Wed, 3 May 2023 10:28:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230197AbjECO2v (ORCPT ); Wed, 3 May 2023 10:28:51 -0400 Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A81341FF6 for ; Wed, 3 May 2023 07:28:48 -0700 (PDT) Received: by mail-qk1-x733.google.com with SMTP id af79cd13be357-75178b082a5so131339385a.1 for ; Wed, 03 May 2023 07:28:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20221208.gappssmtp.com; s=20221208; t=1683124128; x=1685716128; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=qmZ4DsDKrMe1S2UiM4lF3iPDDNQLVdB3fN/nPm0O+pI=; b=xOFe0H29iDHpimotgJRcaLX6/SmYqAhUgt7QykkIwaN5ArFrr67+xXLlpVVlQMvSm1 RPn5ONAVfnr8xMJvUZgHIsgicCh2NlpHkZzlbotmQg6XyHJqN7GmMCOqgT9QEq8Nk0DP 6XQSoE+g/zWIZz8yvYHylhmN5XnjZ8zEB0Dl9ALOgHd+jkrXz5ToigDNZ+eabCcyxBr4 duYN1MXQv6CJKolySvtsT3waS1hGoiQBI737dGIe+txiceEkohRDoMTQuKYoAh5GolMY y9RKrb4aUP/zDYZBwSqZuOcVknnLVFmCgmjVsnlBTLxd9dXRHcHEs8SKonvX5HVljRV6 aI3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683124128; x=1685716128; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qmZ4DsDKrMe1S2UiM4lF3iPDDNQLVdB3fN/nPm0O+pI=; b=b6gFqM8kdNBQnB3VRQyfO/+nfwWZmzor3pJPYf2u+y+UenKI31VQCnfpLFTspUha69 fLmBQ77UVOPB1C+qpe6Eo90roSYlRyOMchAEh63YdoowVjJsI5ENIm66GyQtHMLwLFt5 cqPgcKTAwmWCzJ1sDWEXZhAn0e80YoUwbKA1Rg3QZZm6XaoChRo57lXvuP6twSdt3AaA g7zp8c6qXdWIw/VnMgAW2eQNmoUsgBb3a2QrXpTHRb9jfUAjZ+V/QjqILUriebq7l+gS tp4wk08KYwbRPGnB97MpOgQng4AsKgiHkV47yhpSHsLlwjK5RUS9LiRnOkdd7QiVSuf0 pwrw== X-Gm-Message-State: AC+VfDy5KNubW7gAxQauIKr+xOlstDNMlZJlcZcIHlDXWVW3bbwtvcPc Xi9pZAZdAFEm5SNyh2hTis6a7jV1XWmnBTAQFHA= X-Received: by 2002:ad4:4ea6:0:b0:5fd:7701:88c5 with SMTP id ed6-20020ad44ea6000000b005fd770188c5mr11763060qvb.6.1683124127795; Wed, 03 May 2023 07:28:47 -0700 (PDT) Received: from localhost (2603-7000-0c01-2716-8f57-5681-ccd3-4a2e.res6.spectrum.com. [2603:7000:c01:2716:8f57:5681:ccd3:4a2e]) by smtp.gmail.com with ESMTPSA id b27-20020a0cb3db000000b005e750d07153sm10588375qvf.135.2023.05.03.07.28.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 May 2023 07:28:46 -0700 (PDT) Date: Wed, 3 May 2023 10:28:46 -0400 From: Johannes Weiner To: Domenico Cerasuolo Cc: sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, minchan@kernel.org, ngupta@vflare.org, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm: fix zswap writeback race condition Message-ID: <20230503142846.GA193380@cmpxchg.org> References: <20230503132148.9682-1-cerasuolodomenico@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230503132148.9682-1-cerasuolodomenico@gmail.com> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 03, 2023 at 03:21:48PM +0200, Domenico Cerasuolo wrote: > The zswap writeback mechanism can cause a race condition resulting in > memory corruption, where a swapped out page gets swapped in with data > that was written to a different page. > > The race unfolds like this: > 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 > > The fix: > Once the swap page cache has been allocated (case ZSWAP_SWAPCACHE_NEW), > zswap-shrink work just checks that the local zswap_entry reference is > still the same as the one in the tree. If it's not the same it means > that it's either been invalidated or replaced, in both cases the > writeback is aborted because the local entry contains stale data. > > Reproducer: > I originally found this by running `stress` overnight to validate my > work on the zswap writeback mechanism, it manifested after hours on my > test machine. The key to make it happen is having zswap writebacks, so > whatever setup pumps /sys/kernel/debug/zswap/written_back_pages should > do the trick. > In order to reproduce this faster on a vm, I setup a system with ~100M > of available memory and a 500M swap file, then running > `stress --vm 1 --vm-bytes 300000000 --vm-stride 4000` makes it happen > in matter of tens of minutes. One can speed things up even more by > swinging /sys/module/zswap/parameters/max_pool_percent up and down > between, say, 20 and 1; this makes it reproduce in tens of seconds. > It's crucial to set `--vm-stride` to something other than 4096 otherwise > `stress` won't realize that memory has been corrupted because all pages > would have the same data. > > Signed-off-by: Domenico Cerasuolo > --- > mm/zswap.c | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/mm/zswap.c b/mm/zswap.c > index f6c89049cf70..d20d60266bc8 100644 > --- a/mm/zswap.c > +++ b/mm/zswap.c > @@ -995,6 +995,19 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle) > goto fail; > > case ZSWAP_SWAPCACHE_NEW: /* page is locked */ > + /* > + * if the entry in the tree has been replaced, it means that we would > + * be overriding the swap page with stale data, let's not do that. > + */ > + 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); I think it would be beneficial to explain the exact race in the comment above. That our local reference to the zswap entry doesn't exclude swapping from invalidating and recyling 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. With that, please add: Acked-by: Johannes Weiner