Re: [PATCH V3] mm/vmstat: Add events for THP migration without split

From: Anshuman Khandual
Date: Wed Jul 08 2020 - 03:16:45 EST



On 07/08/2020 05:31 AM, John Hubbard wrote:
> On 2020-07-06 17:06, Anshuman Khandual wrote:
>> Add following new vmstat events which will help in validating THP migration
>> without split. Statistics reported through these new VM events will help in
>> performance debugging.
>>
>> 1. THP_MIGRATION_SUCCESS
>> 2. THP_MIGRATION_FAILURE
>> 3. THP_MIGRATION_SPLIT
>>
>> In addition, these new events also update normal page migration statistics
>> appropriately via PGMIGRATE_SUCCESS and PGMIGRATE_FAILURE. While here, this
>> updates current trace event 'mm_migrate_pages' to accommodate now available
>> THP statistics.
>>
>> Cc: Daniel Jordan <daniel.m.jordan@xxxxxxxxxx>
>> Cc: Hugh Dickins <hughd@xxxxxxxxxx>
>> Cc: Matthew Wilcox <willy@xxxxxxxxxxxxx>
>> Cc: Zi Yan <ziy@xxxxxxxxxx>
>> Cc: John Hubbard <jhubbard@xxxxxxxxxx>
>> Cc: Naoya Horiguchi <n-horiguchi@xxxxxxxxxxxxx>
>> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
>> Cc: linux-mm@xxxxxxxxx
>> Cc: linux-kernel@xxxxxxxxxxxxxxx
>> Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@xxxxxxx>
>
>
> This is missing the Documentation/ Cc's, did you run scripts/get_maintainer.pl? I'm
> adding linux-doc and Jonathan Corbet, for now at least. And I'll refrain from
> trimming the reply.

I had missed the Documention/ Cc's, thanks for adding them here. Will
update the Cc list next time around.

