[Ubuntu 14.04] Setting process affinity in kernel mode (1/2 – kernel modification)

Standard

Prerequisite)

Step 1 and 2 from http://study.prl85.com/building-ubuntu-kernel/
(=Ready to compile kernel)

Procedure)

1. Find the file containing sched_setaffinity function from non-compiled kernel source directory.
(In this case, kernel/sched/core.c)

bsw@bsw-P5K:~$ cd linux-3.13.0-orig/
bsw@bsw-P5K:~/linux-3.13.0-orig$ grep -R "long sched_setaffinity" *
include/linux/sched.h:extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);
kernel/sched/core.c:long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)

2. In kernel/sched/core.c, add EXPORT_SYMBOL_GPL(sched_setaffinity); after the end of sched_setaffinity() definition.

long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
{
cpumask_var_t cpus_allowed, new_mask;
struct task_struct *p;
int retval;
...
out_unlock:
free_cpumask_var(new_mask);
out_free_cpus_allowed:
free_cpumask_var(cpus_allowed);
out_put_task:
put_task_struct(p);
return retval;
}
EXPORT_SYMBOL_GPL(sched_setaffinity);

3. In kernel/sched/core.c, add EXPORT_SYMBOL_GPL(sched_getaffinity); after the end of sched_getaffinity() definition.

long sched_getaffinity(pid_t pid, struct cpumask *mask)
{
        struct task_struct *p;
        unsigned long flags;
        int retval;

        rcu_read_lock();

        retval = -ESRCH;
        p = find_process_by_pid(pid);
        if (!p)
                goto out_unlock;

        retval = security_task_getscheduler(p);
        if (retval)
                goto out_unlock;

        raw_spin_lock_irqsave(&p->pi_lock, flags);
        cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
        raw_spin_unlock_irqrestore(&p->pi_lock, flags);

out_unlock:
        rcu_read_unlock();

        return retval;
}
EXPORT_SYMBOL_GPL(sched_getaffinity);

4. Build new kernel and reboot
(Refer to step 5~9 from http://study.prl85.com/building-ubuntu-kernel)

Reference)
http://stackoverflow.com/questions/1151285/setting-process-affinity-in-kernel-mode
http://study.prl85.com/building-ubuntu-kernel

Leave a Reply

Your email address will not be published. Required fields are marked *