Received: by 2002:a05:7412:1e0b:b0:fc:a2b0:25d7 with SMTP id kr11csp708040rdb; Thu, 15 Feb 2024 12:55:50 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX+zY0tUIz7RRJCGdt8mv+1kjcDLBL1aF7g1eJG2SfqFWtvOihn7r1NyrDfTulg/iiRPT8su2R5HN9WWOL2lxtWNzGjVeb2484LfZVyWQ== X-Google-Smtp-Source: AGHT+IGm3Jk5c0xQkBvYxr//ZMYT3r0Sa04sTsdb+cffnyG1/RDel3NjRzj2NBNcAS2BpSnkbTb1 X-Received: by 2002:ac8:7f05:0:b0:42c:7aef:615c with SMTP id f5-20020ac87f05000000b0042c7aef615cmr3845444qtk.21.1708030550557; Thu, 15 Feb 2024 12:55:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708030550; cv=pass; d=google.com; s=arc-20160816; b=IGTAjF5lL/tfg09yD1WOJp9EGU+SkzXfiEGeN7VC+uvAYFJvGxgP8HfOZF/LRUmKQr cuuofmwqLYFAz0iYnrk+yBnHCIeM3coS0sRnQBWHe2DLQ4yd5zxRtPUWf4HWy95vBQWH PbUgNl++9zesT15mxXQc8LEPYbxjJ2vS0v//6Wdo8MIuQmhJhIcNEvVC9sN0jkJ7q3cE WA7T1GarAaAQlMAEdVbedwUwXqwWfiH2zJ1wlGijNuhAujDaaOgQq7BjgfJ8+VSzqt+U Vp6h2ML42Gsq7rfUt60xEWz+cdKHba6sTUMMTehCb8rIpIRXG6qwYX3UTRUkgGBQ5d35 ZJeg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:sender:dkim-signature; bh=r7KeUmQ5rtQIKjg5bbnS3xkReICqYXg+NK0gShllzn0=; fh=IohQuBK+Bm+gGsleUoy4rjgUwTkAS9Wb0NpqALjKQLk=; b=qQ7sTWax0nt6M4fvNTzI8qgUZkrZpSU9dLWD5cm1MNmiC/FIcvmKac502zoGz3QJyd 8vlhVSdhqjH4JdVakWKuD5CrKZ0fzIUT+eelI2baKl/wrx6HS2awDXTJzd+ZFajEQpVM ob+qZfin+BDNSyoF7G6wTeUyfTIo7RKYMZCKOheh3xSLDTaOesiagbB4tT2rQ4aZz4Cz Olie2KMToY3Iab/tGrlEK5uxmQ//86qnB8wcQ5GoMMTyR3Q3zzqCu0Xq5CPJQGDKmONy xczX+m/PUJ/DgARpSo4q/vkpCQpWk9VGWl9UcF7XZvCxqZNit1+QmDxUD+5tyOHzJTye 2mbQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XTWdWTFO; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-67673-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67673-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id e9-20020ac85989000000b0042dce156773si2285873qte.553.2024.02.15.12.55.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 12:55:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67673-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XTWdWTFO; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-67673-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67673-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E1F3E1C23D91 for ; Thu, 15 Feb 2024 20:55:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 820A713B2BE; Thu, 15 Feb 2024 20:55:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XTWdWTFO" Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.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 24EB26A349; Thu, 15 Feb 2024 20:55:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708030516; cv=none; b=KM3svwPKGBaIMeMnJBqlL3PXknvT+C1OEWGxX3hwbnC/9Zc3hfK5e6GJMTVS6csRM7QrEZSvL6NES8mKS3l9hdfHOUITGHE0zGagWBrddu4yDrNu6Pdtmfw8vbUzWxtIIZSeRFYhZWwvTq0EofFTWULNyH7veRxaut8OYH76hho= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708030516; c=relaxed/simple; bh=+BMQn3bOtWH+FWUM64l+j3RtIw016nUn+Jnnh0s+X2c=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Kn5N8qmlAAxRZhIhoiJYPfJS2Jd4OmSPffCJ5GDw1XGFjibs3FKOm/q5y5VXdn1fJqoLAKutgIt0NlYvaUejuUkj6tS8VFaYHCSdyd8q+p/opSDVJdMi/5LZTHpPuu1TQWoyd4lXha6nziNWT9ztaGn89oGFAIXO3xQ6KvUsm0A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XTWdWTFO; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1d7232dcb3eso10822795ad.2; Thu, 15 Feb 2024 12:55:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708030514; x=1708635314; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:from:to:cc:subject:date:message-id :reply-to; bh=r7KeUmQ5rtQIKjg5bbnS3xkReICqYXg+NK0gShllzn0=; b=XTWdWTFO41LkP3ajBo/IfnkDBaOM5TO5ob3AWl+3iQV4d5UYxfDsbgb0k+KDwBSM7l CxrJUFNFm9LOs1ls15BwoQdwRPRBOq4FVf+sc+Tp2+DpbCGWMOB+JiVpj0aFv51oFEyW QvFo/JS+azW0zR/IDebriZJt3WbdkvLnXHpbM4tgFCuEVaywTez9hTtHYZEu7XRh6M9A JhKo/W04HXExW1pTVxZ6pHFOVmzB45sVuefClgs+4A6CxtSOBVIUtgPKXmNjXum/sbS2 e+fu5sTzQ/JpTWdg4L61AaYV86MLiqEWGnfEsLG0UkUAM7lABG0C6djWjngrpdQ6kzMR 7lcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708030514; x=1708635314; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=r7KeUmQ5rtQIKjg5bbnS3xkReICqYXg+NK0gShllzn0=; b=ex6hmhtN9VItBU7/i/z2gOxbJaOnKXX9pOZjAl4xkhr7+uzRy8zKPKtPgszDHG8Hl7 +kyKvGOlZKMAFf00bbvHYduGl4ZjbM2X+7wp+9wNEcHtrxX+f9bJBO1pLIQCjCryb7gt F8CYp8yk8DSGBdj5q8dRUHeWag8VQo4anscYNcTBx1JdDrolLBe4BTmTeeCLHS9Jw+Nb ciT2feyuVOQVLlmRKJmpgz+EXdcdCqJ/Ng9jtj8RYmEID4GFXOz6ryVss/x+x0H8lSm4 e+KuONewq8JY/iKdv+KQXlS7QjOhtOshrmVFxnF9GzOofrgzBHB6MC46MER9jofaQSgQ 1fzQ== X-Forwarded-Encrypted: i=1; AJvYcCWLtgHCAFKsMercILbrOjNCoOLRLKHO0dKVW3ck6erSNf4+XycaPSt5ReYD8Ns3Lzd8UR7GQbyTr/9P3IFHM1LcsuOzFRmTKPDcLwQRPfFonykvkd0NsVY2pf2E1E+r9BoSiYCL X-Gm-Message-State: AOJu0YwK6oXof1NNG7Nk6WZig3pCXParU237DjeGqvcM8gGLKuP0ExK4 0ZHOCTEcvJWNsKLBqeJZKXUPtEAZ6HrBOmJUSSpVwOVzv8VOVYnY X-Received: by 2002:a17:902:7c01:b0:1d9:f495:cfa4 with SMTP id x1-20020a1709027c0100b001d9f495cfa4mr2658373pll.62.1708030514195; Thu, 15 Feb 2024 12:55:14 -0800 (PST) Received: from google.com ([2620:0:1000:8411:9dc5:487d:7c08:30bb]) by smtp.gmail.com with ESMTPSA id x9-20020a170902e04900b001db561fa164sm1671380plx.25.2024.02.15.12.55.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 12:55:13 -0800 (PST) Sender: Minchan Kim Date: Thu, 15 Feb 2024 12:55:11 -0800 From: Minchan Kim To: David Hildenbrand Cc: Kairui Song , linux-mm@kvack.org, Andrew Morton , "Huang, Ying" , Chris Li , Yu Zhao , Barry Song , SeongJae Park , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Yosry Ahmed , stable@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] mm/swap: fix race when skipping swapcache Message-ID: References: <20240206182559.32264-1-ryncsn@gmail.com> <1d259a51-46e6-4d3b-9455-38dbcc17b168@redhat.com> <4c651673-132f-4cd8-997e-175f586fd2e6@redhat.com> 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-Disposition: inline In-Reply-To: <4c651673-132f-4cd8-997e-175f586fd2e6@redhat.com> Hi David, On Thu, Feb 15, 2024 at 09:03:28PM +0100, David Hildenbrand wrote: < snip > > > > > > > We would detect later, that the PTE changed, but we would temporarily > > > mess with that swap slot that we might no longer "own". > > > > > > I was thinking about alternatives, it's tricky because of the concurrent > > > MADV_DONTNEED possibility. Something with another fake-swap entry type > > > (similar to migration entries) might work, but would require more changes. > > > > Yeah, in the long term I also think more work is needed for the swap subsystem. > > > > In my opinion, for this particular issue, or, for cache bypassed > > swapin, a new swap map value similar to SWAP_MAP_BAD/SWAP_MAP_SHMEM > > might be needed, that may even help to simplify the swap count release > > routine for cache bypassed swapin, and improve the performance. > > The question is if we really want to track that in the swapcache and not > rather in the page table. > > Imagine the following: > > (1) allocate the folio and lock it (we do that already) > > (2) take the page table lock. If the PTE is still the same, insert a new > "swapin_in_process" fake swp entry that references the locked folio. > > (3) read the folio from swap. This will unlock the folio IIUC. (we do that > already) > > (4) relock the folio. (we do that already, might not want to fail) > > (4) take the PTE lock. If the PTE did not change, turn it into a present PTE > entry. Otherwise, cleanup. > > > Any concurrent swap-in users would spot the new "swapin_in_process" fake swp > entry and wait for the page lock (just like we do with migration entries). > > Zap code would mostly only clear the "swapin_in_process" fake swp entry and > leave the cleanup to (4) above. Fortunately, concurrent fork() is impossible > as that cannot race with page faults. > > There might be one minor thing to optimize with the folio lock above. But in > essence, it would work just like migration entries, just that they are > installed only while we actually do read the content from disk etc. That's a great idea. I was thinking to have the synchronization in the page table but couldn't reach to the other non_swap_entry idea. Only concern of the approach is that it would be harder to have the fix in the stable tree. If there isn't strong objection, I prefer the Kairui's orginal solution(with some tweak of scheduler if it's necessary) first and then pursue your idea on latest tree.