From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5469CC48260 for ; Mon, 5 Feb 2024 22:56:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C25A46B0075; Mon, 5 Feb 2024 17:56:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BD6336B0078; Mon, 5 Feb 2024 17:56:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A9DD76B007B; Mon, 5 Feb 2024 17:56:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 97B8E6B0075 for ; Mon, 5 Feb 2024 17:56:00 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 566EF40AD7 for ; Mon, 5 Feb 2024 22:56:00 +0000 (UTC) X-FDA: 81759259680.17.A637E21 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf05.hostedemail.com (Postfix) with ESMTP id 9359B10000E for ; Mon, 5 Feb 2024 22:55:58 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rdnGVU4Q; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3fWfBZQoKCIY8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3fWfBZQoKCIY8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707173758; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2ng2e0Mk0os0T2CuSo4hFQWlpZrULav2tPmScvzzfgA=; b=WqDAouJB5hATMGEHcBNWFEH1zCiqE+TL04Usi06j6T79yVEWejqWb5zcvQBHNnY13pUIQa UtN0I25+fYG6ALAMTU2nt5THVvbCjJMvLYkWKulE1USV2/I2ULXBr9CaAm/ELhNRqi2suU nDPZRhllzK+tlUZqoI3davdjw/bgP8A= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=rdnGVU4Q; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3fWfBZQoKCIY8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3fWfBZQoKCIY8y218krwonqyyqvo.mywvsx47-wwu5kmu.y1q@flex--yosryahmed.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707173758; a=rsa-sha256; cv=none; b=IsDyud8+DAn4xGzI31nVEp7KgTOehg9+MJwnSRuFuRORIL9MLXcZon8nFvOWwLNijCOBcK U1HgEah77ld3QcJYNP6hvvS4Vtcq6oIUpS/3kL3qkwrvn4TUmcpOem/41ABtdBIwnsJp9x S66u26ir9yYni0CSNtfC2yQlxlhBKtM= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5f0fd486b9aso69974347b3.2 for ; Mon, 05 Feb 2024 14:55:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707173757; x=1707778557; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2ng2e0Mk0os0T2CuSo4hFQWlpZrULav2tPmScvzzfgA=; b=rdnGVU4QbmMKW8hGd6cK0CIYQHE5lqyzHVsAgjJj4kbfiePu8S+RP2zvRuSPnSZ4N5 44C7CCsEKogkrsUQdYz5a6mzWbciotYv9fqwx9bt5D5zhrS7Y4a0luudIwg7x/ZdxxLg TIHqsI/7VmXEu2y/0RHbT7xlWvSjYDXlxgBt3/MmvbDQwAyRPiGcKobIRMZY00GG5xge Vct7+b8gz4BcWRRcU/f5U4lgYrJUX6RrTaRDsvhWZek984ltUjagY3pOifzNS9/yQ1EA CKgVHBFf5kjsdNTscbPwEUD/USDcGJTiaufe80DRvpJgwZVyeQ0K0QUlwo1sz4tTi9k6 2l9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707173757; x=1707778557; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2ng2e0Mk0os0T2CuSo4hFQWlpZrULav2tPmScvzzfgA=; b=ZXmzmhymEC/dY6C9J/qY8ID1t6fs7mgENNw5XZylRfFdxLxdwbddUBl++WVqlNyDzF 3FtzpYpz3BtXXnU/Bg94gWzFEJyxqE/eapUnbO09ucvr26CDJpMkFHaBoQ/OrmF9SFRD PzmJjP8UAptIlz8ls4IRKnDsQlNG6lhG7IaolSQsoomJy45iL6WriqvWzs2vh1lO5Aun 8tNQJMu4xfOmkC0sDDoavO2OWbYgCob8Dz1Ntq6VA8uwd7j9IvYlc7ukbYj2fk1UJN+7 ulUHl+YxTPAd2ImpMRoCR+QGNJTkoIgs1Xr1AYRGElV6apWLhE+FHaIaH9aKVPhFjWhR 82+A== X-Gm-Message-State: AOJu0YzXtzozj8gMTfVxPofePMZWZJXKaVHdAQtIHLQMgLkDQUzf6Let cTA7ODUB6ULUUq/L6pahruMB9Y5O0IMeQOzVtivkDNpRstnCct6m5lhX7ZvynsNrpQrOz3enFcQ 4/Uv1bOYgkkanqFHSOQ== X-Google-Smtp-Source: AGHT+IH9gyc29/buiu53gaGpMwLh7J/ItpL9EQ5ysoaPUZAnd1ueM5sG0fNmWuyiFaAwabgq4g9O/sCU77WwpXBB X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:29b4]) (user=yosryahmed job=sendgmr) by 2002:a05:690c:398:b0:5ff:5135:ffcf with SMTP id bh24-20020a05690c039800b005ff5135ffcfmr186830ywb.4.1707173757594; Mon, 05 Feb 2024 14:55:57 -0800 (PST) Date: Mon, 5 Feb 2024 22:55:55 +0000 In-Reply-To: <20240204083411.3762683-1-chengming.zhou@linux.dev> Mime-Version: 1.0 References: <20240204083411.3762683-1-chengming.zhou@linux.dev> Message-ID: Subject: Re: [PATCH] mm/zswap: invalidate old entry when store fail or !zswap_enabled From: Yosry Ahmed To: chengming.zhou@linux.dev Cc: hannes@cmpxchg.org, nphamcs@gmail.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chengming Zhou Content-Type: text/plain; charset="us-ascii" X-Rspamd-Queue-Id: 9359B10000E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: f837webpp91drpgnd8zjj5pxtfz1apkn X-HE-Tag: 1707173758-468412 X-HE-Meta: U2FsdGVkX1+MM4vClfVgX8UQ8VauvKBbChv4RsX/RNURDowbD9rATYdaLWc9gddPfa4l74a0z5zAbV0jCqBTUKU5OUqEFBfaU4FK0vxxCOKoV+QnRF6cH3h9A8ZQ7VuNej3db7Pd8T/5hi/GbN9/6qj6R4gtNLfNoYUWa+1t8umnab82wrkkYUQECeU6i67KKf2cZR2798C+TWBEHeX/SrlrvMF1Jrv8E7gg7IUUOJ7CXrytitVfnMB4so1dIX6RDS+exODPOEjxxjDN7dPHIk2lXAZOJ+TTVNtWuEL+oFP+21m1AoQ1oZBng4Ckff6TOU4GQqjFeSvVe0JnyHRevOE0qYV6WarQtIjhbIxt718+zZ4cy3TTfhmVu1Z0C0pPM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npSIJMFS/lQeNsCfoeFb9er5xVSltAnrYgaSAQDqWrA0C947GDaCd8pPice0Z+eHY5PW6PtihJE5FoJlfyInsdimPk2LQIAiZQKBwT7Kt2UDsJkR9G0jZGRHxZAwEIpPV8qrTLcw9JjK24hy5bduH+Wsnotu0x5LEwybfMyrQXV/ltVW83f7UvXjtqqGi8BU62ZTByfd6utBBfS+MLePgvw0UwiQ1msF+vDdC9LAUKSSWkNjrxX3nmmqgHpCaOKcVOahy3cyRFRPe4a042Shm2V9x8nS1XBwJWob4 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Sun, Feb 04, 2024 at 08:34:11AM +0000, chengming.zhou@linux.dev wrote: > From: Chengming Zhou > > We may encounter duplicate entry in the zswap_store(): > > 1. swap slot that freed to per-cpu swap cache, doesn't invalidate > the zswap entry, then got reused. This has been fixed. > > 2. !exclusive load mode, swapin folio will leave its zswap entry > on the tree, then swapout again. This has been removed. > > 3. one folio can be dirtied again after zswap_store(), so need to > zswap_store() again. This should be handled correctly. > > So we must invalidate the old duplicate entry before insert the > new one, which actually doesn't have to be done at the beginning > of zswap_store(). And this is a normal situation, we shouldn't > WARN_ON(1) in this case, so delete it. (The WARN_ON(1) seems want > to detect swap entry UAF problem? But not very necessary here.) > > The good point is that we don't need to lock tree twice in the > store success path. > > Note we still need to invalidate the old duplicate entry in the > store failure path, otherwise the new data in swapfile could be > overwrite by the old data in zswap pool when lru writeback. I think this may have been introduced by 42c06a0e8ebe ("mm: kill frontswap"). Frontswap used to check if the page was present in frontswap and invalidate it before calling into zswap, so it would invalidate a previously stored page when it is dirtied and swapped out again, even if zswap is disabled. Johannes, does this sound correct to you? If yes, I think we need a proper Fixes tag and a stable backport as this may cause data corruption. > > We have to do this even when !zswap_enabled since zswap can be > disabled anytime. If the folio store success before, then got > dirtied again but zswap disabled, we won't invalidate the old > duplicate entry in the zswap_store(). So later lru writeback > may overwrite the new data in swapfile. > > This fix is not good, since we have to grab lock to check everytime > even when zswap is disabled, but it's simple. Frontswap had a bitmap that we can query locklessly to find out if there is an outdated stored page. I think we can overcome this with the xarray, we can do a lockless lookup first, and only take the lock if there is an outdated entry to remove. Meanwhile I am not sure if acquiring the lock on every swapout even with zswap disabled is acceptable, but I think it's the simplest fix for now, unless we revive the bitmap. > > Signed-off-by: Chengming Zhou > --- > mm/zswap.c | 33 +++++++++++++++------------------ > 1 file changed, 15 insertions(+), 18 deletions(-) > > diff --git a/mm/zswap.c b/mm/zswap.c > index cd67f7f6b302..0b7599f4116d 100644 > --- a/mm/zswap.c > +++ b/mm/zswap.c > @@ -1518,18 +1518,8 @@ bool zswap_store(struct folio *folio) > return false; > > if (!zswap_enabled) > - return false; > + goto check_old; > > - /* > - * If this is a duplicate, it must be removed before attempting to store > - * it, otherwise, if the store fails the old page won't be removed from > - * the tree, and it might be written back overriding the new data. > - */ > - spin_lock(&tree->lock); > - entry = zswap_rb_search(&tree->rbroot, offset); > - if (entry) > - zswap_invalidate_entry(tree, entry); > - spin_unlock(&tree->lock); > objcg = get_obj_cgroup_from_folio(folio); > if (objcg && !obj_cgroup_may_zswap(objcg)) { > memcg = get_mem_cgroup_from_objcg(objcg); > @@ -1608,15 +1598,11 @@ bool zswap_store(struct folio *folio) > /* map */ > spin_lock(&tree->lock); > /* > - * A duplicate entry should have been removed at the beginning of this > - * function. Since the swap entry should be pinned, if a duplicate is > - * found again here it means that something went wrong in the swap > - * cache. > + * The folio could be dirtied again, invalidate the possible old entry > + * before insert this new entry. > */ > - while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) { > - WARN_ON(1); > + while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) > zswap_invalidate_entry(tree, dupentry); > - } I always thought the loop here was confusing. We are holding the lock, so it should be guaranteed that if we get -EEXIST once and invalidate it, we won't find it the next time around. This should really be a cmpxchg operation, which is simple with the xarray. We can probably do the same with the rbtree, but perhaps it's not worth it if the xarray change is coming soon. For now, I think an if condition is clearer: if (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) { zswap_invalidate_entry(tree, dupentry); /* Must succeed, we just removed the dup under the lock */ WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry)); } > if (entry->length) { > INIT_LIST_HEAD(&entry->lru); > zswap_lru_add(&entry->pool->list_lru, entry); > @@ -1638,6 +1624,17 @@ bool zswap_store(struct folio *folio) > reject: > if (objcg) > obj_cgroup_put(objcg); > +check_old: > + /* > + * If zswap store fail or zswap disabled, we must invalidate possible > + * old entry which previously stored by this folio. Otherwise, later > + * writeback could overwrite the new data in swapfile. > + */ > + spin_lock(&tree->lock); > + entry = zswap_rb_search(&tree->rbroot, offset); > + if (entry) > + zswap_invalidate_entry(tree, entry); > + spin_unlock(&tree->lock); > return false; > > shrink: > -- > 2.40.1 >