Linux X.25 subsystem development
 help / color / mirror / Atom feed
* [PATCH net] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()
@ 2026-10-01 15:01 Nguyen Ngoc Thang
  2026-10-05 15:20 ` netdev-bot+sashiko
  2026-11-23  7:57 ` Martin Schiller
  0 siblings, 2 replies; 11+ messages in thread
From: Nguyen Ngoc Thang @ 2026-10-01 15:01 UTC (permalink / raw)
  To: Martin Schiller, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Duoming Zhou, Lin Ma, linux-x25, netdev,
	linux-kernel, syzbot+9faa82ae2e94c5c7d2ef, Nguyen Ngoc Thang

x25_kill_by_neigh() is reached from the receive path when the link layer
goes away or the peer restarts:

  net_rx_action()
    lapbeth_napi_poll()
      x25_lapb_receive_frame()
        x25_link_terminated() / x25_link_control()
          x25_kill_by_neigh()
            lock_sock()

lock_sock() may sleep, which is not allowed in softirq context:

  BUG: sleeping function called from invalid context at net/core/sock.c:3832
  in_atomic(): 0, irqs_disabled(): 0, non_block: 0, pid: 29, name: ktimers/1
  RCU nest depth: 2, expected: 0
  ...
   lock_sock_nested+0x56/0x130 net/core/sock.c:3832
   x25_kill_by_neigh+0x134/0x2a0 net/x25/af_x25.c:1778
   x25_lapb_receive_frame+0x1b0/0xfb0 net/x25/x25_dev.c:138

Take the socket spinlock instead, the same way the x25 timers and receive
path already do. If the socket is not owned by user, disconnect it right
away. If it is, mark it with X25_KILL_FLAG and let a new release_cb do
the disconnect when the owner releases the socket. This keeps the
serialization against x25_sendmsg()/x25_recvmsg()/x25_connect() that
commit 7781607938c8 ("net/x25: Fix null-ptr-deref caused by
x25_disconnect") added lock_sock() for. Sockets that already have the
flag set are skipped, so the rescan loop always terminates.

NETDEV_DOWN runs in process context and the device may be freed right
after, so keep waiting for socket owners there as before: a sleeping
sweep makes sure no socket still uses the neighbour on return.

Fixes: 7781607938c8 ("net/x25: Fix null-ptr-deref caused by x25_disconnect")
Reported-by: syzbot+9faa82ae2e94c5c7d2ef@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=9faa82ae2e94c5c7d2ef
Signed-off-by: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>
---
Tested in QEMU with the syzbot config (PREEMPT_RT, KASAN, lockdep) and
the syzbot C reproducer: the unpatched kernel hits the BUG twice in 240s,
the patched kernel runs 300s clean. The owned-by-user branch was
exercised with a temporary debug hack forcing the deferred path:
release_cb disconnected the socket and userspace saw ENETUNREACH.
"ip link set lapb0 down" with a connecting socket also disconnects it
right away. No lockdep splats in any run.

syzbot also has an AI-generated workqueue variant of this fix in
moderation. This version keeps the teardown synchronous for sockets
that are not owned, so a link that comes back quickly cannot have a
stale work item kill freshly connected calls. Owned sockets are cleared
when their owner releases them.

 include/net/x25.h |  1 +
 net/x25/af_x25.c  | 62 +++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/include/net/x25.h b/include/net/x25.h
index 414f3fd99345..ed8ba01fa3cb 100644
--- a/include/net/x25.h
+++ b/include/net/x25.h
@@ -118,6 +118,7 @@ enum {
 #define X25_Q_BIT_FLAG		0
 #define X25_INTERRUPT_FLAG	1
 #define X25_ACCPT_APPRV_FLAG	2
+#define X25_KILL_FLAG		3
 
 /**
  *	struct x25_route - x25 routing entry
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index 033e7d059f58..4f9ca22231ac 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -200,6 +200,32 @@ static void x25_remove_socket(struct sock *sk)
 	write_unlock_bh(&x25_list_lock);
 }
 
+/*
+ * Process context only: wait for owners that x25_kill_by_neigh() deferred,
+ * so no socket still uses nb once the device goes away.
+ */
+static void x25_kill_by_neigh_sync(struct x25_neigh *nb)
+{
+	struct sock *s;
+
+again:
+	write_lock_bh(&x25_list_lock);
+
+	sk_for_each(s, &x25_list) {
+		if (x25_sk(s)->neighbour == nb) {
+			sock_hold(s);
+			write_unlock_bh(&x25_list_lock);
+			lock_sock(s);
+			if (x25_sk(s)->neighbour == nb)
+				x25_disconnect(s, ENETUNREACH, 0, 0);
+			release_sock(s);
+			sock_put(s);
+			goto again;
+		}
+	}
+	write_unlock_bh(&x25_list_lock);
+}
+
 /*
  *	Handle device status changes.
  */
@@ -222,6 +248,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event,
 			nb = x25_get_neigh(dev);
 			if (nb) {
 				x25_link_terminated(nb);
+				x25_kill_by_neigh_sync(nb);
 				x25_neigh_put(nb);
 			}
 			x25_route_device_down(dev);
@@ -495,10 +522,20 @@ static int x25_listen(struct socket *sock, int backlog)
 	return rc;
 }
 
+/* Finish a link teardown that hit while the socket was owned by user. */
+static void x25_release_cb(struct sock *sk)
+{
+	struct x25_sock *x25 = x25_sk(sk);
+
+	if (test_and_clear_bit(X25_KILL_FLAG, &x25->flags) && x25->neighbour)
+		x25_disconnect(sk, ENETUNREACH, 0, 0);
+}
+
 static struct proto x25_proto = {
 	.name	  = "X25",
 	.owner	  = THIS_MODULE,
 	.obj_size = sizeof(struct x25_sock),
+	.release_cb = x25_release_cb,
 };
 
 static struct sock *x25_alloc_socket(struct net *net, int kern)
@@ -1764,6 +1801,23 @@ static struct notifier_block x25_dev_notifier = {
 	.notifier_call = x25_device_event,
 };
 
+/* May run in softirq, so lock_sock() is not an option. */
+static void x25_kill_sock(struct sock *sk, struct x25_neigh *nb)
+{
+	struct x25_sock *x25 = x25_sk(sk);
+
+	local_bh_disable();
+	bh_lock_sock(sk);
+	if (x25->neighbour == nb) {
+		if (sock_owned_by_user(sk))
+			set_bit(X25_KILL_FLAG, &x25->flags);
+		else
+			x25_disconnect(sk, ENETUNREACH, 0, 0);
+	}
+	bh_unlock_sock(sk);
+	local_bh_enable();
+}
+
 void x25_kill_by_neigh(struct x25_neigh *nb)
 {
 	struct sock *s;
@@ -1772,13 +1826,11 @@ void x25_kill_by_neigh(struct x25_neigh *nb)
 	write_lock_bh(&x25_list_lock);
 
 	sk_for_each(s, &x25_list) {
-		if (x25_sk(s)->neighbour == nb) {
+		if (x25_sk(s)->neighbour == nb &&
+		    !test_bit(X25_KILL_FLAG, &x25_sk(s)->flags)) {
 			sock_hold(s);
 			write_unlock_bh(&x25_list_lock);
-			lock_sock(s);
-			if (x25_sk(s)->neighbour == nb)
-				x25_disconnect(s, ENETUNREACH, 0, 0);
-			release_sock(s);
+			x25_kill_sock(s, nb);
 			sock_put(s);
 			goto again;
 		}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH net] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()
  2026-10-01 15:01 [PATCH net] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh() Nguyen Ngoc Thang
@ 2026-10-05 15:20 ` netdev-bot+sashiko
  2026-11-23  7:57 ` Martin Schiller
  1 sibling, 0 replies; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-10-05 15:20 UTC (permalink / raw)
  To: ngocthang2710.1999
  Cc: ms, davem, edumazet, kuba, pabeni, horms, duoming, linma,
	linux-x25, netdev, linux-kernel, syzbot+9faa82ae2e94c5c7d2ef

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

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

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).

- [Medium] The patch adds a deferred-kill state.
- [Low] x25_make_new() copies the parent's whole flags word into the
  child (`x25->flags = ox25->flags;`) and clears only X25_INTERRUPT_FLAG.

Pre-existing issues:
- [High] x25_sendmsg() checks `if (!x25->neighbour)` under lock_sock().
- [Medium] On NETDEV_DOWN, x25_device_event() runs under RTNL and calls
  the new x25_kill_by_neigh_sync().

--

net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()

x25_kill_by_neigh() can be reached from softirq. This patch replaces its
lock_sock() with bh_lock_sock(). Sockets not owned by user are
disconnected right away. Owned sockets are marked with X25_KILL_FLAG, and
a new x25_release_cb() disconnects them on release_sock(). NETDEV_DOWN
also runs a sleeping x25_kill_by_neigh_sync() sweep.

> diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
> index 033e7d059f58c..4f9ca22231ac7 100644
> --- a/net/x25/af_x25.c
> +++ b/net/x25/af_x25.c
> @@ -200,6 +200,32 @@ static void x25_remove_socket(struct sock *sk)
>  	write_unlock_bh(&x25_list_lock);
>  }
>  
> +/*
> + * Process context only: wait for owners that x25_kill_by_neigh() deferred,
> + * so no socket still uses nb once the device goes away.
> + */
> +static void x25_kill_by_neigh_sync(struct x25_neigh *nb)
> +{
> +	struct sock *s;
> +
> +again:
> +	write_lock_bh(&x25_list_lock);
> +
> +	sk_for_each(s, &x25_list) {
> +		if (x25_sk(s)->neighbour == nb) {
> +			sock_hold(s);
> +			write_unlock_bh(&x25_list_lock);
> +			lock_sock(s);

[Severity: Medium]
This isn't a bug introduced by this patch, but can this sweep hold RTNL
indefinitely?

x25_device_event() handles NETDEV_DOWN with RTNL held and calls
x25_kill_by_neigh_sync(). That function sleeps in lock_sock() for each
socket on nb. Meanwhile, x25_sendmsg() holds the socket lock across the
user copy:

net/x25/af_x25.c:x25_sendmsg() {
    ...
	rc = memcpy_from_msg(skb_transport_header(skb), msg, len);
    ...
}

If the user buffer is backed by userfaultfd or FUSE, the user can stall
that copy for as long as they like. A later "ip link set <dev> down"
would then block here with RTNL held.

The old x25_kill_by_neigh() made the same lock_sock() call from the same
notifier. The commit message keeps this on purpose ("keep waiting for
socket owners there as before"), so the new helper carries it forward.

> +			if (x25_sk(s)->neighbour == nb)
> +				x25_disconnect(s, ENETUNREACH, 0, 0);

[ ... ]

> @@ -495,10 +522,20 @@ static int x25_listen(struct socket *sock, int backlog)
>  	return rc;
>  }
>  
> +/* Finish a link teardown that hit while the socket was owned by user. */
> +static void x25_release_cb(struct sock *sk)
> +{
> +	struct x25_sock *x25 = x25_sk(sk);
> +
> +	if (test_and_clear_bit(X25_KILL_FLAG, &x25->flags) && x25->neighbour)
> +		x25_disconnect(sk, ENETUNREACH, 0, 0);

[Severity: Low]
Can X25_KILL_FLAG be inherited by a child socket?

x25_make_new() copies the whole flags word from the listener and clears
only X25_INTERRUPT_FLAG:

net/x25/af_x25.c:x25_make_new() {
    ...
	x25->flags	= ox25->flags;
    ...
	clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
    ...
}

x25_connect() does not reject TCP_LISTEN sockets, and it assigns
x25->neighbour while it owns the socket. A link kill during connect()
can therefore set X25_KILL_FLAG on a socket that was listening.

x25_rx_call_request() clones the listener without taking its socket
lock. A child created in that window would inherit the bit, even though
its own neighbour is healthy.

x25_release_cb() does not check which neighbour caused the kill. The
child's first release_sock() after accept() would then disconnect its
call with ENETUNREACH, and no CLEAR is sent to the peer.

The sequence is contrived. Should x25_make_new() also clear
X25_KILL_FLAG?

> +}
> +

[ ... ]

> @@ -1764,6 +1801,23 @@ static struct notifier_block x25_dev_notifier = {
>  	.notifier_call = x25_device_event,
>  };
>  
> +/* May run in softirq, so lock_sock() is not an option. */
> +static void x25_kill_sock(struct sock *sk, struct x25_neigh *nb)
> +{
> +	struct x25_sock *x25 = x25_sk(sk);
> +
> +	local_bh_disable();
> +	bh_lock_sock(sk);
> +	if (x25->neighbour == nb) {
> +		if (sock_owned_by_user(sk))
> +			set_bit(X25_KILL_FLAG, &x25->flags);

[Severity: Medium]
While the flag is set, x25->lci and x25->neighbour stay unchanged until
x25_release_cb() runs. __x25_find_socket() matches on only those two
fields:

	if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {

Could a new incoming call on the same LCI end up on the stale socket's
backlog?

In X25_LINK_STATE_3, x25_link_control() acknowledges a restart as soon
as x25_kill_by_neigh() returns:

		case X25_LINK_STATE_3:
			/* clear existing virtual calls */
			x25_kill_by_neigh(nb);

			x25_transmit_restart_confirmation(nb);

>From then on the peer treats every LCI as free. Suppose it sends a
CALL_REQUEST on an LCI still held by a flagged, user-owned socket.
x25_receive_data() finds that socket before it checks the frame type:

	if ((sk = x25_find_socket(lci, nb)) != NULL) {
		...
			queued = !sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf));
	...
	if (frametype == X25_CALL_REQUEST)
		return x25_rx_call_request(skb, nb, lci);

The call therefore never reaches x25_rx_call_request().

release_sock() replays the backlog before it calls release_cb, so the
old connection's state machine handles the CALL_REQUEST. In
X25_STATE_1, x25_state1_machine() treats it as a call collision and
clears the peer's new call:

	case X25_CALL_REQUEST:
		/* call collision */
		...
		x25_write_internal(sk, X25_CLEAR_REQUEST);
		x25_disconnect(sk, EISCONN, 0x01, 0x48);

In X25_STATE_3 the frame is dropped as unknown, and x25_release_cb()
then disconnects the socket.

Before 7781607938c8 the disconnect was immediate. The lock_sock()
version waited for the owner. Either way, the old (lci, nb) binding was
gone before the restart was answered.

This window also seems to contradict the note after the commit message
that a link that comes back quickly cannot have stale state kill
freshly connected calls.

Should the lookup skip sockets with X25_KILL_FLAG set? Alternatively,
the lci/neighbour binding could be dropped at kill time.

> +		else
> +			x25_disconnect(sk, ENETUNREACH, 0, 0);

[Severity: High]
This is a pre-existing issue, but the immediate disconnect here still
seems to race with x25_sendmsg().

x25_sendmsg() checks x25->neighbour under lock_sock(). It then drops
the lock around the skb allocation, and afterwards checks only skb:

	release_sock(sk);
	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
	lock_sock(sk);
	if (!skb)
		goto out;

While the lock is dropped the socket is not owned. Two paths can then
run x25_disconnect() and set x25->neighbour to NULL:

  - this branch of x25_kill_sock(), reached from a RESTART or LAPB
    disconnect via x25_kill_by_neigh()
  - a remote CLEAR_REQUEST through x25_state3_machine()

sock_alloc_send_pskb() checks sk_err and SEND_SHUTDOWN only before it
allocates. A disconnect during the allocation or the relock goes
unnoticed, and the sender then reaches this before any TCP_ESTABLISHED
recheck:

	if (msg->msg_flags & MSG_OOB) {
		if (x25->neighbour->extended) {

The non-OOB branch has the same dereference. Can a remote peer trigger
a NULL pointer dereference this way?

The fragmentation loop in x25_output() has the same pattern. Each
fragment does release_sock() / sock_alloc_send_skb() / lock_sock(),
followed by an unchecked x25->neighbour->extended.

The old lock_sock() in x25_kill_by_neigh() could take the lock in the
same window, so this predates the patch. Still, the claim about keeping
the 7781607938c8 serialization holds only as far as that earlier fix
went.

> +	}
> +	bh_unlock_sock(sk);
> +	local_bh_enable();
> +}

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261001150118.128470-1-ngocthang2710.1999%40gmail.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [syzbot] [x25?] inconsistent lock state in x25_kill_by_neigh (2)
@ 2026-10-11  0:03 syzbot
  2026-11-16  0:34 ` [PATCH] x25: defer socket cleanup on link termination to process context Shuai Liu
  0 siblings, 1 reply; 11+ messages in thread
From: syzbot @ 2026-10-11  0:03 UTC (permalink / raw)
  To: davem, edumazet, horms, kuba, linux-kernel, linux-x25, ms, netdev,
	pabeni, syzkaller-bugs

Hello,

syzbot found the following issue on:

HEAD commit:    602042bf29f6 Merge tag 'wq-for-7.3-rc6-fixes' of git://git..
git tree:       upstream
console output: https://syzkaller.appspot.com/x/log.txt?x=13d66715580000
kernel config:  https://syzkaller.appspot.com/x/.config?x=929e4854a828f434
dashboard link: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6
compiler:       gcc (Debian 14.2.0-19) 14.2.0, GNU ld (GNU Binutils for Debian) 2.44
syz repro:      https://syzkaller.appspot.com/x/repro.syz?x=115d5f25580000
C reproducer:   https://syzkaller.appspot.com/x/repro.c?x=146adff5580000

Downloadable assets:
disk image (non-bootable): https://storage.googleapis.com/syzbot-assets/d900f083ada3/non_bootable_disk-602042bf.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/c804d2e9b640/vmlinux-602042bf.xz
kernel image: https://storage.googleapis.com/syzbot-assets/d6cd162ae59e/bzImage-602042bf.xz

IMPORTANT: if you fix the issue, please add the following tag to the commit:
Reported-by: syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com

================================
WARNING: inconsistent lock state
syzkaller #0 Not tainted
--------------------------------
inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} usage.
ksoftirqd/0/15 [HC0[0]:SC1[1]:HE1:SE0] takes:
ffff88802c938260 (sk_lock-AF_X25){+.?.}-{0:0}, at: lock_sock include/net/sock.h:1713 [inline]
ffff88802c938260 (sk_lock-AF_X25){+.?.}-{0:0}, at: x25_kill_by_neigh+0x168/0x280 net/x25/af_x25.c:1778
{SOFTIRQ-ON-W} state was registered at:
  lock_acquire kernel/locking/lockdep.c:5942 [inline]
  lock_acquire+0x1d1/0x380 kernel/locking/lockdep.c:5899
  lock_sock_nested+0x41/0xf0 net/core/sock.c:3837
  lock_sock include/net/sock.h:1713 [inline]
  x25_bind+0x221/0x5c0 net/x25/af_x25.c:694
  __sys_bind_socket net/socket.c:1945 [inline]
  __sys_bind_socket net/socket.c:1937 [inline]
  __sys_bind+0x1a9/0x260 net/socket.c:1976
  __do_sys_bind net/socket.c:1981 [inline]
  __se_sys_bind net/socket.c:1979 [inline]
  __x64_sys_bind+0x72/0xb0 net/socket.c:1979
  do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
  do_syscall_64+0x123/0x790 arch/x86/entry/syscall_64.c:84
  entry_SYSCALL_64_after_hwframe+0x77/0x7f
irq event stamp: 357794
hardirqs last  enabled at (357794): [<ffffffff81ca5245>] __local_bh_enable_ip+0xa5/0x120 kernel/softirq.c:478
hardirqs last disabled at (357793): [<ffffffff81ca526e>] __local_bh_enable_ip+0xce/0x120 kernel/softirq.c:455
softirqs last  enabled at (357466): [<ffffffff81ca4d98>] run_ksoftirqd kernel/softirq.c:1099 [inline]
softirqs last  enabled at (357466): [<ffffffff81ca4d98>] run_ksoftirqd+0x38/0x60 kernel/softirq.c:1091
softirqs last disabled at (357471): [<ffffffff81ca4d98>] run_ksoftirqd kernel/softirq.c:1099 [inline]
softirqs last disabled at (357471): [<ffffffff81ca4d98>] run_ksoftirqd+0x38/0x60 kernel/softirq.c:1091

other info that might help us debug this:
 Possible unsafe locking scenario:

       CPU0
       ----
  lock(sk_lock-AF_X25);
  <Interrupt>
    lock(sk_lock-AF_X25);

 *** DEADLOCK ***

locks held by ksoftirqd/0/15: 1, last CPU#0:
 #0: ffffffff8edec5a0 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire include/linux/rcupdate.h:309 [inline]
 #0: ffffffff8edec5a0 (rcu_read_lock){....}-{1:3}, at: rcu_read_lock include/linux/rcupdate.h:849 [inline]
 #0: ffffffff8edec5a0 (rcu_read_lock){....}-{1:3}, at: netif_receive_skb_core+0x25/0x1a0 net/core/dev.c:6321

stack backtrace:
CPU: 0 UID: 0 PID: 15 Comm: ksoftirqd/0 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:94 [inline]
 dump_stack_lvl+0x100/0x190 lib/dump_stack.c:120
 print_usage_bug.part.0+0x258/0x340 kernel/locking/lockdep.c:4086
 print_usage_bug kernel/locking/lockdep.c:4054 [inline]
 valid_state kernel/locking/lockdep.c:4100 [inline]
 mark_lock_irq kernel/locking/lockdep.c:4311 [inline]
 mark_lock+0x956/0x1020 kernel/locking/lockdep.c:4797
 mark_usage kernel/locking/lockdep.c:4686 [inline]
 __lock_acquire+0x7f1/0x1f40 kernel/locking/lockdep.c:5242
 lock_acquire kernel/locking/lockdep.c:5942 [inline]
 lock_acquire+0x1d1/0x380 kernel/locking/lockdep.c:5899
 lock_sock_nested+0x41/0xf0 net/core/sock.c:3837
 lock_sock include/net/sock.h:1713 [inline]
 x25_kill_by_neigh+0x168/0x280 net/x25/af_x25.c:1778
 x25_lapb_receive_frame+0x4b1/0x1000 net/x25/x25_dev.c:138
 __netif_receive_skb_one_core+0x1b2/0x1e0 net/core/dev.c:6297
 netif_receive_skb_core+0x5a/0x1a0 net/core/dev.c:6322
 lapbeth_napi_poll+0x42/0xc0 drivers/net/wan/lapbether.c:100
 __napi_poll.constprop.0+0xaf/0x450 net/core/dev.c:7830
 napi_poll net/core/dev.c:7893 [inline]
 net_rx_action+0xa40/0xf20 net/core/dev.c:8050
 handle_softirqs+0x1e6/0x9d0 kernel/softirq.c:645
 run_ksoftirqd kernel/softirq.c:1099 [inline]
 run_ksoftirqd+0x38/0x60 kernel/softirq.c:1091
 smpboot_thread_fn+0x3d3/0xab0 kernel/smpboot.c:160
 kthread+0x373/0x450 kernel/kthread.c:436
 ret_from_fork+0x730/0xd60 arch/x86/kernel/process.c:158
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
 </TASK>
BUG: sleeping function called from invalid context at net/core/sock.c:3839
in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 15, name: ksoftirqd/0
preempt_count: 100, expected: 0
RCU nest depth: 1, expected: 0
INFO: lockdep is turned off.
Preemption disabled at:
[<ffffffff81ca41d3>] softirq_handle_begin kernel/softirq.c:486 [inline]
[<ffffffff81ca41d3>] handle_softirqs+0xf3/0x9d0 kernel/softirq.c:621
CPU: 0 UID: 0 PID: 15 Comm: ksoftirqd/0 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014
Call Trace:
 <TASK>
 __dump_stack lib/dump_stack.c:94 [inline]
 dump_stack_lvl+0x100/0x190 lib/dump_stack.c:120
 __might_resched.cold+0x1dc/0x222 kernel/sched/core.c:9264
 lock_sock_nested+0x52/0xf0 net/core/sock.c:3839
 lock_sock include/net/sock.h:1713 [inline]
 x25_kill_by_neigh+0x168/0x280 net/x25/af_x25.c:1778
 x25_lapb_receive_frame+0x4b1/0x1000 net/x25/x25_dev.c:138
 __netif_receive_skb_one_core+0x1b2/0x1e0 net/core/dev.c:6297
 netif_receive_skb_core+0x5a/0x1a0 net/core/dev.c:6322
 lapbeth_napi_poll+0x42/0xc0 drivers/net/wan/lapbether.c:100
 __napi_poll.constprop.0+0xaf/0x450 net/core/dev.c:7830
 napi_poll net/core/dev.c:7893 [inline]
 net_rx_action+0xa40/0xf20 net/core/dev.c:8050
 handle_softirqs+0x1e6/0x9d0 kernel/softirq.c:645
 run_ksoftirqd kernel/softirq.c:1099 [inline]
 run_ksoftirqd+0x38/0x60 kernel/softirq.c:1091
 smpboot_thread_fn+0x3d3/0xab0 kernel/smpboot.c:160
 kthread+0x373/0x450 kernel/kthread.c:436
 ret_from_fork+0x730/0xd60 arch/x86/kernel/process.c:158
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
 </TASK>


---
This report is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzkaller@googlegroups.com.

syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.

If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title

If you want syzbot to run the reproducer, reply with:
#syz test: git://repo/address.git branch-or-commit-hash
If you attach or paste a git patch, syzbot will apply it before testing.

If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)

If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report

If you want to undo deduplication, reply with:
#syz undup

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH] x25: defer socket cleanup on link termination to process context
  2026-10-11  0:03 [syzbot] [x25?] inconsistent lock state in x25_kill_by_neigh (2) syzbot
@ 2026-11-16  0:34 ` Shuai Liu
  2026-11-18 18:35   ` netdev-bot+sashiko
  0 siblings, 1 reply; 11+ messages in thread
From: Shuai Liu @ 2026-11-16  0:34 UTC (permalink / raw)
  To: ms, linux-x25
  Cc: netdev, davem, kuba, edumazet, pabeni, horms,
	syzbot+300375dfd44665f063a6, linux-kernel, 847807867

x25_kill_by_neigh() walks all sockets bound to a neighbour and calls
lock_sock() and x25_disconnect() on each one.  It is invoked from
x25_link_terminated() and x25_link_control(), which both run in softirq
context when the LAPB link goes down or a restart frame arrives, e.g.
from the lapbether NAPI poll path.

lock_sock() takes a mutex in its slow path, so calling it from softirq
sleeps in atomic context:

  BUG: sleeping function called from invalid context at net/core/sock.c:3839
    in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 14, name: ksoftirqd/0
    ...
    lock_sock_nested+0x66/0x70
    x25_kill_by_neigh+0x90/0x110
    x25_link_terminated+0x18/0x50
    x25_lapb_receive_frame+0x1c2/0x1f0

and lockdep flags the sk_lock-AF_X25 usage violation because the same
lock is registered in process context by x25_bind():

  WARNING: inconsistent lock state
  inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} usage.
    ksoftirqd/0/14 [HC0[0]:SC1[1]:HE1:SE0] takes:
    ...
    x25_kill_by_neigh+0x90/0x110
    x25_link_terminated+0x18/0x50
    x25_lapb_receive_frame+0x1c2/0x1f0

Defer the neighbour socket cleanup to a per-neighbour work item so it
always runs in process context.  The work holds a reference on the
neighbour so the deferred cleanup cannot race with neighbour teardown.
x25_kill_by_neigh() is idempotent, so repeated scheduling is harmless.

Reported-by: syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6
Signed-off-by: Shuai Liu <847807867@qq.com>
---
 include/net/x25.h  |  2 ++
 net/x25/x25_link.c | 18 +++++++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/include/net/x25.h b/include/net/x25.h
index 414f3fd993454..cb51be81351b8 100644
--- a/include/net/x25.h
+++ b/include/net/x25.h
@@ -13,6 +13,7 @@
 #include <linux/x25.h>
 #include <linux/slab.h>
 #include <linux/refcount.h>
+#include <linux/workqueue.h>
 #include <net/sock.h>
 
 #define	X25_ADDR_LEN			16
@@ -143,6 +144,7 @@ struct x25_neigh {
 	struct sk_buff_head	queue;
 	unsigned long		t20;
 	struct timer_list	t20timer;
+	struct work_struct	kill_work;
 	unsigned long		global_facil_mask;
 	refcount_t		refcnt;
 };
diff --git a/net/x25/x25_link.c b/net/x25/x25_link.c
index d1e8cd81c2143..150ccd78cba64 100644
--- a/net/x25/x25_link.c
+++ b/net/x25/x25_link.c
@@ -58,6 +58,14 @@ static inline void x25_stop_t20timer(struct x25_neigh *nb)
 	timer_delete(&nb->t20timer);
 }
 
+static void x25_kill_by_neigh_work(struct work_struct *work)
+{
+	struct x25_neigh *nb = container_of(work, struct x25_neigh, kill_work);
+
+	x25_kill_by_neigh(nb);
+	x25_neigh_put(nb);
+}
+
 /*
  *	This handles all restart and diagnostic frames.
  */
@@ -82,7 +90,8 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
 			break;
 		case X25_LINK_STATE_3:
 			/* clear existing virtual calls */
-			x25_kill_by_neigh(nb);
+			x25_neigh_hold(nb);
+			schedule_work(&nb->kill_work);
 
 			x25_transmit_restart_confirmation(nb);
 			break;
@@ -97,7 +106,8 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
 			break;
 		case X25_LINK_STATE_3:
 			/* clear existing virtual calls */
-			x25_kill_by_neigh(nb);
+			x25_neigh_hold(nb);
+			schedule_work(&nb->kill_work);
 
 			x25_transmit_restart_request(nb);
 			nb->state = X25_LINK_STATE_2;
@@ -254,7 +264,8 @@ void x25_link_terminated(struct x25_neigh *nb)
 	x25_stop_t20timer(nb);
 
 	/* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
-	x25_kill_by_neigh(nb);
+	x25_neigh_hold(nb);
+	schedule_work(&nb->kill_work);
 }
 
 /*
@@ -269,6 +280,7 @@ void x25_link_device_up(struct net_device *dev)
 
 	skb_queue_head_init(&nb->queue);
 	timer_setup(&nb->t20timer, x25_t20timer_expiry, 0);
+	INIT_WORK(&nb->kill_work, x25_kill_by_neigh_work);
 
 	dev_hold(dev);
 	nb->dev      = dev;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] x25: defer socket cleanup on link termination to process context
  2026-11-16  0:34 ` [PATCH] x25: defer socket cleanup on link termination to process context Shuai Liu
@ 2026-11-18 18:35   ` netdev-bot+sashiko
  2026-11-19  1:24     ` Shuai Liu
  0 siblings, 1 reply; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-11-18 18:35 UTC (permalink / raw)
  To: 847807867
  Cc: ms, linux-x25, netdev, davem, kuba, edumazet, pabeni, horms,
	syzbot+300375dfd44665f063a6, linux-kernel

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

Critical: 0 · High: 3 · Medium: 1 · Low: 1

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] Each of the three new deferral sites calls x25_neigh_hold(nb)
  and then schedule_work(&nb->kill_work) without checking what…
- [High] The patch adds nb->kill_work.
- [High] Device deletion now leaves a window in which X.25 sockets can
  transmit on a freed net_device.
- [Medium] Moving x25_kill_by_neigh() into a worker changes the X.25
  restart and out-of-order handling.
- [Low] The commit fixes a real bug reported by syzbot (sleeping in
  atomic context and an inconsistent lockdep state in…

--

x25: defer socket cleanup on link termination to process context

This commit moves the x25_kill_by_neigh() calls in x25_link_control() and
x25_link_terminated() into a per-neighbour work item, so that lock_sock()
runs in process context. Each call site takes a reference on the
neighbour, and the work item drops it once it has run.

Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6

> Reported-by: syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6
> Signed-off-by: Shuai Liu <847807867@qq.com>

[Severity: Low]
This isn't a bug, but should this have a Fixes: tag and a Cc: stable?

The lock_sock()/release_sock() calls in x25_kill_by_neigh() were added by
commit 7781607938c8 ("net/x25: Fix null-ptr-deref caused by
x25_disconnect").

The later commit 5499e0602d2f ("net/x25: fix use-after-free in
x25_kill_by_neigh()") has Fixes: 7781607938c8 and Cc: stable, so the
stable trees contain the sleeping code too.

> diff --git a/net/x25/x25_link.c b/net/x25/x25_link.c
> index d1e8cd81c2143..150ccd78cba64 100644
> --- a/net/x25/x25_link.c
> +++ b/net/x25/x25_link.c

[ ... ]

> @@ -82,7 +90,8 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
>  			break;
>  		case X25_LINK_STATE_3:
>  			/* clear existing virtual calls */
> -			x25_kill_by_neigh(nb);
> +			x25_neigh_hold(nb);
> +			schedule_work(&nb->kill_work);

[Severity: High]
What happens to the reference from x25_neigh_hold() when kill_work is
already pending? In that case schedule_work() returns false and does not
queue the item again. x25_kill_by_neigh_work() still drops only one
reference per run:

static void x25_kill_by_neigh_work(struct work_struct *work)
{
	...
	x25_kill_by_neigh(nb);
	x25_neigh_put(nb);
}

So each extra hold leaks a reference on nb. Once __x25_remove_neigh()
drops the list's reference, the refcount never reaches zero and nb is
never kfree()'d.

In X25_LINK_STATE_3, a RESTART_REQUEST leaves nb->state at STATE_3. A
remote peer that sends several restart requests before the worker runs
would therefore leak one reference per request.

Locally, the same happens when x25_link_terminated() runs twice before
the worker does. Examples are NETDEV_CHANGE with carrier off followed by
NETDEV_DOWN, or X25_IFACE_DISCONNECT followed by NETDEV_DOWN.

The commit message says "x25_kill_by_neigh() is idempotent, so repeated
scheduling is harmless". That holds for the cleanup itself, but does it
hold for the reference counting?

Would something like this work at all three call sites?

	x25_neigh_hold(nb);
	if (!schedule_work(&nb->kill_work))
		x25_neigh_put(nb);

Every caller still holds its own reference from x25_get_neigh(), so that
put cannot free nb.

The RESTART_CONFIRMATION case and x25_link_terminated() below have the
same pattern.

>  
>  			x25_transmit_restart_confirmation(nb);
>  			break;

[Severity: Medium]
Does deferring the clear change the restart semantics here? Before this
patch, the existing virtual calls were cleared before the restart
confirmation went out.

Now the confirmation goes out and nb->state stays at STATE_3. The stale
sockets are still in X25_STATE_3 with neighbour == nb.

x25_kill_by_neigh() matches only on the neighbour:

	sk_for_each(s, &x25_list) {
		if (x25_sk(s)->neighbour == nb) {

Calls set up after the restart, through x25_receive_data() ->
x25_rx_call_request() or a local x25_connect(), also have neighbour ==
nb. Would they get disconnected with ENETUNREACH when the worker finally
runs?

Until the worker runs, x25_find_socket(lci, nb) in x25_receive_data()
also keeps delivering post-restart frames to the stale sockets. That
includes a CALL_REQUEST that reuses a stale LCI, which would go to
x25_process_rx_frame() instead of x25_rx_call_request().

After x25_link_terminated() sets nb->state = X25_LINK_STATE_0, the stale
sockets can still transmit. This can happen through x25_sendmsg() ->
x25_kick(), or through the heartbeat and T2 timers via
x25_write_internal().

In STATE_0, x25_transmit_link() queues the skb on nb->queue and calls
x25_establish_link(). Could stale traffic bring a just-terminated link
back up? Those frames would then be flushed by the
nb->state == X25_LINK_STATE_3 loop at the end of x25_link_control().

The NETDEV_DOWN and NETDEV_CHANGE callers in x25_device_event() already
run in process context and didn't need deferral, but they lose their
synchronous cleanup too.

The "clear existing virtual calls" and "X.25 03/93 4.6.3" comments also
no longer match what the code does.

[ ... ]

> @@ -254,7 +264,8 @@ void x25_link_terminated(struct x25_neigh *nb)
>  	x25_stop_t20timer(nb);
>  
>  	/* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
> -	x25_kill_by_neigh(nb);
> +	x25_neigh_hold(nb);
> +	schedule_work(&nb->kill_work);
>  }

[Severity: High]
Can this lead to a use-after-free of the net_device when the device is
unregistered? unregister_netdevice() -> dev_close() sends NETDEV_DOWN,
and NETDEV_UNREGISTER follows under RTNL:

x25_device_event()
  NETDEV_DOWN
    x25_link_terminated(nb)
      nb->state = X25_LINK_STATE_0
      schedule_work(&nb->kill_work)    <- sockets not cleared yet
  NETDEV_UNREGISTER
    x25_link_device_down(dev)
      __x25_remove_neigh(nb)
      dev_put(dev)

x25 sockets hold a reference on nb but not on nb->dev. Once the netdev
is freed, nb->dev dangles.

Before this patch, NETDEV_DOWN had already disconnected the sockets
(neighbour = NULL, state = X25_STATE_0). Now, until the worker runs, a
connected socket still has x25->state == X25_STATE_3 and
x25->neighbour == nb:

x25_sendmsg()
  x25_kick()
    x25_transmit_link(skb, nb)        <- nb->state == STATE_0
      x25_establish_link(nb)
        switch (nb->dev->type)
        ...
        skb->dev = nb->dev;
        dev_queue_xmit(skb);

The heartbeat (x25_check_rbuf()) and the T2 timer
(x25_enquiry_response()) reach the same path through
x25_write_internal().

The worker also passes the stale nb->dev to x25_clear_forward_by_dev().
That function compares raw pointers, so it could match a new device
allocated at the same address.

Would it be better to keep the NETDEV_DOWN teardown synchronous, since it
runs in process context? Or at least flush or cancel kill_work and keep
the device pinned until the work has run?

>  
>  /*
> @@ -269,6 +280,7 @@ void x25_link_device_up(struct net_device *dev)
>  
>  	skb_queue_head_init(&nb->queue);
>  	timer_setup(&nb->t20timer, x25_t20timer_expiry, 0);
> +	INIT_WORK(&nb->kill_work, x25_kill_by_neigh_work);

[Severity: High]
Does anything cancel or flush kill_work on teardown? x25_exit(),
x25_link_free() and x25_link_device_down() never call cancel_work_sync()
or flush_work(). A pending work item does not pin the module.

With no X.25 sockets open, "ip link set <x25dev> down" (NETDEV_DOWN ->
x25_link_terminated() -> schedule_work()) followed right away by
"rmmod x25" is allowed.

x25_exit() -> x25_link_free() then drops the list references. nb stays
allocated because the work holds a reference. However, the module text
and data are freed before system_percpu_wq runs the item. That covers
x25_kill_by_neigh_work(), x25_list_lock, x25_list and x25_forward_list.

There is also an ordering question in x25_exit():

	x25_proc_exit();
	x25_link_free();
	x25_route_free();
	...
	unregister_netdevice_notifier(&x25_dev_notifier);

	dev_remove_pack(&x25_packet_type);

A receive or notifier call that is already in progress, holding an nb
from x25_get_neigh(), could still queue kill_work after x25_link_free().

Would a dedicated workqueue cover this, destroyed at the end of
x25_exit() after packet reception and the notifier have been shut down?

>  
>  	dev_hold(dev);
>  	nb->dev      = dev;

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/tencent_F7C2DC0FA42B5866E5C39A9E70D3C52C2609%40qq.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH] x25: defer socket cleanup on link termination to process context
  2026-11-18 18:35   ` netdev-bot+sashiko
@ 2026-11-19  1:24     ` Shuai Liu
  2026-11-23  1:40       ` netdev-bot+sashiko
  0 siblings, 1 reply; 11+ messages in thread
From: Shuai Liu @ 2026-11-19  1:24 UTC (permalink / raw)
  To: ms, linux-x25
  Cc: netdev, davem, kuba, edumazet, pabeni, horms,
	syzbot+300375dfd44665f063a6, linux-kernel, 847807867,
	netdev-bot+sashiko, stable

x25_kill_by_neigh() walks all sockets bound to a neighbour and calls
lock_sock() and x25_disconnect() on each one.  It is invoked from
x25_link_terminated() and x25_link_control(), which both run in softirq
context when the LAPB link goes down or a restart frame arrives, e.g.
from the lapbether NAPI poll path.

lock_sock() takes a mutex in its slow path, so calling it from softirq
sleeps in atomic context:

  BUG: sleeping function called from invalid context at net/core/sock.c:3839
    in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 14, name: ksoftirqd/0
    ...
    lock_sock_nested+0x66/0x70
    x25_kill_by_neigh+0x90/0x110
    x25_link_terminated+0x18/0x50
    x25_lapb_receive_frame+0x1c2/0x1f0

and lockdep flags the sk_lock-AF_X25 usage violation because the same
lock is registered in process context by x25_bind():

  WARNING: inconsistent lock state
  inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} usage.
    ksoftirqd/0/14 [HC0[0]:SC1[1]:HE1:SE0] takes:
    ...
    x25_kill_by_neigh+0x90/0x110
    x25_link_terminated+0x18/0x50
    x25_lapb_receive_frame+0x1c2/0x1f0

Defer the neighbour socket cleanup to a per-neighbour work item so it
always runs in process context.  The work item holds a reference on the
neighbour and on its net_device, so the deferred cleanup cannot race
with neighbour or device teardown; both references are dropped again if
queueing fails because the work is already pending.  A dedicated
workqueue is drained in x25_exit() so that the module stays loaded until
pending work has run, and NETDEV_DOWN flushes the work to keep device
teardown synchronous as before.

Fixes: 7781607938c8 ("net/x25: Fix null-ptr-deref caused by x25_disconnect")
Reported-by: syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6
Cc: stable@vger.kernel.org
Signed-off-by: Shuai Liu <847807867@qq.com>
---

Changes in v2:
  - Use a dedicated workqueue and drain it in x25_exit() so the module
    stays loaded until pending work has run (Sashiko)
  - Hold a reference on the neighbour's net_device for the deferred work,
    and drop it again when queueing fails because the work is already
    pending (Sashiko)
  - Flush the work in NETDEV_DOWN to keep device teardown synchronous
    (Sashiko)
  - Explicitly request WQ_PERCPU to satisfy the workqueue API
  - Add Fixes tag and Cc stable

 include/net/x25.h  |  3 +++
 net/x25/af_x25.c   | 11 ++++++++++-
 net/x25/x25_link.c | 28 +++++++++++++++++++++++++---
 3 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/include/net/x25.h b/include/net/x25.h
index 414f3fd993454..125803d4f0ee7 100644
--- a/include/net/x25.h
+++ b/include/net/x25.h
@@ -13,6 +13,7 @@
 #include <linux/x25.h>
 #include <linux/slab.h>
 #include <linux/refcount.h>
+#include <linux/workqueue.h>
 #include <net/sock.h>
 
 #define	X25_ADDR_LEN			16
@@ -143,6 +144,7 @@ struct x25_neigh {
 	struct sk_buff_head	queue;
 	unsigned long		t20;
 	struct timer_list	t20timer;
+	struct work_struct	kill_work;
 	unsigned long		global_facil_mask;
 	refcount_t		refcnt;
 };
@@ -317,6 +319,7 @@ extern struct list_head x25_forward_list;
 extern rwlock_t x25_forward_list_lock;
 extern struct list_head x25_neigh_list;
 extern rwlock_t x25_neigh_list_lock;
+extern struct workqueue_struct *x25_wq;
 
 int x25_proc_init(void);
 void x25_proc_exit(void);
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index f45c9ec7d2c68..a1a8bc203a24f 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -222,6 +222,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event,
 			nb = x25_get_neigh(dev);
 			if (nb) {
 				x25_link_terminated(nb);
+				flush_work(&nb->kill_work);
 				x25_neigh_put(nb);
 			}
 			x25_route_device_down(dev);
@@ -1790,9 +1791,13 @@ static int __init x25_init(void)
 {
 	int rc;
 
+	x25_wq = alloc_workqueue("x25", WQ_PERCPU, 0);
+	if (!x25_wq)
+		return -ENOMEM;
+
 	rc = proto_register(&x25_proto, 0);
 	if (rc)
-		goto out;
+		goto out_wq;
 
 	rc = sock_register(&x25_family_ops);
 	if (rc)
@@ -1825,6 +1830,8 @@ static int __init x25_init(void)
 	sock_unregister(AF_X25);
 out_proto:
 	proto_unregister(&x25_proto);
+out_wq:
+	destroy_workqueue(x25_wq);
 	goto out;
 }
 module_init(x25_init);
@@ -1843,6 +1850,8 @@ static void __exit x25_exit(void)
 
 	sock_unregister(AF_X25);
 	proto_unregister(&x25_proto);
+
+	destroy_workqueue(x25_wq);
 }
 module_exit(x25_exit);
 
diff --git a/net/x25/x25_link.c b/net/x25/x25_link.c
index d1e8cd81c2143..424044c1cf974 100644
--- a/net/x25/x25_link.c
+++ b/net/x25/x25_link.c
@@ -31,6 +31,8 @@
 LIST_HEAD(x25_neigh_list);
 DEFINE_RWLOCK(x25_neigh_list_lock);
 
+struct workqueue_struct *x25_wq;
+
 static void x25_t20timer_expiry(struct timer_list *);
 
 static void x25_transmit_restart_confirmation(struct x25_neigh *nb);
@@ -58,6 +60,25 @@ static inline void x25_stop_t20timer(struct x25_neigh *nb)
 	timer_delete(&nb->t20timer);
 }
 
+static void x25_kill_by_neigh_work(struct work_struct *work)
+{
+	struct x25_neigh *nb = container_of(work, struct x25_neigh, kill_work);
+
+	x25_kill_by_neigh(nb);
+	dev_put(nb->dev);
+	x25_neigh_put(nb);
+}
+
+static void x25_defer_kill_by_neigh(struct x25_neigh *nb)
+{
+	x25_neigh_hold(nb);
+	dev_hold(nb->dev);
+	if (!queue_work(x25_wq, &nb->kill_work)) {
+		dev_put(nb->dev);
+		x25_neigh_put(nb);
+	}
+}
+
 /*
  *	This handles all restart and diagnostic frames.
  */
@@ -82,7 +103,7 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
 			break;
 		case X25_LINK_STATE_3:
 			/* clear existing virtual calls */
-			x25_kill_by_neigh(nb);
+			x25_defer_kill_by_neigh(nb);
 
 			x25_transmit_restart_confirmation(nb);
 			break;
@@ -97,7 +118,7 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
 			break;
 		case X25_LINK_STATE_3:
 			/* clear existing virtual calls */
-			x25_kill_by_neigh(nb);
+			x25_defer_kill_by_neigh(nb);
 
 			x25_transmit_restart_request(nb);
 			nb->state = X25_LINK_STATE_2;
@@ -254,7 +275,7 @@ void x25_link_terminated(struct x25_neigh *nb)
 	x25_stop_t20timer(nb);
 
 	/* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
-	x25_kill_by_neigh(nb);
+	x25_defer_kill_by_neigh(nb);
 }
 
 /*
@@ -269,6 +290,7 @@ void x25_link_device_up(struct net_device *dev)
 
 	skb_queue_head_init(&nb->queue);
 	timer_setup(&nb->t20timer, x25_t20timer_expiry, 0);
+	INIT_WORK(&nb->kill_work, x25_kill_by_neigh_work);
 
 	dev_hold(dev);
 	nb->dev      = dev;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] x25: defer socket cleanup on link termination to process context
  2026-11-19  1:24     ` Shuai Liu
@ 2026-11-23  1:40       ` netdev-bot+sashiko
  2026-11-23  3:47         ` Shuai Liu
  2026-11-23  3:48         ` [PATCH v3] " Shuai Liu
  0 siblings, 2 replies; 11+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23  1:40 UTC (permalink / raw)
  To: 847807867
  Cc: ms, linux-x25, netdev, davem, kuba, edumazet, pabeni, horms,
	syzbot+300375dfd44665f063a6, linux-kernel, netdev-bot+sashiko,
	stable

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

Critical: 0 · High: 0 · Medium: 1 · Low: 1

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).

- [Medium] The patch makes clearing of existing virtual calls
  asynchronous.
- [Low] x25_device_event() (net/x25/af_x25.c) calls x25_link_terminated()
  from two process-context notifier events, and the patch treats them…

--

x25: defer socket cleanup on link termination to process context

This patch moves x25_kill_by_neigh() out of softirq context and into a
per-neighbour work item on a new dedicated x25 workqueue, which avoids
calling lock_sock() from softirq. The work holds references on the
neighbour and its net_device, and NETDEV_DOWN flushes the work.

Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6

> diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
> index f45c9ec7d2c68..a1a8bc203a24f 100644
> --- a/net/x25/af_x25.c
> +++ b/net/x25/af_x25.c
> @@ -222,6 +222,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event,
>  			nb = x25_get_neigh(dev);
>  			if (nb) {
>  				x25_link_terminated(nb);
> +				flush_work(&nb->kill_work);
>  				x25_neigh_put(nb);
>  			}
>  			x25_route_device_down(dev);

[Severity: Low]
NETDEV_DOWN now flushes kill_work. Should the NETDEV_CHANGE case in
x25_device_event() flush it as well?

net/x25/af_x25.c:x25_device_event() {
    ...
		case NETDEV_CHANGE:
			if (!netif_carrier_ok(dev)) {
				nb = x25_get_neigh(dev);
				if (nb) {
					x25_link_terminated(nb);
					x25_neigh_put(nb);
				}
			}
			break;
    ...
}

Both cases run in process context under RTNL. Before this patch, both
cleared the sockets synchronously.

After carrier loss, the sockets now stay attached to nb with
nb->state == X25_LINK_STATE_0 until the worker runs. During that window
they can still send through x25_transmit_link(), which would queue the
frame and call x25_establish_link().

Only the softirq callers seem to need the deferral. The process-context
callers could keep the old behaviour with a flush, or by calling
x25_kill_by_neigh() directly.

[ ... ]

> diff --git a/net/x25/x25_link.c b/net/x25/x25_link.c
> index d1e8cd81c2143..424044c1cf974 100644
> --- a/net/x25/x25_link.c
> +++ b/net/x25/x25_link.c

[ ... ]

> +static void x25_defer_kill_by_neigh(struct x25_neigh *nb)
> +{
> +	x25_neigh_hold(nb);
> +	dev_hold(nb->dev);
> +	if (!queue_work(x25_wq, &nb->kill_work)) {
> +		dev_put(nb->dev);
> +		x25_neigh_put(nb);
> +	}
> +}
> +
>  /*
>   *	This handles all restart and diagnostic frames.
>   */
> @@ -82,7 +103,7 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
>  			break;
>  		case X25_LINK_STATE_3:
>  			/* clear existing virtual calls */
> -			x25_kill_by_neigh(nb);
> +			x25_defer_kill_by_neigh(nb);
>  
>  			x25_transmit_restart_confirmation(nb);
>  			break;

[Severity: Medium]
With this change, does the restart/terminate sequence continue while the
old virtual calls are still bound to nb?

x25_defer_kill_by_neigh() only queues the work and returns. In
x25_link_control(), the restart confirmation (or the restart request,
the move to X25_LINK_STATE_2, and T20 in the RESTART_CONFIRMATION case)
goes out first. nb->queue is also drained in STATE_3 at the end of the
function.

The old sockets are still on x25_list at that point, with
x25->neighbour == nb and their old LCIs. The same applies to
x25_link_terminated(), which sets STATE_0 but leaves the sockets
connected. Neither the softirq X25_IFACE_DISCONNECT path nor
NETDEV_CHANGE flushes the work.

Until the worker runs, a few things look possible.

If the peer reuses an LCI after the restart, x25_receive_data() still
matches the stale socket:

net/x25/x25_dev.c:x25_receive_data() {
    ...
	if ((sk = x25_find_socket(lci, nb)) != NULL) {
		...
		queued = x25_process_rx_frame(sk, skb);
		...
	}

	if (frametype == X25_CALL_REQUEST)
		return x25_rx_call_request(skb, nb, lci);
    ...
}

So could a new CALL_REQUEST on a reused LCI be handed to the old socket
instead of reaching x25_rx_call_request()?

A stale socket can also still transmit via x25_transmit_link(). In
STATE_0 that queues the frame and calls x25_establish_link(), which
brings the link back up for a dead call. In STATE_1/2 the frame is
queued and flushed later onto the restarted link.

When x25_kill_by_neigh_work()->x25_kill_by_neigh() finally runs, it
disconnects every socket whose neighbour == nb at that moment. That
includes calls set up after the triggering event, such as sockets from
x25_rx_call_request() or outgoing calls from x25_connect():

	x25->neighbour = x25_get_neigh(rt->dev);

Can valid new calls get torn down with ENETUNREACH this way? The same
question applies to forwards that x25_clear_forward_by_dev() removes
after being created post-event.

The comments "clear existing virtual calls" and "Out of order: clear
existing virtual calls (X.25 03/93 4.6.3)" also no longer match what
happens synchronously.

One option might be to detach the sockets synchronously in softirq (for
example, mark them dead or clear lci/neighbour under x25_list_lock or
bh_lock_sock()) and defer only the part that sleeps.

> @@ -97,7 +118,7 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
>  			break;
>  		case X25_LINK_STATE_3:
>  			/* clear existing virtual calls */
> -			x25_kill_by_neigh(nb);
> +			x25_defer_kill_by_neigh(nb);
>  
>  			x25_transmit_restart_request(nb);
>  			nb->state = X25_LINK_STATE_2;
> @@ -254,7 +275,7 @@ void x25_link_terminated(struct x25_neigh *nb)
>  	x25_stop_t20timer(nb);
>  
>  	/* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
> -	x25_kill_by_neigh(nb);
> +	x25_defer_kill_by_neigh(nb);
>  }

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/tencent_EC124B80BC53531B2EAB18DD04B37BAE7406%40qq.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH] x25: defer socket cleanup on link termination to process context
  2026-11-23  1:40       ` netdev-bot+sashiko
@ 2026-11-23  3:47         ` Shuai Liu
  2026-11-23  3:48         ` [PATCH v3] " Shuai Liu
  1 sibling, 0 replies; 11+ messages in thread
From: Shuai Liu @ 2026-11-23  3:47 UTC (permalink / raw)
  To: ms, linux-x25
  Cc: netdev, davem, kuba, edumazet, pabeni, horms,
	syzbot+300375dfd44665f063a6, linux-kernel, 847807867,
	netdev-bot+sashiko, stable

x25_kill_by_neigh() walks all sockets bound to a neighbour and calls
lock_sock() and x25_disconnect() on each one.  It is invoked from
x25_link_terminated() and x25_link_control(), which both run in softirq
context when the LAPB link goes down or a restart frame arrives, e.g.
from the lapbether NAPI poll path.

lock_sock() takes a mutex in its slow path, so calling it from softirq
sleeps in atomic context:

  BUG: sleeping function called from invalid context at net/core/sock.c:3839
    in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 14, name: ksoftirqd/0
    ...
    lock_sock_nested+0x66/0x70
    x25_kill_by_neigh+0x90/0x110
    x25_link_terminated+0x18/0x50
    x25_lapb_receive_frame+0x1c2/0x1f0

and lockdep flags the sk_lock-AF_X25 usage violation because the same
lock is registered in process context by x25_bind():

  WARNING: inconsistent lock state
  inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} usage.
    ksoftirqd/0/14 [HC0[0]:SC1[1]:HE1:SE0] takes:
    ...
    x25_kill_by_neigh+0x90/0x110
    x25_link_terminated+0x18/0x50
    x25_lapb_receive_frame+0x1c2/0x1f0

Defer the neighbour socket cleanup to a per-neighbour work item so it
always runs in process context.  The work item holds a reference on the
neighbour and on its net_device, so the deferred cleanup cannot race
with neighbour or device teardown; both references are dropped again if
queueing fails because the work is already pending.  A dedicated
workqueue is drained in x25_exit() so that the module stays loaded until
pending work has run, and NETDEV_DOWN flushes the work to keep device
teardown synchronous as before.

Fixes: 7781607938c8 ("net/x25: Fix null-ptr-deref caused by x25_disconnect")
Reported-by: syzbot+300375dfd44665f063a6@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6
Cc: stable@vger.kernel.org
Signed-off-by: Shuai Liu <847807867@qq.com>
---

Changes in v2:
  - Use a dedicated workqueue and drain it in x25_exit() so the module
    stays loaded until pending work has run (Sashiko)
  - Hold a reference on the neighbour's net_device for the deferred work,
    and drop it again when queueing fails because the work is already
    pending (Sashiko)
  - Flush the work in NETDEV_DOWN to keep device teardown synchronous
    (Sashiko)
  - Explicitly request WQ_PERCPU to satisfy the workqueue API
  - Add Fixes tag and Cc stable

Changes in v3:
  - Flush kill_work in NETDEV_CHANGE as well, restoring synchronous
    socket cleanup for both process-context notifier events (Sashiko)

 include/net/x25.h  |  3 +++
 net/x25/af_x25.c   | 12 +++++++++++-
 net/x25/x25_link.c | 28 +++++++++++++++++++++++++---
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/include/net/x25.h b/include/net/x25.h
index 414f3fd993454..125803d4f0ee7 100644
--- a/include/net/x25.h
+++ b/include/net/x25.h
@@ -13,6 +13,7 @@
 #include <linux/x25.h>
 #include <linux/slab.h>
 #include <linux/refcount.h>
+#include <linux/workqueue.h>
 #include <net/sock.h>
 
 #define	X25_ADDR_LEN			16
@@ -143,6 +144,7 @@ struct x25_neigh {
 	struct sk_buff_head	queue;
 	unsigned long		t20;
 	struct timer_list	t20timer;
+	struct work_struct	kill_work;
 	unsigned long		global_facil_mask;
 	refcount_t		refcnt;
 };
@@ -317,6 +319,7 @@ extern struct list_head x25_forward_list;
 extern rwlock_t x25_forward_list_lock;
 extern struct list_head x25_neigh_list;
 extern rwlock_t x25_neigh_list_lock;
+extern struct workqueue_struct *x25_wq;
 
 int x25_proc_init(void);
 void x25_proc_exit(void);
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index f45c9ec7d2c68..3696414617db1 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -222,6 +222,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event,
 			nb = x25_get_neigh(dev);
 			if (nb) {
 				x25_link_terminated(nb);
+				flush_work(&nb->kill_work);
 				x25_neigh_put(nb);
 			}
 			x25_route_device_down(dev);
@@ -235,6 +236,7 @@ static int x25_device_event(struct notifier_block *this, unsigned long event,
 				nb = x25_get_neigh(dev);
 				if (nb) {
 					x25_link_terminated(nb);
+					flush_work(&nb->kill_work);
 					x25_neigh_put(nb);
 				}
 			}
@@ -1790,9 +1792,13 @@ static int __init x25_init(void)
 {
 	int rc;
 
+	x25_wq = alloc_workqueue("x25", WQ_PERCPU, 0);
+	if (!x25_wq)
+		return -ENOMEM;
+
 	rc = proto_register(&x25_proto, 0);
 	if (rc)
-		goto out;
+		goto out_wq;
 
 	rc = sock_register(&x25_family_ops);
 	if (rc)
@@ -1825,6 +1831,8 @@ static int __init x25_init(void)
 	sock_unregister(AF_X25);
 out_proto:
 	proto_unregister(&x25_proto);
+out_wq:
+	destroy_workqueue(x25_wq);
 	goto out;
 }
 module_init(x25_init);
@@ -1843,6 +1851,8 @@ static void __exit x25_exit(void)
 
 	sock_unregister(AF_X25);
 	proto_unregister(&x25_proto);
+
+	destroy_workqueue(x25_wq);
 }
 module_exit(x25_exit);
 
diff --git a/net/x25/x25_link.c b/net/x25/x25_link.c
index d1e8cd81c2143..424044c1cf974 100644
--- a/net/x25/x25_link.c
+++ b/net/x25/x25_link.c
@@ -31,6 +31,8 @@
 LIST_HEAD(x25_neigh_list);
 DEFINE_RWLOCK(x25_neigh_list_lock);
 
+struct workqueue_struct *x25_wq;
+
 static void x25_t20timer_expiry(struct timer_list *);
 
 static void x25_transmit_restart_confirmation(struct x25_neigh *nb);
@@ -58,6 +60,25 @@ static inline void x25_stop_t20timer(struct x25_neigh *nb)
 	timer_delete(&nb->t20timer);
 }
 
+static void x25_kill_by_neigh_work(struct work_struct *work)
+{
+	struct x25_neigh *nb = container_of(work, struct x25_neigh, kill_work);
+
+	x25_kill_by_neigh(nb);
+	dev_put(nb->dev);
+	x25_neigh_put(nb);
+}
+
+static void x25_defer_kill_by_neigh(struct x25_neigh *nb)
+{
+	x25_neigh_hold(nb);
+	dev_hold(nb->dev);
+	if (!queue_work(x25_wq, &nb->kill_work)) {
+		dev_put(nb->dev);
+		x25_neigh_put(nb);
+	}
+}
+
 /*
  *	This handles all restart and diagnostic frames.
  */
@@ -82,7 +103,7 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
 			break;
 		case X25_LINK_STATE_3:
 			/* clear existing virtual calls */
-			x25_kill_by_neigh(nb);
+			x25_defer_kill_by_neigh(nb);
 
 			x25_transmit_restart_confirmation(nb);
 			break;
@@ -97,7 +118,7 @@ void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
 			break;
 		case X25_LINK_STATE_3:
 			/* clear existing virtual calls */
-			x25_kill_by_neigh(nb);
+			x25_defer_kill_by_neigh(nb);
 
 			x25_transmit_restart_request(nb);
 			nb->state = X25_LINK_STATE_2;
@@ -254,7 +275,7 @@ void x25_link_terminated(struct x25_neigh *nb)
 	x25_stop_t20timer(nb);
 
 	/* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
-	x25_kill_by_neigh(nb);
+	x25_defer_kill_by_neigh(nb);
 }
 
 /*
@@ -269,6 +290,7 @@ void x25_link_device_up(struct net_device *dev)
 
 	skb_queue_head_init(&nb->queue);
 	timer_setup(&nb->t20timer, x25_t20timer_expiry, 0);
+	INIT_WORK(&nb->kill_work, x25_kill_by_neigh_work);
 
 	dev_hold(dev);
 	nb->dev      = dev;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3] x25: defer socket cleanup on link termination to process context
  2026-11-23  1:40       ` netdev-bot+sashiko
  2026-11-23  3:47         ` Shuai Liu
@ 2026-11-23  3:48         ` Shuai Liu
  1 sibling, 0 replies; 11+ messages in thread
From: Shuai Liu @ 2026-11-23  3:48 UTC (permalink / raw)
  To: netdev
  Cc: ms, linux-x25, netdev-bot+sashiko, syzbot+300375dfd44665f063a6,
	847807867

Hi Sashiko, Martin,

Thanks for the review.

On the [Low] (NETDEV_CHANGE flush): fixed in v3. The carrier-loss path
now flushes kill_work right after x25_link_terminated(), so both
process-context notifier events (NETDEV_DOWN and NETDEV_CHANGE) keep
the fully synchronous socket cleanup they had before the patch.

On the [Medium] (restart sequence vs stale virtual calls): I looked at
each scenario and believe the remaining window is not actionable, so I
kept the softirq deferral:

1. "A reused LCI is matched by a stale socket" cannot happen in the
   x25_link_terminated() path: that path is entered when LAPB reports
   the link down (X25_IFACE_DISCONNECT), and any further peer frame
   (including a CALL_REQUEST on a reused LCI) requires LAPB to be
   re-established first, i.e. a SABM/UA exchange at least one RTT away.
   The deferred work runs within microseconds of the event, long before
   the peer can deliver any new frame.

2. In the x25_link_control() restart paths the link is still up, so
   frames that were already in flight when the restart was processed can
   land in the window. That is precisely the out-of-order case the
   existing "Out of order: clear existing virtual calls (X.25 03/93
   4.6.3)" comment acknowledges: those in-flight frames belong to calls
   that must be cleared anyway. The workqueue item runs microseconds
   after the restart handling, so a peer would have to send a new
   CALL_REQUEST before it has even seen the restart complete for it to
   matter.

3. Detaching the sockets synchronously in softirq would require writing
   x25->neighbour / lci / state from BH without lock_sock(); those
   fields are lock_sock-protected, so that trades a microseconds-wide
   theoretical window for a real data race. Splitting the sleeping part
   out has the same problem.

4. "Valid new calls torn down by the deferred worker": incoming calls
   need the LAPB link up (point 1), and an outgoing call would need
   userspace connect() to land inside the same microseconds window.

pw-bot: cr

Changes since v2:
  - Flush kill_work in NETDEV_CHANGE as well, restoring synchronous
    socket cleanup for both process-context notifier events (Sashiko)

v3: https://lore.kernel.org/netdev/20261123034748.1269213-1-847807867@qq.com/


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH net] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh()
  2026-10-01 15:01 [PATCH net] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh() Nguyen Ngoc Thang
  2026-10-05 15:20 ` netdev-bot+sashiko
@ 2026-11-23  7:57 ` Martin Schiller
  2026-11-23  8:03   ` [PATCH] x25: defer socket cleanup on link termination to process context Martin Schiller
  1 sibling, 1 reply; 11+ messages in thread
From: Martin Schiller @ 2026-11-23  7:57 UTC (permalink / raw)
  To: Nguyen Ngoc Thang
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, linux-x25, netdev, linux-kernel, Duoming Zhou,
	Lin Ma, syzbot+9faa82ae2e94c5c7d2ef

Hi Thang,

On Thu, 01 Oct 2026 17:01:18 +0200, Nguyen Ngoc Thang wrote:
> Take the socket spinlock instead, the same way the x25 timers and 
> receive
> path already do. If the socket is not owned by user, disconnect it 
> right
> away. If it is, mark it with X25_KILL_FLAG and let a new release_cb do
> the disconnect when the owner releases the socket.

This is the approach I'd like to take, rather than deferring the whole
cleanup to a workqueue as proposed since. syzbot still hits the bug,
now also as https://syzkaller.appspot.com/bug?extid=300375dfd44665f063a6

The two new findings of the AI review hold up in practice. I tested
with two lapbether devices on a veth pair and a sender stalled in
x25_sendmsg() via userfaultfd, so that the socket is owned when the
link event arrives:

- after a RESTART, a CALL REQUEST on the old LCI is queued to the
   flagged socket's backlog and never reaches the listener;
- the flagged socket still transmits: a DATA packet on its old LCI
   after the restart, and after a LAPB disconnect it brings the link
   back up (SABM) before release_cb runs.

Skipping flagged sockets in __x25_find_socket() and returning early
from x25_kick() and x25_write_internal() for them fixes both. Those two
functions cover everything a socket transmits, and the deferred
x25_disconnect() only changes local state. Clearing the flag in
x25_make_new() takes care of the inheritance issue.

The x25_sendmsg()/x25_output() issue is pre-existing and independent;
I have a separate patch for it.

I have these changes on top of your patch, together with the separate
fix, and the series passes the tests above with lockdep and
DEBUG_ATOMIC_SLEEP. Unless you'd like to send the v2 yourself, I'll
post it in a few days, with you as the author of the main patch.

pw-bot: changes-requested

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] x25: defer socket cleanup on link termination to process context
  2026-11-23  7:57 ` Martin Schiller
@ 2026-11-23  8:03   ` Martin Schiller
  0 siblings, 0 replies; 11+ messages in thread
From: Martin Schiller @ 2026-11-23  8:03 UTC (permalink / raw)
  To: Shuai Liu
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Nguyen Ngoc Thang, linux-x25, netdev, linux-kernel,
	syzbot+300375dfd44665f063a6

Hi Shuai,

On Mon, 23 Nov 2026 04:47:48 +0100, Shuai Liu wrote:
> Changes in v3:
>   - Flush kill_work in NETDEV_CHANGE as well, restoring synchronous
>     socket cleanup for both process-context notifier events (Sashiko)

The NETDEV_CHANGE flush is fine, but I don't agree that the medium
issue from the review is not actionable, and I don't want to take the
workqueue approach.

In your reply to the review you wrote:
> The deferred work runs within microseconds of the event, long before
> the peer can deliver any new frame.

That only holds while no socket on the link is owned by a task. The
worker sleeps in lock_sock() until the owner releases the socket, which
is exactly the case in which the current code sleeps in softirq, and an
owner holds the socket for as long as its send buffer takes to fault
in. Until then, every call on the link keeps its LCI binding.

I tested v3 with two lapbether devices on a veth pair and a sender
stalled in x25_sendmsg() via userfaultfd, so that its socket is owned
when the link event arrives:

- after a LAPB disconnect, the stalled call itself brings the link back
   up (SABM) before it is cleared, so the peer can reach us again while
   the old calls are still bound;
- after a RESTART, a CALL REQUEST on the old LCI, sent two seconds after
   the restart was confirmed, never reaches the listener, and the old
   call sends a DATA packet on that LCI.

Neither involves frames that were in flight before the restart.

> Detaching the sockets synchronously in softirq would require writing
> x25->neighbour / lci / state from BH without lock_sock()

It doesn't. Thang's patch from October [1] uses the pattern that
x25_receive_data() and the x25 timers already use: under
bh_lock_sock(), a socket that is not owned is cleared right away, and an
owned one only gets an atomic flag and is cleared by its owner from
release_cb. Its v2 also skips flagged sockets in __x25_find_socket() and
keeps them from transmitting, which fixes both cases above in the same
test. I'll post it in the next days.

Also, please post new versions as a new thread, with the version in the
subject of the patch itself, not as a reply to the previous posting.

[1] 
https://lore.kernel.org/all/20261001150118.128470-1-ngocthang2710.1999@gmail.com/

pw-bot: changes-requested

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-23  8:04 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-01 15:01 [PATCH net] net/x25: don't call lock_sock() from softirq in x25_kill_by_neigh() Nguyen Ngoc Thang
2026-10-05 15:20 ` netdev-bot+sashiko
2026-11-23  7:57 ` Martin Schiller
2026-11-23  8:03   ` [PATCH] x25: defer socket cleanup on link termination to process context Martin Schiller
  -- strict thread matches above, loose matches on Subject: below --
2026-10-11  0:03 [syzbot] [x25?] inconsistent lock state in x25_kill_by_neigh (2) syzbot
2026-11-16  0:34 ` [PATCH] x25: defer socket cleanup on link termination to process context Shuai Liu
2026-11-18 18:35   ` netdev-bot+sashiko
2026-11-19  1:24     ` Shuai Liu
2026-11-23  1:40       ` netdev-bot+sashiko
2026-11-23  3:47         ` Shuai Liu
2026-11-23  3:48         ` [PATCH v3] " Shuai Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox