aboutsummaryrefslogtreecommitdiff
path: root/sys/kern/kern_resource.c
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2013-10-21 16:44:53 +0000
committerKonstantin Belousov <kib@FreeBSD.org>2013-10-21 16:44:53 +0000
commit9110db818a1e36cffed5208b6d005d9c1b84236a (patch)
tree34a18ded32934f0a4679a54dd36c7f1f14b2d63d /sys/kern/kern_resource.c
parentce95d2f9228a2b4ff144a011ff315b0256277481 (diff)
downloadsrc-9110db818a1e36cffed5208b6d005d9c1b84236a.tar.gz
src-9110db818a1e36cffed5208b6d005d9c1b84236a.zip
Add a resource limit for the total number of kqueues available to the
user. Kqueue now saves the ucred of the allocating thread, to correctly decrement the counter on close. Under some specific and not real-world use scenario for kqueue, it is possible for the kqueues to consume memory proportional to the square of the number of the filedescriptors available to the process. Limit allows administrator to prevent the abuse. This is kernel-mode side of the change, with the user-mode enabling commit following. Reported and tested by: pho Discussed with: jmg Sponsored by: The FreeBSD Foundation MFC after: 2 weeks
Notes
Notes: svn path=/head/; revision=256849
Diffstat (limited to 'sys/kern/kern_resource.c')
-rw-r--r--sys/kern/kern_resource.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c
index 57ee671aa796..470eaedd52d7 100644
--- a/sys/kern/kern_resource.c
+++ b/sys/kern/kern_resource.c
@@ -1432,3 +1432,21 @@ chgptscnt(uip, diff, max)
}
return (1);
}
+
+int
+chgkqcnt(struct uidinfo *uip, int diff, rlim_t max)
+{
+
+ if (diff > 0 && max != 0) {
+ if (atomic_fetchadd_long(&uip->ui_kqcnt, (long)diff) +
+ diff > max) {
+ atomic_subtract_long(&uip->ui_kqcnt, (long)diff);
+ return (0);
+ }
+ } else {
+ atomic_add_long(&uip->ui_kqcnt, (long)diff);
+ if (uip->ui_kqcnt < 0)
+ printf("negative kqcnt for uid = %d\n", uip->ui_uid);
+ }
+ return (1);
+}