From patchwork Sun Feb 4 08:34:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13544568 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 F198DC48286 for ; Sun, 4 Feb 2024 08:35:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85B4D6B0078; Sun, 4 Feb 2024 03:35:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E4216B007B; Sun, 4 Feb 2024 03:35:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 685086B007D; Sun, 4 Feb 2024 03:35:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4FBFA6B0078 for ; Sun, 4 Feb 2024 03:35:32 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 26B9A40424 for ; Sun, 4 Feb 2024 08:35:32 +0000 (UTC) X-FDA: 81753462504.27.00CD1C1 Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) by imf27.hostedemail.com (Postfix) with ESMTP id 659044000F for ; Sun, 4 Feb 2024 08:35:30 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=LChvhoaI; spf=pass (imf27.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.174 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707035730; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=Pbt84SQ0eMAct0yO+KuRCNRVPEgcExEkWU6nZFC8ea8=; b=AYOXIirNf0XHEEtvppHMnX3+n3S+P/Zgco/47O1jOzhB11ozkdtpkcsQhNg+JuNJRq05CV x+cN9PdwQDqQ+2Ugce/PDGX8EfE4C53GAyuycfHHDVDbt3H6TcajzkzHUuog6EDMPa/fir KPmnblxGYSRJUyPMcMHW9rwoOIQCi+A= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707035730; a=rsa-sha256; cv=none; b=fJVzjUIuWYF54IO4lpW0I0sexQBlvrg7nRsOdoBhmbRjv90t0P2ftwlCJExjOnHUVrW50I G4FP5/YERnhxZhjNRKk2Y0CrFX2+/cwVdXHHDuzVk3SZvbzO4B8UDm2e72OFMg0+DPL7Rt Os1UCV0wTcZJhI9ax+BVIY/Bob5LieQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=LChvhoaI; spf=pass (imf27.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.174 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1707035727; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Pbt84SQ0eMAct0yO+KuRCNRVPEgcExEkWU6nZFC8ea8=; b=LChvhoaIhcZ6/hsqUPJEAUZUHV/gRdrP42CjAcuvwiYvTOsVaLLc6efBOMJk+LpmTog1wQ pOm5xY1EOmu42MeHI95DQcyqYCQ588RUrZQD/l1Yvj+HkWCihFqZ9KntIDAlmJJ640OYSR iaAGKZn+vdSF+CHtaOX71CxoxmjGVKU= From: chengming.zhou@linux.dev To: hannes@cmpxchg.org, yosryahmed@google.com, nphamcs@gmail.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, chengming.zhou@linux.dev, Chengming Zhou Subject: [PATCH] mm/zswap: invalidate old entry when store fail or !zswap_enabled Date: Sun, 4 Feb 2024 08:34:11 +0000 Message-Id: <20240204083411.3762683-1-chengming.zhou@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 659044000F X-Rspam-User: X-Stat-Signature: 1obxxxszosi1kf1rnawb37g3qi9a59jo X-Rspamd-Server: rspam03 X-HE-Tag: 1707035730-130113 X-HE-Meta: U2FsdGVkX1/p2Od4srOvva4zrxNN4Tbxx6oiR3npCgomIkIYB4mzQrfdrby9acdCbWtOGanQ87S4fjBf3b4uwnng/vU5+dbTfx71vOvbzvjZyeZ2/gdIQcwN1eE+Y5DyVjo0ou5w/D86pR7lXuoBawPbV4+cWB1DToWsorDhkdZ7qzN6N9tuaZpECc/xgaee66ySFdSWeenQHccAkMlp5d1rt/MkbqVOn7bbyMegBE3vz+qAdVPSlljlUsZVg+FkNl9I5fpbxFThW7sOjf4T0YW/2Q4w+xzGuvSHNyrt8378LCOQGdMHbyixwdnuzzmTDy7FYotaSnfFsuo/KWXYEUFdWYfLuMusEGCODvkuyUPFGLPB14v0HWhP/U09tI7gpKFOY9FoU20EA02R4pRH7cOlNagzzuukMrcspZbotLp2lVVuviIQSjbiwnZCsUxEvbcGS/E+DsLldV6Zj+8Ak307rVx1IqPMiR/NYgGHRne5zqrUnNKJqzvE92EIdhyFQcTp4Hx/iOqEksmYQ+Ig4b0FwwgOVizYXJExU6j74dvA0M0KN7HXQpRkUuysw31RXteXfMnZ8F8Sak8b5f4Lse08CfBiEp2h7z41/+ccexJRbtKiXS4uQ+3dXQ4EuPYyGwOia7UFl7Phgm9HNJKrm5jlETKGbj148xxt9c14kBCUH17ZYtN9KaajoPLkq33mYSMUdN3bBfHIdvZaKLES1tNjYkmASAWlcleC0Bo1mQAsU1Y6Wd1uSjcOLv/fb2nAtfvJ8m4V72HzjrH4BdawQWtiw4+9S+rLOiNyLxfjj7glSZhFg/3Qmjea4TMc/bXulcdw0+Q4sSQe9P3KIRIzSMqcfoctz2H2CfbAGfCZpqq0CNaMmjYoPhxbpG2lrlqxCM0AnapMU24Ea6cUkyqFM6WYLT3iWLmdVAvUJVGfBn03ogDdHcj8dLx6NzKpZ16pMgmL3JTbkxTXnaDMiAy ZWDKMdV2 xIx5vfa4/QvpXKLMvufZHTs32lOnKsF15yQ/eKy/9eszamKPyUpGaVX/0JwtjdskxEhc6jvsI3j/MogkAFrqMP/snMgns0xtteQ8V7MuHV3pui349pqnlCM3rirNaggw82KptiGfYGDqW30bqm0WWuqtfu4U+mIdPQwA0ydmGZT12eO6+qKFl9YFLXD3AHTYiijTxBp2071vCnQnWPdWQ9P1gf+j0IuU34BOZ 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: 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. 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. Signed-off-by: Chengming Zhou Acked-by: Johannes Weiner --- 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); - } 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: