Netdev List
 help / color / mirror / Atom feed
From: David Ahern <dsahern@kernel.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, idosch@nvidia.com, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	horms@kernel.org, ralf@mandelbit.com, ja@ssi.bg,
	David Ahern <dahern@nvidia.com>
Subject: [PATCH net-next v2 03/13] ipv6: don't insert a PMTU exception whose MTU could not be stored
Date: Wed, 25 Nov 2026 18:33:29 -0700	[thread overview]
Message-ID: <e158d506fc9aab4b823c28cc1a3bfcaff0e7f67b.1795642432.git.dahern@nvidia.com> (raw)
In-Reply-To: <cover.1795642432.git.dahern@nvidia.com>

From: David Ahern <dahern@nvidia.com>

rt6_do_update_pmtu() sets the MTU of a dst with dst_metric_set(). A dst
shares the read-only metrics of its route until it needs its own, and
dst_metric_set() allocates writable ones with GFP_ATOMIC on first use,
doing nothing if that fails, without telling the caller.

For a PMTU update on a new exception the caller then goes on to insert
an exception that has no learned MTU, which lowers nothing, replaces
any exception for that destination that was there, and invalidates the
cached dsts of the nexthop. For an update on an existing exception the
reduction is lost while the stamp that keeps the exception alive is
refreshed.

Allocate the writable metrics in rt6_do_update_pmtu() and return an
error if that fails. Do not insert the new exception then, and release
it. For an exception that is already in use there is nothing to fall
back on, but nothing is claimed either: the next packet too big that
exceeds its MTU makes the update again.

The handlers that update a dst in place do not take a lock either, and
each one decides to lower the MTU from a value it read before. Storing
it unconditionally lets a handler that decided on 1400 overwrite a
1280 that another one stored in between. Lower the MTU with a compare
and exchange, so that it only ever goes down and a smaller value
stored meanwhile is kept.

Fixes: 45e4fd26683c ("ipv6: Only create RTF_CACHE routes after encountering pmtu exception")
Signed-off-by: David Ahern <dahern@nvidia.com>
---
 net/ipv6/route.c | 52 +++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 47 insertions(+), 5 deletions(-)

diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 3337a8fe2550..52a30a62bcec 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -1546,6 +1546,34 @@ static struct net *rt6_exception_net(const struct rt6_info *rt)
 	return container_of(rt->dst.ops, struct net, ipv6.ip6_dst_ops);
 }
 
+/* Lower the MTU in the metrics of @rt to @mtu.
+ *
+ * A dst shares the read-only metrics of its route until it needs its own.
+ * Allocating them can fail, and dst_metric_set() does not say: the PMTU would
+ * be lost.
+ *
+ * Only ever lower it, and with a compare and exchange: PMTU handlers update a
+ * dst in place without a lock, and each decides to lower the MTU from a value
+ * it read earlier, so storing unconditionally could overwrite a smaller value
+ * that another handler stored in between.
+ */
+static int rt6_lower_mtu(struct rt6_info *rt, u32 mtu)
+{
+	u32 *metrics = dst_metrics_write_ptr(&rt->dst);
+	u32 old;
+
+	if (!metrics)
+		return -ENOMEM;
+
+	old = READ_ONCE(metrics[RTAX_MTU - 1]);
+	do {
+		if (old && old <= mtu)
+			break;
+	} while (!try_cmpxchg(&metrics[RTAX_MTU - 1], &old, mtu));
+
+	return 0;
+}
+
 /* Remove rt6_ex from hash table and free the memory
  * Caller must hold rt6_exception_lock
  */
@@ -2913,13 +2941,23 @@ static void rt6_update_expires(struct rt6_info *rt0, int timeout)
 	rt0->rt6i_flags |= RTF_EXPIRES;
 }
 
-static void rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
+static int rt6_do_update_pmtu(struct rt6_info *rt, u32 mtu)
 {
 	struct net *net = dev_net(rt->dst.dev);
+	int err;
+
+	/* If the MTU cannot be stored the PMTU would be lost and, for a new
+	 * exception, an exception would be inserted that does not lower
+	 * anything.
+	 */
+	err = rt6_lower_mtu(rt, mtu);
+	if (err)
+		return err;
 
-	dst_metric_set(&rt->dst, RTAX_MTU, mtu);
 	rt->rt6i_flags |= RTF_MODIFIED;
 	rt6_update_expires(rt, READ_ONCE(net->ipv6.sysctl.ip6_rt_mtu_expires));
+
+	return 0;
 }
 
 static bool rt6_cache_allowed_for_pmtu(const struct rt6_info *rt)
@@ -2960,7 +2998,11 @@ static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
 		return;
 
 	if (!rt6_cache_allowed_for_pmtu(rt6)) {
-		rt6_do_update_pmtu(rt6, mtu);
+		/* If the update cannot be made, the next packet too big that
+		 * exceeds this dst's MTU triggers it again
+		 */
+		if (rt6_do_update_pmtu(rt6, mtu))
+			return;
 		/* update rt6_ex->stamp for cache */
 		if (rt6->rt6i_flags & RTF_CACHE)
 			rt6_update_exception_stamp_rt(rt6);
@@ -2998,8 +3040,8 @@ static void __ip6_rt_update_pmtu(struct dst_entry *dst, const struct sock *sk,
 
 		nrt6 = ip6_rt_cache_alloc(&res, daddr, saddr);
 		if (nrt6) {
-			rt6_do_update_pmtu(nrt6, mtu);
-			if (rt6_insert_exception(nrt6, &res))
+			if (rt6_do_update_pmtu(nrt6, mtu) ||
+			    rt6_insert_exception(nrt6, &res))
 				dst_release_immediate(&nrt6->dst);
 		}
 out_unlock:
-- 
2.54.0 (Apple Git-157)


  parent reply	other threads:[~2026-11-26  1:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  1:33 [PATCH net-next v2 00/13] ipv6: validate cached dsts against the netns route generation David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 01/13] ipv6: allocate the exception entry before removing the one it replaces David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 02/13] ipv6: keep a superseded pcpu dst working until its holders notice David Ahern
2026-11-26  1:33 ` David Ahern [this message]
2026-11-26  1:33 ` [PATCH net-next v2 04/13] ipv6: invalidate the failing dst in ip6_link_failure() David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 05/13] ipv6: tell route dumps about exception inserts with a table sequence David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 06/13] ipv6: validate dsts against the netns route generation David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 07/13] ipv6: make rt_genid_bump_ipv6() a plain counter increment David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 08/13] nexthop: factor out nh_has_f6i() David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 09/13] nexthop: bump the IPv6 route generation on group changes David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 10/13] ipv6: stamp dsts with the generation sampled before the FIB lookup David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 11/13] selftests: net: pmtu: check that a deleted cached exception stays deleted David Ahern
2026-11-26  1:33 ` [PATCH net-next v2 12/13] selftests: net: fib_tests: Add stale pcpu test David Ahern

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=e158d506fc9aab4b823c28cc1a3bfcaff0e7f67b.1795642432.git.dahern@nvidia.com \
    --to=dsahern@kernel.org \
    --cc=dahern@nvidia.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=ja@ssi.bg \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=ralf@mandelbit.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