Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2789827rdb; Mon, 5 Feb 2024 19:13:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IEE/mTTQCRt7I0Ur+WdBBaVRh4o7mVRMSPoG9vijD3K2ykUvbGYDYNMYyF20k5LgCBl0IDm X-Received: by 2002:a17:906:27d3:b0:a36:8431:6edf with SMTP id k19-20020a17090627d300b00a3684316edfmr724152ejc.37.1707189211743; Mon, 05 Feb 2024 19:13:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707189211; cv=pass; d=google.com; s=arc-20160816; b=pE47rwN5MAsqr28nFKqq7yQX+inIb6vLYJXIV2ySHTHECiZwLGBl4RgAzYaAmE6mt+ RT5/Yw3J0yJV37kqH2TTyudLX/9fIttSpSMIm5Vvg9VARBsyTqujqG+0bmEsGOPuvpOX CzxOl/5y8vfKml9pU1SfCjmz/cxekDoHkyO4G6c98WE5pUQR4V6c+MWzyjViRPGFgiJx nixuRlPjOElXwmbTfcC49HjmP87orurVAHO+GGNwiP5P2Jh5ollsTlmyBUH++SL9/W/j mhX0P7t8dtH2C90DV277vd1B+RMd4R/vBIP/Jg6nNnDg/tm0k7OW7f2QKahe6ar5y0Ul TlEw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=9l3WILuSJF+kJd5Jl6uK0KvdfHoanQQl0YMdjWYvA3Q=; fh=ATZOJCfCqiZtiP6X53P8nL0CDgAE6nx4pCg/O1o9NHU=; b=LAHsy8Tt8THRV/fzRYIW2X1sbvKvCzeKe1KB4fYHym9bOx9EOFw1lV7PZhKtQqPyd9 mSuG82KnOK/Tx9+XHb83ayWQVEWuYgSFY44kQsKcz9mi3NeX1USRiWZ7Pln5tYCeOxFA q7P/s9mWtULNsZdeYQcpPXuKxtSedXDEDjtHxyTJgYpP4RFhN5L0GPwDYk0nAzveEGOY UnZgl9i9rt6xpldjUQuTeSQ2QBAgu41SjKbwW7wBE7lfzZeB7n/FkBds7VMbo4ow9JR0 VkQLjcpprgVZF+P/4uVB8YBewhjaeQGr4Qnmj7koaN21P4nFPr26o8UpTevXTYrU1QCz 9h3A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=dK1Xig5O; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-54242-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54242-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com X-Forwarded-Encrypted: i=1; AJvYcCU03iaZoiTdK7XPdAppBKLNEPJkRLArUO76p5HHxwEv9cOWTDnbp1XkRy+pOy1M3JHkhY98PrxqQLUrkVb9zu7Cn/YQPTmxFgnpNEBx9Q== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a24-20020a170906469800b00a37c745e7besi586931ejr.150.2024.02.05.19.13.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 19:13:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-54242-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=@gmail.com header.s=20230601 header.b=dK1Xig5O; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-54242-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-54242-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 7E1621F23EC3 for ; Tue, 6 Feb 2024 03:13:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6CAA273197; Tue, 6 Feb 2024 03:13:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dK1Xig5O" Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC97233CC8 for ; Tue, 6 Feb 2024 03:13:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707189203; cv=none; b=maYjEA64crz+XzFLrWHmOzBSZ3YAJ0Dqm74gGdS2VplRSd1gykFQ7q7lP24NCQ9x6yU4yZGljIIqGQPub2hllDgRNaXtXrDSzOSo6CzqG8XcUBpsjOEnVgQCu792bKKmzXEZn3ZF40N1KOD28tkVG9oPPAIhfXGqOa417wdv3Uc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707189203; c=relaxed/simple; bh=Wi6FX0RldvPV3w1VEeuYS/+ytY/jHtcKSnNphtqVodY=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=FkkvY+5m52P8R2xvmDTrfxwAtmn+dyvb3JUQ+d3J5dcg0NkiI06XZwukqvuksu7ZtcyFD2KgglHV5hhJ5IWjaUw0Eg+lK2Nor82RpcZndkaA8qSo5jBRj7McjRA4egOVHweUOHw+l02M/H/IVchNPy7i/yAsVlTbNztqqPMVNrI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dK1Xig5O; arc=none smtp.client-ip=209.85.208.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lj1-f181.google.com with SMTP id 38308e7fff4ca-2d0a6b9c9bcso24899091fa.1 for ; Mon, 05 Feb 2024 19:13:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707189200; x=1707794000; 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=9l3WILuSJF+kJd5Jl6uK0KvdfHoanQQl0YMdjWYvA3Q=; b=dK1Xig5OSj/xzMIGJiJ6vYrE6TGhGkykcYOsxl2dM54XsKI9ky3sMKrkTHCokXEGpR IGJ1T276QKzcBkSXJyAME/IqASzeIufh15fX6vRXgP5553izUqKf4JsVdvJGUzB5YNrF RFsewrCQibigEL8+6Mlz1TKapHxNHyaSMS3j4BQ6YmZoBsfTnhgcgmrf8117pmpKMfWR cObwmsgihl25KAzqST1qgP5KWCi36DuGKlLTexxmuAfWlqmV623ZPJ0YlIH8C4abxJtC WufQrzeaK+kDdMvQtd37zIUWcpSS8XCizkFlycDUc5aEDQcDRtbkGCI98nbj2n7x3Ier WAGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707189200; x=1707794000; 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=9l3WILuSJF+kJd5Jl6uK0KvdfHoanQQl0YMdjWYvA3Q=; b=DZHq46CDYnndAZz/4+WqkpTO5ZcxWjAdJ4uSe3C2lQOamANUV41JuGZ/nIBGrJeqxB h3t6hL/pDUgyd9QOtpwO02OoOihLIgzR8W0qP6MYp2qwQyV3W0Zx5urDTDuo6fye/V2Y RjJ/t0M0bB1sHLTDhuVLLkpvgDXTrAeNi23+RRXMVEgSWdHQsCh4nU4flAmRqA53XDnk iRHfLImEwY+NpL+I69LvdctGmqZsvzOB1HZ3AwRruttV2J06FfLOUZn9NzZAJS1HlKLC pZhjbIMsLEitwmH/BFmEA8MsMLMeBTKxEgOU4COIjovC6hasLAoUA4G20daoX/Zp2oCc g2Qg== X-Gm-Message-State: AOJu0Yx+rH1/LPF6RtFZLMwhSi5Ry35zRbFgmNZ+A0O8XFZslmlOD3AC 3rHPutpGBStDWk+HGAZIwttQGjnJ32Wi8uc6NNR2F29LZjTHwjWoMCFdbLwiEelUyg4DsTe4au/ lkuqI1nw8EiJYepynkh+63T5vvs8= X-Received: by 2002:a05:651c:1059:b0:2d0:b2ce:3956 with SMTP id x25-20020a05651c105900b002d0b2ce3956mr930605ljm.32.1707189199603; Mon, 05 Feb 2024 19:13:19 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240205110959.4021-1-ryncsn@gmail.com> <20240206022409.202536-1-sj@kernel.org> In-Reply-To: <20240206022409.202536-1-sj@kernel.org> From: Kairui Song Date: Tue, 6 Feb 2024 11:13:01 +0800 Message-ID: Subject: Re: [PATCH] mm/swap: fix race condition in direct swapin path To: SeongJae Park Cc: linux-mm@kvack.org, Andrew Morton , "Huang, Ying" , Chris Li , Minchan Kim , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Yosry Ahmed , David Hildenbrand , Yu Zhao , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Feb 6, 2024 at 10:24=E2=80=AFAM SeongJae Park wrote= : > > Hi Kairui, > > On Mon, 5 Feb 2024 19:09:59 +0800 Kairui Song wrote: > > [...] > > mm/memory.c | 19 +++++++++++++++++++ > > mm/swap.h | 5 +++++ > > mm/swapfile.c | 16 ++++++++++++++++ > > 3 files changed, 40 insertions(+) > > > > diff --git a/mm/memory.c b/mm/memory.c > > index 7e1f4849463a..fd7c55a292f1 100644 > > --- a/mm/memory.c > > +++ b/mm/memory.c > > @@ -3867,6 +3867,20 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > > if (!folio) { > > if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && > > __swap_count(entry) =3D=3D 1) { > > + /* > > + * With swap count =3D=3D 1, after we read the en= try, > > + * other threads could finish swapin first, free > > + * the entry, then swapout the modified page usin= g > > + * the same entry. Now the content we just read i= s > > + * stalled, and it's undetectable as pte_same() > > + * returns true due to entry reuse. > > + * > > + * So pin the swap entry using the cache flag eve= n > > + * cache is not used. > > + */ > > + if (swapcache_prepare(entry)) > > + goto out; > > + > > I'm getting below build error after this patch. I guess maybe the code n= eed to > take care of CONFIG_SWAP unset case? > > .../mm/memory.c: In function 'do_swap_page': > .../mm/memory.c:4004:8: error: implicit declaration of function 'swap= cache_prepare'; did you mean 'swapcache_clear'? [-Werror=3Dimplicit-functio= n-declaration] > 4004 | if (swapcache_prepare(entry)) > | ^~~~~~~~~~~~~~~~~ > | swapcache_clear > > Ah, right. Thanks for the feedback. For the CONFIG_SWAP unset case I added an empty function for swapcache_clear, but the original swapcache_prepare also needs an empty placeholder now. Will add that in V2.