Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: weir@nebusec.ai
Cc: netdev@vger.kernel.org, edumazet@kernel.org, davem@davemloft.net,
	kuba@kernel.org, pabeni@redhat.com, willemb@google.com,
	horms@kernel.org, gnault@redhat.com, kuniyu@google.com,
	michael.bommarito@gmail.com, alice.kernel@fastmail.im,
	nicoyip.dev@gmail.com, hengyul@cs.unc.edu, jchapman@katalix.com,
	vega@nebusec.ai, caoruide123@gmail.com
Subject: Re: [PATCH net v2] l2tp: bound the reorder queue
Date: Tue, 24 Nov 2026 16:30:44 +0000	[thread overview]
Message-ID: <179553784453.3135.9272758416307793942@kernel.org> (raw)
In-Reply-To: <ccbcb9ac392bc81663a7a14691058b3f03eac3c0.1795437306.git.caoruide123@gmail.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 2 · Low: 3

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] With reordering enabled (reorder_timeout != 0), the new fixed
  cap of 64 combined with timeout-only recovery turns a single lost…
- [Medium] The two new enqueue helpers enforce the same reorder-queue cap
  but update the userspace-visible session counters differently.
- [Medium] The new reorder_timer adds a second dequeuer that runs in
  timer softirq.
- [Low] The comment above l2tp_recv_data_seq() says it will "queue it if
  acceptable.
- [Low] The deadline and the expiry check disagree by one tick.
- [Low] l2tp_recv_dequeue() now also runs from timer context and writes
  session->nr and session->reorder_skip under reorder_q.lock.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but skbs parked in
  session->reorder_q keep the non-refcounted (noref) dst they were…

--

l2tp: bound the reorder queue

This patch caps the L2TP session reorder queue at 64 packets, rejects
duplicate sequence numbers, and inserts sequenced packets by walking from
the tail with a modular sequence comparison. It adds a per-session
reorder_timer that runs l2tp_recv_dequeue() at the head expiry, and it
moves the session->nr advance under the reorder queue lock. Session
teardown shuts down the timer, and l2tp_session_free() purges the queue a
second time.

> diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c
> index 0cd3e38bf662f..42900c3cff632 100644
> --- a/net/l2tp/l2tp_core.c
> +++ b/net/l2tp/l2tp_core.c

[ ... ]

> @@ -636,30 +641,104 @@ EXPORT_SYMBOL_GPL(l2tp_session_register);

[ ... ]

>  static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *skb)
>  {
>  	struct sk_buff *skbp;
> -	struct sk_buff *tmp;
>  	u32 ns = L2TP_SKB_CB(skb)->ns;
> +	bool saw_seq = false;
>  
>  	spin_lock_bh(&session->reorder_q.lock);
> -	skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
> -		if (L2TP_SKB_CB(skbp)->ns > ns) {
> -			__skb_queue_before(&session->reorder_q, skbp, skb);
> -			atomic_long_inc(&session->stats.rx_oos_packets);
> +	if (test_bit(0, &session->dead)) {
> +		kfree_skb(skb);
> +		goto out;
> +	}
> +	if (skb_queue_len(&session->reorder_q) >= L2TP_REORDER_MAX_QUEUE &&
> +	    ns != session->nr) {
> +		atomic_long_inc(&session->stats.rx_seq_discards);
> +		atomic_long_inc(&session->stats.rx_errors);
> +		kfree_skb(skb);
> +		goto out;
> +	}

[Severity: High]
With reordering enabled, can one lost packet cause a stall that repeats
every reorder_timeout?

Suppose session->nr == g and packet g is lost. Packets g+1..g+64 fill
reorder_q, and the head blocks because cb->ns != session->nr. This cap
check then frees every later arrival from g+65 on. The expected-Ns
exception never fires, because the L2TP data channel does not retransmit
g.

When the head expires, l2tp_recv_dequeue() drops it, sets reorder_skip,
resyncs nr to the next queued Ns and drains the queue. That leaves
session->nr == g+65:

l2tp_recv_dequeue() {
    ...
		if (time_after(jiffies, cb->expires)) {
			...
			session->reorder_skip = 1;
			__skb_unlink(skb, &session->reorder_q);
    ...
}

This check already dropped g+65. The next arrival is inside the nr_max/2
receive window, so it gets queued and blocks again.

The nr_oos recovery exists only in the reorder_timeout == 0 branch of
l2tp_recv_data_seq(). The reorder branch comes straight here:

	if (session->reorder_timeout != 0) {
		...
		l2tp_recv_queue_skb(session, skb);
		goto out;
	}

So the queue refills to 64, further packets are dropped, and the cycle
repeats. The session would deliver about 63 packets per reorder_timeout
for as long as traffic stays above about 64 packets per reorder_timeout.
The only exception is when all 64 queued packets expire in the same tick.
Then reorder_skip stays set and the next packet resyncs nr.

Before this patch, the whole backlog was queued and drained after one
timeout. nr then matched the next arrival and the session recovered.

The commit message says:

    The existing timeout recovery then skips a missing sequence number when
    the queue limit is reached.

Is that accurate? Nothing seems to skip when the limit is reached. Only
head expiry does, and it reopens a gap every cycle.

Would a full queue need to force a skip or a dequeue here?

[ ... ]

> +static bool l2tp_recv_queue_tail_skb(struct l2tp_session *session,
> +				     struct sk_buff *skb)
> +{

[ ... ]

> +	spin_lock_bh(&session->reorder_q.lock);
> +	if (test_bit(0, &session->dead))
> +		goto reject;

[ ... ]

> +	qlen = skb_queue_len(&session->reorder_q);
> +	if (qlen >= L2TP_REORDER_MAX_QUEUE) {
> +		if (qlen > L2TP_REORDER_MAX_QUEUE)
> +			goto reject;
> +		if (!(cb->has_seq &&
> +		      (cb->ns == session->nr || session->reorder_skip)))
> +			goto reject;

[ ... ]

> +	__skb_queue_tail(&session->reorder_q, skb);
> +	spin_unlock_bh(&session->reorder_q.lock);
> +	return true;
> +
> +reject:
> +	spin_unlock_bh(&session->reorder_q.lock);
> +	return false;
> +}

[Severity: Medium]
Should the two enqueue helpers count drops the same way?

In l2tp_recv_queue_skb(), a cap drop or a duplicate drop increments both
rx_seq_discards and rx_errors. The session->dead drop increments neither.

In l2tp_recv_queue_tail_skb(), every rejection returns false: dead,
qlen above the cap, not expected/recovery at the cap, or head not
dequeuable. The callers end up at the discard label in
l2tp_recv_common(), which only increments rx_errors:

discard:
	atomic_long_inc(&session->stats.rx_errors);
	kfree_skb(skb);

So a cap drop is a sequence discard in reorder mode but not in
non-reorder mode. A dead-session drop is an rx error on one path and is
not counted at all on the other.

These counters are exported via netlink (L2TP_ATTR_RX_SEQ_DISCARDS and
L2TP_ATTR_RX_ERRORS), the pppol2tp stats ioctl and debugfs.

[ ... ]

> @@ -729,6 +803,16 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
>  				goto out;
>  		}
>  		__skb_unlink(skb, &session->reorder_q);
> +		dequeued = true;
> +
> +		/* Advance Nr under the queue lock so the enqueue cap
> +		 * checks observe a consistent expected sequence number.
> +		 */
> +		if (cb->has_seq) {
> +			session->nr++;
> +			session->nr &= session->nr_max;
> +			trace_session_seqnum_update(session);
> +		}
>  
>  		/* Process the skb. We release the queue lock while we
>  		 * do so to let other contexts process the queue.

[Severity: Medium]
Can the new reorder_timer deliver packets to the pseudowire out of order?

The timer is not pinned. l2tp_recv_dequeue_timer() can run on one CPU
while the receive path runs l2tp_recv_dequeue() on another. Both advance
nr under the lock, then drop the lock before calling recv_skb:

CPU A (timer)
  drops expired head, resyncs to N+2
  unlinks N+2, nr = N+3
  spin_unlock_bh()

CPU B (rx softirq)
  spin_lock_bh()
  head N+3 == nr, unlinks it, nr = N+4
  spin_unlock_bh()
  recv_skb(N+3)

CPU A
  recv_skb(N+2)

Nothing orders recv_skb(N+2) on CPU A before recv_skb(N+3) on CPU B. So
ppp_input(), sock_queue_rcv_skb() or l2tp_eth could get them in reverse
order.

The changelog says concurrent timer and receive dequeue is left
unchanged. Before this patch, though, dequeue only ran from the session's
receive path. Is this second, concurrent dequeuer intended?

> @@ -739,9 +823,22 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
>  	}
>  
>  out:
> +	if (skb_queue_empty(&session->reorder_q))
> +		timer_delete(&session->reorder_timer);
> +	else if (dequeued || !timer_pending(&session->reorder_timer))
> +		timer_reduce(&session->reorder_timer,
> +			     L2TP_SKB_CB(skb_peek(&session->reorder_q))->expires);

[Severity: Low]
Do the timer deadline and the expiry check disagree by one tick?

The timer is armed for exactly the head's expires value, but the walk
uses a strict comparison:

		if (time_after(jiffies, cb->expires)) {

When the timer fires at jiffies == expires, the head is not treated as
expired. A blocked sequenced head takes goto out. dequeued is false, and
the timer is not pending because its callback is running. timer_reduce()
then re-arms it for the same deadline, which has already passed, so it
fires again on the next tick.

That costs an extra callback and a locked queue walk on each expiry.
Would arming at expires + 1, or using time_after_eq() in the check, make
the two agree?

>  	spin_unlock_bh(&session->reorder_q.lock);
>  }
>  
> +static void l2tp_recv_dequeue_timer(struct timer_list *timer)
> +{
> +	struct l2tp_session *session = timer_container_of(session, timer,
> +							 reorder_timer);
> +
> +	l2tp_recv_dequeue(session);
> +}

[Severity: High]
This is a pre-existing issue and was not introduced by this patch.

skbs parked in session->reorder_q keep the noref dst they were received
with. They are delivered after the RCU section that kept that dst alive
has ended.

For connected UDP tunnel sockets, udp_v4_early_demux() attaches
sk->sk_rx_dst with skb_dst_set_noref(), and ip_route_input_noref()
attaches cached input routes as noref. udp_queue_rcv_one_skb() then calls
encap_rcv() without forcing or dropping the dst. Neither
l2tp_udp_recv_core() nor l2tp_recv_common() drops it before queueing.

Once the softirq ends, the route can be replaced and freed after a grace
period, for example an rt_genid bump followed by udp_sk_rx_dst_set()
releasing the old dst. Delivery happens later, either from another
packet's receive or, with this patch, from l2tp_recv_dequeue_timer().

For a PPPoL2TP socket that is connected but not PPPOX_BOUND:

l2tp_recv_dequeue_timer()
  l2tp_recv_dequeue()
    l2tp_recv_dequeue_skb()
      pppol2tp_recv()
        sock_queue_rcv_skb()
          __sock_queue_rcv_skb()
            skb_dst_force()
              dst_hold_safe() on the stale dst

Can that rcuref_get() run on a freed dst_entry?

The rcu_read_lock() in pppol2tp_recv() hides the WARN_ON in
skb_dst_force(). It does not protect a dst that came from an earlier RCU
section. l2tp_eth calls skb_dst_drop() first, and the ppp_input() path
only drops the dst without dereferencing it.

The v2 changelog says the timer path "does not dereference dst on its
own". That is true for l2tp_core, but the recv_skb callback it calls
does dereference it.

Would calling skb_dst_drop() before queueing the skb in
l2tp_recv_common() fix this? Calling skb_dst_force() while still inside
the original RCU section would also work.

[ ... ]

> @@ -783,7 +880,8 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
>  	 * sequence number to re-enable packet reception.
>  	 */
>  	if (cb->ns == session->nr) {
> -		skb_queue_tail(&session->reorder_q, skb);
> +		if (!l2tp_recv_queue_tail_skb(session, skb))
> +			goto discard;

[Severity: Low]
l2tp_recv_dequeue() now also runs from timer context and writes
session->nr and session->reorder_skip under reorder_q.lock. Is this
unlocked read of session->nr a data race?

l2tp_recv_data_seq() still uses plain, unlocked accesses on the same
state. It reads session->nr in l2tp_seq_check_rx_window() and in the
comparison above. Further down it also accesses reorder_skip, nr_oos and
nr_oos_count, including this write:

		if (session->nr_oos_count > session->nr_oos_count_max) {
			session->reorder_skip = 1;
		}

On the timer side:

			if (session->reorder_skip) {
				session->reorder_skip = 0;
				session->nr = cb->ns;

KCSAN would likely report this once the timer runs on another CPU.

l2tp_recv_queue_tail_skb() also does not re-check Ns against nr when qlen
is below the cap. In non-reorder mode, a packet accepted on a stale nr
could be appended after the timer has resynced nr. It would then block
the head until reorder_skip is set again or the head expires.

The commit message says nr is advanced under the lock "so the cap checks
observe a consistent expected sequence number". Does that also hold for
this enqueue decision, which is made outside the lock?

>  	} else {

[ ... ]

> @@ -802,7 +900,8 @@ static int l2tp_recv_data_seq(struct l2tp_session *session, struct sk_buff *skb)
>  			trace_session_pkt_oos(session, cb->ns);
>  			goto discard;
>  		}
> -		skb_queue_tail(&session->reorder_q, skb);
> +		if (!l2tp_recv_queue_tail_skb(session, skb))
> +			goto discard;
>  	}

[Severity: Low]
This isn't a bug, but the comment above l2tp_recv_data_seq() no longer
matches what it does:

/* If packet has sequence numbers, queue it if acceptable. Returns 0 if
 * acceptable, else non-zero.
 */

The reorder branch calls l2tp_recv_queue_skb(), which can now reject and
free the skb itself: when session->dead is set, when the cap is reached
with ns != nr, or for a duplicate Ns. l2tp_recv_data_seq() still does
goto out and returns 0 in those cases. This branch, in contrast, returns
1 when l2tp_recv_queue_tail_skb() rejects.

The v2 changelog says the comment was left unchanged because a rejected
skb stays with the caller. That reasoning only covers the return-1 path.

Could the comment be updated to describe who owns the skb on each return?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/ccbcb9ac392bc81663a7a14691058b3f03eac3c0.1795437306.git.caoruide123%40gmail.com

  reply	other threads:[~2026-11-24 16:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 16:20 [PATCH net v2] l2tp: bound the reorder queue Ren Wei
2026-11-24 16:30 ` netdev-bot+sashiko [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-11-23 16:22 netdev-bot+sinfo

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=179553784453.3135.9272758416307793942@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=alice.kernel@fastmail.im \
    --cc=caoruide123@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=gnault@redhat.com \
    --cc=hengyul@cs.unc.edu \
    --cc=horms@kernel.org \
    --cc=jchapman@katalix.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=michael.bommarito@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=nicoyip.dev@gmail.com \
    --cc=pabeni@redhat.com \
    --cc=vega@nebusec.ai \
    --cc=weir@nebusec.ai \
    --cc=willemb@google.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