[PATCH 1/3] ida: Introduce ida_weight()

Michal Wajdeczko michal.wajdeczko at intel.com
Wed Nov 1 12:31:39 UTC 2023


Add helper function that will calculate number of allocated IDs
in the IDA.  This might be helpful both for drivers to estimate
saturation of used IDs and for testing the IDA implementation.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko at intel.com>
---
 include/linux/idr.h |  1 +
 lib/idr.c           | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/include/linux/idr.h b/include/linux/idr.h
index a0dce14090a9..f477e35c9619 100644
--- a/include/linux/idr.h
+++ b/include/linux/idr.h
@@ -255,6 +255,7 @@ struct ida {
 int ida_alloc_range(struct ida *, unsigned int min, unsigned int max, gfp_t);
 void ida_free(struct ida *, unsigned int id);
 void ida_destroy(struct ida *ida);
+unsigned long ida_weight(struct ida *ida);
 
 /**
  * ida_alloc() - Allocate an unused ID.
diff --git a/lib/idr.c b/lib/idr.c
index 13f2758c2377..ed987a0fc25a 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -554,6 +554,31 @@ void ida_destroy(struct ida *ida)
 }
 EXPORT_SYMBOL(ida_destroy);
 
+/**
+ * ida_weight() - Calculate number of allocated IDs.
+ * @ida: IDA handle.
+ *
+ * Return: Number of allocated IDs in this IDA.
+ */
+unsigned long ida_weight(struct ida *ida)
+{
+	XA_STATE(xas, &ida->xa, 0);
+	struct ida_bitmap *bitmap;
+	unsigned long weight = 0;
+	unsigned long flags;
+
+	xas_lock_irqsave(&xas, flags);
+	xas_for_each(&xas, bitmap, ULONG_MAX) {
+		weight += xa_is_value(bitmap) ?
+			hweight_long(xa_to_value(bitmap)) :
+			bitmap_weight(bitmap->bitmap, IDA_BITMAP_BITS);
+	}
+	xas_unlock_irqrestore(&xas, flags);
+
+	return weight;
+}
+EXPORT_SYMBOL(ida_weight);
+
 #ifndef __KERNEL__
 extern void xa_dump_index(unsigned long index, unsigned int shift);
 #define IDA_CHUNK_SHIFT		ilog2(IDA_BITMAP_BITS)
-- 
2.25.1



More information about the Intel-gfx-trybot mailing list