Skip to contentSkip to conversation

mm/page_isolation: fix potential warning from user

PATCH Superseded Qian Cai 3 participants 10 messages Jan 20, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Qian Cai

author
#0 lore raw

PATCH v2 1/1 mm/page_isolation: fix potential warning from user

It makes sense to call the WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE) from start_isolate_page_range(), but should avoid triggering it from userspace, i.e, from is_mem_section_removable() because it could be a DoS if warn_on_panic is set.

While at it, simplify the code a bit by removing an unnecessary jump label and a local variable, so set_migratetype_isolate() could really return a bool.

---

v2: Improve the commit log.
Warn for all start_isolate_page_range() users not just offlining.

 mm/page_alloc.c     | 11 ++++-------
 mm/page_isolation.c | 30 +++++++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
mm/page_alloc.c +4 -7
index 621716a25639..3c4eb750a199 100644
@@ -8231,7 +8231,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		if (is_migrate_cma(migratetype))
 			return NULL;
 
-		goto unmovable;
+		return page;
 	}
 
 	for (; iter < pageblock_nr_pages; iter++) {
@@ -8241,7 +8241,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		page = pfn_to_page(pfn + iter);
 
 		if (PageReserved(page))
-			goto unmovable;
+			return page;
 
 		/*
 		 * If the zone is movable and we have ruled out all reserved
@@ -8261,7 +8261,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 			unsigned int skip_pages;
 
 			if (!hugepage_migration_supported(page_hstate(head)))
-				goto unmovable;
+				return page;
 
 			skip_pages = compound_nr(head) - (page - head);
 			iter += skip_pages - 1;
@@ -8303,12 +8303,9 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		 * is set to both of a memory hole page and a _used_ kernel
 		 * page at boot.
 		 */
-		goto unmovable;
+		return page;
 	}
 	return NULL;
-unmovable:
-	WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
-	return pfn_to_page(pfn + iter);
 }
 
 #ifdef CONFIG_CONTIG_ALLOC
mm/page_isolation.c +17 -13
index e70586523ca3..31f5516f5d54 100644
@@ -15,12 +15,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/page_isolation.h>
 
-static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
+static bool set_migratetype_isolate(struct page *page, int migratetype,
+				    int isol_flags)
 {
-	struct page *unmovable = NULL;
+	struct page *unmovable = ERR_PTR(-EBUSY);
 	struct zone *zone;
 	unsigned long flags;
-	int ret = -EBUSY;
 
 	zone = page_zone(page);
 
@@ -49,21 +49,25 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol_
 									NULL);
 
 		__mod_zone_freepage_state(zone, -nr_pages, mt);
-		ret = 0;
 	}
 
 out:
 	spin_unlock_irqrestore(&zone->lock, flags);
-	if (!ret)
+
+	if (!unmovable) {
 		drain_all_pages(zone);
-	else if ((isol_flags & REPORT_FAILURE) && unmovable)
-		/*
-		 * printk() with zone->lock held will guarantee to trigger a
-		 * lockdep splat, so defer it here.
-		 */
-		dump_page(unmovable, "unmovable page");
-
-	return ret;
+	} else {
+		WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
+
+		if ((isol_flags & REPORT_FAILURE) && !IS_ERR(unmovable))
+			/*
+			 * printk() with zone->lock held will likely trigger a
+			 * lockdep splat, so defer it here.
+			 */
+			dump_page(unmovable, "unmovable page");
+	}
+
+	return !!unmovable;
 }
 
 static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
Signature · 1 line
2.21.0 (Apple Git-122.2)
#1 lore raw
Quoted 70 lines · Qian Cai

It makes sense to call the WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE) from start_isolate_page_range(), but should avoid triggering it from userspace, i.e, from is_mem_section_removable() because it could be a DoS if warn_on_panic is set.

While at it, simplify the code a bit by removing an unnecessary jump label and a local variable, so set_migratetype_isolate() could really return a bool.

---

v2: Improve the commit log.
Warn for all start_isolate_page_range() users not just offlining.

 mm/page_alloc.c     | 11 ++++-------
 mm/page_isolation.c | 30 +++++++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
mm/page_alloc.c +4 -7
index 621716a25639..3c4eb750a199 100644
@@ -8231,7 +8231,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		if (is_migrate_cma(migratetype))
 			return NULL;
 
-		goto unmovable;
+		return page;
 	}
 
 	for (; iter < pageblock_nr_pages; iter++) {
@@ -8241,7 +8241,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		page = pfn_to_page(pfn + iter);
 
 		if (PageReserved(page))
-			goto unmovable;
+			return page;
 
 		/*
 		 * If the zone is movable and we have ruled out all reserved
@@ -8261,7 +8261,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 			unsigned int skip_pages;
 
 			if (!hugepage_migration_supported(page_hstate(head)))
-				goto unmovable;
+				return page;
 
 			skip_pages = compound_nr(head) - (page - head);
 			iter += skip_pages - 1;
@@ -8303,12 +8303,9 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		 * is set to both of a memory hole page and a _used_ kernel
 		 * page at boot.
 		 */
-		goto unmovable;
+		return page;
 	}
 	return NULL;
-unmovable:
-	WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
-	return pfn_to_page(pfn + iter);
 }
 
 #ifdef CONFIG_CONTIG_ALLOC
mm/page_isolation.c +0 -0
index e70586523ca3..31f5516f5d54 100644

mm/page_isolation.c line 19

Quoted 6 lines
mm/page_isolation.c +2 -1
@@ -15,12 +15,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/page_isolation.h>
 
-static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
+static bool set_migratetype_isolate(struct page *page, int migratetype,
+				    int isol_flags)

Why this change?

mm/page_isolation.c line 21

Quoted 3 lines
mm/page_isolation.c +1 -1
 {
-	struct page *unmovable = NULL;
+	struct page *unmovable = ERR_PTR(-EBUSY);

Also, why this change?

Quoted 22 lines
mm/page_isolation.c +2 -6
 	struct zone *zone;
 	unsigned long flags;
-	int ret = -EBUSY;
 
 	zone = page_zone(page);
 
@@ -49,21 +49,25 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol_
 									NULL);
 
 		__mod_zone_freepage_state(zone, -nr_pages, mt);
-		ret = 0;
 	}
 
 out:
 	spin_unlock_irqrestore(&zone->lock, flags);
-	if (!ret)
+
+	if (!unmovable) {
 		drain_all_pages(zone);
-	else if ((isol_flags & REPORT_FAILURE) && unmovable)
-		/*
-		 * printk() with zone->lock held will guarantee to trigger a

mm/page_isolation.c static int set_migratetype_isolate(struct page *page, int migratetype, int isol_ · line 63

Quoted 10 lines
mm/page_isolation.c +5 -5
-		 * lockdep splat, so defer it here.
-		 */
-		dump_page(unmovable, "unmovable page");
-
-	return ret;
+	} else {
+		WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
+
+		if ((isol_flags & REPORT_FAILURE) && !IS_ERR(unmovable))
+			/*

Why this change? (!IS_ERR)

Some things here look unrelated - or I am missing something :)

Signature · 3 lines
Thanks,

David / dhildenb
author
#2 lore raw
Quoted 126 lines · David Hildenbrand
Quoted 70 lines · Qian Cai

It makes sense to call the WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE) from start_isolate_page_range(), but should avoid triggering it from userspace, i.e, from is_mem_section_removable() because it could be a DoS if warn_on_panic is set.

While at it, simplify the code a bit by removing an unnecessary jump label and a local variable, so set_migratetype_isolate() could really return a bool.

---

v2: Improve the commit log.
Warn for all start_isolate_page_range() users not just offlining.

mm/page_alloc.c | 11 ++++-------
mm/page_isolation.c | 30 +++++++++++++++++-------------

2 files changed, 21 insertions(+), 20 deletions(-)
mm/page_alloc.c +4 -7
index 621716a25639..3c4eb750a199 100644
@@ -8231,7 +8231,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		if (is_migrate_cma(migratetype))
 			return NULL;
 
-		goto unmovable;
+		return page;
 	}
 
 	for (; iter < pageblock_nr_pages; iter++) {
@@ -8241,7 +8241,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		page = pfn_to_page(pfn + iter);
 
 		if (PageReserved(page))
-			goto unmovable;
+			return page;
 
 		/*
 		 * If the zone is movable and we have ruled out all reserved
@@ -8261,7 +8261,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 			unsigned int skip_pages;
 
 			if (!hugepage_migration_supported(page_hstate(head)))
-				goto unmovable;
+				return page;
 
 			skip_pages = compound_nr(head) - (page - head);
 			iter += skip_pages - 1;
@@ -8303,12 +8303,9 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		 * is set to both of a memory hole page and a _used_ kernel
 		 * page at boot.
 		 */
-		goto unmovable;
+		return page;
 	}
 	return NULL;
-unmovable:
-	WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
-	return pfn_to_page(pfn + iter);
 }
 
 #ifdef CONFIG_CONTIG_ALLOC
mm/page_isolation.c +0 -0
index e70586523ca3..31f5516f5d54 100644

mm/page_isolation.c line 19

Quoted 6 lines
mm/page_isolation.c +2 -1
@@ -15,12 +15,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/page_isolation.h>
 
-static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
+static bool set_migratetype_isolate(struct page *page, int migratetype,
+				    int isol_flags)

Why this change?

mm/page_isolation.c line 21

