🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
5 messages
5 total messages Started by Zhiqi Zhang Sun, 01 Nov 2026 08:17
[PATCH v2 0/2] lib/ts_fsm: preserve patterns across tc dump and restore
#9245
Author: Zhiqi Zhang
Date: Sun, 01 Nov 2026 08:17
65 lines
14221 bytes
Dumping and restoring a tc text ematch configured with the FSM algorithm
can change its packet matching behaviour or fail with EINVAL. This series
preserves the public token types returned by textsearch_get_pattern() and
adds regression coverage for exporting and recreating an FSM configuration.

Changes since v1:
- Describe the tc userspace impact in the fix's commit message.
- Split the production fix and KUnit tests; mark the fix for stable.
- Add KUNIT_EXPECT_MEMEQ for the exported representation, as Andrew suggested.
- Add tc maintainers and netdev to the recipients.
- Add actual loopback packet classification and performance measurements.
The production code change is identical to v1.

Validation on base 2a500a04d6f969318c05f46aedb6e73fc1963a00:
- The same updated KUnit test and ARM64 GCC configuration fail 13 of 15
  tests before the fix and pass all 15 after it. ARM32 GCC and ARM64 Clang
  also pass 15/15. Compilers: GCC 13.3 and Clang 18.1.3; QEMU 8.2.2.
- In an isolated ARM64 QEMU guest, a tc basic filter matches the four-byte
  UDP payload "7777" and rejects "AAAA". Recreating its DIGIT/MULTI text
  ematch from the rtnetlink dump makes both payloads match before the fix;
  after the fix the restored rule still rejects "AAAA". Every probe is
  observed in the classifier's evaluated/matched counters. Restoring a
  PRINT/MULTI rule returns EINVAL before the fix and succeeds after it.
- The guests have no external NIC. These tests use direct rtnetlink,
  rather than claiming coverage of a particular tc save/restore utility.
- W=1 reports no warnings for changed objects. Sparse has no diagnostics
  for them; an unchanged ARM64 vDSO object has a declaration diagnostic.

A native ARM64 userspace microbenchmark of the mechanically extracted FSM
matcher (GCC 13.3, 64 paired samples per case) measured median paired
slowdowns of about 1-5% for the tested long character-class scans and
about 13% for a one-byte rejection case with appreciable variability.
These results do not establish in-kernel or network throughput. The extra
lookup trades matching cost for avoiding a second stored pattern copy.

Mainline cbbddb6b08cc9900a87cccaa9b22bbe06aa4f50a, linux-next and
mm-nonmm-unstable still have the affected code. The series applies to all
three checked heads; they were not built or booted for this revision.
No x86, big-endian, sanitizer, module-load or stable-branch runtime testing
was performed. The packet test measures matching, not dropping actions or
network throughput.

I identified the issue and reviewed, built and tested the v1 fix. OpenAI
Codex drafted the fix, tests and changelogs and drove the additional v2
validation. The added assertion and revised messages are AI-assisted.

v1: https://lore.kernel.org/all/20261031052307.23533-1-zhiqizhangg@gmail.com/
Review: https://lore.kernel.org/all/20261031202211.22f18bca7819dbe2d107d17d@linux-foundation.org/

Zhiqi Zhang (2):
  lib/ts_fsm: preserve token types when exporting patterns
  lib/ts_fsm: add KUnit coverage for exported patterns

 lib/Kconfig.debug                |  14 ++++
 lib/tests/Makefile               |   1 +
 lib/tests/textsearch_fsm_kunit.c | 138 +++++++++++++++++++++++++++++++
 lib/ts_fsm.c                     |   7 +-
 4 files changed, 154 insertions(+), 6 deletions(-)
 create mode 100644 lib/tests/textsearch_fsm_kunit.c


base-commit: 2a500a04d6f969318c05f46aedb6e73fc1963a00
-- 
2.54.0 (Apple Git-157)
[PATCH v2 1/2] lib/ts_fsm: preserve token types when exporting patterns
#9246
Author: Zhiqi Zhang
Date: Sun, 01 Nov 2026 08:17
71 lines
13996 bytes
Dumping and restoring a tc filter with an FSM text ematch can silently
change which packets it matches, or fail to restore it with EINVAL. This
happens when userspace reuses the pattern returned by RTM_GETTFILTER to
configure another filter. Dumping alone does not change the live filter.

fsm_init() replaces each public token type with an internal character-class
mask, and fsm_get_pattern() returns that converted array. For example,
TS_FSM_DIGIT (2) becomes the _D mask (4). Restoring the exported pattern
interprets 4 as TS_FSM_PRINT. With a DIGIT/MULTI pattern, a rule that
matched the UDP payload "7777" but rejected "AAAA" starts matching both
after restoration. Other exported masks exceed TS_FSM_TYPE_MAX and are
rejected with EINVAL.

Keep the public token types in the configuration and look up their masks
in match_token(). This preserves the exported representation without
adding another pattern allocation. Initialization still validates every
type before it can be used to index token_map. The extra lookup during
character-class matching avoids storing a second copy of the pattern.

I identified this issue. OpenAI Codex helped confirm it and drafted the
fix and changelog. I reviewed the fix, built it and verified that the
tests passed.

Fixes: 6408f79cce40 ("[LIB]: Naive finite state machine based textsearch")
Cc: stable@vger.kernel.org
Signed-off-by: Zhiqi Zhang <zhiqizhangg@gmail.com>
---
Changes in v2: describe the tc impact, split out the KUnit tests and add
stable. The production code change is unchanged from v1.

Validation: updated KUnit tests change from 2/15 to 15/15 passing on ARM64
GCC; ARM32 GCC and ARM64 Clang also pass. An isolated QEMU rtnetlink/UDP
test demonstrates altered packet matching and EINVAL restoration before
the fix, with both corrected afterwards. See the cover letter for details.

An ARM64 userspace microbenchmark of the extracted matcher measured median
paired slowdowns of about 1-5% for tested long character-class scans and
13% for a one-byte rejection case with appreciable variability. This is
not a measurement of in-kernel or packet throughput.

 lib/ts_fsm.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/lib/ts_fsm.c b/lib/ts_fsm.c
index 053615f4f..8725ea6f0 100644
--- a/lib/ts_fsm.c
+++ b/lib/ts_fsm.c
@@ -126,7 +126,7 @@ _W|_L,         _W|_L,        _W|_L,        _W|_L};		/* 252-255 */
 static inline int match_token(struct ts_fsm_token *t, u8 d)
 {
 	if (t->type)
-		return (token_lookup_tbl[d] & t->type) != 0;
+		return (token_lookup_tbl[d] & token_map[t->type]) != 0;
 	else
 		return t->value == d;
 }
@@ -288,11 +288,6 @@ static struct ts_config *fsm_init(const void *pattern, unsigned int len,
 	fsm->ntokens = ntokens;
 	memcpy(fsm->tokens, pattern, len);
 
-	for (i = 0; i < fsm->ntokens; i++) {
-		struct ts_fsm_token *t = &fsm->tokens[i];
-		t->type = token_map[t->type];
-	}
-
 	return conf;
 
 errout:
-- 
2.54.0 (Apple Git-157)
[PATCH v2 2/2] lib/ts_fsm: add KUnit coverage for exported patterns
#9247
Author: Zhiqi Zhang
Date: Sun, 01 Nov 2026 08:17
204 lines
17972 bytes
Add regression coverage for exporting an FSM pattern and preparing another
configuration from it. Check all 14 public token types with positive and
negative matching examples, and a sequence containing a repeated class.

Compare the exported bytes with the input pattern as well as checking
matching behaviour. Verify that the original configuration remains usable
after exporting and recreating the pattern. Register cleanup actions so
that early assertion failures still destroy the configurations.

OpenAI Codex drafted these tests and the changelog and drove the recorded
automated validation.

Signed-off-by: Zhiqi Zhang <zhiqizhangg@gmail.com>
---
 lib/Kconfig.debug                |  14 ++++
 lib/tests/Makefile               |   1 +
 lib/tests/textsearch_fsm_kunit.c | 138 +++++++++++++++++++++++++++++++
 3 files changed, 153 insertions(+)
 create mode 100644 lib/tests/textsearch_fsm_kunit.c

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 2d22638ec..416d033b0 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2531,6 +2531,20 @@ config STRING_HELPERS_KUNIT_TEST
 	depends on KUNIT
 	default KUNIT_ALL_TESTS
 
+config TEXTSEARCH_FSM_KUNIT_TEST
+	tristate "KUnit tests for FSM text search" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	select TEXTSEARCH
+	select TEXTSEARCH_FSM
+	default KUNIT_ALL_TESTS
+	help
+	  Test that exporting and recreating an FSM text search configuration
+	  preserves its matching behaviour. Exercise each public token type
+	  and a sequence of tokens with a repeated character class. Check
+	  that exporting also leaves the original configuration usable.
+
+	  If unsure, say N.
+
 config STRING_KUNIT_BENCH
        bool "Benchmark string functions at runtime"
        depends on STRING_KUNIT_TEST
diff --git a/lib/tests/Makefile b/lib/tests/Makefile
index df0537e1b..fa47ef5d3 100644
--- a/lib/tests/Makefile
+++ b/lib/tests/Makefile
@@ -54,6 +54,7 @@ CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable)
 obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
 obj-$(CONFIG_STRING_KUNIT_TEST) += string_kunit.o
 obj-$(CONFIG_STRING_HELPERS_KUNIT_TEST) += string_helpers_kunit.o
