Skip to contentSkip to conversation

mm/codetag: swap tags when migrate pages

PATCH Superseded David Wang 3 participants 8 messages Nov 28, 2024 – Dec 2, 2024

Subsystems (4)
lore
Share & tools
lore

David Wang

author
#0 lore raw

PATCH 1/1 mm/codetag: swap tags when migrate pages

The initial solution for codetag adjustment during page migration uses three kinds of low level plumbings, those steps can be replaced by swapping tags, which only needs one kind of low level plumbing, and code is more clear.

---
 include/linux/pgalloc_tag.h |  4 ++--
 lib/alloc_tag.c             | 35 +++++++++++++++++++----------------
 mm/migrate.c                |  2 +-
 3 files changed, 22 insertions(+), 19 deletions(-)
include/linux/pgalloc_tag.h +2 -2
index 0e43ab653ab6..3469c4b20105 100644
@@ -231,7 +231,7 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
 }
 
 void pgalloc_tag_split(struct folio *folio, int old_order, int new_order);
-void pgalloc_tag_copy(struct folio *new, struct folio *old);
+void pgalloc_tag_swap(struct folio *new, struct folio *old);
 
 void __init alloc_tag_sec_init(void);
 
@@ -245,7 +245,7 @@ static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL
 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
 static inline void alloc_tag_sec_init(void) {}
 static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) {}
-static inline void pgalloc_tag_copy(struct folio *new, struct folio *old) {}
+static inline void pgalloc_tag_swap(struct folio *new, struct folio *old) {}
 
 #endif /* CONFIG_MEM_ALLOC_PROFILING */
 
lib/alloc_tag.c +19 -16
index 2414a7ee7ec7..b45efde50c40 100644
@@ -189,26 +189,29 @@ void pgalloc_tag_split(struct folio *folio, int old_order, int new_order)
 	}
 }
 
-void pgalloc_tag_copy(struct folio *new, struct folio *old)
+void pgalloc_tag_swap(struct folio *new, struct folio *old)
 {
-	union pgtag_ref_handle handle;
-	union codetag_ref ref;
-	struct alloc_tag *tag;
+	union pgtag_ref_handle handles[2];
+	union codetag_ref refs[2];
+	struct alloc_tag *tags[2];
+	struct folio *folios[2] = {new, old};
+	int i;
 
-	tag = pgalloc_tag_get(&old->page);
-	if (!tag)
-		return;
+	for (i = 0; i < 2; i++) {
+		tags[i] = pgalloc_tag_get(&folios[i]->page);
+		if (!tags[i])
+			return;
+		if (!get_page_tag_ref(&folios[i]->page, &refs[i], &handles[i]))
+			return;
+	}
 
-	if (!get_page_tag_ref(&new->page, &ref, &handle))
-		return;
+	swap(tags[0], tags[1]);
 
-	/* Clear the old ref to the original allocation tag. */
-	clear_page_tag_ref(&old->page);
-	/* Decrement the counters of the tag on get_new_folio. */
-	alloc_tag_sub(&ref, folio_size(new));
-	__alloc_tag_ref_set(&ref, tag);
-	update_page_tag_ref(handle, &ref);
-	put_page_tag_ref(handle);
+	for (i = 0; i < 2; i++) {
+		__alloc_tag_ref_set(&refs[i], tags[i]);
+		update_page_tag_ref(handles[i], &refs[i]);
+		put_page_tag_ref(handles[i]);
+	}
 }
 
 static void shutdown_mem_profiling(bool remove_file)
mm/migrate.c +1 -1
index 2ce6b4b814df..cc68583c86f9 100644
@@ -745,7 +745,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 		folio_set_readahead(newfolio);
 
 	folio_copy_owner(newfolio, folio);
-	pgalloc_tag_copy(newfolio, folio);
+	pgalloc_tag_swap(newfolio, folio);
 
 	mem_cgroup_migrate(folio, newfolio);
 }
Signature · 1 line
2.39.2
#1 lore raw
Quoted 5 lines · David Wang

The initial solution for codetag adjustment during page migration uses three kinds of low level plumbings, those steps can be replaced by swapping tags, which only needs one kind of low level plumbing, and code is more clear.

This description does not explain the real issue. I would suggest something like:

Current solution to adjust codetag references during page migration is done in 3 steps:
1. sets the codetag reference of the old page as empty (not pointing to any codetag);
2. subtracts counters of the new page to compensate for its own allocation;
3. sets codetag reference of the new page to point to the codetag of the old page.
This does not work if CONFIG_MEM_ALLOC_PROFILING_DEBUG=n because set_codetag_empty() becomes NOOP. Instead, let's simply swap codetag references so that the new page is referencing the old codetag and the old page is referencing the new codetag. This way accounting stays valid and the logic makes more sense.

Fixes e0a955bf7f61 ("mm/codetag: add pgalloc_tag_copy()")
Quoted 3 lines

This above Link: seems unusual. Maybe uses Closes instead like this:

Closed: https://lore.kernel.org/lkml/[email protected]/

Quoted 47 lines
---
 include/linux/pgalloc_tag.h |  4 ++--
 lib/alloc_tag.c             | 35 +++++++++++++++++++----------------
 mm/migrate.c                |  2 +-
 3 files changed, 22 insertions(+), 19 deletions(-)
include/linux/pgalloc_tag.h +2 -2
index 0e43ab653ab6..3469c4b20105 100644
@@ -231,7 +231,7 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
 }
 
 void pgalloc_tag_split(struct folio *folio, int old_order, int new_order);
-void pgalloc_tag_copy(struct folio *new, struct folio *old);
+void pgalloc_tag_swap(struct folio *new, struct folio *old);
 
 void __init alloc_tag_sec_init(void);
 
@@ -245,7 +245,7 @@ static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL
 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
 static inline void alloc_tag_sec_init(void) {}
 static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) {}
-static inline void pgalloc_tag_copy(struct folio *new, struct folio *old) {}
+static inline void pgalloc_tag_swap(struct folio *new, struct folio *old) {}
 
 #endif /* CONFIG_MEM_ALLOC_PROFILING */
 
lib/alloc_tag.c +6 -4
index 2414a7ee7ec7..b45efde50c40 100644
@@ -189,26 +189,29 @@ void pgalloc_tag_split(struct folio *folio, int old_order, int new_order)
        }
 }
 
-void pgalloc_tag_copy(struct folio *new, struct folio *old)
+void pgalloc_tag_swap(struct folio *new, struct folio *old)
 {
-       union pgtag_ref_handle handle;
-       union codetag_ref ref;
-       struct alloc_tag *tag;
+       union pgtag_ref_handle handles[2];
+       union codetag_ref refs[2];
+       struct alloc_tag *tags[2];
+       struct folio *folios[2] = {new, old};
+       int i;

lib/alloc_tag.c void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) · line 205

Quoted 10 lines
lib/alloc_tag.c +6 -3
 
-       tag = pgalloc_tag_get(&old->page);
-       if (!tag)
-               return;
+       for (i = 0; i < 2; i++) {
+               tags[i] = pgalloc_tag_get(&folios[i]->page);
+               if (!tags[i])
+                       return;
+               if (!get_page_tag_ref(&folios[i]->page, &refs[i], &handles[i]))
+                       return;

If any of the above getters fail on the second cycle, you will miss calling put_page_tag_ref(handles[0]) and releasing the page_tag_ref you obtained on the first cycle. It might be cleaner to drop the use of arrays and use new/old.

Quoted 37 lines
lib/alloc_tag.c +7 -9
+       }
 
-       if (!get_page_tag_ref(&new->page, &ref, &handle))
-               return;
+       swap(tags[0], tags[1]);
 
-       /* Clear the old ref to the original allocation tag. */
-       clear_page_tag_ref(&old->page);
-       /* Decrement the counters of the tag on get_new_folio. */
-       alloc_tag_sub(&ref, folio_size(new));
-       __alloc_tag_ref_set(&ref, tag);
-       update_page_tag_ref(handle, &ref);
-       put_page_tag_ref(handle);
+       for (i = 0; i < 2; i++) {
+               __alloc_tag_ref_set(&refs[i], tags[i]);
+               update_page_tag_ref(handles[i], &refs[i]);
+               put_page_tag_ref(handles[i]);
+       }
 }
 
 static void shutdown_mem_profiling(bool remove_file)
mm/migrate.c +1 -2
index 2ce6b4b814df..cc68583c86f9 100644
@@ -745,7 +745,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
                folio_set_readahead(newfolio);
 
        folio_copy_owner(newfolio, folio);
-       pgalloc_tag_copy(newfolio, folio);
+       pgalloc_tag_swap(newfolio, folio);
 
        mem_cgroup_migrate(folio, newfolio);
 }
--

2.39.2

#2 lore raw
Quoted 24 lines · Suren Baghdasaryan
Quoted 5 lines · David Wang

The initial solution for codetag adjustment during page migration uses three kinds of low level plumbings, those steps can be replaced by swapping tags, which only needs one kind of low level plumbing, and code is more clear.

This description does not explain the real issue. I would suggest something like:

Current solution to adjust codetag references during page migration is done in 3 steps:
1. sets the codetag reference of the old page as empty (not pointing to any codetag);
2. subtracts counters of the new page to compensate for its own allocation;
3. sets codetag reference of the new page to point to the codetag of the old page.
This does not work if CONFIG_MEM_ALLOC_PROFILING_DEBUG=n because set_codetag_empty() becomes NOOP. Instead, let's simply swap codetag references so that the new page is referencing the old codetag and the old page is referencing the new codetag. This way accounting stays valid and the logic makes more sense.

Quoted 8 lines
Fixes e0a955bf7f61 ("mm/codetag: add pgalloc_tag_copy()")
Quoted 3 lines

This above Link: seems unusual. Maybe uses Closes instead like this:

Closed: https://lore.kernel.org/lkml/[email protected]/

Sorry, fat fingered. Should have been:

This above Link: seems unusual. Maybe use Closes instead like this:

Quoted 101 lines
Quoted 48 lines
---
 include/linux/pgalloc_tag.h |  4 ++--
 lib/alloc_tag.c             | 35 +++++++++++++++++++----------------
 mm/migrate.c                |  2 +-
 3 files changed, 22 insertions(+), 19 deletions(-)
include/linux/pgalloc_tag.h +2 -2
index 0e43ab653ab6..3469c4b20105 100644
@@ -231,7 +231,7 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
 }
 
 void pgalloc_tag_split(struct folio *folio, int old_order, int new_order);
-void pgalloc_tag_copy(struct folio *new, struct folio *old);
+void pgalloc_tag_swap(struct folio *new, struct folio *old);
 
 void __init alloc_tag_sec_init(void);
 
@@ -245,7 +245,7 @@ static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL
 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
 static inline void alloc_tag_sec_init(void) {}
 static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) {}
-static inline void pgalloc_tag_copy(struct folio *new, struct folio *old) {}
+static inline void pgalloc_tag_swap(struct folio *new, struct folio *old) {}
 
 #endif /* CONFIG_MEM_ALLOC_PROFILING */
 
lib/alloc_tag.c +6 -4
index 2414a7ee7ec7..b45efde50c40 100644
@@ -189,26 +189,29 @@ void pgalloc_tag_split(struct folio *folio, int old_order, int new_order)
        }
 }
 
-void pgalloc_tag_copy(struct folio *new, struct folio *old)
+void pgalloc_tag_swap(struct folio *new, struct folio *old)
 {
-       union pgtag_ref_handle handle;
-       union codetag_ref ref;
-       struct alloc_tag *tag;
+       union pgtag_ref_handle handles[2];
+       union codetag_ref refs[2];
+       struct alloc_tag *tags[2];
+       struct folio *folios[2] = {new, old};
+       int i;

lib/alloc_tag.c void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) · line 205

Quoted 10 lines
lib/alloc_tag.c +6 -3
 
-       tag = pgalloc_tag_get(&old->page);
-       if (!tag)
-               return;
+       for (i = 0; i < 2; i++) {
+               tags[i] = pgalloc_tag_get(&folios[i]->page);
+               if (!tags[i])
+                       return;
+               if (!get_page_tag_ref(&folios[i]->page, &refs[i], &handles[i]))
+                       return;

If any of the above getters fail on the second cycle, you will miss calling put_page_tag_ref(handles[0]) and releasing the page_tag_ref you obtained on the first cycle. It might be cleaner to drop the use of arrays and use new/old.

Quoted 37 lines
lib/alloc_tag.c +7 -9
+       }
 
