Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1489297 > unrolled thread

[PATCH] signals: Avoid unnecessary taking of sighand->siglock

Started byWaiman Long <Waiman.Long@hpe.com>
First post2016-09-22 20:30 +0200
Last post2016-09-23 11:40 +0200
Articles 2 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH] signals: Avoid unnecessary taking of sighand->siglock Waiman Long <Waiman.Long@hpe.com> - 2016-09-22 20:30 +0200
    Re: [PATCH] signals: Avoid unnecessary taking of sighand->siglock Oleg Nesterov <oleg@redhat.com> - 2016-09-23 11:40 +0200

#1489297 — [PATCH] signals: Avoid unnecessary taking of sighand->siglock

FromWaiman Long <Waiman.Long@hpe.com>
Date2016-09-22 20:30 +0200
Subject[PATCH] signals: Avoid unnecessary taking of sighand->siglock
Message-ID<skgUW-1qI-17@gated-at.bofh.it>
When running certain database workload on a high-end system with many
CPUs, it was found that spinlock contention in the sigprocmask syscalls
became a significant portion of the overall CPU cycles as shown below.

  9.30%  9.30%  905387  dataserver  /proc/kcore 0x7fff8163f4d2
  [k] _raw_spin_lock_irq
            |
            ---_raw_spin_lock_irq
               |
               |--99.34%-- __set_current_blocked
               |          sigprocmask
               |          sys_rt_sigprocmask
               |          system_call_fastpath
               |          |
               |          |--50.63%-- __swapcontext
               |          |          |
               |          |          |--99.91%-- upsleepgeneric
               |          |
               |          |--49.36%-- __setcontext
               |          |          ktskRun

Looking further into the swapcontext function in glibc, it was found
that the function always call sigprocmask() without checking if there
are changes in the signal mask.

This patch adds a check in the __set_current_blocked() function to
avoid taking the sighand->siglock spinlock if there is no change in
the signal mask. This will prevent unneeded spinlock contention when
many threads are trying to call sigprocmask().

With this patch applied, the spinlock contention in sigprocmask() was
gone.

This patch is currently only active for 64-bit architectures.

Signed-off-by: Waiman Long <Waiman.Long@hpe.com>
---
 kernel/signal.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/kernel/signal.c b/kernel/signal.c
index af21afc..5850b11 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2485,6 +2485,16 @@ void __set_current_blocked(const sigset_t *newset)
 {
 	struct task_struct *tsk = current;
 
+	/*
+	 * In case the signal mask hasn't changed, we won't need to take
+	 * the lock. As the current blocked mask can be modified by other
+	 * CPUs, we need to do an atomic read without lock. In other words,
+	 * this check will only be done on 64-bit systems.
+	 */
+#if _NSIG_WORDS == 1
+	if (READ_ONCE(tsk->blocked.sig[0]) == newset->sig[0])
+		return;
+#endif
 	spin_lock_irq(&tsk->sighand->siglock);
 	__set_task_blocked(tsk, newset);
 	spin_unlock_irq(&tsk->sighand->siglock);
-- 
1.7.1

[toc] | [next] | [standalone]


#1489894

FromOleg Nesterov <oleg@redhat.com>
Date2016-09-23 11:40 +0200
Message-ID<skv7A-1Uk-25@gated-at.bofh.it>
In reply to#1489297
On 09/22, Waiman Long wrote:
>
> This patch is currently only active for 64-bit architectures.

Why?

> --- a/kernel/signal.c
> +++ b/kernel/signal.c
> @@ -2485,6 +2485,16 @@ void __set_current_blocked(const sigset_t *newset)
>  {
>  	struct task_struct *tsk = current;
>  
> +	/*
> +	 * In case the signal mask hasn't changed, we won't need to take
> +	 * the lock. As the current blocked mask can be modified by other
> +	 * CPUs,

No, nobody else should modify current->blocked.

Yes, we need to cleanup the usage of force_sig_info(), and probably remove
the "struct task_struct *t" argument.

> we need to do an atomic read without lock. In other words,
> +	 * this check will only be done on 64-bit systems.
> +	 */
> +#if _NSIG_WORDS == 1
> +	if (READ_ONCE(tsk->blocked.sig[0]) == newset->sig[0])
> +		return;
> +#endif

OK, agreed, but this should not depend on _NSIG_WORDS == 1 and
READ_ONCE() looks confusing. It seems you need to add the new helper
into include/linux/signal.h.

Oleg.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web