Prototype for direct map awareness in page allocator
Subsystems (7)
Mike Rapoport
maintainercover Prototype for direct map awareness in page allocator
From: "Mike Rapoport (IBM)" <[email protected]>
Hi,
This is a third attempt to make page allocator aware of the direct map layout and allow grouping of the pages that must be unmapped from the direct map.
This a new implementation of __GFP_UNMAPPED, kinda a follow up for this set:
https://lore.kernel.org/all/[email protected]
but instead of using a migrate type to cache the unmapped pages, the current implementation adds a dedicated cache to serve __GFP_UNMAPPED allocations.
The last two patches in the series demonstrate how __GFP_UNMAPPED can be used in two in-tree use cases.
First one is to switch secretmem to use the new mechanism, which is straight forward optimization.
The second use-case is to enable __GFP_UNMAPPED in x86::module_alloc() that is essentially used as a method to allocate code pages and thus requires permission changes for basic pages in the direct map.
This set is x86 specific at the moment because other architectures either do not support set_memory APIs that split the direct^w linear map (e.g. PowerPC) or only enable set_memory APIs when the linear map uses basic page size (like arm64).
The patches are only lightly tested.
== Motivation ==
There are use-cases that need to remove pages from the direct map or at least map them with 4K granularity. Whenever this is done e.g. with set_memory/set_direct_map APIs, the PUD and PMD sized mappings in the direct map are split into smaller pages.
To reduce the performance hit caused by the fragmentation of the direct map it makes sense to group and/or cache the pages removed from the direct map so that the split large pages won't be all over the place.
There were RFCs for grouped page allocations for vmalloc permissions [1] and for using PKS to protect page tables [2] as well as an attempt to use a pool of large pages in secretmtm [3], but these suggestions address each use case separately, while having a common mechanism at the core mm level could be used by all use cases.
== Implementation overview ==
The pages that need to be removed from the direct map are grouped in a dedicated cache. When there is a page allocation request with __GFP_UNMAPPED set, it is redirected from __alloc_pages() to that cache using a new unmapped_alloc() function.
The cache is implemented as a buddy allocator and it can handle high order requests.
The cache starts empty and whenever it does not have enough pages to satisfy an allocation request the cache attempts to allocate PMD_SIZE page to replenish the cache. If PMD_SIZE page cannot be allocated, the cache is replenished with a page of the highest order available. That page is removed from the direct map and added to the local buddy allocator.
There is also a shrinker that releases pages from the unmapped cache when there us a memory pressure in the system. When shrinker releases a page it is mapped back into the direct map.
[1] https://lore.kernel.org/lkml/[email protected]
[2] https://lore.kernel.org/lkml/[email protected]
[3] https://lore.kernel.org/lkml/[email protected]
Mike Rapoport (IBM) (5):
mm: intorduce __GFP_UNMAPPED and unmapped_alloc() mm/unmapped_alloc: add debugfs file similar to /proc/pagetypeinfo mm/unmapped_alloc: add shrinker
EXPERIMENTAL: x86: use __GFP_UNMAPPED for modele_alloc()
EXPERIMENTAL: mm/secretmem: use __GFP_UNMAPPED
arch/x86/Kconfig | 3 + arch/x86/kernel/module.c | 2 +- include/linux/gfp_types.h | 11 +- include/linux/page-flags.h | 6 + include/linux/pageblock-flags.h | 28 +++ include/trace/events/mmflags.h | 10 +- mm/Kconfig | 4 + mm/Makefile | 1 + mm/internal.h | 24 +++ mm/page_alloc.c | 39 +++- mm/secretmem.c | 26 +-- mm/unmapped-alloc.c | 334 ++++++++++++++++++++++++++++++++ mm/vmalloc.c | 2 +- 13 files changed, 459 insertions(+), 31 deletions(-) create mode 100644 mm/unmapped-alloc.c
base-commit: fe15c26ee26efa11741a7b632e9f23b01aca4cc6
Signature · 1 line
2.35.1
Mike Rapoport
maintainerRFC PATCH 1/5 mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
From: "Mike Rapoport (IBM)" <[email protected]>
When set_memory or set_direct_map APIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.
To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with __GFP_UNMAPPED flag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.
This cache is replenished with higher order pages with preference for PMD_SIZE pages when possible so that there will be fewer splits of large pages in the direct map.
The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
--- arch/x86/Kconfig | 3 + include/linux/gfp_types.h | 11 +- include/linux/page-flags.h | 6 + include/linux/pageblock-flags.h | 28 +++++ include/trace/events/mmflags.h | 10 +- mm/Kconfig | 4 + mm/Makefile | 1 + mm/internal.h | 22 ++++ mm/page_alloc.c | 29 ++++- mm/unmapped-alloc.c | 215 ++++++++++++++++++++++++++++++++ 10 files changed, 325 insertions(+), 4 deletions(-) create mode 100644 mm/unmapped-alloc.c
arch/x86/Kconfig +3 -0
@@ -35,6 +35,9 @@ config X86_64 select ARCH_HAS_ELFCORE_COMPAT select ZONE_DMA32 +config ARCH_WANTS_GFP_UNMAPPED + def_bool y if X86_64 + config FORCE_DYNAMIC_FTRACE def_bool y depends on X86_32
include/linux/gfp_types.h +10 -1
@@ -60,6 +60,12 @@ typedef unsigned int __bitwise gfp_t;#else #define ___GFP_NOLOCKDEP 0#endif +#ifdef CONFIG_UNMAPPED_ALLOC +#define ___GFP_UNMAPPED 0x10000000u +#else +#define ___GFP_UNMAPPED 0 +#endif +/* If the above are modified, __GFP_BITS_SHIFT may need updating */ /*
@@ -101,12 +107,15 @@ typedef unsigned int __bitwise gfp_t;* node with no fallbacks or placement policy enforcements. * * %__GFP_ACCOUNT causes the allocation to be accounted to kmemcg. +* +* %__GFP_UNMAPPED removes the allocated pages from the direct map. */ #define __GFP_RECLAIMABLE ((__force gfp_t)___GFP_RECLAIMABLE)#define __GFP_WRITE ((__force gfp_t)___GFP_WRITE)#define __GFP_HARDWALL ((__force gfp_t)___GFP_HARDWALL)#define __GFP_THISNODE ((__force gfp_t)___GFP_THISNODE)#define __GFP_ACCOUNT ((__force gfp_t)___GFP_ACCOUNT) +#define __GFP_UNMAPPED ((__force gfp_t)___GFP_UNMAPPED)/** * DOC: Watermark modifiers
@@ -252,7 +261,7 @@ typedef unsigned int __bitwise gfp_t;#define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (27 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT (28 + IS_ENABLED(CONFIG_LOCKDEP))#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/**
include/linux/page-flags.h +6 -0
@@ -922,6 +922,7 @@ static inline bool is_page_hwpoison(struct page *page)#define PG_offline 0x00000100#define PG_table 0x00000200#define PG_guard 0x00000400 +#define PG_unmapped 0x00000800#define PageType (page, flag) \ ((page->page_type & (PAGE_TYPE_BASE | flag)) == PAGE_TYPE_BASE)
@@ -992,6 +993,11 @@ PAGE_TYPE_OPS(Table, table)*/ PAGE_TYPE_OPS (Guard, guard) +/* +* Marks pages in free lists of unmapped allocator. + */ +PAGE_TYPE_OPS(Unmapped, unmapped) +extern bool is_free_buddy_page (struct page *page);PAGEFLAG (Isolated, isolated,PF_ANY );
include/linux/pageblock-flags.h +28 -0
@@ -21,6 +21,9 @@ enum pageblock_bits {/* 3 bits required for migrate types */ PB_migrate_skip,/* If set the block is skipped by compaction */ + PB_unmapped =7 ,/* if set the block has pages unmapped in the direct +map */ +/* * Assume the bits will always align on a word. If this assumption * changes then get/set pageblock needs updating.
@@ -95,4 +98,29 @@ static inline void set_pageblock_skip(struct page *page) }#endif /* CONFIG_COMPACTION */ +#ifdef CONFIG_UNMAPPED_ALLOC +#define get_pageblock_unmapped (page) \ + get_pfnblock_flags_mask(page, page_to_pfn(page), \ + (1 << (PB_unmapped))) +#define clear_pageblock_unmapped (page) \ + set_pfnblock_flags_mask(page, 0, page_to_pfn(page), \ + (1 << PB_unmapped)) +#define set_pageblock_unmapped (page) \ + set_pfnblock_flags_mask(page, (1 << PB_unmapped), \ + page_to_pfn(page), \ + (1 << PB_unmapped)) +#else /* CONFIG_UNMAPPED_ALLOC */ +static inline bool get_pageblock_unmapped (struct page *page) +{ +return false; +} +static inline void clear_pageblock_unmapped (struct page *page) +{ +} +static inline void set_pageblock_unmapped (struct page *page) +{ +} +#endif /* CONFIG_UNMAPPED_ALLOC */ + +#endif /* PAGEBLOCK_FLAGS_H */
include/trace/events/mmflags.h +9 -1
@@ -61,9 +61,17 @@#define __def_gfpflag_names_kasan#endif +#ifdef CONFIG_UNMAPPED_ALLOC +#define __def_gfpflag_names_unmapped \ + , gfpflag_string(__GFP_UNMAPPED) +#else +#define __def_gfpflag_names_unmapped +#endif +#define show_gfp_flags (flags) \ (flags) ? __print_flags(flags, "|", \ - __def_gfpflag_names __def_gfpflag_names_kasan \ + __def_gfpflag_names __def_gfpflag_names_kasan \ + __def_gfpflag_names_unmapped \ ) : "none"#ifdef CONFIG_MMU
mm/Kconfig +4 -0
@@ -1202,6 +1202,10 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. # } +config UNMAPPED_ALLOC + def_bool y + depends on ARCH_WANTS_GFP_UNMAPPED + source "mm/damon/Kconfig" endmenu
mm/Makefile +1 -0
@@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_UNMAPPED_ALLOC) += unmapped-alloc.o
mm/internal.h +22 -0
@@ -1042,4 +1042,26 @@ struct vma_prepare {struct vm_area_struct *remove; struct vm_area_struct *remove2; }; + +/* +* mm/unmapped-alloc.c +*/ +#ifdef CONFIG_UNMAPPED_ALLOC +int unmapped_alloc_init (void ); +struct page *unmapped_pages_alloc (gfp_t gfp,int order); +void unmapped_pages_free (struct page *page,int order); +#else +static inline int unmapped_alloc_init (void ) +{ +return 0 ; +} + +static inline struct page *unmapped_pages_alloc (gfp_t gfp,int order) +{ +return NULL ; +} + +static inline void unmapped_pages_free (struct page *page,int order) {} +#endif +#endif /* __MM_INTERNAL_H */
mm/page_alloc.c +27 -2
@@ -589,7 +589,7 @@ void set_pfnblock_flags_mask(struct page *page, unsigned long flags,unsigned long bitidx, word_bitidx;unsigned long word; -BUILD_BUG_ON (NR_PAGEBLOCK_BITS !=4 ); +BUILD_BUG_ON (NR_PAGEBLOCK_BITS !=8 );BUILD_BUG_ON (MIGRATE_TYPES > (1 << PB_migratetype_bits)); bitmap =get_pageblock_bitmap (page, pfn);
@@ -746,6 +746,13 @@ static inline bool pcp_allowed_order(unsigned int order)return false; } +static inline bool pcp_allowed (unsigned int order,gfp_t gfp) +{ +if (unlikely (gfp & __GFP_UNMAPPED)) +return false; +return pcp_allowed_order (order); +} +static inline void free_the_page (struct page *page,unsigned int order) {if (pcp_allowed_order (order))/* Via pcp? */
@@ -1460,6 +1467,11 @@ static __always_inline bool free_pages_prepare(struct page *page, PAGE_SIZE << order); } +if (get_pageblock_unmapped (page)) { +unmapped_pages_free (page, order); +return false; + } +kernel_poison_pages (page,1 << order);/*
@@ -3525,6 +3537,13 @@ void free_unref_page_list(struct list_head *list)/* Prepare pages for freeing */ list_for_each_entry_safe (page, next, list, lru) {unsigned long pfn =page_to_pfn (page); + +if (get_pageblock_unmapped (page)) { +list_del (&page->lru); +unmapped_pages_free (page,0 ); +continue ; + } +if (!free_unref_page_prepare (page, pfn,0 )) {list_del (&page->lru);continue ;
@@ -3856,7 +3875,7 @@ struct page *rmqueue(struct zone *preferred_zone,*/ WARN_ON_ONCE ((gfp_flags & __GFP_NOFAIL) && (order >1 )); -if (likely (pcp_allowed_order (order))) { +if (likely (pcp_allowed (order, gfp_flags))) {/* * MIGRATE_MOVABLE pcplist could have the pages on CMA area and * we need to skip it when CMA area isn't allowed.
@@ -5581,6 +5600,11 @@ struct page *__alloc_pages(gfp_t gfp, unsigned int order, int preferred_nid, &alloc_gfp, &alloc_flags))return NULL ; +if (alloc_gfp & __GFP_UNMAPPED) { + page =unmapped_pages_alloc (gfp, order); + goto out; + } +/* * Forbid the first pass from falling back to types that fragment * memory until all local zones are considered.
@@ -8437,6 +8461,7 @@ void __init free_area_init(unsigned long *max_zone_pfn) }memmap_init (); +unmapped_alloc_init (); } static int __init cmdline_parse_core(char *p, unsigned long *core,
mm/unmapped-alloc.c new file +215 -0
215 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 27 lines · Mike Rapoport
(quoted diff) +2
-0+ +static inline void __free_one_page(struct page *page, unsigned intorder,
mm/unmapped-alloc.c new file +17
-0+ bool cache_refill) +{ +unsigned long pfn =page_to_pfn (page); +unsigned long buddy_pfn; +unsigned long combined_pfn; +struct page *buddy; +unsigned long flags; + +spin_lock_irqsave (&free_area->lock, flags); + +if (cache_refill) { +set_pageblock_unmapped (page); + free_area[order].nr_cached++; + } + +while (order <MAX_ORDER -1 ) { + buddy = find_unmapped_buddy_page_pfn(page, pfn,order,
mm/unmapped-alloc.c new file +6
-0+ &buddy_pfn); +if (!buddy) +break ; + +del_page_from_free_list (buddy, order); + combined_pfn = buddy_pfn & pfn;
mm/unmapped-alloc.c line 107
Quoted 10 lines
mm/unmapped-alloc.c new file +10
-0+ page = page + (combined_pfn - pfn); + pfn = combined_pfn; + order++; + } + +set_unmapped_order (page, order); +add_to_free_list (page, order); +spin_unlock_irqrestore (&free_area->lock, flags); +} +
The page has to be zeroed before it goes back on the list, right? I didn't see it.
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 33 lines · Rick P
Quoted 32 lines · Mike Rapoport
(quoted diff) +2
-0+ +static inline void __free_one_page(struct page *page, unsigned intorder,
mm/unmapped-alloc.c new file +17
-0+ bool cache_refill) +{ +unsigned long pfn =page_to_pfn (page); +unsigned long buddy_pfn; +unsigned long combined_pfn; +struct page *buddy; +unsigned long flags; + +spin_lock_irqsave (&free_area->lock, flags); + +if (cache_refill) { +set_pageblock_unmapped (page); + free_area[order].nr_cached++; + } + +while (order <MAX_ORDER -1 ) { + buddy = find_unmapped_buddy_page_pfn(page, pfn,order,
mm/unmapped-alloc.c new file +11
-0+ &buddy_pfn); +if (!buddy) +break ; + +del_page_from_free_list (buddy, order); + combined_pfn = buddy_pfn & pfn; + page = page + (combined_pfn - pfn); + pfn = combined_pfn; + order++; + } +
Quoted 8 lines
mm/unmapped-alloc.c line 107
Quoted 5 lines
mm/unmapped-alloc.c new file +5
-0+set_unmapped_order (page, order); +add_to_free_list (page, order); +spin_unlock_irqrestore (&free_area->lock, flags); +} +The page has to be zeroed before it goes back on the list, right? I didn't see it.
You are right, I missed it.
Signature · 2 lines
Sincerely yours, Mike.
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 44 lines · Mike Rapoport
Quoted 34 lines · Rick P
Quoted 33 lines · Mike Rapoport
(quoted diff) +2
-0+ +static inline void __free_one_page(struct page *page, unsignedint
order,mm/unmapped-alloc.c new file +17
-0+ bool cache_refill) +{ +unsigned long pfn =page_to_pfn (page); +unsigned long buddy_pfn; +unsigned long combined_pfn; +struct page *buddy; +unsigned long flags; + +spin_lock_irqsave (&free_area->lock, flags); + +if (cache_refill) { +set_pageblock_unmapped (page); + free_area[order].nr_cached++; + } + +while (order <MAX_ORDER -1 ) { + buddy = find_unmapped_buddy_page_pfn(page, pfn,order,
mm/unmapped-alloc.c new file +11
-0+ &buddy_pfn); +if (!buddy) +break ; + +del_page_from_free_list (buddy, order); + combined_pfn = buddy_pfn & pfn; + page = page + (combined_pfn - pfn); + pfn = combined_pfn; + order++; + } +Quoted 8 lines
mm/unmapped-alloc.c line 107
Quoted 5 lines
mm/unmapped-alloc.c new file +5
-0+set_unmapped_order (page, order); +add_to_free_list (page, order); +spin_unlock_irqrestore (&free_area->lock, flags); +} +The page has to be zeroed before it goes back on the list, right? I didn't see it.
Quoted 1 line
You are right, I missed it.
The text_poke() family is probably the way on x86, but I don't think there is a cross-arch way that doesn't involve kernel shootdowns...
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Mike RapoportQuoted 11 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.
Quoted 6 lines
This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Hello,
To me it seems unnecessary to increase pageblock bitmap size just to distinguish if it is allocated with __GFP_UNMAPPED.
I think this can be implemented as an independent cache on top of buddy allocator, and introducing new API for unmapped page allocation and freeing?
Thanks,
Hyeonggon
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 19 lines · Hyeonggon Yoo
Quoted 11 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.Quoted 6 lines
This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Quoted 8 lines
Hello,
To me it seems unnecessary to increase pageblock bitmap size just to distinguish if it is allocated with
__GFP_UNMAPPED.I think this can be implemented as an independent cache on top of buddy allocator, and introducing new API for unmapped page allocation and freeing?
Yes, it could. But with such API we'd also need to take care of mapping/unmapping these pages for the modules use case which is not that difficult, but still more complex than a flag to vmalloc().
As for secretmem, freeing of secretmem pages happens in the page cache, so changing that to use, say, unmapped_free() would be quite intrusive.
Another reason to mark the entire pageblock as unmapped is possibility to steal pages from that pageblock into the unmmaped cache when they become free. When we allocate pages to replenish the unmapped cache, we split the large mapping in the direct map, so even the pages in the same pageblock that do not get into the cache are still mapped at PTE level. When they are freed, they will be put in the cache and so fewer explicit cache refills that split the large mappings will be required.
Quoted 2 lines
Thanks,
Hyeonggon
Signature · 2 lines
Sincerely yours, Mike.
Michal Hocko
reviewerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Mike RapoportQuoted 11 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.
Quoted 6 lines
This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Why do we need a dedicated gfp flag for all this when a dedicated allocator is used anyway. What prevents users to call unmapped_pages_{alloc,free}?
Signature · 2 lines
Michal Hocko SUSE Labs
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 16 lines · Michal Hocko
Quoted 15 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.
Quoted 5 lines
Quoted 2 lines
The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Why do we need a dedicated gfp flag for all this when a dedicated allocator is used anyway. What prevents users to call unmapped_pages_{alloc,free}?
Using unmapped_pages_{alloc,free} adds complexity to the users which IMO outweighs the cost of a dedicated gfp flag.
For modules we'd have to make x86::module_{alloc,free}() take care of mapping and unmapping the allocated pages in the modules virtual address range. This also might become relevant for another architectures in future and than we'll have several complex module_alloc()s.
And for secretmem while using unmapped_pages_alloc() is easy, the free path becomes really complex because actual page freeing for fd-based memory is deeply buried in the page cache code.
My gut feeling is that for PKS using a gfp flag would save a lot of hassle as well.
I also think that some of the core buddy allocator code might be reused and unmapped_pages_{alloc,free} could be statics in mm/page_alloc.c and won't be exposed at all. For now I've just copied free list helpers to a separate file out of laziness.
Quoted 3 lines
Signature · 2 lines
Michal Hocko SUSE Labs
Signature · 2 lines
Sincerely yours, Mike.
Michal Hocko
reviewerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 17 lines · Mike Rapoport
Quoted 16 lines · Michal Hocko
Quoted 15 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Why do we need a dedicated gfp flag for all this when a dedicated allocator is used anyway. What prevents users to call unmapped_pages_{alloc,free}?
Using unmapped_pages_{alloc,free} adds complexity to the users which IMO outweighs the cost of a dedicated gfp flag.
Aren't those users rare and very special anyway?
Quoted 4 lines
For modules we'd have to make x86::module_{alloc,free}() take care of mapping and unmapping the allocated pages in the modules virtual address range. This also might become relevant for another architectures in future and than we'll have several complex module_alloc()s.
The module_alloc use is lacking any justification. More context would be more than useful. Also vmalloc support for the proposed __GFP_UNMAPPED likely needs more explanation as well.
Quoted 3 lines
And for secretmem while using
unmapped_pages_alloc()is easy, the free path becomes really complex because actual page freeing for fd-based memory is deeply buried in the page cache code.
Why is that a problem? You already hook into the page freeing path and special case unmapped memory.
Quoted 2 lines
My gut feeling is that for PKS using a gfp flag would save a lot of hassle as well.
Well, my take on this is that this is not a generic page allocator functionality. It is clearly an allocator on top of the page allocator. In general gfp flags are scarce and convenience argument usually fires back later on in hard to predict ways. So I've learned to be careful here. I am not saying this is a no-go but right now I do not see any acutal advantage. The vmalloc usecase could be interesting in that regards but it is not really clear to me whether this is a good idea in the first place.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 21 lines · Michal Hocko
Quoted 20 lines · Mike Rapoport
Quoted 19 lines · Michal Hocko
Quoted 17 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Why do we need a dedicated gfp flag for all this when a dedicated allocator is used anyway. What prevents users to call unmapped_pages_{alloc,free}?
Using unmapped_pages_{alloc,free} adds complexity to the users which IMO outweighs the cost of a dedicated gfp flag.
Aren't those users rare and very special anyway?
I think it's mostly about the freeing that can happen from a generic context not aware of the special allocation, so it's not about how rare it is, but how complex would be to determine exhaustively those contexts and do something in them.
Quoted 9 lines
Quoted 4 lines
For modules we'd have to make x86::module_{alloc,free}() take care of mapping and unmapping the allocated pages in the modules virtual address range. This also might become relevant for another architectures in future and than we'll have several complex module_alloc()s.
The
module_allocuse is lacking any justification. More context would be more than useful. Also vmalloc support for the proposed__GFP_UNMAPPEDlikely needs more explanation as well.
Quoted 6 lines
Quoted 3 lines
And for secretmem while using
unmapped_pages_alloc()is easy, the free path becomes really complex because actual page freeing for fd-based memory is deeply buried in the page cache code.Why is that a problem? You already hook into the page freeing path and special case unmapped memory.
But the proposal of unmapped_pages_free() would suggest this would no longer be the case?
But maybe we could, as a compromise, provide unmapped_pages_alloc() to get rid of the new __GFP flag, provide unmapped_pages_free() to annotate places that are known to free unmapped memory explicitly, but the generic page freeing would also keep the hook?
Quoted 12 lines
Quoted 2 lines
My gut feeling is that for PKS using a gfp flag would save a lot of hassle as well.
Well, my take on this is that this is not a generic page allocator functionality. It is clearly an allocator on top of the page allocator. In general gfp flags are scarce and convenience argument usually fires back later on in hard to predict ways. So I've learned to be careful here. I am not saying this is a no-go but right now I do not see any acutal advantage. The vmalloc usecase could be interesting in that regards but it is not really clear to me whether this is a good idea in the first place.
Michal Hocko
reviewerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 28 lines · Vlastimil Babka
Quoted 27 lines · Michal Hocko
Quoted 25 lines · Mike Rapoport
Quoted 16 lines · Michal Hocko
Quoted 15 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.Quoted 5 lines
Quoted 2 lines
The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Why do we need a dedicated gfp flag for all this when a dedicated allocator is used anyway. What prevents users to call unmapped_pages_{alloc,free}?
Using unmapped_pages_{alloc,free} adds complexity to the users which IMO outweighs the cost of a dedicated gfp flag.
Quoted 6 lines
Quoted 1 line
Aren't those users rare and very special anyway?
I think it's mostly about the freeing that can happen from a generic context not aware of the special allocation, so it's not about how rare it is, but how complex would be to determine exhaustively those contexts and do something in them.
Yes, I can see a challenge with put_page users but that is not really related to the gfp flag as those are only relevant for the allocation context.
Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
For modules we'd have to make x86::module_{alloc,free}() take care of mapping and unmapping the allocated pages in the modules virtual address range. This also might become relevant for another architectures in future and than we'll have several complex module_alloc()s.
The
module_allocuse is lacking any justification. More context would be more than useful. Also vmalloc support for the proposed__GFP_UNMAPPEDlikely needs more explanation as well.Quoted 3 lines
And for secretmem while using
unmapped_pages_alloc()is easy, the free path becomes really complex because actual page freeing for fd-based memory is deeply buried in the page cache code.
Quoted 5 lines
Quoted 2 lines
Why is that a problem? You already hook into the page freeing path and special case unmapped memory.
But the proposal of
unmapped_pages_free()would suggest this would no longer be the case?
I can see a check in the freeing path.
Quoted 4 lines
But maybe we could, as a compromise, provide
unmapped_pages_alloc()to get rid of the new__GFPflag, provideunmapped_pages_free()to annotate places that are known to free unmapped memory explicitly, but the generic page freeing would also keep the hook?
Honestly I do not see a different option if those pages are to be reference counted. Unless they can use a destructor concept like hugetlb pages. At least secret mem usecase cannot AFAICS.
Signature · 2 lines
Michal Hocko SUSE Labs
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Michal HockoQuoted 29 lines · Michal Hocko
Quoted 17 lines · Mike Rapoport
Quoted 16 lines · Michal Hocko
Quoted 15 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Why do we need a dedicated gfp flag for all this when a dedicated allocator is used anyway. What prevents users to call unmapped_pages_{alloc,free}?
Using unmapped_pages_{alloc,free} adds complexity to the users which IMO outweighs the cost of a dedicated gfp flag.
Aren't those users rare and very special anyway?
Quoted 8 lines
Quoted 4 lines
For modules we'd have to make x86::module_{alloc,free}() take care of mapping and unmapping the allocated pages in the modules virtual address range. This also might become relevant for another architectures in future and than we'll have several complex module_alloc()s.
The
module_allocuse is lacking any justification. More context would be more than useful. Also vmalloc support for the proposed__GFP_UNMAPPEDlikely needs more explanation as well.
Right now module_alloc() boils down to vmalloc() with the virtual range limited to the modules area. The allocated chunk contains both code and data. When CONFIG_STRICT_MODULE_RWX is set, parts of the memory allocated with module_alloc() remapped with different permissions both in vmalloc address space and in the direct map. The change of permissions for small ranges causes splits of large pages in the direct map.
If we were to use unmapped_pages_alloc() in modules_alloc(), we would have to implement the part of vmalloc() that reserves the virtual addresses and maps the allocated memory there in module_alloc().
Quoted 6 lines
Quoted 3 lines
And for secretmem while using
unmapped_pages_alloc()is easy, the free path becomes really complex because actual page freeing for fd-based memory is deeply buried in the page cache code.Why is that a problem? You already hook into the page freeing path and special case unmapped memory.
I didn't say there is a problem with unmapped_pages_alloc() in secretmem, I said there is a problem with unmapped_pages_free() and hence are the special case for unmapped memory in the freeing path.
Quoted 11 lines
Quoted 2 lines
My gut feeling is that for PKS using a gfp flag would save a lot of hassle as well.
Well, my take on this is that this is not a generic page allocator functionality. It is clearly an allocator on top of the page allocator. In general gfp flags are scarce and convenience argument usually fires back later on in hard to predict ways. So I've learned to be careful here. I am not saying this is a no-go but right now I do not see any acutal advantage. The vmalloc usecase could be interesting in that regards but it is not really clear to me whether this is a good idea in the first place.
I don't see the usage of a gfp flag as a convenience argument, but rather it feels for me that a gfp flag will cause less maintenance burden. Of course this is subjective.
And although this is an allocator on top of the page allocator, it is still very tightly coupled with the core page allocator. I'm still think that using a migrate type for this would have been more elegant, but I realize that a migrate type would have more impact on the allocation path.
Quoted 3 lines
Signature · 2 lines
Michal Hocko SUSE Labs
Signature · 2 lines
Sincerely yours, Mike.
Michal Hocko
reviewerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 39 lines · Mike Rapoport
Quoted 29 lines · Michal Hocko
Quoted 17 lines · Mike Rapoport
Quoted 16 lines · Michal Hocko
Quoted 15 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Why do we need a dedicated gfp flag for all this when a dedicated allocator is used anyway. What prevents users to call unmapped_pages_{alloc,free}?
Using unmapped_pages_{alloc,free} adds complexity to the users which IMO outweighs the cost of a dedicated gfp flag.
Aren't those users rare and very special anyway?
Quoted 8 lines
Quoted 4 lines
For modules we'd have to make x86::module_{alloc,free}() take care of mapping and unmapping the allocated pages in the modules virtual address range. This also might become relevant for another architectures in future and than we'll have several complex module_alloc()s.
The
module_allocuse is lacking any justification. More context would be more than useful. Also vmalloc support for the proposed__GFP_UNMAPPEDlikely needs more explanation as well.
Quoted 6 lines
Right now
module_alloc()boils down tovmalloc()with the virtual range limited to the modules area. The allocated chunk contains both code and data. WhenCONFIG_STRICT_MODULE_RWXis set, parts of the memory allocated withmodule_alloc()remapped with different permissions both in vmalloc address space and in the direct map. The change of permissions for small ranges causes splits of large pages in the direct map.
OK, so you want to reduce that direct map fragmentation? Is that a real problem? My impression is that modules are mostly static thing. BPF might be a different thing though. I have a recollection that BPF guys were dealing with direct map fragmention as well.
Quoted 3 lines
If we were to use
unmapped_pages_alloc()inmodules_alloc(), we would have to implement the part ofvmalloc()that reserves the virtual addresses and maps the allocated memory there inmodule_alloc().
Another option would be to provide an allocator for the backing pages to vmalloc. But I do agree that a gfp flag is a less laborous way to achieve the same. So the primary question really is whether we really need vmalloc support for unmapped memory.
Signature · 2 lines
Michal Hocko SUSE Labs
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 40 lines · Michal Hocko
Quoted 39 lines · Mike Rapoport
Quoted 37 lines · Michal Hocko
Quoted 17 lines · Mike Rapoport
Quoted 16 lines · Michal Hocko
Quoted 15 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
Why do we need a dedicated gfp flag for all this when a dedicated allocator is used anyway. What prevents users to call unmapped_pages_{alloc,free}?
Using unmapped_pages_{alloc,free} adds complexity to the users which IMO outweighs the cost of a dedicated gfp flag.
Aren't those users rare and very special anyway?
Quoted 4 lines
For modules we'd have to make x86::module_{alloc,free}() take care of mapping and unmapping the allocated pages in the modules virtual address range. This also might become relevant for another architectures in future and than we'll have several complex module_alloc()s.
The
module_allocuse is lacking any justification. More context would be more than useful. Also vmalloc support for the proposed__GFP_UNMAPPEDlikely needs more explanation as well.
Quoted 8 lines
Quoted 6 lines
Right now
module_alloc()boils down tovmalloc()with the virtual range limited to the modules area. The allocated chunk contains both code and data. WhenCONFIG_STRICT_MODULE_RWXis set, parts of the memory allocated withmodule_alloc()remapped with different permissions both in vmalloc address space and in the direct map. The change of permissions for small ranges causes splits of large pages in the direct map.OK, so you want to reduce that direct map fragmentation?
Yes.
Quoted 1 line
Is that a real problem?
A while ago Intel folks published report [1] that showed better performance with large pages in the direct map for majority of benchmarks.
Quoted 3 lines
My impression is that modules are mostly static thing. BPF might be a different thing though. I have a recollection that BPF guys were dealing with direct map fragmention as well.
Modules are indeed static, but module_alloc() used by anything that allocates code pages, e.g. kprobes, ftrace and BPF. Besides, Thomas mentioned that having code in 2M pages reduces iTLB pressure [2], but that's not only about avoiding the splits in the direct map but also about using large mappings in the modules address space.
BPF guys suggested an allocator for executable memory [3] mainly because they've seen performance improvement of 0.6% - 0.9% in their setups [4].
Quoted 8 lines
Quoted 3 lines
If we were to use
unmapped_pages_alloc()inmodules_alloc(), we would have to implement the part ofvmalloc()that reserves the virtual addresses and maps the allocated memory there inmodule_alloc().Another option would be to provide an allocator for the backing pages to vmalloc. But I do agree that a gfp flag is a less laborous way to achieve the same. So the primary question really is whether we really need vmalloc support for unmapped memory.
I'm not sure I follow here. module_alloc() is essentially an alias to vmalloc(), so to reduce direct map fragmentation caused by code allocations the most sensible way IMO is to support unmapped memory in vmalloc().
I also think vmalloc with unmmapped pages can provide backing pages for execmem_alloc() Song proposed.
Quoted 3 lines
Signature · 2 lines
Michal Hocko SUSE Labs
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/all/87mt86rbvy.ffs@tglx/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/bpf/[email protected]/
Signature · 2 lines
Sincerely yours, Mike.
Michal Hocko
reviewerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 1 line · Mike Rapoport
On Tue, Mar 28, 2023 at 09:39:37AM +0200, Michal Hocko wrote:
[...]
Quoted 13 lines
Quoted 1 line
OK, so you want to reduce that direct map fragmentation?
Yes.
Quoted 1 line
Is that a real problem?
A while ago Intel folks published report [1] that showed better performance with large pages in the direct map for majority of benchmarks.
Quoted 3 lines
My impression is that modules are mostly static thing. BPF might be a different thing though. I have a recollection that BPF guys were dealing with direct map fragmention as well.
Quoted 8 lines
Modules are indeed static, but
module_alloc()used by anything that allocates code pages, e.g. kprobes, ftrace and BPF. Besides, Thomas mentioned that having code in 2M pages reduces iTLB pressure [2], but that's not only about avoiding the splits in the direct map but also about using large mappings in the modules address space.BPF guys suggested an allocator for executable memory [3] mainly because they've seen performance improvement of 0.6% - 0.9% in their setups [4].
These are fair arguments and it would have been better to have them in the RFC. Also it is not really clear to me what is the actual benefit of the unmapping for those usecases. I do get they want to benefit from caching on the same permission setup but do they need unmapping as well?
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
If we were to use
unmapped_pages_alloc()inmodules_alloc(), we would have to implement the part ofvmalloc()that reserves the virtual addresses and maps the allocated memory there inmodule_alloc().
Quoted 8 lines
Quoted 4 lines
Another option would be to provide an allocator for the backing pages to vmalloc. But I do agree that a gfp flag is a less laborous way to achieve the same. So the primary question really is whether we really need vmalloc support for unmapped memory.
I'm not sure I follow here.
module_alloc()is essentially an alias tovmalloc(), so to reduce direct map fragmentation caused by code allocations the most sensible way IMO is to support unmapped memory invmalloc().
What I meant to say is that vmalloc is currently using the page allocator (resp bulk allocator) for the backing storage. I can imagine an extension to replace this default allocator by something else (e.g. a given allocation function). This would be more generic and it would allow different usecases (e.g. benefit from caching withtout doing the actual unmapping).
Quoted 2 lines
I also think vmalloc with unmmapped pages can provide backing pages for
execmem_alloc()Song proposed.
Why would you need to have execmem_alloc have its memory virtually mapped into vmalloc space?
Quoted 8 lines
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/all/87mt86rbvy.ffs@tglx/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/bpf/[email protected]/Signature · 2 lines
Sincerely yours, Mike.
Signature · 2 lines
Michal Hocko SUSE Labs
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 22 lines · Michal Hocko
Quoted 1 line · Mike Rapoport
On Tue, Mar 28, 2023 at 09:39:37AM +0200, Michal Hocko wrote:
[...]
Quoted 19 lines
Quoted 1 line
OK, so you want to reduce that direct map fragmentation?
Yes.
Quoted 1 line
Is that a real problem?
A while ago Intel folks published report [1] that showed better performance with large pages in the direct map for majority of benchmarks.
Quoted 3 lines
My impression is that modules are mostly static thing. BPF might be a different thing though. I have a recollection that BPF guys were dealing with direct map fragmention as well.
Modules are indeed static, but
module_alloc()used by anything that allocates code pages, e.g. kprobes, ftrace and BPF. Besides, Thomas mentioned that having code in 2M pages reduces iTLB pressure [2], but that's not only about avoiding the splits in the direct map but also about using large mappings in the modules address space.
Quoted 7 lines
Quoted 2 lines
BPF guys suggested an allocator for executable memory [3] mainly because they've seen performance improvement of 0.6% - 0.9% in their setups [4].
These are fair arguments and it would have been better to have them in the RFC. Also it is not really clear to me what is the actual benefit of the unmapping for those usecases. I do get they want to benefit from caching on the same permission setup but do they need unmapping as well?
The pages allocated with module_alloc() get different permissions depending on whether they belong to text, rodata, data etc. The permissions are updated in both vmalloc address space and in the direct map. The updates to the direct map cause splits of the large pages. If we cache large pages as unmapped we take out the entire 2M page from the direct map and then if/when it becomes free it can be returned to the direct map as a 2M page.
Generally, the unmapped allocations are intended for use-cases that anyway map the memory elsewhere than direct map and need to modify direct mappings of the memory, be it modules_alloc(), secretmem, PKS page tables or maybe even some of the encrypted VM memory.
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
If we were to use
unmapped_pages_alloc()inmodules_alloc(), we would have to implement the part ofvmalloc()that reserves the virtual addresses and maps the allocated memory there inmodule_alloc().Quoted 8 lines
Quoted 4 lines
Another option would be to provide an allocator for the backing pages to vmalloc. But I do agree that a gfp flag is a less laborous way to achieve the same. So the primary question really is whether we really need vmalloc support for unmapped memory.
I'm not sure I follow here.
module_alloc()is essentially an alias tovmalloc(), so to reduce direct map fragmentation caused by code allocations the most sensible way IMO is to support unmapped memory invmalloc().
Quoted 6 lines
What I meant to say is that vmalloc is currently using the page allocator (resp bulk allocator) for the backing storage. I can imagine an extension to replace this default allocator by something else (e.g. a given allocation function). This would be more generic and it would allow different usecases (e.g. benefit from caching withtout doing the actual unmapping).
The whole point of unmapped cache is to allow non-default permissions in the direct map without splitting large pages there. So the cache that unmaps large pages in the direct map and then handles out subpages of that page seems like the most logical thing to do. With the current use cases the callers anyway map these pages at different virtual addresses, i.e. page cache or vmalloc.
Quoted 5 lines
Quoted 2 lines
I also think vmalloc with unmmapped pages can provide backing pages for
execmem_alloc()Song proposed.Why would you need to have
execmem_allochave its memory virtually mapped into vmalloc space?
Currently all the memory allocated for code is managed in a subset of vmalloc() space. The intention of execmem_alloc() was to replace module_alloc() for the code pages, so it's natural that it will use the same virtual ranges.
But anyway, execmem_alloc() is a long shot as it also requires quite a refactoring of modules loading.
Quoted 12 lines
Quoted 8 lines
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/all/87mt86rbvy.ffs@tglx/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/bpf/[email protected]/Signature · 2 lines
Sincerely yours, Mike.Signature · 2 lines
Michal Hocko SUSE Labs
Signature · 2 lines
Sincerely yours, Mike.
Michal Hocko
reviewerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 31 lines · Mike Rapoport
Quoted 22 lines · Michal Hocko
Quoted 1 line · Mike Rapoport
On Tue, Mar 28, 2023 at 09:39:37AM +0200, Michal Hocko wrote:
[...]
Quoted 19 lines
Quoted 1 line
OK, so you want to reduce that direct map fragmentation?
Yes.
Quoted 1 line
Is that a real problem?
A while ago Intel folks published report [1] that showed better performance with large pages in the direct map for majority of benchmarks.
Quoted 3 lines
My impression is that modules are mostly static thing. BPF might be a different thing though. I have a recollection that BPF guys were dealing with direct map fragmention as well.
Modules are indeed static, but
module_alloc()used by anything that allocates code pages, e.g. kprobes, ftrace and BPF. Besides, Thomas mentioned that having code in 2M pages reduces iTLB pressure [2], but that's not only about avoiding the splits in the direct map but also about using large mappings in the modules address space.Quoted 7 lines
Quoted 2 lines
BPF guys suggested an allocator for executable memory [3] mainly because they've seen performance improvement of 0.6% - 0.9% in their setups [4].
These are fair arguments and it would have been better to have them in the RFC. Also it is not really clear to me what is the actual benefit of the unmapping for those usecases. I do get they want to benefit from caching on the same permission setup but do they need unmapping as well?
Quoted 4 lines
The pages allocated with
module_alloc()get different permissions depending on whether they belong to text, rodata, data etc. The permissions are updated in both vmalloc address space and in the direct map. The updates to the direct map cause splits of the large pages.
That much is clear (wouldn't hurt to mention that in the changelog though).
Quoted 8 lines
If we cache large pages as
unmapped we take out the entire 2M page from the direct map and then if/when it becomes free it can be returned to the direct map as a 2M page.Generally, the unmapped allocations are intended for use-cases that anyway map the memory elsewhere than direct map and need to modify direct mappings of the memory, be it
modules_alloc(), secretmem, PKS page tables or maybe even some of the encrypted VM memory.
I believe we are still not on the same page here. I do understand that you want to re-use the caching capability of the unmapped_pages_alloc for modules allocations as well. My question is whether module_alloc really benefits from the fact that the memory is unmapped?
I guess you want to say that it does because it wouldn't have to change the permission for the direct map but I do not see that anywhere in the patch... Also consinder the slow path where module_alloc needs to allocate a fresh (huge)page and unmap it. Does the overhead of the unmapping suits needs of all module_alloc users? Module loader is likely not interesting as it tends to be rather static but what about BPF programs?
[...]
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
I also think vmalloc with unmmapped pages can provide backing pages for
execmem_alloc()Song proposed.Why would you need to have
execmem_allochave its memory virtually mapped into vmalloc space?Currently all the memory allocated for code is managed in a subset of
vmalloc()space. The intention ofexecmem_alloc()was to replacemodule_alloc()for the code pages, so it's natural that it will use the same virtual ranges.
Ohh, sorry my bad. I have only now realized I have misread and thought about secretmem_alloc...
Signature · 2 lines
Michal Hocko SUSE Labs
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 49 lines · Michal Hocko
Quoted 31 lines · Mike Rapoport
Quoted 22 lines · Michal Hocko
Quoted 1 line · Mike Rapoport
On Tue, Mar 28, 2023 at 09:39:37AM +0200, Michal Hocko wrote:
[...]
Quoted 19 lines
Quoted 1 line
OK, so you want to reduce that direct map fragmentation?
Yes.
Quoted 1 line
Is that a real problem?
A while ago Intel folks published report [1] that showed better performance with large pages in the direct map for majority of benchmarks.
Quoted 3 lines
My impression is that modules are mostly static thing. BPF might be a different thing though. I have a recollection that BPF guys were dealing with direct map fragmention as well.
Modules are indeed static, but
module_alloc()used by anything that allocates code pages, e.g. kprobes, ftrace and BPF. Besides, Thomas mentioned that having code in 2M pages reduces iTLB pressure [2], but that's not only about avoiding the splits in the direct map but also about using large mappings in the modules address space.Quoted 7 lines
Quoted 2 lines
BPF guys suggested an allocator for executable memory [3] mainly because they've seen performance improvement of 0.6% - 0.9% in their setups [4].
These are fair arguments and it would have been better to have them in the RFC. Also it is not really clear to me what is the actual benefit of the unmapping for those usecases. I do get they want to benefit from caching on the same permission setup but do they need unmapping as well?
Quoted 4 lines
The pages allocated with
module_alloc()get different permissions depending on whether they belong to text, rodata, data etc. The permissions are updated in both vmalloc address space and in the direct map. The updates to the direct map cause splits of the large pages.That much is clear (wouldn't hurt to mention that in the changelog though).
Quoted 8 lines
If we cache large pages as
unmapped we take out the entire 2M page from the direct map and then if/when it becomes free it can be returned to the direct map as a 2M page.Generally, the unmapped allocations are intended for use-cases that anyway map the memory elsewhere than direct map and need to modify direct mappings of the memory, be it
modules_alloc(), secretmem, PKS page tables or maybe even some of the encrypted VM memory.
Quoted 8 lines
I believe we are still not on the same page here. I do understand that you want to re-use the caching capability of the
unmapped_pages_allocfor modules allocations as well. My question is whethermodule_allocreally benefits from the fact that the memory is unmapped?I guess you want to say that it does because it wouldn't have to change the permission for the direct map but I do not see that anywhere in the patch...
This happens automagically outside the patch :)
Currently, to change memory permissions modules code calls set_memory APIs and passes vmalloced address to them. set_memory functions lookup the direct map alias and update the permissions there as well.
If the memory allocated with module_alloc() is unmapped in the direct map, there won't be an alias for set_memory APIs to update.
Quoted 5 lines
Also consinder the slow path where
module_allocneeds to allocate a fresh (huge)page and unmap it. Does the overhead of the unmapping suits needs of allmodule_allocusers? Module loader is likely not interesting as it tends to be rather static but what about BPF programs?
The overhead of unmapping pages in the direct map on allocation path will be offset by reduced overhead of updating permissions in the direct map after the allocation. Both are using the same APIs and if today the permission update causes a split of a large page, unmapping of a large page won't.
Of course in a loaded system unmapped_alloc() won't be able to always allocated large pages to replenish the cache, but still there will be fewer updates to the direct map.
Quoted 3 lines
Signature · 2 lines
Michal Hocko SUSE Labs
Signature · 2 lines
Sincerely yours, Mike.
Michal Hocko
reviewerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 79 lines · Mike Rapoport
Quoted 49 lines · Michal Hocko
Quoted 31 lines · Mike Rapoport
Quoted 22 lines · Michal Hocko
Quoted 1 line · Mike Rapoport
On Tue, Mar 28, 2023 at 09:39:37AM +0200, Michal Hocko wrote:
[...]
Quoted 19 lines
Quoted 1 line
OK, so you want to reduce that direct map fragmentation?
Yes.
Quoted 1 line
Is that a real problem?
A while ago Intel folks published report [1] that showed better performance with large pages in the direct map for majority of benchmarks.
Quoted 3 lines
My impression is that modules are mostly static thing. BPF might be a different thing though. I have a recollection that BPF guys were dealing with direct map fragmention as well.
Modules are indeed static, but
module_alloc()used by anything that allocates code pages, e.g. kprobes, ftrace and BPF. Besides, Thomas mentioned that having code in 2M pages reduces iTLB pressure [2], but that's not only about avoiding the splits in the direct map but also about using large mappings in the modules address space.Quoted 7 lines
Quoted 2 lines
BPF guys suggested an allocator for executable memory [3] mainly because they've seen performance improvement of 0.6% - 0.9% in their setups [4].
These are fair arguments and it would have been better to have them in the RFC. Also it is not really clear to me what is the actual benefit of the unmapping for those usecases. I do get they want to benefit from caching on the same permission setup but do they need unmapping as well?
Quoted 4 lines
The pages allocated with
module_alloc()get different permissions depending on whether they belong to text, rodata, data etc. The permissions are updated in both vmalloc address space and in the direct map. The updates to the direct map cause splits of the large pages.That much is clear (wouldn't hurt to mention that in the changelog though).
Quoted 8 lines
If we cache large pages as
unmapped we take out the entire 2M page from the direct map and then if/when it becomes free it can be returned to the direct map as a 2M page.Generally, the unmapped allocations are intended for use-cases that anyway map the memory elsewhere than direct map and need to modify direct mappings of the memory, be it
modules_alloc(), secretmem, PKS page tables or maybe even some of the encrypted VM memory.Quoted 8 lines
I believe we are still not on the same page here. I do understand that you want to re-use the caching capability of the
unmapped_pages_allocfor modules allocations as well. My question is whethermodule_allocreally benefits from the fact that the memory is unmapped?I guess you want to say that it does because it wouldn't have to change the permission for the direct map but I do not see that anywhere in the patch...
This happens automagically outside the patch :)
Currently, to change memory permissions modules code calls
set_memoryAPIs and passes vmalloced address to them.set_memoryfunctions lookup the direct map alias and update the permissions there as well.
If the memory allocated withmodule_alloc()is unmapped in the direct map, there won't be an alias forset_memoryAPIs to update.Quoted 5 lines
Also consinder the slow path where
module_allocneeds to allocate a fresh (huge)page and unmap it. Does the overhead of the unmapping suits needs of allmodule_allocusers? Module loader is likely not interesting as it tends to be rather static but what about BPF programs?The overhead of unmapping pages in the direct map on allocation path will be offset by reduced overhead of updating permissions in the direct map after the allocation. Both are using the same APIs and if today the permission update causes a split of a large page, unmapping of a large page won't.
Quoted 3 lines
Of course in a loaded system
unmapped_alloc()won't be able to always allocated large pages to replenish the cache, but still there will be fewer updates to the direct map.
Ok, all of that is a changelog material. I would recommend to go this way. Start by the simple unmapped_pages_alloc interface and use it for the secret memory. There shouldn't be anything controversial there. In a follow up patch add a support for the vmalloc which would add a new gfp flag with a justification that this is the simplest way to support module_alloc usecase and do not feel shy of providing as much context as you can. Ideally with some numbers for the best/worst/avg cases.
Thanks
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Mike RapoportMike, please Cc linux-modules if you want modules folks' input as well ;)
Quoted 14 lines · Mike Rapoport
Quoted 1 line · Michal Hocko
OK, so you want to reduce that direct map fragmentation?
Yes.
Quoted 1 line
Is that a real problem?
A while ago Intel folks published report [1] that showed better performance with large pages in the direct map for majority of benchmarks.
Quoted 3 lines
My impression is that modules are mostly static thing. BPF might be a different thing though. I have a recollection that BPF guys were dealing with direct map fragmention as well.
Quoted 5 lines
Modules are indeed static, but
module_alloc()used by anything that allocates code pages, e.g. kprobes, ftrace and BPF. Besides, Thomas mentioned that having code in 2M pages reduces iTLB pressure [2], but that's not only about avoiding the splits in the direct map but also about using large mappings in the modules address space.
It is easily overlooked why such things create direct fragmentation -- it's because of the special permission stuff done, module_alloc() targets memory which can be executed somehow.
Quoted 2 lines
BPF guys suggested an allocator for executable memory [3] mainly because they've seen performance improvement of 0.6% - 0.9% in their setups [4].
The performance metrics were completely opaque to some synthetic environment and our goal is to showcase real value with reproducible performance benchmarks. Since now Song is convinced that modules need to be a first class citizen in order to generalize a special allocator we may sooner rather than later real reproducible performance data to show the benefit of such a special allocator. One of the big differences with eBPF programs is that modules *can* be rather large in size. What is the average size of modules? Well let's take a look:
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| wc -l
9173
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
175.1
175 MiB is pretty large.
Ignoring the top 5 piggies:
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r |head -5 58315248 - ./drivers/gpu/drm/amd/amdgpu/amdgpu.ko 29605592 - ./drivers/gpu/drm/i915/i915.ko
18591256 - ./drivers/gpu/drm/nouveau/nouveau.ko
16867048 - ./fs/xfs/xfs.ko
14209440 - ./fs/btrfs/btrfs.ko
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-5)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}' 160.54
Ignoring the top 10 largest modules:
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-10)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}' 154.656
Ignoring the top 20 piggies:
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-20)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}' 146.384
Ignoring the top 100 bloated modules:
mcgrof@bigtwin mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-100)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}' 117.97
Ignoring the top 1000 bloated modules:
mcgrof@bigtwin mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-1000)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
64.4686
Ignoring the top 2000 bloated modules:
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-2000)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
48.7869
Ignoring top 3000
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-3000)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
39.6037
Ignoring top 4000
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-4000)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
33.106
Ignoring top 5000
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-5000)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
28.0925
But at least on the driver front we know we won't always have loaded *all* GPU drivers, but *one*. So the math needs to consider what module sizes are for modules actually used.
Let's see what the average module size is on on a big system:
mcgrof@bigtwin ~ $ lsmod | grep -v Module| awk '{print $1}' | xargs sudo modinfo --field filename | xargs stat -c "%s - %n" | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
313.432
On a small desktop:
mcgrof@desktop ~ $ lsmod | grep -v Module| awk '{print $1}' | xargs sudo modinfo --field filename | xargs stat -c "%s - %n" | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
292.786
For each finit_module we also do a vmalloc twice actually, one for the kernel_read_*() which lets us read the file from userspace, and then a second set of allocations where we copy data over, each step either using vzalloc() or module_alloc() (vmalloc() with special permissions), this is more easily reflected and visible now on linux-next with Song's new module_memory_alloc().
However -- for the context of *this* effort, we're only talking about the executable sections, the areas we'd use module_alloc() for.
On a big system:
mcgrof@bigtwin ~ $ lsmod | grep -v Module| awk '{print $1}' | xargs sudo modinfo --field filename | xargs size --radix=10 | awk '{print $1}'| grep -v text| awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
88.7964
On a small desktop:
mcgrof@desktop ~ $ lsmod | grep -v Module| awk '{print $1}' | xargs sudo modinfo --field filename | xargs size --radix=10 | awk '{print $1}'| grep -v text| awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}'
92.1473
Regardless, the amount of memory allocated is pretty significant, to the point a 400 CPU system easily run out of vmap space (yes the kernel does some stupid stuff, which we're correcting over time):
https://lkml.kernel.org/r/[email protected]
To help with this we have some recent efforts to help with this pressure on vmap space:
https://lkml.kernel.org/r/[email protected]
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
If we were to use
unmapped_pages_alloc()inmodules_alloc(), we would have to implement the part ofvmalloc()that reserves the virtual addresses and maps the allocated memory there inmodule_alloc().
Quoted 8 lines
Quoted 4 lines
Another option would be to provide an allocator for the backing pages to vmalloc. But I do agree that a gfp flag is a less laborous way to achieve the same. So the primary question really is whether we really need vmalloc support for unmapped memory.
I'm not sure I follow here.
module_alloc()is essentially an alias tovmalloc(), so to reduce direct map fragmentation caused by code allocations the most sensible way IMO is to support unmapped memory invmalloc().
The big win also would be to use huge pages from a performance point of view, specially reducing iTLB pressure, however that is *slightly* orthogonal to your goal of reducing direct map fragmentation. However, we should not ignore that strategy as it is a special use case which *could* be leveraged in other ways too. And so I'd prefer to see that over just a flag to hide those allocations. It would not only reduce that fragmentation, but reduce iTLB pressure which I think *is* what experimentation revealed to show more gains.
Quoted 2 lines
I also think vmalloc with unmmapped pages can provide backing pages for
execmem_alloc()Song proposed.
Only if you consider huge pages. I don't see that here.
Luis
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 6 lines · Luis Chamberlain
differences with eBPF programs is that modules *can* be rather large in size. What is the average size of modules? Well let's take a look:
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| wc -l
9173
ummm ... wc -c, surely?
Quoted 4 lines
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-5)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}' 160.54
... which invalidates all of these.
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines · Luis Chamberlain
differences with eBPF programs is that modules *can* be rather large in size. What is the average size of modules? Well let's take a look:
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| wc -l
9173ummm ... wc -c, surely?
That's the number of allmodconfig modules found.
mcgrof@fulton ~/linux (git::sysctl-next)$ find ./ -name \*.ko| head -2 ./arch/x86/crypto/twofish-x86_64.ko ./arch/x86/crypto/serpent-avx2.ko
mcgrof@fulton ~/linux (git::sysctl-next)$ find ./ -name \*.ko| head -2 | wc -l
2
mcgrof@fulton ~/linux (git::sysctl-next)$ find ./ -name \*.ko| head -2 | wc -c
70
wc -c would give a lot more. wc -l gives me the module count.
Quoted 6 lines
Quoted 4 lines
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-5)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}' 160.54
... which invalidates all of these.
Not sure ? But regardless the *.text* lookup is what we care for though which was later.
Luis
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 30 lines · Luis Chamberlain
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines · Luis Chamberlain
differences with eBPF programs is that modules *can* be rather large in size. What is the average size of modules? Well let's take a look:
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| wc -l
9173ummm ... wc -c, surely?
That's the number of allmodconfig modules found.
mcgrof@fulton ~/linux (git::sysctl-next)$ find ./ -name \*.ko| head -2
./arch/x86/crypto/twofish-x86_64.ko./arch/x86/crypto/serpent-avx2.ko
mcgrof@fulton ~/linux (git::sysctl-next)$ find ./ -name \*.ko| head -2 | wc -l
2
mcgrof@fulton ~/linux (git::sysctl-next)$ find ./ -name \*.ko| head -2 | wc -c
70wc -c would give a lot more. wc -l gives me the module count.
Quoted 5 lines
Quoted 4 lines
mcgrof@bigtwin /mirror/code/mcgrof/linux-next (git::master)$ find ./ -name \*.ko| xargs stat -c "%s - %n" | sort -n -k 1 -r | tail -$((9173-5)) | awk 'BEGIN {sum=0} {sum+=$1} END {print sum/NR/1024}' 160.54
Quoted 4 lines
Quoted 1 line
... which invalidates all of these.
Not sure ? But regardless the *.text* lookup is what we care for though which was later.
Which gets me thinking it'd be super nice if kmod tools supported querying this for us, then no fat finger could mess up the math:
For all modules available:
* Average module size
* Average .text module size
For only modules loaded:
* Average module size
* Average .text module size
Luis
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Mike RapoportQuoted 11 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.
Quoted 6 lines
This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
So I'm late to this discussion, I stumbled in because of my own run in with executable memory allocation.
I understand that post LSF this patchset seems to not be going anywhere, but OTOH there's also been a desire for better executable memory allocation; as noted by tglx and elsewhere, there _is_ a definite performance impact on page size with kernel text - I've seen numbers in the multiple single digit percentage range in the past.
This patchset does seem to me to be roughly the right approach for that, and coupled with the slab allocator for sub-page sized allocations it seems there's the potential for getting a nice interface that spans the full range of allocation sizes, from small bpf/trampoline allocations up to modules.
Is this patchset worth reviving/continuing with? Was it really just the needed module refactoring that was the blocker?
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 28 lines · Kent Overstreet
Quoted 11 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.Quoted 6 lines
This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
So I'm late to this discussion, I stumbled in because of my own run in with executable memory allocation.
I understand that post LSF this patchset seems to not be going anywhere, but OTOH there's also been a desire for better executable memory allocation; as noted by tglx and elsewhere, there _is_ a definite performance impact on page size with kernel text - I've seen numbers in the multiple single digit percentage range in the past.
Quoted 8 lines
This patchset does seem to me to be roughly the right approach for that, and coupled with the slab allocator for sub-page sized allocations it seems there's the potential for getting a nice interface that spans the full range of allocation sizes, from small bpf/trampoline allocations up to modules.
Is this patchset worth reviving/continuing with? Was it really just the needed module refactoring that was the blocker?
As I see it, this patchset only one building block out of three? four? If we are to repurpose it for code allocations it should be something like
1) allocate 2M page to fill the cache
2) remove this page from the direct map
3) map the 2M page ROX in module address space (usually some part of vmalloc address space)
4) allocate a smaller chunk of that page to the actual caller (bpf, modules, whatever)
Right now (3) and (4) won't work for modules because they mix code and data in a single allocation.
So module refactoring is a blocker and another blocker is to teach vmalloc to map the areas for the executable memory with 2M pages and probably something else.
I remember there was an attempt to add VM_ALLOW_HUGE_VMAP to x86::module_alloc(), but it caused problems and was reverted. Sorry, I could not find the lore link.
There was a related discussion here:
https://lore.kernel.org/all/[email protected]/
Signature · 2 lines
Sincerely yours, Mike.
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 49 lines · Mike Rapoport
Quoted 28 lines · Kent Overstreet
Quoted 11 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
When
set_memoryorset_direct_mapAPIs used to change attribute or permissions for chunks of several pages, the large PMD that maps these pages in the direct map must be split. Fragmenting the direct map in such manner causes TLB pressure and, eventually, performance degradation.To avoid excessive direct map fragmentation, add ability to allocate "unmapped" pages with
__GFP_UNMAPPEDflag that will cause removal of the allocated pages from the direct map and use a cache of the unmapped pages.Quoted 6 lines
This cache is replenished with higher order pages with preference for
PMD_SIZEpages when possible so that there will be fewer splits of large pages in the direct map.The cache is implemented as a buddy allocator, so it can serve high order allocations of unmapped pages.
So I'm late to this discussion, I stumbled in because of my own run in with executable memory allocation.
I understand that post LSF this patchset seems to not be going anywhere, but OTOH there's also been a desire for better executable memory allocation; as noted by tglx and elsewhere, there _is_ a definite performance impact on page size with kernel text - I've seen numbers in the multiple single digit percentage range in the past.
Quoted 8 lines
This patchset does seem to me to be roughly the right approach for that, and coupled with the slab allocator for sub-page sized allocations it seems there's the potential for getting a nice interface that spans the full range of allocation sizes, from small bpf/trampoline allocations up to modules.
Is this patchset worth reviving/continuing with? Was it really just the needed module refactoring that was the blocker?
As I see it, this patchset only one building block out of three? four? If we are to repurpose it for code allocations it should be something like
1) allocate 2M page to fill the cache
2) remove this page from the direct map
3) map the 2M page ROX in module address space (usually some part of vmalloc address space)
4) allocate a smaller chunk of that page to the actual caller (bpf, modules, whatever)
Quoted 2 lines
Right now (3) and (4) won't work for modules because they mix code and data in a single allocation.
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Thanks,
Song
[1] https://lore.kernel.org/linux-mm/[email protected]/
[...]
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I started on a slab allocator for executable memory allocations the other day (very minimal, but tested it for bcachefs and it works).
But I'd love to hear more about your current approach as well.
Cheers,
Kent
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 9 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
Yes, the API is based on Thomas's suggestion, like 90% from the discussions.
Quoted 5 lines
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I am planning to implement the allocator based on this (reuse vmap_area logic):
https://lore.kernel.org/linux-mm/[email protected]/
Thanks,
Song
Quoted 8 lines
I started on a slab allocator for executable memory allocations the other day (very minimal, but tested it for bcachefs and it works).
But I'd love to hear more about your current approach as well.
Cheers,
Kent
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 15 lines · Song Liu
Quoted 9 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
Yes, the API is based on Thomas's suggestion, like 90% from the discussions.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I am planning to implement the allocator based on this (reuse
vmap_arealogic):
Ah, you're still doing vmap_area approach.
Mike's approach looks like it'll be _much_ lighter weight and higher performance, to me. vmalloc is known to be slow compared to the buddy allocator, and with Mike's approach we're only modifying mappings once per 2 MB chunk.
I don't see anything in your code for sub-page sized allocations too, so perhaps I should keep going with my slab allocator.
Could you share your thoughts on your approach vs. Mike's? I'm newer to this area of the code than you two so maybe there's an angle I've missed :)
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 27 lines · Kent Overstreet
Quoted 15 lines · Song Liu
Quoted 9 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
Yes, the API is based on Thomas's suggestion, like 90% from the discussions.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I am planning to implement the allocator based on this (reuse
vmap_arealogic):Ah, you're still doing
vmap_areaapproach.
Quoted 7 lines
Mike's approach looks like it'll be _much_ lighter weight and higher performance, to me. vmalloc is known to be slow compared to the buddy allocator, and with Mike's approach we're only modifying mappings once per 2 MB chunk.
I don't see anything in your code for sub-page sized allocations too, so perhaps I should keep going with my slab allocator.
The vmap_area approach handles sub-page allocations. In 5/5 of set [2], we showed that multiple BPF programs share the same page with some kernel text (_etext).
Quoted 3 lines
Could you share your thoughts on your approach vs. Mike's? I'm newer to this area of the code than you two so maybe there's an angle I've missed :)
AFAICT, tree based solution (vmap_area) is more efficient than bitmap based solution.
First, for 2MiB page with 64B chunk size, we need a bitmap of
2MiB / 64B = 32k bit = 4k bytes
While the tree based solution can adapt to the number of allocations within This 2MiB page. Also, searching a free range within 4kB of bitmap may actually be slower than searching in the tree.
Second, bitmap based solution cannot handle > 2MiB allocation cleanly, while tree based solution can. For example, if a big driver uses 3MiB, the tree based allocator can allocate 4MiB for it, and use the rest 1MiB for smaller allocations.
Thanks,
Song
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 51 lines · Song Liu
Quoted 27 lines · Kent Overstreet
Quoted 15 lines · Song Liu
Quoted 9 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
Yes, the API is based on Thomas's suggestion, like 90% from the discussions.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I am planning to implement the allocator based on this (reuse
vmap_arealogic):Ah, you're still doing
vmap_areaapproach.Quoted 7 lines
Mike's approach looks like it'll be _much_ lighter weight and higher performance, to me. vmalloc is known to be slow compared to the buddy allocator, and with Mike's approach we're only modifying mappings once per 2 MB chunk.
I don't see anything in your code for sub-page sized allocations too, so perhaps I should keep going with my slab allocator.
The
vmap_areaapproach handles sub-page allocations. In 5/5 of set [2], we showed that multiple BPF programs share the same page with some kernel text (_etext).Quoted 3 lines
Could you share your thoughts on your approach vs. Mike's? I'm newer to this area of the code than you two so maybe there's an angle I've missed :)
AFAICT, tree based solution (
vmap_area) is more efficient than bitmap based solution.First, for 2MiB page with 64B chunk size, we need a bitmap of
2MiB / 64B = 32k bit = 4k bytes
Quoted 8 lines
While the tree based solution can adapt to the number of allocations within This 2MiB page. Also, searching a free range within 4kB of bitmap may actually be slower than searching in the tree.
Second, bitmap based solution cannot handle > 2MiB allocation cleanly, while tree based solution can. For example, if a big driver uses 3MiB, the tree based allocator can allocate 4MiB for it, and use the rest 1MiB for smaller allocations.
Missed one:
Third, bitmap based solution requires a "size" parameter in free(). It is an overhead for the user. Tree based solution doesn't have this issue.
Thanks,
Song
Quoted 2 lines
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 43 lines · Song Liu
Quoted 42 lines · Song Liu
Quoted 27 lines · Kent Overstreet
Quoted 15 lines · Song Liu
Quoted 9 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
Yes, the API is based on Thomas's suggestion, like 90% from the discussions.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I am planning to implement the allocator based on this (reuse
vmap_arealogic):Ah, you're still doing
vmap_areaapproach.Quoted 7 lines
Mike's approach looks like it'll be _much_ lighter weight and higher performance, to me. vmalloc is known to be slow compared to the buddy allocator, and with Mike's approach we're only modifying mappings once per 2 MB chunk.
I don't see anything in your code for sub-page sized allocations too, so perhaps I should keep going with my slab allocator.
The
vmap_areaapproach handles sub-page allocations. In 5/5 of set [2], we showed that multiple BPF programs share the same page with some kernel text (_etext).
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Could you share your thoughts on your approach vs. Mike's? I'm newer to this area of the code than you two so maybe there's an angle I've missed :)
AFAICT, tree based solution (
vmap_area) is more efficient than bitmap based solution.
Tree based requires quite a bit of overhead for the rbtree pointers, and additional vmap_area structs.
With a buddy allocator based approach, there's no additional state that needs to be allocated, since it all fits in struct page.
Quoted 10 lines
Quoted 10 lines
First, for 2MiB page with 64B chunk size, we need a bitmap of
2MiB / 64B = 32k bit = 4k bytesWhile the tree based solution can adapt to the number of allocations within This 2MiB page. Also, searching a free range within 4kB of bitmap may actually be slower than searching in the tree.
Second, bitmap based solution cannot handle > 2MiB allocation cleanly, while tree based solution can. For example, if a big driver uses 3MiB, the tree based allocator can allocate 4MiB for it, and use the rest 1MiB for smaller allocations.
We're not talking about a bitmap based solution for >= PAGE_SIZE allocations, the alternative is a buddy allocator - so no searching, just per power-of-two freelists.
Quoted 5 lines
Missed one:
Third, bitmap based solution requires a "size" parameter in
free(). It is an overhead for the user. Tree based solution doesn't have this issue.
No, we can recover the size of the allocation via compound_order() - hasn't historically been done for alloc_pages() allocations to avoid setting up the state in each page for compound head/tail, but it perhaps should be (and is with folios, which we've generally been switching to).
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 76 lines · Kent Overstreet
Quoted 43 lines · Song Liu
Quoted 42 lines · Song Liu
Quoted 27 lines · Kent Overstreet
Quoted 15 lines · Song Liu
Quoted 9 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
Yes, the API is based on Thomas's suggestion, like 90% from the discussions.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I am planning to implement the allocator based on this (reuse
vmap_arealogic):Ah, you're still doing
vmap_areaapproach.Quoted 7 lines
Mike's approach looks like it'll be _much_ lighter weight and higher performance, to me. vmalloc is known to be slow compared to the buddy allocator, and with Mike's approach we're only modifying mappings once per 2 MB chunk.
I don't see anything in your code for sub-page sized allocations too, so perhaps I should keep going with my slab allocator.
The
vmap_areaapproach handles sub-page allocations. In 5/5 of set [2], we showed that multiple BPF programs share the same page with some kernel text (_etext).Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Could you share your thoughts on your approach vs. Mike's? I'm newer to this area of the code than you two so maybe there's an angle I've missed :)
AFAICT, tree based solution (
vmap_area) is more efficient than bitmap based solution.Tree based requires quite a bit of overhead for the rbtree pointers, and additional
vmap_areastructs.With a buddy allocator based approach, there's no additional state that needs to be allocated, since it all fits in struct page.
Quoted 10 lines
Quoted 10 lines
First, for 2MiB page with 64B chunk size, we need a bitmap of
2MiB / 64B = 32k bit = 4k bytesWhile the tree based solution can adapt to the number of allocations within This 2MiB page. Also, searching a free range within 4kB of bitmap may actually be slower than searching in the tree.
Second, bitmap based solution cannot handle > 2MiB allocation cleanly, while tree based solution can. For example, if a big driver uses 3MiB, the tree based allocator can allocate 4MiB for it, and use the rest 1MiB for smaller allocations.
We're not talking about a bitmap based solution for >=
PAGE_SIZEallocations, the alternative is a buddy allocator - so no searching, just per power-of-two freelists.Quoted 3 lines
Missed one:
Quoted 7 lines
Quoted 2 lines
Third, bitmap based solution requires a "size" parameter in
free(). It is an overhead for the user. Tree based solution doesn't have this issue.No, we can recover the size of the allocation via
compound_order()- hasn't historically been done foralloc_pages()allocations to avoid setting up the state in each page for compound head/tail, but it perhaps should be (and is with folios, which we've generally been switching to).
If we use compound_order(), we will round up to power of 2 for all allocations. Does this mean we will use 4MiB for a 2.1MiB allocation?
Thanks,
Song
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 2 lines · Song Liu
If we use
compound_order(), we will round up to power of 2 for all allocations. Does this mean we will use 4MiB for a 2.1MiB allocation?
Yes. This means we lose on average 33% - I believe, someone with better statistics might correct me - to internal fragmentation. But the buddy allocator will be better at avoiding and dealing with external fragmentation over time.
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Kent OverstreetQuoted 49 lines · Kent Overstreet
Quoted 47 lines · Song Liu
Quoted 46 lines · Song Liu
Quoted 27 lines · Kent Overstreet
Quoted 15 lines · Song Liu
Quoted 9 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
Yes, the API is based on Thomas's suggestion, like 90% from the discussions.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I am planning to implement the allocator based on this (reuse
vmap_arealogic):Ah, you're still doing
vmap_areaapproach.Quoted 7 lines
Mike's approach looks like it'll be _much_ lighter weight and higher performance, to me. vmalloc is known to be slow compared to the buddy allocator, and with Mike's approach we're only modifying mappings once per 2 MB chunk.
I don't see anything in your code for sub-page sized allocations too, so perhaps I should keep going with my slab allocator.
The
vmap_areaapproach handles sub-page allocations. In 5/5 of set [2], we showed that multiple BPF programs share the same page with some kernel text (_etext).Quoted 3 lines
Could you share your thoughts on your approach vs. Mike's? I'm newer to this area of the code than you two so maybe there's an angle I've missed :)
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
AFAICT, tree based solution (
vmap_area) is more efficient than bitmap based solution.Tree based requires quite a bit of overhead for the rbtree pointers, and additional
vmap_areastructs.With a buddy allocator based approach, there's no additional state that needs to be allocated, since it all fits in struct page.
To allocate memory for text, we will allocate 2MiB, make it ROX, and then use it for many small allocations. IIUC, buddy allocator will use unallocated parts of this page for metadata. I guess this may be a problem, as the whole page is ROX now, and we have to use text_poke to write to it.
OTOH, if we allocate extra memory for metadata (tree based solution), all the metadata operations can be regular read/write.
Thanks,
Song
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 4 lines · Song Liu
To allocate memory for text, we will allocate 2MiB, make it ROX, and then use it for many small allocations. IIUC, buddy allocator will use unallocated parts of this page for metadata. I guess this may be a problem, as the whole page is ROX now, and we have to use
text_poketo write to it.
The standard kernel buddy allocator does _not_ store anything in the page itself - because the page might be a highmem page.
That's also why I went with the bitmap for my slab allocator; standard kernel slab allocator stores a freelist ptr in free objects.
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 10 lines · Kent Overstreet
Quoted 4 lines · Song Liu
To allocate memory for text, we will allocate 2MiB, make it ROX, and then use it for many small allocations. IIUC, buddy allocator will use unallocated parts of this page for metadata. I guess this may be a problem, as the whole page is ROX now, and we have to use
text_poketo write to it.The standard kernel buddy allocator does _not_ store anything in the page itself - because the page might be a highmem page.
Thanks for the correction.
Song
Quoted 3 lines
That's also why I went with the bitmap for my slab allocator; standard kernel slab allocator stores a freelist ptr in free objects.
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Song LiuQuoted 4 lines · Song Liu
Quoted 4 lines
Second, bitmap based solution cannot handle > 2MiB allocation cleanly, while tree based solution can. For example, if a big driver uses 3MiB, the tree based allocator can allocate 4MiB for it, and use the rest 1MiB for smaller allocations.
2 MB is just a good default size for the initial unmapped allocations if the main consideration is avoiding mapping fragmentation. The approach can work with larger allocations if required.
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Kent OverstreetHi Kent,
Quoted 26 lines · Kent Overstreet
Quoted 15 lines · Song Liu
Quoted 9 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
Yes, the API is based on Thomas's suggestion, like 90% from the discussions.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I am planning to implement the allocator based on this (reuse
vmap_arealogic):Ah, you're still doing
vmap_areaapproach.
Quoted 7 lines
Mike's approach looks like it'll be _much_ lighter weight and higher performance, to me. vmalloc is known to be slow compared to the buddy allocator, and with Mike's approach we're only modifying mappings once per 2 MB chunk.
I don't see anything in your code for sub-page sized allocations too, so perhaps I should keep going with my slab allocator.
Your allocator implicitly relies on vmalloc because of module_alloc ;-)
What I was thinking is that we can replace module_alloc() calls in your allocator with something based on my unmapped_alloc(). If we make the part that refills the cache also take care of creating the mapping in the module address space, that should cover everything.
Quoted 4 lines
Could you share your thoughts on your approach vs. Mike's? I'm newer to this area of the code than you two so maybe there's an angle I've missed :)
Signature · 2 lines
Sincerely yours, Mike.
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 38 lines · Mike Rapoport
Hi Kent,
Quoted 26 lines · Kent Overstreet
Quoted 15 lines · Song Liu
Quoted 9 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Hey Song, I was reviewing that thread too,
Are you taking a different approach based on Thomas's feedback? I think he had some fair points in that thread.
Yes, the API is based on Thomas's suggestion, like 90% from the discussions.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
My own feeling is that the buddy allocator is our tool for allocating larger variable sized physically contiguous allocations, so I'd like to see something based on that - I think we could do a hybrid buddy/slab allocator approach, like we have for regular memory allocations.
I am planning to implement the allocator based on this (reuse
vmap_arealogic):Ah, you're still doing
vmap_areaapproach.Quoted 7 lines
Mike's approach looks like it'll be _much_ lighter weight and higher performance, to me. vmalloc is known to be slow compared to the buddy allocator, and with Mike's approach we're only modifying mappings once per 2 MB chunk.
I don't see anything in your code for sub-page sized allocations too, so perhaps I should keep going with my slab allocator.
Quoted 6 lines
Your allocator implicitly relies on vmalloc because of
module_alloc;-)What I was thinking is that we can replace
module_alloc()calls in your allocator with something based on myunmapped_alloc(). If we make the part that refills the cache also take care of creating the mapping in the module address space, that should cover everything.
Here are what I found as I work more on the code:
1. It takes quite some work to create a clean interface and make sure all the users of module_alloc can use the new allocator on all archs. (archs with text poke need to work with ROX memory from the allocator; archs without text poke need to have a clean fall back mechanism, etc.). Most of this work is independent of the actual allocator, so we can do this part and plug in whatever allocator we want (buddy+slab or vmap-based or any other solutions).
2. vmap_area based solution will work. And it will be one solution for both < PAGE_SIZE and > PAGE_SIZE allocations. Given module_alloc is not in any hot path (AFAIK), I don't see any practical issues with this solution. It will be a little tricky to place and name the code, as it uses vmalloc logic, but it is technically a module allocator.
I will prioritize building the interface, and migrating users to it. If we do this part right, changing the underlying allocator should be straightforward.
Thanks,
Song
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 23 lines · Song Liu
Quoted 4 lines · Mike Rapoport
What I was thinking is that we can replace
module_alloc()calls in your allocator with something based on myunmapped_alloc(). If we make the part that refills the cache also take care of creating the mapping in the module address space, that should cover everything.Here are what I found as I work more on the code:
1. It takes quite some work to create a clean interface and make sure all the users of
module_alloccan use the new allocator on all archs. (archs with text poke need to work with ROX memory from the allocator; archs without text poke need to have a clean fall back mechanism, etc.). Most of this work is independent of the actual allocator, so we can do this part and plug in whatever allocator we want (buddy+slab or vmap-based or any other solutions).2.
vmap_areabased solution will work. And it will be one solution for both <PAGE_SIZEand >PAGE_SIZEallocations. Givenmodule_allocis not in any hot path (AFAIK), I don't see any practical issues with this solution. It will be a little tricky to place and name the code, as it uses vmalloc logic, but it is technically a module allocator.
Quoted 3 lines
I will prioritize building the interface, and migrating users to it. If we do this part right, changing the underlying allocator should be straightforward.
Correct, that's the only workable approach.
Once you have solved #1 the mechanism of the underlying allocator (#2) is pretty much exchangeable.
Any attempt to solve #2 before #1 is doomed by definition.
Thanks,
tglx
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Mike RapoportQuoted 6 lines · Mike Rapoport
Your allocator implicitly relies on vmalloc because of
module_alloc;-)What I was thinking is that we can replace
module_alloc()calls in your allocator with something based on myunmapped_alloc(). If we make the part that refills the cache also take care of creating the mapping in the module address space, that should cover everything.
Yeah, that's exactly what I was thinking :)
Liam was also just mentioning on IRC vmalloc lock contention came up again at LSF, and that's historically always been an isuse - going with your patchset for the backend nicely avoids that.
If I have time (hah! big if :) I'll see if I can cook up a patchset that combines our two approaches over the weekend.
Mike Rapoport
maintainerRe: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 8 lines · Kent Overstreet
Quoted 6 lines · Mike Rapoport
Your allocator implicitly relies on vmalloc because of
module_alloc;-)What I was thinking is that we can replace
module_alloc()calls in your allocator with something based on myunmapped_alloc(). If we make the part that refills the cache also take care of creating the mapping in the module address space, that should cover everything.
Quoted 5 lines
Yeah, that's exactly what I was thinking :)
Liam was also just mentioning on IRC vmalloc lock contention came up again at LSF, and that's historically always been an isuse - going with your patchset for the backend nicely avoids that.
Unfortunately not because we still need to map the pages in the modules area which is essentially a subset of vmalloc address space.
Quoted 2 lines
If I have time (hah! big if :) I'll see if I can cook up a patchset that combines our two approaches over the weekend.
Now there is also an interest about unmapped allocations from KVM folks, so I might continue pursuing unmapped allocator, probably just without a new GFP flag and hooks into page allocator.
Signature · 2 lines
Sincerely yours, Mike.
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 2 lines · Mike Rapoport
Unfortunately not because we still need to map the pages in the modules area which is essentially a subset of vmalloc address space.
Why wouldn't we be doing that once per 2 MB page?
Quoted 7 lines
Quoted 3 lines
If I have time (hah! big if :) I'll see if I can cook up a patchset that combines our two approaches over the weekend.
Now there is also an interest about unmapped allocations from KVM folks, so I might continue pursuing unmapped allocator, probably just without a new GFP flag and hooks into page allocator.
Cool - it does seem like a generally valuable capability :)
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Song LiuQuoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
For reference, here's my own (early, but functioning :) slab allocator:
Look forward to comparing!
-->--
From 6eeb6b8ef4271ea1a8d9cac7fbaeeb7704951976 Mon Sep 17 00:00:00 2001
From: Kent Overstreet <[email protected]>
Date: Wed, 17 May 2023 01:22:06 -0400
Subject: [PATCH] mm: jit/text allocator
This provides a new, very simple slab allocator for jit/text, i.e. bpf, ftrace trampolines, or bcachefs unpack functions.
With this API we can avoid ever mapping pages both writeable and executable (not implemented in this patch: need to tweak module_alloc()), and it also supports sub-page sized allocations.
include/linux/jitalloc.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_JITALLOC_H +#define _LINUX_JITALLOC_H + +void jit_update (void *buf,void *new_buf,size_t len); +void jit_free (void *buf); +void *jit_alloc (void *buf,size_t len); + +#endif /* _LINUX_JITALLOC_H */
mm/Kconfig +3 -0
@@ -1202,6 +1202,9 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. # } +config JITALLOC + bool + source "mm/damon/Kconfig" endmenu
mm/Makefile +1 -0
@@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_JITALLOC) += jitalloc.o
mm/jitalloc.c new file +187 -0
187 lines not on this page · raw message
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 8 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
For reference, here's my own (early, but functioning :) slab allocator:
Look forward to comparing!
This looks similar to the bpf_prog_pack allocator we use for BPF at the moment. (search for bpf_prog_pack in kernel/bpf/core.c). I guess we can also use bpf_prog_pack for bcachefs for now.
Thanks,
Song
Quoted 247 lines
-->--
From6eeb6b8ef4271ea1a8d9cac7fbaeeb7704951976Mon Sep 17 00:00:00 2001
From: Kent Overstreet <[email protected]>
Date: Wed, 17 May 2023 01:22:06 -0400
Subject: [PATCH] mm: jit/text allocatorThis provides a new, very simple slab allocator for jit/text, i.e. bpf, ftrace trampolines, or bcachefs unpack functions.
With this API we can avoid ever mapping pages both writeable and executable (not implemented in this patch: need to tweak
module_alloc()), and it also supports sub-page sized allocations.Signed-off-by Kent Overstreet <[email protected]>include/linux/jitalloc.h new file +9
-0@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_JITALLOC_H +#define _LINUX_JITALLOC_H + +void jit_update (void *buf,void *new_buf,size_t len); +void jit_free (void *buf); +void *jit_alloc (void *buf,size_t len); + +#endif /* _LINUX_JITALLOC_H */ mm/Kconfig +3
-0@@ -1202,6 +1202,9 @@ config LRU_GEN_STATS This option has a per-memcg and per-node memory overhead. # } +config JITALLOC + bool + source "mm/damon/Kconfig" endmenumm/Makefile +1
-0@@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o +obj-$(CONFIG_JITALLOC) += jitalloc.omm/jitalloc.c new file +187
-0@@ -0,0 +1,187 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/gfp.h> +#include <linux/highmem.h> +#include <linux/jitalloc.h> +#include <linux/mm.h> +#include <linux/moduleloader.h> +#include <linux/mutex.h> +#include <linux/set_memory.h> +#include <linux/vmalloc.h> + +#include <asm/text-patching.h> + +static DEFINE_MUTEX (jit_alloc_lock); + +struct jit_cache { +unsigned obj_size_bits; +unsigned objs_per_slab; +struct list_head partial; +}; + +#define JITALLOC_MIN_SIZE 16 +#define NR_JIT_CACHES ilog2(PAGE_SIZE / JITALLOC_MIN_SIZE) + +static struct jit_cache jit_caches[NR_JIT_CACHES ]; + +struct jit_slab { +unsigned long __page_flags; + +struct jit_cache *cache; + void *executably_mapped;; +unsigned long *objs_allocated;/* bitmap of free objects */ +struct list_head list; +}; + +#define folio_jit_slab (folio) (_Generic((folio), \ + const struct folio *: (const struct jit_slab *)(folio), \ + struct folio *: (struct jit_slab *)(folio))) + +#define jit_slab_folio (s) (_Generic((s), \ + const struct jit_slab *: (const struct folio *)s, \ + struct jit_slab *: (struct folio *)s)) + +static struct jit_slab *jit_slab_alloc (struct jit_cache *cache) +{ +void *executably_mapped =module_alloc (PAGE_SIZE ); +struct page *page; +struct folio *folio; +struct jit_slab *slab; +unsigned long *objs_allocated; + +if (!executably_mapped) +return NULL ; + + objs_allocated =kcalloc (BITS_TO_LONGS (cache->objs_per_slab),sizeof (unsigned long ),GFP_KERNEL ); +if (!objs_allocated ) { +vfree (executably_mapped); +return NULL ; + } + +set_vm_flush_reset_perms (executably_mapped); +set_memory_rox ((unsigned long ) executably_mapped,1 ); + + page =vmalloc_to_page (executably_mapped); + folio =page_folio (page); + +__folio_set_slab (folio); + slab =folio_jit_slab (folio); + slab->cache = cache; + slab->executably_mapped = executably_mapped; + slab->objs_allocated = objs_allocated; +INIT_LIST_HEAD (&slab->list); + +return slab; +} + +static void *jit_cache_alloc (void *buf,size_t len,struct jit_cache *cache) +{ +struct jit_slab *s = + list_first_entry_or_null(&cache->partial, struct jit_slab, list) ?: +jit_slab_alloc (cache); +unsigned obj_idx, nr_allocated; + +if (!s) +return NULL ; + + obj_idx =find_first_zero_bit (s->objs_allocated, cache->objs_per_slab); + +BUG_ON (obj_idx >= cache->objs_per_slab); +__set_bit (obj_idx, s->objs_allocated); + + nr_allocated =bitmap_weight (s->objs_allocated, s->cache->objs_per_slab); + +if (nr_allocated == s->cache->objs_per_slab) { +list_del_init (&s->list); + }else if (nr_allocated ==1 ) { +list_del (&s->list); +list_add (&s->list, &s->cache->partial); + } + +return s->executably_mapped + (obj_idx << cache->obj_size_bits); +} + +void jit_update (void *buf,void *new_buf,size_t len) +{ +text_poke_copy (buf, new_buf, len); +} +EXPORT_SYMBOL_GPL (jit_update); + +void jit_free (void *buf) +{ +struct page *page; +struct folio *folio; +struct jit_slab *s; +unsigned obj_idx, nr_allocated; +size_t offset; + +if (!buf) +return ; + + page =vmalloc_to_page (buf); + folio =page_folio (page); + offset =offset_in_folio (folio, buf); + +if (!folio_test_slab (folio)) { +vfree (buf); +return ; + } + + s =folio_jit_slab (folio); + +mutex_lock (&jit_alloc_lock); + obj_idx = offset >> s->cache->obj_size_bits; + +__clear_bit (obj_idx, s->objs_allocated); + + nr_allocated =bitmap_weight (s->objs_allocated, s->cache->objs_per_slab); + +if (nr_allocated ==0 ) { +list_del (&s->list); +kfree (s->objs_allocated); +folio_put (folio); + }else if (nr_allocated +1 == s->cache->objs_per_slab) { +list_del (&s->list); +list_add (&s->list, &s->cache->partial); + } + +mutex_unlock (&jit_alloc_lock); +} +EXPORT_SYMBOL_GPL (jit_free); + +void *jit_alloc (void *buf,size_t len) +{ +unsigned jit_cache_idx =ilog2 (roundup_pow_of_two (len) /16 ); +void *p; + +if (jit_cache_idx <NR_JIT_CACHES ) { +mutex_lock (&jit_alloc_lock); + p =jit_cache_alloc (buf, len, &jit_caches[jit_cache_idx]); +mutex_unlock (&jit_alloc_lock); + }else { + p =module_alloc (len); +if (p) { +set_vm_flush_reset_perms (p); +set_memory_rox ((unsigned long ) p,DIV_ROUND_UP (len,PAGE_SIZE )); + } + } + +if (p && buf) +jit_update (p, buf, len); + +return p; +} +EXPORT_SYMBOL_GPL (jit_alloc); + +static int __init jit_alloc_init (void ) +{ +for (unsigned i =0 ; i <ARRAY_SIZE (jit_caches); i++) { + jit_caches[i].obj_size_bits =ilog2 (JITALLOC_MIN_SIZE ) + i; + jit_caches[i].objs_per_slab =PAGE_SIZE >> jit_caches[i].obj_size_bits; + +INIT_LIST_HEAD (&jit_caches[i].partial); + } + +return 0 ; +} +core_initcall (jit_alloc_init);
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 7 lines · Song Liu
Quoted 5 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
Quoted 7 lines
Quoted 3 lines
For reference, here's my own (early, but functioning :) slab allocator:
Look forward to comparing!
This looks similar to the
bpf_prog_packallocator we use for BPF at the moment. (search forbpf_prog_packinkernel/bpf/core.c). I guess we can also usebpf_prog_packfor bcachefs for now.
The code in bpf/core.c does a _lot_, my preference would be to split that up and refactor the bpf code to use something generic :)
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 11 lines · Kent Overstreet
Quoted 9 lines · Song Liu
Quoted 7 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
For reference, here's my own (early, but functioning :) slab allocator:
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Look forward to comparing!
This looks similar to the
bpf_prog_packallocator we use for BPF at the moment. (search forbpf_prog_packinkernel/bpf/core.c). I guess we can also usebpf_prog_packfor bcachefs for now.The code in
bpf/core.cdoes a _lot_, my preference would be to split that up and refactor the bpf code to use something generic :)
The code is actually splitted into two parts:
bpf_prog_pack_[alloc|free] vs. bpf_jit_binary_pack_[alloc|free].
The latter has logic just for BPF. But the former can be reused by other users (maybe with a little refactoring).
Once the vmap_area based solution is committed, we will replace the former with the new allocator.
Thanks,
Song
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
In reply to Kent OverstreetQuoted 22 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
For reference, here's my own (early, but functioning :) slab allocator:
Look forward to comparing!
-->--
From6eeb6b8ef4271ea1a8d9cac7fbaeeb7704951976Mon Sep 17 00:00:00 2001
From: Kent Overstreet <[email protected]>
Date: Wed, 17 May 2023 01:22:06 -0400
Subject: [PATCH] mm: jit/text allocatorThis provides a new, very simple slab allocator for jit/text, i.e. bpf, ftrace trampolines, or bcachefs unpack functions.
With this API we can avoid ever mapping pages both writeable and executable (not implemented in this patch: need to tweak
module_alloc()), and it also supports sub-page sized allocations.Signed-off-by Kent Overstreet <[email protected]>
[...]
Quoted 16 lines
mm/jitalloc.c new file +16
-0+static void *jit_cache_alloc (void *buf,size_t len,struct jit_cache *cache) +{ +struct jit_slab *s = + list_first_entry_or_null(&cache->partial, struct jit_slab, list) ?: +jit_slab_alloc (cache); +unsigned obj_idx, nr_allocated; + +if (!s) +return NULL ; + + obj_idx =find_first_zero_bit (s->objs_allocated, cache->objs_per_slab); + +BUG_ON (obj_idx >= cache->objs_per_slab); +__set_bit (obj_idx, s->objs_allocated); + + nr_allocated =bitmap_weight (s->objs_allocated, s->cache->objs_per_slab);
mm/jitalloc.c line 102
Quoted 10 lines
mm/jitalloc.c new file +10
-0+ +if (nr_allocated == s->cache->objs_per_slab) { +list_del_init (&s->list); + }else if (nr_allocated ==1 ) { +list_del (&s->list); +list_add (&s->list, &s->cache->partial); + } + +return s->executably_mapped + (obj_idx << cache->obj_size_bits); +}
IIUC, "len" is ignored in jit_cache_alloc(), so it can only handle <=16 byte allocations?
Thanks,
Song
Re: [RFC PATCH 1/5] mm: intorduce __GFP_UNMAPPED and unmapped_alloc()
Quoted 48 lines · Song Liu
Quoted 22 lines · Kent Overstreet
Quoted 2 lines · Song Liu
I am working on patches based on the discussion in [1]. I am planning to send v1 for review in a week or so.
For reference, here's my own (early, but functioning :) slab allocator:
Look forward to comparing!
-->--
From6eeb6b8ef4271ea1a8d9cac7fbaeeb7704951976Mon Sep 17 00:00:00 2001
From: Kent Overstreet <[email protected]>
Date: Wed, 17 May 2023 01:22:06 -0400
Subject: [PATCH] mm: jit/text allocatorThis provides a new, very simple slab allocator for jit/text, i.e. bpf, ftrace trampolines, or bcachefs unpack functions.
With this API we can avoid ever mapping pages both writeable and executable (not implemented in this patch: need to tweak
module_alloc()), and it also supports sub-page sized allocations.Signed-off-by Kent Overstreet <[email protected]>[...]
Quoted 21 lines
mm/jitalloc.c new file +21
-0+static void *jit_cache_alloc (void *buf,size_t len,struct jit_cache *cache) +{ +struct jit_slab *s = + list_first_entry_or_null(&cache->partial, struct jit_slab, list) ?: +jit_slab_alloc (cache); +unsigned obj_idx, nr_allocated; + +if (!s) +return NULL ; + + obj_idx =find_first_zero_bit (s->objs_allocated, cache->objs_per_slab); + +BUG_ON (obj_idx >= cache->objs_per_slab); +__set_bit (obj_idx, s->objs_allocated); + + nr_allocated =bitmap_weight (s->objs_allocated, s->cache->objs_per_slab); + +if (nr_allocated == s->cache->objs_per_slab) { +list_del_init (&s->list); + }else if (nr_allocated ==1 ) { +list_del (&s->list);
Quoted 8 lines
mm/jitalloc.c line 102
Quoted 5 lines
mm/jitalloc.c new file +5
-0+list_add (&s->list, &s->cache->partial); + } + +return s->executably_mapped + (obj_idx << cache->obj_size_bits); +}IIUC, "len" is ignored in
jit_cache_alloc(), so it can only handle <=16 byte allocations?
len is a redundant parameter (good catch); at that point we've picked a cache for the specific allocation size.
Since there's multiple caches for each power of two size, it can handle allocations up to PAGE_SIZE.
Mike Rapoport
maintainerRFC PATCH 2/5 mm/unmapped_alloc: add debugfs file similar to /proc/pagetypeinfo
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
Present statistics about unmapped_alloc in debugfs
--- mm/unmapped-alloc.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
mm/unmapped-alloc.c +35 -0
@@ -3,6 +3,7 @@#include <linux/gfp.h> #include <linux/mmzone.h> #include <linux/printk.h> +#include <linux/debugfs.h> #include <linux/spinlock.h> #include <linux/set_memory.h>
@@ -213,3 +214,37 @@ int unmapped_alloc_init(void)return 0 ; } + +static int unmapped_alloc_debug_show (struct seq_file *m,void *private) +{ +int order; + +seq_printf (m,"MAX_ORDER: %d\n" ,MAX_ORDER ); +seq_putc (m,'\n' ); + +seq_printf (m,"%-10s" ,"Order:" ); +for (order =0 ; order <MAX_ORDER ; ++order) +seq_printf (m,"%5d " , order); +seq_putc (m,'\n' ); + +seq_printf (m,"%-10s" ,"Free:" ); +for (order =0 ; order <MAX_ORDER ; ++order) +seq_printf (m,"%5lu " , free_area[order].nr_free); +seq_putc (m,'\n' ); + +seq_printf (m,"%-10s" ,"Cached:" ); +for (order =0 ; order <MAX_ORDER ; ++order) +seq_printf (m,"%5lu " , free_area[order].nr_cached); +seq_putc (m,'\n' ); + +return 0 ; +} +DEFINE_SHOW_ATTRIBUTE (unmapped_alloc_debug); + +static int __init unmapped_alloc_init_late (void ) +{ +debugfs_create_file ("unmapped_alloc" ,0444 ,NULL , +NULL , &unmapped_alloc_debug_fops); +return 0 ; +} +late_initcall (unmapped_alloc_init_late);
Signature · 1 line
2.35.1
Mike Rapoport
maintainerRFC PATCH 3/5 mm/unmapped_alloc: add shrinker
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
Allow shrinking unmapped caches when there is a memory pressure.
--- mm/internal.h | 2 ++ mm/page_alloc.c | 12 ++++++- mm/unmapped-alloc.c | 86 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 2 deletions(-)
mm/internal.h +2 -0
@@ -1064,4 +1064,6 @@ static inline struct page *unmapped_pages_alloc(gfp_t gfp, int order)static inline void unmapped_pages_free (struct page *page,int order) {}#endif +void __free_unmapped_page (struct page *page,unsigned int order); +#endif /* __MM_INTERNAL_H */
mm/page_alloc.c +11 -1
@@ -123,6 +123,11 @@ typedef int __bitwise fpi_t;*/ #define FPI_SKIP_KASAN_POISON ((__force fpi_t)BIT(2)) +/* +* Free pages from the unmapped cache +*/ +#define FPI_UNMAPPED ((__force fpi_t)BIT(3)) +/* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ static DEFINE_MUTEX (pcp_batch_high_lock);#define MIN_PERCPU_PAGELIST_HIGH_FRACTION (8)
@@ -1467,7 +1472,7 @@ static __always_inline bool free_pages_prepare(struct page *page, PAGE_SIZE << order); } -if (get_pageblock_unmapped (page)) { +if (!(fpi_flags &FPI_UNMAPPED ) &&get_pageblock_unmapped (page)) {unmapped_pages_free (page, order);return false; }
@@ -1636,6 +1641,11 @@ static void free_one_page(struct zone *zone,spin_unlock_irqrestore (&zone->lock, flags); } +void __free_unmapped_page (struct page *page,unsigned int order) +{ +__free_pages_ok (page, order,FPI_UNMAPPED |FPI_TO_TAIL ); +} +static void __meminit __init_single_page (struct page *page,unsigned long pfn,unsigned long zone,int nid) {
mm/unmapped-alloc.c +85 -1
115 lines not on this page · raw message
Signature · 1 line
2.35.1
Mike Rapoport
maintainerRFC PATCH 4/5 EXPERIMENTAL: x86: use __GFP_UNMAPPED for modele_alloc()
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
--- arch/x86/kernel/module.c | 2 +- mm/vmalloc.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/module.c +1 -1
@@ -67,7 +67,7 @@ static unsigned long int get_module_load_offset(void)void *module_alloc (unsigned long size) { -gfp_t gfp_mask =GFP_KERNEL ; +gfp_t gfp_mask =GFP_KERNEL | __GFP_UNMAPPED;void *p;if (PAGE_ALIGN (size) >MODULES_LEN )
mm/vmalloc.c +1 -1
@@ -2892,7 +2892,7 @@ vm_area_alloc_pages(gfp_t gfp, int nid,* to fails, fallback to a single page allocator that is * more permissive. */ -if (!order) { +if (!order && !(gfp & __GFP_UNMAPPED)) {gfp_t bulk_gfp = gfp & ~__GFP_NOFAIL;while (nr_allocated < nr_pages) {
Signature · 1 line
2.35.1
Re: [RFC PATCH 4/5] EXPERIMENTAL: x86: use __GFP_UNMAPPED for modele_alloc()
mm/vmalloc.c vm_area_alloc_pages(gfp_t gfp, int nid, · line 2898
Quoted 13 lines · Mike Rapoport
mm/vmalloc.c +1
-1@@ -2892,7 +2892,7 @@ vm_area_alloc_pages(gfp_t gfp, int nid,* to fails, fallback to a single page allocator that is * more permissive. */ -if (!order) { +if (!order && !(gfp & __GFP_UNMAPPED)) {gfp_t bulk_gfp = gfp & ~__GFP_NOFAIL;while (nr_allocated < nr_pages) {
This is obviously a quick POC patch, but I guess we should skip the whole vm_remove_mappings() thing since it would reset the direct map to RW for these unmapped pages. Or rather modules shouldn't set VM_FLUSH_RESET_PERMS if it uses this.
Mike Rapoport
maintainerRFC PATCH 5/5 EXPERIMENTAL: mm/secretmem: use __GFP_UNMAPPED
In reply to Mike RapoportFrom: "Mike Rapoport (IBM)" <[email protected]>
--- mm/secretmem.c | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-)
mm/secretmem.c +1 -25
@@ -53,7 +53,6 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf)struct inode *inode =file_inode (vmf->vma->vm_file);pgoff_t offset = vmf->pgoff;gfp_t gfp = vmf->gfp_mask; -unsigned long addr;struct page *page;vm_fault_t ret;int err;
@@ -66,38 +65,22 @@ static vm_fault_t secretmem_fault(struct vm_fault *vmf) retry: page =find_lock_page (mapping, offset);if (!page) { - page =alloc_page (gfp | __GFP_ZERO); + page =alloc_page (gfp | __GFP_ZERO | __GFP_UNMAPPED);if (!page) { ret =VM_FAULT_OOM ; goto out; } - err =set_direct_map_invalid_noflush (page); -if (err) { -put_page (page); - ret =vmf_error (err); - goto out; - } -__SetPageUptodate (page); err =add_to_page_cache_lru (page, mapping, offset, gfp);if (unlikely (err)) {put_page (page); -/* -* If a split of large page was required, it -* already happened when we marked the page invalid -* which guarantees that this call won't fail -*/ -set_direct_map_default_noflush (page);if (err == -EEXIST ) goto retry; ret =vmf_error (err); goto out; } - - addr = (unsigned long )page_address (page); -flush_tlb_kernel_range (addr, addr +PAGE_SIZE ); } vmf->page = page;
@@ -150,15 +133,8 @@ static int secretmem_migrate_folio(struct address_space *mapping,return -EBUSY ; } -static void secretmem_free_folio (struct folio *folio) -{ -set_direct_map_default_noflush (&folio->page); -folio_zero_segment (folio,0 ,folio_size (folio)); -} -const struct address_space_operations secretmem_aops = { .dirty_folio = noop_dirty_folio, - .free_folio = secretmem_free_folio, .migrate_folio = secretmem_migrate_folio, };
Signature · 1 line
2.35.1
Quoted 9 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
Hi,
This is a third attempt to make page allocator aware of the direct map
layout and allow grouping of the pages that must be unmapped from the direct map.
Quoted 8 lines
This a new implementation of
__GFP_UNMAPPED, kinda a follow up for this set:https://lore.kernel.org/all/[email protected]
but instead of using a migrate type to cache the unmapped pages, the current implementation adds a dedicated cache to serve
__GFP_UNMAPPEDallocations.
It seems a downside to having a page allocator outside of _the_ page allocator is you don't get all of the features that are baked in there. For example does secretmem care about numa? I guess in this implementation there is just one big cache for all nodes.
Probably most users would want __GFP_ZERO. Would secretmem care about __GFP_ACCOUNT? I'm sure there is more, but I guess the question is, is the idea that these features all get built into unmapped-alloc at some point? The alternate approach is to have little caches for each usage like the grouped pages, which is probably less efficient when you have a bunch of them. Or solve it just for modules like the bpf allocator. Those are the tradeoffs for the approaches that have been explored, right?
Mike Rapoport
maintainerQuoted 19 lines · Rick P
Quoted 9 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
Hi,
This is a third attempt to make page allocator aware of the direct map
layout and allow grouping of the pages that must be unmapped from the direct map.Quoted 8 lines
This a new implementation of
__GFP_UNMAPPED, kinda a follow up for this set:https://lore.kernel.org/all/[email protected]
but instead of using a migrate type to cache the unmapped pages, the current implementation adds a dedicated cache to serve
__GFP_UNMAPPEDallocations.
Quoted 7 lines
It seems a downside to having a page allocator outside of _the_ page allocator is you don't get all of the features that are baked in there. For example does secretmem care about numa? I guess in this implementation there is just one big cache for all nodes.
Probably most users would want
__GFP_ZERO. Would secretmem care about__GFP_ACCOUNT?
The intention was that the pages in cache are always zeroed, so __GFP_ZERO is always implicitly there, at least should have been. __GFP_ACCOUNT is respected in this implementation. If you look at the changes to __alloc_pages(), after getting pages from unmapped cache there is 'goto out' to the point where the accounting is handled.
Quoted 7 lines
I'm sure there is more, but I guess the question is, is
the idea that these features all get built into unmapped-alloc at some point? The alternate approach is to have little caches for each usage like the grouped pages, which is probably less efficient when you have a bunch of them. Or solve it just for modules like the bpf allocator. Those are the tradeoffs for the approaches that have been explored, right?
I think that no matter what cache we'll use it won't be able to support all features _the_ page allocator has. Indeed if we'd have per case cache implementation we can tune that implementation to support features of interest for that use case, but then we'll be less efficient in reducing splits of the large pages. Not to mention increase in complexity as there will be several caches doing similar but yet different things.
This POC mostly targets secretmem and modules, so this was pretty much about GFP_KERNEL without considerations for NUMA, but I think extending this unmapped alloc for NUMA should be simple enough but it will increase memory overhead even more.
Signature · 2 lines
Sincerely yours, Mike.
Quoted 25 lines · Mike Rapoport
Quoted 24 lines · Rick P
Quoted 9 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
Hi,
This is a third attempt to make page allocator aware of the direct map
layout and allow grouping of the pages that must be unmapped from the direct map.Quoted 8 lines
This a new implementation of
__GFP_UNMAPPED, kinda a follow up for this set:https://lore.kernel.org/all/[email protected]
but instead of using a migrate type to cache the unmapped pages, the current implementation adds a dedicated cache to serve
__GFP_UNMAPPEDallocations.It seems a downside to having a page allocator outside of _the_ page allocator is you don't get all of the features that are baked in there. For example does secretmem care about numa? I guess in this implementation there is just one big cache for all nodes.
Quoted 5 lines
Quoted 2 lines
Probably most users would want
__GFP_ZERO. Would secretmem care about__GFP_ACCOUNT?The intention was that the pages in cache are always zeroed, so
__GFP_ZEROis always implicitly there, at least should have been.
Would it be possible to drop that assumption/requirement, i.e. allow allocation of __GFP_UNMAPPED without __GFP_ZERO? At a glance, __GFP_UNMAPPED looks like it would be a great fit for backing guest memory, in particular for confidential VMs. And for some flavors of CoCo, i.e. TDX, the trusted intermediary is responsible for zeroing/initializing guest memory as the untrusted host (kernel/KVM) doesn't have access to the guest's encryption key. In other words, zeroing in the kernel would be unnecessary work.
Mike Rapoport
maintainerQuoted 32 lines · Sean Christopherson
Quoted 25 lines · Mike Rapoport
Quoted 24 lines · Rick P
Quoted 9 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
Hi,
This is a third attempt to make page allocator aware of the direct map
layout and allow grouping of the pages that must be unmapped from the direct map.Quoted 8 lines
This a new implementation of
__GFP_UNMAPPED, kinda a follow up for this set:https://lore.kernel.org/all/[email protected]
but instead of using a migrate type to cache the unmapped pages, the current implementation adds a dedicated cache to serve
__GFP_UNMAPPEDallocations.It seems a downside to having a page allocator outside of _the_ page allocator is you don't get all of the features that are baked in there. For example does secretmem care about numa? I guess in this implementation there is just one big cache for all nodes.
Quoted 5 lines
Quoted 2 lines
Probably most users would want
__GFP_ZERO. Would secretmem care about__GFP_ACCOUNT?The intention was that the pages in cache are always zeroed, so
__GFP_ZEROis always implicitly there, at least should have been.
Quoted 7 lines
Would it be possible to drop that assumption/requirement, i.e. allow allocation of
__GFP_UNMAPPEDwithout__GFP_ZERO? At a glance,__GFP_UNMAPPEDlooks like it would be a great fit for backing guest memory, in particular for confidential VMs. And for some flavors of CoCo, i.e. TDX, the trusted intermediary is responsible for zeroing/initializing guest memory as the untrusted host (kernel/KVM) doesn't have access to the guest's encryption key. In other words, zeroing in the kernel would be unnecessary work.
Making and unmapped allocation without __GFP_ZERO shouldn't be a problem.
However, using a gfp flag and hooking up into the free path in page allocator have issues and preferably should be avoided.
Will something like unmapped_alloc() and unmapped_free() work for your usecase?
Signature · 2 lines
Sincerely yours, Mike.
Quoted 41 lines · Mike Rapoport
Quoted 32 lines · Sean Christopherson
Quoted 25 lines · Mike Rapoport
Quoted 24 lines · Rick P
Quoted 9 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
Hi,
This is a third attempt to make page allocator aware of the direct map
layout and allow grouping of the pages that must be unmapped from the direct map.Quoted 8 lines
This a new implementation of
__GFP_UNMAPPED, kinda a follow up for this set:https://lore.kernel.org/all/[email protected]
but instead of using a migrate type to cache the unmapped pages, the current implementation adds a dedicated cache to serve
__GFP_UNMAPPEDallocations.It seems a downside to having a page allocator outside of _the_ page allocator is you don't get all of the features that are baked in there. For example does secretmem care about numa? I guess in this implementation there is just one big cache for all nodes.
Quoted 5 lines
Quoted 2 lines
Probably most users would want
__GFP_ZERO. Would secretmem care about__GFP_ACCOUNT?The intention was that the pages in cache are always zeroed, so
__GFP_ZEROis always implicitly there, at least should have been.Quoted 7 lines
Would it be possible to drop that assumption/requirement, i.e. allow allocation of
__GFP_UNMAPPEDwithout__GFP_ZERO? At a glance,__GFP_UNMAPPEDlooks like it would be a great fit for backing guest memory, in particular for confidential VMs. And for some flavors of CoCo, i.e. TDX, the trusted intermediary is responsible for zeroing/initializing guest memory as the untrusted host (kernel/KVM) doesn't have access to the guest's encryption key. In other words, zeroing in the kernel would be unnecessary work.
Quoted 7 lines
Making and unmapped allocation without
__GFP_ZEROshouldn't be a problem.However, using a gfp flag and hooking up into the free path in page allocator have issues and preferably should be avoided.
Will something like
unmapped_alloc()andunmapped_free()work for your usecase?
Yep, I'm leaning more and more towards having KVM implement its own ioctl() for managing this type of memory. Wiring that up to use dedicated APIs should be no problem.
Thanks!
Mike Rapoport
maintainerQuoted 18 lines · Sean Christopherson
Quoted 17 lines · Mike Rapoport
Quoted 10 lines · Sean Christopherson
Quoted 1 line · Mike Rapoport
On Thu, Mar 09, 2023 at 01:59:00AM +0000, Edgecombe, Rick P wrote:
Would it be possible to drop that assumption/requirement, i.e. allow allocation of
__GFP_UNMAPPEDwithout__GFP_ZERO? At a glance,__GFP_UNMAPPEDlooks like it would be a great fit for backing guest memory, in particular for confidential VMs. And for some flavors of CoCo, i.e. TDX, the trusted intermediary is responsible for zeroing/initializing guest memory as the untrusted host (kernel/KVM) doesn't have access to the guest's encryption key. In other words, zeroing in the kernel would be unnecessary work.Making and unmapped allocation without
__GFP_ZEROshouldn't be a problem.However, using a gfp flag and hooking up into the free path in page allocator have issues and preferably should be avoided.
Quoted 6 lines
Quoted 2 lines
Will something like
unmapped_alloc()andunmapped_free()work for your usecase?Yep, I'm leaning more and more towards having KVM implement its own
ioctl()for managing this type of memory. Wiring that up to use dedicated APIs should be no problem.
Ok, I've dropped the GFP flag and made unmapped_pages_{alloc,free} the only APIs.
Totally untested version of what I've got is here:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git/log/?h=unmapped-alloc/rfc-v2
I have some thoughts about adding support for 1G pages, but this is still really vague at the moment.
Quoted 1 line
Thanks!
Signature · 2 lines
Sincerely yours, Mike.
Quoted 2 lines · Mike Rapoport
The last two patches in the series demonstrate how
__GFP_UNMAPPEDcan be used in two in-tree use cases.
dma_alloc_attrs(DMA_ATTR_NO_KERNEL_MAPPING) would be another easy one.
Mike Rapoport
maintainer(adding Mel)
Quoted 102 lines · Mike Rapoport
From: "Mike Rapoport (IBM)" <[email protected]>
Hi,
This is a third attempt to make page allocator aware of the direct map layout and allow grouping of the pages that must be unmapped from the direct map.
This a new implementation of
__GFP_UNMAPPED, kinda a follow up for this set:https://lore.kernel.org/all/[email protected]
but instead of using a migrate type to cache the unmapped pages, the current implementation adds a dedicated cache to serve
__GFP_UNMAPPEDallocations.The last two patches in the series demonstrate how
__GFP_UNMAPPEDcan be used in two in-tree use cases.First one is to switch secretmem to use the new mechanism, which is straight forward optimization.
The second use-case is to enable
__GFP_UNMAPPEDinx86::module_alloc()that is essentially used as a method to allocate code pages and thus requires permission changes for basic pages in the direct map.This set is x86 specific at the moment because other architectures either do not support
set_memoryAPIs that split the direct^w linear map (e.g. PowerPC) or only enableset_memoryAPIs when the linear map uses basic page size (like arm64).The patches are only lightly tested.
== Motivation ==
There are use-cases that need to remove pages from the direct map or at least map them with 4K granularity. Whenever this is done e.g. with set_memory/set_direct_map APIs, the PUD and PMD sized mappings in the direct map are split into smaller pages.
To reduce the performance hit caused by the fragmentation of the direct map it makes sense to group and/or cache the pages removed from the direct map so that the split large pages won't be all over the place.
There were RFCs for grouped page allocations for vmalloc permissions [1] and for using PKS to protect page tables [2] as well as an attempt to use a pool of large pages in secretmtm [3], but these suggestions address each use case separately, while having a common mechanism at the core mm level could be used by all use cases.
== Implementation overview ==
The pages that need to be removed from the direct map are grouped in a dedicated cache. When there is a page allocation request with
__GFP_UNMAPPEDset, it is redirected from__alloc_pages()to that cache using a newunmapped_alloc()function.The cache is implemented as a buddy allocator and it can handle high order requests.
The cache starts empty and whenever it does not have enough pages to satisfy an allocation request the cache attempts to allocate
PMD_SIZEpage to replenish the cache. IfPMD_SIZEpage cannot be allocated, the cache is replenished with a page of the highest order available. That page is removed from the direct map and added to the local buddy allocator.There is also a shrinker that releases pages from the unmapped cache when there us a memory pressure in the system. When shrinker releases a page it is mapped back into the direct map.
[1] https://lore.kernel.org/lkml/[email protected]
[2] https://lore.kernel.org/lkml/[email protected]
[3] https://lore.kernel.org/lkml/[email protected]Mike Rapoport (IBM) (5):
mm: intorduce__GFP_UNMAPPEDandunmapped_alloc()mm/unmapped_alloc: add debugfs file similar to/proc/pagetypeinfomm/unmapped_alloc: add shrinker
EXPERIMENTAL: x86: use__GFP_UNMAPPEDformodele_alloc()
EXPERIMENTAL: mm/secretmem: use__GFP_UNMAPPEDarch/x86/Kconfig | 3 + arch/x86/kernel/module.c | 2 +- include/linux/gfp_types.h | 11 +- include/linux/page-flags.h | 6 + include/linux/pageblock-flags.h | 28 +++ include/trace/events/mmflags.h | 10 +- mm/Kconfig | 4 + mm/Makefile | 1 + mm/internal.h | 24 +++ mm/page_alloc.c | 39 +++- mm/secretmem.c | 26 +-- mm/unmapped-alloc.c | 334 ++++++++++++++++++++++++++++++++ mm/vmalloc.c | 2 +- 13 files changed, 459 insertions(+), 31 deletions(-) create mode 100644 mm/unmapped-alloc.cbase-commit:
fe15c26ee26efa11741a7b632e9f23b01aca4cc6Signature · 1 line
2.35.1
Signature · 2 lines
Sincerely yours, Mike.