Blame view

kernel/livepatch/transition.c 17.5 KB
1ccea77e2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
d83a7cb37   Josh Poimboeuf   livepatch: change...
2
3
4
5
  /*
   * transition.c - Kernel Live Patching transition functions
   *
   * Copyright (C) 2015-2016 Josh Poimboeuf <jpoimboe@redhat.com>
d83a7cb37   Josh Poimboeuf   livepatch: change...
6
7
8
9
10
11
   */
  
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  
  #include <linux/cpu.h>
  #include <linux/stacktrace.h>
10517429b   Jiri Kosina   livepatch: make k...
12
  #include "core.h"
d83a7cb37   Josh Poimboeuf   livepatch: change...
13
14
15
16
17
18
  #include "patch.h"
  #include "transition.h"
  #include "../sched/sched.h"
  
  #define MAX_STACK_ENTRIES  100
  #define STACK_ERR_BUF_SIZE 128
cba82dea3   Miroslav Benes   livepatch: Send a...
19
  #define SIGNALS_TIMEOUT 15
d83a7cb37   Josh Poimboeuf   livepatch: change...
20
21
22
  struct klp_patch *klp_transition_patch;
  
  static int klp_target_state = KLP_UNDEFINED;
cba82dea3   Miroslav Benes   livepatch: Send a...
23
  static unsigned int klp_signals_cnt;
d83a7cb37   Josh Poimboeuf   livepatch: change...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  /*
   * This work can be performed periodically to finish patching or unpatching any
   * "straggler" tasks which failed to transition in the first attempt.
   */
  static void klp_transition_work_fn(struct work_struct *work)
  {
  	mutex_lock(&klp_mutex);
  
  	if (klp_transition_patch)
  		klp_try_complete_transition();
  
  	mutex_unlock(&klp_mutex);
  }
  static DECLARE_DELAYED_WORK(klp_transition_work, klp_transition_work_fn);
  
  /*
842c08846   Petr Mladek   livepatch: Fix st...
40
   * This function is just a stub to implement a hard force
6932689e4   Paul E. McKenney   livepatch: Replac...
41
   * of synchronize_rcu(). This requires synchronizing
842c08846   Petr Mladek   livepatch: Fix st...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
   * tasks even in userspace and idle.
   */
  static void klp_sync(struct work_struct *work)
  {
  }
  
  /*
   * We allow to patch also functions where RCU is not watching,
   * e.g. before user_exit(). We can not rely on the RCU infrastructure
   * to do the synchronization. Instead hard force the sched synchronization.
   *
   * This approach allows to use RCU functions for manipulating func_stack
   * safely.
   */
  static void klp_synchronize_transition(void)
  {
  	schedule_on_each_cpu(klp_sync);
  }
  
  /*
d83a7cb37   Josh Poimboeuf   livepatch: change...
62
63
64
65
66
67
68
69
70
   * The transition to the target patch state is complete.  Clean up the data
   * structures.
   */
  static void klp_complete_transition(void)
  {
  	struct klp_object *obj;
  	struct klp_func *func;
  	struct task_struct *g, *task;
  	unsigned int cpu;
af0267960   Joe Lawrence   livepatch: add tr...
71
72
73
74
  	pr_debug("'%s': completing %s transition
  ",
  		 klp_transition_patch->mod->name,
  		 klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
d697bad58   Petr Mladek   livepatch: Remove...
75
  	if (klp_transition_patch->replace && klp_target_state == KLP_PATCHED) {
7e35e4eb7   Petr Mladek   livepatch: Keep r...
76
  		klp_unpatch_replaced_patches(klp_transition_patch);
d697bad58   Petr Mladek   livepatch: Remove...
77
78
  		klp_discard_nops(klp_transition_patch);
  	}
e1452b607   Jason Baron   livepatch: Add at...
79

d83a7cb37   Josh Poimboeuf   livepatch: change...
80
81
82
83
84
85
86
87
88
89
90
91
92
  	if (klp_target_state == KLP_UNPATCHED) {
  		/*
  		 * All tasks have transitioned to KLP_UNPATCHED so we can now
  		 * remove the new functions from the func_stack.
  		 */
  		klp_unpatch_objects(klp_transition_patch);
  
  		/*
  		 * Make sure klp_ftrace_handler() can no longer see functions
  		 * from this patch on the ops->func_stack.  Otherwise, after
  		 * func->transition gets cleared, the handler may choose a
  		 * removed function.
  		 */
842c08846   Petr Mladek   livepatch: Fix st...
93
  		klp_synchronize_transition();
d83a7cb37   Josh Poimboeuf   livepatch: change...
94
  	}
d0807da78   Miroslav Benes   livepatch: Remove...
95
96
  	klp_for_each_object(klp_transition_patch, obj)
  		klp_for_each_func(obj, func)
d83a7cb37   Josh Poimboeuf   livepatch: change...
97
  			func->transition = false;
3ec24776b   Josh Poimboeuf   livepatch: allow ...
98

d83a7cb37   Josh Poimboeuf   livepatch: change...
99
100
  	/* Prevent klp_ftrace_handler() from seeing KLP_UNDEFINED state */
  	if (klp_target_state == KLP_PATCHED)
842c08846   Petr Mladek   livepatch: Fix st...
101
  		klp_synchronize_transition();
d83a7cb37   Josh Poimboeuf   livepatch: change...
102
103
104
105
106
107
108
109
110
111
112
113
114
  
  	read_lock(&tasklist_lock);
  	for_each_process_thread(g, task) {
  		WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING));
  		task->patch_state = KLP_UNDEFINED;
  	}
  	read_unlock(&tasklist_lock);
  
  	for_each_possible_cpu(cpu) {
  		task = idle_task(cpu);
  		WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING));
  		task->patch_state = KLP_UNDEFINED;
  	}
93862e385   Joe Lawrence   livepatch: add (u...
115
116
117
118
119
120
121
122
  	klp_for_each_object(klp_transition_patch, obj) {
  		if (!klp_is_object_loaded(obj))
  			continue;
  		if (klp_target_state == KLP_PATCHED)
  			klp_post_patch_callback(obj);
  		else if (klp_target_state == KLP_UNPATCHED)
  			klp_post_unpatch_callback(obj);
  	}
6116c3033   Joe Lawrence   livepatch: move t...
123
124
125
  	pr_notice("'%s': %s complete
  ", klp_transition_patch->mod->name,
  		  klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
d83a7cb37   Josh Poimboeuf   livepatch: change...
126
127
128
129
130
131
132
133
134
135
136
137
  	klp_target_state = KLP_UNDEFINED;
  	klp_transition_patch = NULL;
  }
  
  /*
   * This is called in the error path, to cancel a transition before it has
   * started, i.e. klp_init_transition() has been called but
   * klp_start_transition() hasn't.  If the transition *has* been started,
   * klp_reverse_transition() should be used instead.
   */
  void klp_cancel_transition(void)
  {
3ec24776b   Josh Poimboeuf   livepatch: allow ...
138
139
  	if (WARN_ON_ONCE(klp_target_state != KLP_PATCHED))
  		return;
af0267960   Joe Lawrence   livepatch: add tr...
140
141
142
  	pr_debug("'%s': canceling patching transition, going to unpatch
  ",
  		 klp_transition_patch->mod->name);
3ec24776b   Josh Poimboeuf   livepatch: allow ...
143
  	klp_target_state = KLP_UNPATCHED;
d83a7cb37   Josh Poimboeuf   livepatch: change...
144
145
146
147
148
149
150
151
152
153
154
155
  	klp_complete_transition();
  }
  
  /*
   * Switch the patched state of the task to the set of functions in the target
   * patch state.
   *
   * NOTE: If task is not 'current', the caller must ensure the task is inactive.
   * Otherwise klp_ftrace_handler() might read the wrong 'patch_state' value.
   */
  void klp_update_patch_state(struct task_struct *task)
  {
842c08846   Petr Mladek   livepatch: Fix st...
156
  	/*
6932689e4   Paul E. McKenney   livepatch: Replac...
157
  	 * A variant of synchronize_rcu() is used to allow patching functions
842c08846   Petr Mladek   livepatch: Fix st...
158
159
160
  	 * where RCU is not watching, see klp_synchronize_transition().
  	 */
  	preempt_disable_notrace();
d83a7cb37   Josh Poimboeuf   livepatch: change...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  
  	/*
  	 * This test_and_clear_tsk_thread_flag() call also serves as a read
  	 * barrier (smp_rmb) for two cases:
  	 *
  	 * 1) Enforce the order of the TIF_PATCH_PENDING read and the
  	 *    klp_target_state read.  The corresponding write barrier is in
  	 *    klp_init_transition().
  	 *
  	 * 2) Enforce the order of the TIF_PATCH_PENDING read and a future read
  	 *    of func->transition, if klp_ftrace_handler() is called later on
  	 *    the same CPU.  See __klp_disable_patch().
  	 */
  	if (test_and_clear_tsk_thread_flag(task, TIF_PATCH_PENDING))
  		task->patch_state = READ_ONCE(klp_target_state);
842c08846   Petr Mladek   livepatch: Fix st...
176
  	preempt_enable_notrace();
d83a7cb37   Josh Poimboeuf   livepatch: change...
177
178
179
180
181
182
  }
  
  /*
   * Determine whether the given stack trace includes any references to a
   * to-be-patched or to-be-unpatched function.
   */
25e39e32b   Thomas Gleixner   livepatch: Simpli...
183
184
  static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
  				unsigned int nr_entries)
d83a7cb37   Josh Poimboeuf   livepatch: change...
185
186
187
188
  {
  	unsigned long func_addr, func_size, address;
  	struct klp_ops *ops;
  	int i;
25e39e32b   Thomas Gleixner   livepatch: Simpli...
189
190
  	for (i = 0; i < nr_entries; i++) {
  		address = entries[i];
d83a7cb37   Josh Poimboeuf   livepatch: change...
191
192
193
194
195
196
197
198
199
200
201
202
203
  
  		if (klp_target_state == KLP_UNPATCHED) {
  			 /*
  			  * Check for the to-be-unpatched function
  			  * (the func itself).
  			  */
  			func_addr = (unsigned long)func->new_func;
  			func_size = func->new_size;
  		} else {
  			/*
  			 * Check for the to-be-patched function
  			 * (the previous func).
  			 */
19514910d   Petr Mladek   livepatch: Change...
204
  			ops = klp_find_ops(func->old_func);
d83a7cb37   Josh Poimboeuf   livepatch: change...
205
206
207
  
  			if (list_is_singular(&ops->func_stack)) {
  				/* original function */
19514910d   Petr Mladek   livepatch: Change...
208
  				func_addr = (unsigned long)func->old_func;
d83a7cb37   Josh Poimboeuf   livepatch: change...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  				func_size = func->old_size;
  			} else {
  				/* previously patched function */
  				struct klp_func *prev;
  
  				prev = list_next_entry(func, stack_node);
  				func_addr = (unsigned long)prev->new_func;
  				func_size = prev->new_size;
  			}
  		}
  
  		if (address >= func_addr && address < func_addr + func_size)
  			return -EAGAIN;
  	}
  
  	return 0;
  }
  
  /*
   * Determine whether it's safe to transition the task to the target patch state
   * by looking for any to-be-patched or to-be-unpatched functions on its stack.
   */
  static int klp_check_stack(struct task_struct *task, char *err_buf)
  {
  	static unsigned long entries[MAX_STACK_ENTRIES];
d83a7cb37   Josh Poimboeuf   livepatch: change...
234
235
  	struct klp_object *obj;
  	struct klp_func *func;
25e39e32b   Thomas Gleixner   livepatch: Simpli...
236
  	int ret, nr_entries;
d83a7cb37   Josh Poimboeuf   livepatch: change...
237

25e39e32b   Thomas Gleixner   livepatch: Simpli...
238
  	ret = stack_trace_save_tsk_reliable(task, entries, ARRAY_SIZE(entries));
25e39e32b   Thomas Gleixner   livepatch: Simpli...
239
  	if (ret < 0) {
d83a7cb37   Josh Poimboeuf   livepatch: change...
240
241
242
243
244
245
  		snprintf(err_buf, STACK_ERR_BUF_SIZE,
  			 "%s: %s:%d has an unreliable stack
  ",
  			 __func__, task->comm, task->pid);
  		return ret;
  	}
25e39e32b   Thomas Gleixner   livepatch: Simpli...
246
  	nr_entries = ret;
d83a7cb37   Josh Poimboeuf   livepatch: change...
247
248
249
250
251
  
  	klp_for_each_object(klp_transition_patch, obj) {
  		if (!obj->patched)
  			continue;
  		klp_for_each_func(obj, func) {
25e39e32b   Thomas Gleixner   livepatch: Simpli...
252
  			ret = klp_check_stack_func(func, entries, nr_entries);
d83a7cb37   Josh Poimboeuf   livepatch: change...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
  			if (ret) {
  				snprintf(err_buf, STACK_ERR_BUF_SIZE,
  					 "%s: %s:%d is sleeping on function %s
  ",
  					 __func__, task->comm, task->pid,
  					 func->old_name);
  				return ret;
  			}
  		}
  	}
  
  	return 0;
  }
  
  /*
   * Try to safely switch a task to the target patch state.  If it's currently
   * running, or it's sleeping on a to-be-patched or to-be-unpatched function, or
   * if the stack is unreliable, return false.
   */
  static bool klp_try_switch_task(struct task_struct *task)
  {
f36e66451   Petr Mladek   livepatch: Use st...
274
  	static char err_buf[STACK_ERR_BUF_SIZE];
d83a7cb37   Josh Poimboeuf   livepatch: change...
275
276
277
278
  	struct rq *rq;
  	struct rq_flags flags;
  	int ret;
  	bool success = false;
d83a7cb37   Josh Poimboeuf   livepatch: change...
279
280
281
282
283
284
285
286
  
  	err_buf[0] = '\0';
  
  	/* check if this task has already switched over */
  	if (task->patch_state == klp_target_state)
  		return true;
  
  	/*
67059d65f   Miroslav Benes   Revert "livepatch...
287
288
289
290
291
292
293
  	 * For arches which don't have reliable stack traces, we have to rely
  	 * on other methods (e.g., switching tasks at kernel exit).
  	 */
  	if (!klp_have_reliable_stack())
  		return false;
  
  	/*
d83a7cb37   Josh Poimboeuf   livepatch: change...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  	 * Now try to check the stack for any to-be-patched or to-be-unpatched
  	 * functions.  If all goes well, switch the task to the target patch
  	 * state.
  	 */
  	rq = task_rq_lock(task, &flags);
  
  	if (task_running(rq, task) && task != current) {
  		snprintf(err_buf, STACK_ERR_BUF_SIZE,
  			 "%s: %s:%d is running
  ", __func__, task->comm,
  			 task->pid);
  		goto done;
  	}
  
  	ret = klp_check_stack(task, err_buf);
  	if (ret)
  		goto done;
  
  	success = true;
  
  	clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
  	task->patch_state = klp_target_state;
  
  done:
  	task_rq_unlock(rq, task, &flags);
  
  	/*
  	 * Due to console deadlock issues, pr_debug() can't be used while
  	 * holding the task rq lock.  Instead we have to use a temporary buffer
  	 * and print the debug message after releasing the lock.
  	 */
  	if (err_buf[0] != '\0')
  		pr_debug("%s", err_buf);
  
  	return success;
d83a7cb37   Josh Poimboeuf   livepatch: change...
329
330
331
  }
  
  /*
0b3d52790   Miroslav Benes   livepatch: Remove...
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
   * Sends a fake signal to all non-kthread tasks with TIF_PATCH_PENDING set.
   * Kthreads with TIF_PATCH_PENDING set are woken up.
   */
  static void klp_send_signals(void)
  {
  	struct task_struct *g, *task;
  
  	if (klp_signals_cnt == SIGNALS_TIMEOUT)
  		pr_notice("signaling remaining tasks
  ");
  
  	read_lock(&tasklist_lock);
  	for_each_process_thread(g, task) {
  		if (!klp_patch_pending(task))
  			continue;
  
  		/*
  		 * There is a small race here. We could see TIF_PATCH_PENDING
  		 * set and decide to wake up a kthread or send a fake signal.
  		 * Meanwhile the task could migrate itself and the action
  		 * would be meaningless. It is not serious though.
  		 */
  		if (task->flags & PF_KTHREAD) {
  			/*
  			 * Wake up a kthread which sleeps interruptedly and
  			 * still has not been migrated.
  			 */
  			wake_up_state(task, TASK_INTERRUPTIBLE);
  		} else {
  			/*
  			 * Send fake signal to all non-kthread tasks which are
  			 * still not migrated.
  			 */
  			spin_lock_irq(&task->sighand->siglock);
  			signal_wake_up(task, 0);
  			spin_unlock_irq(&task->sighand->siglock);
  		}
  	}
  	read_unlock(&tasklist_lock);
  }
  
  /*
d83a7cb37   Josh Poimboeuf   livepatch: change...
374
375
376
377
378
379
380
381
382
383
384
   * Try to switch all remaining tasks to the target patch state by walking the
   * stacks of sleeping tasks and looking for any to-be-patched or
   * to-be-unpatched functions.  If such functions are found, the task can't be
   * switched yet.
   *
   * If any tasks are still stuck in the initial patch state, schedule a retry.
   */
  void klp_try_complete_transition(void)
  {
  	unsigned int cpu;
  	struct task_struct *g, *task;
958ef1e39   Petr Mladek   livepatch: Simpli...
385
  	struct klp_patch *patch;
d83a7cb37   Josh Poimboeuf   livepatch: change...
386
387
388
389
390
  	bool complete = true;
  
  	WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED);
  
  	/*
d83a7cb37   Josh Poimboeuf   livepatch: change...
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  	 * Try to switch the tasks to the target patch state by walking their
  	 * stacks and looking for any to-be-patched or to-be-unpatched
  	 * functions.  If such functions are found on a stack, or if the stack
  	 * is deemed unreliable, the task can't be switched yet.
  	 *
  	 * Usually this will transition most (or all) of the tasks on a system
  	 * unless the patch includes changes to a very common function.
  	 */
  	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.
  	 */
  	get_online_cpus();
  	for_each_possible_cpu(cpu) {
  		task = idle_task(cpu);
  		if (cpu_online(cpu)) {
  			if (!klp_try_switch_task(task))
  				complete = false;
  		} else if (task->patch_state != klp_target_state) {
  			/* offline idle tasks can be switched immediately */
  			clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
  			task->patch_state = klp_target_state;
  		}
  	}
  	put_online_cpus();
  
  	if (!complete) {
cba82dea3   Miroslav Benes   livepatch: Send a...
423
424
425
  		if (klp_signals_cnt && !(klp_signals_cnt % SIGNALS_TIMEOUT))
  			klp_send_signals();
  		klp_signals_cnt++;
d83a7cb37   Josh Poimboeuf   livepatch: change...
426
427
428
429
430
431
432
433
434
  		/*
  		 * Some tasks weren't able to be switched over.  Try again
  		 * later and/or wait for other methods like kernel exit
  		 * switching.
  		 */
  		schedule_delayed_work(&klp_transition_work,
  				      round_jiffies_relative(HZ));
  		return;
  	}
d83a7cb37   Josh Poimboeuf   livepatch: change...
435
  	/* we're done, now cleanup the data structures */
958ef1e39   Petr Mladek   livepatch: Simpli...
436
  	patch = klp_transition_patch;
d83a7cb37   Josh Poimboeuf   livepatch: change...
437
  	klp_complete_transition();
958ef1e39   Petr Mladek   livepatch: Simpli...
438
439
  
  	/*
7e35e4eb7   Petr Mladek   livepatch: Keep r...
440
  	 * It would make more sense to free the unused patches in
958ef1e39   Petr Mladek   livepatch: Simpli...
441
442
443
  	 * klp_complete_transition() but it is called also
  	 * from klp_cancel_transition().
  	 */
7e35e4eb7   Petr Mladek   livepatch: Keep r...
444
445
446
447
  	if (!patch->enabled)
  		klp_free_patch_async(patch);
  	else if (patch->replace)
  		klp_free_replaced_patches_async(patch);
d83a7cb37   Josh Poimboeuf   livepatch: change...
448
449
450
451
452
453
454
455
456
457
458
459
  }
  
  /*
   * Start the transition to the specified target patch state so tasks can begin
   * switching to it.
   */
  void klp_start_transition(void)
  {
  	struct task_struct *g, *task;
  	unsigned int cpu;
  
  	WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED);
af0267960   Joe Lawrence   livepatch: add tr...
460
461
462
  	pr_notice("'%s': starting %s transition
  ",
  		  klp_transition_patch->mod->name,
d83a7cb37   Josh Poimboeuf   livepatch: change...
463
464
465
  		  klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
  
  	/*
d83a7cb37   Josh Poimboeuf   livepatch: change...
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
  	 * Mark all normal tasks as needing a patch state update.  They'll
  	 * switch either in klp_try_complete_transition() or as they exit the
  	 * kernel.
  	 */
  	read_lock(&tasklist_lock);
  	for_each_process_thread(g, task)
  		if (task->patch_state != klp_target_state)
  			set_tsk_thread_flag(task, TIF_PATCH_PENDING);
  	read_unlock(&tasklist_lock);
  
  	/*
  	 * Mark all idle tasks as needing a patch state update.  They'll switch
  	 * either in klp_try_complete_transition() or at the idle loop switch
  	 * point.
  	 */
  	for_each_possible_cpu(cpu) {
  		task = idle_task(cpu);
  		if (task->patch_state != klp_target_state)
  			set_tsk_thread_flag(task, TIF_PATCH_PENDING);
  	}
cba82dea3   Miroslav Benes   livepatch: Send a...
486
487
  
  	klp_signals_cnt = 0;
d83a7cb37   Josh Poimboeuf   livepatch: change...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  }
  
  /*
   * Initialize the global target patch state and all tasks to the initial patch
   * state, and initialize all function transition states to true in preparation
   * for patching or unpatching.
   */
  void klp_init_transition(struct klp_patch *patch, int state)
  {
  	struct task_struct *g, *task;
  	unsigned int cpu;
  	struct klp_object *obj;
  	struct klp_func *func;
  	int initial_state = !state;
  
  	WARN_ON_ONCE(klp_target_state != KLP_UNDEFINED);
  
  	klp_transition_patch = patch;
  
  	/*
  	 * Set the global target patch state which tasks will switch to.  This
  	 * has no effect until the TIF_PATCH_PENDING flags get set later.
  	 */
  	klp_target_state = state;
af0267960   Joe Lawrence   livepatch: add tr...
512
513
514
  	pr_debug("'%s': initializing %s transition
  ", patch->mod->name,
  		 klp_target_state == KLP_PATCHED ? "patching" : "unpatching");
d83a7cb37   Josh Poimboeuf   livepatch: change...
515
  	/*
d83a7cb37   Josh Poimboeuf   livepatch: change...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
  	 * Initialize all tasks to the initial patch state to prepare them for
  	 * switching to the target state.
  	 */
  	read_lock(&tasklist_lock);
  	for_each_process_thread(g, task) {
  		WARN_ON_ONCE(task->patch_state != KLP_UNDEFINED);
  		task->patch_state = initial_state;
  	}
  	read_unlock(&tasklist_lock);
  
  	/*
  	 * Ditto for the idle "swapper" tasks.
  	 */
  	for_each_possible_cpu(cpu) {
  		task = idle_task(cpu);
  		WARN_ON_ONCE(task->patch_state != KLP_UNDEFINED);
  		task->patch_state = initial_state;
  	}
  
  	/*
  	 * Enforce the order of the task->patch_state initializations and the
  	 * func->transition updates to ensure that klp_ftrace_handler() doesn't
  	 * see a func in transition with a task->patch_state of KLP_UNDEFINED.
  	 *
  	 * Also enforce the order of the klp_target_state write and future
  	 * TIF_PATCH_PENDING writes to ensure klp_update_patch_state() doesn't
  	 * set a task->patch_state to KLP_UNDEFINED.
  	 */
  	smp_wmb();
  
  	/*
  	 * Set the func transition states so klp_ftrace_handler() will know to
  	 * switch to the transition logic.
  	 *
  	 * When patching, the funcs aren't yet in the func_stack and will be
  	 * made visible to the ftrace handler shortly by the calls to
  	 * klp_patch_object().
  	 *
  	 * When unpatching, the funcs are already in the func_stack and so are
  	 * already visible to the ftrace handler.
  	 */
  	klp_for_each_object(patch, obj)
  		klp_for_each_func(obj, func)
  			func->transition = true;
  }
  
  /*
   * This function can be called in the middle of an existing transition to
   * reverse the direction of the target patch state.  This can be done to
   * effectively cancel an existing enable or disable operation if there are any
   * tasks which are stuck in the initial patch state.
   */
  void klp_reverse_transition(void)
  {
  	unsigned int cpu;
  	struct task_struct *g, *task;
af0267960   Joe Lawrence   livepatch: add tr...
572
573
574
575
576
  	pr_debug("'%s': reversing transition from %s
  ",
  		 klp_transition_patch->mod->name,
  		 klp_target_state == KLP_PATCHED ? "patching to unpatching" :
  						   "unpatching to patching");
d83a7cb37   Josh Poimboeuf   livepatch: change...
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  	klp_transition_patch->enabled = !klp_transition_patch->enabled;
  
  	klp_target_state = !klp_target_state;
  
  	/*
  	 * Clear all TIF_PATCH_PENDING flags to prevent races caused by
  	 * klp_update_patch_state() running in parallel with
  	 * klp_start_transition().
  	 */
  	read_lock(&tasklist_lock);
  	for_each_process_thread(g, task)
  		clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
  	read_unlock(&tasklist_lock);
  
  	for_each_possible_cpu(cpu)
  		clear_tsk_thread_flag(idle_task(cpu), TIF_PATCH_PENDING);
  
  	/* Let any remaining calls to klp_update_patch_state() complete */
842c08846   Petr Mladek   livepatch: Fix st...
595
  	klp_synchronize_transition();
d83a7cb37   Josh Poimboeuf   livepatch: change...
596
597
598
599
600
601
602
603
604
605
606
  
  	klp_start_transition();
  }
  
  /* Called from copy_process() during fork */
  void klp_copy_process(struct task_struct *child)
  {
  	child->patch_state = current->patch_state;
  
  	/* TIF_PATCH_PENDING gets copied in setup_thread_stack() */
  }
43347d56c   Miroslav Benes   livepatch: send a...
607
608
  
  /*
c99a2be79   Miroslav Benes   livepatch: force ...
609
610
611
612
613
614
615
616
617
618
   * Drop TIF_PATCH_PENDING of all tasks on admin's request. This forces an
   * existing transition to finish.
   *
   * NOTE: klp_update_patch_state(task) requires the task to be inactive or
   * 'current'. This is not the case here and the consistency model could be
   * broken. Administrator, who is the only one to execute the
   * klp_force_transitions(), has to be aware of this.
   */
  void klp_force_transition(void)
  {
68007289b   Petr Mladek   livepatch: Don't ...
619
  	struct klp_patch *patch;
c99a2be79   Miroslav Benes   livepatch: force ...
620
621
622
623
624
625
626
627
628
629
630
631
632
  	struct task_struct *g, *task;
  	unsigned int cpu;
  
  	pr_warn("forcing remaining tasks to the patched state
  ");
  
  	read_lock(&tasklist_lock);
  	for_each_process_thread(g, task)
  		klp_update_patch_state(task);
  	read_unlock(&tasklist_lock);
  
  	for_each_possible_cpu(cpu)
  		klp_update_patch_state(idle_task(cpu));
ecba29f43   Petr Mladek   livepatch: Introd...
633
  	klp_for_each_patch(patch)
68007289b   Petr Mladek   livepatch: Don't ...
634
  		patch->forced = true;
c99a2be79   Miroslav Benes   livepatch: force ...
635
  }