Skip to content

Commit 51f43d5

Browse files
zq-david-wangakpm00
authored andcommitted
mm/codetag: swap tags when migrate pages
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. Link: https://lkml.kernel.org/r/20241129025213.34836-1-00107082@163.com Fixes: e0a955b ("mm/codetag: add pgalloc_tag_copy()") Signed-off-by: David Wang <00107082@163.com> Closes: https://lore.kernel.org/lkml/20241124074318.399027-1-00107082@163.com/ Acked-by: Suren Baghdasaryan <surenb@google.com> Suggested-by: Suren Baghdasaryan <surenb@google.com> Acked-by: Yu Zhao <yuzhao@google.com> Cc: Kent Overstreet <kent.overstreet@linux.dev> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 914eec5 commit 51f43d5

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

include/linux/pgalloc_tag.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
231231
}
232232

233233
void pgalloc_tag_split(struct folio *folio, int old_order, int new_order);
234-
void pgalloc_tag_copy(struct folio *new, struct folio *old);
234+
void pgalloc_tag_swap(struct folio *new, struct folio *old);
235235

236236
void __init alloc_tag_sec_init(void);
237237

@@ -245,7 +245,7 @@ static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL
245245
static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
246246
static inline void alloc_tag_sec_init(void) {}
247247
static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new_order) {}
248-
static inline void pgalloc_tag_copy(struct folio *new, struct folio *old) {}
248+
static inline void pgalloc_tag_swap(struct folio *new, struct folio *old) {}
249249

250250
#endif /* CONFIG_MEM_ALLOC_PROFILING */
251251

lib/alloc_tag.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,26 +189,34 @@ void pgalloc_tag_split(struct folio *folio, int old_order, int new_order)
189189
}
190190
}
191191

192-
void pgalloc_tag_copy(struct folio *new, struct folio *old)
192+
void pgalloc_tag_swap(struct folio *new, struct folio *old)
193193
{
194-
union pgtag_ref_handle handle;
195-
union codetag_ref ref;
196-
struct alloc_tag *tag;
194+
union pgtag_ref_handle handle_old, handle_new;
195+
union codetag_ref ref_old, ref_new;
196+
struct alloc_tag *tag_old, *tag_new;
197197

198-
tag = pgalloc_tag_get(&old->page);
199-
if (!tag)
198+
tag_old = pgalloc_tag_get(&old->page);
199+
if (!tag_old)
200+
return;
201+
tag_new = pgalloc_tag_get(&new->page);
202+
if (!tag_new)
200203
return;
201204

202-
if (!get_page_tag_ref(&new->page, &ref, &handle))
205+
if (!get_page_tag_ref(&old->page, &ref_old, &handle_old))
203206
return;
207+
if (!get_page_tag_ref(&new->page, &ref_new, &handle_new)) {
208+
put_page_tag_ref(handle_old);
209+
return;
210+
}
211+
212+
/* swap tags */
213+
__alloc_tag_ref_set(&ref_old, tag_new);
214+
update_page_tag_ref(handle_old, &ref_old);
215+
__alloc_tag_ref_set(&ref_new, tag_old);
216+
update_page_tag_ref(handle_new, &ref_new);
204217

205-
/* Clear the old ref to the original allocation tag. */
206-
clear_page_tag_ref(&old->page);
207-
/* Decrement the counters of the tag on get_new_folio. */
208-
alloc_tag_sub(&ref, folio_size(new));
209-
__alloc_tag_ref_set(&ref, tag);
210-
update_page_tag_ref(handle, &ref);
211-
put_page_tag_ref(handle);
218+
put_page_tag_ref(handle_old);
219+
put_page_tag_ref(handle_new);
212220
}
213221

214222
static void shutdown_mem_profiling(bool remove_file)

mm/migrate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
745745
folio_set_readahead(newfolio);
746746

747747
folio_copy_owner(newfolio, folio);
748-
pgalloc_tag_copy(newfolio, folio);
748+
pgalloc_tag_swap(newfolio, folio);
749749

750750
mem_cgroup_migrate(folio, newfolio);
751751
}

0 commit comments

Comments
 (0)