Skip to contentSkip to conversation

A few cleanup and fixup patches for migration

PATCH Superseded Miaohe Lin 7 participants 73 messages Mar 4, 2022 – Mar 9, 2022

Subsystems (5)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

cover A few cleanup and fixup patches for migration

Hi everyone,
This series contains a few patches to remove unneeded variables, jump label and use helper to simplify the code. Also we fix some bugs such as page refcounts leak , pte_unmap on an not mapped pte and so on. More details can be found in the respective changelogs. Thanks!

Miaohe Lin (16):
mm/migration: remove unneeded local variable mapping_locked mm/migration: remove unneeded out label
mm/migration: remove unneeded local variable page_lru mm/migration: reduce the rcu lock duration mm/migration: fix the confusing PageTransHuge check mm/migration: use helper function vma_lookup() in
add_page_for_migration
mm/migration: use helper macro min_t in do_pages_stat mm/migration: avoid unneeded nodemask_t initialization mm/migration: remove some duplicated codes in migrate_pages mm/migration: remove PG_writeback handle in folio_migrate_flags mm/migration: remove unneeded lock page and PageMovable check mm/migration: fix potential page refcounts leak in migrate_pages mm/migration: return errno when isolate_huge_page failed mm/migration: fix potential invalid node access for reclaim-based
migration
mm/migration: fix possible do_pages_stat_array racing with memory
offline
mm/migration: fix potential pte_unmap on an not mapped pte

 include/linux/migrate.h |   2 +-
 include/linux/swapops.h |   4 +-
 mm/filemap.c            |  10 +--
 mm/hugetlb.c            |   2 +-
 mm/migrate.c            | 138 ++++++++++++++++------------------------
 5 files changed, 65 insertions(+), 91 deletions(-)
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 1/16 mm/migration: remove unneeded local variable mapping_locked

When mapping_locked is true, TTU_RMAP_LOCKED is always set to ttu. We can check ttu instead so mapping_locked can be removed. And ttu is either 0 or TTU_RMAP_LOCKED now. Change '|=' to '=' to reflect this.

---
 mm/migrate.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +2 -4
index 42e3a944f35c..50bc62d85eaf 100644
@@ -1230,7 +1230,6 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 		goto put_anon;
 
 	if (page_mapped(hpage)) {
-		bool mapping_locked = false;
 		enum ttu_flags ttu = 0;
 
 		if (!PageAnon(hpage)) {
@@ -1244,14 +1243,13 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 			if (unlikely(!mapping))
 				goto unlock_put_anon;
 
-			mapping_locked = true;
-			ttu |= TTU_RMAP_LOCKED;
+			ttu = TTU_RMAP_LOCKED;
 		}
 
 		try_to_migrate(src, ttu);
 		page_was_mapped = 1;
 
-		if (mapping_locked)
+		if (ttu & TTU_RMAP_LOCKED)
 			i_mmap_unlock_write(mapping);
 	}
 
Signature · 1 line
2.23.0
Reviewed-by on v1 1/16
#2 lore raw

Re: [PATCH 01/16] mm/migration: remove unneeded local variable mapping_locked

Quoted 6 lines · Miaohe Lin

When mapping_locked is true, TTU_RMAP_LOCKED is always set to ttu. We can check ttu instead so mapping_locked can be removed. And ttu is either 0 or TTU_RMAP_LOCKED now. Change '|=' to '=' to reflect this.

Thanks.

#3 lore raw

Re: [PATCH 01/16] mm/migration: remove unneeded local variable mapping_locked

In reply to Miaohe Lin
Quoted 27 lines · Miaohe Lin

When mapping_locked is true, TTU_RMAP_LOCKED is always set to ttu. We can check ttu instead so mapping_locked can be removed. And ttu is either 0 or TTU_RMAP_LOCKED now. Change '|=' to '=' to reflect this.

---
 mm/migrate.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +0 -3
index 42e3a944f35c..50bc62d85eaf 100644
@@ -1230,7 +1230,6 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 		goto put_anon;
 
 	if (page_mapped(hpage)) {
-		bool mapping_locked = false;
 		enum ttu_flags ttu = 0;
 
 		if (!PageAnon(hpage)) {
@@ -1244,14 +1243,13 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 			if (unlikely(!mapping))
 				goto unlock_put_anon;
 
-			mapping_locked = true;
-			ttu |= TTU_RMAP_LOCKED;

mm/migrate.c static int unmap_and_move_huge_page(new_page_t get_new_page, · line 1254

Quoted 10 lines
mm/migrate.c +2 -1
+			ttu = TTU_RMAP_LOCKED;
 		}
 
 		try_to_migrate(src, ttu);
 		page_was_mapped = 1;
 
-		if (mapping_locked)
+		if (ttu & TTU_RMAP_LOCKED)
 			i_mmap_unlock_write(mapping);
 	}

I think this is a code style issue. The original code looks more readable for me.

Best Regards,
Huang, Ying

author
#4 lore raw

Re: [PATCH 01/16] mm/migration: remove unneeded local variable mapping_locked

Quoted 35 lines · Ying
Quoted 33 lines · Miaohe Lin

When mapping_locked is true, TTU_RMAP_LOCKED is always set to ttu. We can check ttu instead so mapping_locked can be removed. And ttu is either 0 or TTU_RMAP_LOCKED now. Change '|=' to '=' to reflect this.

---
 mm/migrate.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +1 -3
index 42e3a944f35c..50bc62d85eaf 100644
@@ -1230,7 +1230,6 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 		goto put_anon;
 
 	if (page_mapped(hpage)) {
-		bool mapping_locked = false;
 		enum ttu_flags ttu = 0;
 
 		if (!PageAnon(hpage)) {
@@ -1244,14 +1243,13 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 			if (unlikely(!mapping))
 				goto unlock_put_anon;
 
-			mapping_locked = true;
-			ttu |= TTU_RMAP_LOCKED;
+			ttu = TTU_RMAP_LOCKED;
 		}
 
 		try_to_migrate(src, ttu);
 		page_was_mapped = 1;
 
Quoted 8 lines

mm/migrate.c static int unmap_and_move_huge_page(new_page_t get_new_page, · line 1254

Quoted 4 lines
mm/migrate.c +1 -1
-		if (mapping_locked)
+		if (ttu & TTU_RMAP_LOCKED)
 			i_mmap_unlock_write(mapping);
 	}

I think this is a code style issue. The original code looks more readable for me.

I think "ttu |= TTU_RMAP_LOCKED;" is the product of the code changing. And (ttu & TTU_RMAP_LOCKED) can do the right thing as mapping_locked while removing the page_was_mapped. But if you insist on this, I will drop this patch.

Thanks.

Quoted 5 lines

Best Regards,
Huang, Ying

.

author
#5 lore raw

PATCH 2/16 mm/migration: remove unneeded out label

In reply to Miaohe Lin

We can do prep_transhuge_page when newpage is not NULL. Thus we can remove out label to simplify the code.

---
 mm/migrate.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
mm/migrate.c +2 -5
index 50bc62d85eaf..bc1867a5706c 100644
@@ -2025,12 +2025,9 @@ static struct page *alloc_misplaced_dst_page_thp(struct page *page,
 
 	newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
 				   HPAGE_PMD_ORDER);
-	if (!newpage)
-		goto out;
+	if (newpage)
+		prep_transhuge_page(newpage);
 
-	prep_transhuge_page(newpage);
-
-out:
 	return newpage;
 }
 
Signature · 1 line
2.23.0
#7 lore raw

Re: [PATCH 02/16] mm/migration: remove unneeded out label

In reply to Miaohe Lin
Quoted 16 lines · Miaohe Lin

We can do prep_transhuge_page when newpage is not NULL. Thus we can remove out label to simplify the code.

---
 mm/migrate.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
mm/migrate.c +0 -0
index 50bc62d85eaf..bc1867a5706c 100644
@@ -2025,12 +2025,9 @@ static struct page *alloc_misplaced_dst_page_thp(struct page *page,
 
 	newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
 				   HPAGE_PMD_ORDER);

mm/migrate.c static struct page *alloc_misplaced_dst_page_thp(struct page *page, · line 2032

Quoted 10 lines
mm/migrate.c +2 -5
-	if (!newpage)
-		goto out;
+	if (newpage)
+		prep_transhuge_page(newpage);
 
-	prep_transhuge_page(newpage);
-
-out:
 	return newpage;
 }

I don't think this change is necessary. The original code is simple and follows the common practice for error processing. The new code is OK, but it's unnecessary to change.

Best Regards,
Huang, Ying

author
#8 lore raw

Re: [PATCH 02/16] mm/migration: remove unneeded out label

Quoted 23 lines · Ying
Quoted 21 lines · Miaohe Lin

We can do prep_transhuge_page when newpage is not NULL. Thus we can remove out label to simplify the code.

---
 mm/migrate.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
mm/migrate.c +2 -2
index 50bc62d85eaf..bc1867a5706c 100644
@@ -2025,12 +2025,9 @@ static struct page *alloc_misplaced_dst_page_thp(struct page *page,
 
 	newpage = alloc_pages_node(nid, (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
 				   HPAGE_PMD_ORDER);
-	if (!newpage)
-		goto out;
+	if (newpage)
+		prep_transhuge_page(newpage);
 
Quoted 10 lines

mm/migrate.c static struct page *alloc_misplaced_dst_page_thp(struct page *page, · line 2032

Quoted 5 lines
mm/migrate.c +0 -3
-	prep_transhuge_page(newpage);
-
-out:
 	return newpage;
 }

I don't think this change is necessary. The original code is simple and follows the common practice for error processing. The new code is OK, but it's unnecessary to change.

IMO, this out label looks 'overkill'. We should remove it and make code more succinct. Does this make sense to you? Thanks.

Quoted 4 lines

Best Regards,
Huang, Ying
.

author
#9 lore raw

PATCH 3/16 mm/migration: remove unneeded local variable page_lru

In reply to Miaohe Lin

We can use page_is_file_lru() directly to help account the isolated pages to simplify the code a bit as same as local variable follflags.

---
 mm/migrate.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
mm/migrate.c +2 -6
index bc1867a5706c..da5a81052468 100644
@@ -1609,7 +1609,6 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 {
 	struct vm_area_struct *vma;
 	struct page *page;
-	unsigned int follflags;
 	int err;
 
 	mmap_read_lock(mm);
@@ -1619,8 +1618,7 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 		goto out;
 
 	/* FOLL_DUMP to ignore special (like zero) pages */
-	follflags = FOLL_GET | FOLL_DUMP;
-	page = follow_page(vma, addr, follflags);
+	page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
 
 	err = PTR_ERR(page);
 	if (IS_ERR(page))
@@ -2033,7 +2031,6 @@ static struct page *alloc_misplaced_dst_page_thp(struct page *page,
 
 static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
 {
-	int page_lru;
 	int nr_pages = thp_nr_pages(page);
 	int order = compound_order(page);
 
@@ -2060,8 +2057,7 @@ static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
 	if (isolate_lru_page(page))
 		return 0;
 
-	page_lru = page_is_file_lru(page);
-	mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
+	mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_is_file_lru(page),
 			    nr_pages);
 
 	/*
Signature · 1 line
2.23.0
#10 lore raw

Re: [PATCH 03/16] mm/migration: remove unneeded local variable page_lru

Quoted 2 lines · Miaohe Lin

We can use page_is_file_lru() directly to help account the isolated pages to simplify the code a bit as same as local variable follflags.

Looks good, but there are two independent changes here. Even though they are small they should probably be split into two patches.

Quoted 45 lines
---
 mm/migrate.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
mm/migrate.c +2 -6
index bc1867a5706c..da5a81052468 100644
@@ -1609,7 +1609,6 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 {
 	struct vm_area_struct *vma;
 	struct page *page;
-	unsigned int follflags;
 	int err;
 
 	mmap_read_lock(mm);
@@ -1619,8 +1618,7 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 		goto out;
 
 	/* FOLL_DUMP to ignore special (like zero) pages */
-	follflags = FOLL_GET | FOLL_DUMP;
-	page = follow_page(vma, addr, follflags);
+	page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
 
 	err = PTR_ERR(page);
 	if (IS_ERR(page))
@@ -2033,7 +2031,6 @@ static struct page *alloc_misplaced_dst_page_thp(struct page *page,
 
 static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
 {
-	int page_lru;
 	int nr_pages = thp_nr_pages(page);
 	int order = compound_order(page);
 
@@ -2060,8 +2057,7 @@ static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
 	if (isolate_lru_page(page))
 		return 0;
 
-	page_lru = page_is_file_lru(page);
-	mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
+	mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_is_file_lru(page),
 			    nr_pages);
 
 	/*
author
#11 lore raw

Re: [PATCH 03/16] mm/migration: remove unneeded local variable page_lru

Quoted 7 lines · Alistair Popple
Quoted 2 lines · Miaohe Lin

We can use page_is_file_lru() directly to help account the isolated pages to simplify the code a bit as same as local variable follflags.

Looks good, but there are two independent changes here. Even though they are small they should probably be split into two patches.

Sounds reasonable. Will try to do this in v2. Thanks.

Quoted 46 lines
Quoted 46 lines
---
 mm/migrate.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
mm/migrate.c +2 -6
index bc1867a5706c..da5a81052468 100644
@@ -1609,7 +1609,6 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 {
 	struct vm_area_struct *vma;
 	struct page *page;
-	unsigned int follflags;
 	int err;
 
 	mmap_read_lock(mm);
@@ -1619,8 +1618,7 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 		goto out;
 
 	/* FOLL_DUMP to ignore special (like zero) pages */
-	follflags = FOLL_GET | FOLL_DUMP;
-	page = follow_page(vma, addr, follflags);
+	page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
 
 	err = PTR_ERR(page);
 	if (IS_ERR(page))
@@ -2033,7 +2031,6 @@ static struct page *alloc_misplaced_dst_page_thp(struct page *page,
 
 static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
 {
-	int page_lru;
 	int nr_pages = thp_nr_pages(page);
 	int order = compound_order(page);
 
@@ -2060,8 +2057,7 @@ static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
 	if (isolate_lru_page(page))
 		return 0;
 
-	page_lru = page_is_file_lru(page);
-	mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
+	mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_is_file_lru(page),
 			    nr_pages);
 
 	/*
author
#12 lore raw

PATCH 4/16 mm/migration: reduce the rcu lock duration

In reply to Miaohe Lin

rcu_read_lock is required by grabbing the task refcount but it's not needed for ptrace_may_access. So we could release the rcu lock after task refcount is successfully grabbed to reduce the rcu holding time.

---
 mm/migrate.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/migrate.c +1 -2
index da5a81052468..26943bd819e8 100644
@@ -1907,17 +1907,16 @@ static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
 		return ERR_PTR(-ESRCH);
 	}
 	get_task_struct(task);
+	rcu_read_unlock();
 
 	/*
 	 * Check if this process has the right to modify the specified
 	 * process. Use the regular "ptrace_may_access()" checks.
 	 */
 	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
-		rcu_read_unlock();
 		mm = ERR_PTR(-EPERM);
 		goto out;
 	}
-	rcu_read_unlock();
 
 	mm = ERR_PTR(security_task_movememory(task));
 	if (IS_ERR(mm))
Signature · 1 line
2.23.0
Reviewed-by on v1 4/16
#13 lore raw

Re: [PATCH 04/16] mm/migration: reduce the rcu lock duration

Quoted 6 lines · Miaohe Lin

rcu_read_lock is required by grabbing the task refcount but it's not needed for ptrace_may_access. So we could release the rcu lock after task refcount is successfully grabbed to reduce the rcu holding time.

Also simple.

#14 lore raw

Re: [PATCH 04/16] mm/migration: reduce the rcu lock duration

In reply to Miaohe Lin
Quoted 22 lines · Miaohe Lin

rcu_read_lock is required by grabbing the task refcount but it's not needed for ptrace_may_access. So we could release the rcu lock after task refcount is successfully grabbed to reduce the rcu holding time.

---
 mm/migrate.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/migrate.c +1 -0
index da5a81052468..26943bd819e8 100644
@@ -1907,17 +1907,16 @@ static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
 		return ERR_PTR(-ESRCH);
 	}
 	get_task_struct(task);
+	rcu_read_unlock();
 
 	/*
 	 * Check if this process has the right to modify the specified
 	 * process. Use the regular "ptrace_may_access()" checks.

mm/migrate.c static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes) · line 1922

Quoted 10 lines
mm/migrate.c +0 -2
 	 */
 	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
-		rcu_read_unlock();
 		mm = ERR_PTR(-EPERM);
 		goto out;
 	}
-	rcu_read_unlock();
 
 	mm = ERR_PTR(security_task_movememory(task));
 	if (IS_ERR(mm))

Digged some history via git blame, found that the RCU read lock is extended in the following commit,

"
3268c63eded4612a3d07b56d1e02ce7731e6608e

Author:     Christoph Lameter <[email protected]>
AuthorDate: Wed Mar 21 16:34:06 2012 -0700
Commit:     Linus Torvalds <[email protected]>
CommitDate: Wed Mar 21 17:54:58 2012 -0700

mm: fix move/migrate_pages() race on task struct

Migration functions perform the rcu_read_unlock too early. As a result the task pointed to may change from under us. This can result in an oops, as reported by Dave Hansen in https://lkml.org/lkml/2012/2/23/302.

The following patch extend the period of the rcu_read_lock until after the permissions checks are done. We also take a refcount so that the task reference is stable when calling security check functions and performing cpuset node validation (which takes a mutex).

The refcount is dropped before actual page migration occurs so there is no change to the refcounts held during page migration.

Also move the determination of the mm of the task struct to immediately before the do_migrate*() calls so that it is clear that we switch from handling the task during permission checks to the mm for the actual migration. Since the determination is only done once and we then no longer use the task_struct we can be sure that we operate on a specific address space that will not change from under us.
"

After that, the permission checking has been changed from __task_cred() to ptrace_may_access(). So the situation may change somewhat. Cced some names found in git history to verify.

Best Regards,
Huang, Ying

author
#15 lore raw

Re: [PATCH 04/16] mm/migration: reduce the rcu lock duration

Quoted 67 lines · Ying
Quoted 22 lines · Miaohe Lin

rcu_read_lock is required by grabbing the task refcount but it's not needed for ptrace_may_access. So we could release the rcu lock after task refcount is successfully grabbed to reduce the rcu holding time.

---
 mm/migrate.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/migrate.c +1 -0
index da5a81052468..26943bd819e8 100644
@@ -1907,17 +1907,16 @@ static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
 		return ERR_PTR(-ESRCH);
 	}
 	get_task_struct(task);
+	rcu_read_unlock();
 
 	/*
 	 * Check if this process has the right to modify the specified
 	 * process. Use the regular "ptrace_may_access()" checks.

mm/migrate.c static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes) · line 1922

Quoted 10 lines
mm/migrate.c +0 -2
 	 */
 	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
-		rcu_read_unlock();
 		mm = ERR_PTR(-EPERM);
 		goto out;
 	}
-	rcu_read_unlock();
 
 	mm = ERR_PTR(security_task_movememory(task));
 	if (IS_ERR(mm))

Digged some history via git blame, found that the RCU read lock is extended in the following commit,

"
3268c63eded4612a3d07b56d1e02ce7731e6608e

Author:     Christoph Lameter <[email protected]>
AuthorDate: Wed Mar 21 16:34:06 2012 -0700
Commit:     Linus Torvalds <[email protected]>
CommitDate: Wed Mar 21 17:54:58 2012 -0700

mm: fix move/migrate_pages() race on task struct

Migration functions perform the rcu_read_unlock too early. As a result the task pointed to may change from under us. This can result in an oops, as reported by Dave Hansen in https://lkml.org/lkml/2012/2/23/302.

The following patch extend the period of the rcu_read_lock until after the permissions checks are done. We also take a refcount so that the task reference is stable when calling security check functions and performing cpuset node validation (which takes a mutex).

The refcount is dropped before actual page migration occurs so there is no change to the refcounts held during page migration.

Also move the determination of the mm of the task struct to immediately before the do_migrate*() calls so that it is clear that we switch from handling the task during permission checks to the mm for the actual migration. Since the determination is only done once and we then no longer use the task_struct we can be sure that we operate on a specific address space that will not change from under us.
"

Quoted 2 lines

After that, the permission checking has been changed from __task_cred() to ptrace_may_access(). So the situation may change somewhat. Cced

In ptrace_may_access, __task_cred is access while holding the rcu read lock. It seems this is ensured by the ptrace_may_access itself.

Quoted 1 line

some names found in git history to verify.

Thanks for your carefulness.

Quoted 5 lines

Best Regards,
Huang, Ying
.

#16 lore raw

Re: [PATCH 04/16] mm/migration: reduce the rcu lock duration

Quoted 68 lines · Miaohe Lin
Quoted 67 lines · Ying
Quoted 22 lines · Miaohe Lin

rcu_read_lock is required by grabbing the task refcount but it's not needed for ptrace_may_access. So we could release the rcu lock after task refcount is successfully grabbed to reduce the rcu holding time.

---
 mm/migrate.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/migrate.c +1 -0
index da5a81052468..26943bd819e8 100644
@@ -1907,17 +1907,16 @@ static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
 		return ERR_PTR(-ESRCH);
 	}
 	get_task_struct(task);
+	rcu_read_unlock();
 
 	/*
 	 * Check if this process has the right to modify the specified
 	 * process. Use the regular "ptrace_may_access()" checks.

mm/migrate.c static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes) · line 1922

Quoted 10 lines
mm/migrate.c +0 -2
 	 */
 	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
-		rcu_read_unlock();
 		mm = ERR_PTR(-EPERM);
 		goto out;
 	}
