Skip to contentSkip to conversation

mm: page_alloc: simplify pageblock migratetype check in __free_one_page().

PATCH Superseded Zi Yan 5 participants 13 messages Apr 1, 2022 – Apr 4, 2022

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Zi Yan

author
#0 lore raw

PATCH 1/2 mm: page_alloc: simplify pageblock migratetype check in __free_one_page().

From: Zi Yan <[email protected]>

Move pageblock migratetype check code in the while loop to simplify the logic. It also saves redundant buddy page checking code.

---
 mm/page_alloc.c | 46 +++++++++++++++++-----------------------------
 1 file changed, 17 insertions(+), 29 deletions(-)
mm/page_alloc.c +17 -29
index 856473e54155..2ea106146686 100644
@@ -1054,7 +1054,6 @@ static inline void __free_one_page(struct page *page,
 		int migratetype, fpi_t fpi_flags)
 {
 	struct capture_control *capc = task_capc(zone);
-	unsigned int max_order = pageblock_order;
 	unsigned long buddy_pfn;
 	unsigned long combined_pfn;
 	struct page *buddy;
@@ -1070,8 +1069,7 @@ static inline void __free_one_page(struct page *page,
 	VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page);
 	VM_BUG_ON_PAGE(bad_range(zone, page), page);
 
-continue_merging:
-	while (order < max_order) {
+	while (order < MAX_ORDER - 1) {
 		if (compaction_capture(capc, page, order, migratetype)) {
 			__mod_zone_freepage_state(zone, -(1 << order),
 								migratetype);
@@ -1082,6 +1080,22 @@ static inline void __free_one_page(struct page *page,
 
 		if (!page_is_buddy(page, buddy, order))
 			goto done_merging;
+
+		if (unlikely(order >= pageblock_order)) {
+			/*
+			 * We want to prevent merge between freepages on pageblock
+			 * without fallbacks and normal pageblock. Without this,
+			 * pageblock isolation could cause incorrect freepage or CMA
+			 * accounting or HIGHATOMIC accounting.
+			 */
+			int buddy_mt = get_pageblock_migratetype(buddy);
+
+			if (migratetype != buddy_mt
+					&& (!migratetype_is_mergeable(migratetype) ||
+						!migratetype_is_mergeable(buddy_mt)))
+				goto done_merging;
+		}
+
 		/*
 		 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
 		 * merge with it and move up one order.
@@ -1095,32 +1109,6 @@ static inline void __free_one_page(struct page *page,
 		pfn = combined_pfn;
 		order++;
 	}
-	if (order < MAX_ORDER - 1) {
-		/* If we are here, it means order is >= pageblock_order.
-		 * We want to prevent merge between freepages on pageblock
-		 * without fallbacks and normal pageblock. Without this,
-		 * pageblock isolation could cause incorrect freepage or CMA
-		 * accounting or HIGHATOMIC accounting.
-		 *
-		 * We don't want to hit this code for the more frequent
-		 * low-order merging.
-		 */
-		int buddy_mt;
-
-		buddy_pfn = __find_buddy_pfn(pfn, order);
-		buddy = page + (buddy_pfn - pfn);
-
-		if (!page_is_buddy(page, buddy, order))
-			goto done_merging;
-		buddy_mt = get_pageblock_migratetype(buddy);
-
-		if (migratetype != buddy_mt
-				&& (!migratetype_is_mergeable(migratetype) ||
-					!migratetype_is_mergeable(buddy_mt)))
-			goto done_merging;
-		max_order = order + 1;
-		goto continue_merging;
-	}
 
 done_merging:
 	set_buddy_order(page, order);
Signature · 1 line
2.35.1
author
#1 lore raw

PATCH 2/2 mm: wrap __find_buddy_pfn() with a necessary buddy page validation.

From: Zi Yan <[email protected]>

Whenever the buddy of a page is found from __find_buddy_pfn(), page_is_buddy() should be used to check its validity. Add a helper function find_buddy_page_pfn() to find the buddy page and do the check together.

---
 mm/internal.h       | 24 ++---------------
 mm/page_alloc.c     | 63 ++++++++++++++++++++++++++++++++++++++-------
 mm/page_isolation.c |  9 +++----
 3 files changed, 59 insertions(+), 37 deletions(-)
mm/internal.h +2 -22
index 876e66237c89..791653c95bf1 100644
@@ -211,28 +211,8 @@ struct alloc_context {
 	bool spread_dirty_pages;
 };
 
-/*
- * Locate the struct page for both the matching buddy in our
- * pair (buddy1) and the combined O(n+1) page they form (page).
- *
- * 1) Any buddy B1 will have an order O twin B2 which satisfies
- * the following equation:
- *     B2 = B1 ^ (1 << O)
- * For example, if the starting buddy (buddy2) is #8 its order
- * 1 buddy is #10:
- *     B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
- *
- * 2) Any buddy B will have an order O+1 parent P which
- * satisfies the following equation:
- *     P = B & ~(1 << O)
- *
- * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
- */
-static inline unsigned long
-__find_buddy_pfn(unsigned long page_pfn, unsigned int order)
-{
-	return page_pfn ^ (1 << order);
-}
+extern bool find_buddy_page_pfn(struct page *page, unsigned int order,
+				struct page **buddy, unsigned long *buddy_pfn);
 
 extern struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
 				unsigned long end_pfn, struct zone *zone);
mm/page_alloc.c +54 -9

87 lines not on this page · raw message

mm/page_isolation.c +3 -6
index f67c4c70f17f..743c52f489f5 100644
@@ -70,7 +70,7 @@ static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
 	unsigned long flags, nr_pages;
 	bool isolated_page = false;
 	unsigned int order;
-	unsigned long pfn, buddy_pfn;
+	unsigned long buddy_pfn;
 	struct page *buddy;
 
 	zone = page_zone(page);
@@ -89,11 +89,8 @@ static void unset_migratetype_isolate(struct page *page, unsigned migratetype)
 	if (PageBuddy(page)) {
 		order = buddy_order(page);
 		if (order >= pageblock_order && order < MAX_ORDER - 1) {
-			pfn = page_to_pfn(page);
-			buddy_pfn = __find_buddy_pfn(pfn, order);
-			buddy = page + (buddy_pfn - pfn);
-
-			if (!is_migrate_isolate_page(buddy)) {
+			if (find_buddy_page_pfn(page, order, &buddy, &buddy_pfn) &&
+			    !is_migrate_isolate_page(buddy)) {
 				isolated_page = !!__isolate_free_page(page, order);
 				/*
 				 * Isolating a free page in an isolated pageblock
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH 2/2] mm: wrap __find_buddy_pfn() with a necessary buddy page validation.

mm/internal.h struct alloc_context { · line 215

Quoted 3 lines · Zi Yan
mm/internal.h +2 -0
+extern bool find_buddy_page_pfn(struct page *page, unsigned int order,
+                               struct page **buddy, unsigned long *buddy_pfn);

Wouldn't it make more sense to just return the 'struct page *buddy' here, instead of the 'bool'?

So a NULL buddy means the obvious "no buddy found".

I dislike those "pass return value by reference" in general, and the above has _two_ of them.

We can get rid of at least one very obviously.

Linus

reviewer
#3 lore raw

Re: [PATCH 2/2] mm: wrap __find_buddy_pfn() with a necessary buddy page validation.

Quoted 8 lines · Linus Torvalds

mm/internal.h struct alloc_context { · line 215

Quoted 3 lines · Zi Yan
mm/internal.h +2 -0
+extern bool find_buddy_page_pfn(struct page *page, unsigned int order,
+                               struct page **buddy, unsigned long *buddy_pfn);

Wouldn't it make more sense to just return the 'struct page *buddy' here, instead of the 'bool'?

Quoted 6 lines

So a NULL buddy means the obvious "no buddy found".

I dislike those "pass return value by reference" in general, and the above has _two_ of them.

We can get rid of at least one very obviously.

Sure. We can get rid of both and calculate buddy_pfn from buddy after a successful call.

--
Best Regards,
Yan, Zi

#4 lore raw
In reply to Zi Yan

On 01.04.22 15:58, Zi Yan wrote:

It's weird, your mails arrive on my end as empty body with attachment. I first suspected Thunderbird, but I get the same result on the google mail web client.

Not sure why that happens.

Signature · 3 lines
Thanks,

David / dhildenb
reviewer
#5 lore raw
Quoted 7 lines · David Hildenbrand

On 01.04.22 15:58, Zi Yan wrote:

It's weird, your mails arrive on my end as empty body with attachment. I first suspected Thunderbird, but I get the same result on the google mail web client.

Not sure why that happens.

No idea. They look fine (except mangled links by outlook) on my outlook desk client and web client on my side. lore looks OK too:
https://lore.kernel.org/linux-mm/[email protected]/

--
Best Regards,
Yan, Zi

#6 lore raw
Quoted 8 lines · Zi Yan
Quoted 6 lines · David Hildenbrand

On 01.04.22 15:58, Zi Yan wrote:

It's weird, your mails arrive on my end as empty body with attachment. I first suspected Thunderbird, but I get the same result on the google mail web client.

Quoted 5 lines
Quoted 1 line

Not sure why that happens.

No idea. They look fine (except mangled links by outlook) on my outlook desk client and web client on my side. lore looks OK too:
https://lore.kernel.org/linux-mm/[email protected]/

I can spot in the raw mail I receive

"Content-Type: application/octet-stream; x-default=true"

But that seems to differ to the lore mail:

https://lore.kernel.org/linux-mm/[email protected]/raw

Maybe something in my mail server chain decides to do some nasty conversion (grml, wouldn't be the first time)

Signature · 3 lines
Thanks,

David / dhildenb
#7 lore raw
Quoted 19 lines · David Hildenbrand
Quoted 8 lines · Zi Yan
Quoted 6 lines · David Hildenbrand

On 01.04.22 15:58, Zi Yan wrote:

It's weird, your mails arrive on my end as empty body with attachment. I first suspected Thunderbird, but I get the same result on the google mail web client.

Quoted 5 lines
Quoted 1 line

Not sure why that happens.

No idea. They look fine (except mangled links by outlook) on my outlook desk client and web client on my side. lore looks OK too:
https://lore.kernel.org/linux-mm/[email protected]/

I can spot in the raw mail I receive

"Content-Type: application/octet-stream; x-default=true"

Quoted 8 lines

But that seems to differ to the lore mail:

https://lore.kernel.org/linux-mm/[email protected]/raw

Maybe something in my mail server chain decides to do some nasty conversion (grml, wouldn't be the first time)

Weird thing is that this only happens with your mails. I opened an internal ticket, sorry for the noise.

Signature · 3 lines
Thanks,

David / dhildenb
#8 lore raw
Quoted 25 lines · David Hildenbrand
Quoted 24 lines · David Hildenbrand
Quoted 8 lines · Zi Yan
Quoted 6 lines · David Hildenbrand

On 01.04.22 15:58, Zi Yan wrote:

It's weird, your mails arrive on my end as empty body with attachment. I first suspected Thunderbird, but I get the same result on the google mail web client.

Quoted 5 lines
Quoted 1 line

Not sure why that happens.

No idea. They look fine (except mangled links by outlook) on my outlook desk client and web client on my side. lore looks OK too:
https://lore.kernel.org/linux-mm/[email protected]/

I can spot in the raw mail I receive

"Content-Type: application/octet-stream; x-default=true"

But that seems to differ to the lore mail:

https://lore.kernel.org/linux-mm/[email protected]/raw

Quoted 6 lines
Quoted 3 lines

Maybe something in my mail server chain decides to do some nasty conversion (grml, wouldn't be the first time)

Weird thing is that this only happens with your mails. I opened an internal ticket, sorry for the noise.

Zi's patch emails I received didn't have Content-Type, that might have something to do with this. (But his reply later in the thread did have one.) Also last week I got one patch email with no Content-Type either and my Gnus decided to convert it to octet-stream, I guess to be on the safe side. No idea if something similar is happening to you, but wanted to mention it anyway.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#9 lore raw
Quoted 34 lines · Kalle Valo
Quoted 25 lines · David Hildenbrand
Quoted 24 lines · David Hildenbrand
Quoted 8 lines · Zi Yan
Quoted 6 lines · David Hildenbrand

On 01.04.22 15:58, Zi Yan wrote:

It's weird, your mails arrive on my end as empty body with attachment. I first suspected Thunderbird, but I get the same result on the google mail web client.

Quoted 5 lines
Quoted 1 line

Not sure why that happens.

No idea. They look fine (except mangled links by outlook) on my outlook desk client and web client on my side. lore looks OK too:
https://lore.kernel.org/linux-mm/[email protected]/

I can spot in the raw mail I receive

"Content-Type: application/octet-stream; x-default=true"

But that seems to differ to the lore mail:

https://lore.kernel.org/linux-mm/[email protected]/raw

Quoted 6 lines
Quoted 3 lines

Maybe something in my mail server chain decides to do some nasty conversion (grml, wouldn't be the first time)

Weird thing is that this only happens with your mails. I opened an internal ticket, sorry for the noise.

Quoted 6 lines

Zi's patch emails I received didn't have Content-Type, that might have something to do with this. (But his reply later in the thread did have one.) Also last week I got one patch email with no Content-Type either and my Gnus decided to convert it to octet-stream, I guess to be on the safe side. No idea if something similar is happening to you, but wanted to mention it anyway.

Just to clarify, I assumed Gnus was doing the conversion to octet-stream but I never verified that.

Heh, interestingly enough that patch was sent from redhat.com:

https://lore.kernel.org/all/[email protected]/

Is that just a coincidence or are Redhat servers doing something strange? If you find out, do let me know. I'm very curious :)

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
reviewer
#10 lore raw
Quoted 36 lines · Kalle Valo
Quoted 34 lines · Kalle Valo
Quoted 31 lines · David Hildenbrand
Quoted 19 lines · David Hildenbrand
Quoted 8 lines · Zi Yan
Quoted 6 lines · David Hildenbrand

On 01.04.22 15:58, Zi Yan wrote:

It's weird, your mails arrive on my end as empty body with attachment. I first suspected Thunderbird, but I get the same result on the google mail web client.

Quoted 5 lines
Quoted 1 line

Not sure why that happens.

No idea. They look fine (except mangled links by outlook) on my outlook desk client and web client on my side. lore looks OK too:
https://lore.kernel.org/linux-mm/[email protected]/

I can spot in the raw mail I receive

"Content-Type: application/octet-stream; x-default=true"

Quoted 8 lines

But that seems to differ to the lore mail:

https://lore.kernel.org/linux-mm/[email protected]/raw

Maybe something in my mail server chain decides to do some nasty conversion (grml, wouldn't be the first time)

Weird thing is that this only happens with your mails. I opened an internal ticket, sorry for the noise.

Quoted 6 lines
Quoted 6 lines

Zi's patch emails I received didn't have Content-Type, that might have something to do with this. (But his reply later in the thread did have one.) Also last week I got one patch email with no Content-Type either and my Gnus decided to convert it to octet-stream, I guess to be on the safe side. No idea if something similar is happening to you, but wanted to mention it anyway.

The emails I got from linux-mm mailing list do not have Content-Type either, but the ones I got directly from my git-send-email have it. David is in the cc list, so the emails sent to him should have Content-Type.

FYI, I sent the emails using git 2.35.1 via fastmail.com and have transferEncoding=quoted-printable.

Quoted 10 lines

Just to clarify, I assumed Gnus was doing the conversion to octet-stream but I never verified that.

Heh, interestingly enough that patch was sent from redhat.com:

https://lore.kernel.org/all/[email protected]/

Is that just a coincidence or are Redhat servers doing something strange? If you find out, do let me know. I'm very curious :)

--
Best Regards,
Yan, Zi

#11 lore raw
Quoted 45 lines · Zi Yan
Quoted 36 lines · Kalle Valo
Quoted 34 lines · Kalle Valo
Quoted 31 lines · David Hildenbrand
Quoted 19 lines · David Hildenbrand
Quoted 8 lines · Zi Yan
Quoted 6 lines · David Hildenbrand

On 01.04.22 15:58, Zi Yan wrote:

It's weird, your mails arrive on my end as empty body with attachment. I first suspected Thunderbird, but I get the same result on the google mail web client.

Quoted 5 lines
Quoted 1 line

Not sure why that happens.

No idea. They look fine (except mangled links by outlook) on my outlook desk client and web client on my side. lore looks OK too:
https://lore.kernel.org/linux-mm/[email protected]/

I can spot in the raw mail I receive

"Content-Type: application/octet-stream; x-default=true"

Quoted 8 lines

But that seems to differ to the lore mail:

https://lore.kernel.org/linux-mm/[email protected]/raw

Maybe something in my mail server chain decides to do some nasty conversion (grml, wouldn't be the first time)

Weird thing is that this only happens with your mails. I opened an internal ticket, sorry for the noise.

Quoted 6 lines
Quoted 6 lines

Zi's patch emails I received didn't have Content-Type, that might have something to do with this. (But his reply later in the thread did have one.) Also last week I got one patch email with no Content-Type either and my Gnus decided to convert it to octet-stream, I guess to be on the safe side. No idea if something similar is happening to you, but wanted to mention it anyway.

Quoted 3 lines

The emails I got from linux-mm mailing list do not have Content-Type either, but the ones I got directly from my git-send-email have it. David is in the cc list, so the emails sent to him should have Content-Type.

I assume I received them without a Content-type, or mail servers stripped them, and Mimecast servers (which RH uses) fail to auto-detect and add a wrong Content-type. Wouldn't be the first time that Mimecast messed with mail encodings etc, unfortunately.

Signature · 3 lines
Thanks,

David / dhildenb
#12 lore raw
In reply to Kalle Valo
Quoted 46 lines · Kalle Valo
Quoted 34 lines · Kalle Valo
Quoted 25 lines · David Hildenbrand
Quoted 24 lines · David Hildenbrand
Quoted 8 lines · Zi Yan
Quoted 6 lines · David Hildenbrand

On 01.04.22 15:58, Zi Yan wrote:

It's weird, your mails arrive on my end as empty body with attachment. I first suspected Thunderbird, but I get the same result on the google mail web client.

Quoted 5 lines
Quoted 1 line

Not sure why that happens.

No idea. They look fine (except mangled links by outlook) on my outlook desk client and web client on my side. lore looks OK too:
https://lore.kernel.org/linux-mm/[email protected]/

I can spot in the raw mail I receive

"Content-Type: application/octet-stream; x-default=true"

But that seems to differ to the lore mail:

https://lore.kernel.org/linux-mm/[email protected]/raw

Quoted 6 lines
Quoted 3 lines

Maybe something in my mail server chain decides to do some nasty conversion (grml, wouldn't be the first time)

Weird thing is that this only happens with your mails. I opened an internal ticket, sorry for the noise.

Quoted 6 lines

Zi's patch emails I received didn't have Content-Type, that might have something to do with this. (But his reply later in the thread did have one.) Also last week I got one patch email with no Content-Type either and my Gnus decided to convert it to octet-stream, I guess to be on the safe side. No idea if something similar is happening to you, but wanted to mention it anyway.

Just to clarify, I assumed Gnus was doing the conversion to octet-stream but I never verified that.

Quoted 7 lines

Heh, interestingly enough that patch was sent from redhat.com:

https://lore.kernel.org/all/[email protected]/

Is that just a coincidence or are Redhat servers doing something strange? If you find out, do let me know. I'm very curious :)

It is strange. Also mails from Andrew result in the same, unusable/unreadable mails in my inbox. :(

Right now I assume that Mimecast servers try to auto-detecting and adding "Content-type:", and somehow mess up. And I do think that it doesn't always mess up; some patch content (encoding?) seems to trigger that.

Signature · 3 lines
Thanks,

David / dhildenb