Skip to contentSkip to conversation

dlm: various fixes

PATCH RESEND In review Alexander Aring 2 participants 11 messages Nov 20 – Nov 23 Distributed lock manager (DLM)

lore
Share & tools
lore

Alexander Aring

author maintainer
#0 lore raw

v2 cover dlm: various fixes

Hi David,

This patch series contains mostly AI-assisted fixes and cleanups across the DLM subsystem:

- Add missing length checks for oversized inner DLM messages, this prevents potential buffer overruns or malformed packet handling.

- Enforce RSB assignment before using LKBs, this ensures LKBs cannot exist in lkbxa without an associated RSB set. It resolves an issue with concurrent DLM user requests.

- Prevent DLM_ASSERT() on canceled pending requests, this fixes an assertion failure triggered when a initial request is canceled and recovery is triggered during this state.

- Fix lockspace node memory leak during release, this resolves a leak involving delayed member removal during active lockspace releasing members. Active releasing members will not run recovery so the delayed cleanup of removed of members will never occur.

- Fixes a case where the reconnect mechanism gets stuck if a socket error occurs while the socket send buffer is full. This was found by the netdev-bot (sashik AI bot) [3]. I tested it with tcpkill again, and everything still seems to run fine. To test this specific case, I used netem to slow down networking so the socket buffer would fill up while running tcpkill to trigger socket errors.

Ensuring that LKBs always have an RSB assigned should also reduce the likelihood of subtle bugs elsewhere in the codebase where an RSB presence check might be missing.

There is a pending sysfs fix [0] that conflicts with [1]. The fix for
[1] does not go through the dlm tree so [0] is delayed until [1] is available.

- Alex

[0] https://lore.kernel.org/gfs2/[email protected]/
[1] https://lore.kernel.org/gfs2/[email protected]/
[2] https://lore.kernel.org/gfs2/[email protected]/

changes since v2:
- add patch to fix the socket hang on send buffer limitation
- drop the Assisted-by tags

Alexander Aring (2):
dlm: fix socket hang on send buffer limitation with socket error
dlm: fix memory leak on active leaving member

Ameer Hamza (1):
dlm: don't set RSB_MASTER_UNCERTAIN on rsb with no master

Jérémy Jean (1):
dlm: reject wrapped messages with oversized inner lengths

Yogesh Gaur (1):
dlm: don't publish a lkb in ls_lkbxa before it has an rsb

 fs/dlm/config.c   |  9 +++++
 fs/dlm/lock.c     | 98 ++++++++++++++++++++++++++++++++---------------
 fs/dlm/lowcomms.c | 47 ++++++++++++++++-------
 fs/dlm/midcomms.c |  8 ++++
 4 files changed, 118 insertions(+), 44 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH RESEND v2 1/5 dlm: fix socket hang on send buffer limitation with socket error

As Sashiko AI bot mentioned [0], a socket connection can get stuck when the send buffer limitation is reached and a socket error simultaneously occurs.

This issue can be reproduced with the following setup:
1. Set sk_sndbuf to SOCK_MIN_SNDBUF.
2. Slow down DLM connections using netem.
3. Use tcpkill to randomly send TCP resets to DLM connections.

I instrumented debug printouts to confirm that the sk_write_space() notifier callbacks were being executed, using step 3 to trigger random socket errors.

With these changes, I can no longer reproduce the hang.

Changes included:
- Removed sk_write_pending counting, as this should not be modified at the socket application layer (or is at least unnecessary).
- Moved clearing the CF_SEND_PENDING bit—which allows re-queuing swork (send worker for sendmsg())—to the sk_write_space() callback, since this callback notifies us that the underlying socket is no longer constrained by its send buffer.
- Handled the race condition between sendmsg() and evaluating SOCK_NOSPACE after sendmsg(), where sk_write_space() could be called in between, using CF_APP_LIMITED:
- In sk_write_space(), queue swork again if CF_APP_LIMITED is set.
- If CF_APP_LIMITED is not set, do nothing as send_to_sock() will handle it, confirming the race occurred.
- Introduced new handling in lowcomms_error_report() when a socket error occurs during send buffer limitation. If CF_APP_LIMITED is set, swork will be re-queued, which will fail and trigger a reconnect.
- Added various comments explaining the interaction with CF_APP_LIMITED.

[0] https://lore.kernel.org/netdev/[email protected]/

---
 fs/dlm/lowcomms.c | 47 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 14 deletions(-)
fs/dlm/lowcomms.c +33 -14
index 3929b164dc0d..6c972f1b412c 100644
@@ -521,12 +521,20 @@ static void lowcomms_write_space(struct sock *sk)
 
 	sk_clear_nospace(sk);
 
-	spin_lock_bh(&con->writequeue_lock);
-	if (test_and_clear_bit(CF_APP_LIMITED, &con->flags))
-		con->sock->sk->sk_write_pending--;
-
-	lowcomms_queue_swork(con);
-	spin_unlock_bh(&con->writequeue_lock);
+	if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
+		/* signal to send again by clearing
+		 * CF_SEND_PENDING and queue swork.
+		 */
+		spin_lock_bh(&con->writequeue_lock);
+		clear_bit(CF_SEND_PENDING, &con->flags);
+		lowcomms_queue_swork(con);
+		spin_unlock_bh(&con->writequeue_lock);
+	} else {
+		/* CF_APP_LIMITED is cleared, so send_to_sock() will
+		 * simply reschedule work without hitting the
+		 * CF_APP_LIMITED path.
+		 */
+	}
 }
 
 static void lowcomms_state_change(struct sock *sk)