Quoted 3 lines
mm/page_isolation.c +1 -1
 {
-	struct page *unmovable = NULL;
+	struct page *unmovable = ERR_PTR(-EBUSY);

Also, why this change?

Quoted 22 lines
mm/page_isolation.c +0 -2
 	struct zone *zone;
 	unsigned long flags;
-	int ret = -EBUSY;
 
 	zone = page_zone(page);
 
@@ -49,21 +49,25 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol_
 									NULL);
 
 		__mod_zone_freepage_state(zone, -nr_pages, mt);
-		ret = 0;
 	}
 

out:

	spin_unlock_irqrestore(&zone->lock, flags);
mm/page_isolation.c +2 -1
-	if (!ret)
+
+	if (!unmovable) {
		drain_all_pages(zone);
mm/page_isolation.c +0 -3
-	else if ((isol_flags & REPORT_FAILURE) && unmovable)
-		/*
-		 * printk() with zone->lock held will guarantee to trigger a

mm/page_isolation.c static int set_migratetype_isolate(struct page *page, int migratetype, int isol_ · line 63

Quoted 10 lines
mm/page_isolation.c +5 -5
-		 * lockdep splat, so defer it here.
-		 */
-		dump_page(unmovable, "unmovable page");
-
-	return ret;
+	} else {
+		WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
+
+		if ((isol_flags & REPORT_FAILURE) && !IS_ERR(unmovable))
+			/*

Why this change? (!IS_ERR)

Quoted 1 line

Some things here look unrelated - or I am missing something :)

The original “ret” variable looks ugly to me, so I just removed that and consolidated with the “unmovable” pointer to always be able to report an error. Since this cleanup is really small, I did not bother send a separate patch for it.

#3 lore raw
In reply to David Hildenbrand
Quoted 122 lines · David Hildenbrand
Quoted 70 lines · Qian Cai

It makes sense to call the WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE) from start_isolate_page_range(), but should avoid triggering it from userspace, i.e, from is_mem_section_removable() because it could be a DoS if warn_on_panic is set.

While at it, simplify the code a bit by removing an unnecessary jump label and a local variable, so set_migratetype_isolate() could really return a bool.

---

v2: Improve the commit log.
Warn for all start_isolate_page_range() users not just offlining.

 mm/page_alloc.c     | 11 ++++-------
 mm/page_isolation.c | 30 +++++++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
mm/page_alloc.c +4 -7
index 621716a25639..3c4eb750a199 100644
@@ -8231,7 +8231,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		if (is_migrate_cma(migratetype))
 			return NULL;
 
-		goto unmovable;
+		return page;
 	}
 
 	for (; iter < pageblock_nr_pages; iter++) {
@@ -8241,7 +8241,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		page = pfn_to_page(pfn + iter);
 
 		if (PageReserved(page))
-			goto unmovable;
+			return page;
 
 		/*
 		 * If the zone is movable and we have ruled out all reserved
@@ -8261,7 +8261,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 			unsigned int skip_pages;
 
 			if (!hugepage_migration_supported(page_hstate(head)))
-				goto unmovable;
+				return page;
 
 			skip_pages = compound_nr(head) - (page - head);
 			iter += skip_pages - 1;
@@ -8303,12 +8303,9 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		 * is set to both of a memory hole page and a _used_ kernel
 		 * page at boot.
 		 */
-		goto unmovable;
+		return page;
 	}
 	return NULL;
-unmovable:
-	WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
-	return pfn_to_page(pfn + iter);
 }
 
 #ifdef CONFIG_CONTIG_ALLOC
mm/page_isolation.c +0 -0
index e70586523ca3..31f5516f5d54 100644

mm/page_isolation.c line 19

Quoted 6 lines
mm/page_isolation.c +2 -1
@@ -15,12 +15,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/page_isolation.h>
 
-static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
+static bool set_migratetype_isolate(struct page *page, int migratetype,
+				    int isol_flags)

Why this change?

mm/page_isolation.c line 21

Quoted 3 lines
mm/page_isolation.c +1 -1
 {
-	struct page *unmovable = NULL;
+	struct page *unmovable = ERR_PTR(-EBUSY);

Also, why this change?

Quoted 22 lines
mm/page_isolation.c +2 -6
 	struct zone *zone;
 	unsigned long flags;
-	int ret = -EBUSY;
 
 	zone = page_zone(page);
 
@@ -49,21 +49,25 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol_
 									NULL);
 
 		__mod_zone_freepage_state(zone, -nr_pages, mt);
-		ret = 0;
 	}
 
 out:
 	spin_unlock_irqrestore(&zone->lock, flags);
-	if (!ret)
+
+	if (!unmovable) {
 		drain_all_pages(zone);
-	else if ((isol_flags & REPORT_FAILURE) && unmovable)
-		/*
-		 * printk() with zone->lock held will guarantee to trigger a

mm/page_isolation.c static int set_migratetype_isolate(struct page *page, int migratetype, int isol_ · line 63

Quoted 10 lines
mm/page_isolation.c +5 -5
-		 * lockdep splat, so defer it here.
-		 */
-		dump_page(unmovable, "unmovable page");
-
-	return ret;
+	} else {
+		WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
+
+		if ((isol_flags & REPORT_FAILURE) && !IS_ERR(unmovable))
+			/*

Why this change? (!IS_ERR)

Quoted 2 lines

Some things here look unrelated - or I am missing something :)

FWIW, I'd prefer this change without any such cleanups (e.g., I don't like returning a bool from this function and the IS_ERR handling, makes the function harder to read than before)

Signature · 3 lines
Thanks,

David / dhildenb
author
#4 lore raw
Quoted 127 lines · David Hildenbrand
Quoted 122 lines · David Hildenbrand
Quoted 70 lines · Qian Cai

It makes sense to call the WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE) from start_isolate_page_range(), but should avoid triggering it from userspace, i.e, from is_mem_section_removable() because it could be a DoS if warn_on_panic is set.

While at it, simplify the code a bit by removing an unnecessary jump label and a local variable, so set_migratetype_isolate() could really return a bool.

---

v2: Improve the commit log.
Warn for all start_isolate_page_range() users not just offlining.

mm/page_alloc.c | 11 ++++-------
mm/page_isolation.c | 30 +++++++++++++++++-------------

2 files changed, 21 insertions(+), 20 deletions(-)
mm/page_alloc.c +4 -7
index 621716a25639..3c4eb750a199 100644
@@ -8231,7 +8231,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		if (is_migrate_cma(migratetype))
 			return NULL;
 
-		goto unmovable;
+		return page;
 	}
 
 	for (; iter < pageblock_nr_pages; iter++) {
@@ -8241,7 +8241,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		page = pfn_to_page(pfn + iter);
 
 		if (PageReserved(page))
-			goto unmovable;
+			return page;
 
 		/*
 		 * If the zone is movable and we have ruled out all reserved
@@ -8261,7 +8261,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 			unsigned int skip_pages;
 
 			if (!hugepage_migration_supported(page_hstate(head)))
-				goto unmovable;
+				return page;
 
 			skip_pages = compound_nr(head) - (page - head);
 			iter += skip_pages - 1;
@@ -8303,12 +8303,9 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		 * is set to both of a memory hole page and a _used_ kernel
 		 * page at boot.
 		 */
-		goto unmovable;
+		return page;
 	}
 	return NULL;
-unmovable:
-	WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
-	return pfn_to_page(pfn + iter);
 }
 
 #ifdef CONFIG_CONTIG_ALLOC
mm/page_isolation.c +0 -0
index e70586523ca3..31f5516f5d54 100644

mm/page_isolation.c line 19

Quoted 6 lines
mm/page_isolation.c +2 -1
@@ -15,12 +15,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/page_isolation.h>
 
-static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
+static bool set_migratetype_isolate(struct page *page, int migratetype,
+				    int isol_flags)

Why this change?

mm/page_isolation.c line 21

Quoted 3 lines
mm/page_isolation.c +1 -1
 {
-	struct page *unmovable = NULL;
+	struct page *unmovable = ERR_PTR(-EBUSY);

Also, why this change?

Quoted 22 lines
mm/page_isolation.c +0 -2
 	struct zone *zone;
 	unsigned long flags;
-	int ret = -EBUSY;
 
 	zone = page_zone(page);
 
@@ -49,21 +49,25 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol_
 									NULL);
 
 		__mod_zone_freepage_state(zone, -nr_pages, mt);
-		ret = 0;
 	}
 

out:

	spin_unlock_irqrestore(&zone->lock, flags);
