The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: jpoimboe@kernel.org, rostedt@kernel.org
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org
Subject: [PATCH 09/12] unwind: Make unwind_task_info::unwind_mask consistent
Date: Wed, 24 Sep 2025 09:59:57 +0200	[thread overview]
Message-ID: <20250924080119.384384486@infradead.org> (raw)
In-Reply-To: 20250924075948.579302904@infradead.org

The unwind_task_info::unwind_mask was manipulated using a mixture of:

  regular store
  WRITE_ONCE()
  try_cmpxchg()
  set_bit()
  atomic_long_*()

Clean up and make it consistently atomic_long_t.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 include/linux/unwind_deferred.h       |    4 ++--
 include/linux/unwind_deferred_types.h |    3 ++-
 kernel/unwind/deferred.c              |   17 +++++++++--------
 3 files changed, 13 insertions(+), 11 deletions(-)

--- a/include/linux/unwind_deferred.h
+++ b/include/linux/unwind_deferred.h
@@ -46,7 +46,7 @@ void unwind_deferred_task_exit(struct ta
 static __always_inline void unwind_reset_info(void)
 {
 	struct unwind_task_info *info = &current->unwind_info;
-	unsigned long bits = info->unwind_mask;
+	unsigned long bits = atomic_long_read(&info->unwind_mask);
 
 	/* Was there any unwinding? */
 	if (likely(!bits))
@@ -56,7 +56,7 @@ static __always_inline void unwind_reset
 		/* Is a task_work going to run again before going back */
 		if (bits & UNWIND_PENDING)
 			return;
-	} while (!try_cmpxchg(&info->unwind_mask, &bits, 0UL));
+	} while (!atomic_long_try_cmpxchg(&info->unwind_mask, &bits, 0UL));
 	current->unwind_info.id.id = 0;
 
 	if (unlikely(info->cache)) {
--- a/include/linux/unwind_deferred_types.h
+++ b/include/linux/unwind_deferred_types.h
@@ -3,6 +3,7 @@
 #define _LINUX_UNWIND_USER_DEFERRED_TYPES_H
 
 #include <linux/types.h>
+#include <linux/atomic.h>
 
 struct unwind_cache {
 	unsigned long		unwind_completed;
@@ -32,7 +33,7 @@ union unwind_task_id {
 };
 
 struct unwind_task_info {
-	unsigned long		unwind_mask;
+	atomic_long_t		unwind_mask;
 	struct unwind_cache	*cache;
 	struct callback_head	work;
 	union unwind_task_id	id;
--- a/kernel/unwind/deferred.c
+++ b/kernel/unwind/deferred.c
@@ -53,7 +53,7 @@ DEFINE_STATIC_SRCU(unwind_srcu);
 
 static inline bool unwind_pending(struct unwind_task_info *info)
 {
-	return test_bit(UNWIND_PENDING_BIT, &info->unwind_mask);
+	return atomic_long_read(&info->unwind_mask) & UNWIND_USED;
 }
 
 /*
@@ -142,7 +142,7 @@ int unwind_user_faultable(struct unwind_
 	cache->nr_entries = trace->nr;
 
 	/* Clear nr_entries on way back to user space */
-	set_bit(UNWIND_USED_BIT, &info->unwind_mask);
+	atomic_long_or(UNWIND_USED, &info->unwind_mask);
 
 	return 0;
 }
@@ -160,7 +160,7 @@ static void process_unwind_deferred(stru
 
 	/* Clear pending bit but make sure to have the current bits */
 	bits = atomic_long_fetch_andnot(UNWIND_PENDING,
-				  (atomic_long_t *)&info->unwind_mask);
+					&info->unwind_mask);
 	/*
 	 * From here on out, the callback must always be called, even if it's
 	 * just an empty trace.
@@ -265,7 +265,7 @@ int unwind_deferred_request(struct unwin
 
 	*cookie = get_cookie(info);
 
-	old = READ_ONCE(info->unwind_mask);
+	old = atomic_long_read(&info->unwind_mask);
 
 	/* Is this already queued or executed */
 	if (old & bit)
@@ -278,7 +278,7 @@ int unwind_deferred_request(struct unwin
 	 * to have a callback.
 	 */
 	bits = UNWIND_PENDING | bit;
-	old = atomic_long_fetch_or(bits, (atomic_long_t *)&info->unwind_mask);
+	old = atomic_long_fetch_or(bits, &info->unwind_mask);
 	if (old & bits) {
 		/*
 		 * If the work's bit was set, whatever set it had better
@@ -292,7 +292,7 @@ int unwind_deferred_request(struct unwin
 	ret = task_work_add(current, &info->work, twa_mode);
 
 	if (WARN_ON_ONCE(ret))
-		WRITE_ONCE(info->unwind_mask, 0);
+		atomic_long_set(&info->unwind_mask, 0);
 
 	return ret;
 }
@@ -324,7 +324,8 @@ void unwind_deferred_cancel(struct unwin
 	guard(rcu)();
 	/* Clear this bit from all threads */
 	for_each_process_thread(g, t) {
-		clear_bit(bit, &t->unwind_info.unwind_mask);
+		atomic_long_andnot(UNWIND_USED,
+				   &t->unwind_info.unwind_mask);
 		if (t->unwind_info.cache)
 			clear_bit(bit, &t->unwind_info.cache->unwind_completed);
 	}
@@ -354,7 +355,7 @@ void unwind_task_init(struct task_struct
 
 	memset(info, 0, sizeof(*info));
 	init_task_work(&info->work, unwind_deferred_task_work);
-	info->unwind_mask = 0;
+	atomic_long_set(&info->unwind_mask, 0);
 }
 
 void unwind_task_free(struct task_struct *task)



  parent reply	other threads:[~2025-09-24  8:03 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-24  7:59 [PATCH 00/12] Various fixes and x86 support Peter Zijlstra
2025-09-24  7:59 ` [PATCH 01/12] task_work: Fix NMI race condition Peter Zijlstra
2025-10-01 15:31   ` Steven Rostedt
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` [PATCH 02/12] unwind: Shorten lines Peter Zijlstra
2025-10-01 15:32   ` Steven Rostedt
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` [PATCH 03/12] unwind: Add required include files Peter Zijlstra
2025-10-01 15:32   ` Steven Rostedt
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` [PATCH 04/12] unwind: Simplify unwind_reset_info() Peter Zijlstra
2025-10-01 15:33   ` Steven Rostedt
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` [PATCH 05/12] unwind: Add comment to unwind_deferred_task_exit() Peter Zijlstra
2025-10-01 15:35   ` Steven Rostedt
2025-10-20 10:16     ` Peter Zijlstra
2025-10-22 15:16       ` Steven Rostedt
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` [PATCH 06/12] unwind: Fix unwind_deferred_request() vs NMI Peter Zijlstra
2025-10-01 15:37   ` Steven Rostedt
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` [PATCH 07/12] unwind: Clarify calling context Peter Zijlstra
2025-10-01 15:38   ` Steven Rostedt
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` [PATCH 08/12] unwind: Simplify unwind_user_faultable() Peter Zijlstra
2025-10-01 15:40   ` Steven Rostedt
2025-10-20 10:17     ` Peter Zijlstra
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` Peter Zijlstra [this message]
2025-10-01 15:47   ` [PATCH 09/12] unwind: Make unwind_task_info::unwind_mask consistent Steven Rostedt
2025-10-20 10:20     ` Peter Zijlstra
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` [PATCH 10/12] unwind: Simplify unwind_user_next_fp() alignment check Peter Zijlstra
2025-10-01 15:55   ` Steven Rostedt
2025-10-20 10:28     ` Peter Zijlstra
2025-10-22 15:20       ` Steven Rostedt
2025-10-23  9:53         ` Peter Zijlstra
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  7:59 ` [PATCH 11/12] unwind: Implement compat fp unwind Peter Zijlstra
2025-10-17 15:47   ` Jens Remus
2025-10-20  9:16     ` Jens Remus
2025-10-20 10:39       ` Peter Zijlstra
2025-10-20 10:48         ` Peter Zijlstra
2025-10-22 15:23           ` Steven Rostedt
2025-10-24 13:45             ` Peter Zijlstra
2025-10-22 14:55         ` Jens Remus
2025-10-24 13:40           ` Peter Zijlstra
2025-10-20 10:38     ` Peter Zijlstra
2025-10-22 18:31   ` Steven Rostedt
2025-10-24 14:10     ` Peter Zijlstra
2025-10-24 14:16       ` Peter Zijlstra
2025-10-29  9:36   ` [tip: perf/core] " tip-bot2 for Peter Zijlstra
2025-09-24  8:00 ` [PATCH 12/12] unwind_user/x86: Enable frame pointer unwinding on x86 Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250924080119.384384486@infradead.org \
    --to=peterz@infradead.org \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox