callbacks.txt 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. ======================
  2. (Un)patching Callbacks
  3. ======================
  4. Livepatch (un)patch-callbacks provide a mechanism for livepatch modules
  5. to execute callback functions when a kernel object is (un)patched. They
  6. can be considered a "power feature" that extends livepatching abilities
  7. to include:
  8. - Safe updates to global data
  9. - "Patches" to init and probe functions
  10. - Patching otherwise unpatchable code (i.e. assembly)
  11. In most cases, (un)patch callbacks will need to be used in conjunction
  12. with memory barriers and kernel synchronization primitives, like
  13. mutexes/spinlocks, or even stop_machine(), to avoid concurrency issues.
  14. Callbacks differ from existing kernel facilities:
  15. - Module init/exit code doesn't run when disabling and re-enabling a
  16. patch.
  17. - A module notifier can't stop a to-be-patched module from loading.
  18. Callbacks are part of the klp_object structure and their implementation
  19. is specific to that klp_object. Other livepatch objects may or may not
  20. be patched, irrespective of the target klp_object's current state.
  21. Callbacks can be registered for the following livepatch actions:
  22. * Pre-patch - before a klp_object is patched
  23. * Post-patch - after a klp_object has been patched and is active
  24. across all tasks
  25. * Pre-unpatch - before a klp_object is unpatched (ie, patched code is
  26. active), used to clean up post-patch callback
  27. resources
  28. * Post-unpatch - after a klp_object has been patched, all code has
  29. been restored and no tasks are running patched code,
  30. used to cleanup pre-patch callback resources
  31. Each callback is optional, omitting one does not preclude specifying any
  32. other. However, the livepatching core executes the handlers in
  33. symmetry: pre-patch callbacks have a post-unpatch counterpart and
  34. post-patch callbacks have a pre-unpatch counterpart. An unpatch
  35. callback will only be executed if its corresponding patch callback was
  36. executed. Typical use cases pair a patch handler that acquires and
  37. configures resources with an unpatch handler tears down and releases
  38. those same resources.
  39. A callback is only executed if its host klp_object is loaded. For
  40. in-kernel vmlinux targets, this means that callbacks will always execute
  41. when a livepatch is enabled/disabled. For patch target kernel modules,
  42. callbacks will only execute if the target module is loaded. When a
  43. module target is (un)loaded, its callbacks will execute only if the
  44. livepatch module is enabled.
  45. The pre-patch callback, if specified, is expected to return a status
  46. code (0 for success, -ERRNO on error). An error status code indicates
  47. to the livepatching core that patching of the current klp_object is not
  48. safe and to stop the current patching request. (When no pre-patch
  49. callback is provided, the transition is assumed to be safe.) If a
  50. pre-patch callback returns failure, the kernel's module loader will:
  51. - Refuse to load a livepatch, if the livepatch is loaded after
  52. targeted code.
  53. or:
  54. - Refuse to load a module, if the livepatch was already successfully
  55. loaded.
  56. No post-patch, pre-unpatch, or post-unpatch callbacks will be executed
  57. for a given klp_object if the object failed to patch, due to a failed
  58. pre_patch callback or for any other reason.
  59. If a patch transition is reversed, no pre-unpatch handlers will be run
  60. (this follows the previously mentioned symmetry -- pre-unpatch callbacks
  61. will only occur if their corresponding post-patch callback executed).
  62. If the object did successfully patch, but the patch transition never
  63. started for some reason (e.g., if another object failed to patch),
  64. only the post-unpatch callback will be called.
  65. Example Use-cases
  66. =================
  67. Update global data
  68. ------------------
  69. A pre-patch callback can be useful to update a global variable. For
  70. example, 75ff39ccc1bd ("tcp: make challenge acks less predictable")
  71. changes a global sysctl, as well as patches the tcp_send_challenge_ack()
  72. function.
  73. In this case, if we're being super paranoid, it might make sense to
  74. patch the data *after* patching is complete with a post-patch callback,
  75. so that tcp_send_challenge_ack() could first be changed to read
  76. sysctl_tcp_challenge_ack_limit with READ_ONCE.
  77. Support __init and probe function patches
  78. -----------------------------------------
  79. Although __init and probe functions are not directly livepatch-able, it
  80. may be possible to implement similar updates via pre/post-patch
  81. callbacks.
  82. 48900cb6af42 ("virtio-net: drop NETIF_F_FRAGLIST") change the way that
  83. virtnet_probe() initialized its driver's net_device features. A
  84. pre/post-patch callback could iterate over all such devices, making a
  85. similar change to their hw_features value. (Client functions of the
  86. value may need to be updated accordingly.)
  87. Test cases
  88. ==========
  89. What follows is not an exhaustive test suite of every possible livepatch
  90. pre/post-(un)patch combination, but a selection that demonstrates a few
  91. important concepts. Each test case uses the kernel modules located in
  92. the samples/livepatch/ and assumes that no livepatches are loaded at the
  93. beginning of the test.
  94. Test 1
  95. ------
  96. Test a combination of loading a kernel module and a livepatch that
  97. patches a function in the first module. (Un)load the target module
  98. before the livepatch module:
  99. - load target module
  100. - load livepatch
  101. - disable livepatch
  102. - unload target module
  103. - unload livepatch
  104. First load a target module:
  105. % insmod samples/livepatch/livepatch-callbacks-mod.ko
  106. [ 34.475708] livepatch_callbacks_mod: livepatch_callbacks_mod_init
  107. On livepatch enable, before the livepatch transition starts, pre-patch
  108. callbacks are executed for vmlinux and livepatch_callbacks_mod (those
  109. klp_objects currently loaded). After klp_objects are patched according
  110. to the klp_patch, their post-patch callbacks run and the transition
  111. completes:
  112. % insmod samples/livepatch/livepatch-callbacks-demo.ko
  113. [ 36.503719] livepatch: enabling patch 'livepatch_callbacks_demo'
  114. [ 36.504213] livepatch: 'livepatch_callbacks_demo': initializing patching transition
  115. [ 36.504238] livepatch_callbacks_demo: pre_patch_callback: vmlinux
  116. [ 36.504721] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state
  117. [ 36.505849] livepatch: 'livepatch_callbacks_demo': starting patching transition
  118. [ 37.727133] livepatch: 'livepatch_callbacks_demo': completing patching transition
  119. [ 37.727232] livepatch_callbacks_demo: post_patch_callback: vmlinux
  120. [ 37.727860] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state
  121. [ 37.728792] livepatch: 'livepatch_callbacks_demo': patching complete
  122. Similarly, on livepatch disable, pre-patch callbacks run before the
  123. unpatching transition starts. klp_objects are reverted, post-patch
  124. callbacks execute and the transition completes:
  125. % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
  126. [ 38.510209] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition
  127. [ 38.510234] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux
  128. [ 38.510982] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state
  129. [ 38.512209] livepatch: 'livepatch_callbacks_demo': starting unpatching transition
  130. [ 39.711132] livepatch: 'livepatch_callbacks_demo': completing unpatching transition
  131. [ 39.711210] livepatch_callbacks_demo: post_unpatch_callback: vmlinux
  132. [ 39.711779] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state
  133. [ 39.712735] livepatch: 'livepatch_callbacks_demo': unpatching complete
  134. % rmmod samples/livepatch/livepatch-callbacks-demo.ko
  135. % rmmod samples/livepatch/livepatch-callbacks-mod.ko
  136. [ 42.534183] livepatch_callbacks_mod: livepatch_callbacks_mod_exit
  137. Test 2
  138. ------
  139. This test is similar to the previous test, but (un)load the livepatch
  140. module before the target kernel module. This tests the livepatch core's
  141. module_coming handler:
  142. - load livepatch
  143. - load target module
  144. - disable livepatch
  145. - unload livepatch
  146. - unload target module
  147. On livepatch enable, only pre/post-patch callbacks are executed for
  148. currently loaded klp_objects, in this case, vmlinux:
  149. % insmod samples/livepatch/livepatch-callbacks-demo.ko
  150. [ 44.553328] livepatch: enabling patch 'livepatch_callbacks_demo'
  151. [ 44.553997] livepatch: 'livepatch_callbacks_demo': initializing patching transition
  152. [ 44.554049] livepatch_callbacks_demo: pre_patch_callback: vmlinux
  153. [ 44.554845] livepatch: 'livepatch_callbacks_demo': starting patching transition
  154. [ 45.727128] livepatch: 'livepatch_callbacks_demo': completing patching transition
  155. [ 45.727212] livepatch_callbacks_demo: post_patch_callback: vmlinux
  156. [ 45.727961] livepatch: 'livepatch_callbacks_demo': patching complete
  157. When a targeted module is subsequently loaded, only its pre/post-patch
  158. callbacks are executed:
  159. % insmod samples/livepatch/livepatch-callbacks-mod.ko
  160. [ 46.560845] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod'
  161. [ 46.561988] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
  162. [ 46.563452] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
  163. [ 46.565495] livepatch_callbacks_mod: livepatch_callbacks_mod_init
  164. On livepatch disable, all currently loaded klp_objects' (vmlinux and
  165. livepatch_callbacks_mod) pre/post-unpatch callbacks are executed:
  166. % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
  167. [ 48.568885] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition
  168. [ 48.568910] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux
  169. [ 48.569441] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state
  170. [ 48.570502] livepatch: 'livepatch_callbacks_demo': starting unpatching transition
  171. [ 49.759091] livepatch: 'livepatch_callbacks_demo': completing unpatching transition
  172. [ 49.759171] livepatch_callbacks_demo: post_unpatch_callback: vmlinux
  173. [ 49.759742] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state
  174. [ 49.760690] livepatch: 'livepatch_callbacks_demo': unpatching complete
  175. % rmmod samples/livepatch/livepatch-callbacks-demo.ko
  176. % rmmod samples/livepatch/livepatch-callbacks-mod.ko
  177. [ 52.592283] livepatch_callbacks_mod: livepatch_callbacks_mod_exit
  178. Test 3
  179. ------
  180. Test loading the livepatch after a targeted kernel module, then unload
  181. the kernel module before disabling the livepatch. This tests the
  182. livepatch core's module_going handler:
  183. - load target module
  184. - load livepatch
  185. - unload target module
  186. - disable livepatch
  187. - unload livepatch
  188. First load a target module, then the livepatch:
  189. % insmod samples/livepatch/livepatch-callbacks-mod.ko
  190. [ 54.607948] livepatch_callbacks_mod: livepatch_callbacks_mod_init
  191. % insmod samples/livepatch/livepatch-callbacks-demo.ko
  192. [ 56.613919] livepatch: enabling patch 'livepatch_callbacks_demo'
  193. [ 56.614411] livepatch: 'livepatch_callbacks_demo': initializing patching transition
  194. [ 56.614436] livepatch_callbacks_demo: pre_patch_callback: vmlinux
  195. [ 56.614818] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state
  196. [ 56.615656] livepatch: 'livepatch_callbacks_demo': starting patching transition
  197. [ 57.759070] livepatch: 'livepatch_callbacks_demo': completing patching transition
  198. [ 57.759147] livepatch_callbacks_demo: post_patch_callback: vmlinux
  199. [ 57.759621] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_LIVE] Normal state
  200. [ 57.760307] livepatch: 'livepatch_callbacks_demo': patching complete
  201. When a target module is unloaded, the livepatch is only reverted from
  202. that klp_object (livepatch_callbacks_mod). As such, only its pre and
  203. post-unpatch callbacks are executed when this occurs:
  204. % rmmod samples/livepatch/livepatch-callbacks-mod.ko
  205. [ 58.623409] livepatch_callbacks_mod: livepatch_callbacks_mod_exit
  206. [ 58.623903] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away
  207. [ 58.624658] livepatch: reverting patch 'livepatch_callbacks_demo' on unloading module 'livepatch_callbacks_mod'
  208. [ 58.625305] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away
  209. When the livepatch is disabled, pre and post-unpatch callbacks are run
  210. for the remaining klp_object, vmlinux:
  211. % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
  212. [ 60.638420] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition
  213. [ 60.638444] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux
  214. [ 60.638996] livepatch: 'livepatch_callbacks_demo': starting unpatching transition
  215. [ 61.727088] livepatch: 'livepatch_callbacks_demo': completing unpatching transition
  216. [ 61.727165] livepatch_callbacks_demo: post_unpatch_callback: vmlinux
  217. [ 61.727985] livepatch: 'livepatch_callbacks_demo': unpatching complete
  218. % rmmod samples/livepatch/livepatch-callbacks-demo.ko
  219. Test 4
  220. ------
  221. This test is similar to the previous test, however the livepatch is
  222. loaded first. This tests the livepatch core's module_coming and
  223. module_going handlers:
  224. - load livepatch
  225. - load target module
  226. - unload target module
  227. - disable livepatch
  228. - unload livepatch
  229. First load the livepatch:
  230. % insmod samples/livepatch/livepatch-callbacks-demo.ko
  231. [ 64.661552] livepatch: enabling patch 'livepatch_callbacks_demo'
  232. [ 64.662147] livepatch: 'livepatch_callbacks_demo': initializing patching transition
  233. [ 64.662175] livepatch_callbacks_demo: pre_patch_callback: vmlinux
  234. [ 64.662850] livepatch: 'livepatch_callbacks_demo': starting patching transition
  235. [ 65.695056] livepatch: 'livepatch_callbacks_demo': completing patching transition
  236. [ 65.695147] livepatch_callbacks_demo: post_patch_callback: vmlinux
  237. [ 65.695561] livepatch: 'livepatch_callbacks_demo': patching complete
  238. When a targeted kernel module is subsequently loaded, only its
  239. pre/post-patch callbacks are executed:
  240. % insmod samples/livepatch/livepatch-callbacks-mod.ko
  241. [ 66.669196] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod'
  242. [ 66.669882] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
  243. [ 66.670744] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
  244. [ 66.672873] livepatch_callbacks_mod: livepatch_callbacks_mod_init
  245. When the target module is unloaded, the livepatch is only reverted from
  246. the livepatch_callbacks_mod klp_object. As such, only pre and
  247. post-unpatch callbacks are executed when this occurs:
  248. % rmmod samples/livepatch/livepatch-callbacks-mod.ko
  249. [ 68.680065] livepatch_callbacks_mod: livepatch_callbacks_mod_exit
  250. [ 68.680688] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away
  251. [ 68.681452] livepatch: reverting patch 'livepatch_callbacks_demo' on unloading module 'livepatch_callbacks_mod'
  252. [ 68.682094] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away
  253. % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
  254. [ 70.689225] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition
  255. [ 70.689256] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux
  256. [ 70.689882] livepatch: 'livepatch_callbacks_demo': starting unpatching transition
  257. [ 71.711080] livepatch: 'livepatch_callbacks_demo': completing unpatching transition
  258. [ 71.711481] livepatch_callbacks_demo: post_unpatch_callback: vmlinux
  259. [ 71.711988] livepatch: 'livepatch_callbacks_demo': unpatching complete
  260. % rmmod samples/livepatch/livepatch-callbacks-demo.ko
  261. Test 5
  262. ------
  263. A simple test of loading a livepatch without one of its patch target
  264. klp_objects ever loaded (livepatch_callbacks_mod):
  265. - load livepatch
  266. - disable livepatch
  267. - unload livepatch
  268. Load the livepatch:
  269. % insmod samples/livepatch/livepatch-callbacks-demo.ko
  270. [ 74.711081] livepatch: enabling patch 'livepatch_callbacks_demo'
  271. [ 74.711595] livepatch: 'livepatch_callbacks_demo': initializing patching transition
  272. [ 74.711639] livepatch_callbacks_demo: pre_patch_callback: vmlinux
  273. [ 74.712272] livepatch: 'livepatch_callbacks_demo': starting patching transition
  274. [ 75.743137] livepatch: 'livepatch_callbacks_demo': completing patching transition
  275. [ 75.743219] livepatch_callbacks_demo: post_patch_callback: vmlinux
  276. [ 75.743867] livepatch: 'livepatch_callbacks_demo': patching complete
  277. As expected, only pre/post-(un)patch handlers are executed for vmlinux:
  278. % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
  279. [ 76.716254] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition
  280. [ 76.716278] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux
  281. [ 76.716666] livepatch: 'livepatch_callbacks_demo': starting unpatching transition
  282. [ 77.727089] livepatch: 'livepatch_callbacks_demo': completing unpatching transition
  283. [ 77.727194] livepatch_callbacks_demo: post_unpatch_callback: vmlinux
  284. [ 77.727907] livepatch: 'livepatch_callbacks_demo': unpatching complete
  285. % rmmod samples/livepatch/livepatch-callbacks-demo.ko
  286. Test 6
  287. ------
  288. Test a scenario where a vmlinux pre-patch callback returns a non-zero
  289. status (ie, failure):
  290. - load target module
  291. - load livepatch -ENODEV
  292. - unload target module
  293. First load a target module:
  294. % insmod samples/livepatch/livepatch-callbacks-mod.ko
  295. [ 80.740520] livepatch_callbacks_mod: livepatch_callbacks_mod_init
  296. Load the livepatch module, setting its 'pre_patch_ret' value to -19
  297. (-ENODEV). When its vmlinux pre-patch callback executed, this status
  298. code will propagate back to the module-loading subsystem. The result is
  299. that the insmod command refuses to load the livepatch module:
  300. % insmod samples/livepatch/livepatch-callbacks-demo.ko pre_patch_ret=-19
  301. [ 82.747326] livepatch: enabling patch 'livepatch_callbacks_demo'
  302. [ 82.747743] livepatch: 'livepatch_callbacks_demo': initializing patching transition
  303. [ 82.747767] livepatch_callbacks_demo: pre_patch_callback: vmlinux
  304. [ 82.748237] livepatch: pre-patch callback failed for object 'vmlinux'
  305. [ 82.748637] livepatch: failed to enable patch 'livepatch_callbacks_demo'
  306. [ 82.749059] livepatch: 'livepatch_callbacks_demo': canceling transition, going to unpatch
  307. [ 82.749060] livepatch: 'livepatch_callbacks_demo': completing unpatching transition
  308. [ 82.749868] livepatch: 'livepatch_callbacks_demo': unpatching complete
  309. [ 82.765809] insmod: ERROR: could not insert module samples/livepatch/livepatch-callbacks-demo.ko: No such device
  310. % rmmod samples/livepatch/livepatch-callbacks-mod.ko
  311. [ 84.774238] livepatch_callbacks_mod: livepatch_callbacks_mod_exit
  312. Test 7
  313. ------
  314. Similar to the previous test, setup a livepatch such that its vmlinux
  315. pre-patch callback returns success. However, when a targeted kernel
  316. module is later loaded, have the livepatch return a failing status code:
  317. - load livepatch
  318. - setup -ENODEV
  319. - load target module
  320. - disable livepatch
  321. - unload livepatch
  322. Load the livepatch, notice vmlinux pre-patch callback succeeds:
  323. % insmod samples/livepatch/livepatch-callbacks-demo.ko
  324. [ 86.787845] livepatch: enabling patch 'livepatch_callbacks_demo'
  325. [ 86.788325] livepatch: 'livepatch_callbacks_demo': initializing patching transition
  326. [ 86.788427] livepatch_callbacks_demo: pre_patch_callback: vmlinux
  327. [ 86.788821] livepatch: 'livepatch_callbacks_demo': starting patching transition
  328. [ 87.711069] livepatch: 'livepatch_callbacks_demo': completing patching transition
  329. [ 87.711143] livepatch_callbacks_demo: post_patch_callback: vmlinux
  330. [ 87.711886] livepatch: 'livepatch_callbacks_demo': patching complete
  331. Set a trap so subsequent pre-patch callbacks to this livepatch will
  332. return -ENODEV:
  333. % echo -19 > /sys/module/livepatch_callbacks_demo/parameters/pre_patch_ret
  334. The livepatch pre-patch callback for subsequently loaded target modules
  335. will return failure, so the module loader refuses to load the kernel
  336. module. Notice that no post-patch or pre/post-unpatch callbacks are
  337. executed for this klp_object:
  338. % insmod samples/livepatch/livepatch-callbacks-mod.ko
  339. [ 90.796976] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod'
  340. [ 90.797834] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
  341. [ 90.798900] livepatch: pre-patch callback failed for object 'livepatch_callbacks_mod'
  342. [ 90.799652] livepatch: patch 'livepatch_callbacks_demo' failed for module 'livepatch_callbacks_mod', refusing to load module 'livepatch_callbacks_mod'
  343. [ 90.819737] insmod: ERROR: could not insert module samples/livepatch/livepatch-callbacks-mod.ko: No such device
  344. However, pre/post-unpatch callbacks run for the vmlinux klp_object:
  345. % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
  346. [ 92.823547] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition
  347. [ 92.823573] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux
  348. [ 92.824331] livepatch: 'livepatch_callbacks_demo': starting unpatching transition
  349. [ 93.727128] livepatch: 'livepatch_callbacks_demo': completing unpatching transition
  350. [ 93.727327] livepatch_callbacks_demo: post_unpatch_callback: vmlinux
  351. [ 93.727861] livepatch: 'livepatch_callbacks_demo': unpatching complete
  352. % rmmod samples/livepatch/livepatch-callbacks-demo.ko
  353. Test 8
  354. ------
  355. Test loading multiple targeted kernel modules. This test-case is
  356. mainly for comparing with the next test-case.
  357. - load busy target module (0s sleep),
  358. - load livepatch
  359. - load target module
  360. - unload target module
  361. - disable livepatch
  362. - unload livepatch
  363. - unload busy target module
  364. Load a target "busy" kernel module which kicks off a worker function
  365. that immediately exits:
  366. % insmod samples/livepatch/livepatch-callbacks-busymod.ko sleep_secs=0
  367. [ 96.910107] livepatch_callbacks_busymod: livepatch_callbacks_mod_init
  368. [ 96.910600] livepatch_callbacks_busymod: busymod_work_func, sleeping 0 seconds ...
  369. [ 96.913024] livepatch_callbacks_busymod: busymod_work_func exit
  370. Proceed with loading the livepatch and another ordinary target module,
  371. notice that the post-patch callbacks are executed and the transition
  372. completes quickly:
  373. % insmod samples/livepatch/livepatch-callbacks-demo.ko
  374. [ 98.917892] livepatch: enabling patch 'livepatch_callbacks_demo'
  375. [ 98.918426] livepatch: 'livepatch_callbacks_demo': initializing patching transition
  376. [ 98.918453] livepatch_callbacks_demo: pre_patch_callback: vmlinux
  377. [ 98.918955] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state
  378. [ 98.923835] livepatch: 'livepatch_callbacks_demo': starting patching transition
  379. [ 99.743104] livepatch: 'livepatch_callbacks_demo': completing patching transition
  380. [ 99.743156] livepatch_callbacks_demo: post_patch_callback: vmlinux
  381. [ 99.743679] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state
  382. [ 99.744616] livepatch: 'livepatch_callbacks_demo': patching complete
  383. % insmod samples/livepatch/livepatch-callbacks-mod.ko
  384. [ 100.930955] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod'
  385. [ 100.931668] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
  386. [ 100.932645] livepatch_callbacks_demo: post_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
  387. [ 100.934125] livepatch_callbacks_mod: livepatch_callbacks_mod_init
  388. % rmmod samples/livepatch/livepatch-callbacks-mod.ko
  389. [ 102.942805] livepatch_callbacks_mod: livepatch_callbacks_mod_exit
  390. [ 102.943640] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away
  391. [ 102.944585] livepatch: reverting patch 'livepatch_callbacks_demo' on unloading module 'livepatch_callbacks_mod'
  392. [ 102.945455] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away
  393. % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
  394. [ 104.953815] livepatch: 'livepatch_callbacks_demo': initializing unpatching transition
  395. [ 104.953838] livepatch_callbacks_demo: pre_unpatch_callback: vmlinux
  396. [ 104.954431] livepatch_callbacks_demo: pre_unpatch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state
  397. [ 104.955426] livepatch: 'livepatch_callbacks_demo': starting unpatching transition
  398. [ 106.719073] livepatch: 'livepatch_callbacks_demo': completing unpatching transition
  399. [ 106.722633] livepatch_callbacks_demo: post_unpatch_callback: vmlinux
  400. [ 106.723282] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state
  401. [ 106.724279] livepatch: 'livepatch_callbacks_demo': unpatching complete
  402. % rmmod samples/livepatch/livepatch-callbacks-demo.ko
  403. % rmmod samples/livepatch/livepatch-callbacks-busymod.ko
  404. [ 108.975660] livepatch_callbacks_busymod: livepatch_callbacks_mod_exit
  405. Test 9
  406. ------
  407. A similar test as the previous one, but force the "busy" kernel module
  408. to do longer work.
  409. The livepatching core will refuse to patch a task that is currently
  410. executing a to-be-patched function -- the consistency model stalls the
  411. current patch transition until this safety-check is met. Test a
  412. scenario where one of a livepatch's target klp_objects sits on such a
  413. function for a long time. Meanwhile, load and unload other target
  414. kernel modules while the livepatch transition is in progress.
  415. - load busy target module (30s sleep)
  416. - load livepatch
  417. - load target module
  418. - unload target module
  419. - disable livepatch
  420. - unload livepatch
  421. - unload busy target module
  422. Load the "busy" kernel module, this time make it do 30 seconds worth of
  423. work:
  424. % insmod samples/livepatch/livepatch-callbacks-busymod.ko sleep_secs=30
  425. [ 110.993362] livepatch_callbacks_busymod: livepatch_callbacks_mod_init
  426. [ 110.994059] livepatch_callbacks_busymod: busymod_work_func, sleeping 30 seconds ...
  427. Meanwhile, the livepatch is loaded. Notice that the patch transition
  428. does not complete as the targeted "busy" module is sitting on a
  429. to-be-patched function:
  430. % insmod samples/livepatch/livepatch-callbacks-demo.ko
  431. [ 113.000309] livepatch: enabling patch 'livepatch_callbacks_demo'
  432. [ 113.000764] livepatch: 'livepatch_callbacks_demo': initializing patching transition
  433. [ 113.000791] livepatch_callbacks_demo: pre_patch_callback: vmlinux
  434. [ 113.001289] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state
  435. [ 113.005208] livepatch: 'livepatch_callbacks_demo': starting patching transition
  436. Load a second target module (this one is an ordinary idle kernel
  437. module). Note that *no* post-patch callbacks will be executed while the
  438. livepatch is still in transition:
  439. % insmod samples/livepatch/livepatch-callbacks-mod.ko
  440. [ 115.012740] livepatch: applying patch 'livepatch_callbacks_demo' to loading module 'livepatch_callbacks_mod'
  441. [ 115.013406] livepatch_callbacks_demo: pre_patch_callback: livepatch_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
  442. [ 115.015315] livepatch_callbacks_mod: livepatch_callbacks_mod_init
  443. Request an unload of the simple kernel module. The patch is still
  444. transitioning, so its pre-unpatch callbacks are skipped:
  445. % rmmod samples/livepatch/livepatch-callbacks-mod.ko
  446. [ 117.022626] livepatch_callbacks_mod: livepatch_callbacks_mod_exit
  447. [ 117.023376] livepatch: reverting patch 'livepatch_callbacks_demo' on unloading module 'livepatch_callbacks_mod'
  448. [ 117.024533] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_mod -> [MODULE_STATE_GOING] Going away
  449. Finally the livepatch is disabled. Since none of the patch's
  450. klp_object's post-patch callbacks executed, the remaining klp_object's
  451. pre-unpatch callbacks are skipped:
  452. % echo 0 > /sys/kernel/livepatch/livepatch_callbacks_demo/enabled
  453. [ 119.035408] livepatch: 'livepatch_callbacks_demo': reversing transition from patching to unpatching
  454. [ 119.035485] livepatch: 'livepatch_callbacks_demo': starting unpatching transition
  455. [ 119.711166] livepatch: 'livepatch_callbacks_demo': completing unpatching transition
  456. [ 119.714179] livepatch_callbacks_demo: post_unpatch_callback: vmlinux
  457. [ 119.714653] livepatch_callbacks_demo: post_unpatch_callback: livepatch_callbacks_busymod -> [MODULE_STATE_LIVE] Normal state
  458. [ 119.715437] livepatch: 'livepatch_callbacks_demo': unpatching complete
  459. % rmmod samples/livepatch/livepatch-callbacks-demo.ko
  460. % rmmod samples/livepatch/livepatch-callbacks-busymod.ko
  461. [ 141.279111] livepatch_callbacks_busymod: busymod_work_func exit
  462. [ 141.279760] livepatch_callbacks_busymod: livepatch_callbacks_mod_exit