mm/page_isolation.c +2 -1
-	if (!ret)
+
+	if (!unmovable) {
		drain_all_pages(zone);
mm/page_isolation.c +0 -3
-	else if ((isol_flags & REPORT_FAILURE) && unmovable)
-		/*
-		 * printk() with zone->lock held will guarantee to trigger a

mm/page_isolation.c static int set_migratetype_isolate(struct page *page, int migratetype, int isol_ · line 63

Quoted 10 lines
mm/page_isolation.c +5 -5
-		 * lockdep splat, so defer it here.
-		 */
-		dump_page(unmovable, "unmovable page");
-
-	return ret;
+	} else {
+		WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
+
+		if ((isol_flags & REPORT_FAILURE) && !IS_ERR(unmovable))
+			/*

Why this change? (!IS_ERR)

Quoted 6 lines
Quoted 2 lines

Some things here look unrelated - or I am missing something :)

FWIW, I'd prefer this change without any such cleanups (e.g., I don't like returning a bool from this function and the IS_ERR handling, makes the function harder to read than before)

What is Michal or Andrew’s opinion? BTW, a bonus point to return a bool is that it helps the code robustness in general, as UBSAN will be able to catch any abuse.

#5 lore raw
Quoted 130 lines · Qian Cai
Quoted 130 lines · David Hildenbrand
Quoted 124 lines · David Hildenbrand
Quoted 70 lines · Qian Cai

It makes sense to call the WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE) from start_isolate_page_range(), but should avoid triggering it from userspace, i.e, from is_mem_section_removable() because it could be a DoS if warn_on_panic is set.

While at it, simplify the code a bit by removing an unnecessary jump label and a local variable, so set_migratetype_isolate() could really return a bool.

---

v2: Improve the commit log.
Warn for all start_isolate_page_range() users not just offlining.

mm/page_alloc.c | 11 ++++-------
mm/page_isolation.c | 30 +++++++++++++++++-------------

2 files changed, 21 insertions(+), 20 deletions(-)
mm/page_alloc.c +4 -7
index 621716a25639..3c4eb750a199 100644
@@ -8231,7 +8231,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		if (is_migrate_cma(migratetype))
 			return NULL;
 
-		goto unmovable;
+		return page;
 	}
 
 	for (; iter < pageblock_nr_pages; iter++) {
@@ -8241,7 +8241,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		page = pfn_to_page(pfn + iter);
 
 		if (PageReserved(page))
-			goto unmovable;
+			return page;
 
 		/*
 		 * If the zone is movable and we have ruled out all reserved
@@ -8261,7 +8261,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 			unsigned int skip_pages;
 
 			if (!hugepage_migration_supported(page_hstate(head)))
-				goto unmovable;
+				return page;
 
 			skip_pages = compound_nr(head) - (page - head);
 			iter += skip_pages - 1;
@@ -8303,12 +8303,9 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		 * is set to both of a memory hole page and a _used_ kernel
 		 * page at boot.
 		 */
-		goto unmovable;
+		return page;
 	}
 	return NULL;
-unmovable:
-	WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
-	return pfn_to_page(pfn + iter);
 }
 
 #ifdef CONFIG_CONTIG_ALLOC
mm/page_isolation.c +0 -0
index e70586523ca3..31f5516f5d54 100644

mm/page_isolation.c line 19

Quoted 6 lines
mm/page_isolation.c +2 -1
@@ -15,12 +15,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/page_isolation.h>
 
-static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
+static bool set_migratetype_isolate(struct page *page, int migratetype,
+				    int isol_flags)

Why this change?

mm/page_isolation.c line 21

Quoted 3 lines
mm/page_isolation.c +1 -1
 {
-	struct page *unmovable = NULL;
+	struct page *unmovable = ERR_PTR(-EBUSY);

Also, why this change?

Quoted 22 lines
mm/page_isolation.c +0 -2
 	struct zone *zone;
 	unsigned long flags;
-	int ret = -EBUSY;
 
 	zone = page_zone(page);
 
@@ -49,21 +49,25 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol_
 									NULL);
 
 		__mod_zone_freepage_state(zone, -nr_pages, mt);
-		ret = 0;
 	}
 

out:

	spin_unlock_irqrestore(&zone->lock, flags);
mm/page_isolation.c +2 -1
-	if (!ret)
+
+	if (!unmovable) {
		drain_all_pages(zone);
mm/page_isolation.c +0 -3
-	else if ((isol_flags & REPORT_FAILURE) && unmovable)
-		/*
-		 * printk() with zone->lock held will guarantee to trigger a

mm/page_isolation.c static int set_migratetype_isolate(struct page *page, int migratetype, int isol_ · line 63

Quoted 10 lines
mm/page_isolation.c +5 -5
-		 * lockdep splat, so defer it here.
-		 */
-		dump_page(unmovable, "unmovable page");
-
-	return ret;
+	} else {
+		WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
+
+		if ((isol_flags & REPORT_FAILURE) && !IS_ERR(unmovable))
+			/*

Why this change? (!IS_ERR)

Some things here look unrelated - or I am missing something :)

Quoted 8 lines
Quoted 3 lines

FWIW, I'd prefer this change without any such cleanups (e.g., I don't like returning a bool from this function and the IS_ERR handling, makes the function harder to read than before)

What is Michal or Andrew’s opinion? BTW, a bonus point to return a bool is that it helps the code robustness in general, as UBSAN will be able to catch any abuse.

A return type of bool on a function that does not test a property ("has_...", "is"...") is IMHO confusing.

If we have an int, it is clear that "0" means "success". With a bool (true/false), it is not clear.

Signature · 3 lines
Thanks,

David / dhildenb
author
#6 lore raw
Quoted 139 lines · David Hildenbrand
Quoted 134 lines · Qian Cai
Quoted 133 lines · David Hildenbrand
Quoted 122 lines · David Hildenbrand
Quoted 70 lines · Qian Cai

It makes sense to call the WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE) from start_isolate_page_range(), but should avoid triggering it from userspace, i.e, from is_mem_section_removable() because it could be a DoS if warn_on_panic is set.

While at it, simplify the code a bit by removing an unnecessary jump label and a local variable, so set_migratetype_isolate() could really return a bool.

---

v2: Improve the commit log.
Warn for all start_isolate_page_range() users not just offlining.

mm/page_alloc.c | 11 ++++-------
mm/page_isolation.c | 30 +++++++++++++++++-------------

2 files changed, 21 insertions(+), 20 deletions(-)
mm/page_alloc.c +4 -7
index 621716a25639..3c4eb750a199 100644
@@ -8231,7 +8231,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		if (is_migrate_cma(migratetype))
 			return NULL;
 
-		goto unmovable;
+		return page;
 	}
 
 	for (; iter < pageblock_nr_pages; iter++) {
@@ -8241,7 +8241,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		page = pfn_to_page(pfn + iter);
 
 		if (PageReserved(page))
-			goto unmovable;
+			return page;
 
 		/*
 		 * If the zone is movable and we have ruled out all reserved
@@ -8261,7 +8261,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 			unsigned int skip_pages;
 
 			if (!hugepage_migration_supported(page_hstate(head)))
-				goto unmovable;
+				return page;
 
 			skip_pages = compound_nr(head) - (page - head);
 			iter += skip_pages - 1;
@@ -8303,12 +8303,9 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		 * is set to both of a memory hole page and a _used_ kernel
 		 * page at boot.
 		 */
-		goto unmovable;
+		return page;
 	}
 	return NULL;
