History

livepatch: Remove Nop structures when unused

Replaced patches are removed from the stack when the transition is finished. It means that Nop structures will never be needed again and can be removed. Why should we care? + Nop structures give the impression that the function is patched even though the ftrace handler has no effect. + Ftrace handlers do not come for free. They cause slowdown that might be visible in some workloads. The ftrace-related slowdown might actually be the reason why the function is no longer patched in the new cumulative patch. One would expect that cumulative patch would help solve these problems as well. + Cumulative patches are supposed to replace any earlier version of the patch. The amount of NOPs depends on which version was replaced. This multiplies the amount of scenarios that might happen. One might say that NOPs are innocent. But there are even optimized NOP instructions for different processors, for example, see arch/x86/kernel/alternative.c. And klp_ftrace_handler() is much more complicated. + It sounds natural to clean up a mess that is no longer needed. It could only be worse if we do not do it. This patch allows to unpatch and free the dynamic structures independently when the transition finishes. The free part is a bit tricky because kobject free callbacks are called asynchronously. We could not wait for them easily. Fortunately, we do not have to. Any further access can be avoided by removing them from the dynamic lists.

Petr Mladek committed 7 years ago

Commit d697bad1 parent e1452b65 files · +76 −9Browse files

Showing the changes to kernel/livepatch/patch.c (1 of 5 files). Show all

Unified · Split

kernel/livepatch/patch.c+26−5
@@ -246,15 +246,26 @@ err: · 245 unmodified lines
246246 return ret;
247247 }
248248
249−void klp_unpatch_object(struct klp_object *obj)
249+static void __klp_unpatch_object(struct klp_object *obj, bool nops_only)
250250 {
251251 struct klp_func *func;
252252
253− klp_for_each_func(obj, func)
253+ klp_for_each_func(obj, func) {
254+ if (nops_only && !func->nop)
255+ continue;
256+
254257 if (func->patched)
255258 klp_unpatch_func(func);
259+ }
256260
257− obj->patched = false;
261+ if (obj->dynamic || !nops_only)
262+ obj->patched = false;
263+}
264+
265+
266+void klp_unpatch_object(struct klp_object *obj)
267+{
268+ __klp_unpatch_object(obj, false);
258269 }
259270
260271 int klp_patch_object(struct klp_object *obj)
@@ -277,11 +288,21 @@ int klp_patch_object(struct klp_object *obj) · 16 unmodified lines
277288 return 0;
278289 }
279290
280−void klp_unpatch_objects(struct klp_patch *patch)
291+static void __klp_unpatch_objects(struct klp_patch *patch, bool nops_only)
281292 {
282293 struct klp_object *obj;
283294
284295 klp_for_each_object(patch, obj)
285296 if (obj->patched)
286− klp_unpatch_object(obj);
297+ __klp_unpatch_object(obj, nops_only);
298+}
299+
300+void klp_unpatch_objects(struct klp_patch *patch)
301+{
302+ __klp_unpatch_objects(patch, false);
303+}
304+
305+void klp_unpatch_objects_dynamic(struct klp_patch *patch)
306+{
307+ __klp_unpatch_objects(patch, true);
287308 }