livepatch: Avoid blocking tasklist_lock too long
PATCH 1/1 livepatch: Avoid blocking tasklist_lock too long
Problem
=======
As more and more patches accumulate in the cumulative livepatch, we see an increasing number of latency spikes from latency-sensitive services during livepatch upgrades. The most recent case is the redis data_server [0].
With some tracing added to the userspace redis code and a hard trace in the kernel, we finally tracked the root cause down to tasklist_lock contention during livepatch transition.
When redis flushes data, it clones a new task, which takes the write side of tasklist_lock. If a livepatch upgrade is in progress at the same time, tasklist_lock is held for the whole transition. Below is the trace we captured while reproducing the issue:
=== [2026-11-15 15:18:06] tid=1635309 comm=data_server WRITE wait=79372 us
queued_write_lock_slowpath+276
queued_write_lock_slowpath+276
_raw_write_lock_irq+69
copy_process+3474
kernel_clone+157
__do_sys_clone+102
__x64_sys_clone+37
do_syscall_64+56
entry_SYSCALL_64_after_hwframe+100=== [2026-11-15 15:18:06] klp scan held tasklist_lock(read) ~ 96864 us (comm=insmod)
klp_enable_patch.cold+247
init_module+477
do_one_initcall+70
do_init_module+82
load_module+2526
__do_sys_finit_module+200
__x64_sys_finit_module+24
do_syscall_64+56
entry_SYSCALL_64_after_hwframe+100=== [2026-11-15 15:18:08] klp scan held tasklist_lock(read) ~ 3504145 us (comm=kworker/8:2)
klp_transition_work_fn+36
process_one_work+492
worker_thread+83
kthread+235
ret_from_fork+31 crash> p klp_signals_cnt
klp_signals_cnt = $1 = 1
The latency budget of our redis workload is 50 ms, while the clone() call observed during the livepatch transition took ~79 ms.
Environment used for the reproduction
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
server: 256C AMD EPYC server
tasks: 1000~2000
number of patched functions: around 80
Solution
========
The redis application can work around this issue by removing the clone() from its data flush path, but I believe we should improve this in the kernel if we can.
In 2025, we discussed completely replacing tasklist_lock with RCU [1], but Petr pointed out that it is far more complicated than we expected [2], so we ended up abandoning that idea.
We still have a more lightweight way to partially replace this heavy tasklist_lock with RCU, as Josh suggested [3]:
: Maybe klp_try_complete_transition() could iterate the tasks in two : passes? The first pass would use rcu_read_lock(). Then if all tasks : appear to be patched, try again with tasklist_lock.
This patch implements that suggestion.
Verification
============
I verified this solution on my test server:
server: 64C AMD virtual server
tasks: ~2000 processes with ~8000 threads, ~30 running number of patched functions: 200
- Before this patch
the maximum clone() latency during the livepatch transition exceeded 500 ms.
- After this patch
the maximum clone() latency during the livepatch transition is below 20 ms, which is within the range expected by the redis side.
--- kernel/livepatch/transition.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
kernel/livepatch/transition.c +20 -0
@@ -444,7 +444,27 @@ void klp_try_complete_transition(void)* * Usually this will transition most (or all) of the tasks on a system * unless the patch includes changes to a very common function. +* +* To avoid holding the tasklist_lock for too long, we iterate over the +* tasks in two passes. In the first pass, we use the lightweight RCU +* read lock to iterate through most tasks. This pass will cover the +* majority of tasks, except for some newly forked tasks which may be +* added into the tasklist concurrently. In the second pass, we acquire +* the tasklist_lock to handle the tasks missed in the first pass. */ + +/* First pass: Iterate over tasks using the lightweight RCU read lock. */ +rcu_read_lock (); +for_each_process_thread (g, task) { +/* klp_check_stack() requires preemption to be disabled */ +if (task == current) +continue ; + +klp_try_switch_task (task); + } +rcu_read_unlock (); + +/* Second pass: Iterate over missed tasks, holding tasklist_lock. */ read_lock (&tasklist_lock);for_each_process_thread (g, task)if (!klp_try_switch_task (task))
Signature · 1 line
2.52.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] livepatch: race between klp_try_complete_transition() and klp_copy_process() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] livepatch: race between klp_try_complete_transition() and klp_copy_process()
--
commit ceb01fc48f77409ab4a5313629520f441760c90c
Author: Yafang Shao <[email protected]>
livepatch: Avoid blocking tasklist_lock too long
This commit splits the task iteration in klp_try_complete_transition() into a first pass under rcu_read_lock() and a second pass under tasklist_lock to reduce tasklist_lock hold times during livepatch transitions.
Quoted 17 lines
kernel/livepatch/transition.c +8
-0@@ -444,7 +444,27 @@ void klp_try_complete_transition(void)* * Usually this will transition most (or all) of the tasks on a system * unless the patch includes changes to a very common function. +* +* To avoid holding the tasklist_lock for too long, we iterate over the +* tasks in two passes. In the first pass, we use the lightweight RCU +* read lock to iterate through most tasks. This pass will cover the +* majority of tasks, except for some newly forked tasks which may be +* added into the tasklist concurrently. In the second pass, we acquire +* the tasklist_lock to handle the tasks missed in the first pass. */ +
kernel/livepatch/transition.c void klp_try_complete_transition(void) · line 465
Quoted 10 lines
kernel/livepatch/transition.c +10
-0+/* First pass: Iterate over tasks using the lightweight RCU read lock. */ +rcu_read_lock (); +for_each_process_thread (g, task) { +/* klp_check_stack() requires preemption to be disabled */ +if (task == current) +continue ; + +klp_try_switch_task (task); + } +rcu_read_unlock ();
[Severity: Medium]
Can this RCU pass race with klp_copy_process() on CONFIG_PREEMPT_RT kernels and leave a newly forked child with TIF_PATCH_PENDING still set when the transition completes?
klp_copy_process() relies on tasklist_lock to serialize against transition updates while copying TIF_PATCH_PENDING and patch_state from current to child:
kernel/livepatch/transition.c:klp_copy_process() {
if (test_tsk_thread_flag(current, TIF_PATCH_PENDING))
set_tsk_thread_flag(child, TIF_PATCH_PENDING);
else
clear_tsk_thread_flag(child, TIF_PATCH_PENDING);
child->patch_state = current->patch_state;
}On CONFIG_PREEMPT_RT, write_lock_irq(&tasklist_lock) in copy_process() is a preemptible rtmutex lock, and unwinders such as x86_64 ORC unwind reliably across in-kernel preemption frames:
CPU0 (parent in fork) CPU1 (klp_try_complete_transition)copy_process()
write_lock_irq(&tasklist_lock)
klp_copy_process(child)
set_tsk_thread_flag(child,
TIF_PATCH_PENDING)
<preempted>
/* First pass under rcu_read_lock() */
klp_try_switch_task(parent)
...
clear_tsk_thread_flag(parent,
TIF_PATCH_PENDING)
parent->patch_state = klp_target_state
child->patch_state =
current->patch_state
write_unlock_irq(&tasklist_lock)
/* Second pass under tasklist_lock */
klp_try_switch_task(child)
if (task->patch_state == klp_target_state)
return true;
klp_complete_transition()
WARN_ON_ONCE(test_tsk_thread_flag(child,
TIF_PATCH_PENDING)) <-- stale flagBecause child->patch_state already matches klp_target_state, the second pass under tasklist_lock returns early in klp_try_switch_task() without clearing TIF_PATCH_PENDING on child. klp_complete_transition() then triggers WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING)) and leaves the flag set on the child task.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 95 lines · On Tue, Nov 17, 2026 at 9:29 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] livepatch: race betweenklp_try_complete_transition()andklp_copy_process()
--commit
ceb01fc48f77409ab4a5313629520f441760c90c
Author: Yafang Shao <[email protected]>livepatch: Avoid blocking
tasklist_locktoo longThis commit splits the task iteration in
klp_try_complete_transition()into a first pass underrcu_read_lock()and a second pass undertasklist_lockto reducetasklist_lockhold times during livepatch transitions.Link https://github.com/redis/redis [0]Quoted 17 lines
kernel/livepatch/transition.c +8
-0@@ -444,7 +444,27 @@ void klp_try_complete_transition(void)* * Usually this will transition most (or all) of the tasks on a system * unless the patch includes changes to a very common function. +* +* To avoid holding the tasklist_lock for too long, we iterate over the +* tasks in two passes. In the first pass, we use the lightweight RCU +* read lock to iterate through most tasks. This pass will cover the +* majority of tasks, except for some newly forked tasks which may be +* added into the tasklist concurrently. In the second pass, we acquire +* the tasklist_lock to handle the tasks missed in the first pass. */ +kernel/livepatch/transition.c void klp_try_complete_transition(void) · line 465
Quoted 10 lines
kernel/livepatch/transition.c +10
-0+/* First pass: Iterate over tasks using the lightweight RCU read lock. */ +rcu_read_lock (); +for_each_process_thread (g, task) { +/* klp_check_stack() requires preemption to be disabled */ +if (task == current) +continue ; + +klp_try_switch_task (task); + } +rcu_read_unlock ();[Severity: Medium]
Can this RCU pass race withklp_copy_process()onCONFIG_PREEMPT_RTkernels and leave a newly forked child withTIF_PATCH_PENDINGstill set when the transition completes?
klp_copy_process()relies ontasklist_lockto serialize against transition updates while copyingTIF_PATCH_PENDINGandpatch_statefrom current to child:kernel/livepatch/transition.c:klp_copy_process() { if (test_tsk_thread_flag(current, TIF_PATCH_PENDING)) set_tsk_thread_flag(child, TIF_PATCH_PENDING); else clear_tsk_thread_flag(child, TIF_PATCH_PENDING); child->patch_state = current->patch_state; }On
CONFIG_PREEMPT_RT,write_lock_irq(&tasklist_lock)incopy_process()is a preemptible rtmutex lock, and unwinders such asx86_64ORC unwind reliably across in-kernel preemption frames:CPU0 (parent in fork) CPU1 (
klp_try_complete_transition)copy_process()
write_lock_irq(&tasklist_lock)
klp_copy_process(child)set_tsk_thread_flag(child, TIF_PATCH_PENDING) <preempted> /* First pass under rcu_read_lock() */ klp_try_switch_task(parent) ... clear_tsk_thread_flag(parent, TIF_PATCH_PENDING) parent->patch_state = klp_target_state child->patch_state = current->patch_state write_unlock_irq(&tasklist_lock) /* Second pass under tasklist_lock */ klp_try_switch_task(child) if (task->patch_state == klp_target_state) return true; klp_complete_transition() WARN_ON_ONCE(test_tsk_thread_flag(child, TIF_PATCH_PENDING)) <-- stale flag
Quoted 5 lines
Because
child->patch_statealready matchesklp_target_state, the second pass undertasklist_lockreturns early inklp_try_switch_task()without clearingTIF_PATCH_PENDINGon child.klp_complete_transition()then triggersWARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING))and leaves the flag set on the child task.
Nice catch!
Perhaps it can be resolved by explicitly disabling the preemption?
kernel/livepatch/transition.c +2 -0
@@ -706,12 +706,14 @@ void klp_copy_process(struct task_struct *child)* klp_update_patch_state(current) and __klp_sched_try_switch(), but we * cannot race with them because we are current. */ +preempt_disable ();if (test_tsk_thread_flag (current,TIF_PATCH_PENDING ))set_tsk_thread_flag (child,TIF_PATCH_PENDING );else clear_tsk_thread_flag (child,TIF_PATCH_PENDING ); child->patch_state = current->patch_state; +preempt_enable (); }/*
Signature · 2 lines
Regards Yafang
Josh Poimboeuf
maintainerQuoted 130 lines · Yafang Shao
Problem
=======As more and more patches accumulate in the cumulative livepatch, we see an increasing number of latency spikes from latency-sensitive services during livepatch upgrades. The most recent case is the redis
data_server[0].With some tracing added to the userspace redis code and a hard trace in the kernel, we finally tracked the root cause down to
tasklist_lockcontention during livepatch transition.When redis flushes data, it clones a new task, which takes the write side of
tasklist_lock. If a livepatch upgrade is in progress at the same time,tasklist_lockis held for the whole transition. Below is the trace we captured while reproducing the issue:=== [2026-11-15 15:18:06] tid=1635309 comm=data_server WRITE wait=79372 us
queued_write_lock_slowpath+276 queued_write_lock_slowpath+276 _raw_write_lock_irq+69 copy_process+3474 kernel_clone+157 __do_sys_clone+102 __x64_sys_clone+37 do_syscall_64+56 entry_SYSCALL_64_after_hwframe+100=== [2026-11-15 15:18:06] klp scan held
tasklist_lock(read)~ 96864 us (comm=insmod)klp_enable_patch.cold+247 init_module+477 do_one_initcall+70 do_init_module+82 load_module+2526 __do_sys_finit_module+200 __x64_sys_finit_module+24 do_syscall_64+56 entry_SYSCALL_64_after_hwframe+100=== [2026-11-15 15:18:08] klp scan held
tasklist_lock(read)~ 3504145 us (comm=kworker/8:2)klp_transition_work_fn+36 process_one_work+492 worker_thread+83 kthread+235 ret_from_fork+31crash> p
klp_signals_cnt
klp_signals_cnt= $1 = 1The latency budget of our redis workload is 50 ms, while the
clone()call observed during the livepatch transition took ~79 ms.Environment used for the reproduction
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~server: 256C AMD EPYC server
tasks: 1000~2000
number of patched functions: around 80Solution
========The redis application can work around this issue by removing the
clone()from its data flush path, but I believe we should improve this in the kernel if we can.In 2025, we discussed completely replacing
tasklist_lockwith RCU [1], but Petr pointed out that it is far more complicated than we expected [2], so we ended up abandoning that idea.We still have a more lightweight way to partially replace this heavy
tasklist_lockwith RCU, as Josh suggested [3]:: Maybe
klp_try_complete_transition()could iterate the tasks in two : passes? The first pass would usercu_read_lock(). Then if all tasks : appear to be patched, try again withtasklist_lock.This patch implements that suggestion.
Verification
============I verified this solution on my test server:
server: 64C AMD virtual server
tasks: ~2000 processes with ~8000 threads, ~30 running number of patched functions: 200- Before this patch
the maximumclone()latency during the livepatch transition exceeded 500 ms.- After this patch
the maximumclone()latency during the livepatch transition is below 20 ms, which is within the range expected by the redis side.Link https://github.com/redis/redis [0]Suggested-by Josh Poimboeuf <[email protected]>Signed-off-by Yafang Shao <[email protected]>--- kernel/livepatch/transition.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)kernel/livepatch/transition.c +13
-0@@ -444,7 +444,27 @@ void klp_try_complete_transition(void)* * Usually this will transition most (or all) of the tasks on a system * unless the patch includes changes to a very common function. +* +* To avoid holding the tasklist_lock for too long, we iterate over the +* tasks in two passes. In the first pass, we use the lightweight RCU +* read lock to iterate through most tasks. This pass will cover the +* majority of tasks, except for some newly forked tasks which may be +* added into the tasklist concurrently. In the second pass, we acquire +* the tasklist_lock to handle the tasks missed in the first pass. */ + +/* First pass: Iterate over tasks using the lightweight RCU read lock. */ +rcu_read_lock (); +for_each_process_thread (g, task) { +/* klp_check_stack() requires preemption to be disabled */ +if (task == current)
kernel/livepatch/transition.c void klp_try_complete_transition(void) · line 470
Quoted 10 lines
kernel/livepatch/transition.c +7
-0+continue ; + +klp_try_switch_task (task); + } +rcu_read_unlock (); + +/* Second pass: Iterate over missed tasks, holding tasklist_lock. */ read_lock (&tasklist_lock);for_each_process_thread (g, task)if (!klp_try_switch_task (task))
I think the performance can be improved further by only doing the 2nd pass if the 1st pass fully succeeded?
kernel/livepatch/transition.c +13 -7
@@ -460,16 +460,22 @@ void klp_try_complete_transition(void)if (task == current)continue ; -klp_try_switch_task (task); +if (!klp_try_switch_task (task)) + complete = false; }rcu_read_unlock (); -/* Second pass: Iterate over missed tasks, holding tasklist_lock. */ -read_lock (&tasklist_lock); -for_each_process_thread (g, task) -if (!klp_try_switch_task (task)) - complete = false; -read_unlock (&tasklist_lock); +/* +* If the first pass succeeded, grab the more heavyweight tasklist lock +* to check current and any newly forked tasks. +*/ +if (complete) { +read_lock (&tasklist_lock); +for_each_process_thread (g, task) +if (!klp_try_switch_task (task)) + complete = false; +read_unlock (&tasklist_lock); + }/* * Ditto for the idle "swapper" tasks.
Quoted 137 lines · Josh Poimboeuf
Quoted 135 lines · Yafang Shao
Problem
=======As more and more patches accumulate in the cumulative livepatch, we see an increasing number of latency spikes from latency-sensitive services during livepatch upgrades. The most recent case is the redis
data_server[0].With some tracing added to the userspace redis code and a hard trace in the kernel, we finally tracked the root cause down to
tasklist_lockcontention during livepatch transition.When redis flushes data, it clones a new task, which takes the write side of
tasklist_lock. If a livepatch upgrade is in progress at the same time,tasklist_lockis held for the whole transition. Below is the trace we captured while reproducing the issue:=== [2026-11-15 15:18:06] tid=1635309 comm=data_server WRITE wait=79372 us
queued_write_lock_slowpath+276 queued_write_lock_slowpath+276 _raw_write_lock_irq+69 copy_process+3474 kernel_clone+157 __do_sys_clone+102 __x64_sys_clone+37 do_syscall_64+56 entry_SYSCALL_64_after_hwframe+100=== [2026-11-15 15:18:06] klp scan held
tasklist_lock(read)~ 96864 us (comm=insmod)klp_enable_patch.cold+247 init_module+477 do_one_initcall+70 do_init_module+82 load_module+2526 __do_sys_finit_module+200 __x64_sys_finit_module+24 do_syscall_64+56 entry_SYSCALL_64_after_hwframe+100=== [2026-11-15 15:18:08] klp scan held
tasklist_lock(read)~ 3504145 us (comm=kworker/8:2)klp_transition_work_fn+36 process_one_work+492 worker_thread+83 kthread+235 ret_from_fork+31crash> p
klp_signals_cnt
klp_signals_cnt= $1 = 1The latency budget of our redis workload is 50 ms, while the
clone()call observed during the livepatch transition took ~79 ms.Environment used for the reproduction
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~server: 256C AMD EPYC server
tasks: 1000~2000
number of patched functions: around 80Solution
========The redis application can work around this issue by removing the
clone()from its data flush path, but I believe we should improve this in the kernel if we can.In 2025, we discussed completely replacing
tasklist_lockwith RCU [1], but Petr pointed out that it is far more complicated than we expected [2], so we ended up abandoning that idea.We still have a more lightweight way to partially replace this heavy
tasklist_lockwith RCU, as Josh suggested [3]:: Maybe
klp_try_complete_transition()could iterate the tasks in two : passes? The first pass would usercu_read_lock(). Then if all tasks : appear to be patched, try again withtasklist_lock.This patch implements that suggestion.
Verification
============I verified this solution on my test server:
server: 64C AMD virtual server
tasks: ~2000 processes with ~8000 threads, ~30 running number of patched functions: 200- Before this patch
the maximumclone()latency during the livepatch transition exceeded 500 ms.- After this patch
the maximumclone()latency during the livepatch transition is below 20 ms, which is within the range expected by the redis side.Link https://github.com/redis/redis [0]Suggested-by Josh Poimboeuf <[email protected]>Signed-off-by Yafang Shao <[email protected]>--- kernel/livepatch/transition.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)kernel/livepatch/transition.c +18
-0@@ -444,7 +444,27 @@ void klp_try_complete_transition(void)* * Usually this will transition most (or all) of the tasks on a system * unless the patch includes changes to a very common function. +* +* To avoid holding the tasklist_lock for too long, we iterate over the +* tasks in two passes. In the first pass, we use the lightweight RCU +* read lock to iterate through most tasks. This pass will cover the +* majority of tasks, except for some newly forked tasks which may be +* added into the tasklist concurrently. In the second pass, we acquire +* the tasklist_lock to handle the tasks missed in the first pass. */ + +/* First pass: Iterate over tasks using the lightweight RCU read lock. */ +rcu_read_lock (); +for_each_process_thread (g, task) { +/* klp_check_stack() requires preemption to be disabled */ +if (task == current) +continue ; + +klp_try_switch_task (task); + } +rcu_read_unlock ();
Quoted 8 lines
kernel/livepatch/transition.c void klp_try_complete_transition(void) · line 470
Quoted 5 lines
kernel/livepatch/transition.c +2
-0+ +/* Second pass: Iterate over missed tasks, holding tasklist_lock. */ read_lock (&tasklist_lock);for_each_process_thread (g, task)if (!klp_try_switch_task (task))I think the performance can be improved further by only doing the 2nd pass if the 1st pass fully succeeded?
nice suggestion.
I will do it.
Quoted 35 lines
kernel/livepatch/transition.c +13
-7@@ -460,16 +460,22 @@ void klp_try_complete_transition(void)if (task == current)continue ; -klp_try_switch_task (task); +if (!klp_try_switch_task (task)) + complete = false; }rcu_read_unlock (); -/* Second pass: Iterate over missed tasks, holding tasklist_lock. */ -read_lock (&tasklist_lock); -for_each_process_thread (g, task) -if (!klp_try_switch_task (task)) - complete = false; -read_unlock (&tasklist_lock); +/* +* If the first pass succeeded, grab the more heavyweight tasklist lock +* to check current and any newly forked tasks. +*/ +if (complete) { +read_lock (&tasklist_lock); +for_each_process_thread (g, task) +if (!klp_try_switch_task (task)) + complete = false; +read_unlock (&tasklist_lock); + }/* * Ditto for the idle "swapper" tasks.
Signature · 2 lines
Regards Yafang