sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}(hhparenthuba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget'/translations/zh_CN/staging/static-keysmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}(hhhh2ubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/zh_TW/staging/static-keysmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}(hhhhFubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/it_IT/staging/static-keysmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}(hhhhZubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/ja_JP/staging/static-keysmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}(hhhhnubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/ko_KR/staging/static-keysmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}(hhhhubah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/sp_SP/staging/static-keysmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(h Static Keysh]h Static Keys}(hhhhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhA/var/lib/git/docbuild/linux/Documentation/staging/static-keys.rsthKubhwarning)}(hXCDEPRECATED API: The use of 'struct static_key' directly, is now DEPRECATED. In addition static_key_{true,false}() is also DEPRECATED. IE DO NOT use the following:: struct static_key false = STATIC_KEY_INIT_FALSE; struct static_key true = STATIC_KEY_INIT_TRUE; static_key_true() static_key_false() The updated API replacements are:: DEFINE_STATIC_KEY_TRUE(key); DEFINE_STATIC_KEY_FALSE(key); DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count); DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count); static_branch_likely() static_branch_unlikely()h](h paragraph)}(hDEPRECATED API:h]hDEPRECATED API:}(hhhhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhubh)}(hThe use of 'struct static_key' directly, is now DEPRECATED. In addition static_key_{true,false}() is also DEPRECATED. IE DO NOT use the following::h]hThe use of ‘struct static_key’ directly, is now DEPRECATED. In addition static_key_{true,false}() is also DEPRECATED. IE DO NOT use the following:}(hThe use of 'struct static_key' directly, is now DEPRECATED. In addition static_key_{true,false}() is also DEPRECATED. IE DO NOT use the following:hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hhubh literal_block)}(hstruct static_key false = STATIC_KEY_INIT_FALSE; struct static_key true = STATIC_KEY_INIT_TRUE; static_key_true() static_key_false()h]hstruct static_key false = STATIC_KEY_INIT_FALSE; struct static_key true = STATIC_KEY_INIT_TRUE; static_key_true() static_key_false()}(hhhhubah}(h]h ]h"]h$]h&] xml:spacepreserveuh1hhhhK hhubh)}(h"The updated API replacements are::h]h!The updated API replacements are:}(h!The updated API replacements are:hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhubh)}(hDEFINE_STATIC_KEY_TRUE(key); DEFINE_STATIC_KEY_FALSE(key); DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count); DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count); static_branch_likely() static_branch_unlikely()h]hDEFINE_STATIC_KEY_TRUE(key); DEFINE_STATIC_KEY_FALSE(key); DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count); DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count); static_branch_likely() static_branch_unlikely()}(hhhhubah}(h]h ]h"]h$]h&]hhuh1hhhhKhhubeh}(h]h ]h"]h$]h&]uh1hhhhhhhhNubh)}(hhh](h)}(hAbstracth]hAbstract}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hStatic keys allows the inclusion of seldom used features in performance-sensitive fast-path kernel code, via a GCC feature and a code patching technique. A quick example::h]hStatic keys allows the inclusion of seldom used features in performance-sensitive fast-path kernel code, via a GCC feature and a code patching technique. A quick example:}(hStatic keys allows the inclusion of seldom used features in performance-sensitive fast-path kernel code, via a GCC feature and a code patching technique. A quick example:hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hDEFINE_STATIC_KEY_FALSE(key); ... if (static_branch_unlikely(&key)) do unlikely code else do likely code ... static_branch_enable(&key); ... static_branch_disable(&key); ...h]hDEFINE_STATIC_KEY_FALSE(key); ... if (static_branch_unlikely(&key)) do unlikely code else do likely code ... static_branch_enable(&key); ... static_branch_disable(&key); ...}(hhhj1ubah}(h]h ]h"]h$]h&]hhuh1hhhhK!hjhhubh)}(h~The static_branch_unlikely() branch will be generated into the code with as little impact to the likely code path as possible.h]h~The static_branch_unlikely() branch will be generated into the code with as little impact to the likely code path as possible.}(hjAhj?hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK0hjhhubeh}(h]abstractah ]h"]abstractah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h Motivationh]h Motivation}(hjZhjXhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjUhhhhhK5ubh)}(hXCurrently, tracepoints are implemented using a conditional branch. The conditional check requires checking a global variable for each tracepoint. Although the overhead of this check is small, it increases when the memory cache comes under pressure (memory cache lines for these global variables may be shared with other memory accesses). As we increase the number of tracepoints in the kernel this overhead may become more of an issue. In addition, tracepoints are often dormant (disabled) and provide no direct kernel functionality. Thus, it is highly desirable to reduce their impact as much as possible. Although tracepoints are the original motivation for this work, other kernel code paths should be able to make use of the static keys facility.h]hXCurrently, tracepoints are implemented using a conditional branch. The conditional check requires checking a global variable for each tracepoint. Although the overhead of this check is small, it increases when the memory cache comes under pressure (memory cache lines for these global variables may be shared with other memory accesses). As we increase the number of tracepoints in the kernel this overhead may become more of an issue. In addition, tracepoints are often dormant (disabled) and provide no direct kernel functionality. Thus, it is highly desirable to reduce their impact as much as possible. Although tracepoints are the original motivation for this work, other kernel code paths should be able to make use of the static keys facility.}(hjhhjfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK8hjUhhubeh}(h] motivationah ]h"] motivationah$]h&]uh1hhhhhhhhK5ubh)}(hhh](h)}(hSolutionh]hSolution}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj|hhhhhKEubh)}(hLgcc (v4.5) adds a new 'asm goto' statement that allows branching to a label:h]hPgcc (v4.5) adds a new ‘asm goto’ statement that allows branching to a label:}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKHhj|hhubh)}(h8https://gcc.gnu.org/ml/gcc-patches/2009-07/msg01556.htmlh]h reference)}(hjh]h8https://gcc.gnu.org/ml/gcc-patches/2009-07/msg01556.html}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]refurijuh1jhjubah}(h]h ]h"]h$]h&]uh1hhhhKJhj|hhubh)}(hUsing the 'asm goto', we can create branches that are either taken or not taken by default, without the need to check memory. Then, at run-time, we can patch the branch site to change the branch direction.h]hUsing the ‘asm goto’, we can create branches that are either taken or not taken by default, without the need to check memory. Then, at run-time, we can patch the branch site to change the branch direction.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKLhj|hhubh)}(hEFor example, if we have a simple branch that is disabled by default::h]hDFor example, if we have a simple branch that is disabled by default:}(hDFor example, if we have a simple branch that is disabled by default:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKPhj|hhubh)}(hKif (static_branch_unlikely(&key)) printk("I am the true branch\n");h]hKif (static_branch_unlikely(&key)) printk("I am the true branch\n");}(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhKRhj|hhubh)}(hXThus, by default the 'printk' will not be emitted. And the code generated will consist of a single atomic 'no-op' instruction (5 bytes on x86), in the straight-line code path. When the branch is 'flipped', we will patch the 'no-op' in the straight-line codepath with a 'jump' instruction to the out-of-line true branch. Thus, changing branch direction is expensive but branch selection is basically 'free'. That is the basic tradeoff of this optimization.h]hXThus, by default the ‘printk’ will not be emitted. And the code generated will consist of a single atomic ‘no-op’ instruction (5 bytes on x86), in the straight-line code path. When the branch is ‘flipped’, we will patch the ‘no-op’ in the straight-line codepath with a ‘jump’ instruction to the out-of-line true branch. Thus, changing branch direction is expensive but branch selection is basically ‘free’. That is the basic tradeoff of this optimization.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKUhj|hhubh)}(hvThis lowlevel patching mechanism is called 'jump label patching', and it gives the basis for the static keys facility.h]hzThis lowlevel patching mechanism is called ‘jump label patching’, and it gives the basis for the static keys facility.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK]hj|hhubeh}(h]solutionah ]h"]solutionah$]h&]uh1hhhhhhhhKEubh)}(hhh](h)}(h(Static key label API, usage and examplesh]h(Static key label API, usage and examples}(hj hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKaubh)}(hGIn order to make use of this optimization you must first define a key::h]hFIn order to make use of this optimization you must first define a key:}(hFIn order to make use of this optimization you must first define a key:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKdhjhhubh)}(hDEFINE_STATIC_KEY_TRUE(key);h]hDEFINE_STATIC_KEY_TRUE(key);}(hhhj$ubah}(h]h ]h"]h$]h&]hhuh1hhhhKfhjhhubh)}(hor::h]hor:}(hor:hj2hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhjhhubh)}(hDEFINE_STATIC_KEY_FALSE(key);h]hDEFINE_STATIC_KEY_FALSE(key);}(hhhjAubah}(h]h ]h"]h$]h&]hhuh1hhhhKjhjhhubh)}(hiThe key must be global, that is, it can't be allocated on the stack or dynamically allocated at run-time.h]hkThe key must be global, that is, it can’t be allocated on the stack or dynamically allocated at run-time.}(hjQhjOhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKmhjhhubh)}(h!The key is then used in code as::h]h The key is then used in code as:}(h The key is then used in code as:hj]hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKphjhhubh)}(hVif (static_branch_unlikely(&key)) do unlikely code else do likely codeh]hVif (static_branch_unlikely(&key)) do unlikely code else do likely code}(hhhjlubah}(h]h ]h"]h$]h&]hhuh1hhhhKrhjhhubh)}(hOr::h]hOr:}(hOr:hjzhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKwhjhhubh)}(hTif (static_branch_likely(&key)) do likely code else do unlikely codeh]hTif (static_branch_likely(&key)) do likely code else do unlikely code}(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhKyhjhhubh)}(hKeys defined via DEFINE_STATIC_KEY_TRUE(), or DEFINE_STATIC_KEY_FALSE, may be used in either static_branch_likely() or static_branch_unlikely() statements.h]hKeys defined via DEFINE_STATIC_KEY_TRUE(), or DEFINE_STATIC_KEY_FALSE, may be used in either static_branch_likely() or static_branch_unlikely() statements.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK~hjhhubh)}(h Branch(es) can be set true via::h]hBranch(es) can be set true via:}(hBranch(es) can be set true via:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hstatic_branch_enable(&key);h]hstatic_branch_enable(&key);}(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhKhjhhubh)}(hor false via::h]h or false via:}(h or false via:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hstatic_branch_disable(&key);h]hstatic_branch_disable(&key);}(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhKhjhhubh)}(h:The branch(es) can then be switched via reference counts::h]h9The branch(es) can then be switched via reference counts:}(h9The branch(es) can then be switched via reference counts:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h5static_branch_inc(&key); ... static_branch_dec(&key);h]h5static_branch_inc(&key); ... static_branch_dec(&key);}(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhKhjhhubh)}(hXThus, 'static_branch_inc()' means 'make the branch true', and 'static_branch_dec()' means 'make the branch false' with appropriate reference counting. For example, if the key is initialized true, a static_branch_dec(), will switch the branch to false. And a subsequent static_branch_inc(), will change the branch back to true. Likewise, if the key is initialized false, a 'static_branch_inc()', will change the branch to true. And then a 'static_branch_dec()', will again make the branch false.h]hXThus, ‘static_branch_inc()’ means ‘make the branch true’, and ‘static_branch_dec()’ means ‘make the branch false’ with appropriate reference counting. For example, if the key is initialized true, a static_branch_dec(), will switch the branch to false. And a subsequent static_branch_inc(), will change the branch back to true. Likewise, if the key is initialized false, a ‘static_branch_inc()’, will change the branch to true. And then a ‘static_branch_dec()’, will again make the branch false.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hThe state and the reference count can be retrieved with 'static_key_enabled()' and 'static_key_count()'. In general, if you use these functions, they should be protected with the same mutex used around the enable/disable or increment/decrement function.h]hXThe state and the reference count can be retrieved with ‘static_key_enabled()’ and ‘static_key_count()’. In general, if you use these functions, they should be protected with the same mutex used around the enable/disable or increment/decrement function.}(hj hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hX{Note that switching branches results in some locks being taken, particularly the CPU hotplug lock (in order to avoid races against CPUs being brought in the kernel while the kernel is getting patched). Calling the static key API from within a hotplug notifier is thus a sure deadlock recipe. In order to still allow use of the functionality, the following functions are provided:h]hX{Note that switching branches results in some locks being taken, particularly the CPU hotplug lock (in order to avoid races against CPUs being brought in the kernel while the kernel is getting patched). Calling the static key API from within a hotplug notifier is thus a sure deadlock recipe. In order to still allow use of the functionality, the following functions are provided:}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh block_quote)}(hhh]h)}(hstatic_key_enable_cpuslocked() static_key_disable_cpuslocked() static_branch_enable_cpuslocked() static_branch_disable_cpuslocked()h]hstatic_key_enable_cpuslocked() static_key_disable_cpuslocked() static_branch_enable_cpuslocked() static_branch_disable_cpuslocked()}(hj-hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj(ubah}(h]h ]h"]h$]h&]uh1j&hjhhhhhNubh)}(hThese functions are *not* general purpose, and must only be used when you really know that you're in the above context, and no other.h](hThese functions are }(hThese functions are hj?hhhNhNubhemphasis)}(h*not*h]hnot}(hhhjJhhhNhNubah}(h]h ]h"]h$]h&]uh1jHhj?ubhn general purpose, and must only be used when you really know that you’re in the above context, and no other.}(hl general purpose, and must only be used when you really know that you're in the above context, and no other.hj?hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h:Where an array of keys is required, it can be defined as::h]h9Where an array of keys is required, it can be defined as:}(h9Where an array of keys is required, it can be defined as:hjchhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h*DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count);h]h*DEFINE_STATIC_KEY_ARRAY_TRUE(keys, count);}(hhhjrubah}(h]h ]h"]h$]h&]hhuh1hhhhKhjhhubh)}(hor::h]hor:}(hor:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h+DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count);h]h+DEFINE_STATIC_KEY_ARRAY_FALSE(keys, count);}(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhKhjhhubhenumerated_list)}(hhh]h list_item)}(h;Architecture level code patching interface, 'jump labels' h]h)}(h9Architecture level code patching interface, 'jump labels'h]h=Architecture level code patching interface, ‘jump labels’}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubah}(h]h ]h"]h$]h&]enumtypearabicprefixhsuffix)startKuh1jhjhhhhhKubh)}(hXtThere are a few functions and macros that architectures must implement in order to take advantage of this optimization. If there is no architecture support, we simply fall back to a traditional, load, test, and jump sequence. Also, the struct jump_entry table must be at least 4-byte aligned because the static_key->entry field makes use of the two least significant bits.h]hXtThere are a few functions and macros that architectures must implement in order to take advantage of this optimization. If there is no architecture support, we simply fall back to a traditional, load, test, and jump sequence. Also, the struct jump_entry table must be at least 4-byte aligned because the static_key->entry field makes use of the two least significant bits.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh bullet_list)}(hhh](j)}(h9``select HAVE_ARCH_JUMP_LABEL``, see: arch/x86/Kconfig h]hdefinition_list)}(hhh]hdefinition_list_item)}(h7``select HAVE_ARCH_JUMP_LABEL``, see: arch/x86/Kconfig h](hterm)}(h ``select HAVE_ARCH_JUMP_LABEL``,h](hliteral)}(h``select HAVE_ARCH_JUMP_LABEL``h]hselect HAVE_ARCH_JUMP_LABEL}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh,}(h,hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubh definition)}(hhh]h)}(hsee: arch/x86/Kconfigh]hsee: arch/x86/Kconfig}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(hJ``#define JUMP_LABEL_NOP_SIZE``, see: arch/x86/include/asm/jump_label.h h]j)}(hhh]j)}(hH``#define JUMP_LABEL_NOP_SIZE``, see: arch/x86/include/asm/jump_label.h h](j)}(h ``#define JUMP_LABEL_NOP_SIZE``,h](j)}(h``#define JUMP_LABEL_NOP_SIZE``h]h#define JUMP_LABEL_NOP_SIZE}(hhhjEhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjAubh,}(hjhjAhhhNhNubeh}(h]h ]h"]h$]h&]uh1jhhhKhj=ubj )}(hhh]h)}(h&see: arch/x86/include/asm/jump_label.hh]h&see: arch/x86/include/asm/jump_label.h}(hjbhj`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj]ubah}(h]h ]h"]h$]h&]uh1j hj=ubeh}(h]h ]h"]h$]h&]uh1jhhhKhj:ubah}(h]h ]h"]h$]h&]uh1jhj6ubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(h{``__always_inline bool arch_static_branch(struct static_key *key, bool branch)``, see: arch/x86/include/asm/jump_label.h h]j)}(hhh]j)}(hy``__always_inline bool arch_static_branch(struct static_key *key, bool branch)``, see: arch/x86/include/asm/jump_label.h h](j)}(hQ``__always_inline bool arch_static_branch(struct static_key *key, bool branch)``,h](j)}(hP``__always_inline bool arch_static_branch(struct static_key *key, bool branch)``h]hL__always_inline bool arch_static_branch(struct static_key *key, bool branch)}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh,}(hjhjhhhNhNubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubj )}(hhh]h)}(h&see: arch/x86/include/asm/jump_label.hh]h&see: arch/x86/include/asm/jump_label.h}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(h``__always_inline bool arch_static_branch_jump(struct static_key *key, bool branch)``, see: arch/x86/include/asm/jump_label.h h]j)}(hhh]j)}(h~``__always_inline bool arch_static_branch_jump(struct static_key *key, bool branch)``, see: arch/x86/include/asm/jump_label.h h](j)}(hV``__always_inline bool arch_static_branch_jump(struct static_key *key, bool branch)``,h](j)}(hU``__always_inline bool arch_static_branch_jump(struct static_key *key, bool branch)``h]hQ__always_inline bool arch_static_branch_jump(struct static_key *key, bool branch)}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh,}(hjhjhhhNhNubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubj )}(hhh]h)}(h&see: arch/x86/include/asm/jump_label.hh]h&see: arch/x86/include/asm/jump_label.h}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]uh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(h}``void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type)``, see: arch/x86/kernel/jump_label.c h]j)}(hhh]j)}(h{``void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type)``, see: arch/x86/kernel/jump_label.c h](j)}(hX``void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type)``,h](j)}(hW``void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type)``h]hSvoid arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type)}(hhhj5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj1ubh,}(hjhj1hhhNhNubeh}(h]h ]h"]h$]h&]uh1jhhhKhj-ubj )}(hhh]h)}(h!see: arch/x86/kernel/jump_label.ch]h!see: arch/x86/kernel/jump_label.c}(hjRhjPhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjMubah}(h]h ]h"]h$]h&]uh1j hj-ubeh}(h]h ]h"]h$]h&]uh1jhhhKhj*ubah}(h]h ]h"]h$]h&]uh1jhj&ubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubj)}(hA``struct jump_entry``, see: arch/x86/include/asm/jump_label.h h]j)}(hhh]j)}(h?``struct jump_entry``, see: arch/x86/include/asm/jump_label.h h](j)}(h``struct jump_entry``,h](j)}(h``struct jump_entry``h]hstruct jump_entry}(hhhjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh,}(hjhjhhhNhNubeh}(h]h ]h"]h$]h&]uh1jhhhKhj}ubj )}(hhh]h)}(h&see: arch/x86/include/asm/jump_label.hh]h&see: arch/x86/include/asm/jump_label.h}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j hj}ubeh}(h]h ]h"]h$]h&]uh1jhhhKhjzubah}(h]h ]h"]h$]h&]uh1jhjvubah}(h]h ]h"]h$]h&]uh1jhjhhhNhNubeh}(h]h ]h"]h$]h&]bullet*uh1jhhhKhjhhubj)}(hhh]j)}(h6Static keys / jump label analysis, results (x86_64): h]h)}(h4Static keys / jump label analysis, results (x86_64):h]h4Static keys / jump label analysis, results (x86_64):}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubah}(h]h ]h"]h$]h&]jjjhjjjKuh1jhjhhhhhKubh)}(hhAs an example, let's add the following branch to 'getppid()', such that the system call now looks like::h]hmAs an example, let’s add the following branch to ‘getppid()’, such that the system call now looks like:}(hgAs an example, let's add the following branch to 'getppid()', such that the system call now looks like:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hX SYSCALL_DEFINE0(getppid) { int pid; + if (static_branch_unlikely(&key)) + printk("I am the true branch\n"); rcu_read_lock(); pid = task_tgid_vnr(rcu_dereference(current->real_parent)); rcu_read_unlock(); return pid; }h]hX SYSCALL_DEFINE0(getppid) { int pid; + if (static_branch_unlikely(&key)) + printk("I am the true branch\n"); rcu_read_lock(); pid = task_tgid_vnr(rcu_dereference(current->real_parent)); rcu_read_unlock(); return pid; }}(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhKhjhhubh)}(hAThe resulting instructions with jump labels generated by GCC is::h]h@The resulting instructions with jump labels generated by GCC is:}(h@The resulting instructions with jump labels generated by GCC is:hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXgffffffff81044290 : ffffffff81044290: 55 push %rbp ffffffff81044291: 48 89 e5 mov %rsp,%rbp ffffffff81044294: e9 00 00 00 00 jmpq ffffffff81044299 ffffffff81044299: 65 48 8b 04 25 c0 b6 mov %gs:0xb6c0,%rax ffffffff810442a0: 00 00 ffffffff810442a2: 48 8b 80 80 02 00 00 mov 0x280(%rax),%rax ffffffff810442a9: 48 8b 80 b0 02 00 00 mov 0x2b0(%rax),%rax ffffffff810442b0: 48 8b b8 e8 02 00 00 mov 0x2e8(%rax),%rdi ffffffff810442b7: e8 f4 d9 00 00 callq ffffffff81051cb0 ffffffff810442bc: 5d pop %rbp ffffffff810442bd: 48 98 cltq ffffffff810442bf: c3 retq ffffffff810442c0: 48 c7 c7 e3 54 98 81 mov $0xffffffff819854e3,%rdi ffffffff810442c7: 31 c0 xor %eax,%eax ffffffff810442c9: e8 71 13 6d 00 callq ffffffff8171563f ffffffff810442ce: eb c9 jmp ffffffff81044299 h]hXgffffffff81044290 : ffffffff81044290: 55 push %rbp ffffffff81044291: 48 89 e5 mov %rsp,%rbp ffffffff81044294: e9 00 00 00 00 jmpq ffffffff81044299 ffffffff81044299: 65 48 8b 04 25 c0 b6 mov %gs:0xb6c0,%rax ffffffff810442a0: 00 00 ffffffff810442a2: 48 8b 80 80 02 00 00 mov 0x280(%rax),%rax ffffffff810442a9: 48 8b 80 b0 02 00 00 mov 0x2b0(%rax),%rax ffffffff810442b0: 48 8b b8 e8 02 00 00 mov 0x2e8(%rax),%rdi ffffffff810442b7: e8 f4 d9 00 00 callq ffffffff81051cb0 ffffffff810442bc: 5d pop %rbp ffffffff810442bd: 48 98 cltq ffffffff810442bf: c3 retq ffffffff810442c0: 48 c7 c7 e3 54 98 81 mov $0xffffffff819854e3,%rdi ffffffff810442c7: 31 c0 xor %eax,%eax ffffffff810442c9: e8 71 13 6d 00 callq ffffffff8171563f ffffffff810442ce: eb c9 jmp ffffffff81044299 }(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhKhjhhubh)}(h3Without the jump label optimization it looks like::h]h2Without the jump label optimization it looks like:}(h2Without the jump label optimization it looks like:hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXffffffff810441f0 : ffffffff810441f0: 8b 05 8a 52 d8 00 mov 0xd8528a(%rip),%eax # ffffffff81dc9480 ffffffff810441f6: 55 push %rbp ffffffff810441f7: 48 89 e5 mov %rsp,%rbp ffffffff810441fa: 85 c0 test %eax,%eax ffffffff810441fc: 75 27 jne ffffffff81044225 ffffffff810441fe: 65 48 8b 04 25 c0 b6 mov %gs:0xb6c0,%rax ffffffff81044205: 00 00 ffffffff81044207: 48 8b 80 80 02 00 00 mov 0x280(%rax),%rax ffffffff8104420e: 48 8b 80 b0 02 00 00 mov 0x2b0(%rax),%rax ffffffff81044215: 48 8b b8 e8 02 00 00 mov 0x2e8(%rax),%rdi ffffffff8104421c: e8 2f da 00 00 callq ffffffff81051c50 ffffffff81044221: 5d pop %rbp ffffffff81044222: 48 98 cltq ffffffff81044224: c3 retq ffffffff81044225: 48 c7 c7 13 53 98 81 mov $0xffffffff81985313,%rdi ffffffff8104422c: 31 c0 xor %eax,%eax ffffffff8104422e: e8 60 0f 6d 00 callq ffffffff81715193 ffffffff81044233: eb c9 jmp ffffffff810441fe ffffffff81044235: 66 66 2e 0f 1f 84 00 data32 nopw %cs:0x0(%rax,%rax,1) ffffffff8104423c: 00 00 00 00h]hXffffffff810441f0 : ffffffff810441f0: 8b 05 8a 52 d8 00 mov 0xd8528a(%rip),%eax # ffffffff81dc9480 ffffffff810441f6: 55 push %rbp ffffffff810441f7: 48 89 e5 mov %rsp,%rbp ffffffff810441fa: 85 c0 test %eax,%eax ffffffff810441fc: 75 27 jne ffffffff81044225 ffffffff810441fe: 65 48 8b 04 25 c0 b6 mov %gs:0xb6c0,%rax ffffffff81044205: 00 00 ffffffff81044207: 48 8b 80 80 02 00 00 mov 0x280(%rax),%rax ffffffff8104420e: 48 8b 80 b0 02 00 00 mov 0x2b0(%rax),%rax ffffffff81044215: 48 8b b8 e8 02 00 00 mov 0x2e8(%rax),%rdi ffffffff8104421c: e8 2f da 00 00 callq ffffffff81051c50 ffffffff81044221: 5d pop %rbp ffffffff81044222: 48 98 cltq ffffffff81044224: c3 retq ffffffff81044225: 48 c7 c7 13 53 98 81 mov $0xffffffff81985313,%rdi ffffffff8104422c: 31 c0 xor %eax,%eax ffffffff8104422e: e8 60 0f 6d 00 callq ffffffff81715193 ffffffff81044233: eb c9 jmp ffffffff810441fe ffffffff81044235: 66 66 2e 0f 1f 84 00 data32 nopw %cs:0x0(%rax,%rax,1) ffffffff8104423c: 00 00 00 00}(hhhj8ubah}(h]h ]h"]h$]h&]hhuh1hhhhKhjhhubh)}(hThus, the disable jump label case adds a 'mov', 'test' and 'jne' instruction vs. the jump label case just has a 'no-op' or 'jmp 0'. (The jmp 0, is patched to a 5 byte atomic no-op instruction at boot-time.) Thus, the disabled jump label case adds::h]hX Thus, the disable jump label case adds a ‘mov’, ‘test’ and ‘jne’ instruction vs. the jump label case just has a ‘no-op’ or ‘jmp 0’. (The jmp 0, is patched to a 5 byte atomic no-op instruction at boot-time.) Thus, the disabled jump label case adds:}(hThus, the disable jump label case adds a 'mov', 'test' and 'jne' instruction vs. the jump label case just has a 'no-op' or 'jmp 0'. (The jmp 0, is patched to a 5 byte atomic no-op instruction at boot-time.) Thus, the disabled jump label case adds:hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hI6 (mov) + 2 (test) + 2 (jne) = 10 - 5 (5 byte jump 0) = 5 addition bytes.h]hI6 (mov) + 2 (test) + 2 (jne) = 10 - 5 (5 byte jump 0) = 5 addition bytes.}(hhhjUubah}(h]h ]h"]h$]h&]hhuh1hhhhMhjhhubh)}(hXIf we then include the padding bytes, the jump label code saves, 16 total bytes of instruction memory for this small function. In this case the non-jump label function is 80 bytes long. Thus, we have saved 20% of the instruction footprint. We can in fact improve this even further, since the 5-byte no-op really can be a 2-byte no-op since we can reach the branch with a 2-byte jmp. However, we have not yet implemented optimal no-op sizes (they are currently hard-coded).h]hXIf we then include the padding bytes, the jump label code saves, 16 total bytes of instruction memory for this small function. In this case the non-jump label function is 80 bytes long. Thus, we have saved 20% of the instruction footprint. We can in fact improve this even further, since the 5-byte no-op really can be a 2-byte no-op since we can reach the branch with a 2-byte jmp. However, we have not yet implemented optimal no-op sizes (they are currently hard-coded).}(hjehjchhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hSince there are a number of static key API uses in the scheduler paths, 'pipe-test' (also known as 'perf bench sched pipe') can be used to show the performance improvement. Testing done on 3.3.0-rc2:h]hSince there are a number of static key API uses in the scheduler paths, ‘pipe-test’ (also known as ‘perf bench sched pipe’) can be used to show the performance improvement. Testing done on 3.3.0-rc2:}(hjshjqhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hjump label disabled::h]hjump label disabled:}(hjump label disabled:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM#hjhhubh)}(hXPerformance counter stats for 'bash -c /tmp/pipe-test' (50 runs): 855.700314 task-clock # 0.534 CPUs utilized ( +- 0.11% ) 200,003 context-switches # 0.234 M/sec ( +- 0.00% ) 0 CPU-migrations # 0.000 M/sec ( +- 39.58% ) 487 page-faults # 0.001 M/sec ( +- 0.02% ) 1,474,374,262 cycles # 1.723 GHz ( +- 0.17% ) stalled-cycles-frontend stalled-cycles-backend 1,178,049,567 instructions # 0.80 insns per cycle ( +- 0.06% ) 208,368,926 branches # 243.507 M/sec ( +- 0.06% ) 5,569,188 branch-misses # 2.67% of all branches ( +- 0.54% ) 1.601607384 seconds time elapsed ( +- 0.07% )h]hXPerformance counter stats for 'bash -c /tmp/pipe-test' (50 runs): 855.700314 task-clock # 0.534 CPUs utilized ( +- 0.11% ) 200,003 context-switches # 0.234 M/sec ( +- 0.00% ) 0 CPU-migrations # 0.000 M/sec ( +- 39.58% ) 487 page-faults # 0.001 M/sec ( +- 0.02% ) 1,474,374,262 cycles # 1.723 GHz ( +- 0.17% ) stalled-cycles-frontend stalled-cycles-backend 1,178,049,567 instructions # 0.80 insns per cycle ( +- 0.06% ) 208,368,926 branches # 243.507 M/sec ( +- 0.06% ) 5,569,188 branch-misses # 2.67% of all branches ( +- 0.54% ) 1.601607384 seconds time elapsed ( +- 0.07% )}(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhM%hjhhubh)}(hjump label enabled::h]hjump label enabled:}(hjump label enabled:hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM4hjhhubh)}(hXPerformance counter stats for 'bash -c /tmp/pipe-test' (50 runs): 841.043185 task-clock # 0.533 CPUs utilized ( +- 0.12% ) 200,004 context-switches # 0.238 M/sec ( +- 0.00% ) 0 CPU-migrations # 0.000 M/sec ( +- 40.87% ) 487 page-faults # 0.001 M/sec ( +- 0.05% ) 1,432,559,428 cycles # 1.703 GHz ( +- 0.18% ) stalled-cycles-frontend stalled-cycles-backend 1,175,363,994 instructions # 0.82 insns per cycle ( +- 0.04% ) 206,859,359 branches # 245.956 M/sec ( +- 0.04% ) 4,884,119 branch-misses # 2.36% of all branches ( +- 0.85% ) 1.579384366 seconds time elapsedh]hXPerformance counter stats for 'bash -c /tmp/pipe-test' (50 runs): 841.043185 task-clock # 0.533 CPUs utilized ( +- 0.12% ) 200,004 context-switches # 0.238 M/sec ( +- 0.00% ) 0 CPU-migrations # 0.000 M/sec ( +- 40.87% ) 487 page-faults # 0.001 M/sec ( +- 0.05% ) 1,432,559,428 cycles # 1.703 GHz ( +- 0.18% ) stalled-cycles-frontend stalled-cycles-backend 1,175,363,994 instructions # 0.82 insns per cycle ( +- 0.04% ) 206,859,359 branches # 245.956 M/sec ( +- 0.04% ) 4,884,119 branch-misses # 2.36% of all branches ( +- 0.85% ) 1.579384366 seconds time elapsed}(hhhjubah}(h]h ]h"]h$]h&]hhuh1hhhhM6hjhhubh)}(hX$The percentage of saved branches is .7%, and we've saved 12% on 'branch-misses'. This is where we would expect to get the most savings, since this optimization is about reducing the number of branches. In addition, we've saved .2% on instructions, and 2.8% on cycles and 1.4% on elapsed time.h]hX,The percentage of saved branches is .7%, and we’ve saved 12% on ‘branch-misses’. This is where we would expect to get the most savings, since this optimization is about reducing the number of branches. In addition, we’ve saved .2% on instructions, and 2.8% on cycles and 1.4% on elapsed time.}(hjhjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMEhjhhubeh}(h]'static-key-label-api-usage-and-examplesah ]h"](static key label api, usage and examplesah$]h&]uh1hhhhhhhhKaubeh}(h] static-keysah ]h"] static keysah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjerror_encodingUTF-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confapep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacefile_insertion_enabled raw_enabledKline_length_limitM'syntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_link embed_imagesenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(jjjRjOjyjvjjjju nametypes}(jNjRNjyNjNjNuh}(jhjOjjvjUjj|jju footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages](hsystem_message)}(hhh]h)}(h:Enumerated list start value not ordinal-1: "4" (ordinal 4)h]h>Enumerated list start value not ordinal-1: “4” (ordinal 4)}(hhhjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj_ubah}(h]h ]h"]h$]h&]levelKtypeINFOsourcehlineKuh1j]hjhhhhhKubj^)}(hhh]h)}(h:Enumerated list start value not ordinal-1: "5" (ordinal 5)h]h>Enumerated list start value not ordinal-1: “5” (ordinal 5)}(hhhj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj{ubah}(h]h ]h"]h$]h&]levelKtypejxsourcehlineKuh1j]hjhhhhhKubetransform_messages] transformerN include_log] decorationNhhub.