Skip to contentSkip to conversation

FAILED: patch "[PATCH] mm: kfence: fix objcgs vector allocation" failed to apply to 5.15-stable tree

PATCH [email protected] 3 participants 5 messages Apr 28, 2022 – Apr 29, 2022

Subsystems (3)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

The patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

thanks,

greg k-h

------------------ original commit in Linus's tree ------------------

From 8f0b36497303487d5a32c75789c77859cc2ee895 Mon Sep 17 00:00:00 2001
From: Muchun Song <[email protected]>
Date: Fri, 1 Apr 2022 11:28:36 -0700
Subject: [PATCH] mm: kfence: fix objcgs vector allocation

If the kfence object is allocated to be used for objects vector, then this slot of the pool eventually being occupied permanently since the vector is never freed. The solutions could be (1) freeing vector when the kfence object is freed or (2) allocating all vectors statically.

Since the memory consumption of object vectors is low, it is better to chose (2) to fix the issue and it is also can reduce overhead of vectors allocating in the future.

Fixes d3fb45f370d9 ("mm, kfence: insert KFENCE hooks for SLAB")
Cc Alexander Potapenko <[email protected]>
Cc Dmitry Vyukov <[email protected]>
Cc Xiongchun Duan <[email protected]>
mm/kfence/core.c +10 -1
index 2f9fdfde1941..a203747ad2c0 100644
@@ -566,6 +566,8 @@ static unsigned long kfence_init_pool(void)
 	 * enters __slab_free() slow-path.
 	 */
 	for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
+		struct slab *slab = page_slab(&pages[i]);
+
 		if (!i || (i % 2))
 			continue;
 
@@ -573,7 +575,11 @@ static unsigned long kfence_init_pool(void)
 		if (WARN_ON(compound_head(&pages[i]) != &pages[i]))
 			return addr;
 
-		__SetPageSlab(&pages[i]);
+		__folio_set_slab(slab_folio(slab));
+#ifdef CONFIG_MEMCG
+		slab->memcg_data = (unsigned long)&kfence_metadata[i / 2 - 1].objcg |
+				   MEMCG_DATA_OBJCGS;
+#endif
 	}
 
 	/*
@@ -1033,6 +1039,9 @@ void __kfence_free(void *addr)
 {
 	struct kfence_metadata *meta = addr_to_metadata((unsigned long)addr);
 
+#ifdef CONFIG_MEMCG
+	KFENCE_WARN_ON(meta->objcg);
+#endif
 	/*
 	 * If the objects of the cache are SLAB_TYPESAFE_BY_RCU, defer freeing
 	 * the object, as the object page may be recycled for other-typed
mm/kfence/kfence.h +3 -0
index 2a2d5de9d379..9a6c4b1b12a8 100644
@@ -89,6 +89,9 @@ struct kfence_metadata {
 	struct kfence_track free_track;
 	/* For updating alloc_covered on frees. */
 	u32 alloc_stack_hash;
+#ifdef CONFIG_MEMCG
+	struct obj_cgroup *objcg;
+#endif
 };
 
 extern struct kfence_metadata kfence_metadata[CONFIG_KFENCE_NUM_OBJECTS];
#1 lore raw
Quoted 7 lines · On Thu, Apr 28, 2022 at 11:36 PM <[email protected]> wrote:

The patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

I have fixed all conflicts and the attachment is the new patch for 5.15.

Thanks.

From 99e78b2e14ecbfedf3e9b1c6f47bc9587b129985 Mon Sep 17 00:00:00 2001
From: Muchun Song <[email protected]>
Date: Sun, 27 Mar 2022 10:39:04 +0800
Subject: [PATCH] mm: kfence: fix objcgs vector allocation

If the kfence object is allocated to be used for objects vector, then this slot of the pool eventually being occupied permanently since the vector is never freed. The solutions could be 1) freeing vector when the kfence object is freed or 2) allocating all vectors statically. Since the memory consumption of object vectors is low, it is better to chose 2) to fix the issue and it is also can reduce overhead of vectors allocating in the future.

Fixes d3fb45f370d9 ("mm, kfence: insert KFENCE hooks for SLAB")
---
 mm/kfence/core.c   | 11 ++++++++++-
 mm/kfence/kfence.h |  3 +++
 2 files changed, 13 insertions(+), 1 deletion(-)
mm/kfence/core.c +10 -1
index 84555b8233ef..863f2f7cc036 100644
@@ -448,6 +448,8 @@ static bool __init kfence_init_pool(void)
 	 * enters __slab_free() slow-path.
 	 */
 	for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
+		struct page *page = &pages[i];
+
 		if (!i || (i % 2))
 			continue;
 
@@ -455,7 +457,11 @@ static bool __init kfence_init_pool(void)
 		if (WARN_ON(compound_head(&pages[i]) != &pages[i]))
 			goto err;
 
-		__SetPageSlab(&pages[i]);
+		__SetPageSlab(page);
+#ifdef CONFIG_MEMCG
+		page->memcg_data = (unsigned long)&kfence_metadata[i / 2 - 1].objcg |
+				   MEMCG_DATA_OBJCGS;
+#endif
 	}
 
 	/*
@@ -804,6 +810,9 @@ void __kfence_free(void *addr)
 {
 	struct kfence_metadata *meta = addr_to_metadata((unsigned long)addr);
 
+#ifdef CONFIG_MEMCG
+	KFENCE_WARN_ON(meta->objcg);
+#endif
 	/*
 	 * If the objects of the cache are SLAB_TYPESAFE_BY_RCU, defer freeing
 	 * the object, as the object page may be recycled for other-typed
mm/kfence/kfence.h +3 -0
index c1f23c61e5f9..c83ea759a91a 100644
@@ -87,6 +87,9 @@ struct kfence_metadata {
 	/* Allocation and free stack information. */
 	struct kfence_track alloc_track;
 	struct kfence_track free_track;
+#ifdef CONFIG_MEMCG
+	struct obj_cgroup *objcg;
+#endif
 };
 
 extern struct kfence_metadata kfence_metadata[CONFIG_KFENCE_NUM_OBJECTS];
Signature · 1 line
2.11.0
maintainer
#2 lore raw
Quoted 9 lines · Muchun Song
Quoted 7 lines · On Thu, Apr 28, 2022 at 11:36 PM <[email protected]> wrote:

The patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

Quoted 3 lines

I have fixed all conflicts and the attachment is the new patch for 5.15.

Thanks.

I wanted to test, but unfortunately this doesn't apply to 5.15.36.

#3 lore raw
Quoted 11 lines · Marco Elver
Quoted 9 lines · Muchun Song
Quoted 7 lines · On Thu, Apr 28, 2022 at 11:36 PM <[email protected]> wrote:

The patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

Quoted 5 lines
Quoted 3 lines

I have fixed all conflicts and the attachment is the new patch for 5.15.

Thanks.

I wanted to test, but unfortunately this doesn't apply to 5.15.36.

My bad. I was based on v5.15.33. I have made a new version for v5.15.36 (see attachment) and tested it.

Thanks.

From 1b446e199c0392ee1f44b709365cee3bb09306cf Mon Sep 17 00:00:00 2001
From: Muchun Song <[email protected]>
Date: Sun, 27 Mar 2022 10:39:04 +0800
Subject: [PATCH] mm: kfence: fix objcgs vector allocation

If the kfence object is allocated to be used for objects vector, then this slot of the pool eventually being occupied permanently since the vector is never freed. The solutions could be 1) freeing vector when the kfence object is freed or 2) allocating all vectors statically. Since the memory consumption of object vectors is low, it is better to chose 2) to fix the issue and it is also can reduce overhead of vectors allocating in the future.

Fixes d3fb45f370d9 ("mm, kfence: insert KFENCE hooks for SLAB")
---
 mm/kfence/core.c   | 11 ++++++++++-
 mm/kfence/kfence.h |  3 +++
 2 files changed, 13 insertions(+), 1 deletion(-)
mm/kfence/core.c +10 -1
index 86260e8f2830..66076d8742b7 100644
@@ -528,6 +528,8 @@ static bool __init kfence_init_pool(void)
 	 * enters __slab_free() slow-path.
 	 */
 	for (i = 0; i < KFENCE_POOL_SIZE / PAGE_SIZE; i++) {
+		struct page *page = &pages[i];
+
 		if (!i || (i % 2))
 			continue;
 
@@ -535,7 +537,11 @@ static bool __init kfence_init_pool(void)
 		if (WARN_ON(compound_head(&pages[i]) != &pages[i]))
 			goto err;
 
-		__SetPageSlab(&pages[i]);
+		__SetPageSlab(page);
+#ifdef CONFIG_MEMCG
+		page->memcg_data = (unsigned long)&kfence_metadata[i / 2 - 1].objcg |
+				   MEMCG_DATA_OBJCGS;
+#endif
 	}
 
 	/*
@@ -911,6 +917,9 @@ void __kfence_free(void *addr)
 {
 	struct kfence_metadata *meta = addr_to_metadata((unsigned long)addr);
 
+#ifdef CONFIG_MEMCG
+	KFENCE_WARN_ON(meta->objcg);
+#endif
 	/*
 	 * If the objects of the cache are SLAB_TYPESAFE_BY_RCU, defer freeing
 	 * the object, as the object page may be recycled for other-typed
mm/kfence/kfence.h +3 -0
index 92bf6eff6060..600f2e2431d6 100644
@@ -89,6 +89,9 @@ struct kfence_metadata {
 	struct kfence_track free_track;
 	/* For updating alloc_covered on frees. */
 	u32 alloc_stack_hash;
+#ifdef CONFIG_MEMCG
+	struct obj_cgroup *objcg;
+#endif
 };
 
 extern struct kfence_metadata kfence_metadata[CONFIG_KFENCE_NUM_OBJECTS];
Signature · 1 line
2.11.0
author maintainer
#4 lore raw
Quoted 14 lines · Muchun Song
Quoted 13 lines · Marco Elver
Quoted 11 lines · Muchun Song
Quoted 7 lines · On Thu, Apr 28, 2022 at 11:36 PM <[email protected]> wrote:

The patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.

I have fixed all conflicts and the attachment is the new patch for 5.15.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Thanks.

I wanted to test, but unfortunately this doesn't apply to 5.15.36.

My bad. I was based on v5.15.33. I have made a new version for v5.15.36 (see attachment) and tested it.

Thanks.

Now queued up, thanks.

greg k-h