From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 C04A83C1097 for ; Wed, 12 Aug 2026 20:56:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786568184; cv=none; b=gQvelvTMpu5hG0JwdrbHHQUG3ONFYRALkms1MrOKtUDYRfhtvN85f6N4DkUhBUVnEAPBwAANTHiYOTze9US+oEWXJc9pzSnmIDIYP0eHyoQXMq8zyZguquzQhhoYUjeA5hBXdPVbro/vsoXf7LB+yck/rROmaN/Q/HSICHgBePU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786568184; c=relaxed/simple; bh=TBfqvz+gLgt7CKNkQwWGEqRENB5+9LdJ3ewY2E5HALE=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=uFi5r0529wMgVzzEpZamgiCTebdqqP9m2eOGwdP1gsyxbXaK6Mmeyugi+/BrMNaq3kv0oABCWn9DQy4uvwFh0kBDHkg0t/JCkHy2YQls+vGM1X86wZp4J1qKX96ZFDgLFwF0Bpk1bNSkmoVqLtQkIDk1nQ03iTvfP97a9C7++BY= 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=sJuD0MeZ; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="sJuD0MeZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 539091F00A3A; Wed, 12 Aug 2026 20:56:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1786568161; bh=82pZ7FBofPwXZDC19M5abl9obtJ4k7a/DClzoby56us=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=sJuD0MeZV9Ay33iB8f9ooLxiB6V72jxWnkUS0PwKrbBOBARbX9W9XiG1cbk3b+v5N M+XaVJTq7xWV7b0Xj8lcCCbw1RIWJfUhr4U1unpASod0BvTiaUUin4n5bxCh1qSDEX m4zwXPqEOZjm0sH3N3aP9HcMdt6TwK0Wx40ySi+0= Date: Wed, 12 Aug 2026 13:56:00 -0700 From: Andrew Morton To: Youngjun Park Cc: Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Jianyue Wu , her0gyugyu@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 0/4] mm, swap: keep hibernation swap slots out of the swap cache Message-Id: <20260812135600.b486a8217c0bd1396f397693@linux-foundation.org> In-Reply-To: References: <20260811132209.2862708-1-youngjun.park@lge.com> <20260811114622.6a04927b0c7ca0c2d6b39cce@linux-foundation.org> 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 Wed, 12 Aug 2026 21:13:37 +0900 Youngjun Park wrote: > On Tue, Aug 11, 2026 at 11:46:22AM -0700, Andrew Morton wrote: > > When fixing things, please always take care to describe the > > userspace-visible runtime effects of the bug, particularly when > > proposing a -stable backport. > > Thank you for the advice and sorry for it. > I will make sure future patches describe the userspace-visible effects clearly! It's common, trust me ;) > > For [1/4] Gemini tells me "At a high level, this bug triggers silent > > memory corruption, process crashes, or data instability across > > completely unrelated userspace applications - typically occurring after a > > system resumes from hibernation (suspend-to-disk)." Which is what I > > figured too. > > That is right, with one small correction. this can happen with > uswsusp while the hibernation image is being created, not at resume > time. Memory corruption, process crashes, or data instability across > completely unrelated userspace applications can occur there. OK. > > Do we have any reports of this? Reported-by/Closes? > > I found this while working on giving hibernation slots their own > marker in the swap table, which I had discussed with Kairui. > (https://lore.kernel.org/linux-mm/abp7aDgYLrxF3Me8@KASONG-MC4/) > As far as I know there are no reports, so there is no Reported-by/Closes to > add. OK. > > I'd like to grab [1/4] only, and defer the other three until 7.3-rc1. > > This might be mistaken, but from a quick read, it's not clear what > > benefit those three patches offer our users. > > Simply put, they are close to cleanups that remove unneeded work. > (with a some little optimization.) > > Patch 2 keeps bad slots out of the swap cache, so readahead no > longer wastes a folio on them. > > Patch 3 keeps hibernation slots out of the swap cache, with the same > effect. no wasted folio at readahead time. > > Patch 4 removes dead code. So I'll retain [1/4] with the below changelog. Please plan on sending out the other patches after 7.3-rc1. From: Youngjun Park Subject: mm, swap: don't free a hibernation slot that is in the swap cache Date: Tue, 11 Aug 2026 22:22:06 +0900 A slot with a folio in the swap cache is freed when the folio leaves the cache, not when its count drops. swap_put_entries_cluster() follows that rule. swap_free_hibernation_slot() does not, it calls __swap_cluster_free_entries() whether or not a folio sits on the slot. Cluster readahead can put one there. It walks a raw page_cluster sized window of offsets around the faulting entry, and a hibernation slot passes __swap_cache_add_check() because it is not a folio and its count is not zero. Freeing the slot then clears the entry under that folio. The folio is now unreachable from the swap table, and the offset goes back to the allocator. The folio is still on the LRU though, so reclaim can pick it up later. It then takes the old offset out of folio->swap and overwrites the table entry there, which by then may belong to someone else. This bug can trigger silent memory corruption, process crashes, or data instability across completely unrelated userspace applications - typically occurring when uswsusp is preparing the hibernation image. I found this while working on giving hibernation slots their own marker in the swap table, which I had discussed with Kairui. (https://lore.kernel.org/linux-mm/abp7aDgYLrxF3Me8@KASONG-MC4/) As far as I know there are no reports, so there is no Reported-by/Closes to add. Check for a cached folio before freeing. The slot is then left in the ordinary state where only the swap cache holds it, and it is freed when the folio leaves the cache, either through the reclaim below or through normal reclaim later. Link: https://lore.kernel.org/20260811132209.2862708-2-youngjun.park@lge.com Fixes: 0d6af9bcf383 ("mm, swap: use the swap table to track the swap count") Signed-off-by: Youngjun Park Acked-by: Kairui Song Cc: Baoquan He Cc: Barry Song Cc: Chris Li Cc: Kemeng Shi Cc: Nhat Pham Cc: Signed-off-by: Andrew Morton --- mm/swapfile.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) --- a/mm/swapfile.c~mm-swap-dont-free-a-hibernation-slot-that-is-in-the-swap-cache +++ a/mm/swapfile.c @@ -2196,7 +2196,14 @@ void swap_free_hibernation_slot(swp_entr ci = swap_cluster_lock(si, offset); __swap_cluster_put_entry(ci, offset % SWAPFILE_CLUSTER); - __swap_cluster_free_entries(si, ci, offset % SWAPFILE_CLUSTER, 1); + /* + * A slot with a folio in the swap cache is freed when the folio + * leaves the cache, the same rule swap_put_entries_cluster() follows. + * Readahead can put a folio here, and freeing the slot now would + * leave that folio with no entry behind it. + */ + if (!swp_tb_is_folio(__swap_table_get(ci, offset % SWAPFILE_CLUSTER))) + __swap_cluster_free_entries(si, ci, offset % SWAPFILE_CLUSTER, 1); swap_cluster_unlock(ci); /* In theory readahead might add it to the swap cache by accident */ _