-unmovable:
-	WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
-	return pfn_to_page(pfn + iter);
 }
 
 #ifdef CONFIG_CONTIG_ALLOC
mm/page_isolation.c +0 -0
index e70586523ca3..31f5516f5d54 100644

mm/page_isolation.c line 19

Quoted 6 lines
mm/page_isolation.c +2 -1
@@ -15,12 +15,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/page_isolation.h>
 
-static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
+static bool set_migratetype_isolate(struct page *page, int migratetype,
+				    int isol_flags)

Why this change?

mm/page_isolation.c line 21

Quoted 3 lines
mm/page_isolation.c +1 -1
 {
-	struct page *unmovable = NULL;
+	struct page *unmovable = ERR_PTR(-EBUSY);

Also, why this change?

Quoted 22 lines
mm/page_isolation.c +0 -2
 	struct zone *zone;
 	unsigned long flags;
-	int ret = -EBUSY;
 
 	zone = page_zone(page);
 
@@ -49,21 +49,25 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol_
 									NULL);
 
 		__mod_zone_freepage_state(zone, -nr_pages, mt);
-		ret = 0;
 	}
 

out:

	spin_unlock_irqrestore(&zone->lock, flags);
mm/page_isolation.c +2 -1
-	if (!ret)
+
+	if (!unmovable) {
		drain_all_pages(zone);
mm/page_isolation.c +0 -3
-	else if ((isol_flags & REPORT_FAILURE) && unmovable)
-		/*
-		 * printk() with zone->lock held will guarantee to trigger a

mm/page_isolation.c static int set_migratetype_isolate(struct page *page, int migratetype, int isol_ · line 63

Quoted 10 lines
mm/page_isolation.c +5 -5
-		 * lockdep splat, so defer it here.
-		 */
-		dump_page(unmovable, "unmovable page");
-
-	return ret;
+	} else {
+		WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
+
+		if ((isol_flags & REPORT_FAILURE) && !IS_ERR(unmovable))
+			/*

Why this change? (!IS_ERR)

Quoted 2 lines

Some things here look unrelated - or I am missing something :)

FWIW, I'd prefer this change without any such cleanups (e.g., I don't like returning a bool from this function and the IS_ERR handling, makes the function harder to read than before)

Quoted 7 lines
Quoted 4 lines

What is Michal or Andrew’s opinion? BTW, a bonus point to return a bool is that it helps the code robustness in general, as UBSAN will be able to catch any abuse.

A return type of bool on a function that does not test a property ("has_...", "is"...") is IMHO confusing.

That is fine. It could be renamed to set_migratetype_is_isolate() or is_set_migratetype_isolate() which seems pretty minor because we have no consistency in the naming of this in linux kernel at all, i.e., many existing bool function names without those test of properties.

Quoted 9 lines

If we have an int, it is clear that "0" means "success". With a bool (true/false), it is not clear.

Signature · 3 lines
Thanks,

David / dhildenb
#7 lore raw
Quoted 144 lines · Qian Cai
Quoted 144 lines · David Hildenbrand
Quoted 138 lines · Qian Cai
Quoted 127 lines · David Hildenbrand
Quoted 122 lines · David Hildenbrand
Quoted 70 lines · Qian Cai

It makes sense to call the WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE) from start_isolate_page_range(), but should avoid triggering it from userspace, i.e, from is_mem_section_removable() because it could be a DoS if warn_on_panic is set.

While at it, simplify the code a bit by removing an unnecessary jump label and a local variable, so set_migratetype_isolate() could really return a bool.

---

v2: Improve the commit log.
Warn for all start_isolate_page_range() users not just offlining.

mm/page_alloc.c | 11 ++++-------
mm/page_isolation.c | 30 +++++++++++++++++-------------

2 files changed, 21 insertions(+), 20 deletions(-)
mm/page_alloc.c +4 -7
index 621716a25639..3c4eb750a199 100644
@@ -8231,7 +8231,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		if (is_migrate_cma(migratetype))
 			return NULL;
 
-		goto unmovable;
+		return page;
 	}
 
 	for (; iter < pageblock_nr_pages; iter++) {
@@ -8241,7 +8241,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		page = pfn_to_page(pfn + iter);
 
 		if (PageReserved(page))
-			goto unmovable;
+			return page;
 
 		/*
 		 * If the zone is movable and we have ruled out all reserved
@@ -8261,7 +8261,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 			unsigned int skip_pages;
 
 			if (!hugepage_migration_supported(page_hstate(head)))
-				goto unmovable;
+				return page;
 
 			skip_pages = compound_nr(head) - (page - head);
 			iter += skip_pages - 1;
@@ -8303,12 +8303,9 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		 * is set to both of a memory hole page and a _used_ kernel
 		 * page at boot.
 		 */
-		goto unmovable;
+		return page;
 	}
 	return NULL;
