Skip to contentSkip to conversation

ioctl()-based API to query VMAs from /proc/<pid>/maps

PATCH Applied Andrii Nakryiko 7 participants 26 messages Jun 27, 2024 – Jul 29, 2024

Subsystems (9)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

v6 cover ioctl()-based API to query VMAs from /proc/<pid>/maps

Implement binary ioctl()-based interface to /proc/<pid>/maps file to allow applications to query VMA information more efficiently than reading *all* VMAs nonselectively through text-based interface of /proc/<pid>/maps file.

Patch #2 goes into a lot of details and background on some common patterns of using /proc/<pid>/maps in the area of performance profiling and subsequent symbolization of captured stack traces. As mentioned in that patch, patterns of VMA querying can differ depending on specific use case, but can generally be grouped into two main categories: the need to query a small subset of VMAs covering a given batch of addresses, or reading/storing/caching all (typically, executable) VMAs upfront for later processing.

The new PROCMAP_QUERY ioctl() API added in this patch set was motivated by the former pattern of usage. Earlier revisions had a patch adding a tool that faithfully reproduces an efficient VMA matching pass of a symbolizer, collecting a subset of covering VMAs for a given set of addresses as efficiently as possible. This tool served both as a testing ground, as well as a benchmarking tool. It implements everything both for currently existing text-based /proc/<pid>/maps interface, as well as for newly-added PROCMAP_QUERY ioctl(). This revision dropped the tool from the patch set and, once the API lands upstream, this tool might be added separately on Github as an example.

Based on discussion on earlier revisions of this patch set, it turned out that this ioctl() API is competitive with highly-optimized text-based pre-processing pattern that perf tool is using. Based on perf discussion, this revision adds more flexibility in specifying a subset of VMAs that are of interest. Now it's possible to specify desired permissions of VMAs (e.g., request only executable ones) and/or restrict to only a subset of VMAs that have file backing. This further improves the efficiency when using this new API thanks to more selective (executable VMAs only) querying.

In addition to a custom benchmarking tool, and experimental perf integration (available at [0]), Daniel Mueller has since also implemented an experimental integration into blazesym (see [1]), a library used for stack trace symbolization by our server fleet-wide profiler and another on-device profiler agent that runs on weaker ARM devices. The latter ARM-based device profiler is especially sensitive to performance, and so we benchmarked and compared text-based /proc/<pid>/maps solution to the equivalent one using PROCMAP_QUERY ioctl().

Results are very encouraging, giving us 5x improvement for end-to-end so-called "address normalization" pass, which is the part of the symbolization process that happens locally on ARM device, before being sent out for further heavier-weight processing on more powerful remote server. Note that this is not an artificial microbenchmark. It's a full end-to-end API call being measured with real-world data on real-world device.

TEXT-BASED
==========
Benchmarking main/normalize_process_no_build_ids_uncached_maps main/normalize_process_no_build_ids_uncached_maps

	  time:   [49.777 µs 49.982 µs 50.250 µs]

IOCTL-BASED
===========
Benchmarking main/normalize_process_no_build_ids_uncached_maps main/normalize_process_no_build_ids_uncached_maps

	  time:   [10.328 µs 10.391 µs 10.457 µs]
	  change: [−79.453% −79.304% −79.166%] (p = 0.00 < 0.02)
	  Performance has improved.

You can see above that we see the drop from 50µs down to 10µs for exactly the same amount of work, with the same data and target process.

With the aforementioned custom tool, we see about ~40x improvement (it might vary a bit, depending on a specific captured set of addresses). And even for perf-based benchmark it's on par or slightly ahead when using permission-based filtering (fetching only executable VMAs).

Earlier revisions attempted to use per-VMA locking, if kernel was compiled with CONFIG_PER_VMA_LOCK=y, but it turned out that anon_vma_name() is not yet compatible with per-VMA locking and assumes mmap_lock to be taken, which makes the use of per-VMA locking for this API premature. It was agreed ([2]) to continue for now with just mmap_lock, but the code structure is such that it should be easy to add per-VMA locking support once all the pieces are ready.

One thing that did not change was basing this new API as an ioctl() command on /proc/<pid>/maps file. An ioctl-based API on top of pidfd was considered, but has its own downsides. Implementing ioctl() directly on pidfd will cause access permission checks on every single ioctl(), which leads to performance concerns and potential spam of capable() audit messages. It also prevents a nice pattern, possible with /proc/<pid>/maps, in which application opens /proc/self/maps FD (requiring no additional capabilities) and passed this FD to profiling agent for querying. To achieve similar pattern, a new file would have to be created from pidf just for VMA querying, which is considered to be inferior to just querying /proc/<pid>/maps FD as proposed in current approach. These aspects were discussed in the hallway track at recent LSF/MM/BPF 2024 and sticking to procfs ioctl() was the final agreement we arrived at.

This patch set is based on top of mm-unstable branch in mm tree.

[0] https://github.com/anakryiko/linux/commits/procfs-proc-maps-ioctl-v2/
[1] https://github.com/libbpf/blazesym/pull/675
[2] https://lore.kernel.org/bpf/7rm3izyq2vjp5evdjc7c6z4crdd3oerpiknumdnmmemwyiwx7t@hleldw7iozi3/

v5->v6:
- make vma_page_size an __u64 field (Liam);
v4->v5:
- added tests in selftests/proc (Andrew);
- added vma_page_size field (Liam);
- dropped the benchmark tool and BPF selftests parts, I'll send them

    directly to bpf-next once this API makes it into that tree;
v3->v4:
  - drop per-VMA locking changes for now, we'll need anon_vma_name() to be
    compatible with vma->vm_lock approach (Suren, Liam);

v2->v3:
- drop mmap_lock aggressively under CONFIG_PER_VMA_LOCK (Liam);
- code massaging to abstract per-VMA vs mmap_lock differences (Liam);
v1->v2:
- per-VMA lock is used, if possible (Liam, Suren);
- added file-backed VMA querying (perf folks);
- added permission-based VMA querying (perf folks);
- split out build ID into separate patch (Suren);
- better documented API, added mention of ioctl() into procfs docs (Greg).

Andrii Nakryiko (6):
fs/procfs: extract logic for getting VMA name constituents fs/procfs: implement efficient VMA querying API for /proc/<pid>/maps fs/procfs: add build ID fetching to PROCMAP_QUERY API docs/procfs: call out ioctl()-based PROCMAP_QUERY command existence
tools: sync uapi/linux/fs.h header into tools subdir selftests/proc: add PROCMAP_QUERY ioctl tests

 Documentation/filesystems/proc.rst         |   9 +
 fs/proc/task_mmu.c                         | 383 ++++++++++++++++++---
 include/uapi/linux/fs.h                    | 158 ++++++++-
 tools/include/uapi/linux/fs.h              | 184 +++++++++-
 tools/testing/selftests/proc/Makefile      |   1 +
 tools/testing/selftests/proc/proc-pid-vm.c |  86 +++++
 6 files changed, 754 insertions(+), 67 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v6 1/6 fs/procfs: extract logic for getting VMA name constituents

Extract generic logic to fetch relevant pieces of data to describe VMA name. This could be just some string (either special constant or user-provided), or a string with some formatted wrapping text (e.g., "[anon_shmem:<something>]"), or, commonly, file path. seq_file-based logic has different methods to handle all three cases, but they are currently mixed in with extracting underlying sources of data.

This patch splits this into data fetching and data formatting, so that data fetching can be reused later on.

There should be no functional changes.

---
 fs/proc/task_mmu.c | 125 +++++++++++++++++++++++++--------------------
 1 file changed, 71 insertions(+), 54 deletions(-)
fs/proc/task_mmu.c +71 -54
index 93fb2c61b154..507b7dc7c4c8 100644
@@ -239,6 +239,67 @@ static int do_maps_open(struct inode *inode, struct file *file,
 				sizeof(struct proc_maps_private));
 }
 
+static void get_vma_name(struct vm_area_struct *vma,
+			 const struct path **path,
+			 const char **name,
+			 const char **name_fmt)
+{
+	struct anon_vma_name *anon_name = vma->vm_mm ? anon_vma_name(vma) : NULL;
+
+	*name = NULL;
+	*path = NULL;
+	*name_fmt = NULL;
+
+	/*
+	 * Print the dentry name for named mappings, and a
+	 * special [heap] marker for the heap:
+	 */
+	if (vma->vm_file) {
+		/*
+		 * If user named this anon shared memory via
+		 * prctl(PR_SET_VMA ..., use the provided name.
+		 */
+		if (anon_name) {
+			*name_fmt = "[anon_shmem:%s]";
+			*name = anon_name->name;
+		} else {
+			*path = file_user_path(vma->vm_file);
+		}
+		return;
+	}
+
+	if (vma->vm_ops && vma->vm_ops->name) {
+		*name = vma->vm_ops->name(vma);
+		if (*name)
+			return;
+	}
+
+	*name = arch_vma_name(vma);
+	if (*name)
+		return;
+
+	if (!vma->vm_mm) {
+		*name = "[vdso]";
+		return;
+	}
+
+	if (vma_is_initial_heap(vma)) {
+		*name = "[heap]";
+		return;
+	}
+
+	if (vma_is_initial_stack(vma)) {
+		*name = "[stack]";
+		return;
+	}
+
+	if (anon_name) {
+		*name_fmt = "[anon:%s]";
+		*name = anon_name->name;
+		return;
+	}
+}
+
 static void show_vma_header_prefix(struct seq_file *m,
 				   unsigned long start, unsigned long end,
 				   vm_flags_t flags, unsigned long long pgoff,
@@ -262,17 +323,15 @@ static void show_vma_header_prefix(struct seq_file *m,
 static void
 show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
 {
-	struct anon_vma_name *anon_name = NULL;
-	struct mm_struct *mm = vma->vm_mm;
-	struct file *file = vma->vm_file;
+	const struct path *path;
+	const char *name_fmt, *name;
 	vm_flags_t flags = vma->vm_flags;
 	unsigned long ino = 0;
 	unsigned long long pgoff = 0;
 	unsigned long start, end;
 	dev_t dev = 0;
-	const char *name = NULL;
 
-	if (file) {
+	if (vma->vm_file) {
 		const struct inode *inode = file_user_inode(vma->vm_file);
 
 		dev = inode->i_sb->s_dev;
@@ -283,57 +342,15 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
 	start = vma->vm_start;
 	end = vma->vm_end;
 	show_vma_header_prefix(m, start, end, flags, pgoff, dev, ino);
-	if (mm)
-		anon_name = anon_vma_name(vma);
 
-	/*
-	 * Print the dentry name for named mappings, and a
-	 * special [heap] marker for the heap:
-	 */
-	if (file) {
+	get_vma_name(vma, &path, &name, &name_fmt);
+	if (path) {
 		seq_pad(m, ' ');
-		/*
-		 * If user named this anon shared memory via
-		 * prctl(PR_SET_VMA ..., use the provided name.
-		 */
-		if (anon_name)
-			seq_printf(m, "[anon_shmem:%s]", anon_name->name);
-		else
-			seq_path(m, file_user_path(file), "\n");
-		goto done;
-	}
-
-	if (vma->vm_ops && vma->vm_ops->name) {
-		name = vma->vm_ops->name(vma);
-		if (name)
-			goto done;
-	}
-
-	name = arch_vma_name(vma);
-	if (!name) {
-		if (!mm) {
-			name = "[vdso]";
-			goto done;
-		}
-
-		if (vma_is_initial_heap(vma)) {
-			name = "[heap]";
-			goto done;
-		}
-
-		if (vma_is_initial_stack(vma)) {
-			name = "[stack]";
-			goto done;
-		}
-
-		if (anon_name) {
-			seq_pad(m, ' ');
-			seq_printf(m, "[anon:%s]", anon_name->name);
-		}
-	}
-
-done:
-	if (name) {
+		seq_path(m, path, "\n");
+	} else if (name_fmt) {
+		seq_pad(m, ' ');
+		seq_printf(m, name_fmt, name);
+	} else if (name) {
 		seq_pad(m, ' ');
 		seq_puts(m, name);
 	}
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v6 2/6 fs/procfs: implement efficient VMA querying API for /proc/<pid>/maps

In reply to Andrii Nakryiko

/proc/<pid>/maps file is extremely useful in practice for various tasks involving figuring out process memory layout, what files are backing any given memory range, etc. One important class of applications that absolutely rely on this are profilers/stack symbolizers (perf tool being one of them). Patterns of use differ, but they generally would fall into two categories.

In on-demand pattern, a profiler/symbolizer would normally capture stack trace containing absolute memory addresses of some functions, and would then use /proc/<pid>/maps file to find corresponding backing ELF files (normally, only executable VMAs are of interest), file offsets within them, and then continue from there to get yet more information (ELF symbols, DWARF information) to get human-readable symbolic information. This pattern is used by Meta's fleet-wide profiler, as one example.

In preprocessing pattern, application doesn't know the set of addresses of interest, so it has to fetch all relevant VMAs (again, probably only executable ones), store or cache them, then proceed with profiling and stack trace capture. Once done, it would do symbolization based on stored VMA information. This can happen at much later point in time. This patterns is used by perf tool, as an example.

In either case, there are both performance and correctness requirement involved. This address to VMA information translation has to be done as efficiently as possible, but also not miss any VMA (especially in the case of loading/unloading shared libraries). In practice, correctness can't be guaranteed (due to process dying before VMA data can be captured, or shared library being unloaded, etc), but any effort to maximize the chance of finding the VMA is appreciated.

Unfortunately, for all the /proc/<pid>/maps file universality and usefulness, it doesn't fit the above use cases 100%.

First, it's main purpose is to emit all VMAs sequentially, but in practice captured addresses would fall only into a smaller subset of all process' VMAs, mainly containing executable text. Yet, library would need to parse most or all of the contents to find needed VMAs, as there is no way to skip VMAs that are of no use. Efficient library can do the linear pass and it is still relatively efficient, but it's definitely an overhead that can be avoided, if there was a way to do more targeted querying of the relevant VMA information.

Second, it's a text based interface, which makes its programmatic use from applications and libraries more cumbersome and inefficient due to the need to handle text parsing to get necessary pieces of information. The overhead is actually payed both by kernel, formatting originally binary VMA data into text, and then by user space application, parsing it back into binary data for further use.

For the on-demand pattern of usage, described above, another problem when writing generic stack trace symbolization library is an unfortunate performance-vs-correctness tradeoff that needs to be made. Library has to make a decision to either cache parsed contents of /proc/<pid>/maps (after initial processing) to service future requests (if application requests to symbolize another set of addresses (for the same process), captured at some later time, which is typical for periodic/continuous profiling cases) to avoid higher costs of re-parsing this file. Or it has to choose to cache the contents in memory to speed up future requests. In the former case, more memory is used for the cache and there is a risk of getting stale data if application loads or unloads shared libraries, or otherwise changed its set of VMAs somehow, e.g., through additional mmap() calls. In the latter case, it's the performance hit that comes from re-opening the file and re-parsing its contents all over again.

This patch aims to solve this problem by providing a new API built on top of /proc/<pid>/maps. It's meant to address both non-selectiveness and text nature of /proc/<pid>/maps, by giving user more control of what sort of VMA(s) needs to be queried, and being binary-based interface eliminates the overhead of text formatting (on kernel side) and parsing (on user space side).

It's also designed to be extensible and forward/backward compatible by including required struct size field, which user has to provide. We use established copy_struct_from_user() approach to handle extensibility.

User has a choice to pick either getting VMA that covers provided address or -ENOENT if none is found (exact, least surprising, case). Or, with an extra query flag (PROCMAP_QUERY_COVERING_OR_NEXT_VMA), they can get either VMA that covers the address (if there is one), or the closest next VMA (i.e., VMA with the smallest vm_start > addr). The latter allows more efficient use, but, given it could be a surprising behavior, requires an explicit opt-in.

There is another query flag that is useful for some use cases. PROCMAP_QUERY_FILE_BACKED_VMA instructs this API to only return file-backed VMAs. Combining this with PROCMAP_QUERY_COVERING_OR_NEXT_VMA makes it possible to efficiently iterate only file-backed VMAs of the process, which is what profilers/symbolizers are normally interested in.

All the above querying flags can be combined with (also optional) set of desired VMA permissions flags. This allows to, for example, iterate only an executable subset of VMAs, which is what preprocessing pattern, used by perf tool, would benefit from, as the assumption is that captured stack traces would have addresses of executable code. This saves time by skipping non-executable VMAs altogether efficienty.

All these querying flags (modifiers) are orthogonal and can be combined in a semantically meaningful and natural way.

Basing this ioctl()-based API on top of /proc/<pid>/maps's FD makes sense given it's querying the same set of VMA data. It's also benefitial because permission checks for /proc/<pid>/maps is performed at open time once, and the actual data read of text contents of /proc/<pid>/maps is done without further permission checks. We piggyback on this pattern with ioctl()-based API as well, as that's a desired property. Both for performance reasons, but also for security and flexibility reasons.

Allowing application to open an FD for /proc/self/maps without any extra capabilities, and then passing it to some sort of profiling agent through Unix-domain socket, would allow such profiling agent to not require some of the capabilities that are otherwise expected when opening /proc/<pid>/maps file for *another* process. This is a desirable property for some more restricted setups.

This new ioctl-based implementation doesn't interfere with seq_file-based implementation of /proc/<pid>/maps textual interface, and so could be used together or independently without paying any price for that.

Note also, that fetching VMA name (e.g., backing file path, or special hard-coded or user-provided names) is optional just like build ID. If user sets vma_name_size to zero, kernel code won't attempt to retrieve it, saving resources.

Earlier versions of this patch set were adding per-VMA locking, which is why we have a code structure that is ready for abstracting mmap_lock vs vm_lock differences (query_vma_setup(), query_vma_teardown(), and query_vma_find_by_addr()), but given anon_vma_name() is not yet compatible with per-VMA locking, initial implementation sticks to using only mmap_lock for now. It will be easy to add back per-VMA locking once all the pieces are ready later on. Which is why we keep existing code structure with setup/teardown/query helper functions.

---
 fs/proc/task_mmu.c      | 235 ++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/fs.h | 130 +++++++++++++++++++++-
 2 files changed, 364 insertions(+), 1 deletion(-)
fs/proc/task_mmu.c +235 -0
index 507b7dc7c4c8..674405b99d0d 100644
@@ -375,11 +375,246 @@ static int pid_maps_open(struct inode *inode, struct file *file)
 	return do_maps_open(inode, file, &proc_pid_maps_op);
 }
 
+#define PROCMAP_QUERY_VMA_FLAGS (				\
+		PROCMAP_QUERY_VMA_READABLE |			\
+		PROCMAP_QUERY_VMA_WRITABLE |			\
+		PROCMAP_QUERY_VMA_EXECUTABLE |			\
+		PROCMAP_QUERY_VMA_SHARED			\
+)
+
+#define PROCMAP_QUERY_VALID_FLAGS_MASK (			\
+		PROCMAP_QUERY_COVERING_OR_NEXT_VMA |		\
+		PROCMAP_QUERY_FILE_BACKED_VMA |			\
+		PROCMAP_QUERY_VMA_FLAGS				\
+)
+
+static int query_vma_setup(struct mm_struct *mm)
+{
+	return mmap_read_lock_killable(mm);
+}
+
+static void query_vma_teardown(struct mm_struct *mm, struct vm_area_struct *vma)
+{
+	mmap_read_unlock(mm);
+}
+
+static struct vm_area_struct *query_vma_find_by_addr(struct mm_struct *mm, unsigned long addr)
+{
+	return find_vma(mm, addr);
+}
+
+static struct vm_area_struct *query_matching_vma(struct mm_struct *mm,
+						 unsigned long addr, u32 flags)
+{
+	struct vm_area_struct *vma;
+
+next_vma:
+	vma = query_vma_find_by_addr(mm, addr);
+	if (!vma)
+		goto no_vma;
+
+	/* user requested only file-backed VMA, keep iterating */
+	if ((flags & PROCMAP_QUERY_FILE_BACKED_VMA) && !vma->vm_file)
+		goto skip_vma;
+
+	/* VMA permissions should satisfy query flags */
+	if (flags & PROCMAP_QUERY_VMA_FLAGS) {
+		u32 perm = 0;
+
+		if (flags & PROCMAP_QUERY_VMA_READABLE)
+			perm |= VM_READ;
+		if (flags & PROCMAP_QUERY_VMA_WRITABLE)
+			perm |= VM_WRITE;
+		if (flags & PROCMAP_QUERY_VMA_EXECUTABLE)
+			perm |= VM_EXEC;
+		if (flags & PROCMAP_QUERY_VMA_SHARED)
+			perm |= VM_MAYSHARE;
+
+		if ((vma->vm_flags & perm) != perm)
+			goto skip_vma;
+	}
+
+	/* found covering VMA or user is OK with the matching next VMA */
+	if ((flags & PROCMAP_QUERY_COVERING_OR_NEXT_VMA) || vma->vm_start <= addr)
+		return vma;
+
+skip_vma:
+	/*
+	 * If the user needs closest matching VMA, keep iterating.
+	 */
+	addr = vma->vm_end;
+	if (flags & PROCMAP_QUERY_COVERING_OR_NEXT_VMA)
+		goto next_vma;
+no_vma:
+	return ERR_PTR(-ENOENT);
+}
+
+static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
+{
+	struct procmap_query karg;
+	struct vm_area_struct *vma;
+	struct mm_struct *mm;
+	const char *name = NULL;
+	char *name_buf = NULL;
+	__u64 usize;
+	int err;
+
+	if (copy_from_user(&usize, (void __user *)uarg, sizeof(usize)))
+		return -EFAULT;
+	/* argument struct can never be that large, reject abuse */
+	if (usize > PAGE_SIZE)
+		return -E2BIG;
+	/* argument struct should have at least query_flags and query_addr fields */
+	if (usize < offsetofend(struct procmap_query, query_addr))
+		return -EINVAL;
+	err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
+	if (err)
+		return err;
+
+	/* reject unknown flags */
+	if (karg.query_flags & ~PROCMAP_QUERY_VALID_FLAGS_MASK)
+		return -EINVAL;
+	/* either both buffer address and size are set, or both should be zero */
+	if (!!karg.vma_name_size != !!karg.vma_name_addr)
+		return -EINVAL;
+
+	mm = priv->mm;
+	if (!mm || !mmget_not_zero(mm))
+		return -ESRCH;
+
+	err = query_vma_setup(mm);
+	if (err) {
+		mmput(mm);
+		return err;
+	}
+
+	vma = query_matching_vma(mm, karg.query_addr, karg.query_flags);
+	if (IS_ERR(vma)) {
+		err = PTR_ERR(vma);
+		vma = NULL;
+		goto out;
+	}
+
+	karg.vma_start = vma->vm_start;
+	karg.vma_end = vma->vm_end;
+
+	karg.vma_flags = 0;
+	if (vma->vm_flags & VM_READ)
+		karg.vma_flags |= PROCMAP_QUERY_VMA_READABLE;
+	if (vma->vm_flags & VM_WRITE)
+		karg.vma_flags |= PROCMAP_QUERY_VMA_WRITABLE;
+	if (vma->vm_flags & VM_EXEC)
+		karg.vma_flags |= PROCMAP_QUERY_VMA_EXECUTABLE;
+	if (vma->vm_flags & VM_MAYSHARE)
+		karg.vma_flags |= PROCMAP_QUERY_VMA_SHARED;
+
+	karg.vma_page_size = vma_kernel_pagesize(vma);
+
+	if (vma->vm_file) {
+		const struct inode *inode = file_user_inode(vma->vm_file);
+
+		karg.vma_offset = ((__u64)vma->vm_pgoff) << PAGE_SHIFT;
+		karg.dev_major = MAJOR(inode->i_sb->s_dev);
+		karg.dev_minor = MINOR(inode->i_sb->s_dev);
+		karg.inode = inode->i_ino;
+	} else {
+		karg.vma_offset = 0;
+		karg.dev_major = 0;
+		karg.dev_minor = 0;
+		karg.inode = 0;
+	}
+
+	if (karg.build_id_size) {
+		__u32 build_id_sz;
+
+		err = build_id_parse(vma, build_id_buf, &build_id_sz);
+		if (err) {
+			karg.build_id_size = 0;
+		} else {
+			if (karg.build_id_size < build_id_sz) {
+				err = -ENAMETOOLONG;
+				goto out;
+			}
+			karg.build_id_size = build_id_sz;
+		}
+	}
+
+	if (karg.vma_name_size) {
+		size_t name_buf_sz = min_t(size_t, PATH_MAX, karg.vma_name_size);
+		const struct path *path;
+		const char *name_fmt;
+		size_t name_sz = 0;
+
+		get_vma_name(vma, &path, &name, &name_fmt);
+
+		if (path || name_fmt || name) {
+			name_buf = kmalloc(name_buf_sz, GFP_KERNEL);
+			if (!name_buf) {
+				err = -ENOMEM;
+				goto out;
+			}
+		}
+		if (path) {
+			name = d_path(path, name_buf, name_buf_sz);
+			if (IS_ERR(name)) {
+				err = PTR_ERR(name);
+				goto out;
+			}
+			name_sz = name_buf + name_buf_sz - name;
+		} else if (name || name_fmt) {
+			name_sz = 1 + snprintf(name_buf, name_buf_sz, name_fmt ?: "%s", name);
+			name = name_buf;
+		}
+		if (name_sz > name_buf_sz) {
+			err = -ENAMETOOLONG;
+			goto out;
+		}
+		karg.vma_name_size = name_sz;
+	}
+
+	/* unlock vma or mmap_lock, and put mm_struct before copying data to user */
+	query_vma_teardown(mm, vma);
+	mmput(mm);
+
+	if (karg.vma_name_size && copy_to_user((void __user *)karg.vma_name_addr,
+					       name, karg.vma_name_size)) {
+		kfree(name_buf);
+		return -EFAULT;
+	}
+	kfree(name_buf);
+
+	if (copy_to_user(uarg, &karg, min_t(size_t, sizeof(karg), usize)))
+		return -EFAULT;
+
+	return 0;
+
+out:
+	query_vma_teardown(mm, vma);
+	mmput(mm);
+	kfree(name_buf);
+	return err;
+}
+
+static long procfs_procmap_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct seq_file *seq = file->private_data;
+	struct proc_maps_private *priv = seq->private;
+
+	switch (cmd) {
+	case PROCMAP_QUERY:
+		return do_procmap_query(priv, (void __user *)arg);
+	default:
+		return -ENOIOCTLCMD;
+	}
+}
+
 const struct file_operations proc_pid_maps_operations = {
 	.open		= pid_maps_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
 	.release	= proc_map_release,
+	.unlocked_ioctl = procfs_procmap_ioctl,
+	.compat_ioctl	= procfs_procmap_ioctl,
 };
 
 /*
include/uapi/linux/fs.h +129 -1

141 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH v6 3/6 fs/procfs: add build ID fetching to PROCMAP_QUERY API

In reply to Andrii Nakryiko

The need to get ELF build ID reliably is an important aspect when dealing with profiling and stack trace symbolization, and /proc/<pid>/maps textual representation doesn't help with this.

To get backing file's ELF build ID, application has to first resolve VMA, then use it's start/end address range to follow a special /proc/<pid>/map_files/<start>-<end> symlink to open the ELF file (this is necessary because backing file might have been removed from the disk or was already replaced with another binary in the same file path.

Such approach, beyond just adding complexity of having to do a bunch of extra work, has extra security implications. Because application opens underlying ELF file and needs read access to its entire contents (as far as kernel is concerned), kernel puts additional capable() checks on following /proc/<pid>/map_files/<start>-<end> symlink. And that makes sense in general.

But in the case of build ID, profiler/symbolizer doesn't need the contents of ELF file, per se. It's only build ID that is of interest, and ELF build ID itself doesn't provide any sensitive information.

So this patch adds a way to request backing file's ELF build ID along the rest of VMA information in the same API. User has control over whether this piece of information is requested or not by either setting build_id_size field to zero or non-zero maximum buffer size they provided through build_id_addr field (which encodes user pointer as __u64 field). This is a completely optional piece of information, and so has no performance implications for user cases that don't care about build ID, while improving performance and simplifying the setup for those application that do need it.

Kernel already implements build ID fetching, which is used from BPF subsystem. We are reusing this code here, but plan a follow up changes to make it work better under more relaxed assumption (compared to what existing code assumes) of being called from user process context, in which page faults are allowed. BPF-specific implementation currently bails out if necessary part of ELF file is not paged in, all due to extra BPF-specific restrictions (like the need to fetch build ID in restrictive contexts such as NMI handler).

---
 fs/proc/task_mmu.c      | 25 ++++++++++++++++++++++++-
 include/uapi/linux/fs.h | 28 ++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 1 deletion(-)
fs/proc/task_mmu.c +24 -1
index 674405b99d0d..32bef3eeab7f 100644
@@ -22,6 +22,7 @@
 #include <linux/pkeys.h>
 #include <linux/minmax.h>
 #include <linux/overflow.h>
+#include <linux/buildid.h>
 
 #include <asm/elf.h>
 #include <asm/tlb.h>
@@ -445,6 +446,7 @@ static struct vm_area_struct *query_matching_vma(struct mm_struct *mm,
 	addr = vma->vm_end;
 	if (flags & PROCMAP_QUERY_COVERING_OR_NEXT_VMA)
 		goto next_vma;
+
 no_vma:
 	return ERR_PTR(-ENOENT);
 }
@@ -455,7 +457,7 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
 	struct vm_area_struct *vma;
 	struct mm_struct *mm;
 	const char *name = NULL;
-	char *name_buf = NULL;
+	char build_id_buf[BUILD_ID_SIZE_MAX], *name_buf = NULL;
 	__u64 usize;
 	int err;
 
@@ -477,6 +479,8 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
 	/* either both buffer address and size are set, or both should be zero */
 	if (!!karg.vma_name_size != !!karg.vma_name_addr)
 		return -EINVAL;
+	if (!!karg.build_id_size != !!karg.build_id_addr)
+		return -EINVAL;
 
 	mm = priv->mm;
 	if (!mm || !mmget_not_zero(mm))
@@ -539,6 +543,21 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
 		}
 	}
 
