Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jinchao Wang" <wangjinchao600@gmail.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v2 13/13] Documentation/dev-tools: document KWatch
Date: Fri, 17 Jul 2026 13:30:15 +0000	[thread overview]
Message-ID: <20260717133016.506F81F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260717130726.1904101-1-wangjinchao600@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Race condition during CPU offline leads to spurious WARN_ONCE and potential Use-After-Free.
- [High] KWatch blindly filters out hits from the armed task's callees and local interrupts.
- [Medium] Missing Kconfig dependency on TRACING/TRACEPOINTS.
--

--- Patch [13]: [RFC PATCH v2 13/13] Documentation/dev-tools: document KWatch ---
commit 2276ea12010818a130be33709ca8b2c8928c26a9
Author: Jinchao Wang <wangjinchao600@gmail.com>

Documentation/dev-tools: document KWatch

Describe what KWatch is for, how it compares with KASAN and KFENCE,
the debugfs configuration interface, the watch expression syntax,
how to read hits from the trace buffer (including after a crash),
and the current limitations.

> diff --git a/mm/kwatch/Kconfig b/mm/kwatch/Kconfig
> new file mode 100644
> index 0000000000000..6ec9aa448ecec
> --- /dev/null
> +++ b/mm/kwatch/Kconfig
> @@ -0,0 +1,28 @@
> +config KWATCH
> +	tristate "Kernel Watch Framework"
> +	depends on PERF_EVENTS && HAVE_HW_BREAKPOINT && DEBUG_FS
> +	depends on HAVE_REINSTALL_HW_BREAKPOINT
> +	depends on KPROBES && KRETPROBES
> +	depends on STACKTRACE

[Severity: Medium]
Should this configuration depend on or select TRACING or TRACEPOINTS?

Since KWatch outputs successful memory access hits exclusively through the
kwatch_hit tracepoint, building without tracepoint support results in KWatch
compiling and consuming hardware debug registers, but silently failing to
report any hits when memory corruption occurs.

> +	help
> +	  A generalized hardware-assisted memory monitor utility.

[ ... ]

> diff --git a/mm/kwatch/hwbp.c b/mm/kwatch/hwbp.c
> new file mode 100644
> index 0000000000000..d1e93754cce82
> --- /dev/null
> +++ b/mm/kwatch/hwbp.c

[ ... ]

> +static int kwatch_hwbp_cpu_offline(unsigned int cpu)
> +{
> +	struct kwatch_watchpoint *wp;
> +	struct perf_event *bp;
> +
> +	mutex_lock(&kwatch_all_wp_mutex);
> +	list_for_each_entry(wp, &kwatch_all_wp_list, list) {
> +		bp = per_cpu(*wp->event, cpu);
> +		if (bp) {
> +			unregister_hw_breakpoint(bp);
> +			per_cpu(*wp->event, cpu) = NULL;

[Severity: High]
Does this sequence open a race condition during CPU offline?

If an asynchronous IPI from kwatch_hwbp_arm() interrupts the offlining CPU
exactly between the unregistration and the pointer being cleared:

	unregister_hw_breakpoint(bp);
	/* IPI arrives here */
	per_cpu(*wp->event, cpu) = NULL;

The IPI handler in kwatch_hwbp_arm_local() will read the non-NULL,
unregistered breakpoint and pass it to modify_wide_hw_breakpoint_local().

Could this access unlinked and potentially freed memory (if the RCU grace
period elapses), corrupting debug registers or triggering a spurious
WARN_ONCE?

> +		}
> +	}
> +	mutex_unlock(&kwatch_all_wp_mutex);
> +	return 0;
> +}

[ ... ]

> diff --git a/mm/kwatch/probe.c b/mm/kwatch/probe.c
> new file mode 100644
> index 0000000000000..249aa50c9f782
> --- /dev/null
> +++ b/mm/kwatch/probe.c

[ ... ]

> +bool kwatch_probe_validate_hit(struct pt_regs *regs,
> +			       struct task_struct *arm_tsk)
> +{
> +	struct kwatch_tsk_ctx *ctx = kwatch_tsk_ctx_get(false);
> +	const struct kwatch_config *cfg = kwatch_probe_ctx.cfg;
> +
> +	if (unlikely(!ctx || !cfg))
> +		return true;
> +
> +	if (arm_tsk != current || ctx->depth != cfg->depth + 1)
> +		return true;
> +
> +	return false;

[Severity: High]
Does this logic inadvertently filter out genuine corruptions?

Returning false when arm_tsk == current and ctx->depth == cfg->depth + 1
prevents hits from being logged for the current function. However, since
depth is only incremented when recursively entering the watched function,
does this also silently ignore memory corruptions caused by the watched
function's callees, or by any interrupt handler that preempts the watched
task on the same CPU?

> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260717125023.1895892-1-wangjinchao600@gmail.com?part=13

  reply	other threads:[~2026-07-17 13:30 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-17 12:50 [RFC PATCH v2 00/13] mm/kwatch: dynamic hardware watchpoints for hunting memory corruption Jinchao Wang
2026-07-17 13:02 ` [RFC PATCH v2 01/13] arch: add HAVE_REINSTALL_HW_BREAKPOINT Jinchao Wang
2026-07-17 13:02 ` [RFC PATCH v2 02/13] x86/hw_breakpoint: Unify breakpoint install/uninstall Jinchao Wang
2026-07-17 13:15   ` sashiko-bot
2026-07-17 13:03 ` [RFC PATCH v2 03/13] x86/hw_breakpoint: Add arch_reinstall_hw_breakpoint Jinchao Wang
2026-07-17 13:03 ` [RFC PATCH v2 04/13] HWBP: Add modify_wide_hw_breakpoint_local() API Jinchao Wang
2026-07-17 13:21   ` sashiko-bot
2026-07-17 13:04 ` [RFC PATCH v2 05/13] mm/kwatch: add watch expression parser and dereference engine Jinchao Wang
2026-07-17 13:15   ` sashiko-bot
2026-07-17 13:04 ` [RFC PATCH v2 06/13] mm/kwatch: add lockless per-task context pool Jinchao Wang
2026-07-17 13:19   ` sashiko-bot
2026-07-17 13:04 ` [RFC PATCH v2 07/13] stacktrace: export stack_trace_save_regs() Jinchao Wang
2026-07-17 13:05 ` [RFC PATCH v2 08/13] mm/kwatch: add hardware breakpoint backend Jinchao Wang
2026-07-17 13:24   ` sashiko-bot
2026-07-17 13:05 ` [RFC PATCH v2 09/13] mm/kwatch: add probe lifecycle runtime Jinchao Wang
2026-07-17 13:20   ` sashiko-bot
2026-07-17 13:06 ` [RFC PATCH v2 10/13] mm/kwatch: add anchor thread for global watchpoints Jinchao Wang
2026-07-17 13:20   ` sashiko-bot
2026-07-17 13:06 ` [RFC PATCH v2 11/13] mm/kwatch: add debugfs control plane Jinchao Wang
2026-07-17 13:27   ` sashiko-bot
2026-07-17 13:07 ` [RFC PATCH v2 12/13] mm/kwatch: add KUnit tests for the watch expression parser Jinchao Wang
2026-07-17 13:07 ` [RFC PATCH v2 13/13] Documentation/dev-tools: document KWatch Jinchao Wang
2026-07-17 13:30   ` sashiko-bot [this message]
2026-07-17 13:41 ` [RFC PATCH v2 00/13] mm/kwatch: dynamic hardware watchpoints for hunting memory corruption Dave Hansen

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=20260717133016.506F81F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=wangjinchao600@gmail.com \
    /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