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 CFD30CE7A94 for ; Mon, 25 Sep 2023 12:25:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 482938D0021; Mon, 25 Sep 2023 08:25:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 42FA78D0001; Mon, 25 Sep 2023 08:25:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F7708D0021; Mon, 25 Sep 2023 08:25:19 -0400 (EDT) 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 2047F8D0001 for ; Mon, 25 Sep 2023 08:25:19 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DFCB5C09D7 for ; Mon, 25 Sep 2023 12:25:18 +0000 (UTC) X-FDA: 81275039916.08.8573340 Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) by imf11.hostedemail.com (Postfix) with ESMTP id F097C40010 for ; Mon, 25 Sep 2023 12:25:15 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=NvBe09A2; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf11.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.222.176 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695644716; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YfegDYKBT6wkjhbo8Ou4Wlol6W+gElO2eBlC6j3i9zc=; b=QqZ5T61G7P+RUPixrwkqW1XKQ/3EXb2KVMUUuEQ7FOOec1Wov9/PeZLkQwiN7valkf0BYg ISz+KBH2yUIpzQIbhDn/8iZTggM3Y6kcbWR0xbNU1UcdAPyktS8rP1PQFL/IYmz9oyIp01 0m5Porqj2HsLCurCv0APhmS3Gq9TyRQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=NvBe09A2; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf11.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.222.176 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695644716; a=rsa-sha256; cv=none; b=NPVecZHwDXSyn2QZWsl73fP6QdHjMtocZbGxwgm2B5CEEbSVX4yspFkZLr1BXQavBvArRO uVX6STlibIjrsHcnWXbWi1QuVN60xvtixi77CbkRjIWoICVBfE5evf6WOYkh286pN4iVW3 h1CuaXDScNdUfzxma4vXs79wrczlBn8= Received: by mail-qk1-f176.google.com with SMTP id af79cd13be357-7743448d88eso99732385a.2 for ; Mon, 25 Sep 2023 05:25:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20230601.gappssmtp.com; s=20230601; t=1695644715; x=1696249515; darn=kvack.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=YfegDYKBT6wkjhbo8Ou4Wlol6W+gElO2eBlC6j3i9zc=; b=NvBe09A2/xqK1AdDKqMxcAqbnHO6enCua4bCx6USH7XDdcoieI5IdgA+Jaikwsstg9 6WNWf01xkgcPTJm8dQttRzgBfULYCdVmBfwjHTvhC9asjVbT0ZSgIhR6QqGsd1NHdqMA ELTXErdByARTTgXLf1oEibjC+DqJWo0AwJNRbkH9WxuO5Bj+GQsSqowXMPxhD0HEnLMF AwG6qjujWKdaOpVLZrpq1yaXPYNVpUFsPA3tk5qMJREKQ8p6kEfOnGs8OnDydEGD5q5b QMcA434FljJw2MfvA20o/bUn/TQd8ouScGjRZknBTpVe5gwo7X5f1m92mioA7+z4ACsP jHPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695644715; x=1696249515; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YfegDYKBT6wkjhbo8Ou4Wlol6W+gElO2eBlC6j3i9zc=; b=ilwxyW/OSK9Bs7UCThu4C2uDfJxnmJ04w4Jqqgr9uHxD9JC6d4AN/IpIphR0nGWS3M mnutdLCt0ZakgbdYEbP0yu+0rddOpl6Rkl/hTMmURvHswQL/ca9vKghpTnEV3wJe1JYp b89xDWU6x/RUTVhzgv7iVD8AaLcsu4BgLuueQwv+YJT6E+2yKvzMd9HJOnvrep5YpSBQ vG46RuBrdWM3/tLAFgB921ydQEg9jYlIIZQqLNpRhG3tbKRVue5e7kheI405j9ycNNK5 7qivGpvcoVugxs5hIScffI2xm6qFIG9qqq7Kis4CH5rt9z4uiLuyzCFhItol7XaWfvql sKvg== X-Gm-Message-State: AOJu0YwghXKgPLwzP9wl4z7izD2Ch6ryd91xUqtkdMOz9m70VnCAH6RL 5AnmR+vtBnRdfOc3pNzYBF60+A== X-Google-Smtp-Source: AGHT+IE0HQi6fewMm+p89bKgS2wyZhegjV0VPfXglDGQjZ3Ga/xG8IlIvVqHzJPZevS7sicmbehZHA== X-Received: by 2002:a0c:f512:0:b0:65b:748:cdd3 with SMTP id j18-20020a0cf512000000b0065b0748cdd3mr3188605qvm.29.1695644714927; Mon, 25 Sep 2023 05:25:14 -0700 (PDT) Received: from localhost ([2620:10d:c091:400::5:ba06]) by smtp.gmail.com with ESMTPSA id e27-20020a0caa5b000000b0065b0d2f9121sm1012863qvb.68.2023.09.25.05.25.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 05:25:14 -0700 (PDT) Date: Mon, 25 Sep 2023 08:25:13 -0400 From: Johannes Weiner To: Domenico Cerasuolo Cc: sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, akpm@linux-foundation.org, nphamcs@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: Re: [PATCH] mm: zswap: fix potential memory corruption on duplicate store Message-ID: <20230925122513.GA347250@cmpxchg.org> References: <20230922172211.1704917-1-cerasuolodomenico@gmail.com> <20230922174225.GF124289@cmpxchg.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspamd-Queue-Id: F097C40010 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 77wnb4djrd3tuu58nq4pjkfezk9xwi18 X-HE-Tag: 1695644715-97642 X-HE-Meta: U2FsdGVkX1/r7ruxg+8EaQCo5xyBlINTx6dKsx8DTRIE8VmS0cgIy8QGAc9a4DSVMu2aKaqiWToXHqtDdaxdIWSopk2g872ku0VERHwPGIFH4y7G1bYlNV8LU9cD/v4klFcJefwTnV22IRyqQgo5H5DHkfePxHbVD/14OEn6bG7oB62nIBPgEy5SZJP7Ji9SRcn6kZUo5yYdiZJwKMB6TF6m7qOjcBDbEQ9gGS+8l7sqM5XsToVGW+t2p30g0fselvRXojyJbI5cNcFPq7wgIVGzR3cM/Ar3Oww8eEtSRd/3mm+mJGaReHrXBfHjoHI6cbP0g94z4jJ9jg5UpQNa1ppHHWUx+mX5Z7xW1Ywa1P6N1xqr/8CyOOU6dw3jzzpagOa9/cyy4q6xDC3hSuj/B+4Ccc7mRUh+212K03q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qiaYECxN0NTAEwqQEYnSCU= 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: On Mon, Sep 25, 2023 at 10:36:06AM +0200, Domenico Cerasuolo wrote: > On Fri, Sep 22, 2023 at 7:42 PM Johannes Weiner wrote: > > > > On Fri, Sep 22, 2023 at 07:22:11PM +0200, Domenico Cerasuolo wrote: > > > 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. > > > > > > Based on commit ce9ecca0238b ("Linux 6.6-rc2") > > > > > > Fixes: 42c06a0e8ebe ("mm: kill frontswap") > > > Signed-off-by: Domenico Cerasuolo > > > > Acked-by: Johannes Weiner > > > > > @@ -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); > > > > Do we still need the dupe handling at the end of the function then? > > > > The dupe store happens because a page that's already in swapcache has > > changed and we're trying to swap_writepage() it again with new data. > > > > But the page is locked at this point, pinning the swap entry. So even > > after the tree lock is dropped I don't see how *another* store to the > > tree at this offset could occur while we're compressing. > > My reasoning here was that frontswap used to invalidate a dupe right before > calling store(), so I thought that the check at the end of zswap_store() was > actually needed. Ok the git history is actually really enlightening of how this came to be. Initially, frontswap didn't invalidate. Only zswap did. Then someone ran into exactly the scenario that you encountered: commit fb993fa1a2f669215fa03a09eed7848f2663e336 Author: Weijie Yang Date: Tue Dec 2 15:59:25 2014 -0800 mm: frontswap: invalidate expired data on a dup-store failure If a frontswap dup-store failed, it should invalidate the expired page in the backend, or it could trigger some data corruption issue. Such as: 1. use zswap as the frontswap backend with writeback feature 2. store a swap page(version_1) to entry A, success 3. dup-store a newer page(version_2) to the same entry A, fail 4. use __swap_writepage() write version_2 page to swapfile, success 5. zswap do shrink, writeback version_1 page to swapfile 6. version_2 page is overwrited by version_1, data corrupt. This patch fixes this issue by invalidating expired data immediately when meet a dup-store failure. This split the invalidation duty: On store success, zswap would invalidate. On store failure, frontswap would. Then this patch moved the invalidate in frontswap to before the store: commit d1dc6f1bcf1e998e7ce65fc120da371ab047a999 Author: Dan Streetman Date: Wed Jun 24 16:58:18 2015 -0700 frontswap: allow multiple backends at which point the after-store invalidate in zswap became unnecessary. > Since we acquire the tree lock anyway to add the new entry to the LRU, wouldn't > checking the result of zswap_rb_insert be a very cheap sanity check? We could > treat it as an error and fail the store(), or just add a warning and still > invalidate the dupe? Yeah, it's less about performance and more about code clarity. A warning probably makes the most sense. It gives us the sanity check and an opportunity to document expectations wrt the swapcache, while keeping the code resilient against data corruption.