Re: [PATCH v4 1/4] mm/zsmalloc: encode class index in obj value for lockless class lookup

From: Nhat Pham

Date: Tue Jun 09 2026 - 14:09:40 EST


On Tue, Jun 9, 2026 at 4:35 AM Wenchao Hao <haowenchao22@xxxxxxxxx> wrote:
>
> From: Wenchao Hao <haowenchao@xxxxxxxxxx>
>
> + * ceil(log2(ZS_MAX_PAGES_PER_ZSPAGE)) at preprocessor time, for use
> + * in #if below. Kconfig restricts ZSMALLOC_CHAIN_SIZE to [4, 16].
> + */
> +#if ZS_MAX_PAGES_PER_ZSPAGE <= 4
> +#define ZS_CHAIN_LOG2 2
> +#elif ZS_MAX_PAGES_PER_ZSPAGE <= 8
> +#define ZS_CHAIN_LOG2 3
> +#elif ZS_MAX_PAGES_PER_ZSPAGE <= 16
> +#define ZS_CHAIN_LOG2 4

Do we not have log2 macro to use here?

> +#else
> +#error "ZSMALLOC_CHAIN_SIZE out of expected range [4,16]"
> +#endif
> +
> +/* PAGE_SHIFT - 5 = log2(PAGE_SIZE / 32); 32 = ZS_MIN_ALLOC_SIZE floor. */
> +#define ZS_MAX_OBJ_PER_PAGE_LOG2 (PAGE_SHIFT - 5)
> +
> +/*
> + * obj_idx width that keeps ZS_MIN_ALLOC_SIZE at its 32-byte floor.
> + * Below this, ZS_MIN_ALLOC_SIZE is auto-raised by the MAX(32, ...)
> + * formula -- still correct, but objects are coarser.
> + */
> +#define ZS_OBJ_IDX_DENSE_BITS (ZS_CHAIN_LOG2 + ZS_MAX_OBJ_PER_PAGE_LOG2)

ZS_CHAIN_LOG2 is (roughly) number of bits to represent the zpage index
in the zspage, and ZS_MAX_OBJ_PER_PAGE_LOG2 is roughly the number of
bits to represent the object index within the zpage, correct?

> +
> +/*
> + * Encode class_idx only when obj has spare bits; otherwise
> + * ZS_OBJ_CLASS_BITS folds to 0 (32-bit, or 64-bit UML/fallback).
> + */
> +#if BITS_PER_LONG >= 64 && \
> + ZS_OBJ_PFN_SHIFT >= (CLASS_BITS + 1) + ZS_OBJ_IDX_DENSE_BITS
> +#define ZS_OBJ_CLASS_BITS (CLASS_BITS + 1)

Seems good to me, albeit a bit complicated... Maybe it's because of
the naming of these macros?