Re: [PATCH v2 06/12] mm, swap: implement helpers for reserving data in the swap table
From: Kairui Song
Date: Sun Feb 01 2026 - 21:31:35 EST
On Thu, Jan 29, 2026 at 3:28 PM YoungJun Park <youngjun.park@xxxxxxx> wrote:
>
> On Wed, Jan 28, 2026 at 05:28:30PM +0800, Kairui Song wrote:
> > From: Kairui Song <kasong@xxxxxxxxxxx>
>
> > +static inline bool swp_tb_is_countable(unsigned long swp_tb)
> > +{
> > + return (swp_tb_is_shadow(swp_tb) || swp_tb_is_folio(swp_tb) ||
> > + swp_tb_is_null(swp_tb));
> > +}
Hi YoungJun,
Thanks for the review.
>
> What do you think about simplifying swp_tb_is_countable by just checking
> !swp_tb_is_bad(swp_tb)?
>
> Since this function appears to be called frequently, reducing the number of
> comparisons would be beneficial for performance. If validation is
> necessary for debugging perhaps we could introduce a separate version for debugging
> purposes.
There are already two variants for getting the count of a swap table
entry: swp_tb_get_count and __swp_tb_get_count. Ideally callers that
know the swap table entry is valid can just call __swp_tb_get_count
for lower overhead, swp_tb_is_countable is less frequently used.
>
> > +static inline int swp_tb_get_count(unsigned long swp_tb)
> > +{
> > + if (swp_tb_is_countable(swp_tb))
> > + return __swp_tb_get_count(swp_tb);
> > + return -EINVAL;
> > }
>
> Or, could we simply drop the check in swp_tb_get_count and call
> __swp_tb_get_count directly?
> If we define SWP_TB_BAD to have 0 in the count bits (MSB), it will
> naturally yield a count of 0.
One reason I used `countable` and not `bad` is because I'd introduce
other type of swap table entries soon, i.e. hibernation type for
hibernation only usage. Calling swp_tb_get_count on them returns
-EINVAL or other error code, which I think is looking good. I'm open
to suggestions on the naming and design.