>
>
>> ---
>> Changes in V3:
>>
>> - Formatted new events documentation with 'fmt' tool per Matthew
>> - Made events universally available i.e dropped ARCH_ENABLE_THP_MIGRATION
>> - Added THP_MIGRATION_SPLIT
>> - Updated trace_mm_migrate_pages() with THP events
>> - Made THP events update normal page migration events as well
>>
>> Changes in V2: (https://patchwork.kernel.org/patch/11586893/)
>>
>> - Dropped PMD reference both from code and commit message per Matthew
>> - Added documentation and updated the commit message per Daniel
>>
>> Changes in V1: (https://patchwork.kernel.org/patch/11564497/)
>>
>> - Changed function name as thp_pmd_migration_success() per John
>> - Folded in a fix (https://patchwork.kernel.org/patch/11563009/) from Hugh
>>
>> Changes in RFC V2: (https://patchwork.kernel.org/patch/11554861/)
>>
>> - Decopupled and renamed VM events from their implementation per Zi and John
>> - Added THP_PMD_MIGRATION_FAILURE VM event upon allocation failure and split
>>
>> Changes in RFC V1: (https://patchwork.kernel.org/patch/11542055/)
>>
>> Â Documentation/vm/page_migration.rst | 19 +++++++++++
>> Â include/linux/vm_event_item.hÂÂÂÂÂÂ |Â 3 ++
>> Â include/trace/events/migrate.hÂÂÂÂÂ | 17 ++++++++--
>> Â mm/migrate.cÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ | 49 ++++++++++++++++++++++++++---
>> Â mm/vmstat.cÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |Â 3 ++
>> Â 5 files changed, 84 insertions(+), 7 deletions(-)
>>
>> diff --git a/Documentation/vm/page_migration.rst b/Documentation/vm/page_migration.rst
>> index 1d6cd7db4e43..e65d49f3cf86 100644
>> --- a/Documentation/vm/page_migration.rst
>> +++ b/Documentation/vm/page_migration.rst
>> @@ -253,5 +253,24 @@ which are function pointers of struct address_space_operations.
>> ÂÂÂÂÂÂ PG_isolated is alias with PG_reclaim flag so driver shouldn't use the flag
>> ÂÂÂÂÂÂ for own purpose.
>> Â +Quantifying Migration
>> +=====================
>> +Following events can be used to quantify page migration.
>> +
>> +1. PGMIGRATE_SUCCESSÂÂÂÂÂÂ /* Normal page migration success */
>
>
> This doesn't explain what's going on with the new combined counter
> behavior. See the proposal below.
>
>
>> +2. PGMIGRATE_FAILÂÂÂÂÂÂÂÂÂ /* Normal page migration failure */
>> +3. THP_MIGRATION_SUCCESSÂÂ /* Transparent huge page migration success */
>> +4. THP_MIGRATION_FAILUREÂÂ /* Transparent huge page migration failure */
>
>
> Shouldn't that be THP_MIGRATION_FAIL, in order to match the existing naming
> scheme here?

Sure, will change it.

>
>
>> +5. THP_MIGRATION_SPLITÂÂÂÂ /* Transparent huge page got split, retried */
>> +
>> +THP_MIGRATION_SUCCESS is when THP is migrated successfully without getting
>> +split into it's subpages. THP_MIGRATION_FAILURE is when THP could neither
>> +be migrated nor be split. THP_MIGRATION_SPLIT is when THP could not
>> +just be migrated as is but instead get split into it's subpages and later
>> +retried as normal pages. THP events would also update normal page migration
>> +statistics PGMIGRATE_SUCCESS and PGMIGRATE_FAILURE. These events will help
>> +in quantifying and analyzing various THP migration events including both
>> +success and failure cases.
>> +
>
>
> I would like to propose the following instead of the above. It's rewritten
> so as to add explanations for the new behavior, remove the redundancy (the last
> sentence in particular is a justification, and belongs in a commit log if anywhere,
> not in the doc itself), fix a few typos, use the full 80 columns, and clarify a
> bit as well. It uses the new THP_MIGRATION_FAIL name:
>
>
> Monitoring Migration
> =====================
>
> The following events (counters) can be used to monitor page migration.
>
> 1. PGMIGRATE_SUCCESS: Normal page migration success. Each count means that a
> ÂÂ page was migrated. If the page was a non-THP page, then this counter is
> ÂÂ increased by one. If the page was a THP, then this counter is increased by
> ÂÂ the number of THP subpages. For example, migration of a single 2MB THP that
> ÂÂ has 4KB-size base pages (subpages) will cause this counter to increase by
> ÂÂ 512.
>
> 2. PGMIGRATE_FAIL: Normal page migration failure. Same counting rules as for
> ÂÂ _SUCCESS, above: this will be increased by the number of subpages, if it was
> ÂÂ a THP.
>
> 3. THP_MIGRATION_SUCCESS: A THP was migrated without being split.
>
> 4. THP_MIGRATION_FAIL: A THP could not be migrated at all, even after being
> ÂÂ split.
>
> 5. THP_MIGRATION_SPLIT: A THP was migrated, but not as such: first, the THP had
> ÂÂ to be split. After splitting, a migration retry was used for the sub-pages,
> ÂÂ and that retry succeeded.

The last part might not be true. All that THP_MIGRATION_SPLIT tracks is whether
the THP was split or not. Whether it's subpages got successfully migrated later
or not, would be hidden in overall PGMIGRATE_SUCCESS and PGMIGRATE_FAIL values.

>
> THP_MIGRATION_* events also update the appropriate PGMIGRATE_SUCCESS or
> PGMIGRATE_FAILURE events. For example, a THP migration failure will cause both
> THP_MIGRATION_FAIL and PGMIGRATE_FAIL to increase.

Above explanation (probably with some changes) looks better, will update.

>
>
>> Â Christoph Lameter, May 8, 2006.
>> Â Minchan Kim, Mar 28, 2016.
>> diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h
>> index 24fc7c3ae7d6..5e7ffa025589 100644
>> --- a/include/linux/vm_event_item.h
>> +++ b/include/linux/vm_event_item.h
>> @@ -95,6 +95,9 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
>> ÂÂÂÂÂÂÂÂÂ THP_ZERO_PAGE_ALLOC_FAILED,
>> ÂÂÂÂÂÂÂÂÂ THP_SWPOUT,
>> ÂÂÂÂÂÂÂÂÂ THP_SWPOUT_FALLBACK,
>> +ÂÂÂÂÂÂÂ THP_MIGRATION_SUCCESS,
>> +ÂÂÂÂÂÂÂ THP_MIGRATION_FAILURE,
>> +ÂÂÂÂÂÂÂ THP_MIGRATION_SPLIT,
>> Â #endif
>> Â #ifdef CONFIG_MEMORY_BALLOON
>> ÂÂÂÂÂÂÂÂÂ BALLOON_INFLATE,
>> diff --git a/include/trace/events/migrate.h b/include/trace/events/migrate.h
>> index 705b33d1e395..4d434398d64d 100644
>> --- a/include/trace/events/migrate.h
>> +++ b/include/trace/events/migrate.h
>> @@ -46,13 +46,18 @@ MIGRATE_REASON
>> Â TRACE_EVENT(mm_migrate_pages,
>> Â ÂÂÂÂÂ TP_PROTO(unsigned long succeeded, unsigned long failed,
>> -ÂÂÂÂÂÂÂÂ enum migrate_mode mode, int reason),
>> +ÂÂÂÂÂÂÂÂ unsigned long thp_succeeded, unsigned long thp_failed,
>> +ÂÂÂÂÂÂÂÂ unsigned long thp_split, enum migrate_mode mode, int reason),
>> Â -ÂÂÂ TP_ARGS(succeeded, failed, mode, reason),
>> +ÂÂÂ TP_ARGS(succeeded, failed, thp_succeeded, thp_failed,
>> +ÂÂÂÂÂÂÂ thp_split, mode, reason),
>> Â ÂÂÂÂÂ TP_STRUCT__entry(
>> ÂÂÂÂÂÂÂÂÂ __field(ÂÂÂ unsigned long,ÂÂÂÂÂÂÂ succeeded)
>> ÂÂÂÂÂÂÂÂÂ __field(ÂÂÂ unsigned long,ÂÂÂÂÂÂÂ failed)
>> +ÂÂÂÂÂÂÂ __field(ÂÂÂ unsigned long,ÂÂÂÂÂÂÂ thp_succeeded)
>> +ÂÂÂÂÂÂÂ __field(ÂÂÂ unsigned long,ÂÂÂÂÂÂÂ thp_failed)
>> +ÂÂÂÂÂÂÂ __field(ÂÂÂ unsigned long,ÂÂÂÂÂÂÂ thp_split)
>> ÂÂÂÂÂÂÂÂÂ __field(ÂÂÂ enum migrate_mode,ÂÂÂ mode)
>> ÂÂÂÂÂÂÂÂÂ __field(ÂÂÂ int,ÂÂÂÂÂÂÂÂÂÂÂ reason)
>> ÂÂÂÂÂ ),
>> @@ -60,13 +65,19 @@ TRACE_EVENT(mm_migrate_pages,
>> ÂÂÂÂÂ TP_fast_assign(
>> ÂÂÂÂÂÂÂÂÂ __entry->succeededÂÂÂ = succeeded;
>> ÂÂÂÂÂÂÂÂÂ __entry->failedÂÂÂÂÂÂÂ = failed;
>> +ÂÂÂÂÂÂÂ __entry->thp_succeededÂÂÂ = thp_succeeded;
>> +ÂÂÂÂÂÂÂ __entry->thp_failedÂÂÂ = thp_failed;
>> +ÂÂÂÂÂÂÂ __entry->thp_splitÂÂÂ = thp_split;
>> ÂÂÂÂÂÂÂÂÂ __entry->modeÂÂÂÂÂÂÂ = mode;
>> ÂÂÂÂÂÂÂÂÂ __entry->reasonÂÂÂÂÂÂÂ = reason;
>> ÂÂÂÂÂ ),
>> Â -ÂÂÂ TP_printk("nr_succeeded=%lu nr_failed=%lu mode=%s reason=%s",
>> +ÂÂÂ TP_printk("nr_succeeded=%lu nr_failed=%lu nr_thp_succeeded=%lu nr_thp_failed=%lu nr_thp_split=%lu mode=%s reason=%s",
>> ÂÂÂÂÂÂÂÂÂ __entry->succeeded,
>> ÂÂÂÂÂÂÂÂÂ __entry->failed,
>> +ÂÂÂÂÂÂÂ __entry->thp_succeeded,
>> +ÂÂÂÂÂÂÂ __entry->thp_failed,
>> +ÂÂÂÂÂÂÂ __entry->thp_split,
>> ÂÂÂÂÂÂÂÂÂ __print_symbolic(__entry->mode, MIGRATE_MODE),
>> ÂÂÂÂÂÂÂÂÂ __print_symbolic(__entry->reason, MIGRATE_REASON))
>> Â );
>> diff --git a/mm/migrate.c b/mm/migrate.c
>> index f37729673558..baf3cc477d11 100644
>> --- a/mm/migrate.c
>> +++ b/mm/migrate.c
>> @@ -1429,22 +1429,35 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
>> ÂÂÂÂÂÂÂÂÂ enum migrate_mode mode, int reason)
>> Â {
>> ÂÂÂÂÂ int retry = 1;
>> +ÂÂÂ int thp_retry = 1;
>> ÂÂÂÂÂ int nr_failed = 0;
>> ÂÂÂÂÂ int nr_succeeded = 0;
>> +ÂÂÂ int nr_thp_succeeded = 0;
>> +ÂÂÂ int nr_thp_failed = 0;
>> +ÂÂÂ int nr_thp_split = 0;
>> ÂÂÂÂÂ int pass = 0;
>> +ÂÂÂ bool is_thp = false;
>> ÂÂÂÂÂ struct page *page;
>> ÂÂÂÂÂ struct page *page2;
>> ÂÂÂÂÂ int swapwrite = current->flags & PF_SWAPWRITE;
>> -ÂÂÂ int rc;
>> +ÂÂÂ int rc, thp_nr_pages;
>> Â ÂÂÂÂÂ if (!swapwrite)
>> ÂÂÂÂÂÂÂÂÂ current->flags |= PF_SWAPWRITE;
>> Â -ÂÂÂ for(pass = 0; pass < 10 && retry; pass++) {
>> +ÂÂÂ for (pass = 0; pass < 10 && (retry || thp_retry); pass++) {
>> ÂÂÂÂÂÂÂÂÂ retry = 0;
>> +ÂÂÂÂÂÂÂ thp_retry = 0;
>> Â ÂÂÂÂÂÂÂÂÂ list_for_each_entry_safe(page, page2, from, lru) {
>> Â retry:
>> +ÂÂÂÂÂÂÂÂÂÂÂ /*
>> +ÂÂÂÂÂÂÂÂÂÂÂÂ * THP statistics is based on the source huge page.
>> +ÂÂÂÂÂÂÂÂÂÂÂÂ * Capture required information that might get lost
>> +ÂÂÂÂÂÂÂÂÂÂÂÂ * during migration.
>> +ÂÂÂÂÂÂÂÂÂÂÂÂ */
>> +ÂÂÂÂÂÂÂÂÂÂÂ is_thp = PageTransHuge(page);
>> +ÂÂÂÂÂÂÂÂÂÂÂ thp_nr_pages = hpage_nr_pages(page);
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ cond_resched();
>> Â ÂÂÂÂÂÂÂÂÂÂÂÂÂ if (PageHuge(page))
>> @@ -1475,15 +1488,30 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unlock_page(page);
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (!rc) {
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ list_safe_reset_next(page, page2, lru);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_thp_split++;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto retry;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (is_thp) {
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_thp_failed++;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_failed += thp_nr_pages;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_failed++;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ goto out;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ case -EAGAIN:
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (is_thp) {
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ thp_retry++;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ retry++;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ case MIGRATEPAGE_SUCCESS:
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (is_thp) {
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_thp_succeeded++;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_succeeded += thp_nr_pages;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_succeeded++;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ default:
>> @@ -1493,19 +1521,32 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ * removed from migration page list and not
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ * retried in the next outer loop.
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ */
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ if (is_thp) {
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_thp_failed++;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_failed += thp_nr_pages;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ nr_failed++;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ break;
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂ }
>> -ÂÂÂ nr_failed += retry;
>> +ÂÂÂ nr_failed += retry + thp_retry;
>> +ÂÂÂ nr_thp_failed += thp_retry;
>> ÂÂÂÂÂ rc = nr_failed;
>> Â out:
>> ÂÂÂÂÂ if (nr_succeeded)
>> ÂÂÂÂÂÂÂÂÂ count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
>> ÂÂÂÂÂ if (nr_failed)
>> ÂÂÂÂÂÂÂÂÂ count_vm_events(PGMIGRATE_FAIL, nr_failed);
>> -ÂÂÂ trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
>> +ÂÂÂ if (nr_thp_succeeded)
>> +ÂÂÂÂÂÂÂ count_vm_events(THP_MIGRATION_SUCCESS, nr_thp_succeeded);
>> +ÂÂÂ if (nr_thp_failed)
>> +ÂÂÂÂÂÂÂ count_vm_events(THP_MIGRATION_FAILURE, nr_thp_failed);
>> +ÂÂÂ if (nr_thp_split)
>> +ÂÂÂÂÂÂÂ count_vm_events(THP_MIGRATION_SPLIT, nr_thp_split);
>
> As mentioned in the other thread, those "ifs" should be removed. I think you
> can also get away with removing the two pre-existing "ifs" in the same
> patch, too, just to keep the whole set consistent and more readable.

Makes sense, will do.