Implementing alignment guarantees for kmalloc()
Implementing alignment guarantees for kmalloc()
Posted Oct 20, 2019 19:28 UTC (Sun) by epa (subscriber, #39769)In reply to: Implementing alignment guarantees for kmalloc() by wilevers
Parent article: Implementing alignment guarantees for kmalloc()
I think the question is more whether there are any callers that have a specific requirement for unaligned allocations, that is, cases where there are so many tiny allocations that the wasted space matters. And if they exist, whether they wouldn't be better served with large requests to kmalloc() feeding to their own internal allocator which portions them out into tiny slices.
(This from the perspective of a non-kernel-developer, but the same issue arises in any API, having only one real implementation, where a particular behaviour is theoretically possible but doesn't arise in practice. In my view the answer is almost always to tighten up the specification, codifying the implicit guarantees that have held up to now.)
Posted Oct 20, 2019 23:36 UTC (Sun)
by Cyberax (✭ supporter ✭, #52523)
[Link]
So by default kmalloc() should return an aligned block, but there should be a flag explicitly requesting unaligned block. This way there won't be a need to have multiple allocators in each subsystem needing this.
Posted Oct 21, 2019 7:56 UTC (Mon)
by vbabka (subscriber, #91706)
[Link] (3 responses)
The new alignment guarantees are only for power of two sizes, and that's when the common SLAB and SLUB configurations already don't waste any memory. For other sizes, kmalloc() will mostly round them up to the nearest power-of-two anyway (exceptions are 96 and 192 bytes, see your /proc/slabinfo), so the waste comes from that. Those who allocate significant number of "oddly sized" objects should create own cache for them by kmem_cache_create() with precise size and optional alignment, which will minimize the waste.
Posted Oct 21, 2019 16:48 UTC (Mon)
by jreiser (subscriber, #11027)
[Link] (2 responses)
Posted Oct 22, 2019 19:55 UTC (Tue)
by wilevers (subscriber, #110407)
[Link] (1 responses)
Posted Oct 23, 2019 14:20 UTC (Wed)
by rweikusat2 (subscriber, #117920)
[Link]
Implementing alignment guarantees for kmalloc()
Typically such unspoken ABIs are extended explicitly, by adding new flags.
Implementing alignment guarantees for kmalloc()
The new alignment guarantees are only for power of two sizes... The result should be aligned to min(PAGE_SIZE, n & ~(-1+n)), which is the place value of the lowest-order '1' bit in the requested size (but limited to the sizeof one page). So if the request is for 40 bytes then the result should be 8-byte aligned. The rationale is: the alignment of a struct having that size.
Implementing alignment guarantees for kmalloc()
Implementing alignment guarantees for kmalloc()
Implementing alignment guarantees for kmalloc()