From nobody Fri Dec 19 04:55:15 2025 Received: from mail-vs1-f74.google.com (mail-vs1-f74.google.com [209.85.217.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07C80265CB4 for ; Fri, 14 Feb 2025 13:59:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.217.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739541558; cv=none; b=Kk3DyXWmugjp8hyZU1S4lNA40Qugn1tr9dDIyBqA4QYh1euWTvR3Gawk7BVrkBdlKTlMKKxCCDew++px5owdZS62x0ImChpj0ZqZd6finCeFt3si3TmeMQw4buP2ipdXct3HpIh/wetBu2sPg1EkgyVUgYiRpCo/kvbo26ckczU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739541558; c=relaxed/simple; bh=eeTZCCgdaqFJ3bnGpZOk2P/FIXNGp94xTCFjKijwb7I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=r+y4TSKrMnp2IToxqX4GSAd/+PgFAGTiMMagRElsOmiCYf8994XgVAbo3oIXbF2UmDVcwJQHneOCAvZfkydHtR0hlB+stsNTli/S9XkgRHTq/Pe8M8yWvJClw0yXGi+i+SNjiHgDaEKqJpncIC8ATLq8Js7n8kfu4qKbLfR8BfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=f9pvQvY/; arc=none smtp.client-ip=209.85.217.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="f9pvQvY/" Received: by mail-vs1-f74.google.com with SMTP id ada2fe7eead31-4ba9a0100a7so405446137.2 for ; Fri, 14 Feb 2025 05:59:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739541555; x=1740146355; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rzDdFyfmHOJuJt4qhZZS1RtuCn1nriy1Y4zi8I6ANzI=; b=f9pvQvY/0nSCsDpYfrfFS9uz0UrXSKWsIz3jYQ9p650XmQla13cvJBIYy2Rk54UKsw kBgD1PcTwCtYq/mCUrCzwddKRHESjNmyW5MrujdMmdxcSIzCB+VHLV+n3B8dhdN/3lz+ LR47FlEJKDIJZJuMyYmYr/5n2NtFgWaCrgzIXslMVXzUCv//VyUC6+C1pYg9Y4czACXh QdszHXXDtj12kjmhK6jfaBcjLbXlptH4CiHwqBqK2Le2oLPnluXPOFhHgBKHNkw0/VsY RmqtD9QL6owlYrKmStfM1PnDoZthJW8v4HXBl9d7sKhRTUU2PNvv6gvEeulD7nmNx1+6 2Xig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739541555; x=1740146355; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rzDdFyfmHOJuJt4qhZZS1RtuCn1nriy1Y4zi8I6ANzI=; b=mDUajxbKuR+2W7X5vGYLpuECq1U+tB7xRjNRuUSqSzS81NxjCcam1RXMuB+M6UprW8 8e1RfsRrJ+yj7Bgt0BybcbgwZFzki1I6PXAZwe/Pl81+7ShYyX3seUBdJtRH6W71l1Az XqOf18nNjjeDQF8uvCyqov/91N+fwFPWNG5DJcRF1bdCS0SLWeZhTUbRoC/H1zt2jmN9 l5hpYQvBj4aD9IpnRlC1y9jqZymfODpzUgnVav9uLz6JoCkyygcs6CSlElMDVtUE7sCz JoT5xHKf7FyfcrSwGHO7bYns+/li7ETqwNkTZSL+1HJJI1qVcUaRLeeHbyiu7PVvANF8 gFEA== X-Gm-Message-State: AOJu0Yy3RUf1bpKwcDbbJAI0i+ofrN/+hU15yU2py7S1k4gBDAiGs9+2 Xa3dQ73lOoS6+TGimyUZwSZ8G7gr9FL99wasZ8ArzxozgumjZDE6E+6bP282B3ComEl3XKwuRAn CaazMXRqmOQ== X-Google-Smtp-Source: AGHT+IFENciHSa2g6hQHsURgowLSjE6iIAIM4cSi0EY4/omVpg5j91N9vZV4rJxmleL8tD6MAFqsjkLhE1cHHw== X-Received: from vsbib7.prod.google.com ([2002:a05:6102:2b87:b0:4bb:e1c9:70ee]) (user=edumazet job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6102:290d:b0:4bb:dba6:99b7 with SMTP id ada2fe7eead31-4bbf222750dmr9789437137.13.1739541554918; Fri, 14 Feb 2025 05:59:14 -0800 (PST) Date: Fri, 14 Feb 2025 13:59:10 +0000 In-Reply-To: <20250214135911.2037402-1-edumazet@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250214135911.2037402-1-edumazet@google.com> X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog Message-ID: <20250214135911.2037402-2-edumazet@google.com> Subject: [PATCH 1/2] posix-timers: Make next_posix_timer_id an atomic_t From: Eric Dumazet To: Anna-Maria Behnsen , Frederic Weisbecker , Thomas Gleixner Cc: linux-kernel , Benjamin Segall , Eric Dumazet , Eric Dumazet Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Instead of relying on a global and shared hash_lock to protect sig->next_posix_timer_id, make it atomic. This allows the following patch to use RCU. Signed-off-by: Eric Dumazet --- include/linux/sched/signal.h | 2 +- kernel/time/posix-timers.c | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index d5d03d919df8..72649d7fce2a 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 1b675aee99a9..204a351a2fd3 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -105,13 +105,14 @@ static int posix_timer_add(struct k_itimer *timer) * 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; + id =3D atomic_inc_return(&sig->next_posix_timer_id) - 1; =20 - /* Write the next ID back. Clamp it to the positive space */ - sig->next_posix_timer_id =3D (id + 1) & INT_MAX; + /* Clamp id to the positive space */ + id &=3D INT_MAX; =20 head =3D &posix_timers_hashtable[hash(sig, id)]; + + spin_lock(&hash_lock); if (!__posix_timers_find(head, sig, id)) { hlist_add_head_rcu(&timer->t_hash, head); spin_unlock(&hash_lock); --=20 2.48.1.601.g30ceb7b040-goog From nobody Fri Dec 19 04:55:15 2025 Received: from mail-qk1-f202.google.com (mail-qk1-f202.google.com [209.85.222.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D30F265CBB for ; Fri, 14 Feb 2025 13:59:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739541559; cv=none; b=TMganlbNVCvQaEZukVzvXD3O/isniPPlmZQyQHboF/T1EFC2m3p4kgioiqTzDZ/pVotsdMwIwyQx8/c52lNFJ2YdYpGEiSKJ7rD5DZMfRbAKZjoD55vyiaznWTcJyHRzmwGs3QPFBpn7D1uWLEsGPuBfz7g8Pot2d3y4I0kGvXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739541559; c=relaxed/simple; bh=g+Ef+bvJCXnr2fnE6ROAKPn2T26076corKifdGv/dhE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DkXxWbiJML0i7P5hG7fxMUPPVIzGyPwUAbJmVqMkiySaG+FeT5B0FApVXC4GXtSVw4T966BRz+UXc+Bu8956Anc9fk9uHAp9b2WhRJjxoXuDT2u72cg1OAoBlYNLSfwCse7bUn8l3DQvL+BWhJBgIFIxPkh0DEgvh5e6n2kTOIM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Z5ozKLeg; arc=none smtp.client-ip=209.85.222.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--edumazet.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z5ozKLeg" Received: by mail-qk1-f202.google.com with SMTP id af79cd13be357-7c0784e91e4so208773485a.1 for ; Fri, 14 Feb 2025 05:59:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739541556; x=1740146356; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/AyOaPuXyGpDU+d1RnpKJaFpTE21ht1sJRDkQSr5LTU=; b=Z5ozKLegQd8wqTH9xOpJhcnQl1Wnsnd96fQo3K2JM61Y9K06dczL6+50kautk+cG2i z6s+KXWoZhpO6+0ZccpJBo1M56Ng9wlKgCJY8XFjnl+UZFN+pAHVZcDNgO/EODKSflIb YLeF7Px8dF9jWUzTRkW97UM2YkTGajarcDYCoRvnx6IguYBdcSjUE3ANJWugJTzGYVGw NelQccYN7SBAvZRCuaWE3U/BSaUbGrXGH446bP9JYbEy2fxAVMUnxgfRjnrnSG6C4jdH QyNLheY+A1ECAy6dA4k6bj6zqYYD9CKji6zzT6BKGqKUr/mXi2hYQszphWZcMooP58Lw Zvww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739541556; x=1740146356; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/AyOaPuXyGpDU+d1RnpKJaFpTE21ht1sJRDkQSr5LTU=; b=xNxupqHq5t6TbhVTinLNbuvlo5W3ynms2Ki1e0fDdkRxk9lvx5vbKZR2HKKGeRhDpc UzM0Evsurh11YPuTD/mS3rV67jBoryJfC3iJ4hWJNjJch9V4lBLN28WDyn8nRYqrZndT LHfg+HLNoBJFw1gJMj89hZzwp2ah1E+/AoSXeINhjNBAeq3ZYFxPFIC07fjHpZ1UI0QH qMMhn6at0tn/fbzFhdq+HsEVsKGTkRS4hOUfCGzNchXTEiQotEyCUIJIKRF5VV4lwxe5 uUTckCXG2L0kSg3qBik1AwdtsgagSX+UxzyFpBa5DWmugBuxRHmxj0A/f+zTTGKLA+ax VgKA== X-Gm-Message-State: AOJu0YxY/1O88rECuyLOdLD5DtET/+k8LpQIsosRsaNm1kJgUU0x1aII YEUCWdnGpbqfwxhtGyBzPo88Kt0sumYZs2rsWt/+pmKeT0IxY3uwqkzL7VRz+ZZw5BkZUy0lOb+ vYRD+UTStlg== X-Google-Smtp-Source: AGHT+IFk1SYInwiB6mhJ0ITE2lup7Kgau5WwsvYxr3MNuBisVxDBaec6CE0IuS4GB3hIjUkZ5gG36fKqG+BZYg== X-Received: from qkd3.prod.google.com ([2002:a05:620a:a003:b0:7c0:6ed4:fcee]) (user=edumazet job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:1993:b0:7c0:8950:e3ed with SMTP id af79cd13be357-7c08950e4ffmr65514385a.54.1739541556299; Fri, 14 Feb 2025 05:59:16 -0800 (PST) Date: Fri, 14 Feb 2025 13:59:11 +0000 In-Reply-To: <20250214135911.2037402-1-edumazet@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250214135911.2037402-1-edumazet@google.com> X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog Message-ID: <20250214135911.2037402-3-edumazet@google.com> Subject: [PATCH 2/2] posix-timers: Use RCU in posix_timer_add() From: Eric Dumazet To: Anna-Maria Behnsen , Frederic Weisbecker , Thomas Gleixner Cc: linux-kernel , Benjamin Segall , Eric Dumazet , Eric Dumazet Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If many posix timers are hashed in posix_timers_hashtable, hash_lock can be held for long durations. This can be really bad in some cases as Thomas explained in https://lore.kernel.org/all/87ednpyyeo.ffs@tglx/ We can perform all searches under RCU, then acquire the lock only when there is a good chance to need it, and after cpu caches were populated. I also added a cond_resched() in the possible long loop. Signed-off-by: Eric Dumazet --- kernel/time/posix-timers.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 204a351a2fd3..dd2f9016d3dc 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -112,7 +112,19 @@ static int posix_timer_add(struct k_itimer *timer) =20 head =3D &posix_timers_hashtable[hash(sig, id)]; =20 + rcu_read_lock(); + if (__posix_timers_find(head, sig, id)) { + rcu_read_unlock(); + cond_resched(); + continue; + } + rcu_read_unlock(); spin_lock(&hash_lock); + /* + * We must perform the lookup under hash_lock protection + * because another thread could have used the same id. + * This is very unlikely, but possible. + */ if (!__posix_timers_find(head, sig, id)) { hlist_add_head_rcu(&timer->t_hash, head); spin_unlock(&hash_lock); --=20 2.48.1.601.g30ceb7b040-goog