@@ -205,6 +205,7 @@ typedef unsigned __bitwise xa_mark_t;
205205#define XA_MARK_2 ((__force xa_mark_t)2U)
206206#define XA_PRESENT ((__force xa_mark_t)8U)
207207#define XA_MARK_MAX XA_MARK_2
208+ #define XA_FREE_MARK XA_MARK_0
208209
209210enum xa_lock_type {
210211 XA_LOCK_IRQ = 1 ,
@@ -217,9 +218,12 @@ enum xa_lock_type {
217218 */
218219#define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
219220#define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
221+ #define XA_FLAGS_TRACK_FREE ((__force gfp_t)4U)
220222#define XA_FLAGS_MARK (mark ) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
221223 (__force unsigned)(mark)))
222224
225+ #define XA_FLAGS_ALLOC (XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
226+
223227/**
224228 * struct xarray - The anchor of the XArray.
225229 * @xa_lock: Lock that protects the contents of the XArray.
@@ -273,6 +277,15 @@ struct xarray {
273277 */
274278#define DEFINE_XARRAY (name ) DEFINE_XARRAY_FLAGS(name, 0)
275279
280+ /**
281+ * DEFINE_XARRAY_ALLOC() - Define an XArray which can allocate IDs.
282+ * @name: A string that names your XArray.
283+ *
284+ * This is intended for file scope definitions of allocating XArrays.
285+ * See also DEFINE_XARRAY().
286+ */
287+ #define DEFINE_XARRAY_ALLOC (name ) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
288+
276289void xa_init_flags (struct xarray * , gfp_t flags );
277290void * xa_load (struct xarray * , unsigned long index );
278291void * xa_store (struct xarray * , unsigned long index , void * entry , gfp_t );
@@ -439,6 +452,7 @@ void *__xa_erase(struct xarray *, unsigned long index);
439452void * __xa_store (struct xarray * , unsigned long index , void * entry , gfp_t );
440453void * __xa_cmpxchg (struct xarray * , unsigned long index , void * old ,
441454 void * entry , gfp_t );
455+ int __xa_alloc (struct xarray * , u32 * id , u32 max , void * entry , gfp_t );
442456void __xa_set_mark (struct xarray * , unsigned long index , xa_mark_t );
443457void __xa_clear_mark (struct xarray * , unsigned long index , xa_mark_t );
444458
@@ -518,6 +532,93 @@ static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
518532 return entry ;
519533}
520534
535+ /**
536+ * xa_alloc() - Find somewhere to store this entry in the XArray.
537+ * @xa: XArray.
538+ * @id: Pointer to ID.
539+ * @max: Maximum ID to allocate (inclusive).
540+ * @entry: New entry.
541+ * @gfp: Memory allocation flags.
542+ *
543+ * Allocates an unused ID in the range specified by @id and @max.
544+ * Updates the @id pointer with the index, then stores the entry at that
545+ * index. A concurrent lookup will not see an uninitialised @id.
546+ *
547+ * Context: Process context. Takes and releases the xa_lock. May sleep if
548+ * the @gfp flags permit.
549+ * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
550+ * there is no more space in the XArray.
551+ */
552+ static inline int xa_alloc (struct xarray * xa , u32 * id , u32 max , void * entry ,
553+ gfp_t gfp )
554+ {
555+ int err ;
556+
557+ xa_lock (xa );
558+ err = __xa_alloc (xa , id , max , entry , gfp );
559+ xa_unlock (xa );
560+
561+ return err ;
562+ }
563+
564+ /**
565+ * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
566+ * @xa: XArray.
567+ * @id: Pointer to ID.
568+ * @max: Maximum ID to allocate (inclusive).
569+ * @entry: New entry.
570+ * @gfp: Memory allocation flags.
571+ *
572+ * Allocates an unused ID in the range specified by @id and @max.
573+ * Updates the @id pointer with the index, then stores the entry at that
574+ * index. A concurrent lookup will not see an uninitialised @id.
575+ *
576+ * Context: Process context. Takes and releases the xa_lock while
577+ * disabling softirqs. May sleep if the @gfp flags permit.
578+ * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
579+ * there is no more space in the XArray.
580+ */
581+ static inline int xa_alloc_bh (struct xarray * xa , u32 * id , u32 max , void * entry ,
582+ gfp_t gfp )
583+ {
584+ int err ;
585+
586+ xa_lock_bh (xa );
587+ err = __xa_alloc (xa , id , max , entry , gfp );
588+ xa_unlock_bh (xa );
589+
590+ return err ;
591+ }
592+
593+ /**
594+ * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
595+ * @xa: XArray.
596+ * @id: Pointer to ID.
597+ * @max: Maximum ID to allocate (inclusive).
598+ * @entry: New entry.
599+ * @gfp: Memory allocation flags.
600+ *
601+ * Allocates an unused ID in the range specified by @id and @max.
602+ * Updates the @id pointer with the index, then stores the entry at that
603+ * index. A concurrent lookup will not see an uninitialised @id.
604+ *
605+ * Context: Process context. Takes and releases the xa_lock while
606+ * disabling interrupts. May sleep if the @gfp flags permit.
607+ * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
608+ * there is no more space in the XArray.
609+ */
610+ static inline int xa_alloc_irq (struct xarray * xa , u32 * id , u32 max , void * entry ,
611+ gfp_t gfp )
612+ {
613+ int err ;
614+
615+ xa_lock_irq (xa );
616+ err = __xa_alloc (xa , id , max , entry , gfp );
617+ xa_unlock_irq (xa );
618+
619+ return err ;
620+ }
621+
521622/* Everything below here is the Advanced API. Proceed with caution. */
522623
523624/*
0 commit comments