sysctl vm.objects: report backing object and swap use

For anonymous objects, provide a handle kvo_me naming the object,
and report the handle of the backing object.  This allows userspace
to deconstruct the shadow chain.  Right now the handle is the address
of the object in KVA, but this is not guaranteed.

For the same anonymous objects, report the swap space used for actually
swapped out pages, in kvo_swapped field.  I do not believe that it is
useful to report full 64bit counter there, so only uint32_t value is
returned, clamped to the max.

For kinfo_vmentry, report anonymous object handle backing the entry,
so that the shadow chain for the specific mapping can be deconstructed.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D29771
This commit is contained in:
Konstantin Belousov 2021-04-15 12:27:02 +03:00
parent 4342ba184c
commit ecfbddf0cd
5 changed files with 55 additions and 5 deletions

View File

@ -2538,6 +2538,9 @@ kern_proc_vmmap_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, int flags)
bzero(kve, sizeof(*kve));
obj = entry->object.vm_object;
if (obj != NULL) {
if ((obj->flags & OBJ_ANON) != 0)
kve->kve_obj = (uintptr_t)obj;
for (tobj = obj; tobj != NULL;
tobj = tobj->backing_object) {
VM_OBJECT_RLOCK(tobj);

View File

@ -529,12 +529,17 @@ struct kinfo_vmentry {
uint32_t kve_vn_rdev_freebsd11; /* Device id if device. */
uint16_t kve_vn_mode; /* File mode. */
uint16_t kve_status; /* Status flags. */
uint64_t kve_vn_fsid; /* dev_t of vnode location */
union {
uint64_t _kve_vn_fsid; /* dev_t of vnode location */
uint64_t _kve_obj; /* handle of anon obj */
} kve_type_spec;
uint64_t kve_vn_rdev; /* Device id if device. */
int _kve_ispare[8]; /* Space for more stuff. */
/* Truncated before copyout in sysctl */
char kve_path[PATH_MAX]; /* Path to VM obj, if any. */
};
#define kve_vn_fsid kve_type_spec._kve_vn_fsid
#define kve_obj kve_type_spec._kve_obj
/*
* The "vm.objects" sysctl provides a list of all VM objects in the system
@ -552,11 +557,18 @@ struct kinfo_vmobject {
uint64_t kvo_resident; /* Number of resident pages. */
uint64_t kvo_active; /* Number of active pages. */
uint64_t kvo_inactive; /* Number of inactive pages. */
uint64_t kvo_vn_fsid;
uint64_t _kvo_qspare[7];
uint32_t _kvo_ispare[8];
union {
uint64_t _kvo_vn_fsid;
uint64_t _kvo_backing_obj; /* Handle for the backing obj */
} kvo_type_spec; /* Type-specific union */
uint64_t kvo_me; /* Uniq handle for anon obj */
uint64_t _kvo_qspare[6];
uint32_t kvo_swapped; /* Number of swapped pages */
uint32_t _kvo_ispare[7];
char kvo_path[PATH_MAX]; /* Pathname, if any. */
};
#define kvo_vn_fsid kvo_type_spec._kvo_vn_fsid
#define kvo_backing_obj kvo_type_spec._kvo_backing_obj
/*
* The KERN_PROC_KSTACK sysctl allows a process to dump the kernel stacks of

View File

@ -1763,6 +1763,29 @@ swp_pager_force_dirty(vm_page_t m)
vm_page_launder(m);
}
u_long
swap_pager_swapped_pages(vm_object_t object)
{
struct swblk *sb;
vm_pindex_t pi;
u_long res;
int i;
VM_OBJECT_ASSERT_LOCKED(object);
if (object->type != OBJT_SWAP)
return (0);
for (res = 0, pi = 0; (sb = SWAP_PCTRIE_LOOKUP_GE(
&object->un_pager.swp.swp_blks, pi)) != NULL;
pi = sb->p + SWAP_META_PAGES) {
for (i = 0; i < SWAP_META_PAGES; i++) {
if (sb->d[i] != SWAPBLK_NONE)
res++;
}
}
return (res);
}
/*
* swap_pager_swapoff_object:
*

View File

@ -81,6 +81,7 @@ void swap_pager_swap_init(void);
int swap_pager_nswapdev(void);
int swap_pager_reserve(vm_object_t, vm_pindex_t, vm_size_t);
void swap_pager_status(int *total, int *used);
u_long swap_pager_swapped_pages(vm_object_t object);
void swapoff_all(void);
#endif /* _KERNEL */

View File

@ -73,6 +73,7 @@ __FBSDID("$FreeBSD$");
#include <sys/systm.h>
#include <sys/blockcount.h>
#include <sys/cpuset.h>
#include <sys/limits.h>
#include <sys/lock.h>
#include <sys/mman.h>
#include <sys/mount.h>
@ -2524,6 +2525,7 @@ sysctl_vm_object_list(SYSCTL_HANDLER_ARGS)
struct vattr va;
vm_object_t obj;
vm_page_t m;
u_long sp;
int count, error;
if (req->oldptr == NULL) {
@ -2590,8 +2592,17 @@ sysctl_vm_object_list(SYSCTL_HANDLER_ARGS)
freepath = NULL;
fullpath = "";
kvo->kvo_type = vm_object_kvme_type(obj, &vp);
if (vp != NULL)
if (vp != NULL) {
vref(vp);
} else if ((obj->flags & OBJ_ANON) != 0) {
MPASS(kvo->kvo_type == KVME_TYPE_DEFAULT ||
kvo->kvo_type == KVME_TYPE_SWAP);
kvo->kvo_me = (uintptr_t)obj;
/* tmpfs objs are reported as vnodes */
kvo->kvo_backing_obj = (uintptr_t)obj->backing_object;
sp = swap_pager_swapped_pages(obj);
kvo->kvo_swapped = sp > UINT32_MAX ? UINT32_MAX : sp;
}
VM_OBJECT_RUNLOCK(obj);
if (vp != NULL) {
vn_fullpath(vp, &fullpath, &freepath);