[v1 resend 02/12] mm/migrate_device: flags for selecting device private THP pages

Balbir Singh posted 12 patches 3 months ago
There is a newer version of this series
[v1 resend 02/12] mm/migrate_device: flags for selecting device private THP pages
Posted by Balbir Singh 3 months ago
Add flags to mark zone device migration pages.

MIGRATE_VMA_SELECT_COMPOUND will be used to select THP pages during
migrate_vma_setup() and MIGRATE_PFN_COMPOUND will make migrating
device pages as compound pages during device pfn migration.

Cc: Karol Herbst <kherbst@redhat.com>
Cc: Lyude Paul <lyude@redhat.com>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: David Airlie <airlied@gmail.com>
Cc: Simona Vetter <simona@ffwll.ch>
Cc: "Jérôme Glisse" <jglisse@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Zi Yan <ziy@nvidia.com>
Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Jane Chu <jane.chu@oracle.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Donet Tom <donettom@linux.ibm.com>

Signed-off-by: Balbir Singh <balbirs@nvidia.com>
---
 include/linux/migrate.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/include/linux/migrate.h b/include/linux/migrate.h
index aaa2114498d6..1661e2d5479a 100644
--- a/include/linux/migrate.h
+++ b/include/linux/migrate.h
@@ -167,6 +167,7 @@ static inline int migrate_misplaced_folio(struct folio *folio, int node)
 #define MIGRATE_PFN_VALID	(1UL << 0)
 #define MIGRATE_PFN_MIGRATE	(1UL << 1)
 #define MIGRATE_PFN_WRITE	(1UL << 3)
+#define MIGRATE_PFN_COMPOUND	(1UL << 4)
 #define MIGRATE_PFN_SHIFT	6
 
 static inline struct page *migrate_pfn_to_page(unsigned long mpfn)
