From mboxrd@z Thu Jan 1 00:00:00 1970 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 010ADC4828D for ; Tue, 6 Feb 2024 15:00:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3A966B0074; Tue, 6 Feb 2024 10:00:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DE9B46B0075; Tue, 6 Feb 2024 10:00:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB2A36B007B; Tue, 6 Feb 2024 10:00:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BB1086B0074 for ; Tue, 6 Feb 2024 10:00:25 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 915271C09DE for ; Tue, 6 Feb 2024 15:00:25 +0000 (UTC) X-FDA: 81761690010.14.F38C24E Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) by imf13.hostedemail.com (Postfix) with ESMTP id 0BAD72003A for ; Tue, 6 Feb 2024 15:00:21 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=tUv79qfN; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf13.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.218.48 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=1707231622; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8lm1WVEGM70O+cn3cikjEPo6m6D1TFmEKWlvuRMtKW0=; b=lkve6FGCTvteUawx7oRQGmRHhxqLgU/55Hdy0zuZ+I4OwSdjfBweUonUSFjWjG3WLk2Svv w3ezfxzABcXLXSF8sDoLrYibLu5t/Xc6l7entJQaidZl6io06rolmPHB46ojoUsUIYmqPV yP8jpQXWXJlkuoJXm6FZUsNKF/LzaKE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=cmpxchg-org.20230601.gappssmtp.com header.s=20230601 header.b=tUv79qfN; dmarc=pass (policy=none) header.from=cmpxchg.org; spf=pass (imf13.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.218.48 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707231622; a=rsa-sha256; cv=none; b=8K2qS6uI+0020l3MsNvPPvrX8bwjfbC9T/avIHi1SpNVsDP//OFu6vV3x0vmn6JenjQ9/k hkPGBJQoAUtcuxYAAhyXkEhJPySkMFtf/w2ypmIjj/wcENUTEtAJuNkwF7sicXa6uVgWUP drxrA9cmVUQVMX8AIBkPynOd9eQD1xU= Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-a384a7e6103so33128366b.1 for ; Tue, 06 Feb 2024 07:00:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20230601.gappssmtp.com; s=20230601; t=1707231620; x=1707836420; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=8lm1WVEGM70O+cn3cikjEPo6m6D1TFmEKWlvuRMtKW0=; b=tUv79qfNFfwvMSBgGFQoCjEZkS/88lauqeHe6pZnP+Hm4TNyYUv2TJSPDuTQZ5Cuyj nd7NX17Lf1Ou7lhem9ZbNx0KYUreLle9G2SHuGkZz/BxILda3/aaQVGGNvVwYTHv70hC uCeI6Z9foX1v5CHI8C0gi7ACueyiRP5jMcxH7vjQH6Un7edXaXI2zOIAORvIINY9bf0e XJBQRykpSLNmHuUeOGCtj7fKu0gUi8wN7xEJ/hcvCh969/u1E6bCfKXVNWG6wz+5yd57 S9K38ol/SZ3yGlZv0dwAC2i7LgmUE4CsIBB3Pqj/FSiNjleFFey78/AlT+nQoGeGqxtM HqDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707231620; x=1707836420; h=in-reply-to: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=8lm1WVEGM70O+cn3cikjEPo6m6D1TFmEKWlvuRMtKW0=; b=WeWBXU0ey2T5fCUaHtM0fqIFx33sUW+Hpf/5GmdUnfHYkmksV2eaxWN5I47HMZD/4/ Z+a4iEsU9OAeTbNOiuHNckkMdvExK6kK0ECaoAtI457b+T3wHtLWdMwpuJ82xiQocMbh bs0931kM7MIaRlGyJ/k9fcvNjpZH7L80snvMz9tchqlXvgBOhEaNfUfuaXksIEtWxv5d 3ZKFQ5+XgXOxJAphLGUer74sDmwmA1D54IJ8oiQ3ZB+HOv3UCN6ET+RPV1fUDV6vOdYi UixFAP3GUGwvPgCeMVelyP4IxcCcz5UnF1ezJ+Ui4qXjVFoMqA7vxirOgq2jyd39ZeBc y7Sg== X-Gm-Message-State: AOJu0YytD9fIVdwYxkrxRjJFo7R8Bnj+gwSROKoDe+vUTsidbHCgSAN8 dQhU4ZIMAdhXqHD3ppuLsaNAkVdgncumV2vyadHx1rdkwwd+PpF9rIpvHP266dI= X-Google-Smtp-Source: AGHT+IGceBr3BsfPSINxik1wZC9wfeqv3DtYddrVTGjXJSAa0LOiPkbthXoSt5XOCFWUqO14w9hajA== X-Received: by 2002:a17:906:80d3:b0:a38:4b2c:8178 with SMTP id a19-20020a17090680d300b00a384b2c8178mr434721ejx.19.1707231620196; Tue, 06 Feb 2024 07:00:20 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCVgJHarqCma8HvB4toK09zIBbeF/vn+Hodrsf4cUEiVZB62SW8VfBlBDme8C6t2Y6OXpdCAglRYf6XoYKHIlaM5cCrVBBZKuowQ5ElUrZezJom3+VbV7fXpxgh2etzjGFMvIAKv2F/XkuN56zuEQyjUfkxm57aFYYIjMg4UvE0DIRdvBbcyBcd4KBGfGc3SEzEXuMwB1CX4yw2Qp7qquazdY4ZrJKS+y3M2x/I= Received: from localhost ([2a02:8071:6401:180:f8f5:527f:9670:eba8]) by smtp.gmail.com with ESMTPSA id h21-20020a170906585500b00a369b47996esm1242213ejs.80.2024.02.06.07.00.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 07:00:19 -0800 (PST) Date: Tue, 6 Feb 2024 16:00:14 +0100 From: Johannes Weiner To: Chengming Zhou Cc: Yosry Ahmed , nphamcs@gmail.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chengming Zhou Subject: Re: [PATCH] mm/zswap: invalidate old entry when store fail or !zswap_enabled Message-ID: <20240206150014.GA54958@cmpxchg.org> References: <20240204083411.3762683-1-chengming.zhou@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 0BAD72003A X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 3med1f8x6fx17m9oemkzfmqpqi6wbw9p X-HE-Tag: 1707231621-117028 X-HE-Meta: U2FsdGVkX19JxP64WyXQ0ifTn6kF13YMYQv/feBaHcs/O81wrH1aBn44bLQa9jQP5xSPBB2R2EW7AQwarAYKP5aaTz7k1RKeEwO2wjaFo4wzR5Ka64xDgp8i70E88nSwlUo+jDbxlnoj0B032Phcdd7ISi3+HMR+MeANdgJoDI2wWh4yDJv2ZwMkoQauv0NQTI+Qu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VbXPktYMPCLbGUCfQ2mtFV/NP65zadSFlUPIq+Zo9ibyMMptXY25s1Cvc1leZU7s1oJPuGZk4FVYdae8cihnOBk2X0Sqpdilp9BOgjPbm2LECKBLQ5R10HYHcTkBxpO7orMZm2JkdRGv2c9aKIW3qITqWqUg90/e/faqvzRhdTsiTgWY0fA4aXrJo7TAvXG1m4xLItIx9uO6bjfhfKVjtUWQBKJ/zzzgD0vKl71T/ZXLeiFpNuXN4RKsF5zi4gphe3Brj1/+hEtl7dor8NTGIEQe8fgaYYR0+LIDjpo6utR+4r5YxnZnhXI9xry01R1LlE/kyjRRYNei+oPhPfAZ6UCIc/iF1EyGM4StnKDaQP4feXLYzaC9dnt28UQbZX+aT+2eKYf6CCX2R3Ohp39l5lqWI4HLwQhtTtimMWhIxU1RglGYZt62DRK8vyZuOQnBsirgisidObhKiPM8Njdo+CgrdHZdERp45+g== 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: On Tue, Feb 06, 2024 at 10:23:33AM +0800, Chengming Zhou wrote: > On 2024/2/6 06:55, Yosry Ahmed wrote: > > On Sun, Feb 04, 2024 at 08:34:11AM +0000, chengming.zhou@linux.dev wrote: > >> 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. > > > > I think this may have been introduced by 42c06a0e8ebe ("mm: kill > > frontswap"). Frontswap used to check if the page was present in > > frontswap and invalidate it before calling into zswap, so it would > > invalidate a previously stored page when it is dirtied and swapped out > > again, even if zswap is disabled. > > > > Johannes, does this sound correct to you? If yes, I think we need a > > proper Fixes tag and a stable backport as this may cause data > > corruption. > > I haven't looked into that commit. If this is true, will add: > > Fixes: 42c06a0e8ebe ("mm: kill frontswap") You're right, this was introduced by the frontswap removal. The Fixes tag is appropriate, as well as CC: stable@vger.kernel.org. > >> 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. > >> > >> This fix is not good, since we have to grab lock to check everytime > >> even when zswap is disabled, but it's simple. > > > > Frontswap had a bitmap that we can query locklessly to find out if there > > is an outdated stored page. I think we can overcome this with the > > xarray, we can do a lockless lookup first, and only take the lock if > > there is an outdated entry to remove. > > Yes, agree! We can lockless lookup once xarray lands in. > > > Meanwhile I am not sure if acquiring the lock on every swapout even with > > zswap disabled is acceptable, but I think it's the simplest fix for now, > > unless we revive the bitmap. > > Yeah, it's simple. I think bitmap is not needed if we will use xarray. I don't think the lock is a dealbreaker in the short term. We also take it in the load and invalidate paths even if zswap is disabled, to maintain coherency during intermittent enabling/disabling. It hasn't been an issue in production at least. > >> Signed-off-by: Chengming Zhou > > > > For now, I think an if condition is clearer: > > > > if (zswap_rb_insert(&tree->rbroot, entry, &dupentry) == -EEXIST) { > > zswap_invalidate_entry(tree, dupentry); > > /* Must succeed, we just removed the dup under the lock */ > > WARN_ON(zswap_rb_insert(&tree->rbroot, entry, &dupentry)); > > } > > This is clearer, will change to this version. Agreed! With that: Acked-by: Johannes Weiner