Skip to content

Commit 0937e3b

Browse files
jpoimboeJiri Kosina
authored andcommitted
livepatch: simplify disable error path
If registering the function with ftrace has previously succeeded, unregistering will almost never fail. Even if it does, it's not a fatal error. We can still carry on and disable the klp_func from being used by removing it from the klp_ops func stack. Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Reviewed-by: Miroslav Benes <mbenes@suse.cz> Reviewed-by: Petr Mladek <pmladek@suse.cz> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 4421f8f commit 0937e3b

File tree

1 file changed

+17
-50
lines changed

1 file changed

+17
-50
lines changed

kernel/livepatch/core.c

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -322,32 +322,20 @@ static void notrace klp_ftrace_handler(unsigned long ip,
322322
klp_arch_set_pc(regs, (unsigned long)func->new_func);
323323
}
324324

325-
static int klp_disable_func(struct klp_func *func)
325+
static void klp_disable_func(struct klp_func *func)
326326
{
327327
struct klp_ops *ops;
328-
int ret;
329-
330-
if (WARN_ON(func->state != KLP_ENABLED))
331-
return -EINVAL;
332328

333-
if (WARN_ON(!func->old_addr))
334-
return -EINVAL;
329+
WARN_ON(func->state != KLP_ENABLED);
330+
WARN_ON(!func->old_addr);
335331

336332
ops = klp_find_ops(func->old_addr);
337333
if (WARN_ON(!ops))
338-
return -EINVAL;
334+
return;
339335

340336
if (list_is_singular(&ops->func_stack)) {
341-
ret = unregister_ftrace_function(&ops->fops);
342-
if (ret) {
343-
pr_err("failed to unregister ftrace handler for function '%s' (%d)\n",
344-
func->old_name, ret);
345-
return ret;
346-
}
347-
348-
ret = ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0);
349-
if (ret)
350-
pr_warn("function unregister succeeded but failed to clear the filter\n");
337+
WARN_ON(unregister_ftrace_function(&ops->fops));
338+
WARN_ON(ftrace_set_filter_ip(&ops->fops, func->old_addr, 1, 0));
351339

352340
list_del_rcu(&func->stack_node);
353341
list_del(&ops->node);
@@ -357,8 +345,6 @@ static int klp_disable_func(struct klp_func *func)
357345
}
358346

359347
func->state = KLP_DISABLED;
360-
361-
return 0;
362348
}
363349

364350
static int klp_enable_func(struct klp_func *func)
@@ -419,23 +405,15 @@ static int klp_enable_func(struct klp_func *func)
419405
return ret;
420406
}
421407

422-
static int klp_disable_object(struct klp_object *obj)
408+
static void klp_disable_object(struct klp_object *obj)
423409
{
424410
struct klp_func *func;
425-
int ret;
426411

427-
for (func = obj->funcs; func->old_name; func++) {
428-
if (func->state != KLP_ENABLED)
429-
continue;
430-
431-
ret = klp_disable_func(func);
432-
if (ret)
433-
return ret;
434-
}
412+
for (func = obj->funcs; func->old_name; func++)
413+
if (func->state == KLP_ENABLED)
414+
klp_disable_func(func);
435415

436416
obj->state = KLP_DISABLED;
437-
438-
return 0;
439417
}
440418

441419
static int klp_enable_object(struct klp_object *obj)
@@ -451,22 +429,19 @@ static int klp_enable_object(struct klp_object *obj)
451429

452430
for (func = obj->funcs; func->old_name; func++) {
453431
ret = klp_enable_func(func);
454-
if (ret)
455-
goto unregister;
432+
if (ret) {
433+
klp_disable_object(obj);
434+
return ret;
435+
}
456436
}
457437
obj->state = KLP_ENABLED;
458438

459439
return 0;
460-
461-
unregister:
462-
WARN_ON(klp_disable_object(obj));
463-
return ret;
464440
}
465441

466442
static int __klp_disable_patch(struct klp_patch *patch)
467443
{
468444
struct klp_object *obj;
469-
int ret;
470445

471446
/* enforce stacking: only the last enabled patch can be disabled */
472447
if (!list_is_last(&patch->list, &klp_patches) &&
@@ -476,12 +451,8 @@ static int __klp_disable_patch(struct klp_patch *patch)
476451
pr_notice("disabling patch '%s'\n", patch->mod->name);
477452

478453
for (obj = patch->objs; obj->funcs; obj++) {
479-
if (obj->state != KLP_ENABLED)
480-
continue;
481-
482-
ret = klp_disable_object(obj);
483-
if (ret)
484-
return ret;
454+
if (obj->state == KLP_ENABLED)
455+
klp_disable_object(obj);
485456
}
486457

487458
patch->state = KLP_DISABLED;
@@ -931,18 +902,14 @@ static void klp_module_notify_going(struct klp_patch *patch,
931902
{
932903
struct module *pmod = patch->mod;
933904
struct module *mod = obj->mod;
934-
int ret;
935905

936906
if (patch->state == KLP_DISABLED)
937907
goto disabled;
938908

939909
pr_notice("reverting patch '%s' on unloading module '%s'\n",
940910
pmod->name, mod->name);
941911

942-
ret = klp_disable_object(obj);
943-
if (ret)
944-
pr_warn("failed to revert patch '%s' on module '%s' (%d)\n",
945-
pmod->name, mod->name, ret);
912+
klp_disable_object(obj);
946913

947914
disabled:
948915
klp_free_object_loaded(obj);

0 commit comments

Comments
 (0)