Skip to contentSkip to conversation

mm/memcontrol: avoid unnecessary PageTransHuge() when counting compound page

PATCH In review Yafang Shao 3 participants 6 messages May 5, 2019 – May 7, 2019

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

PATCH 1/1 mm/memcontrol: avoid unnecessary PageTransHuge() when counting compound page

If CONFIG_TRANSPARENT_HUGEPAGE is not set, hpage_nr_pages() is always 1; if CONFIG_TRANSPARENT_HUGEPAGE is set, hpage_nr_pages() will call PageTransHuge() to judge whether the page is compound page or not. So we can use the result of hpage_nr_pages() to avoid uneccessary PageTransHuge().

---
 mm/memcontrol.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
mm/memcontrol.c +6 -7
index 2535e54..65c6f7c 100644
@@ -6306,7 +6306,6 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
 {
 	struct mem_cgroup *memcg;
 	unsigned int nr_pages;
-	bool compound;
 	unsigned long flags;
 
 	VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
@@ -6328,8 +6327,7 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
 		return;
 
 	/* Force-charge the new page. The old one will be freed soon */
-	compound = PageTransHuge(newpage);
-	nr_pages = compound ? hpage_nr_pages(newpage) : 1;
+	nr_pages = hpage_nr_pages(newpage);
 
 	page_counter_charge(&memcg->memory, nr_pages);
 	if (do_memsw_account())
@@ -6339,7 +6337,7 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
 	commit_charge(newpage, memcg, false);
 
 	local_irq_save(flags);
-	mem_cgroup_charge_statistics(memcg, newpage, compound, nr_pages);
+	mem_cgroup_charge_statistics(memcg, newpage, nr_pages > 1, nr_pages);
 	memcg_check_events(memcg, newpage);
 	local_irq_restore(flags);
 }
@@ -6533,6 +6531,7 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
 	struct mem_cgroup *memcg, *swap_memcg;
 	unsigned int nr_entries;
 	unsigned short oldid;
+	bool compound;
 
 	VM_BUG_ON_PAGE(PageLRU(page), page);
 	VM_BUG_ON_PAGE(page_count(page), page);
@@ -6553,8 +6552,9 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
 	 */
 	swap_memcg = mem_cgroup_id_get_online(memcg);
 	nr_entries = hpage_nr_pages(page);
+	compound = nr_entries > 1;
 	/* Get references for the tail pages, too */
-	if (nr_entries > 1)
+	if (compound)
 		mem_cgroup_id_get_many(swap_memcg, nr_entries - 1);
 	oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg),
 				   nr_entries);
@@ -6579,8 +6579,7 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
 	 * only synchronisation we have for updating the per-CPU variables.
 	 */
 	VM_BUG_ON(!irqs_disabled());
-	mem_cgroup_charge_statistics(memcg, page, PageTransHuge(page),
-				     -nr_entries);
+	mem_cgroup_charge_statistics(memcg, page, compound, -nr_entries);
 	memcg_check_events(memcg, page);
 
 	if (!mem_cgroup_is_root(memcg))
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 5 lines · Yafang Shao

If CONFIG_TRANSPARENT_HUGEPAGE is not set, hpage_nr_pages() is always 1; if CONFIG_TRANSPARENT_HUGEPAGE is set, hpage_nr_pages() will call PageTransHuge() to judge whether the page is compound page or not. So we can use the result of hpage_nr_pages() to avoid uneccessary PageTransHuge().

The changelog doesn't describe motivation. Does this result in a better code/performance?

Quoted 67 lines
---
 mm/memcontrol.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
mm/memcontrol.c +6 -7
index 2535e54..65c6f7c 100644
@@ -6306,7 +6306,6 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
 {
 	struct mem_cgroup *memcg;
 	unsigned int nr_pages;
-	bool compound;
 	unsigned long flags;
 
 	VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
@@ -6328,8 +6327,7 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
 		return;
 
 	/* Force-charge the new page. The old one will be freed soon */
-	compound = PageTransHuge(newpage);
-	nr_pages = compound ? hpage_nr_pages(newpage) : 1;
+	nr_pages = hpage_nr_pages(newpage);
 
 	page_counter_charge(&memcg->memory, nr_pages);
 	if (do_memsw_account())
@@ -6339,7 +6337,7 @@ void mem_cgroup_migrate(struct page *oldpage, struct page *newpage)
 	commit_charge(newpage, memcg, false);
 
 	local_irq_save(flags);
-	mem_cgroup_charge_statistics(memcg, newpage, compound, nr_pages);
+	mem_cgroup_charge_statistics(memcg, newpage, nr_pages > 1, nr_pages);
 	memcg_check_events(memcg, newpage);
 	local_irq_restore(flags);
 }
@@ -6533,6 +6531,7 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
 	struct mem_cgroup *memcg, *swap_memcg;
 	unsigned int nr_entries;
 	unsigned short oldid;
+	bool compound;
 
 	VM_BUG_ON_PAGE(PageLRU(page), page);
 	VM_BUG_ON_PAGE(page_count(page), page);
@@ -6553,8 +6552,9 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
 	 */
 	swap_memcg = mem_cgroup_id_get_online(memcg);
 	nr_entries = hpage_nr_pages(page);
