title: good explanation of __read_mostly, __init, __exit macros
body: <p>The macro expansion of <code>__read_mostly</code> :</p>
  
  <pre><code>#define __read_mostly __attribute__((__section__(".data..read_mostly"))
  </code></pre>
  
  <p>This one is from <code>cache.h</code></p>
  
  <p><code>__init</code>:</p>
  
  <pre><code>#define __init          __section(.init.text) __cold notrace
  </code></pre>
  
  <p>from <code>init.h</code></p>
  
  <p><code>__exit</code>:</p>
  
  <pre><code>#define __exit          __section(.exit.text) __exitused __cold notrace
  </code></pre>
  
  <p>After searching through net i have not found any good explanation of
  what is happening there.</p>
  
  <p>Additonal question : I have heard about various "linker magic"
  employed in kernel development. Any information
  regarding this will be wonderful.</p>
  
  <p>I have some ideas about these macros about <strong>what they do</strong>. Like <code>__init</code> supposed to indicate that the function code can be removed after initialization. <code>__read_mostly</code> is for indicating that the data is seldom written and by this it minimizes cache misses. But i have not idea about <strong>How they do it</strong>. I mean they are <code>gcc</code> extensions. So in theory they can be demonstrated by small userland c code.  </p>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>I have tried to test the <code>__section__</code> with arbitrary section name. the test code :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define __read_mostly __attribute__((__section__("MY_DATA")))
  
  struct ro {
      char a;
      int b;
      char * c;
  };
  
  struct ro my_ro  __read_mostly = {
      .a = 'a',
      .b = 3,
      .c = NULL,
  };
  
  
  int main(int argc, char **argv) {
      printf("hello");
      printf("my ro %c %d %p \n", my_ro.a, my_ro.b, my_ro.c);
      return 0;
  }
  </code></pre>
  
  <p>Now with <code>__read_mostly</code> the generated assembly code :</p>
  
  <pre><code>    .file   "ro.c"
  .globl my_ro
      .section    MY_DATA,"aw",@progbits
      .align 16
      .type   my_ro, @object
      .size   my_ro, 16
  my_ro:
      .byte   97
      .zero   3
      .long   3
      .quad   0
      .section    .rodata
  .LC0:
      .string "hello"
  .LC1:
      .string "my ro %c %d %p \n"
      .text
  .globl main
      .type   main, @function
  main:
  .LFB0:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset 6, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register 6
      pushq   %rbx
      subq    $24, %rsp
      movl    %edi, -20(%rbp)
      movq    %rsi, -32(%rbp)
      movl    $.LC0, %eax
      movq    %rax, %rdi
      movl    $0, %eax
      .cfi_offset 3, -24
      call    printf
      movq    my_ro+8(%rip), %rcx
      movl    my_ro+4(%rip), %edx
      movzbl  my_ro(%rip), %eax
      movsbl  %al, %ebx
      movl    $.LC1, %eax
      movl    %ebx, %esi
      movq    %rax, %rdi
      movl    $0, %eax
      call    printf
      movl    $0, %eax
      addq    $24, %rsp
      popq    %rbx
      leave
      .cfi_def_cfa 7, 8
      ret
      .cfi_endproc
  .LFE0:
      .size   main, .-main
      .ident  "GCC: (GNU) 4.4.6 20110731 (Red Hat 4.4.6-3)"
      .section    .note.GNU-stack,"",@progbits
  </code></pre>
  
  <p>Now without the <code>__read_mostly</code> macro the assembly code remains more or less the same.</p>
  
  <p>this is the diff</p>
  
  <pre><code>--- rm.S    2012-07-17 16:17:05.795771270 +0600
  +++ rw.S    2012-07-17 16:19:08.633895693 +0600
  @@ -1,6 +1,6 @@
      .file   "ro.c"
   .globl my_ro
  -   .section    MY_DATA,"aw",@progbits
  +   .data
      .align 16
      .type   my_ro, @object
      .size   my_ro, 16
  </code></pre>
  
  <p>So essentially only the a subsection is created, nothing fancy.</p>
  
  <p>Even the objdump disassmbly does not show any difference. </p>
  
  <p>So my final conclusion about them, its the <strong>linker's job</strong> do something for data section marked with a special name. I think linux kernel uses some kind of custom <strong>linker script</strong> do achieve these things.</p>
  
  <p>One of the thing about <code>__read_mostly</code>, data which were put there can be grouped and managed in a way so that cache misses can be reduced.</p>
  
  <p>Someone at <strong>lkml</strong> submitted a patch to remove <code>__read_mostly</code>. Which spawned a fascinated discussion on the merits and demerits of <code>__read_mostly</code>.</p>
  
  <p>here is the link : <a href="https://lkml.org/lkml/2007/12/13/477">https://lkml.org/lkml/2007/12/13/477</a></p>
  
  <p>I will post further update on <code>__init</code> and <code>__exit</code>.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>These macros <code>__init</code> , <code>__exit</code> and <code>__read_mostly</code> put the contents of <strong>data</strong>(in case of <code>__read_mostly</code>) and <strong>text</strong>(in cases of <code>__init</code> and <code>__exit</code>) are put into custom named sections. These sections are utilized by the linker. Now as linker is not used as its default behaviour for various reasons, A <strong>linker script</strong> is employed to achieve the purposes of these macros.</p>
  
  <p>A background may be found how a custom linker script can be used to eliminate <strong>dead code</strong>(code which is linked to by linker but never executed). This issue is of very high importance in embedded scenarios. This document discusses how a linker script can be fine tuned to remove <strong>dead code</strong> : elinux.org/images/2/2d/ELC2010-gc-sections_Denys_Vlasenko.pdf</p>
  
  <p>In case kernel the initial linker script can be found <code>include/asm-generic/vmlinux.lds.h</code>. This is not the final script. This is kind of <strong>starting point</strong>, the <strong>linker script</strong> is further modified for different platforms.</p>
  
  <p>A quick look at this file the portions of interest can immediately found:</p>
  
  <pre><code>#define READ_MOSTLY_DATA(align)                     \
      . = ALIGN(align);                       \
      *(.data..read_mostly)                       \
      . = ALIGN(align);
  </code></pre>
  
  <p>It seems this section is using the ".data..readmostly" section.</p>
  
  <p>Also you can find <code>__init</code> and <code>__exit</code> section related linker commands :</p>
  
  <pre><code>#define INIT_TEXT                           \
      *(.init.text)                           \
      DEV_DISCARD(init.text)                      \
      CPU_DISCARD(init.text)                      \
      MEM_DISCARD(init.text)
  
  #define EXIT_TEXT                           \
      *(.exit.text)                           \
      DEV_DISCARD(exit.text)                      \
      CPU_DISCARD(exit.text)                      \
      MEM_DISCARD(exit.text)
  </code></pre>
  
  <p>Linking seems pretty complex thing to do :)</p>