2005-01-06 19:10:42 +01:00
|
|
|
/*-
|
1996-06-18 00:53:27 +02: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-03-01 00:42:47 +01:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1996-06-18 00:53:27 +02: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 03:08:13 +02:00
|
|
|
* $FreeBSD$
|
1996-06-18 00:53:27 +02:00
|
|
|
*/
|
|
|
|
|
2005-06-30 09:49:22 +02:00
|
|
|
#include "opt_compat.h"
|
|
|
|
|
1996-06-18 00:53:27 +02:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2001-05-01 10:13:21 +02:00
|
|
|
#include <sys/lock.h>
|
2003-09-29 22:53:19 +02:00
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/malloc.h>
|
2007-04-23 08:12:24 +02:00
|
|
|
#include <sys/mount.h>
|
1996-06-18 00:53:27 +02: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 22:45:22 +02:00
|
|
|
#include <sys/resourcevar.h>
|
2013-03-09 03:32:23 +01:00
|
|
|
#include <sys/rwlock.h>
|
2008-10-04 16:08:16 +02:00
|
|
|
#include <sys/sbuf.h>
|
2010-03-11 15:49:06 +01:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2008-12-29 13:41:32 +01:00
|
|
|
#include <sys/sysent.h>
|
|
|
|
#endif
|
2001-12-04 02:35:06 +01:00
|
|
|
#include <sys/uio.h>
|
2003-09-29 22:53:19 +02:00
|
|
|
#include <sys/vnode.h>
|
2001-05-01 10:13:21 +02:00
|
|
|
|
2001-12-04 02:35:06 +01:00
|
|
|
#include <fs/pseudofs/pseudofs.h>
|
2001-05-23 11:42:29 +02:00
|
|
|
#include <fs/procfs/procfs.h>
|
1996-06-18 00:53:27 +02:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
2008-12-12 13:12:36 +01:00
|
|
|
#include <vm/vm_extern.h>
|
1996-06-18 00:53:27 +02:00
|
|
|
#include <vm/pmap.h>
|
|
|
|
#include <vm/vm_map.h>
|
|
|
|
#include <vm/vm_page.h>
|
|
|
|
#include <vm/vm_object.h>
|
|
|
|
|
1996-10-30 04:45:00 +01:00
|
|
|
#define MEBUFFERSIZE 256
|
1996-06-18 00:53:27 +02:00
|
|
|
|
1996-10-30 04:45:00 +01: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-18 00:53:27 +02:00
|
|
|
int
|
2001-12-04 02:35:06 +01:00
|
|
|
procfs_doprocmap(PFS_FILL_ARGS)
|
1996-06-18 00:53:27 +02:00
|
|
|
{
|
2008-12-12 13:12:36 +01:00
|
|
|
struct vmspace *vm;
|
|
|
|
vm_map_t map;
|
2008-12-08 13:34:52 +01:00
|
|
|
vm_map_entry_t entry, tmp_entry;
|
2007-04-23 08:12:24 +02:00
|
|
|
struct vnode *vp;
|
2003-09-29 22:53:19 +02:00
|
|
|
char *fullpath, *freepath;
|
2010-12-02 18:37:16 +01:00
|
|
|
struct ucred *cred;
|
2012-10-22 19:50:54 +02:00
|
|
|
int error;
|
2008-12-08 13:34:52 +01:00
|
|
|
unsigned int last_timestamp;
|
2010-03-11 15:49:06 +01:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 09:49:22 +02:00
|
|
|
int wrap32 = 0;
|
|
|
|
#endif
|
1996-06-18 00:53:27 +02:00
|
|
|
|
2003-08-14 17:26:44 +02:00
|
|
|
PROC_LOCK(p);
|
|
|
|
error = p_candebug(td, p);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
1996-06-18 00:53:27 +02:00
|
|
|
if (uio->uio_rw != UIO_READ)
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
|
2010-03-11 15:49:06 +01:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2011-01-26 21:03:58 +01:00
|
|
|
if (SV_CURPROC_FLAG(SV_ILP32)) {
|
|
|
|
if (!(SV_PROC_FLAG(p, SV_ILP32)))
|
2005-06-30 09:49:22 +02:00
|
|
|
return (EOPNOTSUPP);
|
|
|
|
wrap32 = 1;
|
|
|
|
}
|
|
|
|
#endif
|
2007-04-15 19:06:09 +02:00
|
|
|
|
2008-12-12 13:12:36 +01:00
|
|
|
vm = vmspace_acquire_ref(p);
|
|
|
|
if (vm == NULL)
|
|
|
|
return (ESRCH);
|
|
|
|
map = &vm->vm_map;
|
2007-04-23 08:12:24 +02:00
|
|
|
vm_map_lock_read(map);
|
2008-10-04 16:08:16 +02:00
|
|
|
for (entry = map->header.next; entry != &map->header;
|
|
|
|
entry = entry->next) {
|
1996-06-18 00:53:27 +02:00
|
|
|
vm_object_t obj, tobj, lobj;
|
1999-01-21 09:29:12 +01:00
|
|
|
int ref_count, shadow_count, flags;
|
2008-12-08 13:34:52 +01:00
|
|
|
vm_offset_t e_start, e_end, addr;
|
1996-06-18 00:53:27 +02:00
|
|
|
int resident, privateresident;
|
|
|
|
char *type;
|
2008-12-08 13:34:52 +01:00
|
|
|
vm_eflags_t e_eflags;
|
|
|
|
vm_prot_t e_prot;
|
1996-06-18 00:53:27 +02:00
|
|
|
|
1999-02-07 22:48:23 +01:00
|
|
|
if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
|
1996-06-18 00:53:27 +02:00
|
|
|
continue;
|
|
|
|
|
2008-12-08 13:34:52 +01:00
|
|
|
e_eflags = entry->eflags;
|
|
|
|
e_prot = entry->protection;
|
|
|
|
e_start = entry->start;
|
|
|
|
e_end = entry->end;
|
2007-04-23 08:12:24 +02:00
|
|
|
privateresident = 0;
|
1996-06-18 00:53:27 +02:00
|
|
|
obj = entry->object.vm_object;
|
2007-04-23 08:12:24 +02:00
|
|
|
if (obj != NULL) {
|
2013-06-05 19:00:10 +02:00
|
|
|
VM_OBJECT_RLOCK(obj);
|
2007-04-23 08:12:24 +02:00
|
|
|
if (obj->shadow_count == 1)
|
|
|
|
privateresident = obj->resident_page_count;
|
|
|
|
}
|
2010-12-02 18:37:16 +01:00
|
|
|
cred = (entry->cred) ? entry->cred : (obj ? obj->cred : NULL);
|
1996-06-18 00:53:27 +02:00
|
|
|
|
|
|
|
resident = 0;
|
|
|
|
addr = entry->start;
|
|
|
|
while (addr < entry->end) {
|
2007-04-23 08:12:24 +02:00
|
|
|
if (pmap_extract(map->pmap, addr))
|
1996-06-18 00:53:27 +02:00
|
|
|
resident++;
|
|
|
|
addr += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2007-04-23 08:12:24 +02:00
|
|
|
for (lobj = tobj = obj; tobj; tobj = tobj->backing_object) {
|
|
|
|
if (tobj != obj)
|
2013-06-05 19:00:10 +02:00
|
|
|
VM_OBJECT_RLOCK(tobj);
|
2007-04-23 08:12:24 +02:00
|
|
|
if (lobj != obj)
|
2013-06-05 19:00:10 +02:00
|
|
|
VM_OBJECT_RUNLOCK(lobj);
|
1996-06-18 00:53:27 +02:00
|
|
|
lobj = tobj;
|
2007-04-23 08:12:24 +02:00
|
|
|
}
|
2008-12-08 13:34:52 +01:00
|
|
|
last_timestamp = map->timestamp;
|
|
|
|
vm_map_unlock_read(map);
|
1996-06-18 00:53:27 +02:00
|
|
|
|
2003-09-29 22:53:19 +02:00
|
|
|
freepath = NULL;
|
|
|
|
fullpath = "-";
|
1998-04-29 06:28:22 +02:00
|
|
|
if (lobj) {
|
2015-06-02 20:37:04 +02:00
|
|
|
vp = NULL;
|
2008-12-08 13:34:52 +01:00
|
|
|
switch (lobj->type) {
|
2001-12-04 02:35:06 +01:00
|
|
|
default:
|
|
|
|
case OBJT_DEFAULT:
|
1998-04-29 06:28:22 +02:00
|
|
|
type = "default";
|
|
|
|
break;
|
2001-12-04 02:35:06 +01:00
|
|
|
case OBJT_VNODE:
|
1998-04-29 06:28:22 +02:00
|
|
|
type = "vnode";
|
2007-04-23 08:12:24 +02:00
|
|
|
vp = lobj->handle;
|
|
|
|
vref(vp);
|
1998-04-29 06:28:22 +02:00
|
|
|
break;
|
2001-12-04 02:35:06 +01:00
|
|
|
case OBJT_SWAP:
|
2015-06-02 20:37:04 +02:00
|
|
|
if ((lobj->flags & OBJ_TMPFS_NODE) != 0) {
|
|
|
|
type = "vnode";
|
|
|
|
if ((lobj->flags & OBJ_TMPFS) != 0) {
|
|
|
|
vp = lobj->un_pager.swp.swp_tmpfs;
|
|
|
|
vref(vp);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
type = "swap";
|
|
|
|
}
|
1998-04-29 06:28:22 +02:00
|
|
|
break;
|
2009-07-24 15:50:29 +02:00
|
|
|
case OBJT_SG:
|
2001-12-04 02:35:06 +01:00
|
|
|
case OBJT_DEVICE:
|
1998-04-29 06:28:22 +02:00
|
|
|
type = "device";
|
|
|
|
break;
|
|
|
|
}
|
2007-04-23 08:12:24 +02:00
|
|
|
if (lobj != obj)
|
2013-06-05 19:00:10 +02:00
|
|
|
VM_OBJECT_RUNLOCK(lobj);
|
2003-12-07 18:40:00 +01:00
|
|
|
|
1998-04-29 06:28:22 +02:00
|
|
|
flags = obj->flags;
|
|
|
|
ref_count = obj->ref_count;
|
|
|
|
shadow_count = obj->shadow_count;
|
2013-06-05 19:00:10 +02:00
|
|
|
VM_OBJECT_RUNLOCK(obj);
|
2007-04-23 08:12:24 +02:00
|
|
|
if (vp != NULL) {
|
|
|
|
vn_fullpath(td, vp, &fullpath, &freepath);
|
2008-11-04 20:04:01 +01:00
|
|
|
vrele(vp);
|
2007-04-23 08:12:24 +02:00
|
|
|
}
|
1996-07-27 20:28:10 +02:00
|
|
|
} else {
|
|
|
|
type = "none";
|
1998-04-29 06:28:22 +02:00
|
|
|
flags = 0;
|
|
|
|
ref_count = 0;
|
|
|
|
shadow_count = 0;
|
1996-06-18 00:53:27 +02: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 22:45:22 +02:00
|
|
|
* start, end, resident, private resident, cow, access, type,
|
|
|
|
* charged, charged uid.
|
1996-06-18 00:53:27 +02:00
|
|
|
*/
|
2008-10-04 16:08:16 +02: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 22:45:22 +02: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 13:34:52 +01:00
|
|
|
(u_long)e_start, (u_long)e_end,
|
2005-06-30 09:49:22 +02:00
|
|
|
resident, privateresident,
|
2010-03-11 15:49:06 +01:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 09:49:22 +02:00
|
|
|
wrap32 ? NULL : obj, /* Hide 64 bit value */
|
|
|
|
#else
|
|
|
|
obj,
|
|
|
|
#endif
|
2008-12-08 13:34:52 +01:00
|
|
|
(e_prot & VM_PROT_READ)?"r":"-",
|
|
|
|
(e_prot & VM_PROT_WRITE)?"w":"-",
|
|
|
|
(e_prot & VM_PROT_EXECUTE)?"x":"-",
|
1998-04-29 06:28:22 +02:00
|
|
|
ref_count, shadow_count, flags,
|
2008-12-08 13:34:52 +01: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 22:45:22 +02:00
|
|
|
type, fullpath,
|
2010-12-02 18:37:16 +01:00
|
|
|
cred ? "CH":"NCH", cred ? cred->cr_ruid : -1);
|
2003-09-29 22:53:19 +02:00
|
|
|
|
|
|
|
if (freepath != NULL)
|
|
|
|
free(freepath, M_TEMP);
|
2008-12-10 17:11:09 +01:00
|
|
|
vm_map_lock_read(map);
|
2008-10-04 16:08:16 +02:00
|
|
|
if (error == -1) {
|
|
|
|
error = 0;
|
1996-10-30 04:45:00 +01:00
|
|
|
break;
|
2007-04-23 08:12:24 +02:00
|
|
|
}
|
2008-12-29 13:45:11 +01:00
|
|
|
if (last_timestamp != map->timestamp) {
|
2008-12-08 13:34:52 +01: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-18 00:53:27 +02:00
|
|
|
}
|
2007-04-23 08:12:24 +02:00
|
|
|
vm_map_unlock_read(map);
|
2008-12-12 13:12:36 +01:00
|
|
|
vmspace_free(vm);
|
2001-12-04 02:35:06 +01:00
|
|
|
return (error);
|
1996-06-18 07:16:00 +02:00
|
|
|
}
|