From nobody Fri Dec 19 21:48:10 2025 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 9ADE618BDDC; Wed, 13 Mar 2024 16:47:53 +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=1710348473; cv=none; b=smMjY2KTGGeo2e2G3M40l0QeCMCHVIsJcOdZZDa4vBkmkWLTfV0nPcXaWnSOkyccVyRRLKpG5GhrgoPGJQmWQkr7kln6xxaMzI9GGdgWM1gaLRzLH+V5cd9/DRJs+NLpG15M36uYa+EoUn/aWEyLhLomtAO1p08zhvCtkvabLos= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348473; c=relaxed/simple; bh=9ppfcAmk5ARlmOn1RywsrvxcJ1ZO4QB48VYdpXKkrlY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qqJSXJjKeVxptLA4e+kGbR52y5I+YBHbjjjpHOW5/GOOk6Jg4q8d8eFJ9ZeRk1ChWRlOaWnom0XzV3PBUDl3LCuQyBJs9XTVFDVL5rYhtyE1JmUTMb3T9S4Bqyay/PEk7ydTbKAlRRud+URZHdY50M+zDCjbVs6GVrUpFjTRcEE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pt5iMp4T; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pt5iMp4T" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95571C43394; Wed, 13 Mar 2024 16:47:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710348473; bh=9ppfcAmk5ARlmOn1RywsrvxcJ1ZO4QB48VYdpXKkrlY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pt5iMp4TaIwNv8VMrGJwR3kJHfEG6cdBAsaBpOTYe7PZnEcduBcHHa/xUlzw8cwQp EeXh1oqfBGu25LG7/SHQmnQKeZdIefAv19Hh2yeaXrAgkSXJ+0RfHBYCfU30/DRQgw IVAwk4yyTeBcj7IA0QtUPlMivZTL/6/5y1n/gqdWhRYV/uRIvB5dmEdlhYtZdArak/ SzIE7J7lEp6jd6vDc0o6pOy94xiurWalseBXc01mG90QcsTd9k7gXBCbWGgoG1APel qNMTGPN5EowzIAGrHdjolhX6UKUo9kWidCPDZG5RqoGL3aCcIGt6A5MyNVFS5NLbDE /r1apsQYXZE7g== 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 63/73] getrusage: use sig->stats_lock rather than lock_task_sighand() Date: Wed, 13 Mar 2024 12:46:30 -0400 Message-ID: <20240313164640.616049-64-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: quoted-printable Content-Type: text/plain; charset="utf-8" From: Oleg Nesterov [ Upstream commit f7ec1cd5cc7ef3ad964b677ba82b8b77f1c93009 ] lock_task_sighand() can trigger a hard lockup. If NR_CPUS threads call getrusage() at the same time and the process has NR_THREADS, spin_lock_irq will spin with irqs disabled O(NR_CPUS * NR_THREADS) time. Change getrusage() to use sig->stats_lock, it was specifically designed for this type of use. This way it runs lockless in the likely case. TODO: - Change do_task_stat() to use sig->stats_lock too, then we can remove spin_lock_irq(siglock) in wait_task_zombie(). - Turn sig->stats_lock into seqcount_rwlock_t, this way the readers in the slow mode won't exclude each other. See https://lore.kernel.org/all/20230913154907.GA26210@redhat.com/ - stats_lock has to disable irqs because ->siglock can be taken in irq context, it would be very nice to change __exit_signal() to avoid the siglock->stats_lock dependency. Link: https://lkml.kernel.org/r/20240122155053.GA26214@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 | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/kernel/sys.c b/kernel/sys.c index f1ae8fa627145..efc213ae4c5ad 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -1739,7 +1739,9 @@ void getrusage(struct task_struct *p, int who, struct= rusage *r) unsigned long maxrss; struct mm_struct *mm; struct signal_struct *sig =3D p->signal; + unsigned int seq =3D 0; =20 +retry: memset(r, 0, sizeof(*r)); utime =3D stime =3D 0; maxrss =3D 0; @@ -1751,8 +1753,7 @@ void getrusage(struct task_struct *p, int who, struct= rusage *r) goto out_thread; } =20 - if (!lock_task_sighand(p, &flags)) - return; + flags =3D read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq); =20 switch (who) { case RUSAGE_BOTH: @@ -1780,14 +1781,23 @@ void getrusage(struct task_struct *p, int who, stru= ct rusage *r) r->ru_oublock +=3D sig->oublock; if (maxrss < sig->maxrss) maxrss =3D sig->maxrss; + + rcu_read_lock(); __for_each_thread(sig, t) accumulate_thread_rusage(t, r); + rcu_read_unlock(); + break; =20 default: BUG(); } - unlock_task_sighand(p, &flags); + + if (need_seqretry(&sig->stats_lock, seq)) { + seq =3D 1; + goto retry; + } + done_seqretry_irqrestore(&sig->stats_lock, seq, flags); =20 if (who =3D=3D RUSAGE_CHILDREN) goto out_children; --=20 2.43.0