Subpage allocations for IOMMU tables
RFC 1/1 Subpage allocations for IOMMU tables
Hi Jason & co,
I have been experimenting for how we might support allocations smaller than PAGE_SIZE from iommu_alloc_pages_sz(). Are there any existing ideas or plans for how this could be done?
This would be particularly beneficial for ARM platforms where the tendency of using 16KB or 64KB system page sizes is growing, but the IOMMU is fixed at 4KB page size, leading to wasted memory.
One possible/draft approach is below. When a small allocation is requested, a per-node per-size folio is created. A section of this is used to satisfy the allocation, and the folio is added to a pool so that it can be used to satisfy later allocations too.
The main challenge is handling the iommu_pages_list used for freeing allocations. In the whole-page case, this is nicely handled by using the list_head in in struct ioptdesc. But now we may have up to 16 allocations per page, where each one can be placed on a different list.
So, in this approach, I have made iommu_pages_list manage both a list of regular full-page ioptdesc allocations, and also a list of subpage allocations that live within these subpage folios. The subpage list heads are stored in a dynamically allocated struct iommu_subpage_meta.
I don't love the approach but it at least seems to be working and I figured it would be good to get some opinions before I go further.
Other options:
- Replace the list_head with a pointer to the virt address of the next page in the list. We can detect whether we're working with a full page or subpage based on virt address, which means we only need one pointer in iommu_pages_list.
- Bump allocator with per-CPU pools of partial subpage folios, along the lines of the networking subsystem's page_frag_cache. A little simpler than the approach here, but we'd have to evict and abandon filling the folio if the next request is for a different NUMA node, and handle CPU hotplug.
- Allocate the subpage-tracking data in the unmap path? Falling back to immediate flush and free if allocation fails, along the lines of tlb_remove_table(). Would need more thought.
- Implement generation-based deferred freeing in iommu core, eliminating the need for iommu_pages_list?
--- drivers/iommu/Kconfig | 9 + drivers/iommu/Makefile | 1 + drivers/iommu/generic_pt/iommu_pt.h | 2 +- drivers/iommu/iommu-pages-kunit.c | 240 ++++++++++++++++++ drivers/iommu/iommu-pages.c | 368 +++++++++++++++++++++++----- drivers/iommu/iommu-pages.h | 50 ++-- include/linux/iommu.h | 10 +- 7 files changed, 603 insertions(+), 77 deletions(-) create mode 100644 drivers/iommu/iommu-pages-kunit.c
drivers/iommu/Kconfig +9 -0
@@ -419,6 +419,15 @@ config IOMMU_DEBUG_PAGEALLOC You must enable "iommu.debug_pagealloc" from the kernel command line to activate the runtime checks. + If unsure, say N. + +config IOMMU_PAGES_KUNIT_TEST + tristate "KUnit tests for IOMMU page and subpage allocator" + depends on KUNIT + default KUNIT_ALL_TESTS + help + Enable KUnit tests for the IOMMU page and subpage allocator. + If unsure, say N. endif # IOMMU_SUPPORT
drivers/iommu/Makefile +1 -0
@@ -39,3 +39,4 @@ obj-$(CONFIG_APPLE_DART) += apple-dart.o obj-$(CONFIG_VSI_IOMMU) += vsi-iommu.o obj-$(CONFIG_BCM2712_IOMMU) += bcm2712-iommu.o bcm2712-iommu-cache.o obj-$(CONFIG_IOMMU_DEBUG_PAGEALLOC) += iommu-debug-pagealloc.o +obj-$(CONFIG_IOMMU_PAGES_KUNIT_TEST) += iommu-pages-kunit.o
drivers/iommu/generic_pt/iommu_pt.h +1 -1
@@ -98,7 +98,7 @@ static void gather_range_pending(struct iommupt_pending_gather *pending, }iommu_pages_list_splice (&pending->free_list, &iotlb_gather->freelist); -INIT_LIST_HEAD (&pending->free_list.pages); + pending->free_list =IOMMU_PAGES_LIST_INIT (pending->free_list);if (pt_feature (common,PT_FEAT_DETAILED_GATHER )) { iotlb_gather->pt.leaf_levels_bitmap |=
drivers/iommu/iommu-pages-kunit.c new file +240 -0
240 lines not on this page · raw message
drivers/iommu/iommu-pages.c +309 -59
493 lines not on this page · raw message
drivers/iommu/iommu-pages.h +35 -15
92 lines not on this page · raw message
include/linux/iommu.h +8 -2
@@ -334,11 +334,17 @@ typedef unsigned int ioasid_t;* iommu_alloc_pages_node_sz() can be used this way! */ struct iommu_pages_list { +/* Full pages, linked through their ioptdesc */ struct list_head pages; +/* Sub-page allocations, linked through their folio's metadata */ +struct list_head subpages; }; -#define IOMMU_PAGES_LIST_INIT (name) \ - ((struct iommu_pages_list){ .pages = LIST_HEAD_INIT(name.pages) }) +#define IOMMU_PAGES_LIST_INIT (name) \ + ((struct iommu_pages_list){ \ + .pages = LIST_HEAD_INIT(name.pages), \ + .subpages = LIST_HEAD_INIT(name.subpages), \ + })#ifdef CONFIG_IOMMU_API
Signature · 1 line
2.55.0
Quoted 9 lines · Daniel Drake
Hi Jason & co,
I have been experimenting for how we might support allocations smaller than
PAGE_SIZEfromiommu_alloc_pages_sz(). Are there any existing ideas or plans for how this could be done?This would be particularly beneficial for ARM platforms where the tendency of using 16KB or 64KB system page sizes is growing, but the IOMMU is fixed at 4KB page size, leading to wasted memory.
Oh? People built SMMUs without the 16k option? That's sad news. In the systems I have the SMMUv3 supports 64k/16k so there is no waste..
Quoted 10 lines
One possible/draft approach is below. When a small allocation is requested, a per-node per-size folio is created. A section of this is used to satisfy the allocation, and the folio is added to a pool so that it can be used to satisfy later allocations too.
The main challenge is handling the
iommu_pages_listused for freeing allocations. In the whole-page case, this is nicely handled by using thelist_headin in struct ioptdesc. But now we may have up to 16 allocations per page, where each one can be placed on a different list.
Quoted 8 lines
So, in this approach, I have made
iommu_pages_listmanage both a list of regular full-page ioptdesc allocations, and also a list of subpage allocations that live within these subpage folios. The subpage list heads are stored in a dynamically allocated structiommu_subpage_meta.I don't love the approach but it at least seems to be working and I figured it would be good to get some opinions before I go further.
Yeah, you've hit on the main issues. Throw in the coming RCU HEAD issue too and you get more problems. The iommu_pages_list abstraction was intended to give some flexbility to solve this.
The idea Matthew was aiming toward was to allocate a dedicated struct for each folio that could have the right sized stuff. You can have an array of list heads, for example. Then you are not limited to overlaying on struct folio.
He once even talked about making this into some kind of generic sub page allocator with metadata, which seems quite interesting.
In this mode the core mm would provide the pooling and the subsystem could request XX bytes memory + YY bytes metadata with some way to convert from a memory pointer to the metadata pointer and back again.
There is also the exciting stuff power does with it's sub page allocator, not sure that is a great example though.
Another option might be to involve generic_pt somehow and have it populate the tree with the full folio. At least this way the memory is not "lost" but it may not be used optimally. I doubt many people need a 16x level 3 page for example
Another thought might be to use (pre) allocated memory to hold the free list during unmap and then flush if we run out of memory. This eliminates the linked list which seems helpful. Not sure what kind of performance we'd get though.
Quoted 5 lines
Other options:
- Replace thelist_headwith a pointer to the virt address of the next page in the list. We can detect whether we're working with a full page or subpage based on virt address, which means we only need one pointer iniommu_pages_list.
So long as a linked list is used you need 4x the pointers, which seems to be the main problem.
Quoted 5 lines
- Bump allocator with per-CPU pools of partial subpage folios, along the lines of the networking subsystem's
page_frag_cache. A little simpler than the approach here, but we'd have to evict and abandon filling the folio if the next request is for a different NUMA node, and handle CPU hotplug.
Free side can't gurantee it will only free the same per-CPU pages, so you are still back to needing 4x pointers.
Quoted 5 lines
- Allocate the subpage-tracking data in the unmap path? Falling back to immediate flush and free if allocation fails, along the lines of
tlb_remove_table(). Would need more thought.
- Implement generation-based deferred freeing in iommu core, eliminating the need foriommu_pages_list?
What would this look like?
There is also the 32 bit ARM option which avoided using a free list so it doesn't need folio memory. This allows it to use a normal kmemcache.
free list is fundamental to the design of generic_pt, but if more people than just arm32 need this it would be reasonable to implement a generic_pt_nolist variation. Driver can select which version it wants. Maybe this is implemented as some combination of immediate flushing and a small free array allocation from a cache?
Jason