git: 4be27e6c5ae3 - main - ibcore: Avoid quadratic uverbs ID allocation

From: Krzysztof Galazka <kgalazka_at_FreeBSD.org>
Date: Fri, 27 Nov 2026 13:33:01 UTC
The branch main has been updated by kgalazka:

URL: https://cgit.FreeBSD.org/src/commit/?id=4be27e6c5ae3c0ff6b8eb14f76f5b51cb5bcdeae

commit 4be27e6c5ae3c0ff6b8eb14f76f5b51cb5bcdeae
Author:     Rafal Lewandowski <rafal.lewandowski@intel.com>
AuthorDate: 2026-11-27 12:11:06 +0000
Commit:     Krzysztof Galazka <kgalazka@FreeBSD.org>
CommitDate: 2026-11-27 13:29:17 +0000

    ibcore: Avoid quadratic uverbs ID allocation
    
    FreeBSD's LinuxKPI xa_alloc() starts at index zero for each allocation.
    The per-file uverbs XArray holds protection domains, completion queues,
    queue pairs, and memory regions. As the number of live objects grows,
    each new handle probes all occupied lower IDs, making large connection
    setups increasingly expensive.
    
    Keep the optimization in the OFED uverbs caller: track the next candidate
    ID in ib_uverbs_file, reserve under the XArray lock, and advance the
    cursor on success. On a collision, search forward and wrap through the
    32-bit handle space. Existing lookup, commit, erase, and destruction
    paths are unchanged. Freed lower IDs are reused after wraparound.
    
    Signed-off-by: Lewandowski, Rafal <rafal.lewandowski@intel.com>
    
    Reviewed by:    bartosz.sobczak_intel.com, kib
    MFC after:      2 weeks
    Sponsored by:   Intel Corporation
    Differential Revision:  https://reviews.freebsd.org/D60405
---
 sys/ofed/drivers/infiniband/core/ib_rdma_core.c | 30 +++++++++++++++++++------
 sys/ofed/drivers/infiniband/core/uverbs.h       |  1 +
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/sys/ofed/drivers/infiniband/core/ib_rdma_core.c b/sys/ofed/drivers/infiniband/core/ib_rdma_core.c
index 456c5df9a22f..645cb998bdac 100644
--- a/sys/ofed/drivers/infiniband/core/ib_rdma_core.c
+++ b/sys/ofed/drivers/infiniband/core/ib_rdma_core.c
@@ -295,13 +295,29 @@ static struct ib_uobject *alloc_uobj(struct uverbs_attr_bundle *attrs,
 
 static int idr_add_uobj(struct ib_uobject *uobj)
 {
-       /*
-        * We start with allocating an idr pointing to NULL. This represents an
-        * object which isn't initialized yet. We'll replace it later on with
-        * the real object once we commit.
-        */
-	return xa_alloc(&uobj->ufile->idr, &uobj->id, NULL, xa_limit_32b,
-			GFP_KERNEL);
+	struct ib_uverbs_file *ufile = uobj->ufile;
+	u32 id, start;
+	int ret;
+
+	xa_lock(&ufile->idr);
+	start = ufile->idr_next;
+	id = start;
+	do {
+		ret = __xa_insert(&ufile->idr, id, NULL, GFP_KERNEL);
+		if (ret != -EEXIST)
+			break;
+		id++;
+	} while (id != start);
+
+	if (!ret) {
+		uobj->id = id;
+		ufile->idr_next = id + 1;
+	} else if (ret == -EEXIST) {
+		ret = -ENOMEM;
+	}
+	xa_unlock(&ufile->idr);
+
+	return ret;
 }
 
 /* Returns the ib_uobject or an error. The caller should check for IS_ERR. */
diff --git a/sys/ofed/drivers/infiniband/core/uverbs.h b/sys/ofed/drivers/infiniband/core/uverbs.h
index 81b4a1a8210e..ed6b22fe99ab 100644
--- a/sys/ofed/drivers/infiniband/core/uverbs.h
+++ b/sys/ofed/drivers/infiniband/core/uverbs.h
@@ -163,6 +163,7 @@ struct ib_uverbs_file {
 	struct list_head umaps;
 
 	struct xarray		idr;
+	u32			idr_next;
 };
 
 struct ib_uverbs_event {