2

I have written an LKM in Linux. The ioctl function, my_ioctl is called by a user-level program foo.c. I want to change the scheduling policy of foo.c. Therefore I am doing the following in my my_ioctl function from this link:

struct sched_attr attr;
int ret;
unsigned int flags = 0;

attr.size = sizeof(attr);
attr.sched_flags = 0;
attr.sched_nice = 0;
attr.sched_priority = 0;

/* This creates a 10ms/30ms reservation */
attr.sched_policy = SCHED_DEADLINE;
attr.sched_runtime = 10 * 1000 * 1000;
attr.sched_period = attr.sched_deadline = 30 * 1000 * 1000;

ret = sched_setattr(current->pid, &attr, flags);
if (ret < 0) {
    perror("sched_setattr");
    exit(-1);
}

The sched_setattr is following:

int sched_setattr(pid_t pid,
          const struct sched_attr *attr,
          unsigned int flags) {
    return syscall(__NR_sched_setattr, pid, attr, flags);
}

I have changed the syscall to sys_mycall because it's LKM. struct sched_attr is also defined in the above mentioned Linux Kernel documentation link. However, I could not change the scheduling policy by this. It throws me error like scheduling policy cannot be changed from kernel space. I don't understand why this is the case. There is an utility chrt which does the same thing for a process from the user-space; then why is this not possible from a LKM? Or am I missing something?

soham
  • 1,508
  • 6
  • 30
  • 47
  • Can you show what your #include statements look like at the top of your file? – Aditya Garg Jul 07 '18 at 01:10
  • Im trying to reproduce your error, but my LKM doesn't recognize sched_setattr – Aditya Garg Jul 07 '18 at 01:23
  • It won't recognize. You have to write your own (kernel) library where you have to make kernel specific syscall i.e `sys_mycall` as I mentioned. My `#include` has my own header file which has `sched_setattr` defined as I have shown in the main post. – soham Jul 08 '18 at 02:02
  • Im curious, if you just want to change the scheduling parameters of a user level program, why do you need to go into kernel space at all? You can construct the desired sched_attr struct in userspace and then call the system call sched_setattr. If you pass in 0 for the PID, it changes the parameters on the calling thread. – Aditya Garg Jul 09 '18 at 20:02
  • That's true. It can be done from user space. However, we may need to have a central control because this is related to scheduling which may affect other processes. That's why kernel-space management is better, in my opinion. – soham Jul 10 '18 at 20:15

0 Answers0