Skip to contentSkip to conversation

memcg: fix shmem_unuse_inode charging

PATCH In review Hugh Dickins 4 participants 12 messages Jun 29, 2008 – Jun 30, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memcg: fix shmem_unuse_inode charging

The mem_cgroup_cache_charge in shmem_unuse_inode has been moved after the radix_tree_preload, so generating a BUG: sleeping function called from invalid context. Move it back, uncharging where necessary.

---
Should follow mmotm's memcg-remove-refcnt-from-page_cgroup.patch

mm/shmem.c | 28 ++++++++++++----------------

 1 file changed, 12 insertions(+), 16 deletions(-)
mm/shmem.c +12 -16
@@ -922,27 +922,24 @@ found:
 	error = 1;
 	if (!inode)
 		goto out;
-	error = radix_tree_preload(GFP_KERNEL);
-	if (error)
-		goto out;
-	/*
-	 * Because we use GFP_NOWAIT in add_to_page_cache(), we can see -ENOMEM
-	 * failure because of memory pressure in memory resource controller.
-	 * Then, precharge page while we can wait, uncharge at failure will be
-	 * automatically done in add_to_page_cache()
-	 */
+	/* Precharge page using GFP_KERNEL while we can wait */
 	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
 	if (error)
-		goto preload_out;
-
+		goto out;
+	error = radix_tree_preload(GFP_KERNEL);
+	if (error) {
+		mem_cgroup_uncharge_cache_page(page);
+		goto out;
+	}
 	error = 1;
 
 	spin_lock(&info->lock);
 	ptr = shmem_swp_entry(info, idx, NULL);
-	if (ptr && ptr->val == entry.val)
-		error = add_to_page_cache(page, inode->i_mapping, idx,
-					GFP_NOWAIT);
-	else /* we don't have to account this page. */
+	if (ptr && ptr->val == entry.val) {
+		error = add_to_page_cache(page, inode->i_mapping,
+						idx, GFP_NOWAIT);
+		/* does mem_cgroup_uncharge_cache_page on error */
+	} else	/* we must compensate for our precharge above */
 		mem_cgroup_uncharge_cache_page(page);
 
 	if (error == -EEXIST) {
@@ -969,7 +966,6 @@ found:
 	if (ptr)
 		shmem_swp_unmap(ptr);
 	spin_unlock(&info->lock);
-preload_out:
 	radix_tree_preload_end();
 out:
 	unlock_page(page);
#1 lore raw

PATCH 1/1 memcg: shmem_getpage release page sooner

No big deal, but since mem_cgroup_shrink_usage doesn't require a page to operate upon, page_cache_release the swappage before calling it, so it's not pinned across the reclaim.

---
Should follow mmotm's memcg-helper-function-for-relcaim-from-shmem.patch

 mm/shmem.c |    8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
mm/shmem.c +3 -5
@@ -1315,16 +1315,14 @@ repeat:
 			shmem_swp_unmap(entry);
 			spin_unlock(&info->lock);
 			unlock_page(swappage);
+			page_cache_release(swappage);
 			if (error == -ENOMEM) {
 				/* allow reclaim from this memory cgroup */
 				error = mem_cgroup_shrink_usage(current->mm,
-					gfp & ~__GFP_HIGHMEM);
-				if (error) {
-					page_cache_release(swappage);
+								gfp);
+				if (error)
 					goto failed;
-				}
 			}
-			page_cache_release(swappage);
 			goto repeat;
 		}
 	} else if (sgp == SGP_READ && !filepage) {
Acked-by on v1 1/1
#2 lore raw

Re: [PATCH] memcg: shmem_getpage release page sooner

Quoted 5 lines · On Sun, 29 Jun 2008 01:14:30 +0100 (BST) Hugh Dickins <[email protected]> wrote:

No big deal, but since mem_cgroup_shrink_usage doesn't require a page to operate upon, page_cache_release the swappage before calling it, so it's not pinned across the reclaim.

this one is better, thanks.

Quoted 29 lines

---
Should follow mmotm's memcg-helper-function-for-relcaim-from-shmem.patch

 mm/shmem.c |    8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
mm/shmem.c +3 -5
@@ -1315,16 +1315,14 @@ repeat:
 			shmem_swp_unmap(entry);
 			spin_unlock(&info->lock);
 			unlock_page(swappage);
+			page_cache_release(swappage);
 			if (error == -ENOMEM) {
 				/* allow reclaim from this memory cgroup */
 				error = mem_cgroup_shrink_usage(current->mm,
-					gfp & ~__GFP_HIGHMEM);
-				if (error) {
-					page_cache_release(swappage);
+								gfp);
+				if (error)
 					goto failed;
-				}
 			}
-			page_cache_release(swappage);
 			goto repeat;
 		}
 	} else if (sgp == SGP_READ && !filepage) {
#3 lore raw

PATCH 1/1 memcg: mem_cgroup_shrink_usage css_put

In reply to Hugh Dickins

mem_cgroup_shrink_usage makes no charge: balance its css_get with a css_put.

---
Should follow mmotm's memcg-helper-function-for-relcaim-from-shmem.patch

 mm/memcontrol.c |    1 +
 1 file changed, 1 insertion(+)
mm/memcontrol.c +1 -0
@@ -801,6 +801,7 @@ int mem_cgroup_shrink_usage(struct mm_st
 		progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
 	} while (!progress && --retry);
 
+	css_put(&mem->css);
 	if (!retry)
 		return -ENOMEM;
 	return 0;
Acked-by on v1 1/1
#4 lore raw

Re: [PATCH] memcg: mem_cgroup_shrink_usage css_put

Quoted 3 lines · On Sun, 29 Jun 2008 01:15:38 +0100 (BST) Hugh Dickins <[email protected]> wrote:

mem_cgroup_shrink_usage makes no charge: balance its css_get with a css_put.

Sorry for my mistakes

Quoted 22 lines

---
Should follow mmotm's memcg-helper-function-for-relcaim-from-shmem.patch

 mm/memcontrol.c |    1 +
 1 file changed, 1 insertion(+)
mm/memcontrol.c +1 -1
@@ -801,6 +801,7 @@ int mem_cgroup_shrink_usage(struct mm_st
 		progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
 	} while (!progress && --retry);
 
+	css_put(&mem->css);
 	if (!retry)
 		return -ENOMEM;
 	return 0;
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Reviewed-by
#5 lore raw

Re: [PATCH] memcg: mem_cgroup_shrink_usage css_put

Quoted 10 lines · KAMEZAWA Hiroyuki
Quoted 3 lines · On Sun, 29 Jun 2008 01:15:38 +0100 (BST) Hugh Dickins <[email protected]> wrote:

mem_cgroup_shrink_usage makes no charge: balance its css_get with a css_put.

Sorry for my mistakes

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
#6 lore raw

PATCH 1/1 memcg: further checking of disabled flag

In reply to Hugh Dickins

Further adjustments to the mem_cgroup_subsys.disabled tests: add one to mem_cgroup_shrink_usage; move mem_cgroup_charge_common's into its callers, before they've done any work; and add one to mem_cgroup_move_lists, to avoid the overhead of its bit spin locking and unlocking.

---
Should follow mmotm's memcg-clean-up-checking-of-the-disabled-flag.patch

 mm/memcontrol.c |   15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
mm/memcontrol.c +12 -3
@@ -354,6 +354,9 @@ void mem_cgroup_move_lists(struct page *
 	struct mem_cgroup_per_zone *mz;
 	unsigned long flags;
 
+	if (mem_cgroup_subsys.disabled)
+		return;
+
 	/*
 	 * We cannot lock_page_cgroup while holding zone's lru_lock,
 	 * because other holders of lock_page_cgroup can be interrupted
@@ -533,9 +536,6 @@ static int mem_cgroup_charge_common(stru
 	unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
 	struct mem_cgroup_per_zone *mz;
 
-	if (mem_cgroup_subsys.disabled)
-		return 0;
-
 	pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
 	if (unlikely(pc == NULL))
 		goto err;
@@ -620,6 +620,9 @@ err:
 
 int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
 {
+	if (mem_cgroup_subsys.disabled)
+		return 0;
+
 	/*
 	 * If already mapped, we don't have to account.
 	 * If page cache, page->mapping has address_space.
@@ -638,6 +641,9 @@ int mem_cgroup_charge(struct page *page,
 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
 				gfp_t gfp_mask)
 {
+	if (mem_cgroup_subsys.disabled)
+		return 0;
+
 	/*
 	 * Corner case handling. This is called from add_to_page_cache()
 	 * in usual. But some FS (shmem) precharges this page before calling it
@@ -789,6 +795,9 @@ int mem_cgroup_shrink_usage(struct mm_st
 	int progress = 0;
 	int retry = MEM_CGROUP_RECLAIM_RETRIES;
 
+	if (mem_cgroup_subsys.disabled)
+		return 0;
+
 	rcu_read_lock();
 	mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
 	css_get(&mem->css);
Acked-by on v1 1/1
#7 lore raw

Re: [PATCH] memcg: further checking of disabled flag

Quoted 6 lines · On Sun, 29 Jun 2008 01:17:17 +0100 (BST) Hugh Dickins <[email protected]> wrote:

Further adjustments to the mem_cgroup_subsys.disabled tests: add one to mem_cgroup_shrink_usage; move mem_cgroup_charge_common's into its callers, before they've done any work; and add one to mem_cgroup_move_lists, to avoid the overhead of its bit spin locking and unlocking.

seems better

Quoted 59 lines

---
Should follow mmotm's memcg-clean-up-checking-of-the-disabled-flag.patch

 mm/memcontrol.c |   15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
mm/memcontrol.c +12 -3
@@ -354,6 +354,9 @@ void mem_cgroup_move_lists(struct page *
 	struct mem_cgroup_per_zone *mz;
 	unsigned long flags;
 
+	if (mem_cgroup_subsys.disabled)
+		return;
+
 	/*
 	 * We cannot lock_page_cgroup while holding zone's lru_lock,
 	 * because other holders of lock_page_cgroup can be interrupted
@@ -533,9 +536,6 @@ static int mem_cgroup_charge_common(stru
 	unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
 	struct mem_cgroup_per_zone *mz;
 
-	if (mem_cgroup_subsys.disabled)
-		return 0;
-
 	pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
 	if (unlikely(pc == NULL))
 		goto err;
@@ -620,6 +620,9 @@ err:
 
 int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
 {
+	if (mem_cgroup_subsys.disabled)
+		return 0;
+
 	/*
 	 * If already mapped, we don't have to account.
 	 * If page cache, page->mapping has address_space.
@@ -638,6 +641,9 @@ int mem_cgroup_charge(struct page *page,
 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
 				gfp_t gfp_mask)
 {
+	if (mem_cgroup_subsys.disabled)
+		return 0;
+
 	/*
 	 * Corner case handling. This is called from add_to_page_cache()
 	 * in usual. But some FS (shmem) precharges this page before calling it
@@ -789,6 +795,9 @@ int mem_cgroup_shrink_usage(struct mm_st
 	int progress = 0;
 	int retry = MEM_CGROUP_RECLAIM_RETRIES;
 
+	if (mem_cgroup_subsys.disabled)
+		return 0;
+
 	rcu_read_lock();
 	mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
 	css_get(&mem->css);
Acked-by
#8 lore raw

Re: [PATCH] memcg: further checking of disabled flag

Quoted 8 lines · KAMEZAWA Hiroyuki
Quoted 5 lines · On Sun, 29 Jun 2008 01:17:17 +0100 (BST) Hugh Dickins <[email protected]> wrote:

Further adjustments to the mem_cgroup_subsys.disabled tests: add one to mem_cgroup_shrink_usage; move mem_cgroup_charge_common's into its callers, before they've done any work; and add one to mem_cgroup_move_lists, to avoid the overhead of its bit spin locking and unlocking.

Quoted 5 lines
Quoted 1 line

seems better

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
Reviewed-by on v1 1/1
#9 lore raw

Re: [PATCH] memcg: further checking of disabled flag

In reply to Hugh Dickins
Quoted 6 lines · Hugh Dickins

Further adjustments to the mem_cgroup_subsys.disabled tests: add one to mem_cgroup_shrink_usage; move mem_cgroup_charge_common's into its callers, before they've done any work; and add one to mem_cgroup_move_lists, to avoid the overhead of its bit spin locking and unlocking.

Quoted 3 lines

---
Should follow mmotm's memcg-clean-up-checking-of-the-disabled-flag.patch

But this needn't follow memcg-clean-up-checking-of-the-disabled-flag.patch, because this patch is not a fix to it. :)

Acked-by on v1 1/1
#10 lore raw
In reply to Hugh Dickins
Quoted 5 lines · On Sun, 29 Jun 2008 01:13:20 +0100 (BST) Hugh Dickins <[email protected]> wrote:

The mem_cgroup_cache_charge in shmem_unuse_inode has been moved after the radix_tree_preload, so generating a BUG: sleeping function called from invalid context. Move it back, uncharging where necessary.

Sure, thanks,

Quoted 57 lines

---
Should follow mmotm's memcg-remove-refcnt-from-page_cgroup.patch

mm/shmem.c | 28 ++++++++++++----------------

 1 file changed, 12 insertions(+), 16 deletions(-)
mm/shmem.c +12 -16
@@ -922,27 +922,24 @@ found:
 	error = 1;
 	if (!inode)
 		goto out;
-	error = radix_tree_preload(GFP_KERNEL);
-	if (error)
-		goto out;
-	/*
-	 * Because we use GFP_NOWAIT in add_to_page_cache(), we can see -ENOMEM
-	 * failure because of memory pressure in memory resource controller.
-	 * Then, precharge page while we can wait, uncharge at failure will be
-	 * automatically done in add_to_page_cache()
-	 */
+	/* Precharge page using GFP_KERNEL while we can wait */
 	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
 	if (error)
-		goto preload_out;
-
+		goto out;
+	error = radix_tree_preload(GFP_KERNEL);
+	if (error) {
+		mem_cgroup_uncharge_cache_page(page);
+		goto out;
+	}
 	error = 1;
 
 	spin_lock(&info->lock);
 	ptr = shmem_swp_entry(info, idx, NULL);
-	if (ptr && ptr->val == entry.val)
-		error = add_to_page_cache(page, inode->i_mapping, idx,
-					GFP_NOWAIT);
-	else /* we don't have to account this page. */
+	if (ptr && ptr->val == entry.val) {
+		error = add_to_page_cache(page, inode->i_mapping,
+						idx, GFP_NOWAIT);
+		/* does mem_cgroup_uncharge_cache_page on error */
+	} else	/* we must compensate for our precharge above */
 		mem_cgroup_uncharge_cache_page(page);
 
 	if (error == -EEXIST) {
@@ -969,7 +966,6 @@ found:
 	if (ptr)
 		shmem_swp_unmap(ptr);
 	spin_unlock(&info->lock);
-preload_out:
 	radix_tree_preload_end();
 out:
 	unlock_page(page);
Acked-by
#11 lore raw
Quoted 7 lines · KAMEZAWA Hiroyuki
Quoted 4 lines · On Sun, 29 Jun 2008 01:13:20 +0100 (BST) Hugh Dickins <[email protected]> wrote:

The mem_cgroup_cache_charge in shmem_unuse_inode has been moved after the radix_tree_preload, so generating a BUG: sleeping function called from invalid context. Move it back, uncharging where necessary.

Quoted 5 lines
Quoted 1 line

Sure, thanks,

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL