|
|
Log in / Subscribe / Register

Pulling slabs out of struct page

Pulling slabs out of struct page

Posted Oct 8, 2021 19:45 UTC (Fri) by Cyberax (✭ supporter ✭, #52523)
Parent article: Pulling slabs out of struct page

What is the reason for placing _refcount at the end of the structure rather than at the beginning?


to post comments

_refcount

Posted Oct 8, 2021 20:02 UTC (Fri) by corbet (editor, #1) [Link] (1 responses)

For struct slab, it has to be in the same place as with struct page. As for why its location in struct page, that's probably an outcome of years of history. But is there some special reason why _refcount should be at the beginning?

_refcount

Posted Oct 8, 2021 23:53 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link]

I mean, typically you would want to have all the shared fields to come first in the structure. That's how inheritance is usually done in plain C.

Pulling slabs out of struct page

Posted Oct 8, 2021 21:10 UTC (Fri) by willy (subscriber, #9762) [Link] (2 responses)

Ooh, ooh, I know this one! Hope you like horror films ...

Slub needs to cmpxchg_double() freelist & counters. That means that freelist has to be dword aligned on both 32 and 64 bit. _refcount is word sized on 32-bit and half-word sized on 64-bit. For compactness, we pair it with _mapcount. Now the pair are either one or two words, depending on 32/64 bit. So _refcount has to be after freelist+counters in order for them to be dword aligned on both 32 and 64 bit. It's advantageous to make the main union as large as possible, so _refcount has to go towards the end of the struct.

There are other considerations in the layout of struct page, but these are the ones that pertain to the location of _refcount.

Pulling slabs out of struct page

Posted Oct 8, 2021 23:55 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link]

Ah. I suspected as much. Thanks for the night-time alignment horror story!

Pulling slabs out of struct page

Posted Oct 10, 2021 0:54 UTC (Sun) by vivo (subscriber, #48315) [Link]

this one deserve a place in the weekly "Brief items" page


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds