[PATCH net-next v3 1/3] amt: make the A-B source walk linear
From: Omar Ramadan
Date: Sun Nov 22 2026 - 18:57:43 EST
amt_lookup_act_srcs() computes A-B, the sources of the group's state that
a received record does not name, by comparing every source in the group
with every source in the record. That runs under tunnel->lock in
softirq, and only the packet length bounds the record, so a single
Membership Update can already cost |A| * |B| address compares at the
current 128-source default.
Mark the group's sources that the record names with one hash lookup per
record source, then act on the unmarked ones in a single pass over the
group, clearing the marks as it goes. The cost becomes O(|A| + |B|) and
the result is unchanged: a marked source is skipped whatever its filter
state, exactly as the pairwise compare skipped it.
Found by review (Sashiko, on the limits patch); not triggered at runtime.
Assisted-by: LLM
Signed-off-by: Omar Ramadan <omar@xxxxxxxxxxxxx>
---
drivers/net/amt.c | 43 ++++++++++++++++++++++---------------------
include/net/amt.h | 2 ++
2 files changed, 24 insertions(+), 21 deletions(-)
diff --git a/drivers/net/amt.c b/drivers/net/amt.c
index b145ed3ca..79d68bcc7 100644
--- a/drivers/net/amt.c
+++ b/drivers/net/amt.c
@@ -1649,7 +1649,7 @@ static void amt_lookup_act_srcs(struct amt_tunnel_list *tunnel,
union amt_addr src = {0,};
struct hlist_node *t;
u16 nsrcs;
- int i, j;
+ int i;
if (!v6) {
igmp_grec = grec;
@@ -1705,28 +1705,29 @@ static void amt_lookup_act_srcs(struct amt_tunnel_list *tunnel,
}
break;
case AMT_OPS_SUB:
- /* A-B */
- for (i = 0; i < amt->hash_buckets; i++) {
- hlist_for_each_entry_safe(snode, t, &gnode->sources[i],
- node) {
- if (!amt_status_filter(snode, filter))
- continue;
- for (j = 0; j < nsrcs; j++) {
- if (!v6)
- src.ip4 = igmp_grec->grec_src[j];
+ /* A-B: mark every source of A that B names, then act on the
+ * unmarked ones in a single pass over the group, clearing the
+ * marks as we go. This is O(|A| + |B|) rather than |A| * |B|.
+ */
+ for (i = 0; i < nsrcs; i++) {
+ if (!v6)
+ src.ip4 = igmp_grec->grec_src[i];
#if IS_ENABLED(CONFIG_IPV6)
- else
- memcpy(&src.ip6,
- &mld_grec->grec_src[j],
- sizeof(struct in6_addr));
+ else
+ memcpy(&src.ip6, &mld_grec->grec_src[i],
+ sizeof(struct in6_addr));
#endif
- if (amt_addr_equal(&snode->source_addr,
- &src))
- goto out_sub;
- }
- amt_act_src(tunnel, gnode, snode, act);
- continue;
-out_sub:;
+ snode = amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL,
+ &src);
+ if (snode)
+ snode->sub_mark = true;
+ }
+ for (i = 0; i < amt->hash_buckets; i++) {
+ hlist_for_each_entry(snode, &gnode->sources[i], node) {
+ if (snode->sub_mark)
+ snode->sub_mark = false;
+ else if (amt_status_filter(snode, filter))
+ amt_act_src(tunnel, gnode, snode, act);
}
}
break;
diff --git a/include/net/amt.h b/include/net/amt.h
index 9ba097127..f36efcdd9 100644
--- a/include/net/amt.h
+++ b/include/net/amt.h
@@ -287,6 +287,8 @@ struct amt_source_node {
struct delayed_work source_timer;
union amt_addr source_addr;
enum amt_source_status status;
+ /* Scratch mark for the A-B source walk, under tunnel_list->lock. */
+ bool sub_mark;
#define AMT_SOURCE_OLD 0
#define AMT_SOURCE_NEW 1
u8 flags;
--
2.43.0