Skip to content

Commit ea4b5b3

Browse files
osalvadorvilardagaakpm00
authored andcommitted
mm,page_owner: update metadata for tail pages
Patch series "page_owner: Fix refcount imbalance and print fixup", v4. This series consists of a refactoring/correctness of updating the metadata of tail pages, a couple of fixups for the refcounting part and a fixup for the stack_start() function. From this series on, instead of counting the stacks, we count the outstanding nr_base_pages each stack has, which gives us a much better memory overview. The other fixup is for the migration part. A more detailed explanation can be found in the changelog of the respective patches. This patch (of 4): __set_page_owner_handle() and __reset_page_owner() update the metadata of all pages when the page is of a higher-order, but we miss to do the same when the pages are migrated. __folio_copy_owner() only updates the metadata of the head page, meaning that the information stored in the first page and the tail pages will not match. Strictly speaking that is not a big problem because 1) we do not print tail pages and 2) upon splitting all tail pages will inherit the metadata of the head page, but it is better to have all metadata in check should there be any problem, so it can ease debugging. For that purpose, a couple of helpers are created __update_page_owner_handle() which updates the metadata on allocation, and __update_page_owner_free_handle() which does the same when the page is freed. __folio_copy_owner() will make use of both as it needs to entirely replace the page_owner metadata for the new page. Link: https://lkml.kernel.org/r/20240404070702.2744-1-osalvador@suse.de Link: https://lkml.kernel.org/r/20240404070702.2744-2-osalvador@suse.de Signed-off-by: Oscar Salvador <osalvador@suse.de> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Tested-by: Kefeng Wang <wangkefeng.wang@huawei.com> Cc: Alexander Potapenko <glider@google.com> Cc: Alexandre Ghiti <alexghiti@rivosinc.com> Cc: Andrey Konovalov <andreyknvl@gmail.com> Cc: Marco Elver <elver@google.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Alexandre Ghiti <alexghiti@rivosinc.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent c0205ea commit ea4b5b3

1 file changed

Lines changed: 74 additions & 63 deletions

File tree

mm/page_owner.c

Lines changed: 74 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,58 @@ static void dec_stack_record_count(depot_stack_handle_t handle)
228228
refcount_dec(&stack_record->count);
229229
}
230230

231-
void __reset_page_owner(struct page *page, unsigned short order)
231+
static inline void __update_page_owner_handle(struct page_ext *page_ext,
232+
depot_stack_handle_t handle,
233+
unsigned short order,
234+
gfp_t gfp_mask,
235+
short last_migrate_reason, u64 ts_nsec,
236+
pid_t pid, pid_t tgid, char *comm)
232237
{
233238
int i;
239+
struct page_owner *page_owner;
240+
241+
for (i = 0; i < (1 << order); i++) {
242+
page_owner = get_page_owner(page_ext);
243+
page_owner->handle = handle;
244+
page_owner->order = order;
245+
page_owner->gfp_mask = gfp_mask;
246+
page_owner->last_migrate_reason = last_migrate_reason;
247+
page_owner->pid = pid;
248+
page_owner->tgid = tgid;
249+
page_owner->ts_nsec = ts_nsec;
250+
strscpy(page_owner->comm, comm,
251+
sizeof(page_owner->comm));
252+
__set_bit(PAGE_EXT_OWNER, &page_ext->flags);
253+
__set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
254+
page_ext = page_ext_next(page_ext);
255+
}
256+
}
257+
258+
static inline void __update_page_owner_free_handle(struct page_ext *page_ext,
259+
depot_stack_handle_t handle,
260+
unsigned short order,
261+
pid_t pid, pid_t tgid,
262+
u64 free_ts_nsec)
263+
{
264+
int i;
265+
struct page_owner *page_owner;
266+
267+
for (i = 0; i < (1 << order); i++) {
268+
page_owner = get_page_owner(page_ext);
269+
/* Only __reset_page_owner() wants to clear the bit */
270+
if (handle) {
271+
__clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
272+
page_owner->free_handle = handle;
273+
}
274+
page_owner->free_ts_nsec = free_ts_nsec;
275+
page_owner->free_pid = current->pid;
276+
page_owner->free_tgid = current->tgid;
277+
page_ext = page_ext_next(page_ext);
278+
}
279+
}
280+
281+
void __reset_page_owner(struct page *page, unsigned short order)
282+
{
234283
struct page_ext *page_ext;
235284
depot_stack_handle_t handle;
236285
depot_stack_handle_t alloc_handle;
@@ -245,16 +294,10 @@ void __reset_page_owner(struct page *page, unsigned short order)
245294
alloc_handle = page_owner->handle;
246295

247296
handle = save_stack(GFP_NOWAIT | __GFP_NOWARN);
248-
for (i = 0; i < (1 << order); i++) {
249-
__clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
250-
page_owner->free_handle = handle;
251-
page_owner->free_ts_nsec = free_ts_nsec;
252-
page_owner->free_pid = current->pid;
253-
page_owner->free_tgid = current->tgid;
254-
page_ext = page_ext_next(page_ext);
255-
page_owner = get_page_owner(page_ext);
256-
}
297+
__update_page_owner_free_handle(page_ext, handle, order, current->pid,
298+
current->tgid, free_ts_nsec);
257299
page_ext_put(page_ext);
300+
258301
if (alloc_handle != early_handle)
259302
/*
260303
* early_handle is being set as a handle for all those
@@ -266,44 +309,21 @@ void __reset_page_owner(struct page *page, unsigned short order)
266309
dec_stack_record_count(alloc_handle);
267310
}
268311

269-
static inline void __set_page_owner_handle(struct page_ext *page_ext,
270-
depot_stack_handle_t handle,
271-
unsigned short order, gfp_t gfp_mask)
272-
{
273-
struct page_owner *page_owner;
274-
int i;
275-
u64 ts_nsec = local_clock();
276-
277-
for (i = 0; i < (1 << order); i++) {
278-
page_owner = get_page_owner(page_ext);
279-
page_owner->handle = handle;
280-
page_owner->order = order;
281-
page_owner->gfp_mask = gfp_mask;
282-
page_owner->last_migrate_reason = -1;
283-
page_owner->pid = current->pid;
284-
page_owner->tgid = current->tgid;
285-
page_owner->ts_nsec = ts_nsec;
286-
strscpy(page_owner->comm, current->comm,
287-
sizeof(page_owner->comm));
288-
__set_bit(PAGE_EXT_OWNER, &page_ext->flags);
289-
__set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
290-
291-
page_ext = page_ext_next(page_ext);
292-
}
293-
}
294-
295312
noinline void __set_page_owner(struct page *page, unsigned short order,
296313
gfp_t gfp_mask)
297314
{
298315
struct page_ext *page_ext;
316+
u64 ts_nsec = local_clock();
299317
depot_stack_handle_t handle;
300318

301319
handle = save_stack(gfp_mask);
302320

303321
page_ext = page_ext_get(page);
304322
if (unlikely(!page_ext))
305323
return;
306-
__set_page_owner_handle(page_ext, handle, order, gfp_mask);
324+
__update_page_owner_handle(page_ext, handle, order, gfp_mask, -1,
325+
current->pid, current->tgid, ts_nsec,
326+
current->comm);
307327
page_ext_put(page_ext);
308328
inc_stack_record_count(handle, gfp_mask);
309329
}
@@ -342,7 +362,7 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old)
342362
{
343363
struct page_ext *old_ext;
344364
struct page_ext *new_ext;
345-
struct page_owner *old_page_owner, *new_page_owner;
365+
struct page_owner *old_page_owner;
346366

347367
old_ext = page_ext_get(&old->page);
348368
if (unlikely(!old_ext))
@@ -355,31 +375,21 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old)
355375
}
356376

357377
old_page_owner = get_page_owner(old_ext);
358-
new_page_owner = get_page_owner(new_ext);
359-
new_page_owner->order = old_page_owner->order;
360-
new_page_owner->gfp_mask = old_page_owner->gfp_mask;
361-
new_page_owner->last_migrate_reason =
362-
old_page_owner->last_migrate_reason;
363-
new_page_owner->handle = old_page_owner->handle;
364-
new_page_owner->pid = old_page_owner->pid;
365-
new_page_owner->tgid = old_page_owner->tgid;
366-
new_page_owner->free_pid = old_page_owner->free_pid;
367-
new_page_owner->free_tgid = old_page_owner->free_tgid;
368-
new_page_owner->ts_nsec = old_page_owner->ts_nsec;
369-
new_page_owner->free_ts_nsec = old_page_owner->ts_nsec;
370-
strcpy(new_page_owner->comm, old_page_owner->comm);
371-
378+
__update_page_owner_handle(new_ext, old_page_owner->handle,
379+
old_page_owner->order, old_page_owner->gfp_mask,
380+
old_page_owner->last_migrate_reason,
381+
old_page_owner->ts_nsec, old_page_owner->pid,
382+
old_page_owner->tgid, old_page_owner->comm);
372383
/*
373-
* We don't clear the bit on the old folio as it's going to be freed
374-
* after migration. Until then, the info can be useful in case of
375-
* a bug, and the overall stats will be off a bit only temporarily.
376-
* Also, migrate_misplaced_transhuge_page() can still fail the
377-
* migration and then we want the old folio to retain the info. But
378-
* in that case we also don't need to explicitly clear the info from
379-
* the new page, which will be freed.
384+
* Do not proactively clear PAGE_EXT_OWNER{_ALLOCATED} bits as the folio
385+
* will be freed after migration. Keep them until then as they may be
386+
* useful.
380387
*/
381-
__set_bit(PAGE_EXT_OWNER, &new_ext->flags);
382-
__set_bit(PAGE_EXT_OWNER_ALLOCATED, &new_ext->flags);
388+
__update_page_owner_free_handle(new_ext, 0, old_page_owner->order,
389+
old_page_owner->free_pid,
390+
old_page_owner->free_tgid,
391+
old_page_owner->free_ts_nsec);
392+
383393
page_ext_put(new_ext);
384394
page_ext_put(old_ext);
385395
}
@@ -787,8 +797,9 @@ static void init_pages_in_zone(pg_data_t *pgdat, struct zone *zone)
787797
goto ext_put_continue;
788798

789799
/* Found early allocated page */
790-
__set_page_owner_handle(page_ext, early_handle,
791-
0, 0);
800+
__update_page_owner_handle(page_ext, early_handle, 0, 0,
801+
-1, local_clock(), current->pid,
802+
current->tgid, current->comm);
792803
count++;
793804
ext_put_continue:
794805
page_ext_put(page_ext);

0 commit comments

Comments
 (0)