Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 8 Dec 2019 22:33:51 +0000 (UTC)
From:      Doug Moore <dougm@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r355538 - in head: lib/libprocstat sys/vm
Message-ID:  <201912082233.xB8MXppR089426@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dougm
Date: Sun Dec  8 22:33:51 2019
New Revision: 355538
URL: https://svnweb.freebsd.org/changeset/base/355538

Log:
  Define a vm_map method for user-space for advancing from a map entry
  to its successor in cases where examining a map entry requires a
  helper like kvm_read_all.  Use that method, with kvm_read_all, to fix
  procstat_getfiles_kvm, which tries to find the successor now without
  using such a helper.  This addresses a problem introduced by r355491.
  
  Reviewed by: markj (previous version)
  Discussed with: kib
  Differential Revision: https://reviews.freebsd.org/D22728

Modified:
  head/lib/libprocstat/libprocstat.c
  head/sys/vm/vm_map.h

Modified: head/lib/libprocstat/libprocstat.c
==============================================================================
--- head/lib/libprocstat/libprocstat.c	Sun Dec  8 21:30:04 2019	(r355537)
+++ head/lib/libprocstat/libprocstat.c	Sun Dec  8 22:33:51 2019	(r355538)
@@ -445,6 +445,15 @@ getctty(kvm_t *kd, struct kinfo_proc *kp)
 	return (sess.s_ttyvp);
 }
 
+static int
+procstat_vm_map_reader(void *token, vm_map_entry_t addr, vm_map_entry_t dest)
+{
+	kvm_t *kd;
+
+	kd = (kvm_t *)token;
+	return (kvm_read_all(kd, (unsigned long)addr, dest, sizeof(*dest)));
+}
+
 static struct filestat_list *
 procstat_getfiles_kvm(struct procstat *procstat, struct kinfo_proc *kp, int mmapped)
 {
@@ -454,7 +463,6 @@ procstat_getfiles_kvm(struct procstat *procstat, struc
 	struct vm_object object;
 	struct vmspace vmspace;
 	vm_map_entry_t entryp;
-	vm_map_t map;
 	vm_object_t objp;
 	struct vnode *vp;
 	struct file **ofiles;
@@ -615,17 +623,11 @@ do_mmapped:
 			    (void *)kp->ki_vmspace);
 			goto exit;
 		}
-		map = &vmspace.vm_map;
 
-		for (entryp = vm_map_entry_first(map);
-		    entryp != &kp->ki_vmspace->vm_map.header;
-		    entryp = vm_map_entry_succ(&vmentry)) {
-			if (!kvm_read_all(kd, (unsigned long)entryp, &vmentry,
-			    sizeof(vmentry))) {
-				warnx("can't read vm_map_entry at %p",
-				    (void *)entryp);
-				continue;
-			}
+		vmentry = vmspace.vm_map.header;
+		for (entryp = vm_map_entry_read_succ(kd, &vmentry, procstat_vm_map_reader);
+		    entryp != NULL && entryp != &kp->ki_vmspace->vm_map.header;
+		     entryp = vm_map_entry_read_succ(kd, &vmentry, procstat_vm_map_reader)) {
 			if (vmentry.eflags & MAP_ENTRY_IS_SUB_MAP)
 				continue;
 			if ((objp = vmentry.object.vm_object) == NULL)
@@ -660,6 +662,8 @@ do_mmapped:
 			if (entry != NULL)
 				STAILQ_INSERT_TAIL(head, entry, next);
 		}
+		if (entryp == NULL)
+			warnx("can't read vm_map_entry");
 	}
 exit:
 	return (head);

Modified: head/sys/vm/vm_map.h
==============================================================================
--- head/sys/vm/vm_map.h	Sun Dec  8 21:30:04 2019	(r355537)
+++ head/sys/vm/vm_map.h	Sun Dec  8 22:33:51 2019	(r355538)
@@ -402,6 +402,47 @@ long vmspace_resident_count(struct vmspace *vmspace);
 
 #define VM_MAP_WIRE_WRITE	4	/* Validate writable. */
 
+typedef int vm_map_entry_reader(void *token, vm_map_entry_t addr, 
+    vm_map_entry_t dest);
+
+#ifndef _KERNEL
+/*
+ * Find the successor of a map_entry, using a reader to dereference pointers.
+ * '*clone' is a copy of a vm_map entry.  'reader' is used to copy a map entry
+ * at some address into '*clone'.  Change *clone to a copy of the next map
+ * entry, and return the address of that entry, or NULL if copying has failed.
+ *
+ * This function is made available to user-space code that needs to traverse
+ * map entries.
+ */
+static inline vm_map_entry_t
+vm_map_entry_read_succ(void *token, struct vm_map_entry *const clone,
+    vm_map_entry_reader reader)
+{
+	vm_map_entry_t after, backup;
+	vm_offset_t start;
+
+	after = clone->right;
+	start = clone->start;
+	if (!reader(token, after, clone))
+		return (NULL);
+	backup = clone->left;
+	if (!reader(token, backup, clone))
+		return (NULL);
+	if (clone->start > start) {
+		do {
+			after = backup;
+			backup = clone->left;
+			if (!reader(token, backup, clone))
+				return (NULL);
+		} while (clone->start != start);
+	}
+	if (!reader(token, after, clone))
+		return (NULL);
+	return (after);
+}
+#endif				/* ! _KERNEL */
+
 static inline vm_map_entry_t
 vm_map_entry_first(vm_map_t map)
 {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201912082233.xB8MXppR089426>