Re: [PATCH v2 10/19] mm, swap: consolidate cluster reclaim and check logic
From: YoungJun Park
Date: Fri Nov 21 2025 - 01:58:15 EST
On Thu, Nov 20, 2025 at 11:32:37PM +0800, Kairui Song wrote:
...
> > > static bool cluster_scan_range(struct swap_info_struct *si,
> > > @@ -901,7 +909,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
> > > unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER);
> > > unsigned long end = min(start + SWAPFILE_CLUSTER, si->max);
> >
> > The Original code. I'm wondering if there's an off-by-one error here. Looking at the code
> > below, it seems the design allows the end offset to go through the
> > logic as well. Shouldn't it be 'start + SWAPFILE_CLUSTER - 1' and
> > 'si->max - 1'?
>
> You mean the `offset <= end` check below? That's fine because the for
> loops starts with `end -= nr_pages`.
>
That's right! I missed that. Thanks for the clarification
> >
> > > unsigned int nr_pages = 1 << order;
> > > - bool need_reclaim, ret;
> > > + bool need_reclaim;
> > >
> > > lockdep_assert_held(&ci->lock);
> > >
> > > @@ -913,20 +921,13 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si,
> > > if (!cluster_scan_range(si, ci, offset, nr_pages, &need_reclaim))
> > > continue;
> > > if (need_reclaim) {
> > > - ret = cluster_reclaim_range(si, ci, offset, offset + nr_pages);
> > > - /*
> > > - * Reclaim drops ci->lock and cluster could be used
> > > - * by another order. Not checking flag as off-list
> > > - * cluster has no flag set, and change of list
> > > - * won't cause fragmentation.
> > > - */
> > > + found = cluster_reclaim_range(si, ci, offset, order);
> > > if (!cluster_is_usable(ci, order))
> > > goto out;
> >
> > This check resolves the issue I mentioned in my previous review.
> >
> > > - if (cluster_is_empty(ci))
> > > - offset = start;
> > > /* Reclaim failed but cluster is usable, try next */
> > > - if (!ret)
> > > + if (!found)
> > > continue;
> > > + offset = found;
> > > }
> > > if (!cluster_alloc_range(si, ci, offset, usage, order))
> > > break;
> >
> > I think the reason cluster_is_usable() is checked redundantly here is
> > because cluster_reclaim_range() returns an unsigned int (offset), making
> > it impossible to distinguish error values.
> >
> > What if we make offset an output parameter (satisfying the assumption
> > that it can be changed in reclaim_range) and return an error value
> > instead? This would eliminate the redundant cluster_is_usable() check
> > and simplify the logic. Also, the consecutive "offset = found, found =
> > offset" is a bit confusing, and this approach could eliminate that as
> > well.
> >
> > What do you think?
>
> That's a good suggestion indeed, I'll try to make the code cleaner
> this way. Thanks!
Great~ I look forward to seeing the updated version. Thanks for
considering the suggestion.
Youngjun Park