Received: by 2002:a05:7412:7c14:b0:fa:6e18:a558 with SMTP id ii20csp452335rdb; Mon, 22 Jan 2024 09:07:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IGM6TxhWrVvOKsdN2rVPasoRa5CwOdTD2xuhZrBVBvmRuEAddXOFtkg9uxpET+BMXCEO3DA X-Received: by 2002:aa7:dd54:0:b0:559:d75f:4902 with SMTP id o20-20020aa7dd54000000b00559d75f4902mr126636edw.74.1705943235737; Mon, 22 Jan 2024 09:07:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705943235; cv=pass; d=google.com; s=arc-20160816; b=Q5gSb9wYuOYMKvnBOvzPm9tsTaBB3UWSYXgjUzTpXigTavoqrTCiHY7DXGLBY7YTIZ Q1RAOX9nOqLSbMPSK73vPbY9m/1nT/QoKE9H7jSwo0IV5bCBDKxRnmnk6aud6pT7TEK1 jFeqGFvF0FoY9efypWUgnKC4Phe2K55QPkJLFFJo91eXBY/uUSOe3+MIXn/zQnJ0Mo35 In89fxcx7TZYVden+lvXbxMd9u8gB2LbV+zTJLWQ+LUN6FnYKeEskg2PpOMoPSYsfTSq 2Ay/hoPDuEmLpfP1kwWRaPktZv3tNS9QPGH6P52p8dDLjzH6KJveZiSnlCkR/m3wD+2h YONA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:message-id :subject:cc:to:from:date:dkim-signature; bh=/CvXrObtjsC2jxDLLgV2Oyu6BbmZSwCCJ7Mdf9Rmjzc=; fh=NrPUQ5fb2LYhfrBFB5+JVUXAypO1HjJgVIb7BqDPkhw=; b=O7TqU10K+XAZGoudWFKnM8RZbvn3U11qn0FW14Td8aEGu/dp0VuDGOx0J3lr5oJp8A V88Wa6G5DNsIi5rSOPYe0OnFkrx9PR4+Df9xPpVIJwy4OklGVP6I77ZfqNTfkNzGUFPT tS6YNbJg2ndhOWH5C59t/U0MqQRzPLCT4oka3EfBbYaYf44ntOdSwinC8jvfIZNnfXZM zjMfnp7qi2QV+4OWHBdhKqF7Or21ZAehmeVYLV1b2gqjl/ZJFNW2CzQqEbbK3FI50A2b RW+VRLtxg+zP9nfNUjBSXE4YQzyUsPXx3kr45ijXICz5aFpuyEGpfGHekUhOLNBYAvML 7lwg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RPbdCksU; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-33630-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33630-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id r10-20020a056402234a00b0055c05b981d7si2245036eda.207.2024.01.22.09.07.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 09:07:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-33630-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RPbdCksU; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-33630-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33630-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 356FA1F21437 for ; Mon, 22 Jan 2024 17:07:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5756A4A999; Mon, 22 Jan 2024 15:52:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="RPbdCksU" 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 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-Type: text/plain; charset=us-ascii 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 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, struct rusage *r) struct task_struct *t; unsigned long flags; u64 tgutime, tgstime, utime, stime; - unsigned long maxrss = 0; + unsigned long maxrss; + struct mm_struct *mm; struct signal_struct *sig = p->signal; - memset((char *)r, 0, sizeof (*r)); + memset(r, 0, sizeof(*r)); utime = stime = 0; + maxrss = 0; if (who == RUSAGE_THREAD) { task_cputime_adjusted(current, &utime, &stime); accumulate_thread_rusage(p, r); maxrss = sig->maxrss; - goto out; + goto out_thread; } if (!lock_task_sighand(p, &flags)) @@ -1819,9 +1821,6 @@ void getrusage(struct task_struct *p, int who, struct rusage *r) fallthrough; case RUSAGE_SELF: - thread_group_cputime_adjusted(p, &tgutime, &tgstime); - utime += tgutime; - stime += tgstime; r->ru_nvcsw += sig->nvcsw; r->ru_nivcsw += sig->nivcsw; r->ru_minflt += sig->min_flt; @@ -1839,19 +1838,24 @@ void getrusage(struct task_struct *p, int who, struct rusage *r) } unlock_task_sighand(p, &flags); -out: - r->ru_utime = ns_to_kernel_old_timeval(utime); - r->ru_stime = ns_to_kernel_old_timeval(stime); + if (who == RUSAGE_CHILDREN) + goto out_children; - if (who != RUSAGE_CHILDREN) { - struct mm_struct *mm = get_task_mm(p); + thread_group_cputime_adjusted(p, &tgutime, &tgstime); + utime += tgutime; + stime += tgstime; - if (mm) { - setmax_mm_hiwater_rss(&maxrss, mm); - mmput(mm); - } +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_stime = ns_to_kernel_old_timeval(stime); } SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru) -- 2.25.1.362.g51ebf55