+	compound = nr_entries > 1;
 	/* Get references for the tail pages, too */
-	if (nr_entries > 1)
+	if (compound)
 		mem_cgroup_id_get_many(swap_memcg, nr_entries - 1);
 	oldid = swap_cgroup_record(entry, mem_cgroup_id(swap_memcg),
 				   nr_entries);
@@ -6579,8 +6579,7 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
 	 * only synchronisation we have for updating the per-CPU variables.
 	 */
 	VM_BUG_ON(!irqs_disabled());
-	mem_cgroup_charge_statistics(memcg, page, PageTransHuge(page),
-				     -nr_entries);
+	mem_cgroup_charge_statistics(memcg, page, compound, -nr_entries);
 	memcg_check_events(memcg, page);
 
 	if (!mem_cgroup_is_root(memcg))
Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 11 lines · Michal Hocko
Quoted 5 lines · Yafang Shao

If CONFIG_TRANSPARENT_HUGEPAGE is not set, hpage_nr_pages() is always 1; if CONFIG_TRANSPARENT_HUGEPAGE is set, hpage_nr_pages() will call PageTransHuge() to judge whether the page is compound page or not. So we can use the result of hpage_nr_pages() to avoid uneccessary PageTransHuge().

The changelog doesn't describe motivation. Does this result in a better code/performance?

It is a better code, I think.
Regarding the performance, I don't think it is easy to measure.

Thanks
Yafang

#3 lore raw
Quoted 9 lines · Yafang Shao
Quoted 8 lines · Michal Hocko
Quoted 5 lines · Yafang Shao

If CONFIG_TRANSPARENT_HUGEPAGE is not set, hpage_nr_pages() is always 1; if CONFIG_TRANSPARENT_HUGEPAGE is set, hpage_nr_pages() will call PageTransHuge() to judge whether the page is compound page or not. So we can use the result of hpage_nr_pages() to avoid uneccessary PageTransHuge().

Quoted 6 lines
Quoted 3 lines

The changelog doesn't describe motivation. Does this result in a better code/performance?

It is a better code, I think.
Regarding the performance, I don't think it is easy to measure.

I am not convinced the patch is worth it. The code aesthetic is a matter of taste. On the other hand, the change will be an additional step in the git history so git blame take an additional step to get to the original commit which is a bit annoying. Also every change, even a trivially looking one, can cause surprising side effects. These are all arguments make a change to the code.

So unless the resulting code is really much more cleaner, easier to read or maintain, or it is a part of a larger series that makes further steps easier,then I would prefer not touching the code.

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 11 lines · Michal Hocko
Quoted 2 lines · Yafang Shao

It is a better code, I think.
Regarding the performance, I don't think it is easy to measure.

I am not convinced the patch is worth it. The code aesthetic is a matter of taste. On the other hand, the change will be an additional step in the git history so git blame take an additional step to get to the original commit which is a bit annoying. Also every change, even a trivially looking one, can cause surprising side effects. These are all arguments make a change to the code.

Quoted 3 lines

So unless the resulting code is really much more cleaner, easier to read or maintain, or it is a part of a larger series that makes further steps easier,then I would prefer not touching the code.

Aside from what Michal already said, which I agree with, when skimming code reading PageTransHuge has much clearer intent to me than checking nr_pages. We already have a non-trivial number of checks which are unclear at first glance in the mm code and, while this isn't nearly as bad as some of those, and might not make the situation much worse, I also don't think changing to nr_pages checks makes the situation any better, either.

#5 lore raw
Quoted 17 lines · Chris Down
Quoted 11 lines · Michal Hocko
Quoted 2 lines · Yafang Shao

It is a better code, I think.
Regarding the performance, I don't think it is easy to measure.

I am not convinced the patch is worth it. The code aesthetic is a matter of taste. On the other hand, the change will be an additional step in the git history so git blame take an additional step to get to the original commit which is a bit annoying. Also every change, even a trivially looking one, can cause surprising side effects. These are all arguments make a change to the code.

Quoted 3 lines

So unless the resulting code is really much more cleaner, easier to read or maintain, or it is a part of a larger series that makes further steps easier,then I would prefer not touching the code.

Quoted 6 lines

Aside from what Michal already said, which I agree with, when skimming code reading PageTransHuge has much clearer intent to me than checking nr_pages. We already have a non-trivial number of checks which are unclear at first glance in the mm code and, while this isn't nearly as bad as some of those, and might not make the situation much worse, I also don't think changing to nr_pages checks makes the situation any better, either.

I agree with dropping this patch, but I don't agree with your opinion that PageTransHuge() can make the code clear.

The motivation I send this patch is because 'compound' and 'PageTransHuge' confused me.
I prefer to remove the paratmeter 'compound' compeletely from some functions(i.e. mem_cgroup_commit_charge, mem_cgroup_migrate, mem_cgroup_swapout, mem_cgroup_move_account) in memcontrol.c completely,
because whether this page is compound or not doesn't depends on the callsite, but only depends on the page itself.
I mean we can use the page to judge whether the page is compound or not. I didn't do that because I'm not sure whether it is worth. The other reason comfused me is compound page may not be thp. Of course it can only be thp in the current callsites.
Maybe 'thp' is better than 'compound'.

Thanks
Yafang