mirror of
https://github.com/hardkernel/linux.git
synced 2026-06-09 12:17:12 +09:00
getrusage: move thread_group_cputime_adjusted() outside of lock_task_sighand()
[ Upstream commit daa694e4137571b4ebec330f9a9b4d54aa8b8089 ] Patch series "getrusage: use sig->stats_lock", v2. This patch (of 2): thread_group_cputime() does its own locking, we can safely shift thread_group_cputime_adjusted() which does another for_each_thread loop outside of ->siglock protected section. This is also preparation for the next patch which changes getrusage() to use stats_lock instead of siglock, thread_group_cputime() takes the same lock. With the current implementation recursive read_seqbegin_or_lock() is fine, thread_group_cputime() can't enter the slow mode if the caller holds stats_lock, yet this looks more safe and better performance-wise. Link: https://lkml.kernel.org/r/20240122155023.GA26169@redhat.com Link: https://lkml.kernel.org/r/20240122155050.GA26205@redhat.com Signed-off-by: Oleg Nesterov <oleg@redhat.com> Reported-by: Dylan Hatch <dylanbhatch@google.com> Tested-by: Dylan Hatch <dylanbhatch@google.com> Cc: Eric W. Biederman <ebiederm@xmission.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
committed by
Sasha Levin
parent
eba76e4808
commit
d9fe6ef245
38
kernel/sys.c
38
kernel/sys.c
@@ -1778,17 +1778,19 @@ void getrusage(struct task_struct *p, int who, struct rusage *r)
|
|||||||
struct task_struct *t;
|
struct task_struct *t;
|
||||||
unsigned long flags;
|
unsigned long flags;
|
||||||
u64 tgutime, tgstime, utime, stime;
|
u64 tgutime, tgstime, utime, stime;
|
||||||
unsigned long maxrss = 0;
|
unsigned long maxrss;
|
||||||
|
struct mm_struct *mm;
|
||||||
struct signal_struct *sig = p->signal;
|
struct signal_struct *sig = p->signal;
|
||||||
|
|
||||||
memset((char *)r, 0, sizeof (*r));
|
memset(r, 0, sizeof(*r));
|
||||||
utime = stime = 0;
|
utime = stime = 0;
|
||||||
|
maxrss = 0;
|
||||||
|
|
||||||
if (who == RUSAGE_THREAD) {
|
if (who == RUSAGE_THREAD) {
|
||||||
task_cputime_adjusted(current, &utime, &stime);
|
task_cputime_adjusted(current, &utime, &stime);
|
||||||
accumulate_thread_rusage(p, r);
|
accumulate_thread_rusage(p, r);
|
||||||
maxrss = sig->maxrss;
|
maxrss = sig->maxrss;
|
||||||
goto out;
|
goto out_thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lock_task_sighand(p, &flags))
|
if (!lock_task_sighand(p, &flags))
|
||||||
@@ -1812,9 +1814,6 @@ void getrusage(struct task_struct *p, int who, struct rusage *r)
|
|||||||
fallthrough;
|
fallthrough;
|
||||||
|
|
||||||
case RUSAGE_SELF:
|
case RUSAGE_SELF:
|
||||||
thread_group_cputime_adjusted(p, &tgutime, &tgstime);
|
|
||||||
utime += tgutime;
|
|
||||||
stime += tgstime;
|
|
||||||
r->ru_nvcsw += sig->nvcsw;
|
r->ru_nvcsw += sig->nvcsw;
|
||||||
r->ru_nivcsw += sig->nivcsw;
|
r->ru_nivcsw += sig->nivcsw;
|
||||||
r->ru_minflt += sig->min_flt;
|
r->ru_minflt += sig->min_flt;
|
||||||
@@ -1834,19 +1833,24 @@ void getrusage(struct task_struct *p, int who, struct rusage *r)
|
|||||||
}
|
}
|
||||||
unlock_task_sighand(p, &flags);
|
unlock_task_sighand(p, &flags);
|
||||||
|
|
||||||
out:
|
if (who == RUSAGE_CHILDREN)
|
||||||
|
goto out_children;
|
||||||
|
|
||||||
|
thread_group_cputime_adjusted(p, &tgutime, &tgstime);
|
||||||
|
utime += tgutime;
|
||||||
|
stime += tgstime;
|
||||||
|
|
||||||
|
out_thread:
|
||||||
|
mm = get_task_mm(p);
|
||||||
|
if (mm) {
|
||||||
|
setmax_mm_hiwater_rss(&maxrss, mm);
|
||||||
|
mmput(mm);
|
||||||
|
}
|
||||||
|
|
||||||
|
out_children:
|
||||||
|
r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
|
||||||
r->ru_utime = ns_to_kernel_old_timeval(utime);
|
r->ru_utime = ns_to_kernel_old_timeval(utime);
|
||||||
r->ru_stime = ns_to_kernel_old_timeval(stime);
|
r->ru_stime = ns_to_kernel_old_timeval(stime);
|
||||||
|
|
||||||
if (who != RUSAGE_CHILDREN) {
|
|
||||||
struct mm_struct *mm = get_task_mm(p);
|
|
||||||
|
|
||||||
if (mm) {
|
|
||||||
setmax_mm_hiwater_rss(&maxrss, mm);
|
|
||||||
mmput(mm);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
|
SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
|
||||||
|
|||||||
Reference in New Issue
Block a user