-	rcu_read_unlock();
 
 	mm = ERR_PTR(security_task_movememory(task));
 	if (IS_ERR(mm))

Digged some history via git blame, found that the RCU read lock is extended in the following commit,

"
3268c63eded4612a3d07b56d1e02ce7731e6608e

Author:     Christoph Lameter <[email protected]>
AuthorDate: Wed Mar 21 16:34:06 2012 -0700
Commit:     Linus Torvalds <[email protected]>
CommitDate: Wed Mar 21 17:54:58 2012 -0700

mm: fix move/migrate_pages() race on task struct

Migration functions perform the rcu_read_unlock too early. As a result the task pointed to may change from under us. This can result in an oops, as reported by Dave Hansen in https://lkml.org/lkml/2012/2/23/302.

The following patch extend the period of the rcu_read_lock until after the permissions checks are done. We also take a refcount so that the task reference is stable when calling security check functions and performing cpuset node validation (which takes a mutex).

The refcount is dropped before actual page migration occurs so there is no change to the refcounts held during page migration.

Also move the determination of the mm of the task struct to immediately before the do_migrate*() calls so that it is clear that we switch from handling the task during permission checks to the mm for the actual migration. Since the determination is only done once and we then no longer use the task_struct we can be sure that we operate on a specific address space that will not change from under us.
"

Quoted 5 lines
Quoted 2 lines

After that, the permission checking has been changed from __task_cred() to ptrace_may_access(). So the situation may change somewhat. Cced

In ptrace_may_access, __task_cred is access while holding the rcu read lock. It seems this is ensured by the ptrace_may_access itself.

Please read the patch above. Before extending rcu_read_lock protected region, __task_cred() is protected by rcu_read_lock already. The patch above combines 2 regions into 1.

Best Regards,
Huang, Ying

Quoted 9 lines
Quoted 1 line

some names found in git history to verify.

Thanks for your carefulness.

Quoted 5 lines

Best Regards,
Huang, Ying
.

author
#17 lore raw

Re: [PATCH 04/16] mm/migration: reduce the rcu lock duration

Quoted 73 lines · Ying
Quoted 71 lines · Miaohe Lin
Quoted 69 lines · Ying
Quoted 22 lines · Miaohe Lin

rcu_read_lock is required by grabbing the task refcount but it's not needed for ptrace_may_access. So we could release the rcu lock after task refcount is successfully grabbed to reduce the rcu holding time.

---
 mm/migrate.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/migrate.c +1 -0
index da5a81052468..26943bd819e8 100644
@@ -1907,17 +1907,16 @@ static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
 		return ERR_PTR(-ESRCH);
 	}
 	get_task_struct(task);
+	rcu_read_unlock();
 
 	/*
 	 * Check if this process has the right to modify the specified
 	 * process. Use the regular "ptrace_may_access()" checks.

mm/migrate.c static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes) · line 1922

Quoted 10 lines
mm/migrate.c +0 -2
 	 */
 	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
-		rcu_read_unlock();
 		mm = ERR_PTR(-EPERM);
 		goto out;
 	}
-	rcu_read_unlock();
 
 	mm = ERR_PTR(security_task_movememory(task));
 	if (IS_ERR(mm))

Digged some history via git blame, found that the RCU read lock is extended in the following commit,

"
3268c63eded4612a3d07b56d1e02ce7731e6608e

Author:     Christoph Lameter <[email protected]>
AuthorDate: Wed Mar 21 16:34:06 2012 -0700
Commit:     Linus Torvalds <[email protected]>
CommitDate: Wed Mar 21 17:54:58 2012 -0700

mm: fix move/migrate_pages() race on task struct

Migration functions perform the rcu_read_unlock too early. As a result the task pointed to may change from under us. This can result in an oops, as reported by Dave Hansen in https://lkml.org/lkml/2012/2/23/302.

The following patch extend the period of the rcu_read_lock until after the permissions checks are done. We also take a refcount so that the task reference is stable when calling security check functions and performing cpuset node validation (which takes a mutex).

The refcount is dropped before actual page migration occurs so there is no change to the refcounts held during page migration.

Also move the determination of the mm of the task struct to immediately before the do_migrate*() calls so that it is clear that we switch from handling the task during permission checks to the mm for the actual migration. Since the determination is only done once and we then no longer use the task_struct we can be sure that we operate on a specific address space that will not change from under us.
"

After that, the permission checking has been changed from __task_cred() to ptrace_may_access(). So the situation may change somewhat. Cced

Quoted 7 lines
Quoted 2 lines

In ptrace_may_access, __task_cred is access while holding the rcu read lock. It seems this is ensured by the ptrace_may_access itself.

Please read the patch above. Before extending rcu_read_lock protected region, __task_cred() is protected by rcu_read_lock already. The patch above combines 2 regions into 1.

Yep, you're right. Thanks.

Quoted 14 lines

Best Regards,
Huang, Ying

Quoted 9 lines
Quoted 1 line

some names found in git history to verify.

Thanks for your carefulness.

Quoted 5 lines

Best Regards,
Huang, Ying
.

.

author
#18 lore raw

PATCH 5/16 mm/migration: fix the confusing PageTransHuge check

In reply to Miaohe Lin

prep_transhuge_page should be called when PageTransHuge(page) is true. The newly allocated new_page is not yet PageTransHuge though it could pass the check as PageTransHuge only checks PageHead now.

---
 mm/migrate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/migrate.c +1 -1
index 26943bd819e8..15cac2dabc93 100644
@@ -1560,7 +1560,7 @@ struct page *alloc_migration_target(struct page *page, unsigned long private)
 
 	new_page = __alloc_pages(gfp_mask, order, nid, mtc->nmask);
 
-	if (new_page && PageTransHuge(new_page))
+	if (new_page && PageTransHuge(page))
 		prep_transhuge_page(new_page);
 
 	return new_page;
Signature · 1 line
2.23.0
Reviewed-by on v1 5/16
#19 lore raw

Re: [PATCH 05/16] mm/migration: fix the confusing PageTransHuge check

Quoted 6 lines · Miaohe Lin

prep_transhuge_page should be called when PageTransHuge(page) is true. The newly allocated new_page is not yet PageTransHuge though it could pass the check as PageTransHuge only checks PageHead now.

At least I agree with you.

author
#20 lore raw

PATCH 6/16 mm/migration: use helper function vma_lookup() in add_page_for_migration

In reply to Miaohe Lin

We could use helper function vma_lookup() to lookup the needed vma to simplify the code.

---
 mm/migrate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/migrate.c +2 -2
index 15cac2dabc93..bc79d7338780 100644
@@ -1613,8 +1613,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 
 	mmap_read_lock(mm);
 	err = -EFAULT;
-	vma = find_vma(mm, addr);
-	if (!vma || addr < vma->vm_start || !vma_migratable(vma))
+	vma = vma_lookup(mm, addr);
+	if (!vma || !vma_migratable(vma))
 		goto out;
 
 	/* FOLL_DUMP to ignore special (like zero) pages */
Signature · 1 line
2.23.0
author
#21 lore raw

PATCH 7/16 mm/migration: use helper macro min_t in do_pages_stat

In reply to Miaohe Lin

We could use helper macro min_t to help set the chunk_nr to simplify the code.

---
 mm/migrate.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/migrate.c +1 -3
