From mboxrd@z Thu Jan 1 00:00:00 1970 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 80402151CCC; Tue, 20 Feb 2024 21:36:28 +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=1708464988; cv=none; b=M/2Qbj9yqNw5Icw0FQKYJhBOQJQlHtYMENj5d8VfLhg+AHPsSEGliLmE/TtDtf+REIu05VBgdnC4yNOX9ZEZSgVYimaIhoXOxg8ySHO7PPL7IZDAOo160SKxZGgcAGIglM7bL+q2mvfATG/lpH5mXgVG9g2tizAzTqShV6Hythc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708464988; c=relaxed/simple; bh=JgpH/NGl2V2BvVZbc9eaWmwlknfFV0Qj+58A/EiR5Y4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p7a8/nmMAa2z0vOYV/3clkOG5LrqS5mQBTaDQWUOVSIB692w9F3kFqPNns1LWDL7vHIaQkxtlVV42c1V/9EXUNVnrAyKbdu9lcGO5+z/M4sol5vGfG4FXUecZdnEpdXynwtNXme5+43jhBMVHXa/gIvMUZJhIwGmCA591rykFQM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=JAXkW8Il; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="JAXkW8Il" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0A8A8C433C7; Tue, 20 Feb 2024 21:36:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1708464988; bh=JgpH/NGl2V2BvVZbc9eaWmwlknfFV0Qj+58A/EiR5Y4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JAXkW8IlPuH4TqS0YPq38lyoTI/m5wo589l1C1LefD19W3IpHiKmfnfrG5TZcaB9/ 9mvfqdQ2x44FqLykHJGhnCN/VpVn/zbXHyHZ/35uOcLkZ5CQzH/HDIIio3CvMahKAl JL+ITCtG1dHMyxaFt034quUC8ziZf9BGUvxZMF58= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Oleg Nesterov , Dylan Hatch , "Eric W. Biederman" , Andrew Morton Subject: [PATCH 6.7 201/309] getrusage: use sig->stats_lock rather than lock_task_sighand() Date: Tue, 20 Feb 2024 21:56:00 +0100 Message-ID: <20240220205639.477709536@linuxfoundation.org> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240220205633.096363225@linuxfoundation.org> References: <20240220205633.096363225@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.7-stable review patch. If anyone has any objections, please let me know. ------------------ From: Oleg Nesterov commit f7ec1cd5cc7ef3ad964b677ba82b8b77f1c93009 upstream. 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: Greg Kroah-Hartman --- kernel/sys.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) --- a/kernel/sys.c +++ b/kernel/sys.c @@ -1788,7 +1788,9 @@ void getrusage(struct task_struct *p, in unsigned long maxrss; struct mm_struct *mm; struct signal_struct *sig = p->signal; + unsigned int seq = 0; +retry: memset(r, 0, sizeof(*r)); utime = stime = 0; maxrss = 0; @@ -1800,8 +1802,7 @@ void getrusage(struct task_struct *p, in goto out_thread; } - if (!lock_task_sighand(p, &flags)) - return; + flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq); switch (who) { case RUSAGE_BOTH: @@ -1829,14 +1830,23 @@ void getrusage(struct task_struct *p, in r->ru_oublock += sig->oublock; if (maxrss < sig->maxrss) maxrss = sig->maxrss; + + rcu_read_lock(); __for_each_thread(sig, t) accumulate_thread_rusage(t, r); + rcu_read_unlock(); + break; default: BUG(); } - unlock_task_sighand(p, &flags); + + if (need_seqretry(&sig->stats_lock, seq)) { + seq = 1; + goto retry; + } + done_seqretry_irqrestore(&sig->stats_lock, seq, flags); if (who == RUSAGE_CHILDREN) goto out_children;