Live Patching
 help / color / mirror / Atom feed
* question re klp_transition_work kickoff timeout
@ 2023-03-22 15:41 Alexey Dobriyan
  2023-03-22 16:07 ` Miroslav Benes
  2023-03-23 16:09 ` Petr Mladek
  0 siblings, 2 replies; 7+ messages in thread
From: Alexey Dobriyan @ 2023-03-22 15:41 UTC (permalink / raw)
  To: Josh Poimboeuf; +Cc: live-patching

Hi, Josh.

I've been profiling how much time livepatching takes and I have a question
regarding these lines:

	void klp_try_complete_transition(void)
	{
		...
		if (!complete) {
			schedule_delayed_work(&klp_transition_work, round_jiffies_relative(HZ));
			return;
		}

	}

The problem here is that if the system is idle, then the previous loop
checking idle tasks will reliably sets "complete = false" and then
patching wastes time waiting for next second so that klp_transition_work
will repeat the same code without reentering itself.

I've created trivial patch which changes 2 functions and it takes
~1.3 seconds to complete transition:

	[   33.829506] livepatch: 'main': starting patching transition
	[   35.190721] livepatch: 'main': patching complete

I don't know what's the correct timeout to retry transition work
but it can be made near-instant:

	[  100.930758] livepatch: 'main': starting patching transition
	[  100.956190] livepatch: 'main': patching complete


	Alexey (CloudLinux)


--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -435,8 +435,7 @@ void klp_try_complete_transition(void)
 		 * later and/or wait for other methods like kernel exit
 		 * switching.
 		 */
-		schedule_delayed_work(&klp_transition_work,
-				      round_jiffies_relative(HZ));
+		schedule_delayed_work(&klp_transition_work, msecs_to_jiffies(1));
 		return;
 	}
 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: question re klp_transition_work kickoff timeout
  2023-03-22 15:41 question re klp_transition_work kickoff timeout Alexey Dobriyan
@ 2023-03-22 16:07 ` Miroslav Benes
  2023-03-23  9:52   ` Alexey Dobriyan
  2023-03-23 16:09 ` Petr Mladek
  1 sibling, 1 reply; 7+ messages in thread
From: Miroslav Benes @ 2023-03-22 16:07 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: Josh Poimboeuf, live-patching

Hi,

On Wed, 22 Mar 2023, Alexey Dobriyan wrote:

> Hi, Josh.
> 
> I've been profiling how much time livepatching takes and I have a question
> regarding these lines:
> 
> 	void klp_try_complete_transition(void)
> 	{
> 		...
> 		if (!complete) {
> 			schedule_delayed_work(&klp_transition_work, round_jiffies_relative(HZ));
> 			return;
> 		}
> 
> 	}
> 
> The problem here is that if the system is idle, then the previous loop
> checking idle tasks will reliably sets "complete = false" and then
> patching wastes time waiting for next second so that klp_transition_work
> will repeat the same code without reentering itself.

Only if klp_try_switch_task() cannot switch the idle task right away. We 
then prod it using wake_up_if_idle() and handle it in the next iteration.

So the question might be why klp_try_switch_task() did not succeed in the 
first place.

> I've created trivial patch which changes 2 functions and it takes
> ~1.3 seconds to complete transition:
> 
> 	[   33.829506] livepatch: 'main': starting patching transition
> 	[   35.190721] livepatch: 'main': patching complete
> 
> I don't know what's the correct timeout to retry transition work
> but it can be made near-instant:
> 
> 	[  100.930758] livepatch: 'main': starting patching transition
> 	[  100.956190] livepatch: 'main': patching complete

There is really no correct timeout. The application of a live patch is 
a lazy slow path. We generally do not care when it is finished unless it 
is stuck for some reason or takes really long.

What is your motivation in measuring it?

Regards
Miroslav

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: question re klp_transition_work kickoff timeout
  2023-03-22 16:07 ` Miroslav Benes
@ 2023-03-23  9:52   ` Alexey Dobriyan
  2023-03-23 15:15     ` Miroslav Benes
  0 siblings, 1 reply; 7+ messages in thread
