title: Access "current_task" pointer of another cpu in a SMP based linux system
body: <p>I'm writing some kernel driver where I need to check which thread is running at a certain point on another cores. My driver runs one kernel thread on per core and I need to sync from time to time some of the threads to do certain task. What I can observe from debugging logs is that sometimes one thread waits for some other threads too much. I did some patch where I store the <code>__preempt_count</code> on other cores to check if any softirq/hardirq or preemption disabled delays my thread.
  I also used the FTRACE to check the irqsoff and preemptirqsoff for the max duration of IRQs off and preemption disabled. </p>
  
  <p>Till now I was able to spot kerneloops thread which is disabling interrupts up to 20msec, which I find too long.
  Did a <code>systemctl disable kerneloops</code> and got rid of this issue.</p>
  
  <p>Now I seem to deal with some preemption disabled windows. For future analysis of this driver I need a way to figure out which threads are being executed at a particular point in time on other cores. I'm trying to use FTRACE mainly with events for IRQ entry/exit, I also use <code>trace_printk</code> to push some debug log in the ftrace buffer to to have everything in one log, etc.</p>
  
  <p>However, one thing that I would like to do is to access the <code>current_task</code> structure of other cores (the <code>current</code> ptr) and print the <code>comm</code> field which gives the name of the task (or pid value).
  But I'm having hard times in getting this done.</p>
  
  <p>For <code>__preempt_count</code> I had no issue:</p>
  
  <pre><code>int *p = per_cpu_ptr(&amp;__preempt_count,cpu);
  pr_info("Preempt count of cpu%u is 0x%08x\n",cpu,*p);
  </code></pre>
  
  <p>So far I had no issue with declaring or accessing per cpu variables, but for some reason, the <code>current_task</code> pointer triggers a page fault when trying to access it.</p>
  
  <pre><code>char buf[10];
  struct task_struct *task = per_cpu_ptr(current_task,cpu);
  snprintf(buf,8,"%s",task-&gt;comm);
  pr_info("Task name: %s",buf);
  </code></pre>
  
  <p>Above code triggers always a page fault, NULL ptr bla bla.
  I couldn't find the reason till now. I tried to print the pointer value for <code>task</code>, I got the same page fault.</p>
  
  <p>Might be this because the address is not accessible by other cores? In kernel space should not be the case afaik. I also had no issue till now with per core variables and I played a lot with this.</p>
  
  <p>Bottom line: what would be the right approach to access the <code>current_task</code> of other cores and print the comm/pid fields?</p>
  
  <p>Many Thanks,</p>
  
  <p>Daniel</p>