From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796304651; b=OemH49Tc8JsKay+WPNFg8f/1Xcs9O05mUsn64vei01LFdIeGII/4glooQBAdY6uwkoYw 1zcoW5IzePDjfcj4923rX6VjAiqyZKz39WfFTtNE5baOPl4Ga8iuO1B6t5D3xgksc71Ro dOC8D3f8PxctfWL9h7TgXjWAR/ZxN6+TZnuCvS5UVv6UoFG+sBepD4NiSQP7A0QzES8xq 5ZtzyVKRdP/wzs6p307VV/JFIC1+dLr5nd9BsLRylf8i7fDJfUzgtvjTGWEVeIyvvN4d3 E6MA6D3ca5SdXnyB7uEHa3EAR1gVg/Sql174JiBlzMW9EOzuDhaTA/mu3kR7R/laYRA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796304651; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=6MRKbkYuasAsk6fI3Oz12E0i8WUoLscolglinPGfrnY=; b=bI8s8lTj/4oUnx/WKUbQfb2PXwEB6ChERTNgptbXxDGQ2GIw+ilIVXzQoXTMWEXMiVAb eewxBhd2BvxFn9IJbabv4oFNshHYlLWGwsKQlE6dZqEpN26bsjIoTMpGq4+MoemrPA9qb t7ljTlmfQQVzL6emEE9r6Eherr1/+suWIgkOThV+H76l9XLBJoLhjq0V7K8Cc8l4laoLd ZSNVvoVSmdjffdom4LVKudDPOxPvyGowu5dJMCTO9gMEnnWcnuCybvK0KipR2kSxsOA2R yVzdI5FRziNOybiBYKa3OHiUfBGQuFkSLNabYtfIqMUC+4ewuP2NrM1HSCBMyv2/L0Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=M8eKypZn; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 991AE606A32C for ; Thu, 3 Dec 2026 13:30:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=M8eKypZn Received: by smtp.kernel.org (Postfix) with ESMTPSA id 630FF1F008A0; Thu, 3 Dec 2026 13:30:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796304651; bh=6MRKbkYuasAsk6fI3Oz12E0i8WUoLscolglinPGfrnY=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=M8eKypZnJ8ro3JGDX3owjSxZbJ+/n97F0gFqkq+dCNexDOS15t5ZAsof9vVJXeJdQ qRtonnmBxIaODKOZxnnlpIyh+fZixszuF4TLKMEOsW3AGW6cZdS3RvBFQLG6q0YW4v aVgXo83B+U/hPaJL2eG88WjFXk97pJheaNhSmdbWdbxKig0eZCFcg/M+xf6tucIJi/ jBBkNwhIg5n44bNJbbD+pxvxEX7FXCVQrXwEcGPybvN5dDWRq/OCkhQ+zbaZ6IbhUt UX6C1bEcP/opdwWxuzXHGGvU3C9k8b86BoJj+pjRhx92NDqgZpnJspOzYSUQiLvp8T zf2n1VuAqIp8w== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] tracing: Make event_enable_count_probe() count atomic Reply-To: sashiko-reviews@lists.linux.dev To: "Jaewon Jung" Cc: linux-trace-kernel@vger.kernel.org In-Reply-To: <20261203131451.2470625-1-jaewon8868@gmail.com> References: <20261203025249.2117813-1-jaewon8868@gmail.com> <20261203131451.2470625-1-jaewon8868@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 13:30:50 +0000 X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] tracing: TOCTOU race in event_enable_count_probe() exceeds configured count - [Medium] tracing: dropped test_bit() fast path bounces flags in update_event_probe() - [Medium] tracing: signedness mismatch on count in event_enable_func() and probe - [Low] tracing: redundant smp_rmb() before atomic bitop in event_enable_count_probe() -- commit 47ef71f07e0f5d4db4d3227448bffe6c9bf7018f Author: Jaewon Jung tracing: Make event_enable_count_probe() count atomic This commit converts struct event_probe_data's count field to atomic_long_t and updates event_enable_count_probe() to claim the event state transition with test_and_clear_bit()/test_and_set_bit() before decrementing count in a cmpxchg loop. > diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c > index 0d4195b43208e..bb1309c9fd00e 100644 > --- a/kernel/trace/trace_events.c > +++ b/kernel/trace/trace_events.c > @@ -4402,17 +4402,19 @@ EXPORT_SYMBOL_GPL(trace_put_event_file); > #ifdef CONFIG_DYNAMIC_FTRACE > struct event_probe_data { > struct trace_event_file *file; > - unsigned long count; > + atomic_long_t count; > int ref; > bool enable; > }; > =20 > -static void update_event_probe(struct event_probe_data *data) > +static bool update_event_probe(struct event_probe_data *data) > { > if (data->enable) > - clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); > + return test_and_clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, > + &data->file->flags); > else > - set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags); > + return !test_and_set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, > + &data->file->flags); > } [Severity: Medium] Does removing the read-only test_bit() check cause unnecessary cacheline bouncing on file->flags? Previously, event_enable_count_probe() returned early via test_bit() when the event was already in the target state. Calling update_event_probe() directly now runs an atomic bitop on every probed function call while count !=3D 0, dirtying the trace_event_file cacheline that trace_trigger_soft_disabled() reads on every event hit across all CPUs. event_enable_probe() also calls update_event_probe() and ignores the return value, so on weakly ordered architectures it now pays for a fully ordered test_and_{clear,set}_bit() instead of an unordered clear_bit()/set_bit(). Keeping a read-only test_bit() pre-check in event_enable_count_probe() and unordered clear_bit()/set_bit() for event_enable_probe() would avoid both regressions. [ ... ] > @@ -4447,18 +4450,28 @@ event_enable_count_probe(unsigned long ip, unsign= ed long parent_ip, > =20 > edata =3D *pdata; > =20 > - if (!edata->count) > + count =3D atomic_long_read(&edata->count); > + if (!count) > return; > =20 > - /* Skip if the event is in a state we want to switch to */ > - if (edata->enable =3D=3D !test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, > - &edata->file->flags)) > + /* Make sure we see count before claiming the state transition */ > + smp_rmb(); [Severity: Low] Is this smp_rmb() needed? update_event_probe() immediately performs test_and_clear_bit() or test_and_set_bit(), which are already fully ordered. This smp_rmb() also has no paired smp_wmb(), so on weakly ordered architectures like arm64 it emits an extra read barrier on every non-zero-count probe call. > =20 > + /* Atomically claim the state transition; skip if already there */ > + if (!update_event_probe(edata)) > return; > =20 > - if (edata->count !=3D -1) > - (edata->count)--; > + if (count !=3D -1) { > + /* Retry on contention so a failed cmpxchg does not lose a decrement */ > + for (;;) { > + long old =3D atomic_long_cmpxchg(&edata->count, count, > + count - 1); > =20 > - update_event_probe(edata); > + if (old =3D=3D count || old <=3D 0) > + break; > + count =3D old; > + } > + } > } [Severity: Medium] Can a finite-count probe still exceed its configured count here? event_enable_count_probe() flips EVENT_FILE_FL_SOFT_DISABLED_BIT via update_event_probe() before decrementing edata->count. If an opposite toggle (such as a paired disable_event probe) runs in between, two CPUs can both observe count =3D=3D 1 and perform a state transition: CPU0 CPU1 count =3D atomic_long_read() /* 1 */ update_event_probe() /* clears bit */ /* disable_event sets bit back */ count =3D atomic_long_read() /* 1 */ update_event_probe() <-- 2nd flip atomic_long_cmpxchg(1 -> 0) atomic_long_cmpxchg(1 -> 0) /* sees old =3D=3D 0, breaks */ The same race occurs if CPU0 pauses between atomic_long_read() and update_event_probe() while CPU1 finishes the transition and decrements count to 0. Because CPU0 breaks out on old <=3D 0 without reverting the bit flip, the target event is left enabled or disabled past its configured limit. [ ... ] > @@ -4655,7 +4670,7 @@ event_enable_func(struct trace_array *tr, struct ft= race_hash *hash, > goto out_put; > =20 > data->enable =3D enable; > - data->count =3D count; > + atomic_long_set(&data->count, count); > data->file =3D file; [Severity: Medium] Can event_enable_func() store a negative count in data->count? event_enable_func() still parses count into an unsigned long with kstrtoul() without rejecting values above LONG_MAX, so inputs in (LONG_MAX, ULONG_MAX - 1] wrap to negative signed values (< -1) in atomic_long_t. In event_enable_count_probe(), those values pass both if (!count) and if (count !=3D -1). For LONG_MIN, count - 1 causes signed integer underflow inside a notrace callback, and for any count < -1, a losing CPU in the cmpxchg loop sees old <=3D 0 and breaks out without retrying, silently dropping the decrement. Rejecting values above LONG_MAX (or parsing with kstrtol()) in event_enable_func() avoids the mismatch and would also allow using atomic_long_dec_if_positive(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203131451.2470= 625-1-jaewon8868@gmail.com?part=3D1