-       if (!get_page_tag_ref(&new->page, &ref, &handle))
-               return;
+       swap(tags[0], tags[1]);
 
-       /* Clear the old ref to the original allocation tag. */
-       clear_page_tag_ref(&old->page);
-       /* Decrement the counters of the tag on get_new_folio. */
-       alloc_tag_sub(&ref, folio_size(new));
-       __alloc_tag_ref_set(&ref, tag);
-       update_page_tag_ref(handle, &ref);
-       put_page_tag_ref(handle);
+       for (i = 0; i < 2; i++) {
+               __alloc_tag_ref_set(&refs[i], tags[i]);
+               update_page_tag_ref(handles[i], &refs[i]);
+               put_page_tag_ref(handles[i]);
+       }
 }
 
 static void shutdown_mem_profiling(bool remove_file)
mm/migrate.c +1 -2
index 2ce6b4b814df..cc68583c86f9 100644
@@ -745,7 +745,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
                folio_set_readahead(newfolio);
 
        folio_copy_owner(newfolio, folio);
-       pgalloc_tag_copy(newfolio, folio);
+       pgalloc_tag_swap(newfolio, folio);
 
        mem_cgroup_migrate(folio, newfolio);
 }
--

2.39.2

author
#3 lore raw
In reply to Suren Baghdasaryan

Hi,

Quoted 23 lines · Suren Baghdasaryan
Quoted 5 lines · David Wang

The initial solution for codetag adjustment during page migration uses three kinds of low level plumbings, those steps can be replaced by swapping tags, which only needs one kind of low level plumbing, and code is more clear.

This description does not explain the real issue. I would suggest something like:

Current solution to adjust codetag references during page migration is done in 3 steps:
1. sets the codetag reference of the old page as empty (not pointing to any codetag);
2. subtracts counters of the new page to compensate for its own allocation;
3. sets codetag reference of the new page to point to the codetag of the old page.
This does not work if CONFIG_MEM_ALLOC_PROFILING_DEBUG=n because set_codetag_empty() becomes NOOP. Instead, let's simply swap codetag references so that the new page is referencing the old codetag and the old page is referencing the new codetag. This way accounting stays valid and the logic makes more sense.

Quoted 10 lines
Fixes e0a955bf7f61 ("mm/codetag: add pgalloc_tag_copy()")
Quoted 3 lines

This above Link: seems unusual. Maybe uses Closes instead like this:

Closed: https://lore.kernel.org/lkml/[email protected]/

Copy that~!

Quoted 48 lines
Quoted 48 lines
---
 include/linux/pgalloc_tag.h |  4 ++--
 lib/alloc_tag.c             | 35 +++++++++++++++++++----------------
 mm/migrate.c                |  2 +-
 3 files changed, 22 insertions(+), 19 deletions(-)
include/linux/pgalloc_tag.h +2 -2
index 0e43ab653ab6..3469c4b20105 100644
@@ -231,7 +231,7 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
 }
 
 void pgalloc_tag_split(struct folio *folio, int old_order, int new_order);
-void pgalloc_tag_copy(struct folio *new, struct folio *old);
+void pgalloc_tag_swap(struct folio *new, struct folio *old);
 
 void __init alloc_tag_sec_init(void);
 
@@ -245,7 +245,7 @@ static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL
 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
 static inline void alloc_tag_sec_init(void) {}
 static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) {}
-static inline void pgalloc_tag_copy(struct folio *new, struct folio *old) {}
+static inline void pgalloc_tag_swap(struct folio *new, struct folio *old) {}
 
 #endif /* CONFIG_MEM_ALLOC_PROFILING */
 
lib/alloc_tag.c +6 -4
index 2414a7ee7ec7..b45efde50c40 100644
@@ -189,26 +189,29 @@ void pgalloc_tag_split(struct folio *folio, int old_order, int new_order)
        }
 }
 
-void pgalloc_tag_copy(struct folio *new, struct folio *old)
+void pgalloc_tag_swap(struct folio *new, struct folio *old)
 {
-       union pgtag_ref_handle handle;
-       union codetag_ref ref;
-       struct alloc_tag *tag;
+       union pgtag_ref_handle handles[2];
+       union codetag_ref refs[2];
+       struct alloc_tag *tags[2];
+       struct folio *folios[2] = {new, old};
+       int i;
 
Quoted 14 lines

lib/alloc_tag.c void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) · line 205

Quoted 9 lines
lib/alloc_tag.c +6 -3
-       tag = pgalloc_tag_get(&old->page);
-       if (!tag)
-               return;
+       for (i = 0; i < 2; i++) {
+               tags[i] = pgalloc_tag_get(&folios[i]->page);
+               if (!tags[i])
+                       return;
+               if (!get_page_tag_ref(&folios[i]->page, &refs[i], &handles[i]))
+                       return;

If any of the above getters fail on the second cycle, you will miss calling put_page_tag_ref(handles[0]) and releasing the page_tag_ref you obtained on the first cycle. It might be cleaner to drop the use of arrays and use new/old.

Thanks for catching this. I will make the change.

Quoted 38 lines
Quoted 38 lines
lib/alloc_tag.c +7 -9
+       }
 
-       if (!get_page_tag_ref(&new->page, &ref, &handle))
-               return;
+       swap(tags[0], tags[1]);
 
-       /* Clear the old ref to the original allocation tag. */
-       clear_page_tag_ref(&old->page);
-       /* Decrement the counters of the tag on get_new_folio. */
-       alloc_tag_sub(&ref, folio_size(new));
-       __alloc_tag_ref_set(&ref, tag);
-       update_page_tag_ref(handle, &ref);
-       put_page_tag_ref(handle);
+       for (i = 0; i < 2; i++) {
+               __alloc_tag_ref_set(&refs[i], tags[i]);
+               update_page_tag_ref(handles[i], &refs[i]);
+               put_page_tag_ref(handles[i]);
+       }
 }
 
 static void shutdown_mem_profiling(bool remove_file)
mm/migrate.c +1 -2
index 2ce6b4b814df..cc68583c86f9 100644
@@ -745,7 +745,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
                folio_set_readahead(newfolio);
 
        folio_copy_owner(newfolio, folio);
-       pgalloc_tag_copy(newfolio, folio);
+       pgalloc_tag_swap(newfolio, folio);
 
        mem_cgroup_migrate(folio, newfolio);
 }
--

2.39.2

author
#4 lore raw

PATCH v2 1/1 mm/codetag: swap tags when migrate pages

In reply to David Wang

Current solution to adjust codetag references during page migration is done in 3 steps:
1. sets the codetag reference of the old page as empty (not pointing to any codetag);
2. subtracts counters of the new page to compensate for its own allocation;
3. sets codetag reference of the new page to point to the codetag of the old page.
This does not work if CONFIG_MEM_ALLOC_PROFILING_DEBUG=n because set_codetag_empty() becomes NOOP. Instead, let's simply swap codetag references so that the new page is referencing the old codetag and the old page is referencing the new codetag. This way accounting stays valid and the logic makes more sense.

---
 include/linux/pgalloc_tag.h |  4 ++--
 lib/alloc_tag.c             | 36 ++++++++++++++++++++++--------------
 mm/migrate.c                |  2 +-
 3 files changed, 25 insertions(+), 17 deletions(-)
include/linux/pgalloc_tag.h +2 -2
index 0e43ab653ab6..3469c4b20105 100644
@@ -231,7 +231,7 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
 }
 
 void pgalloc_tag_split(struct folio *folio, int old_order, int new_order);
-void pgalloc_tag_copy(struct folio *new, struct folio *old);
+void pgalloc_tag_swap(struct folio *new, struct folio *old);
 
 void __init alloc_tag_sec_init(void);
 
@@ -245,7 +245,7 @@ static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL
 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
 static inline void alloc_tag_sec_init(void) {}
 static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) {}
-static inline void pgalloc_tag_copy(struct folio *new, struct folio *old) {}
+static inline void pgalloc_tag_swap(struct folio *new, struct folio *old) {}
 
 #endif /* CONFIG_MEM_ALLOC_PROFILING */
 
lib/alloc_tag.c +22 -14
index 2414a7ee7ec7..35f7560a309a 100644
@@ -189,26 +189,34 @@ void pgalloc_tag_split(struct folio *folio, int old_order, int new_order)
 	}
 }
 
-void pgalloc_tag_copy(struct folio *new, struct folio *old)
+void pgalloc_tag_swap(struct folio *new, struct folio *old)
 {
-	union pgtag_ref_handle handle;
-	union codetag_ref ref;
-	struct alloc_tag *tag;
+	union pgtag_ref_handle handle_old, handle_new;
+	union codetag_ref ref_old, ref_new;
+	struct alloc_tag *tag_old, *tag_new;
 
-	tag = pgalloc_tag_get(&old->page);
-	if (!tag)
+	tag_old = pgalloc_tag_get(&old->page);
+	if (!tag_old)
+		return;
+	tag_new = pgalloc_tag_get(&new->page);
+	if (!tag_new)
 		return;
 
-	if (!get_page_tag_ref(&new->page, &ref, &handle))
+	if (!get_page_tag_ref(&old->page, &ref_old, &handle_old))
 		return;
+	if (!get_page_tag_ref(&new->page, &ref_new, &handle_new)) {
+		put_page_tag_ref(handle_old);
+		return;
+	}
+
+	/* swap tags */
+	__alloc_tag_ref_set(&ref_old, tag_new);
+	update_page_tag_ref(handle_old, &ref_old);
+	__alloc_tag_ref_set(&ref_new, tag_old);
+	update_page_tag_ref(handle_new, &ref_new);
 
-	/* Clear the old ref to the original allocation tag. */
-	clear_page_tag_ref(&old->page);
-	/* Decrement the counters of the tag on get_new_folio. */
-	alloc_tag_sub(&ref, folio_size(new));
-	__alloc_tag_ref_set(&ref, tag);
-	update_page_tag_ref(handle, &ref);
-	put_page_tag_ref(handle);
+	put_page_tag_ref(handle_old);
+	put_page_tag_ref(handle_new);
 }
 
 static void shutdown_mem_profiling(bool remove_file)
mm/migrate.c +1 -1
index 2ce6b4b814df..cc68583c86f9 100644
@@ -745,7 +745,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 		folio_set_readahead(newfolio);
 
 	folio_copy_owner(newfolio, folio);
-	pgalloc_tag_copy(newfolio, folio);
+	pgalloc_tag_swap(newfolio, folio);
 
 	mem_cgroup_migrate(folio, newfolio);
 }
Signature · 1 line
2.39.2
maintainer Acked-by on v2 1/1
#5 lore raw
Quoted 14 lines · David Wang

Current solution to adjust codetag references during page migration is done in 3 steps:
1. sets the codetag reference of the old page as empty (not pointing to any codetag);
2. subtracts counters of the new page to compensate for its own allocation;
3. sets codetag reference of the new page to point to the codetag of the old page.
This does not work if CONFIG_MEM_ALLOC_PROFILING_DEBUG=n because set_codetag_empty() becomes NOOP. Instead, let's simply swap codetag references so that the new page is referencing the old codetag and the old page is referencing the new codetag. This way accounting stays valid and the logic makes more sense.

Quoted 5 lines

Thanks! This looks fine to me but IIRC I did not Ack this patch before. In the future please do not add Acked-by until you get an explicit Ack. That said,

Quoted 97 lines
---
 include/linux/pgalloc_tag.h |  4 ++--
 lib/alloc_tag.c             | 36 ++++++++++++++++++++++--------------
 mm/migrate.c                |  2 +-
 3 files changed, 25 insertions(+), 17 deletions(-)
include/linux/pgalloc_tag.h +2 -2
index 0e43ab653ab6..3469c4b20105 100644
@@ -231,7 +231,7 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
 }
 
 void pgalloc_tag_split(struct folio *folio, int old_order, int new_order);
-void pgalloc_tag_copy(struct folio *new, struct folio *old);
+void pgalloc_tag_swap(struct folio *new, struct folio *old);
 
 void __init alloc_tag_sec_init(void);
 
@@ -245,7 +245,7 @@ static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL
 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
 static inline void alloc_tag_sec_init(void) {}
 static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) {}
-static inline void pgalloc_tag_copy(struct folio *new, struct folio *old) {}
+static inline void pgalloc_tag_swap(struct folio *new, struct folio *old) {}
 
 #endif /* CONFIG_MEM_ALLOC_PROFILING */
 
lib/alloc_tag.c +22 -14
index 2414a7ee7ec7..35f7560a309a 100644
@@ -189,26 +189,34 @@ void pgalloc_tag_split(struct folio *folio, int old_order, int new_order)
        }
 }
 
-void pgalloc_tag_copy(struct folio *new, struct folio *old)
+void pgalloc_tag_swap(struct folio *new, struct folio *old)
 {
-       union pgtag_ref_handle handle;
-       union codetag_ref ref;
-       struct alloc_tag *tag;
+       union pgtag_ref_handle handle_old, handle_new;
+       union codetag_ref ref_old, ref_new;
+       struct alloc_tag *tag_old, *tag_new;
 
-       tag = pgalloc_tag_get(&old->page);
-       if (!tag)
+       tag_old = pgalloc_tag_get(&old->page);
+       if (!tag_old)
+               return;
+       tag_new = pgalloc_tag_get(&new->page);
+       if (!tag_new)
                return;
 
-       if (!get_page_tag_ref(&new->page, &ref, &handle))
+       if (!get_page_tag_ref(&old->page, &ref_old, &handle_old))
                return;
+       if (!get_page_tag_ref(&new->page, &ref_new, &handle_new)) {
+               put_page_tag_ref(handle_old);
+               return;
+       }
+
+       /* swap tags */
+       __alloc_tag_ref_set(&ref_old, tag_new);
+       update_page_tag_ref(handle_old, &ref_old);
+       __alloc_tag_ref_set(&ref_new, tag_old);
+       update_page_tag_ref(handle_new, &ref_new);
 
-       /* Clear the old ref to the original allocation tag. */
-       clear_page_tag_ref(&old->page);
-       /* Decrement the counters of the tag on get_new_folio. */
-       alloc_tag_sub(&ref, folio_size(new));
-       __alloc_tag_ref_set(&ref, tag);
-       update_page_tag_ref(handle, &ref);
-       put_page_tag_ref(handle);
+       put_page_tag_ref(handle_old);
+       put_page_tag_ref(handle_new);
 }
 
 static void shutdown_mem_profiling(bool remove_file)
mm/migrate.c +1 -2
index 2ce6b4b814df..cc68583c86f9 100644
@@ -745,7 +745,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
                folio_set_readahead(newfolio);
 
        folio_copy_owner(newfolio, folio);
-       pgalloc_tag_copy(newfolio, folio);
+       pgalloc_tag_swap(newfolio, folio);
 
        mem_cgroup_migrate(folio, newfolio);
 }
--

2.39.2

author
#6 lore raw
Quoted 15 lines · Suren Baghdasaryan
Quoted 14 lines · David Wang

Current solution to adjust codetag references during page migration is done in 3 steps:
1. sets the codetag reference of the old page as empty (not pointing to any codetag);
2. subtracts counters of the new page to compensate for its own allocation;
3. sets codetag reference of the new page to point to the codetag of the old page.
This does not work if CONFIG_MEM_ALLOC_PROFILING_DEBUG=n because set_codetag_empty() becomes NOOP. Instead, let's simply swap codetag references so that the new page is referencing the old codetag and the old page is referencing the new codetag. This way accounting stays valid and the logic makes more sense.

Quoted 7 lines
Quoted 5 lines

Thanks! This looks fine to me but IIRC I did not Ack this patch

Sorry, I am kind of new to the kernel development process, still catching up.

Quoted 2 lines

before. In the future please do not add Acked-by until you get an explicit Ack. That said,

Roger that~
I was wondering when "Acked-by~ or "Rreviewd-by" were added in other commits. Now I understand, I am not supposed to do that.

Thanks
David

Quoted 1 line
Acked-by on v2 1/1
#7 lore raw
In reply to David Wang
Quoted 14 lines · David Wang

Current solution to adjust codetag references during page migration is done in 3 steps:
1. sets the codetag reference of the old page as empty (not pointing to any codetag);
2. subtracts counters of the new page to compensate for its own allocation;
3. sets codetag reference of the new page to point to the codetag of the old page.
This does not work if CONFIG_MEM_ALLOC_PROFILING_DEBUG=n because set_codetag_empty() becomes NOOP. Instead, let's simply swap codetag references so that the new page is referencing the old codetag and the old page is referencing the new codetag. This way accounting stays valid and the logic makes more sense.

Quoted 5 lines