Skip to contentSkip to conversation

create hugetlb flags to consolidate state

PATCH In review Mike Kravetz 5 participants 27 messages Jan 22, 2021 – Feb 4, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v3 cover create hugetlb flags to consolidate state

While discussing a series of hugetlb fixes in [1], it became evident that the hugetlb specific page state information is stored in a somewhat haphazard manner. Code dealing with state information would be easier to read, understand and maintain if this information was stored in a consistent manner.

This series uses page.private of the hugetlb head page for storing a set of hugetlb specific page flags. Routines are priovided for test, set and clear of the flags.

[1] https://lore.kernel.org/r/[email protected]

v2 -> v3
Fixed !CONFIG_HUGETLB_PAGE build bug and moved BUILD_BUG_ON to hugetlb_init (Oscar)
Enhanced patch 3 (HPageTemporary flag) commit message (Oscar) Fixed typo enhanced coment in scan_movable_pages (Miaohe)
Patch -> v2
Went back to functions similar to 'normal' page flags (Matthew/Muchun) Decided to leave check in unmap_and_move_huge_page and print warning
RFC -> PATCH
Simplified to use a single set of flag manipulation routines (Oscar) Moved flags and routines to hugetlb.h (Muchun)
Changed format of page flag names (Muchun)
Changed subpool routine names (Matthew)
More comments in code (Oscar)

Based on v5.11-rc4-mmotm-2021-01-21-20-07

Mike Kravetz (5):
hugetlb: use page.private for hugetlb specific page flags
hugetlb: convert page_huge_active() HPageMigratable flag
hugetlb: only set HPageMigratable for migratable hstates
hugetlb: convert PageHugeTemporary() to HPageTemporary flag
hugetlb: convert PageHugeFreed to HPageFreed flag

 fs/hugetlbfs/inode.c       |  14 +---
 include/linux/hugetlb.h    |  91 ++++++++++++++++++++++
 include/linux/page-flags.h |   6 --
 mm/hugetlb.c               | 152 ++++++++++++-------------------------
 mm/memory_hotplug.c        |   9 ++-
 mm/migrate.c               |   9 +--
 6 files changed, 154 insertions(+), 127 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH v3 1/5 hugetlb: use page.private for hugetlb specific page flags

As hugetlbfs evolved, state information about hugetlb pages was added. One 'convenient' way of doing this was to use available fields in tail pages. Over time, it has become difficult to know the meaning or contents of fields simply by looking at a small bit of code. Sometimes, the naming is just confusing. For example: The PagePrivate flag indicates a huge page reservation was consumed and needs to be restored if an error is encountered and the page is freed before it is instantiated. The page.private field contains the pointer to a subpool if the page is associated with one.

In an effort to make the code more readable, use page.private to contain hugetlb specific page flags. These flags will have test, set and clear functions similar to those used for 'normal' page flags. More importantly, an enum of flag values will be created with names that actually reflect their purpose.

In this patch,
- Create infrastructure for hugetlb specific page flag functions
- Move subpool pointer to page[1].private to make way for flags Create routines with meaningful names to modify subpool field
- Use new HPageRestoreReserve flag instead of PagePrivate

Conversion of other state information will happen in subsequent patches.

---
 fs/hugetlbfs/inode.c    | 12 ++------
 include/linux/hugetlb.h | 68 +++++++++++++++++++++++++++++++++++++++++
 mm/hugetlb.c            | 48 +++++++++++++++--------------
 3 files changed, 96 insertions(+), 32 deletions(-)
fs/hugetlbfs/inode.c +3 -9
index 21c20fd5f9ee..b00801fd6002 100644
@@ -966,15 +966,9 @@ static int hugetlbfs_migrate_page(struct address_space *mapping,
 	if (rc != MIGRATEPAGE_SUCCESS)
 		return rc;
 
-	/*
-	 * page_private is subpool pointer in hugetlb pages.  Transfer to
-	 * new page.  PagePrivate is not associated with page_private for
-	 * hugetlb pages and can not be set here as only page_huge_active
-	 * pages can be migrated.
-	 */
-	if (page_private(page)) {
-		set_page_private(newpage, page_private(page));
-		set_page_private(page, 0);
+	if (hugetlb_page_subpool(page)) {
+		hugetlb_set_page_subpool(newpage, hugetlb_page_subpool(page));
+		hugetlb_set_page_subpool(page, NULL);
 	}
 
 	if (mode != MIGRATE_SYNC_NO_COPY)
include/linux/hugetlb.h +68 -0

80 lines not on this page · raw message

mm/hugetlb.c +25 -23

138 lines not on this page · raw message

Signature · 1 line
2.29.2
maintainer Reviewed-by on v3 1/5
#2 lore raw

Re: [PATCH v3 1/5] hugetlb: use page.private for hugetlb specific page flags

Quoted 22 lines · Mike Kravetz

As hugetlbfs evolved, state information about hugetlb pages was added. One 'convenient' way of doing this was to use available fields in tail pages. Over time, it has become difficult to know the meaning or contents of fields simply by looking at a small bit of code. Sometimes, the naming is just confusing. For example: The PagePrivate flag indicates a huge page reservation was consumed and needs to be restored if an error is encountered and the page is freed before it is instantiated. The page.private field contains the pointer to a subpool if the page is associated with one.

In an effort to make the code more readable, use page.private to contain hugetlb specific page flags. These flags will have test, set and clear functions similar to those used for 'normal' page flags. More importantly, an enum of flag values will be created with names that actually reflect their purpose.

In this patch,
- Create infrastructure for hugetlb specific page flag functions
- Move subpool pointer to page[1].private to make way for flags Create routines with meaningful names to modify subpool field
- Use new HPageRestoreReserve flag instead of PagePrivate

Quoted 3 lines

Conversion of other state information will happen in subsequent patches.

Signature · 2 lines
Oscar Salvador
SUSE L3
reviewer Acked-by on v3 1/5
#3 lore raw

Re: [PATCH v3 1/5] hugetlb: use page.private for hugetlb specific page flags

In reply to Mike Kravetz

[sorry for jumping in late]

Quoted 9 lines · Mike Kravetz

As hugetlbfs evolved, state information about hugetlb pages was added. One 'convenient' way of doing this was to use available fields in tail pages. Over time, it has become difficult to know the meaning or contents of fields simply by looking at a small bit of code. Sometimes, the naming is just confusing. For example: The PagePrivate flag indicates a huge page reservation was consumed and needs to be restored if an error is encountered and the page is freed before it is instantiated. The page.private field contains the pointer to a subpool if the page is associated with one.

OK, I thought the page.private was abused more than for this very specific case.

Quoted 5 lines

In an effort to make the code more readable, use page.private to contain hugetlb specific page flags. These flags will have test, set and clear functions similar to those used for 'normal' page flags. More importantly, an enum of flag values will be created with names that actually reflect their purpose.

This is definitely a step into the right direction!

Quoted 4 lines

In this patch,
- Create infrastructure for hugetlb specific page flag functions
- Move subpool pointer to page[1].private to make way for flags Create routines with meaningful names to modify subpool field

This makes some sense as well. It is really important that the primary state is stored in the head page. The respective data can be in tail pages.

Quoted 1 line

- Use new HPageRestoreReserve flag instead of PagePrivate

Much better! Although wouldn't HPageReserve be sufficient? The flag name doesn't really need to tell explicitly what to do with the reserve, right? Or would that be too confusing?

Quoted 3 lines

Conversion of other state information will happen in subsequent patches.

I do not see any problems with the patch. I hope I haven't overlooked anything...

Quoted 5 lines
---
 fs/hugetlbfs/inode.c    | 12 ++------
 include/linux/hugetlb.h | 68 +++++++++++++++++++++++++++++++++++++++++
 mm/hugetlb.c            | 48 +++++++++++++++--------------
 3 files changed, 96 insertions(+), 32 deletions(-)
Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw

Re: [PATCH v3 1/5] hugetlb: use page.private for hugetlb specific page flags

Quoted 33 lines · Michal Hocko

[sorry for jumping in late]

Quoted 9 lines · Mike Kravetz

As hugetlbfs evolved, state information about hugetlb pages was added. One 'convenient' way of doing this was to use available fields in tail pages. Over time, it has become difficult to know the meaning or contents of fields simply by looking at a small bit of code. Sometimes, the naming is just confusing. For example: The PagePrivate flag indicates a huge page reservation was consumed and needs to be restored if an error is encountered and the page is freed before it is instantiated. The page.private field contains the pointer to a subpool if the page is associated with one.

OK, I thought the page.private was abused more than for this very specific case.

Quoted 5 lines

In an effort to make the code more readable, use page.private to contain hugetlb specific page flags. These flags will have test, set and clear functions similar to those used for 'normal' page flags. More importantly, an enum of flag values will be created with names that actually reflect their purpose.

This is definitely a step into the right direction!

Quoted 4 lines

In this patch,
- Create infrastructure for hugetlb specific page flag functions
- Move subpool pointer to page[1].private to make way for flags Create routines with meaningful names to modify subpool field

This makes some sense as well. It is really important that the primary state is stored in the head page. The respective data can be in tail pages.

Quoted 5 lines
Quoted 1 line

- Use new HPageRestoreReserve flag instead of PagePrivate

Much better! Although wouldn't HPageReserve be sufficient? The flag name doesn't really need to tell explicitly what to do with the reserve, right? Or would that be too confusing?

Thanks for taking a look.

HPageReserve could be sufficient. I don't have a strong opinion and was just trying to add as much meaning to the name as possible. If you do not have a strong opinion, I would just leave it as is.

Signature · 1 line
Mike Kravetz
reviewer
#5 lore raw

Re: [PATCH v3 1/5] hugetlb: use page.private for hugetlb specific page flags

In reply to Mike Kravetz

Sorry forgot one thing.

Quoted 3 lines · Mike Kravetz

+ * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
+ * allocation time. Cleared when page is fully instantiated. Free
+ * routine checks flag to restore a reservation on error paths.

Can we document that the pool is stored in head+1->private here please?

Quoted 5 lines

+ */

include/linux/hugetlb.h +4 -0
+enum hugetlb_page_flags {
+	HPG_restore_reserve = 0,
+	__NR_HPAGEFLAGS,
+};
Signature · 2 lines
Michal Hocko
SUSE Labs
#6 lore raw

PATCH v3 2/5 hugetlb: convert page_huge_active() HPageMigratable flag

In reply to Mike Kravetz

Use the new hugetlb page specific flag HPageMigratable to replace the page_huge_active interfaces. By it's name, page_huge_active implied that a huge page was on the active list. However, that is not really what code checking the flag wanted to know. It really wanted to determine if the huge page could be migrated. This happens when the page is actually added to the page cache and/or task page table. This is the reasoning behind the name change.

The VM_BUG_ON_PAGE() calls in the *_huge_active() interfaces are not really necessary as we KNOW the page is a hugetlb page. Therefore, they are removed.

The routine page_huge_active checked for PageHeadHuge before testing the active bit. This is unnecessary in the case where we hold a reference or lock and know it is a hugetlb head page. page_huge_active is also called without holding a reference or lock (scan_movable_pages), and can race with code freeing the page. The extra check in page_huge_active shortened the race window, but did not prevent the race. Offline code calling scan_movable_pages already deals with these races, so removing the check is acceptable. Add comment to racy code.

---
 fs/hugetlbfs/inode.c       |  2 +-
 include/linux/hugetlb.h    |  5 +++++
 include/linux/page-flags.h |  6 -----
 mm/hugetlb.c               | 45 ++++++++++----------------------------
 mm/memory_hotplug.c        |  9 +++++++-
 5 files changed, 25 insertions(+), 42 deletions(-)
fs/hugetlbfs/inode.c +1 -1
index b00801fd6002..e1d7ed2a53a9 100644
@@ -735,7 +735,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
 
-		set_page_huge_active(page);
+		SetHPageMigratable(page);
 		/*
 		 * unlock_page because locked by add_to_page_cache()
 		 * put_page() due to reference from alloc_huge_page()
include/linux/hugetlb.h +5 -0
index a7eb05315c6e..58be44a915d1 100644
@@ -480,9 +480,13 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
  *	allocation time.  Cleared when page is fully instantiated.  Free
  *	routine checks flag to restore a reservation on error paths.
+ * HPG_migratable  - Set after a newly allocated page is added to the page
+ *	cache and/or page tables.  Indicates the page is a candidate for
+ *	migration.
  */
 enum hugetlb_page_flags {
 	HPG_restore_reserve = 0,
+	HPG_migratable,
 	__NR_HPAGEFLAGS,
 };
 
@@ -525,6 +529,7 @@ static inline void ClearHPage##uname(struct page *page)		\
  * Create functions associated with hugetlb page flags
  */
 HPAGEFLAG(RestoreReserve, restore_reserve)
+HPAGEFLAG(Migratable, migratable)
 
 #ifdef CONFIG_HUGETLB_PAGE
 
include/linux/page-flags.h +0 -6
index ec5d0290e0ee..db914477057b 100644
@@ -592,15 +592,9 @@ static inline void ClearPageCompound(struct page *page)
 #ifdef CONFIG_HUGETLB_PAGE
 int PageHuge(struct page *page);
 int PageHeadHuge(struct page *page);
-bool page_huge_active(struct page *page);
 #else
 TESTPAGEFLAG_FALSE(Huge)
 TESTPAGEFLAG_FALSE(HeadHuge)
-
-static inline bool page_huge_active(struct page *page)
-{
-	return 0;
-}
 #endif
 
 
mm/hugetlb.c +11 -34

93 lines not on this page · raw message

mm/memory_hotplug.c +8 -1
index f9d57b9be8c7..465164be4d2d 100644
@@ -1260,7 +1260,14 @@ static int scan_movable_pages(unsigned long start, unsigned long end,
 		if (!PageHuge(page))
 			continue;
 		head = compound_head(page);
-		if (page_huge_active(head))
+		/*
+		 * This test is racy as we hold no reference or lock.  The
+		 * hugetlb page could have been free'ed and head is no longer
+		 * a hugetlb page before the following check.  In such unlikely
+		 * cases false positives and negatives are possible.  Calling
+		 * code must deal with these scenarios.
+		 */
+		if (HPageMigratable(head))
 			goto found;
 		skip = compound_nr(head) - (page - head);
 		pfn += skip - 1;
Signature · 1 line
2.29.2
reviewer Acked-by on v3 2/5
#7 lore raw

Re: [PATCH v3 2/5] hugetlb: convert page_huge_active() HPageMigratable flag

Quoted 7 lines · Mike Kravetz

Use the new hugetlb page specific flag HPageMigratable to replace the page_huge_active interfaces. By it's name, page_huge_active implied that a huge page was on the active list. However, that is not really what code checking the flag wanted to know. It really wanted to determine if the huge page could be migrated. This happens when the page is actually added to the page cache and/or task page table. This is the reasoning behind the name change.

yeah, definitely less confusing!

Quoted 13 lines

The VM_BUG_ON_PAGE() calls in the *_huge_active() interfaces are not really necessary as we KNOW the page is a hugetlb page. Therefore, they are removed.

The routine page_huge_active checked for PageHeadHuge before testing the active bit. This is unnecessary in the case where we hold a reference or lock and know it is a hugetlb head page. page_huge_active is also called without holding a reference or lock (scan_movable_pages), and can race with code freeing the page. The extra check in page_huge_active shortened the race window, but did not prevent the race. Offline code calling scan_movable_pages already deals with these races, so removing the check is acceptable. Add comment to racy code.

Quoted 4 lines

One nit below
[...]

include/linux/hugetlb.h unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, · line 485

Quoted 11 lines
include/linux/hugetlb.h +3 -0
index a7eb05315c6e..58be44a915d1 100644
@@ -480,9 +480,13 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
  *	allocation time.  Cleared when page is fully instantiated.  Free
  *	routine checks flag to restore a reservation on error paths.
+ * HPG_migratable  - Set after a newly allocated page is added to the page
+ *	cache and/or page tables.  Indicates the page is a candidate for
+ *	migration.

The state change is synchronized by hugetlb_lock.

Pls.

Quoted 6 lines
include/linux/hugetlb.h +1 -0
  */
 enum hugetlb_page_flags {
 	HPG_restore_reserve = 0,
+	HPG_migratable,
 	__NR_HPAGEFLAGS,
 };
Signature · 2 lines
Michal Hocko
SUSE Labs
#8 lore raw

Re: [PATCH v3 2/5] hugetlb: convert page_huge_active() HPageMigratable flag

Quoted 47 lines · Michal Hocko
Quoted 7 lines · Mike Kravetz

Use the new hugetlb page specific flag HPageMigratable to replace the page_huge_active interfaces. By it's name, page_huge_active implied that a huge page was on the active list. However, that is not really what code checking the flag wanted to know. It really wanted to determine if the huge page could be migrated. This happens when the page is actually added to the page cache and/or task page table. This is the reasoning behind the name change.

yeah, definitely less confusing!

Quoted 13 lines

The VM_BUG_ON_PAGE() calls in the *_huge_active() interfaces are not really necessary as we KNOW the page is a hugetlb page. Therefore, they are removed.

The routine page_huge_active checked for PageHeadHuge before testing the active bit. This is unnecessary in the case where we hold a reference or lock and know it is a hugetlb head page. page_huge_active is also called without holding a reference or lock (scan_movable_pages), and can race with code freeing the page. The extra check in page_huge_active shortened the race window, but did not prevent the race. Offline code calling scan_movable_pages already deals with these races, so removing the check is acceptable. Add comment to racy code.

Quoted 4 lines

One nit below
[...]

include/linux/hugetlb.h unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, · line 485

Quoted 11 lines
include/linux/hugetlb.h +3 -0
index a7eb05315c6e..58be44a915d1 100644
@@ -480,9 +480,13 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
  *	allocation time.  Cleared when page is fully instantiated.  Free
  *	routine checks flag to restore a reservation on error paths.
+ * HPG_migratable  - Set after a newly allocated page is added to the page
+ *	cache and/or page tables.  Indicates the page is a candidate for
+ *	migration.

The state change is synchronized by hugetlb_lock.

Quoted 1 line

Pls.

I will update/explain this. But ... hugetlb_lock does not synchronize all changes of this flag. The flag is set without holding the lock for newly allocated pages after being added to page cache and/or page tables. This 'signals' the page is a candidate for migration. When the migration code (isolation, putback, move state) deals with the flag, it does hold the hugetlb_lock.

This patch did not change any of the synchronization.

Signature · 1 line
Mike Kravetz
#9 lore raw

PATCH v3 3/5 hugetlb: only set HPageMigratable for migratable hstates

In reply to Mike Kravetz

The HP_Migratable flag indicates a page is a candidate for migration. Only set the flag if the page's hstate supports migration. This allows the migration paths to detect non-migratable pages earlier. If migration is not supported for the hstate, HP_Migratable will not be set, the page will not be isolated and no attempt will be made to migrate. We should never get to unmap_and_move_huge_page for a page where migration is not supported, so throw a warning if we do.

---
 fs/hugetlbfs/inode.c    | 2 +-
 include/linux/hugetlb.h | 9 +++++++++
 mm/hugetlb.c            | 8 ++++----
 mm/migrate.c            | 9 ++++-----
 4 files changed, 18 insertions(+), 10 deletions(-)
fs/hugetlbfs/inode.c +1 -1
index e1d7ed2a53a9..93f7b8d3c5fd 100644
@@ -735,7 +735,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
 
-		SetHPageMigratable(page);
+		SetHPageMigratableIfSupported(page);
 		/*
 		 * unlock_page because locked by add_to_page_cache()
 		 * put_page() due to reference from alloc_huge_page()
include/linux/hugetlb.h +9 -0
index 58be44a915d1..cd1960541f2a 100644
@@ -740,6 +740,15 @@ static inline bool hugepage_migration_supported(struct hstate *h)
 	return arch_hugetlb_migration_supported(h);
 }
 
+/*
+ * Only set HPageMigratable if migration supported for page
+ */
+static inline void SetHPageMigratableIfSupported(struct page *page)
+{
+	if (hugepage_migration_supported(page_hstate(page)))
+		SetHPageMigratable(page);
+}
+
 /*
  * Movability check is different as compared to migration check.
  * It determines whether or not a huge page should be placed on
mm/hugetlb.c +4 -4
index f1a3c8230dbf..4da1a29ac5e2 100644
@@ -4194,7 +4194,7 @@ static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
 				make_huge_pte(vma, new_page, 1));
 		page_remove_rmap(old_page, true);
 		hugepage_add_new_anon_rmap(new_page, vma, haddr);
-		SetHPageMigratable(new_page);
+		SetHPageMigratableIfSupported(new_page);
 		/* Make the old page be freed below */
 		new_page = old_page;
 	}
@@ -4436,7 +4436,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
 	 * been isolated for migration.
 	 */
 	if (new_page)
-		SetHPageMigratable(page);
+		SetHPageMigratableIfSupported(page);
 
 	unlock_page(page);
 out:
@@ -4747,7 +4747,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
 	update_mmu_cache(dst_vma, dst_addr, dst_pte);
 
 	spin_unlock(ptl);
-	SetHPageMigratable(page);
+	SetHPageMigratableIfSupported(page);
 	if (vm_shared)
 		unlock_page(page);
 	ret = 0;
@@ -5589,7 +5589,7 @@ void putback_active_hugepage(struct page *page)
 {
 	VM_BUG_ON_PAGE(!PageHead(page), page);
 	spin_lock(&hugetlb_lock);
-	SetHPageMigratable(page);
+	SetHPageMigratableIfSupported(page);
 	list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
 	spin_unlock(&hugetlb_lock);
 	put_page(page);
mm/migrate.c +4 -5
index a3e1acc72ad7..c8d19e83f372 100644
@@ -1275,13 +1275,12 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 	struct address_space *mapping = NULL;
 
 	/*
-	 * Migratability of hugepages depends on architectures and their size.
-	 * This check is necessary because some callers of hugepage migration
-	 * like soft offline and memory hotremove don't walk through page
-	 * tables or check whether the hugepage is pmd-based or not before
-	 * kicking migration.
+	 * Support for migration should be checked at isolation time.
+	 * Therefore, we should never get here if migration is not supported
+	 * for the page.
 	 */
 	if (!hugepage_migration_supported(page_hstate(hpage))) {
+		VM_WARN_ON(1);
 		list_move_tail(&hpage->lru, ret);
 		return -ENOSYS;
 	}
Signature · 1 line
2.29.2
maintainer Reviewed-by on v3 3/5
#10 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

Quoted 9 lines · Mike Kravetz

The HP_Migratable flag indicates a page is a candidate for migration. Only set the flag if the page's hstate supports migration. This allows the migration paths to detect non-migratable pages earlier. If migration is not supported for the hstate, HP_Migratable will not be set, the page will not be isolated and no attempt will be made to migrate. We should never get to unmap_and_move_huge_page for a page where migration is not supported, so throw a warning if we do.

I wish there was a better way to do this like opencode it at fault path, but as you mentioned that is troublesome.

I am not a big fan of the name either, too long for my taste but I cannot come up with anything better so:

Quoted 106 lines
---
 fs/hugetlbfs/inode.c    | 2 +-
 include/linux/hugetlb.h | 9 +++++++++
 mm/hugetlb.c            | 8 ++++----
 mm/migrate.c            | 9 ++++-----
 4 files changed, 18 insertions(+), 10 deletions(-)
fs/hugetlbfs/inode.c +1 -1
index e1d7ed2a53a9..93f7b8d3c5fd 100644
@@ -735,7 +735,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
 
-		SetHPageMigratable(page);
+		SetHPageMigratableIfSupported(page);
 		/*
 		 * unlock_page because locked by add_to_page_cache()
 		 * put_page() due to reference from alloc_huge_page()
include/linux/hugetlb.h +9 -0
index 58be44a915d1..cd1960541f2a 100644
@@ -740,6 +740,15 @@ static inline bool hugepage_migration_supported(struct hstate *h)
 	return arch_hugetlb_migration_supported(h);
 }
 
+/*
+ * Only set HPageMigratable if migration supported for page
+ */
+static inline void SetHPageMigratableIfSupported(struct page *page)
+{
+	if (hugepage_migration_supported(page_hstate(page)))
+		SetHPageMigratable(page);
+}
+
 /*
  * Movability check is different as compared to migration check.
  * It determines whether or not a huge page should be placed on
mm/hugetlb.c +4 -4
index f1a3c8230dbf..4da1a29ac5e2 100644
@@ -4194,7 +4194,7 @@ static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
 				make_huge_pte(vma, new_page, 1));
 		page_remove_rmap(old_page, true);
 		hugepage_add_new_anon_rmap(new_page, vma, haddr);
-		SetHPageMigratable(new_page);
+		SetHPageMigratableIfSupported(new_page);
 		/* Make the old page be freed below */
 		new_page = old_page;
 	}
@@ -4436,7 +4436,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
 	 * been isolated for migration.
 	 */
 	if (new_page)
-		SetHPageMigratable(page);
+		SetHPageMigratableIfSupported(page);
 
 	unlock_page(page);
 out:
@@ -4747,7 +4747,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
 	update_mmu_cache(dst_vma, dst_addr, dst_pte);
 
 	spin_unlock(ptl);
-	SetHPageMigratable(page);
+	SetHPageMigratableIfSupported(page);
 	if (vm_shared)
 		unlock_page(page);
 	ret = 0;
@@ -5589,7 +5589,7 @@ void putback_active_hugepage(struct page *page)
 {
 	VM_BUG_ON_PAGE(!PageHead(page), page);
 	spin_lock(&hugetlb_lock);
-	SetHPageMigratable(page);
+	SetHPageMigratableIfSupported(page);
 	list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
 	spin_unlock(&hugetlb_lock);
 	put_page(page);
mm/migrate.c +4 -5
index a3e1acc72ad7..c8d19e83f372 100644
@@ -1275,13 +1275,12 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 	struct address_space *mapping = NULL;
 
 	/*
-	 * Migratability of hugepages depends on architectures and their size.
-	 * This check is necessary because some callers of hugepage migration
-	 * like soft offline and memory hotremove don't walk through page
-	 * tables or check whether the hugepage is pmd-based or not before
-	 * kicking migration.
+	 * Support for migration should be checked at isolation time.
+	 * Therefore, we should never get here if migration is not supported
+	 * for the page.
 	 */
 	if (!hugepage_migration_supported(page_hstate(hpage))) {
+		VM_WARN_ON(1);
 		list_move_tail(&hpage->lru, ret);
 		return -ENOSYS;
 	}
Signature · 1 line
2.29.2
Signature · 2 lines
Oscar Salvador
SUSE L3
reviewer
#11 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

In reply to Mike Kravetz
Quoted 34 lines · Mike Kravetz

The HP_Migratable flag indicates a page is a candidate for migration. Only set the flag if the page's hstate supports migration. This allows the migration paths to detect non-migratable pages earlier. If migration is not supported for the hstate, HP_Migratable will not be set, the page will not be isolated and no attempt will be made to migrate. We should never get to unmap_and_move_huge_page for a page where migration is not supported, so throw a warning if we do.

---
 fs/hugetlbfs/inode.c    | 2 +-
 include/linux/hugetlb.h | 9 +++++++++
 mm/hugetlb.c            | 8 ++++----
 mm/migrate.c            | 9 ++++-----
 4 files changed, 18 insertions(+), 10 deletions(-)
fs/hugetlbfs/inode.c +1 -1
index e1d7ed2a53a9..93f7b8d3c5fd 100644
@@ -735,7 +735,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
 
-		SetHPageMigratable(page);
+		SetHPageMigratableIfSupported(page);
 		/*
 		 * unlock_page because locked by add_to_page_cache()
 		 * put_page() due to reference from alloc_huge_page()
include/linux/hugetlb.h +0 -0
index 58be44a915d1..cd1960541f2a 100644

include/linux/hugetlb.h static inline bool hugepage_migration_supported(struct hstate *h) · line 746

Quoted 7 lines
include/linux/hugetlb.h +4 -0
@@ -740,6 +740,15 @@ static inline bool hugepage_migration_supported(struct hstate *h)
 	return arch_hugetlb_migration_supported(h);
 }
 
+/*
+ * Only set HPageMigratable if migration supported for page
+ */
+static inline void SetHPageMigratableIfSupported(struct page *page)

This is really mouthful...

include/linux/hugetlb.h static inline bool hugepage_migration_supported(struct hstate *h) · line 749

Quoted 3 lines
include/linux/hugetlb.h +3 -0
+{
+	if (hugepage_migration_supported(page_hstate(page)))
+		SetHPageMigratable(page);

and it is really a trivial wrapper. I do understand why you want to prevent from the code duplication and potentially a missing check but this all is just an internal hugetlb code. Even if the flag is set on non-migrateable hugetlb page then this will not be fatal. The migration can fail even on those pages for which migration is supported right?

So I am not really sure this is an improvement in the end. But up to you I do not really have a strong opinion here.

Signature · 2 lines
Michal Hocko
SUSE Labs
#12 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

Quoted 55 lines · Michal Hocko
Quoted 34 lines · Mike Kravetz

The HP_Migratable flag indicates a page is a candidate for migration. Only set the flag if the page's hstate supports migration. This allows the migration paths to detect non-migratable pages earlier. If migration is not supported for the hstate, HP_Migratable will not be set, the page will not be isolated and no attempt will be made to migrate. We should never get to unmap_and_move_huge_page for a page where migration is not supported, so throw a warning if we do.

---
 fs/hugetlbfs/inode.c    | 2 +-
 include/linux/hugetlb.h | 9 +++++++++
 mm/hugetlb.c            | 8 ++++----
 mm/migrate.c            | 9 ++++-----
 4 files changed, 18 insertions(+), 10 deletions(-)
fs/hugetlbfs/inode.c +1 -1
index e1d7ed2a53a9..93f7b8d3c5fd 100644
@@ -735,7 +735,7 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
 
-		SetHPageMigratable(page);
+		SetHPageMigratableIfSupported(page);
 		/*
 		 * unlock_page because locked by add_to_page_cache()
 		 * put_page() due to reference from alloc_huge_page()
include/linux/hugetlb.h +0 -0
index 58be44a915d1..cd1960541f2a 100644

include/linux/hugetlb.h static inline bool hugepage_migration_supported(struct hstate *h) · line 746

Quoted 7 lines
include/linux/hugetlb.h +4 -0
@@ -740,6 +740,15 @@ static inline bool hugepage_migration_supported(struct hstate *h)
 	return arch_hugetlb_migration_supported(h);
 }
 
+/*
+ * Only set HPageMigratable if migration supported for page
+ */
+static inline void SetHPageMigratableIfSupported(struct page *page)

This is really mouthful...

include/linux/hugetlb.h static inline bool hugepage_migration_supported(struct hstate *h) · line 749

Quoted 3 lines
include/linux/hugetlb.h +3 -0
+{
+	if (hugepage_migration_supported(page_hstate(page)))
+		SetHPageMigratable(page);

and it is really a trivial wrapper. I do understand why you want to prevent from the code duplication and potentially a missing check but this all is just an internal hugetlb code. Even if the flag is set on non-migrateable hugetlb page then this will not be fatal. The migration can fail even on those pages for which migration is supported right?

Quoted 2 lines

So I am not really sure this is an improvement in the end. But up to you I do not really have a strong opinion here.

Yes, this patch is somewhat optional. It should be a minor improvement in cases where we are dealing with hpages in a non-migratable hstate. Although, I do not believe this is the common case.

The real reason for even looking into this was a comment by Oscar. With the name change to HPageMigratable, it implies that the page is migratable. However, this is not the case if the page's hstate does not support migration. So, if we check the hstate when setting the flag we can eliminate those cases where the page is certainly not migratable.

I don't really love this patch. It has minimal functional value.

Oscar, what do you think about dropping this?

Signature · 1 line
Mike Kravetz
maintainer
#13 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

Quoted 10 lines · Mike Kravetz

Yes, this patch is somewhat optional. It should be a minor improvement in cases where we are dealing with hpages in a non-migratable hstate. Although, I do not believe this is the common case.

The real reason for even looking into this was a comment by Oscar. With the name change to HPageMigratable, it implies that the page is migratable. However, this is not the case if the page's hstate does not support migration. So, if we check the hstate when setting the flag we can eliminate those cases where the page is certainly not migratable.

Quoted 3 lines

I don't really love this patch. It has minimal functional value.

Oscar, what do you think about dropping this?

Yeah, I remember this topic arose during a discussion of patch#2 in the early versions, about whether the renaming to HPageMigratable made sense.

Back then I thought that we could have this in one place at fault-path [1], which should have made this prettier, but it is not the case. True is that the optimization is little, so I am fine with dropping this patch.

unmap_and_move_huge_page() fences off pages belonging to non-migratable hstates.

[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/#23914033

Thanks

Signature · 2 lines
Oscar Salvador
SUSE L3
maintainer
#14 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

Quoted 15 lines · On Thu, 28 Jan 2021 06:52:21 +0100 Oscar Salvador <[email protected]> wrote:
Quoted 10 lines · Mike Kravetz

Yes, this patch is somewhat optional. It should be a minor improvement in cases where we are dealing with hpages in a non-migratable hstate. Although, I do not believe this is the common case.

The real reason for even looking into this was a comment by Oscar. With the name change to HPageMigratable, it implies that the page is migratable. However, this is not the case if the page's hstate does not support migration. So, if we check the hstate when setting the flag we can eliminate those cases where the page is certainly not migratable.

Quoted 3 lines

I don't really love this patch. It has minimal functional value.

Oscar, what do you think about dropping this?

Quoted 8 lines

Yeah, I remember this topic arose during a discussion of patch#2 in the early versions, about whether the renaming to HPageMigratable made sense.

Back then I thought that we could have this in one place at fault-path [1], which should have made this prettier, but it is not the case. True is that the optimization is little, so I am fine with dropping this patch.

I've dropped it.

Quoted 4 lines

unmap_and_move_huge_page() fences off pages belonging to non-migratable hstates.

[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/#23914033

#15 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Thu, 28 Jan 2021 06:52:21 +0100 Oscar Salvador <[email protected]> wrote:
Quoted 10 lines · Mike Kravetz

Yes, this patch is somewhat optional. It should be a minor improvement in cases where we are dealing with hpages in a non-migratable hstate. Although, I do not believe this is the common case.

The real reason for even looking into this was a comment by Oscar. With the name change to HPageMigratable, it implies that the page is migratable. However, this is not the case if the page's hstate does not support migration. So, if we check the hstate when setting the flag we can eliminate those cases where the page is certainly not migratable.

Quoted 3 lines

I don't really love this patch. It has minimal functional value.

Oscar, what do you think about dropping this?

Yeah, I remember this topic arose during a discussion of patch#2 in the early versions, about whether the renaming to HPageMigratable made sense.

Quoted 6 lines
Quoted 4 lines

Back then I thought that we could have this in one place at fault-path [1], which should have made this prettier, but it is not the case. True is that the optimization is little, so I am fine with dropping this patch.

I've dropped it.

Thanks Andrew.

Michal suggested that comments describing synchronization be added for each flag. Since I did 'one patch per flag', that would be an update to each patch. Or, I could simply add a patch to update the comment block based on what you already have.

Let me know what is best/easiest for you.

Signature · 1 line
Mike Kravetz
maintainer
#16 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

Quoted 29 lines · On Thu, 28 Jan 2021 14:00:29 -0800 Mike Kravetz <[email protected]> wrote:
Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Thu, 28 Jan 2021 06:52:21 +0100 Oscar Salvador <[email protected]> wrote:
Quoted 10 lines · Mike Kravetz

Yes, this patch is somewhat optional. It should be a minor improvement in cases where we are dealing with hpages in a non-migratable hstate. Although, I do not believe this is the common case.

The real reason for even looking into this was a comment by Oscar. With the name change to HPageMigratable, it implies that the page is migratable. However, this is not the case if the page's hstate does not support migration. So, if we check the hstate when setting the flag we can eliminate those cases where the page is certainly not migratable.

Quoted 3 lines

I don't really love this patch. It has minimal functional value.

Oscar, what do you think about dropping this?

Yeah, I remember this topic arose during a discussion of patch#2 in the early versions, about whether the renaming to HPageMigratable made sense.

Quoted 6 lines
Quoted 4 lines

Back then I thought that we could have this in one place at fault-path [1], which should have made this prettier, but it is not the case. True is that the optimization is little, so I am fine with dropping this patch.

I've dropped it.

Quoted 8 lines

Thanks Andrew.

Michal suggested that comments describing synchronization be added for each flag. Since I did 'one patch per flag', that would be an update to each patch. Or, I could simply add a patch to update the comment block based on what you already have.

Let me know what is best/easiest for you.

I guess just one patch is best for reviewers. Then I'll split up into a sprinkle of -fix patches if I'm feeling energetic ;)

reviewer
#17 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

Quoted 38 lines · Andrew Morton
Quoted 36 lines · On Thu, 28 Jan 2021 14:00:29 -0800 Mike Kravetz <[email protected]> wrote:
Quoted 21 lines · Andrew Morton
Quoted 19 lines · On Thu, 28 Jan 2021 06:52:21 +0100 Oscar Salvador <[email protected]> wrote:
Quoted 10 lines · Mike Kravetz

Yes, this patch is somewhat optional. It should be a minor improvement in cases where we are dealing with hpages in a non-migratable hstate. Although, I do not believe this is the common case.

The real reason for even looking into this was a comment by Oscar. With the name change to HPageMigratable, it implies that the page is migratable. However, this is not the case if the page's hstate does not support migration. So, if we check the hstate when setting the flag we can eliminate those cases where the page is certainly not migratable.

Quoted 3 lines

I don't really love this patch. It has minimal functional value.

Oscar, what do you think about dropping this?

Yeah, I remember this topic arose during a discussion of patch#2 in the early versions, about whether the renaming to HPageMigratable made sense.

Quoted 6 lines
Quoted 4 lines

Back then I thought that we could have this in one place at fault-path [1], which should have made this prettier, but it is not the case. True is that the optimization is little, so I am fine with dropping this patch.

I've dropped it.

Thanks Andrew.

Michal suggested that comments describing synchronization be added for each flag. Since I did 'one patch per flag', that would be an update to each patch. Or, I could simply add a patch to update the comment block based on what you already have.

Quoted 4 lines
Quoted 1 line

Let me know what is best/easiest for you.

I guess just one patch is best for reviewers. Then I'll split up into a sprinkle of -fix patches if I'm feeling energetic ;)

A single patch on top should be just fine. It is not like this would be a bisectability breaker.

Signature · 2 lines
Michal Hocko
SUSE Labs
#18 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

In reply to Andrew Morton
Quoted 11 lines · Andrew Morton
Quoted 7 lines · On Thu, 28 Jan 2021 14:00:29 -0800 Mike Kravetz <[email protected]> wrote:

Michal suggested that comments describing synchronization be added for each flag. Since I did 'one patch per flag', that would be an update to each patch. Or, I could simply add a patch to update the comment block based on what you already have.

Let me know what is best/easiest for you.

I guess just one patch is best for reviewers. Then I'll split up into a sprinkle of -fix patches if I'm feeling energetic ;)

Here is a patch to update the comments for all those flags. It should apply on top of what is in your tree.

From: Mike Kravetz <[email protected]>
Date: Fri, 29 Jan 2021 10:36:12 -0800
Subject: [PATCH] huegtlb: add synchronization information for new hugetlb
specific flags

Adding comments, no functional change.

---
 include/linux/hugetlb.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/hugetlb.h +10 -0
index e5e6ffd55392..cf70795d2209 100644
@@ -480,14 +480,24 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
  *	allocation time.  Cleared when page is fully instantiated.  Free
  *	routine checks flag to restore a reservation on error paths.
+ *	Synchronization:  Examined or modified by code that knows it has
+ *	the only reference to page.  i.e. After allocation but before use
+ *	or when the page is being freed.
  * HPG_migratable  - Set after a newly allocated page is added to the page
  *	cache and/or page tables.  Indicates the page is a candidate for
  *	migration.
+ *	Synchronization:  Initially set after new page allocation with no
+ *	locking.  When examined and modified during migration processing
+ *	(isolate, migrate, putback) the hugetlb_lock is held.
  * HPG_temporary - - Set on a page that is temporarily allocated from the buddy
  *	allocator.  Typically used for migration target pages when no pages
  *	are available in the pool.  The hugetlb free page path will
  *	immediately free pages with this flag set to the buddy allocator.
+ *	Synchronization: Can be set after huge page allocation from buddy when
+ *	code knows it has only reference.  All other examinations and
+ *	modifications require hugetlb_lock.
  * HPG_freed - Set when page is on the free lists.
+ *	Synchronization: hugetlb_lock held for examination and modification.
  */
 enum hugetlb_page_flags {
 	HPG_restore_reserve = 0,
Signature · 1 line
2.29.2
reviewer Acked-by
#19 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

Quoted 16 lines · Mike Kravetz
Quoted 11 lines · Andrew Morton
Quoted 7 lines · On Thu, 28 Jan 2021 14:00:29 -0800 Mike Kravetz <[email protected]> wrote:

Michal suggested that comments describing synchronization be added for each flag. Since I did 'one patch per flag', that would be an update to each patch. Or, I could simply add a patch to update the comment block based on what you already have.

Let me know what is best/easiest for you.

I guess just one patch is best for reviewers. Then I'll split up into a sprinkle of -fix patches if I'm feeling energetic ;)

Here is a patch to update the comments for all those flags. It should apply on top of what is in your tree.

Quoted 8 lines

From: Mike Kravetz <[email protected]>
Date: Fri, 29 Jan 2021 10:36:12 -0800
Subject: [PATCH] huegtlb: add synchronization information for new hugetlb
specific flags

Adding comments, no functional change.

Thanks Mike!

Quoted 36 lines
---
 include/linux/hugetlb.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/hugetlb.h +10 -0
index e5e6ffd55392..cf70795d2209 100644
@@ -480,14 +480,24 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
  *	allocation time.  Cleared when page is fully instantiated.  Free
  *	routine checks flag to restore a reservation on error paths.
+ *	Synchronization:  Examined or modified by code that knows it has
+ *	the only reference to page.  i.e. After allocation but before use
+ *	or when the page is being freed.
  * HPG_migratable  - Set after a newly allocated page is added to the page
  *	cache and/or page tables.  Indicates the page is a candidate for
  *	migration.
+ *	Synchronization:  Initially set after new page allocation with no
+ *	locking.  When examined and modified during migration processing
+ *	(isolate, migrate, putback) the hugetlb_lock is held.
  * HPG_temporary - - Set on a page that is temporarily allocated from the buddy
  *	allocator.  Typically used for migration target pages when no pages
  *	are available in the pool.  The hugetlb free page path will
  *	immediately free pages with this flag set to the buddy allocator.
+ *	Synchronization: Can be set after huge page allocation from buddy when
+ *	code knows it has only reference.  All other examinations and
+ *	modifications require hugetlb_lock.
  * HPG_freed - Set when page is on the free lists.
+ *	Synchronization: hugetlb_lock held for examination and modification.
  */
 enum hugetlb_page_flags {
 	HPG_restore_reserve = 0,
Signature · 1 line
2.29.2
Signature · 2 lines
Michal Hocko
SUSE Labs
#20 lore raw

Re: [PATCH v3 3/5] hugetlb: only set HPageMigratable for migratable hstates

Quoted 22 lines · Michal Hocko
Quoted 21 lines · Mike Kravetz
Quoted 11 lines · Andrew Morton
Quoted 7 lines · On Thu, 28 Jan 2021 14:00:29 -0800 Mike Kravetz <[email protected]> wrote:

Michal suggested that comments describing synchronization be added for each flag. Since I did 'one patch per flag', that would be an update to each patch. Or, I could simply add a patch to update the comment block based on what you already have.

Let me know what is best/easiest for you.

I guess just one patch is best for reviewers. Then I'll split up into a sprinkle of -fix patches if I'm feeling energetic ;)

Here is a patch to update the comments for all those flags. It should apply on top of what is in your tree.

From: Mike Kravetz <[email protected]>
Date: Fri, 29 Jan 2021 10:36:12 -0800
Subject: [PATCH] huegtlb: add synchronization information for new hugetlb
specific flags

Quoted 7 lines
Quoted 3 lines

Adding comments, no functional change.

Thanks Mike!

Hi Andrew,

You may have missed this due to the depth of email thread. Can you pick this up, or would you like me to resend?

Thanks,

Signature · 1 line
Mike Kravetz
Quoted 38 lines
Quoted 37 lines
---
 include/linux/hugetlb.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/hugetlb.h +10 -0
index e5e6ffd55392..cf70795d2209 100644
@@ -480,14 +480,24 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  * HPG_restore_reserve - Set when a hugetlb page consumes a reservation at
  *	allocation time.  Cleared when page is fully instantiated.  Free
  *	routine checks flag to restore a reservation on error paths.
+ *	Synchronization:  Examined or modified by code that knows it has
+ *	the only reference to page.  i.e. After allocation but before use
+ *	or when the page is being freed.
  * HPG_migratable  - Set after a newly allocated page is added to the page
  *	cache and/or page tables.  Indicates the page is a candidate for
  *	migration.
+ *	Synchronization:  Initially set after new page allocation with no
+ *	locking.  When examined and modified during migration processing
+ *	(isolate, migrate, putback) the hugetlb_lock is held.
  * HPG_temporary - - Set on a page that is temporarily allocated from the buddy
  *	allocator.  Typically used for migration target pages when no pages
  *	are available in the pool.  The hugetlb free page path will
  *	immediately free pages with this flag set to the buddy allocator.
+ *	Synchronization: Can be set after huge page allocation from buddy when
+ *	code knows it has only reference.  All other examinations and
+ *	modifications require hugetlb_lock.
  * HPG_freed - Set when page is on the free lists.
+ *	Synchronization: hugetlb_lock held for examination and modification.
  */
 enum hugetlb_page_flags {
 	HPG_restore_reserve = 0,
Signature · 1 line
2.29.2
#21 lore raw

PATCH v3 4/5 hugetlb: convert PageHugeTemporary() to HPageTemporary flag

In reply to Mike Kravetz

Use new hugetlb specific HPageTemporary flag to replace the PageHugeTemporary() interfaces. PageHugeTemporary does contain a PageHuge() check. However, this interface is only used within hugetlb code where we know we are dealing with a hugetlb page. Therefore, the check can be eliminated.

---
 include/linux/hugetlb.h |  6 ++++++
 mm/hugetlb.c            | 36 +++++++-----------------------------
 2 files changed, 13 insertions(+), 29 deletions(-)
include/linux/hugetlb.h +6 -0
index cd1960541f2a..3c86c3a0e144 100644
@@ -483,10 +483,15 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  * HPG_migratable  - Set after a newly allocated page is added to the page
  *	cache and/or page tables.  Indicates the page is a candidate for
  *	migration.
+ * HPG_temporary - - Set on a page that is temporarily allocated from the buddy
+ *	allocator.  Typically used for migration target pages when no pages
+ *	are available in the pool.  The hugetlb free page path will
+ *	immediately free pages with this flag set to the buddy allocator.
  */
 enum hugetlb_page_flags {
 	HPG_restore_reserve = 0,
 	HPG_migratable,
+	HPG_temporary,
 	__NR_HPAGEFLAGS,
 };
 
@@ -530,6 +535,7 @@ static inline void ClearHPage##uname(struct page *page)		\
  */
 HPAGEFLAG(RestoreReserve, restore_reserve)
 HPAGEFLAG(Migratable, migratable)
+HPAGEFLAG(Temporary, temporary)
 
 #ifdef CONFIG_HUGETLB_PAGE
 
mm/hugetlb.c +7 -29
index 4da1a29ac5e2..70ffa1027988 100644
@@ -1353,28 +1353,6 @@ struct hstate *size_to_hstate(unsigned long size)
 	return NULL;
 }
 
-/*
- * Internal hugetlb specific page flag. Do not use outside of the hugetlb
- * code
- */
-static inline bool PageHugeTemporary(struct page *page)
-{
-	if (!PageHuge(page))
-		return false;
-
-	return (unsigned long)page[2].mapping == -1U;
-}
-
-static inline void SetPageHugeTemporary(struct page *page)
-{
-	page[2].mapping = (void *)-1U;
-}
-
-static inline void ClearPageHugeTemporary(struct page *page)
-{
-	page[2].mapping = NULL;
-}
-
 static void __free_huge_page(struct page *page)
 {
 	/*
@@ -1422,9 +1400,9 @@ static void __free_huge_page(struct page *page)
 	if (restore_reserve)
 		h->resv_huge_pages++;
 
-	if (PageHugeTemporary(page)) {
+	if (HPageTemporary(page)) {
 		list_del(&page->lru);
-		ClearPageHugeTemporary(page);
+		ClearHPageTemporary(page);
 		update_and_free_page(h, page);
 	} else if (h->surplus_huge_pages_node[nid]) {
 		/* remove the page from active list */
@@ -1860,7 +1838,7 @@ static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,
 	 * codeflow
 	 */
 	if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
-		SetPageHugeTemporary(page);
+		SetHPageTemporary(page);
 		spin_unlock(&hugetlb_lock);
 		put_page(page);
 		return NULL;
@@ -1891,7 +1869,7 @@ static struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
 	 * We do not account these pages as surplus because they are only
 	 * temporary and will be released properly on the last reference
 	 */
-	SetPageHugeTemporary(page);
+	SetHPageTemporary(page);
 
 	return page;
 }
@@ -5612,12 +5590,12 @@ void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
 	 * here as well otherwise the global surplus count will not match
 	 * the per-node's.
 	 */
-	if (PageHugeTemporary(newpage)) {
+	if (HPageTemporary(newpage)) {
 		int old_nid = page_to_nid(oldpage);
 		int new_nid = page_to_nid(newpage);
 
-		SetPageHugeTemporary(oldpage);
-		ClearPageHugeTemporary(newpage);
+		SetHPageTemporary(oldpage);
+		ClearHPageTemporary(newpage);
 
 		spin_lock(&hugetlb_lock);
 		if (h->surplus_huge_pages_node[old_nid]) {
Signature · 1 line
2.29.2
Reviewed-by on v3 4/5
#22 lore raw

Re: [External] [PATCH v3 4/5] hugetlb: convert PageHugeTemporary() to HPageTemporary flag

Quoted 4 lines · Mike Kravetz

X-Gm-Spam: 0
X-Gm-Phishy: 0

Quoted 8 lines

Use new hugetlb specific HPageTemporary flag to replace the PageHugeTemporary() interfaces. PageHugeTemporary does contain a PageHuge() check. However, this interface is only used within hugetlb code where we know we are dealing with a hugetlb page. Therefore, the check can be eliminated.

Quoted 115 lines
---
 include/linux/hugetlb.h |  6 ++++++
 mm/hugetlb.c            | 36 +++++++-----------------------------
 2 files changed, 13 insertions(+), 29 deletions(-)
include/linux/hugetlb.h +6 -0
index cd1960541f2a..3c86c3a0e144 100644
@@ -483,10 +483,15 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  * HPG_migratable  - Set after a newly allocated page is added to the page
  *     cache and/or page tables.  Indicates the page is a candidate for
  *     migration.
+ * HPG_temporary - - Set on a page that is temporarily allocated from the buddy
+ *     allocator.  Typically used for migration target pages when no pages
+ *     are available in the pool.  The hugetlb free page path will
+ *     immediately free pages with this flag set to the buddy allocator.
  */
 enum hugetlb_page_flags {
        HPG_restore_reserve = 0,
        HPG_migratable,
+       HPG_temporary,
        __NR_HPAGEFLAGS,
 };
 
@@ -530,6 +535,7 @@ static inline void ClearHPage##uname(struct page *page)             \
  */
 HPAGEFLAG(RestoreReserve, restore_reserve)
 HPAGEFLAG(Migratable, migratable)
+HPAGEFLAG(Temporary, temporary)
 
 #ifdef CONFIG_HUGETLB_PAGE
 
mm/hugetlb.c +7 -30
index 4da1a29ac5e2..70ffa1027988 100644
@@ -1353,28 +1353,6 @@ struct hstate *size_to_hstate(unsigned long size)
        return NULL;
 }
 
-/*
- * Internal hugetlb specific page flag. Do not use outside of the hugetlb
- * code
- */
-static inline bool PageHugeTemporary(struct page *page)
-{
-       if (!PageHuge(page))
-               return false;
-
-       return (unsigned long)page[2].mapping == -1U;
-}
-
-static inline void SetPageHugeTemporary(struct page *page)
-{
-       page[2].mapping = (void *)-1U;
-}
-
-static inline void ClearPageHugeTemporary(struct page *page)
-{
-       page[2].mapping = NULL;
-}
-
 static void __free_huge_page(struct page *page)
 {
        /*
@@ -1422,9 +1400,9 @@ static void __free_huge_page(struct page *page)
        if (restore_reserve)
                h->resv_huge_pages++;
 
-       if (PageHugeTemporary(page)) {
+       if (HPageTemporary(page)) {
                list_del(&page->lru);
-               ClearPageHugeTemporary(page);
+               ClearHPageTemporary(page);
                update_and_free_page(h, page);
        } else if (h->surplus_huge_pages_node[nid]) {
                /* remove the page from active list */
@@ -1860,7 +1838,7 @@ static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,
         * codeflow
         */
        if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
-               SetPageHugeTemporary(page);
+               SetHPageTemporary(page);
                spin_unlock(&hugetlb_lock);
                put_page(page);
                return NULL;
@@ -1891,7 +1869,7 @@ static struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
         * We do not account these pages as surplus because they are only
         * temporary and will be released properly on the last reference
         */
-       SetPageHugeTemporary(page);
+       SetHPageTemporary(page);
 
        return page;
 }
@@ -5612,12 +5590,12 @@ void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
         * here as well otherwise the global surplus count will not match
         * the per-node's.
         */
-       if (PageHugeTemporary(newpage)) {
+       if (HPageTemporary(newpage)) {
                int old_nid = page_to_nid(oldpage);
                int new_nid = page_to_nid(newpage);
 
-               SetPageHugeTemporary(oldpage);
-               ClearPageHugeTemporary(newpage);
+               SetHPageTemporary(oldpage);
+               ClearHPageTemporary(newpage);
 
                spin_lock(&hugetlb_lock);
                if (h->surplus_huge_pages_node[old_nid]) {
--

2.29.2

reviewer Acked-by on v3 4/5
#23 lore raw

Re: [PATCH v3 4/5] hugetlb: convert PageHugeTemporary() to HPageTemporary flag

In reply to Mike Kravetz
Quoted 5 lines · Mike Kravetz

Use new hugetlb specific HPageTemporary flag to replace the PageHugeTemporary() interfaces. PageHugeTemporary does contain a PageHuge() check. However, this interface is only used within hugetlb code where we know we are dealing with a hugetlb page. Therefore, the check can be eliminated.

A dedicated flag is definitely something I would go for if that was available back then when I introduced it.

Quoted 2 lines

Thanks!

Quoted 114 lines
---
 include/linux/hugetlb.h |  6 ++++++
 mm/hugetlb.c            | 36 +++++++-----------------------------
 2 files changed, 13 insertions(+), 29 deletions(-)
include/linux/hugetlb.h +6 -0
index cd1960541f2a..3c86c3a0e144 100644
@@ -483,10 +483,15 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  * HPG_migratable  - Set after a newly allocated page is added to the page
  *	cache and/or page tables.  Indicates the page is a candidate for
  *	migration.
+ * HPG_temporary - - Set on a page that is temporarily allocated from the buddy
+ *	allocator.  Typically used for migration target pages when no pages
+ *	are available in the pool.  The hugetlb free page path will
+ *	immediately free pages with this flag set to the buddy allocator.
  */
 enum hugetlb_page_flags {
 	HPG_restore_reserve = 0,
 	HPG_migratable,
+	HPG_temporary,
 	__NR_HPAGEFLAGS,
 };
 
@@ -530,6 +535,7 @@ static inline void ClearHPage##uname(struct page *page)		\
  */
 HPAGEFLAG(RestoreReserve, restore_reserve)
 HPAGEFLAG(Migratable, migratable)
+HPAGEFLAG(Temporary, temporary)
 
 #ifdef CONFIG_HUGETLB_PAGE
 
mm/hugetlb.c +7 -29
index 4da1a29ac5e2..70ffa1027988 100644
@@ -1353,28 +1353,6 @@ struct hstate *size_to_hstate(unsigned long size)
 	return NULL;
 }
 
-/*
- * Internal hugetlb specific page flag. Do not use outside of the hugetlb
- * code
- */
-static inline bool PageHugeTemporary(struct page *page)
-{
-	if (!PageHuge(page))
-		return false;
-
-	return (unsigned long)page[2].mapping == -1U;
-}
-
-static inline void SetPageHugeTemporary(struct page *page)
-{
-	page[2].mapping = (void *)-1U;
-}
-
-static inline void ClearPageHugeTemporary(struct page *page)
-{
-	page[2].mapping = NULL;
-}
-
 static void __free_huge_page(struct page *page)
 {
 	/*
@@ -1422,9 +1400,9 @@ static void __free_huge_page(struct page *page)
 	if (restore_reserve)
 		h->resv_huge_pages++;
 
-	if (PageHugeTemporary(page)) {
+	if (HPageTemporary(page)) {
 		list_del(&page->lru);
-		ClearPageHugeTemporary(page);
+		ClearHPageTemporary(page);
 		update_and_free_page(h, page);
 	} else if (h->surplus_huge_pages_node[nid]) {
 		/* remove the page from active list */
@@ -1860,7 +1838,7 @@ static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,
 	 * codeflow
 	 */
 	if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
-		SetPageHugeTemporary(page);
+		SetHPageTemporary(page);
 		spin_unlock(&hugetlb_lock);
 		put_page(page);
 		return NULL;
@@ -1891,7 +1869,7 @@ static struct page *alloc_migrate_huge_page(struct hstate *h, gfp_t gfp_mask,
 	 * We do not account these pages as surplus because they are only
 	 * temporary and will be released properly on the last reference
 	 */
-	SetPageHugeTemporary(page);
+	SetHPageTemporary(page);
 
 	return page;
 }
@@ -5612,12 +5590,12 @@ void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)
 	 * here as well otherwise the global surplus count will not match
 	 * the per-node's.
 	 */
-	if (PageHugeTemporary(newpage)) {
+	if (HPageTemporary(newpage)) {
 		int old_nid = page_to_nid(oldpage);
 		int new_nid = page_to_nid(newpage);
 
-		SetPageHugeTemporary(oldpage);
-		ClearPageHugeTemporary(newpage);
+		SetHPageTemporary(oldpage);
+		ClearHPageTemporary(newpage);
 
 		spin_lock(&hugetlb_lock);
 		if (h->surplus_huge_pages_node[old_nid]) {
Signature · 1 line
2.29.2
Signature · 2 lines
Michal Hocko
SUSE Labs
#24 lore raw

PATCH v3 5/5 hugetlb: convert PageHugeFreed to HPageFreed flag

In reply to Mike Kravetz

Use new hugetlb specific HPageFreed flag to replace the PageHugeFreed interfaces.

---
 include/linux/hugetlb.h |  3 +++
 mm/hugetlb.c            | 23 ++++-------------------
 2 files changed, 7 insertions(+), 19 deletions(-)
include/linux/hugetlb.h +3 -0
index 3c86c3a0e144..f09085c0afea 100644
@@ -487,11 +487,13 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  *	allocator.  Typically used for migration target pages when no pages
  *	are available in the pool.  The hugetlb free page path will
  *	immediately free pages with this flag set to the buddy allocator.
+ * HPG_freed - Set when page is on the free lists.
  */
 enum hugetlb_page_flags {
 	HPG_restore_reserve = 0,
 	HPG_migratable,
 	HPG_temporary,
+	HPG_freed,
 	__NR_HPAGEFLAGS,
 };
 
@@ -536,6 +538,7 @@ static inline void ClearHPage##uname(struct page *page)		\
 HPAGEFLAG(RestoreReserve, restore_reserve)
 HPAGEFLAG(Migratable, migratable)
 HPAGEFLAG(Temporary, temporary)
+HPAGEFLAG(Freed, freed)
 
 #ifdef CONFIG_HUGETLB_PAGE
 
mm/hugetlb.c +4 -19
index 70ffa1027988..c1f283cee0a9 100644
@@ -79,21 +79,6 @@ DEFINE_SPINLOCK(hugetlb_lock);
 static int num_fault_mutexes;
 struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp;
 
-static inline bool PageHugeFreed(struct page *head)
-{
-	return page_private(head + 4) == -1UL;
-}
-
-static inline void SetPageHugeFreed(struct page *head)
-{
-	set_page_private(head + 4, -1UL);
-}
-
-static inline void ClearPageHugeFreed(struct page *head)
-{
-	set_page_private(head + 4, 0);
-}
-
 /* Forward declaration */
 static int hugetlb_acct_memory(struct hstate *h, long delta);
 
@@ -1043,7 +1028,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)
 	list_move(&page->lru, &h->hugepage_freelists[nid]);
 	h->free_huge_pages++;
 	h->free_huge_pages_node[nid]++;
-	SetPageHugeFreed(page);
+	SetHPageFreed(page);
 }
 
 static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
@@ -1060,7 +1045,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
 
 		list_move(&page->lru, &h->hugepage_activelist);
 		set_page_refcounted(page);
-		ClearPageHugeFreed(page);
+		ClearHPageFreed(page);
 		h->free_huge_pages--;
 		h->free_huge_pages_node[nid]--;
 		return page;
@@ -1474,7 +1459,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
 	spin_lock(&hugetlb_lock);
 	h->nr_huge_pages++;
 	h->nr_huge_pages_node[nid]++;
-	ClearPageHugeFreed(page);
+	ClearHPageFreed(page);
 	spin_unlock(&hugetlb_lock);
 }
 
@@ -1747,7 +1732,7 @@ int dissolve_free_huge_page(struct page *page)
 		 * We should make sure that the page is already on the free list
 		 * when it is dissolved.
 		 */
-		if (unlikely(!PageHugeFreed(head))) {
+		if (unlikely(!HPageFreed(head))) {
 			spin_unlock(&hugetlb_lock);
 			cond_resched();
 
Signature · 1 line
2.29.2
reviewer Acked-by on v3 5/5
#25 lore raw

Re: [PATCH v3 5/5] hugetlb: convert PageHugeFreed to HPageFreed flag

Quoted 6 lines · Mike Kravetz

Use new hugetlb specific HPageFreed flag to replace the PageHugeFreed interfaces.

Again I would love to see locking that is required to gain a stable value.

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs
#26 lore raw

Re: [PATCH v3 5/5] hugetlb: convert PageHugeFreed to HPageFreed flag

Quoted 4 lines · Michal Hocko
Quoted 3 lines · Mike Kravetz

Use new hugetlb specific HPageFreed flag to replace the PageHugeFreed interfaces.

Quoted 8 lines
Quoted 3 lines

Again I would love to see locking that is required to gain a stable value.

Will do

Signature · 1 line
Mike Kravetz