LinuxKPI: Add support for XA_FLAGS_ALLOC1 xarray flag

XA_FLAGS_ALLOC1 causes allocation of xarray entries starting at 1

Required by drm-kmod 5.7

MFC after:	1 week
Reviewed by:	hselasky, manu
Differential Revision:	https://reviews.freebsd.org/D33293
This commit is contained in:
Vladimir Kondratyev 2021-11-24 03:05:40 +03:00
parent 04d42cb453
commit e705066cd8
2 changed files with 11 additions and 6 deletions

View File

@ -40,6 +40,7 @@
#define XA_FLAGS_ALLOC (1U << 0)
#define XA_FLAGS_LOCK_IRQ (1U << 1)
#define XA_FLAGS_ALLOC1 (1U << 2)
#define XA_ERROR(x) \
ERR_PTR(x)
@ -53,6 +54,7 @@
struct xarray {
struct radix_tree_root root;
struct mtx mtx; /* internal mutex */
uint32_t flags; /* see XA_FLAGS_XXX */
};
/*

View File

@ -102,13 +102,13 @@ __xa_alloc(struct xarray *xa, uint32_t *pindex, void *ptr, uint32_t mask, gfp_t
XA_ASSERT_LOCKED(xa);
/* mask cannot be zero */
MPASS(mask != 0);
/* mask should allow to allocate at least one item */
MPASS(mask > (xa->flags & XA_FLAGS_ALLOC1) != 0 ? 1 : 0);
/* mask can be any power of two value minus one */
MPASS((mask & (mask + 1)) == 0);
*pindex = 0;
*pindex = (xa->flags & XA_FLAGS_ALLOC1) != 0 ? 1 : 0;
retry:
retval = radix_tree_insert(&xa->root, *pindex, ptr);
@ -159,13 +159,13 @@ __xa_alloc_cyclic(struct xarray *xa, uint32_t *pindex, void *ptr, uint32_t mask,
XA_ASSERT_LOCKED(xa);
/* mask cannot be zero */
MPASS(mask != 0);
/* mask should allow to allocate at least one item */
MPASS(mask > (xa->flags & XA_FLAGS_ALLOC1) != 0 ? 1 : 0);
/* mask can be any power of two value minus one */
MPASS((mask & (mask + 1)) == 0);
*pnext_index = 0;
*pnext_index = (xa->flags & XA_FLAGS_ALLOC1) != 0 ? 1 : 0;
retry:
retval = radix_tree_insert(&xa->root, *pnext_index, ptr);
@ -177,6 +177,8 @@ __xa_alloc_cyclic(struct xarray *xa, uint32_t *pindex, void *ptr, uint32_t mask,
}
(*pnext_index)++;
(*pnext_index) &= mask;
if (*pnext_index == 0 && (xa->flags & XA_FLAGS_ALLOC1) != 0)
(*pnext_index)++;
goto retry;
case -ENOMEM:
if (likely(gfp & M_WAITOK)) {
@ -302,6 +304,7 @@ xa_init_flags(struct xarray *xa, uint32_t flags)
mtx_init(&xa->mtx, "lkpi-xarray", NULL, MTX_DEF | MTX_RECURSE);
xa->root.gfp_mask = GFP_NOWAIT;
xa->flags = flags;
}
/*