LWN: Comments on "Implementing alignment guarantees for kmalloc()" https://lwn.net/Articles/802469/ This is a special feed containing comments posted to the individual LWN article titled "Implementing alignment guarantees for kmalloc()". en-us Thu, 09 Jan 2025 20:50:32 +0000 Thu, 09 Jan 2025 20:50:32 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Implementing alignment guarantees for kmalloc() https://lwn.net/Articles/802911/ https://lwn.net/Articles/802911/ rweikusat2 <div class="FormattedComment"> The expression was n &amp; ~(-1 + n). That's a slightly steganographically obscured version of n &amp; ~(n - 1). Assuming that n is some non-zero binary number, subtracting one from that causes the lowest set 1 bit to change to 0 and all 0 bits below it to 1. All other 1 bits of n are also 1 in n - 1. Hence n &amp; ~(n - 1) leaves only the lowest bit of n set. <br> <p> </div> Wed, 23 Oct 2019 14:20:33 +0000 Implementing alignment guarantees for kmalloc() https://lwn.net/Articles/802879/ https://lwn.net/Articles/802879/ wilevers <div class="FormattedComment"> Try parsing this. Then again. Any questions?<br> </div> Tue, 22 Oct 2019 19:55:02 +0000 Implementing alignment guarantees for kmalloc() https://lwn.net/Articles/802796/ https://lwn.net/Articles/802796/ jreiser <i>The new alignment guarantees are only for power of two sizes...</i> &nbsp;&nbsp;The result should be aligned to <tt>min(PAGE_SIZE, n &amp; ~(-1+n))</tt>, 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 <tt>struct</tt> having that size. Mon, 21 Oct 2019 16:48:26 +0000 Implementing alignment guarantees for kmalloc() https://lwn.net/Articles/802734/ https://lwn.net/Articles/802734/ vbabka <div class="FormattedComment"> <font class="QuotedText">&gt; 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.</font><br> <p> 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. <br> </div> Mon, 21 Oct 2019 07:56:41 +0000 Implementing alignment guarantees for kmalloc() https://lwn.net/Articles/802726/ https://lwn.net/Articles/802726/ Cyberax <div class="FormattedComment"> <font class="QuotedText">&gt; 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. </font><br> Typically such unspoken ABIs are extended explicitly, by adding new flags. <br> <p> 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.<br> </div> Sun, 20 Oct 2019 23:36:07 +0000 Implementing alignment guarantees for kmalloc() https://lwn.net/Articles/802722/ https://lwn.net/Articles/802722/ epa <div class="FormattedComment"> Because the kernel is full of callers whose intent is to get aligned access, but don't explicitly specify it. Why do I say their intent is to get aligned access? Simply because of the fact that access always has been aligned up to now, in all but a few oddball configurations. If it almost always behaves that way in practice, any support in callers for unaligned access won't get exercised and will inevitably rot away. By the same argument you can say that malloc() (in user space / libc) is effectively an API that never returns null on failure, since it never does so in practice and nobody writes application code that handles it correctly; even with the best of intentions such code wouldn't get exercised enough for it to have a chance of working correctly.<br> <p> 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.<br> <p> (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.)<br> </div> Sun, 20 Oct 2019 19:28:07 +0000 Implementing alignment guarantees for kmalloc() https://lwn.net/Articles/802721/ https://lwn.net/Articles/802721/ wilevers <div class="FormattedComment"> I'm confused. Why can't we have a separate API for callers that have a specific alignment requirement? That would allow any allocator to optimally supply what is expected instead of having to guess the caller's intent.<br> <p> </div> Sun, 20 Oct 2019 18:39:40 +0000