Skip to contentSkip to conversation

RDMA/efa: Add AH cache for AH reuse

PATCH Superseded Yonatan Nachum 4 participants 15 messages May 12 – Jun 3

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover RDMA/efa: Add AH cache for AH reuse

Changelog:
v2:
* Zero-initialize AH cache key on cache lookup.
v1: https://lore.kernel.org/all/[email protected]/

-------------------------------------------------------------------------
New EFA devices don't support the creation of multiple AHs to the same remote on the same PD. To overcome this limitation, introduce an AH cache that manages AH reuse transparently.

The cache uses an rhashtable keyed by (PD, GID) to track active address handles with refcounts. On create AH, the driver returns an existing AH number if one is already cached, or creates a new one and caches it. On destroy AH, the driver only issues the device destroy command when the last reference is dropped.

A per-entry mutex serializes concurrent device commands on the same cache entry, preventing create-before-destroy races on the device.

Yonatan Nachum (2):
RDMA/efa: Add initialization of AH cache rhashtable RDMA/efa: Add AH cache handling on create and destroy AH

 drivers/infiniband/hw/efa/Makefile       |   4 +-
 drivers/infiniband/hw/efa/efa_ah_cache.c | 154 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |  41 ++++++
 drivers/infiniband/hw/efa/efa_com.c      |  12 +-
 drivers/infiniband/hw/efa/efa_com.h      |   5 +-
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  27 ++++
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 8 files changed, 245 insertions(+), 8 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 1/2 RDMA/efa: Add initialization of AH cache rhashtable

New EFA devices don't support the creation of multiple address handles to the same remote on the same PD.
To overcome this limitation, introduce an AH cache rhashtable which will store the refcounts of the same AH creation on the same PD and will allow the driver to manage AH reuse. The hashtable key is the combination of PD and GID. Add initialization and teardown logic for the rhashtable.

---
 drivers/infiniband/hw/efa/Makefile       |  4 +--
 drivers/infiniband/hw/efa/efa_ah_cache.c | 30 ++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h | 36 ++++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_com.c      | 12 +++++++-
 drivers/infiniband/hw/efa/efa_com.h      |  5 +++-
 5 files changed, 83 insertions(+), 4 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h
drivers/infiniband/hw/efa/Makefile +2 -2
index 6e83083af0bc..a6a433b0ba2f 100644
@@ -1,9 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
-# Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All rights reserved.
+# Copyright 2018-2026 Amazon.com, Inc. or its affiliates. All rights reserved.
 #
 # Makefile for Amazon Elastic Fabric Adapter (EFA) device driver.
 #
 
 obj-$(CONFIG_INFINIBAND_EFA) += efa.o
 
-efa-y := efa_com_cmd.o efa_com.o efa_main.o efa_verbs.o
+efa-y := efa_com_cmd.o efa_ah_cache.o efa_com.o efa_main.o efa_verbs.o
drivers/infiniband/hw/efa/efa_ah_cache.c new file +30 -0
new file mode 100644
index 000000000000..d31871eb9748
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright 2026 Amazon.com, Inc. or its affiliates. All rights reserved.
+ */
+
+#include "efa_ah_cache.h"
+
+static const struct rhashtable_params ah_cache_params = {
+	.key_len = sizeof(struct efa_ah_cache_key),
+	.key_offset = offsetof(struct efa_ah_cache_entry, key),
+	.head_offset = offsetof(struct efa_ah_cache_entry, linkage),
+};
+
+int efa_ah_cache_init(struct efa_ah_cache *ah_cache)
+{
+	int err;
+
+	mutex_init(&ah_cache->lock);
+	err = rhashtable_init(&ah_cache->hashtable, &ah_cache_params);
+	if (err)
+		mutex_destroy(&ah_cache->lock);
+
+	return err;
+}
+
+void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache)
+{
+	rhashtable_destroy(&ah_cache->hashtable);
+	mutex_destroy(&ah_cache->lock);
+}
drivers/infiniband/hw/efa/efa_ah_cache.h new file +36 -0
new file mode 100644
index 000000000000..25288fdf778a
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/*
+ * Copyright 2026 Amazon.com, Inc. or its affiliates. All rights reserved.
+ */
+
+#ifndef _EFA_AH_CACHE_H_
+#define _EFA_AH_CACHE_H_
+
+#include <linux/refcount.h>
+#include <linux/rhashtable.h>
+
+#define EFA_AH_GID_SIZE 16
+
+struct efa_ah_cache_key {
+	u8 gid[EFA_AH_GID_SIZE];
+	u16 pd;
+};
+
+struct efa_ah_cache_entry {
+	struct efa_ah_cache_key key;
+	u16 ah;
+	bool initialized;
+	refcount_t refcount;
+	struct rhash_head linkage;
+	struct mutex lock; /* Serializes device commands per cache entry */
+};
+
+struct efa_ah_cache {
+	struct rhashtable hashtable;
+	struct mutex lock; /* Protects AH cache hashtable */
+};
+
+int efa_ah_cache_init(struct efa_ah_cache *ah_cache);
+void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache);
+
+#endif /* _EFA_AH_CACHE_H_ */
drivers/infiniband/hw/efa/efa_com.c +11 -1
index e97b5f0d7003..b9b922583709 100644
@@ -688,6 +688,8 @@ void efa_com_admin_destroy(struct efa_com_dev *edev)
 
 	size = aenq->depth * sizeof(*aenq->entries);
 	dma_free_coherent(edev->dmadev, size, aenq->entries, aenq->dma_addr);
+
+	efa_ah_cache_destroy(&edev->ah_cache);
 }
 
 /**
@@ -746,6 +748,12 @@ int efa_com_admin_init(struct efa_com_dev *edev,
 		return -ENODEV;
 	}
 
+	err = efa_ah_cache_init(&edev->ah_cache);
+	if (err) {
+		ibdev_err(edev->efa_dev, "Failed to init AH cache\n");
+		return err;
+	}
+
 	aq->depth = EFA_ADMIN_QUEUE_DEPTH;
 
 	aq->dmadev = edev->dmadev;
@@ -758,7 +766,7 @@ int efa_com_admin_init(struct efa_com_dev *edev,
 
 	err = efa_com_init_comp_ctxt(aq);
 	if (err)
-		return err;
+		goto err_destroy_ah_cache;
 
 	err = efa_com_admin_init_sq(edev);
 	if (err)
@@ -796,6 +804,8 @@ int efa_com_admin_init(struct efa_com_dev *edev,
 			  aq->sq.entries, aq->sq.dma_addr);
 err_destroy_comp_ctxt:
 	devm_kfree(edev->dmadev, aq->comp_ctx);
+err_destroy_ah_cache:
+	efa_ah_cache_destroy(&edev->ah_cache);
 
 	return err;
 }
drivers/infiniband/hw/efa/efa_com.h +4 -1
index 4d9ca97e4296..dc365df7f3dd 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
 /*
- * Copyright 2018-2025 Amazon.com, Inc. or its affiliates. All rights reserved.
+ * Copyright 2018-2026 Amazon.com, Inc. or its affiliates. All rights reserved.
  */
 
 #ifndef _EFA_COM_H_
@@ -14,6 +14,7 @@
 
 #include <rdma/ib_verbs.h>
 
+#include "efa_ah_cache.h"
 #include "efa_common_defs.h"
 #include "efa_admin_defs.h"
 #include "efa_admin_cmds_defs.h"
@@ -112,6 +113,8 @@ struct efa_com_dev {
 	u32 supported_features;
 	u32 dma_addr_bits;
 
+	struct efa_ah_cache ah_cache;
+
 	struct efa_com_mmio_read mmio_read;
 };
 
Signature · 1 line
2.50.1
#10 lore raw

PATCH v2 2/2 RDMA/efa: Add AH cache handling on create and destroy AH

In reply to Yonatan Nachum

On create AH, first check if the AH cache entry already exists and if so, returns the already stored AH number. If the entry doesn't exist, the driver creates it and calls the device to create the AH. A per-entry mutex serializes concurrent device commands on the same AH cache entry, ensuring only one thread issues the device create while others wait and reuse the result. If the device create fails, the entry remains uninitialized so subsequent threads calls can create the AH.

On destroy AH, the refcount is checked and if it's the last reference, the driver issues the device destroy command while holding the entry mutex. The entry remains in the hashtable during destroy to allow concurrent create threads to find it and wait on the entry mutex, preventing create-before-destroy races on the device. After the device destroy completes, the entry is either recycled if new users arrived or removed and freed.

---
 drivers/infiniband/hw/efa/efa_ah_cache.c | 124 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |   5 +
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  27 +++++
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 5 files changed, 162 insertions(+), 4 deletions(-)
drivers/infiniband/hw/efa/efa_ah_cache.c +124 -0
index d31871eb9748..689ab42b11e4 100644
@@ -3,6 +3,8 @@
  * Copyright 2026 Amazon.com, Inc. or its affiliates. All rights reserved.
  */
 
+#include <linux/slab.h>
+
 #include "efa_ah_cache.h"
 
 static const struct rhashtable_params ah_cache_params = {
@@ -28,3 +30,125 @@ void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache)
 	rhashtable_destroy(&ah_cache->hashtable);
 	mutex_destroy(&ah_cache->lock);
 }
+
+static struct efa_ah_cache_entry *efa_ah_cache_lookup(struct efa_ah_cache *ah_cache, u16 pd,
+						      u8 *gid)
+	__must_hold(&ah_cache->lock)
+{
+	struct efa_ah_cache_key key = {};
+
+	memcpy(key.gid, gid, sizeof(key.gid));
+	key.pd = pd;
+
+	return rhashtable_lookup_fast(&ah_cache->hashtable, &key, ah_cache_params);
+}
+
+/**
+ * efa_ah_cache_get_or_create - Get or create an AH cache entry
+ * @ah_cache: AH cache
+ * @pd: Protection domain number
+ * @gid: GID address
+ *
+ * Look up an AH cache entry by PD and GID. If found, increment the refcount and
+ * return it. If not found, allocate a new entry and insert it into the
+ * hashtable. The entry is returned unlocked.
+ *
+ * Return: Pointer to the entry on success, ERR_PTR on failure.
+ */
+struct efa_ah_cache_entry *efa_ah_cache_get_or_create(struct efa_ah_cache *ah_cache, u16 pd,
+						      u8 *gid)
+{
+	struct efa_ah_cache_entry *entry;
+	int err;
+
+	mutex_lock(&ah_cache->lock);
+
+	entry = efa_ah_cache_lookup(ah_cache, pd, gid);
+	if (entry) {
+		refcount_inc(&entry->refcount);
+		mutex_unlock(&ah_cache->lock);
+		return entry;
+	}
+
+	entry = kvzalloc(sizeof(*entry), GFP_KERNEL);
+	if (!entry) {
+		mutex_unlock(&ah_cache->lock);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	memcpy(entry->key.gid, gid, sizeof(entry->key.gid));
+	entry->key.pd = pd;
+	refcount_set(&entry->refcount, 1);
+	mutex_init(&entry->lock);
+
+	err = rhashtable_insert_fast(&ah_cache->hashtable, &entry->linkage, ah_cache_params);
+	if (err) {
+		mutex_destroy(&entry->lock);
+		kvfree(entry);
+		mutex_unlock(&ah_cache->lock);
+		return ERR_PTR(err);
+	}
+
+	mutex_unlock(&ah_cache->lock);
+	return entry;
+}
+
+/**
+ * efa_ah_cache_put_unless_last - Release a reference to an AH cache entry
+ * @ah_cache: AH cache
+ * @pd: Protection domain number
+ * @gid: GID address
+ *
+ * If this is not the last reference, decrement the refcount and return NULL.
+ * If this is the last reference, return the entry with its mutex locked
+ * without decrementing.
+ *
+ * Return: Pointer to the locked entry if last reference, NULL otherwise.
+ */
+struct efa_ah_cache_entry *efa_ah_cache_put_unless_last(struct efa_ah_cache *ah_cache, u16 pd,
+							u8 *gid)
+{
+	struct efa_ah_cache_entry *entry;
+
+	mutex_lock(&ah_cache->lock);
+	entry = efa_ah_cache_lookup(ah_cache, pd, gid);
+	if (!entry) {
+		mutex_unlock(&ah_cache->lock);
+		return NULL;
+	}
+
+	if (refcount_dec_not_one(&entry->refcount)) {
+		mutex_unlock(&ah_cache->lock);
+		return NULL;
+	}
+
+	mutex_lock(&entry->lock);
+	mutex_unlock(&ah_cache->lock);
+	return entry;
+}
+
+/**
+ * efa_ah_cache_put - Release the final reference to an AH cache entry
+ * @ah_cache: AH cache
+ * @entry: AH cache entry
+ *
+ * Decrement the refcount. If it reaches zero, the entry is removed from the
+ * hashtable and freed. Otherwise, the entry is kept for reuse.
+ *
+ * Called after the device destroy completes or on a failed create to release
+ * the caller's reference.
+ */
+void efa_ah_cache_put(struct efa_ah_cache *ah_cache, struct efa_ah_cache_entry *entry)
+{
+	mutex_lock(&ah_cache->lock);
+	if (!refcount_dec_and_test(&entry->refcount)) {
+		mutex_unlock(&ah_cache->lock);
+		return;
+	}
+
+	rhashtable_remove_fast(&ah_cache->hashtable, &entry->linkage, ah_cache_params);
+	mutex_unlock(&ah_cache->lock);
+
+	mutex_destroy(&entry->lock);
+	kvfree(entry);
+}
drivers/infiniband/hw/efa/efa_ah_cache.h +5 -0
index 25288fdf778a..9ddd9fedcf76 100644
@@ -32,5 +32,10 @@ struct efa_ah_cache {
 
 int efa_ah_cache_init(struct efa_ah_cache *ah_cache);
 void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache);
+struct efa_ah_cache_entry *efa_ah_cache_get_or_create(struct efa_ah_cache *ah_cache, u16 pd,
+						      u8 *gid);
+struct efa_ah_cache_entry *efa_ah_cache_put_unless_last(struct efa_ah_cache *ah_cache, u16 pd,
+							u8 *gid);
+void efa_ah_cache_put(struct efa_ah_cache *ah_cache, struct efa_ah_cache_entry *entry);
 
 #endif /* _EFA_AH_CACHE_H_ */
drivers/infiniband/hw/efa/efa_com_cmd.c +27 -0
index 63c7f07806a8..34a130fa92b5 100644
@@ -320,8 +320,20 @@ int efa_com_create_ah(struct efa_com_dev *edev,
 	struct efa_admin_create_ah_resp cmd_completion;
 	struct efa_com_admin_queue *aq = &edev->aq;
 	struct efa_admin_create_ah_cmd ah_cmd = {};
+	struct efa_ah_cache_entry *entry;
 	int err;
 
+	entry = efa_ah_cache_get_or_create(&edev->ah_cache, params->pdn, params->dest_addr);
+	if (IS_ERR(entry))
+		return PTR_ERR(entry);
+
+	mutex_lock(&entry->lock);
+	if (entry->initialized) {
+		result->ah = entry->ah;
+		mutex_unlock(&entry->lock);
+		return 0;
+	}
+
 	ah_cmd.aq_common_desc.opcode = EFA_ADMIN_CREATE_AH;
 
 	memcpy(ah_cmd.dest_addr, params->dest_addr, sizeof(ah_cmd.dest_addr));
@@ -333,13 +345,18 @@ int efa_com_create_ah(struct efa_com_dev *edev,
 			       (struct efa_admin_acq_entry *)&cmd_completion,
 			       sizeof(cmd_completion));
 	if (err) {
+		mutex_unlock(&entry->lock);
+		efa_ah_cache_put(&edev->ah_cache, entry);
 		ibdev_err_ratelimited(edev->efa_dev,
 				      "Failed to create ah for %pI6 [%d]\n",
 				      ah_cmd.dest_addr, err);
 		return err;
 	}
 
+	entry->ah = cmd_completion.ah;
+	entry->initialized = true;
 	result->ah = cmd_completion.ah;
+	mutex_unlock(&entry->lock);
 
 	return 0;
 }
@@ -350,8 +367,13 @@ int efa_com_destroy_ah(struct efa_com_dev *edev,
 	struct efa_admin_destroy_ah_resp cmd_completion;
 	struct efa_admin_destroy_ah_cmd ah_cmd = {};
 	struct efa_com_admin_queue *aq = &edev->aq;
+	struct efa_ah_cache_entry *entry;
 	int err;
 
+	entry = efa_ah_cache_put_unless_last(&edev->ah_cache, params->pdn, params->gid);
+	if (!entry)
+		return 0;
+
 	ah_cmd.aq_common_desc.opcode = EFA_ADMIN_DESTROY_AH;
 	ah_cmd.ah = params->ah;
 	ah_cmd.pd = params->pdn;
@@ -362,12 +384,17 @@ int efa_com_destroy_ah(struct efa_com_dev *edev,
 			       (struct efa_admin_acq_entry *)&cmd_completion,
 			       sizeof(cmd_completion));
 	if (err) {
+		mutex_unlock(&entry->lock);
 		ibdev_err_ratelimited(edev->efa_dev,
 				      "Failed to destroy ah-%d pd-%d [%d]\n",
 				      ah_cmd.ah, ah_cmd.pd, err);
 		return err;
 	}
 
+	entry->initialized = false;
+	mutex_unlock(&entry->lock);
+	efa_ah_cache_put(&edev->ah_cache, entry);
+
 	return 0;
 }
 
drivers/infiniband/hw/efa/efa_com_cmd.h +1 -0
index ef15b3c38429..39bd4e06684a 100644
@@ -106,6 +106,7 @@ struct efa_com_create_ah_result {
 
 struct efa_com_destroy_ah_params {
 	u16 ah;
+	u8 gid[EFA_GID_SIZE];
 	u16 pdn;
 };
 
drivers/infiniband/hw/efa/efa_verbs.c +5 -4
index 7bd0838ebc99..cc777392a112 100644
@@ -2055,10 +2055,11 @@ int efa_mmap(struct ib_ucontext *ibucontext,
 
 static int efa_ah_destroy(struct efa_dev *dev, struct efa_ah *ah)
 {
-	struct efa_com_destroy_ah_params params = {
-		.ah = ah->ah,
-		.pdn = to_epd(ah->ibah.pd)->pdn,
-	};
+	struct efa_com_destroy_ah_params params = {};
+
+	params.ah = ah->ah;
+	memcpy(params.gid, ah->id, sizeof(params.gid));
+	params.pdn = to_epd(ah->ibah.pd)->pdn;
 
 	return efa_com_destroy_ah(&dev->edev, &params);
 }
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH for-next v2 1/2] RDMA/efa: Add initialization of AH cache rhashtable

In reply to Yonatan Nachum
Quoted 86 lines · Yonatan Nachum

New EFA devices don't support the creation of multiple address handles to the same remote on the same PD.
To overcome this limitation, introduce an AH cache rhashtable which will store the refcounts of the same AH creation on the same PD and will allow the driver to manage AH reuse. The hashtable key is the combination of PD and GID. Add initialization and teardown logic for the rhashtable.

---
  drivers/infiniband/hw/efa/Makefile       |  4 +--
  drivers/infiniband/hw/efa/efa_ah_cache.c | 30 ++++++++++++++++++++
  drivers/infiniband/hw/efa/efa_ah_cache.h | 36 ++++++++++++++++++++++++
  drivers/infiniband/hw/efa/efa_com.c      | 12 +++++++-
  drivers/infiniband/hw/efa/efa_com.h      |  5 +++-
  5 files changed, 83 insertions(+), 4 deletions(-)

create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h

drivers/infiniband/hw/efa/Makefile +2 -2
index 6e83083af0bc..a6a433b0ba2f 100644
@@ -1,9 +1,9 @@
  # SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
-# Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All rights reserved.
+# Copyright 2018-2026 Amazon.com, Inc. or its affiliates. All rights reserved.
  #
  # Makefile for Amazon Elastic Fabric Adapter (EFA) device driver.
  #
 
  obj-$(CONFIG_INFINIBAND_EFA) += efa.o
 
-efa-y := efa_com_cmd.o efa_com.o efa_main.o efa_verbs.o
+efa-y := efa_com_cmd.o efa_ah_cache.o efa_com.o efa_main.o efa_verbs.o
drivers/infiniband/hw/efa/efa_ah_cache.c new file +30 -0
new file mode 100644
index 000000000000..d31871eb9748
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
+/*
+ * Copyright 2026 Amazon.com, Inc. or its affiliates. All rights reserved.
+ */
+
+#include "efa_ah_cache.h"
+
+static const struct rhashtable_params ah_cache_params = {
+	.key_len = sizeof(struct efa_ah_cache_key),
+	.key_offset = offsetof(struct efa_ah_cache_entry, key),
+	.head_offset = offsetof(struct efa_ah_cache_entry, linkage),
+};
+
+int efa_ah_cache_init(struct efa_ah_cache *ah_cache)
+{
+	int err;
+
+	mutex_init(&ah_cache->lock);
+	err = rhashtable_init(&ah_cache->hashtable, &ah_cache_params);
+	if (err)
+		mutex_destroy(&ah_cache->lock);
+
+	return err;
+}
+
+void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache)
+{
+	rhashtable_destroy(&ah_cache->hashtable);
+	mutex_destroy(&ah_cache->lock);
+}
drivers/infiniband/hw/efa/efa_ah_cache.h new file +7 -0
new file mode 100644
index 000000000000..25288fdf778a
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/*
+ * Copyright 2026 Amazon.com, Inc. or its affiliates. All rights reserved.
+ */
+
+#ifndef _EFA_AH_CACHE_H_
+#define _EFA_AH_CACHE_H_

drivers/infiniband/hw/efa/efa_ah_cache.h line 17

Quoted 10 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +10 -0
+
+#include <linux/refcount.h>
+#include <linux/rhashtable.h>
+
+#define EFA_AH_GID_SIZE 16
+
+struct efa_ah_cache_key {
+	u8 gid[EFA_AH_GID_SIZE];
+	u16 pd;
+};

I am not sure if we add __packed to avoid memory hole.

Zhu Yanjun

Quoted 92 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +19 -0
+
+struct efa_ah_cache_entry {
+	struct efa_ah_cache_key key;
+	u16 ah;
+	bool initialized;
+	refcount_t refcount;
+	struct rhash_head linkage;
+	struct mutex lock; /* Serializes device commands per cache entry */
+};
+
+struct efa_ah_cache {
+	struct rhashtable hashtable;
+	struct mutex lock; /* Protects AH cache hashtable */
+};
+
+int efa_ah_cache_init(struct efa_ah_cache *ah_cache);
+void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache);
+
+#endif /* _EFA_AH_CACHE_H_ */
drivers/infiniband/hw/efa/efa_com.c +11 -1
index e97b5f0d7003..b9b922583709 100644
@@ -688,6 +688,8 @@ void efa_com_admin_destroy(struct efa_com_dev *edev)
 
  	size = aenq->depth * sizeof(*aenq->entries);
  	dma_free_coherent(edev->dmadev, size, aenq->entries, aenq->dma_addr);
+
+	efa_ah_cache_destroy(&edev->ah_cache);
  }
 
  /**
@@ -746,6 +748,12 @@ int efa_com_admin_init(struct efa_com_dev *edev,
  		return -ENODEV;
  	}
 
+	err = efa_ah_cache_init(&edev->ah_cache);
+	if (err) {
+		ibdev_err(edev->efa_dev, "Failed to init AH cache\n");
+		return err;
+	}
+
  	aq->depth = EFA_ADMIN_QUEUE_DEPTH;
 
  	aq->dmadev = edev->dmadev;
@@ -758,7 +766,7 @@ int efa_com_admin_init(struct efa_com_dev *edev,
 
  	err = efa_com_init_comp_ctxt(aq);
  	if (err)
-		return err;
+		goto err_destroy_ah_cache;
 
  	err = efa_com_admin_init_sq(edev);
  	if (err)
@@ -796,6 +804,8 @@ int efa_com_admin_init(struct efa_com_dev *edev,
  			  aq->sq.entries, aq->sq.dma_addr);
  err_destroy_comp_ctxt:
  	devm_kfree(edev->dmadev, aq->comp_ctx);
+err_destroy_ah_cache:
+	efa_ah_cache_destroy(&edev->ah_cache);
 
  	return err;
  }
drivers/infiniband/hw/efa/efa_com.h +4 -1
index 4d9ca97e4296..dc365df7f3dd 100644
@@ -1,6 +1,6 @@
  /* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
  /*
- * Copyright 2018-2025 Amazon.com, Inc. or its affiliates. All rights reserved.
+ * Copyright 2018-2026 Amazon.com, Inc. or its affiliates. All rights reserved.
   */
 
  #ifndef _EFA_COM_H_
@@ -14,6 +14,7 @@
 
  #include <rdma/ib_verbs.h>
 
+#include "efa_ah_cache.h"
  #include "efa_common_defs.h"
  #include "efa_admin_defs.h"
  #include "efa_admin_cmds_defs.h"
@@ -112,6 +113,8 @@ struct efa_com_dev {
  	u32 supported_features;
  	u32 dma_addr_bits;
 
+	struct efa_ah_cache ah_cache;
+
  	struct efa_com_mmio_read mmio_read;
  };
 
#3 lore raw

Re: [PATCH for-next v2 1/2] RDMA/efa: Add initialization of AH cache rhashtable

Quoted 48 lines · Zhu Yanjun
Quoted 34 lines · Yonatan Nachum

New EFA devices don't support the creation of multiple address handles to the same remote on the same PD.
To overcome this limitation, introduce an AH cache rhashtable which will store the refcounts of the same AH creation on the same PD and will allow the driver to manage AH reuse. The hashtable key is the combination of PD and GID. Add initialization and teardown logic for the rhashtable.

---
 drivers/infiniband/hw/efa/Makefile       |  4 +--
 drivers/infiniband/hw/efa/efa_ah_cache.c | 30 ++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h | 36 ++++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_com.c      | 12 +++++++-
 drivers/infiniband/hw/efa/efa_com.h      |  5 +++-
 5 files changed, 83 insertions(+), 4 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h
drivers/infiniband/hw/efa/efa_ah_cache.h new file +7 -0
new file mode 100644
index 000000000000..25288fdf778a
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/*
+ * Copyright 2026 Amazon.com, Inc. or its affiliates. All rights reserved.
+ */
+
+#ifndef _EFA_AH_CACHE_H_
+#define _EFA_AH_CACHE_H_

drivers/infiniband/hw/efa/efa_ah_cache.h line 17

Quoted 10 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +10 -0
+
+#include <linux/refcount.h>
+#include <linux/rhashtable.h>
+
+#define EFA_AH_GID_SIZE 16
+
+struct efa_ah_cache_key {
+	u8 gid[EFA_AH_GID_SIZE];
+	u16 pd;
+};

I am not sure if we add __packed to avoid memory hole.

Quoted 1 line

Zhu Yanjun

Currently there is no holes in the struct (verified using pahole) and we Zero-initialize AH cache key so I don't think packed is really needed.

Thanks

#4 lore raw

Re: [PATCH for-next v2 1/2] RDMA/efa: Add initialization of AH cache rhashtable

Quoted 48 lines · Yonatan Nachum
Quoted 47 lines · Zhu Yanjun
Quoted 34 lines · Yonatan Nachum

New EFA devices don't support the creation of multiple address handles to the same remote on the same PD.
To overcome this limitation, introduce an AH cache rhashtable which will store the refcounts of the same AH creation on the same PD and will allow the driver to manage AH reuse. The hashtable key is the combination of PD and GID. Add initialization and teardown logic for the rhashtable.

---
  drivers/infiniband/hw/efa/Makefile       |  4 +--
  drivers/infiniband/hw/efa/efa_ah_cache.c | 30 ++++++++++++++++++++
  drivers/infiniband/hw/efa/efa_ah_cache.h | 36 ++++++++++++++++++++++++
  drivers/infiniband/hw/efa/efa_com.c      | 12 +++++++-
  drivers/infiniband/hw/efa/efa_com.h      |  5 +++-
  5 files changed, 83 insertions(+), 4 deletions(-)

create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h

drivers/infiniband/hw/efa/efa_ah_cache.h new file +7 -0
new file mode 100644
index 000000000000..25288fdf778a
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/*
+ * Copyright 2026 Amazon.com, Inc. or its affiliates. All rights reserved.
+ */
+
+#ifndef _EFA_AH_CACHE_H_
+#define _EFA_AH_CACHE_H_

drivers/infiniband/hw/efa/efa_ah_cache.h line 17

Quoted 10 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +10 -0
+
+#include <linux/refcount.h>
+#include <linux/rhashtable.h>
+
+#define EFA_AH_GID_SIZE 16
+
+struct efa_ah_cache_key {
+	u8 gid[EFA_AH_GID_SIZE];
+	u16 pd;
+};

I am not sure if we add __packed to avoid memory hole.

Quoted 3 lines
Quoted 1 line

Zhu Yanjun

Currently there is no holes in the struct (verified using pahole) and we Zero-initialize AH cache key so I don't think packed is really needed.

I’m wondering whether you’ve tested this on all architectures.

Explicitly using __packed or ensuring proper memory alignment

would likely be a better solution to avoid memory holes in the key structure.

Zhu Yanjun

Quoted 2 lines

Thanks

Signature · 2 lines
Best Regards,
Yanjun.Zhu
#5 lore raw

Re: [PATCH for-next v2 1/2] RDMA/efa: Add initialization of AH cache rhashtable

Quoted 58 lines · Zhu Yanjun
Quoted 48 lines · Yonatan Nachum
Quoted 47 lines · Zhu Yanjun
Quoted 34 lines · Yonatan Nachum

New EFA devices don't support the creation of multiple address handles to the same remote on the same PD.
To overcome this limitation, introduce an AH cache rhashtable which will store the refcounts of the same AH creation on the same PD and will allow the driver to manage AH reuse. The hashtable key is the combination of PD and GID. Add initialization and teardown logic for the rhashtable.

---
 drivers/infiniband/hw/efa/Makefile       |  4 +--
 drivers/infiniband/hw/efa/efa_ah_cache.c | 30 ++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h | 36 ++++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_com.c      | 12 +++++++-
 drivers/infiniband/hw/efa/efa_com.h      |  5 +++-
 5 files changed, 83 insertions(+), 4 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h
drivers/infiniband/hw/efa/efa_ah_cache.h new file +7 -0
new file mode 100644
index 000000000000..25288fdf778a
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
+/*
+ * Copyright 2026 Amazon.com, Inc. or its affiliates. All rights reserved.
+ */
+
+#ifndef _EFA_AH_CACHE_H_
+#define _EFA_AH_CACHE_H_

drivers/infiniband/hw/efa/efa_ah_cache.h line 17

Quoted 10 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +10 -0
+
+#include <linux/refcount.h>
+#include <linux/rhashtable.h>
+
+#define EFA_AH_GID_SIZE 16
+
+struct efa_ah_cache_key {
+	u8 gid[EFA_AH_GID_SIZE];
+	u16 pd;
+};

I am not sure if we add __packed to avoid memory hole.

Quoted 3 lines
Quoted 1 line

Zhu Yanjun

Currently there is no holes in the struct (verified using pahole) and we Zero-initialize AH cache key so I don't think packed is really needed.

I’m wondering whether you’ve tested this on all architectures.

Explicitly using __packed or ensuring proper memory alignment

Quoted 11 lines

would likely be a better solution to avoid memory holes in the key structure.

Zhu Yanjun

Quoted 1 line

Thanks

Signature · 2 lines
Best Regards,
Yanjun.Zhu

It was tested on x86 and arm which are the supported architectures for EFA. Current implementation handles holes and I prefer to not add packed annotation.

Thanks

#6 lore raw

Re: [PATCH for-next v2 1/2] RDMA/efa: Add initialization of AH cache rhashtable

In reply to Yonatan Nachum
Quoted 20 lines · Yonatan Nachum

New EFA devices don't support the creation of multiple address handles to the same remote on the same PD.
To overcome this limitation, introduce an AH cache rhashtable which will store the refcounts of the same AH creation on the same PD and will allow the driver to manage AH reuse. The hashtable key is the combination of PD and GID. Add initialization and teardown logic for the rhashtable.

