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

From: Hu Xinyao

Date: Tue Dec 01 2026 - 22:32:59 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Hu Xinyao <huxinyao@xxxxxxxxxxxxxxx>
---
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