linux/Documentation/core-api
Vlastimil Babka ad59baa316 slab, rust: extend kmalloc() alignment guarantees to remove Rust padding
Slab allocators have been guaranteeing natural alignment for
power-of-two sizes since commit 59bb47985c ("mm, sl[aou]b: guarantee
natural alignment for kmalloc(power-of-two)"), while any other sizes are
guaranteed to be aligned only to ARCH_KMALLOC_MINALIGN bytes (although
in practice are aligned more than that in non-debug scenarios).

Rust's allocator API specifies size and alignment per allocation, which
have to satisfy the following rules, per Alice Ryhl [1]:

  1. The alignment is a power of two.
  2. The size is non-zero.
  3. When you round up the size to the next multiple of the alignment,
     then it must not overflow the signed type isize / ssize_t.

In order to map this to kmalloc()'s guarantees, some requested
allocation sizes have to be padded to the next power-of-two size [2].
For example, an allocation of size 96 and alignment of 32 will be padded
to an allocation of size 128, because the existing kmalloc-96 bucket
doesn't guarantee alignent above ARCH_KMALLOC_MINALIGN. Without slab
debugging active, the layout of the kmalloc-96 slabs however naturally
align the objects to 32 bytes, so extending the size to 128 bytes is
wasteful.

To improve the situation we can extend the kmalloc() alignment
guarantees in a way that

1) doesn't change the current slab layout (and thus does not increase
   internal fragmentation) when slab debugging is not active
2) reduces waste in the Rust allocator use case
3) is a superset of the current guarantee for power-of-two sizes.

The extended guarantee is that alignment is at least the largest
power-of-two divisor of the requested size. For power-of-two sizes the
largest divisor is the size itself, but let's keep this case documented
separately for clarity.

For current kmalloc size buckets, it means kmalloc-96 will guarantee
alignment of 32 bytes and kmalloc-196 will guarantee 64 bytes.

This covers the rules 1 and 2 above of Rust's API as long as the size is
a multiple of the alignment. The Rust layer should now only need to
round up the size to the next multiple if it isn't, while enforcing the
rule 3.

Implementation-wise, this changes the alignment calculation in
create_boot_cache(). While at it also do the calulation only for caches
with the SLAB_KMALLOC flag, because the function is also used to create
the initial kmem_cache and kmem_cache_node caches, where no alignment
guarantee is necessary.

In the Rust allocator's krealloc_aligned(), remove the code that padded
sizes to the next power of two (suggested by Alice Ryhl) as it's no
longer necessary with the new guarantees.

Reported-by: Alice Ryhl <aliceryhl@google.com>
Reported-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/all/CAH5fLggjrbdUuT-H-5vbQfMazjRDpp2%2Bk3%3DYhPyS17ezEqxwcw@mail.gmail.com/ [1]
Link: https://lore.kernel.org/all/CAH5fLghsZRemYUwVvhk77o6y1foqnCeDzW4WZv6ScEWna2+_jw@mail.gmail.com/ [2]
Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Acked-by: Roman Gushchin <roman.gushchin@linux.dev>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
2024-07-03 12:23:27 +02:00
..
irq Documentation: irqdomain: Fix typo of "at least once" 2022-08-18 11:11:52 -06:00
wrappers docs: put atomic*.txt and memory-barriers.txt into the core-api book 2022-09-29 12:55:06 -06:00
asm-annotations.rst docs: move x86 documentation into Documentation/arch/ 2023-03-30 12:58:51 -06:00
assoc_array.rst
boot-time-mm.rst
cachetlb.rst mm: remove ARCH_IMPLEMENTS_FLUSH_DCACHE_FOLIO 2023-08-24 16:20:19 -07:00
circular-buffers.rst
cpu_hotplug.rst arch: Remove Itanium (IA-64) architecture 2023-09-11 08:13:17 +00:00
debug-objects.rst
debugging-via-ohci1394.rst Documentation: Drop or replace remaining mentions of IA64 2023-09-11 08:13:18 +00:00
dma-api-howto.rst docs: dma: correct dma_set_mask() sample code 2024-04-02 10:07:15 -06:00
dma-api.rst docs: dma-api: Fix description of the sync_sg API 2023-11-17 08:52:13 -07:00
dma-attributes.rst
dma-isa-lpc.rst
entry.rst Docs: typos/spelling 2024-05-02 10:02:29 -06:00
errseq.rst
floating-point.rst arch: add ARCH_HAS_KERNEL_FPU_SUPPORT 2024-05-19 14:36:17 -07:00
genalloc.rst
generic-radix-tree.rst
genericirq.rst Docu: genericirq.rst: fix irq-example 2023-08-28 12:45:31 -06:00
gfp_mask-from-fs-io.rst
idr.rst
index.rst - A series ("kbuild: enable more warnings by default") from Arnd 2024-05-22 18:59:29 -07:00
kernel-api.rst Documentation: core-api: Drop :export: for int_log.h 2023-07-25 17:40:25 +01:00
kobject.rst
kref.rst
librs.rst
local_ops.rst timers: Update the documentation to reflect on the new timer_shutdown() API 2022-11-24 15:09:12 +01:00
maple_tree.rst maple_tree: update the documentation of maple tree 2023-12-10 16:51:32 -08:00
memory-allocation.rst slab, rust: extend kmalloc() alignment guarantees to remove Rust padding 2024-07-03 12:23:27 +02:00
memory-hotplug.rst
mm-api.rst mm/slab, docs: switch mm-api docs generation from slab.c to slub.c 2023-12-05 11:11:34 +01:00
netlink.rst doc/netlink: Update genetlink-legacy documentation 2023-08-27 17:17:09 -07:00
packing.rst Documentation: core-api: packing: correct spelling 2023-02-15 21:40:54 -08:00
padata.rst Documentation: core-api: padata: correct spelling 2023-02-16 16:58:01 -07:00
pin_user_pages.rst Documentation/gpu: VM_BIND locking document 2023-11-29 20:54:43 +01:00
printk-basics.rst
printk-formats.rst printk changes for 6.6 2023-09-04 13:20:19 -07:00
printk-index.rst docs:core-api: fixed typos and grammar in printk-index page 2024-05-07 08:01:11 -06:00
protection-keys.rst
rbtree.rst
refcount-vs-atomic.rst
swiotlb.rst Documentation/core-api: add swiotlb documentation 2024-05-02 15:51:01 +02:00
symbol-namespaces.rst
this_cpu_ops.rst arch: Remove cmpxchg_double 2023-06-05 09:36:39 +02:00
timekeeping.rst
tracepoint.rst
unaligned-memory-access.rst
watch_queue.rst
workqueue.rst Documentation/core-api: Update events_freezable_power references. 2024-04-03 08:45:18 -10:00
xarray.rst