-unmovable:
-	WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
-	return pfn_to_page(pfn + iter);
 }
 
 #ifdef CONFIG_CONTIG_ALLOC
mm/page_isolation.c +0 -0
index e70586523ca3..31f5516f5d54 100644

mm/page_isolation.c line 19

Quoted 6 lines
mm/page_isolation.c +2 -1
@@ -15,12 +15,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/page_isolation.h>
 
-static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
+static bool set_migratetype_isolate(struct page *page, int migratetype,
+				    int isol_flags)

Why this change?

mm/page_isolation.c line 21

Quoted 3 lines
mm/page_isolation.c +1 -1
 {
-	struct page *unmovable = NULL;
+	struct page *unmovable = ERR_PTR(-EBUSY);

Also, why this change?

Quoted 22 lines
mm/page_isolation.c +0 -2
 	struct zone *zone;
 	unsigned long flags;
-	int ret = -EBUSY;
 
 	zone = page_zone(page);
 
@@ -49,21 +49,25 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol_
 									NULL);
 
 		__mod_zone_freepage_state(zone, -nr_pages, mt);
-		ret = 0;
 	}
 

out:

	spin_unlock_irqrestore(&zone->lock, flags);
mm/page_isolation.c +2 -1
-	if (!ret)
+
+	if (!unmovable) {
		drain_all_pages(zone);
mm/page_isolation.c +0 -3
-	else if ((isol_flags & REPORT_FAILURE) && unmovable)
-		/*
-		 * printk() with zone->lock held will guarantee to trigger a

mm/page_isolation.c static int set_migratetype_isolate(struct page *page, int migratetype, int isol_ · line 63

Quoted 10 lines
mm/page_isolation.c +5 -5
-		 * lockdep splat, so defer it here.
-		 */
-		dump_page(unmovable, "unmovable page");
-
-	return ret;
+	} else {
+		WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
+
+		if ((isol_flags & REPORT_FAILURE) && !IS_ERR(unmovable))
+			/*

Why this change? (!IS_ERR)

Quoted 6 lines
Quoted 2 lines

Some things here look unrelated - or I am missing something :)

FWIW, I'd prefer this change without any such cleanups (e.g., I don't like returning a bool from this function and the IS_ERR handling, makes the function harder to read than before)

What is Michal or Andrew’s opinion? BTW, a bonus point to return a bool is that it helps the code robustness in general, as UBSAN will be able to catch any abuse.

Quoted 7 lines
Quoted 2 lines

A return type of bool on a function that does not test a property ("has_...", "is"...") is IMHO confusing.

That is fine. It could be renamed to set_migratetype_is_isolate() or is_set_migratetype_isolate() which seems pretty minor because we have no consistency in the naming of this in linux kernel at all, i.e., many existing bool function names without those test of properties.

It does not query a property, so "is_set_migratetype_isolate()" is plain wrong.

Anyhow, Michal does not seem to care.

Signature · 3 lines
Thanks,

David / dhildenb
#8 lore raw
Quoted 3 lines · David Hildenbrand
Quoted 2 lines · Qian Cai
Quoted 2 lines · David Hildenbrand

On 20.01.20 14:56, Qian Cai wrote:

[...]

Quoted 17 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

FWIW, I'd prefer this change without any such cleanups (e.g., I don't like returning a bool from this function and the IS_ERR handling, makes the function harder to read than before)

What is Michal or Andrew’s opinion? BTW, a bonus point to return a bool is that it helps the code robustness in general, as UBSAN will be able to catch any abuse.

Quoted 7 lines
Quoted 2 lines

A return type of bool on a function that does not test a property ("has_...", "is"...") is IMHO confusing.

That is fine. It could be renamed to set_migratetype_is_isolate() or is_set_migratetype_isolate() which seems pretty minor because we have no consistency in the naming of this in linux kernel at all, i.e., many existing bool function names without those test of properties.

Quoted 4 lines

It does not query a property, so "is_set_migratetype_isolate()" is plain wrong.

Anyhow, Michal does not seem to care.

Well, TBH I have missed this change. My bad. I have mostly checked that the WARN_ONCE is not gated by the check and didn't expect more changes. But I have likely missed that change in the previous version already. You guys are too quick with new version to my standard.

Anyway, I do agree that bool is clumsy here. Returning false on success is just head scratching. Nobody really consumes the errno value but I would just leave it that way or if there is a strong need to change then do it in a separate patch.

Signature · 2 lines
Michal Hocko
SUSE Labs
Acked-by on v2 1/1
#9 lore raw
In reply to Qian Cai
Quoted 4 lines · Qian Cai

It makes sense to call the WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE) from start_isolate_page_range(), but should avoid triggering it from userspace, i.e, from is_mem_section_removable() because it could be a DoS if warn_on_panic is set.

Let's just make it clear that this mostly a pre-cautious because a real DoS should be pretty much impossible. But let's see whether somebody want to make a CVE out of it ;)

Quoted 6 lines

While at it, simplify the code a bit by removing an unnecessary jump label and a local variable, so set_migratetype_isolate() could really return a bool.

Thanks!

Quoted 112 lines

---

v2: Improve the commit log.
Warn for all start_isolate_page_range() users not just offlining.

 mm/page_alloc.c     | 11 ++++-------
 mm/page_isolation.c | 30 +++++++++++++++++-------------
 2 files changed, 21 insertions(+), 20 deletions(-)
mm/page_alloc.c +4 -7
index 621716a25639..3c4eb750a199 100644
@@ -8231,7 +8231,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		if (is_migrate_cma(migratetype))
 			return NULL;
 
-		goto unmovable;
+		return page;
 	}
 
 	for (; iter < pageblock_nr_pages; iter++) {
@@ -8241,7 +8241,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		page = pfn_to_page(pfn + iter);
 
 		if (PageReserved(page))
-			goto unmovable;
+			return page;
 
 		/*
 		 * If the zone is movable and we have ruled out all reserved
@@ -8261,7 +8261,7 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 			unsigned int skip_pages;
 
 			if (!hugepage_migration_supported(page_hstate(head)))
-				goto unmovable;
+				return page;
 
 			skip_pages = compound_nr(head) - (page - head);
 			iter += skip_pages - 1;
@@ -8303,12 +8303,9 @@ struct page *has_unmovable_pages(struct zone *zone, struct page *page,
 		 * is set to both of a memory hole page and a _used_ kernel
 		 * page at boot.
 		 */
-		goto unmovable;
+		return page;
 	}
 	return NULL;
-unmovable:
-	WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
-	return pfn_to_page(pfn + iter);
 }
 
 #ifdef CONFIG_CONTIG_ALLOC
mm/page_isolation.c +17 -13
index e70586523ca3..31f5516f5d54 100644
@@ -15,12 +15,12 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/page_isolation.h>
 
-static int set_migratetype_isolate(struct page *page, int migratetype, int isol_flags)
+static bool set_migratetype_isolate(struct page *page, int migratetype,
+				    int isol_flags)
 {
-	struct page *unmovable = NULL;
+	struct page *unmovable = ERR_PTR(-EBUSY);
 	struct zone *zone;
 	unsigned long flags;
-	int ret = -EBUSY;
 
 	zone = page_zone(page);
 
@@ -49,21 +49,25 @@ static int set_migratetype_isolate(struct page *page, int migratetype, int isol_
 									NULL);
 
 		__mod_zone_freepage_state(zone, -nr_pages, mt);
-		ret = 0;
 	}
 
 out:
 	spin_unlock_irqrestore(&zone->lock, flags);
-	if (!ret)
+
+	if (!unmovable) {
 		drain_all_pages(zone);
-	else if ((isol_flags & REPORT_FAILURE) && unmovable)
-		/*
-		 * printk() with zone->lock held will guarantee to trigger a
-		 * lockdep splat, so defer it here.
-		 */
-		dump_page(unmovable, "unmovable page");
-
-	return ret;
+	} else {
+		WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE);
+
+		if ((isol_flags & REPORT_FAILURE) && !IS_ERR(unmovable))
+			/*
+			 * printk() with zone->lock held will likely trigger a
+			 * lockdep splat, so defer it here.
+			 */
+			dump_page(unmovable, "unmovable page");
+	}
+
+	return !!unmovable;
 }
 
 static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
Signature · 1 line
2.21.0 (Apple Git-122.2)
Signature · 2 lines
Michal Hocko
SUSE Labs