All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC V2] mm: add the zero case to page[1].compound_nr in set_compound_order
@ 2022-12-13 23:45 Nico Pache
  2022-12-13 23:47 ` Mike Kravetz
                   ` (4 more replies)
  0 siblings, 5 replies; 15+ messages in thread
From: Nico Pache @ 2022-12-13 23:45 UTC (permalink / raw)
  To: linux-kernel, linux-mm
  Cc: muchun.song, mike.kravetz, akpm, willy, gerald.schaefer

Since commit 1378a5ee451a ("mm: store compound_nr as well as
compound_order") the page[1].compound_nr must be explicitly set to 0 if
calling set_compound_order(page, 0).

This can lead to bugs if the caller of set_compound_order(page, 0) forgets
to explicitly set compound_nr=0. An example of this is commit ba9c1201beaa
("mm/hugetlb: clear compound_nr before freeing gigantic pages")

Collapse these calls into the set_compound_order by utilizing branchless
bitmaths [1].

[1] https://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching

V2: slight changes to commit log and remove extra '//' in the comments

Signed-off-by: Nico Pache <npache@redhat.com>
---
 include/linux/mm.h | 6 +++++-
 mm/hugetlb.c       | 6 ------
 2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 6a05a3bc0a28..9510f6294706 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -938,9 +938,13 @@ static inline int head_compound_pincount(struct page *head)
 
 static inline void set_compound_order(struct page *page, unsigned int order)
 {
+	unsigned long shift = (1U << order);
 	page[1].compound_order = order;
 #ifdef CONFIG_64BIT
-	page[1].compound_nr = 1U << order;
+	// Branchless conditional:
+	// order  > 0 --> compound_nr = shift
+	// order == 0 --> compound_nr = 0
+	page[1].compound_nr = shift ^ (-order  ^ shift) & shift;
 #endif
 }
 
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 3d9f4abec17c..706dec43a6a2 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1344,9 +1344,6 @@ static void __destroy_compound_gigantic_page(struct page *page,
 	}
 
 	set_compound_order(page, 0);
-#ifdef CONFIG_64BIT
-	page[1].compound_nr = 0;
-#endif
 	__ClearPageHead(page);
 }
 
@@ -1865,9 +1862,6 @@ static bool __prep_compound_gigantic_page(struct page *page, unsigned int order,
 		__ClearPageReserved(p);
 	}
 	set_compound_order(page, 0);
-#ifdef CONFIG_64BIT
-	page[1].compound_nr = 0;
-#endif
 	__ClearPageHead(page);
 	return false;
 }
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2022-12-16  6:47 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-13 23:45 [RFC V2] mm: add the zero case to page[1].compound_nr in set_compound_order Nico Pache
2022-12-13 23:47 ` Mike Kravetz
2022-12-13 23:53   ` Nico Pache
2022-12-14  0:27     ` Nico Pache
2022-12-14  1:02       ` Mike Kravetz
2022-12-14  6:38         ` Sidhartha Kumar
2022-12-15  1:05           ` Nico Pache
2022-12-14  1:43 ` kernel test robot
2022-12-14  5:25 ` kernel test robot
2022-12-14 17:04 ` Matthew Wilcox
2022-12-15  2:48   ` Nico Pache
2022-12-15 21:38     ` Nico Pache
2022-12-15 21:47       ` Matthew Wilcox
2022-12-15 22:02         ` Nico Pache
2022-12-16  6:46 ` kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.