index bc79d7338780..c84eec19072a 100644
@@ -1858,9 +1858,7 @@ static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
 	while (nr_pages) {
 		unsigned long chunk_nr;
 
-		chunk_nr = nr_pages;
-		if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
-			chunk_nr = DO_PAGES_STAT_CHUNK_NR;
+		chunk_nr = min_t(unsigned long, nr_pages, DO_PAGES_STAT_CHUNK_NR);
 
 		if (in_compat_syscall()) {
 			if (get_compat_pages_array(chunk_pages, pages,
Signature · 1 line
2.23.0
maintainer
#23 lore raw

Re: [PATCH 07/16] mm/migration: use helper macro min_t in do_pages_stat

In reply to Miaohe Lin
Quoted 8 lines · On Fri, 4 Mar 2022 17:34:00 +0800 Miaohe Lin <[email protected]> wrote:

We could use helper macro min_t to help set the chunk_nr to simplify the code.

...

mm/migrate.c static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages, · line 1864

Quoted 10 lines
mm/migrate.c +1 -3
@@ -1858,9 +1858,7 @@ static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
 	while (nr_pages) {
 		unsigned long chunk_nr;
 
-		chunk_nr = nr_pages;
-		if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
-			chunk_nr = DO_PAGES_STAT_CHUNK_NR;
+		chunk_nr = min_t(unsigned long, nr_pages, DO_PAGES_STAT_CHUNK_NR);
 
 		if (in_compat_syscall()) {
 			if (get_compat_pages_array(chunk_pages, pages,

Getting the types correct is better than using min_t().

mm/migrate.c renamed from mm/migrate.c~mm-migration-use-helper-macro-min_t-in-do_pages_stat-fix +2 -4
@@ -1844,14 +1844,12 @@ static int do_pages_stat(struct mm_struc
 			 const void __user * __user *pages,
 			 int __user *status)
 {
-#define DO_PAGES_STAT_CHUNK_NR 16
+#define DO_PAGES_STAT_CHUNK_NR 16UL
 	const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
 	int chunk_status[DO_PAGES_STAT_CHUNK_NR];
 
 	while (nr_pages) {
-		unsigned long chunk_nr;
-
-		chunk_nr = min_t(unsigned long, nr_pages, DO_PAGES_STAT_CHUNK_NR);
+		unsigned long chunk_nr = min(nr_pages, DO_PAGES_STAT_CHUNK_NR);
 
 		if (in_compat_syscall()) {
 			if (get_compat_pages_array(chunk_pages, pages,

_

author
#24 lore raw

Re: [PATCH 07/16] mm/migration: use helper macro min_t in do_pages_stat

Quoted 18 lines · Andrew Morton
Quoted 16 lines · On Fri, 4 Mar 2022 17:34:00 +0800 Miaohe Lin <[email protected]> wrote:

We could use helper macro min_t to help set the chunk_nr to simplify the code.

...

mm/migrate.c +1 -3
@@ -1858,9 +1858,7 @@ static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
 	while (nr_pages) {
 		unsigned long chunk_nr;
 
-		chunk_nr = nr_pages;
-		if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
-			chunk_nr = DO_PAGES_STAT_CHUNK_NR;
+		chunk_nr = min_t(unsigned long, nr_pages, DO_PAGES_STAT_CHUNK_NR);
 
Quoted 5 lines

mm/migrate.c static int do_pages_stat(struct mm_struc · line 1855

Quoted 2 lines
mm/migrate.c +0 -0
 		if (in_compat_syscall()) {
 			if (get_compat_pages_array(chunk_pages, pages,

Getting the types correct is better than using min_t().

Looks good. Many thanks for your suggestion. Will do it in v2.

Thanks.

Quoted 23 lines
mm/migrate.c renamed from mm/migrate.c~mm-migration-use-helper-macro-min_t-in-do_pages_stat-fix +2 -4
@@ -1844,14 +1844,12 @@ static int do_pages_stat(struct mm_struc
 			 const void __user * __user *pages,
 			 int __user *status)
 {
-#define DO_PAGES_STAT_CHUNK_NR 16
+#define DO_PAGES_STAT_CHUNK_NR 16UL
 	const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
 	int chunk_status[DO_PAGES_STAT_CHUNK_NR];
 
 	while (nr_pages) {
-		unsigned long chunk_nr;
-
-		chunk_nr = min_t(unsigned long, nr_pages, DO_PAGES_STAT_CHUNK_NR);
+		unsigned long chunk_nr = min(nr_pages, DO_PAGES_STAT_CHUNK_NR);
 
 		if (in_compat_syscall()) {
 			if (get_compat_pages_array(chunk_pages, pages,

_

.

author
#25 lore raw

PATCH 8/16 mm/migration: avoid unneeded nodemask_t initialization

In reply to Miaohe Lin

Avoid unneeded next_pass and this_pass initialization as they're always set before using to save possible cpu cycles when there are plenty of nodes in the system.

---
 mm/migrate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/migrate.c +2 -2
index c84eec19072a..abb0c6715e1f 100644
@@ -2378,8 +2378,8 @@ static int establish_migrate_target(int node, nodemask_t *used,
  */
 static void __set_migration_target_nodes(void)
 {
-	nodemask_t next_pass	= NODE_MASK_NONE;
-	nodemask_t this_pass	= NODE_MASK_NONE;
+	nodemask_t next_pass;
+	nodemask_t this_pass;
 	nodemask_t used_targets = NODE_MASK_NONE;
 	int node, best_distance;
 
Signature · 1 line
2.23.0
Reviewed-by on v1 8/16
#26 lore raw

Re: [PATCH 08/16] mm/migration: avoid unneeded nodemask_t initialization

Quoted 6 lines · Miaohe Lin

Avoid unneeded next_pass and this_pass initialization as they're always set before using to save possible cpu cycles when there are plenty of nodes in the system.

Thanks.

Reviewed-by on v1 8/16
#27 lore raw

Re: [PATCH 08/16] mm/migration: avoid unneeded nodemask_t initialization

In reply to Miaohe Lin
Quoted 14 lines · Miaohe Lin

Avoid unneeded next_pass and this_pass initialization as they're always set before using to save possible cpu cycles when there are plenty of nodes in the system.

---
  mm/migrate.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
mm/migrate.c +0 -0
index c84eec19072a..abb0c6715e1f 100644

mm/migrate.c static int establish_migrate_target(int node, nodemask_t *used, · line 2384

Quoted 9 lines
mm/migrate.c +2 -2
@@ -2378,8 +2378,8 @@ static int establish_migrate_target(int node, nodemask_t *used,
   */
  static void __set_migration_target_nodes(void)
  {
-	nodemask_t next_pass	= NODE_MASK_NONE;
-	nodemask_t this_pass	= NODE_MASK_NONE;
+	nodemask_t next_pass;
+	nodemask_t this_pass;
  	nodemask_t used_targets = NODE_MASK_NONE;
  	int node, best_distance;

LGTM.

author
#28 lore raw

PATCH 9/16 mm/migration: remove some duplicated codes in migrate_pages

In reply to Miaohe Lin

Remove the duplicated codes in migrate_pages to simplify the code. Minor readability improvement. No functional change intended.

---
 mm/migrate.c | 36 ++++++++++++------------------------
 1 file changed, 12 insertions(+), 24 deletions(-)
mm/migrate.c +12 -24
index abb0c6715e1f..cb970d201147 100644
@@ -1414,14 +1414,11 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
 						nr_thp_split++;
 						goto retry;
 					}
-
-					nr_failed_pages += nr_subpages;
-					break;
-				}
-
 				/* Hugetlb migration is unsupported */
-				if (!no_subpage_counting)
+				} else if (!no_subpage_counting) {
 					nr_failed++;
+				}
+
 				nr_failed_pages += nr_subpages;
 				break;
 			case -ENOMEM:
@@ -1436,28 +1433,22 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
 						nr_thp_split++;
 						goto retry;
 					}
-
-					nr_failed_pages += nr_subpages;
-					goto out;
+				} else if (!no_subpage_counting) {
+					nr_failed++;
 				}
 
-				if (!no_subpage_counting)
-					nr_failed++;
 				nr_failed_pages += nr_subpages;
 				goto out;
 			case -EAGAIN:
-				if (is_thp) {
+				if (is_thp)
 					thp_retry++;
-					break;
-				}
-				retry++;
+				else
+					retry++;
 				break;
 			case MIGRATEPAGE_SUCCESS:
 				nr_succeeded += nr_subpages;
-				if (is_thp) {
+				if (is_thp)
 					nr_thp_succeeded++;
-					break;
-				}
 				break;
 			default:
 				/*
@@ -1466,14 +1457,11 @@ 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) {
+				if (is_thp)
 					nr_thp_failed++;
-					nr_failed_pages += nr_subpages;
-					break;
-				}
-
-				if (!no_subpage_counting)
+				else if (!no_subpage_counting)
 					nr_failed++;
+
 				nr_failed_pages += nr_subpages;
 				break;
 			}
Signature · 1 line
2.23.0
reviewer
#29 lore raw

Re: [PATCH 09/16] mm/migration: remove some duplicated codes in migrate_pages

Quoted 8 lines · Miaohe Lin

Remove the duplicated codes in migrate_pages to simplify the code. Minor readability improvement. No functional change intended.

---
 mm/migrate.c | 36 ++++++++++++------------------------
 1 file changed, 12 insertions(+), 24 deletions(-)

LGTM. Thanks. Reviewed-by: Zi Yan <[email protected]>

--
Best Regards,
Yan, Zi

author
#31 lore raw

PATCH 10/16 mm/migration: remove PG_writeback handle in folio_migrate_flags

In reply to Miaohe Lin

When newfolio reaches here, it's guaranteed that PG_writeback is not set because caller ensures writeback must have been completed. Remove this unneeded check and cleanup the relevant comment.

---
 mm/migrate.c | 12 ------------
 1 file changed, 12 deletions(-)
mm/migrate.c +0 -12
index cb970d201147..1de5289a4af0 100644
@@ -549,18 +549,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 	if (!folio_test_hugetlb(folio))
 		folio->private = NULL;
 
-	/*
-	 * If any waiters have accumulated on the new page then
-	 * wake them up.
-	 */
-	if (folio_test_writeback(newfolio))
-		folio_end_writeback(newfolio);
-
-	/*
-	 * PG_readahead shares the same bit with PG_reclaim.  The above
-	 * end_page_writeback() may clear PG_readahead mistakenly, so set the
-	 * bit after that.
-	 */
 	if (folio_test_readahead(folio))
 		folio_set_readahead(newfolio);
 
Signature · 1 line
2.23.0
maintainer
#32 lore raw

Re: [PATCH 10/16] mm/migration: remove PG_writeback handle in folio_migrate_flags

Quoted 3 lines · On Fri, 4 Mar 2022 17:34:03 +0800 Miaohe Lin <[email protected]> wrote:

When newfolio reaches here, it's guaranteed that PG_writeback is not set because caller ensures writeback must have been completed. Remove this unneeded check and cleanup the relevant comment.

What guarantees that writeback cannot start after the caller has checked?

I see no such check in iomap_migrate_page()?

Quoted 11 lines
mm/migrate.c +0 -5
@@ -549,18 +549,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 	if (!folio_test_hugetlb(folio))
 		folio->private = NULL;
 
-	/*
-	 * If any waiters have accumulated on the new page then
-	 * wake them up.
-	 */
-	if (folio_test_writeback(newfolio))

mm/migrate.c void folio_migrate_flags(struct folio *newfolio, struct folio *folio) · line 554

Quoted 10 lines
mm/migrate.c +0 -7
-		folio_end_writeback(newfolio);
-
-	/*
-	 * PG_readahead shares the same bit with PG_reclaim.  The above
-	 * end_page_writeback() may clear PG_readahead mistakenly, so set the
-	 * bit after that.
-	 */
 	if (folio_test_readahead(folio))
 		folio_set_readahead(newfolio);
 

folio_migrate_flags() and folio_migrate_copy() are global, export-to-modules functions but have no interface documentation. That was bad of us.

I wonder why those two functions are exported to modules anyway.

author
#33 lore raw

Re: [PATCH 10/16] mm/migration: remove PG_writeback handle in folio_migrate_flags

Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Fri, 4 Mar 2022 17:34:03 +0800 Miaohe Lin <[email protected]> wrote:

When newfolio reaches here, it's guaranteed that PG_writeback is not set because caller ensures writeback must have been completed. Remove this unneeded check and cleanup the relevant comment.

What guarantees that writeback cannot start after the caller has checked?

Drivers should set writeback flag under page lock. But I'am not sure this is always obeyed.

Quoted 2 lines

I see no such check in iomap_migrate_page()?

iomap_migrate_page acts as a migratepage callback. The check is actually done in __unmap_and_move.

Quoted 7 lines
Quoted 7 lines
mm/migrate.c +0 -0
@@ -549,18 +549,6 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 	if (!folio_test_hugetlb(folio))
 		folio->private = NULL;
 
Quoted 6 lines

mm/migrate.c void folio_migrate_flags(struct folio *newfolio, struct folio *folio) · line 557

Quoted 6 lines
mm/migrate.c +0 -6
-	/*
-	 * If any waiters have accumulated on the new page then
-	 * wake them up.
-	 */
-	if (folio_test_writeback(newfolio))
-		folio_end_writeback(newfolio);

IIUC, this should also be broken too. If a page can be flagged writeback under page lock, we can't simply do folio_end_writeback here as page might still be under the use of drivers. More works should be done to handle the case correctly.

Quoted 14 lines

mm/migrate.c void folio_migrate_flags(struct folio *newfolio, struct folio *folio) · line 554

Quoted 9 lines
mm/migrate.c +0 -6
-
-	/*
-	 * PG_readahead shares the same bit with PG_reclaim.  The above
-	 * end_page_writeback() may clear PG_readahead mistakenly, so set the
-	 * bit after that.
-	 */
 	if (folio_test_readahead(folio))
 		folio_set_readahead(newfolio);
 

folio_migrate_flags() and folio_migrate_copy() are global, export-to-modules functions but have no interface documentation. That was bad of us.

Quoted 1 line

I wonder why those two functions are exported to modules anyway.

That's really a pity.

Quoted 2 lines

.

Many thanks for your comment and reply!

author
#34 lore raw

PATCH 11/16 mm/migration: remove unneeded lock page and PageMovable check

In reply to Miaohe Lin

When non-lru movable page was freed from under us, __ClearPageMovable must have been done. Even if it's not done, __ClearPageIsolated here won't hurt as page will be freed soon. So we can thus remove unneeded lock page and PageMovable check here.

---
 mm/migrate.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
mm/migrate.c +2 -6
index 1de5289a4af0..e0db06927f02 100644
@@ -1074,12 +1074,8 @@ static int unmap_and_move(new_page_t get_new_page,
 		/* page was freed from under us. So we are done. */
 		ClearPageActive(page);
 		ClearPageUnevictable(page);
-		if (unlikely(__PageMovable(page))) {
-			lock_page(page);
-			if (!PageMovable(page))
-				__ClearPageIsolated(page);
-			unlock_page(page);
-		}
+		if (unlikely(__PageMovable(page)))
+			__ClearPageIsolated(page);
 		goto out;
 	}
 
Signature · 1 line
2.23.0
author
#35 lore raw

PATCH 12/16 mm/migration: fix potential page refcounts leak in migrate_pages

In reply to Miaohe Lin

In -ENOMEM case, there might be some subpages of fail-to-migrate THPs left in thp_split_pages list. We should move them back to migration list so that they could be put back to the right list by the caller otherwise the page refcnt will be leaked here. Also adjust nr_failed and nr_thp_failed accordingly to make vm events account more accurate.

Fixes b5bade978e9b ("mm: migrate: fix the return value of migrate_pages()")
---
 mm/migrate.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/migrate.c +9 -0
index e0db06927f02..6c2dfed2ddb8 100644
@@ -1422,6 +1422,15 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
 				}
 
 				nr_failed_pages += nr_subpages;
+				/*
+				 * There might be some subpages of fail-to-migrate THPs
+				 * left in thp_split_pages list. Move them back to migration
+				 * list so that they could be put back to the right list by
+				 * the caller otherwise the page refcnt will be leaked.
+				 */
+				list_splice_init(&thp_split_pages, from);
+				nr_failed += retry;
+				nr_thp_failed += thp_retry;
 				goto out;
 			case -EAGAIN:
 				if (is_thp)
Signature · 1 line
2.23.0
reviewer
#36 lore raw

Re: [PATCH 12/16] mm/migration: fix potential page refcounts leak in migrate_pages

Quoted 34 lines · Miaohe Lin

In -ENOMEM case, there might be some subpages of fail-to-migrate THPs left in thp_split_pages list. We should move them back to migration list so that they could be put back to the right list by the caller otherwise the page refcnt will be leaked here. Also adjust nr_failed and nr_thp_failed accordingly to make vm events account more accurate.

Fixes b5bade978e9b ("mm: migrate: fix the return value of migrate_pages()")
---
 mm/migrate.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/migrate.c +9 -0
index e0db06927f02..6c2dfed2ddb8 100644
@@ -1422,6 +1422,15 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
 				}
 
 				nr_failed_pages += nr_subpages;
+				/*
+				 * There might be some subpages of fail-to-migrate THPs
+				 * left in thp_split_pages list. Move them back to migration
+				 * list so that they could be put back to the right list by
+				 * the caller otherwise the page refcnt will be leaked.
+				 */
+				list_splice_init(&thp_split_pages, from);
+				nr_failed += retry;
+				nr_thp_failed += thp_retry;
 				goto out;
 			case -EAGAIN:
 				if (is_thp)
Signature · 1 line
2.23.0

LGTM. Thanks. Reviewed-by: Zi Yan <[email protected]>

--
Best Regards,
Yan, Zi

#37 lore raw

Re: [PATCH 12/16] mm/migration: fix potential page refcounts leak in migrate_pages

In reply to Miaohe Lin

Hi Miaohe,

Quoted 19 lines · Miaohe Lin

In -ENOMEM case, there might be some subpages of fail-to-migrate THPs left in thp_split_pages list. We should move them back to migration list so that they could be put back to the right list by the caller otherwise the page refcnt will be leaked here. Also adjust nr_failed and nr_thp_failed accordingly to make vm events account more accurate.

Fixes b5bade978e9b ("mm: migrate: fix the return value of migrate_pages()")
---
  mm/migrate.c | 9 +++++++++
  1 file changed, 9 insertions(+)
mm/migrate.c +0 -0
index e0db06927f02..6c2dfed2ddb8 100644
@@ -1422,6 +1422,15 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
  				}
 

mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1433

Quoted 10 lines
mm/migrate.c +9 -0
  				nr_failed_pages += nr_subpages;
+				/*
+				 * There might be some subpages of fail-to-migrate THPs
+				 * left in thp_split_pages list. Move them back to migration
+				 * list so that they could be put back to the right list by
+				 * the caller otherwise the page refcnt will be leaked.
+				 */
+				list_splice_init(&thp_split_pages, from);
+				nr_failed += retry;
+				nr_thp_failed += thp_retry;

Yes, I think we missed this case before, and your patch looks right. But we should also update the 'rc' to return the correct number of pages that were not migrated, right?

#38 lore raw

Re: [PATCH 12/16] mm/migration: fix potential page refcounts leak in migrate_pages

Quoted 26 lines · Baolin Wang

Hi Miaohe,

Quoted 23 lines · Miaohe Lin

In -ENOMEM case, there might be some subpages of fail-to-migrate THPs left in thp_split_pages list. We should move them back to migration list so that they could be put back to the right list by the caller otherwise the page refcnt will be leaked here. Also adjust nr_failed and nr_thp_failed accordingly to make vm events account more accurate.

Fixes b5bade978e9b ("mm: migrate: fix the return value of

migrate_pages()")

---
  mm/migrate.c | 9 +++++++++
  1 file changed, 9 insertions(+)
mm/migrate.c +5 -0
index e0db06927f02..6c2dfed2ddb8 100644
@@ -1422,6 +1422,15 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
  				}
    				nr_failed_pages += nr_subpages;
+				/*
+				 * There might be some subpages of fail-to-migrate THPs
+				 * left in thp_split_pages list. Move them back to migration
+				 * list so that they could be put back to the right list by
+				 * the caller otherwise the page refcnt will be leaked.
Quoted 8 lines

mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1433

Quoted 4 lines
mm/migrate.c +4 -0
+				 */
+				list_splice_init(&thp_split_pages, from);
+				nr_failed += retry;
+				nr_thp_failed += thp_retry;

Yes, I think we missed this case before, and your patch looks right. But we should also update the 'rc' to return the correct number of pages that were not migrated, right?

Per my understanding, -ENOMEM should be returned to indicate an fatal error.

Best Regards,
Huang, Ying

Reviewed-by
#39 lore raw

Re: [PATCH 12/16] mm/migration: fix potential page refcounts leak in migrate_pages

Quoted 37 lines · Ying
Quoted 26 lines · Baolin Wang

Hi Miaohe,

Quoted 23 lines · Miaohe Lin

In -ENOMEM case, there might be some subpages of fail-to-migrate THPs left in thp_split_pages list. We should move them back to migration list so that they could be put back to the right list by the caller otherwise the page refcnt will be leaked here. Also adjust nr_failed and nr_thp_failed accordingly to make vm events account more accurate.

Fixes b5bade978e9b ("mm: migrate: fix the return value of

migrate_pages()")

---
   mm/migrate.c | 9 +++++++++
   1 file changed, 9 insertions(+)
mm/migrate.c +5 -0
index e0db06927f02..6c2dfed2ddb8 100644
@@ -1422,6 +1422,15 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
   				}
     				nr_failed_pages += nr_subpages;
+				/*
+				 * There might be some subpages of fail-to-migrate THPs
+				 * left in thp_split_pages list. Move them back to migration
+				 * list so that they could be put back to the right list by
+				 * the caller otherwise the page refcnt will be leaked.
Quoted 8 lines

mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1433

Quoted 4 lines
mm/migrate.c +4 -0
+				 */
+				list_splice_init(&thp_split_pages, from);
+				nr_failed += retry;
+				nr_thp_failed += thp_retry;

Yes, I think we missed this case before, and your patch looks right. But we should also update the 'rc' to return the correct number of pages that were not migrated, right?

Quoted 3 lines

Per my understanding, -ENOMEM should be returned to indicate an fatal error.

Ah, right. Sorry for noise.

author
#40 lore raw

Re: [PATCH 12/16] mm/migration: fix potential page refcounts leak in migrate_pages

Quoted 40 lines · Baolin Wang
Quoted 37 lines · Ying
Quoted 34 lines · Baolin Wang

Hi Miaohe,

Quoted 17 lines · Miaohe Lin

In -ENOMEM case, there might be some subpages of fail-to-migrate THPs left in thp_split_pages list. We should move them back to migration list so that they could be put back to the right list by the caller otherwise the page refcnt will be leaked here. Also adjust nr_failed and nr_thp_failed accordingly to make vm events account more accurate.

Fixes b5bade978e9b ("mm: migrate: fix the return value of

migrate_pages()")

---
   mm/migrate.c | 9 +++++++++

   1 file changed, 9 insertions(+)
mm/migrate.c +0 -0
index e0db06927f02..6c2dfed2ddb8 100644
@@ -1422,6 +1422,15 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
                    }

mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1432

Quoted 10 lines
mm/migrate.c +9 -0
                      nr_failed_pages += nr_subpages;
+                /*
+                 * There might be some subpages of fail-to-migrate THPs
+                 * left in thp_split_pages list. Move them back to migration
+                 * list so that they could be put back to the right list by
+                 * the caller otherwise the page refcnt will be leaked.
+                 */
+                list_splice_init(&thp_split_pages, from);
+                nr_failed += retry;
+                nr_thp_failed += thp_retry;

Yes, I think we missed this case before, and your patch looks right. But we should also update the 'rc' to return the correct number of pages that were not migrated, right?

Quoted 6 lines
Quoted 3 lines

Per my understanding, -ENOMEM should be returned to indicate an fatal error.

Ah, right. Sorry for noise.

Oh, I missed this email. So we should return -ENOMEM in this case. Many thanks for both of you.

Quoted 1 line

.

author
#41 lore raw

Re: [PATCH 12/16] mm/migration: fix potential page refcounts leak in migrate_pages

In reply to Baolin Wang
Quoted 25 lines · Baolin Wang

Hi Miaohe,

Quoted 22 lines · Miaohe Lin

In -ENOMEM case, there might be some subpages of fail-to-migrate THPs left in thp_split_pages list. We should move them back to migration list so that they could be put back to the right list by the caller otherwise the page refcnt will be leaked here. Also adjust nr_failed and nr_thp_failed accordingly to make vm events account more accurate.

Fixes b5bade978e9b ("mm: migrate: fix the return value of migrate_pages()")

---
  mm/migrate.c | 9 +++++++++

  1 file changed, 9 insertions(+)
mm/migrate.c +3 -0
index e0db06927f02..6c2dfed2ddb8 100644
@@ -1422,6 +1422,15 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
                   }
                     nr_failed_pages += nr_subpages;
+                /*
+                 * There might be some subpages of fail-to-migrate THPs
+                 * left in thp_split_pages list. Move them back to migration
Quoted 8 lines

mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1433

Quoted 6 lines
mm/migrate.c +6 -0
+                 * list so that they could be put back to the right list by
+                 * the caller otherwise the page refcnt will be leaked.
+                 */
+                list_splice_init(&thp_split_pages, from);
+                nr_failed += retry;
+                nr_thp_failed += thp_retry;

Yes, I think we missed this case before, and your patch looks right. But we should also update the 'rc' to return the correct number of pages that were not migrated, right?

I'am not sure. -ENOMEM case always returns -ENOMEM since commit 95a402c3847c ("[PATCH] page migration:
use allocator function for migrate_pages()"). So I did not change rc. But I think you're right. We should return the correct number of pages that were not migrated in this case.

Thanks.

Quoted 1 line

.

Reviewed-by on v1 12/16
#42 lore raw

Re: [PATCH 12/16] mm/migration: fix potential page refcounts leak in migrate_pages

In reply to Miaohe Lin
Quoted 18 lines · Miaohe Lin

In -ENOMEM case, there might be some subpages of fail-to-migrate THPs left in thp_split_pages list. We should move them back to migration list so that they could be put back to the right list by the caller otherwise the page refcnt will be leaked here. Also adjust nr_failed and nr_thp_failed accordingly to make vm events account more accurate.

Fixes b5bade978e9b ("mm: migrate: fix the return value of migrate_pages()")
---
 mm/migrate.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/migrate.c +0 -0
index e0db06927f02..6c2dfed2ddb8 100644
@@ -1422,6 +1422,15 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
 				}

mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1432

Quoted 10 lines
mm/migrate.c +8 -0
 
 				nr_failed_pages += nr_subpages;
+				/*
+				 * There might be some subpages of fail-to-migrate THPs
+				 * left in thp_split_pages list. Move them back to migration
+				 * list so that they could be put back to the right list by
+				 * the caller otherwise the page refcnt will be leaked.
+				 */
+				list_splice_init(&thp_split_pages, from);
+				nr_failed += retry;

It appears that we don't need to change nr_failed, because we don't use it for this situation. Otherwise looks good to me.

Best Regards,
Huang, Ying

Quoted 4 lines
mm/migrate.c +1 -0
+				nr_thp_failed += thp_retry;
 				goto out;
 			case -EAGAIN:
 				if (is_thp)
author
#43 lore raw

Re: [PATCH 12/16] mm/migration: fix potential page refcounts leak in migrate_pages

Quoted 21 lines · Ying
Quoted 19 lines · Miaohe Lin

In -ENOMEM case, there might be some subpages of fail-to-migrate THPs left in thp_split_pages list. We should move them back to migration list so that they could be put back to the right list by the caller otherwise the page refcnt will be leaked here. Also adjust nr_failed and nr_thp_failed accordingly to make vm events account more accurate.

Fixes b5bade978e9b ("mm: migrate: fix the return value of migrate_pages()")
---
 mm/migrate.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/migrate.c +0 -0
index e0db06927f02..6c2dfed2ddb8 100644
@@ -1422,6 +1422,15 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,
 				}
 
Quoted 13 lines

mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1432

Quoted 9 lines
mm/migrate.c +8 -0
 				nr_failed_pages += nr_subpages;
+				/*
+				 * There might be some subpages of fail-to-migrate THPs
+				 * left in thp_split_pages list. Move them back to migration
+				 * list so that they could be put back to the right list by
+				 * the caller otherwise the page refcnt will be leaked.
+				 */
+				list_splice_init(&thp_split_pages, from);
+				nr_failed += retry;

It appears that we don't need to change nr_failed, because we don't use it for this situation. Otherwise looks good to me.

You're right. nr_failed is not used for this case.

Quoted 1 line

Many thanks for your review.

Quoted 10 lines

Best Regards,
Huang, Ying

mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1436

Quoted 4 lines
mm/migrate.c +1 -0
+				nr_thp_failed += thp_retry;
 				goto out;
 			case -EAGAIN:
 				if (is_thp)

.

author
#44 lore raw

PATCH 13/16 mm/migration: return errno when isolate_huge_page failed

In reply to Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

---
 mm/migrate.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +2 -4
index 6c2dfed2ddb8..279940c0c064 100644
@@ -1618,10 +1618,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 		goto out_putpage;
 
 	if (PageHuge(page)) {
-		if (PageHead(page)) {
-			isolate_huge_page(page, pagelist);
-			err = 1;
-		}
+		if (PageHead(page))
+			err = isolate_huge_page(page, pagelist) ? 1 : -EBUSY;
 	} else {
 		struct page *head;
 
Signature · 1 line
2.23.0
Reviewed-by on v1 13/16
#45 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

Quoted 5 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

IIUC, this should be a bug fix since the status reported to the user is wrong. A Fixes tag may be needed. But anyway:

Thanks.

author
#46 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

Quoted 10 lines · Muchun Song
Quoted 5 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

IIUC, this should be a bug fix since the status reported to the user is wrong. A Fixes tag may be needed. But anyway:

Agree. Thanks for pointing this out and many thanks for your review. :)

Quoted 6 lines

Thanks.

.

#47 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

In reply to Miaohe Lin

Hi Miaohe,

Quoted 13 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

---
  mm/migrate.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +0 -0
index 6c2dfed2ddb8..279940c0c064 100644

mm/migrate.c static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, · line 1622

Quoted 9 lines
mm/migrate.c +2 -4
@@ -1618,10 +1618,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
  		goto out_putpage;
 
  	if (PageHuge(page)) {
-		if (PageHead(page)) {
-			isolate_huge_page(page, pagelist);
-			err = 1;
-		}
+		if (PageHead(page))
+			err = isolate_huge_page(page, pagelist) ? 1 : -EBUSY;

Could you elaborate on which case the huge page isolation can be failed in this case? Or you met a real problem? Cause now we've got this head huge page refcnt, I can not see why we'll fail to isolate this huge page.

author
#48 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

Quoted 11 lines · Baolin Wang

Hi Miaohe,

Quoted 8 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

---
  mm/migrate.c | 6 ++----

  1 file changed, 2 insertions(+), 4 deletions(-)
Quoted 15 lines

mm/migrate.c static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, · line 1621

Quoted 13 lines
mm/migrate.c +2 -4
index 6c2dfed2ddb8..279940c0c064 100644
@@ -1618,10 +1618,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
           goto out_putpage;
         if (PageHuge(page)) {
-        if (PageHead(page)) {
-            isolate_huge_page(page, pagelist);
-            err = 1;
-        }
+        if (PageHead(page))
+            err = isolate_huge_page(page, pagelist) ? 1 : -EBUSY;

Could you elaborate on which case the huge page isolation can be failed in this case? Or you met a real problem? Cause now we've got this head huge page refcnt, I can not see why we'll fail to isolate this huge page.

IIUC, this could happen when hugepage is under migration which cleared HPageMigratable. Page refcnt cannot prevent isolate_huge_page from happening. Or am I miss something?

Many thanks.

Quoted 1 line

.

Reviewed-by
#49 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

Quoted 28 lines · Miaohe Lin
Quoted 11 lines · Baolin Wang

Hi Miaohe,

Quoted 8 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

---
   mm/migrate.c | 6 ++----
   1 file changed, 2 insertions(+), 4 deletions(-)
Quoted 15 lines

mm/migrate.c static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, · line 1621

Quoted 13 lines
mm/migrate.c +2 -4
index 6c2dfed2ddb8..279940c0c064 100644
@@ -1618,10 +1618,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
           goto out_putpage;
         if (PageHuge(page)) {
-        if (PageHead(page)) {
-            isolate_huge_page(page, pagelist);
-            err = 1;
-        }
+        if (PageHead(page))
+            err = isolate_huge_page(page, pagelist) ? 1 : -EBUSY;

Could you elaborate on which case the huge page isolation can be failed in this case? Or you met a real problem? Cause now we've got this head huge page refcnt, I can not see why we'll fail to isolate this huge page.

Quoted 2 lines

IIUC, this could happen when hugepage is under migration which cleared HPageMigratable. Page refcnt cannot prevent isolate_huge_page from happening. Or am I miss something?

Yes, that's possible. Thanks for your explanation. It will be better if you can copy the possible scenario description to the commit log to help other understand the issue.

author
#50 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

Quoted 31 lines · Baolin Wang
Quoted 28 lines · Miaohe Lin
Quoted 26 lines · Baolin Wang

Hi Miaohe,

Quoted 13 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

---
   mm/migrate.c | 6 ++----

   1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +0 -0
index 6c2dfed2ddb8..279940c0c064 100644

mm/migrate.c static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, · line 1621

Quoted 8 lines
mm/migrate.c +2 -4
@@ -1618,10 +1618,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
            goto out_putpage;
          if (PageHuge(page)) {
-        if (PageHead(page)) {
-            isolate_huge_page(page, pagelist);
-            err = 1;
-        }
+        if (PageHead(page))
+            err = isolate_huge_page(page, pagelist) ? 1 : -EBUSY;

Could you elaborate on which case the huge page isolation can be failed in this case? Or you met a real problem? Cause now we've got this head huge page refcnt, I can not see why we'll fail to isolate this huge page.

Quoted 5 lines
Quoted 2 lines

IIUC, this could happen when hugepage is under migration which cleared HPageMigratable. Page refcnt cannot prevent isolate_huge_page from happening. Or am I miss something?

Yes, that's possible. Thanks for your explanation. It will be better if you can copy the possible scenario description to the commit log to help other understand the issue.

Sounds reasonable. Will do. Many thanks for review.

Quoted 2 lines

.

#51 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

In reply to Miaohe Lin
Quoted 13 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

---
 mm/migrate.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +0 -0
index 6c2dfed2ddb8..279940c0c064 100644

mm/migrate.c static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, · line 1622

Quoted 9 lines
mm/migrate.c +2 -4
@@ -1618,10 +1618,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 		goto out_putpage;
 
 	if (PageHuge(page)) {
-		if (PageHead(page)) {
-			isolate_huge_page(page, pagelist);
-			err = 1;
-		}
+		if (PageHead(page))
+			err = isolate_huge_page(page, pagelist) ? 1 : -EBUSY;

IMHO, it's better to determine the proper errno inside isolate_huge_page() instead of in the caller. If you think it's necessary to get errno here. How about change isolate_huge_page() instead?

Best Regards,
Huang, Ying

Quoted 2 lines
mm/migrate.c +0 -0
 	} else {
 		struct page *head;
author
#52 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

Quoted 17 lines · Ying
Quoted 15 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

---
 mm/migrate.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +0 -0
index 6c2dfed2ddb8..279940c0c064 100644
@@ -1618,10 +1618,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 		goto out_putpage;
 
Quoted 12 lines

mm/migrate.c static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, · line 1622

Quoted 7 lines
mm/migrate.c +2 -4
 	if (PageHuge(page)) {
-		if (PageHead(page)) {
-			isolate_huge_page(page, pagelist);
-			err = 1;
-		}
+		if (PageHead(page))
+			err = isolate_huge_page(page, pagelist) ? 1 : -EBUSY;

IMHO, it's better to determine the proper errno inside isolate_huge_page() instead of in the caller. If you think it's necessary to get errno here. How about change isolate_huge_page() instead?

IMO, -EBUSY should be enough for the user (as they could not do much) and this errno keeps consistent with the non-hugetlb page case. What do you think?

Thanks.

Quoted 8 lines

Best Regards,
Huang, Ying

mm/migrate.c static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, · line 1624

Quoted 2 lines
mm/migrate.c +0 -0
 	} else {
 		struct page *head;

.

#53 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

Quoted 31 lines · Miaohe Lin
Quoted 17 lines · Ying
Quoted 15 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

---
 mm/migrate.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +0 -0
index 6c2dfed2ddb8..279940c0c064 100644
@@ -1618,10 +1618,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 		goto out_putpage;
 
Quoted 12 lines

mm/migrate.c static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, · line 1622

Quoted 7 lines
mm/migrate.c +2 -4
 	if (PageHuge(page)) {
-		if (PageHead(page)) {
-			isolate_huge_page(page, pagelist);
-			err = 1;
-		}
+		if (PageHead(page))
+			err = isolate_huge_page(page, pagelist) ? 1 : -EBUSY;

IMHO, it's better to determine the proper errno inside isolate_huge_page() instead of in the caller. If you think it's necessary to get errno here. How about change isolate_huge_page() instead?

Quoted 2 lines

IMO, -EBUSY should be enough for the user (as they could not do much) and this errno keeps consistent with the non-hugetlb page case. What do you think?

I found the prototype of isolate_lru_page() is as follows,

int isolate_lru_page(struct page *page)

And it will return errno directly. I think we should follow same convention here?

Best Regards,
Huang, Ying

author
#54 lore raw

Re: [PATCH 13/16] mm/migration: return errno when isolate_huge_page failed

Quoted 36 lines · Ying
Quoted 31 lines · Miaohe Lin
Quoted 17 lines · Ying
Quoted 15 lines · Miaohe Lin

We should return errno (-EBUSY here) when failed to isolate the huge page rather than always return 1 which could confuse the user.

---
 mm/migrate.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/migrate.c +0 -0
index 6c2dfed2ddb8..279940c0c064 100644
@@ -1618,10 +1618,8 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
 		goto out_putpage;
 
Quoted 12 lines

mm/migrate.c static int add_page_for_migration(struct mm_struct *mm, unsigned long addr, · line 1622

Quoted 7 lines
mm/migrate.c +2 -4
 	if (PageHuge(page)) {
-		if (PageHead(page)) {
-			isolate_huge_page(page, pagelist);
-			err = 1;
-		}
+		if (PageHead(page))
+			err = isolate_huge_page(page, pagelist) ? 1 : -EBUSY;

IMHO, it's better to determine the proper errno inside isolate_huge_page() instead of in the caller. If you think it's necessary to get errno here. How about change isolate_huge_page() instead?

Quoted 2 lines

IMO, -EBUSY should be enough for the user (as they could not do much) and this errno keeps consistent with the non-hugetlb page case. What do you think?

Quoted 7 lines

I found the prototype of isolate_lru_page() is as follows,

int isolate_lru_page(struct page *page)

And it will return errno directly. I think we should follow same convention here?

I see. Sounds reasonable to me. Will try to do it. Thanks.

Quoted 4 lines

Best Regards,
Huang, Ying
.

author
#55 lore raw

PATCH 14/16 mm/migration: fix potential invalid node access for reclaim-based migration

In reply to Miaohe Lin

If we failed to setup hotplug state callbacks for mm/demotion:online in some corner cases, node_demotion will be left uninitialized. Invalid node might be returned from the next_demotion_node() when doing reclaim-based migration. Use kcalloc to allocate node_demotion to fix the issue.

Fixes ac16ec835314 ("mm: migrate: support multiple target nodes demotion")
---
 mm/migrate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/migrate.c +3 -3
index 279940c0c064..7b1c0b988234 100644
@@ -2516,9 +2516,9 @@ static int __init migrate_on_reclaim_init(void)
 {
 	int ret;
 
-	node_demotion = kmalloc_array(nr_node_ids,
-				      sizeof(struct demotion_nodes),
-				      GFP_KERNEL);
+	node_demotion = kcalloc(nr_node_ids,
+				sizeof(struct demotion_nodes),
+				GFP_KERNEL);
 	WARN_ON(!node_demotion);
 
 	ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
Signature · 1 line
2.23.0
#56 lore raw

Re: [PATCH 14/16] mm/migration: fix potential invalid node access for reclaim-based migration

Quoted 16 lines · Miaohe Lin

If we failed to setup hotplug state callbacks for mm/demotion:online in some corner cases, node_demotion will be left uninitialized. Invalid node might be returned from the next_demotion_node() when doing reclaim-based migration. Use kcalloc to allocate node_demotion to fix the issue.

Fixes ac16ec835314 ("mm: migrate: support multiple target nodes demotion")
---
  mm/migrate.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
mm/migrate.c +0 -0
index 279940c0c064..7b1c0b988234 100644

mm/migrate.c static int __init migrate_on_reclaim_init(void) · line 2521

Quoted 9 lines
mm/migrate.c +3 -3
@@ -2516,9 +2516,9 @@ static int __init migrate_on_reclaim_init(void)
  {
  	int ret;
 
-	node_demotion = kmalloc_array(nr_node_ids,
-				      sizeof(struct demotion_nodes),
-				      GFP_KERNEL);
+	node_demotion = kcalloc(nr_node_ids,
+				sizeof(struct demotion_nodes),
+				GFP_KERNEL);

Nit: not sure if this is worthy of this rare corner case, but I think the target demotion nodes' default value should be NUMA_NO_NODE instead of 0.

#57 lore raw

Re: [PATCH 14/16] mm/migration: fix potential invalid node access for reclaim-based migration

Quoted 20 lines · Baolin Wang
Quoted 19 lines · Miaohe Lin

If we failed to setup hotplug state callbacks for mm/demotion:online in some corner cases, node_demotion will be left uninitialized. Invalid node might be returned from the next_demotion_node() when doing reclaim-based migration. Use kcalloc to allocate node_demotion to fix the issue.

Fixes ac16ec835314 ("mm: migrate: support multiple target nodes

demotion")

---
  mm/migrate.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
mm/migrate.c +0 -1
index 279940c0c064..7b1c0b988234 100644
@@ -2516,9 +2516,9 @@ static int __init migrate_on_reclaim_init(void)
  {
  	int ret;
  -	node_demotion = kmalloc_array(nr_node_ids,
-				      sizeof(struct demotion_nodes),
Quoted 8 lines

mm/migrate.c static int __init migrate_on_reclaim_init(void) · line 2521

Quoted 4 lines
mm/migrate.c +3 -1
-				      GFP_KERNEL);
+	node_demotion = kcalloc(nr_node_ids,
+				sizeof(struct demotion_nodes),
+				GFP_KERNEL);

Nit: not sure if this is worthy of this rare corner case, but I think the target demotion nodes' default value should be NUMA_NO_NODE instead of 0.

The "nr" field of "struct demotion_nodes" should be initialized as 0. I think that is checked before "nodes[]" field.

Best Regards,
Huang, Ying

#58 lore raw

Re: [PATCH 14/16] mm/migration: fix potential invalid node access for reclaim-based migration

Quoted 31 lines · Ying
Quoted 20 lines · Baolin Wang
Quoted 19 lines · Miaohe Lin

If we failed to setup hotplug state callbacks for mm/demotion:online in some corner cases, node_demotion will be left uninitialized. Invalid node might be returned from the next_demotion_node() when doing reclaim-based migration. Use kcalloc to allocate node_demotion to fix the issue.

Fixes ac16ec835314 ("mm: migrate: support multiple target nodes

demotion")

---
   mm/migrate.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
mm/migrate.c +0 -1
index 279940c0c064..7b1c0b988234 100644
@@ -2516,9 +2516,9 @@ static int __init migrate_on_reclaim_init(void)
   {
   	int ret;
   -	node_demotion = kmalloc_array(nr_node_ids,
-				      sizeof(struct demotion_nodes),
Quoted 8 lines

mm/migrate.c static int __init migrate_on_reclaim_init(void) · line 2521

Quoted 4 lines
mm/migrate.c +3 -1
-				      GFP_KERNEL);
+	node_demotion = kcalloc(nr_node_ids,
+				sizeof(struct demotion_nodes),
+				GFP_KERNEL);

Nit: not sure if this is worthy of this rare corner case, but I think the target demotion nodes' default value should be NUMA_NO_NODE instead of 0.

Quoted 2 lines

The "nr" field of "struct demotion_nodes" should be initialized as 0. I think that is checked before "nodes[]" field.

Right, but it will be confusing that if nr = 0, while the nodes[] still contains valid node id 0. While we are at this, why not initialize the node_demotion structure with a clear default value? Anyway, no strong opinion on this :)

author
#59 lore raw

Re: [PATCH 14/16] mm/migration: fix potential invalid node access for reclaim-based migration

Quoted 34 lines · Baolin Wang
Quoted 31 lines · Ying
Quoted 28 lines · Baolin Wang
Quoted 15 lines · Miaohe Lin

If we failed to setup hotplug state callbacks for mm/demotion:online in some corner cases, node_demotion will be left uninitialized. Invalid node might be returned from the next_demotion_node() when doing reclaim-based migration. Use kcalloc to allocate node_demotion to fix the issue.

Fixes ac16ec835314 ("mm: migrate: support multiple target nodes

demotion")

---
   mm/migrate.c | 6 +++---

   1 file changed, 3 insertions(+), 3 deletions(-)
mm/migrate.c +0 -0
index 279940c0c064..7b1c0b988234 100644

mm/migrate.c static int __init migrate_on_reclaim_init(void) · line 2521

Quoted 8 lines
mm/migrate.c +3 -2
@@ -2516,9 +2516,9 @@ static int __init migrate_on_reclaim_init(void)
    {
        int ret;
    -    node_demotion = kmalloc_array(nr_node_ids,
-                      sizeof(struct demotion_nodes),
-                      GFP_KERNEL);
+    node_demotion = kcalloc(nr_node_ids,
+                sizeof(struct demotion_nodes),
+                GFP_KERNEL);

Nit: not sure if this is worthy of this rare corner case, but I think the target demotion nodes' default value should be NUMA_NO_NODE instead of 0.

Quoted 4 lines
Quoted 2 lines

The "nr" field of "struct demotion_nodes" should be initialized as 0.  I think that is checked before "nodes[]" field.

Right, but it will be confusing that if nr = 0, while the nodes[] still contains valid node id 0. While we are at this, why not initialize the node_demotion structure with a clear default value? Anyway, no strong opinion on this :)

IMO, this might not deserve initializing the node_demotion structure with a clear default value as cpuhp_setup_state fails at init time should be a rare case.

Thanks both of you.

Quoted 1 line

.

Reviewed-by on v1 14/16
#60 lore raw

Re: [PATCH 14/16] mm/migration: fix potential invalid node access for reclaim-based migration

In reply to Miaohe Lin
Quoted 7 lines · Miaohe Lin

If we failed to setup hotplug state callbacks for mm/demotion:online in some corner cases, node_demotion will be left uninitialized. Invalid node might be returned from the next_demotion_node() when doing reclaim-based migration. Use kcalloc to allocate node_demotion to fix the issue.

Fixes ac16ec835314 ("mm: migrate: support multiple target nodes demotion")

LGTM! Thanks!

Best Regards,
Huang, Ying

Quoted 21 lines
---
 mm/migrate.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/migrate.c +3 -3
index 279940c0c064..7b1c0b988234 100644
@@ -2516,9 +2516,9 @@ static int __init migrate_on_reclaim_init(void)
 {
 	int ret;
 
-	node_demotion = kmalloc_array(nr_node_ids,
-				      sizeof(struct demotion_nodes),
-				      GFP_KERNEL);
+	node_demotion = kcalloc(nr_node_ids,
+				sizeof(struct demotion_nodes),
+				GFP_KERNEL);
 	WARN_ON(!node_demotion);
 
 	ret = cpuhp_setup_state_nocalls(CPUHP_MM_DEMOTION_DEAD, "mm/demotion:offline",
author
#61 lore raw

PATCH 15/16 mm/migration: fix possible do_pages_stat_array racing with memory offline

In reply to Miaohe Lin

When follow_page peeks a page, the page could be reclaimed under heavy memory pressure and thus be offlined while it's still being used by the do_pages_stat_array(). Use FOLL_GET to hold the page refcnt to fix this potential issue.

---
 mm/migrate.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
mm/migrate.c +7 -2
index 7b1c0b988234..98a968e6f465 100644
@@ -1788,13 +1788,18 @@ static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
 			goto set_status;
 
 		/* FOLL_DUMP to ignore special (like zero) pages */
-		page = follow_page(vma, addr, FOLL_DUMP);
+		page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
 
 		err = PTR_ERR(page);
 		if (IS_ERR(page))
 			goto set_status;
 
-		err = page ? page_to_nid(page) : -ENOENT;
+		if (page) {
+			err = page_to_nid(page);
+			put_page(page);
+		} else {
+			err = -ENOENT;
+		}
 set_status:
 		*status = err;
 
Signature · 1 line
2.23.0
#62 lore raw

Re: [PATCH 15/16] mm/migration: fix possible do_pages_stat_array racing with memory offline

Quoted 2 lines · Miaohe Lin

When follow_page peeks a page, the page could be reclaimed under heavy memory pressure

I don't think that memory pressure and reclaiming will be an issue.

Quoted 2 lines

and thus be offlined while it's still being used by the do_pages_stat_array().

"offline" seems a possible problem.

Best Regards,
Huang, Ying

Quoted 32 lines

Use FOLL_GET to hold the page refcnt to fix this
potential issue.

---
 mm/migrate.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
mm/migrate.c +7 -2
index 7b1c0b988234..98a968e6f465 100644
@@ -1788,13 +1788,18 @@ static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
 			goto set_status;
 
 		/* FOLL_DUMP to ignore special (like zero) pages */
-		page = follow_page(vma, addr, FOLL_DUMP);
+		page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
 
 		err = PTR_ERR(page);
 		if (IS_ERR(page))
 			goto set_status;
 
-		err = page ? page_to_nid(page) : -ENOENT;
+		if (page) {
+			err = page_to_nid(page);
+			put_page(page);
+		} else {
+			err = -ENOENT;
+		}
 set_status:
 		*status = err;
#63 lore raw

Re: [PATCH 15/16] mm/migration: fix possible do_pages_stat_array racing with memory offline

Quoted 7 lines · Ying
Quoted 2 lines · Miaohe Lin

When follow_page peeks a page, the page could be reclaimed under heavy memory pressure

I don't think that memory pressure and reclaiming will be an issue.

I think he means a page first to be reclaimed then to be offline could encounter this issue and reclaiming is a precondition.

Thanks.

Quoted 5 lines
Quoted 3 lines

and thus be offlined while it's still being used by the do_pages_stat_array().

"offline" seems a possible problem.

#64 lore raw

Re: [PATCH 15/16] mm/migration: fix possible do_pages_stat_array racing with memory offline

Quoted 11 lines · Muchun Song
Quoted 7 lines · Ying
Quoted 2 lines · Miaohe Lin

When follow_page peeks a page, the page could be reclaimed under heavy memory pressure

I don't think that memory pressure and reclaiming will be an issue.

I think he means a page first to be reclaimed then to be offline could encounter this issue and reclaiming is a precondition.

I don't think reclaiming is a precondition. It seems possible that the virtual page is migrated, then the physical page is offlined.

Best Regards,
Huang, Ying

Quoted 7 lines

Thanks.

Quoted 5 lines
Quoted 3 lines

and thus be offlined while it's still being used by the do_pages_stat_array().

"offline" seems a possible problem.

author
#65 lore raw

Re: [PATCH 15/16] mm/migration: fix possible do_pages_stat_array racing with memory offline

Quoted 9 lines · Ying
Quoted 7 lines · Muchun Song
Quoted 6 lines · Ying
Quoted 2 lines · Miaohe Lin

When follow_page peeks a page, the page could be reclaimed under heavy memory pressure

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I don't think that memory pressure and reclaiming will be an issue.

I think he means a page first to be reclaimed then to be offline could encounter this issue and reclaiming is a precondition.

I don't think reclaiming is a precondition. It seems possible that the virtual page is migrated, then the physical page is offlined.

What I indeed mean is a page could first be reclaimed, migrated and so on. And then be offlined. Sorry for confusing.

Thanks both of you.

Quoted 12 lines

Best Regards,
Huang, Ying

Quoted 7 lines

Thanks.

Quoted 5 lines
Quoted 3 lines

and thus be offlined while it's still being used by the do_pages_stat_array().

"offline" seems a possible problem.

.

author
#66 lore raw

PATCH 16/16 mm/migration: fix potential pte_unmap on an not mapped pte

In reply to Miaohe Lin

__migration_entry_wait and migration_entry_wait_on_locked assume pte is always mapped from caller. But this is not the case when it's called from migration_entry_wait_huge and follow_huge_pmd. And a parameter unmap to indicate whether pte needs to be unmapped to fix this issue.

Fixes 30dad30922cc ("mm: migration: add migrate_entry_wait_huge()")
---
 include/linux/migrate.h |  2 +-
 include/linux/swapops.h |  4 ++--
 mm/filemap.c            | 10 +++++-----
 mm/hugetlb.c            |  2 +-
 mm/migrate.c            | 14 ++++++++------
 5 files changed, 17 insertions(+), 15 deletions(-)
include/linux/migrate.h +1 -1
index 66a34eae8cb6..3ef4ff699bef 100644
@@ -41,7 +41,7 @@ extern int migrate_huge_page_move_mapping(struct address_space *mapping,
 extern int migrate_page_move_mapping(struct address_space *mapping,
 		struct page *newpage, struct page *page, int extra_count);
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl);
+				spinlock_t *ptl, bool unmap);
 void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
 void folio_migrate_copy(struct folio *newfolio, struct folio *folio);
 int folio_migrate_mapping(struct address_space *mapping,
include/linux/swapops.h +2 -2
index d356ab4047f7..d66556875d7d 100644
@@ -213,7 +213,7 @@ static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
 }
 
 extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl);
+					spinlock_t *ptl, bool unmap);
 extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					unsigned long address);
 extern void migration_entry_wait_huge(struct vm_area_struct *vma,
@@ -235,7 +235,7 @@ static inline int is_migration_entry(swp_entry_t swp)
 }
 
 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl) { }
+					spinlock_t *ptl, bool unmap) { }
 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					 unsigned long address) { }
 static inline void migration_entry_wait_huge(struct vm_area_struct *vma,
mm/filemap.c +5 -5
index 8f7e6088ee2a..18c353d52aae 100644
@@ -1389,6 +1389,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * @ptep: mapped pte pointer. Will return with the ptep unmapped. Only required
  *        for pte entries, pass NULL for pmd entries.
  * @ptl: already locked ptl. This function will drop the lock.
+ * @unmap: indicating whether ptep need to be unmapped.
  *
  * Wait for a migration entry referencing the given page to be removed. This is
  * equivalent to put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE) except
@@ -1402,7 +1403,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * there.
  */
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	struct wait_page_queue wait_page;
 	wait_queue_entry_t *wait = &wait_page.wait;
@@ -1439,10 +1440,9 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
 	 * a valid reference to the page, and it must take the ptl to remove the
 	 * migration entry. So the page is valid until the ptl is dropped.
 	 */
-	if (ptep)
-		pte_unmap_unlock(ptep, ptl);
-	else
-		spin_unlock(ptl);
+	spin_unlock(ptl);
+	if (unmap && ptep)
+		pte_unmap(ptep);
 
 	for (;;) {
 		unsigned int flags;
mm/hugetlb.c +1 -1
index 07668781c246..8088128c25db 100644
@@ -6713,7 +6713,7 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address,
 	} else {
 		if (is_hugetlb_entry_migration(pte)) {
 			spin_unlock(ptl);
-			__migration_entry_wait(mm, (pte_t *)pmd, ptl);
+			__migration_entry_wait(mm, (pte_t *)pmd, ptl, false);
 			goto retry;
 		}
 		/*
mm/migrate.c +8 -6
index 98a968e6f465..5519261f54fe 100644
@@ -281,7 +281,7 @@ void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
  * When we return from this function the fault will be retried.
  */
 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	pte_t pte;
 	swp_entry_t entry;
@@ -295,10 +295,12 @@ void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
 	if (!is_migration_entry(entry))
 		goto out;
 
-	migration_entry_wait_on_locked(entry, ptep, ptl);
+	migration_entry_wait_on_locked(entry, ptep, ptl, unmap);
 	return;
 out:
-	pte_unmap_unlock(ptep, ptl);
+	spin_unlock(ptl);
+	if (unmap)
+		pte_unmap(ptep);
 }
 
 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
@@ -306,14 +308,14 @@ void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 {
 	spinlock_t *ptl = pte_lockptr(mm, pmd);
 	pte_t *ptep = pte_offset_map(pmd, address);
-	__migration_entry_wait(mm, ptep, ptl);
+	__migration_entry_wait(mm, ptep, ptl, true);
 }
 
 void migration_entry_wait_huge(struct vm_area_struct *vma,
 		struct mm_struct *mm, pte_t *pte)
 {
 	spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
-	__migration_entry_wait(mm, pte, ptl);
+	__migration_entry_wait(mm, pte, ptl, false);
 }
 
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
@@ -324,7 +326,7 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
 	ptl = pmd_lock(mm, pmd);
 	if (!is_pmd_migration_entry(*pmd))
 		goto unlock;
-	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl);
+	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl, false);
 	return;
 unlock:
 	spin_unlock(ptl);
Signature · 1 line
2.23.0
#67 lore raw

Re: [PATCH 16/16] mm/migration: fix potential pte_unmap on an not mapped pte

Quoted 4 lines · Miaohe Lin

__migration_entry_wait and migration_entry_wait_on_locked assume pte is always mapped from caller. But this is not the case when it's called from migration_entry_wait_huge and follow_huge_pmd. And a parameter unmap to indicate whether pte needs to be unmapped to fix this issue.

This seems a possible issue.

Have you tested it? It appears that it's possible to trigger the issue. If so, you can paste the error log here.

BTW: have you tested the other functionality issues in your patchset?

Best Regards,
Huang, Ying

Quoted 147 lines
Fixes 30dad30922cc ("mm: migration: add migrate_entry_wait_huge()")
---
 include/linux/migrate.h |  2 +-
 include/linux/swapops.h |  4 ++--
 mm/filemap.c            | 10 +++++-----
 mm/hugetlb.c            |  2 +-
 mm/migrate.c            | 14 ++++++++------
 5 files changed, 17 insertions(+), 15 deletions(-)
include/linux/migrate.h +1 -1
index 66a34eae8cb6..3ef4ff699bef 100644
@@ -41,7 +41,7 @@ extern int migrate_huge_page_move_mapping(struct address_space *mapping,
 extern int migrate_page_move_mapping(struct address_space *mapping,
 		struct page *newpage, struct page *page, int extra_count);
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl);
+				spinlock_t *ptl, bool unmap);
 void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
 void folio_migrate_copy(struct folio *newfolio, struct folio *folio);
 int folio_migrate_mapping(struct address_space *mapping,
include/linux/swapops.h +2 -2
index d356ab4047f7..d66556875d7d 100644
@@ -213,7 +213,7 @@ static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
 }
 
 extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl);
+					spinlock_t *ptl, bool unmap);
 extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					unsigned long address);
 extern void migration_entry_wait_huge(struct vm_area_struct *vma,
@@ -235,7 +235,7 @@ static inline int is_migration_entry(swp_entry_t swp)
 }
 
 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl) { }
+					spinlock_t *ptl, bool unmap) { }
 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					 unsigned long address) { }
 static inline void migration_entry_wait_huge(struct vm_area_struct *vma,
mm/filemap.c +5 -5
index 8f7e6088ee2a..18c353d52aae 100644
@@ -1389,6 +1389,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * @ptep: mapped pte pointer. Will return with the ptep unmapped. Only required
  *        for pte entries, pass NULL for pmd entries.
  * @ptl: already locked ptl. This function will drop the lock.
+ * @unmap: indicating whether ptep need to be unmapped.
  *
  * Wait for a migration entry referencing the given page to be removed. This is
  * equivalent to put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE) except
@@ -1402,7 +1403,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * there.
  */
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	struct wait_page_queue wait_page;
 	wait_queue_entry_t *wait = &wait_page.wait;
@@ -1439,10 +1440,9 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
 	 * a valid reference to the page, and it must take the ptl to remove the
 	 * migration entry. So the page is valid until the ptl is dropped.
 	 */
-	if (ptep)
-		pte_unmap_unlock(ptep, ptl);
-	else
-		spin_unlock(ptl);
+	spin_unlock(ptl);
+	if (unmap && ptep)
+		pte_unmap(ptep);
 
 	for (;;) {
 		unsigned int flags;
mm/hugetlb.c +1 -1
index 07668781c246..8088128c25db 100644
@@ -6713,7 +6713,7 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address,
 	} else {
 		if (is_hugetlb_entry_migration(pte)) {
 			spin_unlock(ptl);
-			__migration_entry_wait(mm, (pte_t *)pmd, ptl);
+			__migration_entry_wait(mm, (pte_t *)pmd, ptl, false);
 			goto retry;
 		}
 		/*
mm/migrate.c +8 -6
index 98a968e6f465..5519261f54fe 100644
@@ -281,7 +281,7 @@ void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
  * When we return from this function the fault will be retried.
  */
 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	pte_t pte;
 	swp_entry_t entry;
@@ -295,10 +295,12 @@ void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
 	if (!is_migration_entry(entry))
 		goto out;
 
-	migration_entry_wait_on_locked(entry, ptep, ptl);
+	migration_entry_wait_on_locked(entry, ptep, ptl, unmap);
 	return;
 out:
-	pte_unmap_unlock(ptep, ptl);
+	spin_unlock(ptl);
+	if (unmap)
+		pte_unmap(ptep);
 }
 
 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
@@ -306,14 +308,14 @@ void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 {
 	spinlock_t *ptl = pte_lockptr(mm, pmd);
 	pte_t *ptep = pte_offset_map(pmd, address);
-	__migration_entry_wait(mm, ptep, ptl);
+	__migration_entry_wait(mm, ptep, ptl, true);
 }
 
 void migration_entry_wait_huge(struct vm_area_struct *vma,
 		struct mm_struct *mm, pte_t *pte)
 {
 	spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
-	__migration_entry_wait(mm, pte, ptl);
+	__migration_entry_wait(mm, pte, ptl, false);
 }
 
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
@@ -324,7 +326,7 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
 	ptl = pmd_lock(mm, pmd);
 	if (!is_pmd_migration_entry(*pmd))
 		goto unlock;
-	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl);
+	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl, false);
 	return;
 unlock:
 	spin_unlock(ptl);
author
#68 lore raw

Re: [PATCH 16/16] mm/migration: fix potential pte_unmap on an not mapped pte

Quoted 11 lines · Ying
Quoted 4 lines · Miaohe Lin

__migration_entry_wait and migration_entry_wait_on_locked assume pte is always mapped from caller. But this is not the case when it's called from migration_entry_wait_huge and follow_huge_pmd. And a parameter unmap to indicate whether pte needs to be unmapped to fix this issue.

This seems a possible issue.

Have you tested it? It appears that it's possible to trigger the issue. If so, you can paste the error log here.

This might happen iff on x86 machine with HIGHMEM enabled which is uncommon now (at least in my work environment). So I can't paste the err log. And The issues from this series mainly come from the code investigating with some tests.

Thanks. :)

Quoted 155 lines

BTW: have you tested the other functionality issues in your patchset?

Best Regards,
Huang, Ying

Quoted 139 lines
Fixes 30dad30922cc ("mm: migration: add migrate_entry_wait_huge()")
---
 include/linux/migrate.h |  2 +-
 include/linux/swapops.h |  4 ++--
 mm/filemap.c            | 10 +++++-----
 mm/hugetlb.c            |  2 +-
 mm/migrate.c            | 14 ++++++++------
 5 files changed, 17 insertions(+), 15 deletions(-)
include/linux/migrate.h +1 -1
index 66a34eae8cb6..3ef4ff699bef 100644
@@ -41,7 +41,7 @@ extern int migrate_huge_page_move_mapping(struct address_space *mapping,
 extern int migrate_page_move_mapping(struct address_space *mapping,
 		struct page *newpage, struct page *page, int extra_count);
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl);
+				spinlock_t *ptl, bool unmap);
 void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
 void folio_migrate_copy(struct folio *newfolio, struct folio *folio);
 int folio_migrate_mapping(struct address_space *mapping,
include/linux/swapops.h +2 -2
index d356ab4047f7..d66556875d7d 100644
@@ -213,7 +213,7 @@ static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
 }
 
 extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl);
+					spinlock_t *ptl, bool unmap);
 extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					unsigned long address);
 extern void migration_entry_wait_huge(struct vm_area_struct *vma,
@@ -235,7 +235,7 @@ static inline int is_migration_entry(swp_entry_t swp)
 }
 
 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl) { }
+					spinlock_t *ptl, bool unmap) { }
 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					 unsigned long address) { }
 static inline void migration_entry_wait_huge(struct vm_area_struct *vma,
mm/filemap.c +5 -5
index 8f7e6088ee2a..18c353d52aae 100644
@@ -1389,6 +1389,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * @ptep: mapped pte pointer. Will return with the ptep unmapped. Only required
  *        for pte entries, pass NULL for pmd entries.
  * @ptl: already locked ptl. This function will drop the lock.
+ * @unmap: indicating whether ptep need to be unmapped.
  *
  * Wait for a migration entry referencing the given page to be removed. This is
  * equivalent to put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE) except
@@ -1402,7 +1403,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * there.
  */
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	struct wait_page_queue wait_page;
 	wait_queue_entry_t *wait = &wait_page.wait;
@@ -1439,10 +1440,9 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
 	 * a valid reference to the page, and it must take the ptl to remove the
 	 * migration entry. So the page is valid until the ptl is dropped.
 	 */
-	if (ptep)
-		pte_unmap_unlock(ptep, ptl);
-	else
-		spin_unlock(ptl);
+	spin_unlock(ptl);
+	if (unmap && ptep)
+		pte_unmap(ptep);
 
 	for (;;) {
 		unsigned int flags;
mm/hugetlb.c +1 -1
index 07668781c246..8088128c25db 100644
@@ -6713,7 +6713,7 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address,
 	} else {
 		if (is_hugetlb_entry_migration(pte)) {
 			spin_unlock(ptl);
-			__migration_entry_wait(mm, (pte_t *)pmd, ptl);
+			__migration_entry_wait(mm, (pte_t *)pmd, ptl, false);
 			goto retry;
 		}
 		/*
mm/migrate.c +7 -5
index 98a968e6f465..5519261f54fe 100644
@@ -281,7 +281,7 @@ void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
  * When we return from this function the fault will be retried.
  */
 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	pte_t pte;
 	swp_entry_t entry;
@@ -295,10 +295,12 @@ void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
 	if (!is_migration_entry(entry))
 		goto out;
 
-	migration_entry_wait_on_locked(entry, ptep, ptl);
+	migration_entry_wait_on_locked(entry, ptep, ptl, unmap);
 	return;
 out:
-	pte_unmap_unlock(ptep, ptl);
+	spin_unlock(ptl);
+	if (unmap)
+		pte_unmap(ptep);
 }
 
 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
@@ -306,14 +308,14 @@ void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 {
 	spinlock_t *ptl = pte_lockptr(mm, pmd);
 	pte_t *ptep = pte_offset_map(pmd, address);
-	__migration_entry_wait(mm, ptep, ptl);
+	__migration_entry_wait(mm, ptep, ptl, true);
 }
 
 void migration_entry_wait_huge(struct vm_area_struct *vma,
 		struct mm_struct *mm, pte_t *pte)
 {
 	spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
-	__migration_entry_wait(mm, pte, ptl);
+	__migration_entry_wait(mm, pte, ptl, false);
 }
 
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION

mm/migrate.c void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) · line 332

Quoted 8 lines
mm/migrate.c +1 -1
@@ -324,7 +326,7 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
 	ptl = pmd_lock(mm, pmd);
 	if (!is_pmd_migration_entry(*pmd))
 		goto unlock;
-	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl);
+	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl, false);
 	return;
 unlock:
 	spin_unlock(ptl);

.

#69 lore raw

Re: [PATCH 16/16] mm/migration: fix potential pte_unmap on an not mapped pte

Quoted 8 lines · Miaohe Lin
Quoted 7 lines · Ying
Quoted 4 lines · Miaohe Lin

__migration_entry_wait and migration_entry_wait_on_locked assume pte is always mapped from caller. But this is not the case when it's called from migration_entry_wait_huge and follow_huge_pmd. And a parameter unmap to indicate whether pte needs to be unmapped to fix this issue.

Quoted 7 lines
Quoted 4 lines

This seems a possible issue.

Have you tested it? It appears that it's possible to trigger the issue. If so, you can paste the error log here.

This might happen iff on x86 machine with HIGHMEM enabled which is uncommon now (at least in my work environment).

Yes. 32-bit isn't popular now. But you can always test it via virtual machine.

Quoted 2 lines

So I can't paste the err log. And The
issues from this series mainly come from the code investigating with some tests.

If not too complex, I still think it's better to test your code and verify the problem.

Best Regards,
Huang, Ying

Quoted 157 lines

Thanks. :)

Quoted 155 lines

BTW: have you tested the other functionality issues in your patchset?

Best Regards,
Huang, Ying

Quoted 139 lines
Fixes 30dad30922cc ("mm: migration: add migrate_entry_wait_huge()")
---
 include/linux/migrate.h |  2 +-
 include/linux/swapops.h |  4 ++--
 mm/filemap.c            | 10 +++++-----
 mm/hugetlb.c            |  2 +-
 mm/migrate.c            | 14 ++++++++------
 5 files changed, 17 insertions(+), 15 deletions(-)
include/linux/migrate.h +1 -1
index 66a34eae8cb6..3ef4ff699bef 100644
@@ -41,7 +41,7 @@ extern int migrate_huge_page_move_mapping(struct address_space *mapping,
 extern int migrate_page_move_mapping(struct address_space *mapping,
 		struct page *newpage, struct page *page, int extra_count);
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl);
+				spinlock_t *ptl, bool unmap);
 void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
 void folio_migrate_copy(struct folio *newfolio, struct folio *folio);
 int folio_migrate_mapping(struct address_space *mapping,
include/linux/swapops.h +2 -2
index d356ab4047f7..d66556875d7d 100644
@@ -213,7 +213,7 @@ static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
 }
 
 extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl);
+					spinlock_t *ptl, bool unmap);
 extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					unsigned long address);
 extern void migration_entry_wait_huge(struct vm_area_struct *vma,
@@ -235,7 +235,7 @@ static inline int is_migration_entry(swp_entry_t swp)
 }
 
 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl) { }
+					spinlock_t *ptl, bool unmap) { }
 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					 unsigned long address) { }
 static inline void migration_entry_wait_huge(struct vm_area_struct *vma,
mm/filemap.c +5 -5
index 8f7e6088ee2a..18c353d52aae 100644
@@ -1389,6 +1389,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * @ptep: mapped pte pointer. Will return with the ptep unmapped. Only required
  *        for pte entries, pass NULL for pmd entries.
  * @ptl: already locked ptl. This function will drop the lock.
+ * @unmap: indicating whether ptep need to be unmapped.
  *
  * Wait for a migration entry referencing the given page to be removed. This is
  * equivalent to put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE) except
@@ -1402,7 +1403,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * there.
  */
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	struct wait_page_queue wait_page;
 	wait_queue_entry_t *wait = &wait_page.wait;
@@ -1439,10 +1440,9 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
 	 * a valid reference to the page, and it must take the ptl to remove the
 	 * migration entry. So the page is valid until the ptl is dropped.
 	 */
-	if (ptep)
-		pte_unmap_unlock(ptep, ptl);
-	else
-		spin_unlock(ptl);
+	spin_unlock(ptl);
+	if (unmap && ptep)
+		pte_unmap(ptep);
 
 	for (;;) {
 		unsigned int flags;
mm/hugetlb.c +1 -1
index 07668781c246..8088128c25db 100644
@@ -6713,7 +6713,7 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address,
 	} else {
 		if (is_hugetlb_entry_migration(pte)) {
 			spin_unlock(ptl);
-			__migration_entry_wait(mm, (pte_t *)pmd, ptl);
+			__migration_entry_wait(mm, (pte_t *)pmd, ptl, false);
 			goto retry;
 		}
 		/*
mm/migrate.c +7 -5
index 98a968e6f465..5519261f54fe 100644
@@ -281,7 +281,7 @@ void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
  * When we return from this function the fault will be retried.
  */
 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	pte_t pte;
 	swp_entry_t entry;
@@ -295,10 +295,12 @@ void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
 	if (!is_migration_entry(entry))
 		goto out;
 
-	migration_entry_wait_on_locked(entry, ptep, ptl);
+	migration_entry_wait_on_locked(entry, ptep, ptl, unmap);
 	return;
 out:
-	pte_unmap_unlock(ptep, ptl);
+	spin_unlock(ptl);
+	if (unmap)
+		pte_unmap(ptep);
 }
 
 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
