Skip to content

Commit e5671df

Browse files
glommerdavem330
authored andcommitted
Basic kernel memory functionality for the Memory Controller
This patch lays down the foundation for the kernel memory component of the Memory Controller. As of today, I am only laying down the following files: * memory.independent_kmem_limit * memory.kmem.limit_in_bytes (currently ignored) * memory.kmem.usage_in_bytes (always zero) Signed-off-by: Glauber Costa <glommer@parallels.com> CC: Kirill A. Shutemov <kirill@shutemov.name> CC: Paul Menage <paul@paulmenage.org> CC: Greg Thelen <gthelen@google.com> CC: Johannes Weiner <jweiner@redhat.com> CC: Michal Hocko <mhocko@suse.cz> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 08e34eb commit e5671df

3 files changed

Lines changed: 149 additions & 7 deletions

File tree

‎Documentation/cgroups/memory.txt‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ Features:
4444
- oom-killer disable knob and oom-notifier
4545
- Root cgroup has no limit controls.
4646

47-
Kernel memory and Hugepages are not under control yet. We just manage
48-
pages on LRU. To add more controls, we have to take care of performance.
47+
Hugepages is not under control yet. We just manage pages on LRU. To add more
48+
controls, we have to take care of performance. Kernel memory support is work
49+
in progress, and the current version provides basically functionality.
4950

5051
Brief summary of control files.
5152

@@ -56,8 +57,11 @@ Brief summary of control files.
5657
(See 5.5 for details)
5758
memory.memsw.usage_in_bytes # show current res_counter usage for memory+Swap
5859
(See 5.5 for details)
60+
memory.kmem.usage_in_bytes # show current res_counter usage for kmem only.
61+
(See 2.7 for details)
5962
memory.limit_in_bytes # set/show limit of memory usage
6063
memory.memsw.limit_in_bytes # set/show limit of memory+Swap usage
64+
memory.kmem.limit_in_bytes # if allowed, set/show limit of kernel memory
6165
memory.failcnt # show the number of memory usage hits limits
6266
memory.memsw.failcnt # show the number of memory+Swap hits limits
6367
memory.max_usage_in_bytes # show max memory usage recorded
@@ -72,6 +76,9 @@ Brief summary of control files.
7276
memory.oom_control # set/show oom controls.
7377
memory.numa_stat # show the number of memory usage per numa node
7478

79+
memory.independent_kmem_limit # select whether or not kernel memory limits are
80+
independent of user limits
81+
7582
1. History
7683

7784
The memory controller has a long history. A request for comments for the memory
@@ -255,6 +262,35 @@ When oom event notifier is registered, event will be delivered.
255262
per-zone-per-cgroup LRU (cgroup's private LRU) is just guarded by
256263
zone->lru_lock, it has no lock of its own.
257264

265+
2.7 Kernel Memory Extension (CONFIG_CGROUP_MEM_RES_CTLR_KMEM)
266+
267+
With the Kernel memory extension, the Memory Controller is able to limit
268+
the amount of kernel memory used by the system. Kernel memory is fundamentally
269+
different than user memory, since it can't be swapped out, which makes it
270+
possible to DoS the system by consuming too much of this precious resource.
271+
272+
Some kernel memory resources may be accounted and limited separately from the
273+
main "kmem" resource. For instance, a slab cache that is considered important
274+
enough to be limited separately may have its own knobs.
275+
276+
Kernel memory limits are not imposed for the root cgroup. Usage for the root
277+
cgroup may or may not be accounted.
278+
279+
Memory limits as specified by the standard Memory Controller may or may not
280+
take kernel memory into consideration. This is achieved through the file
281+
memory.independent_kmem_limit. A Value different than 0 will allow for kernel
282+
memory to be controlled separately.
283+
284+
When kernel memory limits are not independent, the limit values set in
285+
memory.kmem files are ignored.
286+
287+
Currently no soft limit is implemented for kernel memory. It is future work
288+
to trigger slab reclaim when those limits are reached.
289+
290+
2.7.1 Current Kernel Memory resources accounted
291+
292+
None
293+
258294
3. User Interface
259295

260296
0. Configuration

‎init/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,17 @@ config CGROUP_MEM_RES_CTLR_SWAP_ENABLED
689689
For those who want to have the feature enabled by default should
690690
select this option (if, for some reason, they need to disable it
691691
then swapaccount=0 does the trick).
692+
config CGROUP_MEM_RES_CTLR_KMEM
693+
bool "Memory Resource Controller Kernel Memory accounting (EXPERIMENTAL)"
694+
depends on CGROUP_MEM_RES_CTLR && EXPERIMENTAL
695+
default n
696+
help
697+
The Kernel Memory extension for Memory Resource Controller can limit
698+
the amount of memory used by kernel objects in the system. Those are
699+
fundamentally different from the entities handled by the standard
700+
Memory Controller, which are page-based, and can be swapped. Users of
701+
the kmem extension can use it to guarantee that no group of processes
702+
will ever exhaust kernel resources alone.
692703

693704
config CGROUP_PERF
694705
bool "Enable perf_event per-cpu per-container group (cgroup) monitoring"

‎mm/memcontrol.c‎

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ struct mem_cgroup {
226226
* the counter to account for mem+swap usage.
227227
*/
228228
struct res_counter memsw;
229+
/*
230+
* the counter to account for kmem usage.
231+
*/
232+
struct res_counter kmem;
229233
/*
230234
* Per cgroup active and inactive list, similar to the
231235
* per zone LRU lists.
@@ -276,6 +280,11 @@ struct mem_cgroup {
276280
* mem_cgroup ? And what type of charges should we move ?
277281
*/
278282
unsigned long move_charge_at_immigrate;
283+
/*
284+
* Should kernel memory limits be stabilished independently
285+
* from user memory ?
286+
*/
287+
int kmem_independent_accounting;
279288
/*
280289
* percpu counter.
281290
*/
@@ -344,9 +353,14 @@ enum charge_type {
344353
};
345354

346355
/* for encoding cft->private value on file */
347-
#define _MEM (0)
348-
#define _MEMSWAP (1)
349-
#define _OOM_TYPE (2)
356+
357+
enum mem_type {
358+
_MEM = 0,
359+
_MEMSWAP,
360+
_OOM_TYPE,
361+
_KMEM,
362+
};
363+
350364
#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
351365
#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
352366
#define MEMFILE_ATTR(val) ((val) & 0xffff)
@@ -3848,10 +3862,17 @@ static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
38483862
u64 val;
38493863

38503864
if (!mem_cgroup_is_root(memcg)) {
3865+
val = 0;
3866+
#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
3867+
if (!memcg->kmem_independent_accounting)
3868+
val = res_counter_read_u64(&memcg->kmem, RES_USAGE);
3869+
#endif
38513870
if (!swap)
3852-
return res_counter_read_u64(&memcg->res, RES_USAGE);
3871+
val += res_counter_read_u64(&memcg->res, RES_USAGE);
38533872
else
3854-
return res_counter_read_u64(&memcg->memsw, RES_USAGE);
3873+
val += res_counter_read_u64(&memcg->memsw, RES_USAGE);
3874+
3875+
return val;
38553876
}
38563877

38573878
val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
@@ -3884,6 +3905,11 @@ static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
38843905
else
38853906
val = res_counter_read_u64(&memcg->memsw, name);
38863907
break;
3908+
#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
3909+
case _KMEM:
3910+
val = res_counter_read_u64(&memcg->kmem, name);
3911+
break;
3912+
#endif
38873913
default:
38883914
BUG();
38893915
break;
@@ -4612,6 +4638,69 @@ static int mem_control_numa_stat_open(struct inode *unused, struct file *file)
46124638
}
46134639
#endif /* CONFIG_NUMA */
46144640

4641+
#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
4642+
static u64 kmem_limit_independent_read(struct cgroup *cgroup, struct cftype *cft)
4643+
{
4644+
return mem_cgroup_from_cont(cgroup)->kmem_independent_accounting;
4645+
}
4646+
4647+
static int kmem_limit_independent_write(struct cgroup *cgroup, struct cftype *cft,
4648+
u64 val)
4649+
{
4650+
struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
4651+
struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4652+
4653+
val = !!val;
4654+
4655+
/*
4656+
* This follows the same hierarchy restrictions than
4657+
* mem_cgroup_hierarchy_write()
4658+
*/
4659+
if (!parent || !parent->use_hierarchy) {
4660+
if (list_empty(&cgroup->children))
4661+
memcg->kmem_independent_accounting = val;
4662+
else
4663+
return -EBUSY;
4664+
}
4665+
else
4666+
return -EINVAL;
4667+
4668+
return 0;
4669+
}
4670+
static struct cftype kmem_cgroup_files[] = {
4671+
{
4672+
.name = "independent_kmem_limit",
4673+
.read_u64 = kmem_limit_independent_read,
4674+
.write_u64 = kmem_limit_independent_write,
4675+
},
4676+
{
4677+
.name = "kmem.usage_in_bytes",
4678+
.private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
4679+
.read_u64 = mem_cgroup_read,
4680+
},
4681+
{
4682+
.name = "kmem.limit_in_bytes",
4683+
.private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
4684+
.read_u64 = mem_cgroup_read,
4685+
},
4686+
};
4687+
4688+
static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
4689+
{
4690+
int ret = 0;
4691+
4692+
ret = cgroup_add_files(cont, ss, kmem_cgroup_files,
4693+
ARRAY_SIZE(kmem_cgroup_files));
4694+
return ret;
4695+
};
4696+
4697+
#else
4698+
static int register_kmem_files(struct cgroup *cont, struct cgroup_subsys *ss)
4699+
{
4700+
return 0;
4701+
}
4702+
#endif
4703+
46154704
static struct cftype mem_cgroup_files[] = {
46164705
{
46174706
.name = "usage_in_bytes",
@@ -4925,6 +5014,7 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
49255014
if (parent && parent->use_hierarchy) {
49265015
res_counter_init(&memcg->res, &parent->res);
49275016
res_counter_init(&memcg->memsw, &parent->memsw);
5017+
res_counter_init(&memcg->kmem, &parent->kmem);
49285018
/*
49295019
* We increment refcnt of the parent to ensure that we can
49305020
* safely access it on res_counter_charge/uncharge.
@@ -4935,6 +5025,7 @@ mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
49355025
} else {
49365026
res_counter_init(&memcg->res, NULL);
49375027
res_counter_init(&memcg->memsw, NULL);
5028+
res_counter_init(&memcg->kmem, NULL);
49385029
}
49395030
memcg->last_scanned_child = 0;
49405031
memcg->last_scanned_node = MAX_NUMNODES;
@@ -4978,6 +5069,10 @@ static int mem_cgroup_populate(struct cgroup_subsys *ss,
49785069

49795070
if (!ret)
49805071
ret = register_memsw_files(cont, ss);
5072+
5073+
if (!ret)
5074+
ret = register_kmem_files(cont, ss);
5075+
49815076
return ret;
49825077
}
49835078

0 commit comments

Comments
 (0)