PERFORCE change 188178 for review

Edward Tomasz Napierala trasz at FreeBSD.org
Tue Jan 25 23:29:18 UTC 2011


http://p4web.freebsd.org/@@188178?ac=10

Change 188178 by trasz at trasz_victim on 2011/01/25 23:28:55

	Rename some routines, document them, and don't display values
	for sloppy resources for processes.

Affected files ...

.. //depot/projects/soc2009/trasz_limits/sys/kern/kern_container.c#65 edit
.. //depot/projects/soc2009/trasz_limits/sys/kern/kern_rctl.c#17 edit
.. //depot/projects/soc2009/trasz_limits/sys/sys/container.h#24 edit

Differences ...

==== //depot/projects/soc2009/trasz_limits/sys/kern/kern_container.c#65 (text+ko) ====

@@ -86,8 +86,13 @@
 SDT_PROBE_DEFINE2(container, kernel, container, join_failure, join-failure, "struct container *", "struct container *");
 SDT_PROBE_DEFINE2(container, kernel, container, leave, leave, "struct container *", "struct container *");
 
+/*
+ * Amount stored in c_resources[] is thousand times bigger than what's
+ * visible to the userland.  It gets fixed up when retrieving resource
+ * usage or adding rules.
+ */
 int
-container_resource_in_thousands(int resource)
+rusage_is_in_thousands(int resource)
 {
 	switch (resource) {
 	case RUSAGE_CPU:
@@ -99,8 +104,11 @@
 	}
 }
 
+/*
+ * Resource usage can drop, as opposed to only grow.
+ */
 static int
-container_resource_reclaimable(int resource)
+rusage_is_reclaimable(int resource)
 {
 
 	switch (resource) {
@@ -113,8 +121,11 @@
 	}
 }
 
+/*
+ * Children inherit resource usage.
+ */
 static int
-container_resource_inheritable(int resource)
+rusage_is_inheritable(int resource)
 {
 
 	switch (resource) {
@@ -140,8 +151,13 @@
 	}
 }
 
+/*
+ * rusage_{add,set}(9) can actually return an error and not update resource
+ * usage counters.  Note that even when resource is not deniable, allocating
+ * resource might cause signals to be sent by RCTL code.
+ */
 static int
-container_resource_deniable(int resource)
+rusage_is_deniable(int resource)
 {
 
 	switch (resource) {
@@ -154,8 +170,13 @@
 	}
 }
 
-static int
-container_resource_sloppy(int resource)
+/*
+ * Per-process resource usage information makes no sense, but per-credential
+ * one does.  This kind of resources are usually allocated for process, but
+ * freed using credentials.
+ */
+int
+rusage_is_sloppy(int resource)
 {
 
 	switch (resource) {
@@ -174,8 +195,11 @@
 	}
 }
 
+/*
+ * XXX: Explain somehow.
+ */
 static int
-container_resource_dampened(int resource)
+rusage_is_dampened(int resource)
 {
 	switch (resource) {
 	case RUSAGE_PCTCPU:
@@ -215,8 +239,8 @@
 	 * Update resource usage in dest.
 	 */
 	for (i = 0; i <= RUSAGE_MAX; i++) {
-		if (!container_resource_sloppy(i) &&
-		    !container_resource_dampened(i)) {
+		if (!rusage_is_sloppy(i) &&
+		    !rusage_is_dampened(i)) {
 			KASSERT(dest->c_resources[i] >= 0,
 			    ("resource usage propagation meltdown: dest < 0"));
 			KASSERT(src->c_resources[i] >= 0,
@@ -224,11 +248,11 @@
 			KASSERT(src->c_resources[i] <= dest->c_resources[i],
 			    ("resource usage propagation meltdown: src > dest"));
 		}
-		if (container_resource_reclaimable(i)) {
+		if (rusage_is_reclaimable(i)) {
 			dest->c_resources[i] -= src->c_resources[i];
 			if (dest->c_resources[i] < 0) {
-				KASSERT(container_resource_sloppy(i) ||
-				    container_resource_dampened(i),
+				KASSERT(rusage_is_sloppy(i) ||
+				    rusage_is_dampened(i),
 				    ("container_sub: usage < 0"));
 				dest->c_resources[i] = 0;
 			}
@@ -259,11 +283,11 @@
 	KASSERT(container != NULL, ("NULL container"));
 
 	for (i = 0; i <= RUSAGE_MAX; i++) {
-		if (container_resource_sloppy(i))
+		if (rusage_is_sloppy(i))
 			continue;
-		if (!container_resource_reclaimable(i))
+		if (!rusage_is_reclaimable(i))
 			continue;
-		if (container_resource_dampened(i))
+		if (rusage_is_dampened(i))
 			continue;
 		KASSERT(container->c_resources[i] == 0,
 		    ("destroying non-empty container: "
@@ -296,8 +320,8 @@
 
 	container->c_resources[resource] += amount;
 	if (container->c_resources[resource] < 0) {
-		KASSERT(container_resource_sloppy(resource) ||
-		    container_resource_dampened(resource),
+		KASSERT(rusage_is_sloppy(resource) ||
+		    rusage_is_dampened(resource),
 		    ("container_alloc_resource: usage < 0"));
 		container->c_resources[resource] = 0;
 	}
@@ -329,7 +353,7 @@
 	mtx_lock(&container_lock);
 #ifdef RCTL
 	error = rctl_enforce(p, resource, amount);
-	if (error && container_resource_deniable(resource)) {
+	if (error && rusage_is_deniable(resource)) {
 		SDT_PROBE(container, kernel, rusage, add_failure, p, resource, amount, 0, 0);
 		mtx_unlock(&container_lock);
 		return (error);
@@ -421,14 +445,14 @@
 
 	diff = amount - p->p_container.c_resources[resource];
 #ifdef notyet
-	KASSERT(diff >= 0 || container_resource_reclaimable(resource),
+	KASSERT(diff >= 0 || rusage_is_reclaimable(resource),
 	    ("rusage_set: usage of non-reclaimable resource %d dropping",
 	     resource));
 #endif
 #ifdef RCTL
 	if (diff > 0) {
 		error = rctl_enforce(p, resource, diff);
-		if (error && container_resource_deniable(resource)) {
+		if (error && rusage_is_deniable(resource)) {
 			SDT_PROBE(container, kernel, rusage, set_failure, p, resource, amount, 0, 0);
 			return (error);
 		}
@@ -540,7 +564,7 @@
 	PROC_LOCK_ASSERT(p, MA_OWNED);
 	KASSERT(amount >= 0, ("rusage_sub: invalid amount for resource %d: %ju",
 	    resource, amount));
-	KASSERT(container_resource_reclaimable(resource),
+	KASSERT(rusage_is_reclaimable(resource),
 	    ("rusage_sub: called for non-reclaimable resource %d", resource));
 
 	mtx_lock(&container_lock);
@@ -564,7 +588,7 @@
 	KASSERT(amount >= 0, ("rusage_sub_cred: invalid amount for resource %d: %ju",
 	    resource, amount));
 #ifdef notyet
-	KASSERT(container_resource_reclaimable(resource),
+	KASSERT(rusage_is_reclaimable(resource),
 	    ("rusage_sub_cred: called for non-reclaimable resource %d", resource));
 #endif
 
@@ -616,7 +640,7 @@
 	 */
 	for (i = 0; i <= RUSAGE_MAX; i++) {
 		if (parent->p_container.c_resources[i] == 0 ||
-		    !container_resource_inheritable(i))
+		    !rusage_is_inheritable(i))
 			continue;
 
 		error = rusage_set_locked(child, i, parent->p_container.c_resources[i]);

==== //depot/projects/soc2009/trasz_limits/sys/kern/kern_rctl.c#17 (text+ko) ====

@@ -247,7 +247,7 @@
 	if (available < 0) {
 #ifdef notyet
 		KASSERT(rule->rr_action != RCTL_ACTION_DENY ||
-		    !container_resource_deniable(rule->rr_resource),
+		    !rusage_is_deniable(rule->rr_resource),
 		    ("rctl_would_exceed: deny rule already exceeded"));
 #endif
 		return (0);
@@ -867,7 +867,7 @@
 		error = str2int64(amountstr, &rule->rr_amount);
 		if (error != 0)
 			goto out;
-		if (container_resource_in_thousands(rule->rr_resource))
+		if (rusage_is_in_thousands(rule->rr_resource))
 			rule->rr_amount *= 1000;
 	}
 
@@ -1086,7 +1086,7 @@
 
 	amount = rule->rr_amount;
 	if (amount != RCTL_AMOUNT_UNDEFINED &&
-	    container_resource_in_thousands(rule->rr_resource))
+	    rusage_is_in_thousands(rule->rr_resource))
 		amount /= 1000;
 
 	sbuf_printf(sb, "%s:%s=%jd",
@@ -1145,7 +1145,7 @@
 }
 
 static struct sbuf *
-rctl_container_to_sbuf(struct container *container)
+rctl_container_to_sbuf(struct container *container, int sloppy)
 {
 	int i;
 	int64_t amount;
@@ -1153,8 +1153,10 @@
 
 	sb = sbuf_new_auto();
 	for (i = 0; i <= RUSAGE_MAX; i++) {
+		if (sloppy == 0 && rusage_is_sloppy(i))
+			continue;
 		amount = container->c_resources[i];
-		if (container_resource_in_thousands(i))
+		if (rusage_is_in_thousands(i))
 			amount /= 1000;
 		sbuf_printf(sb, "%s=%jd,", rctl_resource_name(i), amount);
 	}
@@ -1193,7 +1195,7 @@
 			error = EINVAL;
 			goto out;
 		}
-		outputsbuf = rctl_container_to_sbuf(&p->p_container);
+		outputsbuf = rctl_container_to_sbuf(&p->p_container, 0);
 		break;
 	case RCTL_SUBJECT_TYPE_USER:
 		uip = filter->rr_subject.rs_uip;
@@ -1201,7 +1203,7 @@
 			error = EINVAL;
 			goto out;
 		}
-		outputsbuf = rctl_container_to_sbuf(&uip->ui_container);
+		outputsbuf = rctl_container_to_sbuf(&uip->ui_container, 1);
 		break;
 	case RCTL_SUBJECT_TYPE_LOGINCLASS:
 		lc = filter->rr_subject.hr_loginclass;
@@ -1209,7 +1211,7 @@
 			error = EINVAL;
 			goto out;
 		}
-		outputsbuf = rctl_container_to_sbuf(&lc->lc_container);
+		outputsbuf = rctl_container_to_sbuf(&lc->lc_container, 1);
 		break;
 	case RCTL_SUBJECT_TYPE_JAIL:
 		pr = filter->rr_subject.rs_prison;
@@ -1217,7 +1219,7 @@
 			error = EINVAL;
 			goto out;
 		}
-		outputsbuf = rctl_container_to_sbuf(&pr->pr_container);
+		outputsbuf = rctl_container_to_sbuf(&pr->pr_container, 1);
 		break;
 	default:
 		error = EINVAL;

==== //depot/projects/soc2009/trasz_limits/sys/sys/container.h#24 (text+ko) ====

@@ -113,6 +113,7 @@
 void	container_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
 	    struct ucred *newcred);
 
-int	container_resource_in_thousands(int resource);
+int	rusage_is_in_thousands(int resource);
+int	rusage_is_sloppy(int resource);
 
 #endif /* !_CONTAINER_H_ */


More information about the p4-projects mailing list