cgroups: Resource controller for open files.
RFC 1/1 cgroups: Resource controller for open files.
Add a resource controller for limiting the number of open file handles. This allows us to catch misbehaving processes and return EMFILE instead of ENOMEM for kernel memory limits.
--- fs/Makefile | 1 + fs/file.c | 44 ++++++++ fs/filescontrol.c | 247 ++++++++++++++++++++++++++++++++++++++++++ include/linux/cgroup_subsys.h | 5 + include/linux/fdtable.h | 3 + include/linux/filescontrol.h | 32 ++++++ init/Kconfig | 7 ++ 7 files changed, 339 insertions(+) create mode 100644 fs/filescontrol.c create mode 100644 include/linux/filescontrol.h
fs/Makefile +1 -0
@@ -48,6 +48,7 @@ obj-$(CONFIG_COREDUMP) += coredump.o obj-$(CONFIG_SYSCTL) += drop_caches.o obj-$(CONFIG_FHANDLE) += fhandle.o +obj-$(CONFIG_CGROUP_FILES) += filescontrol.o obj-y += quota/
fs/file.c +44 -0
89 lines not on this page · raw message
fs/filescontrol.c new file +247 -0
247 lines not on this page · raw message
include/linux/cgroup_subsys.h +5 -0
@@ -50,6 +50,11 @@ SUBSYS(net_prio)#if IS_ENABLED (CONFIG_CGROUP_HUGETLB ) SUBSYS(hugetlb)#endif + +#if IS_ENABLED (CONFIG_CGROUP_FILES ) +SUBSYS(files) +#endif +/* * DO NOT ADD ANY SUBSYSTEM WITHOUT EXPLICIT ACKS FROM CGROUP MAINTAINERS. */
include/linux/fdtable.h +3 -0
@@ -57,6 +57,9 @@ struct files_struct {unsigned long close_on_exec_init[1 ];unsigned long open_fds_init[1 ];struct file __rcu * fd_array[NR_OPEN_DEFAULT ]; +#ifdef CONFIG_CGROUP_FILES +struct files_cgroup *files_cgroup; +#endif };struct file_operations ;
include/linux/filescontrol.h new file +32 -0
@@ -0,0 +1,32 @@ +/* filescontrol.h - Files Controller +* +* Copyright 2014 Google Inc. +* Author: Brian Makin <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#ifndef _LINUX_FILESCONTROL_H +#define _LINUX_FILESCONTROL_H + +#include <linux/fdtable.h> + +#ifdef CONFIG_CGROUP_FILES + +extern int files_cgroup_alloc_fd (struct files_struct *files,u64 n); +extern void files_cgroup_unalloc_fd (struct files_struct *files,u64 n); +extern u64 files_cgroup_count_fds (struct files_struct *files); + +void files_cgroup_assign (struct files_struct *files); +void files_cgroup_remove (struct files_struct *files); + +#endif /* CONFIG_CGROUP_FILES */ +#endif /* _LINUX_FILESCONTROL_H */
init/Kconfig +7 -0
@@ -1089,6 +1089,13 @@ config DEBUG_BLK_CGROUP Enable some debugging help. Currently it exports additional stat files in a cgroup which can be useful for debugging. +config CGROUP_FILES + bool "Files Resource Controller for Control Groups" + default n + help + Provides a cgroup resource controller that limits number of open + file handles within a cgroup. + endif # CGROUPS config CHECKPOINT_RESTORE
Signature · 1 line
2.0.0.526.g5318336
Tejun Heo
maintainerHello,
Quoted 3 lines · Binder Makin
Add a resource controller for limiting the number of open file handles. This allows us to catch misbehaving processes and return EMFILE instead of ENOMEM for kernel memory limits.
cc'ing memcg people. Isn't kmemcg supposed to cover this by controlling kernel memory usage?
Thanks.
Signature · 1 line
tejun
Tejun Heo
maintainerHello,
Quoted 4 lines · Binder Makin
kmem keeps the cgroup from DOSing the machine but then you can get a ENOMEM out of some call which is trying to allocate kernel memory. People are used to limiting file handles from rlimits and the EMFILE is easier for them to debug/fix.
EMFILE instead of ENOMEM seems really thin as the sole rationale for a whole new controller, doesn't it?
Thanks.
Signature · 1 line
tejun
Tejun Heo
maintainerQuoted 5 lines · Binder Makin
It does allow processes which are used to handling file handled to deal with the situation more cleanly.
If you get the ENOMEM then you are out of memory and you kill the process, but if you are out of file handles we can clean up the open handles and continue.
I don't know. If the program can manage fds dynamically, it may as well police itself under a sensible limit. I get that programs which already work with EMFILE may benefit from this but this isn't adding any essential feature and the same goal can be trivially achieved without any help from kernel as the benefits from EMFILE handling assumes the program is well-behaving. I'm very doubtful that custom-fitting kernel behavior to specific and rather peculiar program behaviors is a healthy approach.
Thanks.
Signature · 1 line
tejun