mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Oleg Nesterov <oleg@redhat.com>,
	Dylan Hatch <dylanbhatch@google.com>,
	"Eric W . Biederman" <ebiederm@xmission.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Sasha Levin <sashal@kernel.org>
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	[thread overview]
Message-ID: <20240313164640.616049-64-sashal@kernel.org> (raw)
In-Reply-To: <20240313164640.616049-1-sashal@kernel.org>

From: Oleg Nesterov <oleg@redhat.com>

[ 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 <oleg@redhat.com>
Reported-by: Dylan Hatch <dylanbhatch@google.com>
Tested-by: Dylan Hatch <dylanbhatch@google.com>
Cc: Eric W. Biederman <ebiederm@xmission.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 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 = p->signal;
+	unsigned int seq = 0;
 
+retry:
 	memset(r, 0, sizeof(*r));
 	utime = stime = 0;
 	maxrss = 0;
@@ -1751,8 +1753,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:
@@ -1780,14 +1781,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;
-- 
2.43.0


  parent reply	other threads:[~2024-03-13 16:47 UTC|newest]

Thread overview: 98+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-13 16:45 [PATCH 5.10 00/73] 5.10.213-rc1 review Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 01/73] mmc: mmci: stm32: use a buffer for unaligned DMA requests Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 02/73] mmc: mmci: stm32: fix DMA API overlapping mappings warning Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 03/73] lan78xx: Fix white space and style issues Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 04/73] lan78xx: Add missing return code checks Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 05/73] lan78xx: Fix partial packet errors on suspend/resume Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 06/73] lan78xx: Fix race conditions in suspend/resume handling Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 07/73] net: lan78xx: fix runtime PM count underflow on link stop Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 08/73] ixgbe: {dis, en}able irqs in ixgbe_txrx_ring_{dis, en}able Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 09/73] i40e: disable NAPI right after disabling irqs when handling xsk_pool Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 10/73] tracing/net_sched: Fix tracepoints that save qdisc_dev() as a string Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 11/73] geneve: make sure to pull inner header in geneve_rx() Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 12/73] net: ice: Fix potential NULL pointer dereference in ice_bridge_setlink() Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 13/73] net/ipv6: avoid possible UAF in ip6_route_mpath_notify() Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 14/73] cpumap: Zero-initialise xdp_rxq_info struct before running XDP program Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 15/73] net/rds: fix WARNING in rds_conn_connect_if_down Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 16/73] netfilter: nft_ct: fix l3num expectations with inet pseudo family Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 17/73] netfilter: nf_conntrack_h323: Add protection for bmp length out of range Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 18/73] netrom: Fix a data-race around sysctl_netrom_default_path_quality Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 19/73] netrom: Fix a data-race around sysctl_netrom_obsolescence_count_initialiser Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 20/73] netrom: Fix data-races around sysctl_netrom_network_ttl_initialiser Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 21/73] netrom: Fix a data-race around sysctl_netrom_transport_timeout Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 22/73] netrom: Fix a data-race around sysctl_netrom_transport_maximum_tries Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 23/73] netrom: Fix a data-race around sysctl_netrom_transport_acknowledge_delay Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 24/73] netrom: Fix a data-race around sysctl_netrom_transport_busy_delay Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 25/73] netrom: Fix a data-race around sysctl_netrom_transport_requested_window_size Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 26/73] netrom: Fix a data-race around sysctl_netrom_transport_no_activity_timeout Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 27/73] netrom: Fix a data-race around sysctl_netrom_routing_control Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 28/73] netrom: Fix a data-race around sysctl_netrom_link_fails_count Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 29/73] netrom: Fix data-races around sysctl_net_busy_read Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 30/73] selftests/mm: switch to bash from sh Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 31/73] selftests: mm: fix map_hugetlb failure on 64K page size systems Sasha Levin
2024-03-13 16:45 ` [PATCH 5.10 32/73] um: allow not setting extra rpaths in the linux binary Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 33/73] xhci: remove extra loop in interrupt context Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 34/73] xhci: prevent double-fetch of transfer and transfer event TRBs Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 35/73] xhci: process isoc TD properly when there was a transaction error mid TD Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 36/73] xhci: handle isoc Babble and Buffer Overrun events properly Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 37/73] serial: max310x: Use devm_clk_get_optional() to get the input clock Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 38/73] serial: max310x: Try to get crystal clock rate from property Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 39/73] serial: max310x: fail probe if clock crystal is unstable Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 40/73] serial: max310x: Make use of device properties Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 41/73] serial: max310x: use regmap methods for SPI batch operations Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 42/73] serial: max310x: use a separate regmap for each port Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 43/73] serial: max310x: prevent infinite while() loop in port startup Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 44/73] net: Change sock_getsockopt() to take the sk ptr instead of the sock ptr Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 45/73] bpf: net: Change sk_getsockopt() to take the sockptr_t argument Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 46/73] lsm: make security_socket_getpeersec_stream() sockptr_t safe Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 47/73] lsm: fix default return value of the socket_getpeersec_*() hooks Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 48/73] ext4: make ext4_es_insert_extent() return void Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 49/73] ext4: refactor ext4_da_map_blocks() Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 50/73] ext4: convert to exclusive lock while inserting delalloc extents Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 51/73] Drivers: hv: vmbus: Add vmbus_requestor data structure for VMBus hardening Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 52/73] hv_netvsc: Use vmbus_requestor to generate transaction IDs " Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 53/73] hv_netvsc: Wait for completion on request SWITCH_DATA_PATH Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 54/73] hv_netvsc: Process NETDEV_GOING_DOWN on VF hot remove Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 55/73] hv_netvsc: Make netvsc/VF binding check both MAC and serial number Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 56/73] hv_netvsc: use netif_is_bond_master() instead of open code Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 57/73] hv_netvsc: Register VF in netvsc_probe if NET_DEVICE_REGISTER missed Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 58/73] mm/hugetlb: change hugetlb_reserve_pages() to type bool Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 59/73] mm: hugetlb pages should not be reserved by shmat() if SHM_NORESERVE Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 60/73] getrusage: add the "signal_struct *sig" local variable Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 61/73] getrusage: move thread_group_cputime_adjusted() outside of lock_task_sighand() Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 62/73] getrusage: use __for_each_thread() Sasha Levin
2024-03-13 16:46 ` Sasha Levin [this message]
2024-03-13 16:46 ` [PATCH 5.10 64/73] exit: Fix typo in comment: s/sub-theads/sub-threads Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 65/73] exit: wait_task_zombie: kill the no longer necessary spin_lock_irq(siglock) Sasha Levin
2024-03-13 17:03   ` Oleg Nesterov
2024-03-13 20:10     ` Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 66/73] serial: max310x: Unprepare and disable clock in error path Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 67/73] Drivers: hv: vmbus: Drop error message when 'No request id available' Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 68/73] regmap: allow to define reg_update_bits for no bus configuration Sasha Levin
2024-03-13 16:52   ` Mark Brown
2024-03-13 16:46 ` [PATCH 5.10 69/73] regmap: Add bulk read/write callbacks into regmap_config Sasha Levin
2024-03-13 16:53   ` Mark Brown
2024-03-13 16:46 ` [PATCH 5.10 70/73] serial: max310x: make accessing revision id interface-agnostic Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 71/73] serial: max310x: implement I2C support Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 72/73] serial: max310x: fix IO data corruption in batched operations Sasha Levin
2024-03-13 16:46 ` [PATCH 5.10 73/73] Linux 5.10.213-rc1 Sasha Levin
2024-03-13 20:05 ` [PATCH 5.10 00/73] 5.10.213-rc1 review Pavel Machek
2024-03-14  7:03 ` Dominique Martinet
2024-03-14 15:07   ` Daniel Díaz
2024-03-15 16:52   ` Sasha Levin
2024-03-14 14:50 ` Naresh Kamboju
2024-03-14 19:52 ` Florian Fainelli
2024-03-14 21:47 ` Salvatore Bonaccorso
2024-03-15 18:39   ` Sasha Levin
2024-03-26  6:59     ` Salvatore Bonaccorso
2024-03-27 13:45       ` Greg Kroah-Hartman
2024-03-15 10:48 ` Shreeya Patel
2024-03-20 13:41 ` Pavel Machek
2024-03-20 20:03   ` Marek Vasut
2024-03-22  9:48     ` Pavel Machek
2024-03-22 11:23       ` Marek Vasut
2024-03-22 13:35         ` Pavel Machek
2024-03-20 13:44 ` btrfs fix missing in 5.10-stable was " Pavel Machek
2024-03-20 22:12   ` Sasha Levin
2024-03-25  6:55 ` Daniel Díaz
2024-03-25 15:20   ` Daniel Díaz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240313164640.616049-64-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=dylanbhatch@google.com \
    --cc=ebiederm@xmission.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome