Skip to contentSkip to conversation

syscalls/listmount04: Detect backported commit 78f0e33cd6c9 dynamically

PATCH In review Darren Chang via ltp 3 participants 3 messages Jun 3 Arm STM32

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/listmount04: Detect backported commit 78f0e33cd6c9 dynamically

Commit 78f0e33cd6c9 ("fs/namespace: correctly handle errors returned by grab_requested_mnt_ns") changed the expected errno for an invalid mnt_ns_fd from EINVAL to EBADF. This was introduced in v6.17.9.

Currently, the test relies solely on tst_kvercmp() to determine which errno to expect. However, this causes the test to fail on older LTS or Android kernels (e.g., v6.12) that have backported this specific patch. On these kernels, listmount() correctly returns EBADF, but the test expects EINVAL purely based on the older kernel version number.

Fix this by introducing a runtime probe in setup(). By issuing a dummy listmount() syscall with an invalid mnt_ns_fd (-1), we can check the returned errno and accurately determine the kernel's behavior regardless of its version string.

Additionally, rename the 'kver' variable to 'current_fd_err_behavior' and update the macros to clearly reflect the expected errno behavior (INVALID_FD_RET_EINVAL vs INVALID_FD_RET_EBADF) rather than relying on version thresholds, improving overall code readability and semantics.

---
 .../kernel/syscalls/listmount/listmount04.c   | 51 ++++++++++++++-----
 1 file changed, 39 insertions(+), 12 deletions(-)
testcases/kernel/syscalls/listmount/listmount04.c +39 -12
index 919f4c854..38eaffede 100644
@@ -15,22 +15,26 @@
 
 #define _GNU_SOURCE
 
+#include <errno.h>
 #include "config.h"
 #include "tst_test.h"
 #include "lapi/mount.h"
 #include "lapi/syscalls.h"
 
 #define MNT_SIZE 32
+
 /*
  * For commit 78f0e33cd6c9 ("fs/namespace: correctly handle errors returned
  * by grab_requested_mnt_ns") from v6.18-rc7 backported to v6.17.9.
+ * Defines the expected error code behavior when an invalid file descriptor
+ * is passed to mnt_ns_fd (previously known as spare).
  */
-#define BEFORE_6_17_9 1
-#define AFTER_6_17_9 2
+#define INVALID_FD_RET_EINVAL 1
+#define INVALID_FD_RET_EBADF  2
 
 static mnt_id_req *request;
 static uint64_t mnt_ids[MNT_SIZE];
-static int kver;
+static int current_fd_err_behavior;
 
 static struct tcase {
 	int req_usage;
@@ -43,7 +47,7 @@ static struct tcase {
 	uint64_t flags;
 	int exp_errno;
 	char *msg;
-	int kver;
+	int fd_err_behavior;
 } tcases[] = {
 	{
 		.req_usage = 0,
@@ -89,7 +93,7 @@ static struct tcase {
 		.nr_mnt_ids = MNT_SIZE,
 		.exp_errno = EINVAL,
 		.msg = "invalid mnt_id_req.spare",
-		.kver = BEFORE_6_17_9,
+		.fd_err_behavior = INVALID_FD_RET_EINVAL,
 	},
 	{
 		.req_usage = 1,
@@ -100,7 +104,7 @@ static struct tcase {
 		.nr_mnt_ids = MNT_SIZE,
 		.exp_errno = EBADF,
 		.msg = "invalid mnt_id_req.mnt_ns_fd",
-		.kver = AFTER_6_17_9,
+		.fd_err_behavior = INVALID_FD_RET_EBADF,
 	},
 	{
 		.req_usage = 1,
@@ -137,8 +141,9 @@ static void run(unsigned int n)
 	struct tcase *tc = &tcases[n];
 	mnt_id_req *req = NULL;
 
-	if (tc->kver && tc->kver != kver) {
-		tst_res(TCONF, "Test not suitable for current kernel version");
+	/* Skip tests that do not match the runtime behavior of current kernel */
+	if (tc->fd_err_behavior && tc->fd_err_behavior != current_fd_err_behavior) {
+		tst_res(TCONF, "Test not suitable for current kernel's FD error behavior");
 		return;
 	}
 
@@ -159,10 +164,32 @@ static void run(unsigned int n)
 
 static void setup(void)
 {
-	if (tst_kvercmp(6, 17, 9) >= 0)
-		kver = AFTER_6_17_9;
-	else
-		kver = BEFORE_6_17_9;
+	/* 1. If kernel is 6.17.9 or newer, it definitively has the new behavior */
+	if (tst_kvercmp(6, 17, 9) >= 0) {
+		current_fd_err_behavior = INVALID_FD_RET_EBADF;
+		return;
+	}
+
+	/* 2. Runtime probe for older kernels:
+	 * Check if commit 78f0e33cd6c9 was backported by manually issuing
+	 * a syscall with an invalid mnt_ns_fd (-1).
+	 */
+	mnt_id_req probe_req = {
+		.size = MNT_ID_REQ_SIZE_VER0,
+		.mnt_id = LSMT_ROOT,
+		.mnt_ns_fd = -1,
+		.param = 0
+	};
+
+	tst_syscall(__NR_listmount, &probe_req, mnt_ids, MNT_SIZE, 0);
+
+	/* 3. Set global behavior flag based on actual kernel response */
+	if (errno == EBADF) {
+		tst_res(TINFO, "Detected backported commit 78f0e33cd6c9 (returns EBADF)");
+		current_fd_err_behavior = INVALID_FD_RET_EBADF;
+	} else {
+		current_fd_err_behavior = INVALID_FD_RET_EINVAL;
+	}
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.54.0.1032.g2f8565e1d1-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi!

Quoted 22 lines

static void setup(void)
{

testcases/kernel/syscalls/listmount/listmount04.c +16 -4
-	if (tst_kvercmp(6, 17, 9) >= 0)
-		kver = AFTER_6_17_9;
-	else
-		kver = BEFORE_6_17_9;
+	/* 1. If kernel is 6.17.9 or newer, it definitively has the new behavior */
+	if (tst_kvercmp(6, 17, 9) >= 0) {
+		current_fd_err_behavior = INVALID_FD_RET_EBADF;
+		return;
+	}
+
+	/* 2. Runtime probe for older kernels:
+	 * Check if commit 78f0e33cd6c9 was backported by manually issuing
+	 * a syscall with an invalid mnt_ns_fd (-1).
+	 */
+	mnt_id_req probe_req = {
+		.size = MNT_ID_REQ_SIZE_VER0,
+		.mnt_id = LSMT_ROOT,
+		.mnt_ns_fd = -1,
+		.param = 0
+	};

testcases/kernel/syscalls/listmount/listmount04.c static void run(unsigned int n) · line 192

Quoted 10 lines
testcases/kernel/syscalls/listmount/listmount04.c +10 -0
+
+	tst_syscall(__NR_listmount, &probe_req, mnt_ids, MNT_SIZE, 0);
+
+	/* 3. Set global behavior flag based on actual kernel response */
+	if (errno == EBADF) {
+		tst_res(TINFO, "Detected backported commit 78f0e33cd6c9 (returns EBADF)");
+		current_fd_err_behavior = INVALID_FD_RET_EBADF;
+	} else {
+		current_fd_err_behavior = INVALID_FD_RET_EINVAL;
+	}

This is too complicated. I think that we want to settle for:

* new behavior after 6.17.9 (i.e. EBADF)
* any of the two errnos before that (i.e. either EINVAL or EBADF)

For that we should skip to TST_EXP_FAIL_ARR() pass array with only single errno for all cases but the one one where we need to allow both EINVAL and EBADF.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Darren Chang via ltp

Hi Darren,

Thank you for the patch. The overall approach — probing the kernel at runtime instead of relying solely on tst_kvercmp() — is the right fix for backport scenarios. Two issues need to be addressed before this can be accepted.

---

[PATCH] syscalls/listmount04: Detect backported commit 78f0e33cd6c9 dynamically

Quoted 33 lines
testcases/kernel/syscalls/listmount/listmount04.c +20 -0
+	/* 2. Runtime probe for older kernels:
+	 * Check if commit 78f0e33cd6c9 was backported by manually issuing
+	 * a syscall with an invalid mnt_ns_fd (-1).
+	 */
+	mnt_id_req probe_req = {
+		.size = MNT_ID_REQ_SIZE_VER0,
+		.mnt_id = LSMT_ROOT,
+		.mnt_ns_fd = -1,
+		.param = 0
+	};
+
+	tst_syscall(__NR_listmount, &probe_req, mnt_ids, MNT_SIZE, 0);
+
+	/* 3. Set global behavior flag based on actual kernel response */
+	if (errno == EBADF) {
+		tst_res(TINFO, "Detected backported commit 78f0e33cd6c9 (returns EBADF)");
+		current_fd_err_behavior = INVALID_FD_RET_EBADF;
+	} else {
+		current_fd_err_behavior = INVALID_FD_RET_EINVAL;
+	}
Quoted 13 lines
Quoted 13 lines

The else branch silently maps *any* unexpected errno to INVALID_FD_RET_EINVAL. If the probe returns something other than EBADF or EINVAL (e.g. EPERM, ENOMEM, or a hypothetical kernel bug), the test will silently classify the kernel as "pre-backport" and run the EINVAL test case against wrong expectations, yielding a false FAIL or a false PASS.

tst_syscall() already handles ENOSYS (it calls tst_brk()), but all other unexpected errnos fall through to this else branch unchecked.

Please make the check explicit and abort on unexpected values:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
    if (errno == EBADF) {
        tst_res(TINFO, "Detected backported commit 78f0e33cd6c9 (returns EBADF)");
        current_fd_err_behavior = INVALID_FD_RET_EBADF;
    } else if (errno == EINVAL) {
        current_fd_err_behavior = INVALID_FD_RET_EINVAL;
    } else {
        tst_brk(TBROK, "Unexpected errno %d from mnt_ns_fd probe", errno);
    }

---

Quoted 19 lines

+/*
* For commit 78f0e33cd6c9 ("fs/namespace: correctly handle errors returned
* by grab_requested_mnt_ns") from v6.18-rc7 backported to v6.17.9.
+ * Defines the expected error code behavior when an invalid file descriptor
+ * is passed to mnt_ns_fd (previously known as spare).
*/

Quoted 12 lines
Quoted 12 lines

"previously known as spare" is inaccurate. The field in both the kernel's struct mnt_id_req and LTP's mnt_id_req_fallback has always been named mnt_ns_fd. The local helper struct tcase still has a field named spare in this very patch — it was not renamed — so "previously" is wrong in both directions.

If the intent is to connect the tcase.spare field to the kernel's mnt_ns_fd field, say so directly:

* is passed to mnt_ns_fd (mapped from struct tcase's spare field).

Or simply drop the parenthetical — the sentence is clear without it.

---

Commit message:

Quoted 3 lines

Commit 78f0e33cd6c9 ("fs/namespace: correctly handle errors returned by grab_requested_mnt_ns") changed the expected errno for an invalid mnt_ns_fd from EINVAL to EBADF. This was introduced in v6.17.9.

Minor nit (not a blocker): "This was introduced in v6.17.9" only tells half the story. The change landed in mainline at v6.18 (via v6.18-rc7) and was concurrently backported to stable v6.17.9. The sentence could read:

This landed in mainline v6.18 and was backported to stable v6.17.9.

The code in setup() is correct regardless (tst_kvercmp(6, 17, 9) >= 0 is true for all v6.17.9+, v6.18+, and later), but the commit message description of the timeline is incomplete.

---

Verdict: Needs revision

LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp