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 4DDF0481A6 for ; Mon, 19 Feb 2024 18:11: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=1708366311; cv=none; b=ZmEqBVj9y/AXPob50qGML8U6K6MTXJPIVLoZTcCZYuNnZCEwJbwRyECMzvxlmVlUqRMMgKucl1Kfvb0sf5L86XVHzmQ0f/mT/k1ZkRzoxQJM8rJQZi+2aZpdbGXjov38m6yW2jyyBX5y202zs7FmTw0OMsfC86tu6mkf15bTRM8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708366311; c=relaxed/simple; bh=NcgbRnBk5yvuwFEm49lZr76WW/lfS1TDY6vTh7vgMJ0=; h=Subject:To:Cc:From:Date:Message-ID:MIME-Version:Content-Type; b=YWZG95j3wdaQd5YAH7Ab96KIXeeABUT0O7aZrWceRZz5OgkGrWqE2QVSQTNswzdqMTo1r/3qKmj9D7gVIC11dxL7eAAjCEjUdiNtYHCWtKuIVAc3dMg+d5AD2YCvVIl9r5oNm3/X4aDe9MECxY5zLhLAfFPJ7YxGw8YFVycNgzQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=Kpv64+Vb; 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="Kpv64+Vb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA2B7C433C7; Mon, 19 Feb 2024 18:11:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1708366311; bh=NcgbRnBk5yvuwFEm49lZr76WW/lfS1TDY6vTh7vgMJ0=; h=Subject:To:Cc:From:Date:From; b=Kpv64+Vbo9Tot1d3cD0oN4L/hrO1EwHt1KFpLLRVCiah4u7TiQVw/dRcT8I7gFOcn NQdsfDSDcTJ4c/V4wozjThTaxFXAVoXO6nO+Jq/O3gy7a6dK1jGUDpH8AOPUYmIzWb 4r86A54e5mq5cjk1Yba0syN+ELMBnvwUI6FJ9lTY= Subject: FAILED: patch "[PATCH] getrusage: use sig->stats_lock rather than" failed to apply to 6.1-stable tree To: oleg@redhat.com,akpm@linux-foundation.org,dylanbhatch@google.com,ebiederm@xmission.com,stable@vger.kernel.org Cc: From: Date: Mon, 19 Feb 2024 19:11:35 +0100 Message-ID: <2024021934-version-stamp-e8f4@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit The patch below does not apply to the 6.1-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . To reproduce the conflict and resubmit, you may use the following commands: git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y git checkout FETCH_HEAD git cherry-pick -x f7ec1cd5cc7ef3ad964b677ba82b8b77f1c93009 # git commit -s git send-email --to '' --in-reply-to '2024021934-version-stamp-e8f4@gregkh' --subject-prefix 'PATCH 6.1.y' HEAD^.. Possible dependencies: f7ec1cd5cc7e ("getrusage: use sig->stats_lock rather than lock_task_sighand()") daa694e41375 ("getrusage: move thread_group_cputime_adjusted() outside of lock_task_sighand()") 13b7bc60b535 ("getrusage: use __for_each_thread()") c7ac8231ace9 ("getrusage: add the "signal_struct *sig" local variable") thanks, greg k-h ------------------ original commit in Linus's tree ------------------ >From f7ec1cd5cc7ef3ad964b677ba82b8b77f1c93009 Mon Sep 17 00:00:00 2001 From: Oleg Nesterov Date: Mon, 22 Jan 2024 16:50:53 +0100 Subject: [PATCH] getrusage: use sig->stats_lock rather than lock_task_sighand() 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 diff --git a/kernel/sys.c b/kernel/sys.c index 70ad06ad852e..f8e543f1e38a 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -1788,7 +1788,9 @@ void getrusage(struct task_struct *p, int who, struct rusage *r) 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, int who, struct rusage *r) 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, int who, struct rusage *r) 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;