Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp3035623rdb; Fri, 22 Sep 2023 16:28:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFlM2xF2hNkH3pJx4A+52lb5FxBmAItFykRfSzZhgUtxDAR3FZrnOrZwf+W7OT7vUMRaN/J X-Received: by 2002:a17:902:f548:b0:1b8:9fc4:2733 with SMTP id h8-20020a170902f54800b001b89fc42733mr779700plf.3.1695425328116; Fri, 22 Sep 2023 16:28:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695425328; cv=none; d=google.com; s=arc-20160816; b=GjeVpGo88mhXYk4TcS5KM5IeTcb6aHlLt88bP/kcPORDt8vKL3s0zJm8Xq6yifbWkC 12xkQEdgSiOvFbRlL+eo3NaXhRF6emEnY4MXxktL2Ojk9ueIQ2BXO/cfpU++sgOcZjyO 1emiL7IQEDPYbv9SRKy/nYfRvtmDqIEoPCi1Lb3eO/8dRVh52m/uxD5J7q6hxj97IZj0 f6/7HmF+Rxt3KRHquidkGJtIMvRTS4wyu9DOt9iBVvAk08fAr++EvhWOfPfEj9uq0vCl f5CovliOf+1vabpBcjv09G7xmAAeSCxST4wRbB+Nxl6Etdo01YN3TjgznRCs7lDjpZsg q+kQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=aNM5qFpuQrwUs+7E90Z8RXHKxODmGjt566L854L1cyY=; fh=NVYbldFMke6ibwmfa+JdvclOt8cJudJWpI+VHQjtTwA=; b=DZaQ5BJLLnnXJ7ibRZWd8Gq0lmBl68Fiaam85v4wFM1FO1B2tHP5kA7ff5aZG8C7YH GNCaM8TuquATSgXDRfAhdZRI73BrvOd9qiTHJzmf0GMjKchdz5FvCQILWdtwl+VuVNNz ezR8mhNIrQGdo2FdNJYR25qFWuzgh14RITmBuElXkS3kNUoP8E5fMuuyrgMcqq1Dw5Ai MlIguGLPDM1cZvNmyLkuQe6KX0+73SAIJ0FO5HitW8F6Bz+x8GIiLs20JEvCPqmOLWho 004RiIx6Qnv3J3SsfleU6qaAH2zoKEoUBkhA+0tA+T35Mhx0Pvqv0hyS2gfS7GnuM6tJ 5QTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=f8Xu1n9D; 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 Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id lf6-20020a170902fb4600b001b878f9e11csi4684807plb.54.2023.09.22.16.28.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 16:28:48 -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=f8Xu1n9D; 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 A7D5C80C1115; Fri, 22 Sep 2023 11:17:25 -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 S230237AbjIVSR1 (ORCPT + 99 others); Fri, 22 Sep 2023 14:17:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229932AbjIVSR0 (ORCPT ); Fri, 22 Sep 2023 14:17:26 -0400 Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 091BA9F for ; Fri, 22 Sep 2023 11:17:20 -0700 (PDT) Received: by mail-il1-x12d.google.com with SMTP id e9e14a558f8ab-34deefc2016so7730795ab.2 for ; Fri, 22 Sep 2023 11:17:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695406639; x=1696011439; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=aNM5qFpuQrwUs+7E90Z8RXHKxODmGjt566L854L1cyY=; b=f8Xu1n9DKITCfh6xg3x3f4i30pXwiYxdg5V1SqmRhHXkqaK4W0zxWCnAghfqflhoHG Z+1jFgtCux6dd7ZQXQ9a4/2cBoMRDaz3VM/f+k0+vT0z9BDzc5iMocOKrtwL5fbSqU5Y HSnVLE969EtaahtX6JZmClimmGMKAUAA+pMuKNbQqRmnJ9BrRuRRfdDK3NN2lRTMF/EF +aTNm+4jLL+W+h/X37X/v92/FzLjJItlbGPVHPCEGNw9uMHzgiBid8R4mgr9OAyk+B24 IW96TBrQ6WPPAuz9QX+MJnoGfpX2knshZDPazYwcXLBj+GugPN+OvwQlAQ0j+43SjCga aAmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695406639; x=1696011439; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aNM5qFpuQrwUs+7E90Z8RXHKxODmGjt566L854L1cyY=; b=PIEl4UzGuGbk/Rti8z1jgsBgHELUscpC6DarUY7BCkr0T6f9rtPwlhQqpAyTjbyXuP XQX/YpzFfv/wS1nvpfNUWd3PqCPNqqlFQIUG8lMFu0ifmwIefyDzUZEBqpbE7H12flDZ balv5KHlaoLCa7tsKYKlNSM+cJ1PXwPRL0kdTNBtEA8HVkWyLwhHmBqjU5chg1tq9dVq ReRmixLQ2jcpuftLGuDyHTiTboTi2C5+rhcpoCFKCgl0kN4CdsqQRI2p+HrSrfngRN+W +GniCvt+SamUR8i0+tnM6PNd28txW0HtecfD+Sh9YwoNXITSTSk0szr0Lv1x7lmD5We/ aeug== X-Gm-Message-State: AOJu0Yw0hDcLdckkQ8qbsSIma1nABCqf4Sx0TroWFEeIQ1bmRPeRIbmJ 4r90y9eC52BOEQZn8OQbnmkke1TeMkgZYJRwhgo= X-Received: by 2002:a05:6e02:1bcb:b0:351:1ac0:e1cf with SMTP id x11-20020a056e021bcb00b003511ac0e1cfmr391126ilv.4.1695406639285; Fri, 22 Sep 2023 11:17:19 -0700 (PDT) MIME-Version: 1.0 References: <20230922172211.1704917-1-cerasuolodomenico@gmail.com> In-Reply-To: <20230922172211.1704917-1-cerasuolodomenico@gmail.com> From: Nhat Pham Date: Fri, 22 Sep 2023 11:17:08 -0700 Message-ID: Subject: Re: [PATCH] mm: zswap: fix potential memory corruption on duplicate store To: Domenico Cerasuolo Cc: sjenning@redhat.com, ddstreet@ieee.org, vitaly.wool@konsulko.com, akpm@linux-foundation.org, hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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_BLOCKED,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]); Fri, 22 Sep 2023 11:17:25 -0700 (PDT) On Fri, Sep 22, 2023 at 10:22=E2=80=AFAM 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: Nhat Pham > --- > mm/zswap.c | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/mm/zswap.c b/mm/zswap.c > index 412b1409a0d7..9146f9f19061 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 t= o store > + * it, otherwise, if the store fails the old page won't be remove= d from > + * the tree, and it might be written back overriding the new data= . > + */ > + spin_lock(&tree->lock); > + dupentry =3D 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 b= ased on > -- > 2.34.1 >