From mboxrd@z Thu Jan 1 00:00:00 1970 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 7806F2D5CFC; Mon, 5 May 2025 22:20: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=1746483653; cv=none; b=iXCYDkTrLpsQY1MdakGEREaiICsFYaKlpG+EL9pqI+iHCNyL96WzMU7MYJaFtXHmer9TzRCUcGfbGqkoB1G9Yxh4VrU/aY2Q86ggXFu37h8NIbiwzioeFD0VzoZheIhmWMMYhe9cPYxyEyNdYTxXZRDz//xz28bNGCKnIY/oVd8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746483653; c=relaxed/simple; bh=f6DoBQYCAn64ShbpXxTa0d3hwlLhNcvZWiAOMQZcbys=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TeZQhcFi1SyvGmQYY2Ti+pSi840yhZpm41yVkuugitaVhF4JmvLPEkIMNHzSQW58D2RLgRtdMftvDOu7G0UeYRHbxEFaFXXqVuFjl82qisCe9Hc1THdXp7t9Vs+0C1fRz411LTt3a1gaF3y9QHbFVu0BgjMetdyiSjhoWmFxyzQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MolymN6S; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MolymN6S" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A73EAC4CEF1; Mon, 5 May 2025 22:20:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746483653; bh=f6DoBQYCAn64ShbpXxTa0d3hwlLhNcvZWiAOMQZcbys=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MolymN6SMF7IgpmPE3U3i0YO/GtoUzDllBkxb0+sdKiYWgm65/gTlR04n5p3whbsG DOH39/cp1xjM1Un+7s7+prNDfAWk0VXR0W2+lQh7wQfUJ/I2x5T6HPRNurrXS/Ot7k 3k/LxpO7u5myaJIy6F6vK4bcZKpi5/oaEOUCJ+ii5FMb/OtqtAI+Vo7pa3hL9+5P3j NUb2emsc4tptqz7QujjdkH8wKVximVQk+wY570lVzeaoOXVfhCgh4ZLiMsWpBZJk1A CXy7Bp3b+3CJ2YUJnYUIpArpJuKEw++DoZXpDpPp4gvr8IaJequhtZjCF3N+MtgdHj b+FvH7hdiFRow== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Thomas Gleixner , Frederic Weisbecker , Sasha Levin , anna-maria@linutronix.de Subject: [PATCH AUTOSEL 6.14 163/642] posix-timers: Ensure that timer initialization is fully visible Date: Mon, 5 May 2025 18:06:19 -0400 Message-Id: <20250505221419.2672473-163-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505221419.2672473-1-sashal@kernel.org> References: <20250505221419.2672473-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.14.5 Content-Transfer-Encoding: 8bit From: Thomas Gleixner [ Upstream commit 2389c6efd3ad8edb3bcce0019b4edcc7d9c7de19 ] Frederic pointed out that the memory operations to initialize the timer are not guaranteed to be visible, when __lock_timer() observes timer::it_signal valid under timer::it_lock: T0 T1 --------- ----------- do_timer_create() // A new_timer->.... = .... spin_lock(current->sighand) // B WRITE_ONCE(new_timer->it_signal, current->signal) spin_unlock(current->sighand) sys_timer_*() t = __lock_timer() spin_lock(&timr->it_lock) // observes B if (timr->it_signal == current->signal) return timr; if (!t) return; // Is not guaranteed to observe A Protect the write of timer::it_signal, which makes the timer valid, with timer::it_lock as well. This guarantees that T1 must observe the initialization A completely, when it observes the valid signal pointer under timer::it_lock. sighand::siglock must still be taken to protect the signal::posix_timers list. Reported-by: Frederic Weisbecker Suggested-by: Frederic Weisbecker Signed-off-by: Thomas Gleixner Reviewed-by: Frederic Weisbecker Link: https://lore.kernel.org/all/20250308155623.507944489@linutronix.de Signed-off-by: Sasha Levin --- kernel/time/posix-timers.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 7f8644e047558..15ed343693101 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -463,14 +463,21 @@ static int do_timer_create(clockid_t which_clock, struct sigevent *event, if (error) goto out; - spin_lock_irq(¤t->sighand->siglock); - /* This makes the timer valid in the hash table */ - WRITE_ONCE(new_timer->it_signal, current->signal); - hlist_add_head(&new_timer->list, ¤t->signal->posix_timers); - spin_unlock_irq(¤t->sighand->siglock); /* - * After unlocking sighand::siglock @new_timer is subject to - * concurrent removal and cannot be touched anymore + * timer::it_lock ensures that __lock_timer() observes a fully + * initialized timer when it observes a valid timer::it_signal. + * + * sighand::siglock is required to protect signal::posix_timers. + */ + scoped_guard (spinlock_irq, &new_timer->it_lock) { + guard(spinlock)(¤t->sighand->siglock); + /* This makes the timer valid in the hash table */ + WRITE_ONCE(new_timer->it_signal, current->signal); + hlist_add_head(&new_timer->list, ¤t->signal->posix_timers); + } + /* + * After unlocking @new_timer is subject to concurrent removal and + * cannot be touched anymore */ return 0; out: -- 2.39.5