From patchwork Wed Feb 7 02:58:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chengming Zhou X-Patchwork-Id: 197713 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1965990dyb; Tue, 6 Feb 2024 19:00:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6dWPEm8dt1CHIejT1RuBgv6mkgRvryddEkGmDms0YYtSvroIIuZ/PL6QJWyJOtfrV11f8 X-Received: by 2002:a05:6e02:1647:b0:363:c8c2:50f with SMTP id v7-20020a056e02164700b00363c8c2050fmr6127343ilu.30.1707274855560; Tue, 06 Feb 2024 19:00:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707274855; cv=pass; d=google.com; s=arc-20160816; b=SnN1lMbkzzSWmlOCK2SJjFRDXv0vg/qP1nIrOlwFtbNliPZmwK2ygEAzSu5kC3Qho0 TiVu7uzov1SKasm8Da5pyk2vfNv+fbXC02Cw/9d3eIl2+9LaYbdYW28c1ySRZLJn7kYZ aRJNI0YgVA62KHt9c5/4LKLlEZ4VFClFTfHwo8bxnAvWpUGPFQ0v7UyUIAkmdu+4FL50 XN90NFocybh0/nrtQNui/PYphTtxLmLZPLgTmimUpDCeBCXHTVzapG2My+/hRfYZGJx4 pIRBDr4UbJFya/s+PAUP8mZmhyOcPyDcRq7AlpxgKroDf4uBU5bqNSe1c7IsWOFbUXyW s/3A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=YdTolOktysfsfJtvyDb6Ex+kid808dGZqD8vDTLFsAI=; fh=VtqZ33uwsoCtMvMzhFic3cST/5hJ/Gjs5uOgOXCmsWg=; b=LbCyBh/2KAqYiK5h9h0eQLn2Q40XSjP2GM7eHAzFLneWJ9ncExc1xZaMHiz4MYlXxw V8nb2PICWDfh9bAft+UOVZEEZIfNPhjSMcOHinn1WkiIxkU1OUILKfWCVenGwoOjx31V pvL8CT8Kt5uz0E2d8OtYAOOBxiPMkdL/83sCmHKdIO/bMazFEOrhCstv8slHSH2TDZrl ZErvTYSgEd141/2W1WrgoyBL6ZXeNmTnu1xHToW9xdXzpRBKVz+/T++gNqy/hyLgTy/H aIrPIgO2vJxis534HDkXZc/M7MqacGxdPKGgg2zJm23M9RmZ93z0ypAKPbH2685u/WBh 3niA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=q7G7j77C; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-55877-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55877-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev X-Forwarded-Encrypted: i=2; AJvYcCVQBSz/CqsPVpD8rBd+TZzG1dTT6Cg/EE2YuYYwcxUDKokzBZvH+iNT+OA4n0gFOnSY7BVbjJ+B1v0DMO1LWnJuweDcow== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id y26-20020a637d1a000000b005d7bcb4fda2si377968pgc.831.2024.02.06.19.00.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 19:00:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55877-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=q7G7j77C; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-55877-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55877-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 269A8283612 for ; Wed, 7 Feb 2024 02:59:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6BDD9101D5; Wed, 7 Feb 2024 02:58:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="q7G7j77C" Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C100014A8D for ; Wed, 7 Feb 2024 02:58:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707274728; cv=none; b=LCtvXkend1I4lOgxqVuTuqJZi44Bv4Z6wyprQEWZtkc8dbRR0nXPjaBCB9F7SpAmrOItZiblUOeH1GXLFQfo5uYj/7fU1c6pK8xtlfXYR2shGozJlzvP16e1T1XrIx5Aiv3+vrwj6KSg/TmmX0+TBOgFpjyrV7GDNuntQ2CTqm4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707274728; c=relaxed/simple; bh=GssavB2eBu3MCykfKG7uDgs+4Abjn9OOgra4EZk4ZRI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=Fi1QxarIKg8s59frV4dp3VIXQOba7OgKAC451zfOQALMidBFsKZVdiTqhBsFdKNhKgsGSDZURL2pYi7aMd5kq1V9MmdJ7Ca0tviBYLlnr/COaDpETzrR7NDwKGjg7aEKukgNyaOUaE97l+m2OMo9CUkqVuX1Ge1e1N4YQJoG0QQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=q7G7j77C; arc=none smtp.client-ip=91.218.175.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=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=1707274725; 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=YdTolOktysfsfJtvyDb6Ex+kid808dGZqD8vDTLFsAI=; b=q7G7j77CtIpEYihaJPQD5FrbBwFuoFV3H4fjX/HELJ4abMv+2lreF8Kj5Du1EbcV+t5GOE Z+jUosaM3kuUumKWYypEFJcBX9rx/Y0mJbs2C9+CAPG0yFxrPKwQ59CODm6Gh38wwNhcdX n+5PRk20yQ/4NS2e4y/WopprGTOCuAU= 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 v2] mm/zswap: invalidate old entry when store fail or !zswap_enabled Date: Wed, 7 Feb 2024 02:58:27 +0000 Message-Id: <20240207025827.3819141-1-chengming.zhou@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790207438972262638 X-GMAIL-MSGID: 1790207438972262638 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 Signed-off-by: Chengming Zhou Acked-by: Yosry Ahmed --- 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..6c1466633274 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 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); + 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 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: