2021-09-22 11:17:53

by Peter Zijlstra

[permalink] [raw]
Subject: [RFC][PATCH 5/7] sched,livepatch: Use wake_up_if_idle()

Make sure to prod idle CPUs so they call klp_update_patch_state().

Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
---
kernel/livepatch/transition.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)

--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -287,21 +287,21 @@ static int klp_check_task(struct task_st
* 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)
+static int klp_try_switch_task(struct task_struct *task)
{
const char *old_name;
int ret;

/* check if this task has already switched over */
if (task->patch_state == klp_target_state)
- return true;
+ return 0;

/*
* 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;
+ return -EINVAL;

/*
* Now try to check the stack for any to-be-patched or to-be-unpatched
@@ -324,7 +324,7 @@ static bool klp_try_switch_task(struct t
break;
}

- return !ret;
+ return ret;
}

/*
@@ -394,7 +394,7 @@ void klp_try_complete_transition(void)
*/
read_lock(&tasklist_lock);
for_each_process_thread(g, task)
- if (!klp_try_switch_task(task))
+ if (klp_try_switch_task(task))
complete = false;
read_unlock(&tasklist_lock);

@@ -405,8 +405,10 @@ void klp_try_complete_transition(void)
for_each_possible_cpu(cpu) {
task = idle_task(cpu);
if (cpu_online(cpu)) {
- if (!klp_try_switch_task(task))
- complete = false;
+ int ret = klp_try_switch_task(task);
+ if (ret == -EBUSY)
+ wake_up_if_idle(cpu);
+ complete = !ret;
} else if (task->patch_state != klp_target_state) {
/* offline idle tasks can be switched immediately */
clear_tsk_thread_flag(task, TIF_PATCH_PENDING);



2021-09-22 13:07:28

by Miroslav Benes

[permalink] [raw]
Subject: Re: [RFC][PATCH 5/7] sched,livepatch: Use wake_up_if_idle()

> @@ -405,8 +405,10 @@ void klp_try_complete_transition(void)
> for_each_possible_cpu(cpu) {
> task = idle_task(cpu);
> if (cpu_online(cpu)) {
> - if (!klp_try_switch_task(task))
> - complete = false;
> + int ret = klp_try_switch_task(task);
> + if (ret == -EBUSY)
> + wake_up_if_idle(cpu);
> + complete = !ret;

This is broken. You can basically change "complete" only to false (when it
applies). This could leave some tasks in the old patching state.

Anyway, I like the patch set a lot. It moves our infrastructure to a
proper (I hope so) API and it removes few quirks we have along the way.
I'll play with it some more.

Thanks

Miroslav

2021-09-23 12:21:25

by Petr Mladek

[permalink] [raw]
Subject: Re: [RFC][PATCH 5/7] sched,livepatch: Use wake_up_if_idle()

On Wed 2021-09-22 15:05:03, Miroslav Benes wrote:
> > @@ -405,8 +405,10 @@ void klp_try_complete_transition(void)
> > for_each_possible_cpu(cpu) {
> > task = idle_task(cpu);
> > if (cpu_online(cpu)) {
> > - if (!klp_try_switch_task(task))
> > - complete = false;
> > + int ret = klp_try_switch_task(task);
> > + if (ret == -EBUSY)
> > + wake_up_if_idle(cpu);
> > + complete = !ret;
>
> This is broken. You can basically change "complete" only to false (when it
> applies). This could leave some tasks in the old patching state.

I was a bit confused by Mirek's comment ;-) Anyway, the following works for me:

@@ -406,9 +406,12 @@ void klp_try_complete_transition(void)
task = idle_task(cpu);
if (cpu_online(cpu)) {
int ret = klp_try_switch_task(task);
- if (ret == -EBUSY)
- wake_up_if_idle(cpu);
- complete = !ret;
+ if (ret) {
+ complete = false;
+ /* Make idle task go through the main loop. */
+ if (ret == -EBUSY)
+ wake_up_if_idle(cpu);
+ }
} else if (task->patch_state != klp_target_state) {
/* offline idle tasks can be switched immediately */
clear_tsk_thread_flag(task, TIF_PATCH_PENDING);

Best Regards,
Petr