Skip to contentSkip to conversation

damon: adjust large arrays to dynamic allocation

PATCH In review [email protected] 2 participants 2 messages Jul 12, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 damon: adjust large arrays to dynamic allocation

From: Peng Hao <[email protected]>

When KASAN is enabled and built with clang:
mm/damon/lru_sort.c:199:12: error: stack frame size (2328) exceeds limit (2048) in 'damon_lru_sort_apply_parameters' [-Werror,-Wframe-larger-than] static int damon_lru_sort_apply_parameters(void)
^
1 error generated.

This is because damon_lru_sort_quota contains a large array, and assigning this variable to a local variable causes a large amount of stack space to be occupied.

---
 include/linux/damon.h | 2 +-
 mm/damon/core.c       | 8 +++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)
include/linux/damon.h +1 -1
index f7da65e1ac04..8ba0457ab538 100644
@@ -229,7 +229,7 @@ struct damos_quota {
 	unsigned long charge_addr_from;
 
 	/* For prioritization */
-	unsigned long histogram[DAMOS_MAX_SCORE + 1];
+	unsigned long *histogram; //size:DAMOS_MAX_SCORE + 1;
 	unsigned int min_score;
 
 	/* For feedback loop */
mm/damon/core.c +7 -1
index 6392f1cc97a3..0614b49fbc35 100644
@@ -358,6 +358,10 @@ struct damos *damon_new_scheme(struct damos_access_pattern *pattern,
 {
 	struct damos *scheme;
 
+	unsigned long *histogram = kmalloc(DAMOS_MAX_SCORE + 1, GFP_KERNEL);
+	if (!histogram)
+		return NULL;
+
 	scheme = kmalloc(sizeof(*scheme), GFP_KERNEL);
 	if (!scheme)
 		return NULL;
@@ -375,6 +379,7 @@ struct damos *damon_new_scheme(struct damos_access_pattern *pattern,
 	INIT_LIST_HEAD(&scheme->list);
 
 	scheme->quota = *(damos_quota_init(quota));
+	cheme->quota.histogram = histogram;
 	/* quota.goals should be separately set by caller */
 	INIT_LIST_HEAD(&scheme->quota.goals);
 
@@ -408,6 +413,7 @@ static void damon_del_scheme(struct damos *s)
 
 static void damon_free_scheme(struct damos *s)
 {
+	kfree(s->quota.histogram);
 	kfree(s);
 }
 
@@ -1246,7 +1252,7 @@ static void damos_adjust_quota(struct damon_ctx *c, struct damos *s)
 		return;
 
 	/* Fill up the score histogram */
-	memset(quota->histogram, 0, sizeof(quota->histogram));
+	memset(quota->histogram, 0, sizeof(*quota->histogram) * (DAMOS_MAX_SCORE + 1));
 	damon_for_each_target(t, c) {
 		damon_for_each_region(r, t) {
 			if (!__damos_valid_target(r, s))
Signature · 1 line
2.27.0
maintainer
#1 lore raw

Hi Peng,

Quoted 11 lines · On Fri, 12 Jul 2024 09:07:42 +0800 [email protected] wrote:

From: Peng Hao <[email protected]>

When KASAN is enabled and built with clang:
mm/damon/lru_sort.c:199:12: error: stack frame size (2328) exceeds limit (2048) in 'damon_lru_sort_apply_parameters' [-Werror,-Wframe-larger-than] static int damon_lru_sort_apply_parameters(void)
^
1 error generated.

This is because damon_lru_sort_quota contains a large array, and assigning this variable to a local variable causes a large amount of stack space to be occupied.

Is this a successor of a patch[1] that you sent yesterday? If so, could you please explain what's the change from the version and why such changes have made?

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

Quoted 25 lines
---
 include/linux/damon.h | 2 +-
 mm/damon/core.c       | 8 +++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)
include/linux/damon.h +1 -1
index f7da65e1ac04..8ba0457ab538 100644
@@ -229,7 +229,7 @@ struct damos_quota {
 	unsigned long charge_addr_from;
 
 	/* For prioritization */
-	unsigned long histogram[DAMOS_MAX_SCORE + 1];
+	unsigned long *histogram; //size:DAMOS_MAX_SCORE + 1;
 	unsigned int min_score;
 
 	/* For feedback loop */
mm/damon/core.c +0 -0
index 6392f1cc97a3..0614b49fbc35 100644

mm/damon/core.c struct damos *damon_new_scheme(struct damos_access_pattern *pattern, · line 367

Quoted 10 lines
mm/damon/core.c +4 -0
@@ -358,6 +358,10 @@ struct damos *damon_new_scheme(struct damos_access_pattern *pattern,
 {
 	struct damos *scheme;
 
+	unsigned long *histogram = kmalloc(DAMOS_MAX_SCORE + 1, GFP_KERNEL);
+	if (!histogram)
+		return NULL;
+
 	scheme = kmalloc(sizeof(*scheme), GFP_KERNEL);
 	if (!scheme)
 		return NULL;

Some code allocate damos_quota objects without damon_new_scheme(). For example, damon_lru_sort_quota. I think such code should also be updated, to get this change? I think it is simpler to keep the histogram as an array as is and change the stack-allocations as I suggested on the v1, though.

mm/damon/core.c struct damos *damon_new_scheme(struct damos_access_pattern *pattern, · line 382

Quoted 5 lines
mm/damon/core.c +1 -0
@@ -375,6 +379,7 @@ struct damos *damon_new_scheme(struct damos_access_pattern *pattern,
 	INIT_LIST_HEAD(&scheme->list);
 
 	scheme->quota = *(damos_quota_init(quota));
+	cheme->quota.histogram = histogram;

I think this should s/cheme/scheme/? Have you checked if this can be compiled?

Quoted 22 lines
mm/damon/core.c +2 -1
 	/* quota.goals should be separately set by caller */
 	INIT_LIST_HEAD(&scheme->quota.goals);
 
@@ -408,6 +413,7 @@ static void damon_del_scheme(struct damos *s)
 
 static void damon_free_scheme(struct damos *s)
 {
+	kfree(s->quota.histogram);
 	kfree(s);
 }
 
@@ -1246,7 +1252,7 @@ static void damos_adjust_quota(struct damon_ctx *c, struct damos *s)
 		return;
 
 	/* Fill up the score histogram */
-	memset(quota->histogram, 0, sizeof(quota->histogram));
+	memset(quota->histogram, 0, sizeof(*quota->histogram) * (DAMOS_MAX_SCORE + 1));
 	damon_for_each_target(t, c) {
 		damon_for_each_region(r, t) {
 			if (!__damos_valid_target(r, s))
Signature · 1 line
2.27.0

Thanks,
SJ