public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] signals: Avoid unnecessary taking of sighand->siglock
@ 2016-09-27 12:26 Waiman Long
  2016-09-27 16:17 ` Oleg Nesterov
  2016-10-18 16:12 ` Waiman Long
  0 siblings, 2 replies; 4+ messages in thread
From: Waiman Long @ 2016-09-27 12:26 UTC (permalink / raw)
  To: Andrew Morton, Ingo Molnar, Oleg Nesterov, Thomas Gleixner,
	Stas Sergeev
  Cc: linux-kernel, Scott J Norton, Douglas Hatch, Waiman Long

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.

A check was added to 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.

Signed-off-by: Waiman Long <Waiman.Long@hpe.com>
---
 v2->v3:
   - Add a sigequalsets() helper in signal.h and use it for comparison.

 v1->v2:
   - Fix compiler warning in mips.

 include/linux/signal.h |   17 +++++++++++++++++
 kernel/signal.c        |    7 +++++++
 2 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/include/linux/signal.h b/include/linux/signal.h
index b63f63e..5308304 100644
--- a/include/linux/signal.h
+++ b/include/linux/signal.h
@@ -97,6 +97,23 @@ static inline int sigisemptyset(sigset_t *set)
 	}
 }
 
+static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
+{
+	switch (_NSIG_WORDS) {
+	case 4:
+		return	(set1->sig[3] == set2->sig[3]) &&
+			(set1->sig[2] == set2->sig[2]) &&
+			(set1->sig[1] == set2->sig[1]) &&
+			(set1->sig[0] == set2->sig[0]);
+	case 2:
+		return	(set1->sig[1] == set2->sig[1]) &&
+			(set1->sig[0] == set2->sig[0]);
+	case 1:
+		return	set1->sig[0] == set2->sig[0];
+	}
+	return 0;
+}
+
 #define sigmask(sig)	(1UL << ((sig) - 1))
 
 #ifndef __HAVE_ARCH_SIG_SETOPS
diff --git a/kernel/signal.c b/kernel/signal.c
index af21afc..04e8f50 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2485,6 +2485,13 @@ void __set_current_blocked(const sigset_t *newset)
 {
 	struct task_struct *tsk = current;
 
+	/*
+	 * In case the signal mask hasn't changed, there is nothing we need
+	 * to do. The current->blocked shouldn't be modified by other task.
+	 */
+	if (sigequalsets(&tsk->blocked, newset))
+		return;
+
 	spin_lock_irq(&tsk->sighand->siglock);
 	__set_task_blocked(tsk, newset);
 	spin_unlock_irq(&tsk->sighand->siglock);
-- 
1.7.1

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

* Re: [PATCH v3] signals: Avoid unnecessary taking of sighand->siglock
  2016-09-27 12:26 [PATCH v3] signals: Avoid unnecessary taking of sighand->siglock Waiman Long
@ 2016-09-27 16:17 ` Oleg Nesterov
  2016-09-27 18:21   ` Waiman Long
  2016-10-18 16:12 ` Waiman Long
  1 sibling, 1 reply; 4+ messages in thread
From: Oleg Nesterov @ 2016-09-27 16:17 UTC (permalink / raw)
  To: Waiman Long
  Cc: Andrew Morton, Ingo Molnar, Thomas Gleixner, Stas Sergeev,
	linux-kernel, Scott J Norton, Douglas Hatch

On 09/27, Waiman Long wrote:
>
> +static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
> +{
> +	switch (_NSIG_WORDS) {
> +	case 4:
> +		return	(set1->sig[3] == set2->sig[3]) &&
> +			(set1->sig[2] == set2->sig[2]) &&
> +			(set1->sig[1] == set2->sig[1]) &&
> +			(set1->sig[0] == set2->sig[0]);
> +	case 2:
> +		return	(set1->sig[1] == set2->sig[1]) &&
> +			(set1->sig[0] == set2->sig[0]);
> +	case 1:
> +		return	set1->sig[0] == set2->sig[0];
> +	}
> +	return 0;
> +}
> +

OK, this memcmp-by-hand matches other sig* helpers. Well, perhaps

	default:
		BUILD_BUG();

makes sense too, but I won't insist.

> --- a/kernel/signal.c
> +++ b/kernel/signal.c
> @@ -2485,6 +2485,13 @@ void __set_current_blocked(const sigset_t *newset)
>  {
>  	struct task_struct *tsk = current;
>  
> +	/*
> +	 * In case the signal mask hasn't changed, there is nothing we need
> +	 * to do. The current->blocked shouldn't be modified by other task.
> +	 */
> +	if (sigequalsets(&tsk->blocked, newset))
> +		return;
> +

Acked-by: Oleg Nesterov <oleg@redhat.com>

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

* Re: [PATCH v3] signals: Avoid unnecessary taking of sighand->siglock
  2016-09-27 16:17 ` Oleg Nesterov
@ 2016-09-27 18:21   ` Waiman Long
  0 siblings, 0 replies; 4+ messages in thread
From: Waiman Long @ 2016-09-27 18:21 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Andrew Morton, Ingo Molnar, Thomas Gleixner, Stas Sergeev,
	linux-kernel, Scott J Norton, Douglas Hatch

On 09/27/2016 12:17 PM, Oleg Nesterov wrote:
> On 09/27, Waiman Long wrote:
>> +static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
>> +{
>> +	switch (_NSIG_WORDS) {
>> +	case 4:
>> +		return	(set1->sig[3] == set2->sig[3])&&
>> +			(set1->sig[2] == set2->sig[2])&&
>> +			(set1->sig[1] == set2->sig[1])&&
>> +			(set1->sig[0] == set2->sig[0]);
>> +	case 2:
>> +		return	(set1->sig[1] == set2->sig[1])&&
>> +			(set1->sig[0] == set2->sig[0]);
>> +	case 1:
>> +		return	set1->sig[0] == set2->sig[0];
>> +	}
>> +	return 0;
>> +}
>> +
> OK, this memcmp-by-hand matches other sig* helpers. Well, perhaps
>
> 	default:
> 		BUILD_BUG();
>
> makes sense too, but I won't insist.

We already have a BUILD_BUG() call in sigemptyset(). I don't think we 
need more than one in any given source file. The memcmp() call will be 
more efficient for long byte stream. For short one like sigset_t, direct 
comparison is likely to be faster.

> Acked-by: Oleg Nesterov <oleg@redhat.com> 

Thanks for the review.

Cheers,
Longman

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

* Re: [PATCH v3] signals: Avoid unnecessary taking of sighand->siglock
  2016-09-27 12:26 [PATCH v3] signals: Avoid unnecessary taking of sighand->siglock Waiman Long
  2016-09-27 16:17 ` Oleg Nesterov
@ 2016-10-18 16:12 ` Waiman Long
  1 sibling, 0 replies; 4+ messages in thread
From: Waiman Long @ 2016-10-18 16:12 UTC (permalink / raw)
  To: Waiman Long
  Cc: Andrew Morton, Ingo Molnar, Oleg Nesterov, Thomas Gleixner,
	Stas Sergeev, linux-kernel, Scott J Norton, Douglas Hatch

On 09/27/2016 08:26 AM, Waiman Long wrote:
> 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.
>
> A check was added to 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.
>
> Signed-off-by: Waiman Long<Waiman.Long@hpe.com>
> ---
>   v2->v3:
>     - Add a sigequalsets() helper in signal.h and use it for comparison.
>
>   v1->v2:
>     - Fix compiler warning in mips.
>
>   include/linux/signal.h |   17 +++++++++++++++++
>   kernel/signal.c        |    7 +++++++
>   2 files changed, 24 insertions(+), 0 deletions(-)
>
> diff --git a/include/linux/signal.h b/include/linux/signal.h
> index b63f63e..5308304 100644
> --- a/include/linux/signal.h
> +++ b/include/linux/signal.h
> @@ -97,6 +97,23 @@ static inline int sigisemptyset(sigset_t *set)
>   	}
>   }
>
> +static inline int sigequalsets(const sigset_t *set1, const sigset_t *set2)
> +{
> +	switch (_NSIG_WORDS) {
> +	case 4:
> +		return	(set1->sig[3] == set2->sig[3])&&
> +			(set1->sig[2] == set2->sig[2])&&
> +			(set1->sig[1] == set2->sig[1])&&
> +			(set1->sig[0] == set2->sig[0]);
> +	case 2:
> +		return	(set1->sig[1] == set2->sig[1])&&
> +			(set1->sig[0] == set2->sig[0]);
> +	case 1:
> +		return	set1->sig[0] == set2->sig[0];
> +	}
> +	return 0;
> +}
> +
>   #define sigmask(sig)	(1UL<<  ((sig) - 1))
>
>   #ifndef __HAVE_ARCH_SIG_SETOPS
> diff --git a/kernel/signal.c b/kernel/signal.c
> index af21afc..04e8f50 100644
> --- a/kernel/signal.c
> +++ b/kernel/signal.c
> @@ -2485,6 +2485,13 @@ void __set_current_blocked(const sigset_t *newset)
>   {
>   	struct task_struct *tsk = current;
>
> +	/*
> +	 * In case the signal mask hasn't changed, there is nothing we need
> +	 * to do. The current->blocked shouldn't be modified by other task.
> +	 */
> +	if (sigequalsets(&tsk->blocked, newset))
> +		return;
> +
>   	spin_lock_irq(&tsk->sighand->siglock);
>   	__set_task_blocked(tsk, newset);
>   	spin_unlock_irq(&tsk->sighand->siglock);

This is a pretty simple patch. Is that a chance that it can be pulled 
into 4.10?

Cheers,
Longman

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

end of thread, other threads:[~2016-10-18 16:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-27 12:26 [PATCH v3] signals: Avoid unnecessary taking of sighand->siglock Waiman Long
2016-09-27 16:17 ` Oleg Nesterov
2016-09-27 18:21   ` Waiman Long
2016-10-18 16:12 ` Waiman Long

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