transition.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * transition.c - Kernel Live Patching transition functions
  4. *
  5. * Copyright (C) 2015-2016 Josh Poimboeuf <jpoimboe@redhat.com>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/cpu.h>
  9. #include <linux/stacktrace.h>
  10. #include <linux/static_call.h>
  11. #include "core.h"
  12. #include "patch.h"
  13. #include "transition.h"
  14. #define MAX_STACK_ENTRIES 100
  15. static DEFINE_PER_CPU(unsigned long[MAX_STACK_ENTRIES], klp_stack_entries);
  16. #define STACK_ERR_BUF_SIZE 128
  17. #define SIGNALS_TIMEOUT 15
  18. struct klp_patch *klp_transition_patch;
  19. static int klp_target_state = KLP_TRANSITION_IDLE;
  20. static unsigned int klp_signals_cnt;
  21. /*
  22. * When a livepatch is in progress, enable klp stack checking in
  23. * cond_resched(). This helps CPU-bound kthreads get patched.
  24. */
  25. #if defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
  26. #define klp_cond_resched_enable() sched_dynamic_klp_enable()
  27. #define klp_cond_resched_disable() sched_dynamic_klp_disable()
  28. #else /* !CONFIG_PREEMPT_DYNAMIC || !CONFIG_HAVE_PREEMPT_DYNAMIC_CALL */
  29. DEFINE_STATIC_KEY_FALSE(klp_sched_try_switch_key);
  30. EXPORT_SYMBOL(klp_sched_try_switch_key);
  31. #define klp_cond_resched_enable() static_branch_enable(&klp_sched_try_switch_key)
  32. #define klp_cond_resched_disable() static_branch_disable(&klp_sched_try_switch_key)
  33. #endif /* CONFIG_PREEMPT_DYNAMIC && CONFIG_HAVE_PREEMPT_DYNAMIC_CALL */
  34. /*
  35. * This work can be performed periodically to finish patching or unpatching any
  36. * "straggler" tasks which failed to transition in the first attempt.
  37. */
  38. static void klp_transition_work_fn(struct work_struct *work)
  39. {
  40. mutex_lock(&klp_mutex);
  41. if (klp_transition_patch)
  42. klp_try_complete_transition();
  43. mutex_unlock(&klp_mutex);
  44. }
  45. static DECLARE_DELAYED_WORK(klp_transition_work, klp_transition_work_fn);
  46. /*
  47. * This function is just a stub to implement a hard force
  48. * of synchronize_rcu(). This requires synchronizing
  49. * tasks even in userspace and idle.
  50. */
  51. static void klp_sync(struct work_struct *work)
  52. {
  53. }
  54. /*
  55. * We allow to patch also functions where RCU is not watching,
  56. * e.g. before user_exit(). We can not rely on the RCU infrastructure
  57. * to do the synchronization. Instead hard force the sched synchronization.
  58. *
  59. * This approach allows to use RCU functions for manipulating func_stack
  60. * safely.
  61. */
  62. static void klp_synchronize_transition(void)
  63. {
  64. schedule_on_each_cpu(klp_sync);
  65. }
  66. /*
  67. * The transition to the target patch state is complete. Clean up the data
  68. * structures.
  69. */
  70. static void klp_complete_transition(void)
  71. {
  72. struct klp_object *obj;
  73. struct klp_func *func;
  74. struct task_struct *g, *task;
  75. unsigned int cpu;
  76. pr_debug("'%s': completing %s transition\n",
  77. klp_transition_patch->mod->name,
  78. klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
  79. if (klp_transition_patch->replace && klp_target_state == KLP_TRANSITION_PATCHED) {
  80. klp_unpatch_replaced_patches(klp_transition_patch);
  81. klp_discard_nops(klp_transition_patch);
  82. }
  83. if (klp_target_state == KLP_TRANSITION_UNPATCHED) {
  84. /*
  85. * All tasks have transitioned to KLP_TRANSITION_UNPATCHED so we can now
  86. * remove the new functions from the func_stack.
  87. */
  88. klp_unpatch_objects(klp_transition_patch);
  89. /*
  90. * Make sure klp_ftrace_handler() can no longer see functions
  91. * from this patch on the ops->func_stack. Otherwise, after
  92. * func->transition gets cleared, the handler may choose a
  93. * removed function.
  94. */
  95. klp_synchronize_transition();
  96. }
  97. klp_for_each_object(klp_transition_patch, obj)
  98. klp_for_each_func(obj, func)
  99. func->transition = false;
  100. /* Prevent klp_ftrace_handler() from seeing KLP_TRANSITION_IDLE state */
  101. if (klp_target_state == KLP_TRANSITION_PATCHED)
  102. klp_synchronize_transition();
  103. read_lock(&tasklist_lock);
  104. for_each_process_thread(g, task) {
  105. WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING));
  106. task->patch_state = KLP_TRANSITION_IDLE;
  107. }
  108. read_unlock(&tasklist_lock);
  109. for_each_possible_cpu(cpu) {
  110. task = idle_task(cpu);
  111. WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING));
  112. task->patch_state = KLP_TRANSITION_IDLE;
  113. }
  114. klp_for_each_object(klp_transition_patch, obj) {
  115. if (!klp_is_object_loaded(obj))
  116. continue;
  117. if (klp_target_state == KLP_TRANSITION_PATCHED)
  118. klp_post_patch_callback(obj);
  119. else if (klp_target_state == KLP_TRANSITION_UNPATCHED)
  120. klp_post_unpatch_callback(obj);
  121. }
  122. pr_notice("'%s': %s complete\n", klp_transition_patch->mod->name,
  123. klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
  124. klp_target_state = KLP_TRANSITION_IDLE;
  125. klp_transition_patch = NULL;
  126. }
  127. /*
  128. * This is called in the error path, to cancel a transition before it has
  129. * started, i.e. klp_init_transition() has been called but
  130. * klp_start_transition() hasn't. If the transition *has* been started,
  131. * klp_reverse_transition() should be used instead.
  132. */
  133. void klp_cancel_transition(void)
  134. {
  135. if (WARN_ON_ONCE(klp_target_state != KLP_TRANSITION_PATCHED))
  136. return;
  137. pr_debug("'%s': canceling patching transition, going to unpatch\n",
  138. klp_transition_patch->mod->name);
  139. klp_target_state = KLP_TRANSITION_UNPATCHED;
  140. klp_complete_transition();
  141. }
  142. /*
  143. * Switch the patched state of the task to the set of functions in the target
  144. * patch state.
  145. *
  146. * NOTE: If task is not 'current', the caller must ensure the task is inactive.
  147. * Otherwise klp_ftrace_handler() might read the wrong 'patch_state' value.
  148. */
  149. void klp_update_patch_state(struct task_struct *task)
  150. {
  151. /*
  152. * A variant of synchronize_rcu() is used to allow patching functions
  153. * where RCU is not watching, see klp_synchronize_transition().
  154. */
  155. preempt_disable_notrace();
  156. /*
  157. * This test_and_clear_tsk_thread_flag() call also serves as a read
  158. * barrier (smp_rmb) for two cases:
  159. *
  160. * 1) Enforce the order of the TIF_PATCH_PENDING read and the
  161. * klp_target_state read. The corresponding write barriers are in
  162. * klp_init_transition() and klp_reverse_transition().
  163. *
  164. * 2) Enforce the order of the TIF_PATCH_PENDING read and a future read
  165. * of func->transition, if klp_ftrace_handler() is called later on
  166. * the same CPU. See __klp_disable_patch().
  167. */
  168. if (test_and_clear_tsk_thread_flag(task, TIF_PATCH_PENDING))
  169. task->patch_state = READ_ONCE(klp_target_state);
  170. preempt_enable_notrace();
  171. }
  172. /*
  173. * Determine whether the given stack trace includes any references to a
  174. * to-be-patched or to-be-unpatched function.
  175. */
  176. static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
  177. unsigned int nr_entries)
  178. {
  179. unsigned long func_addr, func_size, address;
  180. struct klp_ops *ops;
  181. int i;
  182. if (klp_target_state == KLP_TRANSITION_UNPATCHED) {
  183. /*
  184. * Check for the to-be-unpatched function
  185. * (the func itself).
  186. */
  187. func_addr = (unsigned long)func->new_func;
  188. func_size = func->new_size;
  189. } else {
  190. /*
  191. * Check for the to-be-patched function
  192. * (the previous func).
  193. */
  194. ops = klp_find_ops(func->old_func);
  195. if (list_is_singular(&ops->func_stack)) {
  196. /* original function */
  197. func_addr = (unsigned long)func->old_func;
  198. func_size = func->old_size;
  199. } else {
  200. /* previously patched function */
  201. struct klp_func *prev;
  202. prev = list_next_entry(func, stack_node);
  203. func_addr = (unsigned long)prev->new_func;
  204. func_size = prev->new_size;
  205. }
  206. }
  207. for (i = 0; i < nr_entries; i++) {
  208. address = entries[i];
  209. if (address >= func_addr && address < func_addr + func_size)
  210. return -EAGAIN;
  211. }
  212. return 0;
  213. }
  214. /*
  215. * Determine whether it's safe to transition the task to the target patch state
  216. * by looking for any to-be-patched or to-be-unpatched functions on its stack.
  217. */
  218. static int klp_check_stack(struct task_struct *task, const char **oldname)
  219. {
  220. unsigned long *entries = this_cpu_ptr(klp_stack_entries);
  221. struct klp_object *obj;
  222. struct klp_func *func;
  223. int ret, nr_entries;
  224. /* Protect 'klp_stack_entries' */
  225. lockdep_assert_preemption_disabled();
  226. ret = stack_trace_save_tsk_reliable(task, entries, MAX_STACK_ENTRIES);
  227. if (ret < 0)
  228. return -EINVAL;
  229. nr_entries = ret;
  230. klp_for_each_object(klp_transition_patch, obj) {
  231. if (!obj->patched)
  232. continue;
  233. klp_for_each_func(obj, func) {
  234. ret = klp_check_stack_func(func, entries, nr_entries);
  235. if (ret) {
  236. *oldname = func->old_name;
  237. return -EADDRINUSE;
  238. }
  239. }
  240. }
  241. return 0;
  242. }
  243. static int klp_check_and_switch_task(struct task_struct *task, void *arg)
  244. {
  245. int ret;
  246. if (task_curr(task) && task != current)
  247. return -EBUSY;
  248. ret = klp_check_stack(task, arg);
  249. if (ret)
  250. return ret;
  251. clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
  252. task->patch_state = klp_target_state;
  253. return 0;
  254. }
  255. /*
  256. * Try to safely switch a task to the target patch state. If it's currently
  257. * running, or it's sleeping on a to-be-patched or to-be-unpatched function, or
  258. * if the stack is unreliable, return false.
  259. */
  260. static bool klp_try_switch_task(struct task_struct *task)
  261. {
  262. const char *old_name;
  263. int ret;
  264. /* check if this task has already switched over */
  265. if (task->patch_state == klp_target_state)
  266. return true;
  267. /*
  268. * For arches which don't have reliable stack traces, we have to rely
  269. * on other methods (e.g., switching tasks at kernel exit).
  270. */
  271. if (!klp_have_reliable_stack())
  272. return false;
  273. /*
  274. * Now try to check the stack for any to-be-patched or to-be-unpatched
  275. * functions. If all goes well, switch the task to the target patch
  276. * state.
  277. */
  278. if (task == current)
  279. ret = klp_check_and_switch_task(current, &old_name);
  280. else
  281. ret = task_call_func(task, klp_check_and_switch_task, &old_name);
  282. switch (ret) {
  283. case 0: /* success */
  284. break;
  285. case -EBUSY: /* klp_check_and_switch_task() */
  286. pr_debug("%s: %s:%d is running\n",
  287. __func__, task->comm, task->pid);
  288. break;
  289. case -EINVAL: /* klp_check_and_switch_task() */
  290. pr_debug("%s: %s:%d has an unreliable stack\n",
  291. __func__, task->comm, task->pid);
  292. break;
  293. case -EADDRINUSE: /* klp_check_and_switch_task() */
  294. pr_debug("%s: %s:%d is sleeping on function %s\n",
  295. __func__, task->comm, task->pid, old_name);
  296. break;
  297. default:
  298. pr_debug("%s: Unknown error code (%d) when trying to switch %s:%d\n",
  299. __func__, ret, task->comm, task->pid);
  300. break;
  301. }
  302. return !ret;
  303. }
  304. void __klp_sched_try_switch(void)
  305. {
  306. if (likely(!klp_patch_pending(current)))
  307. return;
  308. /*
  309. * This function is called from cond_resched() which is called in many
  310. * places throughout the kernel. Using the klp_mutex here might
  311. * deadlock.
  312. *
  313. * Instead, disable preemption to prevent racing with other callers of
  314. * klp_try_switch_task(). Thanks to task_call_func() they won't be
  315. * able to switch this task while it's running.
  316. */
  317. preempt_disable();
  318. /*
  319. * Make sure current didn't get patched between the above check and
  320. * preempt_disable().
  321. */
  322. if (unlikely(!klp_patch_pending(current)))
  323. goto out;
  324. /*
  325. * Enforce the order of the TIF_PATCH_PENDING read above and the
  326. * klp_target_state read in klp_try_switch_task(). The corresponding
  327. * write barriers are in klp_init_transition() and
  328. * klp_reverse_transition().
  329. */
  330. smp_rmb();
  331. klp_try_switch_task(current);
  332. out:
  333. preempt_enable();
  334. }
  335. EXPORT_SYMBOL(__klp_sched_try_switch);
  336. /*
  337. * Sends a fake signal to all non-kthread tasks with TIF_PATCH_PENDING set.
  338. * Kthreads with TIF_PATCH_PENDING set are woken up.
  339. */
  340. static void klp_send_signals(void)
  341. {
  342. struct task_struct *g, *task;
  343. if (klp_signals_cnt == SIGNALS_TIMEOUT)
  344. pr_notice("signaling remaining tasks\n");
  345. read_lock(&tasklist_lock);
  346. for_each_process_thread(g, task) {
  347. if (!klp_patch_pending(task))
  348. continue;
  349. /*
  350. * There is a small race here. We could see TIF_PATCH_PENDING
  351. * set and decide to wake up a kthread or send a fake signal.
  352. * Meanwhile the task could migrate itself and the action
  353. * would be meaningless. It is not serious though.
  354. */
  355. if (task->flags & PF_KTHREAD) {
  356. /*
  357. * Wake up a kthread which sleeps interruptedly and
  358. * still has not been migrated.
  359. */
  360. wake_up_state(task, TASK_INTERRUPTIBLE);
  361. } else {
  362. /*
  363. * Send fake signal to all non-kthread tasks which are
  364. * still not migrated.
  365. */
  366. set_notify_signal(task);
  367. }
  368. }
  369. read_unlock(&tasklist_lock);
  370. }
  371. /*
  372. * Try to switch all remaining tasks to the target patch state by walking the
  373. * stacks of sleeping tasks and looking for any to-be-patched or
  374. * to-be-unpatched functions. If such functions are found, the task can't be
  375. * switched yet.
  376. *
  377. * If any tasks are still stuck in the initial patch state, schedule a retry.
  378. */
  379. void klp_try_complete_transition(void)
  380. {
  381. unsigned int cpu;
  382. struct task_struct *g, *task;
  383. struct klp_patch *patch;
  384. bool complete = true;
  385. WARN_ON_ONCE(klp_target_state == KLP_TRANSITION_IDLE);
  386. /*
  387. * Try to switch the tasks to the target patch state by walking their
  388. * stacks and looking for any to-be-patched or to-be-unpatched
  389. * functions. If such functions are found on a stack, or if the stack
  390. * is deemed unreliable, the task can't be switched yet.
  391. *
  392. * Usually this will transition most (or all) of the tasks on a system
  393. * unless the patch includes changes to a very common function.
  394. */
  395. read_lock(&tasklist_lock);
  396. for_each_process_thread(g, task)
  397. if (!klp_try_switch_task(task))
  398. complete = false;
  399. read_unlock(&tasklist_lock);
  400. /*
  401. * Ditto for the idle "swapper" tasks.
  402. */
  403. cpus_read_lock();
  404. for_each_possible_cpu(cpu) {
  405. task = idle_task(cpu);
  406. if (cpu_online(cpu)) {
  407. if (!klp_try_switch_task(task)) {
  408. complete = false;
  409. /* Make idle task go through the main loop. */
  410. wake_up_if_idle(cpu);
  411. }
  412. } else if (task->patch_state != klp_target_state) {
  413. /* offline idle tasks can be switched immediately */
  414. clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
  415. task->patch_state = klp_target_state;
  416. }
  417. }
  418. cpus_read_unlock();
  419. if (!complete) {
  420. if (klp_signals_cnt && !(klp_signals_cnt % SIGNALS_TIMEOUT))
  421. klp_send_signals();
  422. klp_signals_cnt++;
  423. /*
  424. * Some tasks weren't able to be switched over. Try again
  425. * later and/or wait for other methods like kernel exit
  426. * switching.
  427. */
  428. schedule_delayed_work(&klp_transition_work,
  429. round_jiffies_relative(HZ));
  430. return;
  431. }
  432. /* Done! Now cleanup the data structures. */
  433. klp_cond_resched_disable();
  434. patch = klp_transition_patch;
  435. klp_complete_transition();
  436. /*
  437. * It would make more sense to free the unused patches in
  438. * klp_complete_transition() but it is called also
  439. * from klp_cancel_transition().
  440. */
  441. if (!patch->enabled)
  442. klp_free_patch_async(patch);
  443. else if (patch->replace)
  444. klp_free_replaced_patches_async(patch);
  445. }
  446. /*
  447. * Start the transition to the specified target patch state so tasks can begin
  448. * switching to it.
  449. */
  450. void klp_start_transition(void)
  451. {
  452. struct task_struct *g, *task;
  453. unsigned int cpu;
  454. WARN_ON_ONCE(klp_target_state == KLP_TRANSITION_IDLE);
  455. pr_notice("'%s': starting %s transition\n",
  456. klp_transition_patch->mod->name,
  457. klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
  458. /*
  459. * Mark all normal tasks as needing a patch state update. They'll
  460. * switch either in klp_try_complete_transition() or as they exit the
  461. * kernel.
  462. */
  463. read_lock(&tasklist_lock);
  464. for_each_process_thread(g, task)
  465. if (task->patch_state != klp_target_state)
  466. set_tsk_thread_flag(task, TIF_PATCH_PENDING);
  467. read_unlock(&tasklist_lock);
  468. /*
  469. * Mark all idle tasks as needing a patch state update. They'll switch
  470. * either in klp_try_complete_transition() or at the idle loop switch
  471. * point.
  472. */
  473. for_each_possible_cpu(cpu) {
  474. task = idle_task(cpu);
  475. if (task->patch_state != klp_target_state)
  476. set_tsk_thread_flag(task, TIF_PATCH_PENDING);
  477. }
  478. klp_cond_resched_enable();
  479. klp_signals_cnt = 0;
  480. }
  481. /*
  482. * Initialize the global target patch state and all tasks to the initial patch
  483. * state, and initialize all function transition states to true in preparation
  484. * for patching or unpatching.
  485. */
  486. void klp_init_transition(struct klp_patch *patch, int state)
  487. {
  488. struct task_struct *g, *task;
  489. unsigned int cpu;
  490. struct klp_object *obj;
  491. struct klp_func *func;
  492. int initial_state = !state;
  493. WARN_ON_ONCE(klp_target_state != KLP_TRANSITION_IDLE);
  494. klp_transition_patch = patch;
  495. /*
  496. * Set the global target patch state which tasks will switch to. This
  497. * has no effect until the TIF_PATCH_PENDING flags get set later.
  498. */
  499. klp_target_state = state;
  500. pr_debug("'%s': initializing %s transition\n", patch->mod->name,
  501. klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
  502. /*
  503. * Initialize all tasks to the initial patch state to prepare them for
  504. * switching to the target state.
  505. */
  506. read_lock(&tasklist_lock);
  507. for_each_process_thread(g, task) {
  508. WARN_ON_ONCE(task->patch_state != KLP_TRANSITION_IDLE);
  509. task->patch_state = initial_state;
  510. }
  511. read_unlock(&tasklist_lock);
  512. /*
  513. * Ditto for the idle "swapper" tasks.
  514. */
  515. for_each_possible_cpu(cpu) {
  516. task = idle_task(cpu);
  517. WARN_ON_ONCE(task->patch_state != KLP_TRANSITION_IDLE);
  518. task->patch_state = initial_state;
  519. }
  520. /*
  521. * Enforce the order of the task->patch_state initializations and the
  522. * func->transition updates to ensure that klp_ftrace_handler() doesn't
  523. * see a func in transition with a task->patch_state of KLP_TRANSITION_IDLE.
  524. *
  525. * Also enforce the order of the klp_target_state write and future
  526. * TIF_PATCH_PENDING writes to ensure klp_update_patch_state() and
  527. * __klp_sched_try_switch() don't set a task->patch_state to
  528. * KLP_TRANSITION_IDLE.
  529. */
  530. smp_wmb();
  531. /*
  532. * Set the func transition states so klp_ftrace_handler() will know to
  533. * switch to the transition logic.
  534. *
  535. * When patching, the funcs aren't yet in the func_stack and will be
  536. * made visible to the ftrace handler shortly by the calls to
  537. * klp_patch_object().
  538. *
  539. * When unpatching, the funcs are already in the func_stack and so are
  540. * already visible to the ftrace handler.
  541. */
  542. klp_for_each_object(patch, obj)
  543. klp_for_each_func(obj, func)
  544. func->transition = true;
  545. }
  546. /*
  547. * This function can be called in the middle of an existing transition to
  548. * reverse the direction of the target patch state. This can be done to
  549. * effectively cancel an existing enable or disable operation if there are any
  550. * tasks which are stuck in the initial patch state.
  551. */
  552. void klp_reverse_transition(void)
  553. {
  554. unsigned int cpu;
  555. struct task_struct *g, *task;
  556. pr_debug("'%s': reversing transition from %s\n",
  557. klp_transition_patch->mod->name,
  558. klp_target_state == KLP_TRANSITION_PATCHED ? "patching to unpatching" :
  559. "unpatching to patching");
  560. /*
  561. * Clear all TIF_PATCH_PENDING flags to prevent races caused by
  562. * klp_update_patch_state() or __klp_sched_try_switch() running in
  563. * parallel with the reverse transition.
  564. */
  565. read_lock(&tasklist_lock);
  566. for_each_process_thread(g, task)
  567. clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
  568. read_unlock(&tasklist_lock);
  569. for_each_possible_cpu(cpu)
  570. clear_tsk_thread_flag(idle_task(cpu), TIF_PATCH_PENDING);
  571. /*
  572. * Make sure all existing invocations of klp_update_patch_state() and
  573. * __klp_sched_try_switch() see the cleared TIF_PATCH_PENDING before
  574. * starting the reverse transition.
  575. */
  576. klp_synchronize_transition();
  577. /*
  578. * All patching has stopped, now re-initialize the global variables to
  579. * prepare for the reverse transition.
  580. */
  581. klp_transition_patch->enabled = !klp_transition_patch->enabled;
  582. klp_target_state = !klp_target_state;
  583. /*
  584. * Enforce the order of the klp_target_state write and the
  585. * TIF_PATCH_PENDING writes in klp_start_transition() to ensure
  586. * klp_update_patch_state() and __klp_sched_try_switch() don't set
  587. * task->patch_state to the wrong value.
  588. */
  589. smp_wmb();
  590. klp_start_transition();
  591. }
  592. /* Called from copy_process() during fork */
  593. void klp_copy_process(struct task_struct *child)
  594. {
  595. /*
  596. * The parent process may have gone through a KLP transition since
  597. * the thread flag was copied in setup_thread_stack earlier. Bring
  598. * the task flag up to date with the parent here.
  599. *
  600. * The operation is serialized against all klp_*_transition()
  601. * operations by the tasklist_lock. The only exceptions are
  602. * klp_update_patch_state(current) and __klp_sched_try_switch(), but we
  603. * cannot race with them because we are current.
  604. */
  605. if (test_tsk_thread_flag(current, TIF_PATCH_PENDING))
  606. set_tsk_thread_flag(child, TIF_PATCH_PENDING);
  607. else
  608. clear_tsk_thread_flag(child, TIF_PATCH_PENDING);
  609. child->patch_state = current->patch_state;
  610. }
  611. /*
  612. * Drop TIF_PATCH_PENDING of all tasks on admin's request. This forces an
  613. * existing transition to finish.
  614. *
  615. * NOTE: klp_update_patch_state(task) requires the task to be inactive or
  616. * 'current'. This is not the case here and the consistency model could be
  617. * broken. Administrator, who is the only one to execute the
  618. * klp_force_transitions(), has to be aware of this.
  619. */
  620. void klp_force_transition(void)
  621. {
  622. struct klp_patch *patch;
  623. struct task_struct *g, *task;
  624. unsigned int cpu;
  625. pr_warn("forcing remaining tasks to the patched state\n");
  626. read_lock(&tasklist_lock);
  627. for_each_process_thread(g, task)
  628. klp_update_patch_state(task);
  629. read_unlock(&tasklist_lock);
  630. for_each_possible_cpu(cpu)
  631. klp_update_patch_state(idle_task(cpu));
  632. /* Set forced flag for patches being removed. */
  633. if (klp_target_state == KLP_TRANSITION_UNPATCHED)
  634. klp_transition_patch->forced = true;
  635. else if (klp_transition_patch->replace) {
  636. klp_for_each_patch(patch) {
  637. if (patch != klp_transition_patch)
  638. patch->forced = true;
  639. }
  640. }
  641. }