whats wrong with my code ? trying to lock and iterate through read tasks in cfs rq

From: nad gam
Date: Fri Mar 18 2016 - 19:06:38 EST


here is the code of system call I implemented trying to iterate
through cfs tasks of cpu_num rq and retrieve their pids :

#include <linux/kernel.h>
#include <linux/sched.h>
#include "/home/user/linux-3.14.1/kernel/sched/sched.h"
#include <linux/slab.h>
#include <asm/uaccess.h>

asmlinkage long sys_get_tasks_from_rq(int cpu_num,void* dst)
{
struct rq * my_rq;

my_rq=cpu_rq(cpu_num);
int* pids = (int*) kmalloc(sizeof(int)*my_rq->nr_running,GFP_KERNEL);
struct task_struct *p, *n;
int i=0;
raw_spin_lock_irq(&my_rq->lock);
list_for_each_entry_safe(p, n, &my_rq->cfs_tasks, se.group_node){
pids[i]=task_pid_vnr(p);
i++;
}

raw_spin_unlock_irq(&my_rq->lock);

copy_to_user(dst,pids,300);
return 0;
}


when I try to use it in user space I get segmenation fault :

int main(int argc,char** argv){
int cpuNum = atoi(argv[1]);
int pids[300];
syscall(319,cpuNum,pids);
int i;
for (i=0;i<10;i++){
printf("%d \t",pids[i]);
}
}

So I guess Im doing something wrong :/ Could some plz direct me for
where my problem is? Thanks !