Skip to contentSkip to conversation

iocl11.c: New case check PROCMAP_QUERY ioctl() errnos

PATCH Superseded Wei Gao via ltp 2 participants 5 messages Aug 7, 2025 – Aug 12, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iocl11.c: New case check PROCMAP_QUERY ioctl() errnos

---
 runtest/syscalls                           |   1 +
 testcases/kernel/syscalls/ioctl/.gitignore |   1 +
 testcases/kernel/syscalls/ioctl/ioctl11.c  | 183 +++++++++++++++++++++
 3 files changed, 185 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl11.c
runtest/syscalls +1 -0
index 6a17a34f8..8bc7f60d2 100644
@@ -588,6 +588,7 @@ ioctl07      ioctl07
 ioctl08      ioctl08
 ioctl09      ioctl09
 ioctl10      ioctl10
+ioctl11      ioctl11
 
 ioctl_loop01 ioctl_loop01
 ioctl_loop02 ioctl_loop02
testcases/kernel/syscalls/ioctl/.gitignore +1 -0
index dac4583fa..54685e916 100644
@@ -8,6 +8,7 @@
 /ioctl08
 /ioctl09
 /ioctl10
+/ioctl11
 /ioctl_loop01
 /ioctl_loop02
 /ioctl_loop03
testcases/kernel/syscalls/ioctl/ioctl11.c new file +183 -0

183 lines not on this page · raw message

Signature · 5 lines
2.49.0


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

Hi Wei,

Thanks for writing new test. Generally LGTM.

Also, FYI upstream developer enhanced existing tools/testing/selftests/proc/proc-pid-vm.c
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=81510a0eaa6916c2fbb0b2639f3e617a296979a3
That might be a good source for real PROCMAP_QUERY testing.

Few common errors:

1) git commit subject contains typo:
iocl11 => ioctl11
Please remember to fix it.

2) Warning

$ make check-ioctl11
CHECK testcases/kernel/syscalls/ioctl/ioctl11.c
ioctl11.c:32:22: warning: Symbol 'q' has no prototype or library ('tst_') prefix. Should it be static?

3) EMFILE (Too many open files)
=> missing SAFE_FCLOSE(fp) before return start_addr;

# ./ioctl11 -i1200
ioctl11.c:149: TPASS: ioctl(fd, PROCMAP_QUERY, q) : E2BIG (7)
ioctl11.c:149: TPASS: ioctl(fd, PROCMAP_QUERY, q) : EINVAL (22)
ioctl11.c:101: TBROK: fopen(/proc/self/maps,r) failed: EMFILE (24)

ioctl11.c: In function ‘get_vm_start’:
ioctl11.c:113:1: warning: control reaches end of non-void function [-Wreturn-type]
113 | }

      | ^

Other notes below.
...

testcases/kernel/syscalls/ioctl/ioctl11.c line 3

Quoted 9 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +3 -0
new file mode 100644
index 000000000..aef2105cc
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Wei Gao <[email protected]>

Maybe update to 2025?

testcases/kernel/syscalls/ioctl/ioctl11.c line 16

Quoted 13 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +13 -0
+ */
+
+/*\
+ * [Description]
+ *
+ * Test PROCMAP_QUERY ioctl() errnos:
+ *
+ * - EINVAL if q->size is too small
+ * - E2BIG if q->size is larger than page size
+ * - EINVAL on invalid q->flags
+ * - EINVAL if only one of q->vma_name_size and q->vma_name_addr is set
+ * - EINVAL if only one of q->build_id_size and q->build_id_addr is set
+ * - ENAMETOOLONG if build_id_size or name_buf_size is too small

What is name_buf_size? Do you mean vma_name_size?

I wonder whether pointing out in doc some of the relevant commits (6 commits mentioned in "(FEATURED) ioctl()-based API to query VMAs from /proc/<pid>" section in [1]) or just put [2] (or [3] would make sense.

[1] https://kernelnewbies.org/Linux_6.11#Memory_management
[2] https://kernelnewbies.org/Linux_6.11#Binary_interface_for_.2Fproc.2F.3Cpid.3E.2Fmaps
[3] https://lore.kernel.org/all/[email protected]/
ed5d583a88a9 ("fs/procfs: implement efficient VMA querying API for /proc/<pid>/maps")

testcases/kernel/syscalls/ioctl/ioctl11.c line 20

Quoted 4 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +4 -0
+ */
+
+#include "config.h"
+#include <stdlib.h>

Either <stdlib.h> is not needed or some of LTP headers probably adds it as it builds without it.

testcases/kernel/syscalls/ioctl/ioctl11.c line 21

Quoted 1 line
testcases/kernel/syscalls/ioctl/ioctl11.c new file +1 -0
+#include <sys/ioctl.h>

<sys/ioctl.h> is not needed (loaded as part of lapi/ioctl.h.
https://github.com/linux-test-project/ltp/blob/master/doc/old/C-Test-API.asciidoc#lapi-headers

testcases/kernel/syscalls/ioctl/ioctl11.c line 23

Quoted 3 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +2 -0
+#include <errno.h>
 
+#include <fnmatch.h>

Why this header? IMHO not needed.

testcases/kernel/syscalls/ioctl/ioctl11.c line 26

Quoted 4 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +3 -0
+#include "tst_test.h"
+#include "tst_safe_stdio.h"
 
+#include <sys/sysmacros.h>

Why this header? IMHO not needed.

very nit: Also I would first add system headers (these with <>) and then LTP headers. i.e. not mixing them.

testcases/kernel/syscalls/ioctl/ioctl11.c line 29

Quoted 3 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +3 -0
+#include <linux/fs.h>
+#include "lapi/ioctl.h"
+

#include "config.h"
#include <errno.h>
#include <linux/fs.h>
#include "tst_test.h"
#include "tst_safe_stdio.h"
#include "lapi/ioctl.h"

testcases/kernel/syscalls/ioctl/ioctl11.c line 32

Quoted 3 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +3 -0
+#define PROC_MAP_PATH "/proc/self/maps"
+
+struct procmap_query *q;

This should be static.

Quoted 65 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +65 -0
+static int fd = -1;
+static char buf[PATH_MAX];
+static char small_buf[1];
+
+static void setup_normal(void);
+static void setup_big_size(void);
+
+static struct tcase {
+	uint64_t size;
+	uint64_t query_addr;
+	uint64_t query_flags;
+	uint64_t vma_name_addr;
+	uint32_t vma_name_size;
+	uint64_t build_id_addr;
+	uint32_t build_id_size;
+	int exp_errno;
+	void (*setup)(void);
+} tcases[] = {
+	{
+		.size = 1,
+		.exp_errno = EINVAL,
+		.setup = setup_normal
+	},
+	{
+		.exp_errno = E2BIG,
+		.setup = setup_big_size
+	},
+	{
+		.query_flags = -1,
+		.exp_errno = EINVAL,
+		.setup = setup_normal
+	},
+	{
+		.vma_name_size = sizeof(buf),
+		.exp_errno = EINVAL,
+		.setup = setup_normal
+	},
+	{
+		.vma_name_addr = (uint64_t)(unsigned long)buf,
+		.exp_errno = EINVAL,
+		.setup = setup_normal
+	},
+	{
+		.build_id_size = sizeof(buf),
+		.exp_errno = EINVAL,
+		.setup = setup_normal
+	},
+	{
+		.build_id_addr = (uint64_t)(unsigned long)buf,
+		.exp_errno = EINVAL,
+		.setup = setup_normal
+	},
+	{
+		.vma_name_addr = (uint64_t)(unsigned long)small_buf,
+		.vma_name_size = sizeof(small_buf),
+		.exp_errno = ENAMETOOLONG,
+		.setup = setup_normal
+	},
+	{
+		.build_id_addr = (uint64_t)(unsigned long)small_buf,
+		.build_id_size = sizeof(small_buf),
+		.exp_errno = ENAMETOOLONG,
+		.setup = setup_normal
+	},
+};

testcases/kernel/syscalls/ioctl/ioctl11.c line 107

Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +10 -0
+
+static unsigned long get_vm_start(void)
+{
+	FILE *fp = SAFE_FOPEN(PROC_MAP_PATH, "r");
+	char line[1024];
+	unsigned long start_addr = 0;
+
+	if (fgets(line, sizeof(line), fp) != NULL) {
+		if (sscanf(line, "%lx-", &start_addr) != 1)
+			tst_brk(TFAIL, "parse maps file /proc/self/maps failed");

nit: Maybe "map not found in /proc/self/maps"?
Also, PROC_MAP_PATH will never change from /proc/self/maps, so it's probably safe to use it hardcoded. But using just "map not found" would avoid hardcoded file.

Here you needs to also close fp.

Quoted 7 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +7 -0
+		return start_addr;
+	}
+
+	SAFE_FCLOSE(fp);
+	tst_brk(TFAIL, "parse maps file /proc/self/maps failed");
+}
+

testcases/kernel/syscalls/ioctl/ioctl11.c line 124

Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +10 -0
+static void setup_normal(void)
+{
+	q->size = sizeof(*q);
+	q->query_addr = (uint64_t)get_vm_start();
+	q->query_flags = 0;
+}
+
+static void setup_big_size(void)
+{
+	setup_normal();

IMHO content of setup_normal() should be moved to run(), before tc->setup(). Then all ".setup = setup_normal" could be removed.

testcases/kernel/syscalls/ioctl/ioctl11.c line 137

Quoted 13 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +13 -0
+	q->size = getpagesize() + 1;
+}
+
+static void run(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	memset(q, 0, sizeof(*q));
+
+	tc->setup();
+
+	if (tc->size != 0)
+		q->size = tc->size;

Spaces between each if would make code for me more readable.

Quoted 11 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +11 -0
+	if (tc->query_flags != 0)
+		q->query_flags = tc->query_flags;
+	if (tc->vma_name_addr != 0)
+		q->vma_name_addr = tc->vma_name_addr;
+	if (tc->vma_name_size != 0)
+		q->vma_name_size = tc->vma_name_size;
+	if (tc->build_id_addr != 0)
+		q->build_id_addr = tc->build_id_addr;
+	if (tc->build_id_size != 0)
+		q->build_id_size = tc->build_id_size;
+

testcases/kernel/syscalls/ioctl/ioctl11.c line 158

Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +10 -0
+	TST_EXP_FAIL(ioctl(fd, PROCMAP_QUERY, q), tc->exp_errno);
+}
+
+static void setup(void)
+{
+	struct procmap_query q = {};
+
+	fd = SAFE_OPEN(PROC_MAP_PATH, O_RDONLY);
+
+	if (tst_kvercmp(6, 11, 0) < 0) {

If you reverse the condition code is more compact.

testcases/kernel/syscalls/ioctl/ioctl11.c line 161

Quoted 3 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +3 -0
+		TEST(ioctl(fd, PROCMAP_QUERY, q));
+
+		if ((TST_RET == -1) && (TST_ERR == ENOTTY))

Brackets between && are not needed.

testcases/kernel/syscalls/ioctl/ioctl11.c line 163

Quoted 2 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +2 -0
+			tst_brk(TCONF,
+				"This system does not provide support for ioctl(PROCMAP_QUERY)");
	if (tst_kvercmp(6, 11, 0) >= 0)
		return;

	TEST(ioctl(fd, PROCMAP_QUERY, q));
	if (TST_RET == -1 && TST_ERR == ENOTTY) {
		tst_brk(TCONF,
			"This system does not provide support for ioctl(PROCMAP_QUERY)");
	}

testcases/kernel/syscalls/ioctl/ioctl11.c

Quoted 2 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +2 -0
+	}
+

Please remove this blank line ^.

Quoted 1 line

+}

Kind regards,
Petr

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

PATCH v2 1/1 ioctl11.c: New case check PROCMAP_QUERY ioctl() errnos

In reply to Wei Gao via ltp
---
 runtest/syscalls                           |   1 +
 testcases/kernel/syscalls/ioctl/.gitignore |   1 +
 testcases/kernel/syscalls/ioctl/ioctl11.c  | 183 +++++++++++++++++++++
 3 files changed, 185 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl11.c
runtest/syscalls +1 -0
index 6a17a34f8..8bc7f60d2 100644
@@ -588,6 +588,7 @@ ioctl07      ioctl07
 ioctl08      ioctl08
 ioctl09      ioctl09
 ioctl10      ioctl10
+ioctl11      ioctl11
 
 ioctl_loop01 ioctl_loop01
 ioctl_loop02 ioctl_loop02
testcases/kernel/syscalls/ioctl/.gitignore +1 -0
index dac4583fa..54685e916 100644
@@ -8,6 +8,7 @@
 /ioctl08
 /ioctl09
 /ioctl10
+/ioctl11
 /ioctl_loop01
 /ioctl_loop02
 /ioctl_loop03
testcases/kernel/syscalls/ioctl/ioctl11.c new file +183 -0

183 lines not on this page · raw message

Signature · 5 lines
2.49.0


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

PATCH v3 1/1 ioctl11.c: New case check PROCMAP_QUERY ioctl() errnos

---
 runtest/syscalls                           |   1 +
 testcases/kernel/syscalls/ioctl/.gitignore |   1 +
 testcases/kernel/syscalls/ioctl/ioctl11.c  | 214 +++++++++++++++++++++
 3 files changed, 216 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ioctl/ioctl11.c
runtest/syscalls +1 -0
index 6a17a34f8..8bc7f60d2 100644
@@ -588,6 +588,7 @@ ioctl07      ioctl07
 ioctl08      ioctl08
 ioctl09      ioctl09
 ioctl10      ioctl10
+ioctl11      ioctl11
 
 ioctl_loop01 ioctl_loop01
 ioctl_loop02 ioctl_loop02
testcases/kernel/syscalls/ioctl/.gitignore +1 -0
index dac4583fa..54685e916 100644
@@ -8,6 +8,7 @@
 /ioctl08
 /ioctl09
 /ioctl10
+/ioctl11
 /ioctl_loop01
 /ioctl_loop02
 /ioctl_loop03
testcases/kernel/syscalls/ioctl/ioctl11.c new file +214 -0

214 lines not on this page · raw message

Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v3 1/1
#4 lore raw

Re: [LTP] [PATCH v3] ioctl11.c: New case check PROCMAP_QUERY ioctl() errnos

Hi Wei, Andrea,

Quoted 2 lines

+/*\
+ * [Description]

nit: we stopped using this. Could it be removed before merge?

Quoted 11 lines

+ *
+ * Test PROCMAP_QUERY ioctl() errnos:
+ *
+ * - EINVAL if q->size is too small
+ * - E2BIG if q->size is larger than page size
+ * - EINVAL on invalid q->flags
+ * - EINVAL if only one of q->vma_name_size and q->vma_name_addr is set
+ * - EINVAL if only one of q->build_id_size and q->build_id_addr is set
+ * - ENAMETOOLONG if build_id_size or name_buf_size is too small
+ * - ESRCH if attempt to get memory map of a process that did exit and was waited for
+ *

Quoted 4 lines
+ *   [1] https://kernelnewbies.org/Linux_6.11#Memory_management
+ *   [2] https://kernelnewbies.org/Linux_6.11#Binary_interface_for_.2Fproc.2F.3Cpid.3E.2Fmaps
+ *   [3] https://lore.kernel.org/all/[email protected]/

+ * ed5d583a88a9 ("fs/procfs: implement efficient VMA querying API for /proc/<pid>/maps")

nit: [1] and other numbers will look ugly in generated doc:

[1] https://kernelnewbies.org/Linux_6.11#Memory_management [2] https://kernelnewbies.org/Linux_6.11#Binary_interface_for_.2Fproc.2F.3Cpid.3E.2Fmaps [3] https://lore.kernel.org/all/[email protected]/

=> inlined and [1] [2] [3] does not make sense when they refer to nothing.

Instead of

Quoted 4 lines
+ *   [1] https://kernelnewbies.org/Linux_6.11#Memory_management
+ *   [2] https://kernelnewbies.org/Linux_6.11#Binary_interface_for_.2Fproc.2F.3Cpid.3E.2Fmaps
+ *   [3] https://lore.kernel.org/all/[email protected]/

+ * ed5d583a88a9 ("fs/procfs: implement efficient VMA querying API for /proc/<pid>/maps")

I suggest to change before merge to something like:

PROCMAP_QUERY was introduced in 6.11.

* * https://kernelnewbies.org/Linux_6.11#Binary_interface_for_.2Fproc.2F.3Cpid.3E.2Fmaps
* * ed5d583a88a9 ("fs/procfs: implement efficient VMA querying API for /proc/<pid>/maps")

...

Quoted 8 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +8 -0
+static void fork_child_setup(void)
+{
+	int pid = SAFE_FORK();
+
+	if (pid == 0) {
+		TST_CHECKPOINT_WAKE(0);
+		TST_CHECKPOINT_WAIT(1);
+		exit(0);

testcases/kernel/syscalls/ioctl/ioctl11.c line 116

Quoted 10 lines
testcases/kernel/syscalls/ioctl/ioctl11.c new file +10 -0
+	}
+
+	TST_CHECKPOINT_WAIT(0);
+
+	char proc_path[PATH_MAX];
+
+	snprintf(proc_path, PATH_MAX, "/proc/%d/maps", pid);
+	fd = SAFE_OPEN(proc_path, O_RDONLY);
+
+	TST_CHECKPOINT_WAKE(1);

Using of the checkpoints LGTM, but hopefully somebody else recheck it.

Kind regards,
Petr

Review

Quoted 2 lines
(quoted diff) +2 -0
+	SAFE_WAITPID(pid, NULL, 0);
+}

Kind regards,
Petr

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