Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hu Xinyao" <huxinyao@kylinsec.com.cn>
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH] rv: Synchronize per-object storage hash table and fix exit cleanup
Date: Wed, 02 Dec 2026 03:45:04 +0000	[thread overview]
Message-ID: <sashiko-outbox-244797@kernel.org> (raw)
In-Reply-To: <DD5EA120CBEA8D43+1796182228.53654-1-huxinyao@kylinsec.com.cn>

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

      reply	other threads:[~2026-12-02  3:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=sashiko-outbox-244797@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=huxinyao@kylinsec.com.cn \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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