2005-01-06 18:10:42 +00:00
|
|
|
/*-
|
2017-11-20 19:43:44 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1996-06-17 22:53:27 +00:00
|
|
|
* Copyright (c) 1993 Jan-Simon Pendry
|
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Jan-Simon Pendry.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1996-06-17 22:53:27 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)procfs_status.c 8.3 (Berkeley) 2/17/94
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1996-06-17 22:53:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lock.h>
|
2003-09-29 20:53:19 +00:00
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/malloc.h>
|
2007-04-23 06:12:24 +00:00
|
|
|
#include <sys/mount.h>
|
1996-06-17 22:53:27 +00:00
|
|
|
#include <sys/proc.h>
|
Implement global and per-uid accounting of the anonymous memory. Add
rlimit RLIMIT_SWAP that limits the amount of swap that may be reserved
for the uid.
The accounting information (charge) is associated with either map entry,
or vm object backing the entry, assuming the object is the first one
in the shadow chain and entry does not require COW. Charge is moved
from entry to object on allocation of the object, e.g. during the mmap,
assuming the object is allocated, or on the first page fault on the
entry. It moves back to the entry on forks due to COW setup.
The per-entry granularity of accounting makes the charge process fair
for processes that change uid during lifetime, and decrements charge
for proper uid when region is unmapped.
The interface of vm_pager_allocate(9) is extended by adding struct ucred *,
that is used to charge appropriate uid when allocation if performed by
kernel, e.g. md(4).
Several syscalls, among them is fork(2), may now return ENOMEM when
global or per-uid limits are enforced.
In collaboration with: pho
Reviewed by: alc
Approved by: re (kensmith)
2009-06-23 20:45:22 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2013-03-09 02:32:23 +00:00
|
|
|
#include <sys/rwlock.h>
|
2008-10-04 14:08:16 +00:00
|
|
|
#include <sys/sbuf.h>
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2008-12-29 12:41:32 +00:00
|
|
|
#include <sys/sysent.h>
|
|
|
|
#endif
|
2001-12-04 01:35:06 +00:00
|
|
|
#include <sys/uio.h>
|
2021-05-10 15:20:42 +00:00
|
|
|
#include <sys/user.h>
|
2003-09-29 20:53:19 +00:00
|
|
|
#include <sys/vnode.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
|
2001-12-04 01:35:06 +00:00
|
|
|
#include <fs/pseudofs/pseudofs.h>
|
2001-05-23 09:42:29 +00:00
|
|
|
#include <fs/procfs/procfs.h>
|
1996-06-17 22:53:27 +00:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
2008-12-12 12:12:36 +00:00
|
|
|
#include <vm/vm_extern.h>
|
1996-06-17 22:53:27 +00:00
|
|
|
#include <vm/pmap.h>
|
|
|
|
#include <vm/vm_map.h>
|
|
|
|
#include <vm/vm_page.h>
|
|
|
|
#include <vm/vm_object.h>
|
|
|
|
|
1996-10-30 03:45:00 +00:00
|
|
|
#define MEBUFFERSIZE 256
|
1996-06-17 22:53:27 +00:00
|
|
|
|
1996-10-30 03:45:00 +00:00
|
|
|
/*
|
|
|
|
* The map entries can *almost* be read with programs like cat. However,
|
|
|
|
* large maps need special programs to read. It is not easy to implement
|
|
|
|
* a program that can sense the required size of the buffer, and then
|
|
|
|
* subsequently do a read with the appropriate size. This operation cannot
|
|
|
|
* be atomic. The best that we can do is to allow the program to do a read
|
|
|
|
* with an arbitrarily large buffer, and return as much as we can. We can
|
|
|
|
* return an error code if the buffer is too small (EFBIG), then the program
|
|
|
|
* can try a bigger buffer.
|
|
|
|
*/
|
1996-06-17 22:53:27 +00:00
|
|
|
int
|
2001-12-04 01:35:06 +00:00
|
|
|
procfs_doprocmap(PFS_FILL_ARGS)
|
1996-06-17 22:53:27 +00:00
|
|
|
{
|
2008-12-12 12:12:36 +00:00
|
|
|
struct vmspace *vm;
|
|
|
|
vm_map_t map;
|
2008-12-08 12:34:52 +00:00
|
|
|
vm_map_entry_t entry, tmp_entry;
|
2007-04-23 06:12:24 +00:00
|
|
|
struct vnode *vp;
|
2017-12-28 13:23:13 +00:00
|
|
|
char *fullpath, *freepath, *type;
|
2010-12-02 17:37:16 +00:00
|
|
|
struct ucred *cred;
|
2021-02-25 20:11:19 +00:00
|
|
|
vm_object_t lobj, nobj, obj, tobj;
|
2021-05-10 15:20:42 +00:00
|
|
|
int error, flags, kvme, privateresident, ref_count, resident;
|
|
|
|
int shadow_count;
|
2017-12-28 13:23:13 +00:00
|
|
|
vm_offset_t e_start, e_end;
|
|
|
|
vm_eflags_t e_eflags;
|
|
|
|
vm_prot_t e_prot;
|
2008-12-08 12:34:52 +00:00
|
|
|
unsigned int last_timestamp;
|
2017-12-28 13:23:13 +00:00
|
|
|
bool super;
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2017-12-28 13:23:13 +00:00
|
|
|
bool wrap32;
|
2005-06-30 07:49:22 +00:00
|
|
|
#endif
|
1996-06-17 22:53:27 +00:00
|
|
|
|
2003-08-14 15:26:44 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
error = p_candebug(td, p);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
1996-06-17 22:53:27 +00:00
|
|
|
if (uio->uio_rw != UIO_READ)
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2017-12-28 13:23:13 +00:00
|
|
|
wrap32 = false;
|
|
|
|
if (SV_CURPROC_FLAG(SV_ILP32)) {
|
|
|
|
if (!(SV_PROC_FLAG(p, SV_ILP32)))
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
wrap32 = true;
|
|
|
|
}
|
2005-06-30 07:49:22 +00:00
|
|
|
#endif
|
2007-04-15 17:06:09 +00:00
|
|
|
|
2008-12-12 12:12:36 +00:00
|
|
|
vm = vmspace_acquire_ref(p);
|
|
|
|
if (vm == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
map = &vm->vm_map;
|
2007-04-23 06:12:24 +00:00
|
|
|
vm_map_lock_read(map);
|
2019-10-08 07:14:21 +00:00
|
|
|
VM_MAP_ENTRY_FOREACH(entry, map) {
|
1999-02-07 21:48:23 +00:00
|
|
|
if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
|
1996-06-17 22:53:27 +00:00
|
|
|
continue;
|
|
|
|
|
2008-12-08 12:34:52 +00:00
|
|
|
e_eflags = entry->eflags;
|
|
|
|
e_prot = entry->protection;
|
|
|
|
e_start = entry->start;
|
|
|
|
e_end = entry->end;
|
2007-04-23 06:12:24 +00:00
|
|
|
privateresident = 0;
|
2017-12-28 13:23:13 +00:00
|
|
|
resident = 0;
|
1996-06-17 22:53:27 +00:00
|
|
|
obj = entry->object.vm_object;
|
2007-04-23 06:12:24 +00:00
|
|
|
if (obj != NULL) {
|
2013-06-05 17:00:10 +00:00
|
|
|
VM_OBJECT_RLOCK(obj);
|
2007-04-23 06:12:24 +00:00
|
|
|
if (obj->shadow_count == 1)
|
|
|
|
privateresident = obj->resident_page_count;
|
|
|
|
}
|
2010-12-02 17:37:16 +00:00
|
|
|
cred = (entry->cred) ? entry->cred : (obj ? obj->cred : NULL);
|
1996-06-17 22:53:27 +00:00
|
|
|
|
2017-12-28 13:23:13 +00:00
|
|
|
for (lobj = tobj = obj; tobj != NULL;
|
|
|
|
tobj = tobj->backing_object) {
|
2007-04-23 06:12:24 +00:00
|
|
|
if (tobj != obj)
|
2013-06-05 17:00:10 +00:00
|
|
|
VM_OBJECT_RLOCK(tobj);
|
1996-06-17 22:53:27 +00:00
|
|
|
lobj = tobj;
|
2007-04-23 06:12:24 +00:00
|
|
|
}
|
2017-12-28 13:23:13 +00:00
|
|
|
if (obj != NULL)
|
|
|
|
kern_proc_vmmap_resident(map, entry, &resident, &super);
|
2021-02-25 20:11:19 +00:00
|
|
|
for (tobj = obj; tobj != NULL; tobj = nobj) {
|
|
|
|
nobj = tobj->backing_object;
|
2017-12-28 13:23:13 +00:00
|
|
|
if (tobj != obj && tobj != lobj)
|
|
|
|
VM_OBJECT_RUNLOCK(tobj);
|
|
|
|
}
|
2008-12-08 12:34:52 +00:00
|
|
|
last_timestamp = map->timestamp;
|
|
|
|
vm_map_unlock_read(map);
|
1996-06-17 22:53:27 +00:00
|
|
|
|
2003-09-29 20:53:19 +00:00
|
|
|
freepath = NULL;
|
|
|
|
fullpath = "-";
|
1998-04-29 04:28:22 +00:00
|
|
|
if (lobj) {
|
2021-05-10 15:20:42 +00:00
|
|
|
kvme = vm_object_kvme_type(lobj, &vp);
|
|
|
|
if (vp != NULL)
|
2007-04-23 06:12:24 +00:00
|
|
|
vref(vp);
|
2021-05-10 15:20:42 +00:00
|
|
|
switch (kvme) {
|
|
|
|
default:
|
|
|
|
type = "unknown";
|
1998-04-29 04:28:22 +00:00
|
|
|
break;
|
2021-05-10 15:20:42 +00:00
|
|
|
case KVME_TYPE_PHYS:
|
|
|
|
type = "phys";
|
1998-04-29 04:28:22 +00:00
|
|
|
break;
|
2021-05-10 15:20:42 +00:00
|
|
|
case KVME_TYPE_SWAP:
|
2021-05-01 01:18:00 +00:00
|
|
|
type = "swap";
|
|
|
|
break;
|
2021-05-10 15:20:42 +00:00
|
|
|
case KVME_TYPE_DEAD:
|
|
|
|
type = "dead";
|
|
|
|
break;
|
|
|
|
case KVME_TYPE_VNODE:
|
|
|
|
type = "vnode";
|
|
|
|
break;
|
|
|
|
case KVME_TYPE_SG:
|
|
|
|
case KVME_TYPE_DEVICE:
|
|
|
|
case KVME_TYPE_MGTDEVICE:
|
1998-04-29 04:28:22 +00:00
|
|
|
type = "device";
|
|
|
|
break;
|
|
|
|
}
|
2007-04-23 06:12:24 +00:00
|
|
|
if (lobj != obj)
|
2013-06-05 17:00:10 +00:00
|
|
|
VM_OBJECT_RUNLOCK(lobj);
|
2003-12-07 17:40:00 +00:00
|
|
|
|
1998-04-29 04:28:22 +00:00
|
|
|
flags = obj->flags;
|
|
|
|
ref_count = obj->ref_count;
|
|
|
|
shadow_count = obj->shadow_count;
|
2013-06-05 17:00:10 +00:00
|
|
|
VM_OBJECT_RUNLOCK(obj);
|
2007-04-23 06:12:24 +00:00
|
|
|
if (vp != NULL) {
|
2020-08-24 08:57:02 +00:00
|
|
|
vn_fullpath(vp, &fullpath, &freepath);
|
2008-11-04 19:04:01 +00:00
|
|
|
vrele(vp);
|
2007-04-23 06:12:24 +00:00
|
|
|
}
|
1996-07-27 18:28:10 +00:00
|
|
|
} else {
|
|
|
|
type = "none";
|
1998-04-29 04:28:22 +00:00
|
|
|
flags = 0;
|
|
|
|
ref_count = 0;
|
|
|
|
shadow_count = 0;
|
1996-06-17 22:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* format:
|
Implement global and per-uid accounting of the anonymous memory. Add
rlimit RLIMIT_SWAP that limits the amount of swap that may be reserved
for the uid.
The accounting information (charge) is associated with either map entry,
or vm object backing the entry, assuming the object is the first one
in the shadow chain and entry does not require COW. Charge is moved
from entry to object on allocation of the object, e.g. during the mmap,
assuming the object is allocated, or on the first page fault on the
entry. It moves back to the entry on forks due to COW setup.
The per-entry granularity of accounting makes the charge process fair
for processes that change uid during lifetime, and decrements charge
for proper uid when region is unmapped.
The interface of vm_pager_allocate(9) is extended by adding struct ucred *,
that is used to charge appropriate uid when allocation if performed by
kernel, e.g. md(4).
Several syscalls, among them is fork(2), may now return ENOMEM when
global or per-uid limits are enforced.
In collaboration with: pho
Reviewed by: alc
Approved by: re (kensmith)
2009-06-23 20:45:22 +00:00
|
|
|
* start, end, resident, private resident, cow, access, type,
|
|
|
|
* charged, charged uid.
|
1996-06-17 22:53:27 +00:00
|
|
|
*/
|
2008-10-04 14:08:16 +00:00
|
|
|
error = sbuf_printf(sb,
|
Implement global and per-uid accounting of the anonymous memory. Add
rlimit RLIMIT_SWAP that limits the amount of swap that may be reserved
for the uid.
The accounting information (charge) is associated with either map entry,
or vm object backing the entry, assuming the object is the first one
in the shadow chain and entry does not require COW. Charge is moved
from entry to object on allocation of the object, e.g. during the mmap,
assuming the object is allocated, or on the first page fault on the
entry. It moves back to the entry on forks due to COW setup.
The per-entry granularity of accounting makes the charge process fair
for processes that change uid during lifetime, and decrements charge
for proper uid when region is unmapped.
The interface of vm_pager_allocate(9) is extended by adding struct ucred *,
that is used to charge appropriate uid when allocation if performed by
kernel, e.g. md(4).
Several syscalls, among them is fork(2), may now return ENOMEM when
global or per-uid limits are enforced.
In collaboration with: pho
Reviewed by: alc
Approved by: re (kensmith)
2009-06-23 20:45:22 +00:00
|
|
|
"0x%lx 0x%lx %d %d %p %s%s%s %d %d 0x%x %s %s %s %s %s %d\n",
|
2008-12-08 12:34:52 +00:00
|
|
|
(u_long)e_start, (u_long)e_end,
|
2005-06-30 07:49:22 +00:00
|
|
|
resident, privateresident,
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
wrap32 ? NULL : obj, /* Hide 64 bit value */
|
|
|
|
#else
|
|
|
|
obj,
|
|
|
|
#endif
|
2008-12-08 12:34:52 +00:00
|
|
|
(e_prot & VM_PROT_READ)?"r":"-",
|
|
|
|
(e_prot & VM_PROT_WRITE)?"w":"-",
|
|
|
|
(e_prot & VM_PROT_EXECUTE)?"x":"-",
|
1998-04-29 04:28:22 +00:00
|
|
|
ref_count, shadow_count, flags,
|
2008-12-08 12:34:52 +00:00
|
|
|
(e_eflags & MAP_ENTRY_COW)?"COW":"NCOW",
|
|
|
|
(e_eflags & MAP_ENTRY_NEEDS_COPY)?"NC":"NNC",
|
Implement global and per-uid accounting of the anonymous memory. Add
rlimit RLIMIT_SWAP that limits the amount of swap that may be reserved
for the uid.
The accounting information (charge) is associated with either map entry,
or vm object backing the entry, assuming the object is the first one
in the shadow chain and entry does not require COW. Charge is moved
from entry to object on allocation of the object, e.g. during the mmap,
assuming the object is allocated, or on the first page fault on the
entry. It moves back to the entry on forks due to COW setup.
The per-entry granularity of accounting makes the charge process fair
for processes that change uid during lifetime, and decrements charge
for proper uid when region is unmapped.
The interface of vm_pager_allocate(9) is extended by adding struct ucred *,
that is used to charge appropriate uid when allocation if performed by
kernel, e.g. md(4).
Several syscalls, among them is fork(2), may now return ENOMEM when
global or per-uid limits are enforced.
In collaboration with: pho
Reviewed by: alc
Approved by: re (kensmith)
2009-06-23 20:45:22 +00:00
|
|
|
type, fullpath,
|
2010-12-02 17:37:16 +00:00
|
|
|
cred ? "CH":"NCH", cred ? cred->cr_ruid : -1);
|
2003-09-29 20:53:19 +00:00
|
|
|
|
|
|
|
if (freepath != NULL)
|
|
|
|
free(freepath, M_TEMP);
|
2008-12-10 16:11:09 +00:00
|
|
|
vm_map_lock_read(map);
|
2008-10-04 14:08:16 +00:00
|
|
|
if (error == -1) {
|
|
|
|
error = 0;
|
1996-10-30 03:45:00 +00:00
|
|
|
break;
|
2007-04-23 06:12:24 +00:00
|
|
|
}
|
2008-12-29 12:45:11 +00:00
|
|
|
if (last_timestamp != map->timestamp) {
|
2008-12-08 12:34:52 +00:00
|
|
|
/*
|
|
|
|
* Look again for the entry because the map was
|
|
|
|
* modified while it was unlocked. Specifically,
|
|
|
|
* the entry may have been clipped, merged, or deleted.
|
|
|
|
*/
|
|
|
|
vm_map_lookup_entry(map, e_end - 1, &tmp_entry);
|
|
|
|
entry = tmp_entry;
|
|
|
|
}
|
1996-06-17 22:53:27 +00:00
|
|
|
}
|
2007-04-23 06:12:24 +00:00
|
|
|
vm_map_unlock_read(map);
|
2008-12-12 12:12:36 +00:00
|
|
|
vmspace_free(vm);
|
2001-12-04 01:35:06 +00:00
|
|
|
return (error);
|
1996-06-18 05:16:00 +00:00
|
|
|
}
|