Skip to contentSkip to conversation

iommu/iova: use named kmem_cache for iova magazines

PATCH Applied Robin Murphy 6 participants 17 messages Feb 5, 2024 – Feb 9, 2024

Subsystems (3)
lore
Share & tools
lore

Robin Murphy

author maintainer
#0 lore raw

v3 cover iommu/iova: use named kmem_cache for iova magazines

Hi all,

I decided that Pasha's patch[1] was a good excuse to tackle a bit more cleanup of the existing IOVA code, so it can fit in more neatly. Thus to save time and effort I've taken the liberty of putting together this mini-series as the version I'd like to merge.

Cheers,
Robin.

[1] https://lore.kernel.org/linux-iommu/[email protected]/

Pasha Tatashin (1):
iommu/iova: use named kmem_cache for iova magazines

Robin Murphy (2):
iommu/iova: Tidy up iova_cache_get() failure
iommu/iova: Reorganise some code

 drivers/iommu/iova.c | 143 +++++++++++++++++++++++--------------------
 1 file changed, 76 insertions(+), 67 deletions(-)
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
author maintainer
#1 lore raw

PATCH v3 1/3 iommu/iova: Tidy up iova_cache_get() failure

Failure handling in iova_cache_get() is a little messy, and we'd like to add some more to it, so let's tidy up a bit first. By leaving the hotplug handler until last we can take advantage of kmem_cache_destroy() being NULL-safe to have a single cleanup label. We can also improve the error reporting, noting that kmem_cache_create() already screams if it fails, so that one is redundant.

---
 drivers/iommu/iova.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
drivers/iommu/iova.c +16 -17
index d30e453d0fb4..cf95001d85c0 100644
@@ -254,26 +254,20 @@ static void free_iova_mem(struct iova *iova)
 
 int iova_cache_get(void)
 {
+	int err = -ENOMEM;
+
 	mutex_lock(&iova_cache_mutex);
 	if (!iova_cache_users) {
-		int ret;
+		iova_cache = kmem_cache_create("iommu_iova", sizeof(struct iova), 0,
+					       SLAB_HWCACHE_ALIGN, NULL);
+		if (!iova_cache)
+			goto out_err;
 
-		ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", NULL,
-					iova_cpuhp_dead);
-		if (ret) {
-			mutex_unlock(&iova_cache_mutex);
-			pr_err("Couldn't register cpuhp handler\n");
-			return ret;
-		}
-
-		iova_cache = kmem_cache_create(
-			"iommu_iova", sizeof(struct iova), 0,
-			SLAB_HWCACHE_ALIGN, NULL);
-		if (!iova_cache) {
-			cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
-			mutex_unlock(&iova_cache_mutex);
-			pr_err("Couldn't create iova cache\n");
-			return -ENOMEM;
+		err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead",
+					      NULL, iova_cpuhp_dead);
+		if (err) {
+			pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", ERR_PTR(err));
+			goto out_err;
 		}
 	}
 
@@ -281,6 +275,11 @@ int iova_cache_get(void)
 	mutex_unlock(&iova_cache_mutex);
 
 	return 0;
+
+out_err:
+	kmem_cache_destroy(iova_cache);
+	mutex_unlock(&iova_cache_mutex);
+	return err;
 }
 EXPORT_SYMBOL_GPL(iova_cache_get);
 
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
author maintainer
#7 lore raw

PATCH v3 2/3 iommu/iova: Reorganise some code

In reply to Robin Murphy

The iova_cache_{get,put}() calls really represent top-level lifecycle management for the whole IOVA library, so it's long been rather confusing to have them buried right in the middle of the allocator implementation details. Move them to a more expected position at the end of the file, where it will then also be easier to expand them. With this, we can also move the rcache hotplug handler (plus another stray function) into the rcache portion of the file.

---
 drivers/iommu/iova.c | 128 +++++++++++++++++++++----------------------
 1 file changed, 64 insertions(+), 64 deletions(-)
