From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 B20542DC322 for ; Fri, 30 Jan 2026 16:48:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769791697; cv=none; b=tOV/FUuPcXKrVvnPPvZFPdy8/Hx6Le2BoLkkfnx+cL8V8xEp3VbxSBL+a4qPA1KQS5/jxjx9NRTvp6qy7ZnPZYir9E0sQPUvVdnxIHvyDtR6XqJf7ZgdAX8ZjvG1CgxkAay8S/upIx/LWNuZrhEOYPUJD29b0HCWyHyv3W7sRCI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769791697; c=relaxed/simple; bh=8TcyAVAzuazTCLZLMOXh3if7mTGu0BMjq8uuxiKKG+s=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=qDjg7kj1vVzhTWiE+z2q5C4f7EaaVoGUCCZNw8WVLN2pnHrY3zIYmwiJWncsfZ0dIZJRk7MMVDjfzDoGUQ0dGjHLth4fwV/AV9c3f2Xp1N1Ncu1qS0iJBpJpDH8uKJ9DZ6hfLnuZrtqwlNFVlsPnT8vC0Wg5XFD/yJ1Y8yObMpk= 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=enma0iK4; arc=none smtp.client-ip=209.85.216.53 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="enma0iK4" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-34abc7da414so1443059a91.0 for ; Fri, 30 Jan 2026 08:48:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769791696; x=1770396496; darn=vger.kernel.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=d0bsS3V6tz831x+5BztayPCxzrLGcuyzVdHvWPehnBU=; b=enma0iK4uHt+tEIzZxr4PmsZuvwFTlqL/8aB9yHNT2jM7dWl2r/GMhF0t+wZYDC3Xq LzebU6fKbQU/MYkGlm2UtE08PamLUJe92pQoDvApMbxYIW3SzJBrqbnOuarP+5znJhXz Dg29ybzqVIFTD4hsrazEHNYR9byFgReD2SvJqdtmeZhaWee7soDkMOzN6ey9a3pWa1wb 1powYclP6PzMorCMXPHavYLs2JbPfm5v1Vdb/iYLEGD4Od1Bhr5L7qPhMrKRvreetre8 LFvniiD19lr3VCuLAVxNIrMNiZSz8ugWMeMtsNaMJT8Oiop7cZpIsbAAh1Lpjt9b9QTt peQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769791696; x=1770396496; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d0bsS3V6tz831x+5BztayPCxzrLGcuyzVdHvWPehnBU=; b=LyQhnSJJk0aJG3G6QJHvpzfQODd8hoFIIuFzvIB4s9DdZ4Pta3Mzfl/0GGwTOxW+sw nw7I3knUitFXBDycrWKQlGWmr7Umy1xh9YW7arM64I4l6npsiyaUSoaHfYwYgS4/NGHd 5WuJIRX+T8vsFrz0bmnGQ5uAKtpsqgfngttOARbZd5NDb7KA1Szn7eBXSNvLjEwf1LC3 545mr/IybqvcJnwGzljkcbNKLM0aWBflhM4njcc1v9GWqh9qETXxpFMe+MOKpwojuSMu dXq7TwYnS3sywNDXCsaZiIHe+hXO4Rz/2JRdtOhhSiU7ohF6Gt1mP8ubeIXuzmT5DmqK k33w== X-Forwarded-Encrypted: i=1; AJvYcCX0Af51w98KLot1qf/QSYN+0bZItGQV6vOw40Z1AaNhPmB8Fzb+ngqlbqUQNB076jv9d/hQ6+ghjzd7rCk=@vger.kernel.org X-Gm-Message-State: AOJu0Yy4BWyJi4cMFFPjnRick2s5GETl4XPcqo02cQ6jaeqM3s1qklF0 w+ifA74l7u/RRNPrP4eWLVA3kXu7c6IMH9Apm32EZSEOG464RacLGlex X-Gm-Gg: AZuq6aJJFoT2gQzu8qrkGYzyW0NHKEYym3qMQHXDeJTjyuPamOTHSFx1adQQWkfjSwh BFCgER3/4UapZ66As1w6ygZtwgKKU9UFpIs3ssDE7CDjDpXWyyUGEYBl7LRQbhukV23k4xSP1sr 0P1+6VZX7POwJ1KhxZdHHFByHwXlQEXz6AUiwlSk4W+5HpNYgZ10oILMcqWV20stO+251TCMn5C G4N7gU+ospnyZgkwlRxgr0GEaY4D7gPd8QWfhzBX9wQnMO++O4fmpd5efPMXI/LlyRb3EKXV5lT LIQcrgz/oDbFCpouFak+iPw/GRwLZr/6Y44vBhe4IdvoQVkt97Ry4zHCiEMKY/u7v3jhtm1QsSd 3w7Zjmi/zthxyKMCxEyDluDYdOvpvMRllI/W+mGA1u4d53kNOGF9UEywiFeYkJeE8/eV19+QgS4 IbAUhPPW5Gl/2fg/797754dKXSZmk/29nikdqUkDLME43Kx0s= X-Received: by 2002:a17:90b:1348:b0:34c:2db6:57f1 with SMTP id 98e67ed59e1d1-3543b3d1227mr3086067a91.30.1769791695949; Fri, 30 Jan 2026 08:48:15 -0800 (PST) Received: from KASONG-MC4 ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3543c1b0c72sm1048655a91.6.2026.01.30.08.48.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jan 2026 08:48:15 -0800 (PST) Date: Sat, 31 Jan 2026 00:48:07 +0800 From: Kairui Song To: Chris Mason , Andrew Morton Cc: linux-mm@kvack.org, Baoquan He , Barry Song , Chris Li , Nhat Pham , Yosry Ahmed , David Hildenbrand , Johannes Weiner , Youngjun Park , Hugh Dickins , Baolin Wang , Ying Huang , Kemeng Shi , Lorenzo Stoakes , "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, Kairui Song , linux-pm@vger.kernel.org, "Rafael J. Wysocki (Intel)" Subject: Re: [PATCH v5 14/19] mm, swap: cleanup swap entry management workflow Message-ID: References: <20251220-swap-table-p2-v5-0-8862a265a033@tencent.com> <20251220-swap-table-p2-v5-14-8862a265a033@tencent.com> <20260129200604.2518109-1-clm@meta.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: <20260129200604.2518109-1-clm@meta.com> On Thu, Jan 29, 2026 at 11:32:38AM +0800, Chris Mason wrote: > Kairui Song wrote: > > From: Kairui Song > > > > The current swap entry allocation/freeing workflow has never had a clear > > definition. This makes it hard to debug or add new optimizations. > > > > This commit introduces a proper definition of how swap entries would be > > allocated and freed. Now, most operations are folio based, so they will > > never exceed one swap cluster, and we now have a cleaner border between > > swap and the rest of mm, making it much easier to follow and debug, > > especially with new added sanity checks. Also making more optimization > > possible. > > > > Swap entry will be mostly allocated and free with a folio bound. > > The folio lock will be useful for resolving many swap ralated races. > > > > Now swap allocation (except hibernation) always starts with a folio in > > the swap cache, and gets duped/freed protected by the folio lock: > > [ ... ] > > Hi everyone, > > Apologies for looping back around on this one, but I've got some prompt > changes that break down larger commits into sections and run them > independently through review. I reran it on mm-new, and it found something > new: > > > diff --git a/mm/swapfile.c b/mm/swapfile.c > > index 64970ee11fcf7..85bf4f7d9ae7b 100644 > > --- a/mm/swapfile.c > > +++ b/mm/swapfile.c > > [ ... ] > > > @@ -2195,7 +2265,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, > > /* > > * Some architectures may have to restore extra metadata to the page > > * when reading from swap. This metadata may be indexed by swap entry > > - * so this must be called before swap_free(). > > + * so this must be called before folio_put_swap(). > > */ > > arch_swap_restore(folio_swap(entry, folio), folio); > > > > @@ -2236,7 +2306,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, > > new_pte = pte_mkuffd_wp(new_pte); > > setpte: > > set_pte_at(vma->vm_mm, addr, pte, new_pte); > > - swap_free(entry); > > + folio_put_swap(folio, page); > > Does this corrupt the swap map when ksm_might_need_to_copy() returns a > new folio? In that case, folio != swapcache, and the new folio is NOT in > the swap cache with folio->swap = 0. This would trigger > VM_WARN_ON_FOLIO(!folio_test_swapcache(folio), folio) and call > swap_entries_put_map() with entry.val = 0. > > Compare with do_swap_page() which correctly uses folio_put_swap(swapcache, > NULL) when folio != swapcache. Should this use the original entry parameter > or the swapcache folio instead? Thanks again for running the AI review. And it's really helpful. This is a valid case, I missed the KSM copy pages for swapoff indeed. We do need the following change squashed as you suggested. Hi Andrew, can you help squash add following fix? I just ran more stress tests with KSM and racing swapoff, and everything is looking good now. diff --git a/mm/swapfile.c b/mm/swapfile.c index 8c0f31363c1f..d652486898de 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2305,7 +2305,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, new_pte = pte_mkuffd_wp(new_pte); setpte: set_pte_at(vma->vm_mm, addr, pte, new_pte); - folio_put_swap(folio, page); + folio_put_swap(swapcache, folio_file_page(swapcache, swp_offset(entry))); out: if (pte) pte_unmap_unlock(pte, ptl);