Memory controller (RSS Control)
Subsystems (11)
cover Memory controller (RSS Control)
This patch applies on top of Paul Menage's container patches (V7) posted at
http://lkml.org/lkml/2007/2/12/88
It implements a controller within the containers framework for limiting memory usage (RSS usage).
The memory controller was discussed at length in the RFC posted to lkml
http://lkml.org/lkml/2006/10/30/51
Steps to use the controller
--------------------------
0. Download the patches, apply the patches
1. Turn on CONFIG_CONTAINER_MEMCTLR in kernel config, build the kernel and boot into the new kernel
2. mount -t container container -o memctlr /<mount point>
3. cd /<mount point>
optionally do (mkdir <directory>; cd <directory>) under /<mount point>
4. echo $$ > tasks (attaches the current shell to the container)
5. echo -n (limit value) > memctlr_limit
6. cat memctlr_usage
7. Run tasks, check the usage of the controller, reclaim behaviour
8. Report bugs, get bug fixes and iterate (goto step 0).
Advantages of the patchset
--------------------------
1. Zero overhead in struct page (struct page is not expanded)
2. Minimal changes to the core-mm code
3. Shared pages are not reclaimed unless all mappings belong to overlimit containers.
4. It can be used to debug drivers/applications/kernel components in a constrained memory environment (similar to mem=XXX option), except that several containers can be created simultaneously without rebooting and the limits can be changed. NOTE: There is no support for limiting kernel memory allocations and page cache control (presently).
Testing
-------
Ran kernbench and lmbench with containers enabled (container filesystem not mounted), they seemed to run fine
Created containers, attached tasks to containers with lower limits than the memory the tasks require (memory hog tests) and ran some basic tests on them
TODO's and improvement areas
----------------------------
1. Come up with cool page replacement algorithms for containers (if possible without any changes to struct page)
2. Add page cache control
3. Add kernel memory allocator control
4. Extract benchmark numbers and overhead data
Comments & criticism are welcome.
Series
------
memctlr-setup.patch
memctlr-acct.patch
memctlr-reclaim-on-limit.patch
memctlr-doc.patch
Signature · 2 lines
Warm Regards, Balbir Singh
RFC PATCH 1/4 RSS controller setup
This patch sets up the basic controller infrastructure on top of the containers infrastructure. Two files are provided for monitoring and control memctlr_usage and memctlr_limit.
memctlr_usage shows the current usage (in pages, of RSS) and the limit set by the user.
memctlr_limit can be used to set a limit on the RSS usage of the resource. A special value of 0, indicates that the usage is unlimited. The limit is set in units of pages.
---
include/linux/memctlr.h | 22 ++++++ init/Kconfig | 7 + mm/Makefile | 1 mm/memctlr.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+)
include/linux/memctlr.h new file +22 -0
@@ -0,0 +1,22 @@ +/* memctlr.h - Memory Controller for containers +* +* Copyright (C) Balbir Singh, IBM Corp. 2006-2007 +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of version 2.1 of the GNU Lesser General Public License +* as published by the Free Software Foundation. +* +* This program is distributed in the hope that it would be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +*/ + +#ifndef _LINUX_MEMCTLR_H +#define _LINUX_MEMCTLR_H + +#ifdef CONFIG_CONTAINER_MEMCTLR + +#else /* CONFIG_CONTAINER_MEMCTLR */ + +#endif /* CONFIG_CONTAINER_MEMCTLR */ +#endif /* _LINUX_MEMCTLR_H */
init/Kconfig renamed from init/Kconfig~memctlr-setup +7 -0
@@ -306,6 +306,13 @@ config CONTAINER_NS for instance virtual servers and checkpoint/restart jobs. +config CONTAINER_MEMCTLR + bool "A simple RSS based memory controller" + select CONTAINERS + help + Provides a simple Resource Controller for monitoring and + controlling the total Resident Set Size of the tasks in a container + config RELAY bool "Kernel->user space relay support (formerly relayfs)" help
mm/Makefile renamed from mm/Makefile~memctlr-setup +1 -0
@@ -29,3 +29,4 @@ obj-$(CONFIG_MEMORY_HOTPLUG) += memory_h obj-$(CONFIG_FS_XIP) += filemap_xip.o obj-$(CONFIG_MIGRATION) += migrate.o obj-$(CONFIG_SMP) += allocpercpu.o +obj-$(CONFIG_CONTAINER_MEMCTLR) += memctlr.o
mm/memctlr.c new file +169 -0
169 lines not on this page · raw message
_
Signature · 2 lines
Warm Regards, Balbir Singh
Andrew Morton
maintainerRe: [RFC][PATCH][1/4] RSS controller setup
Quoted 4 lines · On Mon, 19 Feb 2007 12:20:26 +0530 Balbir Singh <[email protected]> wrote:
This patch sets up the basic controller infrastructure on top of the containers infrastructure. Two files are provided for monitoring and control
memctlr_usageandmemctlr_limit.
The patches use the identifier "memctlr" a lot. It is hard to remember, and unpronounceable. Something like memcontrol or mem_controller or memory_controller would be more typical.
mm/memctlr.c line 80
Quoted 5 lines
...
mm/memctlr.c new file +3
-0+BUG_ON (!mem); +if ((buffer =kmalloc (nbytes +1 ,GFP_KERNEL )) ==0 ) +return -ENOMEM ;
Please prefer to do
buffer = kmalloc(nbytes + 1, GFP_KERNEL); if (buffer == NULL) reutrn -ENOMEM;
ie: avoid the assign-and-test-in-the-same-statement thing. This affects the whole patchset.
Also, please don't compare pointers to literal zero like that. It makes me get buried it patches to convert it to "NULL". I think this is a sparse thing.
Quoted 12 lines
mm/memctlr.c new file +12
-0+ buffer[nbytes] =0 ; +if (copy_from_user (buffer, userbuf, nbytes)) { + ret = -EFAULT ; + goto out_err; + } + +container_manage_lock (); +if (container_is_removed (cont)) { + ret = -ENODEV ; + goto out_unlock; + } +
mm/memctlr.c line 103
Quoted 10 lines
mm/memctlr.c new file +10
-0+ limit =simple_strtoul (buffer,NULL ,10 ); +/* +* 0 is a valid limit (unlimited resource usage) +*/ +if (!limit &&strcmp (buffer,"0" )) + goto out_unlock; + +spin_lock (&mem->lock); + mem->counter.limit = limit; +spin_unlock (&mem->lock);
The patches do this a lot: a single atomic assignment with a pointless-looking lock/unlock around it. It's often the case that this idiom indicates a bug, or needless locking. I think the only case where it makes sense is when there's some other code somewhere which is doing
spin_lock(&mem->lock); ... use1(mem->counter.limit); ... use2(mem->counter.limit); ... spin_unlock(&mem->lock);
where use1() and use2() expect the two reads of mem->counter.limit to return the same value.
Is that the case in these patches? If not, we might have a problem in there.
Quoted 9 lines
mm/memctlr.c new file +9
-0+ +static ssize_t memctlr_read (struct container *cont,struct cftype *cft, +struct file *file,char __user *userbuf, +size_t nbytes,loff_t *ppos) +{ +unsigned long usage, limit; +char usagebuf[64 ];/* Move away from stack later */ +char *s = usagebuf; +struct memctlr *mem =memctlr_from_cont (cont);
mm/memctlr.c line 130
Quoted 10 lines
mm/memctlr.c new file +10
-0+ +spin_lock (&mem->lock); + usage = mem->counter.usage; + limit = mem->counter.limit; +spin_unlock (&mem->lock); + + s +=sprintf (s,"usage %lu, limit %ld\n" , usage, limit); +return simple_read_from_buffer (userbuf, nbytes, ppos, usagebuf, + s - usagebuf); +}
This output is hard to parse and to extend. I'd suggest either two separate files, or multi-line output:
usage: %lu kB
limit: %lu kB
and what are the units of these numbers? Page counts? If so, please don't do that: it requires appplications and humans to know the current kernel's page size.
Quoted 7 lines
mm/memctlr.c new file +7
-0+static struct cftype memctlr_usage = { + .name ="memctlr_usage" , + .read = memctlr_read, +}; + +static struct cftype memctlr_limit = { + .name ="memctlr_limit" ,
mm/memctlr.c line 148
Quoted 10 lines
mm/memctlr.c new file +10
-0+ .write = memctlr_write, +}; + +static int memctlr_populate (struct container_subsys *ss, +struct container *cont) +{ +int rc; +if ((rc =container_add_file (cont, &memctlr_usage)) <0 ) +return rc; +if ((rc =container_add_file (cont, &memctlr_limit)) <0 )
Clean up the first file here?
Quoted 11 lines
mm/memctlr.c new file +11
-0+return rc; +return 0 ; +} + +static struct container_subsys memctlr_subsys = { + .name ="memctlr" , + .create = memctlr_create, + .destroy = memctlr_destroy, + .populate = memctlr_populate, +}; +
mm/memctlr.c line 169
Quoted 10 lines
mm/memctlr.c new file +10
-0+int __init memctlr_init (void ) +{ +int id; + + id =container_register_subsys (&memctlr_subsys); +printk ("Initializing memctlr version %s, id %d\n" , version, id); +return id <0 ? id :0 ; +} + +module_init (memctlr_init);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC][PATCH][1/4] RSS controller setup
Quoted 6 lines · Andrew Morton
This output is hard to parse and to extend. I'd suggest either two separate files, or multi-line output:
usage: %lu kB
limit: %lu kB
Two separate files would be the container usage model that I envisaged, inherited from the way cpusets does things.
And in this case, it should definitely be the limit in one file, readable and writeable, and the usage in another, probably only readable.
Having to read a file called memctlr_usage to find the current limit sounds wrong.
Hmm, I don't appear to have documented this yet, but I think a good naming scheme for container files is <subsystem>.<whatever> - i.e. these should be memctlr.usage and memctlr.limit. The existing grandfathered Cpusets names violate this, but I'm not sure there's a lot we can do about that.
Quoted 9 lines
mm/memctlr.c line 148
Quoted 7 lines
mm/memctlr.c new file +7
-0+static int memctlr_populate (struct container_subsys *ss, +struct container *cont) +{ +int rc; +if ((rc =container_add_file (cont, &memctlr_usage)) <0 ) +return rc; +if ((rc =container_add_file (cont, &memctlr_limit)) <0 )Clean up the first file here?
Containers don't currently provide an API for a subsystem to clean up files from a directory - that's done automatically when the directory is deleted.
I think I'll probably change the API for container_add_file to return void, but mark an error in the container itself if something goes wrong - that way rather than all the subsystems having to check for error, container_populate_dir() can do so at the end of calling all the subsystems' populate methods.
Paul
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC][PATCH][1/4] RSS controller setup
Quoted 11 lines · Paul Menage
Quoted 6 lines · Andrew Morton
This output is hard to parse and to extend. I'd suggest either two separate files, or multi-line output:
usage: %lu kB
limit: %lu kBTwo separate files would be the container usage model that I envisaged, inherited from the way cpusets does things.
Quoted 7 lines
And in this case, it should definitely be the limit in one file, readable and writeable, and the usage in another, probably only readable.
Having to read a file called
memctlr_usageto find the current limit sounds wrong.
That sound right, I'll fix this.
Quoted 6 lines
Hmm, I don't appear to have documented this yet, but I think a good naming scheme for container files is <subsystem>.<whatever> - i.e. these should be memctlr.usage and memctlr.limit. The existing grandfathered Cpusets names violate this, but I'm not sure there's a lot we can do about that.
Why <subsystem>.<whatever>, dots are harder to parse using regular expressions and sound DOS'ish. I'd prefer "_" to separate the subsystem and whatever :-)
Quoted 14 lines
Quoted 9 lines
mm/memctlr.c line 148
Quoted 7 lines
mm/memctlr.c new file +7
-0+static int memctlr_populate (struct container_subsys *ss, +struct container *cont) +{ +int rc; +if ((rc =container_add_file (cont, &memctlr_usage)) <0 ) +return rc; +if ((rc =container_add_file (cont, &memctlr_limit)) <0 )Clean up the first file here?
Containers don't currently provide an API for a subsystem to clean up files from a directory - that's done automatically when the directory is deleted.
Quoted 6 lines
I think I'll probably change the API for
container_add_fileto return void, but mark an error in the container itself if something goes wrong - that way rather than all the subsystems having to check for error,container_populate_dir()can do so at the end of calling all the subsystems' populate methods.
It should be easy to add container_remove_file() instead of marking an error.
Quoted 1 line
Paul
Signature · 2 lines
Warm Regards, Balbir Singh
Re: [RFC][PATCH][1/4] RSS controller setup
Quoted 2 lines · Balbir Singh
Quoted 1 line · Paul Menage
On 2/19/07, Andrew Morton <[email protected]> wrote:
<snip>
Quoted 10 lines
Quoted 6 lines
Hmm, I don't appear to have documented this yet, but I think a good naming scheme for container files is <subsystem>.<whatever> - i.e. these should be memctlr.usage and memctlr.limit. The existing grandfathered Cpusets names violate this, but I'm not sure there's a lot we can do about that.
Why <subsystem>.<whatever>, dots are harder to parse using regular expressions and sound DOS'ish. I'd prefer "_" to separate the subsystem and whatever :-)
"_" is useful for names with "spaces". Names like mem_controller. "." seems reasonable despite its regex nastyness. Alternatively there's always ":".
<snip>
Cheers,
-Matt Helsley
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC][PATCH][1/4] RSS controller setup
In reply to Andrew MortonQuoted 10 lines · Andrew Morton
Quoted 3 lines · On Mon, 19 Feb 2007 12:20:26 +0530 Balbir Singh <[email protected]> wrote:
This patch sets up the basic controller infrastructure on top of the containers infrastructure. Two files are provided for monitoring and control
memctlr_usageandmemctlr_limit.The patches use the identifier "memctlr" a lot. It is hard to remember, and unpronounceable. Something like memcontrol or
mem_controllerormemory_controllerwould be more typical.
I'll change the name to memory_controller
Quoted 8 lines
mm/memctlr.c line 80
Quoted 5 lines
...
mm/memctlr.c new file +3
-0+BUG_ON (!mem); +if ((buffer =kmalloc (nbytes +1 ,GFP_KERNEL )) ==0 ) +return -ENOMEM ;Please prefer to do
Quoted 7 lines
buffer = kmalloc(nbytes + 1, GFP_KERNEL); if (buffer == NULL) reutrn -ENOMEM;ie: avoid the assign-and-test-in-the-same-statement thing. This affects the whole patchset.
I'll fix that
Quoted 4 lines
Also, please don't compare pointers to literal zero like that. It makes me get buried it patches to convert it to "NULL". I think this is a sparse thing.
Good point, I'll fix it.
Quoted 36 lines
Quoted 12 lines
mm/memctlr.c new file +12
-0+ buffer[nbytes] =0 ; +if (copy_from_user (buffer, userbuf, nbytes)) { + ret = -EFAULT ; + goto out_err; + } + +container_manage_lock (); +if (container_is_removed (cont)) { + ret = -ENODEV ; + goto out_unlock; + } +mm/memctlr.c line 103
Quoted 10 lines
mm/memctlr.c new file +10
-0+ limit =simple_strtoul (buffer,NULL ,10 ); +/* +* 0 is a valid limit (unlimited resource usage) +*/ +if (!limit &&strcmp (buffer,"0" )) + goto out_unlock; + +spin_lock (&mem->lock); + mem->counter.limit = limit; +spin_unlock (&mem->lock);The patches do this a lot: a single atomic assignment with a pointless-looking lock/unlock around it. It's often the case that this idiom indicates a bug, or needless locking. I think the only case where it makes sense is when there's some other code somewhere which is doing
spin_lock(&mem->lock); ... use1(mem->counter.limit); ... use2(mem->counter.limit); ... spin_unlock(&mem->lock);
Quoted 6 lines
where
use1()anduse2()expect the two reads ofmem->counter.limitto return the same value.Is that the case in these patches? If not, we might have a problem in there.
The next set of patches move to atomic values for the limits. That should fix the locking.
Quoted 23 lines
Quoted 9 lines
mm/memctlr.c new file +9
-0+ +static ssize_t memctlr_read (struct container *cont,struct cftype *cft, +struct file *file,char __user *userbuf, +size_t nbytes,loff_t *ppos) +{ +unsigned long usage, limit; +char usagebuf[64 ];/* Move away from stack later */ +char *s = usagebuf; +struct memctlr *mem =memctlr_from_cont (cont);mm/memctlr.c line 130
Quoted 10 lines
mm/memctlr.c new file +10
-0+ +spin_lock (&mem->lock); + usage = mem->counter.usage; + limit = mem->counter.limit; +spin_unlock (&mem->lock); + + s +=sprintf (s,"usage %lu, limit %ld\n" , usage, limit); +return simple_read_from_buffer (userbuf, nbytes, ppos, usagebuf, + s - usagebuf); +}This output is hard to parse and to extend. I'd suggest either two separate files, or multi-line output:
Quoted 7 lines
usage: %lu kB
limit: %lu kBand what are the units of these numbers? Page counts? If so, please don't do that: it requires appplications and humans to know the current kernel's page size.
Yes, this looks much better. I'll move to this format. I get myself lost in "bc" at times, that should have been a hint.
Quoted 11 lines
Quoted 11 lines
mm/memctlr.c new file +11
-0+static struct cftype memctlr_usage = { + .name ="memctlr_usage" , + .read = memctlr_read, +}; + +static struct cftype memctlr_limit = { + .name ="memctlr_limit" , + .write = memctlr_write, +}; + +static int memctlr_populate(struct container_subsys *ss,
Quoted 9 lines
mm/memctlr.c line 148
Quoted 6 lines
mm/memctlr.c new file +6
-0+ struct container *cont) +{ +int rc; +if ((rc =container_add_file (cont, &memctlr_usage)) <0 ) +return rc; +if ((rc =container_add_file (cont, &memctlr_limit)) <0 )Clean up the first file here?
I used cpuset_populate() as an example to code this one up. I don't think there is an easy way in containers to clean up files. I'll double check
Quoted 13 lines
Quoted 13 lines
mm/memctlr.c new file +13
-0+return rc; +return 0 ; +} + +static struct container_subsys memctlr_subsys = { + .name ="memctlr" , + .create = memctlr_create, + .destroy = memctlr_destroy, + .populate = memctlr_populate, +}; + +int __init memctlr_init (void ) +{
Quoted 9 lines
mm/memctlr.c line 169
Quoted 8 lines
mm/memctlr.c new file +8
-0+int id; + + id =container_register_subsys (&memctlr_subsys); +printk ("Initializing memctlr version %s, id %d\n" , version, id); +return id <0 ? id :0 ; +} + +module_init (memctlr_init);
Thanks for the detailed review,
Signature · 2 lines
Warm Regards, Balbir Singh
RFC PATCH 2/4 Add RSS accounting and control
In reply to Balbir SinghThis patch adds the basic accounting hooks to account for pages allocated into the RSS of a process. Accounting is maintained at two levels, in the mm_struct of each task and in the memory controller data structure associated with each node in the container.
When the limit specified for the container is exceeded, the task is killed. RSS accounting is consistent with the current definition of RSS in the kernel. Shared pages are accounted into the RSS of each process as is done in the kernel currently. The code is flexible in that it can be easily modified to work with any definition of RSS.
---
fs/exec.c | 4 + include/linux/memctlr.h | 38 ++++++++++++ include/linux/sched.h | 11 +++ kernel/fork.c | 10 +++ mm/memctlr.c | 148 ++++++++++++++++++++++++++++++++++++++++++------ mm/memory.c | 33 +++++++++- mm/rmap.c | 5 + mm/swapfile.c | 2 8 files changed, 232 insertions(+), 19 deletions(-)
fs/exec.c renamed from fs/exec.c~memctlr-acct +4 -0
@@ -50,6 +50,7 @@#include <linux/tsacct_kern.h> #include <linux/cn_proc.h> #include <linux/audit.h> +#include <linux/memctlr.h> #include <asm/uaccess.h> #include <asm/mmu_context.h>
@@ -313,6 +314,9 @@ void install_arg_page(struct vm_area_strif (unlikely (anon_vma_prepare (vma))) goto out; +if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto out; +flush_dcache_page (page); pte =get_locked_pte (mm, address, &ptl);if (!pte)
include/linux/memctlr.h renamed from include/linux/memctlr.h~memctlr-acct +38 -0
@@ -14,9 +14,47 @@#ifndef _LINUX_MEMCTLR_H#define _LINUX_MEMCTLR_H +enum { +MEMCTLR_CHECK_LIMIT = true, +MEMCTLR_DONT_CHECK_LIMIT = false, +}; +#ifdef CONFIG_CONTAINER_MEMCTLR +struct res_counter { +atomic_long_t usage;/* The current usage of the resource being */ +/* counted */ +atomic_long_t limit;/* The limit on the resource */ +atomic_long_t nr_limit_exceeded; +}; + +extern int memctlr_mm_init (struct mm_struct *mm); +extern void memctlr_mm_free (struct mm_struct *mm); +extern void memctlr_mm_assign_container (struct mm_struct *mm, +struct task_struct *p); +extern int memctlr_update_rss (struct mm_struct *mm,int count,bool check); +#else /* CONFIG_CONTAINER_MEMCTLR */ +static inline int memctlr_mm_init (struct mm_struct *mm) +{ +return 0 ; +} + +static inline void memctlr_mm_free (struct mm_struct *mm) +{ +} + +static inline void memctlr_mm_assign_container (struct mm_struct *mm, +struct task_struct *p) +{ +} + +static inline int memctlr_update_rss (struct mm_struct *mm,int count, +bool check) +{ +return 0 ; +} +#endif /* CONFIG_CONTAINER_MEMCTLR */ #endif /* _LINUX_MEMCTLR_H */
include/linux/sched.h renamed from include/linux/sched.h~memctlr-acct +11 -0
@@ -83,6 +83,7 @@ struct sched_param {#include <linux/timer.h> #include <linux/hrtimer.h> #include <linux/task_io_accounting.h> +#include <linux/memctlr.h> #include <asm/processor.h>
@@ -373,6 +374,16 @@ struct mm_struct {/* aio bits */ rwlock_t ioctx_list_lock;struct kioctx *ioctx_list; +#ifdef CONFIG_CONTAINER_MEMCTLR +/* +* Each mm_struct's container, sums up in the container's counter +* We can extend this such that, VMA's counters sum up into this +* counter +*/ +struct res_counter *counter; +struct container *container; +rwlock_t container_lock; +#endif /* CONFIG_CONTAINER_MEMCTLR */ };struct sighand_struct {
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +10 -0
@@ -50,6 +50,7 @@#include <linux/taskstats_kern.h> #include <linux/random.h> #include <linux/numtasks.h> +#include <linux/memctlr.h> #include <asm/pgtable.h> #include <asm/pgalloc.h>
@@ -342,10 +343,15 @@ static struct mm_struct * mm_init(struct mm->free_area_cache =TASK_UNMAPPED_BASE ; mm->cached_hole_size = ~0UL ; +if (!memctlr_mm_init (mm)) + goto err; +if (likely (!mm_alloc_pgd (mm))) { mm->def_flags =0 ;return mm; } + +err:free_mm (mm);return NULL ; }
@@ -361,6 +367,8 @@ struct mm_struct * mm_alloc(void)if (mm) {memset (mm,0 ,sizeof (*mm)); mm =mm_init (mm); +if (mm) +memctlr_mm_assign_container (mm, current); }return mm; }
@@ -375,6 +383,7 @@ void fastcall __mmdrop(struct mm_structBUG_ON (mm == &init_mm);mm_free_pgd (mm);destroy_context (mm); +memctlr_mm_free (mm);free_mm (mm); }
@@ -500,6 +509,7 @@ static struct mm_struct *dup_mm(struct tif (init_new_context (tsk, mm)) goto fail_nocontext; +memctlr_mm_assign_container (mm, tsk); err =dup_mmap (mm, oldmm);if (err) goto free_pt;
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +132 -16
194 lines not on this page · raw message
mm/memory.c renamed from mm/memory.c~memctlr-acct +30 -3
@@ -50,6 +50,7 @@#include <linux/delayacct.h> #include <linux/init.h> #include <linux/writeback.h> +#include <linux/memctlr.h> #include <asm/pgalloc.h> #include <asm/uaccess.h>
@@ -532,6 +533,7 @@ again:spin_unlock (src_ptl);pte_unmap_nested (src_pte -1 );add_mm_rss (dst_mm, rss[0 ], rss[1 ]); +memctlr_update_rss (dst_mm, rss[0 ] + rss[1 ],MEMCTLR_DONT_CHECK_LIMIT );pte_unmap_unlock (dst_pte -1 , dst_ptl);cond_resched ();if (addr != end)
@@ -1128,6 +1130,7 @@ static int zeromap_pte_range(struct mm_spage_cache_get (page);page_add_file_rmap (page);inc_mm_counter (mm, file_rss); +memctlr_update_rss (mm,1 ,MEMCTLR_DONT_CHECK_LIMIT );set_pte_at (mm, addr, pte, zero_pte); }while (pte++, addr +=PAGE_SIZE , addr != end);arch_leave_lazy_mmu_mode ();
@@ -1223,6 +1226,10 @@ static int insert_page(struct mm_structif (PageAnon (page)) goto out; retval = -ENOMEM ; + +if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto out; +flush_dcache_page (page); pte =get_locked_pte (mm, addr, &ptl);if (!pte)
@@ -1580,6 +1587,9 @@ gotten:cow_user_page (new_page, old_page, address, vma); } +if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto oom; +/* * Re-check the pte - we dropped the lock */
@@ -1612,7 +1622,9 @@ gotten:/* Free the old page.. */ new_page = old_page; ret |=VM_FAULT_WRITE ; - } + }else +memctlr_update_rss (mm,-1 ,MEMCTLR_DONT_CHECK_LIMIT ); +if (new_page)page_cache_release (new_page);if (old_page)
@@ -2024,16 +2036,19 @@ static int do_swap_page(struct mm_structmark_page_accessed (page);lock_page (page); +if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto out_nomap; +/* * Back out if somebody else already faulted in this pte. */ page_table =pte_offset_map_lock (mm, pmd, address, &ptl);if (unlikely (!pte_same (*page_table, orig_pte))) - goto out_nomap; + goto out_nomap_uncharge;if (unlikely (!PageUptodate (page))) { ret =VM_FAULT_SIGBUS ; - goto out_nomap; + goto out_nomap_uncharge; }/* The page isn't present yet, go ahead with the fault. */
@@ -2068,6 +2083,8 @@ unlock:pte_unmap_unlock (page_table, ptl); out:return ret; +out_nomap_uncharge: +memctlr_update_rss (mm,-1 ,MEMCTLR_DONT_CHECK_LIMIT ); out_nomap:pte_unmap_unlock (page_table, ptl);unlock_page (page);
@@ -2092,6 +2109,9 @@ static int do_anonymous_page(struct mm_s/* Allocate our own private page. */ pte_unmap (page_table); +if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto oom; +if (unlikely (anon_vma_prepare (vma))) goto oom; page =alloc_zeroed_user_highpage (vma, address);
@@ -2108,6 +2128,8 @@ static int do_anonymous_page(struct mm_slru_cache_add_active (page);page_add_new_anon_rmap (page, vma, address); }else { +memctlr_update_rss (mm,1 ,MEMCTLR_DONT_CHECK_LIMIT ); +/* Map the ZERO_PAGE - vm_page_prot is readonly */ page =ZERO_PAGE (address);page_cache_get (page);
@@ -2218,6 +2240,9 @@ retry: } } +if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto oom; + page_table =pte_offset_map_lock (mm, pmd, address, &ptl);/* * For a file-backed vma, someone could have truncated or otherwise
@@ -2227,6 +2252,7 @@ retry:if (mapping &&unlikely (sequence != mapping->truncate_count)) {pte_unmap_unlock (page_table, ptl);page_cache_release (new_page); +memctlr_update_rss (mm,-1 ,MEMCTLR_DONT_CHECK_LIMIT );cond_resched (); sequence = mapping->truncate_count;smp_rmb ();
@@ -2265,6 +2291,7 @@ retry: }else {/* One of our sibling threads was faster, back out. */ page_cache_release (new_page); +memctlr_update_rss (mm,-1 ,MEMCTLR_DONT_CHECK_LIMIT ); goto unlock; }
mm/rmap.c renamed from mm/rmap.c~memctlr-acct +5 -0
@@ -602,6 +602,11 @@ void page_remove_rmap(struct page *page,__dec_zone_page_state (page,PageAnon (page) ?NR_ANON_PAGES :NR_FILE_MAPPED ); } +/* +* When we pass MEMCTLR_DONT_CHECK_LIMIT, it is ok to call +* this function under the pte lock (since we will not block in reclaim) +*/ +memctlr_update_rss (vma->vm_mm,-1 ,MEMCTLR_DONT_CHECK_LIMIT ); }/*
mm/swapfile.c renamed from mm/swapfile.c~memctlr-acct +2 -0
@@ -27,6 +27,7 @@#include <linux/mutex.h> #include <linux/capability.h> #include <linux/syscalls.h> +#include <linux/memctlr.h> #include <asm/pgtable.h> #include <asm/tlbflush.h>
@@ -514,6 +515,7 @@ static void unuse_pte(struct vm_area_strset_pte_at (vma->vm_mm, addr, pte,pte_mkold (mk_pte (page, vma->vm_page_prot)));page_add_anon_rmap (page, vma, addr); +memctlr_update_rss (vma->vm_mm,1 ,MEMCTLR_DONT_CHECK_LIMIT );swap_free (entry);/* * Move the page to the active list so it is not
_
Signature · 2 lines
Warm Regards, Balbir Singh
Andrew Morton
maintainerRe: [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 14 lines · On Mon, 19 Feb 2007 12:20:34 +0530 Balbir Singh <[email protected]> wrote:
This patch adds the basic accounting hooks to account for pages allocated into the RSS of a process. Accounting is maintained at two levels, in the
mm_structof each task and in the memory controller data structure associated with each node in the container.When the limit specified for the container is exceeded, the task is killed. RSS accounting is consistent with the current definition of RSS in the kernel. Shared pages are accounted into the RSS of each process as is done in the kernel currently. The code is flexible in that it can be easily modified to work with any definition of RSS.
..
include/linux/memctlr.h line 42
Quoted 4 lines
include/linux/memctlr.h renamed from include/linux/memctlr.h~memctlr-acct +4
-0+static inline int memctlr_mm_init (struct mm_struct *mm) +{ +return 0 ; +}
So it returns zero on success. OK.
Quoted 11 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +1
-0@@ -50,6 +50,7 @@#include <linux/taskstats_kern.h> #include <linux/random.h> #include <linux/numtasks.h> +#include <linux/memctlr.h> #include <asm/pgtable.h> #include <asm/pgalloc.h>
kernel/fork.c static struct mm_struct * mm_init(struct · line 348
Quoted 6 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +3
-0@@ -342,10 +343,15 @@ static struct mm_struct * mm_init(struct mm->free_area_cache =TASK_UNMAPPED_BASE ; mm->cached_hole_size = ~0UL ; +if (!memctlr_mm_init (mm)) + goto err; +
But here we treat zero as an error?
Quoted 12 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +2
-0if (likely (!mm_alloc_pgd (mm))) { mm->def_flags =0 ;return mm; } + +err:free_mm (mm);return NULL ; }
kernel/fork.c static struct mm_struct * mm_init(struct
Quoted 10 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +10
-0+int memctlr_mm_init (struct mm_struct *mm) +{ + mm->counter =kmalloc (sizeof (struct res_counter ),GFP_KERNEL ); +if (!mm->counter) +return 0 ; +atomic_long_set (&mm->counter->usage,0 ); +atomic_long_set (&mm->counter->limit,0 ); +rwlock_init (&mm->container_lock); +return 1 ; +}
ah-ha, we have another Documentation/SubmitChecklist customer.
It would be more conventional to make this return -EFOO on error, zero on success.
kernel/fork.c
Quoted 12 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +12
-0+void memctlr_mm_free (struct mm_struct *mm) +{ +kfree (mm->counter); +} + +static inline void memctlr_mm_assign_container_direct (struct mm_struct *mm, +struct container *cont) +{ +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}
More weird locking here.
kernel/fork.c
Quoted 10 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +10
-0+void memctlr_mm_assign_container (struct mm_struct *mm,struct task_struct *p) +{ +struct container *cont =task_container (p, &memctlr_subsys); +struct memctlr *mem =memctlr_from_cont (cont); + +BUG_ON (!mem); +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}
And here.
Quoted 7 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +7
-0+/* +* Update the rss usage counters for the mm_struct and the container it belongs +* to. We do not fail rss for pages shared during fork (see copy_one_pte()). +*/ +int memctlr_update_rss (struct mm_struct *mm,int count,bool check) +{ +int ret =1 ;
kernel/fork.c
Quoted 10 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +10
-0+struct container *cont; +long usage, limit; +struct memctlr *mem; + +read_lock (&mm->container_lock); + cont = mm->container; +read_unlock (&mm->container_lock); + +if (!cont) + goto done;
And here. I mean, if there was a reason for taking the lock around that read, then testing `cont' outside the lock just invalidated that reason.
kernel/fork.c
Quoted 11 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +11
-0+static inline void memctlr_double_lock (struct memctlr *mem1, +struct memctlr *mem2) +{ +if (mem1 > mem2) { +spin_lock (&mem1->lock); +spin_lock (&mem2->lock); + }else { +spin_lock (&mem2->lock); +spin_lock (&mem1->lock); + } +}
Conventionally we take the lower-addressed lock first when doing this, not the higher-addressed one.
Quoted 9 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +9
-0+static inline void memctlr_double_unlock (struct memctlr *mem1, +struct memctlr *mem2) +{ +if (mem1 > mem2) { +spin_unlock (&mem2->lock); +spin_unlock (&mem1->lock); + }else { +spin_unlock (&mem1->lock); +spin_unlock (&mem2->lock);
kernel/fork.c
Quoted 10 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +7
-0+ } +} + retval = -ENOMEM ; + +if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto out; +
Again, please use zero for success and -EFOO for error.
That way, you don't have to assume that the reason memctlr_update_rss() failed was out-of-memory. Just propagate the error back.
Quoted 14 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +3
-0flush_dcache_page (page); pte =get_locked_pte (mm, addr, &ptl);if (!pte)@@ -1580,6 +1587,9 @@ gotten:cow_user_page (new_page, old_page, address, vma); } +if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto oom; +/* * Re-check the pte - we dropped the lock */
kernel/fork.c gotten: · line 1626
Quoted 6 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +2
-1@@ -1612,7 +1622,9 @@ gotten:/* Free the old page.. */ new_page = old_page; ret |=VM_FAULT_WRITE ; - } + }else +memctlr_update_rss (mm,-1 ,MEMCTLR_DONT_CHECK_LIMIT );
This one doesn't get checked?
Why does MEMCTLR_DONT_CHECK_LIMIT exist?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 13 lines · Andrew Morton
Quoted 11 lines · On Mon, 19 Feb 2007 12:20:34 +0530 Balbir Singh <[email protected]> wrote:
This patch adds the basic accounting hooks to account for pages allocated into the RSS of a process. Accounting is maintained at two levels, in the
mm_structof each task and in the memory controller data structure associated with each node in the container.When the limit specified for the container is exceeded, the task is killed. RSS accounting is consistent with the current definition of RSS in the kernel. Shared pages are accounted into the RSS of each process as is done in the kernel currently. The code is flexible in that it can be easily modified to work with any definition of RSS.
Quoted 9 lines
include/linux/memctlr.h line 42
Quoted 6 lines
..
include/linux/memctlr.h renamed from include/linux/memctlr.h~memctlr-acct +4
-0+static inline int memctlr_mm_init (struct mm_struct *mm) +{ +return 0 ; +}So it returns zero on success. OK.
Oops. it should return 1 on success.
Quoted 14 lines
Quoted 14 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +1
-0@@ -50,6 +50,7 @@#include <linux/taskstats_kern.h> #include <linux/random.h> #include <linux/numtasks.h> +#include <linux/memctlr.h> #include <asm/pgtable.h> #include <asm/pgalloc.h> @@ -342,10 +343,15 @@ static struct mm_struct * mm_init(struct mm->free_area_cache =TASK_UNMAPPED_BASE ; mm->cached_hole_size = ~0UL ;
Quoted 6 lines
kernel/fork.c static struct mm_struct * mm_init(struct · line 348
Quoted 3 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +3
-0+if (!memctlr_mm_init (mm)) + goto err; +But here we treat zero as an error?
It's a BUG, I'll fix it.
Quoted 25 lines
Quoted 12 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +2
-0if (likely (!mm_alloc_pgd (mm))) { mm->def_flags =0 ;return mm; } + +err:free_mm (mm);return NULL ; }kernel/fork.c static struct mm_struct * mm_init(struct
Quoted 10 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +10
-0+int memctlr_mm_init (struct mm_struct *mm) +{ + mm->counter =kmalloc (sizeof (struct res_counter ),GFP_KERNEL ); +if (!mm->counter) +return 0 ; +atomic_long_set (&mm->counter->usage,0 ); +atomic_long_set (&mm->counter->limit,0 ); +rwlock_init (&mm->container_lock); +return 1 ; +}ah-ha, we have another
Documentation/SubmitChecklistcustomer.
Quoted 3 lines
It would be more conventional to make this return
-EFOOon error, zero on success.
ok.. I'll convert the functions to be consistent with the "return 0 on success" philosophy.
Quoted 6 lines
Quoted 6 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +6
-0+void memctlr_mm_free (struct mm_struct *mm) +{ +kfree (mm->counter); +} + +static inline void memctlr_mm_assign_container_direct(struct mm_struct *mm,
Quoted 9 lines
kernel/fork.c
Quoted 6 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +6
-0+ struct container *cont) +{ +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}More weird locking here.
The container field of the mm_struct is protected by a read write spin lock.
Quoted 4 lines
Quoted 4 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +4
-0+void memctlr_mm_assign_container (struct mm_struct *mm,struct task_struct *p) +{ +struct container *cont =task_container (p, &memctlr_subsys); +struct memctlr *mem =memctlr_from_cont (cont);
Quoted 8 lines
kernel/fork.c
Quoted 6 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +6
-0+ +BUG_ON (!mem); +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}And here.
Ditto.
Quoted 13 lines
Quoted 13 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +12
-0+/* +* Update the rss usage counters for the mm_struct and the container it belongs +* to. We do not fail rss for pages shared during fork (see copy_one_pte()). +*/ +int memctlr_update_rss (struct mm_struct *mm,int count,bool check) +{ +int ret =1 ; +struct container *cont; +long usage, limit; +struct memctlr *mem; + +read_lock (&mm->container_lock);
Quoted 9 lines
kernel/fork.c
Quoted 5 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +5
-0+ cont = mm->container; +read_unlock (&mm->container_lock); + +if (!cont) + goto done;And here. I mean, if there was a reason for taking the lock around that read, then testing `cont' outside the lock just invalidated that reason.
We took a consistent snapshot of cont. It cannot change outside the lock, we check the value outside. I am sure I missed something.
Quoted 6 lines
Quoted 6 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +6
-0+static inline void memctlr_double_lock (struct memctlr *mem1, +struct memctlr *mem2) +{ +if (mem1 > mem2) { +spin_lock (&mem1->lock); +spin_lock (&mem2->lock);
Quoted 9 lines
kernel/fork.c
Quoted 5 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +5
-0+ }else { +spin_lock (&mem2->lock); +spin_lock (&mem1->lock); + } +}Conventionally we take the lower-addressed lock first when doing this, not the higher-addressed one.
Will fix this.
Quoted 22 lines
Quoted 9 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +9
-0+static inline void memctlr_double_unlock (struct memctlr *mem1, +struct memctlr *mem2) +{ +if (mem1 > mem2) { +spin_unlock (&mem2->lock); +spin_unlock (&mem1->lock); + }else { +spin_unlock (&mem1->lock); +spin_unlock (&mem2->lock);kernel/fork.c
Quoted 10 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +7
-0+ } +} + retval = -ENOMEM ; + +if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto out; +Again, please use zero for success and
-EFOOfor error.
Quoted 3 lines
That way, you don't have to assume that the reason
memctlr_update_rss()failed was out-of-memory. Just propagate the error back.
Yes, will do.
Quoted 7 lines
Quoted 7 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +0
-0flush_dcache_page (page); pte =get_locked_pte (mm, addr, &ptl);if (!pte)@@ -1580,6 +1587,9 @@ gotten:cow_user_page (new_page, old_page, address, vma); }
Quoted 16 lines
kernel/fork.c gotten: · line 1626
Quoted 13 lines
kernel/fork.c renamed from kernel/fork.c~memctlr-acct +5
-1+if (!memctlr_update_rss (mm,1 ,MEMCTLR_CHECK_LIMIT )) + goto oom; +/* * Re-check the pte - we dropped the lock */ @@ -1612,7 +1622,9 @@ gotten:/* Free the old page.. */ new_page = old_page; ret |=VM_FAULT_WRITE ; - } + }else +memctlr_update_rss (mm,-1 ,MEMCTLR_DONT_CHECK_LIMIT );This one doesn't get checked?
Yes, because we are adding a -1, reducing the rss on failure.
Quoted 2 lines
Why does
MEMCTLR_DONT_CHECK_LIMITexist?
MEMCTLR_DONT_CHECK_LIMIT exists for the following reasons
1. Pages are shared during fork, fork() is not failed at that point since the pages are shared anyway, we allow the RSS limit to be exceeded.
2. When ZERO_PAGE is added, we don't check for limits (zeromap_pte_range).
3. On reducing RSS (passing -1 as the value)
Signature · 2 lines
Warm Regards, Balbir Singh
Andrew Morton
maintainerRe: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 16 lines · On Mon, 19 Feb 2007 16:07:44 +0530 Balbir Singh <[email protected]> wrote:
Quoted 6 lines
Quoted 6 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +6
-0+void memctlr_mm_free (struct mm_struct *mm) +{ +kfree (mm->counter); +} + +static inline void memctlr_mm_assign_container_direct(struct mm_struct *mm,Quoted 9 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 67
Quoted 6 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +6
-0+ struct container *cont) +{ +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}More weird locking here.
Quoted 1 line
The container field of the
mm_structis protected by a read write spin lock.
That doesn't mean anything to me.
What would go wrong if the above locking was simply removed? And how does the locking prevent that fault?
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +4
-0+void memctlr_mm_assign_container (struct mm_struct *mm,struct task_struct *p) +{ +struct container *cont =task_container (p, &memctlr_subsys); +struct memctlr *mem =memctlr_from_cont (cont);Quoted 8 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 78
Quoted 6 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +6
-0+ +BUG_ON (!mem); +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}And here.
Quoted 1 line
Ditto.
ditto ;)
Quoted 23 lines
Quoted 13 lines
Quoted 13 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +12
-0+/* +* Update the rss usage counters for the mm_struct and the container it belongs +* to. We do not fail rss for pages shared during fork (see copy_one_pte()). +*/ +int memctlr_update_rss (struct mm_struct *mm,int count,bool check) +{ +int ret =1 ; +struct container *cont; +long usage, limit; +struct memctlr *mem; + +read_lock (&mm->container_lock);Quoted 9 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 96
Quoted 5 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +5
-0+ cont = mm->container; +read_unlock (&mm->container_lock); + +if (!cont) + goto done;And here. I mean, if there was a reason for taking the lock around that read, then testing `cont' outside the lock just invalidated that reason.
Quoted 2 lines
We took a consistent snapshot of cont. It cannot change outside the lock, we check the value outside. I am sure I missed something.
If it cannot change outside the lock then we don't need to take the lock!
Quoted 8 lines
MEMCTLR_DONT_CHECK_LIMITexists for the following reasons1. Pages are shared during fork,
fork()is not failed at that point since the pages are shared anyway, we allow the RSS limit to be exceeded.
2. WhenZERO_PAGEis added, we don't check for limits (zeromap_pte_range).
3. On reducing RSS (passing -1 as the value)
OK, that might make a nice comment somewhere (if it's not already there).
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 16 lines · Andrew Morton
Quoted 14 lines · On Mon, 19 Feb 2007 16:07:44 +0530 Balbir Singh <[email protected]> wrote:
Quoted 14 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 67
Quoted 12 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +12
-0+void memctlr_mm_free (struct mm_struct *mm) +{ +kfree (mm->counter); +} + +static inline void memctlr_mm_assign_container_direct (struct mm_struct *mm, +struct container *cont) +{ +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}More weird locking here.
Quoted 7 lines
Quoted 1 line
The container field of the
mm_structis protected by a read write spin lock.That doesn't mean anything to me.
What would go wrong if the above locking was simply removed? And how does the locking prevent that fault?
Some pages could charged to the wrong container. Apart from that I do not see anything going bad (I'll double check that).
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 78
Quoted 11 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +10
-0+void memctlr_mm_assign_container (struct mm_struct *mm,struct task_struct *p) +{ +struct container *cont =task_container (p, &memctlr_subsys); +struct memctlr *mem =memctlr_from_cont (cont); + +BUG_ON (!mem); +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}And here.
Quoted 4 lines
Quoted 1 line
Ditto.
ditto ;)
:-)
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 7 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +7
-0+/* +* Update the rss usage counters for the mm_struct and the container it belongs +* to. We do not fail rss for pages shared during fork (see copy_one_pte()). +*/ +int memctlr_update_rss (struct mm_struct *mm,int count,bool check) +{ +int ret =1 ;mm/memctlr.c static inline struct memctlr *memctlr_fr · line 96
Quoted 10 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +10
-0+struct container *cont; +long usage, limit; +struct memctlr *mem; + +read_lock (&mm->container_lock); + cont = mm->container; +read_unlock (&mm->container_lock); + +if (!cont) + goto done;And here. I mean, if there was a reason for taking the lock around that read, then testing `cont' outside the lock just invalidated that reason.
Quoted 5 lines
Quoted 2 lines
We took a consistent snapshot of cont. It cannot change outside the lock, we check the value outside. I am sure I missed something.
If it cannot change outside the lock then we don't need to take the lock!
We took a snapshot that we thought was consistent. We check for the value outside. I guess there is no harm, the worst thing that could happen is wrong accounting during mm->container changes (when a task changes container).
Quoted 9 lines
Quoted 7 lines
MEMCTLR_DONT_CHECK_LIMITexists for the following reasons1. Pages are shared during fork,
fork()is not failed at that point since the pages are shared anyway, we allow the RSS limit to be exceeded.
2. WhenZERO_PAGEis added, we don't check for limits (zeromap_pte_range).
3. On reducing RSS (passing -1 as the value)OK, that might make a nice comment somewhere (if it's not already there).
Yes, thanks for keeping us humble and honest, I'll add it.
Signature · 2 lines
Warm Regards, Balbir Singh
Andrew Morton
maintainerRe: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 19 lines · On Mon, 19 Feb 2007 16:39:33 +0530 Balbir Singh <[email protected]> wrote:
Quoted 18 lines · Andrew Morton
Quoted 15 lines · On Mon, 19 Feb 2007 16:07:44 +0530 Balbir Singh <[email protected]> wrote:
Quoted 5 lines
Quoted 5 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +5
-0+void memctlr_mm_free (struct mm_struct *mm) +{ +kfree (mm->counter); +} +Quoted 9 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 67
Quoted 7 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +7
-0+static inline void memctlr_mm_assign_container_direct (struct mm_struct *mm, +struct container *cont) +{ +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}More weird locking here.
The container field of the
mm_structis protected by a read write spin lock.
Quoted 8 lines
Quoted 5 lines
That doesn't mean anything to me.
What would go wrong if the above locking was simply removed? And how does the locking prevent that fault?
Some pages could charged to the wrong container. Apart from that I do not see anything going bad (I'll double check that).
Argh. Please, think about this.
That locking *doesn't do anything*. Except for that one situation I
described: some other holder of the lock reads mm->container twice inside the lock and requires that the value be the same both times (and that sort of code should be converted to take a local copy, so this locking here can be removed).
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 96
Quoted 7 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +7
-0+ +read_lock (&mm->container_lock); + cont = mm->container; +read_unlock (&mm->container_lock); + +if (!cont) + goto done;And here. I mean, if there was a reason for taking the lock around that read, then testing `cont' outside the lock just invalidated that reason.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
We took a consistent snapshot of cont. It cannot change outside the lock, we check the value outside. I am sure I missed something.
If it cannot change outside the lock then we don't need to take the lock!
We took a snapshot that we thought was consistent.
Consistent with what? That's a single-word read inside that lock.
Quoted 4 lines
We check for the value
outside. I guess there is no harm, the worst thing that could happen is wrong accounting duringmm->containerchanges (when a task changes container).
If container->lock is held when a task is removed from the container then yes, `cont' here can refer to a container to which the task no longer belongs.
More worrisome is the potential for use-after-free. What prevents the pointer at mm->container from referring to freed memory after we're dropped the lock?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 22 lines · Andrew Morton
Quoted 20 lines · On Mon, 19 Feb 2007 16:39:33 +0530 Balbir Singh <[email protected]> wrote:
Quoted 19 lines · Andrew Morton
Quoted 14 lines · On Mon, 19 Feb 2007 16:07:44 +0530 Balbir Singh <[email protected]> wrote:
Quoted 5 lines
Quoted 5 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +5
-0+void memctlr_mm_free (struct mm_struct *mm) +{ +kfree (mm->counter); +} +Quoted 9 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 67
Quoted 7 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +7
-0+static inline void memctlr_mm_assign_container_direct (struct mm_struct *mm, +struct container *cont) +{ +write_lock (&mm->container_lock); + mm->container = cont; +write_unlock (&mm->container_lock); +}More weird locking here.
Quoted 1 line
The container field of the
mm_structis protected by a read write spin lock.That doesn't mean anything to me.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
What would go wrong if the above locking was simply removed? And how does the locking prevent that fault?
Some pages could charged to the wrong container. Apart from that I do not see anything going bad (I'll double check that).
Argh. Please, think about this.
Sure, I will. I guess I am short circuiting my thinking process :-)
Quoted 6 lines
That locking *doesn't do anything*. Except for that one situation I
described: some other holder of the lock readsmm->containertwice inside the lock and requires that the value be the same both times (and that sort of code should be converted to take a local copy, so this locking here can be removed).
Yes, that makes sense.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 96
Quoted 7 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-acct +7
-0+ +read_lock (&mm->container_lock); + cont = mm->container; +read_unlock (&mm->container_lock); + +if (!cont) + goto done;And here. I mean, if there was a reason for taking the lock around that read, then testing `cont' outside the lock just invalidated that reason.
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
We took a consistent snapshot of cont. It cannot change outside the lock, we check the value outside. I am sure I missed something.
If it cannot change outside the lock then we don't need to take the lock!
We took a snapshot that we thought was consistent.
Consistent with what? That's a single-word read inside that lock.
Yes, that makes sense.
Quoted 5 lines
Quoted 4 lines
We check for the value
outside. I guess there is no harm, the worst thing that could happen is wrong accounting duringmm->containerchanges (when a task changes container).
Quoted 8 lines
If
container->lockis held when a task is removed from the container then yes, `cont' here can refer to a container to which the task no longer belongs.More worrisome is the potential for use-after-free. What prevents the pointer at
mm->containerfrom referring to freed memory after we're dropped the lock?
The container cannot be freed unless all tasks holding references to it are gone, that would ensure that all mm->containers are pointing elsewhere and never to a stale value.
I hope my short-circuited brain got this right :-)
Signature · 2 lines
Warm Regards, Balbir Singh
Re: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 8 lines · Balbir Singh
Quoted 5 lines
More worrisome is the potential for use-after-free. What prevents the pointer at
mm->containerfrom referring to freed memory after we're dropped the lock?The container cannot be freed unless all tasks holding references to it are gone,
... or have been moved to other containers. If you're not holding task->alloc_lock or one of the container mutexes, there's nothing to stop the task being moved to another container, and the container being deleted.
If you're in an RCU section then you can guarantee that the container (that you originally read from the task) and its subsystems at least won't be deleted while you're accessing them, but for accounting like this I suspect that's not enough, since you need to be adding to the accounting stats on the correct container. I think you'll need to hold mm->container_lock for the duration of memctl_update_rss()
Paul
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 13 lines · Paul Menage
Quoted 6 lines · Balbir Singh
Quoted 4 lines
More worrisome is the potential for use-after-free. What prevents the pointer at
mm->containerfrom referring to freed memory after we're dropped the lock?The container cannot be freed unless all tasks holding references to it are gone,
... or have been moved to other containers. If you're not holding
task->alloc_lockor one of the container mutexes, there's nothing to stop the task being moved to another container, and the container being deleted.
Quoted 9 lines
If you're in an RCU section then you can guarantee that the container (that you originally read from the task) and its subsystems at least won't be deleted while you're accessing them, but for accounting like this I suspect that's not enough, since you need to be adding to the accounting stats on the correct container. I think you'll need to hold
mm->container_lockfor the duration ofmemctl_update_rss()Paul
Yes, that sounds like the correct thing to do.
Signature · 2 lines
Warm Regards, Balbir Singh
Re: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 20 lines · Balbir Singh
Quoted 19 lines · Paul Menage
Quoted 6 lines · Balbir Singh
Quoted 4 lines
More worrisome is the potential for use-after-free. What prevents the pointer at
mm->containerfrom referring to freed memory after we're dropped the lock?The container cannot be freed unless all tasks holding references to it are gone,
... or have been moved to other containers. If you're not holding
task->alloc_lockor one of the container mutexes, there's nothing to stop the task being moved to another container, and the container being deleted.If you're in an RCU section then you can guarantee that the container (that you originally read from the task) and its subsystems at least won't be deleted while you're accessing them, but for accounting like this I suspect that's not enough, since you need to be adding to the accounting stats on the correct container. I think you'll need to hold
mm->container_lockfor the duration ofmemctl_update_rss()
Quoted 5 lines
Quoted 2 lines
Paul
Yes, that sounds like the correct thing to do.
Accounting accuracy will anyway be affected when a process is migrated while it is still allocating pages. Having a lock here does not necessarily improve the accounting accuracy. Charges from the old container would have to be moved to the new container before deletion which implies all tasks have already left the container and no mm_struct is holding a pointer to it.
The only condition that will break our code will be if the container pointer becomes invalid while we are updating stats. This can be prevented by RCU section as mentioned by Paul. I believe explicit lock and unlock may not provide additional benefit here.
--Vaidy
Re: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 34 lines · Vaidyanathan Srinivasan
Quoted 20 lines · Balbir Singh
Quoted 19 lines · Paul Menage
Quoted 6 lines · Balbir Singh
Quoted 4 lines
More worrisome is the potential for use-after-free. What prevents the pointer at
mm->containerfrom referring to freed memory after we're dropped the lock?The container cannot be freed unless all tasks holding references to it are gone,
... or have been moved to other containers. If you're not holding
task->alloc_lockor one of the container mutexes, there's nothing to stop the task being moved to another container, and the container being deleted.If you're in an RCU section then you can guarantee that the container (that you originally read from the task) and its subsystems at least won't be deleted while you're accessing them, but for accounting like this I suspect that's not enough, since you need to be adding to the accounting stats on the correct container. I think you'll need to hold
mm->container_lockfor the duration ofmemctl_update_rss()Quoted 4 lines
Quoted 2 lines
Paul
Yes, that sounds like the correct thing to do.
Accounting accuracy will anyway be affected when a process is migrated while it is still allocating pages. Having a lock here does not necessarily improve the accounting accuracy. Charges from the old container would have to be moved to the new container before deletion which implies all tasks have already left the container and no
mm_structis holding a pointer to it.
Quoted 5 lines
The only condition that will break our code will be if the container pointer becomes invalid while we are updating stats. This can be prevented by RCU section as mentioned by Paul. I believe explicit lock and unlock may not provide additional benefit here.
Yes, if the container pointer becomes invalid, then consider the following scenario
1. Use RCU, get a reference to the container
2. All tasks/mm's move to newer container (and the accounting information moves)
3. Container is RCU deleted
4. We still charge the older container that is going to be deleted soon
5. Release RCU
6. RCU garbage collects (callback runs)
We end up charging/uncharging a soon to be deleted container, that is not good.
What did I miss?
Quoted 2 lines
--Vaidy
Signature · 2 lines
Warm Regards, Balbir Singh
Re: [ckrm-tech] [RFC][PATCH][2/4] Add RSS accounting and control
Quoted 50 lines · Balbir Singh
Quoted 32 lines · Vaidyanathan Srinivasan
Quoted 20 lines · Balbir Singh
Quoted 19 lines · Paul Menage
Quoted 6 lines · Balbir Singh
Quoted 4 lines
More worrisome is the potential for use-after-free. What prevents the pointer at
mm->containerfrom referring to freed memory after we're dropped the lock?The container cannot be freed unless all tasks holding references to it are gone,
... or have been moved to other containers. If you're not holding
task->alloc_lockor one of the container mutexes, there's nothing to stop the task being moved to another container, and the container being deleted.If you're in an RCU section then you can guarantee that the container (that you originally read from the task) and its subsystems at least won't be deleted while you're accessing them, but for accounting like this I suspect that's not enough, since you need to be adding to the accounting stats on the correct container. I think you'll need to hold
mm->container_lockfor the duration ofmemctl_update_rss()Quoted 4 lines
Quoted 2 lines
Paul
Yes, that sounds like the correct thing to do.
Accounting accuracy will anyway be affected when a process is migrated while it is still allocating pages. Having a lock here does not necessarily improve the accounting accuracy. Charges from the old container would have to be moved to the new container before deletion which implies all tasks have already left the container and no
mm_structis holding a pointer to it.Quoted 5 lines
The only condition that will break our code will be if the container pointer becomes invalid while we are updating stats. This can be prevented by RCU section as mentioned by Paul. I believe explicit lock and unlock may not provide additional benefit here.
Yes, if the container pointer becomes invalid, then consider the following scenario
1. Use RCU, get a reference to the container
2. All tasks/mm's move to newer container (and the accounting information moves)
3. Container is RCU deleted
4. We still charge the older container that is going to be deleted soon
5. Release RCU
6. RCU garbage collects (callback runs)
Quoted 4 lines
We end up charging/uncharging a soon to be deleted container, that is not good.
What did I miss?
You are right. We should go with your read/write lock method. Later we can evaluate if using an RCU and then fixing the wrong charge will work better or worse.
--Vaidy
RFC PATCH 3/4 Add reclaim support
In reply to Balbir SinghThis patch reclaims pages from a container when the container limit is hit. The executable is oom'ed only when the container it is running in, is overlimit and we could not reclaim any pages belonging to the container
A parameter called pushback, controls how much memory is reclaimed when the limit is hit. It should be easy to expose this knob to user space, but currently it is hard coded to 20% of the total limit of the container.
isolate_lru_pages() has been modified to isolate pages belonging to a particular container, so that reclaim code will reclaim only container pages. For shared pages, reclaim does not unmap all mappings of the page, it only unmaps those mappings that are over their limit. This ensures that other containers are not penalized while reclaiming shared pages.
Parallel reclaim per container is not allowed. Each controller has a wait queue that ensures that only one task per control is running reclaim on that container.
---
include/linux/memctlr.h | 8 ++ include/linux/rmap.h | 13 +++- include/linux/swap.h | 4 + mm/memctlr.c | 137 ++++++++++++++++++++++++++++++++++++++++++++---- mm/migrate.c | 2 mm/rmap.c | 96 +++++++++++++++++++++++++++++++-- mm/vmscan.c | 94 ++++++++++++++++++++++++++++---- 7 files changed, 324 insertions(+), 30 deletions(-)
include/linux/memctlr.h renamed from include/linux/memctlr.h~memctlr-reclaim-on-limit +8 -0
@@ -20,6 +20,7 @@ enum { }; #ifdef CONFIG_CONTAINER_MEMCTLR +#include <linux/wait.h> struct res_counter { atomic_long_t usage; /* The current usage of the resource being */
@@ -33,6 +34,9 @@ extern void memctlr_mm_free(struct mm_stextern void memctlr_mm_assign_container (struct mm_struct *mm,struct task_struct *p);extern int memctlr_update_rss (struct mm_struct *mm,int count,bool check); +extern int memctlr_mm_overlimit (struct mm_struct *mm,void *sc_cont); +extern wait_queue_head_t memctlr_reclaim_wq; +extern bool memctlr_reclaim_in_progress;#else /* CONFIG_CONTAINER_MEMCTLR */
@@ -56,5 +60,9 @@ static inline int memctlr_update_rss(strreturn 0 ; } +int memctlr_mm_overlimit (struct mm_struct *mm,void *sc_cont) +{ +return 0 ; +}#endif /* CONFIG_CONTAINER_MEMCTLR */ #endif /* _LINUX_MEMCTLR_H */
include/linux/rmap.h renamed from include/linux/rmap.h~memctlr-reclaim-on-limit +11 -2
@@ -90,7 +90,15 @@ static inline void page_dup_rmap(struct* Called from mm/vmscan.c to handle paging out */ int page_referenced (struct page *,int is_locked); -int try_to_unmap (struct page *,int ignore_refs); +int try_to_unmap (struct page *,int ignore_refs,void *container); +#ifdef CONFIG_CONTAINER_MEMCTLR +bool page_in_container (struct page *page,struct zone *zone,void *container); +#else +static inline bool page_in_container (struct page *page,struct zone *zone,void *container) +{ +return true; +} +#endif /* CONFIG_CONTAINER_MEMCTLR */ /* * Called from mm/filemap_xip.c to unmap empty zero page
@@ -118,7 +126,8 @@ int page_mkclean(struct page *);#define anon_vma_link (vma) do {} while (0)#define page_referenced (page,l) TestClearPageReferenced(page) -#define try_to_unmap (page, refs) SWAP_FAIL +#define try_to_unmap (page, refs, container) SWAP_FAIL +#define page_in_container (page, zone, container) truestatic inline int page_mkclean (struct page *page) {
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +4 -0
@@ -188,6 +188,10 @@ extern void swap_setup(void);/* linux/mm/vmscan.c */ extern unsigned long try_to_free_pages (struct zone **,gfp_t );extern unsigned long shrink_all_memory (unsigned long nr_pages); +#ifdef CONFIG_CONTAINER_MEMCTLR +extern unsigned long memctlr_shrink_mapped_memory (unsigned long nr_pages, +void *container); +#endif extern int vm_swappiness;extern int remove_mapping (struct address_space *mapping,struct page *page);extern long vm_total_pages;
mm/memctlr.c renamed from mm/memctlr.c~memctlr-reclaim-on-limit +127 -10
208 lines not on this page · raw message
mm/migrate.c renamed from mm/migrate.c~memctlr-reclaim-on-limit +1 -1
@@ -623,7 +623,7 @@ static int unmap_and_move(new_page_t get/* * Establish migration ptes or remove ptes */ -try_to_unmap (page,1 ); +try_to_unmap (page,1 ,NULL );if (!page_mapped (page)) rc =move_to_new_page (newpage, page);
mm/rmap.c renamed from mm/rmap.c~memctlr-reclaim-on-limit +91 -5
142 lines not on this page · raw message
mm/vmscan.c renamed from mm/vmscan.c~memctlr-reclaim-on-limit +82 -12
189 lines not on this page · raw message
_
Signature · 2 lines
Warm Regards, Balbir Singh
Andrew Morton
maintainerRe: [RFC][PATCH][3/4] Add reclaim support
Quoted 42 lines · On Mon, 19 Feb 2007 12:20:42 +0530 Balbir Singh <[email protected]> wrote:
This patch reclaims pages from a container when the container limit is hit. The executable is oom'ed only when the container it is running in, is overlimit and we could not reclaim any pages belonging to the container
A parameter called pushback, controls how much memory is reclaimed when the limit is hit. It should be easy to expose this knob to user space, but currently it is hard coded to 20% of the total limit of the container.
isolate_lru_pages()has been modified to isolate pages belonging to a particular container, so that reclaim code will reclaim only container pages. For shared pages, reclaim does not unmap all mappings of the page, it only unmaps those mappings that are over their limit. This ensures that other containers are not penalized while reclaiming shared pages.Parallel reclaim per container is not allowed. Each controller has a wait queue that ensures that only one task per control is running reclaim on that container.
...
include/linux/rmap.h renamed from include/linux/rmap.h~memctlr-reclaim-on-limit +9
-1@@ -90,7 +90,15 @@ static inline void page_dup_rmap(struct* Called from mm/vmscan.c to handle paging out */ int page_referenced (struct page *,int is_locked); -int try_to_unmap (struct page *,int ignore_refs); +int try_to_unmap (struct page *,int ignore_refs,void *container); +#ifdef CONFIG_CONTAINER_MEMCTLR +bool page_in_container (struct page *page,struct zone *zone,void *container); +#else +static inline bool page_in_container (struct page *page,struct zone *zone,void *container) +{ +return true; +} +#endif /* CONFIG_CONTAINER_MEMCTLR */ /* * Called from mm/filemap_xip.c to unmap empty zero page
include/linux/rmap.h int page_mkclean(struct page *); · line 130
Quoted 6 lines
include/linux/rmap.h renamed from include/linux/rmap.h~memctlr-reclaim-on-limit +2
-1@@ -118,7 +126,8 @@ int page_mkclean(struct page *);#define anon_vma_link (vma) do {} while (0)#define page_referenced (page,l) TestClearPageReferenced(page) -#define try_to_unmap (page, refs) SWAP_FAIL +#define try_to_unmap (page, refs, container) SWAP_FAIL +#define page_in_container (page, zone, container) true
I spy a compile error.
The static-inline version looks nicer.
include/linux/swap.h extern void swap_setup(void); · line 194
Quoted 13 lines
include/linux/rmap.h renamed from include/linux/rmap.h~memctlr-reclaim-on-limit +0
-0static inline int page_mkclean (struct page *page) {diff -puN
include/linux/swap.h~memctlr-reclaim-on-limitinclude/linux/swap.hinclude/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +4
-0@@ -188,6 +188,10 @@ extern void swap_setup(void);/* linux/mm/vmscan.c */ extern unsigned long try_to_free_pages (struct zone **,gfp_t );extern unsigned long shrink_all_memory (unsigned long nr_pages); +#ifdef CONFIG_CONTAINER_MEMCTLR +extern unsigned long memctlr_shrink_mapped_memory (unsigned long nr_pages, +void *container); +#endif
Usually one doesn't need to put ifdefs around the declaration like this. If the function doesn't exist and nobody calls it, we're fine. If someone _does_ call it, we'll find out the error at link-time.
Quoted 28 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +27
-0+/* +* checks if the mm's container and scan control passed container match, if +* so, is the container over it's limit. Returns 1 if the container is above +* its limit. +*/ +int memctlr_mm_overlimit (struct mm_struct *mm,void *sc_cont) +{ +struct container *cont; +struct memctlr *mem; +long usage, limit; +int ret =1 ; + +if (!sc_cont) + goto out; + +read_lock (&mm->container_lock); + cont = mm->container; + +/* +* Regular reclaim, let it proceed as usual +*/ +if (!sc_cont) + goto out; + + ret =0 ; +if (cont != sc_cont) + goto out;
include/linux/swap.h extern void swap_setup(void);
Quoted 10 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +10
-0+ + mem =memctlr_from_cont (cont); + usage =atomic_long_read (&mem->counter.usage); + limit =atomic_long_read (&mem->counter.limit); +if (limit && (usage > limit)) + ret =1 ; +out: +read_unlock (&mm->container_lock); +return ret; +}
hm, I wonder how much additional lock traffic all this adds.
Quoted 36 lines
int
memctlr_mm_init(struct mm_struct *mm)
{include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +29
-0mm->counter =kmalloc (sizeof (struct res_counter ),GFP_KERNEL );@@ -77,6 +125,46 @@ void memctlr_mm_assign_container(structwrite_unlock (&mm->container_lock); } +static int memctlr_check_and_reclaim (struct container *cont,long usage, +long limit) +{ +unsigned long nr_pages =0 ; +unsigned long nr_reclaimed =0 ; +int retries = nr_retries; +int ret =1 ; +struct memctlr *mem; + + mem =memctlr_from_cont (cont); +spin_lock (&mem->lock); +while ((retries-- >0 ) && limit && (usage > limit)) { +if (mem->reclaim_in_progress) { +spin_unlock (&mem->lock); +wait_event (mem->wq, !mem->reclaim_in_progress); +spin_lock (&mem->lock); + }else { +if (!nr_pages) + nr_pages = (pushback * limit) /100 ; + mem->reclaim_in_progress = true; +spin_unlock (&mem->lock); + nr_reclaimed +=memctlr_shrink_mapped_memory (nr_pages, + cont); +spin_lock (&mem->lock); + mem->reclaim_in_progress = false; +wake_up_all (&mem->wq); + } +/* +* Resample usage and limit after reclaim
include/linux/swap.h void memctlr_mm_assign_container(struct · line 166
Quoted 10 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +10
-0+*/ + usage =atomic_long_read (&mem->counter.usage); + limit =atomic_long_read (&mem->counter.limit); + } +spin_unlock (&mem->lock); + +if (limit && (usage > limit)) + ret =0 ; +return ret; +}
This all looks a bit racy. And that's common in memory reclaim. We just have to ensure that when the race happens, we do reasonable things.
I suspect the locking in here could simply be removed.
include/linux/swap.h struct scan_control { · line 73
Quoted 8 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +3
-0@@ -66,6 +67,9 @@ struct scan_control {int swappiness;int all_unreclaimable; + +void *container;/* Used by containers for reclaiming */ +/* pages when the limit is exceeded */ };
eww. Why void*?
include/linux/swap.h struct scan_control {
Quoted 7 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +7
-0+#ifdef CONFIG_CONTAINER_MEMCTLR +/* +* Try to free `nr_pages' of memory, system-wide, and return the number of +* freed pages. +* Modelled after shrink_all_memory() +*/ +unsigned long memctlr_shrink_mapped_memory (unsigned long nr_pages,void *container)
80-columns, please.
Quoted 5 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +5
-0+{ +unsigned long ret =0 ; +int pass; +unsigned long nr_total_scanned =0 ; +
include/linux/swap.h
Quoted 10 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +10
-0+struct scan_control sc = { + .gfp_mask =GFP_KERNEL , + .may_swap =0 , + .swap_cluster_max = nr_pages, + .may_writepage =1 , + .swappiness = vm_swappiness, + .container = container, + .may_swap =1 , + .swappiness =100 , + };
swappiness got initialised twice.
Quoted 17 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +17
-0+/* +* We try to shrink LRUs in 3 passes: +* 0 = Reclaim from inactive_list only +* 1 = Reclaim mapped (normal reclaim) +* 2 = 2nd pass of type 1 +*/ +for (pass =0 ; pass <3 ; pass++) { +int prio; + +for (prio =DEF_PRIORITY ; prio >=0 ; prio--) { +unsigned long nr_to_scan = nr_pages - ret; + + sc.nr_scanned =0 ; + ret +=shrink_all_zones (nr_to_scan, prio, + pass,1 , &sc); +if (ret >= nr_pages) + goto out;
include/linux/swap.h
Quoted 10 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +10
-0+ + nr_total_scanned += sc.nr_scanned; +if (sc.nr_scanned && prio <DEF_PRIORITY -2 ) + congestion_wait(WRITE, HZ / 10); + } + } +out: +return ret; +} +#endif
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC][PATCH][3/4] Add reclaim support
Quoted 45 lines · Andrew Morton
Quoted 43 lines · On Mon, 19 Feb 2007 12:20:42 +0530 Balbir Singh <[email protected]> wrote:
This patch reclaims pages from a container when the container limit is hit. The executable is oom'ed only when the container it is running in, is overlimit and we could not reclaim any pages belonging to the container
A parameter called pushback, controls how much memory is reclaimed when the limit is hit. It should be easy to expose this knob to user space, but currently it is hard coded to 20% of the total limit of the container.
isolate_lru_pages()has been modified to isolate pages belonging to a particular container, so that reclaim code will reclaim only container pages. For shared pages, reclaim does not unmap all mappings of the page, it only unmaps those mappings that are over their limit. This ensures that other containers are not penalized while reclaiming shared pages.Parallel reclaim per container is not allowed. Each controller has a wait queue that ensures that only one task per control is running reclaim on that container.
...
include/linux/rmap.h renamed from include/linux/rmap.h~memctlr-reclaim-on-limit +9
-1@@ -90,7 +90,15 @@ static inline void page_dup_rmap(struct* Called from mm/vmscan.c to handle paging out */ int page_referenced (struct page *,int is_locked); -int try_to_unmap (struct page *,int ignore_refs); +int try_to_unmap (struct page *,int ignore_refs,void *container); +#ifdef CONFIG_CONTAINER_MEMCTLR +bool page_in_container (struct page *page,struct zone *zone,void *container); +#else +static inline bool page_in_container (struct page *page,struct zone *zone,void *container) +{ +return true; +} +#endif /* CONFIG_CONTAINER_MEMCTLR */ /* * Called from mm/filemap_xip.c to unmap empty zero page @@ -118,7 +126,8 @@ int page_mkclean(struct page *);#define anon_vma_link (vma) do {} while (0)
Quoted 9 lines
include/linux/rmap.h int page_mkclean(struct page *); · line 130
Quoted 4 lines
include/linux/rmap.h renamed from include/linux/rmap.h~memctlr-reclaim-on-limit +2
-1#define page_referenced (page,l) TestClearPageReferenced(page) -#define try_to_unmap (page, refs) SWAP_FAIL +#define try_to_unmap (page, refs, container) SWAP_FAIL +#define page_in_container (page, zone, container) trueI spy a compile error.
The static-inline version looks nicer.
I will compile with the feature turned off and double check. I'll also convert it to a static inline function.
Quoted 8 lines
Quoted 8 lines
include/linux/rmap.h renamed from include/linux/rmap.h~memctlr-reclaim-on-limit +0
-0static inline int page_mkclean (struct page *page) {diff -puN
include/linux/swap.h~memctlr-reclaim-on-limitinclude/linux/swap.hinclude/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +0
-0@@ -188,6 +188,10 @@ extern void swap_setup(void);/* linux/mm/vmscan.c */ extern unsigned long try_to_free_pages (struct zone **,gfp_t );
Quoted 9 lines
include/linux/swap.h extern void swap_setup(void); · line 194
Quoted 5 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +4
-0extern unsigned long shrink_all_memory (unsigned long nr_pages); +#ifdef CONFIG_CONTAINER_MEMCTLR +extern unsigned long memctlr_shrink_mapped_memory (unsigned long nr_pages, +void *container); +#endif Usually one doesn't need to put ifdefs around the declaration like this. If the function doesn't exist and nobody calls it, we're fine. If someone _does_ call it, we'll find out the error at link-time.
Sure, sounds good. I'll get rid of the #ifdefs.
Quoted 32 lines
Quoted 32 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +31
-0+/* +* checks if the mm's container and scan control passed container match, if +* so, is the container over it's limit. Returns 1 if the container is above +* its limit. +*/ +int memctlr_mm_overlimit (struct mm_struct *mm,void *sc_cont) +{ +struct container *cont; +struct memctlr *mem; +long usage, limit; +int ret =1 ; + +if (!sc_cont) + goto out; + +read_lock (&mm->container_lock); + cont = mm->container; + +/* +* Regular reclaim, let it proceed as usual +*/ +if (!sc_cont) + goto out; + + ret =0 ; +if (cont != sc_cont) + goto out; + + mem =memctlr_from_cont (cont); + usage =atomic_long_read (&mem->counter.usage); + limit =atomic_long_read (&mem->counter.limit);
Quoted 9 lines
include/linux/swap.h
Quoted 6 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +6
-0+if (limit && (usage > limit)) + ret =1 ; +out: +read_unlock (&mm->container_lock); +return ret; +}hm, I wonder how much additional lock traffic all this adds.
It's a read_lock() and most of the locks are read_locks which allow for concurrent access, until the container changes or goes away
Quoted 50 lines
Quoted 36 lines
int
memctlr_mm_init(struct mm_struct *mm)
{include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +29
-0mm->counter =kmalloc (sizeof (struct res_counter ),GFP_KERNEL );@@ -77,6 +125,46 @@ void memctlr_mm_assign_container(structwrite_unlock (&mm->container_lock); } +static int memctlr_check_and_reclaim (struct container *cont,long usage, +long limit) +{ +unsigned long nr_pages =0 ; +unsigned long nr_reclaimed =0 ; +int retries = nr_retries; +int ret =1 ; +struct memctlr *mem; + + mem =memctlr_from_cont (cont); +spin_lock (&mem->lock); +while ((retries-- >0 ) && limit && (usage > limit)) { +if (mem->reclaim_in_progress) { +spin_unlock (&mem->lock); +wait_event (mem->wq, !mem->reclaim_in_progress); +spin_lock (&mem->lock); + }else { +if (!nr_pages) + nr_pages = (pushback * limit) /100 ; + mem->reclaim_in_progress = true; +spin_unlock (&mem->lock); + nr_reclaimed +=memctlr_shrink_mapped_memory (nr_pages, + cont); +spin_lock (&mem->lock); + mem->reclaim_in_progress = false; +wake_up_all (&mem->wq); + } +/* +* Resample usage and limit after reclaim include/linux/swap.h void memctlr_mm_assign_container(struct · line 166
Quoted 10 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +10
-0+*/ + usage =atomic_long_read (&mem->counter.usage); + limit =atomic_long_read (&mem->counter.limit); + } +spin_unlock (&mem->lock); + +if (limit && (usage > limit)) + ret =0 ; +return ret; +}This all looks a bit racy. And that's common in memory reclaim. We just have to ensure that when the race happens, we do reasonable things.
Quoted 2 lines
I suspect the locking in here could simply be removed.
The locking is mostly to ensure that tasks belonging to the same container see a consistent value of reclaim_in_progress. I'll see if the locking can be simplified or simply removed.
Quoted 11 lines
include/linux/swap.h struct scan_control { · line 73
Quoted 8 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +3
-0@@ -66,6 +67,9 @@ struct scan_control {int swappiness;int all_unreclaimable; + +void *container;/* Used by containers for reclaiming */ +/* pages when the limit is exceeded */ };eww. Why void*?
I did not want to expose struct container in mm/vmscan.c. An additional thought was that no matter what container goes in the field would be useful for reclaim.
Quoted 10 lines
include/linux/swap.h struct scan_control {
Quoted 7 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +7
-0+#ifdef CONFIG_CONTAINER_MEMCTLR +/* +* Try to free `nr_pages' of memory, system-wide, and return the number of +* freed pages. +* Modelled after shrink_all_memory() +*/ +unsigned long memctlr_shrink_mapped_memory (unsigned long nr_pages,void *container)80-columns, please.
I'll fix this.
Quoted 9 lines
Quoted 9 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +9
-0+{ +unsigned long ret =0 ; +int pass; +unsigned long nr_total_scanned =0 ; + +struct scan_control sc = { + .gfp_mask =GFP_KERNEL , + .may_swap =0 , + .swap_cluster_max = nr_pages,
Quoted 9 lines
include/linux/swap.h
Quoted 6 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +6
-0+ .may_writepage = 1, + .swappiness = vm_swappiness, + .container = container, + .may_swap = 1, + .swappiness = 100, + };swappiness got initialised twice.
I should have caught that earlier. Thanks for spotting this. I'll fix it.
Quoted 28 lines
Quoted 27 lines
include/linux/swap.h renamed from include/linux/swap.h~memctlr-reclaim-on-limit +27
-0+/* +* We try to shrink LRUs in 3 passes: +* 0 = Reclaim from inactive_list only +* 1 = Reclaim mapped (normal reclaim) +* 2 = 2nd pass of type 1 +*/ +for (pass =0 ; pass <3 ; pass++) { +int prio; + +for (prio =DEF_PRIORITY ; prio >=0 ; prio--) { +unsigned long nr_to_scan = nr_pages - ret; + + sc.nr_scanned =0 ; + ret +=shrink_all_zones (nr_to_scan, prio, + pass,1 , &sc); +if (ret >= nr_pages) + goto out; + + nr_total_scanned += sc.nr_scanned; +if (sc.nr_scanned && prio <DEF_PRIORITY -2 ) +congestion_wait (WRITE ,HZ /10 ); + } + } +out: + return ret; +} +#endif
Signature · 2 lines
Warm Regards, Balbir Singh
Andrew Morton
maintainerRe: [RFC][PATCH][3/4] Add reclaim support
Quoted 39 lines · On Mon, 19 Feb 2007 16:20:53 +0530 Balbir Singh <[email protected]> wrote:
Quoted 29 lines
Quoted 29 lines
+ * so, is the container over it's limit. Returns 1 if the container is above
+ * its limit.
+ */mm/memctlr.c renamed from mm/memctlr.c~memctlr-reclaim-on-limit +26
-0+int memctlr_mm_overlimit (struct mm_struct *mm,void *sc_cont) +{ +struct container *cont; +struct memctlr *mem; +long usage, limit; +int ret =1 ; + +if (!sc_cont) + goto out; + +read_lock (&mm->container_lock); + cont = mm->container; + +/* +* Regular reclaim, let it proceed as usual +*/ +if (!sc_cont) + goto out; + + ret =0 ; +if (cont != sc_cont) + goto out; + + mem =memctlr_from_cont (cont); + usage =atomic_long_read (&mem->counter.usage); + limit =atomic_long_read (&mem->counter.limit);Quoted 9 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 91
Quoted 6 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-reclaim-on-limit +6
-0+if (limit && (usage > limit)) + ret =1 ; +out: +read_unlock (&mm->container_lock); +return ret; +}hm, I wonder how much additional lock traffic all this adds.
Quoted 3 lines
It's a
read_lock()and most of the locks areread_lockswhich allow for concurrent access, until the container changes or goes away
read_lock isn't free, and I suspect we're calling this function pretty often (every pagefault?) It'll be measurable on some workloads, on some hardware.
It probably won't be terribly bad because each lock-taking is associated with a clear_page(). But still, if there's any possibility of lightening the locking up, now is the time to think about it.
Quoted 12 lines
Quoted 11 lines
mm/vmscan.c struct scan_control { · line 73
Quoted 8 lines
mm/vmscan.c renamed from mm/vmscan.c~memctlr-reclaim-on-limit +3
-0@@ -66,6 +67,9 @@ struct scan_control {int swappiness;int all_unreclaimable; + +void *container;/* Used by containers for reclaiming */ +/* pages when the limit is exceeded */ };eww. Why void*?
Quoted 1 line
I did not want to expose struct container in
mm/vmscan.c.
It's already there, via rmap.h
Quoted 3 lines
An additional
thought was that no matter what container goes in the field would be useful for reclaim.
Am having trouble parsing that sentence ;)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC][PATCH][3/4] Add reclaim support
Quoted 43 lines · Andrew Morton
Quoted 37 lines · On Mon, 19 Feb 2007 16:20:53 +0530 Balbir Singh <[email protected]> wrote:
Quoted 28 lines
Quoted 28 lines
+ * so, is the container over it's limit. Returns 1 if the container is above
+ * its limit.
+ */mm/memctlr.c renamed from mm/memctlr.c~memctlr-reclaim-on-limit +25
-0+int memctlr_mm_overlimit (struct mm_struct *mm,void *sc_cont) +{ +struct container *cont; +struct memctlr *mem; +long usage, limit; +int ret =1 ; + +if (!sc_cont) + goto out; + +read_lock (&mm->container_lock); + cont = mm->container; + +/* +* Regular reclaim, let it proceed as usual +*/ +if (!sc_cont) + goto out; + + ret =0 ; +if (cont != sc_cont) + goto out; + + mem =memctlr_from_cont (cont); + usage =atomic_long_read (&mem->counter.usage);Quoted 9 lines
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 91
Quoted 7 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-reclaim-on-limit +7
-0+ limit =atomic_long_read (&mem->counter.limit); +if (limit && (usage > limit)) + ret =1 ; +out: +read_unlock (&mm->container_lock); +return ret; +}hm, I wonder how much additional lock traffic all this adds.
Quoted 3 lines
It's a
read_lock()and most of the locks areread_lockswhich allow for concurrent access, until the container changes or goes away
Quoted 8 lines
read_lockisn't free, and I suspect we're calling this function pretty often (every pagefault?) It'll be measurable on some workloads, on some hardware.It probably won't be terribly bad because each lock-taking is associated with a
clear_page(). But still, if there's any possibility of lightening the locking up, now is the time to think about it.
Yes, good point. I'll revisit to see if barriers can replace the locking or if the locking is required at all?
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
mm/vmscan.c struct scan_control { · line 73
Quoted 8 lines
mm/vmscan.c renamed from mm/vmscan.c~memctlr-reclaim-on-limit +3
-0@@ -66,6 +67,9 @@ struct scan_control {int swappiness;int all_unreclaimable; + +void *container;/* Used by containers for reclaiming */ +/* pages when the limit is exceeded */ };eww. Why void*?
Quoted 4 lines
Quoted 1 line
I did not want to expose struct container in
mm/vmscan.c.It's already there, via
rmap.h
Yes, true
Quoted 7 lines
Quoted 3 lines
An additional
thought was that no matter what container goes in the field would be useful for reclaim.Am having trouble parsing that sentence ;)
The thought was that irrespective of the infrastructure that goes in having an entry for reclaim in scan_control would be useful. I guess the name exposes what the type tries to hide :-)
Signature · 2 lines
Warm Regards, Balbir Singh
Re: [RFC][PATCH][3/4] Add reclaim support
In reply to Balbir SinghQuoted 8 lines · On Mon, 19 Feb 2007 12:20:42 +0530 Balbir Singh <[email protected]> wrote:
mm/memctlr.c renamed from mm/memctlr.c~memctlr-reclaim-on-limit +8
-0+int memctlr_mm_overlimit (struct mm_struct *mm,void *sc_cont) +{ +struct container *cont; +struct memctlr *mem; +long usage, limit; +int ret =1 ; + +if (!sc_cont)
mm/memctlr.c static inline struct memctlr *memctlr_fr · line 92
Quoted 10 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-reclaim-on-limit +9
-0+ goto out; + +read_lock (&mm->container_lock); + cont = mm->container; +out: +read_unlock (&mm->container_lock); +return ret; +} +
should be
==out_and_unlock:
read_unlock(&mm->container_lock); out_: return ret;
-Kame
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [RFC][PATCH][3/4] Add reclaim support
Quoted 25 lines · KAMEZAWA Hiroyuki
Quoted 8 lines · On Mon, 19 Feb 2007 12:20:42 +0530 Balbir Singh <[email protected]> wrote:
mm/memctlr.c renamed from mm/memctlr.c~memctlr-reclaim-on-limit +8
-0+int memctlr_mm_overlimit (struct mm_struct *mm,void *sc_cont) +{ +struct container *cont; +struct memctlr *mem; +long usage, limit; +int ret =1 ; + +if (!sc_cont)mm/memctlr.c static inline struct memctlr *memctlr_fr · line 92
Quoted 10 lines
mm/memctlr.c renamed from mm/memctlr.c~memctlr-reclaim-on-limit +9
-0+ goto out; + +read_lock (&mm->container_lock); + cont = mm->container; +out: +read_unlock (&mm->container_lock); +return ret; +} +should be
==out_and_unlock:
Quoted 4 lines
read_unlock(&mm->container_lock); out_: return ret;
Thanks, that's a much convention!
Quoted 3 lines
-Kame
Signature · 2 lines
Warm Regards, Balbir Singh
RFC PATCH 4/4 RSS controller documentation
In reply to Balbir Singh---
Documentation/memctlr.txt | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+)
Documentation/memctlr.txt new file +70 -0
@@ -0,0 +1,70 @@ +Introduction +------------ + +The memory controller is a controller module written under the containers +framework. It can be used to limit the resource usage of a group of +tasks grouped by the container. + +Accounting +---------- + +The memory controller tracks the RSS usage of the tasks in the container. +The definition of RSS was debated on lkml in the following thread + + http://lkml.org/lkml/2006/10/10/130 + +This patch is flexible, it is easy to adapt the patch to any definition +of RSS. The current accounting is based on the current definition of +RSS. Each page mapped is charged to the container. + +The accounting is done at two levels, each process has RSS accounting in +the mm_struct and in the container it belongs to. The mm_struct accounting +is used when a task switches (migrates to a different) container(s). The +accounting information for the task is subtracted from the source container +and added to the destination container. If as result of the migration, the +destination container goes over limit, no action is taken until some task +in the destination container runs and tries to map a new page in its +page table. + +The current RSS usage can be seen in the memctlr_usage file. The value +is in units of pages. + +Control +------- + +The memctlr_limit file allows the user to set a limit on the number of +pages that can be mapped by the processes in the container. A special +value of 0 (which is the default limit of any new container), indicates +that the container can use unlimited amount of RSS. + +Reclaim +------- + +When the limit set in the container is hit, the memory controller starts +reclaiming pages belonging to the container (simulating a local LRU in +some sense). isolate_lru_pages() has been modified to isolate lru +pages belonging to a specific container. Parallel reclaims on the same +container are not allowed, other tasks end up waiting for the any existing +reclaim to finish. + +The reclaim code uses two internal knobs, retries and pushback. pushback +specifies the percentage of memory to be reclaimed when the container goes +over limit. The retries knob, controls how many times reclaim is retried +before the task is killed (because reclaim failed). + +Shared pages are treated specially during reclaim. They are not force +reclaimed, they are only unmapped from containers which are over limit. +This ensures that other containers do not pay a penalty for a shared +page being reclaimed when a paritcular container goes over its limit. + +NOTE: All limits are hard limits. + +Future Plans +------------ + +The current controller implements only RSS control. It is planned to add +the following components + +1. Page Cache control +2. mlock'ed memory control +3. kernel memory allocation control (memory allocated on behalf of a task)
_
Signature · 2 lines
Warm Regards, Balbir Singh
Andrew Morton
maintainerQuoted 6 lines · On Mon, 19 Feb 2007 12:20:19 +0530 Balbir Singh <[email protected]> wrote:
This patch applies on top of Paul Menage's container patches (V7) posted at
http://lkml.org/lkml/2007/2/12/88It implements a controller within the containers framework for limiting memory usage (RSS usage).
It's good to see someone building on someone else's work for once, rather than everyone going off in different directions. It makes one hope that we might actually achieve something at last.
The key part of this patchset is the reclaim algorithm:
Review static unsigned long isolate_lru_pages(u · line 652
Quoted 12 lines
(quoted diff) +8
-0@@ -636,6 +642,15 @@ static unsigned long isolate_lru_pages(u list_del(&page->lru); target = src; + /* + * For containers, do not scan the page unless it + * belongs to the container we are reclaiming for + */ + if (container && !page_in_container(page, zone, container)) { + scan--; + goto done; + }
Alas, I fear this might have quite bad worst-case behaviour. One small container which is under constant memory pressure will churn the system-wide LRUs like mad, and will consume rather a lot of system time. So it's a point at which container A can deleteriously affect things which are running in other containers, which is exactly what we're supposed to not do.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 7 lines · Andrew Morton
Alas, I fear this might have quite bad worst-case behaviour. One small container which is under constant memory pressure will churn the system-wide LRUs like mad, and will consume rather a lot of system time. So it's a point at which container A can deleteriously affect things which are running in other containers, which is exactly what we're supposed to not do.
I think it's OK for a container to consume lots of system time during reclaim, as long as we can account that time to the container involved (i.e. if it's done during direct reclaim rather than by something like kswapd).
Churning the LRU could well be bad though, I agree.
Paul
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 10 lines · Andrew Morton
Quoted 7 lines
Alas, I fear this might have quite bad worst-case behaviour. One small container which is under constant memory pressure will churn the system-wide LRUs like mad, and will consume rather a lot of system time. So it's a point at which container A can deleteriously affect things which are running in other containers, which is exactly what we're supposed to not do.
Quoted 4 lines
I think it's OK for a container to consume lots of system time during reclaim, as long as we can account that time to the container involved (i.e. if it's done during direct reclaim rather than by something like kswapd).
hmm, is it ok to scan 100Gb of RAM for 10MB RAM container? in UBC patch set we used page beancounters to track containter pages. This allows to make efficient scan contoler and reclamation.
Thanks,
Kirill
Quoted 8 lines · Kirill Korotaev
Quoted 5 lines
I think it's OK for a container to consume lots of system time during reclaim, as long as we can account that time to the container involved (i.e. if it's done during direct reclaim rather than by something like kswapd).
hmm, is it ok to scan 100Gb of RAM for 10MB RAM container? in UBC patch set we used page beancounters to track containter pages. This allows to make efficient scan contoler and reclamation.
I don't mean that we shouldn't go for the most efficient method that's practical. If we can do reclaim without spinning across so much of the LRU, then that's obviously better.
But if the best approach in the general case results in a process in the container spending lots of CPU time trying to do the reclaim, that's probably OK as long as we can account for that time and (once we have a CPU controller) throttle back the container in that case. So then, a container can only hurt itself by thrashing/reclaiming, rather than hurting other containers. (LRU churn notwithstanding ...)
Paul
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 13 lines · Kirill Korotaev
Quoted 9 lines · Andrew Morton
Quoted 7 lines
Alas, I fear this might have quite bad worst-case behaviour. One small container which is under constant memory pressure will churn the system-wide LRUs like mad, and will consume rather a lot of system time. So it's a point at which container A can deleteriously affect things which are running in other containers, which is exactly what we're supposed to not do.
Quoted 4 lines
I think it's OK for a container to consume lots of system time during reclaim, as long as we can account that time to the container involved (i.e. if it's done during direct reclaim rather than by something like kswapd).
Quoted 6 lines
hmm, is it ok to scan 100Gb of RAM for 10MB RAM container? in UBC patch set we used page beancounters to track containter pages. This allows to make efficient scan contoler and reclamation.
Thanks,
Kirill
Hi, Kirill,
Yes, that's a problem, but I think it's a problem that can be solved in steps. First step, add reclaim. Second step, optimize reclaim.
Signature · 2 lines
Warm Regards, Balbir Singh
Quoted 9 lines · Paul Menage
Quoted 7 lines · Andrew Morton
Alas, I fear this might have quite bad worst-case behaviour. One small container which is under constant memory pressure will churn the system-wide LRUs like mad, and will consume rather a lot of system time. So it's a point at which container A can deleteriously affect things which are running in other containers, which is exactly what we're supposed to not do.
Quoted 7 lines
I think it's OK for a container to consume lots of system time during reclaim, as long as we can account that time to the container involved (i.e. if it's done during direct reclaim rather than by something like kswapd).
Churning the LRU could well be bad though, I agree.
I completely agree with you on reclaim consuming time.
Churning the LRU can be avoided by the means I mentioned before
1. Add a container pointer (per page struct), it is also useful for the page cache controller
2. Check if the page belongs to a particular container before the list_del(&page->lru), so that those pages can be skipped.
3. Use a double LRU list by overloading the lru list_head of struct page.
Quoted 2 lines
Paul
Signature · 2 lines
Warm Regards, Balbir Singh
Quoted 9 lines · Andrew Morton
Quoted 6 lines · On Mon, 19 Feb 2007 12:20:19 +0530 Balbir Singh <[email protected]> wrote:
This patch applies on top of Paul Menage's container patches (V7) posted at
http://lkml.org/lkml/2007/2/12/88It implements a controller within the containers framework for limiting memory usage (RSS usage).
Quoted 8 lines
The key part of this patchset is the reclaim algorithm:
Alas, I fear this might have quite bad worst-case behaviour. One small container which is under constant memory pressure will churn the system-wide LRUs like mad, and will consume rather a lot of system time. So it's a point at which container A can deleteriously affect things which are running in other containers, which is exactly what we're supposed to not do.
Nice with a simple memory controller. The downside seems to be that it doesn't scale very well when it comes to reclaim, but maybe that just comes with being simple. Step by step, and maybe this is a good first step?
Ideally I'd like to see unmapped pages handled on a per-container LRU with a fallback to the system-wide LRUs. Shared/mapped pages could be handled using PTE ageing/unmapping instead of page ageing, but that may consume too much resources to be practical.
/ magnus
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 20 lines · Magnus Damm
Quoted 10 lines · Andrew Morton
Quoted 1 line · On Mon, 19 Feb 2007 12:20:19 +0530 Balbir Singh <[email protected]> wrote:
This patch applies on top of Paul Menage's container patches (V7)
posted at
Quoted 5 lines
http://lkml.org/lkml/2007/2/12/88It implements a controller within the containers framework for limiting memory usage (RSS usage).
Quoted 8 lines
The key part of this patchset is the reclaim algorithm:
Alas, I fear this might have quite bad worst-case behaviour. One small container which is under constant memory pressure will churn the system-wide LRUs like mad, and will consume rather a lot of system time. So it's a point at which container A can deleteriously affect things which are running in other containers, which is exactly what we're supposed to not do.
Quoted 5 lines
Nice with a simple memory controller. The downside seems to be that it doesn't scale very well when it comes to reclaim, but maybe that just comes with being simple. Step by step, and maybe this is a good first step?
Thanks, I totally agree.
Quoted 6 lines
Ideally I'd like to see unmapped pages handled on a per-container LRU with a fallback to the system-wide LRUs. Shared/mapped pages could be handled using PTE ageing/unmapping instead of page ageing, but that may consume too much resources to be practical.
/ magnus
Keeping unmapped pages per container sounds interesting. I am not quite sure what PTE ageing, will it look it up.
Signature · 2 lines
Warm Regards, Balbir Singh
Quoted 36 lines · Balbir Singh
Quoted 22 lines · Magnus Damm
Quoted 13 lines · Andrew Morton
Quoted 1 line · On Mon, 19 Feb 2007 12:20:19 +0530 Balbir Singh <[email protected]> wrote:
This patch applies on top of Paul Menage's container patches (V7)
posted at
Quoted 5 lines
http://lkml.org/lkml/2007/2/12/88It implements a controller within the containers framework for limiting memory usage (RSS usage).
The key part of this patchset is the reclaim algorithm:
Quoted 7 lines
Alas, I fear this might have quite bad worst-case behaviour. One small container which is under constant memory pressure will churn the system-wide LRUs like mad, and will consume rather a lot of system time. So it's a point at which container A can deleteriously affect things which
are running in other containers, which is exactly what we're supposed to not do.Quoted 5 lines
Nice with a simple memory controller. The downside seems to be that it doesn't scale very well when it comes to reclaim, but maybe that just comes with being simple. Step by step, and maybe this is a good first step?
Thanks, I totally agree.
Quoted 5 lines
Ideally I'd like to see unmapped pages handled on a per-container LRU with a fallback to the system-wide LRUs. Shared/mapped pages could be handled using PTE ageing/unmapping instead of page ageing, but that may consume too much resources to be practical.
Quoted 4 lines
Quoted 1 line
/ magnus
Keeping unmapped pages per container sounds interesting. I am not quite sure what PTE ageing, will it look it up.
You will most likely have no luck looking it up, so here is what I mean by PTE ageing:
The most common unit for memory resource control seems to be physical pages. Keeping track of pages is simple in the case of a single user per page, but for shared pages tracking the owner becomes more complex.
I consider unmapped pages to only have a single user at a time, so the unit for unmapped memory resource control is physical pages. Apart from implementation details such as fun with struct page and scalability, handling this case is not so complicated.
Mapped or shared pages should be handled in a different way IMO. PTEs should be used instead of using physical pages as unit for resource control and reclaim. For the user this looks pretty much the same as physical pages, apart for memory overcommit.
So instead of using a global page reclaim policy and reserving physical pages per container I propose that resource controlled shared pages should be handled using a PTE replacement policy. This policy is used to keep the most active PTEs in the container backed by physical pages. Inactive PTEs gets unmapped in favour over newer PTEs.
One way to implement this could be by populating the address space of resource controlled processes with multiple smaller LRU2Qs. The compact data structure that I have in mind is basically an array of 256 bytes, one byte per PTE. Associated with this data strucuture are start indexes and lengths for two lists. The indexes are used in a FAT-type of chain to form single linked lists. So we create active and inactive list here - and we move PTEs between the lists when we check the young bits from the page reclaim and when we apply memory pressure. Unmapping is done through the normal page reclaimer but using information from the PTE LRUs.
In my mind this should lead to more fair resource control of mapped pages, but if it is possible to implement with low overhead, that's another question. =)
Thanks for listening.
/ magnus
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 81 lines · Magnus Damm
Quoted 40 lines · Balbir Singh
Quoted 10 lines · Magnus Damm
Quoted 9 lines · Andrew Morton
Quoted 1 line · On Mon, 19 Feb 2007 12:20:19 +0530 Balbir Singh <[email protected]> wrote:
This patch applies on top of Paul Menage's container patches (V7)
posted at
Quoted 4 lines
http://lkml.org/lkml/2007/2/12/88It implements a controller within the containers framework for
limiting
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
memory usage (RSS usage).
The key part of this patchset is the reclaim algorithm:
Alas, I fear this might have quite bad worst-case behaviour. One
small
Quoted 2 lines
Quoted 2 lines
container which is under constant memory pressure will churn the system-wide LRUs like mad, and will consume rather a lot of system
time.
Quoted 3 lines
Quoted 3 lines
So it's a point at which container A can deleteriously affect things which
are running in other containers, which is exactly what we'resupposed to
Quoted 7 lines
Quoted 1 line
not do.
Nice with a simple memory controller. The downside seems to be that it doesn't scale very well when it comes to reclaim, but maybe that just comes with being simple. Step by step, and maybe this is a good first step?
Thanks, I totally agree.
Quoted 5 lines
Ideally I'd like to see unmapped pages handled on a per-container LRU with a fallback to the system-wide LRUs. Shared/mapped pages could be handled using PTE ageing/unmapping instead of page ageing, but that may consume too much resources to be practical.
Quoted 4 lines
Quoted 1 line
/ magnus
Keeping unmapped pages per container sounds interesting. I am not quite sure what PTE ageing, will it look it up.
You will most likely have no luck looking it up, so here is what I mean by PTE ageing:
The most common unit for memory resource control seems to be physical pages. Keeping track of pages is simple in the case of a single user per page, but for shared pages tracking the owner becomes more complex.
I consider unmapped pages to only have a single user at a time, so the unit for unmapped memory resource control is physical pages. Apart from implementation details such as fun with struct page and scalability, handling this case is not so complicated.
Mapped or shared pages should be handled in a different way IMO. PTEs should be used instead of using physical pages as unit for resource control and reclaim. For the user this looks pretty much the same as physical pages, apart for memory overcommit.
So instead of using a global page reclaim policy and reserving physical pages per container I propose that resource controlled shared pages should be handled using a PTE replacement policy. This policy is used to keep the most active PTEs in the container backed by physical pages. Inactive PTEs gets unmapped in favour over newer PTEs.
One way to implement this could be by populating the address space of resource controlled processes with multiple smaller LRU2Qs. The compact data structure that I have in mind is basically an array of 256 bytes, one byte per PTE. Associated with this data strucuture are start indexes and lengths for two lists. The indexes are used in a FAT-type of chain to form single linked lists. So we create active and inactive list here - and we move PTEs between the lists when we check the young bits from the page reclaim and when we apply memory pressure. Unmapping is done through the normal page reclaimer but using information from the PTE LRUs.
Quoted 8 lines
In my mind this should lead to more fair resource control of mapped pages, but if it is possible to implement with low overhead, that's another question. =)
Thanks for listening.
/ magnus
Thanks for explaining PTE aging.
Signature · 2 lines
Warm Regards, Balbir Singh
Quoted 4 lines · Andrew Morton
Quoted 2 lines · On Mon, 19 Feb 2007 12:20:19 +0530 Balbir Singh <[email protected]> wrote:
This patch applies on top of Paul Menage's container patches (V7) posted at
Quoted 9 lines
Quoted 4 lines
http://lkml.org/lkml/2007/2/12/88It implements a controller within the containers framework for limiting memory usage (RSS usage).
It's good to see someone building on someone else's work for once, rather than everyone going off in different directions. It makes one hope that we might actually achieve something at last.
Thanks! It's good to know we are headed in the right direction.
Quoted 5 lines
The key part of this patchset is the reclaim algorithm:
Quoted 2 lines
(quoted diff) +0
-0@@ -636,6 +642,15 @@ static unsigned long isolate_lru_pages(u
Quoted 17 lines
Review static unsigned long isolate_lru_pages(u · line 652
Quoted 10 lines
(quoted diff) +8
-0list_del(&page->lru); target = src; + /* + * For containers, do not scan the page unless it + * belongs to the container we are reclaiming for + */ + if (container && !page_in_container(page, zone, container)) { + scan--; + goto done; + }Alas, I fear this might have quite bad worst-case behaviour. One small
container which is under constant memory pressure will churn the
system-wide LRUs like mad, and will consume rather a lot of system time. So it's a point at which container A can deleteriously affect things which are running in other containers, which is exactly what we're supposed to
not do.
Hmm.. I guess it's space vs time then :-) A CPU controller could control how much time is spent reclaiming ;)
Coming back, I see the problem you mentioned and we have been thinking of several possible solutions. In my introduction I pointed out
"Come up with cool page replacement algorithms for containers (if possible without any changes to struct page)"
The solutions we have looked at are
1. Overload the LRU list_head in struct page to have a global LRU + a per container LRU
+------+ prev +------+ | page +--------------------->| page +--------- | 0 |<---------------------+ 1 |<-------- +------+ next +------+
Global LRU
+------+
+------- + prev |
| | next +-------------------------------------------+
| +------+ |
V ^ V
+------+ | prev +------+ +------+
| page + +------------ + page +---------..... | page +
| 0 |--------------------->+ 1 |<-------- | n |
+------+ next +------+ +------+Global LRU + Container LRU
Page 1 and n belong to the same container, to get to page 0, you need two de-references
2. Modify struct page to point to a container and allow each container to have a per-container LRU along with the global LRU
For efficiency we need the container LRU and we don't want to split the global LRU either.
We need to optimize the reclaim path, but I thought of that as a secondary problem. Once we all agree that the controller looks simple, accounts well and works. We can/should definitely optimize the reclaim path.
Signature · 2 lines
Warm Regards, Balbir Singh