[PATCHv6 17/17] mm/slab: Use compound_head() in page_slab()

Kiryl Shutsemau posted 17 patches 4 days, 15 hours ago
[PATCHv6 17/17] mm/slab: Use compound_head() in page_slab()
Posted by Kiryl Shutsemau 4 days, 15 hours ago
page_slab() contained an open-coded implementation of compound_head().

Replace the duplicated code with a direct call to compound_head().

Signed-off-by: Kiryl Shutsemau <kas@kernel.org>

---

I am not sure if this open-coded version is intentional and required for
memdesc transition. Drop the patch if it is.
---
 mm/slab.h | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)

diff --git a/mm/slab.h b/mm/slab.h
index f68c3ac8126f..970a13ac5b8e 100644
--- a/mm/slab.h
+++ b/mm/slab.h
@@ -137,19 +137,7 @@ static_assert(IS_ALIGNED(offsetof(struct slab, freelist), sizeof(struct freelist
  */
 static inline struct slab *page_slab(const struct page *page)
 {
-	unsigned long info;
-
-	info = READ_ONCE(page->compound_info);
-	if (info & 1) {
-		/* See compound_head() */
-		if (compound_info_has_mask()) {
-			unsigned long p = (unsigned long)page;
-			page = (struct page *)(p & info);
-		} else {
-			page = (struct page *)(info - 1);
-		}
-	}
-
+	page = compound_head(page);
 	if (data_race(page->page_type >> 24) != PGTY_slab)
 		page = NULL;
 
-- 
2.51.2
Re: [PATCHv6 17/17] mm/slab: Use compound_head() in page_slab()
Posted by David Hildenbrand (Arm) 20 hours ago
On 2/2/26 16:56, Kiryl Shutsemau wrote:
> page_slab() contained an open-coded implementation of compound_head().
> 
> Replace the duplicated code with a direct call to compound_head().
> 
> Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
> 
> ---
> 
> I am not sure if this open-coded version is intentional and required for
> memdesc transition. Drop the patch if it is.

commit 2bcd3800f2da1be13b972858f63c66d035b1ec6d
Author: Matthew Wilcox (Oracle) <willy@infradead.org>
Date:   Thu Nov 13 00:09:15 2025 +0000

     slab: Reimplement page_slab()
     
     In order to separate slabs from folios, we need to convert from any page
     in a slab to the slab directly without going through a page to folio
     conversion first.

     Up to this point, page_slab() has followed the example of other memdesc
     converters (page_folio(), page_ptdesc() etc) and just cast the pointer
     to the requested type, regardless of whether the pointer is actually a
     pointer to the correct type or not.
     
     That changes with this commit; we check that the page actually belongs
     to a slab and return NULL if it does not.  Other memdesc converters will
     adopt this convention in future.


I think using compound_head() is fine. For memdescs the function has to be changed to
lookup the memdesc either way, and not go through the head page.

Acked-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David
Re: [PATCHv6 17/17] mm/slab: Use compound_head() in page_slab()
Posted by Muchun Song 3 days, 3 hours ago

> On Feb 2, 2026, at 23:56, Kiryl Shutsemau <kas@kernel.org> wrote:
> 
> page_slab() contained an open-coded implementation of compound_head().
> 
> Replace the duplicated code with a direct call to compound_head().
> 
> Signed-off-by: Kiryl Shutsemau <kas@kernel.org>

Acked-by: Muchun Song <muchun.song@linux.dev>