Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1888652lql; Wed, 13 Mar 2024 10:54:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXta9vIEdCoKM5s0hDEx4iUWtAiy9ex7zfZqlMFF6UDeAGfgitNgLNorLyRt+ZU6tjhkt7C52i0Kqpflx+oJTFGlYoIIrQkHIUOZFQF5Q== X-Google-Smtp-Source: AGHT+IEpkr1FTZCGRAMN5lt3fsJ1RWdI+FmqFu9UoiupHmRfPwN+5dNXE9frnKr3h6bCXqukUxzh X-Received: by 2002:a05:620a:98e:b0:788:3908:652 with SMTP id x14-20020a05620a098e00b0078839080652mr497266qkx.49.1710352499112; Wed, 13 Mar 2024 10:54:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710352499; cv=pass; d=google.com; s=arc-20160816; b=Kzp+tUTzEZeenDVT0w2D7SWL0GXmRnZNXx+pudfvrWjXkZyRGSar63WDcNJehZBKtY viOUcAoWjtg4CBKvaMs3DW2AxoyPKQHPqYdtoQJqsdWgDKZb6xRycEvvkO/ynpS1rIR3 uLXQ4u2ySQTwPzY3R6mhsxH7n6mUl+J4TDAlSv1BGHAa51tenzE/TqTtMiw3FnKQ3L7C KQKgmhG0efulh2Nx5RV5xSegDLuoJPnfTiQTwl4XkdPTHtbK3J6BpnXhztcBtfJguTLn NoOgixER0viTL+Lv5G1Jszxc2D4HyL6vMOdNfl38VgBYlrbVDQHfToBLmRwttT5qsTV7 wV1g== 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=Ir9W0Ydf36vKXZX5wFJR39WL+R3wFx9Uld4WzVHlY/8=; fh=QHYoePTVyKNoydmDXmfPR/lPlJYVhXqsBmqXV4JCFVY=; b=aO3XUxoWeNEu/lAGi5cbpfFsSaXC/qbBCJWRPqWq3O5dBPEqe+RPcSTdxad+vY/nTk VRmNTaOV+G9AHnQ02qwtVV9aEWn3b5y8NQrPu1z9za+D0mWLvYiwZEYTn7/POrgU0UhZ OBf4f8sf1ACVRptU+5j8KFTDNeph/AxdkeKiw89nG+rOBuuhvLzoRGjlBoonNDn2JQJj RKM7TIVHqHFBB4y/pqQYgS1K5waqmOi39sMa4wBofVs8NClPjQZQspWeTudWh5MjzC9F UoOZ7fHUcp0PnbbAo33ZJGpAZC9u2k3oovRLJPjC5NH7y4iJ4VN7B02IFJPUXvj4/V8x anUg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pt5iMp4T; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-102089-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-102089-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id z29-20020a05620a261d00b00787fbf63c39si10959731qko.750.2024.03.13.10.54.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 10:54:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-102089-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pt5iMp4T; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-102089-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-102089-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id D17DD1C23413 for ; Wed, 13 Mar 2024 17:54:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 78D3718C6CA; Wed, 13 Mar 2024 16:47:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pt5iMp4T" 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 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: 8bit 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 = 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