Skip to contentSkip to conversation

memcontrol: only transfer the memcg data for migration

PATCH In review Nhat Pham 2 participants 3 messages Oct 4, 2023

Subsystems (4)
lore
Share & tools
lore

Nhat Pham

author
#0 lore raw

PATCH v2 1/1 memcontrol: only transfer the memcg data for migration

For most migration use cases, only transfer the memcg data from the old folio to the new folio, and clear the old folio's memcg data. No charging and uncharging will be done.

This shaves off some work on the migration path, and avoids the temporary double charging of a folio during its migration.

The only exception is replace_page_cache_folio(), which will use the old mem_cgroup_migrate() (now renamed to mem_cgroup_replace_folio). In that context, the isolation of the old page isn't quite as thorough as with migration, so we cannot use our new implementation directly.

This patch is the result of the following discussion on the new hugetlb memcg accounting behavior:

https://lore.kernel.org/lkml/20231003171329.GB314430@monkey/

This should be added as the second prep patch in the following series:
https://lore.kernel.org/all/[email protected]/
(hugetlb memcg accounting)

and should go right before the following patch:
hugetlb: memcg: account hugetlb-backed memory in memory controller

---
 include/linux/memcontrol.h |  7 ++++++
 mm/filemap.c               |  2 +-
 mm/memcontrol.c            | 45 +++++++++++++++++++++++++++++++++++---
 3 files changed, 50 insertions(+), 4 deletions(-)
include/linux/memcontrol.h +7 -0
index 42bf7e9b1a2f..5daf14da3759 100644
@@ -708,6 +708,8 @@ static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
 
 void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages);
 
+void mem_cgroup_replace_folio(struct folio *old, struct folio *new);
+
 void mem_cgroup_migrate(struct folio *old, struct folio *new);
 
 /**
@@ -1285,6 +1287,11 @@ static inline void mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
 {
 }
 
+static inline void mem_cgroup_replace_folio(struct folio *old,
+		struct folio *new)
+{
+}
+
 static inline void mem_cgroup_migrate(struct folio *old, struct folio *new)
 {
 }
mm/filemap.c +1 -1
index 9481ffaf24e6..673745219c82 100644
@@ -819,7 +819,7 @@ void replace_page_cache_folio(struct folio *old, struct folio *new)
 	new->mapping = mapping;
 	new->index = offset;
 
-	mem_cgroup_migrate(old, new);
+	mem_cgroup_replace_folio(old, new);
 
 	xas_lock_irq(&xas);
 	xas_store(&xas, new);
mm/memcontrol.c +42 -3

64 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 21 lines · Nhat Pham

For most migration use cases, only transfer the memcg data from the old folio to the new folio, and clear the old folio's memcg data. No charging and uncharging will be done.

This shaves off some work on the migration path, and avoids the temporary double charging of a folio during its migration.

The only exception is replace_page_cache_folio(), which will use the old mem_cgroup_migrate() (now renamed to mem_cgroup_replace_folio). In that context, the isolation of the old page isn't quite as thorough as with migration, so we cannot use our new implementation directly.

This patch is the result of the following discussion on the new hugetlb memcg accounting behavior:

https://lore.kernel.org/lkml/20231003171329.GB314430@monkey/

This should be added as the second prep patch in the following series:
https://lore.kernel.org/all/[email protected]/
(hugetlb memcg accounting)

Quoted 5 lines

and should go right before the following patch:
hugetlb: memcg: account hugetlb-backed memory in memory controller

These two tags shouldn't be here, but in the fixlet instead. This is the dependency patch. Otherwise looks good to me:

Quoted 2 lines
author
#2 lore raw
Quoted 29 lines · Johannes Weiner
Quoted 21 lines · Nhat Pham

For most migration use cases, only transfer the memcg data from the old folio to the new folio, and clear the old folio's memcg data. No charging and uncharging will be done.

This shaves off some work on the migration path, and avoids the temporary double charging of a folio during its migration.

The only exception is replace_page_cache_folio(), which will use the old mem_cgroup_migrate() (now renamed to mem_cgroup_replace_folio). In that context, the isolation of the old page isn't quite as thorough as with migration, so we cannot use our new implementation directly.

This patch is the result of the following discussion on the new hugetlb memcg accounting behavior:

https://lore.kernel.org/lkml/20231003171329.GB314430@monkey/

This should be added as the second prep patch in the following series:
https://lore.kernel.org/all/[email protected]/
(hugetlb memcg accounting)

Quoted 5 lines

and should go right before the following patch:
hugetlb: memcg: account hugetlb-backed memory in memory controller

Quoted 7 lines

These two tags shouldn't be here, but in the fixlet instead. This is the dependency patch. Otherwise looks good to me:

Quoted 2 lines

Thanks for the review, Johannes!