dma-contiguous: support percentage sizes in cma_pernuma= and numa_cma=
Subsystems (3)
PATCH v2 1/1 dma-contiguous: support percentage sizes in cma_pernuma= and numa_cma=
While CMA_SIZE_PERCENTAGE supports percentages for built-in defaults, the CMA allocation kernel parameters currently only take a fixed size argument. That makes it difficult to have a single system image that sizes its CMA allocation based on the available memory. To use CMA as backing pool for large, physically contiguous guest memory, I want to be able to say "allocate 90% of system RAM minus 1 GiB for CMA". Per NUMA node.
Extend the CMA parser logic to accept command line options like this:
cma_pernuma=80% numa_cma=0:95%-1G,1:4G
To avoid incomplete allocations on parsing errors, check for them before actually allocating.
---
v1 -> v2:
- Reject an empty numa_cma= and a trailing comma (Sashiko)
- Keep the last valid size for each option or node, so an explicit 0 now overrides a percentage (Sashiko)
- Parse percentages as plain decimal, and reject sizes that a 32-bit phys_addr_t cannot hold
- Reject a bare cma_pernuma or numa_cma without "=", which mainline dereferences as NULL
- Rework the kernel-parameters.txt text accordingly
v1: https://lore.kernel.org/all/[email protected]/
--- .../admin-guide/kernel-parameters.txt | 37 +++- kernel/dma/contiguous.c | 188 +++++++++++++++--- 2 files changed, 194 insertions(+), 31 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +30 -7
@@ -862,10 +862,13 @@ Kernel parameters contiguous memory allocations and optionally the placement constraint by the physical address range of memory allocations. A value of 0 disables CMA - altogether. For more information, see + altogether. The size is absolute; only 'cma_pernuma=' + and 'numa_cma=' accept a percentage. Without 'cma=', + CONFIG_CMA_SIZE_PERCENTAGE can size this area as a + fraction of memory. For more information, see kernel/dma/contiguous.c - cma_pernuma=nn[MG] + cma_pernuma=nn[MG]|nn%[-nn[MG]] [KNL,CMA,EARLY] Sets the size of kernel per-numa memory area for contiguous memory allocations. A value of 0 disables
@@ -875,14 +878,34 @@ Kernel parameters first try to allocate buffer from the pernuma area which is located in node nid, if the allocation fails, they will fallback to the global default memory area. - - numa_cma=<node>:nn[MG][,<node>:nn[MG]] + The size may also be a decimal percentage of each + node's memory as memblock sees it at boot, optionally + less a size to keep back, so cma_pernuma=95%-1G + reserves 95% of each node minus 1G. Hot-added memory is + not counted. A nonzero percentage that leaves nothing + to reserve on a node warns and reserves nothing there. + If the option is given more than once, the last valid + value counts. An invalid value is ignored, and it still + stops CONFIG_CMA_SIZE_PERNUMA from mirroring the + default area onto every node. + + numa_cma=<node>:nn[MG]|nn%[-nn[MG]][,<node>:nn[MG]|nn%[-nn[MG]]] [KNL,CMA,EARLY] Sets the size of kernel numa memory area for contiguous memory allocations. It will reserve CMA - area for the specified node. - - If it is setup together with upper 'cmd_pernuma=' + area for the specified node. A size may also be a + percentage of that node's memory, optionally less a + size to keep back, as for 'cma_pernuma='. A size of 0 + or 0% is the same as not naming the node, so it falls + back to 'cma_pernuma='. A nonzero percentage that + leaves nothing to reserve does not fall back. If a node + is named more than once, also across several numa_cma= + options, the last valid size counts. An entry that does + not parse, including an empty list or a trailing comma, + makes the kernel ignore that whole option, and the + default area is not mirrored either. + + If it is set up together with upper 'cma_pernuma=' (unlikely), its size setting takes priority for the specified numa nodes.
kernel/dma/contiguous.c +164 -24
238 lines not on this page · raw message
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.47.1
Marek Szyprowski
maintainerQuoted 7 lines · Alexander Graf
While
CMA_SIZE_PERCENTAGEsupports percentages for built-in defaults, the CMA allocation kernel parameters currently only take a fixed size argument. That makes it difficult to have a single system image that sizes its CMA allocation based on the available memory. To use CMA as backing pool for large, physically contiguous guest memory, I want to be able to say "allocate 90% of system RAM minus 1 GiB for CMA". Per NUMA node.
Quoted 6 lines
Extend the CMA parser logic to accept command line options like this:
cma_pernuma=80% numa_cma=0:95%-1G,1:4G
To avoid incomplete allocations on parsing errors, check for them before actually allocating.
This looks like an over-engineered feature. I doubt that kernel is the right place to implement such advanced sizing. If You need to explicitly control the amount of CMA on the target machines, maybe You could do it in some scripts deploying the kernel configuration? Calculating it there should be much more flexible imho...
Quoted 13 lines
Signed-off-by Alexander Graf <[email protected]>---
v1 -> v2:- Reject an empty numa_cma= and a trailing comma (Sashiko)
- Keep the last valid size for each option or node, so an explicit 0 now overrides a percentage (Sashiko)
- Parse percentages as plain decimal, and reject sizes that a 32-bitphys_addr_tcannot hold
- Reject a barecma_pernumaornuma_cmawithout "=", which mainline dereferences as NULL
- Rework the kernel-parameters.txt text accordingly
Quoted 2 lines
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Quoted 12 lines · Marek Szyprowski
Quoted 11 lines · Alexander Graf
While
CMA_SIZE_PERCENTAGEsupports percentages for built-in defaults, the CMA allocation kernel parameters currently only take a fixed size argument. That makes it difficult to have a single system image that sizes its CMA allocation based on the available memory. To use CMA as backing pool for large, physically contiguous guest memory, I want to be able to say "allocate 90% of system RAM minus 1 GiB for CMA". Per NUMA node.Extend the CMA parser logic to accept command line options like this:
cma_pernuma=80% numa_cma=0:95%-1G,1:4G
Quoted 8 lines
Quoted 2 lines
To avoid incomplete allocations on parsing errors, check for them before actually allocating.
This looks like an over-engineered feature. I doubt that kernel is the right place to implement such advanced sizing. If You need to explicitly control the amount of CMA on the target machines, maybe You could do it in some scripts deploying the kernel configuration? Calculating it there should be much more flexible imho...
It allows to push more policy into a generic boot image, yes. I suppose we could push that policy into other bits of pre-boot logic, such as the Linux EFI loader, but I'm not convinced it's a net win for simplicity. Pushing it into a boot load would mean pushing into UKI for UKI-based boot flows, which means bloating systemd-boot. Again, not really great.
Ideally all of this would be policy that only takes effect later at boot, so that we don't require pre-boot logic or a reboot. For this to work, we would need to have Linux restrict its memory allocations to a small'ish region instead of spreading them, maybe to the KHO scratch region, or maybe by favoring allocations in phys addr order. At least until the boot is at a point where you could perform the allocation. And then we would also have large physically contiguous blocks available to allocate from and potentially change the tag to CMA.
Any thoughts on how we could best do this?
Alex
Quoted 8 lines · Alexander
Ideally all of this would be policy that only takes effect later at boot, so that we don't require pre-boot logic or a reboot. For this to work, we would need to have Linux restrict its memory allocations to a small'ish region instead of spreading them, maybe to the KHO scratch region, or maybe by favoring allocations in phys addr order. At least until the boot is at a point where you could perform the allocation. And then we would also have large physically contiguous blocks available to allocate from and potentially change the tag to CMA.
That actually seems like a sort of reasonable idea...
What about the reverse, you boot with a huge CMA and then shrink it in the initrd to what you really want? For this you'd just want some command line indicating how much non-CMA memory to have, which should be fairly predictable to get into the initrd?
Jason