Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

12.4 Usage Methodology of Static Keys

To implement dynamic toggling of program branching paths via a static key, a global static key with a default status of true or false must first be defined via either DEFINE_STATIC_KEY_TRUE(key) or DEFINE_STATIC_KEY_FALSE(key). Following this, the function static_branch_likely(key) or static_branch_unlikely(key) is invoked at the location where routing manipulation is required, as demonstrated below:

if (static_branch_likely(&key)) {
	/* likely code */
} else {
	/* unlikely code */
}

or alternatively:

if (static_branch_unlikely(&key)) {
	/* unlikely code */
} else {
	/* likely code */
}

The function static_branch_enable(&key) sets the enabled field of the static key to 1, whereas static_branch_disable(&key) clears the enabled field of the static key to zero. Invoking the function static_branch_enable(&key) prompts the program flow to execute a branch alteration, whereas invoking static_branch_disable(&key) terminates that specific routing modification. If a gradual reference-counted change to the branch routing direction is required, static_branch_inc(&key) and static_branch_dec(&key) can be called. While static_branch_enable() and static_branch_disable() explicitly force the static key's enabled field to 1 or 0, static_branch_inc() and static_branch_dec() serve to increment or decrement the value of the enabled field by 1, respectively. When the enabled counter decrements down to 0, the program branching direction undergoes a structural transformation. The following snippet illustrates a code sequence utilized by the SLAB memory allocation algorithm when carving out memory for sockets:

#ifdef CONFIG_NET
DECLARE_STATIC_KEY_FALSE(memalloc_socks_key);
static inline int sk_memalloc_socks(void)
{
	return static_branch_unlikely(&memalloc_socks_key);
}
#else
static inline int sk_memalloc_socks(void)
{
	return 0;
}
#endif
/* ... Execution Context ... */
if (sk_memalloc_socks()) {
	void *obj = cache_alloc_pfmemalloc(cachep, n, flags);
	if (obj)
		return obj;
}

When network functionality support is enabled via the compilation flag CONFIG_NET, the value of memalloc_socks_key is declared as false via DECLARE_STATIC_KEY_FALSE(). Because memalloc_socks_key defaults to false, the return value of sk_memalloc_socks() evaluates to false, and the CPU does not enter the inner branch of the if statement. When the system enters a memory reclamation state and needs to activate pfmemalloc sockets, the kernel invokes static_branch_enable(&memalloc_socks_key) to alter the state of memalloc_socks_key. At this point, sk_memalloc_socks() returns true during runtime execution, causing the code flow to enter the inner branch of the if statement and execute cache_alloc_pfmemalloc() to allocate memory.

The concrete assembly language execution workflow is as follows: the compiled assembly instruction at the if (sk_memalloc_socks()) check location resolves to a single hardware NOP instruction, enabling the CPU to stream directly down the straight-line sequential track without executing any conditional branching evaluations, thereby perfectly avoiding pipeline stalls triggered by CPU branch mispredictions. When memory reclamation becomes active and the pfmemalloc sockets must be energized, the kernel leverages functions such as text_poke() to dynamically hot-patch the original NOP placeholder into a concrete branch instruction at runtime.

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com