[PATCH RFC 08/12] kcompactd: Add housekeeping notifier for dynamic mask update

Qiliang Yuan posted 12 patches 2 days, 21 hours ago
[PATCH RFC 08/12] kcompactd: Add housekeeping notifier for dynamic mask update
Posted by Qiliang Yuan 2 days, 21 hours ago
The kcompactd threads should respect the housekeeping mask for kthreads
(HK_TYPE_KTHREAD) to avoid running on isolated CPUs. Currently, they
are created without explicit affinity constraints beyond the node
preference.

Add housekeeping_affine() to kcompactd_run() to set initial affinity,
and register a housekeeping notifier to update kcompactd affinity
when the HK_TYPE_KTHREAD mask is changed at runtime.

Signed-off-by: Qiliang Yuan <realwujing@gmail.com>
Signed-off-by: Qiliang Yuan <yuanql9@chinatelecom.cn>
---
 mm/compaction.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/mm/compaction.c b/mm/compaction.c
index 1e8f8eca318c..495106d970f1 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -24,6 +24,7 @@
 #include <linux/page_owner.h>
 #include <linux/psi.h>
 #include <linux/cpuset.h>
+#include <linux/sched/isolation.h>
 #include "internal.h"
 
 #ifdef CONFIG_COMPACTION
@@ -3246,6 +3247,7 @@ void __meminit kcompactd_run(int nid)
 		pr_err("Failed to start kcompactd on node %d\n", nid);
 		pgdat->kcompactd = NULL;
 	} else {
+		housekeeping_affine(pgdat->kcompactd, HK_TYPE_KTHREAD);
 		wake_up_process(pgdat->kcompactd);
 	}
 }
@@ -3320,6 +3322,29 @@ static const struct ctl_table vm_compaction[] = {
 	},
 };
 
+static int kcompactd_housekeeping_reconfigure(struct notifier_block *nb,
+					     unsigned long action, void *data)
+{
+	unsigned int type = (unsigned long)data;
+
+	if (action == HK_UPDATE_MASK && type == HK_TYPE_KTHREAD) {
+		int nid;
+
+		for_each_node_state(nid, N_MEMORY) {
+			pg_data_t *pgdat = NODE_DATA(nid);
+
+			if (pgdat->kcompactd)
+				housekeeping_affine(pgdat->kcompactd, HK_TYPE_KTHREAD);
+		}
+	}
+
+	return NOTIFY_OK;
+}
+
+static struct notifier_block kcompactd_housekeeping_nb = {
+	.notifier_call = kcompactd_housekeeping_reconfigure,
+};
+
 static int __init kcompactd_init(void)
 {
 	int nid;
@@ -3327,6 +3352,7 @@ static int __init kcompactd_init(void)
 	for_each_node_state(nid, N_MEMORY)
 		kcompactd_run(nid);
 	register_sysctl_init("vm", vm_compaction);
+	housekeeping_register_notifier(&kcompactd_housekeeping_nb);
 	return 0;
 }
 subsys_initcall(kcompactd_init)

-- 
2.51.0
Re: [PATCH RFC 08/12] kcompactd: Add housekeeping notifier for dynamic mask update
Posted by Zi Yan 2 days, 13 hours ago
On 6 Feb 2026, at 2:04, Qiliang Yuan wrote:

> The kcompactd threads should respect the housekeeping mask for kthreads
> (HK_TYPE_KTHREAD) to avoid running on isolated CPUs. Currently, they
> are created without explicit affinity constraints beyond the node
> preference.
>
> Add housekeeping_affine() to kcompactd_run() to set initial affinity,
> and register a housekeeping notifier to update kcompactd affinity
> when the HK_TYPE_KTHREAD mask is changed at runtime.
>

How about khugepaged and kswapd?

khugepaged thread is created in start_stop_khugepaged() from mm/khugepaged.c.
kswapd threads are created in kswapd_init() from mm/vmscan.c.

> Signed-off-by: Qiliang Yuan <realwujing@gmail.com>
> Signed-off-by: Qiliang Yuan <yuanql9@chinatelecom.cn>
> ---
>  mm/compaction.c | 26 ++++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)
>


Best Regards,
Yan, Zi