* [PATCH] mm, swap: revalidate populated clusters before scanning
@ 2026-10-19 16:06 Nhat Pham
2026-10-19 16:16 ` Nhat Pham
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Nhat Pham @ 2026-10-19 16:06 UTC (permalink / raw)
To: Andrew Morton
Cc: Chris Li, Kairui Song, Kemeng Shi, Baoquan He, Barry Song,
Youngjun Park, linux-mm, linux-kernel, stable
swap_cluster_populate() can publish a cluster's table with the cluster
lock dropped. A cached allocator can use and empty it before the
populating allocator reacquires the lock. The resumed scan can then
dereference a NULL table.
For example, CPU1 can retain a cached cluster offset on an SSD with
per-page discard disabled:
CPU0 CPU1
---- ----
swap_cluster_populate()
swap_cluster_alloc_table() fails
spin_unlock(&ci->lock)
local_unlock(&percpu_swap_cluster.lock)
swap_cluster_alloc_table()
rcu_assign_pointer(ci->table, table)
swap_alloc_fast()
local_unlock(&percpu_swap_cluster.lock)
mem_cgroup_try_charge_swap() fails
swap_cache_del_folio()
__swap_cluster_free_entries()
free_cluster()
__free_cluster()
swap_cluster_free_table()
rcu_assign_pointer(..., NULL)
local_lock(&percpu_swap_cluster.lock)
spin_lock(&ci->lock)
return ci
alloc_swap_scan_cluster()
cluster_scan_range()
__swap_table_get()
Reject an unusable cluster under its lock before scanning it. Relocate
it only while its table is present.
Fixes: cdd77f84d966 ("mm, swap: consolidate cluster allocation helpers")
Cc: stable@vger.kernel.org
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
---
mm/swapfile.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index c3288910b3e3..988b7316e0a1 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -981,7 +981,11 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
bool need_reclaim, ret, usable;
lockdep_assert_held(&ci->lock);
- VM_WARN_ON(!cluster_is_usable(ci, order));
+ if (!cluster_is_usable(ci, order)) {
+ if (cluster_table_is_alloced(ci))
+ relocate_cluster(si, ci);
+ goto out_unlock;
+ }
if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER)
goto out;
@@ -1010,6 +1014,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
}
out:
relocate_cluster(si, ci);
+out_unlock:
swap_cluster_unlock(ci);
if (si->flags & SWP_SOLIDSTATE) {
this_cpu_write(percpu_swap_cluster.offset[order], next);
base-commit: 4dbe24720eb3811563cd56b35d87ddf3f9beed2a
--
2.53.0-Meta
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm, swap: revalidate populated clusters before scanning
2026-10-19 16:06 [PATCH] mm, swap: revalidate populated clusters before scanning Nhat Pham
@ 2026-10-19 16:16 ` Nhat Pham
2026-10-20 6:58 ` Youngjun Park
2026-10-20 17:57 ` Kairui Song
2 siblings, 0 replies; 10+ messages in thread
From: Nhat Pham @ 2026-10-19 16:16 UTC (permalink / raw)
To: Andrew Morton
Cc: Chris Li, Kairui Song, Baoquan He, Barry Song, Youngjun Park,
linux-mm, linux-kernel, stable
On Mon, Oct 19, 2026 at 9:06 AM Nhat Pham <nphamcs@gmail.com> wrote:
>
> swap_cluster_populate() can publish a cluster's table with the cluster
> lock dropped. A cached allocator can use and empty it before the
> populating allocator reacquires the lock. The resumed scan can then
> dereference a NULL table.
>
> For example, CPU1 can retain a cached cluster offset on an SSD with
> per-page discard disabled:
>
> CPU0 CPU1
> ---- ----
> swap_cluster_populate()
> swap_cluster_alloc_table() fails
> spin_unlock(&ci->lock)
> local_unlock(&percpu_swap_cluster.lock)
> swap_cluster_alloc_table()
> rcu_assign_pointer(ci->table, table)
> swap_alloc_fast()
> local_unlock(&percpu_swap_cluster.lock)
> mem_cgroup_try_charge_swap() fails
> swap_cache_del_folio()
> __swap_cluster_free_entries()
> free_cluster()
> __free_cluster()
> swap_cluster_free_table()
> rcu_assign_pointer(..., NULL)
> local_lock(&percpu_swap_cluster.lock)
> spin_lock(&ci->lock)
> return ci
> alloc_swap_scan_cluster()
> cluster_scan_range()
> __swap_table_get()
>
> Reject an unusable cluster under its lock before scanning it. Relocate
> it only while its table is present.
>
> Fixes: cdd77f84d966 ("mm, swap: consolidate cluster allocation helpers")
> Cc: stable@vger.kernel.org
> Signed-off-by: Nhat Pham <nphamcs@gmail.com>
I'm revisiting the lifecycle of clusters, because vswap introduced
dynamic (i.e run-time freeing) behavior to it. For the most part it
looks good (both physical and virtual side), but this look racy to me.
I don't see how it can be safe. Once you drop the lock, all bets are
off...
But at the same time, I have not been able to reproduce it (yet). Will
report here if I manage to.
(Side note: my vswap code is actually safe in this scenario. CPU0
allocates a new cluster entirely, and lock it before publishing it. So
even if the cluster index is cached in CPU1, this race cannot happen).
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm, swap: revalidate populated clusters before scanning
2026-10-19 16:06 [PATCH] mm, swap: revalidate populated clusters before scanning Nhat Pham
2026-10-19 16:16 ` Nhat Pham
@ 2026-10-20 6:58 ` Youngjun Park
2026-10-20 13:12 ` Nhat Pham
2026-10-20 17:57 ` Kairui Song
2 siblings, 1 reply; 10+ messages in thread
From: Youngjun Park @ 2026-10-20 6:58 UTC (permalink / raw)
To: Nhat Pham
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, linux-mm, linux-kernel, stable
On Mon, Oct 19, 2026 at 09:06:15AM -0700, Nhat Pham wrote:
> swap_cluster_populate() can publish a cluster's table with the cluster
> lock dropped. A cached allocator can use and empty it before the
> populating allocator reacquires the lock. The resumed scan can then
> dereference a NULL table.
>
> For example, CPU1 can retain a cached cluster offset on an SSD with
> per-page discard disabled:
>
> CPU0 CPU1
> ---- ----
> swap_cluster_populate()
> swap_cluster_alloc_table() fails
> spin_unlock(&ci->lock)
> local_unlock(&percpu_swap_cluster.lock)
> swap_cluster_alloc_table()
> rcu_assign_pointer(ci->table, table)
> swap_alloc_fast()
> local_unlock(&percpu_swap_cluster.lock)
> mem_cgroup_try_charge_swap() fails
> swap_cache_del_folio()
> __swap_cluster_free_entries()
> free_cluster()
> __free_cluster()
> swap_cluster_free_table()
> rcu_assign_pointer(..., NULL)
> local_lock(&percpu_swap_cluster.lock)
> spin_lock(&ci->lock)
> return ci
> alloc_swap_scan_cluster()
> cluster_scan_range()
> __swap_table_get()
Hello Nhat.
Good catch!
> Reject an unusable cluster under its lock before scanning it. Relocate
> it only while its table is present.
>
> Fixes: cdd77f84d966 ("mm, swap: consolidate cluster allocation helpers")
> Cc: stable@vger.kernel.org
> Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> ---
> mm/swapfile.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index c3288910b3e3..988b7316e0a1 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -981,7 +981,11 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
> bool need_reclaim, ret, usable;
>
> lockdep_assert_held(&ci->lock);
> - VM_WARN_ON(!cluster_is_usable(ci, order));
> + if (!cluster_is_usable(ci, order)) {
> + if (cluster_table_is_alloced(ci))
> + relocate_cluster(si, ci);
> + goto out_unlock;
> + }
If this patch is applied as bloew, does it prevent the issue described here?
https://lore.kernel.org/linux-mm/20260720071342.50742-2-shikemeng@huaweicloud.com/
(This patch publishes cluster table after lock acquired.)
This patch is chagned the way I suggested during review... :(
As you pointed out, another context can free the table
without alloc context knwoing this situation.
I missed this situation.
IMHO, Publishing the table only after reacquiring the cluster lock, as Kemeng
suggested, and rejecting an unusable cluster under the lock as in this
patch both look correct to me!
Reviewed-by: Youngjun Park <youngjun.park@lge.com>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm, swap: revalidate populated clusters before scanning
2026-10-20 6:58 ` Youngjun Park
@ 2026-10-20 13:12 ` Nhat Pham
2026-10-20 13:18 ` Nhat Pham
0 siblings, 1 reply; 10+ messages in thread
From: Nhat Pham @ 2026-10-20 13:12 UTC (permalink / raw)
To: Youngjun Park
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, linux-mm, linux-kernel, stable
On Mon, Oct 19, 2026 at 11:58 PM Youngjun Park <youngjun.park@lge.com> wrote:
>
> On Mon, Oct 19, 2026 at 09:06:15AM -0700, Nhat Pham wrote:
> > swap_cluster_populate() can publish a cluster's table with the cluster
> > lock dropped. A cached allocator can use and empty it before the
> > populating allocator reacquires the lock. The resumed scan can then
> > dereference a NULL table.
> >
> > For example, CPU1 can retain a cached cluster offset on an SSD with
> > per-page discard disabled:
> >
> > CPU0 CPU1
> > ---- ----
> > swap_cluster_populate()
> > swap_cluster_alloc_table() fails
> > spin_unlock(&ci->lock)
> > local_unlock(&percpu_swap_cluster.lock)
> > swap_cluster_alloc_table()
> > rcu_assign_pointer(ci->table, table)
> > swap_alloc_fast()
> > local_unlock(&percpu_swap_cluster.lock)
> > mem_cgroup_try_charge_swap() fails
> > swap_cache_del_folio()
> > __swap_cluster_free_entries()
> > free_cluster()
> > __free_cluster()
> > swap_cluster_free_table()
> > rcu_assign_pointer(..., NULL)
> > local_lock(&percpu_swap_cluster.lock)
> > spin_lock(&ci->lock)
> > return ci
> > alloc_swap_scan_cluster()
> > cluster_scan_range()
> > __swap_table_get()
>
> Hello Nhat.
>
> Good catch!
>
> > Reject an unusable cluster under its lock before scanning it. Relocate
> > it only while its table is present.
> >
> > Fixes: cdd77f84d966 ("mm, swap: consolidate cluster allocation helpers")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> > ---
> > mm/swapfile.c | 7 ++++++-
> > 1 file changed, 6 insertions(+), 1 deletion(-)
> >
> > diff --git a/mm/swapfile.c b/mm/swapfile.c
> > index c3288910b3e3..988b7316e0a1 100644
> > --- a/mm/swapfile.c
> > +++ b/mm/swapfile.c
> > @@ -981,7 +981,11 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
> > bool need_reclaim, ret, usable;
> >
> > lockdep_assert_held(&ci->lock);
> > - VM_WARN_ON(!cluster_is_usable(ci, order));
> > + if (!cluster_is_usable(ci, order)) {
> > + if (cluster_table_is_alloced(ci))
> > + relocate_cluster(si, ci);
> > + goto out_unlock;
> > + }
>
> If this patch is applied as bloew, does it prevent the issue described here?
>
> https://lore.kernel.org/linux-mm/20260720071342.50742-2-shikemeng@huaweicloud.com/
> (This patch publishes cluster table after lock acquired.)
>
> This patch is chagned the way I suggested during review... :(
Ah I missed that patch series and your review! What happened to it?
Did we just never land it?
Looks like we are still doing neither Keming's approach (table
copying) nor your approach (publishing at the end of
swap_cluster_alloc_table()).
>
> As you pointed out, another context can free the table
> without alloc context knwoing this situation.
> I missed this situation.
Yeah I feel like it's still not safe with your approach...
swap_cluster_alloc_table() does not hold nor acquire cluster's lock.
Between its publishing of the table and the subsequent lock
acquisition in its caller (swap_cluster_populate()), someone can still
come in and free the table.
It's tricky. We do not have an explicit contract for the metadata's
lifetime, so it's a bit of reading the tea leaves for me...
I think this is the last gap, and it's rare enough, so this simple
check should suffice to cover the edgiest of the edge cases :) But who
knows :) If bugs re-surfaced again, I think we can spend a bit more
time ironcladding the lifetime of clusters/tables:
1. Allocate outside of lock context, but make swap_cluster_populate()
lock the cluster, check cluster's status, and only then publish the
table (and return with the cluster locked). Basically Keming's
approach.
2. Carefully enforce metadata's lifetime. Perhaps we can add a refcnt?
In addition to occupied slots holding cluster's refcnt, we can allow
for temporary holders. As long as we can acquire a refcnt, we know for
sure the metadata is not freed under us. This is my approach for the
old vswap patch series, but it's tricky, especially with caching in
the picture.
>
> IMHO, Publishing the table only after reacquiring the cluster lock, as Kemeng
> suggested, and rejecting an unusable cluster under the lock as in this
> patch both look correct to me!
>
> Reviewed-by: Youngjun Park <youngjun.park@lge.com>
Thanks for the review, Youngjun!
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm, swap: revalidate populated clusters before scanning
2026-10-20 13:12 ` Nhat Pham
@ 2026-10-20 13:18 ` Nhat Pham
2026-10-20 17:58 ` Kairui Song
0 siblings, 1 reply; 10+ messages in thread
From: Nhat Pham @ 2026-10-20 13:18 UTC (permalink / raw)
To: Youngjun Park
Cc: Andrew Morton, Chris Li, Kairui Song, Kemeng Shi, Baoquan He,
Barry Song, linux-mm, linux-kernel, stable
On Tue, Oct 20, 2026 at 6:12 AM Nhat Pham <nphamcs@gmail.com> wrote:
>
> On Mon, Oct 19, 2026 at 11:58 PM Youngjun Park <youngjun.park@lge.com> wrote:
> >
> > On Mon, Oct 19, 2026 at 09:06:15AM -0700, Nhat Pham wrote:
> > > swap_cluster_populate() can publish a cluster's table with the cluster
> > > lock dropped. A cached allocator can use and empty it before the
> > > populating allocator reacquires the lock. The resumed scan can then
> > > dereference a NULL table.
> > >
> > > For example, CPU1 can retain a cached cluster offset on an SSD with
> > > per-page discard disabled:
> > >
> > > CPU0 CPU1
> > > ---- ----
> > > swap_cluster_populate()
> > > swap_cluster_alloc_table() fails
> > > spin_unlock(&ci->lock)
> > > local_unlock(&percpu_swap_cluster.lock)
> > > swap_cluster_alloc_table()
> > > rcu_assign_pointer(ci->table, table)
> > > swap_alloc_fast()
> > > local_unlock(&percpu_swap_cluster.lock)
> > > mem_cgroup_try_charge_swap() fails
> > > swap_cache_del_folio()
> > > __swap_cluster_free_entries()
> > > free_cluster()
> > > __free_cluster()
> > > swap_cluster_free_table()
> > > rcu_assign_pointer(..., NULL)
> > > local_lock(&percpu_swap_cluster.lock)
> > > spin_lock(&ci->lock)
> > > return ci
> > > alloc_swap_scan_cluster()
> > > cluster_scan_range()
> > > __swap_table_get()
> >
> > Hello Nhat.
> >
> > Good catch!
> >
> > > Reject an unusable cluster under its lock before scanning it. Relocate
> > > it only while its table is present.
> > >
> > > Fixes: cdd77f84d966 ("mm, swap: consolidate cluster allocation helpers")
> > > Cc: stable@vger.kernel.org
> > > Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> > > ---
> > > mm/swapfile.c | 7 ++++++-
> > > 1 file changed, 6 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/mm/swapfile.c b/mm/swapfile.c
> > > index c3288910b3e3..988b7316e0a1 100644
> > > --- a/mm/swapfile.c
> > > +++ b/mm/swapfile.c
> > > @@ -981,7 +981,11 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
> > > bool need_reclaim, ret, usable;
> > >
> > > lockdep_assert_held(&ci->lock);
> > > - VM_WARN_ON(!cluster_is_usable(ci, order));
> > > + if (!cluster_is_usable(ci, order)) {
> > > + if (cluster_table_is_alloced(ci))
> > > + relocate_cluster(si, ci);
> > > + goto out_unlock;
> > > + }
> >
> > If this patch is applied as bloew, does it prevent the issue described here?
> >
> > https://lore.kernel.org/linux-mm/20260720071342.50742-2-shikemeng@huaweicloud.com/
> > (This patch publishes cluster table after lock acquired.)
> >
> > This patch is chagned the way I suggested during review... :(
>
> Ah I missed that patch series and your review! What happened to it?
> Did we just never land it?
>
> Looks like we are still doing neither Keming's approach (table
> copying) nor your approach (publishing at the end of
> swap_cluster_alloc_table()).
Ah nope, I saw that Kemeng implemented your approach:
https://lore.kernel.org/linux-mm/20260907091356.53026-2-shikemeng@huaweicloud.com/
My bad! But yeah I think we still need this small fix (or Kemeng's
original proposal?), unless I missed something...?
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm, swap: revalidate populated clusters before scanning
2026-10-19 16:06 [PATCH] mm, swap: revalidate populated clusters before scanning Nhat Pham
2026-10-19 16:16 ` Nhat Pham
2026-10-20 6:58 ` Youngjun Park
@ 2026-10-20 17:57 ` Kairui Song
2026-10-20 20:19 ` Nhat Pham
2 siblings, 1 reply; 10+ messages in thread
From: Kairui Song @ 2026-10-20 17:57 UTC (permalink / raw)
To: Nhat Pham
Cc: Andrew Morton, Chris Li, Kemeng Shi, Baoquan He, Barry Song,
Youngjun Park, linux-mm, linux-kernel, stable
On Tue, Oct 20, 2026 at 1:41 AM Nhat Pham <nphamcs@gmail.com> wrote:
>
> swap_cluster_populate() can publish a cluster's table with the cluster
> lock dropped. A cached allocator can use and empty it before the
> populating allocator reacquires the lock. The resumed scan can then
> dereference a NULL table.
>
> For example, CPU1 can retain a cached cluster offset on an SSD with
> per-page discard disabled:
>
> CPU0 CPU1
> ---- ----
> swap_cluster_populate()
> swap_cluster_alloc_table() fails
> spin_unlock(&ci->lock)
> local_unlock(&percpu_swap_cluster.lock)
> swap_cluster_alloc_table()
> rcu_assign_pointer(ci->table, table)
Nice catch!
I think the real problem is that it turns out calling
swap_cluster_alloc_table() without holding ci lock is bad idea... I
think this patch still leaves some other call site too?
We may instead add a "bool locked" to swap_cluster_alloc_table, and
let it lock the ci first before it does any assign (not the alloc) if
the allocation context is not locked. That is, keep the
sleep-allocated tables private until swap_cluster_populate()
reacquires local/global/ci, then install them under ci->lock,
something like pre-cdd77f84d966, always assign it atomically.
That should also cover the problem reported by Kemeng a few weeks
earlier too, and prevent further misuse. My Bad on that change :P
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm, swap: revalidate populated clusters before scanning
2026-10-20 13:18 ` Nhat Pham
@ 2026-10-20 17:58 ` Kairui Song
0 siblings, 0 replies; 10+ messages in thread
From: Kairui Song @ 2026-10-20 17:58 UTC (permalink / raw)
To: Nhat Pham
Cc: Youngjun Park, Andrew Morton, Chris Li, Kemeng Shi, Baoquan He,
Barry Song, linux-mm, linux-kernel, stable
On Tue, Oct 20, 2026 at 9:23 PM Nhat Pham <nphamcs@gmail.com> wrote:
>
> On Tue, Oct 20, 2026 at 6:12 AM Nhat Pham <nphamcs@gmail.com> wrote:
> >
> > On Mon, Oct 19, 2026 at 11:58 PM Youngjun Park <youngjun.park@lge.com> wrote:
> > >
> > > On Mon, Oct 19, 2026 at 09:06:15AM -0700, Nhat Pham wrote:
> > > > swap_cluster_populate() can publish a cluster's table with the cluster
> > > > lock dropped. A cached allocator can use and empty it before the
> > > > populating allocator reacquires the lock. The resumed scan can then
> > > > dereference a NULL table.
> > > >
> > > > For example, CPU1 can retain a cached cluster offset on an SSD with
> > > > per-page discard disabled:
> > > >
> > > > CPU0 CPU1
> > > > ---- ----
> > > > swap_cluster_populate()
> > > > swap_cluster_alloc_table() fails
> > > > spin_unlock(&ci->lock)
> > > > local_unlock(&percpu_swap_cluster.lock)
> > > > swap_cluster_alloc_table()
> > > > rcu_assign_pointer(ci->table, table)
> > > > swap_alloc_fast()
> > > > local_unlock(&percpu_swap_cluster.lock)
> > > > mem_cgroup_try_charge_swap() fails
> > > > swap_cache_del_folio()
> > > > __swap_cluster_free_entries()
> > > > free_cluster()
> > > > __free_cluster()
> > > > swap_cluster_free_table()
> > > > rcu_assign_pointer(..., NULL)
> > > > local_lock(&percpu_swap_cluster.lock)
> > > > spin_lock(&ci->lock)
> > > > return ci
> > > > alloc_swap_scan_cluster()
> > > > cluster_scan_range()
> > > > __swap_table_get()
> > >
> > > Hello Nhat.
> > >
> > > Good catch!
> > >
> > > > Reject an unusable cluster under its lock before scanning it. Relocate
> > > > it only while its table is present.
> > > >
> > > > Fixes: cdd77f84d966 ("mm, swap: consolidate cluster allocation helpers")
> > > > Cc: stable@vger.kernel.org
> > > > Signed-off-by: Nhat Pham <nphamcs@gmail.com>
> > > > ---
> > > > mm/swapfile.c | 7 ++++++-
> > > > 1 file changed, 6 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/mm/swapfile.c b/mm/swapfile.c
> > > > index c3288910b3e3..988b7316e0a1 100644
> > > > --- a/mm/swapfile.c
> > > > +++ b/mm/swapfile.c
> > > > @@ -981,7 +981,11 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
> > > > bool need_reclaim, ret, usable;
> > > >
> > > > lockdep_assert_held(&ci->lock);
> > > > - VM_WARN_ON(!cluster_is_usable(ci, order));
> > > > + if (!cluster_is_usable(ci, order)) {
> > > > + if (cluster_table_is_alloced(ci))
> > > > + relocate_cluster(si, ci);
> > > > + goto out_unlock;
> > > > + }
> > >
> > > If this patch is applied as bloew, does it prevent the issue described here?
> > >
> > > https://lore.kernel.org/linux-mm/20260720071342.50742-2-shikemeng@huaweicloud.com/
> > > (This patch publishes cluster table after lock acquired.)
> > >
> > > This patch is chagned the way I suggested during review... :(
> >
> > Ah I missed that patch series and your review! What happened to it?
> > Did we just never land it?
> >
> > Looks like we are still doing neither Keming's approach (table
> > copying) nor your approach (publishing at the end of
> > swap_cluster_alloc_table()).
>
> Ah nope, I saw that Kemeng implemented your approach:
>
> https://lore.kernel.org/linux-mm/20260907091356.53026-2-shikemeng@huaweicloud.com/
>
> My bad! But yeah I think we still need this small fix (or Kemeng's
> original proposal?), unless I missed something...?
Right I remember that, I acked part of that and was waiting the update
on that sashiko comment, as the first patch isn't perfect, while
Kemeng's mail at "shikemeng@huaweicloud.com" is now bouncing back...
I'm not sure if Kemeng still plans to update that.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm, swap: revalidate populated clusters before scanning
2026-10-20 17:57 ` Kairui Song
@ 2026-10-20 20:19 ` Nhat Pham
2026-10-22 6:52 ` Kairui Song
0 siblings, 1 reply; 10+ messages in thread
From: Nhat Pham @ 2026-10-20 20:19 UTC (permalink / raw)
To: Kairui Song
Cc: Andrew Morton, Chris Li, Kemeng Shi, Baoquan He, Barry Song,
Youngjun Park, linux-mm, linux-kernel, stable
On Tue, Oct 20, 2026 at 10:58 AM Kairui Song <ryncsn@gmail.com> wrote:
>
> On Tue, Oct 20, 2026 at 1:41 AM Nhat Pham <nphamcs@gmail.com> wrote:
> >
> > swap_cluster_populate() can publish a cluster's table with the cluster
> > lock dropped. A cached allocator can use and empty it before the
> > populating allocator reacquires the lock. The resumed scan can then
> > dereference a NULL table.
> >
> > For example, CPU1 can retain a cached cluster offset on an SSD with
> > per-page discard disabled:
> >
> > CPU0 CPU1
> > ---- ----
> > swap_cluster_populate()
> > swap_cluster_alloc_table() fails
> > spin_unlock(&ci->lock)
> > local_unlock(&percpu_swap_cluster.lock)
> > swap_cluster_alloc_table()
> > rcu_assign_pointer(ci->table, table)
>
> Nice catch!
>
> I think the real problem is that it turns out calling
> swap_cluster_alloc_table() without holding ci lock is bad idea... I
> think this patch still leaves some other call site too?
Do we have another callsite? There's a callsite in setup code, but I
assume that's safe...?
But at this point, who knows any more. The caching only stores a
swapfile struct, and an offset. I wonder if there is a potential race
between swapoff-then-re-swapon and cached allocation? That'd be pretty
wild.
>
> We may instead add a "bool locked" to swap_cluster_alloc_table, and
> let it lock the ci first before it does any assign (not the alloc) if
> the allocation context is not locked. That is, keep the
> sleep-allocated tables private until swap_cluster_populate()
> reacquires local/global/ci, then install them under ci->lock,
> something like pre-cdd77f84d966, always assign it atomically.
Hmm so a bitlock?
Lemme think about it a bit.
>
> That should also cover the problem reported by Kemeng a few weeks
> earlier too, and prevent further misuse. My Bad on that change :P
No worries. Lifetime is hard :)
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm, swap: revalidate populated clusters before scanning
2026-10-20 20:19 ` Nhat Pham
@ 2026-10-22 6:52 ` Kairui Song
2026-11-06 13:35 ` Nhat Pham
0 siblings, 1 reply; 10+ messages in thread
From: Kairui Song @ 2026-10-22 6:52 UTC (permalink / raw)
To: Nhat Pham
Cc: Andrew Morton, Chris Li, Kemeng Shi, Baoquan He, Barry Song,
Youngjun Park, linux-mm, LKML, stable
Nhat Pham <nphamcs@gmail.com> 于 2026年10月21日周三 04:19写道:
>
> On Tue, Oct 20, 2026 at 10:58 AM Kairui Song <ryncsn@gmail.com> wrote:
> >
> > On Tue, Oct 20, 2026 at 1:41 AM Nhat Pham <nphamcs@gmail.com> wrote:
> > >
> > > swap_cluster_populate() can publish a cluster's table with the cluster
> > > lock dropped. A cached allocator can use and empty it before the
> > > populating allocator reacquires the lock. The resumed scan can then
> > > dereference a NULL table.
> > >
> > > For example, CPU1 can retain a cached cluster offset on an SSD with
> > > per-page discard disabled:
> > >
> > > CPU0 CPU1
> > > ---- ----
> > > swap_cluster_populate()
> > > swap_cluster_alloc_table() fails
> > > spin_unlock(&ci->lock)
> > > local_unlock(&percpu_swap_cluster.lock)
> > > swap_cluster_alloc_table()
> > > rcu_assign_pointer(ci->table, table)
> >
> > Nice catch!
> >
> > I think the real problem is that it turns out calling
> > swap_cluster_alloc_table() without holding ci lock is bad idea... I
> > think this patch still leaves some other call site too?
>
> Do we have another callsite? There's a callsite in setup code, but I
> assume that's safe...?
>
> But at this point, who knows any more. The caching only stores a
> swapfile struct, and an offset. I wonder if there is a potential race
> between swapoff-then-re-swapon and cached allocation? That'd be pretty
> wild.
>
>
> >
> > We may instead add a "bool locked" to swap_cluster_alloc_table, and
> > let it lock the ci first before it does any assign (not the alloc) if
> > the allocation context is not locked. That is, keep the
> > sleep-allocated tables private until swap_cluster_populate()
> > reacquires local/global/ci, then install them under ci->lock,
> > something like pre-cdd77f84d966, always assign it atomically.
>
> Hmm so a bitlock?
>
> Lemme think about it a bit.
Ah, no, not that complex, just something like:
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -449,9 +449,13 @@ static void swap_cluster_free_table(struct
swap_cluster_info *ci)
swap_cluster_free_count_table(table);
}
-static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
+static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp,
+ bool locked)
{
struct swap_table *table = NULL;
+ struct swap_memcg_table *memcg_table = NULL;
+ unsigned long *zeromap = NULL;
+
struct folio *folio;
/* The cluster must be empty and not on any list during allocation. */
@@ -472,8 +476,8 @@ static int swap_cluster_alloc_table(struct
swap_cluster_info *ci, gfp_t gfp)
#ifdef CONFIG_MEMCG
if (!mem_cgroup_disabled()) {
VM_WARN_ON_ONCE(ci->memcg_table);
- ci->memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
- if (!ci->memcg_table) {
+ memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
+ if (!memcg_table) {
swap_cluster_free_count_table(table);
return -ENOMEM;
}
@@ -484,8 +488,8 @@ static int swap_cluster_alloc_table(struct
swap_cluster_info *ci, gfp_t gfp)
VM_WARN_ON_ONCE(ci->zero_bitmap);
ci->zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
if (!ci->zero_bitmap) {
- swap_cluster_free_table(ci);
swap_cluster_free_count_table(table);
+ kfree(memcg_table);
return -ENOMEM;
}
#endif
@@ -494,6 +498,20 @@ static int swap_cluster_alloc_table(struct
swap_cluster_info *ci, gfp_t gfp)
* Make tables visible to cluster_is_usable() after everything is
* ready.
*/
+ if (!locked) {
+ / * Lock it here before assign, not after the asssign
and return */
...
This should fix all potential issues I think?
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] mm, swap: revalidate populated clusters before scanning
2026-10-22 6:52 ` Kairui Song
@ 2026-11-06 13:35 ` Nhat Pham
0 siblings, 0 replies; 10+ messages in thread
From: Nhat Pham @ 2026-11-06 13:35 UTC (permalink / raw)
To: Kairui Song
Cc: Andrew Morton, Chris Li, Kemeng Shi, Baoquan He, Barry Song,
Youngjun Park, linux-mm, LKML, stable
On Wed, Oct 21, 2026 at 11:53 PM Kairui Song <ryncsn@gmail.com> wrote:
>
>
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -449,9 +449,13 @@ static void swap_cluster_free_table(struct
> swap_cluster_info *ci)
> swap_cluster_free_count_table(table);
> }
>
> -static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
> +static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp,
> + bool locked)
> {
> struct swap_table *table = NULL;
> + struct swap_memcg_table *memcg_table = NULL;
> + unsigned long *zeromap = NULL;
> +
> struct folio *folio;
>
> /* The cluster must be empty and not on any list during allocation. */
> @@ -472,8 +476,8 @@ static int swap_cluster_alloc_table(struct
> swap_cluster_info *ci, gfp_t gfp)
> #ifdef CONFIG_MEMCG
> if (!mem_cgroup_disabled()) {
> VM_WARN_ON_ONCE(ci->memcg_table);
> - ci->memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
> - if (!ci->memcg_table) {
> + memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
> + if (!memcg_table) {
> swap_cluster_free_count_table(table);
> return -ENOMEM;
> }
> @@ -484,8 +488,8 @@ static int swap_cluster_alloc_table(struct
> swap_cluster_info *ci, gfp_t gfp)
> VM_WARN_ON_ONCE(ci->zero_bitmap);
> ci->zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
> if (!ci->zero_bitmap) {
> - swap_cluster_free_table(ci);
> swap_cluster_free_count_table(table);
> + kfree(memcg_table);
> return -ENOMEM;
> }
> #endif
> @@ -494,6 +498,20 @@ static int swap_cluster_alloc_table(struct
> swap_cluster_info *ci, gfp_t gfp)
> * Make tables visible to cluster_is_usable() after everything is
> * ready.
> */
> + if (!locked) {
> + / * Lock it here before assign, not after the asssign
> and return */
> ...
>
> This should fix all potential issues I think?
Lol this shows up twice under vswap's sashiko reports:
https://lore.kernel.org/all/sashiko-outbox-181084@kernel.org
https://lore.kernel.org/all/sashiko-outbox-181086@kernel.org
Now it's personal :P
Anyway, I'm sending a patch soon based on your idea (will keep your
attribution around, if you don't mind - but we'd need more eyes to
review it to ironclad it). I also have a couple more fixes along the
way. Nice thing about sashiko is that it's digging out existing issues
while reviewing new code, for free!
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-11-06 13:35 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-19 16:06 [PATCH] mm, swap: revalidate populated clusters before scanning Nhat Pham
2026-10-19 16:16 ` Nhat Pham
2026-10-20 6:58 ` Youngjun Park
2026-10-20 13:12 ` Nhat Pham
2026-10-20 13:18 ` Nhat Pham
2026-10-20 17:58 ` Kairui Song
2026-10-20 17:57 ` Kairui Song
2026-10-20 20:19 ` Nhat Pham
2026-10-22 6:52 ` Kairui Song
2026-11-06 13:35 ` Nhat Pham
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®