Memory controller introduction (v6)
Subsystems (13)
cover Memory controller introduction (v6)
Here's version 6 of the memory controller (against 2.6.23-rc2-mm2).
The tests that were run has been included in the _Test Results section_ below.
Changelog since version 5
1. Ported to 2.6.23-rc2-mm2
2. Added a css_put() in the case of race between allocation of page_containers (YAMAMOTO Takashi)
Changelog since version 4
1. Renamed meta_page to page_container (Nick Piggin)
2. Moved locking from page flags to last bit of the page_container pointer (Nick Piggin)
3. Fixed a rare race in mem_container_isolate_pages (YAMAMOTO Takashi)
Changelog since version 3
1. Ported to v11 of the containers patchset (2.6.23-rc1-mm1). Paul Menage helped immensely with a detailed review of v3
2. Reclaim is retried to allow reclaim of pages coming in as a result of mapped pages reclaim (swap cache growing as a result of RSS reclaim)
3. page_referenced() is now container aware. During container reclaim, references from other containers do not prevent a page from being reclaimed from a non-referencing container
4. Fixed a possible race condition spotted by YAMAMOTO Takashi
Changelog since version 2
1. Improved error handling in mm/memory.c (spotted by YAMAMOTO Takashi)
2. Test results included
3. try_to_free_mem_container_pages() bug fix (sc->may_writepage is now set to !laptop_mode)
Changelog since version 1
1. Fixed some compile time errors (in mm/migrate.c from Vaidyanathan S)
2. Fixed a panic seen when LIST_DEBUG is enabled
3. Added a mechanism to control whether we track page cache or both page cache and mapped pages (as requested by Pavel)
4. Dave Hansen provided detail review comments on the code.
This patchset implements another version of the memory controller. These patches have been through a big churn, the first set of patches were posted last year and earlier this year at
http://lkml.org/lkml/2007/2/19/10
This patchset draws from the patches listed above and from some of the contents of the patches posted by Vaidyanathan for page cache control.
http://lkml.org/lkml/2007/6/20/92
At OLS, the resource management BOF, it was discussed that we need to manage RSS and unmapped page cache together. This patchset is a step towards that
TODO's
1. Add memory controller water mark support. Reclaim on high water mark
2. Add support for shrinking on limit change
3. Add per zone per container LRU lists (this is being actively worked on by Pavel Emelianov)
4. Figure out a better CLUI for the controller
5. Add better statistics
6. Explore using read_unit64() as recommended by Paul Menage (NOTE: read_ulong() would also be nice to have)
In case you have been using/testing the RSS controller, you'll find that this controller works slower than the RSS controller. The reason being that both swap cache and page cache is accounted for, so pages do go out to swap upon reclaim (they cannot live in the swap cache).
Any test output, feedback, comments, suggestions are welcome! I am committed to fixing any bugs and improving the performance of the memory controller. Do not hesitate to send any fixes, request for fixes that is required.
Using the patches
1. Enable Memory controller configuration
2. Compile and boot the new kernel
3. mount -t container container -o memory /container
will mount the memory controller to the /container mount point
4. mkdir /container/a
5. echo $$ > /container/a/tasks (add tasks to the new container)
6. echo -n <num_pages> > /container/a/memory.limit
example
echo -n 204800 > /container/a/memory.limit, sets the limit to 800 MB on a system with 4K page size
7. run tasks, see the memory controller work
8. Report results, provide feedback
9. Develop/use new patches and go to step 1
Test Results
Results for version 3 of the patch were posted at
http://lwn.net/Articles/242554/
The code was also tested on a power box with regular machine usage scenarios, the config disabled and with a stress suite that touched all the memory in the system and was limited in a container.
Dhaval ran several tests on v6 and gave his thumbs up to the controller (a hard to achieve goal :-) ).
Run kernbench stress
--------------------
Three simultaneously and with one inside a container of 800 MB.
Kernbench results running within the container of 800 MB
--------------------------------------------------------
Thu Aug 16 22:34:59 IST 2007
2.6.23-rc2-mm2-mem-v6
Average Half load -j 4 Run (std deviation):
Elapsed Time 466.548 (47.6014)
User Time 876.598 (10.5273)
System Time 223.136 (1.29247)
Percent CPU 237.2 (23.2744)
Context Switches 146351 (6539.91)
Sleeps 174003 (5031.94)
Average Optimal load -j 32 Run (std deviation):
Elapsed Time 423.496 (60.625)
User Time 897.285 (23.0391)
System Time 228.836 (6.11205)
Percent CPU 257.1 (40.9022)
Context Switches 262134 (123397)
Sleeps 270815 (103597)
Kernbench results running within the default container
------------------------------------------------------
Thu Aug 16 22:34:33 IST 2007
2.6.23-rc2-mm2-mem-v6
Average Half load -j 4 Run (std deviation):
Elapsed Time 424.17 (3.45908)
User Time 841.992 (5.40178)
System Time 213.01 (0.706258)
Percent CPU 248.2 (0.83666)
Context Switches 134254 (9535.83)
Sleeps 167359 (6858.45)
Average Optimal load -j 32 Run (std deviation):
Elapsed Time 407.092 (108.932)
User Time 878.493 (38.6575)
System Time 222.155 (9.77127)
Percent CPU 278.4 (91.5826)
Context Switches 253836 (127708)
Sleeps 263760 (103468)
Kernbench results running within the default container
------------------------------------------------------
Thu Aug 16 22:34:52 IST 2007
2.6.23-rc2-mm2-mem-v6
Average Half load -j 4 Run (std deviation):
Elapsed Time 465.038 (48.5147)
User Time 874.742 (5.86563)
System Time 222.194 (0.561676)
Percent CPU 237.2 (22.5211)
Context Switches 144040 (7052.23)
Sleeps 172130 (5608.73)
Average Optimal load -j 32 Run (std deviation):
Elapsed Time 426.25 (62.34)
User Time 893.938 (20.6732)
System Time 227.717 (5.87502)
Percent CPU 255.6 (40.7163)
Context Switches 259560 (122953)
Sleeps 267402 (101801)
Ebizzy results
--------------
ebizzy on top of rc2-mm2 (without mem-controller) gave this
ebizzy 0.3, Copyright 2006 Intel Corporation
Written by Val Henson <[email protected]always_mmap 0never_mmap 0
chunks 10
prevent coalescing using permissions 0
prevent coalescing using holes 0
records 1048576
records per thread 65536random_size 0chunk_size 262144
threads 16
verbose 1
linear 0
page size 4096
Allocated memory
Wrote memory
Threads starting
Threads finished
real 5m54.362s user 33m49.707s sys 4m11.100s
ebizzy with 2500 pages, and oprofiled.
ebizzy 0.3, Copyright 2006 Intel Corporation
Written by Val Henson <[email protected]always_mmap 0never_mmap 0
chunks 10
prevent coalescing using permissions 0
prevent coalescing using holes 0
records 1048576
records per thread 65536random_size 0chunk_size 262144
threads 16
verbose 1
linear 0
page size 4096
Allocated memory
Wrote memory
Threads starting
Threads finished
real 6m15.561s user 36m2.203s sys 3m30.829s
Documentation
An article describing the design of the memory controller is available at http://lwn.net/Articles/243795/
series
res_counters_infra.patch
mem-control-setup.patch
mem-control-accounting-setup.patch
mem-control-accounting.patch
mem-control-task-migration.patch
mem-control-lru-and-reclaim.patch
mem-control-out-of-memory.patch
mem-control-choose-rss-vs-rss-and-pagecache.patch
mem-control-per-container-page-referenced.patch
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
PATCH 1/9 Memory controller resource counters (v6)
From: Pavel Emelianov <[email protected]>
Introduce generic structures and routines for resource accounting.
Each resource accounting container is supposed to aggregate it, container_subsystem_state and its resource-specific members within.
---
include/linux/res_counter.h | 102 +++++++++++++++++++++++++++++++++++++ init/Kconfig | 7 ++ kernel/Makefile | 1 kernel/res_counter.c | 120 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 230 insertions(+)
include/linux/res_counter.h new file +102 -0
@@ -0,0 +1,102 @@ +#ifndef __RES_COUNTER_H__ +#define __RES_COUNTER_H__ + +/* +* Resource Counters +* Contain common data types and routines for resource accounting +* +* Copyright 2007 OpenVZ SWsoft Inc +* +* Author: Pavel Emelianov <[email protected]> +* +*/ + +#include <linux/container.h> + +/* +* The core object. the container that wishes to account for some +* resource may include this counter into its structures and use +* the helpers described beyond +*/ + +struct res_counter { +/* +* the current resource consumption level +*/ +unsigned long usage; +/* +* the limit that usage cannot exceed +*/ +unsigned long limit; +/* +* the number of unsuccessful attempts to consume the resource +*/ +unsigned long failcnt; +/* +* the lock to protect all of the above. +* the routines below consider this to be IRQ-safe +*/ +spinlock_t lock; +}; + +/* +* Helpers to interact with userspace +* res_counter_read/_write - put/get the specified fields from the +* res_counter struct to/from the user +* +* @counter: the counter in question +* @member: the field to work with (see RES_xxx below) +* @buf: the buffer to opeate on,... +* @nbytes: its size... +* @pos: and the offset. +*/ + +ssize_t res_counter_read (struct res_counter *counter,int member, +const char __user *buf,size_t nbytes,loff_t *pos); +ssize_t res_counter_write (struct res_counter *counter,int member, +const char __user *buf,size_t nbytes,loff_t *pos); + +/* +* the field descriptors. one for each member of res_counter +*/ + +enum { +RES_USAGE , +RES_LIMIT , +RES_FAILCNT , +}; + +/* +* helpers for accounting +*/ + +void res_counter_init (struct res_counter *counter); + +/* +* charge - try to consume more resource. +* +* @counter: the counter +* @val: the amount of the resource. each controller defines its own +* units, e.g. numbers, bytes, Kbytes, etc +* +* returns 0 on success and <0 if the counter->usage will exceed the +* counter->limit _locked call expects the counter->lock to be taken +*/ + +int res_counter_charge_locked (struct res_counter *counter,unsigned long val); +int res_counter_charge (struct res_counter *counter,unsigned long val); + +/* +* uncharge - tell that some portion of the resource is released +* +* @counter: the counter +* @val: the amount of the resource +* +* these calls check for usage underflow and show a warning on the console +* _locked call expects the counter->lock to be taken +*/ + +void res_counter_uncharge_locked (struct res_counter *counter,unsigned long val); +void res_counter_uncharge (struct res_counter *counter,unsigned long val); + +#endif
init/Kconfig renamed from init/Kconfig~res_counters_infra +7 -0
@@ -337,6 +337,13 @@ config CPUSETS Say N if unsure. +config RESOURCE_COUNTERS + bool "Resource counters" + help + This option enables controller independent resource accounting + infrastructure that works with containers + depends on CONTAINERS + config SYSFS_DEPRECATED bool "Create deprecated sysfs files" default y
kernel/Makefile renamed from kernel/Makefile~res_counters_infra +1 -0
@@ -58,6 +58,7 @@ obj-$(CONFIG_RELAY) += relay.o obj-$(CONFIG_SYSCTL) += utsname_sysctl.o obj-$(CONFIG_TASK_DELAY_ACCT) += delayacct.o obj-$(CONFIG_TASKSTATS) += taskstats.o tsacct.o +obj-$(CONFIG_RESOURCE_COUNTERS) += res_counter.o ifneq ($(CONFIG_SCHED_NO_NO_OMIT_FRAME_POINTER),y) # According to Alan Modra <[email protected]>, the -fno-omit-frame-pointer is
kernel/res_counter.c new file +120 -0
120 lines not on this page · raw message
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
PATCH 2/9 Memory controller containers setup (v6)
In reply to Balbir SinghChangelong
1. use depends instead of select in init/Kconfig
2. Port to v11
3. Clean up the usage of names (container files) for v11
Setup the memory container and add basic hooks and controls to integrate and work with the container.
---
include/linux/container_subsys.h | 6 + include/linux/memcontrol.h | 21 ++++++ init/Kconfig | 7 ++ mm/Makefile | 1 mm/memcontrol.c | 127 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 162 insertions(+)
include/linux/container_subsys.h renamed from include/linux/container_subsys.h~mem-control-setup +6 -0
@@ -30,3 +30,9 @@ SUBSYS(ns)#endif /* */ + +#ifdef CONFIG_CONTAINER_MEM_CONT +SUBSYS(mem_container) +#endif + +/* */
include/linux/memcontrol.h new file +21 -0
@@ -0,0 +1,21 @@ +/* memcontrol.h - Memory Controller +* +* Copyright IBM Corporation, 2007 +* Author Balbir Singh <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#ifndef _LINUX_MEMCONTROL_H +#define _LINUX_MEMCONTROL_H + +#endif /* _LINUX_MEMCONTROL_H */ +
init/Kconfig renamed from init/Kconfig~mem-control-setup +7 -0
@@ -364,6 +364,13 @@ config SYSFS_DEPRECATED If you are using a distro that was released in 2006 or later, it should be safe to say N here. +config CONTAINER_MEM_CONT + bool "Memory controller for containers" + depends on CONTAINERS && RESOURCE_COUNTERS + help + Provides a memory controller that manages both page cache and + RSS memory. + config PROC_PID_CPUSET bool "Include legacy /proc/<pid>/cpuset file" depends on CPUSETS
mm/Makefile renamed from mm/Makefile~mem-control-setup +1 -0
@@ -31,4 +31,5 @@ obj-$(CONFIG_FS_XIP) += filemap_xip.o obj-$(CONFIG_MIGRATION) += migrate.o obj-$(CONFIG_SMP) += allocpercpu.o obj-$(CONFIG_QUICKLIST) += quicklist.o +obj-$(CONFIG_CONTAINER_MEM_CONT) += memcontrol.o
mm/memcontrol.c new file +127 -0
127 lines not on this page · raw message
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
PATCH 3/9 Memory controller accounting setup (v6)
In reply to Balbir SinghFrom: Pavel Emelianov <[email protected]>
Changelog for v5
1. Remove inclusion of memcontrol.h from mm_types.h
Changelog
As per Paul's review comments
1. Drop css_get() for the root memory container
2. Use mem_container_from_task() as an optimization instead of using mem_container_from_cont() along with task_container.
Basic setup routines, the mm_struct has a pointer to the container that it belongs to and the the page has a page_container associated with it.
---
include/linux/memcontrol.h | 36 ++++++++++++++++++++++++++++ include/linux/mm_types.h | 6 ++++ include/linux/sched.h | 1 kernel/fork.c | 11 ++++++-- mm/memcontrol.c | 57 +++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 104 insertions(+), 7 deletions(-)
include/linux/memcontrol.h renamed from include/linux/memcontrol.h~mem-control-accounting-setup +36 -0
@@ -3,6 +3,9 @@* Copyright IBM Corporation, 2007 * Author Balbir Singh <[email protected]> * +* Copyright 2007 OpenVZ SWsoft Inc +* Author: Pavel Emelianov <[email protected]> +* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or
@@ -17,5 +20,38 @@#ifndef _LINUX_MEMCONTROL_H#define _LINUX_MEMCONTROL_H +struct mem_container ; +struct page_container ; + +#ifdef CONFIG_CONTAINER_MEM_CONT + +extern void mm_init_container (struct mm_struct *mm,struct task_struct *p); +extern void mm_free_container (struct mm_struct *mm); +extern void page_assign_page_container (struct page *page, +struct page_container *pc); +extern struct page_container *page_get_page_container (struct page *page); + +#else /* CONFIG_CONTAINER_MEM_CONT */ +static inline void mm_init_container (struct mm_struct *mm, +struct task_struct *p) +{ +} + +static inline void mm_free_container (struct mm_struct *mm) +{ +} + +static inline void page_assign_page_container (struct page *page, +struct page_container *pc) +{ +} + +static inline struct page_container *page_get_page_container (struct page *page) +{ +return NULL ; +} + +#endif /* CONFIG_CONTAINER_MEM_CONT */ +#endif /* _LINUX_MEMCONTROL_H */
include/linux/mm_types.h renamed from include/linux/mm_types.h~mem-control-accounting-setup +6 -0
@@ -95,6 +95,9 @@ struct page {unsigned int gfp_mask;unsigned long trace[8 ];#endif +#ifdef CONFIG_CONTAINER_MEM_CONT +unsigned long page_container; +#endif };/*
@@ -226,6 +229,9 @@ struct mm_struct {/* aio bits */ rwlock_t ioctx_list_lock;struct kioctx *ioctx_list; +#ifdef CONFIG_CONTAINER_MEM_CONT +struct mem_container *mem_container; +#endif };#endif /* _LINUX_MM_TYPES_H */
include/linux/sched.h renamed from include/linux/sched.h~mem-control-accounting-setup +1 -0
@@ -87,6 +87,7 @@ struct sched_param {#include <asm/processor.h> +struct mem_container ;struct exec_domain ;struct futex_pi_state ;struct bio ;
kernel/fork.c renamed from kernel/fork.c~mem-control-accounting-setup +8 -3
@@ -50,6 +50,7 @@#include <linux/taskstats_kern.h> #include <linux/random.h> #include <linux/tty.h> +#include <linux/memcontrol.h> #include <asm/pgtable.h> #include <asm/pgalloc.h>
@@ -328,7 +329,7 @@ __cacheline_aligned_in_smp DEFINE_SPINLO#include <linux/init_task.h> -static struct mm_struct *mm_init (struct mm_struct * mm) +static struct mm_struct *mm_init (struct mm_struct * mm,struct task_struct *p) {atomic_set (&mm->mm_users,1 );atomic_set (&mm->mm_count,1 );
@@ -345,11 +346,14 @@ static struct mm_struct * mm_init(struct mm->ioctx_list =NULL ; mm->free_area_cache =TASK_UNMAPPED_BASE ; mm->cached_hole_size = ~0UL ; +mm_init_container (mm, p);if (likely (!mm_alloc_pgd (mm))) { mm->def_flags =0 ;return mm; } + +mm_free_container (mm);free_mm (mm);return NULL ; }
@@ -364,7 +368,7 @@ struct mm_struct * mm_alloc(void) mm =allocate_mm ();if (mm) {memset (mm,0 ,sizeof (*mm)); - mm =mm_init (mm); + mm =mm_init (mm, current); }return mm; }
@@ -378,6 +382,7 @@ void fastcall __mmdrop(struct mm_struct {BUG_ON (mm == &init_mm);mm_free_pgd (mm); +mm_free_container (mm);destroy_context (mm);free_mm (mm); }
@@ -498,7 +503,7 @@ static struct mm_struct *dup_mm(struct t mm->token_priority =0 ; mm->last_interval =0 ; -if (!mm_init (mm)) +if (!mm_init (mm, tsk)) goto fail_nomem;if (init_new_context (tsk, mm))
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-accounting-setup +53 -4
96 lines not on this page · raw message
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
PATCH 4/9 Memory controller memory accounting (v6)
In reply to Balbir SinghChangelog for v6
1. Do a css_put() in the case of a race in allocating page containers (YAMAMOTO Takashi)
Changelog for v5
1. Rename meta_page to page_container
2. Remove PG_metapage and use the lower bit of the page_container pointer for locking
Changelog for v3
1. Fix a probable leak with meta_page's (pointed out by Paul Menage)
2. Introduce a wrapper around mem_container_uncharge for uncharging pages mem_container_uncharge_page()
Changelog
1. Improved error handling, uncharge on errors and check to see if we are leaking pages (review by YAMAMOTO Takashi)
Add the accounting hooks. The accounting is carried out for RSS and Page Cache (unmapped) pages. There is now a common limit and accounting for both. The RSS accounting is accounted at page_add_*_rmap() and page_remove_rmap() time. Page cache is accounted at add_to_page_cache(), __delete_from_page_cache(). Swap cache is also accounted for.
Each page's page_container is protected with the last bit of the page_container pointer, this makes handling of race conditions involving simultaneous mappings of a page easier. A reference count is kept in the page_container to deal with cases where a page might be unmapped from the RSS of all tasks, but still lives in the page cache.
Credits go to Vaidyanathan Srinivasan for helping with reference counting work of the page container. Almost all of the page cache accounting code has help from Vaidyanathan Srinivasan.
---
include/linux/memcontrol.h | 20 +++++ mm/filemap.c | 12 ++- mm/memcontrol.c | 166 ++++++++++++++++++++++++++++++++++++++++++++- mm/memory.c | 43 ++++++++++- mm/migrate.c | 6 + mm/page_alloc.c | 3 mm/rmap.c | 17 ++++ mm/swap_state.c | 12 ++- mm/swapfile.c | 41 ++++++----- 9 files changed, 293 insertions(+), 27 deletions(-)
include/linux/memcontrol.h renamed from include/linux/memcontrol.h~mem-control-accounting +20 -0
@@ -30,6 +30,13 @@ extern void mm_free_container(struct mm_extern void page_assign_page_container (struct page *page,struct page_container *pc);extern struct page_container *page_get_page_container (struct page *page); +extern int mem_container_charge (struct page *page,struct mm_struct *mm); +extern void mem_container_uncharge (struct page_container *pc); + +static inline void mem_container_uncharge_page (struct page *page) +{ +mem_container_uncharge (page_get_page_container (page)); +}#else /* CONFIG_CONTAINER_MEM_CONT */ static inline void mm_init_container(struct mm_struct *mm,
@@ -51,6 +58,19 @@ static inline struct page_container *pagreturn NULL ; } +static inline int mem_container_charge (struct page *page,struct mm_struct *mm) +{ +return 0 ; +} + +static inline void mem_container_uncharge (struct page_container *pc) +{ +} + +static inline void mem_container_uncharge_page (struct page *page) +{ +} +#endif /* CONFIG_CONTAINER_MEM_CONT */ #endif /* _LINUX_MEMCONTROL_H */
diff -puN include/linux/page-flags.h~mem-control-accounting include/linux/page-flags.h
mm/filemap.c renamed from mm/filemap.c~mem-control-accounting +11 -1
@@ -31,6 +31,7 @@#include <linux/syscalls.h> #include <linux/cpuset.h> #include <linux/hardirq.h> /* for BUG_ON(!in_atomic()) only */ +#include <linux/memcontrol.h> #include "internal.h" /*
@@ -116,6 +117,7 @@ void __remove_from_page_cache(struct pag {struct address_space *mapping = page->mapping; +mem_container_uncharge_page (page);radix_tree_delete (&mapping->page_tree, page->index); page->mapping =NULL ; mapping->nrpages--;
@@ -442,6 +444,11 @@ int add_to_page_cache(struct page *page,int error =radix_tree_preload (gfp_mask & ~__GFP_HIGHMEM);if (error ==0 ) { + + error =mem_container_charge (page, current->mm); +if (error) + goto out; +write_lock_irq (&mapping->tree_lock); error =radix_tree_insert (&mapping->page_tree, offset, page);if (!error) {
@@ -451,10 +458,13 @@ int add_to_page_cache(struct page *page, page->index = offset; mapping->nrpages++;__inc_zone_page_state (page,NR_FILE_PAGES ); - } + }else +mem_container_uncharge_page (page); +write_unlock_irq (&mapping->tree_lock);radix_tree_preload_end (); } +out:return error; }EXPORT_SYMBOL (add_to_page_cache);
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-accounting +163 -3
208 lines not on this page · raw message
mm/memory.c renamed from mm/memory.c~mem-control-accounting +39 -4
132 lines not on this page · raw message
mm/migrate.c renamed from mm/migrate.c~mem-control-accounting +6 -0
@@ -28,6 +28,7 @@#include <linux/mempolicy.h> #include <linux/vmalloc.h> #include <linux/security.h> +#include <linux/memcontrol.h> #include "internal.h"
@@ -156,6 +157,11 @@ static void remove_migration_pte(structreturn ; } +if (mem_container_charge (new, mm)) { +pte_unmap (ptep); +return ; + } + ptl =pte_lockptr (mm, pmd);spin_lock (ptl); pte = *ptep;
mm/page_alloc.c renamed from mm/page_alloc.c~mem-control-accounting +3 -0
@@ -42,6 +42,7 @@#include <linux/backing-dev.h> #include <linux/fault-inject.h> #include <linux/page-isolation.h> +#include <linux/memcontrol.h> #include <asm/tlbflush.h> #include <asm/div64.h>
@@ -1018,6 +1019,7 @@ static void fastcall free_hot_cold_page(if (!PageHighMem (page))debug_check_no_locks_freed (page_address (page),PAGE_SIZE ); +page_assign_page_container (page,NULL );arch_free_page (page,0 );kernel_map_pages (page,1 ,0 );
@@ -2637,6 +2639,7 @@ void __meminit memmap_init_zone(unsignedset_page_links (page, zone, nid, pfn);init_page_count (page);reset_page_mapcount (page); +page_assign_page_container (page,NULL );SetPageReserved (page);/*
mm/rmap.c renamed from mm/rmap.c~mem-control-accounting +16 -1
@@ -48,6 +48,7 @@#include <linux/rcupdate.h> #include <linux/module.h> #include <linux/kallsyms.h> +#include <linux/memcontrol.h> #include <asm/tlbflush.h>
@@ -550,8 +551,14 @@ void page_add_anon_rmap(struct page *pagVM_BUG_ON (address < vma->vm_start || address >= vma->vm_end);if (atomic_inc_and_test (&page->_mapcount))__page_set_anon_rmap (page, vma, address); -else +else {__page_check_anon_rmap (page, vma, address); +/* +* We unconditionally charged during prepare, we uncharge here +* This takes care of balancing the reference counts +*/ +mem_container_uncharge_page (page); + } }/*
@@ -582,6 +589,12 @@ void page_add_file_rmap(struct page *pag {if (atomic_inc_and_test (&page->_mapcount))__inc_zone_page_state (page,NR_FILE_MAPPED ); +else +/* +* We unconditionally charged during prepare, we uncharge here +* This takes care of balancing the reference counts +*/ +mem_container_uncharge_page (page); }#ifdef CONFIG_DEBUG_VM
@@ -642,6 +655,8 @@ void page_remove_rmap(struct page *page,page_clear_dirty (page);set_page_dirty (page); } +mem_container_uncharge_page (page); +__dec_zone_page_state (page,PageAnon (page) ?NR_ANON_PAGES :NR_FILE_MAPPED ); }
mm/swapfile.c renamed from mm/swapfile.c~mem-control-accounting +24 -17
128 lines not on this page · raw message
mm/swap_state.c renamed from mm/swap_state.c~mem-control-accounting +11 -1
@@ -17,6 +17,7 @@#include <linux/backing-dev.h> #include <linux/pagevec.h> #include <linux/migrate.h> +#include <linux/memcontrol.h> #include <asm/pgtable.h>
@@ -80,6 +81,11 @@ static int __add_to_swap_cache(struct paBUG_ON (PagePrivate (page)); error =radix_tree_preload (gfp_mask);if (!error) { + + error =mem_container_charge (page, current->mm); +if (error) + goto out; +write_lock_irq (&swapper_space.tree_lock); error =radix_tree_insert (&swapper_space.page_tree, entry.val, page);
@@ -89,10 +95,13 @@ static int __add_to_swap_cache(struct paset_page_private (page, entry.val); total_swapcache_pages++;__inc_zone_page_state (page,NR_FILE_PAGES ); - } + }else +mem_container_uncharge_page (page); +write_unlock_irq (&swapper_space.tree_lock);radix_tree_preload_end (); } +out:return error; }
@@ -132,6 +141,7 @@ void __delete_from_swap_cache(struct pagBUG_ON (PageWriteback (page));BUG_ON (PagePrivate (page)); +mem_container_uncharge_page (page);radix_tree_delete (&swapper_space.page_tree,page_private (page));set_page_private (page,0 );ClearPageSwapCache (page);
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
PATCH 5/9 Memory controller task migration (v6)
In reply to Balbir SinghAllow tasks to migrate from one container to the other. We migrate mm_struct's mem_container only when the thread group id migrates.
---
mm/memcontrol.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-task-migration +35 -0
@@ -326,11 +326,46 @@ static int mem_container_populate(struct ARRAY_SIZE(mem_container_files)); } +static void mem_container_move_task (struct container_subsys *ss, +struct container *cont, +struct container *old_cont, +struct task_struct *p) +{ +struct mm_struct *mm; +struct mem_container *mem, *old_mem; + + mm =get_task_mm (p); +if (mm ==NULL ) +return ; + + mem =mem_container_from_cont (cont); + old_mem =mem_container_from_cont (old_cont); + +if (mem == old_mem) + goto out; + +/* +* Only thread group leaders are allowed to migrate, the mm_struct is +* in effect owned by the leader +*/ +if (p->tgid != p->pid) + goto out; + +css_get (&mem->css); +rcu_assign_pointer (mm->mem_container, mem); +css_put (&old_mem->css); + +out: +mmput (mm); +return ; +} +struct container_subsys mem_container_subsys = { .name ="memory" , .subsys_id = mem_container_subsys_id, .create = mem_container_create, .destroy = mem_container_destroy, .populate = mem_container_populate, + .attach = mem_container_move_task, .early_init =1 , };
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
PATCH 6/9 Memory controller add per container LRU and reclaim (v6)
In reply to Balbir SinghChangelog since v3
1. Added reclaim retry logic to avoid being OOM'ed due to pages from swap cache (coming in due to reclaim) don't overwhelm the container.
Changelog
1. Fix probable NULL pointer dereference based on review comments by YAMAMOTO Takashi
Add the page_container to the per container LRU. The reclaim algorithm has been modified to make the isolate_lru_pages() as a pluggable component. The scan_control data structure now accepts the container on behalf of which reclaims are carried out. try_to_free_pages() has been extended to become container aware.
---
include/linux/memcontrol.h | 12 +++ include/linux/res_counter.h | 23 +++++++ include/linux/swap.h | 3 mm/memcontrol.c | 135 +++++++++++++++++++++++++++++++++++++++++++- mm/swap.c | 2 mm/vmscan.c | 126 ++++++++++++++++++++++++++++++++--------- 6 files changed, 275 insertions(+), 26 deletions(-)
include/linux/memcontrol.h renamed from include/linux/memcontrol.h~mem-control-lru-and-reclaim +12 -0
@@ -32,6 +32,13 @@ extern void page_assign_page_container(sextern struct page_container *page_get_page_container (struct page *page);extern int mem_container_charge (struct page *page,struct mm_struct *mm);extern void mem_container_uncharge (struct page_container *pc); +extern void mem_container_move_lists (struct page_container *pc,bool active); +extern unsigned long mem_container_isolate_pages (unsigned long nr_to_scan, +struct list_head *dst, +unsigned long *scanned,int order, +int mode,struct zone *z, +struct mem_container *mem_cont, +int active);static inline void mem_container_uncharge_page (struct page *page) {
@@ -71,6 +78,11 @@ static inline void mem_container_uncharg { } +static inline void mem_container_move_lists (struct page_container *pc, +bool active) +{ +} +#endif /* CONFIG_CONTAINER_MEM_CONT */ #endif /* _LINUX_MEMCONTROL_H */
include/linux/res_counter.h renamed from include/linux/res_counter.h~mem-control-lru-and-reclaim +23 -0
@@ -99,4 +99,27 @@ int res_counter_charge(struct res_countevoid res_counter_uncharge_locked (struct res_counter *counter,unsigned long val);void res_counter_uncharge (struct res_counter *counter,unsigned long val); +static inline bool res_counter_limit_check_locked (struct res_counter *cnt) +{ +if (cnt->usage < cnt->limit) +return true; + +return false; +} + +/* +* Helper function to detect if the container is within it's limit or +* not. It's currently called from container_rss_prepare() +*/ +static inline bool res_counter_check_under_limit (struct res_counter *cnt) +{ +bool ret; +unsigned long flags; + +spin_lock_irqsave (&cnt->lock, flags); + ret =res_counter_limit_check_locked (cnt); +spin_unlock_irqrestore (&cnt->lock, flags); +return ret; +} +#endif
include/linux/swap.h renamed from include/linux/swap.h~mem-control-lru-and-reclaim +3 -0
@@ -6,6 +6,7 @@#include <linux/mmzone.h> #include <linux/list.h> #include <linux/sched.h> +#include <linux/memcontrol.h> #include <asm/atomic.h> #include <asm/page.h>
@@ -191,6 +192,8 @@ extern void swap_setup(void);/* linux/mm/vmscan.c */ extern unsigned long try_to_free_pages (struct zone **zones,int order,gfp_t gfp_mask); +extern unsigned long try_to_free_mem_container_pages (struct mem_container *mem); +extern int __isolate_lru_page (struct page *page,int mode);extern unsigned long shrink_all_memory (unsigned long nr_pages);extern int vm_swappiness;extern int remove_mapping (struct address_space *mapping,struct page *page);
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-lru-and-reclaim +134 -1
191 lines not on this page · raw message
mm/swap.c renamed from mm/swap.c~mem-control-lru-and-reclaim +2 -0
@@ -29,6 +29,7 @@#include <linux/percpu.h> #include <linux/cpu.h> #include <linux/notifier.h> +#include <linux/memcontrol.h> /* How many pages do we try to swap or page in/out together? */ int page_cluster;
@@ -146,6 +147,7 @@ void fastcall activate_page(struct pageSetPageActive (page);add_page_to_active_list (zone, page);__count_vm_event (PGACTIVATE ); +mem_container_move_lists (page_get_page_container (page), true); }spin_unlock_irq (&zone->lru_lock); }
mm/vmscan.c renamed from mm/vmscan.c~mem-control-lru-and-reclaim +101 -25
225 lines not on this page · raw message
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
PATCH 7/9 Memory controller OOM handling (v6)
In reply to Balbir SinghFrom: Pavel Emelianov <[email protected]>
Out of memory handling for containers over their limit. A task from the container over limit is chosen using the existing OOM logic and killed.
TODO:
1. As discussed in the OLS BOF session, consider implementing a user space policy for OOM handling.
---
include/linux/memcontrol.h | 1 + mm/memcontrol.c | 1 + mm/oom_kill.c | 42 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 4 deletions(-)
include/linux/memcontrol.h renamed from include/linux/memcontrol.h~mem-control-out-of-memory +1 -0
@@ -39,6 +39,7 @@ extern unsigned long mem_container_isola int mode, struct zone *z, struct mem_container *mem_cont, int active); +extern void mem_container_out_of_memory (struct mem_container *mem);static inline void mem_container_uncharge_page (struct page *page) {
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-out-of-memory +1 -0
@@ -322,6 +322,7 @@ int mem_container_charge(struct page *pa }css_put (&mem->css); +mem_container_out_of_memory (mem); goto free_pc; }
mm/oom_kill.c renamed from mm/oom_kill.c~mem-control-out-of-memory +38 -4
84 lines not on this page · raw message
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
PATCH 8/9 Memory controller add switch to control what type of pages to limit (v6)
In reply to Balbir SinghChoose if we want cached pages to be accounted or not. By default both are accounted for. A new set of tunables are added.
echo -n 1 > mem_control_type
switches the accounting to account for only mapped pages
echo -n 3 > mem_control_type
switches the behaviour back
---
include/linux/memcontrol.h | 9 ++++ mm/filemap.c | 2 mm/memcontrol.c | 92 +++++++++++++++++++++++++++++++++++++++++++++ mm/swap_state.c | 2 4 files changed, 103 insertions(+), 2 deletions(-)
include/linux/memcontrol.h renamed from include/linux/memcontrol.h~mem-control-choose-rss-vs-rss-and-pagecache +9 -0
@@ -20,6 +20,8 @@#ifndef _LINUX_MEMCONTROL_H#define _LINUX_MEMCONTROL_H +#include <linux/mm.h> +struct mem_container ;struct page_container ;
@@ -40,6 +42,7 @@ extern unsigned long mem_container_isolastruct mem_container *mem_cont, int active);extern void mem_container_out_of_memory (struct mem_container *mem); +extern int mem_container_cache_charge (struct page *page,struct mm_struct *mm);static inline void mem_container_uncharge_page (struct page *page) {
@@ -84,6 +87,12 @@ static inline void mem_container_move_li { } +static inline int mem_container_cache_charge (struct page *page, +struct mm_struct *mm) +{ +return 0 ; +} +#endif /* CONFIG_CONTAINER_MEM_CONT */ #endif /* _LINUX_MEMCONTROL_H */
mm/filemap.c renamed from mm/filemap.c~mem-control-choose-rss-vs-rss-and-pagecache +1 -1
@@ -445,7 +445,7 @@ int add_to_page_cache(struct page *page,if (error ==0 ) { - error =mem_container_charge (page, current->mm); + error =mem_container_cache_charge (page, current->mm);if (error) goto out;
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-choose-rss-vs-rss-and-pagecache +92 -0
140 lines not on this page · raw message
mm/swap_state.c renamed from mm/swap_state.c~mem-control-choose-rss-vs-rss-and-pagecache +1 -1
@@ -82,7 +82,7 @@ static int __add_to_swap_cache(struct pa error =radix_tree_preload (gfp_mask);if (!error) { - error =mem_container_charge (page, current->mm); + error =mem_container_cache_charge (page, current->mm);if (error) goto out;
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
PATCH 9/9 Memory controller make page_referenced() container aware (v6)
In reply to Balbir SinghMake page_referenced() container aware. Without this patch, page_referenced() can cause a page to be skipped while reclaiming pages. This patch ensures that other containers do not hold pages in a particular container hostage. It is required to ensure that shared pages are freed from a container when they are not actively referenced from the container that brought them in
---
include/linux/memcontrol.h | 6 ++++++ include/linux/rmap.h | 5 +++-- mm/memcontrol.c | 5 +++++ mm/rmap.c | 30 ++++++++++++++++++++++++------ mm/vmscan.c | 4 ++-- 5 files changed, 40 insertions(+), 10 deletions(-)
include/linux/rmap.h renamed from include/linux/rmap.h~mem-control-per-container-page-referenced +3 -2
@@ -8,6 +8,7 @@#include <linux/slab.h> #include <linux/mm.h> #include <linux/spinlock.h> +#include <linux/memcontrol.h> /* * The anon_vma heads a list of private "related" vmas, to scan if
@@ -86,7 +87,7 @@ 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 page_referenced (struct page *,int is_locked,struct mem_container *cnt);int try_to_unmap (struct page *,int ignore_refs);/*
@@ -114,7 +115,7 @@ int page_mkclean(struct page *);#define anon_vma_prepare (vma) (0)#define anon_vma_link (vma) do {} while (0) -#define page_referenced (page,l) TestClearPageReferenced(page) +#define page_referenced (page,l,cnt) TestClearPageReferenced(page)#define try_to_unmap (page, refs) SWAP_FAILstatic inline int page_mkclean (struct page *page)
mm/rmap.c renamed from mm/rmap.c~mem-control-per-container-page-referenced +24 -6
@@ -299,7 +299,8 @@ out:return referenced; } -static int page_referenced_anon (struct page *page) +static int page_referenced_anon (struct page *page, +struct mem_container *mem_cont) {unsigned int mapcount;struct anon_vma *anon_vma;
@@ -312,6 +313,13 @@ static int page_referenced_anon(struct p mapcount =page_mapcount (page);list_for_each_entry (vma, &anon_vma->head, anon_vma_node) { +/* +* If we are reclaiming on behalf of a container, skip +* counting on behalf of references from different +* containers +*/ +if (mem_cont && (mm_container (vma->vm_mm) != mem_cont)) +continue ; referenced +=page_referenced_one (page, vma, &mapcount);if (!mapcount)break ;
@@ -332,7 +340,8 @@ static int page_referenced_anon(struct p* * This function is only called from page_referenced for object-based pages. */ -static int page_referenced_file (struct page *page) +static int page_referenced_file (struct page *page, +struct mem_container *mem_cont) {unsigned int mapcount;struct address_space *mapping = page->mapping;
@@ -365,6 +374,13 @@ static int page_referenced_file(struct p mapcount =page_mapcount (page); vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { +/* +* If we are reclaiming on behalf of a container, skip +* counting on behalf of references from different +* containers +*/ +if (mem_cont && (mm_container (vma->vm_mm) != mem_cont)) +continue ;if ((vma->vm_flags & (VM_LOCKED |VM_MAYSHARE )) == (VM_LOCKED |VM_MAYSHARE )) { referenced++;
@@ -387,7 +403,8 @@ static int page_referenced_file(struct p* Quick test_and_clear_referenced for all mappings to a page, * returns the number of ptes which referenced the page. */ -int page_referenced (struct page *page,int is_locked) +int page_referenced (struct page *page,int is_locked, +struct mem_container *mem_cont) {int referenced =0 ;
@@ -399,14 +416,15 @@ int page_referenced(struct page *page, iif (page_mapped (page) && page->mapping) {if (PageAnon (page)) - referenced +=page_referenced_anon (page); + referenced +=page_referenced_anon (page, mem_cont);else if (is_locked) - referenced +=page_referenced_file (page); + referenced +=page_referenced_file (page, mem_cont);else if (TestSetPageLocked (page)) referenced++;else {if (page->mapping) - referenced +=page_referenced_file (page); + referenced += +page_referenced_file (page, mem_cont);unlock_page (page); } }
mm/vmscan.c renamed from mm/vmscan.c~mem-control-per-container-page-referenced +2 -2
@@ -511,7 +511,7 @@ static unsigned long shrink_page_list(st goto keep_locked; } - referenced =page_referenced (page,1 ); + referenced =page_referenced (page,1 , sc->mem_container);/* In active use or really unfreeable? Activate it. */ if (sc->order <=PAGE_ALLOC_COSTLY_ORDER && referenced &&page_mapping_inuse (page))
@@ -1065,7 +1065,7 @@ force_reclaim_mapped:if (page_mapped (page)) {if (!reclaim_mapped || (total_swap_pages ==0 &&PageAnon (page)) || -page_referenced (page,0 )) { +page_referenced (page,0 , sc->mem_container)) {list_add (&page->lru, &l_active);continue ; }
include/linux/memcontrol.h renamed from include/linux/memcontrol.h~mem-control-per-container-page-referenced +6 -0
@@ -43,6 +43,7 @@ extern unsigned long mem_container_isola int active);extern void mem_container_out_of_memory (struct mem_container *mem);extern int mem_container_cache_charge (struct page *page,struct mm_struct *mm); +extern struct mem_container *mm_container (struct mm_struct *mm);static inline void mem_container_uncharge_page (struct page *page) {
@@ -93,6 +94,11 @@ static inline int mem_container_cache_chreturn 0 ; } +static inline struct mem_container *mm_container (struct mm_struct *mm) +{ +return NULL ; +} +#endif /* CONFIG_CONTAINER_MEM_CONT */ #endif /* _LINUX_MEMCONTROL_H */
mm/memcontrol.c renamed from mm/memcontrol.c~mem-control-per-container-page-referenced +5 -0
@@ -109,6 +109,11 @@ struct mem_container *mem_container_from struct mem_container, css); } +inline struct mem_container *mm_container (struct mm_struct *mm) +{ +return rcu_dereference (mm->mem_container); +} +void mm_init_container (struct mm_struct *mm,struct task_struct *p) {struct mem_container *mem;
_
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL
On Fri, Aug 17, 2007 at 02:12:28PM +0530, Balbir Singh wrote:
Hi Andrew,
Quoted 7 lines
The code was also tested on a power box with regular machine usage scenarios, the config disabled and with a stress suite that touched all the memory in the system and was limited in a container.
Dhaval ran several tests on v6 and gave his thumbs up to the controller (a hard to achieve goal :-) ).
I've been running v6 on x86 and there are a few things I've tried. Most of those results Balbir has already posted.
Quoted 4 lines
Run kernbench stress
--------------------
Three simultaneously and with one inside a container of 800 MB.
The idea here was to create pressure inside the container while having global pressure.
Quoted 58 lines
Kernbench results running within the container of 800 MB
--------------------------------------------------------Thu Aug 16 22:34:59 IST 2007
2.6.23-rc2-mm2-mem-v6
Average Half load -j 4 Run (std deviation):
Elapsed Time 466.548 (47.6014)
User Time 876.598 (10.5273)
System Time 223.136 (1.29247)
Percent CPU 237.2 (23.2744)
Context Switches 146351 (6539.91)
Sleeps 174003 (5031.94)Average Optimal load -j 32 Run (std deviation):
Elapsed Time 423.496 (60.625)
User Time 897.285 (23.0391)
System Time 228.836 (6.11205)
Percent CPU 257.1 (40.9022)
Context Switches 262134 (123397)
Sleeps 270815 (103597)Kernbench results running within the default container
------------------------------------------------------Thu Aug 16 22:34:33 IST 2007
2.6.23-rc2-mm2-mem-v6
Average Half load -j 4 Run (std deviation):
Elapsed Time 424.17 (3.45908)
User Time 841.992 (5.40178)
System Time 213.01 (0.706258)
Percent CPU 248.2 (0.83666)
Context Switches 134254 (9535.83)
Sleeps 167359 (6858.45)Average Optimal load -j 32 Run (std deviation):
Elapsed Time 407.092 (108.932)
User Time 878.493 (38.6575)
System Time 222.155 (9.77127)
Percent CPU 278.4 (91.5826)
Context Switches 253836 (127708)
Sleeps 263760 (103468)Kernbench results running within the default container
------------------------------------------------------Thu Aug 16 22:34:52 IST 2007
2.6.23-rc2-mm2-mem-v6
Average Half load -j 4 Run (std deviation):
Elapsed Time 465.038 (48.5147)
User Time 874.742 (5.86563)
System Time 222.194 (0.561676)
Percent CPU 237.2 (22.5211)
Context Switches 144040 (7052.23)
Sleeps 172130 (5608.73)
Quoted 8 lines
Average Optimal load -j 32 Run (std deviation):
Elapsed Time 426.25 (62.34)
User Time 893.938 (20.6732)
System Time 227.717 (5.87502)
Percent CPU 255.6 (40.7163)
Context Switches 259560 (122953)
Sleeps 267402 (101801)
I've been running v6 for sometime and it has been stable (still not been able to break/crash it, whereas v4 was broken within 1 hour :-) )
Andrew, I feel these patches whould be included in mm so that it gets wider testing.
Signature · 4 lines
regards, Dhaval I would like to change the world but they don't give me the source code!
Re: [Devel] [-mm PATCH 1/9] Memory controller resource counters (v6)
In reply to Balbir Singhkernel/res_counter.c line 19
Quoted 6 lines · Balbir Singh
kernel/res_counter.c new file +4
-0+void res_counter_init (struct res_counter *counter) +{ +spin_lock_init (&counter->lock); + counter->limit = (unsigned long )LONG_MAX ;
why cast?
kernel/res_counter.c line 24
Quoted 3 lines
kernel/res_counter.c new file +3
-0+int res_counter_charge_locked (struct res_counter *counter,unsigned long val) +{ +if (counter->usage > (counter->limit - val)) {
() aren't needed.
Quoted 13 lines
kernel/res_counter.c new file +13
-0+ counter->failcnt++; +return -ENOMEM ; + } + + counter->usage += val; +return 0 ; +} + +int res_counter_charge (struct res_counter *counter,unsigned long val) +{ +int ret; +unsigned long flags; +
kernel/res_counter.c line 47
Quoted 10 lines
kernel/res_counter.c new file +10
-0+spin_lock_irqsave (&counter->lock, flags); + ret =res_counter_charge_locked (counter, val); +spin_unlock_irqrestore (&counter->lock, flags); +return ret; +} + +void res_counter_uncharge_locked (struct res_counter *counter,unsigned long val) +{ +if (WARN_ON (counter->usage < val)) + val = counter->usage;
explicit if and WARN_ON(1) is clearer. I should send a patch banning such type of usage soon.
Quoted 11 lines
kernel/res_counter.c new file +11
-0+ + counter->usage -= val; +} + +void res_counter_uncharge (struct res_counter *counter,unsigned long val) +{ +unsigned long flags; + +spin_lock_irqsave (&counter->lock, flags); +res_counter_uncharge_locked (counter, val); +spin_unlock_irqrestore (&counter->lock, flags);
kernel/res_counter.c line 98
Quoted 10 lines
kernel/res_counter.c new file +9
-0+} +ssize_t res_counter_write (struct res_counter *counter,int member, +const char __user *userbuf,size_t nbytes,loff_t *pos) +{ +int ret; +char *buf, *end; +unsigned long tmp, *val; + + buf =kmalloc (nbytes +1 ,GFP_KERNEL );
please, switch to fixed buffer, allocating memory depending on size told by userspace will beat later. Ditto for other proc writing functions.
Quoted 22 lines
kernel/res_counter.c new file +22
-0+ ret = -ENOMEM ; +if (buf ==NULL ) + goto out; + + buf[nbytes] ='\0' ; + ret = -EFAULT ; +if (copy_from_user (buf, userbuf, nbytes)) + goto out_free; + + ret = -EINVAL ; + tmp =simple_strtoul (buf, &end,10 ); +if (*end !='\0' ) + goto out_free; + + val =res_counter_member (counter, member); + *val = tmp; + ret = nbytes; +out_free: +kfree (buf); +out: +return ret; +}
Re: [Devel] [-mm PATCH 1/9] Memory controller resource counters (v6)
Quoted 10 lines · Alexey Dobriyan
kernel/res_counter.c line 19
Quoted 6 lines · Balbir Singh
kernel/res_counter.c new file +4
-0+void res_counter_init (struct res_counter *counter) +{ +spin_lock_init (&counter->lock); + counter->limit = (unsigned long )LONG_MAX ;why cast?
These patches come from Pavel. They add to readability since limit is unsigned long.
Quoted 6 lines
kernel/res_counter.c line 24
Quoted 3 lines
kernel/res_counter.c new file +3
-0+int res_counter_charge_locked (struct res_counter *counter,unsigned long val) +{ +if (counter->usage > (counter->limit - val)) {() aren't needed.
it makes the code more readable
Quoted 6 lines
kernel/res_counter.c line 47
Quoted 2 lines
kernel/res_counter.c new file +2
-0+if (WARN_ON (counter->usage < val)) + val = counter->usage;explicit if and
WARN_ON(1)is clearer. I should send a patch banning such type of usage soon.
We had a WARN_ON(1) before, but we changed it in v2 or v3 based on review comments from Dave. I think WARN_ON(cond) is more readable than WARN_ON(1) for the same reason as BUG_ON(cond) vs BUG_ON(1)
Quoted 6 lines
kernel/res_counter.c line 98
Quoted 1 line
kernel/res_counter.c new file +1
-0+ buf =kmalloc (nbytes +1 ,GFP_KERNEL );please, switch to fixed buffer, allocating memory depending on size told by userspace will beat later. Ditto for other proc writing functions.
I agree with you in part, but the size of user input is not fixed. Setting a fixed limit seems artificial, I'll see how this can be improved.
Thanks for the detailed review comments,
Signature · 3 lines
Balbir Singh Linux Technology Center IBM, ISTL
Re: [-mm PATCH 1/9] Memory controller resource counters (v6)
In reply to Balbir SinghHi Balbir/Pavel,
As I mentioned to you directly at the kernel summit, I think it might be cleaner to integrate resource counters more closely with control groups. So rather than controllers such as the memory controller having to create their own boilerplate cf_type structures and read/write functions, it should be possible to just call a function something like
control_group_add_rescounter(struct cgroup *cg, struct cgroup_subsys *ss,
struct res_counter *res,
const char *name)
and have it handle all the userspace API. This would simplify the task of keeping a consistent userspace API between different controllers using the resource counter abstraction.
Paul
Quoted 289 lines · Balbir Singh
From: Pavel Emelianov <[email protected]>
Introduce generic structures and routines for resource accounting.
Each resource accounting container is supposed to aggregate it,
container_subsystem_stateand its resource-specific members within.Signed-off-by Pavel Emelianov <[email protected]>Signed-off-by <[email protected]>---
include/linux/res_counter.h | 102 +++++++++++++++++++++++++++++++++++++ init/Kconfig | 7 ++ kernel/Makefile | 1 kernel/res_counter.c | 120 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 230 insertions(+)diff -puN
/dev/nullinclude/linux/res_counter.hinclude/linux/res_counter.h 2007-08-17 13:14:18.000000000 +0530 renamed from dev/null 2007-06-01 20:42:04.000000000 +0530 +102
-0@@ -0,0 +1,102 @@ +#ifndef __RES_COUNTER_H__ +#define __RES_COUNTER_H__ + +/* + * Resource Counters + * Contain common data types and routines for resource accounting + * + * Copyright 2007 OpenVZ SWsoft Inc + * + * Author: Pavel Emelianov <[email protected]> + * + */ + +#include <linux/container.h> + +/* + * The core object. the container that wishes to account for some + * resource may include this counter into its structures and use + * the helpers described beyond + */ + +struct res_counter { + /* + * the current resource consumption level + */ + unsigned long usage; + /* + * the limit that usage cannot exceed + */ + unsigned long limit; + /* + * the number of unsuccessful attempts to consume the resource + */ + unsigned long failcnt; + /* + * the lock to protect all of the above. + * the routines below consider this to be IRQ-safe + */ + spinlock_t lock; +}; + +/* + * Helpers to interact with userspace + * res_counter_read/_write - put/get the specified fields from the + * res_counter struct to/from the user + * + * @counter: the counter in question + * @member: the field to work with (see RES_xxx below) + * @buf: the buffer to opeate on,... + * @nbytes: its size... + * @pos: and the offset. + */ + +ssize_t res_counter_read(struct res_counter *counter, int member, + const char __user *buf, size_t nbytes, loff_t *pos); +ssize_t res_counter_write(struct res_counter *counter, int member, + const char __user *buf, size_t nbytes, loff_t *pos); + +/* + * the field descriptors. one for each member of res_counter + */ + +enum { + RES_USAGE, + RES_LIMIT, + RES_FAILCNT, +}; + +/* + * helpers for accounting + */ + +void res_counter_init(struct res_counter *counter); + +/* + * charge - try to consume more resource. + * + * @counter: the counter + * @val: the amount of the resource. each controller defines its own + * units, e.g. numbers, bytes, Kbytes, etc + * + * returns 0 on success and <0 if the counter->usage will exceed the + * counter->limit _locked call expects the counter->lock to be taken + */ + +int res_counter_charge_locked(struct res_counter *counter, unsigned long val); +int res_counter_charge(struct res_counter *counter, unsigned long val); + +/* + * uncharge - tell that some portion of the resource is released + * + * @counter: the counter + * @val: the amount of the resource + * + * these calls check for usage underflow and show a warning on the console + * _locked call expects the counter->lock to be taken + */ + +void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val); +void res_counter_uncharge(struct res_counter *counter, unsigned long val); + +#endifdiff -puN
init/Kconfig~res_counters_infrainit/Kconfiginit/Kconfig 2007-08-17 13:14:18.000000000 +0530 renamed from init/Kconfig~res_counters_infra 2007-08-17 13:14:18.000000000 +0530 +7
-0@@ -337,6 +337,13 @@ config CPUSETS Say N if unsure. +config RESOURCE_COUNTERS + bool "Resource counters" + help + This option enables controller independent resource accounting + infrastructure that works with containers + depends on CONTAINERS + config SYSFS_DEPRECATED bool "Create deprecated sysfs files" default ydiff -puN
kernel/Makefile~res_counters_infrakernel/Makefilekernel/Makefile 2007-08-17 13:14:18.000000000 +0530 renamed from kernel/Makefile~res_counters_infra 2007-08-17 13:14:18.000000000 +0530 +1
-0@@ -58,6 +58,7 @@ obj-$(CONFIG_RELAY) += relay.o obj-$(CONFIG_SYSCTL) += utsname_sysctl.o obj-$(CONFIG_TASK_DELAY_ACCT) += delayacct.o obj-$(CONFIG_TASKSTATS) += taskstats.o tsacct.o +obj-$(CONFIG_RESOURCE_COUNTERS) += res_counter.o ifneq ($(CONFIG_SCHED_NO_NO_OMIT_FRAME_POINTER),y) # According to Alan Modra <[email protected]>, the -fno-omit-frame-pointer isdiff -puN
/dev/nullkernel/res_counter.ckernel/res_counter.c 2007-08-17 13:14:18.000000000 +0530 renamed from dev/null 2007-06-01 20:42:04.000000000 +0530 +120
-0@@ -0,0 +1,120 @@ +/* + * resource containers + * + * Copyright 2007 OpenVZ SWsoft Inc + * + * Author: Pavel Emelianov <[email protected]> + * + */ + +#include <linux/types.h> +#include <linux/parser.h> +#include <linux/fs.h> +#include <linux/res_counter.h> +#include <linux/uaccess.h> + +void res_counter_init(struct res_counter *counter) +{ + spin_lock_init(&counter->lock); + counter->limit = (unsigned long)LONG_MAX; +} + +int res_counter_charge_locked(struct res_counter *counter, unsigned long val) +{ + if (counter->usage > (counter->limit - val)) { + counter->failcnt++; + return -ENOMEM; + } + + counter->usage += val; + return 0; +} + +int res_counter_charge(struct res_counter *counter, unsigned long val) +{ + int ret; + unsigned long flags; + + spin_lock_irqsave(&counter->lock, flags); + ret = res_counter_charge_locked(counter, val); + spin_unlock_irqrestore(&counter->lock, flags); + return ret; +} + +void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val) +{ + if (WARN_ON(counter->usage < val)) + val = counter->usage; + + counter->usage -= val; +} + +void res_counter_uncharge(struct res_counter *counter, unsigned long val) +{ + unsigned long flags; + + spin_lock_irqsave(&counter->lock, flags); + res_counter_uncharge_locked(counter, val); + spin_unlock_irqrestore(&counter->lock, flags); +} + + +static inline unsigned long *res_counter_member(struct res_counter *counter, + int member) +{ + switch (member) { + case RES_USAGE: + return &counter->usage; + case RES_LIMIT: + return &counter->limit; + case RES_FAILCNT: + return &counter->failcnt; + }; + + BUG(); + return NULL; +} + +ssize_t res_counter_read(struct res_counter *counter, int member, + const char __user *userbuf, size_t nbytes, loff_t *pos) +{ + unsigned long *val; + char buf[64], *s; + + s = buf; + val = res_counter_member(counter, member); + s += sprintf(s, "%lu\n", *val); + return simple_read_from_buffer((void __user *)userbuf, nbytes, + pos, buf, s - buf); +} + +ssize_t res_counter_write(struct res_counter *counter, int member, + const char __user *userbuf, size_t nbytes, loff_t *pos) +{ + int ret; + char *buf, *end; + unsigned long tmp, *val; + + buf = kmalloc(nbytes + 1, GFP_KERNEL); + ret = -ENOMEM; + if (buf == NULL) + goto out; + + buf[nbytes] = '\0'; + ret = -EFAULT; + if (copy_from_user(buf, userbuf, nbytes)) + goto out_free; + + ret = -EINVAL; + tmp = simple_strtoul(buf, &end, 10); + if (*end != '\0') + goto out_free; + + val = res_counter_member(counter, member); + *val = tmp; + ret = nbytes; +out_free: + kfree(buf); +out: + return ret; +}_
--
Warm Regards,
Balbir Singh
Linux Technology Center
IBM, ISTL--
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: [-mm PATCH 1/9] Memory controller resource counters (v6)
Quoted 13 lines · Paul Menage
Hi Balbir/Pavel,
As I mentioned to you directly at the kernel summit, I think it might be cleaner to integrate resource counters more closely with control groups. So rather than controllers such as the memory controller having to create their own boilerplate
cf_typestructures and read/write functions, it should be possible to just call a function something likecontrol_group_add_rescounter(struct cgroup *cg, struct
cgroup_subsys*ss,
structres_counter*res,
const char *name)
Quoted 6 lines
and have it handle all the userspace API. This would simplify the task of keeping a consistent userspace API between different controllers using the resource counter abstraction.
Paul
Yes, I remember discussing it with you. I would expect res_counters definition to be dynamic (to be able to add the guarantee, soft limit, etc) for expansion in the future. In the future, I would also like to do hierarchical resource groups, the hierarchy would represent the current filesystem hierarchy.
Signature · 4 lines
Warm Regards, Balbir Singh Linux Technology Center IBM, ISTL