11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d83a7cb3SJosh Poimboeuf /*
3d83a7cb3SJosh Poimboeuf * transition.c - Kernel Live Patching transition functions
4d83a7cb3SJosh Poimboeuf *
5d83a7cb3SJosh Poimboeuf * Copyright (C) 2015-2016 Josh Poimboeuf <jpoimboe@redhat.com>
6d83a7cb3SJosh Poimboeuf */
7d83a7cb3SJosh Poimboeuf
8d83a7cb3SJosh Poimboeuf #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9d83a7cb3SJosh Poimboeuf
10d83a7cb3SJosh Poimboeuf #include <linux/cpu.h>
11d83a7cb3SJosh Poimboeuf #include <linux/stacktrace.h>
12e3ff7c60SJosh Poimboeuf #include <linux/static_call.h>
1310517429SJiri Kosina #include "core.h"
14d83a7cb3SJosh Poimboeuf #include "patch.h"
15d83a7cb3SJosh Poimboeuf #include "transition.h"
16d83a7cb3SJosh Poimboeuf
17d83a7cb3SJosh Poimboeuf #define MAX_STACK_ENTRIES 100
1842cffe98SJosh Poimboeuf static DEFINE_PER_CPU(unsigned long[MAX_STACK_ENTRIES], klp_stack_entries);
19e92606faSJosh Poimboeuf
20d83a7cb3SJosh Poimboeuf #define STACK_ERR_BUF_SIZE 128
21d83a7cb3SJosh Poimboeuf
22cba82deaSMiroslav Benes #define SIGNALS_TIMEOUT 15
23cba82deaSMiroslav Benes
24d83a7cb3SJosh Poimboeuf struct klp_patch *klp_transition_patch;
25d83a7cb3SJosh Poimboeuf
26*d927752fSWardenjohn static int klp_target_state = KLP_TRANSITION_IDLE;
27d83a7cb3SJosh Poimboeuf
28cba82deaSMiroslav Benes static unsigned int klp_signals_cnt;
29cba82deaSMiroslav Benes
30d83a7cb3SJosh Poimboeuf /*
31e3ff7c60SJosh Poimboeuf * When a livepatch is in progress, enable klp stack checking in
32e3ff7c60SJosh Poimboeuf * cond_resched(). This helps CPU-bound kthreads get patched.
33e3ff7c60SJosh Poimboeuf */
34e3ff7c60SJosh Poimboeuf #if defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
35e3ff7c60SJosh Poimboeuf
36e3ff7c60SJosh Poimboeuf #define klp_cond_resched_enable() sched_dynamic_klp_enable()
37e3ff7c60SJosh Poimboeuf #define klp_cond_resched_disable() sched_dynamic_klp_disable()
38e3ff7c60SJosh Poimboeuf
39e3ff7c60SJosh Poimboeuf #else /* !CONFIG_PREEMPT_DYNAMIC || !CONFIG_HAVE_PREEMPT_DYNAMIC_CALL */
40e3ff7c60SJosh Poimboeuf
41e3ff7c60SJosh Poimboeuf DEFINE_STATIC_KEY_FALSE(klp_sched_try_switch_key);
42e3ff7c60SJosh Poimboeuf EXPORT_SYMBOL(klp_sched_try_switch_key);
43e3ff7c60SJosh Poimboeuf
44e3ff7c60SJosh Poimboeuf #define klp_cond_resched_enable() static_branch_enable(&klp_sched_try_switch_key)
45e3ff7c60SJosh Poimboeuf #define klp_cond_resched_disable() static_branch_disable(&klp_sched_try_switch_key)
46e3ff7c60SJosh Poimboeuf
47e3ff7c60SJosh Poimboeuf #endif /* CONFIG_PREEMPT_DYNAMIC && CONFIG_HAVE_PREEMPT_DYNAMIC_CALL */
48e3ff7c60SJosh Poimboeuf
49e3ff7c60SJosh Poimboeuf /*
50d83a7cb3SJosh Poimboeuf * This work can be performed periodically to finish patching or unpatching any
51d83a7cb3SJosh Poimboeuf * "straggler" tasks which failed to transition in the first attempt.
52d83a7cb3SJosh Poimboeuf */
klp_transition_work_fn(struct work_struct * work)53d83a7cb3SJosh Poimboeuf static void klp_transition_work_fn(struct work_struct *work)
54d83a7cb3SJosh Poimboeuf {
55d83a7cb3SJosh Poimboeuf mutex_lock(&klp_mutex);
56d83a7cb3SJosh Poimboeuf
57d83a7cb3SJosh Poimboeuf if (klp_transition_patch)
58d83a7cb3SJosh Poimboeuf klp_try_complete_transition();
59d83a7cb3SJosh Poimboeuf
60d83a7cb3SJosh Poimboeuf mutex_unlock(&klp_mutex);
61d83a7cb3SJosh Poimboeuf }
62d83a7cb3SJosh Poimboeuf static DECLARE_DELAYED_WORK(klp_transition_work, klp_transition_work_fn);
63d83a7cb3SJosh Poimboeuf
64d83a7cb3SJosh Poimboeuf /*
65842c0884SPetr Mladek * This function is just a stub to implement a hard force
666932689eSPaul E. McKenney * of synchronize_rcu(). This requires synchronizing
67842c0884SPetr Mladek * tasks even in userspace and idle.
68842c0884SPetr Mladek */
klp_sync(struct work_struct * work)69842c0884SPetr Mladek static void klp_sync(struct work_struct *work)
70842c0884SPetr Mladek {
71842c0884SPetr Mladek }
72842c0884SPetr Mladek
73842c0884SPetr Mladek /*
74842c0884SPetr Mladek * We allow to patch also functions where RCU is not watching,
75842c0884SPetr Mladek * e.g. before user_exit(). We can not rely on the RCU infrastructure
76842c0884SPetr Mladek * to do the synchronization. Instead hard force the sched synchronization.
77842c0884SPetr Mladek *
78842c0884SPetr Mladek * This approach allows to use RCU functions for manipulating func_stack
79842c0884SPetr Mladek * safely.
80842c0884SPetr Mladek */
klp_synchronize_transition(void)81842c0884SPetr Mladek static void klp_synchronize_transition(void)
82842c0884SPetr Mladek {
83842c0884SPetr Mladek schedule_on_each_cpu(klp_sync);
84842c0884SPetr Mladek }
85842c0884SPetr Mladek
86842c0884SPetr Mladek /*
87d83a7cb3SJosh Poimboeuf * The transition to the target patch state is complete. Clean up the data
88d83a7cb3SJosh Poimboeuf * structures.
89d83a7cb3SJosh Poimboeuf */
klp_complete_transition(void)90d83a7cb3SJosh Poimboeuf static void klp_complete_transition(void)
91d83a7cb3SJosh Poimboeuf {
92d83a7cb3SJosh Poimboeuf struct klp_object *obj;
93d83a7cb3SJosh Poimboeuf struct klp_func *func;
94d83a7cb3SJosh Poimboeuf struct task_struct *g, *task;
95d83a7cb3SJosh Poimboeuf unsigned int cpu;
96d83a7cb3SJosh Poimboeuf
97af026796SJoe Lawrence pr_debug("'%s': completing %s transition\n",
98af026796SJoe Lawrence klp_transition_patch->mod->name,
99*d927752fSWardenjohn klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
100af026796SJoe Lawrence
101*d927752fSWardenjohn if (klp_transition_patch->replace && klp_target_state == KLP_TRANSITION_PATCHED) {
1027e35e4ebSPetr Mladek klp_unpatch_replaced_patches(klp_transition_patch);
103d697bad5SPetr Mladek klp_discard_nops(klp_transition_patch);
104d697bad5SPetr Mladek }
105e1452b60SJason Baron
106*d927752fSWardenjohn if (klp_target_state == KLP_TRANSITION_UNPATCHED) {
107d83a7cb3SJosh Poimboeuf /*
108*d927752fSWardenjohn * All tasks have transitioned to KLP_TRANSITION_UNPATCHED so we can now
109d83a7cb3SJosh Poimboeuf * remove the new functions from the func_stack.
110d83a7cb3SJosh Poimboeuf */
111d83a7cb3SJosh Poimboeuf klp_unpatch_objects(klp_transition_patch);
112d83a7cb3SJosh Poimboeuf
113d83a7cb3SJosh Poimboeuf /*
114d83a7cb3SJosh Poimboeuf * Make sure klp_ftrace_handler() can no longer see functions
115d83a7cb3SJosh Poimboeuf * from this patch on the ops->func_stack. Otherwise, after
116d83a7cb3SJosh Poimboeuf * func->transition gets cleared, the handler may choose a
117d83a7cb3SJosh Poimboeuf * removed function.
118d83a7cb3SJosh Poimboeuf */
119842c0884SPetr Mladek klp_synchronize_transition();
120d83a7cb3SJosh Poimboeuf }
121d83a7cb3SJosh Poimboeuf
122d0807da7SMiroslav Benes klp_for_each_object(klp_transition_patch, obj)
123d0807da7SMiroslav Benes klp_for_each_func(obj, func)
124d83a7cb3SJosh Poimboeuf func->transition = false;
1253ec24776SJosh Poimboeuf
126*d927752fSWardenjohn /* Prevent klp_ftrace_handler() from seeing KLP_TRANSITION_IDLE state */
127*d927752fSWardenjohn if (klp_target_state == KLP_TRANSITION_PATCHED)
128842c0884SPetr Mladek klp_synchronize_transition();
129d83a7cb3SJosh Poimboeuf
130d83a7cb3SJosh Poimboeuf read_lock(&tasklist_lock);
131d83a7cb3SJosh Poimboeuf for_each_process_thread(g, task) {
132d83a7cb3SJosh Poimboeuf WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING));
133*d927752fSWardenjohn task->patch_state = KLP_TRANSITION_IDLE;
134d83a7cb3SJosh Poimboeuf }
135d83a7cb3SJosh Poimboeuf read_unlock(&tasklist_lock);
136d83a7cb3SJosh Poimboeuf
137d83a7cb3SJosh Poimboeuf for_each_possible_cpu(cpu) {
138d83a7cb3SJosh Poimboeuf task = idle_task(cpu);
139d83a7cb3SJosh Poimboeuf WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING));
140*d927752fSWardenjohn task->patch_state = KLP_TRANSITION_IDLE;
141d83a7cb3SJosh Poimboeuf }
142d83a7cb3SJosh Poimboeuf
14393862e38SJoe Lawrence klp_for_each_object(klp_transition_patch, obj) {
14493862e38SJoe Lawrence if (!klp_is_object_loaded(obj))
14593862e38SJoe Lawrence continue;
146*d927752fSWardenjohn if (klp_target_state == KLP_TRANSITION_PATCHED)
14793862e38SJoe Lawrence klp_post_patch_callback(obj);
148*d927752fSWardenjohn else if (klp_target_state == KLP_TRANSITION_UNPATCHED)
14993862e38SJoe Lawrence klp_post_unpatch_callback(obj);
15093862e38SJoe Lawrence }
15193862e38SJoe Lawrence
1526116c303SJoe Lawrence pr_notice("'%s': %s complete\n", klp_transition_patch->mod->name,
153*d927752fSWardenjohn klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
1546116c303SJoe Lawrence
155*d927752fSWardenjohn klp_target_state = KLP_TRANSITION_IDLE;
156d83a7cb3SJosh Poimboeuf klp_transition_patch = NULL;
157d83a7cb3SJosh Poimboeuf }
158d83a7cb3SJosh Poimboeuf
159d83a7cb3SJosh Poimboeuf /*
160d83a7cb3SJosh Poimboeuf * This is called in the error path, to cancel a transition before it has
161d83a7cb3SJosh Poimboeuf * started, i.e. klp_init_transition() has been called but
162d83a7cb3SJosh Poimboeuf * klp_start_transition() hasn't. If the transition *has* been started,
163d83a7cb3SJosh Poimboeuf * klp_reverse_transition() should be used instead.
164d83a7cb3SJosh Poimboeuf */
klp_cancel_transition(void)165d83a7cb3SJosh Poimboeuf void klp_cancel_transition(void)
166d83a7cb3SJosh Poimboeuf {
167*d927752fSWardenjohn if (WARN_ON_ONCE(klp_target_state != KLP_TRANSITION_PATCHED))
1683ec24776SJosh Poimboeuf return;
1693ec24776SJosh Poimboeuf
170af026796SJoe Lawrence pr_debug("'%s': canceling patching transition, going to unpatch\n",
171af026796SJoe Lawrence klp_transition_patch->mod->name);
172af026796SJoe Lawrence
173*d927752fSWardenjohn klp_target_state = KLP_TRANSITION_UNPATCHED;
174d83a7cb3SJosh Poimboeuf klp_complete_transition();
175d83a7cb3SJosh Poimboeuf }
176d83a7cb3SJosh Poimboeuf
177d83a7cb3SJosh Poimboeuf /*
178d83a7cb3SJosh Poimboeuf * Switch the patched state of the task to the set of functions in the target
179d83a7cb3SJosh Poimboeuf * patch state.
180d83a7cb3SJosh Poimboeuf *
181d83a7cb3SJosh Poimboeuf * NOTE: If task is not 'current', the caller must ensure the task is inactive.
182d83a7cb3SJosh Poimboeuf * Otherwise klp_ftrace_handler() might read the wrong 'patch_state' value.
183d83a7cb3SJosh Poimboeuf */
klp_update_patch_state(struct task_struct * task)184d83a7cb3SJosh Poimboeuf void klp_update_patch_state(struct task_struct *task)
185d83a7cb3SJosh Poimboeuf {
186842c0884SPetr Mladek /*
1876932689eSPaul E. McKenney * A variant of synchronize_rcu() is used to allow patching functions
188842c0884SPetr Mladek * where RCU is not watching, see klp_synchronize_transition().
189842c0884SPetr Mladek */
190842c0884SPetr Mladek preempt_disable_notrace();
191d83a7cb3SJosh Poimboeuf
192d83a7cb3SJosh Poimboeuf /*
193d83a7cb3SJosh Poimboeuf * This test_and_clear_tsk_thread_flag() call also serves as a read
194d83a7cb3SJosh Poimboeuf * barrier (smp_rmb) for two cases:
195d83a7cb3SJosh Poimboeuf *
196d83a7cb3SJosh Poimboeuf * 1) Enforce the order of the TIF_PATCH_PENDING read and the
197e3ff7c60SJosh Poimboeuf * klp_target_state read. The corresponding write barriers are in
198e3ff7c60SJosh Poimboeuf * klp_init_transition() and klp_reverse_transition().
199d83a7cb3SJosh Poimboeuf *
200d83a7cb3SJosh Poimboeuf * 2) Enforce the order of the TIF_PATCH_PENDING read and a future read
201d83a7cb3SJosh Poimboeuf * of func->transition, if klp_ftrace_handler() is called later on
202d83a7cb3SJosh Poimboeuf * the same CPU. See __klp_disable_patch().
203d83a7cb3SJosh Poimboeuf */
204d83a7cb3SJosh Poimboeuf if (test_and_clear_tsk_thread_flag(task, TIF_PATCH_PENDING))
205d83a7cb3SJosh Poimboeuf task->patch_state = READ_ONCE(klp_target_state);
206d83a7cb3SJosh Poimboeuf
207842c0884SPetr Mladek preempt_enable_notrace();
208d83a7cb3SJosh Poimboeuf }
209d83a7cb3SJosh Poimboeuf
210d83a7cb3SJosh Poimboeuf /*
211d83a7cb3SJosh Poimboeuf * Determine whether the given stack trace includes any references to a
212d83a7cb3SJosh Poimboeuf * to-be-patched or to-be-unpatched function.
213d83a7cb3SJosh Poimboeuf */
klp_check_stack_func(struct klp_func * func,unsigned long * entries,unsigned int nr_entries)21425e39e32SThomas Gleixner static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
21525e39e32SThomas Gleixner unsigned int nr_entries)
216d83a7cb3SJosh Poimboeuf {
217d83a7cb3SJosh Poimboeuf unsigned long func_addr, func_size, address;
218d83a7cb3SJosh Poimboeuf struct klp_ops *ops;
219d83a7cb3SJosh Poimboeuf int i;
220d83a7cb3SJosh Poimboeuf
221*d927752fSWardenjohn if (klp_target_state == KLP_TRANSITION_UNPATCHED) {
222d83a7cb3SJosh Poimboeuf /*
223d83a7cb3SJosh Poimboeuf * Check for the to-be-unpatched function
224d83a7cb3SJosh Poimboeuf * (the func itself).
225d83a7cb3SJosh Poimboeuf */
226d83a7cb3SJosh Poimboeuf func_addr = (unsigned long)func->new_func;
227d83a7cb3SJosh Poimboeuf func_size = func->new_size;
228d83a7cb3SJosh Poimboeuf } else {
229d83a7cb3SJosh Poimboeuf /*
230d83a7cb3SJosh Poimboeuf * Check for the to-be-patched function
231d83a7cb3SJosh Poimboeuf * (the previous func).
232d83a7cb3SJosh Poimboeuf */
23319514910SPetr Mladek ops = klp_find_ops(func->old_func);
234d83a7cb3SJosh Poimboeuf
235d83a7cb3SJosh Poimboeuf if (list_is_singular(&ops->func_stack)) {
236d83a7cb3SJosh Poimboeuf /* original function */
23719514910SPetr Mladek func_addr = (unsigned long)func->old_func;
238d83a7cb3SJosh Poimboeuf func_size = func->old_size;
239d83a7cb3SJosh Poimboeuf } else {
240d83a7cb3SJosh Poimboeuf /* previously patched function */
241d83a7cb3SJosh Poimboeuf struct klp_func *prev;
242d83a7cb3SJosh Poimboeuf
243d83a7cb3SJosh Poimboeuf prev = list_next_entry(func, stack_node);
244d83a7cb3SJosh Poimboeuf func_addr = (unsigned long)prev->new_func;
245d83a7cb3SJosh Poimboeuf func_size = prev->new_size;
246d83a7cb3SJosh Poimboeuf }
247d83a7cb3SJosh Poimboeuf }
248d83a7cb3SJosh Poimboeuf
24953910ef7SZhen Lei for (i = 0; i < nr_entries; i++) {
25053910ef7SZhen Lei address = entries[i];
25153910ef7SZhen Lei
252d83a7cb3SJosh Poimboeuf if (address >= func_addr && address < func_addr + func_size)
253d83a7cb3SJosh Poimboeuf return -EAGAIN;
254d83a7cb3SJosh Poimboeuf }
255d83a7cb3SJosh Poimboeuf
256d83a7cb3SJosh Poimboeuf return 0;
257d83a7cb3SJosh Poimboeuf }
258d83a7cb3SJosh Poimboeuf
259d83a7cb3SJosh Poimboeuf /*
260d83a7cb3SJosh Poimboeuf * Determine whether it's safe to transition the task to the target patch state
261d83a7cb3SJosh Poimboeuf * by looking for any to-be-patched or to-be-unpatched functions on its stack.
262d83a7cb3SJosh Poimboeuf */
klp_check_stack(struct task_struct * task,const char ** oldname)26300619f7cSPeter Zijlstra static int klp_check_stack(struct task_struct *task, const char **oldname)
264d83a7cb3SJosh Poimboeuf {
265e92606faSJosh Poimboeuf unsigned long *entries = this_cpu_ptr(klp_stack_entries);
266d83a7cb3SJosh Poimboeuf struct klp_object *obj;
267d83a7cb3SJosh Poimboeuf struct klp_func *func;
26825e39e32SThomas Gleixner int ret, nr_entries;
269d83a7cb3SJosh Poimboeuf
270e92606faSJosh Poimboeuf /* Protect 'klp_stack_entries' */
271e92606faSJosh Poimboeuf lockdep_assert_preemption_disabled();
272e92606faSJosh Poimboeuf
273e92606faSJosh Poimboeuf ret = stack_trace_save_tsk_reliable(task, entries, MAX_STACK_ENTRIES);
27400619f7cSPeter Zijlstra if (ret < 0)
27500619f7cSPeter Zijlstra return -EINVAL;
27625e39e32SThomas Gleixner nr_entries = ret;
277d83a7cb3SJosh Poimboeuf
278d83a7cb3SJosh Poimboeuf klp_for_each_object(klp_transition_patch, obj) {
279d83a7cb3SJosh Poimboeuf if (!obj->patched)
280d83a7cb3SJosh Poimboeuf continue;
281d83a7cb3SJosh Poimboeuf klp_for_each_func(obj, func) {
28225e39e32SThomas Gleixner ret = klp_check_stack_func(func, entries, nr_entries);
283d83a7cb3SJosh Poimboeuf if (ret) {
28400619f7cSPeter Zijlstra *oldname = func->old_name;
28500619f7cSPeter Zijlstra return -EADDRINUSE;
286d83a7cb3SJosh Poimboeuf }
287d83a7cb3SJosh Poimboeuf }
288d83a7cb3SJosh Poimboeuf }
289d83a7cb3SJosh Poimboeuf
290d83a7cb3SJosh Poimboeuf return 0;
291d83a7cb3SJosh Poimboeuf }
292d83a7cb3SJosh Poimboeuf
klp_check_and_switch_task(struct task_struct * task,void * arg)29300619f7cSPeter Zijlstra static int klp_check_and_switch_task(struct task_struct *task, void *arg)
29400619f7cSPeter Zijlstra {
29500619f7cSPeter Zijlstra int ret;
29600619f7cSPeter Zijlstra
29700619f7cSPeter Zijlstra if (task_curr(task) && task != current)
29800619f7cSPeter Zijlstra return -EBUSY;
29900619f7cSPeter Zijlstra
30000619f7cSPeter Zijlstra ret = klp_check_stack(task, arg);
30100619f7cSPeter Zijlstra if (ret)
30200619f7cSPeter Zijlstra return ret;
30300619f7cSPeter Zijlstra
30400619f7cSPeter Zijlstra clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
30500619f7cSPeter Zijlstra task->patch_state = klp_target_state;
30600619f7cSPeter Zijlstra return 0;
30700619f7cSPeter Zijlstra }
30800619f7cSPeter Zijlstra
309d83a7cb3SJosh Poimboeuf /*
310d83a7cb3SJosh Poimboeuf * Try to safely switch a task to the target patch state. If it's currently
311d83a7cb3SJosh Poimboeuf * running, or it's sleeping on a to-be-patched or to-be-unpatched function, or
312d83a7cb3SJosh Poimboeuf * if the stack is unreliable, return false.
313d83a7cb3SJosh Poimboeuf */
klp_try_switch_task(struct task_struct * task)314d83a7cb3SJosh Poimboeuf static bool klp_try_switch_task(struct task_struct *task)
315d83a7cb3SJosh Poimboeuf {
31600619f7cSPeter Zijlstra const char *old_name;
317d83a7cb3SJosh Poimboeuf int ret;
318d83a7cb3SJosh Poimboeuf
319d83a7cb3SJosh Poimboeuf /* check if this task has already switched over */
320d83a7cb3SJosh Poimboeuf if (task->patch_state == klp_target_state)
321d83a7cb3SJosh Poimboeuf return true;
322d83a7cb3SJosh Poimboeuf
323d83a7cb3SJosh Poimboeuf /*
32467059d65SMiroslav Benes * For arches which don't have reliable stack traces, we have to rely
32567059d65SMiroslav Benes * on other methods (e.g., switching tasks at kernel exit).
32667059d65SMiroslav Benes */
32767059d65SMiroslav Benes if (!klp_have_reliable_stack())
32867059d65SMiroslav Benes return false;
32967059d65SMiroslav Benes
33067059d65SMiroslav Benes /*
331d83a7cb3SJosh Poimboeuf * Now try to check the stack for any to-be-patched or to-be-unpatched
332d83a7cb3SJosh Poimboeuf * functions. If all goes well, switch the task to the target patch
333d83a7cb3SJosh Poimboeuf * state.
334d83a7cb3SJosh Poimboeuf */
335383439d3SJosh Poimboeuf if (task == current)
336383439d3SJosh Poimboeuf ret = klp_check_and_switch_task(current, &old_name);
337383439d3SJosh Poimboeuf else
33800619f7cSPeter Zijlstra ret = task_call_func(task, klp_check_and_switch_task, &old_name);
339383439d3SJosh Poimboeuf
34000619f7cSPeter Zijlstra switch (ret) {
34100619f7cSPeter Zijlstra case 0: /* success */
34200619f7cSPeter Zijlstra break;
343d83a7cb3SJosh Poimboeuf
34400619f7cSPeter Zijlstra case -EBUSY: /* klp_check_and_switch_task() */
34500619f7cSPeter Zijlstra pr_debug("%s: %s:%d is running\n",
34600619f7cSPeter Zijlstra __func__, task->comm, task->pid);
34700619f7cSPeter Zijlstra break;
34800619f7cSPeter Zijlstra case -EINVAL: /* klp_check_and_switch_task() */
34900619f7cSPeter Zijlstra pr_debug("%s: %s:%d has an unreliable stack\n",
35000619f7cSPeter Zijlstra __func__, task->comm, task->pid);
35100619f7cSPeter Zijlstra break;
35200619f7cSPeter Zijlstra case -EADDRINUSE: /* klp_check_and_switch_task() */
35300619f7cSPeter Zijlstra pr_debug("%s: %s:%d is sleeping on function %s\n",
35400619f7cSPeter Zijlstra __func__, task->comm, task->pid, old_name);
35500619f7cSPeter Zijlstra break;
35600619f7cSPeter Zijlstra
35700619f7cSPeter Zijlstra default:
35800619f7cSPeter Zijlstra pr_debug("%s: Unknown error code (%d) when trying to switch %s:%d\n",
35900619f7cSPeter Zijlstra __func__, ret, task->comm, task->pid);
36000619f7cSPeter Zijlstra break;
361d83a7cb3SJosh Poimboeuf }
362d83a7cb3SJosh Poimboeuf
36300619f7cSPeter Zijlstra return !ret;
364d83a7cb3SJosh Poimboeuf }
365d83a7cb3SJosh Poimboeuf
__klp_sched_try_switch(void)366e3ff7c60SJosh Poimboeuf void __klp_sched_try_switch(void)
367e3ff7c60SJosh Poimboeuf {
368e3ff7c60SJosh Poimboeuf if (likely(!klp_patch_pending(current)))
369e3ff7c60SJosh Poimboeuf return;
370e3ff7c60SJosh Poimboeuf
371e3ff7c60SJosh Poimboeuf /*
372e3ff7c60SJosh Poimboeuf * This function is called from cond_resched() which is called in many
373e3ff7c60SJosh Poimboeuf * places throughout the kernel. Using the klp_mutex here might
374e3ff7c60SJosh Poimboeuf * deadlock.
375e3ff7c60SJosh Poimboeuf *
376e3ff7c60SJosh Poimboeuf * Instead, disable preemption to prevent racing with other callers of
377e3ff7c60SJosh Poimboeuf * klp_try_switch_task(). Thanks to task_call_func() they won't be
378e3ff7c60SJosh Poimboeuf * able to switch this task while it's running.
379e3ff7c60SJosh Poimboeuf */
380e3ff7c60SJosh Poimboeuf preempt_disable();
381e3ff7c60SJosh Poimboeuf
382e3ff7c60SJosh Poimboeuf /*
383e3ff7c60SJosh Poimboeuf * Make sure current didn't get patched between the above check and
384e3ff7c60SJosh Poimboeuf * preempt_disable().
385e3ff7c60SJosh Poimboeuf */
386e3ff7c60SJosh Poimboeuf if (unlikely(!klp_patch_pending(current)))
387e3ff7c60SJosh Poimboeuf goto out;
388e3ff7c60SJosh Poimboeuf
389e3ff7c60SJosh Poimboeuf /*
390e3ff7c60SJosh Poimboeuf * Enforce the order of the TIF_PATCH_PENDING read above and the
391e3ff7c60SJosh Poimboeuf * klp_target_state read in klp_try_switch_task(). The corresponding
392e3ff7c60SJosh Poimboeuf * write barriers are in klp_init_transition() and
393e3ff7c60SJosh Poimboeuf * klp_reverse_transition().
394e3ff7c60SJosh Poimboeuf */
395e3ff7c60SJosh Poimboeuf smp_rmb();
396e3ff7c60SJosh Poimboeuf
397e3ff7c60SJosh Poimboeuf klp_try_switch_task(current);
398e3ff7c60SJosh Poimboeuf
399e3ff7c60SJosh Poimboeuf out:
400e3ff7c60SJosh Poimboeuf preempt_enable();
401e3ff7c60SJosh Poimboeuf }
402e3ff7c60SJosh Poimboeuf EXPORT_SYMBOL(__klp_sched_try_switch);
403e3ff7c60SJosh Poimboeuf
404d83a7cb3SJosh Poimboeuf /*
4050b3d5279SMiroslav Benes * Sends a fake signal to all non-kthread tasks with TIF_PATCH_PENDING set.
4060b3d5279SMiroslav Benes * Kthreads with TIF_PATCH_PENDING set are woken up.
4070b3d5279SMiroslav Benes */
klp_send_signals(void)4080b3d5279SMiroslav Benes static void klp_send_signals(void)
4090b3d5279SMiroslav Benes {
4100b3d5279SMiroslav Benes struct task_struct *g, *task;
4110b3d5279SMiroslav Benes
4120b3d5279SMiroslav Benes if (klp_signals_cnt == SIGNALS_TIMEOUT)
4130b3d5279SMiroslav Benes pr_notice("signaling remaining tasks\n");
4140b3d5279SMiroslav Benes
4150b3d5279SMiroslav Benes read_lock(&tasklist_lock);
4160b3d5279SMiroslav Benes for_each_process_thread(g, task) {
4170b3d5279SMiroslav Benes if (!klp_patch_pending(task))
4180b3d5279SMiroslav Benes continue;
4190b3d5279SMiroslav Benes
4200b3d5279SMiroslav Benes /*
4210b3d5279SMiroslav Benes * There is a small race here. We could see TIF_PATCH_PENDING
4220b3d5279SMiroslav Benes * set and decide to wake up a kthread or send a fake signal.
4230b3d5279SMiroslav Benes * Meanwhile the task could migrate itself and the action
4240b3d5279SMiroslav Benes * would be meaningless. It is not serious though.
4250b3d5279SMiroslav Benes */
4260b3d5279SMiroslav Benes if (task->flags & PF_KTHREAD) {
4270b3d5279SMiroslav Benes /*
4280b3d5279SMiroslav Benes * Wake up a kthread which sleeps interruptedly and
4290b3d5279SMiroslav Benes * still has not been migrated.
4300b3d5279SMiroslav Benes */
4310b3d5279SMiroslav Benes wake_up_state(task, TASK_INTERRUPTIBLE);
4320b3d5279SMiroslav Benes } else {
4330b3d5279SMiroslav Benes /*
4340b3d5279SMiroslav Benes * Send fake signal to all non-kthread tasks which are
4350b3d5279SMiroslav Benes * still not migrated.
4360b3d5279SMiroslav Benes */
4378df1947cSMiroslav Benes set_notify_signal(task);
4380b3d5279SMiroslav Benes }
4390b3d5279SMiroslav Benes }
4400b3d5279SMiroslav Benes read_unlock(&tasklist_lock);
4410b3d5279SMiroslav Benes }
4420b3d5279SMiroslav Benes
4430b3d5279SMiroslav Benes /*
444d83a7cb3SJosh Poimboeuf * Try to switch all remaining tasks to the target patch state by walking the
445d83a7cb3SJosh Poimboeuf * stacks of sleeping tasks and looking for any to-be-patched or
446d83a7cb3SJosh Poimboeuf * to-be-unpatched functions. If such functions are found, the task can't be
447d83a7cb3SJosh Poimboeuf * switched yet.
448d83a7cb3SJosh Poimboeuf *
449d83a7cb3SJosh Poimboeuf * If any tasks are still stuck in the initial patch state, schedule a retry.
450d83a7cb3SJosh Poimboeuf */
klp_try_complete_transition(void)451d83a7cb3SJosh Poimboeuf void klp_try_complete_transition(void)
452d83a7cb3SJosh Poimboeuf {
453d83a7cb3SJosh Poimboeuf unsigned int cpu;
454d83a7cb3SJosh Poimboeuf struct task_struct *g, *task;
455958ef1e3SPetr Mladek struct klp_patch *patch;
456d83a7cb3SJosh Poimboeuf bool complete = true;
457d83a7cb3SJosh Poimboeuf
458*d927752fSWardenjohn WARN_ON_ONCE(klp_target_state == KLP_TRANSITION_IDLE);
459d83a7cb3SJosh Poimboeuf
460d83a7cb3SJosh Poimboeuf /*
461d83a7cb3SJosh Poimboeuf * Try to switch the tasks to the target patch state by walking their
462d83a7cb3SJosh Poimboeuf * stacks and looking for any to-be-patched or to-be-unpatched
463d83a7cb3SJosh Poimboeuf * functions. If such functions are found on a stack, or if the stack
464d83a7cb3SJosh Poimboeuf * is deemed unreliable, the task can't be switched yet.
465d83a7cb3SJosh Poimboeuf *
466d83a7cb3SJosh Poimboeuf * Usually this will transition most (or all) of the tasks on a system
467d83a7cb3SJosh Poimboeuf * unless the patch includes changes to a very common function.
468d83a7cb3SJosh Poimboeuf */
469d83a7cb3SJosh Poimboeuf read_lock(&tasklist_lock);
470d83a7cb3SJosh Poimboeuf for_each_process_thread(g, task)
471d83a7cb3SJosh Poimboeuf if (!klp_try_switch_task(task))
472d83a7cb3SJosh Poimboeuf complete = false;
473d83a7cb3SJosh Poimboeuf read_unlock(&tasklist_lock);
474d83a7cb3SJosh Poimboeuf
475d83a7cb3SJosh Poimboeuf /*
476d83a7cb3SJosh Poimboeuf * Ditto for the idle "swapper" tasks.
477d83a7cb3SJosh Poimboeuf */
4781daf08a0SSebastian Andrzej Siewior cpus_read_lock();
479d83a7cb3SJosh Poimboeuf for_each_possible_cpu(cpu) {
480d83a7cb3SJosh Poimboeuf task = idle_task(cpu);
481d83a7cb3SJosh Poimboeuf if (cpu_online(cpu)) {
4825de62ea8SPeter Zijlstra if (!klp_try_switch_task(task)) {
483d83a7cb3SJosh Poimboeuf complete = false;
4845de62ea8SPeter Zijlstra /* Make idle task go through the main loop. */
4855de62ea8SPeter Zijlstra wake_up_if_idle(cpu);
4865de62ea8SPeter Zijlstra }
487d83a7cb3SJosh Poimboeuf } else if (task->patch_state != klp_target_state) {
488d83a7cb3SJosh Poimboeuf /* offline idle tasks can be switched immediately */
489d83a7cb3SJosh Poimboeuf clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
490d83a7cb3SJosh Poimboeuf task->patch_state = klp_target_state;
491d83a7cb3SJosh Poimboeuf }
492d83a7cb3SJosh Poimboeuf }
4931daf08a0SSebastian Andrzej Siewior cpus_read_unlock();
494d83a7cb3SJosh Poimboeuf
495d83a7cb3SJosh Poimboeuf if (!complete) {
496cba82deaSMiroslav Benes if (klp_signals_cnt && !(klp_signals_cnt % SIGNALS_TIMEOUT))
497cba82deaSMiroslav Benes klp_send_signals();
498cba82deaSMiroslav Benes klp_signals_cnt++;
499cba82deaSMiroslav Benes
500d83a7cb3SJosh Poimboeuf /*
501d83a7cb3SJosh Poimboeuf * Some tasks weren't able to be switched over. Try again
502d83a7cb3SJosh Poimboeuf * later and/or wait for other methods like kernel exit
503d83a7cb3SJosh Poimboeuf * switching.
504d83a7cb3SJosh Poimboeuf */
505d83a7cb3SJosh Poimboeuf schedule_delayed_work(&klp_transition_work,
506d83a7cb3SJosh Poimboeuf round_jiffies_relative(HZ));
507d83a7cb3SJosh Poimboeuf return;
508d83a7cb3SJosh Poimboeuf }
509d83a7cb3SJosh Poimboeuf
510e3ff7c60SJosh Poimboeuf /* Done! Now cleanup the data structures. */
511e3ff7c60SJosh Poimboeuf klp_cond_resched_disable();
512958ef1e3SPetr Mladek patch = klp_transition_patch;
513d83a7cb3SJosh Poimboeuf klp_complete_transition();
514958ef1e3SPetr Mladek
515958ef1e3SPetr Mladek /*
5167e35e4ebSPetr Mladek * It would make more sense to free the unused patches in
517958ef1e3SPetr Mladek * klp_complete_transition() but it is called also
518958ef1e3SPetr Mladek * from klp_cancel_transition().
519958ef1e3SPetr Mladek */
5207e35e4ebSPetr Mladek if (!patch->enabled)
5217e35e4ebSPetr Mladek klp_free_patch_async(patch);
5227e35e4ebSPetr Mladek else if (patch->replace)
5237e35e4ebSPetr Mladek klp_free_replaced_patches_async(patch);
524d83a7cb3SJosh Poimboeuf }
525d83a7cb3SJosh Poimboeuf
526d83a7cb3SJosh Poimboeuf /*
527d83a7cb3SJosh Poimboeuf * Start the transition to the specified target patch state so tasks can begin
528d83a7cb3SJosh Poimboeuf * switching to it.
529d83a7cb3SJosh Poimboeuf */
klp_start_transition(void)530d83a7cb3SJosh Poimboeuf void klp_start_transition(void)
531d83a7cb3SJosh Poimboeuf {
532d83a7cb3SJosh Poimboeuf struct task_struct *g, *task;
533d83a7cb3SJosh Poimboeuf unsigned int cpu;
534d83a7cb3SJosh Poimboeuf
535*d927752fSWardenjohn WARN_ON_ONCE(klp_target_state == KLP_TRANSITION_IDLE);
536d83a7cb3SJosh Poimboeuf
537af026796SJoe Lawrence pr_notice("'%s': starting %s transition\n",
538af026796SJoe Lawrence klp_transition_patch->mod->name,
539*d927752fSWardenjohn klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
540d83a7cb3SJosh Poimboeuf
541d83a7cb3SJosh Poimboeuf /*
542d83a7cb3SJosh Poimboeuf * Mark all normal tasks as needing a patch state update. They'll
543d83a7cb3SJosh Poimboeuf * switch either in klp_try_complete_transition() or as they exit the
544d83a7cb3SJosh Poimboeuf * kernel.
545d83a7cb3SJosh Poimboeuf */
546d83a7cb3SJosh Poimboeuf read_lock(&tasklist_lock);
547d83a7cb3SJosh Poimboeuf for_each_process_thread(g, task)
548d83a7cb3SJosh Poimboeuf if (task->patch_state != klp_target_state)
549d83a7cb3SJosh Poimboeuf set_tsk_thread_flag(task, TIF_PATCH_PENDING);
550d83a7cb3SJosh Poimboeuf read_unlock(&tasklist_lock);
551d83a7cb3SJosh Poimboeuf
552d83a7cb3SJosh Poimboeuf /*
553d83a7cb3SJosh Poimboeuf * Mark all idle tasks as needing a patch state update. They'll switch
554d83a7cb3SJosh Poimboeuf * either in klp_try_complete_transition() or at the idle loop switch
555d83a7cb3SJosh Poimboeuf * point.
556d83a7cb3SJosh Poimboeuf */
557d83a7cb3SJosh Poimboeuf for_each_possible_cpu(cpu) {
558d83a7cb3SJosh Poimboeuf task = idle_task(cpu);
559d83a7cb3SJosh Poimboeuf if (task->patch_state != klp_target_state)
560d83a7cb3SJosh Poimboeuf set_tsk_thread_flag(task, TIF_PATCH_PENDING);
561d83a7cb3SJosh Poimboeuf }
562cba82deaSMiroslav Benes
563e3ff7c60SJosh Poimboeuf klp_cond_resched_enable();
564e3ff7c60SJosh Poimboeuf
565cba82deaSMiroslav Benes klp_signals_cnt = 0;
566d83a7cb3SJosh Poimboeuf }
567d83a7cb3SJosh Poimboeuf
568d83a7cb3SJosh Poimboeuf /*
569d83a7cb3SJosh Poimboeuf * Initialize the global target patch state and all tasks to the initial patch
570d83a7cb3SJosh Poimboeuf * state, and initialize all function transition states to true in preparation
571d83a7cb3SJosh Poimboeuf * for patching or unpatching.
572d83a7cb3SJosh Poimboeuf */
klp_init_transition(struct klp_patch * patch,int state)573d83a7cb3SJosh Poimboeuf void klp_init_transition(struct klp_patch *patch, int state)
574d83a7cb3SJosh Poimboeuf {
575d83a7cb3SJosh Poimboeuf struct task_struct *g, *task;
576d83a7cb3SJosh Poimboeuf unsigned int cpu;
577d83a7cb3SJosh Poimboeuf struct klp_object *obj;
578d83a7cb3SJosh Poimboeuf struct klp_func *func;
579d83a7cb3SJosh Poimboeuf int initial_state = !state;
580d83a7cb3SJosh Poimboeuf
581*d927752fSWardenjohn WARN_ON_ONCE(klp_target_state != KLP_TRANSITION_IDLE);
582d83a7cb3SJosh Poimboeuf
583d83a7cb3SJosh Poimboeuf klp_transition_patch = patch;
584d83a7cb3SJosh Poimboeuf
585d83a7cb3SJosh Poimboeuf /*
586d83a7cb3SJosh Poimboeuf * Set the global target patch state which tasks will switch to. This
587d83a7cb3SJosh Poimboeuf * has no effect until the TIF_PATCH_PENDING flags get set later.
588d83a7cb3SJosh Poimboeuf */
589d83a7cb3SJosh Poimboeuf klp_target_state = state;
590d83a7cb3SJosh Poimboeuf
591af026796SJoe Lawrence pr_debug("'%s': initializing %s transition\n", patch->mod->name,
592*d927752fSWardenjohn klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
593af026796SJoe Lawrence
594d83a7cb3SJosh Poimboeuf /*
595d83a7cb3SJosh Poimboeuf * Initialize all tasks to the initial patch state to prepare them for
596d83a7cb3SJosh Poimboeuf * switching to the target state.
597d83a7cb3SJosh Poimboeuf */
598d83a7cb3SJosh Poimboeuf read_lock(&tasklist_lock);
599d83a7cb3SJosh Poimboeuf for_each_process_thread(g, task) {
600*d927752fSWardenjohn WARN_ON_ONCE(task->patch_state != KLP_TRANSITION_IDLE);
601d83a7cb3SJosh Poimboeuf task->patch_state = initial_state;
602d83a7cb3SJosh Poimboeuf }
603d83a7cb3SJosh Poimboeuf read_unlock(&tasklist_lock);
604d83a7cb3SJosh Poimboeuf
605d83a7cb3SJosh Poimboeuf /*
606d83a7cb3SJosh Poimboeuf * Ditto for the idle "swapper" tasks.
607d83a7cb3SJosh Poimboeuf */
608d83a7cb3SJosh Poimboeuf for_each_possible_cpu(cpu) {
609d83a7cb3SJosh Poimboeuf task = idle_task(cpu);
610*d927752fSWardenjohn WARN_ON_ONCE(task->patch_state != KLP_TRANSITION_IDLE);
611d83a7cb3SJosh Poimboeuf task->patch_state = initial_state;
612d83a7cb3SJosh Poimboeuf }
613d83a7cb3SJosh Poimboeuf
614d83a7cb3SJosh Poimboeuf /*
615d83a7cb3SJosh Poimboeuf * Enforce the order of the task->patch_state initializations and the
616d83a7cb3SJosh Poimboeuf * func->transition updates to ensure that klp_ftrace_handler() doesn't
617*d927752fSWardenjohn * see a func in transition with a task->patch_state of KLP_TRANSITION_IDLE.
618d83a7cb3SJosh Poimboeuf *
619d83a7cb3SJosh Poimboeuf * Also enforce the order of the klp_target_state write and future
620e3ff7c60SJosh Poimboeuf * TIF_PATCH_PENDING writes to ensure klp_update_patch_state() and
621e3ff7c60SJosh Poimboeuf * __klp_sched_try_switch() don't set a task->patch_state to
622*d927752fSWardenjohn * KLP_TRANSITION_IDLE.
623d83a7cb3SJosh Poimboeuf */
624d83a7cb3SJosh Poimboeuf smp_wmb();
625d83a7cb3SJosh Poimboeuf
626d83a7cb3SJosh Poimboeuf /*
627d83a7cb3SJosh Poimboeuf * Set the func transition states so klp_ftrace_handler() will know to
628d83a7cb3SJosh Poimboeuf * switch to the transition logic.
629d83a7cb3SJosh Poimboeuf *
630d83a7cb3SJosh Poimboeuf * When patching, the funcs aren't yet in the func_stack and will be
631d83a7cb3SJosh Poimboeuf * made visible to the ftrace handler shortly by the calls to
632d83a7cb3SJosh Poimboeuf * klp_patch_object().
633d83a7cb3SJosh Poimboeuf *
634d83a7cb3SJosh Poimboeuf * When unpatching, the funcs are already in the func_stack and so are
635d83a7cb3SJosh Poimboeuf * already visible to the ftrace handler.
636d83a7cb3SJosh Poimboeuf */
637d83a7cb3SJosh Poimboeuf klp_for_each_object(patch, obj)
638d83a7cb3SJosh Poimboeuf klp_for_each_func(obj, func)
639d83a7cb3SJosh Poimboeuf func->transition = true;
640d83a7cb3SJosh Poimboeuf }
641d83a7cb3SJosh Poimboeuf
642d83a7cb3SJosh Poimboeuf /*
643d83a7cb3SJosh Poimboeuf * This function can be called in the middle of an existing transition to
644d83a7cb3SJosh Poimboeuf * reverse the direction of the target patch state. This can be done to
645d83a7cb3SJosh Poimboeuf * effectively cancel an existing enable or disable operation if there are any
646d83a7cb3SJosh Poimboeuf * tasks which are stuck in the initial patch state.
647d83a7cb3SJosh Poimboeuf */
klp_reverse_transition(void)648d83a7cb3SJosh Poimboeuf void klp_reverse_transition(void)
649d83a7cb3SJosh Poimboeuf {
650d83a7cb3SJosh Poimboeuf unsigned int cpu;
651d83a7cb3SJosh Poimboeuf struct task_struct *g, *task;
652d83a7cb3SJosh Poimboeuf
653af026796SJoe Lawrence pr_debug("'%s': reversing transition from %s\n",
654af026796SJoe Lawrence klp_transition_patch->mod->name,
655*d927752fSWardenjohn klp_target_state == KLP_TRANSITION_PATCHED ? "patching to unpatching" :
656af026796SJoe Lawrence "unpatching to patching");
657af026796SJoe Lawrence
658d83a7cb3SJosh Poimboeuf /*
659d83a7cb3SJosh Poimboeuf * Clear all TIF_PATCH_PENDING flags to prevent races caused by
660e3ff7c60SJosh Poimboeuf * klp_update_patch_state() or __klp_sched_try_switch() running in
661e3ff7c60SJosh Poimboeuf * parallel with the reverse transition.
662d83a7cb3SJosh Poimboeuf */
663d83a7cb3SJosh Poimboeuf read_lock(&tasklist_lock);
664d83a7cb3SJosh Poimboeuf for_each_process_thread(g, task)
665d83a7cb3SJosh Poimboeuf clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
666d83a7cb3SJosh Poimboeuf read_unlock(&tasklist_lock);
667d83a7cb3SJosh Poimboeuf
668d83a7cb3SJosh Poimboeuf for_each_possible_cpu(cpu)
669d83a7cb3SJosh Poimboeuf clear_tsk_thread_flag(idle_task(cpu), TIF_PATCH_PENDING);
670d83a7cb3SJosh Poimboeuf
671e3ff7c60SJosh Poimboeuf /*
672e3ff7c60SJosh Poimboeuf * Make sure all existing invocations of klp_update_patch_state() and
673e3ff7c60SJosh Poimboeuf * __klp_sched_try_switch() see the cleared TIF_PATCH_PENDING before
674e3ff7c60SJosh Poimboeuf * starting the reverse transition.
675e3ff7c60SJosh Poimboeuf */
676842c0884SPetr Mladek klp_synchronize_transition();
677d83a7cb3SJosh Poimboeuf
678e3ff7c60SJosh Poimboeuf /*
679e3ff7c60SJosh Poimboeuf * All patching has stopped, now re-initialize the global variables to
680e3ff7c60SJosh Poimboeuf * prepare for the reverse transition.
681e3ff7c60SJosh Poimboeuf */
682e3ff7c60SJosh Poimboeuf klp_transition_patch->enabled = !klp_transition_patch->enabled;
683e3ff7c60SJosh Poimboeuf klp_target_state = !klp_target_state;
684e3ff7c60SJosh Poimboeuf
685e3ff7c60SJosh Poimboeuf /*
686e3ff7c60SJosh Poimboeuf * Enforce the order of the klp_target_state write and the
687e3ff7c60SJosh Poimboeuf * TIF_PATCH_PENDING writes in klp_start_transition() to ensure
688e3ff7c60SJosh Poimboeuf * klp_update_patch_state() and __klp_sched_try_switch() don't set
689e3ff7c60SJosh Poimboeuf * task->patch_state to the wrong value.
690e3ff7c60SJosh Poimboeuf */
691e3ff7c60SJosh Poimboeuf smp_wmb();
692e3ff7c60SJosh Poimboeuf
693d83a7cb3SJosh Poimboeuf klp_start_transition();
694d83a7cb3SJosh Poimboeuf }
695d83a7cb3SJosh Poimboeuf
696d83a7cb3SJosh Poimboeuf /* Called from copy_process() during fork */
klp_copy_process(struct task_struct * child)697d83a7cb3SJosh Poimboeuf void klp_copy_process(struct task_struct *child)
698d83a7cb3SJosh Poimboeuf {
699d83a7cb3SJosh Poimboeuf
700747f7a29SRik van Riel /*
701747f7a29SRik van Riel * The parent process may have gone through a KLP transition since
702747f7a29SRik van Riel * the thread flag was copied in setup_thread_stack earlier. Bring
703747f7a29SRik van Riel * the task flag up to date with the parent here.
704747f7a29SRik van Riel *
705747f7a29SRik van Riel * The operation is serialized against all klp_*_transition()
706e3ff7c60SJosh Poimboeuf * operations by the tasklist_lock. The only exceptions are
707e3ff7c60SJosh Poimboeuf * klp_update_patch_state(current) and __klp_sched_try_switch(), but we
708e3ff7c60SJosh Poimboeuf * cannot race with them because we are current.
709747f7a29SRik van Riel */
710747f7a29SRik van Riel if (test_tsk_thread_flag(current, TIF_PATCH_PENDING))
711747f7a29SRik van Riel set_tsk_thread_flag(child, TIF_PATCH_PENDING);
712747f7a29SRik van Riel else
713747f7a29SRik van Riel clear_tsk_thread_flag(child, TIF_PATCH_PENDING);
714747f7a29SRik van Riel
715747f7a29SRik van Riel child->patch_state = current->patch_state;
716d83a7cb3SJosh Poimboeuf }
71743347d56SMiroslav Benes
71843347d56SMiroslav Benes /*
719c99a2be7SMiroslav Benes * Drop TIF_PATCH_PENDING of all tasks on admin's request. This forces an
720c99a2be7SMiroslav Benes * existing transition to finish.
721c99a2be7SMiroslav Benes *
722c99a2be7SMiroslav Benes * NOTE: klp_update_patch_state(task) requires the task to be inactive or
723c99a2be7SMiroslav Benes * 'current'. This is not the case here and the consistency model could be
724c99a2be7SMiroslav Benes * broken. Administrator, who is the only one to execute the
725c99a2be7SMiroslav Benes * klp_force_transitions(), has to be aware of this.
726c99a2be7SMiroslav Benes */
klp_force_transition(void)727c99a2be7SMiroslav Benes void klp_force_transition(void)
728c99a2be7SMiroslav Benes {
72968007289SPetr Mladek struct klp_patch *patch;
730c99a2be7SMiroslav Benes struct task_struct *g, *task;
731c99a2be7SMiroslav Benes unsigned int cpu;
732c99a2be7SMiroslav Benes
733c99a2be7SMiroslav Benes pr_warn("forcing remaining tasks to the patched state\n");
734c99a2be7SMiroslav Benes
735c99a2be7SMiroslav Benes read_lock(&tasklist_lock);
736c99a2be7SMiroslav Benes for_each_process_thread(g, task)
737c99a2be7SMiroslav Benes klp_update_patch_state(task);
738c99a2be7SMiroslav Benes read_unlock(&tasklist_lock);
739c99a2be7SMiroslav Benes
740c99a2be7SMiroslav Benes for_each_possible_cpu(cpu)
741c99a2be7SMiroslav Benes klp_update_patch_state(idle_task(cpu));
742c99a2be7SMiroslav Benes
74329573083SChengming Zhou /* Set forced flag for patches being removed. */
744*d927752fSWardenjohn if (klp_target_state == KLP_TRANSITION_UNPATCHED)
74529573083SChengming Zhou klp_transition_patch->forced = true;
74629573083SChengming Zhou else if (klp_transition_patch->replace) {
74729573083SChengming Zhou klp_for_each_patch(patch) {
74829573083SChengming Zhou if (patch != klp_transition_patch)
74968007289SPetr Mladek patch->forced = true;
750c99a2be7SMiroslav Benes }
75129573083SChengming Zhou }
75229573083SChengming Zhou }
753