Re: [PATCH v8 09/14] rv: Add generic uprobe infrastructure for RV monitors

From: Gabriele Monaco

Date: Fri Nov 20 2026 - 07:12:49 EST


On Fri, 2026-11-06 at 01:49 +0800, wen.yang@xxxxxxxxx wrote:
> From: Wen Yang <wen.yang@xxxxxxxxx>
>
> Monitors that instrument user-space function boundaries need to register
> uprobes and deregister them safely.  Provide a thin wrapper so monitors
> share a single implementation of this boilerplate.
>
> struct rv_uprobe embeds struct uprobe_consumer directly, avoiding a
> separate heap allocation per probe.  The struct holds an inode reference
> (acquired via igrab()) for the probed binary for the full uprobe lifetime,
> as uprobe_register() does not take an inode reference.  On unregister,
> the inode reference is released with iput() only after
> rv_uprobe_unregister_sync().
>
> rv_uprobe_unregister_sync() calls uprobe_unregister_sync(), draining
> all in-flight handlers before the containing struct may be freed.
>
> The API provides register, nosync unregister, a global handler barrier
> (rv_uprobe_unregister_sync), and an active-state predicate.
>
> Suggested-by: Gabriele Monaco <gmonaco@xxxxxxxxxx>
> Signed-off-by: Wen Yang <wen.yang@xxxxxxxxx>
> ---

This API requires a lot of manual steps from the callers. We can refine
it later on but at least I'd simplify the teardown.

I remember we discussed this already. The uprobe registration requires
both inode and mount to stay alive, path_get() does both, so I don't
think we really need also the igrab()/iput().

Do users really need to carry the inode around together with the path?
Other APIs (e.g. bpf_trace.c and trace_uprobe.c) pass only the path and
get the inode internally, I don't see why we cannot do that and mandate
the path is owned by the caller (which is already the case).

All the other logic might be fine in the callers, but at least let's aim
to simplify what is general enough.

Thanks,
Gabriele

>  include/rv/rv_uprobe.h      | 35 +++++++++++++++++++++++
>  kernel/trace/rv/Kconfig     |  4 +++
>  kernel/trace/rv/Makefile    |  1 +
>  kernel/trace/rv/rv_uprobe.c | 56 +++++++++++++++++++++++++++++++++++++
>  4 files changed, 96 insertions(+)
>  create mode 100644 include/rv/rv_uprobe.h
>  create mode 100644 kernel/trace/rv/rv_uprobe.c
>
> diff --git a/include/rv/rv_uprobe.h b/include/rv/rv_uprobe.h
> new file mode 100644
> index 000000000000..b93c2a81bc22
> --- /dev/null
> +++ b/include/rv/rv_uprobe.h
> @@ -0,0 +1,35 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/* Copyright (C) 2026 Wen Yang <wen.yang@xxxxxxxxx> */
> +/*
> + * Generic uprobe infrastructure for RV monitors.
> + */
> +
> +#ifndef _RV_UPROBE_H
> +#define _RV_UPROBE_H
> +
> +#include <linux/types.h>
> +#include <linux/uprobes.h>
> +
> +struct inode;
> +struct pt_regs;
> +
> +/*
> + * Free the containing struct only after rv_uprobe_unregister_nosync() +
> + * rv_uprobe_unregister_sync() + iput(p->inode).
> + *
> + * The inode reference does not pin the dentry, so callers that re-resolve
> the
> + * probed path must hold a struct path: 9p cache=none remints an inode per
> + * lookup.
> + */
> +struct rv_uprobe {
> + struct uprobe_consumer uc;
> + struct uprobe *uprobe;
> + struct inode *inode;
> +};
> +
> +bool rv_uprobe_is_registered(const struct rv_uprobe *p);
> +int rv_uprobe_register(struct inode *inode, loff_t offset, struct rv_uprobe
> *p);
> +void rv_uprobe_unregister_nosync(struct rv_uprobe *p);
> +void rv_uprobe_unregister_sync(void);
> +
> +#endif /* _RV_UPROBE_H */
> diff --git a/kernel/trace/rv/Kconfig b/kernel/trace/rv/Kconfig
> index efa930f94ea4..2bc412304293 100644
> --- a/kernel/trace/rv/Kconfig
> +++ b/kernel/trace/rv/Kconfig
> @@ -86,6 +86,10 @@ source "kernel/trace/rv/monitors/nomiss/Kconfig"
>  
>  # Add new monitors here
>  
> +config RV_UPROBE
> + bool
> + depends on RV && UPROBES
> +
>  config RV_REACTORS
>   bool "Runtime verification reactors"
>   default y
> diff --git a/kernel/trace/rv/Makefile b/kernel/trace/rv/Makefile
> index 4b84b83b942b..8dc14a1819b8 100644
> --- a/kernel/trace/rv/Makefile
> +++ b/kernel/trace/rv/Makefile
> @@ -22,6 +22,7 @@ obj-$(CONFIG_RV_MON_DEADLINE) +=
> monitors/deadline/deadline.o
>  obj-$(CONFIG_RV_MON_NOMISS) += monitors/nomiss/nomiss.o
>  obj-$(CONFIG_RV_MON_WAKEUP) += monitors/wakeup/wakeup.o
>  # Add new monitors here
> +obj-$(CONFIG_RV_UPROBE) += rv_uprobe.o
>  obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
>  obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
>  obj-$(CONFIG_RV_REACT_PANIC) += reactor_panic.o
> diff --git a/kernel/trace/rv/rv_uprobe.c b/kernel/trace/rv/rv_uprobe.c
> new file mode 100644
> index 000000000000..1c7f8466aeed
> --- /dev/null
> +++ b/kernel/trace/rv/rv_uprobe.c
> @@ -0,0 +1,56 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Generic uprobe infrastructure for RV monitors.
> + */
> +#include <linux/fs.h>
> +#include <linux/uprobes.h>
> +#include <rv/rv_uprobe.h>
> +
> +/*
> + * uprobe_register() takes no inode reference of its own, so grab one here
> and
> + * drop it only after rv_uprobe_unregister_sync().
> + */
> +int rv_uprobe_register(struct inode *inode, loff_t offset, struct rv_uprobe
> *p)
> +{
> + int ret;
> +
> + p->inode = igrab(inode);
> + if (!p->inode)
> + return -ESTALE;
> +
> + p->uprobe = uprobe_register(inode, offset, 0, &p->uc);
> + if (IS_ERR(p->uprobe)) {
> + ret = PTR_ERR(p->uprobe);
> + p->uprobe = NULL;
> + iput(p->inode);
> + p->inode = NULL;
> + return ret;
> + }
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(rv_uprobe_register);
> +
> +bool rv_uprobe_is_registered(const struct rv_uprobe *p)
> +{
> + return p && p->uprobe;
> +}
> +EXPORT_SYMBOL_GPL(rv_uprobe_is_registered);
> +
> +/* The inode stays held: in-flight handlers may still reach it. */
> +void rv_uprobe_unregister_nosync(struct rv_uprobe *p)
> +{
> + if (!p || !p->uprobe)
> + return;
> +
> + uprobe_unregister_nosync(p->uprobe, &p->uc);
> + p->uprobe = NULL;
> +}
> +EXPORT_SYMBOL_GPL(rv_uprobe_unregister_nosync);
> +
> +/* Global barrier over every probe dequeued so far. */
> +void rv_uprobe_unregister_sync(void)
> +{
> + uprobe_unregister_sync();
> +}
> +EXPORT_SYMBOL_GPL(rv_uprobe_unregister_sync);