From patchwork Fri Feb 9 04:41:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13550863 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 E9061C48297 for ; Fri, 9 Feb 2024 04:41:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 564516B0074; Thu, 8 Feb 2024 23:41:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5141E6B0075; Thu, 8 Feb 2024 23:41:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3DC056B0078; Thu, 8 Feb 2024 23:41:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2EC896B0074 for ; Thu, 8 Feb 2024 23:41:27 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EC1C21A047B for ; Fri, 9 Feb 2024 04:41:26 +0000 (UTC) X-FDA: 81771016572.28.ACDD3EB Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by imf16.hostedemail.com (Postfix) with ESMTP id 131D418000A for ; Fri, 9 Feb 2024 04:41:24 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=I335ID8P; spf=pass (imf16.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.175 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=1707453685; 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:in-reply-to:references:references:dkim-signature; bh=n9ZcmxH5x/iNMg1fUZA7WKzgnA9rAGjtfWpy0ewB9vQ=; b=ej9FQe4W7uw1yBGFD/FtJjDEJz7rI5zbfA0jd7UIsAM/EltU/W+FQ/ozK4Uc7KKzTCBCH7 v9x66Sp0Z7rXnleApCzaOkga/Tw+Ny5Qim/shaamLacihiw+9bkclCNfzo7C8E7AEmSHnd JRVglaqrUEQkUMINB/Zh/BcnM3kQBSs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707453685; a=rsa-sha256; cv=none; b=iGQ20QmIJKnT99mWBfOoXgpdxBrmRK/ZlerEQrNTmfArlAQ6peaN/qW875N4em8a9WM2co oA21+qukzzSfM5h/vgP6669qEd64cYQKGWln/mTAt55Qm9G/iW72uV2Xv4mz13Z4ZZ9PHS QMIQWR6KoPY6uPC31ZYcRR3//izHjSY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=I335ID8P; spf=pass (imf16.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.175 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=1707453682; 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: in-reply-to:in-reply-to:references:references; bh=n9ZcmxH5x/iNMg1fUZA7WKzgnA9rAGjtfWpy0ewB9vQ=; b=I335ID8PJECo9lA1iIAbMlbBicrenjJMo0+G62AkIn4CEaiwRTc95cmZBiToYDWLPl3utD nzqLAMSEyNktjNp6pToZNN/b2XmbyhsedEepDXc4yAipLThAChyseDkPJeD/Utu06+MqSs 7YeqyzyG1qccEpOeBYz8QdOCpiupg+w= 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 , Chris Li Subject: [PATCH mm-unstable] mm/zswap: optimize and cleanup the invalidation of duplicate entry Date: Fri, 9 Feb 2024 04:41:12 +0000 Message-Id: <20240209044112.3883835-1-chengming.zhou@linux.dev> In-Reply-To: <20240207115406.3865746-1-chengming.zhou@linux.dev> References: <20240207115406.3865746-1-chengming.zhou@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: bnp7yksdxxy344rrcj443rn388sdws13 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 131D418000A X-Rspam-User: X-HE-Tag: 1707453684-487374 X-HE-Meta: U2FsdGVkX1/EAJAN55R7COPMDLtkNjur6Gev0db5wi/gYDdNrlUx8tuGYs3tNSuxjbaqX29hC9cTQwZzwYjQlfud9+4HBWfAmkF/4OKTTGyESUZ4NOfHLTBpG488HPpZnaHsq/+TfckotW2vkBAWW3LyWir71ANPLZKxbRtGRqHATsrwmN495pOuaL6BtVv5s9sN1UDEQ4ejolLeOZCOmA2TIhYp4wfA1u3GPmaFn6WHlciyIEcisYFb9U/6gSr8AnaYjO/Wby44xwTXLFqsDrZWqeM+okaqmxhCpq2KbY3rurKADDL8Rb1Z1417NBnneN3VBJJzPw/CxhnzA2mByRQG2IsTttBbLz2Qry6ZKrkloV/O6mpnZr0SXsEOZBktfiE+X95vAbunVqrTqqSEv4oj3DluCY+yUfYa4u0Ob50Xx8fNdNHwIKpOigFBuJeAkAwOa/0pOxCPiLhRxAl0JHr6LmlAdhPpFO9zUKRUcl6kRlKscv87sEut4u8MF6kAG+1Wptn7rtSrkt0unhga3/UQqMnsS5BO/gHapqoWVW7GIcC2E4Cc8dsLqneYT7OyQFX9NlTZroFIrboQzflRoldrTHCA6VKCJGSy9N+8cAsmhLuIYwm82A+aNUjmfQrygBUm48YtHObQVdKgICjBkkKoXL2zjdfT/mGSUFzxPgJboskUppbB/4XPXIswMOeg/i8HHP+WgaVkaMYzmJHhT9VVKUaq0QQ77i9osAcqFU8pGy5kcGXuFcfhdeoi2DnqYtU0l+X1PhbbQXwCIX9bVHXoNY8ZxQNWzwrYHvufa4uNCXIWJ9eF+VTKLp4QwEmzT2KqC/bsEIRnySgfeDTj4H80ZTpeac5ojiJSP8cHWEzazTWu06Te3yHZfHW/fXlQaV9X8blyOrtRGFyS1463eceoD23V6Q52ZxJXBBaYL3+h5koeWmGB9hgXMyIBCedoW5mr4h4YtFpiL6qzOjl HNY/p+nq CgvQNmkXS+gvq1M4XN5gGK2XVJj2s9Zkzr4+SUop8/NvwUob3wCThEPgFOtbzWNH0ja4FOKOmo3dYx7tI4kBjTeCfs0Iwa1qReIHqcqlslT5SUbw0pfFibrwZaZBUIaOzeSX4OtzDU3DbOEZ4FNAap0wWoL5z0ZzTEaIk6UMD39oaroEKr0s88UFFF66A4djKVKlivcj2bcGm5L+BBiRvPbuQzgQ0iD++fgjYnnMJl1pOaQiM4NBtT++sj45yeFkzraDoRYVGwBLgRCS3lI8ET1bg3/D+U1ZvDgfzWW5PWKq8Sy1HBd934taIOIY4SzZgUUB3qDBCCi/yTQY= 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 inserting the new one, which actually doesn't have to be done at the beginning of zswap_store(). The good point is that we don't need to lock the tree twice in the normal store success path. And cleanup the loop as we are here. Note we still need to invalidate the old duplicate entry when store failed or zswap is disabled , otherwise the new data in swapfile could be overwrite by the old data in zswap pool when lru writeback. Acked-by: Johannes Weiner Acked-by: Yosry Ahmed Acked-by: Chris Li Acked-by: Nhat Pham Signed-off-by: Chengming Zhou --- mm/zswap.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 96664cdee207..62fe307521c9 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1517,19 +1517,8 @@ bool zswap_store(struct folio *folio) if (folio_test_large(folio)) return false; - /* - * 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); - if (!zswap_enabled) - return false; + goto check_old; objcg = get_obj_cgroup_from_folio(folio); if (objcg && !obj_cgroup_may_zswap(objcg)) { @@ -1609,14 +1598,12 @@ 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 may have been dirtied again, invalidate the + * possibly stale entry before inserting the new entry. */ - while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) { - WARN_ON(1); + if (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) { zswap_invalidate_entry(tree, dupentry); + WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry)); } if (entry->length) { INIT_LIST_HEAD(&entry->lru); @@ -1639,6 +1626,17 @@ bool zswap_store(struct folio *folio) reject: if (objcg) obj_cgroup_put(objcg); +check_old: + /* + * If the zswap store fails or zswap is disabled, we must invalidate the + * possibly stale entry which was previously stored at this offset. + * Otherwise, writeback could overwrite the new data in the 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: