Skip to contentSkip to conversation

RDMA/efa: Add AH cache for AH reuse

PATCH Superseded Yonatan Nachum 4 participants 9 messages Jun 8 – Jun 17

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

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

Changelog:
v4:
* Use kzalloc_obj for AH cache entry allocation instead of kzalloc
v3: https://lore.kernel.org/all/[email protected]/
* Address Sashiko comments in:
https://sashiko.dev/#/patchset/20260512061121.2177521-1-ynachum%40amazon.com
v2: https://lore.kernel.org/all/[email protected]/
* 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 | 163 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |  42 ++++++
 drivers/infiniband/hw/efa/efa_com.c      |  12 +-
 drivers/infiniband/hw/efa/efa_com.h      |   3 +
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  73 +++++++---
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 8 files changed, 281 insertions(+), 26 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 v4 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 | 41 ++++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h | 37 +++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_com.c      | 12 ++++++-
 drivers/infiniband/hw/efa/efa_com.h      |  3 ++
 5 files changed, 94 insertions(+), 3 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 +41 -0

41 lines not on this page · raw message

drivers/infiniband/hw/efa/efa_ah_cache.h new file +37 -0
new file mode 100644
index 000000000000..133181b4466d
@@ -0,0 +1,37 @@
+/* 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 rcu_head rcu_head;
+	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 7cc3f4af0bb9..7097d1c2f23d 100644
@@ -724,6 +724,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);
 }
 
 /**
@@ -782,6 +784,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;
@@ -794,7 +802,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)
@@ -832,6 +840,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 +3 -0
index f8c692b0e092..599db9d583bf 100644
@@ -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"
@@ -113,6 +114,8 @@ struct efa_com_dev {
 	u32 supported_features;
 	u32 dma_addr_bits;
 
+	struct efa_ah_cache ah_cache;
+
 	u32 dev_api_ver;
 	struct efa_com_mmio_read mmio_read;
 };
Signature · 1 line
2.50.1
#2 lore raw

PATCH v4 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 | 122 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |   5 +
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  73 ++++++++++----
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 5 files changed, 187 insertions(+), 23 deletions(-)
drivers/infiniband/hw/efa/efa_ah_cache.c +122 -0
index ab763b06b9bb..b738261dca35 100644
@@ -39,3 +39,125 @@ void efa_ah_cache_destroy(struct efa_ah_cache *ah_cache)
 	rhashtable_free_and_destroy(&ah_cache->hashtable, efa_ah_cache_entry_free, NULL);
 	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 = kzalloc_obj(*entry);
+	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);
+		kfree(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);
+	kfree_rcu(entry, rcu_head);
+}
drivers/infiniband/hw/efa/efa_ah_cache.h +5 -0
index 133181b4466d..573fd29bb416 100644
@@ -33,5 +33,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 +54 -19

112 lines not on this page · raw message

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 434d60235945..6742a4037888 100644
@@ -2032,10 +2032,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
#3 lore raw
In reply to Yonatan Nachum
Quoted 28 lines · Yonatan Nachum

Changelog:
v4:
* Use kzalloc_obj for AH cache entry allocation instead of kzalloc
v3: https://lore.kernel.org/all/[email protected]/
* Address Sashiko comments in:

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

v2: https://lore.kernel.org/all/[email protected]/
* 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 15 lines
 drivers/infiniband/hw/efa/Makefile       |   4 +-
 drivers/infiniband/hw/efa/efa_ah_cache.c | 163 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |  42 ++++++
 drivers/infiniband/hw/efa/efa_com.c      |  12 +-
 drivers/infiniband/hw/efa/efa_com.h      |   3 +
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  73 +++++++---
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 8 files changed, 281 insertions(+), 26 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

Hi, kind reminder.
This series blocks merging EFAv4 device ID and we would want it to land for the next merge window if possible.

I reviewed the last Sashiko comment and it's not relevant since on destroy AH failure, we keep the entry in the hashtable.

Thanks

#4 lore raw
Quoted 41 lines · Yonatan Nachum
Quoted 40 lines · Yonatan Nachum

Changelog:
v4:
* Use kzalloc_obj for AH cache entry allocation instead of kzalloc
v3: https://lore.kernel.org/all/[email protected]/
* Address Sashiko comments in:

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

v2: https://lore.kernel.org/all/[email protected]/
* 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 | 163 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |  42 ++++++
 drivers/infiniband/hw/efa/efa_com.c      |  12 +-
 drivers/infiniband/hw/efa/efa_com.h      |   3 +
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  73 +++++++---
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 8 files changed, 281 insertions(+), 26 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h
Quoted 7 lines
Quoted 3 lines
Signature · 1 line
2.50.1

Hi, kind reminder.
This series blocks merging EFAv4 device ID and we would want it to land for the next merge window if possible.

It is not possible.

The use of entry->lock together with a refcount and an "initialize" flag suggests that the refcount is not being used correctly.

I would expect a single ah_cache lock, with the refcount tracking the number of users of the entry.

Thanks.

Quoted 6 lines

I reviewed the last Sashiko comment and it's not relevant since on destroy AH failure, we keep the entry in the hashtable.

Thanks

#5 lore raw
Quoted 52 lines · Leon Romanovsky
Quoted 41 lines · Yonatan Nachum
Quoted 40 lines · Yonatan Nachum

Changelog:
v4:
* Use kzalloc_obj for AH cache entry allocation instead of kzalloc
v3: https://lore.kernel.org/all/[email protected]/
* Address Sashiko comments in:

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

v2: https://lore.kernel.org/all/[email protected]/
* 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 | 163 +++++++++++++++++++++++
 drivers/infiniband/hw/efa/efa_ah_cache.h |  42 ++++++
 drivers/infiniband/hw/efa/efa_com.c      |  12 +-
 drivers/infiniband/hw/efa/efa_com.h      |   3 +
 drivers/infiniband/hw/efa/efa_com_cmd.c  |  73 +++++++---
 drivers/infiniband/hw/efa/efa_com_cmd.h  |   1 +
 drivers/infiniband/hw/efa/efa_verbs.c    |   9 +-
 8 files changed, 281 insertions(+), 26 deletions(-)
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.c
 create mode 100644 drivers/infiniband/hw/efa/efa_ah_cache.h
Quoted 7 lines
Quoted 3 lines
Signature · 1 line
2.50.1

Hi, kind reminder.
This series blocks merging EFAv4 device ID and we would want it to land for the next merge window if possible.

It is not possible.

Quoted 7 lines

The use of entry->lock together with a refcount and an "initialize" flag suggests that the refcount is not being used correctly.

I would expect a single ah_cache lock, with the refcount tracking the number of users of the entry.

Thanks.

A global AH cache lock would serialize all AH commands for any PD-GID combination, including the ones that go to the device.
The per-entry mutex allows different entries to issue device commands in parallel while only serializing operations on the same entry.

The initialized flag is needed because the entry must exist in the hashtable before the device command completes, so concurrent threads targeting the same PD-GID find it and wait on the per-entry mutex.

I am open to simplifying to a single globlal lock if you prefer, but it comes at the performance cost of serializing all AH commands.

maintainer
#6 lore raw
Quoted 8 lines · Yonatan Nachum

A global AH cache lock would serialize all AH commands for any PD-GID combination, including the ones that go to the device.
The per-entry mutex allows different entries to issue device commands in parallel while only serializing operations on the same entry.

The initialized flag is needed because the entry must exist in the hashtable before the device command completes, so concurrent threads targeting the same PD-GID find it and wait on the per-entry mutex.

I think it looks so weird because it overloads the refcount in two ways.

The scheme really has two different orthogonal ideas:
- A kref which manages the lifetime of the entry and when the kref reaches 0 the entry leaves the hash. The entry is basically just the mutex and a user count. The hash holds a guarenteed singleton locking point to control the HW object creation order.

- A user count which counts how many active AH's are using the HW object, and if it is non-zero then the HW object exists.

The combination of the refcount and initialized is overloading both of these different behaviors, along with the funky refcount logic.

But given the initialized costs as much memory in the struct as another refcount you may as well just replace it with a proper user count.

Then it is alot simpler.
Global lock, do the search, get the kref, unlock and return. Local lock, check the usercount == 0 and allocate HW object, incr, unlock.

Local lock, check the usercount == 1 and dealloc the HW obhect, decr, unlock. put kref.

Ideally the kref put would only grab the global lock when the refcount is 0, but you have to be able to tolerate multiple 0 kref things in the hashtable for that to work.

Jason

Quoted 4 lines

I am open to simplifying to a single globlal lock if you prefer, but it comes at the performance cost of serializing all AH commands.

#7 lore raw
Quoted 33 lines · Jason Gunthorpe
Quoted 8 lines · Yonatan Nachum

A global AH cache lock would serialize all AH commands for any PD-GID combination, including the ones that go to the device.
The per-entry mutex allows different entries to issue device commands in parallel while only serializing operations on the same entry.

The initialized flag is needed because the entry must exist in the hashtable before the device command completes, so concurrent threads targeting the same PD-GID find it and wait on the per-entry mutex.

I think it looks so weird because it overloads the refcount in two ways.

The scheme really has two different orthogonal ideas:
- A kref which manages the lifetime of the entry and when the kref reaches 0 the entry leaves the hash. The entry is basically just the mutex and a user count. The hash holds a guarenteed singleton locking point to control the HW object creation order.

- A user count which counts how many active AH's are using the HW object, and if it is non-zero then the HW object exists.

The combination of the refcount and initialized is overloading both of these different behaviors, along with the funky refcount logic.

But given the initialized costs as much memory in the struct as another refcount you may as well just replace it with a proper user count.

Then it is alot simpler.
Global lock, do the search, get the kref, unlock and return. Local lock, check the usercount == 0 and allocate HW object, incr, unlock.

Quoted 6 lines

Local lock, check the usercount == 1 and dealloc the HW obhect, decr, unlock. put kref.

Ideally the kref put would only grab the global lock when the refcount is 0, but you have to be able to tolerate multiple 0 kref things in the hashtable for that to work.

Jason,

I would suggest that EFA start with the basics. Their command submission path has spinlocks, so their claim of “performance degradation” should be viewed with an appropriate degree of skepticism.

Thanks

Quoted 9 lines

Jason

Quoted 4 lines

I am open to simplifying to a single globlal lock if you prefer, but it comes at the performance cost of serializing all AH commands.

maintainer
#8 lore raw
Quoted 41 lines · Leon Romanovsky
Quoted 33 lines · Jason Gunthorpe
Quoted 8 lines · Yonatan Nachum

A global AH cache lock would serialize all AH commands for any PD-GID combination, including the ones that go to the device.
The per-entry mutex allows different entries to issue device commands in parallel while only serializing operations on the same entry.

The initialized flag is needed because the entry must exist in the hashtable before the device command completes, so concurrent threads targeting the same PD-GID find it and wait on the per-entry mutex.

I think it looks so weird because it overloads the refcount in two ways.

The scheme really has two different orthogonal ideas:
- A kref which manages the lifetime of the entry and when the kref reaches 0 the entry leaves the hash. The entry is basically just the mutex and a user count. The hash holds a guarenteed singleton locking point to control the HW object creation order.

- A user count which counts how many active AH's are using the HW object, and if it is non-zero then the HW object exists.

The combination of the refcount and initialized is overloading both of these different behaviors, along with the funky refcount logic.

But given the initialized costs as much memory in the struct as another refcount you may as well just replace it with a proper user count.

Then it is alot simpler.
Global lock, do the search, get the kref, unlock and return. Local lock, check the usercount == 0 and allocate HW object, incr, unlock.

Quoted 6 lines

Local lock, check the usercount == 1 and dealloc the HW obhect, decr, unlock. put kref.

Ideally the kref put would only grab the global lock when the refcount is 0, but you have to be able to tolerate multiple 0 kref things in the hashtable for that to work.

Quoted 8 lines

Jason,

I would suggest that EFA start with the basics. Their command submission path has spinlocks, so their claim of “performance degradation” should be viewed with an appropriate degree of skepticism.

Thanks

Leon, I appreciate your skepticism about our claims but the spinlock we hold for the brief period of taking a command context and writing command buffer has nothing to do with the subject here. How is that in any way equivalent to serializing all AH commands for a device?

Michael

Quoted 9 lines
Quoted 9 lines

Jason

Quoted 4 lines

I am open to simplifying to a single globlal lock if you prefer, but it comes at the performance cost of serializing all AH commands.