@@ -623,6 +631,20 @@ static void lowcomms_error_report(struct sock *sk)
 		break;
 	}
 
+	/* if waiting on sk_write_space() and an sk_err occurs, the callback
+	 * won't fire. Clear CF_SEND_PENDING so resend tasks can re-queue
+	 * swork, triggering a sendmsg() failure to initiate reconnection.
+	 */
+	spin_lock_bh(&con->writequeue_lock);
+	clear_bit(CF_SEND_PENDING, &con->flags);
+	/* dlm_midcomms_unack_msg_resend() does not always
+	 * trigger lowcomms_queue_swork() as it tries to
+	 * avoid to put pending messages into the lowcomms
+	 * sending buffer. Force it here again.
+	 */
+	lowcomms_queue_swork(con);
+	spin_unlock_bh(&con->writequeue_lock);
+
 	dlm_midcomms_unack_msg_resend(con->nodeid);
 
 	listen_sock.sk_error_report(sk);
@@ -1391,22 +1413,19 @@ static int send_to_sock(struct connection *con)
 		spin_lock_bh(&con->writequeue_lock);
 		if (test_bit(SOCK_NOSPACE, &con->sock->flags) &&
 		    !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
-			/* Notify TCP that we're limited by the
-			 * application window size.
-			 */
-			sk_set_nospace(con->sock->sk);
-			con->sock->sk->sk_write_pending++;
-
-			clear_bit(CF_SEND_PENDING, &con->flags);
 			spin_unlock_bh(&con->writequeue_lock);
 			release_sock(con->sock->sk);
 
-			/* wait for write_space() event */
+			/* wait for sk_write_space() event */
 			return DLM_IO_END;
 		}
 		spin_unlock_bh(&con->writequeue_lock);
 		release_sock(con->sock->sk);
 
+		/* the sk_write_space() came in between sock_sendmsg()
+		 * and check on SOCK_NOSPACE and the socket became
+		 * writeable again so just resched swork.
+		 */
 		return DLM_IO_RESCHED;
 	} else if (ret < 0) {
 		return ret;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCHv2 RESEND v7.4-rc3 1/5] dlm: fix socket hang on send buffer limitation with socket error

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] dlm: missing CF_APP_LIMITED clear in lowcomms_error_report() hangs send queue - [High] dlm…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] dlm: missing CF_APP_LIMITED clear in lowcomms_error_report() hangs send queue
- [High] dlm: unconditional swork queue in lowcomms_error_report() breaks invariants
- [Medium] dlm: race in send_to_sock() sets CF_APP_LIMITED after socket error
- [Low] dlm: missing Fixes trailer in commit message
--

commit 796bee147607d968d752eb2e6e9611c521fe108f
Author: Alexander Aring <[email protected]>

Quoted 35 lines

dlm: fix socket hang on send buffer limitation with socket error

As Sashiko AI bot mentioned [0], a socket connection can get stuck when the send buffer limitation is reached and a socket error simultaneously occurs.

This issue can be reproduced with the following setup:
1. Set sk_sndbuf to SOCK_MIN_SNDBUF.
2. Slow down DLM connections using netem.
3. Use tcpkill to randomly send TCP resets to DLM connections.

I instrumented debug printouts to confirm that the sk_write_space() notifier callbacks were being executed, using step 3 to trigger random socket errors.

With these changes, I can no longer reproduce the hang.

Changes included:
- Removed sk_write_pending counting, as this should not be modified at the socket application layer (or is at least unnecessary).
- Moved clearing the CF_SEND_PENDING bit—which allows re-queuing swork (send worker for sendmsg())—to the sk_write_space() callback, since this callback notifies us that the underlying socket is no longer constrained by its send buffer.
- Handled the race condition between sendmsg() and evaluating SOCK_NOSPACE after sendmsg(), where sk_write_space() could be called in between, using CF_APP_LIMITED:
- In sk_write_space(), queue swork again if CF_APP_LIMITED is set.
- If CF_APP_LIMITED is not set, do nothing as send_to_sock() will handle it, confirming the race occurred.
- Introduced new handling in lowcomms_error_report() when a socket error occurs during send buffer limitation. If CF_APP_LIMITED is set, swork will be re-queued, which will fail and trigger a reconnect.
- Added various comments explaining the interaction with CF_APP_LIMITED.

Quoted 3 lines

[0] https://lore.kernel.org/netdev/[email protected]/

[Severity: Low]
Should this bug fix include a Fixes: tag for the commit that gated lowcomms_queue_swork() on CF_APP_LIMITED?

Fixes dbb751ffab0b ("fs: dlm: parallelize lowcomms socket handling")
Quoted 4 lines
fs/dlm/lowcomms.c +0 -0
index 3929b164dc0d4..6c972f1b412ca 100644

[ ... ]

Quoted 11 lines
fs/dlm/lowcomms.c +7 -0
@@ -623,6 +631,20 @@ static void lowcomms_error_report(struct sock *sk)
 		break;
 	}
 
+	/* if waiting on sk_write_space() and an sk_err occurs, the callback
+	 * won't fire. Clear CF_SEND_PENDING so resend tasks can re-queue
+	 * swork, triggering a sendmsg() failure to initiate reconnection.
+	 */
+	spin_lock_bh(&con->writequeue_lock);
+	clear_bit(CF_SEND_PENDING, &con->flags);
+	/* dlm_midcomms_unack_msg_resend() does not always

fs/dlm/lowcomms.c static void lowcomms_error_report(struct sock *sk) · line 650

Quoted 10 lines
fs/dlm/lowcomms.c +7 -0
+	 * trigger lowcomms_queue_swork() as it tries to
+	 * avoid to put pending messages into the lowcomms
+	 * sending buffer. Force it here again.
+	 */
+	lowcomms_queue_swork(con);
+	spin_unlock_bh(&con->writequeue_lock);
+
 	dlm_midcomms_unack_msg_resend(con->nodeid);
 
 	listen_sock.sk_error_report(sk);

[Severity: High]
Does lowcomms_error_report() also need to clear CF_APP_LIMITED before calling lowcomms_queue_swork(con)?

lowcomms_queue_swork() refuses to schedule con->swork while CF_APP_LIMITED is set:

fs/dlm/lowcomms.c:lowcomms_queue_swork() {
	if (!test_bit(CF_IO_STOP, &con->flags) &&
	    !test_bit(CF_APP_LIMITED, &con->flags) &&
	    !test_and_set_bit(CF_SEND_PENDING, &con->flags))
		queue_work(io_workqueue, &con->swork);
}

When a socket error arrives while CF_APP_LIMITED is set, sk_write_space() will not fire, so leaving CF_APP_LIMITED set here causes every lowcomms_queue_swork(con) call to be a no-op and leaves the connection permanently hung.

[Severity: High]
Should lowcomms_error_report() only clear CF_SEND_PENDING and queue con->swork when test_and_clear_bit(CF_APP_LIMITED, &con->flags) succeeds, matching lowcomms_write_space()?

Accepting a crossover connection in accept_from_sock() attaches the socket to othercon (which has CF_IS_OTHERCON set) via add_sock(), installing lowcomms_error_report() as sk_error_report. Because othercon is receive-only, unconditionally queuing con->swork on a socket error schedules process_send_sockets() on othercon, triggering WARN_ON_ONCE(test_bit(CF_IS_OTHERCON, &con->flags)) and entering an infinite reconnect loop calling dlm_connect(othercon) once othercon->sock is closed.

On a primary connection where CF_APP_LIMITED is not set, unconditionally clearing CF_SEND_PENDING also breaks swork serialization on the unbound io_workqueue by allowing a second con->swork to be queued while process_send_sockets() is already running.

Quoted 18 lines
fs/dlm/lowcomms.c +1 -8
@@ -1391,22 +1413,19 @@ static int send_to_sock(struct connection *con)
 		spin_lock_bh(&con->writequeue_lock);
 		if (test_bit(SOCK_NOSPACE, &con->sock->flags) &&
 		    !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
-			/* Notify TCP that we're limited by the
-			 * application window size.
-			 */
-			sk_set_nospace(con->sock->sk);
-			con->sock->sk->sk_write_pending++;
-
-			clear_bit(CF_SEND_PENDING, &con->flags);
 			spin_unlock_bh(&con->writequeue_lock);
 			release_sock(con->sock->sk);
 
-			/* wait for write_space() event */
+			/* wait for sk_write_space() event */
 			return DLM_IO_END;
 		}

fs/dlm/lowcomms.c static int send_to_sock(struct connection *con) · line 1431

Quoted 10 lines
fs/dlm/lowcomms.c +4 -0
 		spin_unlock_bh(&con->writequeue_lock);
 		release_sock(con->sock->sk);
 
+		/* the sk_write_space() came in between sock_sendmsg()
+		 * and check on SOCK_NOSPACE and the socket became
+		 * writeable again so just resched swork.
+		 */
 		return DLM_IO_RESCHED;
 	} else if (ret < 0) {
 		return ret;

[Severity: Medium]
Can send_to_sock() still set CF_APP_LIMITED and hang after a socket error if the error arrives before lock_sock()?

kworker <softirq>
send_to_sock()
sock_sendmsg()

    ...
      sk_set_nospace()
      return -EAGAIN
                                       tcp_v4_rcv() (TCP RST)
                                         tcp_reset()
                                           tcp_done_with_error()
                                             lowcomms_error_report()

lock_sock(sk)
spin_lock_bh(&con->writequeue_lock)
test_bit(SOCK_NOSPACE, ...)
!test_and_set_bit(CF_APP_LIMITED, ...)
return DLM_IO_END <-- waits forever

Unlike lowcomms_write_space(), neither tcp_done_with_error() nor lowcomms_error_report() clears SOCK_NOSPACE. When send_to_sock() then checks SOCK_NOSPACE without checking sk->sk_err, it sets CF_APP_LIMITED after lowcomms_error_report() has already finished and returns DLM_IO_END on a dead socket that will never fire sk_write_space() or sk_error_report() again. Checking !con->sock->sk->sk_err under lock_sock() (or clearing SOCK_NOSPACE in lowcomms_error_report()) would avoid entering CF_APP_LIMITED on an errored socket.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

Re: [PATCHv2 RESEND v7.4-rc3 1/5] dlm: fix socket hang on send buffer limitation with socket error

Hi,

Quoted 52 lines · On Fri, Nov 20, 2026 at 3:40 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] dlm: missing CF_APP_LIMITED clear in lowcomms_error_report() hangs send queue
- [High] dlm: unconditional swork queue in lowcomms_error_report() breaks invariants
- [Medium] dlm: race in send_to_sock() sets CF_APP_LIMITED after socket error
- [Low] dlm: missing Fixes trailer in commit message
--

commit 796bee147607d968d752eb2e6e9611c521fe108f
Author: Alexander Aring <[email protected]>

Quoted 37 lines

dlm: fix socket hang on send buffer limitation with socket error

As Sashiko AI bot mentioned [0], a socket connection can get stuck when the send buffer limitation is reached and a socket error simultaneously occurs.

This issue can be reproduced with the following setup:
1. Set sk_sndbuf to SOCK_MIN_SNDBUF.
2. Slow down DLM connections using netem.
3. Use tcpkill to randomly send TCP resets to DLM connections.

I instrumented debug printouts to confirm that the sk_write_space() notifier callbacks were being executed, using step 3 to trigger random socket errors.

With these changes, I can no longer reproduce the hang.

Changes included:
- Removed sk_write_pending counting, as this should not be modified at the socket application layer (or is at least unnecessary).
- Moved clearing the CF_SEND_PENDING bit—which allows re-queuing swork (send worker for sendmsg())—to the sk_write_space() callback, since this callback notifies us that the underlying socket is no longer constrained by its send buffer.
- Handled the race condition between sendmsg() and evaluating SOCK_NOSPACE after sendmsg(), where sk_write_space() could be called in between, using CF_APP_LIMITED:
- In sk_write_space(), queue swork again if CF_APP_LIMITED is set.
- If CF_APP_LIMITED is not set, do nothing as send_to_sock() will handle it, confirming the race occurred.
- Introduced new handling in lowcomms_error_report() when a socket error occurs during send buffer limitation. If CF_APP_LIMITED is set, swork will be re-queued, which will fail and trigger a reconnect.
- Added various comments explaining the interaction with CF_APP_LIMITED.

[0] https://lore.kernel.org/netdev/[email protected]/

Quoted 8 lines
Quoted 1 line

[Severity: Low]
Should this bug fix include a Fixes: tag for the commit that gated lowcomms_queue_swork() on CF_APP_LIMITED?

Fixes dbb751ffab0b ("fs: dlm: parallelize lowcomms socket handling")

This is not the right fixes tag, that a socket error comes in when the sending buffer is limited was not handled in the commit that was introducing CF_APP_LIMITED b36930dd508e ("dlm: Handle application limited situations properly.") so far I can see.

Quoted 43 lines
Quoted 4 lines
fs/dlm/lowcomms.c +0 -0
index 3929b164dc0d4..6c972f1b412ca 100644

[ ... ]

Quoted 11 lines
fs/dlm/lowcomms.c +7 -0
@@ -623,6 +631,20 @@ static void lowcomms_error_report(struct sock *sk)
              break;
      }
 
+     /* if waiting on sk_write_space() and an sk_err occurs, the callback
+      * won't fire. Clear CF_SEND_PENDING so resend tasks can re-queue
+      * swork, triggering a sendmsg() failure to initiate reconnection.
+      */
+     spin_lock_bh(&con->writequeue_lock);
+     clear_bit(CF_SEND_PENDING, &con->flags);
+     /* dlm_midcomms_unack_msg_resend() does not always

fs/dlm/lowcomms.c static void lowcomms_error_report(struct sock *sk) · line 650

Quoted 10 lines
fs/dlm/lowcomms.c +7 -0
+      * trigger lowcomms_queue_swork() as it tries to
+      * avoid to put pending messages into the lowcomms
+      * sending buffer. Force it here again.
+      */
+     lowcomms_queue_swork(con);
+     spin_unlock_bh(&con->writequeue_lock);
+
      dlm_midcomms_unack_msg_resend(con->nodeid);
 
      listen_sock.sk_error_report(sk);

[Severity: High]
Does lowcomms_error_report() also need to clear CF_APP_LIMITED before calling lowcomms_queue_swork(con)?

lowcomms_queue_swork() refuses to schedule con->swork while CF_APP_LIMITED is set:

fs/dlm/lowcomms.c:lowcomms_queue_swork() {
        if (!test_bit(CF_IO_STOP, &con->flags) &&
            !test_bit(CF_APP_LIMITED, &con->flags) &&
            !test_and_set_bit(CF_SEND_PENDING, &con->flags))
                queue_work(io_workqueue, &con->swork);
}
Quoted 4 lines

When a socket error arrives while CF_APP_LIMITED is set, sk_write_space() will not fire, so leaving CF_APP_LIMITED set here causes every lowcomms_queue_swork(con) call to be a no-op and leaves the connection permanently hung.

Yes that makes sense and I had this in a previous version [0]. However I wanted to drop the "test_bit()" check.

Quoted 15 lines

[Severity: High]
Should lowcomms_error_report() only clear CF_SEND_PENDING and queue con->swork when test_and_clear_bit(CF_APP_LIMITED, &con->flags) succeeds, matching lowcomms_write_space()?

Accepting a crossover connection in accept_from_sock() attaches the socket to othercon (which has CF_IS_OTHERCON set) via add_sock(), installing lowcomms_error_report() as sk_error_report. Because othercon is receive-only, unconditionally queuing con->swork on a socket error schedules process_send_sockets() on othercon, triggering WARN_ON_ONCE(test_bit(CF_IS_OTHERCON, &con->flags)) and entering an infinite reconnect loop calling dlm_connect(othercon) once othercon->sock is closed.

Quoted 4 lines

