From nobody Thu Dec 18 07:21:06 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 1A7DC266F00; Thu, 13 Mar 2025 11:31:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741865490; cv=none; b=IEpNGvsEtWRPoxfoy8pIkShMU4W0nPPg9mwYQ9fNxT4qsTMKljO4q4mnW9uZ/FPKa2oVpyQExhUdCheT7g4uCpan8ajzxuulzKz+0yo0H5ebWBeCFAcwejsoWrCnno98qzxWVGUCHocfFKiuVw8uGglU89H5WD2SmuPwegmqZGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741865490; c=relaxed/simple; bh=VyyKpvOQneQLDKTqEr/gBnDcoAVrlNMXj0eNWAdKz4k=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=DTKK6ctK7HpEFA/CaXEsTTUSMZWvBD38ESRqgaVc6nFcr6x3iIBAfjAdU2NdKxIHghInsWW6Qhqw5xugsV9v2etKaAA4CtqeMT8LeY+A9335YVbMSUv9wj1z4ihKAupTQ3vCoxqrMS2bpKRhtT8ZU4LDHJBMbwrcKTvzn2Ku11Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0ZG/DtPI; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rl7OKhxn; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0ZG/DtPI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rl7OKhxn" Date: Thu, 13 Mar 2025 11:31:26 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1741865487; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=J6qdKVbOxSJ2QVDWCTSMOSds3wlVzYNTRZcP3a6jYq8=; b=0ZG/DtPIwHX/gE00WSDl/P1f79NrY0GhmC0U/uDzvGdMfIC9LMxv17ou3SpRvEa2NWxZIE EKjPC2V8luoTc/KBbguDDziXtocHm+vL4dHc396cf9XOHpmgRUMwdTKd42dHiN4ixqfjzX ES9rt23GGwg1NvvJQ8LxzjkiA1QLeTIRSDiHLDGZyniGObvoxTWkcITLJtImSSfg8FP0aJ 9W6wNUXG0tej+WPv9JU9RjCmTisdy4/dtJhrom29cVGS+b23mIkPtF6nZ1Phsmk6oBOfiU NN0jf0hLgvKwkBZn5ga4fPp7wNcUDh70kLSFYLPWzBCXCiT+waspca7OXCdgJg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1741865487; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=J6qdKVbOxSJ2QVDWCTSMOSds3wlVzYNTRZcP3a6jYq8=; b=rl7OKhxnPuTSPTJy6nAqzNmdjO6Z2LcoEpiihEDTp8t2o9UaAzhjI7s6PAQFQF8blgoJ7S /dWGg+lFSm0CyCCg== From: "tip-bot2 for Eric Dumazet" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/core] posix-timers: Make signal_struct:: Next_posix_timer_id an atomic_t Cc: Eric Dumazet , Thomas Gleixner , Frederic Weisbecker , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20250308155624.151545978@linutronix.de> References: <20250308155624.151545978@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <174186548696.14745.6975760705487032667.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the timers/core branch of tip: Commit-ID: feb864ee99a2d8a22800342388401f3a3b90d42b Gitweb: https://git.kernel.org/tip/feb864ee99a2d8a22800342388401f3a3= b90d42b Author: Eric Dumazet AuthorDate: Sat, 08 Mar 2025 17:48:36 +01:00 Committer: Thomas Gleixner CommitterDate: Thu, 13 Mar 2025 12:07:17 +01:00 posix-timers: Make signal_struct:: Next_posix_timer_id an atomic_t The global hash_lock protecting the posix timer hash table can be heavily contended especially when there is an extensive linear search for a timer ID. Timer IDs are handed out by monotonically increasing next_posix_timer_id and then validating that there is no timer with the same ID in the hash table. Both operations happen with the global hash lock held. To reduce the hash lock contention the hash will be reworked to a scaled hash with per bucket locks, which requires to handle the ID counter lockless. Prepare for this by making next_posix_timer_id an atomic_t, which can be used lockless with atomic_inc_return(). [ tglx: Adopted from Eric's series, massaged change log and simplified it ] Signed-off-by: Eric Dumazet Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker Acked-by: Frederic Weisbecker Link: https://lore.kernel.org/all/20250219125522.2535263-2-edumazet@google.= com Link: https://lore.kernel.org/all/20250308155624.151545978@linutronix.de --- include/linux/sched/signal.h | 2 +- kernel/time/posix-timers.c | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index d5d03d9..72649d7 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -136,7 +136,7 @@ struct signal_struct { #ifdef CONFIG_POSIX_TIMERS =20 /* POSIX.1b Interval Timers */ - unsigned int next_posix_timer_id; + atomic_t next_posix_timer_id; struct hlist_head posix_timers; struct hlist_head ignored_posix_timers; =20 diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 991d12a..f9a70c1 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -119,21 +119,17 @@ static bool posix_timer_hashed(struct hlist_head *hea= d, struct signal_struct *si static int posix_timer_add(struct k_itimer *timer) { struct signal_struct *sig =3D current->signal; - struct hlist_head *head; - unsigned int cnt, id; =20 /* * FIXME: Replace this by a per signal struct xarray once there is * a plan to handle the resulting CRIU regression gracefully. */ - for (cnt =3D 0; cnt <=3D INT_MAX; cnt++) { - spin_lock(&hash_lock); - id =3D sig->next_posix_timer_id; - - /* Write the next ID back. Clamp it to the positive space */ - sig->next_posix_timer_id =3D (id + 1) & INT_MAX; + for (unsigned int cnt =3D 0; cnt <=3D INT_MAX; cnt++) { + /* Get the next timer ID and clamp it to positive space */ + unsigned int id =3D atomic_fetch_inc(&sig->next_posix_timer_id) & INT_MA= X; + struct hlist_head *head =3D &posix_timers_hashtable[hash(sig, id)]; =20 - head =3D &posix_timers_hashtable[hash(sig, id)]; + spin_lock(&hash_lock); if (!posix_timer_hashed(head, sig, id)) { /* * Set the timer ID and the signal pointer to make