drivers/iommu/iova.c +64 -64
index cf95001d85c0..b5de865ee50b 100644
@@ -24,24 +24,8 @@ static bool iova_rcache_insert(struct iova_domain *iovad,
 static unsigned long iova_rcache_get(struct iova_domain *iovad,
 				     unsigned long size,
 				     unsigned long limit_pfn);
-static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);
 static void free_iova_rcaches(struct iova_domain *iovad);
-
-unsigned long iova_rcache_range(void)
-{
-	return PAGE_SIZE << (IOVA_RANGE_CACHE_MAX_SIZE - 1);
-}
-
-static int iova_cpuhp_dead(unsigned int cpu, struct hlist_node *node)
-{
-	struct iova_domain *iovad;
-
-	iovad = hlist_entry_safe(node, struct iova_domain, cpuhp_dead);
-
-	free_cpu_cached_iovas(cpu, iovad);
-	return 0;
-}
-
+static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);
 static void free_global_cached_iovas(struct iova_domain *iovad);
 
 static struct iova *to_iova(struct rb_node *node)
@@ -252,53 +236,6 @@ static void free_iova_mem(struct iova *iova)
 		kmem_cache_free(iova_cache, iova);
 }
 
-int iova_cache_get(void)
-{
-	int err = -ENOMEM;
-
-	mutex_lock(&iova_cache_mutex);
-	if (!iova_cache_users) {
-		iova_cache = kmem_cache_create("iommu_iova", sizeof(struct iova), 0,
-					       SLAB_HWCACHE_ALIGN, NULL);
-		if (!iova_cache)
-			goto out_err;
-
-		err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead",
-					      NULL, iova_cpuhp_dead);
-		if (err) {
-			pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", ERR_PTR(err));
-			goto out_err;
-		}
-	}
-
-	iova_cache_users++;
-	mutex_unlock(&iova_cache_mutex);
-
-	return 0;
-
-out_err:
-	kmem_cache_destroy(iova_cache);
-	mutex_unlock(&iova_cache_mutex);
-	return err;
-}
-EXPORT_SYMBOL_GPL(iova_cache_get);
-
-void iova_cache_put(void)
-{
-	mutex_lock(&iova_cache_mutex);
-	if (WARN_ON(!iova_cache_users)) {
-		mutex_unlock(&iova_cache_mutex);
-		return;
-	}
-	iova_cache_users--;
-	if (!iova_cache_users) {
-		cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
-		kmem_cache_destroy(iova_cache);
-	}
-	mutex_unlock(&iova_cache_mutex);
-}
-EXPORT_SYMBOL_GPL(iova_cache_put);
-
 /**
  * alloc_iova - allocates an iova
  * @iovad: - iova domain in question
@@ -653,6 +590,11 @@ struct iova_rcache {
 	struct delayed_work work;
 };
 
+unsigned long iova_rcache_range(void)
+{
+	return PAGE_SIZE << (IOVA_RANGE_CACHE_MAX_SIZE - 1);
+}
+
 static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
 {
 	struct iova_magazine *mag;
@@ -989,5 +931,63 @@ static void free_global_cached_iovas(struct iova_domain *iovad)
 		spin_unlock_irqrestore(&rcache->lock, flags);
 	}
 }
+
+static int iova_cpuhp_dead(unsigned int cpu, struct hlist_node *node)
+{
+	struct iova_domain *iovad;
+
+	iovad = hlist_entry_safe(node, struct iova_domain, cpuhp_dead);
+
+	free_cpu_cached_iovas(cpu, iovad);
+	return 0;
+}
+
+int iova_cache_get(void)
+{
+	int err = -ENOMEM;
+
+	mutex_lock(&iova_cache_mutex);
+	if (!iova_cache_users) {
+		iova_cache = kmem_cache_create("iommu_iova", sizeof(struct iova), 0,
+					       SLAB_HWCACHE_ALIGN, NULL);
+		if (!iova_cache)
+			goto out_err;
+
+		err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead",
+					      NULL, iova_cpuhp_dead);
+		if (err) {
+			pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", ERR_PTR(err));
+			goto out_err;
+		}
+	}
+
+	iova_cache_users++;
+	mutex_unlock(&iova_cache_mutex);
+
+	return 0;
+
+out_err:
+	kmem_cache_destroy(iova_cache);
+	mutex_unlock(&iova_cache_mutex);
+	return err;
+}
+EXPORT_SYMBOL_GPL(iova_cache_get);
+
+void iova_cache_put(void)
+{
+	mutex_lock(&iova_cache_mutex);
+	if (WARN_ON(!iova_cache_users)) {
+		mutex_unlock(&iova_cache_mutex);
+		return;
+	}
+	iova_cache_users--;
+	if (!iova_cache_users) {
+		cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
+		kmem_cache_destroy(iova_cache);
+	}
+	mutex_unlock(&iova_cache_mutex);
+}
+EXPORT_SYMBOL_GPL(iova_cache_put);
+
 MODULE_AUTHOR("Anil S Keshavamurthy <[email protected]>");
 MODULE_LICENSE("GPL");
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
author maintainer
#12 lore raw

PATCH v3 3/3 iommu/iova: use named kmem_cache for iova magazines

In reply to Robin Murphy

From: Pasha Tatashin <[email protected]>

The magazine buffers can take gigabytes of kmem memory, dominating all other allocations. For observability purpose create named slab cache so the iova magazine memory overhead can be clearly observed.

With this change:

Quoted 1 line

slabtop -o | head

 Active / Total Objects (% used)    : 869731 / 952904 (91.3%)
 Active / Total Slabs (% used)      : 103411 / 103974 (99.5%)
 Active / Total Caches (% used)     : 135 / 211 (64.0%)
 Active / Total Size (% used)       : 395389.68K / 411430.20K (96.1%)

Minimum / Average / Maximum Object : 0.02K / 0.43K / 8.00K

OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME

244412 244239 99%    1.00K  61103       4    244412K iommu_iova_magazine
 91636  88343 96%    0.03K    739     124      2956K kmalloc-32
 75744  74844 98%    0.12K   2367      32      9468K kernfs_node_cache

On this machine it is now clear that magazine use 242M of kmem memory.

[ rm: adjust to rework of iova_cache_{get,put} ]
---
 drivers/iommu/iova.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/iommu/iova.c +12 -2
index b5de865ee50b..d59d0ea2fd21 100644
@@ -590,6 +590,8 @@ struct iova_rcache {
 	struct delayed_work work;
 };
 
+static struct kmem_cache *iova_magazine_cache;
+
 unsigned long iova_rcache_range(void)
 {
 	return PAGE_SIZE << (IOVA_RANGE_CACHE_MAX_SIZE - 1);
@@ -599,7 +601,7 @@ static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
 {
 	struct iova_magazine *mag;
 
-	mag = kmalloc(sizeof(*mag), flags);
+	mag = kmem_cache_alloc(iova_magazine_cache, flags);
 	if (mag)
 		mag->size = 0;
 
@@ -608,7 +610,7 @@ static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
 
 static void iova_magazine_free(struct iova_magazine *mag)
 {
-	kfree(mag);
+	kmem_cache_free(iova_magazine_cache, mag);
 }
 
 static void
@@ -953,6 +955,12 @@ int iova_cache_get(void)
 		if (!iova_cache)
 			goto out_err;
 
+		iova_magazine_cache = kmem_cache_create("iommu_iova_magazine",
+							sizeof(struct iova_magazine),
+							0, SLAB_HWCACHE_ALIGN, NULL);
+		if (!iova_magazine_cache)
+			goto out_err;
+
 		err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead",
 					      NULL, iova_cpuhp_dead);
 		if (err) {
@@ -968,6 +976,7 @@ int iova_cache_get(void)
 
 out_err:
 	kmem_cache_destroy(iova_cache);
+	kmem_cache_destroy(iova_magazine_cache);
 	mutex_unlock(&iova_cache_mutex);
 	return err;
 }
@@ -984,6 +993,7 @@ void iova_cache_put(void)
 	if (!iova_cache_users) {
 		cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
 		kmem_cache_destroy(iova_cache);
+		kmem_cache_destroy(iova_magazine_cache);
 	}
 	mutex_unlock(&iova_cache_mutex);
 }
Signature · 1 line
2.39.2.101.g768bb238c484.dirty
Acked-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

In reply to Robin Murphy
Quoted 8 lines · Robin Murphy

Failure handling in iova_cache_get() is a little messy, and we'd like to add some more to it, so let's tidy up a bit first. By leaving the hotplug handler until last we can take advantage of kmem_cache_destroy() being NULL-safe to have a single cleanup label. We can also improve the error reporting, noting that kmem_cache_create() already screams if it fails, so that one is redundant.

Much easier to follow :)

Acked-by on v3 2/3
#8 lore raw

Re: [PATCH v3 2/3] iommu/iova: Reorganise some code

In reply to Robin Murphy
Quoted 9 lines · Robin Murphy

The iova_cache_{get,put}() calls really represent top-level lifecycle management for the whole IOVA library, so it's long been rather confusing to have them buried right in the middle of the allocator implementation details. Move them to a more expected position at the end of the file, where it will then also be easier to expand them. With this, we can also move the rcache hotplug handler (plus another stray function) into the rcache portion of the file.

Reviewed-by on v3 1/3
#3 lore raw

Re: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

In reply to Robin Murphy
Quoted 9 lines · Robin Murphy

Failure handling in iova_cache_get() is a little messy, and we'd like to add some more to it, so let's tidy up a bit first. By leaving the hotplug handler until last we can take advantage of kmem_cache_destroy() being NULL-safe to have a single cleanup label. We can also improve the error reporting, noting that kmem_cache_create() already screams if it fails, so that one is redundant.

Thank you,
Pasha

Reviewed-by on v3 2/3
#9 lore raw

Re: [PATCH v3 2/3] iommu/iova: Reorganise some code

In reply to Robin Murphy
Quoted 10 lines · Robin Murphy

The iova_cache_{get,put}() calls really represent top-level lifecycle management for the whole IOVA library, so it's long been rather confusing to have them buried right in the middle of the allocator implementation details. Move them to a more expected position at the end of the file, where it will then also be easier to expand them. With this, we can also move the rcache hotplug handler (plus another stray function) into the rcache portion of the file.

Reviewed-by on v3 1/3
#4 lore raw

Re: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

In reply to Robin Murphy
Quoted 8 lines · Robin Murphy

Failure handling in iova_cache_get() is a little messy, and we'd like to add some more to it, so let's tidy up a bit first. By leaving the hotplug handler until last we can take advantage of kmem_cache_destroy() being NULL-safe to have a single cleanup label. We can also improve the error reporting, noting that kmem_cache_create() already screams if it fails, so that one is redundant.

Regardless of a couple of minor comments, below, FWIW:

Quoted 9 lines
---
  drivers/iommu/iova.c | 33 ++++++++++++++++-----------------
  1 file changed, 16 insertions(+), 17 deletions(-)
drivers/iommu/iova.c +0 -0
index d30e453d0fb4..cf95001d85c0 100644

drivers/iommu/iova.c static void free_iova_mem(struct iova *iova) · line 262

Quoted 10 lines
drivers/iommu/iova.c +4 -1
@@ -254,26 +254,20 @@ static void free_iova_mem(struct iova *iova)
 
  int iova_cache_get(void)
  {
+	int err = -ENOMEM;
+
  	mutex_lock(&iova_cache_mutex);
  	if (!iova_cache_users) {
-		int ret;
+		iova_cache = kmem_cache_create("iommu_iova", sizeof(struct iova), 0,
+					       SLAB_HWCACHE_ALIGN, NULL);

Maybe can use KMEM_CACHE(), but it would mean that the name would change.

Quoted 13 lines
drivers/iommu/iova.c +2 -10
+		if (!iova_cache)
+			goto out_err;
 
-		ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead", NULL,
-					iova_cpuhp_dead);
-		if (ret) {
-			mutex_unlock(&iova_cache_mutex);
-			pr_err("Couldn't register cpuhp handler\n");
-			return ret;
-		}
-
-		iova_cache = kmem_cache_create(
-			"iommu_iova", sizeof(struct iova), 0,

drivers/iommu/iova.c static void free_iova_mem(struct iova *iova) · line 269

Quoted 10 lines
drivers/iommu/iova.c +4 -6
-			SLAB_HWCACHE_ALIGN, NULL);
-		if (!iova_cache) {
-			cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
-			mutex_unlock(&iova_cache_mutex);
-			pr_err("Couldn't create iova cache\n");
-			return -ENOMEM;
+		err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead",
+					      NULL, iova_cpuhp_dead);
+		if (err) {
+			pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", ERR_PTR(err));

Maybe can use pr_fmt with "iova".

Quoted 16 lines
drivers/iommu/iova.c +6 -0
+			goto out_err;
  		}
  	}
 
@@ -281,6 +275,11 @@ int iova_cache_get(void)
  	mutex_unlock(&iova_cache_mutex);
 
  	return 0;
+
+out_err:
+	kmem_cache_destroy(iova_cache);
+	mutex_unlock(&iova_cache_mutex);
+	return err;
  }
  EXPORT_SYMBOL_GPL(iova_cache_get);
 
Reviewed-by on v3 2/3
#10 lore raw

Re: [PATCH v3 2/3] iommu/iova: Reorganise some code

In reply to Robin Murphy
Quoted 10 lines · Robin Murphy

The iova_cache_{get,put}() calls really represent top-level lifecycle management for the whole IOVA library, so it's long been rather confusing to have them buried right in the middle of the allocator implementation details. Move them to a more expected position at the end of the file, where it will then also be easier to expand them. With this, we can also move the rcache hotplug handler (plus another stray function) into the rcache portion of the file.

---

FWIW:

Reviewed-by on v3 3/3
#13 lore raw
In reply to Robin Murphy
Quoted 13 lines · Robin Murphy

From: Pasha Tatashin <[email protected]>

The magazine buffers can take gigabytes of kmem memory, dominating all other allocations. For observability purpose create named slab cache so the iova magazine memory overhead can be clearly observed.

With this change:

Quoted 1 line

slabtop -o | head

  Active / Total Objects (% used)    : 869731 / 952904 (91.3%)
  Active / Total Slabs (% used)      : 103411 / 103974 (99.5%)
  Active / Total Caches (% used)     : 135 / 211 (64.0%)
  Active / Total Size (% used)       : 395389.68K / 411430.20K (96.1%)
Quoted 8 lines

Minimum / Average / Maximum Object : 0.02K / 0.43K / 8.00K

OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME
244412 244239 99% 1.00K 61103 4 244412K iommu_iova_magazine

  91636  88343 96%    0.03K    739     124      2956K kmalloc-32
  75744  74844 98%    0.12K   2367      32      9468K kernfs_node_cache

On this machine it is now clear that magazine use 242M of kmem memory.

Those caches could do with a trimming ...

Quoted 6 lines
[ rm: adjust to rework of iova_cache_{get,put} ]

---

FWIW:

author maintainer
#5 lore raw

Re: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

In reply to John Garry
Quoted 8 lines · John Garry
Quoted 7 lines · Robin Murphy

Failure handling in iova_cache_get() is a little messy, and we'd like to add some more to it, so let's tidy up a bit first. By leaving the hotplug handler until last we can take advantage of kmem_cache_destroy() being NULL-safe to have a single cleanup label. We can also improve the error reporting, noting that kmem_cache_create() already screams if it fails, so that one is redundant.

Quoted 4 lines
Quoted 1 line

Regardless of a couple of minor comments, below, FWIW:

Thanks!

Quoted 13 lines
Quoted 13 lines

---
  drivers/iommu/iova.c | 33 ++++++++++++++++-----------------

  1 file changed, 16 insertions(+), 17 deletions(-)
drivers/iommu/iova.c +2 -0
index d30e453d0fb4..cf95001d85c0 100644
@@ -254,26 +254,20 @@ static void free_iova_mem(struct iova *iova)
   int iova_cache_get(void)
   {
+    int err = -ENOMEM;
+
Quoted 8 lines

drivers/iommu/iova.c static void free_iova_mem(struct iova *iova) · line 262

Quoted 6 lines
drivers/iommu/iova.c +2 -1
       mutex_lock(&iova_cache_mutex);
       if (!iova_cache_users) {
-        int ret;
+        iova_cache = kmem_cache_create("iommu_iova", sizeof(struct 
 iova), 0,
+                           SLAB_HWCACHE_ALIGN, NULL);

Maybe can use KMEM_CACHE(), but it would mean that the name would change.

Oh, I never came across that before. On reflection, I am inclined to think that the "iommu_" names are usefully informative to userspace, and it's not worth churning the structure names themselves just to save a couple of lines here.

Quoted 26 lines
Quoted 15 lines
drivers/iommu/iova.c +2 -12
+        if (!iova_cache)
+            goto out_err;
-        ret = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, 
 "iommu/iova:dead", NULL,
-                    iova_cpuhp_dead);
-        if (ret) {
-            mutex_unlock(&iova_cache_mutex);
-            pr_err("Couldn't register cpuhp handler\n");
-            return ret;
-        }
-
-        iova_cache = kmem_cache_create(
-            "iommu_iova", sizeof(struct iova), 0,
-            SLAB_HWCACHE_ALIGN, NULL);
-        if (!iova_cache) {

drivers/iommu/iova.c static void free_iova_mem(struct iova *iova)

Quoted 10 lines
drivers/iommu/iova.c +4 -4
-            cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
-            mutex_unlock(&iova_cache_mutex);
-            pr_err("Couldn't create iova cache\n");
-            return -ENOMEM;
+        err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, 
 "iommu/iova:dead",
+                          NULL, iova_cpuhp_dead);
+        if (err) {
+            pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", 
 ERR_PTR(err));
Quoted 1 line

Maybe can use pr_fmt with "iova".

That one I did consider, but since we now have just this one print and no imminent reason to add more, I figured I'd just stick it inline, and we can factor out a pr_fmt in future if we ever have cause to.

Cheers,
Robin.

Quoted 15 lines
Quoted 14 lines
drivers/iommu/iova.c +1 -0
+            goto out_err;
           }

      }

drivers/iommu/iova.c +5 -0
@@ -281,6 +275,11 @@ int iova_cache_get(void)
       mutex_unlock(&iova_cache_mutex);
       return 0;
+
+out_err:
+    kmem_cache_destroy(iova_cache);
+    mutex_unlock(&iova_cache_mutex);
+    return err;
   }
   EXPORT_SYMBOL_GPL(iova_cache_get);
author maintainer
#14 lore raw
In reply to John Garry
Quoted 16 lines · John Garry
Quoted 15 lines · Robin Murphy

From: Pasha Tatashin <[email protected]>

The magazine buffers can take gigabytes of kmem memory, dominating all other allocations. For observability purpose create named slab cache so the iova magazine memory overhead can be clearly observed.

With this change:

Quoted 1 line

slabtop -o | head

  Active / Total Objects (% used)    : 869731 / 952904 (91.3%) Active / Total Slabs (% used)      : 103411 / 103974 (99.5%) Active / Total Caches (% used)     : 135 / 211 (64.0%) Active / Total Size (% used)       : 395389.68K / 411430.20K (96.1%) Minimum / Average / Maximum Object : 0.02K / 0.43K / 8.00K

Quoted 8 lines
Quoted 6 lines

OBJS ACTIVE  USE OBJ SIZE  SLABS OBJ/SLAB CACHE SIZE NAME 244412 244239 99%    1.00K  61103       4    244412K iommu_iova_magazine 91636  88343 96%    0.03K    739     124      2956K kmalloc-32 75744  74844 98%    0.12K   2367      32      9468K kernfs_node_cache

On this machine it is now clear that magazine use 242M of kmem memory.

Those caches could do with a trimming ...

See the discussion on v1 for more details:

https://lore.kernel.org/linux-iommu/[email protected]/

but it seems this really is the idle baseline for lots of CPUs * lots of domains - if all those devices get going in anger it's likely that combined iova + iova_magazine usage truly will blow up into gigabytes.

Cheers,
Robin.

Quoted 10 lines
Quoted 7 lines
[ rm: adjust to rework of iova_cache_{get,put} ]

---

FWIW:

Reviewed-by on v3 1/3
#6 lore raw

Re: [PATCH v3 1/3] iommu/iova: Tidy up iova_cache_get() failure

In reply to Robin Murphy
Quoted 7 lines · Robin Murphy

Failure handling in iova_cache_get() is a little messy, and we'd like to add some more to it, so let's tidy up a bit first. By leaving the hotplug handler until last we can take advantage of kmem_cache_destroy() being NULL-safe to have a single cleanup label. We can also improve the error reporting, noting that kmem_cache_create() already screams if it fails, so that one is redundant.

Quoted 5 lines
---
 drivers/iommu/iova.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
Reviewed-by on v3 2/3
#11 lore raw

Re: [PATCH v3 2/3] iommu/iova: Reorganise some code

In reply to Robin Murphy
Quoted 8 lines · Robin Murphy

The iova_cache_{get,put}() calls really represent top-level lifecycle management for the whole IOVA library, so it's long been rather confusing to have them buried right in the middle of the allocator implementation details. Move them to a more expected position at the end of the file, where it will then also be easier to expand them. With this, we can also move the rcache hotplug handler (plus another stray function) into the rcache portion of the file.

Quoted 5 lines
---
 drivers/iommu/iova.c | 128 +++++++++++++++++++++----------------------
 1 file changed, 64 insertions(+), 64 deletions(-)
Reviewed-by on v3 3/3
#15 lore raw
In reply to Robin Murphy
Quoted 20 lines · Robin Murphy

From: Pasha Tatashin <[email protected]>

The magazine buffers can take gigabytes of kmem memory, dominating all other allocations. For observability purpose create named slab cache so the iova magazine memory overhead can be clearly observed.

With this change:

Quoted 1 line

slabtop -o | head

 Active / Total Objects (% used)    : 869731 / 952904 (91.3%)
 Active / Total Slabs (% used)      : 103411 / 103974 (99.5%)
 Active / Total Caches (% used)     : 135 / 211 (64.0%)
 Active / Total Size (% used)       : 395389.68K / 411430.20K (96.1%)

Minimum / Average / Maximum Object : 0.02K / 0.43K / 8.00K

OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME

244412 244239 99%    1.00K  61103       4    244412K iommu_iova_magazine
 91636  88343 96%    0.03K    739     124      2956K kmalloc-32
 75744  74844 98%    0.12K   2367      32      9468K kernfs_node_cache
Quoted 7 lines

On this machine it is now clear that magazine use 242M of kmem memory.

[ rm: adjust to rework of iova_cache_{get,put} ]

---

maintainer Applied
#16 lore raw
In reply to Robin Murphy
Quoted 9 lines · Robin Murphy

Pasha Tatashin (1):
iommu/iova: use named kmem_cache for iova magazines

Robin Murphy (2):
iommu/iova: Tidy up iova_cache_get() failure
iommu/iova: Reorganise some code

 drivers/iommu/iova.c | 143 +++++++++++++++++++++++--------------------
 1 file changed, 76 insertions(+), 67 deletions(-)

Applied, thanks Robin.