On a primary connection where CF_APP_LIMITED is not set, unconditionally clearing CF_SEND_PENDING also breaks swork serialization on the unbound io_workqueue by allowing a second con->swork to be queued while process_send_sockets() is already running.

From what I see in the context of the othercon, that second connection "othercon" will never queue swork as the primary connection is used for connection "lookup". The "othercon" logic is a mess and we should have removed it long ago, as I am sure it causes other problems. I added a TODO a long time ago regarding how an othercon could be introduced using a global DLM pre-known rule where a higher nodeid makes the connection and the other node waits for accept (though the other node can still request a reconnection). [1]

I experimented with that, but I was never able to create a backward-compatible mode that still works. I think if we fix this, we should add an option to opt-in and later make it the default.

- Alex

[0] https://lore.kernel.org/gfs2/[email protected]/
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/dlm/lowcomms.c?h=v7.4-rc4#n91

author maintainer
#4 lore raw

Re: [PATCHv2 RESEND v7.4-rc3 1/5] dlm: fix socket hang on send buffer limitation with socket error

Hi,

On Mon, Nov 23, 2026 at 3:38 PM Alexander Aring <[email protected]> wrote:
...

Quoted 7 lines

From what I see in the context of the othercon, that second connection "othercon" will never queue swork as the primary connection is used for connection "lookup". The "othercon" logic is a mess and we should have removed it long ago, as I am sure it causes other problems. I added a TODO a long time ago regarding how an othercon could be introduced using a global DLM pre-known rule where a higher nodeid

* how the othercon could be removed using...

- Alex

author maintainer
#5 lore raw

PATCH RESEND v2 2/5 dlm: reject wrapped messages with oversized inner lengths

In reply to Alexander Aring

From: Jérémy Jean <[email protected]>

Reject DLM_OPTS packets whose wrapped inner message length exceeds the bytes remaining inside the validated outer packet. This keeps malformed wrapped RCOM replies from reaching downstream length-driven copies.

Fixes 489d8e559c65 ("fs: dlm: add reliable connection if reconnect")
---
 fs/dlm/midcomms.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/dlm/midcomms.c +8 -0
index 045431524494..488a58f2c00b 100644
@@ -652,6 +652,14 @@ static int dlm_opts_check_msglen(const union dlm_packet *p, uint16_t msglen,
 		return -1;
 	}
 
+	p = (const union dlm_packet *)((const unsigned char *)p->opts.o_opts +
+				       le16_to_cpu(p->opts.o_optlen));
+	if (le16_to_cpu(p->header.h_length) > len) {
+		log_print("inner msg too large: %u > %d, will skip this message from node %d",
+			  le16_to_cpu(p->header.h_length), len, nodeid);
+		return -1;
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.43.0
author maintainer
#6 lore raw

PATCH RESEND v2 3/5 dlm: don't publish a lkb in ls_lkbxa before it has an rsb

In reply to Alexander Aring

From: Yogesh Gaur <[email protected]>

_create_lkb() puts a new lkb into ls_lkbxa straight away, which is also what assigns its lkb_id, but the lkb only gets an rsb later, when request_lock(), receive_request(), dlm_recover_master_copy() or dlm_debug_add_lkb() call attach_lkb(). In between, find_lkb() hands the lkb out with lkb_resource still NULL, and its callers go straight for the rsb:

	r = lkb->lkb_resource;

	hold_rsb(r);
	lock_rsb(r);

For the userspace API the lkid is simply whatever was written to the misc device, so a lkid can be aimed at a lkb that another thread is still building, and hold_rsb() reads res_flags off NULL:

  BUG: KASAN: null-ptr-deref in rsb_flag fs/dlm/dlm_internal.h:386 [inline]
  BUG: KASAN: null-ptr-deref in hold_rsb fs/dlm/lock.c:334 [inline]
  BUG: KASAN: null-ptr-deref in unlock_lock fs/dlm/lock.c:3333 [inline]
  BUG: KASAN: null-ptr-deref in dlm_user_unlock+0x2ab/0x690 fs/dlm/lock.c:5956
  Read of size 8 at addr 0000000000000050 by task syz.3.570/7893
   rsb_flag fs/dlm/dlm_internal.h:386 [inline]
   hold_rsb fs/dlm/lock.c:334 [inline]
   unlock_lock fs/dlm/lock.c:3333 [inline]
   dlm_user_unlock+0x2ab/0x690 fs/dlm/lock.c:5956
   device_user_unlock+0x1ca/0x260 fs/dlm/user.c:321

Make it impossible for ls_lkbxa to hold a lkb without an rsb, rather than filtering such lkbs in find_lkb() or reserving a NULL entry that every walker would have to know about. create_lkb() no longer touches ls_lkbxa; attach_lkb() sets lkb_resource and only then allocates the lkb_id and stores the lkb, so the lkb becomes visible already attached. dlm_debug_add_lkb() presets the id it wants and attach_lkb() allocates exactly that one.

attach_lkb() can now fail, and each of its four callers releases the lkb on the error path it already has for a failure just before the attach. On failure attach_lkb() also clears lkb_id, so that __put_lkb() cannot erase another lkb's entry when a requested id was busy. All four callers hold the rsb lock, and nothing takes an rsb lock under ls_lkbxa_lock, so taking it there adds no new lock ordering.

As a side effect an lkb has no lkb_id until it is attached, so trace_dlm_lock_start() and the validate_lock_args() and receive_rcom_lock_args() error messages report 0 for a new lock.

The unchecked r = lkb->lkb_resource goes back to the original DLM import, but an untrusted lkid only became possible once the userspace device interface was added, so that is the tag below.

---
 fs/dlm/lock.c | 90 +++++++++++++++++++++++++++++++++++----------------
 1 file changed, 62 insertions(+), 28 deletions(-)
fs/dlm/lock.c +62 -28
index c715e171bab7..3b3e15157f4c 100644
@@ -1488,10 +1488,38 @@ void free_inactive_rsb(struct dlm_rsb *r)
 /* Attaching/detaching lkb's from rsb's is for rsb reference counting.
    The rsb must exist as long as any lkb's for it do. */
 
-static void attach_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
+/*
+ * An lkb is only put into ls_lkbxa here, once it has an rsb, so
+ * find_lkb() never returns an lkb without one.  The lkb_id is assigned
+ * here too; dlm_debug_add_lkb() presets the one it wants.
+ */
+static int attach_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
 {
+	struct dlm_ls *ls = r->res_ls;
+	struct xa_limit limit = XA_LIMIT(1, U32_MAX);
+	int rv;
+
+	if (lkb->lkb_id)
+		limit = XA_LIMIT(lkb->lkb_id, lkb->lkb_id);
+
 	hold_rsb(r);
 	lkb->lkb_resource = r;
+
+	write_lock_bh(&ls->ls_lkbxa_lock);
+	rv = xa_alloc(&ls->ls_lkbxa, &lkb->lkb_id, lkb, limit, GFP_ATOMIC);
+	write_unlock_bh(&ls->ls_lkbxa_lock);
+
+	if (rv < 0) {
+		log_error(ls, "%s xa error %d", __func__, rv);
+		/* the caller still holds its own rsb reference */
+		lkb->lkb_resource = NULL;
+		put_rsb(r);
+		/* don't let __put_lkb() erase another lkb's id */
+		lkb->lkb_id = 0;
+		return rv;
+	}
+
+	return 0;
 }
 
 static void detach_lkb(struct dlm_lkb *lkb)
@@ -1502,15 +1530,9 @@ static void detach_lkb(struct dlm_lkb *lkb)
 	}
 }
 
-static int _create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret,
-		       unsigned long start, unsigned long end)
+static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
 {
-	struct xa_limit limit;
 	struct dlm_lkb *lkb;
-	int rv;
-
-	limit.max = end;
-	limit.min = start;
 
 	lkb = dlm_allocate_lkb();
 	if (!lkb)
@@ -1525,25 +1547,10 @@ static int _create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret,
 	INIT_LIST_HEAD(&lkb->lkb_ownqueue);
 	INIT_LIST_HEAD(&lkb->lkb_rsb_lookup);
 
-	write_lock_bh(&ls->ls_lkbxa_lock);
-	rv = xa_alloc(&ls->ls_lkbxa, &lkb->lkb_id, lkb, limit, GFP_ATOMIC);
-	write_unlock_bh(&ls->ls_lkbxa_lock);
-
-	if (rv < 0) {
-		log_error(ls, "create_lkb xa error %d", rv);
-		dlm_free_lkb(lkb);
-		return rv;
-	}
-
 	*lkb_ret = lkb;
 	return 0;
 }
 
-static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
-{
-	return _create_lkb(ls, lkb_ret, 1, ULONG_MAX);
-}
-
 static int find_lkb(struct dlm_ls *ls, uint32_t lkid, struct dlm_lkb **lkb_ret)
 {
 	struct dlm_lkb *lkb;
@@ -3294,7 +3301,12 @@ static int request_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
 
 	lock_rsb(r);
 
-	attach_lkb(r, lkb);
+	error = attach_lkb(r, lkb);
+	if (error) {
+		unlock_rsb(r);
+		put_rsb(r);
+		return error;
+	}
 	lkb->lkb_lksb->sb_lkid = lkb->lkb_id;
 
 	error = _request_lock(r, lkb);
@@ -4038,7 +4050,14 @@ static int receive_request(struct dlm_ls *ls, const struct dlm_message *ms)
 		}
 	}
 
-	attach_lkb(r, lkb);
+	error = attach_lkb(r, lkb);
+	if (error) {
+		unlock_rsb(r);
+		put_rsb(r);
+		__put_lkb(ls, lkb);
+		goto fail;
+	}
+
 	error = do_request(r, lkb);
 	send_request_reply(r, lkb, error);
 	do_request_effects(r, lkb, error);
@@ -5642,7 +5661,12 @@ int dlm_recover_master_copy(struct dlm_ls *ls, const struct dlm_rcom *rc,
 		goto out_unlock;
 	}
 
-	attach_lkb(r, lkb);
+	error = attach_lkb(r, lkb);
+	if (error) {
+		__put_lkb(ls, lkb);
+		goto out_unlock;
+	}
+
 	add_lkb(r, lkb, rl->rl_status);
 	ls->ls_recover_locks_in++;
 
@@ -6300,12 +6324,14 @@ int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len,
 	if (!lksb)
 		return -ENOMEM;
 
-	error = _create_lkb(ls, &lkb, lkb_id, lkb_id + 1);
+	error = create_lkb(ls, &lkb);
 	if (error) {
 		kfree(lksb);
 		return error;
 	}
 
+	lkb->lkb_id = lkb_id;
+
 	dlm_set_dflags_val(lkb, lkb_dflags);
 	lkb->lkb_nodeid = lkb_nodeid;
 	lkb->lkb_lksb = lksb;
@@ -6321,7 +6347,15 @@ int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len,
 	}
 
 	lock_rsb(r);
-	attach_lkb(r, lkb);
+	error = attach_lkb(r, lkb);
+	if (error) {
+		unlock_rsb(r);
+		put_rsb(r);
+		kfree(lksb);
+		__put_lkb(ls, lkb);
+		return error;
+	}
+
 	add_lkb(r, lkb, lkb_status);
 	unlock_rsb(r);
 	put_rsb(r);
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCHv2 RESEND v7.4-rc3 3/5] dlm: don't publish a lkb in ls_lkbxa before it has an rsb

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] dlm: __put_lkb() erases active ls_lkbxa entry on find_rsb() failure --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] dlm: __put_lkb() erases active ls_lkbxa entry on find_rsb() failure
--

commit c68d5fa6b30984a97220ff35d349c3276a988660
Author: Yogesh Gaur <[email protected]>

dlm: don't publish a lkb in ls_lkbxa before it has an rsb

This commit moves the allocation of lkb_id and insertion of the lkb into ls_lkbxa from create_lkb() to attach_lkb() so that find_lkb() never returns an lkb with a NULL lkb_resource.

Quoted 4 lines
fs/dlm/lock.c +0 -0
index c715e171bab7e..3b3e15157f4c9 100644

[ ... ]

Quoted 17 lines
fs/dlm/lock.c +3 -1
@@ -6300,12 +6324,14 @@ int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len,
 	if (!lksb)
 		return -ENOMEM;
 
-	error = _create_lkb(ls, &lkb, lkb_id, lkb_id + 1);
+	error = create_lkb(ls, &lkb);
 	if (error) {
 		kfree(lksb);
 		return error;
 	}
 
+	lkb->lkb_id = lkb_id;
+
 	dlm_set_dflags_val(lkb, lkb_dflags);
 	lkb->lkb_nodeid = lkb_nodeid;
 	lkb->lkb_lksb = lksb;

fs/dlm/lock.c int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len, · line 6347

Quoted 1 line
fs/dlm/lock.c +0 -0
@@ -6321,7 +6347,15 @@ int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len,
 	}

[Severity: Medium]
Can setting lkb->lkb_id before find_rsb() cause __put_lkb() to erase an existing lkb's entry in ls_lkbxa if find_rsb() fails?

attach_lkb() clears lkb->lkb_id on failure for that reason, but if find_rsb() fails first (for example, with -EINVAL when len > DLM_RESNAME_MAXLEN), dlm_debug_add_lkb() calls __put_lkb(ls, lkb) while lkb->lkb_id still holds the caller-supplied id, and xa_erase() removes any live lkb already occupying that id from ls_lkbxa. Setting lkb->lkb_id right before attach_lkb() would avoid this.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#8 lore raw

Re: [PATCHv2 RESEND v7.4-rc3 3/5] dlm: don't publish a lkb in ls_lkbxa before it has an rsb

Hi,

Quoted 46 lines · On Fri, Nov 20, 2026 at 3:19 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] dlm: __put_lkb() erases active ls_lkbxa entry on find_rsb() failure
--

commit c68d5fa6b30984a97220ff35d349c3276a988660
Author: Yogesh Gaur <[email protected]>

dlm: don't publish a lkb in ls_lkbxa before it has an rsb

This commit moves the allocation of lkb_id and insertion of the lkb into ls_lkbxa from create_lkb() to attach_lkb() so that find_lkb() never returns an lkb with a NULL lkb_resource.

Quoted 4 lines
fs/dlm/lock.c +0 -0
index c715e171bab7e..3b3e15157f4c9 100644

[ ... ]

Quoted 17 lines
fs/dlm/lock.c +3 -1
@@ -6300,12 +6324,14 @@ int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len,
      if (!lksb)
              return -ENOMEM;
 
-     error = _create_lkb(ls, &lkb, lkb_id, lkb_id + 1);
+     error = create_lkb(ls, &lkb);
      if (error) {
              kfree(lksb);
              return error;
      }
 
+     lkb->lkb_id = lkb_id;
+
      dlm_set_dflags_val(lkb, lkb_dflags);
      lkb->lkb_nodeid = lkb_nodeid;
      lkb->lkb_lksb = lksb;

fs/dlm/lock.c int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len, · line 6347

Quoted 1 line
fs/dlm/lock.c +0 -0
@@ -6321,7 +6347,15 @@ int dlm_debug_add_lkb(struct dlm_ls *ls, uint32_t lkb_id, char *name, int len,
      }

[Severity: Medium]
Can setting lkb->lkb_id before find_rsb() cause __put_lkb() to erase an existing lkb's entry in ls_lkbxa if find_rsb() fails?

Quoted 7 lines

attach_lkb() clears lkb->lkb_id on failure for that reason, but if find_rsb() fails first (for example, with -EINVAL when len > DLM_RESNAME_MAXLEN), dlm_debug_add_lkb() calls __put_lkb(ls, lkb) while lkb->lkb_id still holds the caller-supplied id, and xa_erase() removes any live lkb already occupying that id from ls_lkbxa. Setting lkb->lkb_id right before attach_lkb() would avoid this.

although it is just a debugging feature and the user (root) needs to know what its doing as it can break a lot of things in a invalid production use case, yes it might be better to move that before attach_lkb().

- Alex

author maintainer
#9 lore raw

PATCH RESEND v2 4/5 dlm: don't set RSB_MASTER_UNCERTAIN on rsb with no master

In reply to Alexander Aring

From: Ameer Hamza <[email protected]>

When a local request reuses an inactive rsb, find_rsb_dir() sets RSB_MASTER_UNCERTAIN if res_master_nodeid is not our nodeid. This also matches an rsb that never got a master (res_master_nodeid 0, res_nodeid -1). set_master() then trusts the "cached" master, copies res_nodeid -1 into lkb_nodeid and skips the lookup, and _request_lock() hits the DLM_ASSERT(r->res_nodeid >= 0) in is_remote():

rsb: nodeid -1 master 0 dir 1 flags 200 first 1 rlc 0 name pr

  Kernel panic - not syncing: DLM:  Record message above and reboot.
  Call Trace:
   _request_lock.cold+0x3e/0x3e [dlm]
   request_lock+0xcc/0x110 [dlm]
   dlm_user_request+0x110/0x340 [dlm]
   device_write+0x605/0x6d0 [dlm]

Such an rsb is left behind when a request is cancelled while its lookup is still outstanding and the lockspace then recovers, e.g. because the dir node died: dlm_recover_waiters_post() completes the cancelled lkb instead of resending it, so no master is ever set. Once the rsb is inactive, the next request for the name revives it and crashes as above. If we became the dir node in that recovery, deactivate_rsb() even keeps the rsb as a dir record, so it does not age out.

Before commit c04fecb4d9f7 ("dlm: use rsbtbl as resource directory") the flag was only set when res_nodeid was > 0. Restore that and set the flag only when a master is actually cached, so that set_master() does a normal lookup (or becomes the master itself as dir node) for an rsb that has none. res_first_lkid is still cleared, as the cancelled lkb leaves a stale id behind.

Fixes c04fecb4d9f7 ("dlm: use rsbtbl as resource directory")
---
 fs/dlm/lock.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/dlm/lock.c +6 -2
index 3b3e15157f4c..934afc93464f 100644
@@ -826,8 +826,12 @@ static int find_rsb_dir(struct dlm_ls *ls, const void *name, int len,
 
 	if (from_local && (r->res_master_nodeid != our_nodeid)) {
 		/* Because we have held no locks on this rsb,
-		   res_master_nodeid could have become stale. */
-		rsb_set_flag(r, RSB_MASTER_UNCERTAIN);
+		 * res_master_nodeid could have become stale.  If no master
+		 * was ever set, there is nothing to confirm and set_master()
+		 * must resolve it as for a new rsb.
+		 */
+		if (r->res_master_nodeid)
+			rsb_set_flag(r, RSB_MASTER_UNCERTAIN);
 		r->res_first_lkid = 0;
 	}
 
Signature · 1 line
2.43.0
author maintainer
#10 lore raw

PATCH RESEND v2 5/5 dlm: fix memory leak on active leaving member

In reply to Alexander Aring

When a node calls dlm_lockspace_release() locally, it does not subsequently run recovery. Commit de7b4869b4ec ("dlm: add new configfs entry release_recover for lockspace members") entry began tracking leaving members to perform optional post processing, such as invoking the slot callback. Because a locally leaving member does not execute recovery, it never invokes dlm_config_nodes(), leaving the allocated tracking entries uncleaned.

This issue was detected by kmemleak.

unreferenced object 0xffff88811d68bcc0 (size 32):
comm "dlm_controld", pid 406, jiffies 4294749992 hex dump (first 32 bytes):

    03 00 00 00 00 00 00 00 08 bd 68 1d 81 88 ff ff  ..........h.....
    88 bc 68 1d 81 88 ff ff 00 00 00 00 00 00 00 00  ..h.............
  backtrace (crc be1c20bb):
    kmemleak_alloc+0x85/0xa0
    __kmalloc_cache_noprof+0x2ac/0x380
    drop_node+0x73/0x550
    client_drop_item+0x83/0xe0
    configfs_rmdir+0x735/0x910
    vfs_rmdir+0x24a/0x820
    filename_rmdir+0x3a3/0x4a0
    __x64_sys_rmdir+0x42/0x60
    x64_sys_call+0x1e8d/0x1fb0
    do_syscall_64+0x87/0x490
    entry_SYSCALL_64_after_hwframe+0x76/0x7e

A local node executing dlm_lockspace_release() does not require additional recovery handling, such as invoking the slot callback. Fix this memory leak by freeing the resources allocated in drop_node() when the lockspace configfs entry is released.

Fixes de7b4869b4ec ("dlm: add new configfs entry release_recover for lockspace members")
---
 fs/dlm/config.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/dlm/config.c +9 -0
index 6ebc0fa9a2fa..5e41f795366c 100644
@@ -533,9 +533,18 @@ static struct config_group *make_space(struct config_group *g, const char *name)
 static void drop_space(struct config_group *g, struct config_item *i)
 {
 	struct dlm_space *sp = config_item_to_space(i);
+	struct dlm_member_gone *mb_gone, *mb_safe;
 
 	/* assert list_empty(&sp->members) */
 
+	mutex_lock(&sp->members_lock);
+	list_for_each_entry_safe(mb_gone, mb_safe, &sp->members_gone, list) {
+		list_del(&mb_gone->list);
+		sp->members_gone_count--;
+		kfree(mb_gone);
+	}
+	mutex_unlock(&sp->members_lock);
+
 	configfs_remove_default_groups(&sp->group);
 	config_item_put(i);
 }
Signature · 1 line
2.43.0