From: Alexey Dobriyan @ 2023-03-23  9:52 UTC (permalink / raw)
  To: Miroslav Benes; +Cc: Josh Poimboeuf, live-patching

On Wed, Mar 22, 2023 at 05:07:09PM +0100, Miroslav Benes wrote:
> On Wed, 22 Mar 2023, Alexey Dobriyan wrote:
> 
> > Hi, Josh.
> > 
> > I've been profiling how much time livepatching takes and I have a question
> > regarding these lines:
> > 
> > 	void klp_try_complete_transition(void)
> > 	{
> > 		...
> > 		if (!complete) {
> > 			schedule_delayed_work(&klp_transition_work, round_jiffies_relative(HZ));
> > 			return;
> > 		}
> > 
> > 	}
> > 
> > The problem here is that if the system is idle, then the previous loop
> > checking idle tasks will reliably sets "complete = false" and then
> > patching wastes time waiting for next second so that klp_transition_work
> > will repeat the same code without reentering itself.
> 
> Only if klp_try_switch_task() cannot switch the idle task right away. We 
> then prod it using wake_up_if_idle() and handle it in the next iteration.
> 
> So the question might be why klp_try_switch_task() did not succeed in the 
> first place.

Yes, sort of. Transitioning never happens on the first try becase of idle
tasks (see debugging patch below). But it should happen immediately
because machine is idle!

> > I've created trivial patch which changes 2 functions and it takes
> > ~1.3 seconds to complete transition:
> > 
> > 	[   33.829506] livepatch: 'main': starting patching transition
> > 	[   35.190721] livepatch: 'main': patching complete
> > 
> > I don't know what's the correct timeout to retry transition work
> > but it can be made near-instant:
> > 
> > 	[  100.930758] livepatch: 'main': starting patching transition
> > 	[  100.956190] livepatch: 'main': patching complete
> 
> There is really no correct timeout. The application of a live patch is 
> a lazy slow path. We generally do not care when it is finished unless it 
> is stuck for some reason or takes really long.
> 
> What is your motivation in measuring it?

Just measuring... :-)

	main: tainting kernel with TAINT_LIVEPATCH
	livepatch: enabling patch 'main'
	livepatch: 'main': starting patching transition
	livepatch: XXX 002 klp_try_switch_task 'swapper/0'
		...
	livepatch: XXX 002 klp_try_switch_task 'swapper/31'
	livepatch: XXX nr_a 0, nr_b 30
	livepatch: 'main': patching complete

--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -386,7 +386,8 @@ void klp_try_complete_transition(void)
 	unsigned int cpu;
 	struct task_struct *g, *task;
 	struct klp_patch *patch;
-	bool complete = true;
+	unsigned int nr_a = 0;
+	unsigned int nr_b = 0;
 
 	WARN_ON_ONCE(klp_target_state == KLP_UNDEFINED);
 
@@ -401,8 +402,10 @@ void klp_try_complete_transition(void)
 	 */
 	read_lock(&tasklist_lock);
 	for_each_process_thread(g, task)
-		if (!klp_try_switch_task(task))
-			complete = false;
+		if (!klp_try_switch_task(task)) {
+			pr_err("XXX 001 klp_try_switch_task '%s'\n", task->comm);
+			nr_a += 1;
+		}
 	read_unlock(&tasklist_lock);
 
 	/*
@@ -413,7 +416,8 @@ void klp_try_complete_transition(void)
 		task = idle_task(cpu);
 		if (cpu_online(cpu)) {
 			if (!klp_try_switch_task(task)) {
-				complete = false;
+				pr_err("XXX 002 klp_try_switch_task '%s'\n", task->comm);
+				nr_b += 1;
 				/* Make idle task go through the main loop. */
 				wake_up_if_idle(cpu);
 			}
@@ -425,7 +429,9 @@ void klp_try_complete_transition(void)
 	}
 	cpus_read_unlock();
 
-	if (!complete) {
+	if (nr_a > 0 || nr_b > 0) {
+		pr_err("XXX nr_a %u, nr_b %d\n", nr_a, nr_b);
+
 		if (klp_signals_cnt && !(klp_signals_cnt % SIGNALS_TIMEOUT))
 			klp_send_signals();
 		klp_signals_cnt++;

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: question re klp_transition_work kickoff timeout
  2023-03-23  9:52   ` Alexey Dobriyan
@ 2023-03-23 15:15     ` Miroslav Benes
  0 siblings, 0 replies; 7+ messages in thread
From: Miroslav Benes @ 2023-03-23 15:15 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: Josh Poimboeuf, live-patching

On Thu, 23 Mar 2023, Alexey Dobriyan wrote:

> On Wed, Mar 22, 2023 at 05:07:09PM +0100, Miroslav Benes wrote:
> > On Wed, 22 Mar 2023, Alexey Dobriyan wrote:
> > 
> > > Hi, Josh.
> > > 
> > > I've been profiling how much time livepatching takes and I have a question
> > > regarding these lines:
> > > 
> > > 	void klp_try_complete_transition(void)
> > > 	{
> > > 		...
> > > 		if (!complete) {
> > > 			schedule_delayed_work(&klp_transition_work, round_jiffies_relative(HZ));
> > > 			return;
> > > 		}
> > > 
> > > 	}
> > > 
> > > The problem here is that if the system is idle, then the previous loop
> > > checking idle tasks will reliably sets "complete = false" and then
> > > patching wastes time waiting for next second so that klp_transition_work
> > > will repeat the same code without reentering itself.
> > 
> > Only if klp_try_switch_task() cannot switch the idle task right away. We 
> > then prod it using wake_up_if_idle() and handle it in the next iteration.
> > 
> > So the question might be why klp_try_switch_task() did not succeed in the 
> > first place.
> 
> Yes, sort of. Transitioning never happens on the first try becase of idle
> tasks (see debugging patch below). But it should happen immediately
> because machine is idle!

Yes. My guess is that swapper task is running and cannot be switched 
through klp_try_switch_task() so we prod it and it migrates itself through 
our hook in do_idle().

You can enable a dynamic debug and see if it helps to confirm it

# echo "file kernel/livepatch/* +p" > /sys/kernel/debug/dynamic_debug/control

Miroslav

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: question re klp_transition_work kickoff timeout
  2023-03-22 15:41 question re klp_transition_work kickoff timeout Alexey Dobriyan
  2023-03-22 16:07 ` Miroslav Benes
@ 2023-03-23 16:09 ` Petr Mladek
  2023-03-23 16:22   ` Petr Mladek
  1 sibling, 1 reply; 7+ messages in thread
From: Petr Mladek @ 2023-03-23 16:09 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: Josh Poimboeuf, live-patching

On Wed 2023-03-22 18:41:40, Alexey Dobriyan wrote:
> Hi, Josh.
> 
> I've been profiling how much time livepatching takes and I have a question
> regarding these lines:
> 
> 	void klp_try_complete_transition(void)
> 	{
> 		...
> 		if (!complete) {
> 			schedule_delayed_work(&klp_transition_work, round_jiffies_relative(HZ));
> 			return;
> 		}
> 
> 	}
> 
> The problem here is that if the system is idle, then the previous loop
> checking idle tasks will reliably sets "complete = false" and then
> patching wastes time waiting for next second so that klp_transition_work
> will repeat the same code without reentering itself.
> 
> I've created trivial patch which changes 2 functions and it takes
> ~1.3 seconds to complete transition:
> 
> 	[   33.829506] livepatch: 'main': starting patching transition
> 	[   35.190721] livepatch: 'main': patching complete
> 
> I don't know what's the correct timeout to retry transition work
> but it can be made near-instant:
> 
> 	[  100.930758] livepatch: 'main': starting patching transition
> 	[  100.956190] livepatch: 'main': patching complete
> 
> 
> 	Alexey (CloudLinux)
> 
> 
> --- a/kernel/livepatch/transition.c
> +++ b/kernel/livepatch/transition.c
> @@ -435,8 +435,7 @@ void klp_try_complete_transition(void)
>  		 * later and/or wait for other methods like kernel exit
>  		 * switching.
>  		 */
> -		schedule_delayed_work(&klp_transition_work,
> -				      round_jiffies_relative(HZ));
> +		schedule_delayed_work(&klp_transition_work, msecs_to_jiffies(1));

Note that this affects all iterations, not only the first one. In
practice, it might schedule another klp_transition_work() almost
immediately. Servers typically use HZ = 250 and 1ms is less than 1 jiffy.

Now, klp_try_complete_transition() takes tasklist_lock and blocks
forking and exiting. It sounds scary to do this so frequently.
Is am actually surprised that nobody reported any problems
even with the 1sec period.

Please, stop these micro-optimizations!

Livepatching is about security and reliability. It is not about speed!

The fact that the transition is so lazy is a great feature.
Livepatching should make the system more safe without breaking it.
It is great that it could run in background and do not limit
the normal workload too much.

Best Regards,
Petr

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: question re klp_transition_work kickoff timeout
  2023-03-23 16:09 ` Petr Mladek
@ 2023-03-23 16:22   ` Petr Mladek
  2023-03-23 16:31     ` Alexey Dobriyan
  0 siblings, 1 reply; 7+ messages in thread
From: Petr Mladek @ 2023-03-23 16:22 UTC (permalink / raw)
  To: Alexey Dobriyan; +Cc: Josh Poimboeuf, live-patching

On Thu 2023-03-23 17:09:13, Petr Mladek wrote:
> On Wed 2023-03-22 18:41:40, Alexey Dobriyan wrote:
> > Hi, Josh.
> > 
> > I've been profiling how much time livepatching takes and I have a question
> > regarding these lines:
> > 
> > 	void klp_try_complete_transition(void)
> > 	{
> > 		...
> > 		if (!complete) {
> > 			schedule_delayed_work(&klp_transition_work, round_jiffies_relative(HZ));
> > 			return;
> > 		}
> > 
> > 	}
> > 
> > The problem here is that if the system is idle, then the previous loop
> > checking idle tasks will reliably sets "complete = false" and then
> > patching wastes time waiting for next second so that klp_transition_work
> > will repeat the same code without reentering itself.
> > 
> > I've created trivial patch which changes 2 functions and it takes
> > ~1.3 seconds to complete transition:
> > 
> > 	[   33.829506] livepatch: 'main': starting patching transition
> > 	[   35.190721] livepatch: 'main': patching complete
> > 
> > I don't know what's the correct timeout to retry transition work
> > but it can be made near-instant:
> > 
> > 	[  100.930758] livepatch: 'main': starting patching transition
> > 	[  100.956190] livepatch: 'main': patching complete
> > 
> > 
> > 	Alexey (CloudLinux)
> > 
> > 
> > --- a/kernel/livepatch/transition.c
> > +++ b/kernel/livepatch/transition.c
> > @@ -435,8 +435,7 @@ void klp_try_complete_transition(void)
> >  		 * later and/or wait for other methods like kernel exit
> >  		 * switching.
> >  		 */
> > -		schedule_delayed_work(&klp_transition_work,
> > -				      round_jiffies_relative(HZ));
> > +		schedule_delayed_work(&klp_transition_work, msecs_to_jiffies(1));
> 
> Note that this affects all iterations, not only the first one. In
> practice, it might schedule another klp_transition_work() almost
> immediately. Servers typically use HZ = 250 and 1ms is less than 1 jiffy.
> 
> Now, klp_try_complete_transition() takes tasklist_lock and blocks
> forking and exiting. It sounds scary to do this so frequently.
> Is am actually surprised that nobody reported any problems
> even with the 1sec period.
> 
> Please, stop these micro-optimizations!
> 
> Livepatching is about security and reliability. It is not about speed!
> 
> The fact that the transition is so lazy is a great feature.
> Livepatching should make the system more safe without breaking it.
> It is great that it could run in background and do not limit
> the normal workload too much.

By other words. It might be interesting to find a way how to migrate
the idle tasks during the first klp_try_complete_transition() call.
As a result the 2nd call would not be needed and it would make
livepatching even more lightweight.

But just increasing the frequency of klp_try_complete_transition()
is not the way to go.

Best Regards,
Petr

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: question re klp_transition_work kickoff timeout
  2023-03-23 16:22   ` Petr Mladek
@ 2023-03-23 16:31     ` Alexey Dobriyan
  0 siblings, 0 replies; 7+ messages in thread
From: Alexey Dobriyan @ 2023-03-23 16:31 UTC (permalink / raw)
  To: Petr Mladek; +Cc: Josh Poimboeuf, live-patching

On Thu, Mar 23, 2023 at 05:22:09PM +0100, Petr Mladek wrote:
> On Thu 2023-03-23 17:09:13, Petr Mladek wrote:
> > On Wed 2023-03-22 18:41:40, Alexey Dobriyan wrote:
> > > Hi, Josh.
> > > 
> > > I've been profiling how much time livepatching takes and I have a question
> > > regarding these lines:
> > > 
> > > 	void klp_try_complete_transition(void)
> > > 	{
> > > 		...
> > > 		if (!complete) {
> > > 			schedule_delayed_work(&klp_transition_work, round_jiffies_relative(HZ));
> > > 			return;
> > > 		}
> > > 
> > > 	}
> > > 
> > > The problem here is that if the system is idle, then the previous loop
> > > checking idle tasks will reliably sets "complete = false" and then
> > > patching wastes time waiting for next second so that klp_transition_work
> > > will repeat the same code without reentering itself.
> > > 
> > > I've created trivial patch which changes 2 functions and it takes
> > > ~1.3 seconds to complete transition:
> > > 
> > > 	[   33.829506] livepatch: 'main': starting patching transition
> > > 	[   35.190721] livepatch: 'main': patching complete
> > > 
> > > I don't know what's the correct timeout to retry transition work
> > > but it can be made near-instant:
> > > 
> > > 	[  100.930758] livepatch: 'main': starting patching transition
> > > 	[  100.956190] livepatch: 'main': patching complete
> > > 
> > > 
> > > 	Alexey (CloudLinux)
> > > 
> > > 
> > > --- a/kernel/livepatch/transition.c
> > > +++ b/kernel/livepatch/transition.c
> > > @@ -435,8 +435,7 @@ void klp_try_complete_transition(void)
> > >  		 * later and/or wait for other methods like kernel exit
> > >  		 * switching.
> > >  		 */
> > > -		schedule_delayed_work(&klp_transition_work,
> > > -				      round_jiffies_relative(HZ));
> > > +		schedule_delayed_work(&klp_transition_work, msecs_to_jiffies(1));
> > 
> > Note that this affects all iterations, not only the first one. In
> > practice, it might schedule another klp_transition_work() almost
> > immediately. Servers typically use HZ = 250 and 1ms is less than 1 jiffy.
> > 
> > Now, klp_try_complete_transition() takes tasklist_lock and blocks
> > forking and exiting. It sounds scary to do this so frequently.
> > Is am actually surprised that nobody reported any problems
> > even with the 1sec period.
> > 
> > Please, stop these micro-optimizations!
> > 
> > Livepatching is about security and reliability. It is not about speed!
> > 
> > The fact that the transition is so lazy is a great feature.
> > Livepatching should make the system more safe without breaking it.
> > It is great that it could run in background and do not limit
> > the normal workload too much.
> 
> By other words. It might be interesting to find a way how to migrate
> the idle tasks during the first klp_try_complete_transition() call.
> As a result the 2nd call would not be needed and it would make
> livepatching even more lightweight.
> 
> But just increasing the frequency of klp_try_complete_transition()
> is not the way to go.

1ms is for demo purposes, to show how fast this thing can go.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-03-23 16:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-22 15:41 question re klp_transition_work kickoff timeout Alexey Dobriyan
2023-03-22 16:07 ` Miroslav Benes
2023-03-23  9:52   ` Alexey Dobriyan
2023-03-23 15:15     ` Miroslav Benes
2023-03-23 16:09 ` Petr Mladek
2023-03-23 16:22   ` Petr Mladek
2023-03-23 16:31     ` Alexey Dobriyan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox