diff options
author | kib <kib@FreeBSD.org> | 2009-06-23 20:45:22 +0000 |
---|---|---|
committer | kib <kib@FreeBSD.org> | 2009-06-23 20:45:22 +0000 |
commit | fa686c638eece83a18de058d1934f4722487818b (patch) | |
tree | efadbd0bda4d9f0ec36869d4d465b2cabf2dcd1b /sys/kern/uipc_shm.c | |
parent | 39fa9f1c9918ad9bb25af4f1bbce28c34cb2cd65 (diff) | |
download | FreeBSD-src-fa686c638eece83a18de058d1934f4722487818b.zip FreeBSD-src-fa686c638eece83a18de058d1934f4722487818b.tar.gz |
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)
Diffstat (limited to 'sys/kern/uipc_shm.c')
-rw-r--r-- | sys/kern/uipc_shm.c | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/sys/kern/uipc_shm.c b/sys/kern/uipc_shm.c index 3819154..32bfd2d 100644 --- a/sys/kern/uipc_shm.c +++ b/sys/kern/uipc_shm.c @@ -110,7 +110,7 @@ static struct shmfd *shm_hold(struct shmfd *shmfd); static void shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd); static struct shmfd *shm_lookup(char *path, Fnv32_t fnv); static int shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred); -static void shm_dotruncate(struct shmfd *shmfd, off_t length); +static int shm_dotruncate(struct shmfd *shmfd, off_t length); static fo_rdwr_t shm_read; static fo_rdwr_t shm_write; @@ -167,8 +167,7 @@ shm_truncate(struct file *fp, off_t length, struct ucred *active_cred, if (error) return (error); #endif - shm_dotruncate(shmfd, length); - return (0); + return (shm_dotruncate(shmfd, length)); } static int @@ -242,23 +241,26 @@ shm_close(struct file *fp, struct thread *td) return (0); } -static void +static int shm_dotruncate(struct shmfd *shmfd, off_t length) { vm_object_t object; vm_page_t m; vm_pindex_t nobjsize; + vm_ooffset_t delta; object = shmfd->shm_object; VM_OBJECT_LOCK(object); if (length == shmfd->shm_size) { VM_OBJECT_UNLOCK(object); - return; + return (0); } nobjsize = OFF_TO_IDX(length + PAGE_MASK); /* Are we shrinking? If so, trim the end. */ if (length < shmfd->shm_size) { + delta = ptoa(object->size - nobjsize); + /* Toss in memory pages. */ if (nobjsize < object->size) vm_object_page_remove(object, nobjsize, object->size, @@ -266,8 +268,11 @@ shm_dotruncate(struct shmfd *shmfd, off_t length) /* Toss pages from swap. */ if (object->type == OBJT_SWAP) - swap_pager_freespace(object, nobjsize, - object->size - nobjsize); + swap_pager_freespace(object, nobjsize, delta); + + /* Free the swap accounted for shm */ + swap_release_by_uid(delta, object->uip); + object->charge -= delta; /* * If the last page is partially mapped, then zero out @@ -307,6 +312,15 @@ shm_dotruncate(struct shmfd *shmfd, off_t length) vm_page_cache_free(object, OFF_TO_IDX(length), nobjsize); } + } else { + + /* Attempt to reserve the swap */ + delta = ptoa(nobjsize - object->size); + if (!swap_reserve_by_uid(delta, object->uip)) { + VM_OBJECT_UNLOCK(object); + return (ENOMEM); + } + object->charge += delta; } shmfd->shm_size = length; mtx_lock(&shm_timestamp_lock); @@ -315,6 +329,7 @@ shm_dotruncate(struct shmfd *shmfd, off_t length) mtx_unlock(&shm_timestamp_lock); object->size = nobjsize; VM_OBJECT_UNLOCK(object); + return (0); } /* @@ -332,7 +347,7 @@ shm_alloc(struct ucred *ucred, mode_t mode) shmfd->shm_gid = ucred->cr_gid; shmfd->shm_mode = mode; shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL, - shmfd->shm_size, VM_PROT_DEFAULT, 0); + shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred); KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate")); VM_OBJECT_LOCK(shmfd->shm_object); vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING); |