Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1462713rdb; Mon, 19 Feb 2024 17:31:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWUzAJsgNKt09XGj59BA1DN6H+N2aI43ZVPgX/uQ6U2/K44igxVQCYeL4PaJY1/0fhKRkskEVdSzItdFwyqmVDpJnPTGFKK7ykFirvaTg== X-Google-Smtp-Source: AGHT+IH8SuhH/9bAiHhTRq9iH5Y5gp1bYHcUV3oAXUreanU7AZKcKoor3Jll9XwFRNbzQhl70mpw X-Received: by 2002:a17:906:b0d1:b0:a3d:4ed8:f5bf with SMTP id bk17-20020a170906b0d100b00a3d4ed8f5bfmr10343954ejb.2.1708392718624; Mon, 19 Feb 2024 17:31:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708392718; cv=pass; d=google.com; s=arc-20160816; b=QrcQAH3NdP1itdxbpcLYIVSWWEMIeToPntjz2fTaLVxCnXmK/9KU/YMq//X0kxXBZk +QItDj4m05X406r3J4+cVSTgHU+0HeTzN0n9eNPM8nUAZ4ITjls49Ny2c+1GY7GMjn5V xF/mghU1GzRPQ1seT3jB4HfLrG7DwOYcPv9Ujx3OFq5MBLFTRJhol8SR8jq1Q1LMIVQH FjJJCWg3G6D503C0M/c8KFO2meFbFfRqDU/aW85FlSIwJKTAV5Dx2HfomRTWT/s+yip8 kc/OQtXDxnfRcNsLjQK7+1exb1YhPJCd8k4Bgbvsv6ItZ0xwqujwaF4BH85ZW7Eufj53 DIqw== 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:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=quY5NXL+1QgMY/t9SiQomgeHbAdQhPH2ixy0YuC6J5o=; fh=c6ykRr/2GDVKhJ8pa6QIq2V1WRNUZozmu+a3NiYh1CE=; b=1BpNXmPulNwh8493hHUrJckQPx2ERq3fcs1pvzwGd2ZbVxI7YbXrV3kK2AAceSmxBY keoSHcQD86wGBbRwKWbkg0Fde+gL2SLXm64MAdSfvwfltaIYNbKl4dAg11nQrWL4VdIM eei0qVl9krQu5laDWU9fETUM+INZMJaJcWgoMQVvSsv5zAOOrEH82YL/vPGlBmrhV9/G D/dv32j1WgnTNVF2wqu8w5noEEmAsJbYuIqt9bP6yYoTMJFZzJsWhTLll0eWlk0ClqRa ddWwv1qq+n7y3XzgTltADepgnZKG7XDYcqotbHMdf/Phv6HcIlxu+W9pG9sKwi4DvxTj DFxw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=udjUBuNY; arc=pass (i=1 dkim=pass dkdomain=linux-foundation.org); spf=pass (google.com: domain of linux-kernel+bounces-72183-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72183-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id w8-20020a170906480800b00a3e93bc8b4csi1371513ejq.473.2024.02.19.17.31.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 17:31:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72183-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=udjUBuNY; arc=pass (i=1 dkim=pass dkdomain=linux-foundation.org); spf=pass (google.com: domain of linux-kernel+bounces-72183-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72183-linux.lists.archive=gmail.com@vger.kernel.org" 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 597481F21E45 for ; Tue, 20 Feb 2024 01:31:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 693F538DCC; Tue, 20 Feb 2024 01:31:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="udjUBuNY" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8438C2C18E; Tue, 20 Feb 2024 01:31:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708392708; cv=none; b=HHlUrnh8E1JAtN2eSSPxsaLtMsPPFmI1W3AsSX6pfe2mAnPABA5nUIua8b8TZsUZHdCN1I/2ZlE1YW3Ct8fFqWSlJ+7F1nOhI3EAZKFJi40EFPKhpqol826asQHe2cimKa+EsbLJs77TZKqSScBygqR5euw9BxZ3j5A3gz1FNc4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708392708; c=relaxed/simple; bh=tpxsdC5YcAUY6lkZarOTnYclCEbb54OfNodgJZlo95Q=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=fi64qPOZbxwrd+2XXJ2gmxUbt3Wn7r4JOH/bWnMxJQiIr1mzh7M0UYnrV17yoxM5YT4Qs34Zn3mGzgVoBIE/N3JjhcFPGnSRP9os9vi10n/l2endLSkgKM5qB/H8XyElisVkn5PQVDRuEDCpk7/11YoXi7VJcNFUwhOUBWFCeDU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=udjUBuNY; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98352C433C7; Tue, 20 Feb 2024 01:31:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1708392708; bh=tpxsdC5YcAUY6lkZarOTnYclCEbb54OfNodgJZlo95Q=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=udjUBuNY3u4ggtpqTZdfZV2NKWbZ90+fhEhk7JDSiCyqqNGW4cwhpxQplUgvcZgog gKGRXmrGeyT1aDDEqwmQvl+CA8j7OnpCwrZCj/Xgw4BW1/vTc6H1US1P8FIsEjmnol BEWQh5IuFepzHNtpZSHCuoLimmHcMe2kfB2a7USo= Date: Mon, 19 Feb 2024 17:31:47 -0800 From: Andrew Morton To: Kairui Song Cc: Kairui Song , linux-mm@kvack.org, "Huang, Ying" , Chris Li , Minchan Kim , Barry Song , Yu Zhao , SeongJae Park , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Yosry Ahmed , Aaron Lu , stable@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4] mm/swap: fix race when skipping swapcache Message-Id: <20240219173147.3f4b50b7c9ae554008f50b66@linux-foundation.org> In-Reply-To: <20240219082040.7495-1-ryncsn@gmail.com> References: <20240219082040.7495-1-ryncsn@gmail.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Mon, 19 Feb 2024 16:20:40 +0800 Kairui Song wrote: > From: Kairui Song > > When skipping swapcache for SWP_SYNCHRONOUS_IO, if two or more threads > swapin the same entry at the same time, they get different pages (A, B). > Before one thread (T0) finishes the swapin and installs page (A) > to the PTE, another thread (T1) could finish swapin of page (B), > swap_free the entry, then swap out the possibly modified page > reusing the same entry. It breaks the pte_same check in (T0) because > PTE value is unchanged, causing ABA problem. Thread (T0) will > install a stalled page (A) into the PTE and cause data corruption. > > @@ -3867,6 +3868,20 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > if (!folio) { > if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && > __swap_count(entry) == 1) { > + /* > + * Prevent parallel swapin from proceeding with > + * the cache flag. Otherwise, another thread may > + * finish swapin first, free the entry, and swapout > + * reusing the same entry. It's undetectable as > + * pte_same() returns true due to entry reuse. > + */ > + if (swapcache_prepare(entry)) { > + /* Relax a bit to prevent rapid repeated page faults */ > + schedule_timeout_uninterruptible(1); Well this is unpleasant. How often can we expect this to occur? > + goto out; > + } > + need_clear_cache = true; > + > /* skip swapcache */ > folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, > vma, vmf->address, false);