+obj-$(CONFIG_TEXTSEARCH_FSM_KUNIT_TEST) += textsearch_fsm_kunit.o
 obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o
 obj-$(CONFIG_UTIL_MACROS_KUNIT) += util_macros_kunit.o
 obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o
diff --git a/lib/tests/textsearch_fsm_kunit.c b/lib/tests/textsearch_fsm_kunit.c
new file mode 100644
index 000000000..6cb173b97
--- /dev/null
+++ b/lib/tests/textsearch_fsm_kunit.c
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Tests for exporting and recreating FSM text search configurations. */
+
+#include <kunit/test.h>
+#include <linux/limits.h>
+#include <linux/module.h>
+#include <linux/string.h>
+#include <linux/textsearch.h>
+#include <linux/textsearch_fsm.h>
+
+struct fsm_test_case {
+	const char *name;
+	u16 type;
+	const char *match;
+	const char *miss;
+};
+
+static const struct fsm_test_case fsm_test_cases[] = {
+	{ "specific", TS_FSM_SPECIFIC, "x", "y" },
+	{ "wildcard", TS_FSM_WILDCARD, "A", "" },
+	{ "digit", TS_FSM_DIGIT, "7", "A" },
+	{ "xdigit", TS_FSM_XDIGIT, "f", "g" },
+	{ "print", TS_FSM_PRINT, "A", "\n" },
+	{ "alpha", TS_FSM_ALPHA, "A", "7" },
+	{ "alnum", TS_FSM_ALNUM, "7", "!" },
+	{ "ascii", TS_FSM_ASCII, "A", "\200" },
+	{ "cntrl", TS_FSM_CNTRL, "\n", "A" },
+	{ "graph", TS_FSM_GRAPH, "!", " " },
+	{ "lower", TS_FSM_LOWER, "a", "A" },
+	{ "upper", TS_FSM_UPPER, "A", "a" },
+	{ "punct", TS_FSM_PUNCT, "!", "A" },
+	{ "space", TS_FSM_SPACE, " ", "A" },
+};
+
+static void fsm_case_to_desc(const struct fsm_test_case *params, char *desc)
+{
+	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s", params->name);
+}
+
+KUNIT_ARRAY_PARAM(fsm, fsm_test_cases, fsm_case_to_desc);
+
+static void fsm_destroy_config(void *conf)
+{
+	textsearch_destroy(conf);
+}
+
+static void fsm_expect_matches(struct kunit *test, struct ts_config *conf,
+			       const struct fsm_test_case *params)
+{
+	struct ts_state state = {};
+	unsigned int pos;
+
+	pos = textsearch_find_continuous(conf, &state, params->match,
+					 strlen(params->match));
+	KUNIT_EXPECT_EQ(test, pos, 0U);
+	pos = textsearch_find_continuous(conf, &state, params->miss,
+					 strlen(params->miss));
+	KUNIT_EXPECT_EQ(test, pos, UINT_MAX);
+}
+
+static void fsm_check_roundtrip(struct kunit *test,
+				const struct ts_fsm_token *pattern,
+				unsigned int len,
+				const struct fsm_test_case *params)
+{
+	struct ts_fsm_token *exported;
+	struct ts_config *conf, *recreated;
+	int err;
+
+	conf = textsearch_prepare("fsm", pattern, len, GFP_KERNEL, TS_AUTOLOAD);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, conf);
+	err = kunit_add_action_or_reset(test, fsm_destroy_config, conf);
+	KUNIT_ASSERT_EQ(test, err, 0);
+	fsm_expect_matches(test, conf, params);
+
+	KUNIT_ASSERT_EQ(test, textsearch_get_pattern_len(conf), len);
+	exported = kunit_kmalloc(test, len, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, exported);
+	memcpy(exported, textsearch_get_pattern(conf), len);
+	KUNIT_EXPECT_MEMEQ(test, exported, pattern, len);
+
+	/* Exporting a pattern must preserve the existing search behaviour. */
+	fsm_expect_matches(test, conf, params);
+
+	recreated = textsearch_prepare("fsm", exported, len, GFP_KERNEL,
+				       TS_AUTOLOAD);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, recreated,
+					 "exported token: type=%u recur=%u value=%u",
+					 exported->type, exported->recur,
+					 exported->value);
+	err = kunit_add_action_or_reset(test, fsm_destroy_config, recreated);
+	KUNIT_ASSERT_EQ(test, err, 0);
+	fsm_expect_matches(test, recreated, params);
+	fsm_expect_matches(test, conf, params);
+}
+
+static void fsm_test_pattern_roundtrip(struct kunit *test)
+{
+	const struct fsm_test_case *params = test->param_value;
+	struct ts_fsm_token pattern = {
+		.type = params->type,
+		.recur = TS_FSM_SINGLE,
+		.value = 'x',
+	};
+
+	fsm_check_roundtrip(test, &pattern, sizeof(pattern), params);
+}
+
+static void fsm_test_sequence_roundtrip(struct kunit *test)
+{
+	static const struct ts_fsm_token pattern[] = {
+		{ TS_FSM_DIGIT, TS_FSM_MULTI, 0 },
+		{ TS_FSM_SPECIFIC, TS_FSM_SINGLE, ':' },
+		{ TS_FSM_UPPER, TS_FSM_SINGLE, 0 },
+	};
+	static const struct fsm_test_case params = {
+		.match = "77:A",
+		.miss = "77:a",
+	};
+
+	fsm_check_roundtrip(test, pattern, sizeof(pattern), &params);
+}
+
+static struct kunit_case fsm_kunit_test_cases[] = {
+	KUNIT_CASE_PARAM(fsm_test_pattern_roundtrip, fsm_gen_params),
+	KUNIT_CASE(fsm_test_sequence_roundtrip),
+	{}
+};
+
+static struct kunit_suite fsm_test_suite = {
+	.name = "textsearch-fsm",
+	.test_cases = fsm_kunit_test_cases,
+};
+
+kunit_test_suite(fsm_test_suite);
+
+MODULE_DESCRIPTION("FSM text search configuration tests");
+MODULE_LICENSE("GPL");
-- 
2.54.0 (Apple Git-157)
Re: [PATCH v2 0/2] lib/ts_fsm: preserve patterns across tc dump and restore
#13119
Author: Zhiqi Zhang
Date: Mon, 09 Nov 2026 10:34
24 lines
11756 bytes
Hi Andrew and all,

A gentle ping on this series, sent on November 1:

https://lore.kernel.org/all/20261101081736.51803-1-zhiqizhangg@gmail.com/

v2 describes the tc dump/restore impact, adds the exported-pattern
equality assertion, and splits the fix from the KUnit tests, with
Cc: stable on the fix.

Could you please advise whether this series should go through Andrew's
tree, or whether another tree should handle it?

The netdev CI also flagged the new lib/tests/textsearch_fsm_kunit.c file
as lacking specific MAINTAINERS coverage. How should that file be
covered, in light of the dedicated textsearch entry discussed in the
v1 review? I would appreciate guidance on the intended maintainer and
coverage before preparing an update.

Please let me know if any further changes are needed.

Thanks,
Zhiqi
Re: [PATCH v2 0/2] lib/ts_fsm: preserve patterns across tc dump and restore
#22248
Author: Zhiqi Zhang
Date: Wed, 25 Nov 2026 02:18
22 lines
11939 bytes
Hi Andrew and all,

A follow-up ping on this two-patch series, sent on November 1:

https://lore.kernel.org/all/20261101081736.51803-1-zhiqizhangg@gmail.com/

It fixes a userspace-visible issue where restoring a tc FSM text ematch
from its dump can change which packets the filter matches, or fail with
EINVAL.

v2 describes that impact, splits the fix from the KUnit tests, adds the
exported-pattern equality assertion, and includes Cc: stable on the fix.

Could you please advise which tree should carry this series, and whether
any further changes are needed before it can be picked up? I would also
appreciate guidance on MAINTAINERS coverage for the new
lib/tests/textsearch_fsm_kunit.c file. I can prepare a follow-up revision
once the intended coverage is clear.

Thanks for your time,
Zhiqi
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads