Skip to contentSkip to conversation

mm: compaction: support triggering of proactive compaction by user

PATCH Superseded Charan Teja Reddy 4 participants 10 messages May 18, 2021 – May 29, 2021

Subsystems (7)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm: compaction: support triggering of proactive compaction by user

The proactive compaction[1] gets triggered for every 500msec and run compaction on the node for COMPACTION_HPAGE_ORDER (usually order-9) pages based on the value set to sysctl.compaction_proactiveness. Triggering the compaction for every 500msec in search of COMPACTION_HPAGE_ORDER pages is not needed for all applications, especially on the embedded system usecases which may have few MB's of RAM. Enabling the proactive compaction in its state will endup in running almost always on such systems.

Other side, proactive compaction can still be very much useful for getting a set of higher order pages in some controllable manner(controlled by using the sysctl.compaction_proactiveness). Thus on systems where enabling the proactive compaction always may proove not required, can trigger the same from user space on write to its sysctl interface. As an example, say app launcher decide to launch the memory heavy application which can be launched fast if it gets more higher order pages thus launcher can prepare the system in advance by triggering the proactive compaction from userspace.

This triggering of proactive compaction is done on a write to sysctl.compaction_proactiveness by user.

[1]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?id=facdaa917c4d5a376d09d25865f5a863f906234a

---
changes in V2:
- remove /proc interface trigger for proactive compaction
- Intention is same that add a way to trigger proactive compaction by user.

changes in V1:
- https://lore.kernel.org/lkml/[email protected]/

 include/linux/compaction.h |  2 ++
 include/linux/mmzone.h     |  1 +
 kernel/sysctl.c            |  2 +-
 mm/compaction.c            | 35 ++++++++++++++++++++++++++++++++---
 4 files changed, 36 insertions(+), 4 deletions(-)
include/linux/compaction.h +2 -0
index 4221888..04d5d9f 100644
@@ -84,6 +84,8 @@ static inline unsigned long compact_gap(unsigned int order)
 extern unsigned int sysctl_compaction_proactiveness;
 extern int sysctl_compaction_handler(struct ctl_table *table, int write,
 			void *buffer, size_t *length, loff_t *ppos);
+extern int compaction_proactiveness_sysctl_handler(struct ctl_table *table,
+		int write, void *buffer, size_t *length, loff_t *ppos);
 extern int sysctl_extfrag_threshold;
 extern int sysctl_compact_unevictable_allowed;
 
