License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 17:07:57 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Implement CPU time clocks for the POSIX clock interface.
|
|
|
|
*/
|
|
|
|
|
2017-02-08 20:51:30 +03:00
|
|
|
#include <linux/sched/signal.h>
|
2017-02-05 13:48:36 +03:00
|
|
|
#include <linux/sched/cputime.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
#include <linux/posix-timers.h>
|
|
|
|
#include <linux/errno.h>
|
2008-05-01 15:34:31 +04:00
|
|
|
#include <linux/math64.h>
|
2016-12-24 22:46:01 +03:00
|
|
|
#include <linux/uaccess.h>
|
2008-09-12 20:54:39 +04:00
|
|
|
#include <linux/kernel_stat.h>
|
2009-08-10 06:52:30 +04:00
|
|
|
#include <trace/events/timer.h>
|
2013-04-18 03:31:13 +04:00
|
|
|
#include <linux/tick.h>
|
|
|
|
#include <linux/workqueue.h>
|
2017-06-07 11:42:31 +03:00
|
|
|
#include <linux/compat.h>
|
2017-12-12 14:10:24 +03:00
|
|
|
#include <linux/sched/deadline.h>
|
2022-01-28 22:55:47 +03:00
|
|
|
#include <linux/task_work.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2017-05-31 00:15:41 +03:00
|
|
|
#include "posix-timers.h"
|
|
|
|
|
2017-05-31 00:15:47 +03:00
|
|
|
static void posix_cpu_timer_rearm(struct k_itimer *timer);
|
|
|
|
|
2019-08-21 22:09:06 +03:00
|
|
|
void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit)
|
|
|
|
{
|
|
|
|
posix_cputimers_init(pct);
|
2019-08-21 22:09:24 +03:00
|
|
|
if (cpu_limit != RLIM_INFINITY) {
|
2019-08-26 21:22:24 +03:00
|
|
|
pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC;
|
2019-08-21 22:09:24 +03:00
|
|
|
pct->timers_active = true;
|
|
|
|
}
|
2019-08-21 22:09:06 +03:00
|
|
|
}
|
|
|
|
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
/*
|
2010-03-12 01:04:37 +03:00
|
|
|
* Called after updating RLIMIT_CPU to run cpu timer and update
|
2019-08-26 21:22:24 +03:00
|
|
|
* tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if
|
|
|
|
* necessary. Needs siglock protection since other code may update the
|
2019-08-21 22:09:06 +03:00
|
|
|
* expiration cache as well.
|
prlimit: do not grab the tasklist_lock
Unnecessarily grabbing the tasklist_lock can be a scalability bottleneck
for workloads that also must grab the tasklist_lock for waiting,
killing, and cloning.
The tasklist_lock was grabbed to protect tsk->sighand from disappearing
(becoming NULL). tsk->signal was already protected by holding a
reference to tsk.
update_rlimit_cpu() assumed tsk->sighand != NULL. With this commit, it
attempts to lock_task_sighand(). However, this means that
update_rlimit_cpu() can fail. This only happens when a task is exiting.
Note that during exec, sighand may *change*, but it will not be NULL.
Prior to this commit, the do_prlimit() ensured that update_rlimit_cpu()
would not fail by read locking the tasklist_lock and checking tsk->sighand
!= NULL.
If update_rlimit_cpu() fails, there may be other tasks that are not
exiting that share tsk->signal. However, the group_leader is the last
task to be released, so if we cannot update_rlimit_cpu(group_leader),
then the entire process is exiting.
The only other caller of update_rlimit_cpu() is
selinux_bprm_committing_creds(). It has tsk == current, so
update_rlimit_cpu() cannot fail (current->sighand cannot disappear
until current exits).
This change resulted in a 14% speedup on a microbenchmark where parents
kill and wait on their children, and children getpriority, setpriority,
and getrlimit.
Signed-off-by: Barret Rhoden <brho@google.com>
Link: https://lkml.kernel.org/r/20220106172041.522167-4-brho@google.com
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
2022-01-06 20:20:41 +03:00
|
|
|
*
|
|
|
|
* Returns 0 on success, -ESRCH on failure. Can fail if the task is exiting and
|
|
|
|
* we cannot lock_task_sighand. Cannot fail if task is current.
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
*/
|
prlimit: do not grab the tasklist_lock
Unnecessarily grabbing the tasklist_lock can be a scalability bottleneck
for workloads that also must grab the tasklist_lock for waiting,
killing, and cloning.
The tasklist_lock was grabbed to protect tsk->sighand from disappearing
(becoming NULL). tsk->signal was already protected by holding a
reference to tsk.
update_rlimit_cpu() assumed tsk->sighand != NULL. With this commit, it
attempts to lock_task_sighand(). However, this means that
update_rlimit_cpu() can fail. This only happens when a task is exiting.
Note that during exec, sighand may *change*, but it will not be NULL.
Prior to this commit, the do_prlimit() ensured that update_rlimit_cpu()
would not fail by read locking the tasklist_lock and checking tsk->sighand
!= NULL.
If update_rlimit_cpu() fails, there may be other tasks that are not
exiting that share tsk->signal. However, the group_leader is the last
task to be released, so if we cannot update_rlimit_cpu(group_leader),
then the entire process is exiting.
The only other caller of update_rlimit_cpu() is
selinux_bprm_committing_creds(). It has tsk == current, so
update_rlimit_cpu() cannot fail (current->sighand cannot disappear
until current exits).
This change resulted in a 14% speedup on a microbenchmark where parents
kill and wait on their children, and children getpriority, setpriority,
and getrlimit.
Signed-off-by: Barret Rhoden <brho@google.com>
Link: https://lkml.kernel.org/r/20220106172041.522167-4-brho@google.com
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
2022-01-06 20:20:41 +03:00
|
|
|
int update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
{
|
2017-01-31 06:09:35 +03:00
|
|
|
u64 nsecs = rlim_new * NSEC_PER_SEC;
|
prlimit: do not grab the tasklist_lock
Unnecessarily grabbing the tasklist_lock can be a scalability bottleneck
for workloads that also must grab the tasklist_lock for waiting,
killing, and cloning.
The tasklist_lock was grabbed to protect tsk->sighand from disappearing
(becoming NULL). tsk->signal was already protected by holding a
reference to tsk.
update_rlimit_cpu() assumed tsk->sighand != NULL. With this commit, it
attempts to lock_task_sighand(). However, this means that
update_rlimit_cpu() can fail. This only happens when a task is exiting.
Note that during exec, sighand may *change*, but it will not be NULL.
Prior to this commit, the do_prlimit() ensured that update_rlimit_cpu()
would not fail by read locking the tasklist_lock and checking tsk->sighand
!= NULL.
If update_rlimit_cpu() fails, there may be other tasks that are not
exiting that share tsk->signal. However, the group_leader is the last
task to be released, so if we cannot update_rlimit_cpu(group_leader),
then the entire process is exiting.
The only other caller of update_rlimit_cpu() is
selinux_bprm_committing_creds(). It has tsk == current, so
update_rlimit_cpu() cannot fail (current->sighand cannot disappear
until current exits).
This change resulted in a 14% speedup on a microbenchmark where parents
kill and wait on their children, and children getpriority, setpriority,
and getrlimit.
Signed-off-by: Barret Rhoden <brho@google.com>
Link: https://lkml.kernel.org/r/20220106172041.522167-4-brho@google.com
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
2022-01-06 20:20:41 +03:00
|
|
|
unsigned long irq_fl;
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
|
prlimit: do not grab the tasklist_lock
Unnecessarily grabbing the tasklist_lock can be a scalability bottleneck
for workloads that also must grab the tasklist_lock for waiting,
killing, and cloning.
The tasklist_lock was grabbed to protect tsk->sighand from disappearing
(becoming NULL). tsk->signal was already protected by holding a
reference to tsk.
update_rlimit_cpu() assumed tsk->sighand != NULL. With this commit, it
attempts to lock_task_sighand(). However, this means that
update_rlimit_cpu() can fail. This only happens when a task is exiting.
Note that during exec, sighand may *change*, but it will not be NULL.
Prior to this commit, the do_prlimit() ensured that update_rlimit_cpu()
would not fail by read locking the tasklist_lock and checking tsk->sighand
!= NULL.
If update_rlimit_cpu() fails, there may be other tasks that are not
exiting that share tsk->signal. However, the group_leader is the last
task to be released, so if we cannot update_rlimit_cpu(group_leader),
then the entire process is exiting.
The only other caller of update_rlimit_cpu() is
selinux_bprm_committing_creds(). It has tsk == current, so
update_rlimit_cpu() cannot fail (current->sighand cannot disappear
until current exits).
This change resulted in a 14% speedup on a microbenchmark where parents
kill and wait on their children, and children getpriority, setpriority,
and getrlimit.
Signed-off-by: Barret Rhoden <brho@google.com>
Link: https://lkml.kernel.org/r/20220106172041.522167-4-brho@google.com
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
2022-01-06 20:20:41 +03:00
|
|
|
if (!lock_task_sighand(task, &irq_fl))
|
|
|
|
return -ESRCH;
|
2017-01-31 06:09:35 +03:00
|
|
|
set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, NULL);
|
prlimit: do not grab the tasklist_lock
Unnecessarily grabbing the tasklist_lock can be a scalability bottleneck
for workloads that also must grab the tasklist_lock for waiting,
killing, and cloning.
The tasklist_lock was grabbed to protect tsk->sighand from disappearing
(becoming NULL). tsk->signal was already protected by holding a
reference to tsk.
update_rlimit_cpu() assumed tsk->sighand != NULL. With this commit, it
attempts to lock_task_sighand(). However, this means that
update_rlimit_cpu() can fail. This only happens when a task is exiting.
Note that during exec, sighand may *change*, but it will not be NULL.
Prior to this commit, the do_prlimit() ensured that update_rlimit_cpu()
would not fail by read locking the tasklist_lock and checking tsk->sighand
!= NULL.
If update_rlimit_cpu() fails, there may be other tasks that are not
exiting that share tsk->signal. However, the group_leader is the last
task to be released, so if we cannot update_rlimit_cpu(group_leader),
then the entire process is exiting.
The only other caller of update_rlimit_cpu() is
selinux_bprm_committing_creds(). It has tsk == current, so
update_rlimit_cpu() cannot fail (current->sighand cannot disappear
until current exits).
This change resulted in a 14% speedup on a microbenchmark where parents
kill and wait on their children, and children getpriority, setpriority,
and getrlimit.
Signed-off-by: Barret Rhoden <brho@google.com>
Link: https://lkml.kernel.org/r/20220106172041.522167-4-brho@google.com
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
2022-01-06 20:20:41 +03:00
|
|
|
unlock_task_sighand(task, &irq_fl);
|
|
|
|
return 0;
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:08:48 +03:00
|
|
|
/*
|
|
|
|
* Functions for validating access to tasks.
|
|
|
|
*/
|
2020-04-27 15:55:07 +03:00
|
|
|
static struct pid *pid_for_clock(const clockid_t clock, bool gettime)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2020-04-27 15:55:07 +03:00
|
|
|
const bool thread = !!CPUCLOCK_PERTHREAD(clock);
|
|
|
|
const pid_t upid = CPUCLOCK_PID(clock);
|
|
|
|
struct pid *pid;
|
|
|
|
|
|
|
|
if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
|
|
|
|
return NULL;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-09-06 00:15:08 +03:00
|
|
|
/*
|
|
|
|
* If the encoded PID is 0, then the timer is targeted at current
|
|
|
|
* or the process to which current belongs.
|
|
|
|
*/
|
2020-04-27 15:55:07 +03:00
|
|
|
if (upid == 0)
|
|
|
|
return thread ? task_pid(current) : task_tgid(current);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2020-04-27 15:55:07 +03:00
|
|
|
pid = find_vpid(upid);
|
|
|
|
if (!pid)
|
|
|
|
return NULL;
|
2019-09-06 00:15:08 +03:00
|
|
|
|
2020-04-27 15:55:07 +03:00
|
|
|
if (thread) {
|
|
|
|
struct task_struct *tsk = pid_task(pid, PIDTYPE_PID);
|
|
|
|
return (tsk && same_thread_group(tsk, current)) ? pid : NULL;
|
|
|
|
}
|
2019-09-06 00:15:08 +03:00
|
|
|
|
2020-04-28 21:00:39 +03:00
|
|
|
/*
|
2020-04-27 15:55:07 +03:00
|
|
|
* For clock_gettime(PROCESS) allow finding the process by
|
|
|
|
* with the pid of the current task. The code needs the tgid
|
|
|
|
* of the process so that pid_task(pid, PIDTYPE_TGID) can be
|
|
|
|
* used to find the process.
|
2020-04-28 21:00:39 +03:00
|
|
|
*/
|
2020-04-27 15:55:07 +03:00
|
|
|
if (gettime && (pid == task_pid(current)))
|
|
|
|
return task_tgid(current);
|
2019-09-06 00:15:08 +03:00
|
|
|
|
|
|
|
/*
|
2020-04-27 15:55:07 +03:00
|
|
|
* For processes require that pid identifies a process.
|
2019-09-06 00:15:08 +03:00
|
|
|
*/
|
2020-04-27 15:55:07 +03:00
|
|
|
return pid_has_task(pid, PIDTYPE_TGID) ? pid : NULL;
|
2019-08-21 22:08:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int validate_clock_permissions(const clockid_t clock)
|
|
|
|
{
|
2020-04-26 02:38:54 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2020-04-27 15:55:07 +03:00
|
|
|
ret = pid_for_clock(clock, false) ? 0 : -EINVAL;
|
2020-04-26 02:38:54 +03:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return ret;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2020-04-27 17:38:29 +03:00
|
|
|
static inline enum pid_type clock_pid_type(const clockid_t clock)
|
2020-02-28 20:11:06 +03:00
|
|
|
{
|
2020-04-27 17:38:29 +03:00
|
|
|
return CPUCLOCK_PERTHREAD(clock) ? PIDTYPE_PID : PIDTYPE_TGID;
|
2020-02-28 20:11:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct task_struct *cpu_timer_task_rcu(struct k_itimer *timer)
|
|
|
|
{
|
2020-04-27 17:38:29 +03:00
|
|
|
return pid_task(timer->it.cpu.pid, clock_pid_type(timer->it_clock));
|
2020-02-28 20:11:06 +03:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Update expiry time from increment, and increase overrun count,
|
|
|
|
* given the current clock sample.
|
|
|
|
*/
|
2019-08-27 22:31:02 +03:00
|
|
|
static u64 bump_cpu_timer(struct k_itimer *timer, u64 now)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-27 22:31:02 +03:00
|
|
|
u64 delta, incr, expires = timer->it.cpu.node.expires;
|
2005-04-17 02:20:36 +04:00
|
|
|
int i;
|
|
|
|
|
2019-01-11 16:33:17 +03:00
|
|
|
if (!timer->it_interval)
|
2019-08-27 22:31:02 +03:00
|
|
|
return expires;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
if (now < expires)
|
|
|
|
return expires;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-01-11 16:33:17 +03:00
|
|
|
incr = timer->it_interval;
|
2019-08-27 22:31:02 +03:00
|
|
|
delta = now + incr - expires;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
/* Don't use (incr*2 < delta), incr*2 might overflow. */
|
|
|
|
for (i = 0; incr < delta - incr; i++)
|
|
|
|
incr = incr << 1;
|
|
|
|
|
|
|
|
for (; i >= 0; incr >>= 1, i--) {
|
|
|
|
if (delta < incr)
|
|
|
|
continue;
|
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
timer->it.cpu.node.expires += incr;
|
2018-06-26 16:21:32 +03:00
|
|
|
timer->it_overrun += 1LL << i;
|
2013-06-28 04:06:42 +04:00
|
|
|
delta -= incr;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2019-08-27 22:31:02 +03:00
|
|
|
return timer->it.cpu.node.expires;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:09:19 +03:00
|
|
|
/* Check whether all cache entries contain U64_MAX, i.e. eternal expiry time */
|
|
|
|
static inline bool expiry_cache_is_inactive(const struct posix_cputimers *pct)
|
2013-04-19 18:17:38 +04:00
|
|
|
{
|
2019-08-21 22:09:19 +03:00
|
|
|
return !(~pct->bases[CPUCLOCK_PROF].nextevt |
|
|
|
|
~pct->bases[CPUCLOCK_VIRT].nextevt |
|
|
|
|
~pct->bases[CPUCLOCK_SCHED].nextevt);
|
2013-04-19 18:17:38 +04:00
|
|
|
}
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static int
|
2017-03-26 22:04:15 +03:00
|
|
|
posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-21 22:08:48 +03:00
|
|
|
int error = validate_clock_permissions(which_clock);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
if (!error) {
|
|
|
|
tp->tv_sec = 0;
|
|
|
|
tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
|
|
|
|
if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
|
|
|
|
/*
|
|
|
|
* If sched_clock is using a cycle counter, we
|
|
|
|
* don't have any idea of its true resolution
|
|
|
|
* exported, but it is much more than 1s/HZ.
|
|
|
|
*/
|
|
|
|
tp->tv_nsec = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static int
|
2019-08-21 22:08:48 +03:00
|
|
|
posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-21 22:08:48 +03:00
|
|
|
int error = validate_clock_permissions(clock);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* You can never reset a CPU clock, but we check for other errors
|
|
|
|
* in the call before failing with EPERM.
|
|
|
|
*/
|
2019-08-21 22:08:48 +03:00
|
|
|
return error ? : -EPERM;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-08-21 22:09:00 +03:00
|
|
|
* Sample a per-thread clock for the given task. clkid is validated.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2019-08-21 22:09:01 +03:00
|
|
|
static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-21 22:09:03 +03:00
|
|
|
u64 utime, stime;
|
|
|
|
|
|
|
|
if (clkid == CPUCLOCK_SCHED)
|
|
|
|
return task_sched_runtime(p);
|
|
|
|
|
|
|
|
task_cputime(p, &utime, &stime);
|
|
|
|
|
2019-08-21 22:09:00 +03:00
|
|
|
switch (clkid) {
|
2005-04-17 02:20:36 +04:00
|
|
|
case CPUCLOCK_PROF:
|
2019-08-21 22:09:03 +03:00
|
|
|
return utime + stime;
|
2005-04-17 02:20:36 +04:00
|
|
|
case CPUCLOCK_VIRT:
|
2019-08-21 22:09:03 +03:00
|
|
|
return utime;
|
2019-08-21 22:09:00 +03:00
|
|
|
default:
|
|
|
|
WARN_ON_ONCE(1);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2019-08-21 22:09:01 +03:00
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:09:12 +03:00
|
|
|
static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime)
|
|
|
|
{
|
|
|
|
samples[CPUCLOCK_PROF] = stime + utime;
|
|
|
|
samples[CPUCLOCK_VIRT] = utime;
|
|
|
|
samples[CPUCLOCK_SCHED] = rtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void task_sample_cputime(struct task_struct *p, u64 *samples)
|
|
|
|
{
|
|
|
|
u64 stime, utime;
|
|
|
|
|
|
|
|
task_cputime(p, &utime, &stime);
|
|
|
|
store_samples(samples, stime, utime, p->se.sum_exec_runtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
|
|
|
|
u64 *samples)
|
|
|
|
{
|
|
|
|
u64 stime, utime, rtime;
|
|
|
|
|
|
|
|
utime = atomic64_read(&at->utime);
|
|
|
|
stime = atomic64_read(&at->stime);
|
|
|
|
rtime = atomic64_read(&at->sum_exec_runtime);
|
|
|
|
store_samples(samples, stime, utime, rtime);
|
|
|
|
}
|
|
|
|
|
2015-04-28 23:00:22 +03:00
|
|
|
/*
|
|
|
|
* Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
|
|
|
|
* to avoid race conditions with concurrent updates to cputime.
|
|
|
|
*/
|
|
|
|
static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
|
2009-02-11 13:30:27 +03:00
|
|
|
{
|
2023-01-16 19:53:37 +03:00
|
|
|
u64 curr_cputime = atomic64_read(cputime);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (sum_cputime <= curr_cputime)
|
|
|
|
return;
|
|
|
|
} while (!atomic64_try_cmpxchg(cputime, &curr_cputime, sum_cputime));
|
2015-04-28 23:00:22 +03:00
|
|
|
}
|
2009-02-11 13:30:27 +03:00
|
|
|
|
2019-08-21 22:09:16 +03:00
|
|
|
static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic,
|
|
|
|
struct task_cputime *sum)
|
2015-04-28 23:00:22 +03:00
|
|
|
{
|
2015-04-28 23:00:24 +03:00
|
|
|
__update_gt_cputime(&cputime_atomic->utime, sum->utime);
|
|
|
|
__update_gt_cputime(&cputime_atomic->stime, sum->stime);
|
|
|
|
__update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime);
|
2015-04-28 23:00:22 +03:00
|
|
|
}
|
2009-02-11 13:30:27 +03:00
|
|
|
|
2019-08-21 22:08:51 +03:00
|
|
|
/**
|
|
|
|
* thread_group_sample_cputime - Sample cputime for a given task
|
|
|
|
* @tsk: Task for which cputime needs to be started
|
2019-10-21 10:44:12 +03:00
|
|
|
* @samples: Storage for time samples
|
2019-08-21 22:08:51 +03:00
|
|
|
*
|
|
|
|
* Called from sys_getitimer() to calculate the expiry time of an active
|
|
|
|
* timer. That means group cputime accounting is already active. Called
|
|
|
|
* with task sighand lock held.
|
|
|
|
*
|
|
|
|
* Updates @times with an uptodate sample of the thread group cputimes.
|
|
|
|
*/
|
2019-08-21 22:09:16 +03:00
|
|
|
void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples)
|
2019-08-21 22:08:51 +03:00
|
|
|
{
|
|
|
|
struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
|
2019-08-21 22:09:24 +03:00
|
|
|
struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
|
2019-08-21 22:08:51 +03:00
|
|
|
|
2019-08-21 22:09:24 +03:00
|
|
|
WARN_ON_ONCE(!pct->timers_active);
|
2019-08-21 22:08:51 +03:00
|
|
|
|
2019-08-21 22:09:16 +03:00
|
|
|
proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
|
2019-08-21 22:08:51 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:08:54 +03:00
|
|
|
/**
|
|
|
|
* thread_group_start_cputime - Start cputime and return a sample
|
|
|
|
* @tsk: Task for which cputime needs to be started
|
2019-08-21 22:09:16 +03:00
|
|
|
* @samples: Storage for time samples
|
2019-08-21 22:08:54 +03:00
|
|
|
*
|
2021-03-23 00:39:03 +03:00
|
|
|
* The thread group cputime accounting is avoided when there are no posix
|
2019-08-21 22:08:54 +03:00
|
|
|
* CPU timers armed. Before starting a timer it's required to check whether
|
|
|
|
* the time accounting is active. If not, a full update of the atomic
|
|
|
|
* accounting store needs to be done and the accounting enabled.
|
|
|
|
*
|
|
|
|
* Updates @times with an uptodate sample of the thread group cputimes.
|
|
|
|
*/
|
2019-08-21 22:09:16 +03:00
|
|
|
static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples)
|
2009-02-11 13:30:27 +03:00
|
|
|
{
|
|
|
|
struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
|
2019-08-21 22:09:24 +03:00
|
|
|
struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
|
2009-02-11 13:30:27 +03:00
|
|
|
|
2021-07-26 15:55:08 +03:00
|
|
|
lockdep_assert_task_sighand_held(tsk);
|
|
|
|
|
2015-04-28 23:00:22 +03:00
|
|
|
/* Check if cputimer isn't running. This is accessed without locking. */
|
2019-08-21 22:09:24 +03:00
|
|
|
if (!READ_ONCE(pct->timers_active)) {
|
2019-08-21 22:09:16 +03:00
|
|
|
struct task_cputime sum;
|
|
|
|
|
2009-02-11 13:30:27 +03:00
|
|
|
/*
|
|
|
|
* The POSIX timer interface allows for absolute time expiry
|
|
|
|
* values through the TIMER_ABSTIME flag, therefore we have
|
2015-04-28 23:00:22 +03:00
|
|
|
* to synchronize the timer to the clock every time we start it.
|
2009-02-11 13:30:27 +03:00
|
|
|
*/
|
2017-01-31 06:09:34 +03:00
|
|
|
thread_group_cputime(tsk, &sum);
|
2015-04-28 23:00:24 +03:00
|
|
|
update_gt_cputime(&cputimer->cputime_atomic, &sum);
|
2015-04-28 23:00:22 +03:00
|
|
|
|
|
|
|
/*
|
2019-08-21 22:09:24 +03:00
|
|
|
* We're setting timers_active without a lock. Ensure this
|
|
|
|
* only gets written to in one operation. We set it after
|
|
|
|
* update_gt_cputime() as a small optimization, but
|
|
|
|
* barriers are not required because update_gt_cputime()
|
2015-04-28 23:00:22 +03:00
|
|
|
* can handle concurrent updates.
|
|
|
|
*/
|
2019-08-21 22:09:24 +03:00
|
|
|
WRITE_ONCE(pct->timers_active, true);
|
2015-04-28 23:00:22 +03:00
|
|
|
}
|
2019-08-21 22:09:16 +03:00
|
|
|
proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __thread_group_cputime(struct task_struct *tsk, u64 *samples)
|
|
|
|
{
|
|
|
|
struct task_cputime ct;
|
|
|
|
|
|
|
|
thread_group_cputime(tsk, &ct);
|
|
|
|
store_samples(samples, ct.stime, ct.utime, ct.sum_exec_runtime);
|
2009-02-11 13:30:27 +03:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2019-08-21 22:08:55 +03:00
|
|
|
* Sample a process (thread group) clock for the given task clkid. If the
|
|
|
|
* group's cputime accounting is already enabled, read the atomic
|
2020-02-28 20:08:45 +03:00
|
|
|
* store. Otherwise a full update is required. clkid is already validated.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2019-08-21 22:09:01 +03:00
|
|
|
static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p,
|
|
|
|
bool start)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-21 22:08:55 +03:00
|
|
|
struct thread_group_cputimer *cputimer = &p->signal->cputimer;
|
2019-08-21 22:09:24 +03:00
|
|
|
struct posix_cputimers *pct = &p->signal->posix_cputimers;
|
2019-08-21 22:09:16 +03:00
|
|
|
u64 samples[CPUCLOCK_MAX];
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
|
2019-08-21 22:09:24 +03:00
|
|
|
if (!READ_ONCE(pct->timers_active)) {
|
2019-08-21 22:08:55 +03:00
|
|
|
if (start)
|
2019-08-21 22:09:16 +03:00
|
|
|
thread_group_start_cputime(p, samples);
|
2019-08-21 22:08:55 +03:00
|
|
|
else
|
2019-08-21 22:09:16 +03:00
|
|
|
__thread_group_cputime(p, samples);
|
2019-08-21 22:08:55 +03:00
|
|
|
} else {
|
2019-08-21 22:09:16 +03:00
|
|
|
proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
|
2019-08-21 22:08:55 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:09:16 +03:00
|
|
|
return samples[clkid];
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:08:49 +03:00
|
|
|
static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
|
2013-10-11 19:41:11 +04:00
|
|
|
{
|
2019-08-21 22:08:49 +03:00
|
|
|
const clockid_t clkid = CPUCLOCK_WHICH(clock);
|
|
|
|
struct task_struct *tsk;
|
|
|
|
u64 t;
|
2013-10-11 19:41:11 +04:00
|
|
|
|
2020-04-26 02:38:54 +03:00
|
|
|
rcu_read_lock();
|
2020-04-27 15:55:07 +03:00
|
|
|
tsk = pid_task(pid_for_clock(clock, true), clock_pid_type(clock));
|
2020-04-26 02:38:54 +03:00
|
|
|
if (!tsk) {
|
|
|
|
rcu_read_unlock();
|
2019-08-21 22:08:49 +03:00
|
|
|
return -EINVAL;
|
2020-04-26 02:38:54 +03:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-08-21 22:08:49 +03:00
|
|
|
if (CPUCLOCK_PERTHREAD(clock))
|
2019-08-21 22:09:01 +03:00
|
|
|
t = cpu_clock_sample(clkid, tsk);
|
2019-08-21 22:08:49 +03:00
|
|
|
else
|
2019-08-21 22:09:01 +03:00
|
|
|
t = cpu_clock_sample_group(clkid, tsk, false);
|
2020-04-26 02:38:54 +03:00
|
|
|
rcu_read_unlock();
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-08-21 22:08:49 +03:00
|
|
|
*tp = ns_to_timespec64(t);
|
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
|
2009-11-18 01:14:13 +03:00
|
|
|
* This is called from sys_timer_create() and do_cpu_nanosleep() with the
|
|
|
|
* new timer already all-zeros initialized.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2011-02-01 16:52:12 +03:00
|
|
|
static int posix_cpu_timer_create(struct k_itimer *new_timer)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2020-07-30 13:14:06 +03:00
|
|
|
static struct lock_class_key posix_cpu_timers_key;
|
2020-04-27 15:55:07 +03:00
|
|
|
struct pid *pid;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2020-04-26 02:38:54 +03:00
|
|
|
rcu_read_lock();
|
2020-04-27 15:55:07 +03:00
|
|
|
pid = pid_for_clock(new_timer->it_clock, false);
|
|
|
|
if (!pid) {
|
2020-04-26 02:38:54 +03:00
|
|
|
rcu_read_unlock();
|
2005-04-17 02:20:36 +04:00
|
|
|
return -EINVAL;
|
2020-04-26 02:38:54 +03:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2020-07-30 13:14:06 +03:00
|
|
|
/*
|
|
|
|
* If posix timer expiry is handled in task work context then
|
|
|
|
* timer::it_lock can be taken without disabling interrupts as all
|
2021-03-23 00:39:03 +03:00
|
|
|
* other locking happens in task context. This requires a separate
|
2020-07-30 13:14:06 +03:00
|
|
|
* lock class key otherwise regular posix timer expiry would record
|
|
|
|
* the lock class being taken in interrupt context and generate a
|
|
|
|
* false positive warning.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_POSIX_CPU_TIMERS_TASK_WORK))
|
|
|
|
lockdep_set_class(&new_timer->it_lock, &posix_cpu_timers_key);
|
|
|
|
|
2017-05-31 00:15:44 +03:00
|
|
|
new_timer->kclock = &clock_posix_cpu;
|
2019-08-27 22:31:02 +03:00
|
|
|
timerqueue_init(&new_timer->it.cpu.node);
|
2020-04-27 15:55:07 +03:00
|
|
|
new_timer->it.cpu.pid = get_pid(pid);
|
2020-04-26 02:38:54 +03:00
|
|
|
rcu_read_unlock();
|
2019-08-21 22:08:50 +03:00
|
|
|
return 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2021-07-26 15:55:12 +03:00
|
|
|
static struct posix_cputimer_base *timer_base(struct k_itimer *timer,
|
|
|
|
struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
int clkidx = CPUCLOCK_WHICH(timer->it_clock);
|
|
|
|
|
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
|
|
|
return tsk->posix_cputimers.bases + clkidx;
|
|
|
|
else
|
|
|
|
return tsk->signal->posix_cputimers.bases + clkidx;
|
|
|
|
}
|
|
|
|
|
posix-cpu-timers: Recalc next expiration when timer_settime() ends up not queueing
There are several scenarios that can result in posix_cpu_timer_set()
not queueing the timer but still leaving the threadgroup cputime counter
running or keeping the tick dependency around for a random amount of time.
1) If timer_settime() is called with a 0 expiration on a timer that is
already disabled, the process wide cputime counter will be started
and won't ever get a chance to be stopped by stop_process_timer()
since no timer is actually armed to be processed.
The following snippet is enough to trigger the issue.
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, TIMER_ABSTIME, &val, NULL);
timer_delete(id);
}
2) If timer_settime() is called with a 0 expiration on a timer that is
already armed, the timer is dequeued but not really disarmed. So the
process wide cputime counter and the tick dependency may still remain
a while around.
The following code snippet keeps this overhead around for one week after
the timer deletion:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
val.it_value.tv_sec = 604800;
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, 0, &val, NULL);
timer_delete(id);
}
3) If the timer was initially deactivated, this call to timer_settime()
with an early expiration may have started the process wide cputime
counter even though the timer hasn't been queued and armed because it
has fired early and inline within posix_cpu_timer_set() itself. As a
result the process wide cputime counter may never stop until a new
timer is ever armed in the future.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
4) If the timer was initially armed with a former expiration value
before this call to timer_settime() and the current call sets an
early deadline that has already expired, the timer fires inline
within posix_cpu_timer_set(). In this case it must have been dequeued
before firing inline with its new expiration value, yet it hasn't
been disarmed in this case. So the process wide cputime counter and
the tick dependency may still be around for a while even after the
timer fired.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_sec = 100;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
val.it_value.tv_sec = 0;
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
Fix all these issues with triggering the related base next expiration
recalculation on the next tick. This also implies to re-evaluate the need
to keep around the process wide cputime counter and the tick dependency, in
a similar fashion to disarm_timer().
Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20210726125513.271824-7-frederic@kernel.org
2021-07-26 15:55:13 +03:00
|
|
|
/*
|
|
|
|
* Force recalculating the base earliest expiration on the next tick.
|
|
|
|
* This will also re-evaluate the need to keep around the process wide
|
|
|
|
* cputime counter and tick dependency and eventually shut these down
|
|
|
|
* if necessary.
|
|
|
|
*/
|
|
|
|
static void trigger_base_recalc_expires(struct k_itimer *timer,
|
|
|
|
struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
struct posix_cputimer_base *base = timer_base(timer, tsk);
|
|
|
|
|
|
|
|
base->nextevt = 0;
|
|
|
|
}
|
|
|
|
|
2021-07-26 15:55:09 +03:00
|
|
|
/*
|
|
|
|
* Dequeue the timer and reset the base if it was its earliest expiration.
|
|
|
|
* It makes sure the next tick recalculates the base next expiration so we
|
|
|
|
* don't keep the costly process wide cputime counter around for a random
|
|
|
|
* amount of time, along with the tick dependency.
|
|
|
|
*
|
|
|
|
* If another timer gets queued between this and the next tick, its
|
|
|
|
* expiration will update the base next event if necessary on the next
|
|
|
|
* tick.
|
|
|
|
*/
|
|
|
|
static void disarm_timer(struct k_itimer *timer, struct task_struct *p)
|
|
|
|
{
|
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
|
|
|
struct posix_cputimer_base *base;
|
|
|
|
|
|
|
|
if (!cpu_timer_dequeue(ctmr))
|
|
|
|
return;
|
|
|
|
|
2021-07-26 15:55:12 +03:00
|
|
|
base = timer_base(timer, p);
|
2021-07-26 15:55:09 +03:00
|
|
|
if (cpu_timer_getexpires(ctmr) == base->nextevt)
|
posix-cpu-timers: Recalc next expiration when timer_settime() ends up not queueing
There are several scenarios that can result in posix_cpu_timer_set()
not queueing the timer but still leaving the threadgroup cputime counter
running or keeping the tick dependency around for a random amount of time.
1) If timer_settime() is called with a 0 expiration on a timer that is
already disabled, the process wide cputime counter will be started
and won't ever get a chance to be stopped by stop_process_timer()
since no timer is actually armed to be processed.
The following snippet is enough to trigger the issue.
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, TIMER_ABSTIME, &val, NULL);
timer_delete(id);
}
2) If timer_settime() is called with a 0 expiration on a timer that is
already armed, the timer is dequeued but not really disarmed. So the
process wide cputime counter and the tick dependency may still remain
a while around.
The following code snippet keeps this overhead around for one week after
the timer deletion:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
val.it_value.tv_sec = 604800;
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, 0, &val, NULL);
timer_delete(id);
}
3) If the timer was initially deactivated, this call to timer_settime()
with an early expiration may have started the process wide cputime
counter even though the timer hasn't been queued and armed because it
has fired early and inline within posix_cpu_timer_set() itself. As a
result the process wide cputime counter may never stop until a new
timer is ever armed in the future.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
4) If the timer was initially armed with a former expiration value
before this call to timer_settime() and the current call sets an
early deadline that has already expired, the timer fires inline
within posix_cpu_timer_set(). In this case it must have been dequeued
before firing inline with its new expiration value, yet it hasn't
been disarmed in this case. So the process wide cputime counter and
the tick dependency may still be around for a while even after the
timer fired.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_sec = 100;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
val.it_value.tv_sec = 0;
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
Fix all these issues with triggering the related base next expiration
recalculation on the next tick. This also implies to re-evaluate the need
to keep around the process wide cputime counter and the tick dependency, in
a similar fashion to disarm_timer().
Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20210726125513.271824-7-frederic@kernel.org
2021-07-26 15:55:13 +03:00
|
|
|
trigger_base_recalc_expires(timer, p);
|
2021-07-26 15:55:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Clean up a CPU-clock timer that is about to be destroyed.
|
|
|
|
* This is called from timer deletion with the timer already locked.
|
|
|
|
* If we return TIMER_RETRY, it's necessary to release the timer's lock
|
|
|
|
* and try again. (This happens when the timer is in the middle of firing.)
|
|
|
|
*/
|
2011-02-01 16:52:12 +03:00
|
|
|
static int posix_cpu_timer_del(struct k_itimer *timer)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-27 22:31:02 +03:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
2013-10-11 19:41:11 +04:00
|
|
|
struct sighand_struct *sighand;
|
2020-02-28 20:11:06 +03:00
|
|
|
struct task_struct *p;
|
2019-08-27 22:31:02 +03:00
|
|
|
unsigned long flags;
|
|
|
|
int ret = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2020-02-28 20:11:06 +03:00
|
|
|
rcu_read_lock();
|
|
|
|
p = cpu_timer_task_rcu(timer);
|
|
|
|
if (!p)
|
|
|
|
goto out;
|
2005-10-23 20:25:39 +04:00
|
|
|
|
2013-10-11 19:41:11 +04:00
|
|
|
/*
|
|
|
|
* Protect against sighand release/switch in exit/exec and process/
|
|
|
|
* thread timer list entry concurrent read/writes.
|
|
|
|
*/
|
|
|
|
sighand = lock_task_sighand(p, &flags);
|
|
|
|
if (unlikely(sighand == NULL)) {
|
2013-10-11 02:37:39 +04:00
|
|
|
/*
|
2019-08-27 22:31:02 +03:00
|
|
|
* This raced with the reaping of the task. The exit cleanup
|
|
|
|
* should have removed this timer from the timer queue.
|
2013-10-11 02:37:39 +04:00
|
|
|
*/
|
2019-08-27 22:31:02 +03:00
|
|
|
WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node));
|
2013-10-11 02:37:39 +04:00
|
|
|
} else {
|
|
|
|
if (timer->it.cpu.firing)
|
|
|
|
ret = TIMER_RETRY;
|
|
|
|
else
|
2021-07-26 15:55:09 +03:00
|
|
|
disarm_timer(timer, p);
|
2013-10-11 19:41:11 +04:00
|
|
|
|
|
|
|
unlock_task_sighand(p, &flags);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2013-10-11 02:37:39 +04:00
|
|
|
|
2020-02-28 20:11:06 +03:00
|
|
|
out:
|
|
|
|
rcu_read_unlock();
|
2013-10-11 02:37:39 +04:00
|
|
|
if (!ret)
|
2020-02-28 20:11:06 +03:00
|
|
|
put_pid(ctmr->pid);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2005-10-23 20:25:39 +04:00
|
|
|
return ret;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
static void cleanup_timerqueue(struct timerqueue_head *head)
|
2013-06-28 04:06:42 +04:00
|
|
|
{
|
2019-08-27 22:31:02 +03:00
|
|
|
struct timerqueue_node *node;
|
|
|
|
struct cpu_timer *ctmr;
|
2013-06-28 04:06:42 +04:00
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
while ((node = timerqueue_getnext(head))) {
|
|
|
|
timerqueue_del(head, node);
|
|
|
|
ctmr = container_of(node, struct cpu_timer, node);
|
|
|
|
ctmr->head = NULL;
|
|
|
|
}
|
2013-06-28 04:06:42 +04:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2019-08-19 17:31:45 +03:00
|
|
|
* Clean out CPU timers which are still armed when a thread exits. The
|
|
|
|
* timers are only removed from the list. No other updates are done. The
|
|
|
|
* corresponding posix timers are still accessible, but cannot be rearmed.
|
|
|
|
*
|
2005-04-17 02:20:36 +04:00
|
|
|
* This must be called with the siglock held.
|
|
|
|
*/
|
2019-08-21 22:09:04 +03:00
|
|
|
static void cleanup_timers(struct posix_cputimers *pct)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-27 22:31:02 +03:00
|
|
|
cleanup_timerqueue(&pct->bases[CPUCLOCK_PROF].tqhead);
|
|
|
|
cleanup_timerqueue(&pct->bases[CPUCLOCK_VIRT].tqhead);
|
|
|
|
cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are both called with the siglock held, when the current thread
|
|
|
|
* is being reaped. When the final (leader) thread in the group is reaped,
|
|
|
|
* posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
|
|
|
|
*/
|
|
|
|
void posix_cpu_timers_exit(struct task_struct *tsk)
|
|
|
|
{
|
2019-08-21 22:09:04 +03:00
|
|
|
cleanup_timers(&tsk->posix_cputimers);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
void posix_cpu_timers_exit_group(struct task_struct *tsk)
|
|
|
|
{
|
2019-08-21 22:09:04 +03:00
|
|
|
cleanup_timers(&tsk->signal->posix_cputimers);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert the timer on the appropriate list before any timers that
|
2013-10-11 20:56:49 +04:00
|
|
|
* expire later. This must be called with the sighand lock held.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2020-02-28 20:09:46 +03:00
|
|
|
static void arm_timer(struct k_itimer *timer, struct task_struct *p)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2021-07-26 15:55:12 +03:00
|
|
|
struct posix_cputimer_base *base = timer_base(timer, p);
|
2019-08-27 22:31:02 +03:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
|
|
|
u64 newexp = cpu_timer_getexpires(ctmr);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
if (!cpu_timer_enqueue(&base->tqhead, ctmr))
|
2019-08-21 22:09:08 +03:00
|
|
|
return;
|
2010-03-12 01:04:38 +03:00
|
|
|
|
2019-08-21 22:09:08 +03:00
|
|
|
/*
|
|
|
|
* We are the new earliest-expiring POSIX 1.b timer, hence
|
|
|
|
* need to update expiration cache. Take into account that
|
|
|
|
* for process timers we share expiration cache with itimers
|
|
|
|
* and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
|
|
|
|
*/
|
2019-08-21 22:09:19 +03:00
|
|
|
if (newexp < base->nextevt)
|
2019-08-26 21:22:24 +03:00
|
|
|
base->nextevt = newexp;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-08-21 22:09:08 +03:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
|
|
|
tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
|
|
|
|
else
|
2021-05-13 02:29:21 +03:00
|
|
|
tick_dep_set_signal(p, TICK_DEP_BIT_POSIX_TIMER);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The timer is locked, fire it and arrange for its reload.
|
|
|
|
*/
|
|
|
|
static void cpu_timer_fire(struct k_itimer *timer)
|
|
|
|
{
|
2019-08-27 22:31:02 +03:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
|
|
|
|
cpu-timers: Change SIGEV_NONE timer implementation
When user sets up a timer without associated signal and process does
not use any other cpu timers and does not exit, tsk->signal->cputimer
is enabled and running forever.
Avoid running the timer for no reason.
I used below program to check patch does not break current user space
visible behavior.
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
void consume_cpu(void)
{
int i = 0;
int count = 0;
for(i=0; i<100000000; i++)
count++;
}
int main(void)
{
int i;
struct sigaction act;
struct sigevent evt = { };
timer_t tid;
struct itimerspec spec = { };
evt.sigev_notify = SIGEV_NONE;
assert(timer_create(CLOCK_PROCESS_CPUTIME_ID, &evt, &tid) == 0);
spec.it_value.tv_sec = 10;
assert(timer_settime(tid, 0, &spec, NULL) == 0);
for (i = 0; i < 30; i++) {
consume_cpu();
memset(&spec, 0, sizeof(spec));
assert(timer_gettime(tid, &spec) == 0);
printf("%lu.%09lu\n",
(unsigned long) spec.it_value.tv_sec,
(unsigned long) spec.it_value.tv_nsec);
}
assert(timer_delete(tid) == 0);
return 0;
}
Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2010-03-12 01:04:41 +03:00
|
|
|
if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
|
|
|
|
/*
|
|
|
|
* User don't want any signal.
|
|
|
|
*/
|
2019-08-27 22:31:02 +03:00
|
|
|
cpu_timer_setexpires(ctmr, 0);
|
cpu-timers: Change SIGEV_NONE timer implementation
When user sets up a timer without associated signal and process does
not use any other cpu timers and does not exit, tsk->signal->cputimer
is enabled and running forever.
Avoid running the timer for no reason.
I used below program to check patch does not break current user space
visible behavior.
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
void consume_cpu(void)
{
int i = 0;
int count = 0;
for(i=0; i<100000000; i++)
count++;
}
int main(void)
{
int i;
struct sigaction act;
struct sigevent evt = { };
timer_t tid;
struct itimerspec spec = { };
evt.sigev_notify = SIGEV_NONE;
assert(timer_create(CLOCK_PROCESS_CPUTIME_ID, &evt, &tid) == 0);
spec.it_value.tv_sec = 10;
assert(timer_settime(tid, 0, &spec, NULL) == 0);
for (i = 0; i < 30; i++) {
consume_cpu();
memset(&spec, 0, sizeof(spec));
assert(timer_gettime(tid, &spec) == 0);
printf("%lu.%09lu\n",
(unsigned long) spec.it_value.tv_sec,
(unsigned long) spec.it_value.tv_nsec);
}
assert(timer_delete(tid) == 0);
return 0;
}
Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2010-03-12 01:04:41 +03:00
|
|
|
} else if (unlikely(timer->sigq == NULL)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* This a special case for clock_nanosleep,
|
|
|
|
* not a normal timer from sys_timer_create.
|
|
|
|
*/
|
|
|
|
wake_up_process(timer->it_process);
|
2019-08-27 22:31:02 +03:00
|
|
|
cpu_timer_setexpires(ctmr, 0);
|
2019-01-11 16:33:17 +03:00
|
|
|
} else if (!timer->it_interval) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* One-shot timer. Clear it as soon as it's fired.
|
|
|
|
*/
|
|
|
|
posix_timer_event(timer, 0);
|
2019-08-27 22:31:02 +03:00
|
|
|
cpu_timer_setexpires(ctmr, 0);
|
2005-04-17 02:20:36 +04:00
|
|
|
} else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
|
|
|
|
/*
|
|
|
|
* The signal did not get queued because the signal
|
|
|
|
* was ignored, so we won't get any callback to
|
|
|
|
* reload the timer. But we need to keep it
|
|
|
|
* ticking in case the signal is deliverable next time.
|
|
|
|
*/
|
2017-05-31 00:15:47 +03:00
|
|
|
posix_cpu_timer_rearm(timer);
|
2017-05-31 00:15:42 +03:00
|
|
|
++timer->it_requeue_pending;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Guts of sys_timer_settime for CPU timers.
|
|
|
|
* This is called with the timer locked and interrupts disabled.
|
|
|
|
* If we return TIMER_RETRY, it's necessary to release the timer's lock
|
|
|
|
* and try again. (This happens when the timer is in the middle of firing.)
|
|
|
|
*/
|
2013-10-11 20:56:49 +04:00
|
|
|
static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
|
2017-03-26 22:04:17 +03:00
|
|
|
struct itimerspec64 *new, struct itimerspec64 *old)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-21 22:08:56 +03:00
|
|
|
clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
|
2017-01-31 06:09:34 +03:00
|
|
|
u64 old_expires, new_expires, old_incr, val;
|
2019-08-27 22:31:02 +03:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
2019-08-21 22:08:56 +03:00
|
|
|
struct sighand_struct *sighand;
|
2020-02-28 20:11:06 +03:00
|
|
|
struct task_struct *p;
|
2019-08-21 22:08:56 +03:00
|
|
|
unsigned long flags;
|
2019-08-27 22:31:02 +03:00
|
|
|
int ret = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2020-02-28 20:11:06 +03:00
|
|
|
rcu_read_lock();
|
|
|
|
p = cpu_timer_task_rcu(timer);
|
|
|
|
if (!p) {
|
|
|
|
/*
|
|
|
|
* If p has just been reaped, we can no
|
|
|
|
* longer get any information about it at all.
|
|
|
|
*/
|
|
|
|
rcu_read_unlock();
|
|
|
|
return -ESRCH;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2017-06-20 18:37:36 +03:00
|
|
|
/*
|
|
|
|
* Use the to_ktime conversion because that clamps the maximum
|
|
|
|
* value to KTIME_MAX and avoid multiplication overflows.
|
|
|
|
*/
|
|
|
|
new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
2013-10-11 20:56:49 +04:00
|
|
|
* Protect against sighand release/switch in exit/exec and p->cpu_timers
|
|
|
|
* and p->signal->cpu_timers read/write in arm_timer()
|
|
|
|
*/
|
|
|
|
sighand = lock_task_sighand(p, &flags);
|
|
|
|
/*
|
|
|
|
* If p has just been reaped, we can no
|
2005-04-17 02:20:36 +04:00
|
|
|
* longer get any information about it at all.
|
|
|
|
*/
|
2020-02-28 20:11:06 +03:00
|
|
|
if (unlikely(sighand == NULL)) {
|
|
|
|
rcu_read_unlock();
|
2005-04-17 02:20:36 +04:00
|
|
|
return -ESRCH;
|
2020-02-28 20:11:06 +03:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Disarm any old timer after extracting its expiry time.
|
|
|
|
*/
|
2019-01-11 16:33:17 +03:00
|
|
|
old_incr = timer->it_interval;
|
2019-08-27 22:31:02 +03:00
|
|
|
old_expires = cpu_timer_getexpires(ctmr);
|
|
|
|
|
2005-10-24 18:29:58 +04:00
|
|
|
if (unlikely(timer->it.cpu.firing)) {
|
|
|
|
timer->it.cpu.firing = -1;
|
|
|
|
ret = TIMER_RETRY;
|
2019-08-27 22:31:02 +03:00
|
|
|
} else {
|
|
|
|
cpu_timer_dequeue(ctmr);
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to sample the current value to convert the new
|
|
|
|
* value from to relative and absolute, and to convert the
|
|
|
|
* old value from absolute to relative. To set a process
|
|
|
|
* timer, we need a sample to balance the thread expiry
|
|
|
|
* times (in arm_timer). With an absolute time, we must
|
|
|
|
* check if it's already passed. In short, we need a sample.
|
|
|
|
*/
|
2019-08-21 22:09:01 +03:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
|
|
|
val = cpu_clock_sample(clkid, p);
|
|
|
|
else
|
|
|
|
val = cpu_clock_sample_group(clkid, p, true);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (old) {
|
2013-06-28 04:06:42 +04:00
|
|
|
if (old_expires == 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
old->it_value.tv_sec = 0;
|
|
|
|
old->it_value.tv_nsec = 0;
|
|
|
|
} else {
|
|
|
|
/*
|
2019-08-27 22:31:02 +03:00
|
|
|
* Update the timer in case it has overrun already.
|
|
|
|
* If it has, we'll report it as having overrun and
|
|
|
|
* with the next reloaded timer already ticking,
|
|
|
|
* though we are swallowing that pending
|
|
|
|
* notification here to install the new setting.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2019-08-27 22:31:02 +03:00
|
|
|
u64 exp = bump_cpu_timer(timer, val);
|
|
|
|
|
|
|
|
if (val < exp) {
|
|
|
|
old_expires = exp - val;
|
2017-03-26 22:04:17 +03:00
|
|
|
old->it_value = ns_to_timespec64(old_expires);
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
|
|
|
old->it_value.tv_nsec = 1;
|
|
|
|
old->it_value.tv_sec = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-24 18:29:58 +04:00
|
|
|
if (unlikely(ret)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* We are colliding with the timer actually firing.
|
|
|
|
* Punt after filling in the timer's old value, and
|
|
|
|
* disable this firing since we are already reporting
|
|
|
|
* it as an overrun (thanks to bump_cpu_timer above).
|
|
|
|
*/
|
2013-10-11 20:56:49 +04:00
|
|
|
unlock_task_sighand(p, &flags);
|
2005-04-17 02:20:36 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-10-11 20:56:49 +04:00
|
|
|
if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
|
2013-06-28 04:06:42 +04:00
|
|
|
new_expires += val;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Install the new expiry time (or zero).
|
|
|
|
* For a timer with no notification action, we don't actually
|
|
|
|
* arm the timer (we'll just fake it for timer_gettime).
|
|
|
|
*/
|
2019-08-27 22:31:02 +03:00
|
|
|
cpu_timer_setexpires(ctmr, new_expires);
|
2013-06-28 04:06:42 +04:00
|
|
|
if (new_expires != 0 && val < new_expires) {
|
2020-02-28 20:09:46 +03:00
|
|
|
arm_timer(timer, p);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2013-10-11 20:56:49 +04:00
|
|
|
unlock_task_sighand(p, &flags);
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Install the new reload setting, and
|
|
|
|
* set up the signal and overrun bookkeeping.
|
|
|
|
*/
|
2019-01-11 16:33:17 +03:00
|
|
|
timer->it_interval = timespec64_to_ktime(new->it_interval);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This acts as a modification timestamp for the timer,
|
|
|
|
* so any automatic reload attempt will punt on seeing
|
|
|
|
* that we have reset the timer manually.
|
|
|
|
*/
|
|
|
|
timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
|
|
|
|
~REQUEUE_PENDING;
|
|
|
|
timer->it_overrun_last = 0;
|
|
|
|
timer->it_overrun = -1;
|
|
|
|
|
posix-cpu-timers: Recalc next expiration when timer_settime() ends up not queueing
There are several scenarios that can result in posix_cpu_timer_set()
not queueing the timer but still leaving the threadgroup cputime counter
running or keeping the tick dependency around for a random amount of time.
1) If timer_settime() is called with a 0 expiration on a timer that is
already disabled, the process wide cputime counter will be started
and won't ever get a chance to be stopped by stop_process_timer()
since no timer is actually armed to be processed.
The following snippet is enough to trigger the issue.
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, TIMER_ABSTIME, &val, NULL);
timer_delete(id);
}
2) If timer_settime() is called with a 0 expiration on a timer that is
already armed, the timer is dequeued but not really disarmed. So the
process wide cputime counter and the tick dependency may still remain
a while around.
The following code snippet keeps this overhead around for one week after
the timer deletion:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
val.it_value.tv_sec = 604800;
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, 0, &val, NULL);
timer_delete(id);
}
3) If the timer was initially deactivated, this call to timer_settime()
with an early expiration may have started the process wide cputime
counter even though the timer hasn't been queued and armed because it
has fired early and inline within posix_cpu_timer_set() itself. As a
result the process wide cputime counter may never stop until a new
timer is ever armed in the future.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
4) If the timer was initially armed with a former expiration value
before this call to timer_settime() and the current call sets an
early deadline that has already expired, the timer fires inline
within posix_cpu_timer_set(). In this case it must have been dequeued
before firing inline with its new expiration value, yet it hasn't
been disarmed in this case. So the process wide cputime counter and
the tick dependency may still be around for a while even after the
timer fired.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_sec = 100;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
val.it_value.tv_sec = 0;
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
Fix all these issues with triggering the related base next expiration
recalculation on the next tick. This also implies to re-evaluate the need
to keep around the process wide cputime counter and the tick dependency, in
a similar fashion to disarm_timer().
Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20210726125513.271824-7-frederic@kernel.org
2021-07-26 15:55:13 +03:00
|
|
|
if (val >= new_expires) {
|
|
|
|
if (new_expires != 0) {
|
|
|
|
/*
|
|
|
|
* The designated time already passed, so we notify
|
|
|
|
* immediately, even if the thread never runs to
|
|
|
|
* accumulate more time on this clock.
|
|
|
|
*/
|
|
|
|
cpu_timer_fire(timer);
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
posix-cpu-timers: Recalc next expiration when timer_settime() ends up not queueing
There are several scenarios that can result in posix_cpu_timer_set()
not queueing the timer but still leaving the threadgroup cputime counter
running or keeping the tick dependency around for a random amount of time.
1) If timer_settime() is called with a 0 expiration on a timer that is
already disabled, the process wide cputime counter will be started
and won't ever get a chance to be stopped by stop_process_timer()
since no timer is actually armed to be processed.
The following snippet is enough to trigger the issue.
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, TIMER_ABSTIME, &val, NULL);
timer_delete(id);
}
2) If timer_settime() is called with a 0 expiration on a timer that is
already armed, the timer is dequeued but not really disarmed. So the
process wide cputime counter and the tick dependency may still remain
a while around.
The following code snippet keeps this overhead around for one week after
the timer deletion:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
val.it_value.tv_sec = 604800;
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, 0, &val, NULL);
timer_delete(id);
}
3) If the timer was initially deactivated, this call to timer_settime()
with an early expiration may have started the process wide cputime
counter even though the timer hasn't been queued and armed because it
has fired early and inline within posix_cpu_timer_set() itself. As a
result the process wide cputime counter may never stop until a new
timer is ever armed in the future.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
4) If the timer was initially armed with a former expiration value
before this call to timer_settime() and the current call sets an
early deadline that has already expired, the timer fires inline
within posix_cpu_timer_set(). In this case it must have been dequeued
before firing inline with its new expiration value, yet it hasn't
been disarmed in this case. So the process wide cputime counter and
the tick dependency may still be around for a while even after the
timer fired.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_sec = 100;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
val.it_value.tv_sec = 0;
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
Fix all these issues with triggering the related base next expiration
recalculation on the next tick. This also implies to re-evaluate the need
to keep around the process wide cputime counter and the tick dependency, in
a similar fashion to disarm_timer().
Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20210726125513.271824-7-frederic@kernel.org
2021-07-26 15:55:13 +03:00
|
|
|
* Make sure we don't keep around the process wide cputime
|
|
|
|
* counter or the tick dependency if they are not necessary.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
posix-cpu-timers: Recalc next expiration when timer_settime() ends up not queueing
There are several scenarios that can result in posix_cpu_timer_set()
not queueing the timer but still leaving the threadgroup cputime counter
running or keeping the tick dependency around for a random amount of time.
1) If timer_settime() is called with a 0 expiration on a timer that is
already disabled, the process wide cputime counter will be started
and won't ever get a chance to be stopped by stop_process_timer()
since no timer is actually armed to be processed.
The following snippet is enough to trigger the issue.
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, TIMER_ABSTIME, &val, NULL);
timer_delete(id);
}
2) If timer_settime() is called with a 0 expiration on a timer that is
already armed, the timer is dequeued but not really disarmed. So the
process wide cputime counter and the tick dependency may still remain
a while around.
The following code snippet keeps this overhead around for one week after
the timer deletion:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
val.it_value.tv_sec = 604800;
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, 0, &val, NULL);
timer_delete(id);
}
3) If the timer was initially deactivated, this call to timer_settime()
with an early expiration may have started the process wide cputime
counter even though the timer hasn't been queued and armed because it
has fired early and inline within posix_cpu_timer_set() itself. As a
result the process wide cputime counter may never stop until a new
timer is ever armed in the future.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
4) If the timer was initially armed with a former expiration value
before this call to timer_settime() and the current call sets an
early deadline that has already expired, the timer fires inline
within posix_cpu_timer_set(). In this case it must have been dequeued
before firing inline with its new expiration value, yet it hasn't
been disarmed in this case. So the process wide cputime counter and
the tick dependency may still be around for a while even after the
timer fired.
The following code snippet can reproduce this:
void trigger_process_counter(void)
{
timer_t id;
struct itimerspec val = { };
signal(SIGALRM, SIG_IGN);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
val.it_value.tv_sec = 100;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
val.it_value.tv_sec = 0;
val.it_value.tv_nsec = 1;
timer_settime(id, TIMER_ABSTIME, &val, NULL);
}
Fix all these issues with triggering the related base next expiration
recalculation on the next tick. This also implies to re-evaluate the need
to keep around the process wide cputime counter and the tick dependency, in
a similar fashion to disarm_timer().
Suggested-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20210726125513.271824-7-frederic@kernel.org
2021-07-26 15:55:13 +03:00
|
|
|
sighand = lock_task_sighand(p, &flags);
|
|
|
|
if (!sighand)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!cpu_timer_queued(ctmr))
|
|
|
|
trigger_base_recalc_expires(timer, p);
|
|
|
|
|
|
|
|
unlock_task_sighand(p, &flags);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
out:
|
2020-02-28 20:11:06 +03:00
|
|
|
rcu_read_unlock();
|
2017-01-31 06:09:34 +03:00
|
|
|
if (old)
|
2017-03-26 22:04:17 +03:00
|
|
|
old->it_interval = ns_to_timespec64(old_incr);
|
2015-07-17 23:25:49 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-26 22:04:17 +03:00
|
|
|
static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-21 22:08:57 +03:00
|
|
|
clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
|
2019-08-27 22:31:02 +03:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
|
|
|
u64 now, expires = cpu_timer_getexpires(ctmr);
|
2020-02-28 20:11:06 +03:00
|
|
|
struct task_struct *p;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2020-02-28 20:11:06 +03:00
|
|
|
rcu_read_lock();
|
|
|
|
p = cpu_timer_task_rcu(timer);
|
|
|
|
if (!p)
|
|
|
|
goto out;
|
2013-10-11 02:37:39 +04:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Easy part: convert the reload time.
|
|
|
|
*/
|
2019-01-11 16:33:17 +03:00
|
|
|
itp->it_interval = ktime_to_timespec64(timer->it_interval);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
if (!expires)
|
2020-02-28 20:11:06 +03:00
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sample the clock to take the difference with the expiry time.
|
|
|
|
*/
|
2020-02-28 20:09:19 +03:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
2019-08-21 22:09:01 +03:00
|
|
|
now = cpu_clock_sample(clkid, p);
|
2020-02-28 20:09:19 +03:00
|
|
|
else
|
|
|
|
now = cpu_clock_sample_group(clkid, p, false);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
if (now < expires) {
|
|
|
|
itp->it_value = ns_to_timespec64(expires - now);
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The timer should have expired already, but the firing
|
|
|
|
* hasn't taken place yet. Say it's just about to expire.
|
|
|
|
*/
|
|
|
|
itp->it_value.tv_nsec = 1;
|
|
|
|
itp->it_value.tv_sec = 0;
|
|
|
|
}
|
2020-02-28 20:11:06 +03:00
|
|
|
out:
|
|
|
|
rcu_read_unlock();
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
#define MAX_COLLECTED 20
|
2013-06-28 04:06:43 +04:00
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
static u64 collect_timerqueue(struct timerqueue_head *head,
|
|
|
|
struct list_head *firing, u64 now)
|
|
|
|
{
|
|
|
|
struct timerqueue_node *next;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while ((next = timerqueue_getnext(head))) {
|
|
|
|
struct cpu_timer *ctmr;
|
|
|
|
u64 expires;
|
|
|
|
|
|
|
|
ctmr = container_of(next, struct cpu_timer, node);
|
|
|
|
expires = cpu_timer_getexpires(ctmr);
|
|
|
|
/* Limit the number of timers to expire at once */
|
|
|
|
if (++i == MAX_COLLECTED || now < expires)
|
|
|
|
return expires;
|
|
|
|
|
|
|
|
ctmr->firing = 1;
|
2023-04-17 16:37:55 +03:00
|
|
|
/* See posix_cpu_timer_wait_running() */
|
|
|
|
rcu_assign_pointer(ctmr->handling, current);
|
2019-08-27 22:31:02 +03:00
|
|
|
cpu_timer_dequeue(ctmr);
|
|
|
|
list_add_tail(&ctmr->elist, firing);
|
2013-06-28 04:06:43 +04:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:09:19 +03:00
|
|
|
return U64_MAX;
|
2013-06-28 04:06:43 +04:00
|
|
|
}
|
|
|
|
|
2019-08-27 22:31:02 +03:00
|
|
|
static void collect_posix_cputimers(struct posix_cputimers *pct, u64 *samples,
|
|
|
|
struct list_head *firing)
|
2019-08-21 22:09:20 +03:00
|
|
|
{
|
|
|
|
struct posix_cputimer_base *base = pct->bases;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < CPUCLOCK_MAX; i++, base++) {
|
2019-08-27 22:31:02 +03:00
|
|
|
base->nextevt = collect_timerqueue(&base->tqhead, firing,
|
|
|
|
samples[i]);
|
2019-08-21 22:09:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:10:24 +03:00
|
|
|
static inline void check_dl_overrun(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (tsk->dl.dl_overrun) {
|
|
|
|
tsk->dl.dl_overrun = 0;
|
2022-04-22 17:28:50 +03:00
|
|
|
send_signal_locked(SIGXCPU, SEND_SIG_PRIV, tsk, PIDTYPE_TGID);
|
2017-12-12 14:10:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 22:09:23 +03:00
|
|
|
static bool check_rlimit(u64 time, u64 limit, int signo, bool rt, bool hard)
|
|
|
|
{
|
|
|
|
if (time < limit)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (print_fatal_signals) {
|
|
|
|
pr_info("%s Watchdog Timeout (%s): %s[%d]\n",
|
|
|
|
rt ? "RT" : "CPU", hard ? "hard" : "soft",
|
|
|
|
current->comm, task_pid_nr(current));
|
|
|
|
}
|
2022-04-22 17:28:50 +03:00
|
|
|
send_signal_locked(signo, SEND_SIG_PRIV, current, PIDTYPE_TGID);
|
2019-08-21 22:09:23 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Check for any per-thread CPU timers that have fired and move them off
|
|
|
|
* the tsk->cpu_timers[N] list onto the firing list. Here we update the
|
|
|
|
* tsk->it_*_expires values to reflect the remaining thread CPU timers.
|
|
|
|
*/
|
|
|
|
static void check_thread_timers(struct task_struct *tsk,
|
|
|
|
struct list_head *firing)
|
|
|
|
{
|
2019-08-21 22:09:20 +03:00
|
|
|
struct posix_cputimers *pct = &tsk->posix_cputimers;
|
|
|
|
u64 samples[CPUCLOCK_MAX];
|
2010-03-06 00:42:53 +03:00
|
|
|
unsigned long soft;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2017-12-12 14:10:24 +03:00
|
|
|
if (dl_task(tsk))
|
|
|
|
check_dl_overrun(tsk);
|
|
|
|
|
2019-08-21 22:09:20 +03:00
|
|
|
if (expiry_cache_is_inactive(pct))
|
2015-10-14 22:07:54 +03:00
|
|
|
return;
|
|
|
|
|
2019-08-21 22:09:20 +03:00
|
|
|
task_sample_cputime(tsk, samples);
|
|
|
|
collect_posix_cputimers(pct, samples, firing);
|
2008-01-25 23:08:27 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the special case thread timers.
|
|
|
|
*/
|
2017-07-05 20:25:48 +03:00
|
|
|
soft = task_rlimit(tsk, RLIMIT_RTTIME);
|
2010-03-06 00:42:53 +03:00
|
|
|
if (soft != RLIM_INFINITY) {
|
2019-08-21 22:09:21 +03:00
|
|
|
/* Task RT timeout is accounted in jiffies. RTTIME is usec */
|
2019-08-21 22:09:23 +03:00
|
|
|
unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ);
|
2017-07-05 20:25:48 +03:00
|
|
|
unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME);
|
2008-01-25 23:08:27 +03:00
|
|
|
|
2019-08-21 22:09:23 +03:00
|
|
|
/* At the hard limit, send SIGKILL. No further action. */
|
|
|
|
if (hard != RLIM_INFINITY &&
|
|
|
|
check_rlimit(rttime, hard, SIGKILL, true, true))
|
2008-01-25 23:08:27 +03:00
|
|
|
return;
|
2019-08-21 22:09:22 +03:00
|
|
|
|
2019-08-21 22:09:23 +03:00
|
|
|
/* At the soft limit, send a SIGXCPU every second */
|
|
|
|
if (check_rlimit(rttime, soft, SIGXCPU, true, false)) {
|
2019-08-21 22:09:22 +03:00
|
|
|
soft += USEC_PER_SEC;
|
|
|
|
tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft;
|
2008-01-25 23:08:27 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-21 22:09:10 +03:00
|
|
|
|
2019-08-21 22:09:20 +03:00
|
|
|
if (expiry_cache_is_inactive(pct))
|
2015-07-17 23:25:49 +03:00
|
|
|
tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2015-04-28 23:00:22 +03:00
|
|
|
static inline void stop_process_timers(struct signal_struct *sig)
|
2009-02-10 18:37:31 +03:00
|
|
|
{
|
2019-08-21 22:09:24 +03:00
|
|
|
struct posix_cputimers *pct = &sig->posix_cputimers;
|
2009-02-10 18:37:31 +03:00
|
|
|
|
2019-08-21 22:09:24 +03:00
|
|
|
/* Turn off the active flag. This is done without locking. */
|
|
|
|
WRITE_ONCE(pct->timers_active, false);
|
2015-07-17 23:25:49 +03:00
|
|
|
tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
|
2009-02-10 18:37:31 +03:00
|
|
|
}
|
|
|
|
|
2009-07-29 14:15:26 +04:00
|
|
|
static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
|
2017-01-31 06:09:34 +03:00
|
|
|
u64 *expires, u64 cur_time, int signo)
|
2009-07-29 14:15:26 +04:00
|
|
|
{
|
2011-12-15 17:56:09 +04:00
|
|
|
if (!it->expires)
|
2009-07-29 14:15:26 +04:00
|
|
|
return;
|
|
|
|
|
2017-01-31 06:09:35 +03:00
|
|
|
if (cur_time >= it->expires) {
|
|
|
|
if (it->incr)
|
2011-12-15 17:56:09 +04:00
|
|
|
it->expires += it->incr;
|
2017-01-31 06:09:35 +03:00
|
|
|
else
|
2011-12-15 17:56:09 +04:00
|
|
|
it->expires = 0;
|
2009-07-29 14:15:26 +04:00
|
|
|
|
2009-08-10 06:52:30 +04:00
|
|
|
trace_itimer_expire(signo == SIGPROF ?
|
|
|
|
ITIMER_PROF : ITIMER_VIRTUAL,
|
2017-06-04 12:32:13 +03:00
|
|
|
task_tgid(tsk), cur_time);
|
2022-04-22 17:28:50 +03:00
|
|
|
send_signal_locked(signo, SEND_SIG_PRIV, tsk, PIDTYPE_TGID);
|
2009-07-29 14:15:26 +04:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:09:19 +03:00
|
|
|
if (it->expires && it->expires < *expires)
|
2017-01-31 06:09:35 +03:00
|
|
|
*expires = it->expires;
|
2009-07-29 14:15:26 +04:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Check for any per-thread CPU timers that have fired and move them
|
|
|
|
* off the tsk->*_timers list onto the firing list. Per-thread timers
|
|
|
|
* have already been taken off.
|
|
|
|
*/
|
|
|
|
static void check_process_timers(struct task_struct *tsk,
|
|
|
|
struct list_head *firing)
|
|
|
|
{
|
|
|
|
struct signal_struct *const sig = tsk->signal;
|
2019-08-21 22:09:20 +03:00
|
|
|
struct posix_cputimers *pct = &sig->posix_cputimers;
|
|
|
|
u64 samples[CPUCLOCK_MAX];
|
2010-03-06 00:42:53 +03:00
|
|
|
unsigned long soft;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2015-10-14 22:07:54 +03:00
|
|
|
/*
|
2019-08-21 22:09:24 +03:00
|
|
|
* If there are no active process wide timers (POSIX 1.b, itimers,
|
2019-08-29 13:52:28 +03:00
|
|
|
* RLIMIT_CPU) nothing to check. Also skip the process wide timer
|
|
|
|
* processing when there is already another task handling them.
|
2015-10-14 22:07:54 +03:00
|
|
|
*/
|
2019-08-29 13:52:28 +03:00
|
|
|
if (!READ_ONCE(pct->timers_active) || pct->expiry_active)
|
2015-10-14 22:07:54 +03:00
|
|
|
return;
|
|
|
|
|
2019-08-29 13:52:28 +03:00
|
|
|
/*
|
2015-10-14 22:07:56 +03:00
|
|
|
* Signify that a thread is checking for process timers.
|
|
|
|
* Write access to this field is protected by the sighand lock.
|
|
|
|
*/
|
2019-08-29 13:52:28 +03:00
|
|
|
pct->expiry_active = true;
|
2015-10-14 22:07:56 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2019-08-21 22:08:53 +03:00
|
|
|
* Collect the current process totals. Group accounting is active
|
|
|
|
* so the sample can be taken directly.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2019-08-21 22:09:16 +03:00
|
|
|
proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples);
|
2019-08-21 22:09:20 +03:00
|
|
|
collect_posix_cputimers(pct, samples, firing);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the special case process timers.
|
|
|
|
*/
|
2019-08-21 22:09:20 +03:00
|
|
|
check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF],
|
|
|
|
&pct->bases[CPUCLOCK_PROF].nextevt,
|
2019-08-21 22:09:16 +03:00
|
|
|
samples[CPUCLOCK_PROF], SIGPROF);
|
2019-08-21 22:09:20 +03:00
|
|
|
check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT],
|
|
|
|
&pct->bases[CPUCLOCK_VIRT].nextevt,
|
|
|
|
samples[CPUCLOCK_VIRT], SIGVTALRM);
|
2019-08-21 22:09:16 +03:00
|
|
|
|
2017-07-05 20:25:48 +03:00
|
|
|
soft = task_rlimit(tsk, RLIMIT_CPU);
|
2010-03-06 00:42:53 +03:00
|
|
|
if (soft != RLIM_INFINITY) {
|
2019-08-21 22:09:21 +03:00
|
|
|
/* RLIMIT_CPU is in seconds. Samples are nanoseconds */
|
2017-07-05 20:25:48 +03:00
|
|
|
unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU);
|
2019-08-21 22:09:21 +03:00
|
|
|
u64 ptime = samples[CPUCLOCK_PROF];
|
|
|
|
u64 softns = (u64)soft * NSEC_PER_SEC;
|
|
|
|
u64 hardns = (u64)hard * NSEC_PER_SEC;
|
2019-08-21 22:09:16 +03:00
|
|
|
|
2019-08-21 22:09:23 +03:00
|
|
|
/* At the hard limit, send SIGKILL. No further action. */
|
|
|
|
if (hard != RLIM_INFINITY &&
|
|
|
|
check_rlimit(ptime, hardns, SIGKILL, false, true))
|
2005-04-17 02:20:36 +04:00
|
|
|
return;
|
2019-08-21 22:09:22 +03:00
|
|
|
|
2019-08-21 22:09:23 +03:00
|
|
|
/* At the soft limit, send a SIGXCPU every second */
|
|
|
|
if (check_rlimit(ptime, softns, SIGXCPU, false, false)) {
|
2019-08-21 22:09:22 +03:00
|
|
|
sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1;
|
|
|
|
softns += NSEC_PER_SEC;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2019-08-21 22:09:21 +03:00
|
|
|
|
|
|
|
/* Update the expiry cache */
|
2019-08-21 22:09:20 +03:00
|
|
|
if (softns < pct->bases[CPUCLOCK_PROF].nextevt)
|
|
|
|
pct->bases[CPUCLOCK_PROF].nextevt = softns;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2019-08-21 22:09:20 +03:00
|
|
|
if (expiry_cache_is_inactive(pct))
|
2010-04-28 01:12:15 +04:00
|
|
|
stop_process_timers(sig);
|
2015-10-14 22:07:56 +03:00
|
|
|
|
2019-08-21 22:09:24 +03:00
|
|
|
pct->expiry_active = false;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-05-31 00:15:46 +03:00
|
|
|
* This is called from the signal code (via posixtimer_rearm)
|
2005-04-17 02:20:36 +04:00
|
|
|
* when the last timer signal was delivered and we have to reload the timer.
|
|
|
|
*/
|
2017-05-31 00:15:47 +03:00
|
|
|
static void posix_cpu_timer_rearm(struct k_itimer *timer)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-21 22:08:58 +03:00
|
|
|
clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
|
2020-02-28 20:11:06 +03:00
|
|
|
struct task_struct *p;
|
2013-10-11 20:56:49 +04:00
|
|
|
struct sighand_struct *sighand;
|
|
|
|
unsigned long flags;
|
2017-01-31 06:09:34 +03:00
|
|
|
u64 now;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2020-02-28 20:11:06 +03:00
|
|
|
rcu_read_lock();
|
|
|
|
p = cpu_timer_task_rcu(timer);
|
|
|
|
if (!p)
|
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2021-06-03 02:15:59 +03:00
|
|
|
/* Protect timer list r/w in arm_timer() */
|
|
|
|
sighand = lock_task_sighand(p, &flags);
|
|
|
|
if (unlikely(sighand == NULL))
|
|
|
|
goto out;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Fetch the current sample and update the timer's expiry time.
|
|
|
|
*/
|
2020-02-28 20:09:19 +03:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
2019-08-21 22:09:01 +03:00
|
|
|
now = cpu_clock_sample(clkid, p);
|
2020-02-28 20:09:19 +03:00
|
|
|
else
|
2019-08-21 22:09:01 +03:00
|
|
|
now = cpu_clock_sample_group(clkid, p, true);
|
2020-02-28 20:09:19 +03:00
|
|
|
|
|
|
|
bump_cpu_timer(timer, now);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Now re-arm for the new expiry time.
|
|
|
|
*/
|
2020-02-28 20:09:46 +03:00
|
|
|
arm_timer(timer, p);
|
2013-10-11 20:56:49 +04:00
|
|
|
unlock_task_sighand(p, &flags);
|
2020-02-28 20:11:06 +03:00
|
|
|
out:
|
|
|
|
rcu_read_unlock();
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
/**
|
2019-08-26 21:22:24 +03:00
|
|
|
* task_cputimers_expired - Check whether posix CPU timers are expired
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
*
|
2019-08-21 22:09:13 +03:00
|
|
|
* @samples: Array of current samples for the CPUCLOCK clocks
|
2019-08-26 21:22:24 +03:00
|
|
|
* @pct: Pointer to a posix_cputimers container
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
*
|
2019-08-26 21:22:24 +03:00
|
|
|
* Returns true if any member of @samples is greater than the corresponding
|
|
|
|
* member of @pct->bases[CLK].nextevt. False otherwise
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
*/
|
2019-08-26 21:22:24 +03:00
|
|
|
static inline bool
|
2019-10-21 10:44:12 +03:00
|
|
|
task_cputimers_expired(const u64 *samples, struct posix_cputimers *pct)
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
{
|
2019-08-21 22:09:13 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < CPUCLOCK_MAX; i++) {
|
2019-10-21 10:44:12 +03:00
|
|
|
if (samples[i] >= pct->bases[i].nextevt)
|
2019-08-21 22:09:13 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fastpath_timer_check - POSIX CPU timers fast path.
|
|
|
|
*
|
|
|
|
* @tsk: The task (thread) being checked.
|
|
|
|
*
|
2008-09-12 20:54:39 +04:00
|
|
|
* Check the task and thread group timers. If both are zero (there are no
|
|
|
|
* timers set) return false. Otherwise snapshot the task and thread group
|
|
|
|
* timers and compare them with the corresponding expiration times. Return
|
|
|
|
* true if a timer has expired, else return false.
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
*/
|
2019-08-21 22:09:13 +03:00
|
|
|
static inline bool fastpath_timer_check(struct task_struct *tsk)
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
{
|
2019-08-21 22:09:24 +03:00
|
|
|
struct posix_cputimers *pct = &tsk->posix_cputimers;
|
2008-11-17 17:39:47 +03:00
|
|
|
struct signal_struct *sig;
|
2008-09-12 20:54:39 +04:00
|
|
|
|
2019-08-21 22:09:24 +03:00
|
|
|
if (!expiry_cache_is_inactive(pct)) {
|
2019-08-21 22:09:13 +03:00
|
|
|
u64 samples[CPUCLOCK_MAX];
|
2008-09-12 20:54:39 +04:00
|
|
|
|
2019-08-21 22:09:13 +03:00
|
|
|
task_sample_cputime(tsk, samples);
|
2019-08-21 22:09:24 +03:00
|
|
|
if (task_cputimers_expired(samples, pct))
|
2019-08-21 22:09:13 +03:00
|
|
|
return true;
|
2008-09-12 20:54:39 +04:00
|
|
|
}
|
2008-11-17 17:39:47 +03:00
|
|
|
|
|
|
|
sig = tsk->signal;
|
2019-08-21 22:09:24 +03:00
|
|
|
pct = &sig->posix_cputimers;
|
2015-10-14 22:07:56 +03:00
|
|
|
/*
|
2019-08-21 22:09:24 +03:00
|
|
|
* Check if thread group timers expired when timers are active and
|
|
|
|
* no other thread in the group is already handling expiry for
|
|
|
|
* thread group cputimers. These fields are read without the
|
|
|
|
* sighand lock. However, this is fine because this is meant to be
|
|
|
|
* a fastpath heuristic to determine whether we should try to
|
|
|
|
* acquire the sighand lock to handle timer expiry.
|
2015-10-14 22:07:56 +03:00
|
|
|
*
|
2019-08-21 22:09:24 +03:00
|
|
|
* In the worst case scenario, if concurrently timers_active is set
|
|
|
|
* or expiry_active is cleared, but the current thread doesn't see
|
|
|
|
* the change yet, the timer checks are delayed until the next
|
|
|
|
* thread in the group gets a scheduler interrupt to handle the
|
|
|
|
* timer. This isn't an issue in practice because these types of
|
|
|
|
* delays with signals actually getting sent are expected.
|
2015-10-14 22:07:56 +03:00
|
|
|
*/
|
2019-08-21 22:09:24 +03:00
|
|
|
if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) {
|
2019-08-21 22:09:13 +03:00
|
|
|
u64 samples[CPUCLOCK_MAX];
|
2008-09-12 20:54:39 +04:00
|
|
|
|
2019-08-21 22:09:13 +03:00
|
|
|
proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic,
|
|
|
|
samples);
|
2010-06-11 22:04:46 +04:00
|
|
|
|
2019-08-21 22:09:24 +03:00
|
|
|
if (task_cputimers_expired(samples, pct))
|
2019-08-21 22:09:13 +03:00
|
|
|
return true;
|
2008-09-12 20:54:39 +04:00
|
|
|
}
|
2009-03-23 22:34:11 +03:00
|
|
|
|
2017-12-12 14:10:24 +03:00
|
|
|
if (dl_task(tsk) && tsk->dl.dl_overrun)
|
2019-08-21 22:09:13 +03:00
|
|
|
return true;
|
2017-12-12 14:10:24 +03:00
|
|
|
|
2019-08-21 22:09:13 +03:00
|
|
|
return false;
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
}
|
|
|
|
|
2020-07-30 13:14:06 +03:00
|
|
|
static void handle_posix_cpu_timers(struct task_struct *tsk);
|
|
|
|
|
|
|
|
#ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
|
|
|
|
static void posix_cpu_timers_work(struct callback_head *work)
|
|
|
|
{
|
2023-04-17 16:37:55 +03:00
|
|
|
struct posix_cputimers_work *cw = container_of(work, typeof(*cw), work);
|
|
|
|
|
|
|
|
mutex_lock(&cw->mutex);
|
2020-07-30 13:14:06 +03:00
|
|
|
handle_posix_cpu_timers(current);
|
2023-04-17 16:37:55 +03:00
|
|
|
mutex_unlock(&cw->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Invoked from the posix-timer core when a cancel operation failed because
|
|
|
|
* the timer is marked firing. The caller holds rcu_read_lock(), which
|
|
|
|
* protects the timer and the task which is expiring it from being freed.
|
|
|
|
*/
|
|
|
|
static void posix_cpu_timer_wait_running(struct k_itimer *timr)
|
|
|
|
{
|
|
|
|
struct task_struct *tsk = rcu_dereference(timr->it.cpu.handling);
|
|
|
|
|
|
|
|
/* Has the handling task completed expiry already? */
|
|
|
|
if (!tsk)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Ensure that the task cannot go away */
|
|
|
|
get_task_struct(tsk);
|
|
|
|
/* Now drop the RCU protection so the mutex can be locked */
|
|
|
|
rcu_read_unlock();
|
|
|
|
/* Wait on the expiry mutex */
|
|
|
|
mutex_lock(&tsk->posix_cputimers_work.mutex);
|
|
|
|
/* Release it immediately again. */
|
|
|
|
mutex_unlock(&tsk->posix_cputimers_work.mutex);
|
|
|
|
/* Drop the task reference. */
|
|
|
|
put_task_struct(tsk);
|
|
|
|
/* Relock RCU so the callsite is balanced */
|
|
|
|
rcu_read_lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void posix_cpu_timer_wait_running_nsleep(struct k_itimer *timr)
|
|
|
|
{
|
|
|
|
/* Ensure that timr->it.cpu.handling task cannot go away */
|
|
|
|
rcu_read_lock();
|
|
|
|
spin_unlock_irq(&timr->it_lock);
|
|
|
|
posix_cpu_timer_wait_running(timr);
|
|
|
|
rcu_read_unlock();
|
|
|
|
/* @timr is on stack and is valid */
|
|
|
|
spin_lock_irq(&timr->it_lock);
|
2020-07-30 13:14:06 +03:00
|
|
|
}
|
|
|
|
|
2021-11-02 00:06:15 +03:00
|
|
|
/*
|
|
|
|
* Clear existing posix CPU timers task work.
|
|
|
|
*/
|
|
|
|
void clear_posix_cputimers_work(struct task_struct *p)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* A copied work entry from the old task is not meaningful, clear it.
|
|
|
|
* N.B. init_task_work will not do this.
|
|
|
|
*/
|
|
|
|
memset(&p->posix_cputimers_work.work, 0,
|
|
|
|
sizeof(p->posix_cputimers_work.work));
|
|
|
|
init_task_work(&p->posix_cputimers_work.work,
|
|
|
|
posix_cpu_timers_work);
|
2023-04-17 16:37:55 +03:00
|
|
|
mutex_init(&p->posix_cputimers_work.mutex);
|
2021-11-02 00:06:15 +03:00
|
|
|
p->posix_cputimers_work.scheduled = false;
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:14:06 +03:00
|
|
|
/*
|
|
|
|
* Initialize posix CPU timers task work in init task. Out of line to
|
|
|
|
* keep the callback static and to avoid header recursion hell.
|
|
|
|
*/
|
|
|
|
void __init posix_cputimers_init_work(void)
|
|
|
|
{
|
2021-11-02 00:06:15 +03:00
|
|
|
clear_posix_cputimers_work(current);
|
2020-07-30 13:14:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: All operations on tsk->posix_cputimer_work.scheduled happen either
|
|
|
|
* in hard interrupt context or in task context with interrupts
|
|
|
|
* disabled. Aside of that the writer/reader interaction is always in the
|
|
|
|
* context of the current task, which means they are strict per CPU.
|
|
|
|
*/
|
|
|
|
static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
return tsk->posix_cputimers_work.scheduled;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void __run_posix_cpu_timers(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (WARN_ON_ONCE(tsk->posix_cputimers_work.scheduled))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Schedule task work to actually expire the timers */
|
|
|
|
tsk->posix_cputimers_work.scheduled = true;
|
|
|
|
task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk,
|
|
|
|
unsigned long start)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On !RT kernels interrupts are disabled while collecting expired
|
|
|
|
* timers, so no tick can happen and the fast path check can be
|
|
|
|
* reenabled without further checks.
|
|
|
|
*/
|
|
|
|
if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
|
|
|
|
tsk->posix_cputimers_work.scheduled = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On RT enabled kernels ticks can happen while the expired timers
|
|
|
|
* are collected under sighand lock. But any tick which observes
|
|
|
|
* the CPUTIMERS_WORK_SCHEDULED bit set, does not run the fastpath
|
|
|
|
* checks. So reenabling the tick work has do be done carefully:
|
|
|
|
*
|
|
|
|
* Disable interrupts and run the fast path check if jiffies have
|
|
|
|
* advanced since the collecting of expired timers started. If
|
|
|
|
* jiffies have not advanced or the fast path check did not find
|
|
|
|
* newly expired timers, reenable the fast path check in the timer
|
|
|
|
* interrupt. If there are newly expired timers, return false and
|
|
|
|
* let the collection loop repeat.
|
|
|
|
*/
|
|
|
|
local_irq_disable();
|
|
|
|
if (start != jiffies && fastpath_timer_check(tsk))
|
|
|
|
ret = false;
|
|
|
|
else
|
|
|
|
tsk->posix_cputimers_work.scheduled = false;
|
|
|
|
local_irq_enable();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */
|
|
|
|
static inline void __run_posix_cpu_timers(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
lockdep_posixtimer_enter();
|
|
|
|
handle_posix_cpu_timers(tsk);
|
|
|
|
lockdep_posixtimer_exit();
|
|
|
|
}
|
|
|
|
|
2023-04-17 16:37:55 +03:00
|
|
|
static void posix_cpu_timer_wait_running(struct k_itimer *timr)
|
|
|
|
{
|
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void posix_cpu_timer_wait_running_nsleep(struct k_itimer *timr)
|
|
|
|
{
|
|
|
|
spin_unlock_irq(&timr->it_lock);
|
|
|
|
cpu_relax();
|
|
|
|
spin_lock_irq(&timr->it_lock);
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:14:06 +03:00
|
|
|
static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk,
|
|
|
|
unsigned long start)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */
|
|
|
|
|
|
|
|
static void handle_posix_cpu_timers(struct task_struct *tsk)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct k_itimer *timer, *next;
|
2020-07-30 13:14:06 +03:00
|
|
|
unsigned long flags, start;
|
2019-08-19 17:31:47 +03:00
|
|
|
LIST_HEAD(firing);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2020-07-30 13:14:05 +03:00
|
|
|
if (!lock_task_sighand(tsk, &flags))
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
return;
|
2008-09-14 19:11:46 +04:00
|
|
|
|
2020-07-30 13:14:06 +03:00
|
|
|
do {
|
|
|
|
/*
|
|
|
|
* On RT locking sighand lock does not disable interrupts,
|
|
|
|
* so this needs to be careful vs. ticks. Store the current
|
|
|
|
* jiffies value.
|
|
|
|
*/
|
|
|
|
start = READ_ONCE(jiffies);
|
|
|
|
barrier();
|
2015-10-14 22:07:54 +03:00
|
|
|
|
2020-07-30 13:14:06 +03:00
|
|
|
/*
|
|
|
|
* Here we take off tsk->signal->cpu_timers[N] and
|
|
|
|
* tsk->cpu_timers[N] all the timers that are firing, and
|
|
|
|
* put them on the firing list.
|
|
|
|
*/
|
|
|
|
check_thread_timers(tsk, &firing);
|
|
|
|
|
|
|
|
check_process_timers(tsk, &firing);
|
|
|
|
|
|
|
|
/*
|
2021-03-23 00:39:03 +03:00
|
|
|
* The above timer checks have updated the expiry cache and
|
2020-07-30 13:14:06 +03:00
|
|
|
* because nothing can have queued or modified timers after
|
|
|
|
* sighand lock was taken above it is guaranteed to be
|
|
|
|
* consistent. So the next timer interrupt fastpath check
|
|
|
|
* will find valid data.
|
|
|
|
*
|
|
|
|
* If timer expiry runs in the timer interrupt context then
|
|
|
|
* the loop is not relevant as timers will be directly
|
|
|
|
* expired in interrupt context. The stub function below
|
|
|
|
* returns always true which allows the compiler to
|
|
|
|
* optimize the loop out.
|
|
|
|
*
|
|
|
|
* If timer expiry is deferred to task work context then
|
|
|
|
* the following rules apply:
|
|
|
|
*
|
|
|
|
* - On !RT kernels no tick can have happened on this CPU
|
|
|
|
* after sighand lock was acquired because interrupts are
|
|
|
|
* disabled. So reenabling task work before dropping
|
|
|
|
* sighand lock and reenabling interrupts is race free.
|
|
|
|
*
|
|
|
|
* - On RT kernels ticks might have happened but the tick
|
|
|
|
* work ignored posix CPU timer handling because the
|
|
|
|
* CPUTIMERS_WORK_SCHEDULED bit is set. Reenabling work
|
|
|
|
* must be done very carefully including a check whether
|
|
|
|
* ticks have happened since the start of the timer
|
|
|
|
* expiry checks. posix_cpu_timers_enable_work() takes
|
|
|
|
* care of that and eventually lets the expiry checks
|
|
|
|
* run again.
|
|
|
|
*/
|
|
|
|
} while (!posix_cpu_timers_enable_work(tsk, start));
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-09-12 20:54:39 +04:00
|
|
|
/*
|
2020-07-30 13:14:06 +03:00
|
|
|
* We must release sighand lock before taking any timer's lock.
|
2008-09-12 20:54:39 +04:00
|
|
|
* There is a potential race with timer deletion here, as the
|
|
|
|
* siglock now protects our private firing list. We have set
|
|
|
|
* the firing flag in each timer, so that a deletion attempt
|
|
|
|
* that gets the timer lock before we do will give it up and
|
|
|
|
* spin until we've taken care of that timer below.
|
|
|
|
*/
|
2010-06-11 03:10:18 +04:00
|
|
|
unlock_task_sighand(tsk, &flags);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that all the timers on our list have the firing flag,
|
2011-03-31 05:57:33 +04:00
|
|
|
* no one will touch their list entries but us. We'll take
|
2005-04-17 02:20:36 +04:00
|
|
|
* each timer's lock before clearing its firing flag, so no
|
|
|
|
* timer call will interfere.
|
|
|
|
*/
|
2019-08-27 22:31:02 +03:00
|
|
|
list_for_each_entry_safe(timer, next, &firing, it.cpu.elist) {
|
2009-04-30 03:14:32 +04:00
|
|
|
int cpu_firing;
|
|
|
|
|
2020-07-30 13:14:06 +03:00
|
|
|
/*
|
|
|
|
* spin_lock() is sufficient here even independent of the
|
|
|
|
* expiry context. If expiry happens in hard interrupt
|
|
|
|
* context it's obvious. For task work context it's safe
|
|
|
|
* because all other operations on timer::it_lock happen in
|
|
|
|
* task context (syscall or exit).
|
|
|
|
*/
|
2005-04-17 02:20:36 +04:00
|
|
|
spin_lock(&timer->it_lock);
|
2019-08-27 22:31:02 +03:00
|
|
|
list_del_init(&timer->it.cpu.elist);
|
2009-04-30 03:14:32 +04:00
|
|
|
cpu_firing = timer->it.cpu.firing;
|
2005-04-17 02:20:36 +04:00
|
|
|
timer->it.cpu.firing = 0;
|
|
|
|
/*
|
|
|
|
* The firing flag is -1 if we collided with a reset
|
|
|
|
* of the timer, which already reported this
|
|
|
|
* almost-firing as an overrun. So don't generate an event.
|
|
|
|
*/
|
2009-04-30 03:14:32 +04:00
|
|
|
if (likely(cpu_firing >= 0))
|
2005-04-17 02:20:36 +04:00
|
|
|
cpu_timer_fire(timer);
|
2023-04-17 16:37:55 +03:00
|
|
|
/* See posix_cpu_timer_wait_running() */
|
|
|
|
rcu_assign_pointer(timer->it.cpu.handling, NULL);
|
2005-04-17 02:20:36 +04:00
|
|
|
spin_unlock(&timer->it_lock);
|
|
|
|
}
|
2020-07-30 13:14:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called from the timer interrupt handler. The irq handler has
|
|
|
|
* already updated our counts. We need to check if any timers fire now.
|
|
|
|
* Interrupts are disabled.
|
|
|
|
*/
|
|
|
|
void run_posix_cpu_timers(void)
|
|
|
|
{
|
|
|
|
struct task_struct *tsk = current;
|
|
|
|
|
|
|
|
lockdep_assert_irqs_disabled();
|
|
|
|
|
2020-07-30 13:14:06 +03:00
|
|
|
/*
|
|
|
|
* If the actual expiry is deferred to task work context and the
|
|
|
|
* work is already scheduled there is no point to do anything here.
|
|
|
|
*/
|
|
|
|
if (posix_cpu_timers_work_scheduled(tsk))
|
|
|
|
return;
|
|
|
|
|
2020-07-30 13:14:05 +03:00
|
|
|
/*
|
|
|
|
* The fast path checks that there are no expired thread or thread
|
|
|
|
* group timers. If that's so, just return.
|
|
|
|
*/
|
|
|
|
if (!fastpath_timer_check(tsk))
|
|
|
|
return;
|
|
|
|
|
|
|
|
__run_posix_cpu_timers(tsk);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-03-12 01:04:37 +03:00
|
|
|
* Set one of the process-wide special case CPU timers or RLIMIT_CPU.
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
* The tsk->sighand->siglock must be held by the caller.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2019-08-21 22:09:09 +03:00
|
|
|
void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
|
2017-01-31 06:09:35 +03:00
|
|
|
u64 *newval, u64 *oldval)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2019-08-26 21:22:24 +03:00
|
|
|
u64 now, *nextevt;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-08-21 22:09:09 +03:00
|
|
|
if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED))
|
2019-08-19 17:31:46 +03:00
|
|
|
return;
|
|
|
|
|
2019-08-26 21:22:24 +03:00
|
|
|
nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt;
|
2019-08-21 22:09:09 +03:00
|
|
|
now = cpu_clock_sample_group(clkid, tsk, true);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2019-08-21 22:08:59 +03:00
|
|
|
if (oldval) {
|
2010-03-12 01:04:37 +03:00
|
|
|
/*
|
|
|
|
* We are setting itimer. The *oldval is absolute and we update
|
|
|
|
* it to be relative, *newval argument is relative and we update
|
|
|
|
* it to be absolute.
|
|
|
|
*/
|
2011-12-15 17:56:09 +04:00
|
|
|
if (*oldval) {
|
2017-01-31 06:09:35 +03:00
|
|
|
if (*oldval <= now) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/* Just about to fire. */
|
2017-01-31 06:09:35 +03:00
|
|
|
*oldval = TICK_NSEC;
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
2017-01-31 06:09:35 +03:00
|
|
|
*oldval -= now;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 17:53:32 +03:00
|
|
|
if (*newval)
|
|
|
|
*newval += now;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-08-21 22:09:09 +03:00
|
|
|
* Update expiration cache if this is the earliest timer. CPUCLOCK_PROF
|
|
|
|
* expiry cache is also used by RLIMIT_CPU!.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2019-08-21 22:09:19 +03:00
|
|
|
if (*newval < *nextevt)
|
2019-08-26 21:22:24 +03:00
|
|
|
*nextevt = *newval;
|
2015-07-17 23:25:49 +03:00
|
|
|
|
2021-05-13 02:29:21 +03:00
|
|
|
tick_dep_set_signal(tsk, TICK_DEP_BIT_POSIX_TIMER);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2006-09-29 13:00:29 +04:00
|
|
|
static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
|
2017-06-14 00:29:14 +03:00
|
|
|
const struct timespec64 *rqtp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2017-06-07 11:42:26 +03:00
|
|
|
struct itimerspec64 it;
|
2017-06-14 00:29:14 +03:00
|
|
|
struct k_itimer timer;
|
|
|
|
u64 expires;
|
2005-04-17 02:20:36 +04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a temporary timer and then wait for it to go off.
|
|
|
|
*/
|
|
|
|
memset(&timer, 0, sizeof timer);
|
|
|
|
spin_lock_init(&timer.it_lock);
|
|
|
|
timer.it_clock = which_clock;
|
|
|
|
timer.it_overrun = -1;
|
|
|
|
error = posix_cpu_timer_create(&timer);
|
|
|
|
timer.it_process = current;
|
2019-08-27 22:31:02 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
if (!error) {
|
2017-03-26 22:04:17 +03:00
|
|
|
static struct itimerspec64 zero_it;
|
2017-06-07 11:42:31 +03:00
|
|
|
struct restart_block *restart;
|
2006-09-29 13:00:29 +04:00
|
|
|
|
2017-06-07 11:42:31 +03:00
|
|
|
memset(&it, 0, sizeof(it));
|
2017-06-07 11:42:26 +03:00
|
|
|
it.it_value = *rqtp;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
spin_lock_irq(&timer.it_lock);
|
2017-06-07 11:42:26 +03:00
|
|
|
error = posix_cpu_timer_set(&timer, flags, &it, NULL);
|
2005-04-17 02:20:36 +04:00
|
|
|
if (error) {
|
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!signal_pending(current)) {
|
2019-08-27 22:31:02 +03:00
|
|
|
if (!cpu_timer_getexpires(&timer.it.cpu)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2013-02-15 14:08:11 +04:00
|
|
|
* Our timer fired and was reset, below
|
|
|
|
* deletion can not fail.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2013-02-15 14:08:11 +04:00
|
|
|
posix_cpu_timer_del(&timer);
|
2005-04-17 02:20:36 +04:00
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block until cpu_timer_fire (or a signal) wakes us.
|
|
|
|
*/
|
|
|
|
__set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
schedule();
|
|
|
|
spin_lock_irq(&timer.it_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We were interrupted by a signal.
|
|
|
|
*/
|
2019-08-27 22:31:02 +03:00
|
|
|
expires = cpu_timer_getexpires(&timer.it.cpu);
|
2017-06-07 11:42:26 +03:00
|
|
|
error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
|
2013-02-15 14:08:11 +04:00
|
|
|
if (!error) {
|
2023-04-17 16:37:55 +03:00
|
|
|
/* Timer is now unarmed, deletion can not fail. */
|
2013-02-15 14:08:11 +04:00
|
|
|
posix_cpu_timer_del(&timer);
|
2023-04-17 16:37:55 +03:00
|
|
|
} else {
|
|
|
|
while (error == TIMER_RETRY) {
|
|
|
|
posix_cpu_timer_wait_running_nsleep(&timer);
|
|
|
|
error = posix_cpu_timer_del(&timer);
|
|
|
|
}
|
2013-02-15 14:08:11 +04:00
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2023-04-17 16:37:55 +03:00
|
|
|
spin_unlock_irq(&timer.it_lock);
|
2013-02-15 14:08:11 +04:00
|
|
|
|
2017-06-07 11:42:26 +03:00
|
|
|
if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* It actually did fire already.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-29 13:00:29 +04:00
|
|
|
error = -ERESTART_RESTARTBLOCK;
|
2017-06-07 11:42:26 +03:00
|
|
|
/*
|
|
|
|
* Report back to the user the time still remaining.
|
|
|
|
*/
|
2017-06-07 11:42:31 +03:00
|
|
|
restart = ¤t->restart_block;
|
2017-06-14 00:29:14 +03:00
|
|
|
restart->nanosleep.expires = expires;
|
2017-06-24 21:45:06 +03:00
|
|
|
if (restart->nanosleep.type != TT_NONE)
|
|
|
|
error = nanosleep_copyout(restart, &it.it_value);
|
2006-09-29 13:00:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
|
|
|
|
|
|
|
|
static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
|
2017-06-14 00:34:33 +03:00
|
|
|
const struct timespec64 *rqtp)
|
2006-09-29 13:00:29 +04:00
|
|
|
{
|
2015-02-13 02:01:14 +03:00
|
|
|
struct restart_block *restart_block = ¤t->restart_block;
|
2006-09-29 13:00:29 +04:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Diagnose required errors first.
|
|
|
|
*/
|
|
|
|
if (CPUCLOCK_PERTHREAD(which_clock) &&
|
|
|
|
(CPUCLOCK_PID(which_clock) == 0 ||
|
2017-04-13 18:32:16 +03:00
|
|
|
CPUCLOCK_PID(which_clock) == task_pid_vnr(current)))
|
2006-09-29 13:00:29 +04:00
|
|
|
return -EINVAL;
|
|
|
|
|
2017-06-07 11:42:26 +03:00
|
|
|
error = do_cpu_nanosleep(which_clock, flags, rqtp);
|
2006-09-29 13:00:29 +04:00
|
|
|
|
|
|
|
if (error == -ERESTART_RESTARTBLOCK) {
|
|
|
|
|
2011-02-01 16:51:20 +03:00
|
|
|
if (flags & TIMER_ABSTIME)
|
2006-09-29 13:00:29 +04:00
|
|
|
return -ERESTARTNOHAND;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2011-05-20 15:05:15 +04:00
|
|
|
restart_block->nanosleep.clockid = which_clock;
|
2021-02-01 20:46:41 +03:00
|
|
|
set_restart_fn(restart_block, posix_cpu_nsleep_restart);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2011-05-20 15:05:15 +04:00
|
|
|
clockid_t which_clock = restart_block->nanosleep.clockid;
|
2017-03-26 22:04:18 +03:00
|
|
|
struct timespec64 t;
|
2006-01-10 07:52:37 +03:00
|
|
|
|
2017-03-26 22:04:18 +03:00
|
|
|
t = ns_to_timespec64(restart_block->nanosleep.expires);
|
2006-01-10 07:52:37 +03:00
|
|
|
|
2017-06-07 11:42:26 +03:00
|
|
|
return do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2017-12-29 06:11:36 +03:00
|
|
|
#define PROCESS_CLOCK make_process_cpuclock(0, CPUCLOCK_SCHED)
|
|
|
|
#define THREAD_CLOCK make_thread_cpuclock(0, CPUCLOCK_SCHED)
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2006-01-10 07:52:27 +03:00
|
|
|
static int process_cpu_clock_getres(const clockid_t which_clock,
|
2017-03-26 22:04:15 +03:00
|
|
|
struct timespec64 *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
|
|
|
|
}
|
2006-01-10 07:52:27 +03:00
|
|
|
static int process_cpu_clock_get(const clockid_t which_clock,
|
2017-03-26 22:04:14 +03:00
|
|
|
struct timespec64 *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_get(PROCESS_CLOCK, tp);
|
|
|
|
}
|
|
|
|
static int process_cpu_timer_create(struct k_itimer *timer)
|
|
|
|
{
|
|
|
|
timer->it_clock = PROCESS_CLOCK;
|
|
|
|
return posix_cpu_timer_create(timer);
|
|
|
|
}
|
2006-01-10 07:52:27 +03:00
|
|
|
static int process_cpu_nsleep(const clockid_t which_clock, int flags,
|
2017-06-14 00:34:33 +03:00
|
|
|
const struct timespec64 *rqtp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2017-06-07 11:42:30 +03:00
|
|
|
return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2006-01-10 07:52:27 +03:00
|
|
|
static int thread_cpu_clock_getres(const clockid_t which_clock,
|
2017-03-26 22:04:15 +03:00
|
|
|
struct timespec64 *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_getres(THREAD_CLOCK, tp);
|
|
|
|
}
|
2006-01-10 07:52:27 +03:00
|
|
|
static int thread_cpu_clock_get(const clockid_t which_clock,
|
2017-03-26 22:04:14 +03:00
|
|
|
struct timespec64 *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_get(THREAD_CLOCK, tp);
|
|
|
|
}
|
|
|
|
static int thread_cpu_timer_create(struct k_itimer *timer)
|
|
|
|
{
|
|
|
|
timer->it_clock = THREAD_CLOCK;
|
|
|
|
return posix_cpu_timer_create(timer);
|
|
|
|
}
|
|
|
|
|
2017-05-26 12:03:11 +03:00
|
|
|
const struct k_clock clock_posix_cpu = {
|
2019-11-12 04:26:54 +03:00
|
|
|
.clock_getres = posix_cpu_clock_getres,
|
|
|
|
.clock_set = posix_cpu_clock_set,
|
|
|
|
.clock_get_timespec = posix_cpu_clock_get,
|
|
|
|
.timer_create = posix_cpu_timer_create,
|
|
|
|
.nsleep = posix_cpu_nsleep,
|
|
|
|
.timer_set = posix_cpu_timer_set,
|
|
|
|
.timer_del = posix_cpu_timer_del,
|
|
|
|
.timer_get = posix_cpu_timer_get,
|
|
|
|
.timer_rearm = posix_cpu_timer_rearm,
|
2023-04-17 16:37:55 +03:00
|
|
|
.timer_wait_running = posix_cpu_timer_wait_running,
|
2011-02-01 16:51:06 +03:00
|
|
|
};
|
|
|
|
|
2017-05-26 12:03:11 +03:00
|
|
|
const struct k_clock clock_process = {
|
2019-11-12 04:26:54 +03:00
|
|
|
.clock_getres = process_cpu_clock_getres,
|
|
|
|
.clock_get_timespec = process_cpu_clock_get,
|
|
|
|
.timer_create = process_cpu_timer_create,
|
|
|
|
.nsleep = process_cpu_nsleep,
|
2017-05-26 12:03:11 +03:00
|
|
|
};
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2017-05-26 12:03:11 +03:00
|
|
|
const struct k_clock clock_thread = {
|
2019-11-12 04:26:54 +03:00
|
|
|
.clock_getres = thread_cpu_clock_getres,
|
|
|
|
.clock_get_timespec = thread_cpu_clock_get,
|
|
|
|
.timer_create = thread_cpu_timer_create,
|
2017-05-26 12:03:11 +03:00
|
|
|
};
|