OpenVZ Forum


Home » Mailing lists » Devel » [-mm PATCH 3/9] Memory controller accounting setup (v5)
[-mm PATCH 4/9] Memory controller memory accounting (v5) [message #19700 is a reply to message #19699] Mon, 13 August 2007 17:43 Go to previous messageGo to previous message
Balbir Singh is currently offline  Balbir Singh
Messages: 491
Registered: August 2006
Senior Member
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 meta_page is protected with a bit in page flags, this makes
handling of race conditions involving simultaneous mappings of a page easier.
A reference count is kept in the meta_page 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 meta page. Almost all of the page cache accounting code has help from
Vaidyanathan Srinivasan.

Signed-off-by: Vaidyanathan Srinivasan <svaidy@linux.vnet.ibm.com>
Signed-off-by: <balbir@linux.vnet.ibm.com>
---

 include/linux/memcontrol.h |   33 +++++++-
 mm/filemap.c               |   12 ++-
 mm/memcontrol.c            |  171 +++++++++++++++++++++++++++++++++++++++++++--
 mm/memory.c                |   44 ++++++++++-
 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, 303 insertions(+), 36 deletions(-)

diff -puN include/linux/memcontrol.h~mem-control-accounting include/linux/memcontrol.h
--- linux-2.6.23-rc1-mm1/include/linux/memcontrol.h~mem-control-accounting	2007-08-13 23:06:11.000000000 +0530
+++ linux-2.6.23-rc1-mm1-balbir/include/linux/memcontrol.h	2007-08-13 23:06:11.000000000 +0530
@@ -21,14 +21,22 @@
 #define _LINUX_MEMCONTROL_H
 
 struct mem_container;
-struct meta_page;
+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_meta_page(struct page *page, struct meta_page *mp);
-extern struct meta_page *page_get_meta_page(struct page *page);
+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,
@@ -40,16 +48,29 @@ static inline void mm_free_container(str
 {
 }
 
-static inline void page_assign_meta_page(struct page *page,
-						struct meta_page *mp)
+static inline void page_assign_page_container(struct page *page,
+						struct page_container *pc)
 {
 }
 
-static inline struct meta_page *page_get_meta_page(struct page *page)
+static inline struct page_container *page_get_page_container(struct page *page)
 {
 	return 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
diff -puN mm/filemap.c~mem-control-accounting mm/filemap.c
--- linux-2.6.23-rc1-mm1/mm/filemap.c~mem-control-accounting	2007-08-13 23:06:11.000000000 +0530
+++ linux-2.6.23-rc1-mm1-balbir/mm/filemap.c	2007-08-13 23:06:11.000000000 +0530
@@ -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);
diff -puN mm/memcontrol.c~mem-control-accounting mm/memcontrol.c
--- linux-2.6.23-rc1-mm1/mm/memcontrol.c~mem-control-accounting	2007-08-13 23:06:11.000000000 +0530
+++ linux-2.6.23-rc1-mm1-balbir/mm/memcontrol.c	2007-08-13 23:06:11.000000000 +0530
@@ -21,6 +21,9 @@
 #include <linux/memcontrol.h>
 #include <linux/container.h>
 #include <linux/mm.h>
+#include <linux/page-flags.h>
+#include <linux/bit_spinlock.h>
+#include <linux/rcupdate.h>
 
 struct container_subsys mem_container_subsys;
 
@@ -31,7 +34,9 @@ struct container_subsys mem_container_su
  * to help the administrator determine what knobs to tune.
  *
  * TODO: Add a water mark for the memory controller. Reclaim will begin when
- * we hit the water mark.
+ * we hit the water mark. May be even add a low water mark, such that
+ * no reclaim occurs from a container at it's low water mark, this is
+ * a feature that will be implemented much later in the future.
  */
 struct mem_container {
 	struct container_subsys_state css;
@@ -49,13 +54,23 @@ struct mem_container {
 };
 
 /*
+ * We use the lower bit of the page->page_container pointer as a bit spin
+ * lock. We need to ensure that page->page_container is atleast two
+ * byte aligned (based on comments from Nick Piggin)
+ */
+#define PAGE_CONTAINER_LOCK_BIT 	0x0
+#define PAGE_CONTAINER_LOCK 		(1 << PAGE_CONTAINER_LOCK_BIT)
+
+/*
  * A meta page is associated with every page descriptor. The meta page
  * helps us identify information about the container
  */
-struct meta_page {
+struct page_container {
 	struct list_head lru;		/* per container LRU list */
 	struct page *page;
 	struct mem_container *mem_container;
+	atomic_t ref_cnt;		/* Helpful when pages move b/w  */
+					/* mapped and cached states     */
 };
 
 
@@ -88,14 +103,156 @@ void mm_free_container(struct mm_struct 
 	css_put(&mm->mem_container->css);
 }
 
-void page_assign_meta_page(struct page *page, struct meta_page *mp)
+static inline int page_container_locked(struct page *page)
+{
+	return bit_spin_is_locked(PAGE_CONTAINER_LOCK_BIT,
+					&page->page_container);
+}
+
+void page_assign_page_container(struct page *page, struct page_container *pc)
+{
+	int locked;
+
+	/*
+	 * While resetting the page_container we might not hold the
+	 * page_container lock. free_hot_cold_page() is an example
+	 * of such a scenario
+	 */
+	if (pc)
+		VM_BUG_ON(!page_container_locked(page));
+	locked = (page->page_container & PAGE_CONTAINER_LOCK);
+	page->page_container = ((unsigned long)pc | locked);
+}
+
+struct page_container *page_get_page_container(struct page *page)
 {
-	page->meta_page = mp;
+	return (struct page_container *)
+		(page->page_container & ~PAGE_CONTAINER_LOCK);
+}
+
+void __always_inline lock_page_container(struct page *page)
+{
+	bit_spin_lock(PAGE_CONTAINER_LOCK_BIT, &page->page_container);
+	VM_BUG_ON(!page_container_locked(page));
+}
+
+void __always_inline unlock_page_container(struct page *page)
+{
+	bit_spin_unlock(PAGE_CONTAINER_LOCK_BIT, &page->page_container);
+}
+
+/*
+ * Charge the memory controller for page usage.
+ * Return
+ * 0 if the charge was successful
+ * < 0 if the container is over its limit
+ */
+int mem_container_charge(struct page *page, struct mm_struct *mm)
+{
+	struct mem_container *mem;
+	struct page_container *pc, *race_pc;
+
+	/*
+	 * Should page_container's go to their own slab?
+	 * One could optimize the performance of the charging routine
+	 * by saving a bit in the page_flags and using it as a lock
+	 * to see if the container page already has a page_container associated
+	 * with it
+	 */
+	lock_page_container(page);
+	pc = page_get_page_container(page);
+	/*
+	 * The page_container exists and the page has already been accounted
+	 */
+	if (pc) {
+		atomic_inc(&pc->ref_cnt);
+		goto done;
+	}
+
+	unlock_page_container(page);
+
+	pc = kzalloc(sizeof(struct page_container), GFP_KERNEL);
+	if (pc == NULL)
+		goto err;
+
+	rcu_read_lock();
+	/*
+	 * We always charge the container the mm_struct belongs to
+	 * the mm_struct's mem_container changes on task migration if the
+	 * thread group leader migrates. It's possible that mm is not
+	 * set, if so charge the init_mm (happens for pagecache usage).
+	 */
+	if (!mm)
+		mm = &init_mm;
+
+	mem = rcu_dereference(mm->mem_container);
+	/*
+	 * For every charge from the container, increment reference
+	 * count
+	 */
+	css_get(&mem->css);
+	rcu_read_unloc
...

 
Read Message
Read Message
Read Message
Previous Topic: [-mm PATCH 0/9] Memory controller introduction (v5)
Next Topic: [-mm PATCH 6/9] Memory controller add per container LRU and reclaim (v5)
Goto Forum:
  


Current Time: Fri Oct 18 20:23:37 GMT 2024

Total time taken to generate the page: 0.04828 seconds