From nobody Tue Feb 10 03:00:14 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F38893DB8D for ; Mon, 22 Jan 2024 15:52:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705938733; cv=none; b=ukti1mVnNwwjYiEgRMXy+Oom/8g6oXrmalWhIboPrb4mvDGE+kLPee+Hm7BNBn61S+tMvDKQl2qO90nN4OAdvfkFtuSysarqTXqMxq4lMIjf91S0EsLTy2+0n5KMHDLY5oqkcN9ekpxRMWpeKUjrmSInzTBWSn95DroQ+Y6GPLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705938733; c=relaxed/simple; bh=7De9Fv+OD2bqtwV/7+HprxG7Hd4QITb+OxusYx+b06Y=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=t2tk+NElTSQwdoupu6GGazexUd4ymu3zSP5w94AM2W3rraMt164D1+kIkeBS9zafOrvN+KEwOPic3oDoNUxxkraogNfRfkCqUUIwd/rQeSVAHHx7AA04plra9xSPLNrYRRWU5RxnZN5wPiKDqZqaMEfqGWFyqKKD+veC4NOWkT0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=RPbdCksU; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="RPbdCksU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705938730; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=/CvXrObtjsC2jxDLLgV2Oyu6BbmZSwCCJ7Mdf9Rmjzc=; b=RPbdCksUx1jh6kFE06wLRkwLE6tfCjD0XSdj4GldDLwtqdBTeaYlxOF4yICucFjl7mn0Ne 8WMWRR78nAO8SE6+JeArMWvzKGHpgFhF9sedCzxFlYe5Z4ftVtRu4bVGIeimYdlQ5cOjtT IO4hsOt7rnxbnlu1FwA20K91GGpVMME= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-478-aDoSptkTPciUiorPUn7HbQ-1; Mon, 22 Jan 2024 10:52:07 -0500 X-MC-Unique: aDoSptkTPciUiorPUn7HbQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 207DA3C0008A; Mon, 22 Jan 2024 15:52:06 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.26]) by smtp.corp.redhat.com (Postfix) with SMTP id E0202111E408; Mon, 22 Jan 2024 15:52:04 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Mon, 22 Jan 2024 16:50:52 +0100 (CET) Date: Mon, 22 Jan 2024 16:50:50 +0100 From: Oleg Nesterov To: Andrew Morton Cc: "Eric W. Biederman" , Dylan Hatch , linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] getrusage: move thread_group_cputime_adjusted() outside of lock_task_sighand() Message-ID: <20240122155050.GA26205@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240122155023.GA26169@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Reported-and-tested-by: Dylan Hatch Signed-off-by: Oleg Nesterov --- kernel/sys.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/kernel/sys.c b/kernel/sys.c index e219fcfa112d..70ad06ad852e 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -1785,17 +1785,19 @@ void getrusage(struct task_struct *p, int who, stru= ct rusage *r) struct task_struct *t; unsigned long flags; u64 tgutime, tgstime, utime, stime; - unsigned long maxrss =3D 0; + unsigned long maxrss; + struct mm_struct *mm; struct signal_struct *sig =3D p->signal; =20 - memset((char *)r, 0, sizeof (*r)); + memset(r, 0, sizeof(*r)); utime =3D stime =3D 0; + maxrss =3D 0; =20 if (who =3D=3D RUSAGE_THREAD) { task_cputime_adjusted(current, &utime, &stime); accumulate_thread_rusage(p, r); maxrss =3D sig->maxrss; - goto out; + goto out_thread; } =20 if (!lock_task_sighand(p, &flags)) @@ -1819,9 +1821,6 @@ void getrusage(struct task_struct *p, int who, struct= rusage *r) fallthrough; =20 case RUSAGE_SELF: - thread_group_cputime_adjusted(p, &tgutime, &tgstime); - utime +=3D tgutime; - stime +=3D tgstime; r->ru_nvcsw +=3D sig->nvcsw; r->ru_nivcsw +=3D sig->nivcsw; r->ru_minflt +=3D sig->min_flt; @@ -1839,19 +1838,24 @@ void getrusage(struct task_struct *p, int who, stru= ct rusage *r) } unlock_task_sighand(p, &flags); =20 -out: - r->ru_utime =3D ns_to_kernel_old_timeval(utime); - r->ru_stime =3D ns_to_kernel_old_timeval(stime); + if (who =3D=3D RUSAGE_CHILDREN) + goto out_children; =20 - if (who !=3D RUSAGE_CHILDREN) { - struct mm_struct *mm =3D get_task_mm(p); + thread_group_cputime_adjusted(p, &tgutime, &tgstime); + utime +=3D tgutime; + stime +=3D tgstime; =20 - if (mm) { - setmax_mm_hiwater_rss(&maxrss, mm); - mmput(mm); - } +out_thread: + mm =3D get_task_mm(p); + if (mm) { + setmax_mm_hiwater_rss(&maxrss, mm); + mmput(mm); } + +out_children: r->ru_maxrss =3D maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */ + r->ru_utime =3D ns_to_kernel_old_timeval(utime); + r->ru_stime =3D ns_to_kernel_old_timeval(stime); } =20 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru) --=20 2.25.1.362.g51ebf55