PERFORCE change 185740 for review
Edward Tomasz Napierala
trasz at FreeBSD.org
Sat Nov 13 15:41:28 UTC 2010
http://p4web.freebsd.org/@@185740?ac=10
Change 185740 by trasz at 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 <sys/container.h>
#include <sys/param.h>
+#include <sys/jail.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/lock.h>
+#include <sys/loginclass.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/proc.h>
@@ -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);
}
More information about the p4-projects
mailing list