include/linux/mmzone.h +1 -0
index 0d53eba..9455809 100644
@@ -815,6 +815,7 @@ typedef struct pglist_data {
 	enum zone_type kcompactd_highest_zoneidx;
 	wait_queue_head_t kcompactd_wait;
 	struct task_struct *kcompactd;
+	bool proactive_compact_trigger;
 #endif
 	/*
 	 * This is a per-node reserve of pages that are not available
kernel/sysctl.c +1 -1
index 14edf84..bed2fad 100644
@@ -2840,7 +2840,7 @@ static struct ctl_table vm_table[] = {
 		.data		= &sysctl_compaction_proactiveness,
 		.maxlen		= sizeof(sysctl_compaction_proactiveness),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= compaction_proactiveness_sysctl_handler,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &one_hundred,
 	},
mm/compaction.c +32 -3
index 84fde27..9056693 100644
@@ -2708,6 +2708,30 @@ static void compact_nodes(void)
  */
 unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
 
+int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write,
+		void *buffer, size_t *length, loff_t *ppos)
+{
+	int rc, nid;
+
+	rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
+	if (rc)
+		return rc;
+
+	if (write && sysctl_compaction_proactiveness) {
+		for_each_online_node(nid) {
+			pg_data_t *pgdat = NODE_DATA(nid);
+
+			if (pgdat->proactive_compact_trigger)
+				continue;
+
+			pgdat->proactive_compact_trigger = true;
+			wake_up_interruptible(&pgdat->kcompactd_wait);
+		}
+	}
+
+	return 0;
+}
+
 /*
  * This is the entry point for compacting all nodes via
  * /proc/sys/vm/compact_memory
@@ -2752,7 +2776,8 @@ void compaction_unregister_node(struct node *node)
 
 static inline bool kcompactd_work_requested(pg_data_t *pgdat)
 {
-	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
+	return pgdat->kcompactd_max_order > 0 || kthread_should_stop() ||
+		pgdat->proactive_compact_trigger;
 }
 
 static bool kcompactd_node_suitable(pg_data_t *pgdat)
@@ -2905,7 +2930,8 @@ static int kcompactd(void *p)
 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
 		if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
 			kcompactd_work_requested(pgdat),
-			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC))) {
+			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC)) &&
+			!pgdat->proactive_compact_trigger) {
 
 			psi_memstall_enter(&pflags);
 			kcompactd_do_work(pgdat);
@@ -2919,7 +2945,7 @@ static int kcompactd(void *p)
 
 			if (proactive_defer) {
 				proactive_defer--;
-				continue;
+				goto loop;
 			}
 			prev_score = fragmentation_score_node(pgdat);
 			proactive_compact_node(pgdat);
@@ -2931,6 +2957,9 @@ static int kcompactd(void *p)
 			proactive_defer = score < prev_score ?
 					0 : 1 << COMPACT_MAX_DEFER_SHIFT;
 		}
+loop:
+		if (pgdat->proactive_compact_trigger)
+			pgdat->proactive_compact_trigger = false;
 	}
 
 	return 0;
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a
member of the Code Aurora Forum, hosted by The Linux Foundation
#1 lore raw
Quoted 33 lines

-----邮件原件-----
发件人: [email protected] <[email protected]> 代表 Charan Teja Reddy 发送时间: 2021年5月18日 21:38
收件人: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected] 抄送: [email protected]; [email protected]; [email protected]; Charan Teja Reddy <[email protected]> 主题: [PATCH V2] mm: compaction: support triggering of proactive compaction by user

The proactive compaction[1] gets triggered for every 500msec and run compaction on the node for COMPACTION_HPAGE_ORDER (usually order-9) pages based on the value set to sysctl.compaction_proactiveness. Triggering the compaction for every 500msec in search of COMPACTION_HPAGE_ORDER pages is not needed for all applications, especially on the embedded system usecases which may have few MB's of RAM. Enabling the proactive compaction in its state will endup in running almost always on such systems.

Other side, proactive compaction can still be very much useful for getting a set of higher order pages in some controllable manner(controlled by using the sysctl.compaction_proactiveness). Thus on systems where enabling the proactive compaction always may proove not required, can trigger the same from user space on write to its sysctl interface. As an example, say app launcher decide to launch the memory heavy application which can be launched fast if it gets more higher order pages thus launcher can prepare the system in advance by triggering the proactive compaction from userspace.

Quoted 2 lines

This triggering of proactive compaction is done on a write to sysctl.compaction_proactiveness by user.

If you want to trigger compaction from userspace, you can use " echo 1 > /proc/sys/vm/compact_memory", there is no need to be so complex.

Quoted 136 lines

[1]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?i d=facdaa917c4d5a376d09d25865f5a863f906234a

---
changes in V2:
- remove /proc interface trigger for proactive compaction
- Intention is same that add a way to trigger proactive compaction by user.

changes in V1:

    -

https://lore.kernel.org/lkml/1619098678-8501-1-git-send-email-charante@co
deaurora.org/

 include/linux/compaction.h |  2 ++
 include/linux/mmzone.h     |  1 +
 kernel/sysctl.c            |  2 +-
 mm/compaction.c            | 35
++++++++++++++++++++++++++++++++---
 4 files changed, 36 insertions(+), 4 deletions(-)
include/linux/compaction.h index renamed from include/linux/compaction.h +0 -0

4221888..04d5d9f 100644

include/linux/compaction.h +0 -0
@@ -84,6 +84,8 @@ static inline unsigned long compact_gap(unsigned int

order) extern unsigned int sysctl_compaction_proactiveness; extern int sysctl_compaction_handler(struct ctl_table *table, int write,

include/linux/compaction.h +2 -0
 			void *buffer, size_t *length, loff_t *ppos);
+extern int compaction_proactiveness_sysctl_handler(struct ctl_table *table,
+		int write, void *buffer, size_t *length, loff_t *ppos);
 extern int sysctl_extfrag_threshold;
 extern int sysctl_compact_unevictable_allowed;
include/linux/mmzone.h index renamed from include/linux/mmzone.h +0 -0

0d53eba..9455809 100644

include/linux/mmzone.h +1 -0
@@ -815,6 +815,7 @@ typedef struct pglist_data {
 	enum zone_type kcompactd_highest_zoneidx;
 	wait_queue_head_t kcompactd_wait;
 	struct task_struct *kcompactd;
+	bool proactive_compact_trigger;
 #endif
 	/*
 	 * This is a per-node reserve of pages that are not available diff --git

a/kernel/sysctl.c b/kernel/sysctl.c index 14edf84..bed2fad 100644

kernel/sysctl.c +1 -1
@@ -2840,7 +2840,7 @@ static struct ctl_table vm_table[] = {
 		.data		= &sysctl_compaction_proactiveness,
 		.maxlen		= sizeof(sysctl_compaction_proactiveness),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= compaction_proactiveness_sysctl_handler,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &one_hundred,
 	},
mm/compaction.c index 84fde27..9056693 renamed from mm/compaction.c +0 -0

100644

mm/compaction.c +32 -4
@@ -2708,6 +2708,30 @@ static void compact_nodes(void)
  */
 unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
 
