Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1888464lql; Wed, 13 Mar 2024 10:54:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVDESS6JOOEfOE629MhjcPg4D6qqtJJQI9XNmDOCNhvOlDkl505s8TALBr5F7JhYIqvkHSqLGx8n8Uqp12/0altiuHAkoVnPMO1hGGScQ== X-Google-Smtp-Source: AGHT+IGMZt46SZI9MQ+HBG4NTS1A2IqfUypJcVkSpinKVvlE3FzURPqHcIJG6z9wdhBUnHcTjItp X-Received: by 2002:a17:90a:1344:b0:29c:67:115a with SMTP id y4-20020a17090a134400b0029c0067115amr4107858pjf.42.1710352475676; Wed, 13 Mar 2024 10:54:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710352475; cv=pass; d=google.com; s=arc-20160816; b=qzN4F5a9DMAV3WEiOzyEtACjYms2YMJyQ8dDGkJbL91H8dmKk2RKWvupb0wXei0077 BrCWU2mU2iy9c5tMkFbu6jr0vJ3bji399lKwEE6Dre/+Rvi5KcP4nL2O+N+NH8T0Rtpw 5GFV48dKDYHoe9fdmqKhJeqQeji+aHa3Rfe+OI3DL+VRBZfv9qsmIJ2gLyL5Ufn8dPVn xQrHEgAphbK1gCJTpFc/ckUYOzOE+Y5vF63Q7eVmIj9QxNUAmHtSeF0sHkL62W0Dc21H vBYwvkT7QVPlNgt8dOkX7Yr2iIfSToaes7qeiu0cu5jtP2KfhorgBy1K8d9uuvnoF7bC mntw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=pZTeGFBqmob8MnvjkCxnmx2ruYqo5gYDU33KVTU1h8k=; fh=QHYoePTVyKNoydmDXmfPR/lPlJYVhXqsBmqXV4JCFVY=; b=x+bEHSvK97k4hpL5nFdWAucZ+RGkxC1QTJ9HloHvcQ99z286C7xqQ6d+r1+GE7cLuI QD6TL5qhdRZZ8jQotHfJ6GdJV3zSqLOPDtpo55eM8QkYK8vvVn+FlNDJ4ET+c9nfPnLG XXPxaKgcqwJEyz/izayn+fARRo5yXNnjcrV+7VAITF4k1u64qXjUpFtS9pZJUODsMFlK 3Qa5GezfAqZ93Cv71HscFVTm8ddeF7VqrDZ/Gioo173+EUApmqDVxbmZre7aBDl86/cg KL3mMtvIL6RkYRHHfVQHBcaiFoiUkvfABZDY7e6eWYso2lICj/6IMKe5C8aVBBvH1Zrh 9jww==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nJBTfxNR; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-102087-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-102087-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s15-20020a17090aad8f00b0029c64d1fcfcsi1330515pjq.31.2024.03.13.10.54.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 10:54:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-102087-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nJBTfxNR; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-102087-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-102087-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 26669281669 for ; Wed, 13 Mar 2024 17:54:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 47BFE18BDB8; Wed, 13 Mar 2024 16:47:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nJBTfxNR" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4EC7E6FE3F; Wed, 13 Mar 2024 16:47:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348471; cv=none; b=ieuoPtSUkaru/HgeACkJfsrXdXxKhXcWaYyxOe0XJrr68oJ8nGj6g4t6uBsXF/Wc5+sWYzB+CXLt2kbcG6qVLTQjCspz6K6PYM7BVXgZdj/k4GTn/HqHUzH2Uv1aZ0xOnWXhxLo5N2z5mEnpcIqzCmvIS9LLAbAkRh7nBiq+Owk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348471; c=relaxed/simple; bh=hnIjlYPTknFCqZsr7fqhnFrv6OB7rf2f6hhyOXu6sLs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DD9cuxCtVCT2HAAtgGLJAwzW4/YUOEZcjRlCeyvRcxjCjcIpJgm81FStYkNLfg2kcHmQTgMFuhR9yMMwS1kCU55pPLIvMoZrbY9vwt6oegmsLl0kTAGh5ZSR0P15w3cCEvy5R+x5TI/NhSzFDRywnBTk+aOSyamuOeIoyNvb7Bo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nJBTfxNR; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 440C6C43390; Wed, 13 Mar 2024 16:47:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710348471; bh=hnIjlYPTknFCqZsr7fqhnFrv6OB7rf2f6hhyOXu6sLs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nJBTfxNRjmam5KP1hy4Jw3rzimGUO/DAEHZiu0thJ8V7Ya2HiqoZv8eY+F6P8DDWY MnJyg0syOy3q65Hi6uO3zpeo6BiuDFG6HKM1AHcyxyPEYuDRf4OfIwkmTkaL0Leo/7 a8E3f1xHX2FRqsLyDPiQYHnYS0KQQclWjoEnMUR8j4GVblsSqxHBCMRrLHyRWLWxev kCovex1f6fp+TriXHwmCKHzBst6qH3nSCSWE7A6/TGwUrWXHlhnJcOTecfPYlqmt2d S1ZT+69c+gUUTS1pDKUbJuWeYmjRpvgQN4EyXTVYxxe1lPwwzll+CRuvzxcYyEx4UX UJO7fiLpv2vZg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Oleg Nesterov , Dylan Hatch , "Eric W . Biederman" , Andrew Morton , Sasha Levin Subject: [PATCH 5.10 61/73] getrusage: move thread_group_cputime_adjusted() outside of lock_task_sighand() Date: Wed, 13 Mar 2024 12:46:28 -0400 Message-ID: <20240313164640.616049-62-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240313164640.616049-1-sashal@kernel.org> References: <20240313164640.616049-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.10.213-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.10.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.10.213-rc1 X-KernelTest-Deadline: 2024-03-15T16:46+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Oleg Nesterov [ 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 Reported-by: Dylan Hatch Tested-by: Dylan Hatch Cc: Eric W. Biederman Cc: Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- kernel/sys.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/kernel/sys.c b/kernel/sys.c index 8a53d858d7375..26c8783bd0757 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -1736,17 +1736,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)) @@ -1770,9 +1772,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; @@ -1792,19 +1791,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.43.0