From nobody Fri Dec 19 04:55:35 2025 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) (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 81E481D6DC5 for ; Wed, 19 Feb 2025 12:55:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739969730; cv=none; b=l+lyoCCz//UYyD+RV2XxsO6zESHHfGhdw8rNHkenmzSwFYVpRBqiSMR8wJNr6N6wI78RMNRQifvDqpsBoCfOSms7wzxOq3Fkab2et6sY5VE+h8Pjzb+ROo2MEE4auuoPBWxPoCjiWNsbisQkYdr74/lYfBgNF+RKNIrPZLGMbco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739969730; c=relaxed/simple; bh=eeTZCCgdaqFJ3bnGpZOk2P/FIXNGp94xTCFjKijwb7I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bCgLiXceEB4psbUk6mNbTre/O0QEclnq+ZEBTsiuXDQ7njPiZA3Rxdp/eH4kRQY+Of/29nvU06x9+OfibNdeH+kxSA2+YAwVpxjLzbOB2NFJ62qovgB9/VGt12vIYTxQZ2Fef3KggKE57/sitJ9UV3EMI5Hi8xgy1Pnb7EqpvP4= 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=kko0haXA; arc=none smtp.client-ip=209.85.222.201 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="kko0haXA" Received: by mail-qk1-f201.google.com with SMTP id af79cd13be357-7b864496708so2073469285a.2 for ; Wed, 19 Feb 2025 04:55:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739969727; x=1740574527; 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=kko0haXAbtivy8tOBBJlNo8aIi64ZNjC/sSUHaIVtvksPB67r3KPfGBU8Np/ELu6t2 ohce2a6x4b0suM1hP0VSTHSkmJlGjHr4/FR9lTfkIPIva3F9QlCFNwAuVFUVGuqBlVjk g5DJNpf53+p4kJ1AJDC1o4DI8w8TkFo9khdtqJZ81XrGb7psaumNI3GIKxgD2tU/V/0q 7ZTsfdJMUMLHrPa11A5nrMjb7RYxFbXCtMBERCZzXM8Kfl16FEmKvp7+uRpxaaP9x/4m l2qiX7XMPh4rvQFXXEAfhKYuaTHJQ/N3ss8j7UEtCMzdsMgPkd39Iaxy7enBfxaiztda hGZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739969727; x=1740574527; 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=tU0DNfqDqqHsuLuTqYkpyErznjRCXvU2Aw+ic9wh85HHH+xnzKOtNBkDYa0iN+XV9J gaCrZPoV8okHR0iWAhOYT8SoaTbBTk9g11a4cMLVhpq48EcSOZOuMcRJNBwqp06+KwUw kqKuI3o1ovVH7aQDp4S2dU4cUKi7Zhc4DVTVcBp1m0jwwmbJMzUQILcDAyFOJM/GceYI udTz2U8NNxnZALSLYs/V81psUmwy+7EUYwiDHIXCqnkrMBL/DkfMybIKClXhPprPVdsj USpmGuPp2U7fQqFtVBP9id77QGnsp4CJCAAshPEJZ7a/5squyykSeR8vo01vFO05o9sb pnNw== X-Gm-Message-State: AOJu0YyEa05Uka3ALIzeAyxk27/LTVa8wKnjNmJn2xfj6o72Swy1tmtg RrW30U6bKs93+WJkGHqltEjavLuoV0z5UGt0QqWTDjpi92nx55XBGNZLQmAL9ABhkxzOV05Eju5 WKt/y3ycm3g== X-Google-Smtp-Source: AGHT+IHCyPuA21x22mhemB0CjAcK5OZqSXzGjRnCiufJfTH4Nk4Lgn+bj7sWSPXQSbeJQ7hTT5dphid9tQG+AQ== X-Received: from qkdd16.prod.google.com ([2002:a05:620a:a510:b0:7c0:81e3:4f]) (user=edumazet job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:640b:b0:7c0:b018:5938 with SMTP id af79cd13be357-7c0b0185e03mr923164385a.7.1739969727418; Wed, 19 Feb 2025 04:55:27 -0800 (PST) Date: Wed, 19 Feb 2025 12:55:19 +0000 In-Reply-To: <20250219125522.2535263-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: <20250219125522.2535263-1-edumazet@google.com> X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog Message-ID: <20250219125522.2535263-2-edumazet@google.com> Subject: [PATCH V2 1/4] 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:35 2025 Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) (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 B943D1EFF94 for ; Wed, 19 Feb 2025 12:55:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739969731; cv=none; b=Bmg+3cYRaM6BVX1eshbEgmsn9M1LfcphJkXcg6X9G4hZY2qgLco/d5LgaIEvfLpni/tDyWe1KNujKXZEOcUTBkG83Px3H2urLvb1QzybEqW9Kyj1qzSixgJgzfuMHk9TiK848uWXm9OI20k7TgYBwtRkHrQtni6m21Mnb01aZ/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739969731; c=relaxed/simple; bh=hTWh0S/kO+ddBssr4RWhOpg2O+uuBTdPLr4p+YMc210=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AahfPIdBBFFmvXIlUqMf7hJEQBWkx5d2GRBrRbqPXc05vXCIaKBEnVRl+ZmlZubU37AyJAY3HGv0486HL8PImxR6y6n5yPi96J8nHM5J+g0RMOfQmd6kLsB7G24HRhvGMfXabpClSD7sWjnbQr2hMeSTMEECjDRW8ZwFAOm/ygE= 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=4GVMkfWa; arc=none smtp.client-ip=209.85.160.201 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="4GVMkfWa" Received: by mail-qt1-f201.google.com with SMTP id d75a77b69052e-471f08bd0bdso63016941cf.3 for ; Wed, 19 Feb 2025 04:55:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739969728; x=1740574528; 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=OvtCJCFO4ZH16O04eSMYTdc4XJU1IA2ReSwEN4Affzo=; b=4GVMkfWawU4oranoshcrkaYeVag/xKU9qPSzd1dkSk7BPasGD1IGzXObt2Fo06t7hr EbBqimxSyVVcMCb2LyRXFgMO6ZpXhbfWZuBNqyu6ROfG1Vd+t56hYGzhB1VZj85BXbRx o7BBhYwbJg1+8IMYHmy+wrVg0I/WCUln/kZTQvXH9EwVtzOhheB64bnYzlbzgRAvi1cP I9ULEblohEiJ4WA3cWQ74kGBiHV/kY7N7daNg/zlpjpZSHwTAmUkMCZUWIrS4wTh98ho cSPXtTVKz7FtC/ltY81++C2W5Lwe/hofv3aRc3cRjJb2sOj+myDelpi1CRIR55r05l6s WAPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739969728; x=1740574528; 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=OvtCJCFO4ZH16O04eSMYTdc4XJU1IA2ReSwEN4Affzo=; b=oYr+e70QvYGLX7+SHvZlaOd4oV8yxw81JGtOBDj7UIMQtEBIs7EIG4hAJDEtA2obE8 SCVDCXAn4MRmSdF2YiR27k7XyalOtGNyDJEl7hkXdYEa2q6zFLRzoixJbvY8qL+iDSy6 zfsEw8Tf7aCUGBo6wb/+eLtm5+4HIUsGzLHsE+TH0jxwbIG2ZzM+QxZbWLyubd/SvkpJ IG9LhLhvQac/lBmZmEzHBjR2igEyYCyyqTww55pmvrhu/+cGxMAeOOiTflcbeArDH+O/ YXOK3j0PYgSm9Lx2t8zJnv2ke+XpDkgZvHJ7TRYXhZxBK2i9UavzRDQ0rMbmKiVIFt2T pldw== X-Gm-Message-State: AOJu0YyQjNwml4lHvvx9VnT4FllEayhYRRCTLbkKEFRptyOotel7ZBgl N7cwSiKqB7rth4bpZ/+5mDOsS+7+jqzJny9Wu7Ii46t1+884RY2bk4NARaRYrlrvfOGzcEHyiNf uDKZ7oJqxhQ== X-Google-Smtp-Source: AGHT+IG698mpIVkANi3thGeaLy6YzVcvcPcUZ6wDN3EpWc5ASqq+qArjlVoau4d9wWH+HJZVkhv+y7OB6bKwzw== X-Received: from qtbfy25.prod.google.com ([2002:a05:622a:5a19:b0:471:f25c:e47d]) (user=edumazet job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:740f:b0:472:1040:10b2 with SMTP id d75a77b69052e-47210401398mr7711961cf.39.1739969728679; Wed, 19 Feb 2025 04:55:28 -0800 (PST) Date: Wed, 19 Feb 2025 12:55:20 +0000 In-Reply-To: <20250219125522.2535263-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: <20250219125522.2535263-1-edumazet@google.com> X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog Message-ID: <20250219125522.2535263-3-edumazet@google.com> Subject: [PATCH V2 2/4] posix-timers: Initialise timer->it_id 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" A timer is visible only when both timer->signal and timer->i_id are set to their final values. We can initialize timer->it_id sooner. Signed-off-by: Eric Dumazet --- kernel/time/posix-timers.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 204a351a2fd3..1f73ea955756 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -114,6 +114,7 @@ static int posix_timer_add(struct k_itimer *timer) =20 spin_lock(&hash_lock); if (!__posix_timers_find(head, sig, id)) { + timer->it_id =3D (timer_t)id; hlist_add_head_rcu(&timer->t_hash, head); spin_unlock(&hash_lock); return id; @@ -407,8 +408,7 @@ static int do_timer_create(clockid_t which_clock, struc= t sigevent *event, =20 /* * Add the timer to the hash table. The timer is not yet valid - * because new_timer::it_signal is still NULL. The timer id is also - * not yet visible to user space. + * because new_timer::it_signal is still NULL. */ new_timer_id =3D posix_timer_add(new_timer); if (new_timer_id < 0) { @@ -416,7 +416,6 @@ static int do_timer_create(clockid_t which_clock, struc= t sigevent *event, return new_timer_id; } =20 - new_timer->it_id =3D (timer_t) new_timer_id; new_timer->it_clock =3D which_clock; new_timer->kclock =3D kc; new_timer->it_overrun =3D -1LL; --=20 2.48.1.601.g30ceb7b040-goog From nobody Fri Dec 19 04:55:35 2025 Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) (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 057DB1EA7C0 for ; Wed, 19 Feb 2025 12:55:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739969732; cv=none; b=X1/WOeZZaj2fu2u1F3S8+KWZxFMckXKY9vIfqwGwqHghvyyOANozl2k/a/86sWlkGrBuMLxtPhX5x+1g4dOrC4MPnpm57mF58AgYO91RwixuuVWwgCNVoyRkVkzNmriRt/POrdYB9AWcAdWcm4m68I3Ra96nh0pKj75m/urp/Eg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739969732; c=relaxed/simple; bh=QLuPInGSc5zXtylSkq0HN/KMtYy4Iqlr4IbFwCsJu5A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mlH8nFsx/kpCTrNt8ASRTsIZhSvtOJmoeXIUdqr2fFVcWwTfyBF2I2y+DNsqtv8WSm/m90PI1w4S9k3SwZj1yhMp5DCQ9Ai2TOHiVDAUSIOwsGHXj6sZfKJBV1ZToQ5MGPf4ja4bqlJUhG1BT58CCfMOgeITetZaYNLrj9MATPk= 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=VysxrqJM; arc=none smtp.client-ip=209.85.160.201 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="VysxrqJM" Received: by mail-qt1-f201.google.com with SMTP id d75a77b69052e-471f1b00de4so60123541cf.0 for ; Wed, 19 Feb 2025 04:55:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739969730; x=1740574530; 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=HrQ8DVgfppL9aNYS6t8dxTdjM8ZQud5z1rQs/qyCubM=; b=VysxrqJMKXWWM/pXktchxfP8Ol2K+7A7uMZReww9qqfH2Vsk5YZRkpHLYr1G6OXv6l ta47as8sxvlvyvZi3I+Uhr7pFLOfjhGyv/m+a2rsRZ7JvmW2Py7EzBg2eQDAgxFGnSwT n9dPXu6JK/vSBjr9Urofj9YMuqRkTC+hHEX4QygnhBxI3jJJstyZqPHhg6KJpRDasCdh jLISJXLM0DX+xLaEFMcJci6zShEYP2OSAek1IEMoGjzlvUO8LA8L2GRZ1jfN+yMwdwW6 PlZvpIK5ehDw+jPTmQK134lXiO6IbXH6KlDCZJW1Sx/brYQdaJa08trGDgwuydhsKPni v4+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739969730; x=1740574530; 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=HrQ8DVgfppL9aNYS6t8dxTdjM8ZQud5z1rQs/qyCubM=; b=WERkCq8PPqugOq0H9RIJjtPq4eNSMVQyvn3RAKeZWAmlkN+hXArvcw7ZLLsNG4WWWo sU26zHnpA5/BbN6SonqnVrDGgE6lRT1ylPBnlOl9kbMgAIEaP4bs9RludSAfIOXLgnCo AX82MhP+CvrNRihm8mU0/1CYyic9ilJRAiM6bzb3jERWjxJCX1JgAulvOX/O51NxwgPN fKqxiQPDTsVYuVzdwCVXnFIafQA/uCFaIVn72fjUBdd6noKNDqup2qcgslMQ7P3jEa4E n2ejj+gAIsPhxjIrbbkaO7BCVBy9rELqaWSfbV6o9RTFKbRRyC3qqEjT02+NqL7ubxNz HdVg== X-Gm-Message-State: AOJu0YyezmDOvY0wFQjb2tYSZmZtPLfD4sYMlN2wz5A4hfwZJ3sKVjhL X8jXYlYcr2cCRDIWsuRNbz7IxCrnz16bKNee6tHvBsU55hlfwP7sOBSpOe7osVjuC4N22DzjcKB YZ/5u3lZS7A== X-Google-Smtp-Source: AGHT+IG8L92/+/kY+K+afU01nQid/gl9V3DEdxYtHBqm5UumelJoCh8TZfFk4mvgqJcNvZ7RYpiFrEhBPdmeeA== X-Received: from qtbca21.prod.google.com ([2002:a05:622a:1f15:b0:471:fecd:df28]) (user=edumazet job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:5c08:b0:472:1225:bd98 with SMTP id d75a77b69052e-4721225c536mr6250861cf.50.1739969730016; Wed, 19 Feb 2025 04:55:30 -0800 (PST) Date: Wed, 19 Feb 2025 12:55:21 +0000 In-Reply-To: <20250219125522.2535263-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: <20250219125522.2535263-1-edumazet@google.com> X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog Message-ID: <20250219125522.2535263-4-edumazet@google.com> Subject: [PATCH V2 3/4] posix-timers: Initialise timer->it_signal 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" Instead of leaving a NULL value in timer->it_signal, set it to the current sig pointer, but with the low order bit set. This fixes a potential race, in the unlikely case a thread was preempted long enough that other threads created more than 2^31 itimers. Rename __posix_timers_find() to posix_timers_find() Mask the low order bit in posix_timers_find(). Signed-off-by: Eric Dumazet --- kernel/time/posix-timers.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 1f73ea955756..ed27c7eab456 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -72,15 +72,22 @@ static int hash(struct signal_struct *sig, unsigned int= nr) return hash_32(hash32_ptr(sig) ^ nr, HASH_BITS(posix_timers_hashtable)); } =20 -static struct k_itimer *__posix_timers_find(struct hlist_head *head, +static struct signal_struct *posix_sig_owner(const struct k_itimer *timer) +{ + /* timer->it_signal can be set concurrently */ + unsigned long val =3D (unsigned long)READ_ONCE(timer->it_signal); + + return (struct signal_struct *)(val & ~1UL); +} + +static struct k_itimer *posix_timers_find(struct hlist_head *head, struct signal_struct *sig, timer_t id) { struct k_itimer *timer; =20 hlist_for_each_entry_rcu(timer, head, t_hash, lockdep_is_held(&hash_lock)= ) { - /* timer->it_signal can be set concurrently */ - if ((READ_ONCE(timer->it_signal) =3D=3D sig) && (timer->it_id =3D=3D id)) + if ((posix_sig_owner(timer) =3D=3D sig) && (timer->it_id =3D=3D id)) return timer; } return NULL; @@ -90,8 +97,14 @@ static struct k_itimer *posix_timer_by_id(timer_t id) { struct signal_struct *sig =3D current->signal; struct hlist_head *head =3D &posix_timers_hashtable[hash(sig, id)]; + struct k_itimer *timer; =20 - return __posix_timers_find(head, sig, id); + hlist_for_each_entry_rcu(timer, head, t_hash) { + /* timer->it_signal can be set concurrently */ + if ((READ_ONCE(timer->it_signal) =3D=3D sig) && (timer->it_id =3D=3D id)) + return timer; + } + return NULL; } =20 static int posix_timer_add(struct k_itimer *timer) @@ -113,8 +126,9 @@ static int posix_timer_add(struct k_itimer *timer) head =3D &posix_timers_hashtable[hash(sig, id)]; =20 spin_lock(&hash_lock); - if (!__posix_timers_find(head, sig, id)) { + if (!posix_timers_find(head, sig, id)) { timer->it_id =3D (timer_t)id; + timer->it_signal =3D (struct signal_struct *)((unsigned long)sig | 1UL); hlist_add_head_rcu(&timer->t_hash, head); spin_unlock(&hash_lock); return id; @@ -453,7 +467,7 @@ static int do_timer_create(clockid_t which_clock, struc= t sigevent *event, } /* * After succesful copy out, the timer ID is visible to user space - * now but not yet valid because new_timer::signal is still NULL. + * now but not yet valid because new_timer::signal low order bit is 1. * * Complete the initialization with the clock specific create * callback. @@ -463,7 +477,7 @@ static int do_timer_create(clockid_t which_clock, struc= t sigevent *event, goto out; =20 spin_lock_irq(¤t->sighand->siglock); - /* This makes the timer valid in the hash table */ + /* This makes the timer valid in the hash table, clearing low order bit. = */ WRITE_ONCE(new_timer->it_signal, current->signal); hlist_add_head(&new_timer->list, ¤t->signal->posix_timers); spin_unlock_irq(¤t->sighand->siglock); --=20 2.48.1.601.g30ceb7b040-goog From nobody Fri Dec 19 04:55:35 2025 Received: from mail-qt1-f201.google.com (mail-qt1-f201.google.com [209.85.160.201]) (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 6C02B1F17E9 for ; Wed, 19 Feb 2025 12:55:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739969733; cv=none; b=N57fu2nNyeKZ8FOtH5cwY/2tgkJjzreLH3rIvLDhxZ5AAVWbMRZGAnLwlqRk4HrrZntmXlvBo53juixmLfdcBueGdSvrhaKkotjKGIPteL2tn1MhiI9s8U/TNRWzdcbY6MWh7MyKG44PoLPDuPVrYJ6K7qDJd1pN2zomcVQ1Rog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739969733; c=relaxed/simple; bh=YNpyjz01xX66Zcbq5ZIBxOqE+DPXQAGfAfnvffOlMDk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hKMHq74UDr6R9AVh7aYU4lXV7aiIW/7mGvSwitgh+1WRKXh+KCbgpVZWsG/pRUq0+oMJp586oAavuaWGyy3/ZyQWBKj3lMT1Nfg8rp4+SR43r9Nlgf8w75sxkyb80mFeF5qCBUZMCUEeV13eoGtVfnJ0/HamRTiLq6/K+UyFncw= 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=udfAtBfN; arc=none smtp.client-ip=209.85.160.201 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="udfAtBfN" Received: by mail-qt1-f201.google.com with SMTP id d75a77b69052e-471ec7f1969so15510981cf.1 for ; Wed, 19 Feb 2025 04:55:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739969731; x=1740574531; 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=ID8IGZqUO+EaQVQOsNgcPNApuu1uRf/knYDdUOiCuA8=; b=udfAtBfNYZl8xUqwcc7eiJTXkfEY7PmE0m/yaxQ87ILtnNiUG4s5KBLriiPliM5L07 eUXTLV9hZeKtaofOSYFvhQT2bYEhX81omRiB4aJKpziydLgYrjttfTUu+T5GLsrJhzyO NSTFnldHnf7ow7DR7/aKFHL0RmxeFa5TTcUuV1XSFBVNkgQWX2bu4S0bwVGKvTf2MGlF ZkDY/X2q2BdWdWrkDdKhtWtwuu/oWU0RwGRmld149y/D6KWwm4shrRHeMaALgaHu1+F2 IHlBF4PVWNjImA4KzkBlFExnCcTctcebf4/+aq7p4iSsSrX+Rh96Mr3jX2qGOYsTwvVG IdKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739969731; x=1740574531; 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=ID8IGZqUO+EaQVQOsNgcPNApuu1uRf/knYDdUOiCuA8=; b=CHjnR7NczP+13ptgvieIeEKPpr81nzFIRRXuV6v1E8lHeBOsE/dNn7+o4zoeu74WE2 3bLcUocmEY7SMemn+DTEOGqGxjqhEwkKLLYl+BcfOLDSYOBDmruUvz7PYGXFP93fHTqC pxEGE0xrsoz1p3BZlD6wFV73D3lafVSwUzcr6HsqMtcn+pd2zzDpuUXsMSRQE6GcfUOm WBypwAIfbDktEtu6vNsg7l+LG84Sbz2nIze1Hv4K+8Tr3vC6r9QSUpyZ5MPc81ZG1SYL M/yCCoeOZfokgkuzDe3M72VmXuJ2QlnOYq1CvgNCUq46iE1dS2h4kJyrjkFqJzmsF6ON W+6Q== X-Gm-Message-State: AOJu0Yxv9x2uqfnAs9/cEyUavuYBEAy3uKifbGApije2kfHmkN5gLdki /8uCtacmIYFFo0+39X3g2fG3XJZW9fc1dn8N/8wlNsBBQ2yjUAMc3DCWC05qJAF5stC0GB90Fnq fFOjqw3P3Zg== X-Google-Smtp-Source: AGHT+IGMWYiioxnG7omelh4V0T3z0njtoj2X+co3SS255ZXFQTkE7JlCElj/s4tgxjXkNInggTXn9aHyuvdnpw== X-Received: from qtbfe5.prod.google.com ([2002:a05:622a:4d45:b0:471:ede7:80e3]) (user=edumazet job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:1825:b0:471:fadb:9d41 with SMTP id d75a77b69052e-4720810e19fmr50457841cf.17.1739969731326; Wed, 19 Feb 2025 04:55:31 -0800 (PST) Date: Wed, 19 Feb 2025 12:55:22 +0000 In-Reply-To: <20250219125522.2535263-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: <20250219125522.2535263-1-edumazet@google.com> X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog Message-ID: <20250219125522.2535263-5-edumazet@google.com> Subject: [PATCH V2 4/4] 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. Also add 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 ed27c7eab456..bd73bc4707c1 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -125,7 +125,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)) { timer->it_id =3D (timer_t)id; timer->it_signal =3D (struct signal_struct *)((unsigned long)sig | 1UL); --=20 2.48.1.601.g30ceb7b040-goog