Re: [PATCH v2 08/11] zsmalloc: encode 1-based obj_idx so valid obj is never 0
From: Yosry Ahmed
Date: Tue Nov 10 2026 - 12:04:02 EST
On Tue, Nov 10, 2026 at 02:48:13PM +0900, Sergey Senozhatsky wrote:
> The next patch embeds struct zs_handle into zram and zswap entries,
> and a handle is considered allocated when its obj is not 0. But obj
> can be 0: object 0 of a zspage whose first page is PFN 0 encodes to
> 0, and some platforms do give PFN 0 to the page allocator.
>
> Store obj_idx + 1 in obj, so that a valid obj is never 0, and adjust
> ZS_MIN_ALLOC_SIZE so that the largest index still fits. Unlike a
> separate tag bit, this doesn't take any bits from obj. zsmalloc did
> the same before handles moved to handle_cachep, see
> commit 67296874eb1c ("staging: zsmalloc: Ensure handle is never 0 on
> success").
>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
> ---
> mm/zsmalloc.c | 19 +++++++++++++------
> 1 file changed, 13 insertions(+), 6 deletions(-)
>
> diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
> index 3a6a7253fd01..aa436df0cc4d 100644
> --- a/mm/zsmalloc.c
> +++ b/mm/zsmalloc.c
> @@ -61,7 +61,8 @@
> * Object location (<PFN>, <obj_idx>) is encoded as
> * a single (unsigned long) handle value.
> *
> - * Note that object index <obj_idx> starts from 0.
> + * Note that object index <obj_idx> starts from 0, and is encoded as
> + * <obj_idx> + 1 so that a valid encoded obj value is never 0.
Well, this may be a stupid question, but why not make obj_idx start at
1?