Wednesday, May 28, 2008

Linux High Priority Kernel Thread

Warning: This post contains only kindergarten level techniques about Linux kernel.

I met a problem that a real-time user space program with many threads has higher priority than my computation intensive kernel thread.

I have to adjust the priority of this kernel thread. But unfortunately, I found that the kthread_run doesn't provide me any way to set the priority of my kernel thread. So I wondered how to do it? Fortunately, I found the way to set the priority of a kernel thread. Using

struct sched_param param;
param.sched_priority = ;
if (sched_setscheduler(current, SCHED_FIFO, &param) != 0)
printk("set scheduler and its priority failed\n");

This effectively sets the process's scheduler to FIFO and priority to .

However, when I try insmod .ko, it complains the symbol sched_setscheduler is not found. The module insertion fails just like that. However, I tried to cat /proc/kallsyms, sched_setscheduler is right there, so I don't know what's wrong here.

If you are a newbie like me, you might be scratching your head or starts gazing the ceiling and think, why?

After one hour of trying, I eventually noticed in sched.c, after the function body is declared

int sched_setscheduler(struct task_struct *p, int policy,
struct sched_param *param)
{
...
}
EXPORT_SYMBOL_GPL(sched_setscheduler);

The key is EXPORT_SYMBOL_GPL limits the functions to be linked by modules using GPL.
And my module just didn't contain any license statement. So I added

MODULE_LICENSE("GPL");

Now, everything works like a charm!

No comments: