All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] signal: Avoid preempt_disable() in ptrace_stop() on PREEMPT_RT
@ 2023-04-06 20:57 Sebastian Andrzej Siewior
  2023-04-06 20:57 ` [PATCH 1/2] signal: Add proper comment about the preempt-disable in ptrace_stop() Sebastian Andrzej Siewior
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Sebastian Andrzej Siewior @ 2023-04-06 20:57 UTC (permalink / raw)
  To: linux-kernel
  Cc: Eric W. Biederman, Oleg Nesterov, Peter Zijlstra, Thomas Gleixner

Hi,

this mini series updates the comment to properly explain why the
preempt-disable section has been added and then disables this on
PREEMPT_RT explaining why it can not be done.

Sebastian


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

* [PATCH 1/2] signal: Add proper comment about the preempt-disable in ptrace_stop().
  2023-04-06 20:57 [PATCH 0/2] signal: Avoid preempt_disable() in ptrace_stop() on PREEMPT_RT Sebastian Andrzej Siewior
@ 2023-04-06 20:57 ` Sebastian Andrzej Siewior
  2023-04-06 20:57 ` [PATCH 2/2] signal: Don't disable preemption in ptrace_stop() on PREEMPT_RT Sebastian Andrzej Siewior
  2023-05-24 15:37 ` [PATCH 0/2] signal: Avoid preempt_disable() " Sebastian Andrzej Siewior
  2 siblings, 0 replies; 5+ messages in thread
From: Sebastian Andrzej Siewior @ 2023-04-06 20:57 UTC (permalink / raw)
  To: linux-kernel
  Cc: Eric W. Biederman, Oleg Nesterov, Peter Zijlstra,
	Thomas Gleixner, Sebastian Andrzej Siewior

Commit 53da1d9456fe7 ("fix ptrace slowness") added a preempt-disable section
between read_unlock() and the following schedule() invocation without
explaining why it is needed.

Replace the comment with an explanation why this is needed. Clarify that
it is needed for correctness but for performance reasons.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 kernel/signal.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2337,10 +2337,21 @@ static int ptrace_stop(int exit_code, in
 		do_notify_parent_cldstop(current, false, why);
 
 	/*
-	 * Don't want to allow preemption here, because
-	 * sys_ptrace() needs this task to be inactive.
+	 * The previous do_notify_parent_cldstop() invocation woke ptracer.
+	 * One a PREEMPTION kernel this can result in preemption requirement
+	 * which will be fulfilled after read_unlock() and the ptracer will be
+	 * put on the CPU.
+	 * The ptracer is in wait_task_inactive(, __TASK_TRACED) waiting for
+	 * this task wait in schedule(). If this task gets preempted then it
+	 * remains enqueued on the runqueue. The ptracer will observe this and
+	 * then sleep for a delay of one HZ tick. In the meantime this task
+	 * gets scheduled, enters schedule() and will wait for the ptracer.
 	 *
-	 * XXX: implement read_unlock_no_resched().
+	 * This preemption point is not bad from correctness point of view but
+	 * extends the runtime by one HZ tick time due to the ptracer's sleep.
+	 * The preempt-disable section ensures that there will be no preemption
+	 * between unlock and schedule() and so improving the performance since
+	 * the ptracer has no reason to sleep.
 	 */
 	preempt_disable();
 	read_unlock(&tasklist_lock);

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

* [PATCH 2/2] signal: Don't disable preemption in ptrace_stop() on PREEMPT_RT.
  2023-04-06 20:57 [PATCH 0/2] signal: Avoid preempt_disable() in ptrace_stop() on PREEMPT_RT Sebastian Andrzej Siewior
  2023-04-06 20:57 ` [PATCH 1/2] signal: Add proper comment about the preempt-disable in ptrace_stop() Sebastian Andrzej Siewior
@ 2023-04-06 20:57 ` Sebastian Andrzej Siewior
  2023-05-24 15:37 ` [PATCH 0/2] signal: Avoid preempt_disable() " Sebastian Andrzej Siewior
  2 siblings, 0 replies; 5+ messages in thread
From: Sebastian Andrzej Siewior @ 2023-04-06 20:57 UTC (permalink / raw)
  To: linux-kernel
  Cc: Eric W. Biederman, Oleg Nesterov, Peter Zijlstra,
	Thomas Gleixner, Sebastian Andrzej Siewior

On PREEMPT_RT keeping preemption disabled during the invocation of
cgroup_enter_frozen() is a problem because the function acquires css_set_lock
which is a sleeping lock on PREEMPT_RT and must not be acquired with disabled
preemption.
The preempt-disabled section is only for performance optimisation
reasons and can be avoided.

Extend the comment and don't disable preemption before scheduling on
PREEMPT_RT.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 kernel/signal.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2352,11 +2352,16 @@ static int ptrace_stop(int exit_code, in
 	 * The preempt-disable section ensures that there will be no preemption
 	 * between unlock and schedule() and so improving the performance since
 	 * the ptracer has no reason to sleep.
+	 *
+	 * This optimisation is not doable on PREEMPT_RT due to the spinlock_t
+	 * within the preempt-disable section.
 	 */
-	preempt_disable();
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		preempt_disable();
 	read_unlock(&tasklist_lock);
 	cgroup_enter_frozen();
-	preempt_enable_no_resched();
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		preempt_enable_no_resched();
 	schedule();
 	cgroup_leave_frozen(true);
 

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

* Re: [PATCH 0/2] signal: Avoid preempt_disable() in ptrace_stop() on PREEMPT_RT
  2023-04-06 20:57 [PATCH 0/2] signal: Avoid preempt_disable() in ptrace_stop() on PREEMPT_RT Sebastian Andrzej Siewior
  2023-04-06 20:57 ` [PATCH 1/2] signal: Add proper comment about the preempt-disable in ptrace_stop() Sebastian Andrzej Siewior
  2023-04-06 20:57 ` [PATCH 2/2] signal: Don't disable preemption in ptrace_stop() on PREEMPT_RT Sebastian Andrzej Siewior
@ 2023-05-24 15:37 ` Sebastian Andrzej Siewior
  2 siblings, 0 replies; 5+ messages in thread
From: Sebastian Andrzej Siewior @ 2023-05-24 15:37 UTC (permalink / raw)
  To: linux-kernel
  Cc: Eric W. Biederman, Oleg Nesterov, Peter Zijlstra, Thomas Gleixner

On 2023-04-06 22:57:10 [+0200], To linux-kernel@vger.kernel.org wrote:
> Hi,
> 
> this mini series updates the comment to properly explain why the
> preempt-disable section has been added and then disables this on
> PREEMPT_RT explaining why it can not be done.

a polite ping.

Sebastian

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

* [PATCH 2/2] signal: Don't disable preemption in ptrace_stop() on PREEMPT_RT.
  2023-08-03 10:09 [PATCH REPOST v3 " Sebastian Andrzej Siewior
@ 2023-08-03 10:09 ` Sebastian Andrzej Siewior
  0 siblings, 0 replies; 5+ messages in thread
From: Sebastian Andrzej Siewior @ 2023-08-03 10:09 UTC (permalink / raw)
  To: linux-kernel
  Cc: Eric W. Biederman, Oleg Nesterov, Peter Zijlstra,
	Thomas Gleixner, Sebastian Andrzej Siewior

On PREEMPT_RT keeping preemption disabled during the invocation of
cgroup_enter_frozen() is a problem because the function acquires css_set_lock
which is a sleeping lock on PREEMPT_RT and must not be acquired with disabled
preemption.
The preempt-disabled section is only for performance optimisation
reasons and can be avoided.

Extend the comment and don't disable preemption before scheduling on
PREEMPT_RT.

Acked-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 kernel/signal.c |   13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2333,11 +2333,20 @@ static int ptrace_stop(int exit_code, in
 	 * The preempt-disable section ensures that there will be no preemption
 	 * between unlock and schedule() and so improving the performance since
 	 * the ptracer has no reason to sleep.
+	 *
+	 * On PREEMPT_RT locking tasklist_lock does not disable preemption.
+	 * Therefore the task can be preempted (after
+	 * do_notify_parent_cldstop()) before unlocking tasklist_lock so there
+	 * is no benefit in doing this. The optimisation is harmful on
+	 * PEEMPT_RT because the spinlock_t (in cgroup_enter_frozen()) must not
+	 * be acquired with disabled preemption.
 	 */
-	preempt_disable();
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		preempt_disable();
 	read_unlock(&tasklist_lock);
 	cgroup_enter_frozen();
-	preempt_enable_no_resched();
+	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
+		preempt_enable_no_resched();
 	schedule();
 	cgroup_leave_frozen(true);
 

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

end of thread, other threads:[~2023-08-03 10:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 20:57 [PATCH 0/2] signal: Avoid preempt_disable() in ptrace_stop() on PREEMPT_RT Sebastian Andrzej Siewior
2023-04-06 20:57 ` [PATCH 1/2] signal: Add proper comment about the preempt-disable in ptrace_stop() Sebastian Andrzej Siewior
2023-04-06 20:57 ` [PATCH 2/2] signal: Don't disable preemption in ptrace_stop() on PREEMPT_RT Sebastian Andrzej Siewior
2023-05-24 15:37 ` [PATCH 0/2] signal: Avoid preempt_disable() " Sebastian Andrzej Siewior
2023-08-03 10:09 [PATCH REPOST v3 " Sebastian Andrzej Siewior
2023-08-03 10:09 ` [PATCH 2/2] signal: Don't disable preemption " Sebastian Andrzej Siewior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.