static-keys.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. Static Keys
  2. -----------
  3. By: Jason Baron <jbaron@redhat.com>
  4. 0) Abstract
  5. Static keys allows the inclusion of seldom used features in
  6. performance-sensitive fast-path kernel code, via a GCC feature and a code
  7. patching technique. A quick example:
  8. struct static_key key = STATIC_KEY_INIT_FALSE;
  9. ...
  10. if (static_key_false(&key))
  11. do unlikely code
  12. else
  13. do likely code
  14. ...
  15. static_key_slow_inc();
  16. ...
  17. static_key_slow_inc();
  18. ...
  19. The static_key_false() branch will be generated into the code with as little
  20. impact to the likely code path as possible.
  21. 1) Motivation
  22. Currently, tracepoints are implemented using a conditional branch. The
  23. conditional check requires checking a global variable for each tracepoint.
  24. Although the overhead of this check is small, it increases when the memory
  25. cache comes under pressure (memory cache lines for these global variables may
  26. be shared with other memory accesses). As we increase the number of tracepoints
  27. in the kernel this overhead may become more of an issue. In addition,
  28. tracepoints are often dormant (disabled) and provide no direct kernel
  29. functionality. Thus, it is highly desirable to reduce their impact as much as
  30. possible. Although tracepoints are the original motivation for this work, other
  31. kernel code paths should be able to make use of the static keys facility.
  32. 2) Solution
  33. gcc (v4.5) adds a new 'asm goto' statement that allows branching to a label:
  34. http://gcc.gnu.org/ml/gcc-patches/2009-07/msg01556.html
  35. Using the 'asm goto', we can create branches that are either taken or not taken
  36. by default, without the need to check memory. Then, at run-time, we can patch
  37. the branch site to change the branch direction.
  38. For example, if we have a simple branch that is disabled by default:
  39. if (static_key_false(&key))
  40. printk("I am the true branch\n");
  41. Thus, by default the 'printk' will not be emitted. And the code generated will
  42. consist of a single atomic 'no-op' instruction (5 bytes on x86), in the
  43. straight-line code path. When the branch is 'flipped', we will patch the
  44. 'no-op' in the straight-line codepath with a 'jump' instruction to the
  45. out-of-line true branch. Thus, changing branch direction is expensive but
  46. branch selection is basically 'free'. That is the basic tradeoff of this
  47. optimization.
  48. This lowlevel patching mechanism is called 'jump label patching', and it gives
  49. the basis for the static keys facility.
  50. 3) Static key label API, usage and examples:
  51. In order to make use of this optimization you must first define a key:
  52. struct static_key key;
  53. Which is initialized as:
  54. struct static_key key = STATIC_KEY_INIT_TRUE;
  55. or:
  56. struct static_key key = STATIC_KEY_INIT_FALSE;
  57. If the key is not initialized, it is default false. The 'struct static_key',
  58. must be a 'global'. That is, it can't be allocated on the stack or dynamically
  59. allocated at run-time.
  60. The key is then used in code as:
  61. if (static_key_false(&key))
  62. do unlikely code
  63. else
  64. do likely code
  65. Or:
  66. if (static_key_true(&key))
  67. do likely code
  68. else
  69. do unlikely code
  70. A key that is initialized via 'STATIC_KEY_INIT_FALSE', must be used in a
  71. 'static_key_false()' construct. Likewise, a key initialized via
  72. 'STATIC_KEY_INIT_TRUE' must be used in a 'static_key_true()' construct. A
  73. single key can be used in many branches, but all the branches must match the
  74. way that the key has been initialized.
  75. The branch(es) can then be switched via:
  76. static_key_slow_inc(&key);
  77. ...
  78. static_key_slow_dec(&key);
  79. Thus, 'static_key_slow_inc()' means 'make the branch true', and
  80. 'static_key_slow_dec()' means 'make the branch false' with appropriate
  81. reference counting. For example, if the key is initialized true, a
  82. static_key_slow_dec(), will switch the branch to false. And a subsequent
  83. static_key_slow_inc(), will change the branch back to true. Likewise, if the
  84. key is initialized false, a 'static_key_slow_inc()', will change the branch to
  85. true. And then a 'static_key_slow_dec()', will again make the branch false.
  86. An example usage in the kernel is the implementation of tracepoints:
  87. static inline void trace_##name(proto) \
  88. { \
  89. if (static_key_false(&__tracepoint_##name.key)) \
  90. __DO_TRACE(&__tracepoint_##name, \
  91. TP_PROTO(data_proto), \
  92. TP_ARGS(data_args), \
  93. TP_CONDITION(cond)); \
  94. }
  95. Tracepoints are disabled by default, and can be placed in performance critical
  96. pieces of the kernel. Thus, by using a static key, the tracepoints can have
  97. absolutely minimal impact when not in use.
  98. 4) Architecture level code patching interface, 'jump labels'
  99. There are a few functions and macros that architectures must implement in order
  100. to take advantage of this optimization. If there is no architecture support, we
  101. simply fall back to a traditional, load, test, and jump sequence.
  102. * select HAVE_ARCH_JUMP_LABEL, see: arch/x86/Kconfig
  103. * #define JUMP_LABEL_NOP_SIZE, see: arch/x86/include/asm/jump_label.h
  104. * __always_inline bool arch_static_branch(struct static_key *key), see:
  105. arch/x86/include/asm/jump_label.h
  106. * void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type),
  107. see: arch/x86/kernel/jump_label.c
  108. * __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry, enum jump_label_type type),
  109. see: arch/x86/kernel/jump_label.c
  110. * struct jump_entry, see: arch/x86/include/asm/jump_label.h
  111. 5) Static keys / jump label analysis, results (x86_64):
  112. As an example, let's add the following branch to 'getppid()', such that the
  113. system call now looks like:
  114. SYSCALL_DEFINE0(getppid)
  115. {
  116. int pid;
  117. + if (static_key_false(&key))
  118. + printk("I am the true branch\n");
  119. rcu_read_lock();
  120. pid = task_tgid_vnr(rcu_dereference(current->real_parent));
  121. rcu_read_unlock();
  122. return pid;
  123. }
  124. The resulting instructions with jump labels generated by GCC is:
  125. ffffffff81044290 <sys_getppid>:
  126. ffffffff81044290: 55 push %rbp
  127. ffffffff81044291: 48 89 e5 mov %rsp,%rbp
  128. ffffffff81044294: e9 00 00 00 00 jmpq ffffffff81044299 <sys_getppid+0x9>
  129. ffffffff81044299: 65 48 8b 04 25 c0 b6 mov %gs:0xb6c0,%rax
  130. ffffffff810442a0: 00 00
  131. ffffffff810442a2: 48 8b 80 80 02 00 00 mov 0x280(%rax),%rax
  132. ffffffff810442a9: 48 8b 80 b0 02 00 00 mov 0x2b0(%rax),%rax
  133. ffffffff810442b0: 48 8b b8 e8 02 00 00 mov 0x2e8(%rax),%rdi
  134. ffffffff810442b7: e8 f4 d9 00 00 callq ffffffff81051cb0 <pid_vnr>
  135. ffffffff810442bc: 5d pop %rbp
  136. ffffffff810442bd: 48 98 cltq
  137. ffffffff810442bf: c3 retq
  138. ffffffff810442c0: 48 c7 c7 e3 54 98 81 mov $0xffffffff819854e3,%rdi
  139. ffffffff810442c7: 31 c0 xor %eax,%eax
  140. ffffffff810442c9: e8 71 13 6d 00 callq ffffffff8171563f <printk>
  141. ffffffff810442ce: eb c9 jmp ffffffff81044299 <sys_getppid+0x9>
  142. Without the jump label optimization it looks like:
  143. ffffffff810441f0 <sys_getppid>:
  144. ffffffff810441f0: 8b 05 8a 52 d8 00 mov 0xd8528a(%rip),%eax # ffffffff81dc9480 <key>
  145. ffffffff810441f6: 55 push %rbp
  146. ffffffff810441f7: 48 89 e5 mov %rsp,%rbp
  147. ffffffff810441fa: 85 c0 test %eax,%eax
  148. ffffffff810441fc: 75 27 jne ffffffff81044225 <sys_getppid+0x35>
  149. ffffffff810441fe: 65 48 8b 04 25 c0 b6 mov %gs:0xb6c0,%rax
  150. ffffffff81044205: 00 00
  151. ffffffff81044207: 48 8b 80 80 02 00 00 mov 0x280(%rax),%rax
  152. ffffffff8104420e: 48 8b 80 b0 02 00 00 mov 0x2b0(%rax),%rax
  153. ffffffff81044215: 48 8b b8 e8 02 00 00 mov 0x2e8(%rax),%rdi
  154. ffffffff8104421c: e8 2f da 00 00 callq ffffffff81051c50 <pid_vnr>
  155. ffffffff81044221: 5d pop %rbp
  156. ffffffff81044222: 48 98 cltq
  157. ffffffff81044224: c3 retq
  158. ffffffff81044225: 48 c7 c7 13 53 98 81 mov $0xffffffff81985313,%rdi
  159. ffffffff8104422c: 31 c0 xor %eax,%eax
  160. ffffffff8104422e: e8 60 0f 6d 00 callq ffffffff81715193 <printk>
  161. ffffffff81044233: eb c9 jmp ffffffff810441fe <sys_getppid+0xe>
  162. ffffffff81044235: 66 66 2e 0f 1f 84 00 data32 nopw %cs:0x0(%rax,%rax,1)
  163. ffffffff8104423c: 00 00 00 00
  164. Thus, the disable jump label case adds a 'mov', 'test' and 'jne' instruction
  165. vs. the jump label case just has a 'no-op' or 'jmp 0'. (The jmp 0, is patched
  166. to a 5 byte atomic no-op instruction at boot-time.) Thus, the disabled jump
  167. label case adds:
  168. 6 (mov) + 2 (test) + 2 (jne) = 10 - 5 (5 byte jump 0) = 5 addition bytes.
  169. If we then include the padding bytes, the jump label code saves, 16 total bytes
  170. of instruction memory for this small function. In this case the non-jump label
  171. function is 80 bytes long. Thus, we have saved 20% of the instruction
  172. footprint. We can in fact improve this even further, since the 5-byte no-op
  173. really can be a 2-byte no-op since we can reach the branch with a 2-byte jmp.
  174. However, we have not yet implemented optimal no-op sizes (they are currently
  175. hard-coded).
  176. Since there are a number of static key API uses in the scheduler paths,
  177. 'pipe-test' (also known as 'perf bench sched pipe') can be used to show the
  178. performance improvement. Testing done on 3.3.0-rc2:
  179. jump label disabled:
  180. Performance counter stats for 'bash -c /tmp/pipe-test' (50 runs):
  181. 855.700314 task-clock # 0.534 CPUs utilized ( +- 0.11% )
  182. 200,003 context-switches # 0.234 M/sec ( +- 0.00% )
  183. 0 CPU-migrations # 0.000 M/sec ( +- 39.58% )
  184. 487 page-faults # 0.001 M/sec ( +- 0.02% )
  185. 1,474,374,262 cycles # 1.723 GHz ( +- 0.17% )
  186. <not supported> stalled-cycles-frontend
  187. <not supported> stalled-cycles-backend
  188. 1,178,049,567 instructions # 0.80 insns per cycle ( +- 0.06% )
  189. 208,368,926 branches # 243.507 M/sec ( +- 0.06% )
  190. 5,569,188 branch-misses # 2.67% of all branches ( +- 0.54% )
  191. 1.601607384 seconds time elapsed ( +- 0.07% )
  192. jump label enabled:
  193. Performance counter stats for 'bash -c /tmp/pipe-test' (50 runs):
  194. 841.043185 task-clock # 0.533 CPUs utilized ( +- 0.12% )
  195. 200,004 context-switches # 0.238 M/sec ( +- 0.00% )
  196. 0 CPU-migrations # 0.000 M/sec ( +- 40.87% )
  197. 487 page-faults # 0.001 M/sec ( +- 0.05% )
  198. 1,432,559,428 cycles # 1.703 GHz ( +- 0.18% )
  199. <not supported> stalled-cycles-frontend
  200. <not supported> stalled-cycles-backend
  201. 1,175,363,994 instructions # 0.82 insns per cycle ( +- 0.04% )
  202. 206,859,359 branches # 245.956 M/sec ( +- 0.04% )
  203. 4,884,119 branch-misses # 2.36% of all branches ( +- 0.85% )
  204. 1.579384366 seconds time elapsed
  205. The percentage of saved branches is .7%, and we've saved 12% on
  206. 'branch-misses'. This is where we would expect to get the most savings, since
  207. this optimization is about reducing the number of branches. In addition, we've
  208. saved .2% on instructions, and 2.8% on cycles and 1.4% on elapsed time.