Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH] rv: Synchronize per-object storage hash table and fix exit cleanup
@ 2026-12-02  3:30 Hu Xinyao
  2026-12-02  3:45 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hu Xinyao @ 2026-12-02  3:30 UTC (permalink / raw)
  To: Steven Rostedt, Gabriele Monaco
  Cc: Nam Cao, linux-trace-kernel, linux-kernel, Hu Xinyao

The per-object DA monitor helpers in include/rv/da_monitor.h maintain
da_monitor_ht without writer synchronization and exhibit three lifetime
and publication hazards in the deadline/nomiss monitor:

1. da_create_empty_storage() publishes mon_storage into da_monitor_ht via
   hash_add_rcu() while __GFP_ZERO fields mon_storage->id and
   mon_storage->target are still 0/NULL, before assigning id and
   target. Concurrent RCU readers walking the same hash bucket (where
   id == 0 aliases fair_server_id(0)) can observe the partial entry and
   dereference a NULL target.

2. Multiple tracepoint handlers (such as nomiss handle_sys_enter(),
   handle_newtask(), and handle_exit()) insert into and delete from
   da_monitor_ht concurrently across CPUs without mutual exclusion,
   corrupting bucket hlist pointers or inserting duplicate entries for
   the same id.

3. In deadline.h, handle_exit() only destroys storage when
   p->policy == SCHED_DEADLINE. A task whose sched_setattr() failed
   after handle_sys_enter() pre-allocated storage, or that switched
   back to a non-deadline policy before exiting, leaves mon_storage in
   da_monitor_ht with mon_storage->target pointing to the freed
   &p->dl inside struct task_struct, causing a use-after-free when the
   PID is later reused or iterated. Conversely, handle_sys_enter() on an
   already-exiting target task can allocate storage after handle_exit()
   has run.

Serialize da_monitor_ht writers with da_monitor_ht_lock, initialize id
and target prior to hash_add_rcu(), keep a lockless RCU lookup fast-path
in da_destroy_storage(), defer resetting/freeing via call_rcu() (with
rcu_barrier() in da_monitor_destroy()) so in-flight RCU readers cannot
re-arm HA timers after teardown, clean up storage for any task
(p->pid > 0) in handle_exit(), and skip PF_EXITING tasks in
handle_sys_enter().

Tested in QEMU with CONFIG_KASAN=y, CONFIG_PROVE_LOCKING=y,
CONFIG_DEBUG_LIST=y, rv_test_nomiss KUnit, and concurrent
sched_setattr()/exit stress across unprivileged failures, policy
transitions back to SCHED_OTHER, and exit races.

Fixes: 4a24127bd6cb ("rv: Add support for per-object monitors in DA/HA")
Fixes: b133207deb72 ("rv: Add nomiss deadline monitor")
Cc: stable@vger.kernel.org
Signed-off-by: Hu Xinyao <huxinyao@kylinsec.com.cn>
---
 include/rv/da_monitor.h                      | 134 +++++++++++++------
 kernel/trace/rv/monitors/deadline/deadline.h |   2 +-
 kernel/trace/rv/monitors/nomiss/nomiss.c     |   3 +-
 3 files changed, 94 insertions(+), 45 deletions(-)

diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h
index 6fc5ef8..5c3cc9a 100644
--- a/include/rv/da_monitor.h
+++ b/include/rv/da_monitor.h
@@ -22,6 +22,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/hashtable.h>
+#include <linux/spinlock.h>
 
 /*
  * Per-cpu variables require a unique name although static in some
@@ -416,30 +417,77 @@ struct da_monitor_storage {
 #define DA_MONITOR_HT_BITS 10
 #endif
 static DEFINE_HASHTABLE(da_monitor_ht, DA_MONITOR_HT_BITS);
+static DEFINE_RAW_SPINLOCK(da_monitor_ht_lock);
 
 /*
- * da_create_empty_storage - pre-allocate an empty storage
+ * __da_get_mon_storage - get the monitor storage from the hash table
  */
-static inline struct da_monitor_storage *da_create_empty_storage(da_id_type id)
+static inline struct da_monitor_storage *__da_get_mon_storage(da_id_type id)
 {
 	struct da_monitor_storage *mon_storage;
 
+	lockdep_assert_in_rcu_read_lock();
+	hash_for_each_possible_rcu(da_monitor_ht, mon_storage, node, id) {
+		if (mon_storage->id == id)
+			return mon_storage;
+	}
+
+	return NULL;
+}
+
+static inline struct da_monitor_storage *
+__da_get_mon_storage_locked(da_id_type id)
+{
+	struct da_monitor_storage *mon_storage;
+
+	lockdep_assert_held(&da_monitor_ht_lock);
+	hash_for_each_possible(da_monitor_ht, mon_storage, node, id) {
+		if (mon_storage->id == id)
+			return mon_storage;
+	}
+
+	return NULL;
+}
+
+static inline struct da_monitor_storage *
+__da_insert_storage(da_id_type id, monitor_target target)
+{
+	struct da_monitor_storage *mon_storage, *existing;
+
 	mon_storage = kmalloc_nolock(sizeof(struct da_monitor_storage),
 				     __GFP_ZERO, NUMA_NO_NODE);
 	if (!mon_storage)
 		return NULL;
 
-	hash_add_rcu(da_monitor_ht, &mon_storage->node, id);
 	mon_storage->id = id;
+	mon_storage->target = target;
+
+	scoped_guard(raw_spinlock_irqsave, &da_monitor_ht_lock) {
+		existing = __da_get_mon_storage_locked(id);
+		if (!existing)
+			hash_add_rcu(da_monitor_ht, &mon_storage->node, id);
+		else if (target)
+			WRITE_ONCE(existing->target, target);
+	}
+
+	if (existing) {
+		kfree_nolock(mon_storage);
+		return existing;
+	}
+
 	return mon_storage;
 }
 
+/*
+ * da_create_empty_storage - pre-allocate an empty storage
+ */
+static inline struct da_monitor_storage *da_create_empty_storage(da_id_type id)
+{
+	return __da_insert_storage(id, NULL);
+}
+
 /*
  * da_create_storage - create the per-object storage
- *
- * The caller is responsible to synchronise writers, either with locks or
- * implicitly. For instance, if da_create_storage is only called from a single
- * event for target (e.g. sched_switch), it's safe to call this without locks.
  */
 static inline struct da_monitor *da_create_storage(da_id_type id,
 						   monitor_target target,
@@ -450,28 +498,8 @@ static inline struct da_monitor *da_create_storage(da_id_type id,
 	if (da_mon)
 		return da_mon;
 
-	mon_storage = da_create_empty_storage(id);
-	if (!mon_storage)
-		return NULL;
-
-	mon_storage->target = target;
-	return &mon_storage->rv.da_mon;
-}
-
-/*
- * __da_get_mon_storage - get the monitor storage from the hash table
- */
-static inline struct da_monitor_storage *__da_get_mon_storage(da_id_type id)
-{
-	struct da_monitor_storage *mon_storage;
-
-	lockdep_assert_in_rcu_read_lock();
-	hash_for_each_possible_rcu(da_monitor_ht, mon_storage, node, id) {
-		if (mon_storage->id == id)
-			return mon_storage;
-	}
-
-	return NULL;
+	mon_storage = __da_insert_storage(id, target);
+	return mon_storage ? &mon_storage->rv.da_mon : NULL;
 }
 
 /*
@@ -490,7 +518,8 @@ static struct da_monitor *da_get_monitor(da_id_type id, monitor_target target)
  */
 static inline monitor_target da_get_target(struct da_monitor *da_mon)
 {
-	return container_of(da_mon, struct da_monitor_storage, rv.da_mon)->target;
+	return READ_ONCE(container_of(da_mon, struct da_monitor_storage,
+				      rv.da_mon)->target);
 }
 
 /*
@@ -523,8 +552,13 @@ static inline struct da_monitor *da_fill_empty_storage(da_id_type id,
 						       monitor_target target,
 						       struct da_monitor *da_mon)
 {
-	if (unlikely(da_mon && !da_get_target(da_mon)))
-		container_of(da_mon, struct da_monitor_storage, rv.da_mon)->target = target;
+	struct da_monitor_storage *mon_storage;
+
+	if (unlikely(da_mon && !da_get_target(da_mon))) {
+		mon_storage = container_of(da_mon, struct da_monitor_storage,
+					   rv.da_mon);
+		WRITE_ONCE(mon_storage->target, target);
+	}
 	return da_mon;
 }
 
@@ -540,31 +574,43 @@ static inline monitor_target da_get_target_by_id(da_id_type id)
 
 	if (unlikely(!mon_storage))
 		return NULL;
-	return mon_storage->target;
+	return READ_ONCE(mon_storage->target);
+}
+
+static void __maybe_unused da_destroy_storage_rcu(struct rcu_head *rcu)
+{
+	struct da_monitor_storage *mon_storage =
+		container_of(rcu, struct da_monitor_storage, rcu);
+
+	da_monitor_reset(&mon_storage->rv.da_mon);
+	kfree_rcu(mon_storage, rcu);
 }
 
 /*
  * da_destroy_storage - destroy the per-object storage
  *
- * The caller is responsible to synchronise writers, either with locks or
- * implicitly. For instance, if da_destroy_storage is called at sched_exit and
- * da_create_storage can never occur after that, it's safe to call this without
- * locks.
  * This function includes an RCU read-side critical section to synchronise
- * against da_monitor_destroy().
+ * against da_monitor_destroy(), and defers resetting/freeing until after RCU
+ * grace periods so in-flight readers cannot re-arm HA timers after teardown.
  */
 static inline void da_destroy_storage(da_id_type id)
 {
-	struct da_monitor_storage *mon_storage;
+	struct da_monitor_storage *mon_storage = NULL;
 
 	guard(rcu)();
-	mon_storage = __da_get_mon_storage(id);
+	if (!__da_get_mon_storage(id))
+		return;
+
+	scoped_guard(raw_spinlock_irqsave, &da_monitor_ht_lock) {
+		mon_storage = __da_get_mon_storage_locked(id);
+		if (mon_storage)
+			hash_del_rcu(&mon_storage->node);
+	}
 
 	if (!mon_storage)
 		return;
-	da_monitor_reset_hook(&mon_storage->rv.da_mon);
-	hash_del_rcu(&mon_storage->node);
-	kfree_rcu(mon_storage, rcu);
+
+	call_rcu(&mon_storage->rcu, da_destroy_storage_rcu);
 }
 
 static void __da_monitor_reset_all(void (*reset)(struct da_monitor *))
@@ -589,6 +635,7 @@ static inline void da_monitor_reset_state_all(void)
 
 static inline int da_monitor_init(void)
 {
+	raw_spin_lock_init(&da_monitor_ht_lock);
 	hash_init(da_monitor_ht);
 	return 0;
 }
@@ -600,6 +647,7 @@ static inline void da_monitor_destroy(void)
 	int bkt;
 
 	tracepoint_synchronize_unregister();
+	rcu_barrier();
 	da_monitor_reset_all();
 	da_monitor_sync_hook();
 	/*
diff --git a/kernel/trace/rv/monitors/deadline/deadline.h b/kernel/trace/rv/monitors/deadline/deadline.h
index 78fca87..d7609f2 100644
--- a/kernel/trace/rv/monitors/deadline/deadline.h
+++ b/kernel/trace/rv/monitors/deadline/deadline.h
@@ -196,7 +196,7 @@ static void __maybe_unused handle_newtask(void *data, struct task_struct *task,
 
 static void __maybe_unused handle_exit(void *data, struct task_struct *p, bool group_dead)
 {
-	if (p->policy == SCHED_DEADLINE)
+	if (p->pid > 0)
 		da_destroy_storage(get_entity_id(&p->dl, DL_TASK, DL_TASK));
 }
 
diff --git a/kernel/trace/rv/monitors/nomiss/nomiss.c b/kernel/trace/rv/monitors/nomiss/nomiss.c
index 6e47d37..248cc14 100644
--- a/kernel/trace/rv/monitors/nomiss/nomiss.c
+++ b/kernel/trace/rv/monitors/nomiss/nomiss.c
@@ -194,7 +194,8 @@ static void handle_sys_enter(void *data, struct pt_regs *regs, long id)
 		return;
 	guard(rcu)();
 	p = pid ? find_task_by_vpid(pid) : current;
-	if (unlikely(!p) || new_policy == p->policy)
+	if (unlikely(!p) || (READ_ONCE(p->flags) & PF_EXITING) ||
+	    new_policy == p->policy)
 		return;
 
 	if (p->policy == SCHED_DEADLINE)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] rv: Synchronize per-object storage hash table and fix exit cleanup
  2026-12-02  3:30 [PATCH] rv: Synchronize per-object storage hash table and fix exit cleanup Hu Xinyao
@ 2026-12-02  3:45 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  3:45 UTC (permalink / raw)
  To: Hu Xinyao; +Cc: linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] rv: deferred reset in da_destroy_storage() leaves HA timer
  armed on exit
- [Medium] rv: TOCTOU race on PF_EXITING in handle_sys_enter() leaks
  dangling target
--

commit e1aa8fb39dc01486d1f10cf191d5e9abefc77a07
Author: Hu Xinyao <huxinyao@kylinsec.com.cn>

rv: Synchronize per-object storage hash table and fix exit cleanup

This commit serializes da_monitor_ht updates with a spinlock, defers
per-object monitor reset and teardown via RCU, and updates exit and
syscall entry handling in the deadline and nomiss monitors.

> diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h
> index 6fc5ef8142aca..5c3cc9ab91a65 100644
> --- a/include/rv/da_monitor.h
> +++ b/include/rv/da_monitor.h
[ ... ]
> @@ -540,31 +574,43 @@ static inline monitor_target da_get_target_by_id(da_id_type id)
>  
>  	if (unlikely(!mon_storage))
>  		return NULL;
> -	return mon_storage->target;
> +	return READ_ONCE(mon_storage->target);
> +}
> +
> +static void __maybe_unused da_destroy_storage_rcu(struct rcu_head *rcu)
> +{
> +	struct da_monitor_storage *mon_storage =
> +		container_of(rcu, struct da_monitor_storage, rcu);
> +
> +	da_monitor_reset(&mon_storage->rv.da_mon);
> +	kfree_rcu(mon_storage, rcu);
>  }
>  
>  /*
>   * da_destroy_storage - destroy the per-object storage
>   *
> - * The caller is responsible to synchronise writers, either with locks or
> - * implicitly. For instance, if da_destroy_storage is called at sched_exit and
> - * da_create_storage can never occur after that, it's safe to call this without
> - * locks.
>   * This function includes an RCU read-side critical section to synchronise
> - * against da_monitor_destroy().
> + * against da_monitor_destroy(), and defers resetting/freeing until after RCU
> + * grace periods so in-flight readers cannot re-arm HA timers after teardown.
>   */
>  static inline void da_destroy_storage(da_id_type id)
>  {
> -	struct da_monitor_storage *mon_storage;
> +	struct da_monitor_storage *mon_storage = NULL;
>  
>  	guard(rcu)();
> -	mon_storage = __da_get_mon_storage(id);
> +	if (!__da_get_mon_storage(id))
> +		return;
> +
> +	scoped_guard(raw_spinlock_irqsave, &da_monitor_ht_lock) {
> +		mon_storage = __da_get_mon_storage_locked(id);
> +		if (mon_storage)
> +			hash_del_rcu(&mon_storage->node);
> +	}
>  
>  	if (!mon_storage)
>  		return;
> -	da_monitor_reset_hook(&mon_storage->rv.da_mon);
> -	hash_del_rcu(&mon_storage->node);
> -	kfree_rcu(mon_storage, rcu);
> +
> +	call_rcu(&mon_storage->rcu, da_destroy_storage_rcu);
>  }

[Severity: High]
Does deferring da_monitor_reset() to da_destroy_storage_rcu() without
also resetting synchronously in da_destroy_storage() leave ha_mon->timer
armed across the RCU grace period when an active SCHED_DEADLINE task
exits?

When a running deadline task calls do_exit(), trace_sched_process_exit()
runs handle_exit() -> da_destroy_storage() while the task is still on the
CPU in state running_nomiss with ha_mon->timer armed.  Because
da_destroy_storage() unhashes mon_storage without resetting it, the later
__schedule() in do_task_dead() cannot find the monitor to deliver
sched_switch_suspend_nomiss and cancel the timer.

If the task's deadline expires before the RCU grace period finishes,
__ha_monitor_timer_callback() sees da_mon.monitoring still set, fires a
false-positive deadline miss via ha_react(), and dereferences
ha_get_target(ha_mon) (&p->dl), which can also race with
delayed_put_task_struct() freeing p in the same RCU grace period
(use-after-free).  Calling da_monitor_reset(&mon_storage->rv.da_mon)
synchronously after unhashing mon_storage, in addition to the reset in
da_destroy_storage_rcu(), would cancel the timer on exit while still
catching any timer re-armed by an in-flight reader.

> diff --git a/kernel/trace/rv/monitors/nomiss/nomiss.c b/kernel/trace/rv/monitors/nomiss/nomiss.c
> index 6e47d379f7770..248cc14beab4a 100644
> --- a/kernel/trace/rv/monitors/nomiss/nomiss.c
> +++ b/kernel/trace/rv/monitors/nomiss/nomiss.c
> @@ -194,7 +194,8 @@ static void handle_sys_enter(void *data, struct pt_regs *regs, long id)
>  		return;
>  	guard(rcu)();
>  	p = pid ? find_task_by_vpid(pid) : current;
> -	if (unlikely(!p) || new_policy == p->policy)
> +	if (unlikely(!p) || (READ_ONCE(p->flags) & PF_EXITING) ||
> +	    new_policy == p->policy)
>  		return;
>  
>  	if (p->policy == SCHED_DEADLINE)

[Severity: Medium]
Can a target task (pid != 0) enter do_exit() after handle_sys_enter()
checks PF_EXITING but before __da_insert_storage() inserts mon_storage,
leaving a dangling target pointer in da_monitor_ht?

CPU0                              CPU1 (task p)
handle_sys_enter()
  p = find_task_by_vpid(pid)
  !(p->flags & PF_EXITING)
  da_create_or_get(p->pid, &p->dl)
    ...
      kmalloc_nolock(...)
                                  do_exit()
                                    exit_signals(p)
                                      p->flags |= PF_EXITING
                                    trace_sched_process_exit(p, ...)
                                      handle_exit()
                                        da_destroy_storage(p->pid)
                                          /* not in da_monitor_ht */
      lock(&da_monitor_ht_lock)
      hash_add_rcu(..., &mon_storage->node, p->pid)
      unlock(&da_monitor_ht_lock)
                                    ...
                                    delayed_put_task_struct()
                                      free_task(p)
                                  /* later, after p->pid is reused */
                                  handle_sched_switch()
                                    ...
                                      ha_get_env()
                                        ha_get_target(ha_mon)->dl_defer
                                          <-- use-after-free

When p->pid is later reused by a new task that enters SCHED_DEADLINE,
da_create_or_get() finds the leaked mon_storage via da_get_monitor() and
da_create_storage() returns early because da_mon is non-NULL, bypassing
the existing->target update in __da_insert_storage().  Because
mon_storage->target is already non-NULL, da_fill_empty_storage() does not
overwrite it either, so subsequent nomiss events dereference the freed
task_struct.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/DD5EA120CBEA8D43+1796182228.53654-1-huxinyao@kylinsec.com.cn?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  3:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  3:30 [PATCH] rv: Synchronize per-object storage hash table and fix exit cleanup Hu Xinyao
2026-12-02  3:45 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox