title: How to execute a piece of kernel code on all CPUs?
body: <p>I'm trying to make a kernel module to enable FOP compatibility mode for x87 FPU. This is done via setting bit 2 in <code>IA32_MISC_ENABLE</code> MSR. Here's the code:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;linux/init.h&gt;
  #include &lt;linux/module.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;asm/msr-index.h&gt;
  #include &lt;asm/msr.h&gt;
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("10110111");
  MODULE_DESCRIPTION("Module to enable FOPcode compatibility mode");
  MODULE_VERSION("0.1");
  
  static int __init fopCompat_init(void)
  {
     unsigned long long misc_enable=native_read_msr(MSR_IA32_MISC_ENABLE);
     printk(KERN_INFO "Before trying to set FOP_COMPAT, IA32_MISC_ENABLE=%llx,"
                      " i.e. FOP_COMPAT is %senabled\n"
                      ,misc_enable,misc_enable&amp;MSR_IA32_MISC_ENABLE_X87_COMPAT?"":"NOT ");
  
     wrmsrl(MSR_IA32_MISC_ENABLE,misc_enable|MSR_IA32_MISC_ENABLE_X87_COMPAT);
     misc_enable=native_read_msr(MSR_IA32_MISC_ENABLE);
  
     printk(KERN_INFO "Tried to set FOP_COMPAT. Result: IA32_MISC_ENABLE=%llx,"
                      " i.e. FOP_COMPAT is now %senabled\n"
                      ,misc_enable,misc_enable&amp;MSR_IA32_MISC_ENABLE_X87_COMPAT?"":"NOT ");
     return 0;
  }
  
  static void __exit fopCompat_exit(void)
  {
     const unsigned long long misc_enable=native_read_msr(MSR_IA32_MISC_ENABLE);
     printk(KERN_INFO "Quitting FOP-compat with IA32_MISC_ENABLE=%llx\n",misc_enable);
     if(!(misc_enable &amp; MSR_IA32_MISC_ENABLE_X87_COMPAT))
         printk(KERN_INFO "NOTE: seems some CPUs still have to be set up, "
                          "or compatibility mode will work inconsistently\n");
     printk(KERN_INFO "\n");
  }
  
  module_init(fopCompat_init);
  module_exit(fopCompat_exit);
  </code></pre>
  
  <p>It seems to work, but on multiple <code>insmod/rmmod</code> cycles I sometimes get <code>dmesg</code> output that the compatibility mode wasn't still enabled, although it was immediately after doing <code>wrmsr</code>. After some thinking I realized that it's because the module code was executed on different logical CPUs (I have Core i7 with 4 cores*HT=8 logical CPUs), so I had a 1/8 chance of getting "enabled" print on doing <code>rmmod</code>. After repeating the cycle for about 20 times I got consistent "enabled" prints, and my userspace application happily works with it.</p>
  
  <p>So now my question is: how can I make my code execute on all logical CPUs present on the system, so as to enable compatibility mode for all of them?</p>