+	if (karg.build_id_size) {
+		__u32 build_id_sz;
+
+		err = build_id_parse(vma, build_id_buf, &build_id_sz);
+		if (err) {
+			karg.build_id_size = 0;
+		} else {
+			if (karg.build_id_size < build_id_sz) {
+				err = -ENAMETOOLONG;
+				goto out;
+			}
+			karg.build_id_size = build_id_sz;
+		}
+	}
+
 	if (karg.vma_name_size) {
 		size_t name_buf_sz = min_t(size_t, PATH_MAX, karg.vma_name_size);
 		const struct path *path;
@@ -583,6 +602,10 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
 	}
 	kfree(name_buf);
 
+	if (karg.build_id_size && copy_to_user((void __user *)karg.build_id_addr,
+					       build_id_buf, karg.build_id_size))
+		return -EFAULT;
+
 	if (copy_to_user(uarg, &karg, min_t(size_t, sizeof(karg), usize)))
 		return -EFAULT;
 
include/uapi/linux/fs.h +28 -0
index 5d440f9b5d92..2a4a5f50c98e 100644
@@ -511,6 +511,26 @@ struct procmap_query {
 	 * If set to zero, vma_name_addr should be set to zero as well
 	 */
 	__u32 vma_name_size;		/* in/out */
+	/*
+	 * If set to non-zero value, signals the request to extract and return
+	 * VMA's backing file's build ID, if the backing file is an ELF file
+	 * and it contains embedded build ID.
+	 *
+	 * Kernel will set this field to zero, if VMA has no backing file,
+	 * backing file is not an ELF file, or ELF file has no build ID
+	 * embedded.
+	 *
+	 * Build ID is a binary value (not a string). Kernel will set
+	 * build_id_size field to exact number of bytes used for build ID.
+	 * If build ID is requested and present, but needs more bytes than
+	 * user-supplied maximum buffer size (see build_id_addr field below),
+	 * -E2BIG error will be returned.
+	 *
+	 * If this field is set to non-zero value, build_id_addr should point
+	 * to valid user space memory buffer of at least build_id_size bytes.
+	 * If set to zero, build_id_addr should be set to zero as well
+	 */
+	__u32 build_id_size;		/* in/out */
 	/*
 	 * User-supplied address of a buffer of at least vma_name_size bytes
 	 * for kernel to fill with matched VMA's name (see vma_name_size field
@@ -519,6 +539,14 @@ struct procmap_query {
 	 * Should be set to zero if VMA name should not be returned.
 	 */
 	__u64 vma_name_addr;		/* in */
+	/*
+	 * User-supplied address of a buffer of at least build_id_size bytes
+	 * for kernel to fill with matched VMA's ELF build ID, if available
+	 * (see build_id_size field description above for details).
+	 *
+	 * Should be set to zero if build ID should not be returned.
+	 */
+	__u64 build_id_addr;		/* in */
 };
 
 #endif /* _UAPI_LINUX_FS_H */
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

Quoted 10 lines · Andrii Nakryiko

The need to get ELF build ID reliably is an important aspect when dealing with profiling and stack trace symbolization, and /proc/<pid>/maps textual representation doesn't help with this.

To get backing file's ELF build ID, application has to first resolve VMA, then use it's start/end address range to follow a special /proc/<pid>/map_files/<start>-<end> symlink to open the ELF file (this is necessary because backing file might have been removed from the disk or was already replaced with another binary in the same file path.

Quoted 6 lines

Such approach, beyond just adding complexity of having to do a bunch of extra work, has extra security implications. Because application opens underlying ELF file and needs read access to its entire contents (as far as kernel is concerned), kernel puts additional capable() checks on following /proc/<pid>/map_files/<start>-<end> symlink. And that makes sense in general.

I was curious about this statement. It has still certainly potential for side channels e.g. for files that are execute only, or with some other special protection.

But actually just looking at the parsing code it seems to fail basic TOCTTOU rules, and since you don't check if the VMA mapping is executable (I think), so there's no EBUSY checking for writes, it likely is exploitable.

        /* only supports phdr that fits in one page */
                if (ehdr->e_phnum >
                   (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
                <---------- check in memory
                                return -EINVAL;

        phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));

<---- but page is shared in the page cache. So if anybody manages to map it for write

        for (i = 0; i < ehdr->e_phnum; ++i) {   <----- this loop can go
                        off into the next page.
                        if (phdr[i].p_type == PT_NOTE &&
                                            !parse_build_id(page_addr, build_id, size,
                                                            page_addr + phdr[i].p_offset,
                                                            phdr[i].p_filesz))
                                                                                    return 0;

Here's an untested patch

lib/buildid.c +7 -5
index 7954dd92e36c..6c022fcd03ec 100644
@@ -72,19 +72,20 @@ static int get_build_id_32(const void *page_addr, unsigned char *build_id,
 	Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
 	Elf32_Phdr *phdr;
 	int i;
+	unsigned phnum = READ_ONCE(ehdr->e_phnum);
 
 	/* only supports phdr that fits in one page */
-	if (ehdr->e_phnum >
+	if (phnum >
 	    (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
 		return -EINVAL;
 
 	phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
+	for (i = 0; i < phnum; ++i) {
 		if (phdr[i].p_type == PT_NOTE &&
 		    !parse_build_id(page_addr, build_id, size,
 				    page_addr + phdr[i].p_offset,
-				    phdr[i].p_filesz))
+				    READ_ONCE(phdr[i].p_filesz)))
 			return 0;
 	}
 	return -EINVAL;
@@ -97,15 +98,16 @@ static int get_build_id_64(const void *page_addr, unsigned char *build_id,
 	Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
 	Elf64_Phdr *phdr;
 	int i;
+	unsigned phnum = READ_ONCE(ehdr->e_phnum);
 
 	/* only supports phdr that fits in one page */
-	if (ehdr->e_phnum >
+	if (phnum >
 	    (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
 		return -EINVAL;
 
 	phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
 
-	for (i = 0; i < ehdr->e_phnum; ++i) {
+	for (i = 0; i < phnum; ++i) {
 		if (phdr[i].p_type == PT_NOTE &&
 		    !parse_build_id(page_addr, build_id, size,
 				    page_addr + phdr[i].p_offset,
#5 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

Quoted 49 lines · Andi Kleen
Quoted 10 lines · Andrii Nakryiko

The need to get ELF build ID reliably is an important aspect when dealing with profiling and stack trace symbolization, and /proc/<pid>/maps textual representation doesn't help with this.

To get backing file's ELF build ID, application has to first resolve VMA, then use it's start/end address range to follow a special /proc/<pid>/map_files/<start>-<end> symlink to open the ELF file (this is necessary because backing file might have been removed from the disk or was already replaced with another binary in the same file path.

Quoted 6 lines

Such approach, beyond just adding complexity of having to do a bunch of extra work, has extra security implications. Because application opens underlying ELF file and needs read access to its entire contents (as far as kernel is concerned), kernel puts additional capable() checks on following /proc/<pid>/map_files/<start>-<end> symlink. And that makes sense in general.

I was curious about this statement. It has still certainly potential for side channels e.g. for files that are execute only, or with some other special protection.

But actually just looking at the parsing code it seems to fail basic TOCTTOU rules, and since you don't check if the VMA mapping is executable (I think), so there's no EBUSY checking for writes, it likely is exploitable.

        /* only supports phdr that fits in one page */
                if (ehdr->e_phnum >
                   (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
                <---------- check in memory
                                return -EINVAL;

        phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));

<---- but page is shared in the page cache. So if anybody manages to map it for write

        for (i = 0; i < ehdr->e_phnum; ++i) {   <----- this loop can go
                        off into the next page.
                        if (phdr[i].p_type == PT_NOTE &&
                                            !parse_build_id(page_addr, build_id, size,
                                                            page_addr + phdr[i].p_offset,
                                                            phdr[i].p_filesz))
                                                                                    return 0;
Quoted 3 lines

Here's an untested patch

Yep, makes sense. I'm currently reworking this whole lib/buildid.c implementation to remove all the restrictions on data being in the first page only, and making it work in a faultable context more reliably. I can audit the code for TOCTOU issues and incorporate your feedback. I'll probably post the patch set next week, will cc you as well.

Quoted 47 lines
lib/buildid.c +7 -5
index 7954dd92e36c..6c022fcd03ec 100644
@@ -72,19 +72,20 @@ static int get_build_id_32(const void *page_addr, unsigned char *build_id,
        Elf32_Ehdr *ehdr = (Elf32_Ehdr *)page_addr;
        Elf32_Phdr *phdr;
        int i;
+       unsigned phnum = READ_ONCE(ehdr->e_phnum);
 
        /* only supports phdr that fits in one page */
-       if (ehdr->e_phnum >
+       if (phnum >
            (PAGE_SIZE - sizeof(Elf32_Ehdr)) / sizeof(Elf32_Phdr))
                return -EINVAL;
 
        phdr = (Elf32_Phdr *)(page_addr + sizeof(Elf32_Ehdr));
 
-       for (i = 0; i < ehdr->e_phnum; ++i) {
+       for (i = 0; i < phnum; ++i) {
                if (phdr[i].p_type == PT_NOTE &&
                    !parse_build_id(page_addr, build_id, size,
                                    page_addr + phdr[i].p_offset,
-                                   phdr[i].p_filesz))
+                                   READ_ONCE(phdr[i].p_filesz)))
                        return 0;
        }
        return -EINVAL;
@@ -97,15 +98,16 @@ static int get_build_id_64(const void *page_addr, unsigned char *build_id,
        Elf64_Ehdr *ehdr = (Elf64_Ehdr *)page_addr;
        Elf64_Phdr *phdr;
        int i;
+       unsigned phnum = READ_ONCE(ehdr->e_phnum);
 
        /* only supports phdr that fits in one page */
-       if (ehdr->e_phnum >
+       if (phnum >
            (PAGE_SIZE - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr))
                return -EINVAL;
 
        phdr = (Elf64_Phdr *)(page_addr + sizeof(Elf64_Ehdr));
 
-       for (i = 0; i < ehdr->e_phnum; ++i) {
+       for (i = 0; i < phnum; ++i) {
                if (phdr[i].p_type == PT_NOTE &&
                    !parse_build_id(page_addr, build_id, size,
                                    page_addr + phdr[i].p_offset,
#6 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

Quoted 6 lines

Yep, makes sense. I'm currently reworking this whole lib/buildid.c implementation to remove all the restrictions on data being in the first page only, and making it work in a faultable context more reliably. I can audit the code for TOCTOU issues and incorporate your feedback. I'll probably post the patch set next week, will cc you as well.

Please also add checks that the mapping is executable, to close the obscure "can check the first 4 bytes of every mapped file is ELF\0" hole.

But it will still need the hardening because mappings from ld.so are not EBUSY for writes.

-Andi

#7 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

Quoted 8 lines · Andi Kleen
Quoted 7 lines

Yep, makes sense. I'm currently reworking this whole lib/buildid.c implementation to remove all the restrictions on data being in the first page only, and making it work in a faultable context more reliably. I can audit the code for TOCTOU issues and incorporate your feedback. I'll probably post the patch set next week, will cc you as well.

Quoted 6 lines

Please also add checks that the mapping is executable, to close the obscure "can check the first 4 bytes of every mapped file is ELF\0" hole.

But it will still need the hardening because mappings from ld.so are not EBUSY for writes.

I'm a bit confused. Two things:

1) non-executable file-backed VMA still has build ID associated with it. Note, build ID is extracted from the backing file's content, not from VMA itself. The part of ELF file that contains build ID isn't necessarily mmap()'ed at all

2) What sort of exploitation are we talking about here? it's not enough for backing file to have correct 4 starting bytes (0x7f"ELF"), we still have to find correct PT_NOTE segment, and .note.gnu.build-id section within it, that has correct type (3) and key name "GNU".

I'm trying to understand what we are protecting against here. Especially that opening /proc/<pid>/maps already requires PTRACE_MODE_READ permissions anyways (or pid should be self).

Quoted 2 lines

-Andi

#8 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

Quoted 4 lines

1) non-executable file-backed VMA still has build ID associated with it. Note, build ID is extracted from the backing file's content, not from VMA itself. The part of ELF file that contains build ID isn't necessarily mmap()'ed at all

That's true, but there should be at least one executable mapping for any useful ELF file.

Basically such a check guarantee that you cannot tell anything about a non x mapping not related to ELF.

Quoted 5 lines

2) What sort of exploitation are we talking about here? it's not enough for backing file to have correct 4 starting bytes (0x7f"ELF"), we still have to find correct PT_NOTE segment, and .note.gnu.build-id section within it, that has correct type (3) and key name "GNU".

There's a timing side channel, you can tell where the checks stop. I don't think it's a big problem, but it's still better to avoid such leaks in the first place as much as possible.

Quoted 4 lines

I'm trying to understand what we are protecting against here. Especially that opening /proc/<pid>/maps already requires PTRACE_MODE_READ permissions anyways (or pid should be self).

While that's true for the standard security permission model there might be non standard ones where the relationship is more complicated.

-Andi

#9 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

Quoted 6 lines · Andi Kleen
Quoted 5 lines

1) non-executable file-backed VMA still has build ID associated with it. Note, build ID is extracted from the backing file's content, not from VMA itself. The part of ELF file that contains build ID isn't necessarily mmap()'ed at all

Quoted 6 lines

That's true, but there should be at least one executable mapping for any useful ELF file.

Basically such a check guarantee that you cannot tell anything about a non x mapping not related to ELF.

Ok, I can add this check. If you know off the top of your head how to do that for struct address_space, I'd appreciate the pointer. Quick glance didn't show anything useful in linux/fs.h, but I'll dig deeper a bit later.

Quoted 11 lines
Quoted 5 lines

2) What sort of exploitation are we talking about here? it's not enough for backing file to have correct 4 starting bytes (0x7f"ELF"), we still have to find correct PT_NOTE segment, and .note.gnu.build-id section within it, that has correct type (3) and key name "GNU".

There's a timing side channel, you can tell where the checks stop. I don't think it's a big problem, but it's still better to avoid such leaks in the first place as much as possible.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

I'm trying to understand what we are protecting against here. Especially that opening /proc/<pid>/maps already requires PTRACE_MODE_READ permissions anyways (or pid should be self).

While that's true for the standard security permission model there might be non standard ones where the relationship is more complicated.

Presumably non-standard ones will have more and custom security checks (LSM, seccomp, etc) involved. Basically, I acknowledge your point, but I'm not sure it changes anything about adding this API.

Quoted 1 line

-Andi

#10 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

In reply to Andi Kleen
Quoted 11 lines · Andi Kleen
Quoted 5 lines

1) non-executable file-backed VMA still has build ID associated with it. Note, build ID is extracted from the backing file's content, not from VMA itself. The part of ELF file that contains build ID isn't necessarily mmap()'ed at all

That's true, but there should be at least one executable mapping for any useful ELF file.

Basically such a check guarantee that you cannot tell anything about a non x mapping not related to ELF.

Hey Andi,

So when we were discussing this I was imagining that inode/address_space does have something like VMA's VM_MAYEXEC flag and it would be easy and fast to check that. But it doesn't seem so.

So what exactly did you have in mind when you were proposing that check? Did you mean to do a pass over all VMAs within the process to check if there is at least one executable VMA belonging to address_space? If yes, then that would certainly be way too expensive to be usable.

If I missed something obvious, please point me in the right direction.

As it stands, I don't see any reasonable way to check what you asked performantly. And given this is a bit of over-cautious check, I'm inclined to just not add it. Worst case someone with PTRACE_MODE_READ access would be able to tell if the first 4 bytes of a file are ELF signature or not. Given PTRACE_MODE_READ, I'd imagine that's not really a problem.

Quoted 20 lines
Quoted 6 lines

2) What sort of exploitation are we talking about here? it's not enough for backing file to have correct 4 starting bytes (0x7f"ELF"), we still have to find correct PT_NOTE segment, and .note.gnu.build-id section within it, that has correct type (3) and key name "GNU".

There's a timing side channel, you can tell where the checks stop. I don't think it's a big problem, but it's still better to avoid such leaks in the first place as much as possible.

Quoted 4 lines

I'm trying to understand what we are protecting against here. Especially that opening /proc/<pid>/maps already requires PTRACE_MODE_READ permissions anyways (or pid should be self).

While that's true for the standard security permission model there might be non standard ones where the relationship is more complicated.

-Andi

#11 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

Quoted 5 lines

So what exactly did you have in mind when you were proposing that check? Did you mean to do a pass over all VMAs within the process to check if there is at least one executable VMA belonging to address_space? If yes, then that would certainly be way too expensive to be usable.

I was thinking to only report the build ID when the VMA queried is executable. If software wanted to look up a data symbol and needs that buildid it would need to check a x vma too.

Normally tools iterate over all the mappings anyways so this shouldn't be a big burden for them.

Did I miss something?

I guess an alternative would be a new VMA flag, but iirc we're low on bits there already.

-Andi

#12 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

Quoted 10 lines · Andi Kleen
Quoted 6 lines

So what exactly did you have in mind when you were proposing that check? Did you mean to do a pass over all VMAs within the process to check if there is at least one executable VMA belonging to address_space? If yes, then that would certainly be way too expensive to be usable.

I was thinking to only report the build ID when the VMA queried is executable. If software wanted to look up a data symbol and needs that buildid it would need to check a x vma too.

I think that's way too restrictive and for no good reason, tbh. If there is some .rodata ELF section mapped as r/o VMA, I don't see any reason why user shouldn't be able to request build ID for it.

Quoted 4 lines

Normally tools iterate over all the mappings anyways so this shouldn't be a big burden for them.

This API aims to make this unnecessary. So that tools can request only relevant VMAs based on whatever captured data or code addresses it got from, say, profiling of perf events. And if there are some locks or other global data structures that fall into mapped portions of ELF data sections, the ability to get build ID for those is just as important as getting build ID for executable sections.

Quoted 4 lines

Did I miss something?

I guess an alternative would be a new VMA flag, but iirc we're low on bits there already.

I think we should just keep things as is. I don't think there is any real added security in restricting this just to executable VMAs.

Quoted 2 lines

-Andi

reviewer
#13 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

In reply to Andrii Nakryiko
Quoted 3 lines · Andrii Nakryiko

The need to get ELF build ID reliably is an important aspect when dealing with profiling and stack trace symbolization, and /proc/<pid>/maps textual representation doesn't help with this.

[...]

Quoted 8 lines
fs/proc/task_mmu.c +4 -0
@@ -539,6 +543,21 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
                }
        }
 
+       if (karg.build_id_size) {
+               __u32 build_id_sz;
+
+               err = build_id_parse(vma, build_id_buf, &build_id_sz);

fs/proc/task_mmu.c static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) · line 559

Quoted 10 lines
fs/proc/task_mmu.c +10 -0
+               if (err) {
+                       karg.build_id_size = 0;
+               } else {
+                       if (karg.build_id_size < build_id_sz) {
+                               err = -ENAMETOOLONG;
+                               goto out;
+                       }
+                       karg.build_id_size = build_id_sz;
+               }
+       }

The diff doesn't have enough context lines to see it here, but the two closing curly braces above are another copy of exactly the same code block from the preceding patch. The current state in mainline looks like this, with two repetitions of exactly the same block:

[...]

                karg.dev_minor = 0;
                karg.inode = 0;
        }

        if (karg.build_id_size) {
                __u32 build_id_sz;

                err = build_id_parse(vma, build_id_buf, &build_id_sz);
                if (err) {
                        karg.build_id_size = 0;
                } else {
                        if (karg.build_id_size < build_id_sz) {
                                err = -ENAMETOOLONG;
                                goto out;
                        }
                        karg.build_id_size = build_id_sz;
                }
        }

        if (karg.build_id_size) {
                __u32 build_id_sz;

                err = build_id_parse(vma, build_id_buf, &build_id_sz);
                if (err) {
                        karg.build_id_size = 0;
                } else {
                        if (karg.build_id_size < build_id_sz) {
                                err = -ENAMETOOLONG;
                                goto out;
                        }
                        karg.build_id_size = build_id_sz;
                }
        }

        if (karg.vma_name_size) {

[...]

#14 lore raw

Re: [PATCH v6 3/6] fs/procfs: add build ID fetching to PROCMAP_QUERY API

Quoted 21 lines · Jann Horn
Quoted 3 lines · Andrii Nakryiko

The need to get ELF build ID reliably is an important aspect when dealing with profiling and stack trace symbolization, and /proc/<pid>/maps textual representation doesn't help with this.

[...]

Quoted 15 lines
fs/proc/task_mmu.c +11 -0
@@ -539,6 +543,21 @@ static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg)
                }
        }
 
+       if (karg.build_id_size) {
+               __u32 build_id_sz;
+
+               err = build_id_parse(vma, build_id_buf, &build_id_sz);
+               if (err) {
+                       karg.build_id_size = 0;
+               } else {
+                       if (karg.build_id_size < build_id_sz) {
+                               err = -ENAMETOOLONG;
+                               goto out;
+                       }
Quoted 8 lines

fs/proc/task_mmu.c static int do_procmap_query(struct proc_maps_private *priv, void __user *uarg) · line 559

Quoted 3 lines
fs/proc/task_mmu.c +3 -0
+                       karg.build_id_size = build_id_sz;
+               }
+       }

The diff doesn't have enough context lines to see it here, but the two closing curly braces above are another copy of exactly the same code block from the preceding patch. The current state in mainline looks like this, with two repetitions of exactly the same block:

Yeah, you are right, thanks for the heads up! Seems like a rebase screw up which duplicated build_id logic. It doesn't have any negative effects besides doing the same work twice (if build ID parsing is requested), but I'll definitely will send a fix to drop the duplication.

Quoted 38 lines

[...]

                karg.dev_minor = 0;
                karg.inode = 0;
        }

        if (karg.build_id_size) {
                __u32 build_id_sz;

                err = build_id_parse(vma, build_id_buf, &build_id_sz);
                if (err) {
                        karg.build_id_size = 0;
                } else {
                        if (karg.build_id_size < build_id_sz) {
                                err = -ENAMETOOLONG;
                                goto out;
                        }
                        karg.build_id_size = build_id_sz;
                }
        }

        if (karg.build_id_size) {
                __u32 build_id_sz;

                err = build_id_parse(vma, build_id_buf, &build_id_sz);
                if (err) {
                        karg.build_id_size = 0;
                } else {
                        if (karg.build_id_size < build_id_sz) {
                                err = -ENAMETOOLONG;
                                goto out;
                        }
                        karg.build_id_size = build_id_sz;
                }
        }

        if (karg.vma_name_size) {

[...]

author maintainer
#15 lore raw

PATCH v6 4/6 docs/procfs: call out ioctl()-based PROCMAP_QUERY command existence

In reply to Andrii Nakryiko

Call out PROCMAP_QUERY ioctl() existence in the section describing /proc/PID/maps file in documentation. We refer user to UAPI header for low-level details of this programmatic interface.

---
 Documentation/filesystems/proc.rst | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/filesystems/proc.rst +9 -0
index 82d142de3461..e834779d9611 100644
@@ -443,6 +443,15 @@ is not associated with a file:
 
  or if empty, the mapping is anonymous.
 
+Starting with 6.11 kernel, /proc/PID/maps provides an alternative
+ioctl()-based API that gives ability to flexibly and efficiently query and
+filter individual VMAs. This interface is binary and is meant for more
+efficient and easy programmatic use. `struct procmap_query`, defined in
+linux/fs.h UAPI header, serves as an input/output argument to the
+`PROCMAP_QUERY` ioctl() command. See comments in linus/fs.h UAPI header for
+details on query semantics, supported flags, data returned, and general API
+usage information.
+
 The /proc/PID/smaps is an extension based on maps, showing the memory
 consumption for each of the process's mappings. For each mapping (aka Virtual
 Memory Area, or VMA) there is a series of lines such as the following::
Signature · 1 line
2.43.0
author maintainer
#16 lore raw

PATCH v6 5/6 tools: sync uapi/linux/fs.h header into tools subdir

In reply to Andrii Nakryiko

We need this UAPI header in tools/include subdirectory for using it from BPF selftests.

---
 tools/include/uapi/linux/fs.h | 184 +++++++++++++++++++++++++++++++---
 1 file changed, 172 insertions(+), 12 deletions(-)
tools/include/uapi/linux/fs.h +172 -12
index cc3fea99fd43..2a4a5f50c98e 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
-#ifndef _LINUX_FS_H
-#define _LINUX_FS_H
+#ifndef _UAPI_LINUX_FS_H
+#define _UAPI_LINUX_FS_H
 
 /*
  * This file has definitions for some important file table structures
@@ -13,10 +13,14 @@
 #include <linux/limits.h>
 #include <linux/ioctl.h>
 #include <linux/types.h>
+#ifndef __KERNEL__
 #include <linux/fscrypt.h>
+#endif
 
 /* Use of MS_* flags within the kernel is restricted to core mount(2) code. */
+#if !defined(__KERNEL__)
 #include <linux/mount.h>
+#endif
 
 /*
  * It's silly to have NR_OPEN bigger than NR_FILE, but you can change
@@ -24,8 +28,8 @@
  * nr_file rlimit, so it's safe to set up a ridiculously high absolute
  * upper limit on files-per-process.
  *
- * Some programs (notably those using select()) may have to be
- * recompiled to take full advantage of the new limits..
+ * Some programs (notably those using select()) may have to be 
+ * recompiled to take full advantage of the new limits..  
  */
 
 /* Fixed constants first: */
@@ -308,29 +312,31 @@ struct fsxattr {
 typedef int __bitwise __kernel_rwf_t;
 
 /* high priority request, poll if possible */
-#define RWF_HIPRI	((__kernel_rwf_t)0x00000001)
+#define RWF_HIPRI	((__force __kernel_rwf_t)0x00000001)
 
 /* per-IO O_DSYNC */
-#define RWF_DSYNC	((__kernel_rwf_t)0x00000002)
+#define RWF_DSYNC	((__force __kernel_rwf_t)0x00000002)
 
 /* per-IO O_SYNC */
-#define RWF_SYNC	((__kernel_rwf_t)0x00000004)
+#define RWF_SYNC	((__force __kernel_rwf_t)0x00000004)
 
 /* per-IO, return -EAGAIN if operation would block */
-#define RWF_NOWAIT	((__kernel_rwf_t)0x00000008)
+#define RWF_NOWAIT	((__force __kernel_rwf_t)0x00000008)
 
 /* per-IO O_APPEND */
-#define RWF_APPEND	((__kernel_rwf_t)0x00000010)
+#define RWF_APPEND	((__force __kernel_rwf_t)0x00000010)
 
 /* per-IO negation of O_APPEND */
-#define RWF_NOAPPEND	((__kernel_rwf_t)0x00000020)
+#define RWF_NOAPPEND	((__force __kernel_rwf_t)0x00000020)
 
 /* mask of flags supported by the kernel */
 #define RWF_SUPPORTED	(RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT |\
 			 RWF_APPEND | RWF_NOAPPEND)
 
+#define PROCFS_IOCTL_MAGIC 'f'
+
 /* Pagemap ioctl */
-#define PAGEMAP_SCAN	_IOWR('f', 16, struct pm_scan_arg)
+#define PAGEMAP_SCAN	_IOWR(PROCFS_IOCTL_MAGIC, 16, struct pm_scan_arg)
 
 /* Bitmasks provided in pm_scan_args masks and reported in page_region.categories. */
 #define PAGE_IS_WPALLOWED	(1 << 0)
@@ -389,4 +395,158 @@ struct pm_scan_arg {
 	__u64 return_mask;
 };
 
-#endif /* _LINUX_FS_H */
+/* /proc/<pid>/maps ioctl */
+#define PROCMAP_QUERY	_IOWR(PROCFS_IOCTL_MAGIC, 17, struct procmap_query)
+
+enum procmap_query_flags {
+	/*
+	 * VMA permission flags.
+	 *
+	 * Can be used as part of procmap_query.query_flags field to look up
+	 * only VMAs satisfying specified subset of permissions. E.g., specifying
+	 * PROCMAP_QUERY_VMA_READABLE only will return both readable and read/write VMAs,
+	 * while having PROCMAP_QUERY_VMA_READABLE | PROCMAP_QUERY_VMA_WRITABLE will only
+	 * return read/write VMAs, though both executable/non-executable and
+	 * private/shared will be ignored.
+	 *
+	 * PROCMAP_QUERY_VMA_* flags are also returned in procmap_query.vma_flags
+	 * field to specify actual VMA permissions.
+	 */
+	PROCMAP_QUERY_VMA_READABLE		= 0x01,
+	PROCMAP_QUERY_VMA_WRITABLE		= 0x02,
+	PROCMAP_QUERY_VMA_EXECUTABLE		= 0x04,
+	PROCMAP_QUERY_VMA_SHARED		= 0x08,
+	/*
+	 * Query modifier flags.
+	 *
+	 * By default VMA that covers provided address is returned, or -ENOENT
+	 * is returned. With PROCMAP_QUERY_COVERING_OR_NEXT_VMA flag set, closest
+	 * VMA with vma_start > addr will be returned if no covering VMA is
+	 * found.
+	 *
+	 * PROCMAP_QUERY_FILE_BACKED_VMA instructs query to consider only VMAs that
+	 * have file backing. Can be combined with PROCMAP_QUERY_COVERING_OR_NEXT_VMA
+	 * to iterate all VMAs with file backing.
+	 */
+	PROCMAP_QUERY_COVERING_OR_NEXT_VMA	= 0x10,
+	PROCMAP_QUERY_FILE_BACKED_VMA		= 0x20,
+};
+
+/*
+ * Input/output argument structured passed into ioctl() call. It can be used
+ * to query a set of VMAs (Virtual Memory Areas) of a process.
+ *
+ * Each field can be one of three kinds, marked in a short comment to the
+ * right of the field:
+ *   - "in", input argument, user has to provide this value, kernel doesn't modify it;
+ *   - "out", output argument, kernel sets this field with VMA data;
+ *   - "in/out", input and output argument; user provides initial value (used
+ *     to specify maximum allowable buffer size), and kernel sets it to actual
+ *     amount of data written (or zero, if there is no data).
+ *
+ * If matching VMA is found (according to criterias specified by
+ * query_addr/query_flags, all the out fields are filled out, and ioctl()
+ * returns 0. If there is no matching VMA, -ENOENT will be returned.
+ * In case of any other error, negative error code other than -ENOENT is
+ * returned.
+ *
+ * Most of the data is similar to the one returned as text in /proc/<pid>/maps
+ * file, but procmap_query provides more querying flexibility. There are no
+ * consistency guarantees between subsequent ioctl() calls, but data returned
+ * for matched VMA is self-consistent.
+ */
+struct procmap_query {
+	/* Query struct size, for backwards/forward compatibility */
+	__u64 size;
+	/*
+	 * Query flags, a combination of enum procmap_query_flags values.
+	 * Defines query filtering and behavior, see enum procmap_query_flags.
+	 *
+	 * Input argument, provided by user. Kernel doesn't modify it.
+	 */
+	__u64 query_flags;		/* in */
+	/*
+	 * Query address. By default, VMA that covers this address will
+	 * be looked up. PROCMAP_QUERY_* flags above modify this default
+	 * behavior further.
+	 *
+	 * Input argument, provided by user. Kernel doesn't modify it.
+	 */
+	__u64 query_addr;		/* in */
+	/* VMA starting (inclusive) and ending (exclusive) address, if VMA is found. */
+	__u64 vma_start;		/* out */
+	__u64 vma_end;			/* out */
+	/* VMA permissions flags. A combination of PROCMAP_QUERY_VMA_* flags. */
+	__u64 vma_flags;		/* out */
+	/* VMA backing page size granularity. */
+	__u64 vma_page_size;		/* out */
+	/*
+	 * VMA file offset. If VMA has file backing, this specifies offset
+	 * within the file that VMA's start address corresponds to.
+	 * Is set to zero if VMA has no backing file.
+	 */
+	__u64 vma_offset;		/* out */
+	/* Backing file's inode number, or zero, if VMA has no backing file. */
+	__u64 inode;			/* out */
+	/* Backing file's device major/minor number, or zero, if VMA has no backing file. */
+	__u32 dev_major;		/* out */
+	__u32 dev_minor;		/* out */
+	/*
+	 * If set to non-zero value, signals the request to return VMA name
+	 * (i.e., VMA's backing file's absolute path, with " (deleted)" suffix
+	 * appended, if file was unlinked from FS) for matched VMA. VMA name
+	 * can also be some special name (e.g., "[heap]", "[stack]") or could
+	 * be even user-supplied with prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME).
+	 *
+	 * Kernel will set this field to zero, if VMA has no associated name.
+	 * Otherwise kernel will return actual amount of bytes filled in
+	 * user-supplied buffer (see vma_name_addr field below), including the
+	 * terminating zero.
+	 *
+	 * If VMA name is longer that user-supplied maximum buffer size,
+	 * -E2BIG error is returned.
+	 *
+	 * If this field is set to non-zero value, vma_name_addr should point
+	 * to valid user space memory buffer of at least vma_name_size bytes.
+	 * If set to zero, vma_name_addr should be set to zero as well
+	 */
+	__u32 vma_name_size;		/* in/out */
+	/*
+	 * If set to non-zero value, signals the request to extract and return
+	 * VMA's backing file's build ID, if the backing file is an ELF file
+	 * and it contains embedded build ID.
+	 *
+	 * Kernel will set this field to zero, if VMA has no backing file,
+	 * backing file is not an ELF file, or ELF file has no build ID
+	 * embedded.
+	 *
+	 * Build ID is a binary value (not a string). Kernel will set
+	 * build_id_size field to exact number of bytes used for build ID.
+	 * If build ID is requested and present, but needs more bytes than
+	 * user-supplied maximum buffer size (see build_id_addr field below),
+	 * -E2BIG error will be returned.
+	 *
+	 * If this field is set to non-zero value, build_id_addr should point
+	 * to valid user space memory buffer of at least build_id_size bytes.
+	 * If set to zero, build_id_addr should be set to zero as well
+	 */
+	__u32 build_id_size;		/* in/out */
+	/*
+	 * User-supplied address of a buffer of at least vma_name_size bytes
+	 * for kernel to fill with matched VMA's name (see vma_name_size field
+	 * description above for details).
+	 *
+	 * Should be set to zero if VMA name should not be returned.
+	 */
+	__u64 vma_name_addr;		/* in */
+	/*
+	 * User-supplied address of a buffer of at least build_id_size bytes
+	 * for kernel to fill with matched VMA's ELF build ID, if available
+	 * (see build_id_size field description above for details).
+	 *
+	 * Should be set to zero if build ID should not be returned.
+	 */
+	__u64 build_id_addr;		/* in */
+};
+
+#endif /* _UAPI_LINUX_FS_H */
Signature · 1 line
2.43.0
author maintainer
#17 lore raw

PATCH v6 6/6 selftests/proc: add PROCMAP_QUERY ioctl tests

In reply to Andrii Nakryiko

Extend existing proc-pid-vm.c tests with PROCMAP_QUERY ioctl() API. Test a few successful and negative cases, validating querying filtering and exact vs next VMA logic works as expected.

---
 tools/testing/selftests/proc/Makefile      |  1 +
 tools/testing/selftests/proc/proc-pid-vm.c | 86 ++++++++++++++++++++++
 2 files changed, 87 insertions(+)
tools/testing/selftests/proc/Makefile +1 -0
index cd95369254c0..291e7087f1b3 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -Wall -O2 -Wno-unused-function
 CFLAGS += -D_GNU_SOURCE
+CFLAGS += $(TOOLS_INCLUDES)
 LDFLAGS += -pthread
 
 TEST_GEN_PROGS :=
tools/testing/selftests/proc/proc-pid-vm.c +86 -0

98 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#18 lore raw
In reply to Andrii Nakryiko
Quoted 3 lines · On Thu, 27 Jun 2024 10:08:52 -0700 Andrii Nakryiko <[email protected]> wrote:

Implement binary ioctl()-based interface to /proc/<pid>/maps file to allow applications to query VMA information more efficiently than reading *all* VMAs nonselectively through text-based interface of /proc/<pid>/maps file.

I appreciate the usefulness for monitoring large fleets, so I'll add this version to mm-unstable. As we're almost at -rc6 I'll await further review before deciding on the next steps.

Is it possible/sensible to make this feature Kconfigurable so that people who don't need it can omit it?

#19 lore raw
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Thu, 27 Jun 2024 10:08:52 -0700 Andrii Nakryiko <[email protected]> wrote:

Implement binary ioctl()-based interface to /proc/<pid>/maps file to allow applications to query VMA information more efficiently than reading *all* VMAs nonselectively through text-based interface of /proc/<pid>/maps file.

Quoted 6 lines

I appreciate the usefulness for monitoring large fleets, so I'll add this version to mm-unstable. As we're almost at -rc6 I'll await further review before deciding on the next steps.

Is it possible/sensible to make this feature Kconfigurable so that people who don't need it can omit it?

It's just a matter of #ifdef/#endif, so not hard, technically speaking. But I'm wondering what's the concern? This is mostly newly added code (except factoring out get_vma_name logic, which won't be #ifdef'ed anyways), so if no one is using this new API, then it should cause no issue.

Generally speaking, I'd say if we don't *have to* add the Kconfig option, I'd prefer that. But if you feel strongly, it's not hard for me to do, of course.

Or are you concerned with the vmlinux code size increase? It doesn't seem to be large enough to warrant a Kconfig, IMO (from bloat-o-meter):

do_procmap_query                               -    1308   +1308
get_vma_name                                   -     283    +283
procfs_procmap_ioctl                           -      47     +47

show_map_vma 444 274 -170

But again, do let me know if you insist.

maintainer
#20 lore raw
Quoted 18 lines · On Thu, 27 Jun 2024 13:50:22 -0700 Andrii Nakryiko <[email protected]>…

On Thu, Jun 27, 2024 at 12:59 PM Andrew Morton

Quoted 2 lines

Is it possible/sensible to make this feature Kconfigurable so that people who don't need it can omit it?

It's just a matter of #ifdef/#endif, so not hard, technically speaking. But I'm wondering what's the concern? This is mostly newly added code (except factoring out get_vma_name logic, which won't be #ifdef'ed anyways), so if no one is using this new API, then it should cause no issue.

Generally speaking, I'd say if we don't *have to* add the Kconfig option, I'd prefer that. But if you feel strongly, it's not hard for me to do, of course.

Or are you concerned with the vmlinux code size increase? It doesn't seem to be large enough to warrant a Kconfig, IMO (from bloat-o-meter):

Quoted 6 lines
do_procmap_query                               -    1308   +1308
get_vma_name                                   -     283    +283
procfs_procmap_ioctl                           -      47     +47

show_map_vma 444 274 -170

But again, do let me know if you insist.

Yes, I'm thinking about being nice to small systems ("make tinyconfig"!). The kernel just gets bigger and bigger over time, little bit by little bit.

It's a judgment call - if making it configurable is ugly and/or adds maintenance overhead then no.

#21 lore raw
Quoted 26 lines · Andrew Morton
Quoted 23 lines · On Thu, 27 Jun 2024 13:50:22 -0700 Andrii Nakryiko <[email protected]>…

On Thu, Jun 27, 2024 at 12:59 PM Andrew Morton

Quoted 2 lines

Is it possible/sensible to make this feature Kconfigurable so that people who don't need it can omit it?

It's just a matter of #ifdef/#endif, so not hard, technically speaking. But I'm wondering what's the concern? This is mostly newly added code (except factoring out get_vma_name logic, which won't be #ifdef'ed anyways), so if no one is using this new API, then it should cause no issue.

Generally speaking, I'd say if we don't *have to* add the Kconfig option, I'd prefer that. But if you feel strongly, it's not hard for me to do, of course.

Or are you concerned with the vmlinux code size increase? It doesn't seem to be large enough to warrant a Kconfig, IMO (from bloat-o-meter):

do_procmap_query                               -    1308   +1308
get_vma_name                                   -     283    +283
procfs_procmap_ioctl                           -      47     +47

show_map_vma 444 274 -170

Quoted 9 lines
Quoted 1 line

But again, do let me know if you insist.

Yes, I'm thinking about being nice to small systems ("make tinyconfig"!). The kernel just gets bigger and bigger over time, little bit by little bit.

It's a judgment call - if making it configurable is ugly and/or adds maintenance overhead then no.

I see, thanks for clarifying. I'd vote to not add extra Kconfig to keep things simple and less surprising. All this code is conditional on CONFIG_PROC_FS=y anyways, and there is plenty of code for procfs already. I think this do_procmap_query is just a small addition here that doesn't fundamentally regress anything.

maintainer
#22 lore raw
In reply to Andrii Nakryiko
Quoted 3 lines · On Thu, 27 Jun 2024 10:08:52 -0700 Andrii Nakryiko <[email protected]> wrote:

Implement binary ioctl()-based interface to /proc/<pid>/maps file to allow applications to query VMA information more efficiently than reading *all* VMAs nonselectively through text-based interface of /proc/<pid>/maps file.

I haven't seen any acks or reviewed-by's for this series. lgtm, so I plan to move it into mm-stable later this week.

#23 lore raw
Quoted 9 lines · Andrew Morton
Quoted 3 lines · On Thu, 27 Jun 2024 10:08:52 -0700 Andrii Nakryiko <[email protected]> wrote:

Implement binary ioctl()-based interface to /proc/<pid>/maps file to allow applications to query VMA information more efficiently than reading *all* VMAs nonselectively through text-based interface of /proc/<pid>/maps file.

I haven't seen any acks or reviewed-by's for this series. lgtm, so I plan to move it into mm-stable later this week.

great, thanks!

Acked-by on the v6 series
#24 lore raw
In reply to Andrii Nakryiko
Quoted 4 lines · Andrii Nakryiko

Implement binary ioctl()-based interface to /proc/<pid>/maps file to allow applications to query VMA information more efficiently than reading *all* VMAs nonselectively through text-based interface of /proc/<pid>/maps file.

Thanks for doing this Andrii. It looks to be a step forward for a lot of use cases.

#25 lore raw
Quoted 8 lines · Liam R. Howlett
Quoted 4 lines · Andrii Nakryiko

Implement binary ioctl()-based interface to /proc/<pid>/maps file to allow applications to query VMA information more efficiently than reading *all* VMAs nonselectively through text-based interface of /proc/<pid>/maps file.

Thanks for doing this Andrii. It looks to be a step forward for a lot of use cases.

Yes, looks like ioctl on text files are the way to go. :-)