I'm writing a kernel module that iterates over all processes (using the for_each_process
macro), now, I would like to migrate ones that meet certain criteria to a new NUMA node or processor. I found a few functions defined as extern
in the sched.h
:
extern void sched_setnuma(struct task_struct *p, int node);
extern int migrate_task_to(struct task_struct *p, int cpu);
extern int migrate_swap(struct task_struct *, struct task_struct *);
But can't use them cause the kernel is already compiled.
Is there any way to use these functions or similar ones to achieve a task migration?
EDIT
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched/signal.h>
#include <linux/sched.h>
#include <linux/kprobes.h>
#include <linux/kallsyms.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mohammad Siavashi");
MODULE_DESCRIPTION("This is a test module");
int (*migrate_task_to_func)(struct task_struct *p, int cpu);
void start(void)
{
struct task_struct *t;
for_each_process(t)
{
if (strcmp(t->comm, "stream_task") == 0)
{
migrate_task_to_func(t, 2);
}
}
}
static int __init hello_init(void)
{
migrate_task_to_func = (void *)kallsyms_lookup_name("migrate_task_to");
start();
return 0;
}
static void __exit hello_cleanup(void)
{
printk(KERN_INFO "Cleaning up module.\n");
}
module_init(hello_init);
module_exit(hello_cleanup);