From patchwork Mon Sep 25 13:00:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Domenico Cerasuolo X-Patchwork-Id: 144599 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp1500123vqu; Mon, 25 Sep 2023 14:41:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH9iNvEunpzTI5kvQyifrzcOP35VWnmeoYSNJVnz/fGxYCEppMiSDAVVvvFTDlcOaiHMmca X-Received: by 2002:a05:6870:a990:b0:1c4:ee87:d3ea with SMTP id ep16-20020a056870a99000b001c4ee87d3eamr9751408oab.36.1695678066118; Mon, 25 Sep 2023 14:41:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695678066; cv=none; d=google.com; s=arc-20160816; b=TDo2LM6nulx1QAegmuB2oDOmbqXkUfqjIHevFobyUC/9BlFRlhY+LjNSYfnL0Hbmk3 ZcE94TEJTELOAS1+ioYLksqFX1DqGXoCxyO6dpypnmOAKPdcBa+TzexzoP/8aFXP29sM ni0Lso0LxYnRJDZzFofO4UkzXdP7O3GrIkTndaEctKKc6YpAb5kzYhzNgkhSNsW0Ikm6 yKUlQx6+o8jOkrBGlDFycjYFxRK5Ob230GBJJbz/VP5f630EuFcK+bxlWFWMsYW+PpIt dPUblNY2Teh8UDNpGknYM+DAr4+K64XyM37DsoiSGnqg8VTKPRgGPeMq3lFQYKMMz3td tR8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=d8bPvgPYa85OE64RYeOWcGAjGkQSeFdTUqTGSpUdX4g=; fh=cKMvQAbTP2wmKs4LHJQR/IFnQmdk2rhyIZIVejh/HgQ=; b=lzAofUsP6yE8U7UJd7gs4joRoSMv/ylIZVVusNsTyTTiionLDhO8P1JtNbF1PJxKqW FT4tRRuWyLUyCfscBL0ch607yT8aLQhjuV7IkRj375fnNWCU01z+U+FvdCHRkKZa+9kS 4Q+xLTqq0AyVqeScB0Q33Uw+XFPV0WLYgrOEoiYh0w3r1neSmEvT/7+rRXhAKvdNBLq/ TcbEToiH9awEc+tTEdpkgtkJuX4sI/z/zZW8Yqr5BTD1nQK5jJFdXIj8fWPB7/JGnWN8 UQ34yImzL4amJ6u3BFKtaaneTUP8k4qVNRAEosR8DoLUqqrn5KX1zdV9SuGT6Zwoptlp gqfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=A4IpTBhO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id 36-20020a631564000000b00573fef93a87si10786763pgv.834.2023.09.25.14.41.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 14:41:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=A4IpTBhO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id E6CA080779A3; Mon, 25 Sep 2023 06:00:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231451AbjIYNAU (ORCPT + 29 others); Mon, 25 Sep 2023 09:00:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231539AbjIYNAS (ORCPT ); Mon, 25 Sep 2023 09:00:18 -0400 Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E72BE107 for ; Mon, 25 Sep 2023 06:00:10 -0700 (PDT) Received: by mail-ej1-x62c.google.com with SMTP id a640c23a62f3a-9adca291f99so777695266b.2 for ; Mon, 25 Sep 2023 06:00:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695646809; x=1696251609; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=d8bPvgPYa85OE64RYeOWcGAjGkQSeFdTUqTGSpUdX4g=; b=A4IpTBhOisx6zqUEyyh86iIyD4NhNjg0FhIqcAp7Yt3dN/SVTC1kdXimsMieLbARa7 JxXdul0lBQGQLQX7zVP7gPmNSt8/oYVFJc+b2KCWX4CWpFFE0K2TvuUuYZEq25Gnv74z iWtVyXfzAh1cgyLo7TXtAOtiX6xzTpbChpTZuI4mOWU6LwFVGAdSTJRATJhIVCtgRkjh vWynwrRLypEvDBKvZn8d6uHxVYUBuYWmD074KI1NHilw8TkO7qGmOSLTQyMCmX+J7KIQ cUEuB6P635EFK0WjAsJVIJUxfcy1qCIf9GOjPQKvJMOx/Hx2W0+RqVn2s3Yoh6sh+xnA T+KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695646809; x=1696251609; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=d8bPvgPYa85OE64RYeOWcGAjGkQSeFdTUqTGSpUdX4g=; b=T3QOt4ZJaGnXZ7bH1dSNwut2wtsYMK3hhJa2RQWosq8ieFxI/jhBMR0jfUyT2mU+BR Y5gIXvkjkWdnNbqpphqSn7PYxKyVwgHPj4q6dETvmigBNnag/LVqC9TCu+jj3KRTYO4S zxWIhNEkBp1DzeGf2tF2yZ7VMrPzIroCZq+7EE2d2qNREF7Mn519ldxiC+HfQMU5zqwK VYrIp/sPtt6r7sYjyGPOLJ1UyAkyD5aY/3Ac30eT0Y12YpU7iy0iu7slCKRmraijDnCB 8m8akchMsTRIq0JxHciTKakFvmhPT+T0ZB4/SAjMCC5JPMf7WU52EorG3lP6WSzu7Ai5 dL9g== X-Gm-Message-State: AOJu0Yxef20VaFQEW7gFn31wCDWKtT7gWXFIY279KON+2W/GhB4uneIb UV0l527uO1c5XPyuiwIPG4k= X-Received: by 2002:a17:906:55:b0:9ad:78b7:29ea with SMTP id 21-20020a170906005500b009ad78b729eamr6237656ejg.44.1695646808582; Mon, 25 Sep 2023 06:00:08 -0700 (PDT) Received: from lelloman-5950.. (host-80-182-134-115.pool80182.interbusiness.it. [80.182.134.115]) by smtp.gmail.com with ESMTPSA id i8-20020a17090685c800b009ad7fc17b2asm6301108ejy.224.2023.09.25.06.00.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 06:00:07 -0700 (PDT) From: Domenico Cerasuolo To: sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, akpm@linux-foundation.org, nphamcs@gmail.com, hannes@cmpxchg.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Domenico Cerasuolo Subject: [PATCH v2] mm: zswap: fix potential memory corruption on duplicate store Date: Mon, 25 Sep 2023 15:00:02 +0200 Message-Id: <20230925130002.1929369-1-cerasuolodomenico@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 25 Sep 2023 06:00:29 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778047323475099885 X-GMAIL-MSGID: 1778047323475099885 While stress-testing zswap a memory corruption was happening when writing back pages. __frontswap_store used to check for duplicate entries before attempting to store a page in zswap, this was because if the store fails the old entry isn't removed from the tree. This change removes duplicate entries in zswap_store before the actual attempt. V2: - Added a warning and a comment to the second duplicates check in zswap_store function (Johannes). Based on commit ce9ecca0238b ("Linux 6.6-rc2") Fixes: 42c06a0e8ebe ("mm: kill frontswap") Signed-off-by: Domenico Cerasuolo --- mm/zswap.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/mm/zswap.c b/mm/zswap.c index 412b1409a0d7..083c693602b8 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1218,6 +1218,19 @@ bool zswap_store(struct folio *folio) if (!zswap_enabled || !tree) 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); + dupentry = zswap_rb_search(&tree->rbroot, offset); + if (dupentry) { + zswap_duplicate_entry++; + zswap_invalidate_entry(tree, dupentry); + } + spin_unlock(&tree->lock); + /* * XXX: zswap reclaim does not work with cgroups yet. Without a * cgroup-aware entry LRU, we will push out entries system-wide based on @@ -1333,7 +1346,14 @@ 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. + */ while (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) { + WARN_ON(1); zswap_duplicate_entry++; zswap_invalidate_entry(tree, dupentry); }