Re: [PATCH] mm, swap: revalidate populated clusters before scanning
From: Nhat Pham
Date: Fri Nov 06 2026 - 08:36:15 EST
On Wed, Oct 21, 2026 at 11:53 PM Kairui Song <ryncsn@xxxxxxxxx> 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@xxxxxxxxxx
https://lore.kernel.org/all/sashiko-outbox-181086@xxxxxxxxxx
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!