From owner-p4-projects@FreeBSD.ORG Sat Nov 13 15:41:28 2010 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 72484106567A; Sat, 13 Nov 2010 15:41:28 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1C894106564A for ; Sat, 13 Nov 2010 15:41:28 +0000 (UTC) (envelope-from trasz@freebsd.org) Received: from skunkworks.freebsd.org (skunkworks.freebsd.org [IPv6:2001:4f8:fff6::2d]) by mx1.freebsd.org (Postfix) with ESMTP id 075258FC13 for ; Sat, 13 Nov 2010 15:41:28 +0000 (UTC) Received: from skunkworks.freebsd.org (localhost [127.0.0.1]) by skunkworks.freebsd.org (8.14.4/8.14.4) with ESMTP id oADFfRMI079557 for ; Sat, 13 Nov 2010 15:41:27 GMT (envelope-from trasz@freebsd.org) Received: (from perforce@localhost) by skunkworks.freebsd.org (8.14.4/8.14.4/Submit) id oADFfR6u079554 for perforce@freebsd.org; Sat, 13 Nov 2010 15:41:27 GMT (envelope-from trasz@freebsd.org) Date: Sat, 13 Nov 2010 15:41:27 GMT Message-Id: <201011131541.oADFfR6u079554@skunkworks.freebsd.org> X-Authentication-Warning: skunkworks.freebsd.org: perforce set sender to trasz@freebsd.org using -f From: Edward Tomasz Napierala To: Perforce Change Reviews Precedence: bulk Cc: Subject: PERFORCE change 185740 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 13 Nov 2010 15:41:28 -0000 http://p4web.freebsd.org/@@185740?ac=10 Change 185740 by trasz@trasz_victim on 2010/11/13 15:40:27 Rework RUSAGE_SWAP accounting to use newly added rusage_sub_cred(). Affected files ... .. //depot/projects/soc2009/trasz_limits/sys/kern/kern_container.c#33 edit .. //depot/projects/soc2009/trasz_limits/sys/sys/container.h#15 edit .. //depot/projects/soc2009/trasz_limits/sys/vm/swap_pager.c#12 edit .. //depot/projects/soc2009/trasz_limits/sys/vm/vm_map.c#25 edit .. //depot/projects/soc2009/trasz_limits/sys/vm/vm_pageout.c#17 edit Differences ... ==== //depot/projects/soc2009/trasz_limits/sys/kern/kern_container.c#33 (text+ko) ==== @@ -36,9 +36,11 @@ #include #include +#include #include #include #include +#include #include #include #include @@ -64,9 +66,11 @@ SDT_PROVIDER_DEFINE(container); SDT_PROBE_DEFINE3(container, kernel, rusage, add, add, "struct proc *", "int", "uint64_t"); SDT_PROBE_DEFINE3(container, kernel, rusage, add_failure, add-failure, "struct proc *", "int", "uint64_t"); +SDT_PROBE_DEFINE3(container, kernel, rusage, add_force, add-force, "struct proc *", "int", "uint64_t"); SDT_PROBE_DEFINE3(container, kernel, rusage, set, set, "struct proc *", "int", "uint64_t"); SDT_PROBE_DEFINE3(container, kernel, rusage, set_failure, set-failure, "struct proc *", "int", "uint64_t"); SDT_PROBE_DEFINE3(container, kernel, rusage, sub, sub, "struct proc *", "int", "uint64_t"); +SDT_PROBE_DEFINE3(container, kernel, rusage, sub_cred, sub-cred, "struct ucred *", "int", "uint64_t"); SDT_PROBE_DEFINE1(container, kernel, container, create, create, "struct container *"); SDT_PROBE_DEFINE1(container, kernel, container, destroy, destroy, "struct container *"); SDT_PROBE_DEFINE2(container, kernel, container, join, join, "struct container *", "struct container *"); @@ -120,6 +124,18 @@ } static int +container_resource_sloppy(int resource) +{ + + switch (resource) { + case RUSAGE_SWAP: + return (1); + default: + return (0); + } +} + +static int container_add(struct container *dest, const struct container *src) { int i, error; @@ -169,14 +185,19 @@ * Update resource usage in dest. */ for (i = 0; i <= RUSAGE_MAX; i++) { - KASSERT(dest->c_resources[i] >= 0, - ("resource usage propagation meltdown: dest < 0")); - KASSERT(src->c_resources[i] >= 0, - ("resource usage propagation meltdown: src < 0")); - KASSERT(src->c_resources[i] <= dest->c_resources[i], - ("resource usage propagation meltdown: src > dest")); - if (container_resource_reclaimable(i)) + if (!container_resource_sloppy(i)) { + KASSERT(dest->c_resources[i] >= 0, + ("resource usage propagation meltdown: dest < 0")); + KASSERT(src->c_resources[i] >= 0, + ("resource usage propagation meltdown: src < 0")); + KASSERT(src->c_resources[i] <= dest->c_resources[i], + ("resource usage propagation meltdown: src > dest")); + } + if (container_resource_reclaimable(i)) { dest->c_resources[i] -= src->c_resources[i]; + if (container_resource_sloppy(i) && dest->c_resources[i] < 0) + dest->c_resources[i] = 0; + } } /* @@ -267,7 +288,7 @@ for (i = 0; i <= RUSAGE_MAX; i++) KASSERT(container->c_resources[i] == 0, - ("container->c_resources[%d] != NULL", i)); + ("container->c_resources[%d] != 0", i)); for (i = 0; i <= CONTAINER_PARENTS_MAX; i++) KASSERT(container->c_parents[i] == NULL, ("container->c_parents[%d] != NULL", i)); @@ -284,6 +305,8 @@ KASSERT(container != NULL, ("NULL container")); for (i = 0; i <= RUSAGE_MAX; i++) { + if (container_resource_sloppy(i)) + continue; KASSERT(container->c_resources[i] == 0 || !container_resource_reclaimable(i), ("destroying non-empty container: " @@ -328,6 +351,8 @@ continue; container_assert(parent); for (resource = 0; resource <= RUSAGE_MAX; resource++) { + if (container_resource_sloppy(resource)) + continue; KASSERT(parent->c_resources[resource] >= container->c_resources[resource], ("resource usage propagation meltdown: child > parent")); @@ -351,6 +376,9 @@ KASSERT(container != NULL, ("NULL container")); container->c_resources[resource] += amount; + if (container_resource_sloppy(resource) && container->c_resources[resource] < 0) + container->c_resources[resource] = 0; + for (i = 0; i <= CONTAINER_PARENTS_MAX; i++) { if (container->c_parents[i] == NULL) continue; @@ -395,6 +423,26 @@ return (0); } +/* + * Increase allocation of 'resource' by 'amount' for process 'p'. + */ +void +rusage_add_force(struct proc *p, int resource, uint64_t amount) +{ + + if (p->p_flag & P_SYSTEM) + return; + + SDT_PROBE(container, kernel, rusage, add_force, p, resource, amount, 0, 0); + + KASSERT(amount > 0, ("rusage_add_force: invalid amount for resource %d: %ju", + resource, amount)); + + mtx_lock(&container_lock); + container_alloc_resource(&p->p_container, resource, amount); + mtx_unlock(&container_lock); +} + static int rusage_set_locked(struct proc *p, int resource, uint64_t amount) { @@ -494,6 +542,29 @@ } /* + * Decrease allocation of 'resource' by 'amount' for credential 'cred'. + */ +void +rusage_sub_cred(struct ucred *cred, int resource, uint64_t amount) +{ + + SDT_PROBE(container, kernel, rusage, sub_cred, cred, resource, amount, 0, 0); + + KASSERT(amount > 0, ("rusage_sub_cred: invalid amount for resource %d: %ju", + resource, amount)); + KASSERT(container_resource_reclaimable(resource), + ("rusage_sub_cred: called for non-reclaimable resource %d", resource)); + KASSERT(container_resource_sloppy(resource), + ("rusage_sub_cred: called for non-sloppy resource %d", resource)); + + mtx_lock(&container_lock); + container_alloc_resource(&cred->cr_ruidinfo->ui_container, resource, -amount); + container_alloc_resource(&cred->cr_prison->pr_container, resource, -amount); + container_alloc_resource(&cred->cr_loginclass->lc_container, resource, -amount); + mtx_unlock(&container_lock); +} + +/* * Inherit resource usage information and containing containers * from the parent process. */ ==== //depot/projects/soc2009/trasz_limits/sys/sys/container.h#15 (text+ko) ==== @@ -38,6 +38,7 @@ struct proc; struct hrl_rule_link; +struct ucred; /* * Resource containers. @@ -105,8 +106,10 @@ }; int rusage_add(struct proc *p, int resource, uint64_t amount); +void rusage_add_force(struct proc *p, int resource, uint64_t amount); int rusage_set(struct proc *p, int resource, uint64_t amount); void rusage_sub(struct proc *p, int resource, uint64_t amount); +void rusage_sub_cred(struct ucred *cred, int resource, uint64_t amount); uint64_t rusage_get_limit(struct proc *p, int resource); void container_create(struct container *container); ==== //depot/projects/soc2009/trasz_limits/sys/vm/swap_pager.c#12 (text+ko) ==== @@ -246,6 +246,10 @@ swap_reserved += incr; mtx_unlock(&sw_dev_mtx); +#ifdef CONTAINERS + rusage_add_force(curproc, RUSAGE_SWAP, incr); +#endif + uip = curthread->td_ucred->cr_ruidinfo; PROC_LOCK(curproc); UIDINFO_VMSIZE_LOCK(uip); @@ -259,9 +263,6 @@ { struct ucred *cred; -#ifdef CONTAINERS - rusage_sub(curproc, RUSAGE_SWAP, decr); -#endif PROC_LOCK(curproc); cred = curthread->td_ucred; swap_release_by_cred(decr, cred); @@ -287,6 +288,10 @@ printf("negative vmsize for uid = %d\n", uip->ui_uid); uip->ui_vmsize -= decr; UIDINFO_VMSIZE_UNLOCK(uip); + +#ifdef CONTAINERS + rusage_sub_cred(cred, RUSAGE_SWAP, decr); +#endif } static void swapdev_strategy(struct buf *, struct swdevt *sw); ==== //depot/projects/soc2009/trasz_limits/sys/vm/vm_map.c#25 (text+ko) ==== @@ -322,7 +322,6 @@ rusage_set(p, RUSAGE_RSS, 0); rusage_set(p, RUSAGE_MEMLOCK, 0); rusage_set(p, RUSAGE_VMEM, 0); - rusage_set(p, RUSAGE_SWAP, 0); } #endif ==== //depot/projects/soc2009/trasz_limits/sys/vm/vm_pageout.c#17 (text+ko) ==== @@ -1712,21 +1712,6 @@ size = vmspace_resident_count(vm); rusage_set(p, RUSAGE_RSS, IDX_TO_OFF(size)); } - - /* - * This is the ugly (and temporary, hopefully) part - * of dealing with RUSAGE_SWAP. Basically, we increase - * swap counters in the proper place, and decrease them - * here. Doing it properly will require adding either - * proc or ucred pointer to vm objects; before I do that, - * I want to get a better feeling on how the memory - * management works. In other words, it's post-shm task. - */ - if (vm_map_trylock_read(&vm->vm_map)) { - size = vmspace_swap_count(vm); - vm_map_unlock_read(&vm->vm_map); - rusage_set(p, RUSAGE_SWAP, IDX_TO_OFF(size)); - } #endif vmspace_free(vm); }