@@ -306,14 +308,14 @@ void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 {
 	spinlock_t *ptl = pte_lockptr(mm, pmd);
 	pte_t *ptep = pte_offset_map(pmd, address);
-	__migration_entry_wait(mm, ptep, ptl);
+	__migration_entry_wait(mm, ptep, ptl, true);
 }
 
 void migration_entry_wait_huge(struct vm_area_struct *vma,
 		struct mm_struct *mm, pte_t *pte)
 {
 	spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
-	__migration_entry_wait(mm, pte, ptl);
+	__migration_entry_wait(mm, pte, ptl, false);
 }
 
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION

mm/migrate.c void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) · line 332

Quoted 8 lines
mm/migrate.c +1 -1
@@ -324,7 +326,7 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
 	ptl = pmd_lock(mm, pmd);
 	if (!is_pmd_migration_entry(*pmd))
 		goto unlock;
-	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl);
+	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl, false);
 	return;
 unlock:
 	spin_unlock(ptl);

.

author
#70 lore raw

Re: [PATCH 16/16] mm/migration: fix potential pte_unmap on an not mapped pte

Quoted 12 lines · Ying
Quoted 10 lines · Miaohe Lin
Quoted 9 lines · Ying
Quoted 4 lines · Miaohe Lin

__migration_entry_wait and migration_entry_wait_on_locked assume pte is always mapped from caller. But this is not the case when it's called from migration_entry_wait_huge and follow_huge_pmd. And a parameter unmap to indicate whether pte needs to be unmapped to fix this issue.

This seems a possible issue.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Have you tested it? It appears that it's possible to trigger the issue. If so, you can paste the error log here.

This might happen iff on x86 machine with HIGHMEM enabled which is uncommon now (at least in my work environment).

Yes. 32-bit isn't popular now. But you can always test it via virtual machine.

Good idea.

Quoted 5 lines
Quoted 2 lines

So I can't paste the err log. And The
issues from this series mainly come from the code investigating with some tests.

If not too complex, I still think it's better to test your code and verify the problem.

Sure! :)
Many thanks.

Quoted 163 lines

Best Regards,
Huang, Ying

Quoted 157 lines

Thanks. :)

Quoted 155 lines

BTW: have you tested the other functionality issues in your patchset?

Best Regards,
Huang, Ying

Quoted 139 lines
Fixes 30dad30922cc ("mm: migration: add migrate_entry_wait_huge()")
---
 include/linux/migrate.h |  2 +-
 include/linux/swapops.h |  4 ++--
 mm/filemap.c            | 10 +++++-----
 mm/hugetlb.c            |  2 +-
 mm/migrate.c            | 14 ++++++++------
 5 files changed, 17 insertions(+), 15 deletions(-)
include/linux/migrate.h +1 -1
index 66a34eae8cb6..3ef4ff699bef 100644
@@ -41,7 +41,7 @@ extern int migrate_huge_page_move_mapping(struct address_space *mapping,
 extern int migrate_page_move_mapping(struct address_space *mapping,
 		struct page *newpage, struct page *page, int extra_count);
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl);
+				spinlock_t *ptl, bool unmap);
 void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
 void folio_migrate_copy(struct folio *newfolio, struct folio *folio);
 int folio_migrate_mapping(struct address_space *mapping,
include/linux/swapops.h +2 -2
index d356ab4047f7..d66556875d7d 100644
@@ -213,7 +213,7 @@ static inline swp_entry_t make_writable_migration_entry(pgoff_t offset)
 }
 
 extern void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl);
+					spinlock_t *ptl, bool unmap);
 extern void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					unsigned long address);
 extern void migration_entry_wait_huge(struct vm_area_struct *vma,
@@ -235,7 +235,7 @@ static inline int is_migration_entry(swp_entry_t swp)
 }
 
 static inline void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-					spinlock_t *ptl) { }
+					spinlock_t *ptl, bool unmap) { }
 static inline void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 					 unsigned long address) { }
 static inline void migration_entry_wait_huge(struct vm_area_struct *vma,
mm/filemap.c +5 -5
index 8f7e6088ee2a..18c353d52aae 100644
@@ -1389,6 +1389,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * @ptep: mapped pte pointer. Will return with the ptep unmapped. Only required
  *        for pte entries, pass NULL for pmd entries.
  * @ptl: already locked ptl. This function will drop the lock.
+ * @unmap: indicating whether ptep need to be unmapped.
  *
  * Wait for a migration entry referencing the given page to be removed. This is
  * equivalent to put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE) except
@@ -1402,7 +1403,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * there.
  */
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	struct wait_page_queue wait_page;
 	wait_queue_entry_t *wait = &wait_page.wait;
@@ -1439,10 +1440,9 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
 	 * a valid reference to the page, and it must take the ptl to remove the
 	 * migration entry. So the page is valid until the ptl is dropped.
 	 */
