From patchwork Wed Feb 7 03:38:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 13548087 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 BA289C48297 for ; Wed, 7 Feb 2024 03:39:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 303DF6B0075; Tue, 6 Feb 2024 22:39:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 28CAB6B0081; Tue, 6 Feb 2024 22:39:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17B246B0083; Tue, 6 Feb 2024 22:39:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 09E1F6B0075 for ; Tue, 6 Feb 2024 22:39:15 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AC78DA224C for ; Wed, 7 Feb 2024 03:39:14 +0000 (UTC) X-FDA: 81763602228.22.639955E Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) by imf17.hostedemail.com (Postfix) with ESMTP id D5C0740006 for ; Wed, 7 Feb 2024 03:39:09 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tzISHbzm; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf17.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707277152; 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=r51ZHhQk2ZaTew8zad3jGSIvU9CtfS131K9n+R66Vck=; b=tei0wkspfSrx9SLOEvXA5B8JbmhWaE98n6LO3Mahl+Dofu6goWrYvp/BUCjv9gKRBalOPi SE5gRnMqf9uGg0XhArA20XcaX61EW6mbS0BK2n/QTQTHBwOTm+RU92ZL9UIVrzBY7r1sIV WmHiVjUZ10EbKKNUVow6XmyYoyIvX00= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tzISHbzm; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf17.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.183 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707277152; a=rsa-sha256; cv=none; b=dmt4/BAPm29A2V08T/wfMOwNjwb6bhKime1ybNgG6MTOpyjPsbwqI7M8lf+Pbpmd2HNeGa tFUWAXpr1EpLTuFTGlflW26z052sxTOCXgp/n4Kh0MO/CqDCBzVnJNhjIKE2l2F1dpxP38 uCT6RSHJwioaSXiFZQWS4f2gVK4bXM0= 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=1707277145; 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=r51ZHhQk2ZaTew8zad3jGSIvU9CtfS131K9n+R66Vck=; b=tzISHbzm075OGdT/f9lsPmcEQiaosAn5+saw6etkpXkrnxxsBsgoP5jKFWsQRWogQZXMP7 XCz5X6x2lcU73ooaCivufa3yT4TPJgC25UqqAtDFS50jXx7YHMTcyR6CEqSanl3BSIJEK8 sq4uF7+m6RZYHD/nc8Hm3u6xvNITHNs= 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 , stable@vger.kernel.org Subject: [PATCH v3] mm/zswap: invalidate old entry when store fail or !zswap_enabled Date: Wed, 7 Feb 2024 03:38:57 +0000 Message-Id: <20240207033857.3820921-1-chengming.zhou@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D5C0740006 X-Stat-Signature: msny7f7aqtohwe9b7zjrur6k199r49tm X-Rspam-User: X-HE-Tag: 1707277149-356964 X-HE-Meta: U2FsdGVkX19w6l6zpiv6xJd5ITXLyE6b6xtyPjYtm7mLO7pjY0tMcBHkFEh5RDBHNvXY22vL+74zCCgFjbllhNk+A8HcHYBJsatJAkB1VLO08B61blYZ3gKwgfX5PqJbRnuXdH/XgIFsMLyv1lgQjVXa/C3PpAXQQmMucD8lc4Ubse42xsAyrcF7EaFlleVgAtdj6DnJHweESpXO4alY2u2mTcElHNtRMGvePI0XtgX0pZIftlXmq+WUwVoh3WHPjkVlwuO5gVD3xnoh0e/HB4+hL7tovKb3bNpUG2J2NC3ErMjxKMsMi+wytrNDMyGrRoM4xwKO6b0Q1ZgttuChtVx9GPqa6E5D9JyGGZZozO6GOLSAvVwuweSMAdL91BEp7GE7xJyVFm8Y0DxTKUM7RGTvb0fjbYqlZGZfNRxN/bYVyhXAoaZv4GdicSedUL/8iVoPqnxbnDklLGuRUfX2ebnVPpXdNnBnPpZArFQ0vL62RfPnnOQMc6h79JbQkapP632oes715njUttrHhji495AoFYtibdf5IG0H85Dw7gcvmSLaVQpWez+f+cyOklUusUoRIeN70omvZE1Tb6IM3HQ1KB82nftem17ujH0chEuZ4mdkS8xG2XQZi6jj58PxbNEN/8BzpLH7bRDjKhNzNM2JXDmlK1Ht/aFNC/JIz9HEGrGlVppwUuMGkU9xyQE+DFIeracISANHMprL7g5Ag+AcvtBFYQ4woHVTxR9fjj/xrI9V/29R55rRP5J/9nJYKbRiDOt3qANFtITcQUJ4/eeWNvqZ5fXG8EpA18zrsCKKzzOQR6tvlEdRwDxt3x4jf+OxlSVcjYeN2PCcBEhGeMFHUaGKaZoiXvce3YVc3HmrkYnT0popNRIR5rze5QbwyusHGXGAKZ44qD4dNCB4/RVK2DaMobHu1rKSJNMowBHDqBuagE2JnlT3TDdx/6nSbwSETwN7q0NTZm44eUH Ak0Z+PYX TXjXhx/UqVBVijYNPqB8WYnHg8ncs+hBr0Lh8tvuAiRDurLBElasCJq402XfZElU5+bd2uqTnf1AvzjUeAqfzW+LGOHO+7GasrAgYRgKFFj0SRWzNw5juL6gyL8vTdrn1pXGJoqK+u3kUXAPVbcq5XvZJapWu6OrVuCN2jBZyAoOPibBar9YG23uu1UTDm08zUKUpXXuJSUahK3cfkE0/bc9zF/qZjepuBpMx0shCCMZt79Pt6rRE9GyvTLrCniZTjnIrSWug8kLj1f/4AxfIY9cydWwwswxGo+68VdRp8tBGtZa+bGgNqXOqp7PUSHRLx34sRN9RIqUkqVhbyhrPPkiHSia/oKlKa6BrS/ydVJir1pycKpdFx7Tq4sFAkK+XJ/q7FOW+Hww+HoU= 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. Fixes: 42c06a0e8ebe ("mm: kill frontswap") Cc: Acked-by: Johannes Weiner Acked-by: Yosry Ahmed Signed-off-by: Chengming Zhou Acked-by: Chris Li --- v3: - Fix a few grammatical problems in comments, per Yosry. v2: - Change the duplicate entry invalidation loop to if, since we hold the lock, we won't find it once we invalidate it, per Yosry. - Add Fixes tag. --- mm/zswap.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index cd67f7f6b302..d9d8947d6761 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,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); + VM_WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry)); } if (entry->length) { INIT_LIST_HEAD(&entry->lru); @@ -1638,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: