Skip to contentSkip to conversation

memcg: break the zone->lru_lock on reclaim

PATCH New Ying Han 1 participant 3 messages Jun 15, 2011

Subsystems (8)
lore
Share & tools
lore
author
#1 lore raw

PATCH 1/2 memcg: break the zone->lru_lock in memcg-aware reclaim

In reply to Ying Han

This patch removes the places where we hold the zone->lru_lock and replaces it with per-memcg mz->lru lock. Two supporting functions are added to return the right lock for a page or a mem_cgroup.

Two flags are used to identify the right memcg LRU lock, AcctLRU bit and Used bit. When we delete page from LRU, the AcctLRU bit is used since we first uncharge the page and remove it from lru. When we add page into LRU, the Used bit is used since we first charge the page and then add into the LRU list.

---
 include/linux/memcontrol.h |   17 +++++++
 mm/compaction.c            |   41 +++++++++++------
 mm/huge_memory.c           |    5 +-
 mm/memcontrol.c            |   63 +++++++++++++++++++++++---
 mm/swap.c                  |   69 ++++++++++++++++------------
 mm/vmscan.c                |  106 ++++++++++++++++++++++++++++++-------------
 6 files changed, 215 insertions(+), 86 deletions(-)
include/linux/memcontrol.h +17 -0
index 6facc12..505f9a13 100644
@@ -80,6 +80,9 @@ int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem);
 extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
 extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
 
+spinlock_t *page_lru_lock(struct page *page);
+spinlock_t *mem_cgroup_lru_lock(struct mem_cgroup *mem, struct zone *zone);
+
 static inline
 int mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *cgroup)
 {
@@ -378,6 +381,20 @@ static inline
 void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
 {
 }
+
+static inline spinlock_t *page_lru_lock(struct page *page)
+{
+	struct zone *zone;
+
+	zone = page_zone(page);
+	return &zone->lru_lock;
+}
+
+static inline spinlock_t *
+mem_cgroup_lru_lock(struct mem_cgroup *mem, struct zone *zone)
+{
+	return &zone->lru_lock;
+}
 #endif /* CONFIG_CGROUP_MEM_CONT */
 
 #if !defined(CONFIG_CGROUP_MEM_RES_CTLR) || !defined(CONFIG_DEBUG_VM)
mm/compaction.c +26 -15

74 lines not on this page · raw message

mm/huge_memory.c +3 -2
index d3f601d..8d910c0 100644
@@ -1156,9 +1156,10 @@ static void __split_huge_page_refcount(struct page *page)
 	unsigned long head_index = page->index;
 	struct zone *zone = page_zone(page);
 	int zonestat;
+	spinlock_t *lru_lock = page_lru_lock(page);
 
 	/* prevent PageLRU to go away from under us, and freeze lru stats */
-	spin_lock_irq(&zone->lru_lock);
+	spin_lock_irq(lru_lock);
 	compound_lock(page);
 
 	for (i = 1; i < HPAGE_PMD_NR; i++) {
@@ -1238,7 +1239,7 @@ static void __split_huge_page_refcount(struct page *page)
 
 	ClearPageCompound(page);
 	compound_unlock(page);
-	spin_unlock_irq(&zone->lru_lock);
+	spin_unlock_irq(lru_lock);
 
 	for (i = 1; i < HPAGE_PMD_NR; i++) {
 		struct page *page_tail = page + i;
mm/memcontrol.c +55 -8

127 lines not on this page · raw message

mm/swap.c +40 -29

159 lines not on this page · raw message

mm/vmscan.c +74 -32

339 lines not on this page · raw message

Signature · 8 lines
1.7.3.1

--
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/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
author
#2 lore raw

PATCH 2/2 Move the lru_lock into the lruvec struct.

In reply to Ying Han

The lruvec structure is introduced as part of Johannes patchset. It exists for both global zone and per-memcg-per-zone struct. All the lru operations are done in generic code after this addition.

It is straight-forward to move the lru_lock within the struct.

---
 include/linux/memcontrol.h |    4 ++--
 include/linux/mm_types.h   |    2 +-
 include/linux/mmzone.h     |    8 ++++----
 mm/memcontrol.c            |   23 +++++++++++------------
 mm/page_alloc.c            |    2 +-
 mm/rmap.c                  |    2 +-
 mm/swap.c                  |    2 +-
 mm/vmscan.c                |    8 ++++----
 8 files changed, 25 insertions(+), 26 deletions(-)
include/linux/memcontrol.h +2 -2
index 505f9a13..5df371b 100644
@@ -387,13 +387,13 @@ static inline spinlock_t *page_lru_lock(struct page *page)
 	struct zone *zone;
 
 	zone = page_zone(page);
-	return &zone->lru_lock;
+	return &zone->lruvec.lru_lock;
 }
 
 static inline spinlock_t *
 mem_cgroup_lru_lock(struct mem_cgroup *mem, struct zone *zone)
 {
-	return &zone->lru_lock;
+	return &zone->lruvec.lru_lock;
 }
 #endif /* CONFIG_CGROUP_MEM_CONT */
 
include/linux/mm_types.h +1 -1
index 27c498b..7f303f9 100644
@@ -73,7 +73,7 @@ struct page {
 		void *freelist;		/* SLUB: freelist req. slab lock */
 	};
 	struct list_head lru;		/* Pageout list, eg. active_list
-					 * protected by zone->lru_lock !
+					 * protected by lruvec.lru_lock !
 					 */
 	/*
 	 * On machines where all RAM is mapped into kernel address space,
include/linux/mmzone.h +4 -4
index 4840238..1e1388c 100644
@@ -62,10 +62,10 @@ struct free_area {
 struct pglist_data;
 
 /*
- * zone->lock and zone->lru_lock are two of the hottest locks in the kernel.
+ * zone->lock and lruvec.lru_lock are two of the hottest locks in the kernel.
  * So add a wild amount of padding here to ensure that they fall into separate
- * cachelines.  There are very few zone structures in the machine, so space
- * consumption is not a concern here.
+ * cachelines.  There are very few zone structures in the machine, so
+ * space consumption is not a concern here.
  */
 #if defined(CONFIG_SMP)
 struct zone_padding {
@@ -160,6 +160,7 @@ static inline int is_unevictable_lru(enum lru_list l)
 
 struct lruvec {
 	struct list_head lists[NR_LRU_LISTS];
+	spinlock_t lru_lock;
 };
 
 enum zone_watermarks {
@@ -343,7 +344,6 @@ struct zone {
 	ZONE_PADDING(_pad1_)
 
 	/* Fields commonly accessed by the page reclaim scanner */
-	spinlock_t		lru_lock;
 	struct lruvec		lruvec;
 
 	struct zone_reclaim_stat reclaim_stat;
mm/memcontrol.c +11 -12
index f18669b..925a2e3 100644
@@ -144,7 +144,6 @@ struct mem_cgroup_stat_cpu {
  * per-zone information in memory controller.
  */
 struct mem_cgroup_per_zone {
-	spinlock_t lru_lock;
 	struct lruvec		lruvec;
 	unsigned long		count[NR_LRU_LISTS];
 
@@ -737,7 +736,7 @@ spinlock_t *page_lru_lock(struct page *page)
 
 	zone = page_zone(page);
 	if (mem_cgroup_disabled())
-		return &zone->lru_lock;
+		return &zone->lruvec.lru_lock;
 
 	pc = lookup_page_cgroup(page);
 
@@ -751,7 +750,7 @@ spinlock_t *page_lru_lock(struct page *page)
 
 	mz = page_cgroup_zoneinfo(mem, page);
 
-	return &mz->lru_lock;
+	return &mz->lruvec.lru_lock;
 }
 
 spinlock_t *mem_cgroup_lru_lock(struct mem_cgroup *mem, struct zone *zone)
@@ -760,12 +759,12 @@ spinlock_t *mem_cgroup_lru_lock(struct mem_cgroup *mem, struct zone *zone)
 	int nid, zid;
 
 	if (mem_cgroup_disabled())
-		return &zone->lru_lock;
+		return &zone->lruvec.lru_lock;
 
 	nid = zone_to_nid(zone);
 	zid = zone_idx(zone);
 	mz = mem_cgroup_zoneinfo(mem, nid, zid);
-	return &mz->lru_lock;
+	return &mz->lruvec.lru_lock;
 }
 
 /**
@@ -920,7 +919,7 @@ struct lruvec *mem_cgroup_lru_move_lists(struct zone *zone,
  * At handling SwapCache and other FUSE stuff, pc->mem_cgroup may be changed
  * while it's linked to lru because the page may be reused after it's fully
  * uncharged. To handle that, unlink page_cgroup from LRU when charge it again.
- * It's done under lock_page and expected that zone->lru_lock isnever held.
+ * It's done under lock_page and expected that lruvec.lru_lock is never held.
  */
 static void mem_cgroup_lru_del_before_commit(struct page *page)
 {
@@ -2218,7 +2217,7 @@ static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
 			(1 << PCG_ACCT_LRU) | (1 << PCG_MIGRATION))
 /*
  * Because tail pages are not marked as "used", set it. We're under
- * zone->lru_lock, 'splitting on pmd' and compund_lock.
+ * lruvec.lru_lock, 'splitting on pmd' and compund_lock.
  */
 void mem_cgroup_split_huge_fixup(struct page *head, struct page *tail)
 {
@@ -3313,19 +3312,19 @@ static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
 		struct page *page;
 
 		ret = 0;
-		spin_lock_irqsave(&mz->lru_lock, flags);
+		spin_lock_irqsave(&mz->lruvec.lru_lock, flags);
 		if (list_empty(list)) {
-			spin_unlock_irqrestore(&mz->lru_lock, flags);
+			spin_unlock_irqrestore(&mz->lruvec.lru_lock, flags);
 			break;
 		}
 		page = list_entry(list->prev, struct page, lru);
 		if (busy == page) {
 			list_move(&page->lru, list);
 			busy = NULL;
-			spin_unlock_irqrestore(&mz->lru_lock, flags);
+			spin_unlock_irqrestore(&mz->lruvec.lru_lock, flags);
 			continue;
 		}
-		spin_unlock_irqrestore(&mz->lru_lock, flags);
+		spin_unlock_irqrestore(&mz->lruvec.lru_lock, flags);
 
 		pc = lookup_page_cgroup(page);
 
@@ -4378,7 +4377,7 @@ static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
 	mem->info.nodeinfo[node] = pn;
 	for (zone = 0; zone < MAX_NR_ZONES; zone++) {
 		mz = &pn->zoneinfo[zone];
-		spin_lock_init(&mz->lru_lock);
+		spin_lock_init(&mz->lruvec.lru_lock);
 		for_each_lru(l)
 			INIT_LIST_HEAD(&mz->lruvec.lists[l]);
 	}
mm/page_alloc.c +1 -1
index d8992e1..fbd47b6 100644
@@ -4316,7 +4316,7 @@ static void __paginginit free_area_init_core(struct pglist_data *pgdat,
 #endif
 		zone->name = zone_names[j];
 		spin_lock_init(&zone->lock);
-		spin_lock_init(&zone->lru_lock);
+		spin_lock_init(&zone->lruvec.lru_lock);
 		zone_seqlock_init(zone);
 		zone->zone_pgdat = pgdat;
 
mm/rmap.c +1 -1
index 4a726e0..a94e8d0 100644
@@ -27,7 +27,7 @@
  *       mapping->i_mmap_mutex
  *         anon_vma->mutex
  *           mm->page_table_lock or pte_lock
- *             zone->lru_lock (in mark_page_accessed, isolate_lru_page)
+ *             lruvec.lru_lock (in mark_page_accessed, isolate_lru_page)
  *             swap_lock (in swap_duplicate, swap_info_get)
  *               mmlist_lock (in mmput, drain_mmlist and others)
  *               mapping->private_lock (in __set_page_dirty_buffers)
mm/swap.c +1 -1
index dce5871..2904547 100644
@@ -560,7 +560,7 @@ int lru_add_drain_all(void)
  * passed pages.  If it fell to zero then remove the page from the LRU and
  * free it.
  *
- * Avoid taking zone->lru_lock if possible, but if it is taken, retain it
+ * Avoid taking lruvec.lru_lock if possible, but if it is taken, retain it
  * for the remainder of the operation.
  *
  * The locking in this function is against shrink_inactive_list(): we recheck
mm/vmscan.c +4 -4
index b132cc2..215bb17 100644
@@ -1050,7 +1050,7 @@ int __isolate_lru_page(struct page *page, int mode, int file)
 }
 
 /*
- * zone->lru_lock is heavily contended.  Some of the functions that
+ * lruvec.lru_lock is heavily contended.  Some of the functions that
  * shrink the lists perform better by taking out a batch of pages
  * and working on them outside the LRU lock.
  *
@@ -1520,9 +1520,9 @@ shrink_inactive_list(unsigned long nr_to_scan, struct zone *zone,
  * processes, from rmap.
  *
  * If the pages are mostly unmapped, the processing is fast and it is
- * appropriate to hold zone->lru_lock across the whole operation.  But if
+ * appropriate to hold lruvec.lru_lock across the whole operation.  But if
  * the pages are mapped, the processing is slow (page_referenced()) so we
- * should drop zone->lru_lock around each page.  It's impossible to balance
+ * should drop lruvec.lru_lock around each page.  It's impossible to balance
  * this, so instead we remove the pages from the LRU while processing them.
  * It is safe to rely on PG_active against the non-LRU pages in here because
  * nobody will play with that bit on a non-LRU page.
@@ -3228,7 +3228,7 @@ int page_evictable(struct page *page, struct vm_area_struct *vma)
  * Checks a page for evictability and moves the page to the appropriate
  * zone lru list.
  *
- * Restrictions: zone->lru_lock must be held, page must be on LRU and must
+ * Restrictions: lruvec.lru_lock must be held, page must be on LRU and must
  * have PageUnevictable set.
  */
 static void check_move_unevictable_page(struct page *page, struct zone *zone)
Signature · 8 lines
1.7.3.1

--
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/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Ying Han

author
#0 lore raw

cover memcg: break the zone->lru_lock on reclaim

This patch is based on mmotm-2011-05-12-15-52 plus

"[patch 0/8] mm: memcg naturalization -rc2" patchset

"Fix behavior of per-cpu charge cache draining in memcg."

Now with all the efforts of memcg reclaim, we are going to have better targetting per-memcg reclaim both under global memory pressure and per-memcg memory pressure. The patch fixes the last piece which making the lru_lock to be exclusive across memcgs.

The reasons we have the zone->lru_lock shared due to the following:
1. each page is linked both in per-memcg lru as well as per-zone lru due to the lack of targetting reclaim under global memory pressure.
2. since we have 1), it is easier to maintain and less race conditions to have the zone->lru_lock shared vs per-memcg lock.

After Johannes patchset, there will be no global lru list when memcg is enabled. All the pages are in exclusive per-memcg lru. So it makes senses to make the spinlock exclusive which could be easily causing bad lock contention with lots of memcgs each running memory intensive workload. The zone->lru_lock is still being used if memcg is not enabled and there shouldn't be any change in that condition.

change v1..v0:
1. moved the spinlock lru_lock from mem_cgroup_per_zone struct to lruvec. the later one is introduced by Johannes patch which I think fits the lock much better.

2. add the changes in the CONFIG_TRANSPARENT_HUGEPAGE and CONFIG_COMPACTION.

3. fixed up the crash which are caused by the lumpy reclaim earlier on.

Test:
On my 8-core machine, i created 8 memcgs and each is doing a read from a ramdisk file. The filesize is larger than the hard_limit which triggers the per-memcg direct reclaim. Here I am using ramdisk to avoid the run-to-run noise from the hard drive.

--------------------------------------------------------------------------------------------------------------------------------------------
class name con-bounces contentions waittime-min waittime-max waittime-total acq-bounces acquisitions holdtime-min holdtime-max holdtime-total

Before the patch:

              &(&zone->lru_lock)->rlock:        176843         176913           0.23          31.09      387180.72        1322312        3680197           0.00          45.20     6064010.97
              -------------------------
              &(&zone->lru_lock)->rlock          71797          [<ffffffff810f25a8>] pagevec_lru_move_fn+0x6b/0xc2
              &(&zone->lru_lock)->rlock         105015          [<ffffffff810f2488>] release_pages+0xcd/0x182
              &(&zone->lru_lock)->rlock            101          [<ffffffff810f21ed>] __page_cache_release+0x3f/0x6b
              -------------------------
              &(&zone->lru_lock)->rlock          84143          [<ffffffff810f25a8>] pagevec_lru_move_fn+0x6b/0xc2
              &(&zone->lru_lock)->rlock          92680          [<ffffffff810f2488>] release_pages+0xcd/0x182
              &(&zone->lru_lock)->rlock             90          [<ffffffff810f21ed>] __page_cache_release+0x3f/0x6b

After the patch:

                &(&mz->lru_lock)->rlock:          1911           1911           0.24          13.52        3242.64         100828        3719739           0.00          45.07     5764770.80
                &(&mz->lru_lock)->rlock           1079          [<ffffffff810f24a7>] release_pages+0xe3/0x1a3
                &(&mz->lru_lock)->rlock            815          [<ffffffff810f25c8>] pagevec_lru_move_fn+0x61/0xb1
                &(&mz->lru_lock)->rlock             17          [<ffffffff810f21ef>] __page_cache_release+0x38/0x6b
                &(&mz->lru_lock)->rlock           1207          [<ffffffff810f25c8>] pagevec_lru_move_fn+0x61/0xb1
                &(&mz->lru_lock)->rlock            692          [<ffffffff810f24a7>] release_pages+0xe3/0x1a3
                &(&mz->lru_lock)->rlock             12          [<ffffffff810f21ef>] __page_cache_release+0x38/0x6b

Ying Han (2):
memcg: break the zone->lru_lock in memcg-aware reclaim Move the lru_lock into the lruvec struct.

 include/linux/memcontrol.h |   17 +++++++
 include/linux/mm_types.h   |    2 +-
 include/linux/mmzone.h     |    8 ++--
 mm/compaction.c            |   41 ++++++++++------
 mm/huge_memory.c           |    5 +-
 mm/memcontrol.c            |   66 +++++++++++++++++++++----
 mm/page_alloc.c            |    2 +-
 mm/rmap.c                  |    2 +-
 mm/swap.c                  |   71 ++++++++++++++++------------
 mm/vmscan.c                |  114 ++++++++++++++++++++++++++++++--------------
 10 files changed, 228 insertions(+), 100 deletions(-)
Signature · 8 lines
1.7.3.1

--
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/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>