@@ -185,6 +186,7 @@ enum migrate_vma_direction {
 	MIGRATE_VMA_SELECT_SYSTEM = 1 << 0,
 	MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1,
 	MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2,
+	MIGRATE_VMA_SELECT_COMPOUND = 1 << 3,
 };
 
 struct migrate_vma {
-- 
2.49.0

Re: [v1 resend 02/12] mm/migrate_device: flags for selecting device private THP pages
Posted by Matthew Brost 2 months, 3 weeks ago
On Fri, Jul 04, 2025 at 09:35:01AM +1000, Balbir Singh wrote:
> Add flags to mark zone device migration pages.
> 
> MIGRATE_VMA_SELECT_COMPOUND will be used to select THP pages during
> migrate_vma_setup() and MIGRATE_PFN_COMPOUND will make migrating
> device pages as compound pages during device pfn migration.
> 
> Cc: Karol Herbst <kherbst@redhat.com>
> Cc: Lyude Paul <lyude@redhat.com>
> Cc: Danilo Krummrich <dakr@kernel.org>
> Cc: David Airlie <airlied@gmail.com>
> Cc: Simona Vetter <simona@ffwll.ch>
> Cc: "Jérôme Glisse" <jglisse@redhat.com>
> Cc: Shuah Khan <shuah@kernel.org>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Barry Song <baohua@kernel.org>
> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: Matthew Wilcox <willy@infradead.org>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: Zi Yan <ziy@nvidia.com>
> Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
> Cc: Jane Chu <jane.chu@oracle.com>
> Cc: Alistair Popple <apopple@nvidia.com>
> Cc: Donet Tom <donettom@linux.ibm.com>
> 
> Signed-off-by: Balbir Singh <balbirs@nvidia.com>
> ---
>  include/linux/migrate.h | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/include/linux/migrate.h b/include/linux/migrate.h
> index aaa2114498d6..1661e2d5479a 100644
> --- a/include/linux/migrate.h
> +++ b/include/linux/migrate.h
> @@ -167,6 +167,7 @@ static inline int migrate_misplaced_folio(struct folio *folio, int node)
>  #define MIGRATE_PFN_VALID	(1UL << 0)
>  #define MIGRATE_PFN_MIGRATE	(1UL << 1)
>  #define MIGRATE_PFN_WRITE	(1UL << 3)
> +#define MIGRATE_PFN_COMPOUND	(1UL << 4)

Can some documentation be added around the usage of MIGRATE_PFN_COMPOUND?

In particular, how the field is used in relation to the migrate_vma_* functions?

For example, when MIGRATE_PFN_COMPOUND is set in a returned mpfn, the caller
should check the order of the folio associated with that mpfn, and then expect
the next 1 << order entries in the source array to be unpopulated.

Likewise, when a caller populates an mpfn with MIGRATE_PFN_COMPOUND, the next 1
<< order entries should also be unpopulated.

This behavior wasn’t immediately obvious, so I think it would be helpful to
document it to avoid requiring readers to reverse-engineer the code.

>  #define MIGRATE_PFN_SHIFT	6
>  
>  static inline struct page *migrate_pfn_to_page(unsigned long mpfn)
> @@ -185,6 +186,7 @@ enum migrate_vma_direction {

Maybe out of scope, but migrate_vma_direction is not a great name. While
we are here, it could be cleaned up.

>  	MIGRATE_VMA_SELECT_SYSTEM = 1 << 0,
>  	MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1,
>  	MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2,
> +	MIGRATE_VMA_SELECT_COMPOUND = 1 << 3,

Same here.

For example, what happens if MIGRATE_VMA_SELECT_COMPOUND is select vs unselected
when higher order folio is found.

Matt

>  };
>  
>  struct migrate_vma {
> -- 
> 2.49.0
> 
Re: [v1 resend 02/12] mm/migrate_device: flags for selecting device private THP pages
Posted by Alistair Popple 3 months ago
On Fri, Jul 04, 2025 at 09:35:01AM +1000, Balbir Singh wrote:
> Add flags to mark zone device migration pages.
> 
> MIGRATE_VMA_SELECT_COMPOUND will be used to select THP pages during
> migrate_vma_setup() and MIGRATE_PFN_COMPOUND will make migrating
> device pages as compound pages during device pfn migration.
> 
> Cc: Karol Herbst <kherbst@redhat.com>
> Cc: Lyude Paul <lyude@redhat.com>
> Cc: Danilo Krummrich <dakr@kernel.org>
> Cc: David Airlie <airlied@gmail.com>
> Cc: Simona Vetter <simona@ffwll.ch>
> Cc: "Jérôme Glisse" <jglisse@redhat.com>
> Cc: Shuah Khan <shuah@kernel.org>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: Barry Song <baohua@kernel.org>
> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: Matthew Wilcox <willy@infradead.org>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: Zi Yan <ziy@nvidia.com>
> Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
> Cc: Jane Chu <jane.chu@oracle.com>
> Cc: Alistair Popple <apopple@nvidia.com>
> Cc: Donet Tom <donettom@linux.ibm.com>
> 
> Signed-off-by: Balbir Singh <balbirs@nvidia.com>
> ---
>  include/linux/migrate.h | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/include/linux/migrate.h b/include/linux/migrate.h
> index aaa2114498d6..1661e2d5479a 100644
> --- a/include/linux/migrate.h
> +++ b/include/linux/migrate.h
> @@ -167,6 +167,7 @@ static inline int migrate_misplaced_folio(struct folio *folio, int node)
>  #define MIGRATE_PFN_VALID	(1UL << 0)
>  #define MIGRATE_PFN_MIGRATE	(1UL << 1)
>  #define MIGRATE_PFN_WRITE	(1UL << 3)
> +#define MIGRATE_PFN_COMPOUND	(1UL << 4)

Why is this necessary? Couldn't migrate_vma just use folio_order() to figure out
if it's a compound page or not?

>  #define MIGRATE_PFN_SHIFT	6
>  
>  static inline struct page *migrate_pfn_to_page(unsigned long mpfn)
> @@ -185,6 +186,7 @@ enum migrate_vma_direction {
>  	MIGRATE_VMA_SELECT_SYSTEM = 1 << 0,
>  	MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1,
>  	MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2,
> +	MIGRATE_VMA_SELECT_COMPOUND = 1 << 3,
>  };
>  
>  struct migrate_vma {
> -- 
> 2.49.0
> 
Re: [v1 resend 02/12] mm/migrate_device: flags for selecting device private THP pages
Posted by Balbir Singh 3 months ago
On 7/7/25 15:31, Alistair Popple wrote:
> On Fri, Jul 04, 2025 at 09:35:01AM +1000, Balbir Singh wrote:
>> Add flags to mark zone device migration pages.
>>
>> MIGRATE_VMA_SELECT_COMPOUND will be used to select THP pages during
>> migrate_vma_setup() and MIGRATE_PFN_COMPOUND will make migrating
>> device pages as compound pages during device pfn migration.
>>
>> Cc: Karol Herbst <kherbst@redhat.com>
>> Cc: Lyude Paul <lyude@redhat.com>
>> Cc: Danilo Krummrich <dakr@kernel.org>
>> Cc: David Airlie <airlied@gmail.com>
>> Cc: Simona Vetter <simona@ffwll.ch>
>> Cc: "Jérôme Glisse" <jglisse@redhat.com>
>> Cc: Shuah Khan <shuah@kernel.org>
>> Cc: David Hildenbrand <david@redhat.com>
>> Cc: Barry Song <baohua@kernel.org>
>> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
>> Cc: Ryan Roberts <ryan.roberts@arm.com>
>> Cc: Matthew Wilcox <willy@infradead.org>
>> Cc: Peter Xu <peterx@redhat.com>
>> Cc: Zi Yan <ziy@nvidia.com>
>> Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
>> Cc: Jane Chu <jane.chu@oracle.com>
>> Cc: Alistair Popple <apopple@nvidia.com>
>> Cc: Donet Tom <donettom@linux.ibm.com>
>>
>> Signed-off-by: Balbir Singh <balbirs@nvidia.com>
>> ---
>>  include/linux/migrate.h | 2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/include/linux/migrate.h b/include/linux/migrate.h
>> index aaa2114498d6..1661e2d5479a 100644
>> --- a/include/linux/migrate.h
>> +++ b/include/linux/migrate.h
>> @@ -167,6 +167,7 @@ static inline int migrate_misplaced_folio(struct folio *folio, int node)
>>  #define MIGRATE_PFN_VALID	(1UL << 0)
>>  #define MIGRATE_PFN_MIGRATE	(1UL << 1)
>>  #define MIGRATE_PFN_WRITE	(1UL << 3)
>> +#define MIGRATE_PFN_COMPOUND	(1UL << 4)
> 
> Why is this necessary? Couldn't migrate_vma just use folio_order() to figure out
> if it's a compound page or not?
> 

I can definitely explore that angle. As we move towards mTHP, we'll need additional bits for the various order sizes as well.

Balbir Singh
Re: [v1 resend 02/12] mm/migrate_device: flags for selecting device private THP pages
Posted by Matthew Brost 2 months, 2 weeks ago
On Tue, Jul 08, 2025 at 05:31:49PM +1000, Balbir Singh wrote:
> On 7/7/25 15:31, Alistair Popple wrote:
> > On Fri, Jul 04, 2025 at 09:35:01AM +1000, Balbir Singh wrote:
> >> Add flags to mark zone device migration pages.
> >>
> >> MIGRATE_VMA_SELECT_COMPOUND will be used to select THP pages during
> >> migrate_vma_setup() and MIGRATE_PFN_COMPOUND will make migrating
> >> device pages as compound pages during device pfn migration.
> >>
> >> Cc: Karol Herbst <kherbst@redhat.com>
> >> Cc: Lyude Paul <lyude@redhat.com>
> >> Cc: Danilo Krummrich <dakr@kernel.org>
> >> Cc: David Airlie <airlied@gmail.com>
> >> Cc: Simona Vetter <simona@ffwll.ch>
> >> Cc: "Jérôme Glisse" <jglisse@redhat.com>
> >> Cc: Shuah Khan <shuah@kernel.org>
> >> Cc: David Hildenbrand <david@redhat.com>
> >> Cc: Barry Song <baohua@kernel.org>
> >> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> >> Cc: Ryan Roberts <ryan.roberts@arm.com>
> >> Cc: Matthew Wilcox <willy@infradead.org>
> >> Cc: Peter Xu <peterx@redhat.com>
> >> Cc: Zi Yan <ziy@nvidia.com>
> >> Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
> >> Cc: Jane Chu <jane.chu@oracle.com>
> >> Cc: Alistair Popple <apopple@nvidia.com>
> >> Cc: Donet Tom <donettom@linux.ibm.com>
> >>
> >> Signed-off-by: Balbir Singh <balbirs@nvidia.com>
> >> ---
> >>  include/linux/migrate.h | 2 ++
> >>  1 file changed, 2 insertions(+)
> >>
> >> diff --git a/include/linux/migrate.h b/include/linux/migrate.h
> >> index aaa2114498d6..1661e2d5479a 100644
> >> --- a/include/linux/migrate.h
> >> +++ b/include/linux/migrate.h
> >> @@ -167,6 +167,7 @@ static inline int migrate_misplaced_folio(struct folio *folio, int node)
> >>  #define MIGRATE_PFN_VALID	(1UL << 0)
> >>  #define MIGRATE_PFN_MIGRATE	(1UL << 1)
> >>  #define MIGRATE_PFN_WRITE	(1UL << 3)
> >> +#define MIGRATE_PFN_COMPOUND	(1UL << 4)
> > 
> > Why is this necessary? Couldn't migrate_vma just use folio_order() to figure out
> > if it's a compound page or not?
> > 
> 
> I can definitely explore that angle. As we move towards mTHP, we'll need additional bits for the various order sizes as well.
> 

I agree you probably could get away without having an explict mpfn flag
for compound pages and rely on the folio order everywhere.

Matt

> Balbir Singh
Re: [v1 resend 02/12] mm/migrate_device: flags for selecting device private THP pages
Posted by Matthew Brost 2 months, 2 weeks ago
On Sat, Jul 19, 2025 at 01:06:21PM -0700, Matthew Brost wrote:
> On Tue, Jul 08, 2025 at 05:31:49PM +1000, Balbir Singh wrote:
> > On 7/7/25 15:31, Alistair Popple wrote:
> > > On Fri, Jul 04, 2025 at 09:35:01AM +1000, Balbir Singh wrote:
> > >> Add flags to mark zone device migration pages.
> > >>
> > >> MIGRATE_VMA_SELECT_COMPOUND will be used to select THP pages during
> > >> migrate_vma_setup() and MIGRATE_PFN_COMPOUND will make migrating
> > >> device pages as compound pages during device pfn migration.
> > >>
> > >> Cc: Karol Herbst <kherbst@redhat.com>
> > >> Cc: Lyude Paul <lyude@redhat.com>
> > >> Cc: Danilo Krummrich <dakr@kernel.org>
> > >> Cc: David Airlie <airlied@gmail.com>
> > >> Cc: Simona Vetter <simona@ffwll.ch>
> > >> Cc: "Jérôme Glisse" <jglisse@redhat.com>
> > >> Cc: Shuah Khan <shuah@kernel.org>
> > >> Cc: David Hildenbrand <david@redhat.com>
> > >> Cc: Barry Song <baohua@kernel.org>
> > >> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> > >> Cc: Ryan Roberts <ryan.roberts@arm.com>
> > >> Cc: Matthew Wilcox <willy@infradead.org>
> > >> Cc: Peter Xu <peterx@redhat.com>
> > >> Cc: Zi Yan <ziy@nvidia.com>
> > >> Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
> > >> Cc: Jane Chu <jane.chu@oracle.com>
> > >> Cc: Alistair Popple <apopple@nvidia.com>
> > >> Cc: Donet Tom <donettom@linux.ibm.com>
> > >>
> > >> Signed-off-by: Balbir Singh <balbirs@nvidia.com>
> > >> ---
> > >>  include/linux/migrate.h | 2 ++
> > >>  1 file changed, 2 insertions(+)
> > >>
> > >> diff --git a/include/linux/migrate.h b/include/linux/migrate.h
> > >> index aaa2114498d6..1661e2d5479a 100644
> > >> --- a/include/linux/migrate.h
> > >> +++ b/include/linux/migrate.h
> > >> @@ -167,6 +167,7 @@ static inline int migrate_misplaced_folio(struct folio *folio, int node)
> > >>  #define MIGRATE_PFN_VALID	(1UL << 0)
> > >>  #define MIGRATE_PFN_MIGRATE	(1UL << 1)
> > >>  #define MIGRATE_PFN_WRITE	(1UL << 3)
> > >> +#define MIGRATE_PFN_COMPOUND	(1UL << 4)
> > > 
> > > Why is this necessary? Couldn't migrate_vma just use folio_order() to figure out
> > > if it's a compound page or not?
> > > 
> > 
> > I can definitely explore that angle. As we move towards mTHP, we'll need additional bits for the various order sizes as well.
> > 
> 
> I agree you probably could get away without having an explict mpfn flag
> for compound pages and rely on the folio order everywhere.
> 

Actually, I thought of one case where this is a bit useful—when a PMD is
not faulted during the call to migrate_vma_setup in a RAM → VRAM
migration. This communicates back to the caller that it can create a THP
device page. I suppose the caller could infer this based on the src
entries being unpopulated, but I thought it was worth bringing up this
case.

Not sure the MIGRATE_PFN_COMPOUND flag alone helps in the above scenario
with mTHP though. In that case, you'd probably need the order to be
encoded in the MFN as well.

Matt 

> Matt
> 
> > Balbir Singh