-	if (ptep)
-		pte_unmap_unlock(ptep, ptl);
-	else
-		spin_unlock(ptl);
+	spin_unlock(ptl);
+	if (unmap && ptep)
+		pte_unmap(ptep);
 
 	for (;;) {
 		unsigned int flags;
mm/hugetlb.c +1 -1
index 07668781c246..8088128c25db 100644
@@ -6713,7 +6713,7 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address,
 	} else {
 		if (is_hugetlb_entry_migration(pte)) {
 			spin_unlock(ptl);
-			__migration_entry_wait(mm, (pte_t *)pmd, ptl);
+			__migration_entry_wait(mm, (pte_t *)pmd, ptl, false);
 			goto retry;
 		}
 		/*
mm/migrate.c +7 -5
index 98a968e6f465..5519261f54fe 100644
@@ -281,7 +281,7 @@ void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
  * When we return from this function the fault will be retried.
  */
 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	pte_t pte;
 	swp_entry_t entry;
@@ -295,10 +295,12 @@ void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
 	if (!is_migration_entry(entry))
 		goto out;
 
-	migration_entry_wait_on_locked(entry, ptep, ptl);
+	migration_entry_wait_on_locked(entry, ptep, ptl, unmap);
 	return;
 out:
-	pte_unmap_unlock(ptep, ptl);
+	spin_unlock(ptl);
+	if (unmap)
+		pte_unmap(ptep);
 }
 
 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
@@ -306,14 +308,14 @@ void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 {
 	spinlock_t *ptl = pte_lockptr(mm, pmd);
 	pte_t *ptep = pte_offset_map(pmd, address);
-	__migration_entry_wait(mm, ptep, ptl);
+	__migration_entry_wait(mm, ptep, ptl, true);
 }
 
 void migration_entry_wait_huge(struct vm_area_struct *vma,
 		struct mm_struct *mm, pte_t *pte)
 {
 	spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
-	__migration_entry_wait(mm, pte, ptl);
+	__migration_entry_wait(mm, pte, ptl, false);
 }
 
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION

mm/migrate.c void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd) · line 332

Quoted 8 lines
mm/migrate.c +1 -1
@@ -324,7 +326,7 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
 	ptl = pmd_lock(mm, pmd);
 	if (!is_pmd_migration_entry(*pmd))
 		goto unlock;
-	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl);
+	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl, false);
 	return;
 unlock:
 	spin_unlock(ptl);

.

.

#71 lore raw

Re: [PATCH 16/16] mm/migration: fix potential pte_unmap on an not mapped pte

In reply to Miaohe Lin
Quoted 4 lines · Miaohe Lin

__migration_entry_wait and migration_entry_wait_on_locked assume pte is always mapped from caller. But this is not the case when it's called from migration_entry_wait_huge and follow_huge_pmd. And a parameter unmap to indicate whether pte needs to be unmapped to fix this issue.

[...]

Quoted 13 lines
mm/filemap.c +1 -0
index 8f7e6088ee2a..18c353d52aae 100644
@@ -1389,6 +1389,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * @ptep: mapped pte pointer. Will return with the ptep unmapped. Only required
  *        for pte entries, pass NULL for pmd entries.
  * @ptl: already locked ptl. This function will drop the lock.
+ * @unmap: indicating whether ptep need to be unmapped.
  *
  * Wait for a migration entry referencing the given page to be removed. This is
  * equivalent to put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE) except

mm/filemap.c static inline int folio_wait_bit_common(struct folio *folio, int bit_nr, · line 1406

Quoted 5 lines
mm/filemap.c +1 -1
@@ -1402,7 +1403,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * there.
  */
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)

It's a pity we have to pass unmap all the way down to migration_entry_wait_on_locked().

Quoted 4 lines
mm/filemap.c +0 -0
 {
 	struct wait_page_queue wait_page;
 	wait_queue_entry_t *wait = &wait_page.wait;

mm/filemap.c void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep, · line 1445

Quoted 10 lines
mm/filemap.c +3 -4
@@ -1439,10 +1440,9 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
 	 * a valid reference to the page, and it must take the ptl to remove the
 	 * migration entry. So the page is valid until the ptl is dropped.
 	 */
-	if (ptep)
-		pte_unmap_unlock(ptep, ptl);
-	else
-		spin_unlock(ptl);
+	spin_unlock(ptl);
+	if (unmap && ptep)
+		pte_unmap(ptep);

However we might not have to - afaict this is the only usage of ptep so callers could do the pte_unmap() prior to calling migration_entry_wait_on_locked(). We could then remove both the ptep and unmap parameters. Ie:

migration_entry_wait_on_locked(swp_entry_t entry, spinlock_t *ptl)

This does assume it's ok to change the order of pte unmap/ptl unlock operations. I'm not terribly familiar with CONFIG_HIGHPTE systems, but it seems like that should be ok.

- Alistair

Quoted 70 lines
mm/filemap.c +0 -0
 	for (;;) {
 		unsigned int flags;
mm/hugetlb.c +1 -1
index 07668781c246..8088128c25db 100644
@@ -6713,7 +6713,7 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address,
 	} else {
 		if (is_hugetlb_entry_migration(pte)) {
 			spin_unlock(ptl);
-			__migration_entry_wait(mm, (pte_t *)pmd, ptl);
+			__migration_entry_wait(mm, (pte_t *)pmd, ptl, false);
 			goto retry;
 		}
 		/*
mm/migrate.c +8 -6
index 98a968e6f465..5519261f54fe 100644
@@ -281,7 +281,7 @@ void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
  * When we return from this function the fault will be retried.
  */
 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	pte_t pte;
 	swp_entry_t entry;
@@ -295,10 +295,12 @@ void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
 	if (!is_migration_entry(entry))
 		goto out;
 
-	migration_entry_wait_on_locked(entry, ptep, ptl);
+	migration_entry_wait_on_locked(entry, ptep, ptl, unmap);
 	return;
 out:
-	pte_unmap_unlock(ptep, ptl);
+	spin_unlock(ptl);
+	if (unmap)
+		pte_unmap(ptep);
 }
 
 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
@@ -306,14 +308,14 @@ void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 {
 	spinlock_t *ptl = pte_lockptr(mm, pmd);
 	pte_t *ptep = pte_offset_map(pmd, address);
-	__migration_entry_wait(mm, ptep, ptl);
+	__migration_entry_wait(mm, ptep, ptl, true);
 }
 
 void migration_entry_wait_huge(struct vm_area_struct *vma,
 		struct mm_struct *mm, pte_t *pte)
 {
 	spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
-	__migration_entry_wait(mm, pte, ptl);
+	__migration_entry_wait(mm, pte, ptl, false);
 }
 
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
@@ -324,7 +326,7 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
 	ptl = pmd_lock(mm, pmd);
 	if (!is_pmd_migration_entry(*pmd))
 		goto unlock;
-	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl);
+	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl, false);
 	return;
 unlock:
 	spin_unlock(ptl);
author
#72 lore raw

Re: [PATCH 16/16] mm/migration: fix potential pte_unmap on an not mapped pte

Quoted 50 lines · Alistair Popple
Quoted 4 lines · Miaohe Lin

__migration_entry_wait and migration_entry_wait_on_locked assume pte is always mapped from caller. But this is not the case when it's called from migration_entry_wait_huge and follow_huge_pmd. And a parameter unmap to indicate whether pte needs to be unmapped to fix this issue.

[...]

Quoted 13 lines
mm/filemap.c +1 -0
index 8f7e6088ee2a..18c353d52aae 100644
@@ -1389,6 +1389,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * @ptep: mapped pte pointer. Will return with the ptep unmapped. Only required
  *        for pte entries, pass NULL for pmd entries.
  * @ptl: already locked ptl. This function will drop the lock.
+ * @unmap: indicating whether ptep need to be unmapped.
  *
  * Wait for a migration entry referencing the given page to be removed. This is
  * equivalent to put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE) except

mm/filemap.c static inline int folio_wait_bit_common(struct folio *folio, int bit_nr, · line 1406

Quoted 5 lines
mm/filemap.c +1 -1
@@ -1402,7 +1403,7 @@ static inline int folio_wait_bit_common(struct folio *folio, int bit_nr,
  * there.
  */
 void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)

It's a pity we have to pass unmap all the way down to migration_entry_wait_on_locked().

Quoted 4 lines
mm/filemap.c +0 -0
 {
 	struct wait_page_queue wait_page;
 	wait_queue_entry_t *wait = &wait_page.wait;

mm/filemap.c void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep, · line 1445

Quoted 10 lines
mm/filemap.c +3 -4
@@ -1439,10 +1440,9 @@ void migration_entry_wait_on_locked(swp_entry_t entry, pte_t *ptep,
 	 * a valid reference to the page, and it must take the ptl to remove the
 	 * migration entry. So the page is valid until the ptl is dropped.
 	 */
-	if (ptep)
-		pte_unmap_unlock(ptep, ptl);
-	else
-		spin_unlock(ptl);
+	spin_unlock(ptl);
+	if (unmap && ptep)
+		pte_unmap(ptep);

However we might not have to - afaict this is the only usage of ptep so callers could do the pte_unmap() prior to calling migration_entry_wait_on_locked(). We could then remove both the ptep and unmap parameters. Ie:

Quoted 6 lines

migration_entry_wait_on_locked(swp_entry_t entry, spinlock_t *ptl)

This does assume it's ok to change the order of pte unmap/ptl unlock operations. I'm not terribly familiar with CONFIG_HIGHPTE systems, but it seems like that should be ok.

Looks like a good idea. We can leave the pte_unmap to the caller as only page table spin_lock is needed to make sure the validity of the page.

Will try to do this in V2. Many thanks.

Quoted 72 lines

- Alistair

Quoted 70 lines
mm/filemap.c +0 -0
 	for (;;) {
 		unsigned int flags;
mm/hugetlb.c +1 -1
index 07668781c246..8088128c25db 100644
@@ -6713,7 +6713,7 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address,
 	} else {
 		if (is_hugetlb_entry_migration(pte)) {
 			spin_unlock(ptl);
-			__migration_entry_wait(mm, (pte_t *)pmd, ptl);
+			__migration_entry_wait(mm, (pte_t *)pmd, ptl, false);
 			goto retry;
 		}
 		/*
mm/migrate.c +8 -6
index 98a968e6f465..5519261f54fe 100644
@@ -281,7 +281,7 @@ void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
  * When we return from this function the fault will be retried.
  */
 void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
-				spinlock_t *ptl)
+				spinlock_t *ptl, bool unmap)
 {
 	pte_t pte;
 	swp_entry_t entry;
@@ -295,10 +295,12 @@ void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
 	if (!is_migration_entry(entry))
 		goto out;
 
-	migration_entry_wait_on_locked(entry, ptep, ptl);
+	migration_entry_wait_on_locked(entry, ptep, ptl, unmap);
 	return;
 out:
-	pte_unmap_unlock(ptep, ptl);
+	spin_unlock(ptl);
+	if (unmap)
+		pte_unmap(ptep);
 }
 
 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
@@ -306,14 +308,14 @@ void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
 {
 	spinlock_t *ptl = pte_lockptr(mm, pmd);
 	pte_t *ptep = pte_offset_map(pmd, address);
-	__migration_entry_wait(mm, ptep, ptl);
+	__migration_entry_wait(mm, ptep, ptl, true);
 }
 
 void migration_entry_wait_huge(struct vm_area_struct *vma,
 		struct mm_struct *mm, pte_t *pte)
 {
 	spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
-	__migration_entry_wait(mm, pte, ptl);
+	__migration_entry_wait(mm, pte, ptl, false);
 }
 
 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
@@ -324,7 +326,7 @@ void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
 	ptl = pmd_lock(mm, pmd);
 	if (!is_pmd_migration_entry(*pmd))
 		goto unlock;
-	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl);
+	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), NULL, ptl, false);
 	return;
 unlock:
 	spin_unlock(ptl);