---
 drivers/infiniband/hw/efa/Makefile       |  4 +--
 drivers/infiniband/hw/efa/efa_ah_cache.c | 30 ++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h | 36 ++++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_com.c      | 12 +++++++-
 drivers/infiniband/hw/efa/efa_com.h      |  5 +++-
 5 files changed, 83 insertions(+), 4 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h

<...>

drivers/infiniband/hw/efa/efa_ah_cache.h line 22

Quoted 4 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +4 -0
+struct efa_ah_cache_entry {
+	struct efa_ah_cache_key key;
+	u16 ah;
+	bool initialized;

If this variable is present, it indicates that the reference count is being used incorrectly. In the uninitialized state, the refcount must be 0.

Thanks

Quoted 90 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +14 -0
+	refcount_t refcount;
+	struct rhash_head linkage;
+	struct mutex lock; /* Serializes device commands per cache entry */
+};
+
+struct efa_ah_cache {
+	struct rhashtable hashtable;
+	struct mutex lock; /* Protects AH cache hashtable */
+};
+
+int efa_ah_cache_init(struct efa_ah_cache *ah_cache);
+void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache);
+
+#endif /* _EFA_AH_CACHE_H_ */
drivers/infiniband/hw/efa/efa_com.c +11 -1
index e97b5f0d7003..b9b922583709 100644
@@ -688,6 +688,8 @@ void efa_com_admin_destroy(struct efa_com_dev *edev)
 
 	size = aenq->depth * sizeof(*aenq->entries);
 	dma_free_coherent(edev->dmadev, size, aenq->entries, aenq->dma_addr);
+
+	efa_ah_cache_destroy(&edev->ah_cache);
 }
 
 /**
@@ -746,6 +748,12 @@ int efa_com_admin_init(struct efa_com_dev *edev,
 		return -ENODEV;
 	}
 
+	err = efa_ah_cache_init(&edev->ah_cache);
+	if (err) {
+		ibdev_err(edev->efa_dev, "Failed to init AH cache\n");
+		return err;
+	}
+
 	aq->depth = EFA_ADMIN_QUEUE_DEPTH;
 
 	aq->dmadev = edev->dmadev;
@@ -758,7 +766,7 @@ int efa_com_admin_init(struct efa_com_dev *edev,
 
 	err = efa_com_init_comp_ctxt(aq);
 	if (err)
-		return err;
+		goto err_destroy_ah_cache;
 
 	err = efa_com_admin_init_sq(edev);
 	if (err)
@@ -796,6 +804,8 @@ int efa_com_admin_init(struct efa_com_dev *edev,
 			  aq->sq.entries, aq->sq.dma_addr);
 err_destroy_comp_ctxt:
 	devm_kfree(edev->dmadev, aq->comp_ctx);
+err_destroy_ah_cache:
+	efa_ah_cache_destroy(&edev->ah_cache);
 
 	return err;
 }
drivers/infiniband/hw/efa/efa_com.h +4 -1
index 4d9ca97e4296..dc365df7f3dd 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */
 /*
- * Copyright 2018-2025 Amazon.com, Inc. or its affiliates. All rights reserved.
+ * Copyright 2018-2026 Amazon.com, Inc. or its affiliates. All rights reserved.
  */
 
 #ifndef _EFA_COM_H_
@@ -14,6 +14,7 @@
 
 #include <rdma/ib_verbs.h>
 
+#include "efa_ah_cache.h"
 #include "efa_common_defs.h"
 #include "efa_admin_defs.h"
 #include "efa_admin_cmds_defs.h"
@@ -112,6 +113,8 @@ struct efa_com_dev {
 	u32 supported_features;
 	u32 dma_addr_bits;
 
+	struct efa_ah_cache ah_cache;
+
 	struct efa_com_mmio_read mmio_read;
 };
 
Signature · 1 line
2.50.1
#11 lore raw

Re: [PATCH for-next v2 2/2] RDMA/efa: Add AH cache handling on create and destroy AH

In reply to Yonatan Nachum
Quoted 26 lines · Yonatan Nachum

On create AH, first check if the AH cache entry already exists and if so, returns the already stored AH number. If the entry doesn't exist, the driver creates it and calls the device to create the AH. A per-entry mutex serializes concurrent device commands on the same AH cache entry, ensuring only one thread issues the device create while others wait and reuse the result. If the device create fails, the entry remains uninitialized so subsequent threads calls can create the AH.

On destroy AH, the refcount is checked and if it's the last reference, the driver issues the device destroy command while holding the entry mutex. The entry remains in the hashtable during destroy to allow concurrent create threads to find it and wait on the entry mutex, preventing create-before-destroy races on the device. After the device destroy completes, the entry is either recycled if new users arrived or removed and freed.

---
 drivers/infiniband/hw/efa/efa_ah_cache.c | 124 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |   5 +
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  27 +++++
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 5 files changed, 162 insertions(+), 4 deletions(-)

<...>

Quoted 16 lines
drivers/infiniband/hw/efa/efa_ah_cache.c +16 -0
+
+/**
+ * efa_ah_cache_put - Release the final reference to an AH cache entry
+ * @ah_cache: AH cache
+ * @entry: AH cache entry
+ *
+ * Decrement the refcount. If it reaches zero, the entry is removed from the
+ * hashtable and freed. Otherwise, the entry is kept for reuse.
+ *
+ * Called after the device destroy completes or on a failed create to release
+ * the caller's reference.
+ */
+void efa_ah_cache_put(struct efa_ah_cache *ah_cache, struct efa_ah_cache_entry *entry)
+{
+	mutex_lock(&ah_cache->lock);
+	if (!refcount_dec_and_test(&entry->refcount)) {

drivers/infiniband/hw/efa/efa_ah_cache.c void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache) · line 154

Quoted 10 lines
drivers/infiniband/hw/efa/efa_ah_cache.c +10 -0
+		mutex_unlock(&ah_cache->lock);
+		return;
+	}
+
+	rhashtable_remove_fast(&ah_cache->hashtable, &entry->linkage, ah_cache_params);
+	mutex_unlock(&ah_cache->lock);
+
+	mutex_destroy(&entry->lock);
+	kvfree(entry);
+}

This pattern looks very similar to kref_put_mutex().

Thanks

#7 lore raw

Re: [PATCH for-next v2 1/2] RDMA/efa: Add initialization of AH cache rhashtable

In reply to Leon Romanovsky
Quoted 32 lines · Leon Romanovsky
Quoted 20 lines · Yonatan Nachum

New EFA devices don't support the creation of multiple address handles to the same remote on the same PD.
To overcome this limitation, introduce an AH cache rhashtable which will store the refcounts of the same AH creation on the same PD and will allow the driver to manage AH reuse. The hashtable key is the combination of PD and GID. Add initialization and teardown logic for the rhashtable.

---
 drivers/infiniband/hw/efa/Makefile       |  4 +--
 drivers/infiniband/hw/efa/efa_ah_cache.c | 30 ++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h | 36 ++++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_com.c      | 12 +++++++-
 drivers/infiniband/hw/efa/efa_com.h      |  5 +++-
 5 files changed, 83 insertions(+), 4 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h

<...>

drivers/infiniband/hw/efa/efa_ah_cache.h line 22

Quoted 4 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +4 -0
+struct efa_ah_cache_entry {
+	struct efa_ah_cache_key key;
+	u16 ah;
+	bool initialized;

If this variable is present, it indicates that the reference count is being used incorrectly. In the uninitialized state, the refcount must be 0.

Quoted 1 line

Thanks

The refcount and initialized track two separate properties of the entry:

- refcount represents the number of active users that hold a reference to this cache entry. It is incremented on every create_ah call and decremented on every destroy_ah call for the same (PD, GID).

- initialized indicates whether the entry holds a valid AH number from the device. An entry exists in the hashtable and can be referenced before the device command completes, and also after a successful device destroy (when the entry is recycled for a new create).

These two properties are independent — an entry can be referenced by multiple users while not yet holding a valid AH, and conversely an entry can transition from initialized to uninitialized while still being referenced.

Using refcount=0 to represent the uninitialized state would conflate entry lifetime tracking with device state.

Thanks

#12 lore raw

Re: [PATCH for-next v2 2/2] RDMA/efa: Add AH cache handling on create and destroy AH

In reply to Leon Romanovsky
Quoted 59 lines · Leon Romanovsky
Quoted 26 lines · Yonatan Nachum

On create AH, first check if the AH cache entry already exists and if so, returns the already stored AH number. If the entry doesn't exist, the driver creates it and calls the device to create the AH. A per-entry mutex serializes concurrent device commands on the same AH cache entry, ensuring only one thread issues the device create while others wait and reuse the result. If the device create fails, the entry remains uninitialized so subsequent threads calls can create the AH.

On destroy AH, the refcount is checked and if it's the last reference, the driver issues the device destroy command while holding the entry mutex. The entry remains in the hashtable during destroy to allow concurrent create threads to find it and wait on the entry mutex, preventing create-before-destroy races on the device. After the device destroy completes, the entry is either recycled if new users arrived or removed and freed.

---
 drivers/infiniband/hw/efa/efa_ah_cache.c | 124 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |   5 +
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  27 +++++
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 5 files changed, 162 insertions(+), 4 deletions(-)

<...>

Quoted 16 lines
drivers/infiniband/hw/efa/efa_ah_cache.c +16 -0
+
+/**
+ * efa_ah_cache_put - Release the final reference to an AH cache entry
+ * @ah_cache: AH cache
+ * @entry: AH cache entry
+ *
+ * Decrement the refcount. If it reaches zero, the entry is removed from the
+ * hashtable and freed. Otherwise, the entry is kept for reuse.
+ *
+ * Called after the device destroy completes or on a failed create to release
+ * the caller's reference.
+ */
+void efa_ah_cache_put(struct efa_ah_cache *ah_cache, struct efa_ah_cache_entry *entry)
+{
+	mutex_lock(&ah_cache->lock);
+	if (!refcount_dec_and_test(&entry->refcount)) {

drivers/infiniband/hw/efa/efa_ah_cache.c void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache) · line 154

Quoted 10 lines
drivers/infiniband/hw/efa/efa_ah_cache.c +10 -0
+		mutex_unlock(&ah_cache->lock);
+		return;
+	}
+
+	rhashtable_remove_fast(&ah_cache->hashtable, &entry->linkage, ah_cache_params);
+	mutex_unlock(&ah_cache->lock);
+
+	mutex_destroy(&entry->lock);
+	kvfree(entry);
+}

This pattern looks very similar to kref_put_mutex().

Quoted 1 line

Thanks

We don't follow the kref put semantics here. On last AH put, we don't decrement the refcount and delete the entry. Instead we keep the entry in the hash and only do the last decrement after device destroy completes. This solves races where AH create starts while a device destroy is in progress — by keeping the entry, concurrent create threads find it and block on the per-entry mutex until the destroy completes, ensuring a device create is initiated only after.

Thanks

#8 lore raw

Re: [PATCH for-next v2 1/2] RDMA/efa: Add initialization of AH cache rhashtable

In reply to Leon Romanovsky
Quoted 32 lines · Leon Romanovsky
Quoted 20 lines · Yonatan Nachum

New EFA devices don't support the creation of multiple address handles to the same remote on the same PD.
To overcome this limitation, introduce an AH cache rhashtable which will store the refcounts of the same AH creation on the same PD and will allow the driver to manage AH reuse. The hashtable key is the combination of PD and GID. Add initialization and teardown logic for the rhashtable.

---
 drivers/infiniband/hw/efa/Makefile       |  4 +--
 drivers/infiniband/hw/efa/efa_ah_cache.c | 30 ++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h | 36 ++++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_com.c      | 12 +++++++-
 drivers/infiniband/hw/efa/efa_com.h      |  5 +++-
 5 files changed, 83 insertions(+), 4 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h

<...>

drivers/infiniband/hw/efa/efa_ah_cache.h line 22

Quoted 4 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +4 -0
+struct efa_ah_cache_entry {
+	struct efa_ah_cache_key key;
+	u16 ah;
+	bool initialized;

If this variable is present, it indicates that the reference count is being used incorrectly. In the uninitialized state, the refcount must be 0.

Quoted 1 line

Thanks

The refcount and initialized track two separate properties of the entry:
- refcount represents the number of active users that hold a reference the this cache entry. It is incremented on every create AH call and decremented on every destroy AH call for the same PD, GID.

- initialized indicates whether the entry holds a valid AH number from the device. An entry exists in the hashtable and can referenced before the device command completes, and also after a successful device destroy (when the entry is recycled fir a new create).

This two properties are independent - an entry can be referenced by multiple users while not yet holding a valid AH, and an entry can transition from initialized to uninitialized while still being referenced.

Using refcount=0 to mean 'not yet created on device' would mix two unrelated things into one variable.

#13 lore raw

Re: [PATCH for-next v2 2/2] RDMA/efa: Add AH cache handling on create and destroy AH

In reply to Leon Romanovsky
Quoted 59 lines · Leon Romanovsky
Quoted 26 lines · Yonatan Nachum

On create AH, first check if the AH cache entry already exists and if so, returns the already stored AH number. If the entry doesn't exist, the driver creates it and calls the device to create the AH. A per-entry mutex serializes concurrent device commands on the same AH cache entry, ensuring only one thread issues the device create while others wait and reuse the result. If the device create fails, the entry remains uninitialized so subsequent threads calls can create the AH.

On destroy AH, the refcount is checked and if it's the last reference, the driver issues the device destroy command while holding the entry mutex. The entry remains in the hashtable during destroy to allow concurrent create threads to find it and wait on the entry mutex, preventing create-before-destroy races on the device. After the device destroy completes, the entry is either recycled if new users arrived or removed and freed.

---
 drivers/infiniband/hw/efa/efa_ah_cache.c | 124 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |   5 +
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  27 +++++
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 5 files changed, 162 insertions(+), 4 deletions(-)

<...>

Quoted 16 lines
drivers/infiniband/hw/efa/efa_ah_cache.c +16 -0
+
+/**
+ * efa_ah_cache_put - Release the final reference to an AH cache entry
+ * @ah_cache: AH cache
+ * @entry: AH cache entry
+ *
+ * Decrement the refcount. If it reaches zero, the entry is removed from the
+ * hashtable and freed. Otherwise, the entry is kept for reuse.
+ *
+ * Called after the device destroy completes or on a failed create to release
+ * the caller's reference.
+ */
+void efa_ah_cache_put(struct efa_ah_cache *ah_cache, struct efa_ah_cache_entry *entry)
+{
+	mutex_lock(&ah_cache->lock);
+	if (!refcount_dec_and_test(&entry->refcount)) {

drivers/infiniband/hw/efa/efa_ah_cache.c void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache) · line 154

Quoted 10 lines
drivers/infiniband/hw/efa/efa_ah_cache.c +10 -0
+		mutex_unlock(&ah_cache->lock);
+		return;
+	}
+
+	rhashtable_remove_fast(&ah_cache->hashtable, &entry->linkage, ah_cache_params);
+	mutex_unlock(&ah_cache->lock);
+
+	mutex_destroy(&entry->lock);
+	kvfree(entry);
+}

This pattern looks very similar to kref_put_mutex().

Quoted 1 line

Thanks

We don't follow the kref put semantics here. On last AH put, we don't decrement the refcount and delete the entry. Instead we keep the entry in the hash and only do the last decrement after device destroy completes. This solve s the races where AH create stars while a device destroy is in progress - by keeping the entry, concurrent create threads find it and block on the per-entry mutex until destroy completes, ensuring a device create in initiated only after.

Thanks

#9 lore raw

Re: [PATCH for-next v2 1/2] RDMA/efa: Add initialization of AH cache rhashtable

In reply to Yonatan Nachum
Quoted 45 lines · Yonatan Nachum
Quoted 32 lines · Leon Romanovsky
Quoted 20 lines · Yonatan Nachum

New EFA devices don't support the creation of multiple address handles to the same remote on the same PD.
To overcome this limitation, introduce an AH cache rhashtable which will store the refcounts of the same AH creation on the same PD and will allow the driver to manage AH reuse. The hashtable key is the combination of PD and GID. Add initialization and teardown logic for the rhashtable.

---
 drivers/infiniband/hw/efa/Makefile       |  4 +--
 drivers/infiniband/hw/efa/efa_ah_cache.c | 30 ++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h | 36 ++++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_com.c      | 12 +++++++-
 drivers/infiniband/hw/efa/efa_com.h      |  5 +++-
 5 files changed, 83 insertions(+), 4 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h

<...>

drivers/infiniband/hw/efa/efa_ah_cache.h line 22

Quoted 4 lines
drivers/infiniband/hw/efa/efa_ah_cache.h new file +4 -0
+struct efa_ah_cache_entry {
+	struct efa_ah_cache_key key;
+	u16 ah;
+	bool initialized;

If this variable is present, it indicates that the reference count is being used incorrectly. In the uninitialized state, the refcount must be 0.

Quoted 1 line

Thanks

The refcount and initialized track two separate properties of the entry:
- refcount represents the number of active users that hold a reference the this cache entry. It is incremented on every create AH call and decremented on every destroy AH call for the same PD, GID.

- initialized indicates whether the entry holds a valid AH number from the device. An entry exists in the hashtable and can referenced before the device command completes, and also after a successful device destroy (when the entry is recycled fir a new create).

Quoted 7 lines

This two properties are independent - an entry can be referenced by multiple users while not yet holding a valid AH, and an entry can transition from initialized to uninitialized while still being referenced.

Using refcount=0 to mean 'not yet created on device' would mix two unrelated things into one variable.

Hi, kind reminder

Thanks

maintainer
#14 lore raw
In reply to Yonatan Nachum
Quoted 23 lines · Yonatan Nachum

Changelog:
v2:
* Zero-initialize AH cache key on cache lookup.
v1: https://lore.kernel.org/all/[email protected]/

-------------------------------------------------------------------------
New EFA devices don't support the creation of multiple AHs to the same remote on the same PD. To overcome this limitation, introduce an AH cache that manages AH reuse transparently.

The cache uses an rhashtable keyed by (PD, GID) to track active address handles with refcounts. On create AH, the driver returns an existing AH number if one is already cached, or creates a new one and caches it. On destroy AH, the driver only issues the device destroy command when the last reference is dropped.

A per-entry mutex serializes concurrent device commands on the same cache entry, preventing create-before-destroy races on the device.

Yonatan Nachum (2):
RDMA/efa: Add initialization of AH cache rhashtable RDMA/efa: Add AH cache handling on create and destroy AH

Quoted 11 lines
 drivers/infiniband/hw/efa/Makefile       |   4 +-
 drivers/infiniband/hw/efa/efa_ah_cache.c | 154 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |  41 ++++++
 drivers/infiniband/hw/efa/efa_com.c      |  12 +-
 drivers/infiniband/hw/efa/efa_com.h      |   5 +-
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  27 ++++
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 8 files changed, 245 insertions(+), 8 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h

The sashiko comments seem quite serious, please fix some of them up..

https://sashiko.dev/#/patchset/20260512061121.2177521-1-ynachum%40amazon.com

I would not do the GFP_ACCOUNT suggestion, we don't do that in RDMA (though perhaps we should)

The races look right, the mis-use of the API looks right, and IDK about the leak on cache_destroy, either clean it up or stick a WARN_ON that it must be empty.

Jason