+int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int
 write,
+		void *buffer, size_t *length, loff_t *ppos) {
+	int rc, nid;
+
+	rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
+	if (rc)
+		return rc;
+
+	if (write && sysctl_compaction_proactiveness) {
+		for_each_online_node(nid) {
+			pg_data_t *pgdat = NODE_DATA(nid);
+
+			if (pgdat->proactive_compact_trigger)
+				continue;
+
+			pgdat->proactive_compact_trigger = true;
+			wake_up_interruptible(&pgdat->kcompactd_wait);
+		}
+	}
+
+	return 0;
+}
+
 /*
  * This is the entry point for compacting all nodes via
  * /proc/sys/vm/compact_memory
@@ -2752,7 +2776,8 @@ void compaction_unregister_node(struct node
 *node)
 
 static inline bool kcompactd_work_requested(pg_data_t *pgdat)  {
-	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
+	return pgdat->kcompactd_max_order > 0 || kthread_should_stop() ||
+		pgdat->proactive_compact_trigger;
 }
 
 static bool kcompactd_node_suitable(pg_data_t *pgdat) @@ -2905,7
+2930,8 @@ static int kcompactd(void *p)
 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
 		if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
 			kcompactd_work_requested(pgdat),
-			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC))) {
+			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC)) &&
+			!pgdat->proactive_compact_trigger) {
 
 			psi_memstall_enter(&pflags);
 			kcompactd_do_work(pgdat);
@@ -2919,7 +2945,7 @@ static int kcompactd(void *p)
 
 			if (proactive_defer) {
 				proactive_defer--;
-				continue;
+				goto loop;
 			}
 			prev_score = fragmentation_score_node(pgdat);
 			proactive_compact_node(pgdat);
@@ -2931,6 +2957,9 @@ static int kcompactd(void *p)
 			proactive_defer = score < prev_score ?
 					0 : 1 << COMPACT_MAX_DEFER_SHIFT;
 		}
+loop:
+		if (pgdat->proactive_compact_trigger)
+			pgdat->proactive_compact_trigger = false;
 	}
 
 	return 0;
--

QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation

#2 lore raw

Thanks Kaiping for your review comments!!

Quoted 3 lines · Chu,Kaiping
Quoted 2 lines

This triggering of proactive compaction is done on a write to sysctl.compaction_proactiveness by user.

If you want to trigger compaction from userspace, you can use " echo 1 > /proc/sys/vm/compact_memory", there is no need to be so complex.

1) compact_memory is intended for debug interface. And moreover we can't issue the compaction in some controlled manner as write to this node triggers the full node compaction. This patch aims at users who want to do the compaction in some controlled manner from user space. Example user is app launcher preparing the system before launching a memory hungry app.

2) Also, with the current implementation of proactive compaction, say user sets the sysctl.compaction_proactiveness, the values can have effect only in the next HPAGE_FRAG_CHECK_INTERVAL_MSEC(500msec), IOW, the proactive compaction can run with the new settings only after 500msec which can make the user to wait for 500msec after setting a value in the compaction_proactiveness to think that the value written is came into effectiveness. Say user want to launch a gaming application which has higher memory requirements and its launch time is proportional to the available higher order pages. So, what he can do to get the larger number of pages is set the compaction_proactivness to higher value, continue launching the application and once finishes can set the proactivness to original value. But with the current implementation the value set may not come into effectiveness at all because of the 500msec delay.Thus,the patch also handles the scenario of requiring the proactive compaction to run immediately once user sets the 'compaction_proactiveness'.

May be I need to update the commit log even more clear about why can't we use the 'compact_memory' and requirements to need to run the proactive compaction immediately once user changes the compaction_proactivness.

Quoted 1 line
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora
Forum, a Linux Foundation Collaborative Project
#3 lore raw
In reply to Charan Teja Reddy

Gentle ping.

Thanks,
Charan

Quoted 152 lines · Charan Teja Reddy

The proactive compaction[1] gets triggered for every 500msec and run compaction on the node for COMPACTION_HPAGE_ORDER (usually order-9) pages based on the value set to sysctl.compaction_proactiveness. Triggering the compaction for every 500msec in search of COMPACTION_HPAGE_ORDER pages is not needed for all applications, especially on the embedded system usecases which may have few MB's of RAM. Enabling the proactive compaction in its state will endup in running almost always on such systems.

Other side, proactive compaction can still be very much useful for getting a set of higher order pages in some controllable manner(controlled by using the sysctl.compaction_proactiveness). Thus on systems where enabling the proactive compaction always may proove not required, can trigger the same from user space on write to its sysctl interface. As an example, say app launcher decide to launch the memory heavy application which can be launched fast if it gets more higher order pages thus launcher can prepare the system in advance by triggering the proactive compaction from userspace.

This triggering of proactive compaction is done on a write to sysctl.compaction_proactiveness by user.

[1]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?id=facdaa917c4d5a376d09d25865f5a863f906234a

---
changes in V2:
- remove /proc interface trigger for proactive compaction
- Intention is same that add a way to trigger proactive compaction by user.

changes in V1:
- https://lore.kernel.org/lkml/[email protected]/

 include/linux/compaction.h |  2 ++
 include/linux/mmzone.h     |  1 +
 kernel/sysctl.c            |  2 +-
 mm/compaction.c            | 35 ++++++++++++++++++++++++++++++++---
 4 files changed, 36 insertions(+), 4 deletions(-)
include/linux/compaction.h +2 -0
index 4221888..04d5d9f 100644
@@ -84,6 +84,8 @@ static inline unsigned long compact_gap(unsigned int order)
 extern unsigned int sysctl_compaction_proactiveness;
 extern int sysctl_compaction_handler(struct ctl_table *table, int write,
 			void *buffer, size_t *length, loff_t *ppos);
+extern int compaction_proactiveness_sysctl_handler(struct ctl_table *table,
+		int write, void *buffer, size_t *length, loff_t *ppos);
 extern int sysctl_extfrag_threshold;
 extern int sysctl_compact_unevictable_allowed;
 
include/linux/mmzone.h +1 -0
index 0d53eba..9455809 100644
@@ -815,6 +815,7 @@ typedef struct pglist_data {
 	enum zone_type kcompactd_highest_zoneidx;
 	wait_queue_head_t kcompactd_wait;
 	struct task_struct *kcompactd;
+	bool proactive_compact_trigger;
 #endif
 	/*
 	 * This is a per-node reserve of pages that are not available
kernel/sysctl.c +1 -1
index 14edf84..bed2fad 100644
@@ -2840,7 +2840,7 @@ static struct ctl_table vm_table[] = {
 		.data		= &sysctl_compaction_proactiveness,
 		.maxlen		= sizeof(sysctl_compaction_proactiveness),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= compaction_proactiveness_sysctl_handler,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &one_hundred,
 	},
mm/compaction.c +32 -3
index 84fde27..9056693 100644
@@ -2708,6 +2708,30 @@ static void compact_nodes(void)
  */
 unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
 
+int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write,
+		void *buffer, size_t *length, loff_t *ppos)
+{
+	int rc, nid;
+
+	rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
+	if (rc)
+		return rc;
+
+	if (write && sysctl_compaction_proactiveness) {
+		for_each_online_node(nid) {
+			pg_data_t *pgdat = NODE_DATA(nid);
+
+			if (pgdat->proactive_compact_trigger)
+				continue;
+
+			pgdat->proactive_compact_trigger = true;
+			wake_up_interruptible(&pgdat->kcompactd_wait);
+		}
+	}
+
+	return 0;
+}
+
 /*
  * This is the entry point for compacting all nodes via
  * /proc/sys/vm/compact_memory
@@ -2752,7 +2776,8 @@ void compaction_unregister_node(struct node *node)
 
 static inline bool kcompactd_work_requested(pg_data_t *pgdat)
 {
-	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
+	return pgdat->kcompactd_max_order > 0 || kthread_should_stop() ||
+		pgdat->proactive_compact_trigger;
 }
 
 static bool kcompactd_node_suitable(pg_data_t *pgdat)
@@ -2905,7 +2930,8 @@ static int kcompactd(void *p)
 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
 		if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
 			kcompactd_work_requested(pgdat),
-			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC))) {
+			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC)) &&
+			!pgdat->proactive_compact_trigger) {
 
 			psi_memstall_enter(&pflags);
 			kcompactd_do_work(pgdat);
@@ -2919,7 +2945,7 @@ static int kcompactd(void *p)
 
 			if (proactive_defer) {
 				proactive_defer--;
-				continue;
+				goto loop;
 			}
 			prev_score = fragmentation_score_node(pgdat);
 			proactive_compact_node(pgdat);
@@ -2931,6 +2957,9 @@ static int kcompactd(void *p)
 			proactive_defer = score < prev_score ?
 					0 : 1 << COMPACT_MAX_DEFER_SHIFT;
 		}
+loop:
+		if (pgdat->proactive_compact_trigger)
+			pgdat->proactive_compact_trigger = false;
 	}
 
 	return 0;
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora
Forum, a Linux Foundation Collaborative Project
#4 lore raw
In reply to Charan Teja Reddy
Quoted 20 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Charan Teja Reddy
Sent: Tuesday, May 18, 2021 6:38 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Nitin Gupta <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; Charan Teja Reddy <[email protected]>
Subject: [PATCH V2] mm: compaction: support triggering of proactive compaction by user

External email: Use caution opening links or attachments

Quoted 9 lines

The proactive compaction[1] gets triggered for every 500msec and run compaction on the node for COMPACTION_HPAGE_ORDER (usually order-9) pages based on the value set to sysctl.compaction_proactiveness. Triggering the compaction for every 500msec in search of COMPACTION_HPAGE_ORDER pages is not needed for all applications, especially on the embedded system usecases which may have few MB's of RAM. Enabling the proactive compaction in its state will endup in running almost always on such systems.

You can disable proactive compaction by setting sysctl.compaction_proactiveness to 0.

Quoted 10 lines

Other side, proactive compaction can still be very much useful for getting a set of higher order pages in some controllable manner(controlled by using the sysctl.compaction_proactiveness). Thus on systems where enabling the proactive compaction always may proove not required, can trigger the same from user space on write to its sysctl interface. As an example, say app launcher decide to launch the memory heavy application which can be launched fast if it gets more higher order pages thus launcher can prepare the system in advance by triggering the proactive compaction from userspace.

You can always do: echo 1 > /proc/sys/vm/compact_memory On a small system, this should not take much time.

Hijacking proactive compaction for one-off compaction (say, before a large app launch) does not sound right to me.

Thanks,
Nitin

#5 lore raw

Thanks Nitin for your inputs!!

Quoted 11 lines · Nitin Gupta

The proactive compaction[1] gets triggered for every 500msec and run compaction on the node for COMPACTION_HPAGE_ORDER (usually order-9) pages based on the value set to sysctl.compaction_proactiveness. Triggering the compaction for every 500msec in search of

COMPACTION_HPAGE_ORDER pages is not needed for all applications,

Quoted 4 lines

especially on the embedded system usecases which may have few MB's of RAM. Enabling the proactive compaction in its state will endup in running almost always on such systems.

You can disable proactive compaction by setting sysctl.compaction_proactiveness to 0.

Agree. But proactive compaction got its own uses too like it knows when to stop the compaction, instead of simply doing the full node compaction, thus we don't want to disable it always.

Quoted 10 lines
Quoted 8 lines

As an example, say app
launcher decide to launch the memory heavy application which can be launched fast if it gets more higher order pages thus launcher can prepare the system in advance by triggering the proactive compaction from userspace.

You can always do: echo 1 > /proc/sys/vm/compact_memory On a small system, this should not take much time.

Hmm... With 3GB Snapdragon system, we have observed that write to compact_memory is taking peak time of 400+msec, could be that MIGRATE_SYNC on a full node is causing this peak, which is much time.

Quoted 3 lines

Hijacking proactive compaction for one-off compaction (say, before a large app launch) does not sound right to me.

Actually we are using the proactive compaction to 'just prepare the system before asking for huge memory' as compact_memory can take longer and is not controllable like proactive compaction.

In the V1 of this patch, we actually created a /proc interface(similar to compact_memory), providing a way to trigger the proactive compaction from user space. https://lore.kernel.org/patchwork/patch/1417064/. But since this involved a new /proc interface addition, in V2 we just implemented an alternative way to it.

Another problem, I think, this patch tried to address is that, in the existing implementation it is not guaranteed the user set value of compaction_proactiveness is effective unless atleast HPAGE_FRAG_CHECK_INTERVAL_MSEC(500msec) is elapsed, Right? Does this seems correct provided we had given this user interface and can't specified any where when this value will be effective(where it comes into effect in the next compact thread wake up for proactive compaction).

Consider the below testcase where a user thinks that the application he is going to run is performance critical thus decides to do the below steps:
1) Save the present the compaction_proactiveness (Say it is zero thus disabled)
2) Set the compaction_proactiveness to 100.
3) Allocate memory for the application.
4) Restore the compaction_proactiveness.(set to disabled again)
5) Then proactive compaction is tried to run.

First, Does the user doing the above steps are valid?
If yes, then we should guarantee to the user that proactive compaction atleast tried to run when the user changed the proactiveness. If not, I feel, we should document that 'once user changed the compaction_proactiveness, he need to wait atleast HPAGE_FRAG_CHECK_INTERVAL_MSEC before considering that the value he tried to set is effective and proactive compaction tried to run on that'. Doesn't this seem okay?

--Charan

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora
Forum, a Linux Foundation Collaborative Project
#6 lore raw
Quoted 87 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Charan Teja Kalla
Sent: Thursday, May 27, 2021 2:28 AM
To: Nitin Gupta <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]
Subject: Re: [PATCH V2] mm: compaction: support triggering of proactive compaction by user

External email: Use caution opening links or attachments

Thanks Nitin for your inputs!!

Quoted 2 lines · Nitin Gupta

The proactive compaction[1] gets triggered for every 500msec and run compaction on the node for COMPACTION_HPAGE_ORDER (usually order-

9)

Quoted 9 lines

pages based on the value set to sysctl.compaction_proactiveness. Triggering the compaction for every 500msec in search of

COMPACTION_HPAGE_ORDER pages is not needed for all applications,

Quoted 4 lines

especially on the embedded system usecases which may have few MB's of RAM. Enabling the proactive compaction in its state will endup in running almost always on such systems.

You can disable proactive compaction by setting

sysctl.compaction_proactiveness to 0.

Agree. But proactive compaction got its own uses too like it knows when to stop the compaction, instead of simply doing the full node compaction, thus we don't want to disable it always.

Quoted 10 lines
Quoted 8 lines

As an example, say app
launcher decide to launch the memory heavy application which can be launched fast if it gets more higher order pages thus launcher can prepare the system in advance by triggering the proactive compaction from userspace.

You can always do: echo 1 > /proc/sys/vm/compact_memory On a small system, this should not take much time.

Hmm... With 3GB Snapdragon system, we have observed that write to compact_memory is taking peak time of 400+msec, could be that MIGRATE_SYNC on a full node is causing this peak, which is much time.

Quoted 3 lines

Hijacking proactive compaction for one-off compaction (say, before a large app launch) does not sound right to me.

Actually we are using the proactive compaction to 'just prepare the system before asking for huge memory' as compact_memory can take longer and is not controllable like proactive compaction.

In the V1 of this patch, we actually created a /proc interface(similar to compact_memory), providing a way to trigger the proactive compaction from user space. https://lore.kernel.org/patchwork/patch/1417064/. But since this involved a new /proc interface addition, in V2 we just implemented an alternative way to it.

Another problem, I think, this patch tried to address is that, in the existing implementation it is not guaranteed the user set value of compaction_proactiveness is effective unless atleast HPAGE_FRAG_CHECK_INTERVAL_MSEC(500msec) is elapsed, Right? Does this seems correct provided we had given this user interface and can't specified any where when this value will be effective(where it comes into effect in the next compact thread wake up for proactive compaction).

Consider the below testcase where a user thinks that the application he is going to run is performance critical thus decides to do the below steps:
1) Save the present the compaction_proactiveness (Say it is zero thus disabled)
2) Set the compaction_proactiveness to 100.
3) Allocate memory for the application.
4) Restore the compaction_proactiveness.(set to disabled again)
5) Then proactive compaction is tried to run.

Quoted 8 lines

First, Does the user doing the above steps are valid?
If yes, then we should guarantee to the user that proactive compaction atleast tried to run when the user changed the proactiveness. If not, I feel, we should document that 'once user changed the compaction_proactiveness, he need to wait atleast HPAGE_FRAG_CHECK_INTERVAL_MSEC before considering that the value he tried to set is effective and proactive compaction tried to run on that'. Doesn't this seem okay?

Proactive compaction does not guarantee if the kernel will be able to achieve fragmentation targets implied from the compaction_proactiveness sysctl. It also does not guarantee how much time it will take to reach desired fragmentation levels (if at all possible). Also, HPAGE_FRAG_CHECK_INTERVAL_MSEC is the maximum sleep time, depending on relative timing of your sysctl input and the timeout.

The intent of proactive compaction is to maintain desired fragmentation levels wrt the default hugepage size in the background so we don't have to pay latency cost associated with on-demand compaction. I don't like the idea of forcing a round of compaction on sysctl write. Maybe add a Kconfig parameter for setting HPAGE_FRAG_CHECK_INTERVAL_MSEC to say 1msec?

Thanks,
Nitin

#7 lore raw

Thanks Nitin for your inputs.

Quoted 8 lines · Nitin Gupta
Quoted 8 lines

First, Does the user doing the above steps are valid?
If yes, then we should guarantee to the user that proactive compaction atleast tried to run when the user changed the proactiveness. If not, I feel, we should document that 'once user changed the compaction_proactiveness, he need to wait atleast HPAGE_FRAG_CHECK_INTERVAL_MSEC before considering that the value he tried to set is effective and proactive compaction tried to run on that'. Doesn't this seem okay?

Quoted 4 lines

Proactive compaction does not guarantee if the kernel will be able to achieve fragmentation targets implied from the compaction_proactiveness sysctl. It also does not guarantee how much time it will take to reach desired fragmentation levels (if at all possible).

Shouldn't we add these lines in the Documentation. Will raise a patch If it is fine.

Quoted 2 lines

Maybe add a Kconfig parameter for setting
HPAGE_FRAG_CHECK_INTERVAL_MSEC to say 1msec?

I really don't have an use case to make the HPAGE_FRAG_CHECK_INTERVAL_MSEC as config option. But should we make it as Kconfig option and let the user decide how aggressively proactive compaction should do the job of system defragmentation in his system? Selection will be limited in the range of 10msec to 500msec, defaults to 500msec.

--Thanks

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora
Forum, a Linux Foundation Collaborative Project
#8 lore raw
In reply to Charan Teja Reddy

+CC linux-api

Quoted 19 lines · Charan Teja Reddy

The proactive compaction[1] gets triggered for every 500msec and run compaction on the node for COMPACTION_HPAGE_ORDER (usually order-9) pages based on the value set to sysctl.compaction_proactiveness. Triggering the compaction for every 500msec in search of COMPACTION_HPAGE_ORDER pages is not needed for all applications, especially on the embedded system usecases which may have few MB's of RAM. Enabling the proactive compaction in its state will endup in running almost always on such systems.

Other side, proactive compaction can still be very much useful for getting a set of higher order pages in some controllable manner(controlled by using the sysctl.compaction_proactiveness). Thus on systems where enabling the proactive compaction always may proove not required, can trigger the same from user space on write to its sysctl interface. As an example, say app launcher decide to launch the memory heavy application which can be launched fast if it gets more higher order pages thus launcher can prepare the system in advance by triggering the proactive compaction from userspace.

Quoted 6 lines

This triggering of proactive compaction is done on a write to sysctl.compaction_proactiveness by user.

[1]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?id=facdaa917c4d5a376d09d25865f5a863f906234a

Cancelling all current sleeps immediately when the controlling variable changes doesn't sound wrong to me.
A question below:

Quoted 108 lines

---
changes in V2:
- remove /proc interface trigger for proactive compaction
- Intention is same that add a way to trigger proactive compaction by user.

changes in V1:
- https://lore.kernel.org/lkml/[email protected]/

 include/linux/compaction.h |  2 ++
 include/linux/mmzone.h     |  1 +
 kernel/sysctl.c            |  2 +-
 mm/compaction.c            | 35 ++++++++++++++++++++++++++++++++---
 4 files changed, 36 insertions(+), 4 deletions(-)
include/linux/compaction.h +2 -0
index 4221888..04d5d9f 100644
@@ -84,6 +84,8 @@ static inline unsigned long compact_gap(unsigned int order)
 extern unsigned int sysctl_compaction_proactiveness;
 extern int sysctl_compaction_handler(struct ctl_table *table, int write,
 			void *buffer, size_t *length, loff_t *ppos);
+extern int compaction_proactiveness_sysctl_handler(struct ctl_table *table,
+		int write, void *buffer, size_t *length, loff_t *ppos);
 extern int sysctl_extfrag_threshold;
 extern int sysctl_compact_unevictable_allowed;
 
include/linux/mmzone.h +1 -0
index 0d53eba..9455809 100644
@@ -815,6 +815,7 @@ typedef struct pglist_data {
 	enum zone_type kcompactd_highest_zoneidx;
 	wait_queue_head_t kcompactd_wait;
 	struct task_struct *kcompactd;
+	bool proactive_compact_trigger;
 #endif
 	/*
 	 * This is a per-node reserve of pages that are not available
kernel/sysctl.c +1 -1
index 14edf84..bed2fad 100644
@@ -2840,7 +2840,7 @@ static struct ctl_table vm_table[] = {
 		.data		= &sysctl_compaction_proactiveness,
 		.maxlen		= sizeof(sysctl_compaction_proactiveness),
 		.mode		= 0644,
-		.proc_handler	= proc_dointvec_minmax,
+		.proc_handler	= compaction_proactiveness_sysctl_handler,
 		.extra1		= SYSCTL_ZERO,
 		.extra2		= &one_hundred,
 	},
mm/compaction.c +28 -2
index 84fde27..9056693 100644
@@ -2708,6 +2708,30 @@ static void compact_nodes(void)
  */
 unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
 
+int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write,
+		void *buffer, size_t *length, loff_t *ppos)
+{
+	int rc, nid;
+
+	rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
+	if (rc)
+		return rc;
+
+	if (write && sysctl_compaction_proactiveness) {
+		for_each_online_node(nid) {
+			pg_data_t *pgdat = NODE_DATA(nid);
+
+			if (pgdat->proactive_compact_trigger)
+				continue;
+
+			pgdat->proactive_compact_trigger = true;
+			wake_up_interruptible(&pgdat->kcompactd_wait);
+		}
+	}
+
+	return 0;
+}
+
 /*
  * This is the entry point for compacting all nodes via
  * /proc/sys/vm/compact_memory
@@ -2752,7 +2776,8 @@ void compaction_unregister_node(struct node *node)
 
 static inline bool kcompactd_work_requested(pg_data_t *pgdat)
 {
-	return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
+	return pgdat->kcompactd_max_order > 0 || kthread_should_stop() ||
+		pgdat->proactive_compact_trigger;
 }
 
 static bool kcompactd_node_suitable(pg_data_t *pgdat)
@@ -2905,7 +2930,8 @@ static int kcompactd(void *p)
 		trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
 		if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
 			kcompactd_work_requested(pgdat),
-			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC))) {
+			msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC)) &&
+			!pgdat->proactive_compact_trigger) {
 
 			psi_memstall_enter(&pflags);
 			kcompactd_do_work(pgdat);

mm/compaction.c static int kcompactd(void *p) · line 2948

Quoted 5 lines
mm/compaction.c +1 -1
@@ -2919,7 +2945,7 @@ static int kcompactd(void *p)
 
 			if (proactive_defer) {
 				proactive_defer--;
-				continue;
+				goto loop;

I don't understand this part. If we kick kcompactd from the sysctl handler because we are changing proactiveness, shouldn't we also discard any accumulated defer score?

Quoted 14 lines
mm/compaction.c +3 -0
 			}
 			prev_score = fragmentation_score_node(pgdat);
 			proactive_compact_node(pgdat);
@@ -2931,6 +2957,9 @@ static int kcompactd(void *p)
 			proactive_defer = score < prev_score ?
 					0 : 1 << COMPACT_MAX_DEFER_SHIFT;
 		}
+loop:
+		if (pgdat->proactive_compact_trigger)
+			pgdat->proactive_compact_trigger = false;
 	}
 
 	return 0;
#9 lore raw

Thanks Vlastimil for your inputs!!

Quoted 12 lines · Vlastimil Babka
Quoted 11 lines · Charan Teja Reddy

The proactive compaction[1] gets triggered for every 500msec and run compaction on the node for COMPACTION_HPAGE_ORDER (usually order-9) pages based on the value set to sysctl.compaction_proactiveness. Triggering the compaction for every 500msec in search of COMPACTION_HPAGE_ORDER pages is not needed for all applications, especially on the embedded system usecases which may have few MB's of RAM. Enabling the proactive compaction in its state will endup in running almost always on such systems.
This triggering of proactive compaction is done on a write to sysctl.compaction_proactiveness by user.

Quoted 5 lines
Quoted 3 lines

[1]https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?id=facdaa917c4d5a376d09d25865f5a863f906234a

Cancelling all current sleeps immediately when the controlling variable changes doesn't sound wrong to me.

Agree that cancelling sleeps is not wrong here.

Quoted 13 lines

A question below:

mm/compaction.c static int kcompactd(void *p) · line 2948

Quoted 8 lines

---
changes in V2:
- remove /proc interface trigger for proactive compaction
- Intention is same that add a way to trigger proactive compaction by user.

mm/compaction.c +1 -1
 			if (proactive_defer) {
 				proactive_defer--;
-				continue;
+				goto loop;

I don't understand this part. If we kick kcompactd from the sysctl handler because we are changing proactiveness, shouldn't we also discard any accumulated defer score?

Yes, we should be discarding the accumulated defer score when user changing the proactiveness, even when writing higher proactiveness value than for which it was deferred. Will raise V3 for this.

Quoted 1 line

--Charan

Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora
Forum, a Linux Foundation Collaborative Projec t