Re: [PATCH v2 08/11] zsmalloc: encode 1-based obj_idx so valid obj is never 0
From: Sergey Senozhatsky
Date: Tue Nov 10 2026 - 23:48:07 EST
On (26/11/10 16:44), Yosry Ahmed wrote:
[..]
> > 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?
I didn't write that code! I just resurrected 67296874eb1c! Just joking.
We use idx to calculate offset, we need it to start from 0:
off = offset_in_page(class->size * obj_idx)