title: How to add a field to kernel task_struct?
body: <p>I'm trying to write a kernel module to detect a fork bomb, and to do this, I want to add a field <code>int descendantCount</code> to <code>task_struct</code>. This is my code so far:</p>
  
  <pre><code>struct task_struct *pTask;
  for_each_process(pTask)
  {
      struct task_struct *p; 
      *p = *pTask; 
  
      //trace back to every ancestor 
      for(p = current; p != &amp;init_task; p-&gt;parent)
      {
          //increment the descendant count of p's parent
          p-&gt;descendantCount = p-&gt;descendantCount +1  //want to do something like this
  
      }
  } 
  </code></pre>
  
  <p>Basically, I'm trying to loop through every process, and for each process, go through all of it's ancestors and increment the ancestor's <code>descendantCount</code>, which is the field that I want to add to <code>task_struct</code>. </p>
  
  <p>I found <a href="https://stackoverflow.com/questions/8044652/adding-entry-to-task-struct-and-initializing-to-default-value">this</a>, and <a href="http://www.linuxquestions.org/questions/programming-9/adding-a-new-field-to-task_struct-310638/" rel="nofollow noreferrer">this</a>, but I'm just still really confused on how I would go about doing this, as I'm new to kernel programming... Should I be going to <code>include/linux/sched.h</code> and adding a field there? Something like this: </p>
  
  <pre><code> struct task_struct { 
   ...... 
  
   pid_t pid; 
  
   .... 
   int descendantCount; 
  
   }
  </code></pre>
  
  <p>Any help would be greatly appreciated, thank you!! </p>