RFC mm: Introducing the Maple Tree
Subsystems (17)
cover RFC mm: Introducing the Maple Tree
The maple tree is an RCU-safe range based B-tree designed to use modern processor cache efficiently. There are a number of places in the kernel that a non-overlapping range-based tree would be beneficial, especially one with a simple interface. The first user that is covered in this patch set is the vm_area_struct rbtree in the mm_struct with the long term goal of reducing the contention of the mmap_sem.
The tree has a branching factor of 10 for non-leaf nodes and 16 for leaf nodes. With the increased branching factor, it is significantly short than the rbtree so it has fewer cache misses. As you can see below, the performance is getting very close even without the vmacache. I am looking for ways to optimize the vma code to make this even faster and would like input. As this tree requires allocations to provide RCU functionality, it is worth avoiding expanding the tree only to contract it in the next step.
This patch set is based on 5.10-rc1.
Please note that 35821 lines of this patch is the test code.
It is important to note that this is an RFC and there are limitations around what is currently supported.
The current status of this release is:
- **No support for 32 bit or nommu builds**
- There is a performance regression with regards to kernel builds from -3% to -5% of system time. Elapsed time is within 1 second of 5.10-rc1.
- Removal of the vmacache
- The mm struct uses the maple tree to store and retrieve all VMAs
- Decrease in performance in the following micro-benchmarks:
- will-it-scale brk1 which tests insert speed of a vma as it is written of -25 to -33%. The test does not seem to test what it is meant to test.
- kernbuild (~-4 to -5% system, less that -1% elapsed Amean)
- Increase in performance in the following micro-benchmarks in Hmean:
- will-it-scale malloc1-processes (~1-9%)
- will-it-scale malloc1-threads (~29-71%)
- will-it-scale pthread_mutex1-threads (~0-16%)
- will-it-scale signal1-processes (2-17%)
- will-it-scale brk1-threads **This test doesn't make sense, disregard**
- Converted the following to use the advanced maple tree interface:
- brk()
- mmap_region()
- do_munmap()
- dup_mmap()
- Currently operating in non-RCU mode. Once enabled, RCU mode will be enabled only when more than one thread is active on a task. At that point the maple tree will enable RCU lookup of VMAs.
The long term goal of the maple tree is to reduce mmap_sem contention by removing users that cause contention one by one. This may lead to the lock being completely removed at some point.
The secondary goals is to provide the kernel with a fast RCU tree with a simple interface and to clean up the mm code by removing the integration of the tree within the code and structures themselves.
This patch set is based on 5.10-rc1.
Implementation details:
Each node is 256 Bytes and has multiple node types. The current implementation uses two node types and has a branching factor of 16 for leaf/non-alloc nodes and 10 for internal alloc nodes. The advantage of the internal alloc nodes is the ability to track the largest sub-tree gap for each entry.
Even with the removal of the vmacache, the benchmarks are getting close to the rbtree, but I'd like some help in regards to making things faster. The tree is at a disadvantage of needing to allocate storage space for internal use where as the rbtree stored the data within the vm_area_struct. This is a necessary trade off to move to RCU mode in the future.
Benchmark numbers from mmtests on a 144 core system:
wis-pthreadmutex 5.10-rc1 maple tree
Hmean pthread_mutex1-threads-2 19006635.84 ( 0.00%) 21036173.96 * 10.68%* Hmean pthread_mutex1-threads-5 10228467.75 ( 0.00%) 11890622.20 * 16.25%* Hmean pthread_mutex1-threads-8 13933097.01 ( 0.00%) 13856905.46 * -0.55%* Hmean pthread_mutex1-threads-12 11431792.97 ( 0.00%) 13082725.10 * 14.44%* Hmean pthread_mutex1-threads-21 14536655.83 ( 0.00%) 14318050.44 * -1.50%* Hmean pthread_mutex1-threads-30 14025017.40 ( 0.00%) 15451267.09 * 10.17%* Hmean pthread_mutex1-threads-48 15452188.47 ( 0.00%) 14355672.94 * -7.10%* Hmean pthread_mutex1-threads-79 13680017.69 ( 0.00%) 13664026.29 * -0.12%* Hmean pthread_mutex1-threads-110 14131595.92 ( 0.00%) 13659743.96 * -3.34%* Hmean pthread_mutex1-threads-141 13493999.70 ( 0.00%) 12951941.54 * -4.02%*
wis-signal 5.10-rc1 maple tree
Hmean signal1-processes-2 1804422.41 ( 0.00%) 1827311.56 * 1.27%* Hmean signal1-processes-5 4460172.84 ( 0.00%) 4476128.55 * 0.36%* Hmean signal1-processes-8 6900853.35 ( 0.00%) 6973210.80 * 1.05%* Hmean signal1-processes-12 9929132.51 ( 0.00%) 10055390.13 * 1.27%* Hmean signal1-processes-21 5864723.57 ( 0.00%) 6745451.58 * 15.02%* Hmean signal1-processes-30 6183688.25 ( 0.00%) 6405037.68 * 3.58%* Hmean signal1-processes-48 5439087.23 ( 0.00%) 5631558.18 * 3.54%* Hmean signal1-processes-79 6512401.34 ( 0.00%) 6706141.64 * 2.97%* Hmean signal1-processes-110 8056779.10 ( 0.00%) 8494438.86 * 5.43%* Hmean signal1-processes-141 9320516.16 ( 0.00%) 10571140.16 * 13.42%*
wis-malloc 5.10-rc1 maple tree Hmean brk1-processes-2 4665394.67 ( 0.00%) 3531806.58 * -24.30%* Hmean brk1-processes-5 11772601.71 ( 0.00%) 8829092.61 * -25.00%* Hmean brk1-processes-8 18257028.74 ( 0.00%) 13720140.11 * -24.85%* Hmean brk1-processes-12 26611276.11 ( 0.00%) 20040283.01 * -24.69%* Hmean brk1-processes-21 43293789.47 ( 0.00%) 32553619.66 * -24.81%* Hmean brk1-processes-30 62819136.48 ( 0.00%) 47206392.92 * -24.85%* Hmean brk1-processes-48 99084516.67 ( 0.00%) 74442789.08 * -24.87%* Hmean brk1-processes-79 149256917.87 ( 0.00%) 110957538.42 * -25.66%* Hmean brk1-processes-110 167784473.57 ( 0.00%) 119664465.82 * -28.68%* Hmean brk1-processes-141 186425159.76 ( 0.00%) 128345206.83 * -31.15%*
Hmean malloc1-processes-2 551028.28 ( 0.00%) 493969.15 * -10.36%* Hmean malloc1-processes-5 1351566.71 ( 0.00%) 1113801.87 * -17.59%* Hmean malloc1-processes-8 1277080.35 ( 0.00%) 1252745.65 * -1.91%* Hmean malloc1-processes-12 1318056.55 ( 0.00%) 1309786.94 * -0.63%* Hmean malloc1-processes-21 2154975.49 ( 0.00%) 2068093.55 * -4.03%* Hmean malloc1-processes-30 2741084.95 ( 0.00%) 2809368.92 * 2.49%* Hmean malloc1-processes-48 4143258.89 ( 0.00%) 4069176.07 * -1.79%* Hmean malloc1-processes-79 5138031.43 ( 0.00%) 5162196.46 * 0.47%* Hmean malloc1-processes-110 5022505.12 ( 0.00%) 5439233.18 * 8.30%* Hmean malloc1-processes-141 4956600.59 ( 0.00%) 5393879.66 * 8.82%*
Hmean malloc1-threads-2 13228.04 ( 0.00%) 184582.23 *1295.39%* Hmean malloc1-threads-5 100002.88 ( 0.00%) 169561.76 * 69.56%* Hmean malloc1-threads-8 91218.91 ( 0.00%) 154958.11 * 69.87%* Hmean malloc1-threads-12 97814.95 ( 0.00%) 123943.65 * 26.71%* Hmean malloc1-threads-21 45995.46 ( 0.00%) 43358.07 * -5.73%* Hmean malloc1-threads-30 23986.53 ( 0.00%) 47083.82 * 96.29%* Hmean malloc1-threads-48 15167.31 ( 0.00%) 30307.31 * 99.82%* Hmean malloc1-threads-79 8976.45 ( 0.00%) 8374.31 * -6.71%* Hmean malloc1-threads-110 5355.38 ( 0.00%) 1970.16 * -63.21%* Hmean malloc1-threads-141 8452.32 ( 0.00%) 12303.51 * 45.56%*
kernbench 5.10-rc1 maple tree Amean user-2 884.80 ( 0.00%) 884.09 * 0.08%* Amean syst-2 141.84 ( 0.00%) 147.07 * -3.68%* Amean elsp-2 518.70 ( 0.00%) 520.85 * -0.41%* Amean user-4 900.83 ( 0.00%) 899.11 * 0.19%* Amean syst-4 145.76 ( 0.00%) 151.17 * -3.71%* Amean elsp-4 268.92 ( 0.00%) 268.97 * -0.02%* Amean user-8 916.07 ( 0.00%) 916.70 * -0.07%* Amean syst-8 145.34 ( 0.00%) 151.73 * -4.40%* Amean elsp-8 140.55 ( 0.00%) 141.88 * -0.94%* Amean user-16 930.43 ( 0.00%) 931.27 * -0.09%* Amean syst-16 147.23 ( 0.00%) 153.34 * -4.15%* Amean elsp-16 76.12 ( 0.00%) 76.25 * -0.17%* Amean user-32 980.03 ( 0.00%) 979.52 * 0.05%* Amean syst-32 158.15 ( 0.00%) 164.38 * -3.94%* Amean elsp-32 46.13 ( 0.00%) 45.98 * 0.31%* Amean user-64 1087.31 ( 0.00%) 1092.55 * -0.48%* Amean syst-64 173.28 ( 0.00%) 181.08 * -4.50%* Amean elsp-64 29.51 ( 0.00%) 30.06 * -1.88%* Amean user-128 1610.27 ( 0.00%) 1612.96 * -0.17%* Amean syst-128 228.93 ( 0.00%) 240.80 * -5.19%* Amean elsp-128 25.25 ( 0.00%) 25.42 * -0.70%* Amean user-256 1742.43 ( 0.00%) 1738.54 * 0.22%* Amean syst-256 241.50 ( 0.00%) 256.13 * -6.06%* Amean elsp-256 25.08 ( 0.00%) 25.42 * -1.35%* Amean user-288 1742.33 ( 0.00%) 1739.64 * 0.15%* Amean syst-288 241.16 ( 0.00%) 256.44 * -6.34%* Amean elsp-288 25.10 ( 0.00%) 25.44 * -1.35%*
Liam R. Howlett (28):
radix tree test suite: Enhancements for Maple Tree
radix tree test suite: Add support for fallthrough attribute radix tree test suite: Add support for kmem_cache_free_bulk radix tree test suite: Add keme_cache_alloc_bulk() support Maple Tree: Add new data structure
mm: Start tracking VMAs with maple tree
mm/mmap: Introduce unlock_range() for code cleanup mm/mmap: Change find_vma() to use the maple tree
mm/mmap: Change find_vma_prev() to use maple tree mm/mmap: Change unmapped_area and unmapped_area_topdown to use maple
tree
kernel/fork: Convert dup_mmap to use maple tree
mm: Remove rb tree.
mm/gup: Expose mm_populate_vma() for use when the vma is known mm/mmap: Change do_brk_flags() to expand existing VMA and add
do_brk_munmap()
mm/mmap: Change vm_brk_flags() to use mm_populate_vma()
mm: Move find_vma_intersection to mmap.c and change implementation to
maple tree.
mm/mmap: Change mmap_region to use maple tree state mm/mmap: Drop munmap_vma_range()
mm: Remove vmacache
mm/mmap: Change __do_munmap() to avoid unnecessary lookups. mm/mmap: Change __do_munmap() to use a ma_state
mm/mmap: Move mmap_region() below do_munmap()
mm/mmap: Add do_mas_munmap() and wraper for __do_munmap()
mmap: Use find_vma_intersection in do_mmap() for overlap
mmap: Remove __do_munmap() in favour of do_mas_munmap() mm/mmap: Change do_brk_munmap() to use do_mas_align_munmap()
mmap: Update count_vma_pages_range() to only use one ma_state
mmap: make remove_vma_list() inline
Documentation/core-api/index.rst | 1 + Documentation/core-api/maple-tree.rst | 36 + MAINTAINERS | 12 + arch/x86/kernel/tboot.c | 2 +- drivers/firmware/efi/efi.c | 2 +- fs/exec.c | 3 - fs/proc/task_mmu.c | 1 - include/linux/maple_tree.h | 439 + include/linux/mm.h | 20 +- include/linux/mm_types.h | 30 +- include/linux/mm_types_task.h | 5 - include/linux/sched.h | 1 - include/linux/sched/mm.h | 3 + include/linux/vmacache.h | 28 - include/trace/events/maple_tree.h | 227 + include/trace/events/mmap.h | 97 + init/main.c | 2 + kernel/debug/debug_core.c | 12 - kernel/fork.c | 35 +- lib/Makefile | 3 +- lib/maple_tree.c | 5694 +++ lib/test_maple_tree.c | 35821 ++++++++++++++++ mm/Makefile | 2 +- mm/debug.c | 4 +- mm/gup.c | 20 + mm/init-mm.c | 4 +- mm/mmap.c | 1904 +- mm/mremap.c | 7 +- mm/util.c | 8 + mm/vmacache.c | 117 - tools/testing/radix-tree/.gitignore | 2 + tools/testing/radix-tree/Makefile | 13 +- tools/testing/radix-tree/generated/autoconf.h | 1 + tools/testing/radix-tree/linux.c | 78 +- tools/testing/radix-tree/linux/kernel.h | 8 + tools/testing/radix-tree/linux/maple_tree.h | 3 + tools/testing/radix-tree/linux/slab.h | 2 + tools/testing/radix-tree/maple.c | 58 + tools/testing/radix-tree/test.h | 1 + .../radix-tree/trace/events/maple_tree.h | 8 + 40 files changed, 43554 insertions(+), 1160 deletions(-) create mode 100644 Documentation/core-api/maple-tree.rst create mode 100644 include/linux/maple_tree.h delete mode 100644 include/linux/vmacache.h create mode 100644 include/trace/events/maple_tree.h create mode 100644 lib/maple_tree.c create mode 100644 lib/test_maple_tree.c delete mode 100644 mm/vmacache.c create mode 100644 tools/testing/radix-tree/linux/maple_tree.h create mode 100644 tools/testing/radix-tree/maple.c create mode 100644 tools/testing/radix-tree/trace/events/maple_tree.h
Signature · 1 line
2.28.0
PATCH 1/28 radix tree test suite: Enhancements for Maple Tree
--- tools/testing/radix-tree/linux.c | 16 +++++++++++++++- tools/testing/radix-tree/linux/kernel.h | 1 + 2 files changed, 16 insertions(+), 1 deletion(-)
tools/testing/radix-tree/linux.c +15 -1
@@ -24,15 +24,28 @@ struct kmem_cache {int nr_objs;void *objs;void (*ctor)(void *); +unsigned int non_kernel; }; +void kmem_cache_set_non_kernel (struct kmem_cache *cachep,unsigned int val) +{ + cachep->non_kernel = val; +} + +unsigned long kmem_cache_get_alloc (struct kmem_cache *cachep) +{ +return cachep->size * nr_allocated; +}void *kmem_cache_alloc (struct kmem_cache *cachep,int gfp) {void *p; -if (!(gfp & __GFP_DIRECT_RECLAIM)) +if (!(gfp & __GFP_DIRECT_RECLAIM) && !cachep->non_kernel)return NULL ; +if (!(gfp & __GFP_DIRECT_RECLAIM)) + cachep->non_kernel--; +pthread_mutex_lock (&cachep->lock);if (cachep->nr_objs) {struct radix_tree_node *node = cachep->objs;
@@ -116,5 +129,6 @@ kmem_cache_create(const char *name, unsigned int size, unsigned int align, ret->nr_objs =0 ; ret->objs =NULL ; ret->ctor = ctor; + ret->non_kernel =0 ;return ret; }
tools/testing/radix-tree/linux/kernel.h +1 -0
@@ -14,6 +14,7 @@#include "../../../include/linux/kconfig.h" #define printk printf +#define pr_err printk#define pr_info printk#define pr_debug printk#define pr_cont printk
Signature · 1 line
2.28.0
PATCH 2/28 radix tree test suite: Add support for fallthrough attribute
In reply to Liam R. HowlettAdd support for fallthrough on case statements. Note this does *NOT* check for missing fallthrough, but does allow compiling of code with fallthrough in case statements.
--- tools/testing/radix-tree/linux/kernel.h | 7 +++++++ 1 file changed, 7 insertions(+)
tools/testing/radix-tree/linux/kernel.h +7 -0
@@ -24,4 +24,11 @@#define __must_hold (x)#define EXPORT_PER_CPU_SYMBOL_GPL (x) + +#if __has_attribute (__fallthrough__) +# define fallthrough __attribute__((__fallthrough__)) +#else +# define fallthrough do {} while (0)/* fallthrough */ +#endif /* __has_attribute */ +#endif /* _KERNEL_H */
Signature · 1 line
2.28.0
PATCH 3/28 radix tree test suite: Add support for kmem_cache_free_bulk
In reply to Liam R. Howlett--- tools/testing/radix-tree/linux.c | 9 +++++++++ tools/testing/radix-tree/linux/slab.h | 1 + 2 files changed, 10 insertions(+)
tools/testing/radix-tree/linux.c +9 -0
@@ -91,6 +91,15 @@ void kmem_cache_free(struct kmem_cache *cachep, void *objp)pthread_mutex_unlock (&cachep->lock); } +void kmem_cache_free_bulk (struct kmem_cache *cachep,size_t size,void **list) +{ +if (kmalloc_verbose) +printk ("Bulk free %p[0-%lu]\n" , list, size -1 ); + +for (int i =0 ; i < size; i++) +kmem_cache_free (cachep, list[i]); +} +void *kmalloc (size_t size,gfp_t gfp) {void *ret;
tools/testing/radix-tree/linux/slab.h +1 -0
@@ -24,4 +24,5 @@ struct kmem_cache *kmem_cache_create(const char *name, unsigned int size, unsigned int align, unsigned int flags, void (*ctor)(void *)); +void kmem_cache_free_bulk (struct kmem_cache *cachep,size_t ,void **);#endif /* SLAB_H */
Signature · 1 line
2.28.0
PATCH 4/28 radix tree test suite: Add keme_cache_alloc_bulk() support
In reply to Liam R. Howlett--- tools/testing/radix-tree/linux.c | 51 +++++++++++++++++++++++++++ tools/testing/radix-tree/linux/slab.h | 1 + 2 files changed, 52 insertions(+)
tools/testing/radix-tree/linux.c +51 -0
@@ -99,6 +99,57 @@ void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)for (int i =0 ; i < size; i++)kmem_cache_free (cachep, list[i]); } +int kmem_cache_alloc_bulk (struct kmem_cache *cachep,gfp_t gfp,size_t size, +void **p) +{ +size_t i; + +if (kmalloc_verbose) +printk ("Bulk alloc %lu\n" , size); + +if (!(gfp & __GFP_DIRECT_RECLAIM) && cachep->non_kernel < size) +return 0 ; + +if (!(gfp & __GFP_DIRECT_RECLAIM)) + cachep->non_kernel -= size; + +pthread_mutex_lock (&cachep->lock); +if (cachep->nr_objs >= size) { +struct radix_tree_node *node = cachep->objs; + +for (i =0 ; i < size; i++) { + cachep->nr_objs--; + cachep->objs = node->parent; + p[i] = cachep->objs; + } +pthread_mutex_unlock (&cachep->lock); + node->parent =NULL ; + }else { +pthread_mutex_unlock (&cachep->lock); +for (i =0 ; i < size; i++) { +if (cachep->align) { +posix_memalign (&p[i], cachep->align, + cachep->size * size); + }else { + p[i] =malloc (cachep->size * size); + } +if (cachep->ctor) + cachep->ctor (p[i]); +else if (gfp & __GFP_ZERO) +memset (p[i],0 , cachep->size); + } + } + +for (i =0 ; i < size; i++) { +uatomic_inc (&nr_allocated); +uatomic_inc (&nr_tallocated); +if (kmalloc_verbose) +printf ("Allocating %p from slab\n" , p[i]); + } + +return size; +} +void *kmalloc (size_t size,gfp_t gfp) {
tools/testing/radix-tree/linux/slab.h +1 -0
@@ -25,4 +25,5 @@ struct kmem_cache *kmem_cache_create(const char *name, unsigned int size, void (*ctor)(void *));void kmem_cache_free_bulk (struct kmem_cache *cachep,size_t ,void **); +int kmem_cache_alloc_bulk (struct kmem_cache *cachep,gfp_t ,size_t ,void **);#endif /* SLAB_H */
Signature · 1 line
2.28.0
PATCH 5/28 Maple Tree: Add new data structure
In reply to Liam R. HowlettThe Maple Tree is a new data structure optimised for storing ranges.
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/maple-tree.rst | 36 + MAINTAINERS | 12 + include/linux/maple_tree.h | 439 + include/trace/events/maple_tree.h | 227 + lib/Makefile | 3 +- lib/maple_tree.c | 5693 +++ lib/test_maple_tree.c | 35821 ++++++++++++++++ tools/testing/radix-tree/.gitignore | 2 + tools/testing/radix-tree/Makefile | 13 +- tools/testing/radix-tree/generated/autoconf.h | 1 + tools/testing/radix-tree/linux.c | 2 + tools/testing/radix-tree/linux/maple_tree.h | 3 + tools/testing/radix-tree/maple.c | 58 + tools/testing/radix-tree/test.h | 1 + .../radix-tree/trace/events/maple_tree.h | 8 + 16 files changed, 42317 insertions(+), 3 deletions(-) create mode 100644 Documentation/core-api/maple-tree.rst create mode 100644 include/linux/maple_tree.h create mode 100644 include/trace/events/maple_tree.h create mode 100644 lib/maple_tree.c create mode 100644 lib/test_maple_tree.c create mode 100644 tools/testing/radix-tree/linux/maple_tree.h create mode 100644 tools/testing/radix-tree/maple.c create mode 100644 tools/testing/radix-tree/trace/events/maple_tree.h
Documentation/core-api/index.rst +1 -0
@@ -43,6 +43,7 @@ Library functionality that is used throughout the kernel. this_cpu_ops timekeeping errseq + maple-tree Concurrency primitives ======================
Documentation/core-api/maple-tree.rst new file +36 -0
@@ -0,0 +1,36 @@ +.. SPDX-License-Identifier: GPL-2.0+ + + +========== +Maple Tree +========== + +:Author: Liam R. Howlett + +Overview +======== + +The Maple Tree is an RCU safe Range tree based on the B-Tree. + +Normal API +========== + +Allocating Nodes +---------------- + +Locking +------- + +Advanced API +============ + + +Internal Entries +---------------- + +Functions and structures +======================== + +.. kernel-doc:: include/linux/maple_tree.c +.. kernel-doc:: lib/maple_tree.c +
MAINTAINERS +12 -0
@@ -10456,6 +10456,18 @@ L: [email protected] S: Maintained W: http://www.kernel.org/doc/man-pages +MAPLE TREE +M: Liam R. Howlett <[email protected]> +L: [email protected] +S: Supported +F: Documentation/core-api/maple_tree.rst +F: include/linux/maple_tree.h +F: include/trace/events/maple_tree.h +F: lib/maple_tree.c +F: lib/test_maple_tree.c +F: tools/testing/radix-tree/linux/maple_tree.h +F: tools/testing/radix-tree/maple.c + MARDUK (CREATOR CI40) DEVICE TREE SUPPORT M: Rahul Bedarkar <[email protected]> L: [email protected]
include/linux/maple_tree.h new file +439 -0
439 lines not on this page · raw message
include/trace/events/maple_tree.h new file +227 -0
227 lines not on this page · raw message
lib/Makefile +2 -1
@@ -32,7 +32,8 @@ KCSAN_SANITIZE_random32.o := n lib-y := ctype.o string.o vsprintf.o cmdline.o \ rbtree.o radix-tree.o timerqueue.o xarray.o \ - idr.o extable.o sha1.o irq_regs.o argv_split.o \ + maple_tree.o idr.o extable.o \ + sha1.o irq_regs.o argv_split.o \ flex_proportions.o ratelimit.o show_mem.o \ is_single_threaded.o plist.o decompress.o kobject_uevent.o \ earlycpio.o seq_buf.o siphash.o dec_and_lock.o \
lib/maple_tree.c new file +5693 -0
2,264 lines not on this page · raw message
lib/test_maple_tree.c new file +35821 -0
35,821 lines not on this page · raw message
tools/testing/radix-tree/generated/autoconf.h +1 -0
Lines of this file are not shown. View the raw message.
tools/testing/radix-tree/linux/maple_tree.h new file +3 -0
Lines of this file are not shown. View the raw message.
tools/testing/radix-tree/maple.c new file +58 -0
58 lines not on this page · raw message
tools/testing/radix-tree/trace/events/maple_tree.h new file +8 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.28.0
Part of the diff is not shown here. View the raw message.
PATCH 6/28 mm: Start tracking VMAs with maple tree
In reply to Liam R. HowlettStart tracking the VMAs with the new maple tree structure in parallel with the rb_tree. Add debug and trace events for maple tree operations and duplicate the rb_tree that is created on forks into the maple tree.
--- arch/x86/kernel/tboot.c | 1 + drivers/firmware/efi/efi.c | 1 + include/linux/mm.h | 2 + include/linux/mm_types.h | 2 + include/trace/events/mmap.h | 97 +++++++++++++++ init/main.c | 2 + kernel/fork.c | 4 + mm/init-mm.c | 2 + mm/mmap.c | 228 +++++++++++++++++++++++++++++++++++- 9 files changed, 336 insertions(+), 3 deletions(-)
arch/x86/kernel/tboot.c +1 -0
@@ -90,6 +90,7 @@ void __init tboot_probe(void)static pgd_t *tboot_pg_dir;static struct mm_struct tboot_mm = { .mm_rb =RB_ROOT , + .mm_mt =MTREE_INIT (mm_mt,MAPLE_ALLOC_RANGE ), .pgd = swapper_pg_dir, .mm_users =ATOMIC_INIT (2 ), .mm_count =ATOMIC_INIT (1 ),
drivers/firmware/efi/efi.c +1 -0
@@ -55,6 +55,7 @@ static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR;struct mm_struct efi_mm = { .mm_rb =RB_ROOT , + .mm_mt =MTREE_INIT (mm_mt,MAPLE_ALLOC_RANGE ), .mm_users =ATOMIC_INIT (2 ), .mm_count =ATOMIC_INIT (1 ),MMAP_LOCK_INITIALIZER (efi_mm)
include/linux/mm.h +2 -0
@@ -2467,6 +2467,8 @@ extern bool arch_has_descending_max_zone_pfns(void);/* nommu.c */ extern atomic_long_t mmap_pages_allocated;extern int nommu_shrink_inode_mappings (struct inode *,size_t ,size_t ); +/* maple_tree */ +void vma_store (struct mm_struct *mm,struct vm_area_struct *vma);/* interval_tree.c */ void vma_interval_tree_insert(struct vm_area_struct *node,
include/linux/mm_types.h +2 -0
@@ -8,6 +8,7 @@#include <linux/list.h> #include <linux/spinlock.h> #include <linux/rbtree.h> +#include <linux/maple_tree.h> #include <linux/rwsem.h> #include <linux/completion.h> #include <linux/cpumask.h>
@@ -388,6 +389,7 @@ struct kioctx_table;struct mm_struct {struct {struct vm_area_struct *mmap;/* list of VMAs */ +struct maple_tree mm_mt;struct rb_root mm_rb;u64 vmacache_seqnum;/* per-thread vmacache */ #ifdef CONFIG_MMU
include/trace/events/mmap.h +97 -0
103 lines not on this page · raw message
init/main.c +2 -0
@@ -114,6 +114,7 @@ static int kernel_init(void *);extern void init_IRQ (void );extern void radix_tree_init (void ); +extern void maple_tree_init (void );/* * Debug helper: via this flag we know that we are in 'early bootup code'
@@ -913,6 +914,7 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void) "Interrupts were enabled *very* early, fixing it\n"))local_irq_disable ();radix_tree_init (); +maple_tree_init ();/* * Set up housekeeping before setting up workqueues to allow the unbound
kernel/fork.c +4 -0
@@ -588,6 +588,9 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, rb_link = &tmp->vm_rb.rb_right; rb_parent = &tmp->vm_rb; +/* Link the vma into the MT */ +vma_store (mm, tmp); + mm->map_count++;if (!(tmp->vm_flags &VM_WIPEONFORK )) retval =copy_page_range (tmp, mpnt);
@@ -1004,6 +1007,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, { mm->mmap =NULL ; mm->mm_rb =RB_ROOT ; +mt_init_flags (&mm->mm_mt,MAPLE_ALLOC_RANGE ); mm->vmacache_seqnum =0 ;atomic_set (&mm->mm_users,1 );atomic_set (&mm->mm_count,1 );
mm/init-mm.c +2 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/mm_types.h> #include <linux/rbtree.h> +#include <linux/maple_tree.h> #include <linux/rwsem.h> #include <linux/spinlock.h> #include <linux/list.h>
@@ -28,6 +29,7 @@*/ struct mm_struct init_mm = { .mm_rb =RB_ROOT , + .mm_mt =MTREE_INIT (mm_mt,MAPLE_ALLOC_RANGE ), .pgd = swapper_pg_dir, .mm_users =ATOMIC_INIT (2 ), .mm_count =ATOMIC_INIT (1 ),
mm/mmap.c +225 -3
483 lines not on this page · raw message
Signature · 1 line
2.28.0
kernel test robot
build botRe: [PATCH 06/28] mm: Start tracking VMAs with maple tree
I love your patch! Yet something to improve:
Hi "Liam,
I love your patch! Yet something to improve:
[auto build test ERROR on efi/next]
[also build test ERROR on linus/master v5.10-rc7]
[cannot apply to tip/x86/core hnaz-linux-mm/master next-20201211] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Liam-R-Howlett/RFC-mm-Introducing-the-Maple-Tree/20201211-011029 base: https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git next config: i386-randconfig-s001-20201210 (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce: # apt-get install sparse # sparse version: v0.6.3-179-ga00755aa-dirty # https://github.com/0day-ci/linux/commit/991a17ff32a6bc9549655cd0aea43c386fdea1c6 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Liam-R-Howlett/RFC-mm-Introducing-the-Maple-Tree/20201211-011029 git checkout 991a17ff32a6bc9549655cd0aea43c386fdea1c6 # save the attached .config to linux build tree make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
In file included from include/linux/mm_types.h:11,
from include/linux/mmzone.h:21,
from include/linux/gfp.h:6,
from include/linux/slab.h:15,
from include/linux/crypto.h:20,
from arch/x86/kernel/asm-offsets.c:9:
>> include/linux/maple_tree.h:79:22: error: 'MAPLE_RANGE64_SLOTS' undeclared here (not in a function)
79 | unsigned long pivot[MAPLE_RANGE64_SLOTS - 1];
| ^~~~~~~~~~~~~~~~~~~
>> include/linux/maple_tree.h:85:22: error: 'MAPLE_ARANGE64_SLOTS' undeclared here (not in a function)
85 | unsigned long pivot[MAPLE_ARANGE64_SLOTS - 1];
| ^~~~~~~~~~~~~~~~~~~~
>> include/linux/maple_tree.h:91:28: error: 'MAPLE_NODE_SLOTS' undeclared here (not in a function); did you mean 'MAPLE_ALLOC_SLOTS'?
91 | #define MAPLE_ALLOC_SLOTS (MAPLE_NODE_SLOTS - 1)
| ^~~~~~~~~~~~~~~~
include/linux/maple_tree.h:96:27: note: in expansion of macro 'MAPLE_ALLOC_SLOTS'
96 | struct maple_alloc *slot[MAPLE_ALLOC_SLOTS];
| ^~~~~~~~~~~~~~~~~
--
In file included from include/linux/mm_types.h:11,
from include/linux/mmzone.h:21,
from include/linux/gfp.h:6,
from include/linux/slab.h:15,
from include/linux/crypto.h:20,
from arch/x86/kernel/asm-offsets.c:9:
>> include/linux/maple_tree.h:79:22: error: 'MAPLE_RANGE64_SLOTS' undeclared here (not in a function)
79 | unsigned long pivot[MAPLE_RANGE64_SLOTS - 1];
| ^~~~~~~~~~~~~~~~~~~
>> include/linux/maple_tree.h:85:22: error: 'MAPLE_ARANGE64_SLOTS' undeclared here (not in a function)
85 | unsigned long pivot[MAPLE_ARANGE64_SLOTS - 1];
| ^~~~~~~~~~~~~~~~~~~~
>> include/linux/maple_tree.h:91:28: error: 'MAPLE_NODE_SLOTS' undeclared here (not in a function); did you mean 'MAPLE_ALLOC_SLOTS'?
91 | #define MAPLE_ALLOC_SLOTS (MAPLE_NODE_SLOTS - 1)
| ^~~~~~~~~~~~~~~~
include/linux/maple_tree.h:96:27: note: in expansion of macro 'MAPLE_ALLOC_SLOTS'
96 | struct maple_alloc *slot[MAPLE_ALLOC_SLOTS];
| ^~~~~~~~~~~~~~~~~ make[2]: *** [scripts/Makefile.build:117: arch/x86/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1200: prepare0] Error 2
make[1]: Target 'modules_prepare' not remade because of errors.
make: *** [Makefile:185: __sub-make] Error 2
make: Target 'modules_prepare' not remade because of errors.
--
In file included from include/linux/mm_types.h:11,
from include/linux/mmzone.h:21,
from include/linux/gfp.h:6,
from include/linux/slab.h:15,
from include/linux/crypto.h:20,
from arch/x86/kernel/asm-offsets.c:9:
>> include/linux/maple_tree.h:79:22: error: 'MAPLE_RANGE64_SLOTS' undeclared here (not in a function)
79 | unsigned long pivot[MAPLE_RANGE64_SLOTS - 1];
| ^~~~~~~~~~~~~~~~~~~
>> include/linux/maple_tree.h:85:22: error: 'MAPLE_ARANGE64_SLOTS' undeclared here (not in a function)
85 | unsigned long pivot[MAPLE_ARANGE64_SLOTS - 1];
| ^~~~~~~~~~~~~~~~~~~~
>> include/linux/maple_tree.h:91:28: error: 'MAPLE_NODE_SLOTS' undeclared here (not in a function); did you mean 'MAPLE_ALLOC_SLOTS'?
91 | #define MAPLE_ALLOC_SLOTS (MAPLE_NODE_SLOTS - 1)
| ^~~~~~~~~~~~~~~~
include/linux/maple_tree.h:96:27: note: in expansion of macro 'MAPLE_ALLOC_SLOTS'
96 | struct maple_alloc *slot[MAPLE_ALLOC_SLOTS];
| ^~~~~~~~~~~~~~~~~ make[2]: *** [scripts/Makefile.build:117: arch/x86/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1200: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:185: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.
vim +/MAPLE_RANGE64_SLOTS +79 include/linux/maple_tree.h
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 40
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 41
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 42 /**
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 43 * maple_tree node explained
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 44 *
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 45 * Each node type has a number of slots for entries and a number of slots for
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 46 * pivots. In the case of dense nodes, the pivots are implied by the position
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 47 * and are simply the slot index + the minimum of the node.
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 48 *
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 49 * In regular B-Tree terms, pivots are called keys. The term pivot is used to
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 50 * indicate that the tree is specifying ranges, Pivots may appear in the
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 51 * subtree with an entry attached to the value where as keys are unique to a
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 52 * specific position of a B-tree. Pivot values are inclusive of the slot with
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 53 * the same index.
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 54 *
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 55 *
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 56 * The following illustrates the layout of a range64 nodes slots and pivots.
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 57 *
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 58 * _________________________________
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 59 * Slots -> | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 60 * ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 61 * │ │ │ │ │ │ │ │ └─ Implied maximum
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 62 * │ │ │ │ │ │ │ └─ Pivot 6
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 63 * │ │ │ │ │ │ └─ Pivot 5
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 64 * │ │ │ │ │ └─ Pivot 4
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 65 * │ │ │ │ └─ Pivot 3
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 66 * │ │ │ └─ Pivot 2
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 67 * │ │ └─ Pivot 1
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 68 * │ └─ Pivot 0
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 69 * └─ Implied minimum
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 70 *
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 71 * Slot contents:
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 72 * Internal (non-leaf) nodes contain pointers to other nodes.
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 73 * Leaf nodes contain entries.
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 74 *
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 75 *
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 76 */
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 77 struct maple_range_64 {
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 78 struct maple_pnode *parent;
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 @79 unsigned long pivot[MAPLE_RANGE64_SLOTS - 1];
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 80 void __rcu *slot[MAPLE_RANGE64_SLOTS];
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 81 };
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 82
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 83 struct maple_arange_64 {
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 84 struct maple_pnode *parent;
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 @85 unsigned long pivot[MAPLE_ARANGE64_SLOTS - 1];
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 86 void __rcu *slot[MAPLE_ARANGE64_SLOTS];
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 87 unsigned long gap[MAPLE_ARANGE64_SLOTS];
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 88 unsigned char meta;
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 89 };
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 90
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 @91 #define MAPLE_ALLOC_SLOTS (MAPLE_NODE_SLOTS - 1)
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 92 struct maple_alloc {
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 93 unsigned long total;
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 94 unsigned char node_count;
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 95 unsigned int request_count;
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 96 struct maple_alloc *slot[MAPLE_ALLOC_SLOTS];
d79b3ea66666bc9 Liam R. Howlett 2020-12-10 97 };d79b3ea66666bc9 Liam R. Howlett 2020-12-10 98
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
PATCH 7/28 mm/mmap: Introduce unlock_range() for code cleanup
In reply to Liam R. Howlett--- mm/mmap.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-)
mm/mmap.c +18 -19
@@ -2993,6 +2993,20 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,return __split_vma (mm, vma, addr, new_below); } +static inline void unlock_range (struct vm_area_struct *start,unsigned long limit) +{ +struct mm_struct *mm = start->vm_mm; +struct vm_area_struct *tmp = start; + +while (tmp && tmp->vm_start < limit) { +if (tmp->vm_flags &VM_LOCKED ) { + mm->locked_vm -=vma_pages (tmp); +munlock_vma_pages_all (tmp); + } + + tmp = tmp->vm_next; + } +}/* Munmap is split into 2 main parts -- this part which finds * what needs doing, and the areas themselves, which do the * work. This now handles partial unmappings.
@@ -3081,17 +3095,8 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,/* * unlock any mlock()ed ranges before detaching vmas */ -if (mm->locked_vm) { -struct vm_area_struct *tmp = vma; -while (tmp && tmp->vm_start < end) { -if (tmp->vm_flags &VM_LOCKED ) { - mm->locked_vm -=vma_pages (tmp); -munlock_vma_pages_all (tmp); - } - - tmp = tmp->vm_next; - } - } +if (mm->locked_vm) +unlock_range (vma, end);/* Detach vmas from rbtree */ if (!detach_vmas_to_be_unmapped (mm, vma, prev, end))
@@ -3394,14 +3399,8 @@ void exit_mmap(struct mm_struct *mm)mmap_write_unlock (mm); } -if (mm->locked_vm) { - vma = mm->mmap; -while (vma) { -if (vma->vm_flags &VM_LOCKED ) -munlock_vma_pages_all (vma); - vma = vma->vm_next; - } - } +if (mm->locked_vm) +unlock_range (mm->mmap,ULONG_MAX );arch_exit_mmap (mm);
Signature · 1 line
2.28.0
PATCH 8/28 mm/mmap: Change find_vma() to use the maple tree
In reply to Liam R. HowlettStart using the maple tree to find VMA entries in an mm_struct.
--- mm/mmap.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-)
mm/mmap.c +11 -18
@@ -2484,10 +2484,17 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,EXPORT_SYMBOL (get_unmapped_area); -/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ +/** +* find_vma() - Find the VMA for a given address, or the next vma. May return +* NULL in the case of no vma at addr or above +* @mm The mm_struct to check +* @addr: The address +* +* Returns: The VMA assoicated with addr, or the next vma. +* May return NULL in the case of no vma at addr or above. +*/ struct vm_area_struct *find_vma (struct mm_struct *mm,unsigned long addr) { -struct rb_node *rb_node;struct vm_area_struct *vma;/* Check the cache first. */
@@ -2495,24 +2502,10 @@ struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)if (likely (vma))return vma; - rb_node = mm->mm_rb.rb_node; - -while (rb_node) { -struct vm_area_struct *tmp; - - tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb); - -if (tmp->vm_end > addr) { - vma = tmp; -if (tmp->vm_start <= addr) -break ; - rb_node = rb_node->rb_left; - }else - rb_node = rb_node->rb_right; - } - + vma =mt_find (&mm->mm_mt, &addr,ULONG_MAX );if (vma)vmacache_update (addr, vma); +return vma; }EXPORT_SYMBOL (find_vma);
Signature · 1 line
2.28.0
PATCH 9/28 mm/mmap: Change find_vma_prev() to use maple tree
In reply to Liam R. HowlettChange the implementation of find_vma_prev to use the new maple tree data structure.
--- mm/mmap.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-)
mm/mmap.c +17 -10
@@ -2510,23 +2510,30 @@ struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr) }EXPORT_SYMBOL (find_vma); -/* -* Same as find_vma, but also return a pointer to the previous VMA in *pprev. +/** +* find_vma_prev() - Find the VMA for a given address, or the next vma and +* sets %pprev to the previous VMA, if any. +* @mm The mm_struct to check +* @addr: The address +* @pprev: The pointer to set to the previous VMA +* +* Returns: The VMA associated with @addr, or the next vma. +* May return NULL in the case of no vma at addr or above. */ struct vm_area_struct *find_vma_prev (struct mm_struct *mm,unsigned long addr, -struct vm_area_struct **pprev) +struct vm_area_struct **pprev) {struct vm_area_struct *vma; +MA_STATE (mas, &mm->mm_mt, addr, addr); - vma =find_vma (mm, addr); -if (vma) { - *pprev = vma->vm_prev; - }else { -struct rb_node *rb_node =rb_last (&mm->mm_rb); +rcu_read_lock (); + vma =mas_find (&mas,ULONG_MAX ); +if (!vma) +mas_reset (&mas); - *pprev = rb_node ? rb_entry(rb_node, struct vm_area_struct, vm_rb) : NULL; - } + *pprev =mas_prev (&mas,0 ); +rcu_read_unlock ();return vma; }
Signature · 1 line
2.28.0
PATCH 10/28 mm/mmap: Change unmapped_area and unmapped_area_topdown to use maple tree
In reply to Liam R. HowlettUse the new maple tree data structure to find an unmapped area.
--- mm/mmap.c | 249 ++++++------------------------------------------------ 1 file changed, 27 insertions(+), 222 deletions(-)
mm/mmap.c +27 -222
@@ -2050,260 +2050,65 @@ unsigned long mmap_region(struct file *file, unsigned long addr,return error; } +/* unmapped_area() Find an area between the low_limit and the high_limit with +* the correct alignment and offset, all from @info. Note: current->mm is used +* for the search. +* +* @info: The unmapped area information including the range (low_limit - +* hight_limit), the alignment offset and mask. +* +* Return: A memory address or -ENOMEM. +*/ static unsigned long unmapped_area (struct vm_unmapped_area_info *info) { -/* -* We implement the search by looking for an rbtree node that -* immediately follows a suitable gap. That is, -* - gap_start = vma->vm_prev->vm_end <= info->high_limit - length; -* - gap_end = vma->vm_start >= info->low_limit + length; -* - gap_end - gap_start >= length -*/ +unsigned long length, gap; -struct mm_struct *mm = current->mm; -struct vm_area_struct *vma; -unsigned long length, low_limit, high_limit, gap_start, gap_end; -unsigned long gap; +MA_STATE (mas, ¤t->mm->mm_mt,0 ,0 ); +validate_mm (current->mm); -MA_STATE (mas, &mm->mm_mt,0 ,0 );/* Adjust search length to account for worst case alignment overhead */ length = info->length + info->align_mask;if (length < info->length)return -ENOMEM ; - -/* Maple tree is self contained. */ -rcu_read_lock ();if (mas_get_empty_area (&mas, info->low_limit, info->high_limit -1 , length)) {rcu_read_unlock ();return -ENOMEM ; } -rcu_read_unlock (); gap = mas.index; gap += (info->align_offset - gap) & info->align_mask; - -/* Adjust search limits by the desired length */ -if (info->high_limit < length) -return -ENOMEM ; - high_limit = info->high_limit - length; - -if (info->low_limit > high_limit) -return -ENOMEM ; - low_limit = info->low_limit + length; - -/* Check if rbtree root looks promising */ -if (RB_EMPTY_ROOT (&mm->mm_rb)) - goto check_highest; - vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb); -if (vma->rb_subtree_gap < length) - goto check_highest; - -while (true) { -/* Visit left subtree if it looks promising */ - gap_end =vm_start_gap (vma); -if (gap_end >= low_limit && vma->vm_rb.rb_left) { -struct vm_area_struct *left = -rb_entry (vma->vm_rb.rb_left, - struct vm_area_struct, vm_rb); -if (left->rb_subtree_gap >= length) { - vma = left; -continue ; - } - } - - gap_start = vma->vm_prev ?vm_end_gap (vma->vm_prev) :0 ; -check_current: -/* Check if current node has a suitable gap */ -if (gap_start > high_limit) -return -ENOMEM ; -if (gap_end >= low_limit && - gap_end > gap_start && gap_end - gap_start >= length) - goto found; - -/* Visit right subtree if it looks promising */ -if (vma->vm_rb.rb_right) { -struct vm_area_struct *right = -rb_entry (vma->vm_rb.rb_right, - struct vm_area_struct, vm_rb); -if (right->rb_subtree_gap >= length) { - vma = right; -continue ; - } - } - -/* Go back up the rbtree to find next candidate node */ -while (true) { -struct rb_node *prev = &vma->vm_rb; -if (!rb_parent (prev)) - goto check_highest; - vma =rb_entry (rb_parent (prev), - struct vm_area_struct, vm_rb); -if (prev == vma->vm_rb.rb_left) { - gap_start =vm_end_gap (vma->vm_prev); - gap_end =vm_start_gap (vma); - goto check_current; - } - } - } - -check_highest: -/* Check highest gap, which does not precede any rbtree node */ - gap_start = mm->highest_vm_end; - gap_end =ULONG_MAX ;/* Only for VM_BUG_ON below */ -if (gap_start > high_limit) -return -ENOMEM ; - -found: -/* We found a suitable gap. Clip it with the original low_limit. */ -if (gap_start < info->low_limit) - gap_start = info->low_limit; - -/* Adjust gap address to the desired alignment */ - gap_start += (info->align_offset - gap_start) & info->align_mask; - -VM_BUG_ON (gap_start + info->length > info->high_limit); -VM_BUG_ON (gap_start + info->length > gap_end); - -VM_BUG_ON (gap != gap_start); -return gap_start; +return gap; } +/* unmapped_area() Find an area between the low_limit and the high_limit with +* the correct alignment and offset at the highest available address, all from +* @info. Note: current->mm is used for the search. +* +* @info: The unmapped area information including the range (low_limit - +* hight_limit), the alignment offset and mask. +* +* Return: A memory address or -ENOMEM. +*/ static unsigned long unmapped_area_topdown (struct vm_unmapped_area_info *info) { -struct mm_struct *mm = current->mm; -struct vm_area_struct *vma =NULL ; -unsigned long length, low_limit, high_limit, gap_start, gap_end; -unsigned long gap; +unsigned long length, gap; -MA_STATE (mas, &mm->mm_mt,0 ,0 ); -validate_mm_mt (mm); +MA_STATE (mas, ¤t->mm->mm_mt,0 ,0 ); +validate_mm_mt (current->mm);/* Adjust search length to account for worst case alignment overhead */ length = info->length + info->align_mask;if (length < info->length)return -ENOMEM ; -rcu_read_lock (); -if (mas_get_empty_area_rev (&mas, info->low_limit, info->high_limit, +if (mas_get_empty_area_rev (&mas, info->low_limit, info->high_limit -1 , length)) {rcu_read_unlock ();return -ENOMEM ; } -rcu_read_unlock (); gap = (mas.index + info->align_mask) & ~info->align_mask; gap -= info->align_offset & info->align_mask; -/* -* Adjust search limits by the desired length. -* See implementation comment at top of unmapped_area(). -*/ - gap_end = info->high_limit; -if (gap_end < length) -return -ENOMEM ; - high_limit = gap_end - length; - -if (info->low_limit > high_limit) -return -ENOMEM ; - low_limit = info->low_limit + length; - -/* Check highest gap, which does not precede any rbtree node */ - gap_start = mm->highest_vm_end; -if (gap_start <= high_limit) - goto found_highest; - -/* Check if rbtree root looks promising */ -if (RB_EMPTY_ROOT (&mm->mm_rb)) -return -ENOMEM ; - vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb); -if (vma->rb_subtree_gap < length) -return -ENOMEM ; - -while (true) { -/* Visit right subtree if it looks promising */ - gap_start = vma->vm_prev ?vm_end_gap (vma->vm_prev) :0 ; -if (gap_start <= high_limit && vma->vm_rb.rb_right) { -struct vm_area_struct *right = -rb_entry (vma->vm_rb.rb_right, - struct vm_area_struct, vm_rb); -if (right->rb_subtree_gap >= length) { - vma = right; -continue ; - } - } - -check_current: -/* Check if current node has a suitable gap */ - gap_end =vm_start_gap (vma); -if (gap_end < low_limit) -return -ENOMEM ; -if (gap_start <= high_limit && - gap_end > gap_start && gap_end - gap_start >= length) - goto found; - -/* Visit left subtree if it looks promising */ -if (vma->vm_rb.rb_left) { -struct vm_area_struct *left = -rb_entry (vma->vm_rb.rb_left, - struct vm_area_struct, vm_rb); -if (left->rb_subtree_gap >= length) { - vma = left; -continue ; - } - } - -/* Go back up the rbtree to find next candidate node */ -while (true) { -struct rb_node *prev = &vma->vm_rb; -if (!rb_parent (prev)) -return -ENOMEM ; - vma =rb_entry (rb_parent (prev), - struct vm_area_struct, vm_rb); -if (prev == vma->vm_rb.rb_right) { - gap_start = vma->vm_prev ? -vm_end_gap (vma->vm_prev) :0 ; - goto check_current; - } - } - } - -found: -/* We found a suitable gap. Clip it with the original high_limit. */ -if (gap_end > info->high_limit) - gap_end = info->high_limit; - -found_highest: -/* Compute highest gap address at the desired alignment */ - gap_end -= info->length; - gap_end -= (gap_end - info->align_offset) & info->align_mask; - -VM_BUG_ON (gap_end < info->low_limit); -VM_BUG_ON (gap_end < gap_start); - -if (gap != gap_end) { -pr_err ("%s: %px Gap was found: mt %lu gap_end %lu\n" , __func__, - mm, gap, gap_end); -pr_err ("window was %lu - %lu size %lu\n" , info->high_limit, - info->low_limit, length); -pr_err ("mas.min %lu max %lu mas.last %lu\n" , mas.min, mas.max, - mas.last); -pr_err ("mas.index %lu align mask %lu offset %lu\n" , mas.index, - info->align_mask, info->align_offset); -pr_err ("rb_find_vma find on %lu => %px (%px)\n" , mas.index, -find_vma (mm, mas.index), vma); -#if defined(CONFIG_DEBUG_MAPLE_TREE ) -mt_dump (&mm->mm_mt); -#endif - { -struct vm_area_struct *dv = mm->mmap; - -while (dv) { -printk ("vma %px %lu-%lu\n" , dv, dv->vm_start, dv->vm_end); - dv = dv->vm_next; - } - } -VM_BUG_ON (gap != gap_end); - } - -return gap_end; +return gap; }/*
Signature · 1 line
2.28.0
PATCH 11/28 kernel/fork: Convert dup_mmap to use maple tree
In reply to Liam R. HowlettUse the maple tree iterator to duplicate the mm_struct trees.
--- include/linux/sched/mm.h | 3 +++ kernel/fork.c | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-)
include/linux/sched/mm.h +3 -0
@@ -8,6 +8,7 @@#include <linux/mm_types.h> #include <linux/gfp.h> #include <linux/sync_core.h> +#include <linux/maple_tree.h> /* * Routines for handling mm_structs
@@ -67,11 +68,13 @@ static inline void mmdrop(struct mm_struct *mm)*/ static inline void mmget (struct mm_struct *mm) { +mt_set_in_rcu (&mm->mm_mt);atomic_inc (&mm->mm_users); }static inline bool mmget_not_zero (struct mm_struct *mm) { +mt_set_in_rcu (&mm->mm_mt);return atomic_inc_not_zero (&mm->mm_users); }
kernel/fork.c +17 -5
@@ -472,7 +472,9 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,struct vm_area_struct *mpnt, *tmp, *prev, **pprev;struct rb_node **rb_link, *rb_parent;int retval; -unsigned long charge; +unsigned long charge =0 ; +MA_STATE (old_mas, &oldmm->mm_mt,0 ,0 ); +MA_STATE (mas, &mm->mm_mt,0 ,0 );LIST_HEAD (uf);uprobe_start_dup_mmap ();
@@ -506,7 +508,12 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, goto out; prev =NULL ; -for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { + + retval =mas_entry_count (&mas, oldmm->map_count); +if (retval) + goto fail_nomem; + +mas_for_each (&old_mas, mpnt,ULONG_MAX ) {struct file *file;if (mpnt->vm_flags &VM_DONTCOPY ) {
@@ -520,7 +527,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,*/ if (fatal_signal_pending (current)) { retval = -EINTR ; - goto out; + goto loop_out; }if (mpnt->vm_flags &VM_ACCOUNT ) {unsigned long len =vma_pages (mpnt);
@@ -589,7 +596,9 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, rb_parent = &tmp->vm_rb;/* Link the vma into the MT */ -vma_store (mm, tmp); + mas.index = tmp->vm_start; + mas.last = tmp->vm_end -1 ; +mas_store (&mas, tmp); mm->map_count++;if (!(tmp->vm_flags &VM_WIPEONFORK ))
@@ -599,14 +608,16 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, tmp->vm_ops->open (tmp);if (retval) - goto out; + goto loop_out; }/* a new mm has just been created */ retval =arch_dup_mmap (oldmm, mm); +loop_out: out:mmap_write_unlock (mm);flush_tlb_mm (oldmm);mmap_write_unlock (oldmm); +mas_destroy (&mas);dup_userfaultfd_complete (&uf); fail_uprobe_end:uprobe_end_dup_mmap ();
@@ -1076,6 +1087,7 @@ static inline void __mmput(struct mm_struct *mm) {VM_BUG_ON (atomic_read (&mm->mm_users)); +mt_clear_in_rcu (&mm->mm_mt);uprobe_clear_state (mm);exit_aio (mm);ksm_exit (mm);
Signature · 1 line
2.28.0
PATCH 12/28 mm: Remove rb tree.
In reply to Liam R. HowlettRemove the RB tree and start using the maple tree for vm_area_struct tracking.
--- arch/x86/kernel/tboot.c | 1 - drivers/firmware/efi/efi.c | 1 - include/linux/mm.h | 4 - include/linux/mm_types.h | 27 +- kernel/fork.c | 8 - mm/init-mm.c | 2 - mm/mmap.c | 520 ++++++++----------------------------- mm/util.c | 8 + 8 files changed, 125 insertions(+), 446 deletions(-)
arch/x86/kernel/tboot.c +0 -1
@@ -89,7 +89,6 @@ void __init tboot_probe(void)static pgd_t *tboot_pg_dir;static struct mm_struct tboot_mm = { - .mm_rb =RB_ROOT , .mm_mt =MTREE_INIT (mm_mt,MAPLE_ALLOC_RANGE ), .pgd = swapper_pg_dir, .mm_users =ATOMIC_INIT (2 ),
drivers/firmware/efi/efi.c +0 -1
@@ -54,7 +54,6 @@ static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR;static unsigned long __initdata rt_prop =EFI_INVALID_TABLE_ADDR ;struct mm_struct efi_mm = { - .mm_rb =RB_ROOT , .mm_mt =MTREE_INIT (mm_mt,MAPLE_ALLOC_RANGE ), .mm_users =ATOMIC_INIT (2 ), .mm_count =ATOMIC_INIT (1 ),
include/linux/mm.h +0 -4
@@ -2467,8 +2467,6 @@ extern bool arch_has_descending_max_zone_pfns(void);/* nommu.c */ extern atomic_long_t mmap_pages_allocated;extern int nommu_shrink_inode_mappings (struct inode *,size_t ,size_t ); -/* maple_tree */ -void vma_store (struct mm_struct *mm,struct vm_area_struct *vma);/* interval_tree.c */ void vma_interval_tree_insert(struct vm_area_struct *node,
@@ -2524,8 +2522,6 @@ extern int __split_vma(struct mm_struct *, struct vm_area_struct *,extern int split_vma (struct mm_struct *,struct vm_area_struct *,unsigned long addr,int new_below);extern int insert_vm_struct (struct mm_struct *,struct vm_area_struct *); -extern void __vma_link_rb (struct mm_struct *,struct vm_area_struct *, -struct rb_node **,struct rb_node *);extern void unlink_file_vma (struct vm_area_struct *); extern struct vm_area_struct *copy_vma(struct vm_area_struct **, unsigned long addr, unsigned long len, pgoff_t pgoff,
include/linux/mm_types.h +8 -19
@@ -274,7 +274,6 @@ typedef unsigned long vm_flags_t;* map parts of them. */ struct vm_region { -struct rb_node vm_rb;/* link in global region tree */ vm_flags_t vm_flags;/* VMA vm_flags */ unsigned long vm_start;/* start address of region */ unsigned long vm_end;/* region initialised to here */
@@ -312,19 +311,6 @@ struct vm_area_struct {/* linked list of VM areas per task, sorted by address */ struct vm_area_struct *vm_next, *vm_prev; - -struct rb_node vm_rb; - -/* -* Largest free memory gap in bytes to the left of this VMA. -* Either between this VMA and vma->vm_prev, or between one of the -* VMAs below us in the VMA rbtree and its ->vm_prev. This helps -* get_unmapped_area find a free area of the right size. -*/ -unsigned long rb_subtree_gap; - -/* Second cache line starts here. */ -struct mm_struct *vm_mm;/* The address space we belong to. */ /*
@@ -334,6 +320,12 @@ struct vm_area_struct {pgprot_t vm_page_prot;unsigned long vm_flags;/* Flags, see mm.h. */ +/* Information about our backing store: */ +unsigned long vm_pgoff;/* Offset (within vm_file) in PAGE_SIZE +* units +*/ +/* Second cache line starts here. */ +struct file *vm_file;/* File we map to (can be NULL). */ /* * For areas with an address space and backing store, * linkage into the address_space->i_mmap interval tree.
@@ -352,16 +344,14 @@ struct vm_area_struct {struct list_head anon_vma_chain;/* Serialized by mmap_lock & * page_table_lock */ struct anon_vma *anon_vma;/* Serialized by page_table_lock */ +/* Third cache line starts here. */ /* Function pointers to deal with this struct. */ const struct vm_operations_struct *vm_ops; -/* Information about our backing store: */ -unsigned long vm_pgoff;/* Offset (within vm_file) in PAGE_SIZE -units */ -struct file * vm_file;/* File we map to (can be NULL). */ void * vm_private_data;/* was vm_pte (shared mem) */ +#ifdef CONFIG_SWAP atomic_long_t swap_readahead_info;#endif
@@ -390,7 +380,6 @@ struct mm_struct {struct {struct vm_area_struct *mmap;/* list of VMAs */ struct maple_tree mm_mt; -struct rb_root mm_rb;u64 vmacache_seqnum;/* per-thread vmacache */ #ifdef CONFIG_MMU unsigned long (*get_unmapped_area) (struct file *filp,
kernel/fork.c +0 -8
@@ -470,7 +470,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) {struct vm_area_struct *mpnt, *tmp, *prev, **pprev; -struct rb_node **rb_link, *rb_parent;int retval;unsigned long charge =0 ;MA_STATE (old_mas, &oldmm->mm_mt,0 ,0 );
@@ -497,8 +496,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, mm->exec_vm = oldmm->exec_vm; mm->stack_vm = oldmm->stack_vm; - rb_link = &mm->mm_rb.rb_node; - rb_parent =NULL ; pprev = &mm->mmap; retval =ksm_fork (mm, oldmm);if (retval)
@@ -591,10 +588,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, tmp->vm_prev = prev; prev = tmp; -__vma_link_rb (mm, tmp, rb_link, rb_parent); - rb_link = &tmp->vm_rb.rb_right; - rb_parent = &tmp->vm_rb; -/* Link the vma into the MT */ mas.index = tmp->vm_start; mas.last = tmp->vm_end -1 ;
@@ -1017,7 +1010,6 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, struct user_namespace *user_ns) { mm->mmap =NULL ; - mm->mm_rb =RB_ROOT ;mt_init_flags (&mm->mm_mt,MAPLE_ALLOC_RANGE ); mm->vmacache_seqnum =0 ;atomic_set (&mm->mm_users,1 );
mm/init-mm.c +0 -2
@@ -1,6 +1,5 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/mm_types.h> -#include <linux/rbtree.h> #include <linux/maple_tree.h> #include <linux/rwsem.h> #include <linux/spinlock.h>
@@ -28,7 +27,6 @@* and size this cpu_bitmask to NR_CPUS. */ struct mm_struct init_mm = { - .mm_rb =RB_ROOT , .mm_mt =MTREE_INIT (mm_mt,MAPLE_ALLOC_RANGE ), .pgd = swapper_pg_dir, .mm_users =ATOMIC_INIT (2 ),
mm/mmap.c +109 -411
886 lines not on this page · raw message
mm/util.c +8 -0
@@ -287,6 +287,8 @@ void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma, vma->vm_next = next;if (next) next->vm_prev = vma; +else + mm->highest_vm_end =vm_end_gap (vma); }void __vma_unlink_list (struct mm_struct *mm,struct vm_area_struct *vma)
@@ -301,6 +303,12 @@ void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma) mm->mmap = next;if (next) next->vm_prev = prev; +else { +if (prev) + mm->highest_vm_end =vm_end_gap (prev); +else + mm->highest_vm_end =0 ; + } }/* Check if the vma is being used as a stack by this task */
Signature · 1 line
2.28.0
PATCH 13/28 mm/gup: Expose mm_populate_vma() for use when the vma is known
In reply to Liam R. HowlettWhen a vma is known, avoid calling mm_populate to search for the vma to populate.
--- include/linux/mm.h | 4 ++++ mm/gup.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+)
include/linux/mm.h +4 -0
@@ -2587,8 +2587,12 @@ static inline void mm_populate(unsigned long addr, unsigned long len)/* Ignore errors */ (void) __mm_populate(addr, len, 1); } +extern void mm_populate_vma (struct vm_area_struct *vma,unsigned long start, +unsigned long end);#else static inline void mm_populate (unsigned long addr,unsigned long len) {} +void mm_populate_vma (struct vm_area_struct *vma,unsigned long start, +unsigned long end) {}#endif /* These take the mm semaphore themselves */
mm/gup.c +20 -0
@@ -1431,6 +1431,26 @@ long populate_vma_page_range(struct vm_area_struct *vma, NULL, NULL, locked); } +/* +* mm_populate_vma() - Populate a single range in a single vma. +* @vma: The vma to populate. +* @start: The start address to populate +* @end: The end address to stop populating +* +* Note: Ignores errors. +*/ +void mm_populate_vma (struct vm_area_struct *vma,unsigned long start, +unsigned long end) +{ +struct mm_struct *mm = current->mm; +int locked =1 ; + +mmap_read_lock (mm); +populate_vma_page_range (vma, start, end, &locked); +if (locked) +mmap_read_unlock (mm); +} +/* * __mm_populate - populate and/or mlock pages within a range of address space. *
Signature · 1 line
2.28.0
kernel test robot
build botRe: [PATCH 13/28] mm/gup: Expose mm_populate_vma() for use when the vma is known
I love your patch! Perhaps something to improve:
Hi "Liam,
I love your patch! Perhaps something to improve:
[auto build test WARNING on efi/next]
[cannot apply to tip/x86/core linus/master hnaz-linux-mm/master v5.10-rc7 next-20201210] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Liam-R-Howlett/RFC-mm-Introducing-the-Maple-Tree/20201211-011029 base: https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git next config: sh-allmodconfig (attached as .config) compiler: sh4-linux-gcc (GCC) 9.3.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/e89ae5d0c814effeca4f7907d7dad47134561db2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Liam-R-Howlett/RFC-mm-Introducing-the-Maple-Tree/20201211-011029 git checkout e89ae5d0c814effeca4f7907d7dad47134561db2
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=sh
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
In file included from include/linux/mm_types.h:11,
from include/linux/mmzone.h:21,
from include/linux/gfp.h:6,
from include/linux/mm.h:10,
from arch/sh/kernel/asm-offsets.c:14:
include/linux/maple_tree.h:79:22: error: 'MAPLE_RANGE64_SLOTS' undeclared here (not in a function)
79 | unsigned long pivot[MAPLE_RANGE64_SLOTS - 1];
| ^~~~~~~~~~~~~~~~~~~
include/linux/maple_tree.h:85:22: error: 'MAPLE_ARANGE64_SLOTS' undeclared here (not in a function)
85 | unsigned long pivot[MAPLE_ARANGE64_SLOTS - 1];
| ^~~~~~~~~~~~~~~~~~~~
include/linux/maple_tree.h:91:28: error: 'MAPLE_NODE_SLOTS' undeclared here (not in a function); did you mean 'MAPLE_ALLOC_SLOTS'?
91 | #define MAPLE_ALLOC_SLOTS (MAPLE_NODE_SLOTS - 1)
| ^~~~~~~~~~~~~~~~
include/linux/maple_tree.h:96:27: note: in expansion of macro 'MAPLE_ALLOC_SLOTS'
96 | struct maple_alloc *slot[MAPLE_ALLOC_SLOTS];
| ^~~~~~~~~~~~~~~~~
In file included from arch/sh/kernel/asm-offsets.c:14:
>> include/linux/mm.h:2594:6: warning: no previous prototype for 'mm_populate_vma' [-Wmissing-prototypes] 2594 | void mm_populate_vma(struct vm_area_struct *vma, unsigned long start,
| ^~~~~~~~~~~~~~~
make[2]: *** [scripts/Makefile.build:117: arch/sh/kernel/asm-offsets.s] Error 1 make[2]: Target '__build' not remade because of errors.
make[1]: *** [Makefile:1200: prepare0] Error 2
make[1]: Target 'modules_prepare' not remade because of errors.
make: *** [Makefile:185: __sub-make] Error 2
make: Target 'modules_prepare' not remade because of errors.
vim +/mm_populate_vma +2594 include/linux/mm.h
2581
2582 #ifdef CONFIG_MMU
2583 extern int __mm_populate(unsigned long addr, unsigned long len,
2584 int ignore_errors);
2585 static inline void mm_populate(unsigned long addr, unsigned long len)
2586 {
2587 /* Ignore errors */
2588 (void) __mm_populate(addr, len, 1);
2589 }
2590 extern void mm_populate_vma(struct vm_area_struct *vma, unsigned long start,
2591 unsigned long end);
2592 #else
2593 static inline void mm_populate(unsigned long addr, unsigned long len) {}
> 2594 void mm_populate_vma(struct vm_area_struct *vma, unsigned long start,
2595 unsigned long end) {}
2596 #endif
2597
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
PATCH 14/28 mm/mmap: Change do_brk_flags() to expand existing VMA and add do_brk_munmap()
In reply to Liam R. HowlettAvoid allocating a new VMA when it is not necessary. Expand or contract the existing VMA instead. This avoids unnecessary tree manipulations and allocations.
Once the VMA is known, use it directly when populating to avoid unnecessary lookup work.
--- mm/mmap.c | 285 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 219 insertions(+), 66 deletions(-)
mm/mmap.c +219 -66
@@ -51,7 +51,6 @@#include <asm/cacheflush.h> #include <asm/tlb.h> #include <asm/mmu_context.h> -#define CREATE_TRACE_POINTS #include <trace/events/mmap.h>
@@ -184,18 +183,22 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)return next; } -static int do_brk_flags (unsigned long addr,unsigned long request,unsigned long flags, -struct list_head *uf); +static int do_brk_munmap (struct ma_state *mas,struct vm_area_struct *vma, +unsigned long newbrk,unsigned long oldbrk, +struct list_head *uf); +static int do_brk_flags (struct ma_state *mas,struct vm_area_struct **brkvma, +unsigned long addr,unsigned long request, +unsigned long flags); SYSCALL_DEFINE1(brk, unsigned long, brk) { -unsigned long retval;unsigned long newbrk, oldbrk, origbrk;struct mm_struct *mm = current->mm; -struct vm_area_struct *next; +struct vm_area_struct *brkvma, *next =NULL ;unsigned long min_brk;bool populate;bool downgraded = false;LIST_HEAD (uf); +MA_STATE (mas, &mm->mm_mt,0 ,0 );if (mmap_write_lock_killable (mm))return -EINTR ;
@@ -235,37 +238,41 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) goto success; } -/* -* Always allow shrinking brk. -* __do_munmap() may downgrade mmap_lock to read. -*/ -if (brk <= mm->brk) { +mas_set (&mas, newbrk); + brkvma =mas_walk (&mas); +if (brkvma) {// munmap necessary, there is something at newbrk. +/* +* Always allow shrinking brk. +* do_brk_munmap() may downgrade mmap_lock to read. +*/ int ret; -/* -* mm->brk must to be protected by write mmap_lock so update it -* before downgrading mmap_lock. When __do_munmap() fails, -* mm->brk will be restored from origbrk. +* mm->brk must to be protected by write mmap_lock. +* do_brk_munmap() may downgrade the lock, so update it +* before calling do_brk_munmap(). */ mm->brk = brk; - ret =__do_munmap (mm, newbrk, oldbrk-newbrk, &uf, true); -if (ret <0 ) { - mm->brk = origbrk; - goto out; - }else if (ret ==1 ) { + mas.last = oldbrk -1 ; + ret =do_brk_munmap (&mas, brkvma, newbrk, oldbrk, &uf); +if (ret ==1 ) { downgraded = true; - } - goto success; - } + goto success; + }else if (!ret) + goto success; + mm->brk = origbrk; + goto out; + } + next =mas_next (&mas,ULONG_MAX );/* Check against existing mmap mappings. */ - next =find_vma (mm, oldbrk);if (next && newbrk +PAGE_SIZE >vm_start_gap (next)) goto out;/* Ok, looks good - let it rip. */ -if (do_brk_flags (oldbrk, newbrk-oldbrk,0 , &uf) <0 ) + brkvma =mas_prev (&mas, mm->start_brk); +if (do_brk_flags (&mas, &brkvma, oldbrk, newbrk - oldbrk,0 ) <0 ) goto out; + mm->brk = brk; success:
@@ -276,13 +283,12 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)mmap_write_unlock (mm);userfaultfd_unmap_complete (mm, &uf);if (populate) -mm_populate (oldbrk, newbrk - oldbrk); +mm_populate_vma (brkvma, oldbrk, newbrk);return brk; out: - retval = origbrk;mmap_write_unlock (mm); -return retval; +return origbrk; }#if defined(CONFIG_DEBUG_MAPLE_TREE )
@@ -367,16 +373,16 @@ static void validate_mm(struct mm_struct *mm)validate_mm_mt (mm);while (vma) { +#ifdef CONFIG_DEBUG_VM_RB struct anon_vma *anon_vma = vma->anon_vma;struct anon_vma_chain *avc; -if (anon_vma) {anon_vma_lock_read (anon_vma);list_for_each_entry (avc, &vma->anon_vma_chain, same_vma)anon_vma_interval_tree_verify (avc);anon_vma_unlock_read (anon_vma); } - +#endif highest_address =vm_end_gap (vma); vma = vma->vm_next; i++;
@@ -538,7 +544,21 @@ static void __vma_link_file(struct vm_area_struct *vma)flush_dcache_mmap_unlock (mapping); } } -/* Private + +static inline int vma_mas_store (struct vm_area_struct *vma,struct ma_state *mas) +{ + mas->index = vma->vm_start; + mas->last = vma->vm_end -1 ; +return mas_store_gfp (mas, vma,GFP_KERNEL ); +} + +static inline int vma_mas_remove (struct vm_area_struct *vma,struct ma_state *mas) +{ + mas->index = vma->vm_start; + mas->last = vma->vm_end -1 ; +return mas_store_gfp (mas,NULL ,GFP_KERNEL ); +} +/* * vma_mt_erase() - erase a VMA entry from the maple tree. * * @mm: The struct_mm
@@ -549,7 +569,7 @@ static inline void vma_mt_erase(struct mm_struct *mm, struct vm_area_struct *vmatrace_vma_mt_erase (mm, vma);mtree_erase (&mm->mm_mt, vma->vm_start); } -/* Private +/* * vma_mt_szero() - Set a given range to zero. Used when modifying a * vm_area_struct start or end. *
@@ -563,7 +583,7 @@ static inline void vma_mt_szero(struct mm_struct *mm, unsigned long start,trace_vma_mt_szero (mm, start, end);mtree_store_range (&mm->mm_mt, start, end -1 ,NULL ,GFP_KERNEL ); } -/* Private +/* * vma_mt_store() - Store a given vm_area_struct in the maple tree. * * @mm: The struct_mm
@@ -576,6 +596,12 @@ static inline void vma_mt_store(struct mm_struct *mm, struct vm_area_struct *vma GFP_KERNEL); } + +void vma_store (struct mm_struct *mm,struct vm_area_struct *vma) +{ +vma_mt_store (mm, vma); +} +static void vma_link (struct mm_struct *mm,struct vm_area_struct *vma,struct vm_area_struct *prev) {
@@ -1809,7 +1835,6 @@ static unsigned long unmapped_area(struct vm_unmapped_area_info *info)if (mas_get_empty_area (&mas, info->low_limit, info->high_limit -1 , length)) { -rcu_read_unlock ();return -ENOMEM ; } gap = mas.index;
@@ -1838,7 +1863,6 @@ static unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info)if (mas_get_empty_area_rev (&mas, info->low_limit, info->high_limit -1 , length)) { -rcu_read_unlock ();return -ENOMEM ; } gap = (mas.index + info->align_mask) & ~info->align_mask;
@@ -2025,8 +2049,7 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,EXPORT_SYMBOL (get_unmapped_area);/** -* find_vma() - Find the VMA for a given address, or the next vma. May return -* NULL in the case of no vma at addr or above +* find_vma() - Find the VMA for a given address, or the next vma. * @mm The mm_struct to check * @addr: The address *
@@ -2036,13 +2059,16 @@ EXPORT_SYMBOL(get_unmapped_area);struct vm_area_struct *find_vma (struct mm_struct *mm,unsigned long addr) {struct vm_area_struct *vma; +MA_STATE (mas, &mm->mm_mt, start_addr, start_addr);/* Check the cache first. */ vma =vmacache_find (mm, addr);if (likely (vma))return vma; - vma =mt_find (&mm->mm_mt, &addr,ULONG_MAX ); +rcu_read_lock (); + vma =mas_find (&mas, end_addr -1 ); +rcu_read_unlock ();if (vma)vmacache_update (addr, vma);
@@ -2780,16 +2806,102 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size, }/* -* this is really a simplified "do_mmap". it only handles -* anonymous maps. eventually we may be able to do some -* brk-specific accounting here. +* bkr_munmap() - Unmap a parital vma. +* @mas: The maple tree state. +* @vma: The vma to be modified +* @newbrk: the start of the address to unmap +* @oldbrk: The end of the address to unmap +* +* Returns: 0 on success. +* unmaps a partial VMA mapping. Does not handle alignment, downgrades lock if +* possible. +*/ +static int do_brk_munmap (struct ma_state *mas,struct vm_area_struct *vma, +unsigned long newbrk,unsigned long oldbrk, +struct list_head *uf) +{ +struct mm_struct *mm = vma->vm_mm; +struct vm_area_struct unmap; +unsigned long unmap_pages; +int ret =1 ; + +arch_unmap (mm, newbrk, oldbrk); + +if (likely (vma->vm_start >= newbrk)) {// remove entire mapping(s) +mas_set (mas, newbrk); +if (vma->vm_start != newbrk) +mas_reset (mas);// cause a re-walk for the first overlap. + ret =do_mas_munmap (mas, mm, newbrk, oldbrk-newbrk, uf, true); + goto munmap_full_vma; + } + +vma_init (&unmap, mm); + unmap.vm_start = newbrk; + unmap.vm_end = oldbrk; + ret =userfaultfd_unmap_prep (&unmap, newbrk, oldbrk, uf); +if (ret) +return ret; + ret =1 ; + +// Change the oldbrk of vma to the newbrk of the munmap area +vma_adjust_trans_huge (vma, vma->vm_start, newbrk,0 ); +if (vma->anon_vma) { +anon_vma_lock_write (vma->anon_vma); +anon_vma_interval_tree_pre_update_vma (vma); + } + + vma->vm_end = newbrk; +if (vma_mas_remove (&unmap, mas)) + goto mas_store_fail; + +vmacache_invalidate (vma->vm_mm); +if (vma->anon_vma) { +anon_vma_interval_tree_post_update_vma (vma); +anon_vma_unlock_write (vma->anon_vma); + } + + unmap_pages =vma_pages (&unmap); +if (unmap.vm_flags &VM_LOCKED ) { + mm->locked_vm -= unmap_pages; +munlock_vma_pages_range (&unmap, newbrk, oldbrk); + } + +mmap_write_downgrade (mm); +unmap_region (mm, &unmap, vma, newbrk, oldbrk); +/* Statistics */ +vm_stat_account (mm, unmap.vm_flags, -unmap_pages); +if (unmap.vm_flags &VM_ACCOUNT ) +vm_unacct_memory (unmap_pages); + +munmap_full_vma: +validate_mm_mt (mm); +return ret; + +mas_store_fail: + vma->vm_end = oldbrk; +anon_vma_interval_tree_post_update_vma (vma); +anon_vma_unlock_write (vma->anon_vma); +return -ENOMEM ; +} + +/* +* do_brk_flags() - Increase the brk vma if the flags match. +* @mas: The maple tree state. +* @addr: The start address +* @len: The length of the increase +* @vma: The vma, +* @flags: The VMA Flags +* +* Extend the brk VMA from addr to addr + len. If the VMA is NULL or the flags +* do not match then create a new anonymous VMA. Eventually we may be able to +* do some brk-specific accounting here. */ -static int do_brk_flags (unsigned long addr,unsigned long len, -unsigned long flags,struct list_head *uf) +static int do_brk_flags (struct ma_state *mas,struct vm_area_struct **brkvma, +unsigned long addr,unsigned long len, +unsigned long flags) {struct mm_struct *mm = current->mm; -struct vm_area_struct *vma, *prev; -pgoff_t pgoff = addr >>PAGE_SHIFT ; +struct vm_area_struct *prev =NULL , *vma;int error;unsigned long mapped_addr;validate_mm_mt (mm);
@@ -2807,11 +2919,7 @@ static int do_brk_flags(unsigned long addr, unsigned long len,if (error)return error; -/* Clear old maps, set up prev and uf */ -if (munmap_vma_range (mm, addr, len, &prev, uf)) -return -ENOMEM ; - -/* Check against address space limits *after* clearing old maps... */ +/* Check against address space limits by the changed size */ if (!may_expand_vm (mm, flags, len >>PAGE_SHIFT ))return -ENOMEM ;
@@ -2821,28 +2929,58 @@ static int do_brk_flags(unsigned long addr, unsigned long len,if (security_vm_enough_memory_mm (mm, len >>PAGE_SHIFT ))return -ENOMEM ; -/* Can we just expand an old private anonymous mapping? */ - vma =vma_merge (mm, prev, addr, addr + len, flags, -NULL ,NULL , pgoff,NULL ,NULL_VM_UFFD_CTX ); -if (vma) - goto out; + mas->last = addr + len -1 ; +if (*brkvma) { + vma = *brkvma; +/* Expand the existing vma if possible; almost never a singular +* list, so this will almost always fail. */ -/* -* create a vma struct for an anonymous mapping -*/ - vma =vm_area_alloc (mm); -if (!vma) { -vm_unacct_memory (len >>PAGE_SHIFT ); -return -ENOMEM ; +if ((!vma->anon_vma || +list_is_singular (&vma->anon_vma_chain)) && + ((vma->vm_flags & ~VM_SOFTDIRTY ) == flags)){ + mas->index = vma->vm_start; + +vma_adjust_trans_huge (vma, addr, addr + len,0 ); +if (vma->anon_vma) { +anon_vma_lock_write (vma->anon_vma); +anon_vma_interval_tree_pre_update_vma (vma); + } + vma->vm_end = addr + len; + vma->vm_flags |=VM_SOFTDIRTY ; +if (mas_store_gfp (mas, vma,GFP_KERNEL )) + goto mas_mod_fail; +if (vma->anon_vma) { +anon_vma_interval_tree_post_update_vma (vma); +anon_vma_unlock_write (vma->anon_vma); + } +khugepaged_enter_vma_merge (vma, flags); + goto out; + } + prev = vma; } + mas->index = addr; +mas_walk (mas); + +/* create a vma struct for an anonymous mapping */ + vma =vm_area_alloc (mm); +if (!vma) + goto vma_alloc_fail;vma_set_anonymous (vma); vma->vm_start = addr; vma->vm_end = addr + len; - vma->vm_pgoff = pgoff; + vma->vm_pgoff = addr >>PAGE_SHIFT ; vma->vm_flags = flags; vma->vm_page_prot =vm_get_page_prot (flags); -vma_link (mm, vma, prev); +if (vma_mas_store (vma, mas)) + goto mas_store_fail; + +if (!prev) + prev =mas_prev (mas,0 ); + +__vma_link_list (mm, vma, prev); + mm->map_count++; + *brkvma = vma; out:perf_event_mmap (vma); mm->total_vm += len >>PAGE_SHIFT ;
@@ -2852,15 +2990,29 @@ static int do_brk_flags(unsigned long addr, unsigned long len, vma->vm_flags |=VM_SOFTDIRTY ;validate_mm_mt (mm);return 0 ; + +mas_store_fail: +vm_area_free (vma); +vma_alloc_fail: +vm_unacct_memory (len >>PAGE_SHIFT ); +return -ENOMEM ; + +mas_mod_fail: + vma->vm_end = addr; +anon_vma_interval_tree_post_update_vma (vma); +anon_vma_unlock_write (vma->anon_vma); +return -ENOMEM ; + }int vm_brk_flags (unsigned long addr,unsigned long request,unsigned long flags) {struct mm_struct *mm = current->mm; +struct vm_area_struct *vma =NULL ;unsigned long len;int ret;bool populate; -LIST_HEAD (uf); +MA_STATE (mas, &mm->mm_mt, addr, addr); len =PAGE_ALIGN (request);if (len < request)
@@ -2871,10 +3023,11 @@ int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags)if (mmap_write_lock_killable (mm))return -EINTR ; - ret =do_brk_flags (addr, len, flags, &uf); +// This vma left intentionally blank. +mas_walk (&mas); + ret =do_brk_flags (&mas, &vma, addr, len, flags); populate = ((mm->def_flags &VM_LOCKED ) !=0 );mmap_write_unlock (mm); -userfaultfd_unmap_complete (mm, &uf);if (populate && !ret)mm_populate (addr, len);return ret;
Signature · 1 line
2.28.0
PATCH 15/28 mm/mmap: Change vm_brk_flags() to use mm_populate_vma()
In reply to Liam R. HowlettSkip searching for the vma to populate after creating it by passing in a pointer.
--- mm/mmap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mmap.c +2 -2
@@ -3026,10 +3026,10 @@ int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags)// This vma left intentionally blank. mas_walk (&mas); ret =do_brk_flags (&mas, &vma, addr, len, flags); - populate = ((mm->def_flags &VM_LOCKED ) !=0 );mmap_write_unlock (mm); + populate = ((mm->def_flags &VM_LOCKED ) !=0 );if (populate && !ret) -mm_populate (addr, len); +mm_populate_vma (vma, addr, addr + len);return ret; }EXPORT_SYMBOL (vm_brk_flags);
Signature · 1 line
2.28.0
PATCH 16/28 mm: Move find_vma_intersection to mmap.c and change implementation to maple tree.
In reply to Liam R. HowlettWhen searching for a vma within a range, it is easier to use the maple tree interface. This means the find_vma() call changes to a special case of the find_vma_intersection(). Exported for kvm module.
--- include/linux/mm.h | 10 ++-------- mm/mmap.c | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 21 deletions(-)
include/linux/mm.h +2 -8
@@ -2651,14 +2651,8 @@ extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned lon/* Look up the first VMA which intersects the interval start_addr..end_addr-1, NULL if none. Assume start_addr < end_addr. */ -static inline struct vm_area_struct *find_vma_intersection (struct mm_struct * mm,unsigned long start_addr,unsigned long end_addr) -{ -struct vm_area_struct * vma =find_vma (mm,start_addr); - -if (vma && end_addr <= vma->vm_start) - vma =NULL ; -return vma; -} +extern struct vm_area_struct *find_vma_intersection (struct mm_struct *mm, +unsigned long start_addr,unsigned long end_addr);static inline unsigned long vm_start_gap (struct vm_area_struct *vma) {
mm/mmap.c +28 -13
60 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH 17/28 mm/mmap: Change mmap_region to use maple tree state
In reply to Liam R. Howlett--- lib/maple_tree.c | 1 + mm/mmap.c | 221 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 190 insertions(+), 32 deletions(-)
lib/maple_tree.c +1 -0
@@ -3525,6 +3525,7 @@ static inline void *_mas_store(struct ma_state *mas, void *entry, bool overwriteif (ret >2 )return NULL ; spanning_store: +return content; }
mm/mmap.c +189 -32
338 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH 18/28 mm/mmap: Drop munmap_vma_range()
In reply to Liam R. Howlett--- mm/mmap.c | 23 ----------------------- 1 file changed, 23 deletions(-)
mm/mmap.c +0 -23
@@ -475,29 +475,6 @@ static inline struct vm_area_struct *vma_next(struct mm_struct *mm,return vma->vm_next; } -/* -* munmap_vma_range() - munmap VMAs that overlap a range. -* @mm: The mm struct -* @start: The start of the range. -* @len: The length of the range. -* @pprev: pointer to the pointer that will be set to previous vm_area_struct -* -* Find all the vm_area_struct that overlap from @start to -* @end and munmap them. Set @pprev to the previous vm_area_struct. -* -* Returns: -ENOMEM on munmap failure or 0 on success. -*/ -static inline int -munmap_vma_range (struct mm_struct *mm,unsigned long start,unsigned long len, -struct vm_area_struct **pprev,struct list_head *uf) -{ -// Needs optimization. -while (range_has_overlap (mm, start, start + len, pprev)) { -if (do_munmap (mm, start, len, uf)) -return -ENOMEM ; - } -return 0 ; -}static unsigned long count_vma_pages_range (struct mm_struct *mm,unsigned long addr,unsigned long end) {
Signature · 1 line
2.28.0
PATCH 19/28 mm: Remove vmacache
In reply to Liam R. HowlettThe maple tree is able to find a VMA quick enough to no longer need the vma cache. Remove the vmacache to reduce work in keeping it up to date and code complexity.
--- fs/exec.c | 3 - fs/proc/task_mmu.c | 1 - include/linux/mm_types.h | 1 - include/linux/mm_types_task.h | 5 -- include/linux/sched.h | 1 - include/linux/vmacache.h | 28 -------- kernel/debug/debug_core.c | 12 ---- kernel/fork.c | 5 -- mm/Makefile | 2 +- mm/debug.c | 4 +- mm/mmap.c | 20 ------ mm/vmacache.c | 117 ---------------------------------- 12 files changed, 3 insertions(+), 196 deletions(-) delete mode 100644 include/linux/vmacache.h delete mode 100644 mm/vmacache.c
fs/exec.c +0 -3
@@ -28,7 +28,6 @@#include <linux/file.h> #include <linux/fdtable.h> #include <linux/mm.h> -#include <linux/vmacache.h> #include <linux/stat.h> #include <linux/fcntl.h> #include <linux/swap.h>
@@ -1019,8 +1018,6 @@ static int exec_mmap(struct mm_struct *mm)activate_mm (active_mm, mm);if (IS_ENABLED (CONFIG_ARCH_WANT_IRQS_OFF_ACTIVATE_MM ))local_irq_enable (); - tsk->mm->vmacache_seqnum =0 ; -vmacache_flush (tsk);task_unlock (tsk);if (old_mm) {mmap_read_unlock (old_mm);
fs/proc/task_mmu.c +0 -1
@@ -1,6 +1,5 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/pagewalk.h> -#include <linux/vmacache.h> #include <linux/hugetlb.h> #include <linux/huge_mm.h> #include <linux/mount.h>
include/linux/mm_types.h +0 -1
@@ -380,7 +380,6 @@ struct mm_struct {struct {struct vm_area_struct *mmap;/* list of VMAs */ struct maple_tree mm_mt; -u64 vmacache_seqnum;/* per-thread vmacache */ #ifdef CONFIG_MMU unsigned long (*get_unmapped_area) (struct file *filp, unsigned long addr, unsigned long len,
include/linux/mm_types_task.h +0 -5
@@ -31,11 +31,6 @@#define VMACACHE_SIZE (1U << VMACACHE_BITS)#define VMACACHE_MASK (VMACACHE_SIZE - 1) -struct vmacache { -u64 seqnum; -struct vm_area_struct *vmas[VMACACHE_SIZE ]; -}; -/* * When updating this, please also update struct resident_page_types[] in * kernel/fork.c
include/linux/sched.h +0 -1
@@ -750,7 +750,6 @@ struct task_struct {struct mm_struct *active_mm;/* Per-thread vma caching: */ -struct vmacache vmacache;#ifdef SPLIT_RSS_COUNTING struct task_rss_stat rss_stat;
include/linux/vmacache.h deleted +0 -28
@@ -1,28 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __LINUX_VMACACHE_H -#define __LINUX_VMACACHE_H - -#include <linux/sched.h> -#include <linux/mm.h> - -static inline void vmacache_flush (struct task_struct *tsk) -{ -memset (tsk->vmacache.vmas,0 ,sizeof (tsk->vmacache.vmas)); -} - -extern void vmacache_update (unsigned long addr,struct vm_area_struct *newvma); -extern struct vm_area_struct *vmacache_find (struct mm_struct *mm, -unsigned long addr); - -#ifndef CONFIG_MMU -extern struct vm_area_struct *vmacache_find_exact (struct mm_struct *mm, -unsigned long start, -unsigned long end); -#endif - -static inline void vmacache_invalidate (struct mm_struct *mm) -{ - mm->vmacache_seqnum++; -} - -#endif /* __LINUX_VMACACHE_H */
kernel/debug/debug_core.c +0 -12
@@ -53,7 +53,6 @@#include <linux/pid.h> #include <linux/smp.h> #include <linux/mm.h> -#include <linux/vmacache.h> #include <linux/rcupdate.h> #include <linux/irq.h>
@@ -286,17 +285,6 @@ static void kgdb_flush_swbreak_addr(unsigned long addr)if (!CACHE_FLUSH_IS_SAFE )return ; -if (current->mm) { -int i; - -for (i =0 ; i <VMACACHE_SIZE ; i++) { -if (!current->vmacache.vmas[i]) -continue ; -flush_cache_range (current->vmacache.vmas[i], - addr, addr +BREAK_INSTR_SIZE ); - } - } -/* Force flush instruction cache if it was outside the mm */ flush_icache_range (addr, addr +BREAK_INSTR_SIZE ); }
kernel/fork.c +0 -5
@@ -42,7 +42,6 @@#include <linux/mmu_notifier.h> #include <linux/fs.h> #include <linux/mm.h> -#include <linux/vmacache.h> #include <linux/nsproxy.h> #include <linux/capability.h> #include <linux/cpu.h>
@@ -1011,7 +1010,6 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, { mm->mmap =NULL ;mt_init_flags (&mm->mm_mt,MAPLE_ALLOC_RANGE ); - mm->vmacache_seqnum =0 ;atomic_set (&mm->mm_users,1 );atomic_set (&mm->mm_count,1 );mmap_init_lock (mm);
@@ -1408,9 +1406,6 @@ static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)if (!oldmm)return 0 ; -/* initialize the new vmacache entries */ -vmacache_flush (tsk); -if (clone_flags &CLONE_VM ) {mmget (oldmm); mm = oldmm;
mm/Makefile +1 -1
@@ -50,7 +50,7 @@ obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ readahead.o swap.o truncate.o vmscan.o shmem.o \ util.o mmzone.o vmstat.o backing-dev.o \ mm_init.o percpu.o slab_common.o \ - compaction.o vmacache.o \ + compaction.o \ interval_tree.o list_lru.o workingset.o \ debug.o gup.o $(mmu-y)
mm/debug.c +2 -2
@@ -214,7 +214,7 @@ EXPORT_SYMBOL(dump_vma);void dump_mm (const struct mm_struct *mm) { - pr_emerg("mm %px mmap %px seqnum %llu task_size %lu\n" + pr_emerg("mm %px mmap %px task_size %lu\n" #ifdef CONFIG_MMU "get_unmapped_area %px\n" #endif
@@ -242,7 +242,7 @@ void dump_mm(const struct mm_struct *mm)"tlb_flush_pending %d\n" "def_flags: %#lx(%pGv)\n" , - mm, mm->mmap, (long long ) mm->vmacache_seqnum, mm->task_size, + mm, mm->mmap, mm->task_size, #ifdef CONFIG_MMU mm->get_unmapped_area, #endif
mm/mmap.c +0 -20
@@ -13,7 +13,6 @@#include <linux/slab.h> #include <linux/backing-dev.h> #include <linux/mm.h> -#include <linux/vmacache.h> #include <linux/shm.h> #include <linux/mman.h> #include <linux/pagemap.h>
@@ -693,9 +692,6 @@ inline int vma_expand(struct ma_state *mas, struct vm_area_struct *vma,/* Remove from mm linked list - also updates highest_vm_end */ __vma_unlink_list (mm, next); -/* Kill the cache */ -vmacache_invalidate (mm); -if (file)__remove_shared_vm_struct (next, file, mapping);
@@ -909,8 +905,6 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,if (remove_next) {__vma_unlink_list (mm, next); -/* Kill the cache */ -vmacache_invalidate (mm);if (file)__remove_shared_vm_struct (next, file, mapping); }else if (insert) {
@@ -2197,19 +2191,9 @@ struct vm_area_struct *find_vma_intersection(struct mm_struct *mm, {struct vm_area_struct *vma;MA_STATE (mas, &mm->mm_mt, start_addr, start_addr); - -/* Check the cache first. */ - vma =vmacache_find (mm, start_addr); -if (likely (vma)) -return vma; -rcu_read_lock (); vma =mas_find (&mas, end_addr -1 );rcu_read_unlock (); - -if (vma) -vmacache_update (start_addr, vma); -return vma; }EXPORT_SYMBOL (find_vma_intersection);
@@ -2604,9 +2588,6 @@ detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma, mm->highest_vm_end = prev ?vm_end_gap (prev) :0 ; tail_vma->vm_next =NULL ; -/* Kill the cache */ -vmacache_invalidate (mm); -/* * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or * VM_GROWSUP VMA. Such VMAs can change their size under
@@ -3003,7 +2984,6 @@ static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma,if (vma_mas_remove (&unmap, mas)) goto mas_store_fail; -vmacache_invalidate (vma->vm_mm);if (vma->anon_vma) {anon_vma_interval_tree_post_update_vma (vma);anon_vma_unlock_write (vma->anon_vma);
mm/vmacache.c deleted +0 -117
117 lines not on this page · raw message
Signature · 1 line
2.28.0
PATCH 20/28 mm/mmap: Change __do_munmap() to avoid unnecessary lookups.
In reply to Liam R. HowlettAs there is no longer a vmacache, find_vma() is more expensive and so avoid doing them
--- mm/mmap.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
mm/mmap.c +12 -4
@@ -2720,7 +2720,6 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,if (!vma)return 0 ; - prev = vma->vm_prev;/* we have start < vma->vm_end */ /*
@@ -2744,16 +2743,24 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,if (error)return error; prev = vma; + vma =vma_next (mm, prev); + }else { + prev = vma->vm_prev; } +if (vma->vm_end >= end) + last = vma; +else + last =find_vma_intersection (mm, end -1 , end); +/* Does it split the last one? */ - last =find_vma (mm, end); -if (last && end > last->vm_start) { +if (last && end < last->vm_end) {int error =__split_vma (mm, last, end,1 );if (error)return error; + vma =vma_next (mm, prev); } - vma =vma_next (mm, prev); +if (unlikely (uf)) {/*
@@ -2766,6 +2773,7 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,* failure that it's not worth optimizing it for. */ int error =userfaultfd_unmap_prep (vma, start, end, uf); +if (error)return error; }
Signature · 1 line
2.28.0
PATCH 21/28 mm/mmap: Change __do_munmap() to use a ma_state
In reply to Liam R. Howlett--- mm/mmap.c | 99 ++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 52 deletions(-)
mm/mmap.c +47 -52
@@ -2562,44 +2562,6 @@ static void unmap_region(struct mm_struct *mm,tlb_finish_mmu (&tlb, start, end); } -/* -* Create a list of vma's touched by the unmap, removing them from the mm's -* vma list as we go.. -*/ -static bool -detach_vmas_to_be_unmapped (struct mm_struct *mm,struct vm_area_struct *vma, -struct vm_area_struct *prev,unsigned long end) -{ -struct vm_area_struct **insertion_point; -struct vm_area_struct *tail_vma =NULL ; - - insertion_point = (prev ? &prev->vm_next : &mm->mmap); - vma->vm_prev =NULL ; -vma_mt_szero (mm, vma->vm_start, end); -do { - mm->map_count--; - tail_vma = vma; - vma = vma->vm_next; - }while (vma && vma->vm_start < end); - *insertion_point = vma; -if (vma) - vma->vm_prev = prev; -else - mm->highest_vm_end = prev ?vm_end_gap (prev) :0 ; - tail_vma->vm_next =NULL ; - -/* -* Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or -* VM_GROWSUP VMA. Such VMAs can change their size under -* down_read(mmap_lock) and collide with the VMA we are about to unmap. -*/ -if (vma && (vma->vm_flags &VM_GROWSDOWN )) -return false; -if (prev && (prev->vm_flags &VM_GROWSUP )) -return false; -return true; -} -/* * __split_vma() bypasses sysctl_max_map_count checking. We use this where it * has already been checked or doesn't make sense to fail.
@@ -2679,12 +2641,16 @@ int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,return __split_vma (mm, vma, addr, new_below); } -static inline void unlock_range (struct vm_area_struct *start,unsigned long limit) +static inline int unlock_range (struct vm_area_struct *start, +struct vm_area_struct **tail,unsigned long limit) {struct mm_struct *mm = start->vm_mm;struct vm_area_struct *tmp = start; +int count =0 ;while (tmp && tmp->vm_start < limit) { + *tail = tmp; + count++;if (tmp->vm_flags &VM_LOCKED ) { mm->locked_vm -=vma_pages (tmp);munlock_vma_pages_all (tmp);
@@ -2692,6 +2658,8 @@ static inline void unlock_range(struct vm_area_struct *start, unsigned long limi tmp = tmp->vm_next; } + +return count; }/* Munmap is split into 2 main parts -- this part which finds * what needs doing, and the areas themselves, which do the
@@ -2703,23 +2671,24 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len, {unsigned long end;struct vm_area_struct *vma, *prev, *last; +MA_STATE (mas, &mm->mm_mt, start, start);if ((offset_in_page (start)) || start >TASK_SIZE || len >TASK_SIZE -start)return -EINVAL ; - len =PAGE_ALIGN (len); - end = start + len; -if (len ==0 ) + end = start +PAGE_ALIGN (len); +if (end == start)return -EINVAL ;/* arch_unmap() might do unmaps itself. */ arch_unmap (mm, start, end);/* Find the first overlapping VMA */ - vma =find_vma_intersection (mm, start, end); + vma =mas_find (&mas, end -1 );if (!vma)return 0 ; + mas.last = end -1 ;/* we have start < vma->vm_end */ /*
@@ -2744,6 +2713,8 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,return error; prev = vma; vma =vma_next (mm, prev); + mas.index = start; +mas_reset (&mas); }else { prev = vma->vm_prev; }
@@ -2759,6 +2730,7 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,if (error)return error; vma =vma_next (mm, prev); +mas_reset (&mas); }
@@ -2779,17 +2751,40 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len, }/* -* unlock any mlock()ed ranges before detaching vmas +* unlock any mlock()ed ranges before detaching vmas, count the number +* of VMAs to be dropped, and return the tail entry of the affected +* area. */ -if (mm->locked_vm) -unlock_range (vma, end); + mm->map_count -=unlock_range (vma, &last, end); +/* Drop removed area from the tree */ +mas_store_gfp (&mas,NULL ,GFP_KERNEL ); -/* Detach vmas from the MM linked list and remove from the mm tree*/ -if (!detach_vmas_to_be_unmapped (mm, vma, prev, end)) - downgrade = false; +/* Detach vmas from the MM linked list */ + vma->vm_prev =NULL ; +if (prev) + prev->vm_next = last->vm_next; +else + mm->mmap = last->vm_next; -if (downgrade) -mmap_write_downgrade (mm); +if (last->vm_next) { + last->vm_next->vm_prev = prev; + last->vm_next =NULL ; + }else + mm->highest_vm_end = prev ?vm_end_gap (prev) :0 ; + +/* +* Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or +* VM_GROWSUP VMA. Such VMAs can change their size under +* down_read(mmap_lock) and collide with the VMA we are about to unmap. +*/ +if (downgrade) { +if (last && (last->vm_flags &VM_GROWSDOWN )) + downgrade = false; +else if (prev && (prev->vm_flags &VM_GROWSUP )) + downgrade = false; +else +mmap_write_downgrade (mm); + }unmap_region (mm, vma, prev, start, end);
@@ -3212,7 +3207,7 @@ void exit_mmap(struct mm_struct *mm) }if (mm->locked_vm) -unlock_range (mm->mmap,ULONG_MAX ); +unlock_range (mm->mmap, &vma,ULONG_MAX );arch_exit_mmap (mm);
Signature · 1 line
2.28.0
PATCH 22/28 mm/mmap: Move mmap_region() below do_munmap()
In reply to Liam R. HowlettRelocation of code for the next commit. There should be no changes here.
--- mm/mmap.c | 479 +++++++++++++++++++++++++++--------------------------- 1 file changed, 240 insertions(+), 239 deletions(-)
mm/mmap.c +240 -239
@@ -1703,245 +1703,6 @@ static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags)return (vm_flags & (VM_NORESERVE |VM_SHARED |VM_WRITE )) ==VM_WRITE ; } -unsigned long mmap_region (struct file *file,unsigned long addr, -unsigned long len,vm_flags_t vm_flags,unsigned long pgoff, -struct list_head *uf) -{ -struct mm_struct *mm = current->mm; -struct vm_area_struct *vma =NULL ; -struct vm_area_struct *prev, *next; -pgoff_t pglen = len >>PAGE_SHIFT ; -unsigned long charged =0 ; -unsigned long end = addr + len; -unsigned long merge_start = addr, merge_end = end; -pgoff_t vm_pgoff; -int error; -MA_STATE (mas, &mm->mm_mt, addr, end -1 ); - -/* Check against address space limit. */ -if (!may_expand_vm (mm, vm_flags, len >>PAGE_SHIFT )) { -unsigned long nr_pages; - -/* -* MAP_FIXED may remove pages of mappings that intersects with -* requested mapping. Account for the pages it would unmap. -*/ - nr_pages =count_vma_pages_range (mm, addr, end); - -if (!may_expand_vm (mm, vm_flags, - (len >>PAGE_SHIFT ) - nr_pages)) -return -ENOMEM ; - } - -/* Unmap any existing mapping in the area */ -if (do_munmap (mm, addr, len, uf)) -return -ENOMEM ; - -/* -* Private writable mapping: check memory availability -*/ -if (accountable_mapping (file, vm_flags)) { - charged = len >>PAGE_SHIFT ; -if (security_vm_enough_memory_mm (mm, charged)) -return -ENOMEM ; - vm_flags |=VM_ACCOUNT ; - } - - -if (vm_flags &VM_SPECIAL ) { - prev =mas_prev (&mas,0 ); - goto cannot_expand; - } - -/* Attempt to expand an old mapping */ - -/* Check next */ - next =mas_next (&mas,ULONG_MAX ); -if (next && next->vm_start == end &&vma_policy (next) && -can_vma_merge_before (next, vm_flags,NULL , file, pgoff+pglen, -NULL_VM_UFFD_CTX )) { - merge_end = next->vm_end; - vma = next; - vm_pgoff = next->vm_pgoff - pglen; - } - -/* Check prev */ - prev =mas_prev (&mas,0 ); -if (prev && prev->vm_end == addr && !vma_policy (prev) && -can_vma_merge_after (prev, vm_flags,NULL , file, pgoff, -NULL_VM_UFFD_CTX )) { - merge_start = prev->vm_start; - vma = prev; - vm_pgoff = prev->vm_pgoff; - } - - -/* Actually expand, if possible */ -if (vma && - !vma_expand (&mas, vma, merge_start, merge_end, vm_pgoff, next)) { -khugepaged_enter_vma_merge (prev, vm_flags); - goto expanded; - } - -mas_reset (&mas); -mas_set (&mas, addr); - mas.last = end -1 ; -cannot_expand: -/* -* Determine the object being mapped and call the appropriate -* specific mapper. the address has already been validated, but -* not unmapped, but the maps are removed from the list. -*/ - vma =vm_area_alloc (mm); -if (!vma) { - error = -ENOMEM ; - goto unacct_error; - } - - vma->vm_start = addr; - vma->vm_end = end; - vma->vm_flags = vm_flags; - vma->vm_page_prot =vm_get_page_prot (vm_flags); - vma->vm_pgoff = pgoff; - -if (file) { -if (vm_flags &VM_DENYWRITE ) { - error =deny_write_access (file); -if (error) - goto free_vma; - } -if (vm_flags &VM_SHARED ) { - error =mapping_map_writable (file->f_mapping); -if (error) - goto allow_write_and_free_vma; - } - -/* ->mmap() can change vma->vm_file, but must guarantee that -* vma_link() below can deny write-access if VM_DENYWRITE is set -* and map writably if VM_SHARED is set. This usually means the -* new file must not have been exposed to user-space, yet. -*/ - vma->vm_file =get_file (file); - error =call_mmap (file, vma); -if (error) - goto unmap_and_free_vma; - -/* If vm_flags changed after call_mmap(), we should try merge vma again -* as we may succeed this time. -*/ -if (unlikely (vm_flags != vma->vm_flags && prev && - prev->vm_end == addr && !vma_policy (prev) && -can_vma_merge_after (prev, vm_flags,NULL , file, - pgoff,NULL_VM_UFFD_CTX ))) { - merge_start = prev->vm_start; - vm_pgoff = prev->vm_pgoff; -if (!vma_expand (&mas, prev, merge_start, merge_end, - vm_pgoff, next)) { -/* ->mmap() can change vma->vm_file and fput the original file. So -* fput the vma->vm_file here or we would add an extra fput for file -* and cause general protection fault ultimately. -*/ -fput (vma->vm_file); -vm_area_free (vma); - vma = prev; -/* Update vm_flags and possible addr to pick up the change. We don't -* warn here if addr changed as the vma is not linked by vma_link(). -*/ - addr = vma->vm_start; - vm_flags = vma->vm_flags; - goto unmap_writable; - } - } - -/* Can addr have changed?? -* -* Answer: Yes, several device drivers can do it in their -* f_op->mmap method. -DaveM -* Bug: If addr is changed, prev and the maple tree data should -* be updated for vma_link() -*/ -WARN_ON_ONCE (addr != vma->vm_start); - - addr = vma->vm_start; - vm_flags = vma->vm_flags; - }else if (vm_flags &VM_SHARED ) { - error =shmem_zero_setup (vma); -if (error) - goto free_vma; - }else { -vma_set_anonymous (vma); - } - -/* Allow architectures to sanity-check the vm_flags */ -if (!arch_validate_flags (vma->vm_flags)) { - error = -EINVAL ; -if (file) - goto unmap_and_free_vma; -else - goto free_vma; - } - - mas.index = mas.last = addr; -mas_walk (&mas); -vma_mas_link (mm, vma, &mas, prev); -/* Once vma denies write, undo our temporary denial count */ -if (file) { -unmap_writable: -if (vm_flags &VM_SHARED ) -mapping_unmap_writable (file->f_mapping); -if (vm_flags &VM_DENYWRITE ) -allow_write_access (file); - } - file = vma->vm_file; -expanded: -perf_event_mmap (vma); - -vm_stat_account (mm, vm_flags, len >>PAGE_SHIFT ); -if (vm_flags &VM_LOCKED ) { -if ((vm_flags &VM_SPECIAL ) ||vma_is_dax (vma) || -is_vm_hugetlb_page (vma) || - vma ==get_gate_vma (current->mm)) - vma->vm_flags &=VM_LOCKED_CLEAR_MASK ; -else - mm->locked_vm += (len >>PAGE_SHIFT ); - } - -if (file) -uprobe_mmap (vma); - -/* -* New (or expanded) vma always get soft dirty status. -* Otherwise user-space soft-dirty page tracker won't -* be able to distinguish situation when vma area unmapped, -* then new mapped in-place (which must be aimed as -* a completely new data area). -*/ - vma->vm_flags |=VM_SOFTDIRTY ; - -vma_set_page_prot (vma); - -return addr; - -unmap_and_free_vma: - vma->vm_file =NULL ; -fput (file); - -/* Undo any partial mapping done by a device driver. */ -unmap_region (mm, vma, prev, vma->vm_start, vma->vm_end); - charged =0 ; -if (vm_flags &VM_SHARED ) -mapping_unmap_writable (file->f_mapping); -allow_write_and_free_vma: -if (vm_flags &VM_DENYWRITE ) -allow_write_access (file); -free_vma: -vm_area_free (vma); -unacct_error: -if (charged) -vm_unacct_memory (charged); -return error; -} -/* unmapped_area() Find an area between the low_limit and the high_limit with * the correct alignment and offset, all from @info. Note: current->mm is used * for the search.
@@ -2800,6 +2561,246 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len,return __do_munmap (mm, start, len, uf, false); } +unsigned long mmap_region (struct file *file,unsigned long addr, +unsigned long len,vm_flags_t vm_flags,unsigned long pgoff, +struct list_head *uf) +{ +struct mm_struct *mm = current->mm; +struct vm_area_struct *vma =NULL ; +struct vm_area_struct *prev, *next; +pgoff_t pglen = len >>PAGE_SHIFT ; +unsigned long charged =0 ; +unsigned long end = addr + len; +unsigned long merge_start = addr, merge_end = end; +pgoff_t vm_pgoff; +int error; +MA_STATE (mas, &mm->mm_mt, addr, end -1 ); + +/* Check against address space limit. */ +if (!may_expand_vm (mm, vm_flags, len >>PAGE_SHIFT )) { +unsigned long nr_pages; + +/* +* MAP_FIXED may remove pages of mappings that intersects with +* requested mapping. Account for the pages it would unmap. +*/ + nr_pages =count_vma_pages_range (mm, addr, end); + +if (!may_expand_vm (mm, vm_flags, + (len >>PAGE_SHIFT ) - nr_pages)) +return -ENOMEM ; + } + +/* Unmap any existing mapping in the area */ +if (do_mas_munmap (&mas, mm, addr, len, uf, false)) +return -ENOMEM ; + +/* +* Private writable mapping: check memory availability +*/ +if (accountable_mapping (file, vm_flags)) { + charged = len >>PAGE_SHIFT ; +if (security_vm_enough_memory_mm (mm, charged)) +return -ENOMEM ; + vm_flags |=VM_ACCOUNT ; + } + + +if (vm_flags &VM_SPECIAL ) { + prev =mas_prev (&mas,0 ); + goto cannot_expand; + } + +/* Attempt to expand an old mapping */ + +/* Check next */ + next =mas_next (&mas,ULONG_MAX ); +if (next && next->vm_start == end &&vma_policy (next) && +can_vma_merge_before (next, vm_flags,NULL , file, pgoff + pglen, +NULL_VM_UFFD_CTX )) { + merge_end = next->vm_end; + vma = next; + vm_pgoff = next->vm_pgoff - pglen; + } + +/* Check prev */ + prev =mas_prev (&mas,0 ); +if (prev && prev->vm_end == addr && !vma_policy (prev) && +can_vma_merge_after (prev, vm_flags,NULL , file, pgoff, +NULL_VM_UFFD_CTX )) { + merge_start = prev->vm_start; + vma = prev; + vm_pgoff = prev->vm_pgoff; + } + +/* Actually expand, if possible */ +if (vma && + !vma_expand (&mas, vma, merge_start, merge_end, vm_pgoff, next)) { +khugepaged_enter_vma_merge (prev, vm_flags); + goto expanded; + } + +cannot_expand: +/* +* Determine the object being mapped and call the appropriate +* specific mapper. the address has already been validated, but +* not unmapped, but the maps are removed from the list. +*/ + vma =vm_area_alloc (mm); +if (!vma) { + error = -ENOMEM ; + goto unacct_error; + } + + vma->vm_start = addr; + vma->vm_end = end; + vma->vm_flags = vm_flags; + vma->vm_page_prot =vm_get_page_prot (vm_flags); + vma->vm_pgoff = pgoff; + +if (file) { +if (vm_flags &VM_DENYWRITE ) { + error =deny_write_access (file); +if (error) + goto free_vma; + } +if (vm_flags &VM_SHARED ) { + error =mapping_map_writable (file->f_mapping); +if (error) + goto allow_write_and_free_vma; + } + +/* ->mmap() can change vma->vm_file, but must guarantee that +* vma_link() below can deny write-access if VM_DENYWRITE is set +* and map writably if VM_SHARED is set. This usually means the +* new file must not have been exposed to user-space, yet. +*/ + vma->vm_file =get_file (file); + error =call_mmap (file, vma); +if (error) + goto unmap_and_free_vma; + +/* If vm_flags changed after call_mmap(), we should try merge vma again +* as we may succeed this time. +*/ +if (unlikely (vm_flags != vma->vm_flags && prev && + prev->vm_end == addr && !vma_policy (prev) && +can_vma_merge_after (prev, vm_flags,NULL , file, + pgoff,NULL_VM_UFFD_CTX ))) { + merge_start = prev->vm_start; + vm_pgoff = prev->vm_pgoff; +if (!vma_expand (&mas, prev, merge_start, merge_end, + vm_pgoff, next)) { +/* ->mmap() can change vma->vm_file and fput the original file. So +* fput the vma->vm_file here or we would add an extra fput for file +* and cause general protection fault ultimately. +*/ +fput (vma->vm_file); +vm_area_free (vma); + vma = prev; +/* Update vm_flags and possible addr to pick up the change. We don't +* warn here if addr changed as the vma is not linked by vma_link(). +*/ + addr = vma->vm_start; + vm_flags = vma->vm_flags; + goto unmap_writable; + } + } + +/* Can addr have changed?? +* +* Answer: Yes, several device drivers can do it in their +* f_op->mmap method. -DaveM +* Bug: If addr is changed, prev and the maple tree data should +* be updated for vma_link() +*/ +WARN_ON_ONCE (addr != vma->vm_start); + + addr = vma->vm_start; + vm_flags = vma->vm_flags; + }else if (vm_flags &VM_SHARED ) { + error =shmem_zero_setup (vma); +if (error) + goto free_vma; + }else { +vma_set_anonymous (vma); + } + +/* Allow architectures to sanity-check the vm_flags */ +if (!arch_validate_flags (vma->vm_flags)) { + error = -EINVAL ; +if (file) + goto unmap_and_free_vma; +else + goto free_vma; + } + +/* +* mas was called for the prev vma, and that may not be the correct +* location for the vma being inserted, but is is before that location +* and so the call to vma_mas_link()->vma_mas_store()->mas_store_gfp() +* will detect the write as a spanning store and reset mas if necessary. +*/ + mas.index = mas.last = addr; +mas_walk (&mas); +vma_mas_link (mm, vma, &mas, prev); +/* Once vma denies write, undo our temporary denial count */ +if (file) { +unmap_writable: +if (vm_flags &VM_SHARED ) +mapping_unmap_writable (file->f_mapping); +if (vm_flags &VM_DENYWRITE ) +allow_write_access (file); + } + file = vma->vm_file; +expanded: +perf_event_mmap (vma); + +vm_stat_account (mm, vm_flags, len >>PAGE_SHIFT ); +if (vm_flags &VM_LOCKED ) { +if ((vm_flags &VM_SPECIAL ) ||vma_is_dax (vma) || +is_vm_hugetlb_page (vma) || + vma ==get_gate_vma (current->mm)) + vma->vm_flags &=VM_LOCKED_CLEAR_MASK ; +else + mm->locked_vm += (len >>PAGE_SHIFT ); + } + +if (file) +uprobe_mmap (vma); + +/* +* New (or expanded) vma always get soft dirty status. +* Otherwise user-space soft-dirty page tracker won't +* be able to distinguish situation when vma area unmapped, +* then new mapped in-place (which must be aimed as +* a completely new data area). +*/ + vma->vm_flags |=VM_SOFTDIRTY ; + +vma_set_page_prot (vma); + +return addr; + +unmap_and_free_vma: + vma->vm_file =NULL ; +fput (file); + +/* Undo any partial mapping done by a device driver. */ +unmap_region (mm, vma, prev, vma->vm_start, vma->vm_end); + charged =0 ; +if (vm_flags &VM_SHARED ) +mapping_unmap_writable (file->f_mapping); +allow_write_and_free_vma: +if (vm_flags &VM_DENYWRITE ) +allow_write_access (file); +free_vma: +vm_area_free (vma); +unacct_error: +if (charged) +vm_unacct_memory (charged); +return error; +}static int __vm_munmap (unsigned long start,size_t len,bool downgrade) {int ret;
Signature · 1 line
2.28.0
PATCH 23/28 mm/mmap: Add do_mas_munmap() and wraper for __do_munmap()
In reply to Liam R. HowlettTo avoid extra tree work, it is necessary to support passing in a maple state to key functions. Start this work with __do_munmap().
--- mm/mmap.c | 102 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 30 deletions(-)
mm/mmap.c +72 -30
@@ -2422,34 +2422,24 @@ static inline int unlock_range(struct vm_area_struct *start,return count; } -/* Munmap is split into 2 main parts -- this part which finds -* what needs doing, and the areas themselves, which do the -* work. This now handles partial unmappings. -* Jeremy Fitzhardinge <[email protected]> + +/* do_mas_align_munmap() - munmap the aligned region from @start to @end. +* +* @mas: The maple_state, ideally set up to alter the correct tree location. +* @vma: The starting vm_area_struct +* @mm: The mm_struct +* @start: The aligned start address to munmap. +* @end: The aligned end address to munmap. +* @uf: The userfaultfd list_head +* @downgrade: Set to true to attempt a downwrite of the mmap_sem +* +* */ -int __do_munmap (struct mm_struct *mm,unsigned long start,size_t len, -struct list_head *uf,bool downgrade) +int do_mas_align_munmap (struct ma_state *mas,struct vm_area_struct *vma, +struct mm_struct *mm,unsigned long start, +unsigned long end,struct list_head *uf,bool downgrade) { -unsigned long end; -struct vm_area_struct *vma, *prev, *last; -MA_STATE (mas, &mm->mm_mt, start, start); - -if ((offset_in_page (start)) || start >TASK_SIZE || len >TASK_SIZE -start) -return -EINVAL ; - - end = start +PAGE_ALIGN (len); -if (end == start) -return -EINVAL ; - -/* arch_unmap() might do unmaps itself. */ -arch_unmap (mm, start, end); - -/* Find the first overlapping VMA */ - vma =mas_find (&mas, end -1 ); -if (!vma) -return 0 ; - - mas.last = end -1 ; +struct vm_area_struct *prev, *last;/* we have start < vma->vm_end */ /*
@@ -2474,8 +2464,8 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,return error; prev = vma; vma =vma_next (mm, prev); - mas.index = start; -mas_reset (&mas); + mas->index = start; +mas_reset (mas); }else { prev = vma->vm_prev; }
@@ -2491,7 +2481,7 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,if (error)return error; vma =vma_next (mm, prev); -mas_reset (&mas); +mas_reset (mas); }
@@ -2518,7 +2508,7 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,*/ mm->map_count -=unlock_range (vma, &last, end);/* Drop removed area from the tree */ -mas_store_gfp (&mas,NULL ,GFP_KERNEL ); +mas_store_gfp (mas,NULL ,GFP_KERNEL );/* Detach vmas from the MM linked list */ vma->vm_prev =NULL ;
@@ -2555,6 +2545,58 @@ int __do_munmap(struct mm_struct *mm, unsigned long start, size_t len,return downgrade ?1 :0 ; } +int __do_munmap (struct mm_struct *mm,unsigned long start,size_t len, +struct list_head *uf,bool downgrade) +{ +MA_STATE (mas, &mm->mm_mt, start, start); +return do_mas_munmap (&mas, mm, start, len, uf, downgrade); +} + +/* +* do_mas_munmap() - munmap a given range. +* @mas: The maple state +* @mm: The mm_struct +* @start: The start address to munmap +* @len: The length of the range to munmap +* @uf: The userfaultfd list_head +* @downgrade: set to true if the user wants to attempt to write_downgrade the +* mmap_sem +* +* This function takes a @mas that is in the correct state to remove the +* mapping(s). The @len will be aligned and any arch_unmap work will be +* preformed. +*/ +int do_mas_munmap (struct ma_state *mas,struct mm_struct *mm, +unsigned long start,size_t len,struct list_head *uf, +bool downgrade) +{ +unsigned long end; +struct vm_area_struct *vma; + +if ((offset_in_page (start)) || start >TASK_SIZE || len >TASK_SIZE -start) +return -EINVAL ; + + end = start +PAGE_ALIGN (len); +if (end == start) +return -EINVAL ; + +/* arch_unmap() might do unmaps itself. */ +arch_unmap (mm, start, end); + +/* Find the first overlapping VMA */ + vma =mas_find (mas, end -1 ); +if (!vma) +return 0 ; + + mas->last = end -1 ; +return do_mas_align_munmap (mas, vma, mm, start, end, uf, downgrade); +} +/* do_munmap() - Wrapper function for non-maple tree aware do_munmap() calls. +* @mm: The mm_struct +* @start: The start address to munmap +* @len: The length to be munmapped. +* @uf: The userfaultfd list_head +*/ int do_munmap (struct mm_struct *mm,unsigned long start,size_t len,struct list_head *uf) {
Signature · 1 line
2.28.0
PATCH 24/28 mmap: Use find_vma_intersection in do_mmap() for overlap
In reply to Liam R. HowlettWhen detecting a conflict with MAP_FIXED_NOREPLACE, using the new interface avoids the need for a temp variable
--- mm/mmap.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
mm/mmap.c +1 -3
@@ -1433,9 +1433,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return addr;if (flags &MAP_FIXED_NOREPLACE ) { -struct vm_area_struct *vma =find_vma (mm, addr); - -if (vma && vma->vm_start < addr + len) +if (find_vma_intersection (mm, addr, addr + len))return -EEXIST ; }
Signature · 1 line
2.28.0
PATCH 25/28 mmap: Remove __do_munmap() in favour of do_mas_munmap()
In reply to Liam R. HowlettExport new interface and use it in place of old interface.
--- include/linux/mm.h | 4 ++-- mm/mmap.c | 16 ++++------------ mm/mremap.c | 7 ++++--- 3 files changed, 10 insertions(+), 17 deletions(-)
include/linux/mm.h +2 -2
@@ -2573,8 +2573,8 @@ extern unsigned long mmap_region(struct file *file, unsigned long addr,extern unsigned long do_mmap (struct file *file,unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long pgoff,unsigned long *populate,struct list_head *uf); -extern int __do_munmap (struct mm_struct *,unsigned long ,size_t , -struct list_head *uf,bool downgrade); +extern int do_mas_munmap (struct ma_state *mas,struct mm_struct *mm, +unsigned long start,size_t len,struct list_head *uf,bool downgrade);extern int do_munmap (struct mm_struct *,unsigned long ,size_t ,struct list_head *uf);extern int do_madvise (struct mm_struct *mm,unsigned long start,size_t len_in,int behavior);
mm/mmap.c +4 -12
@@ -2543,13 +2543,6 @@ int do_mas_align_munmap(struct ma_state *mas, struct vm_area_struct *vma,return downgrade ?1 :0 ; } -int __do_munmap (struct mm_struct *mm,unsigned long start,size_t len, -struct list_head *uf,bool downgrade) -{ -MA_STATE (mas, &mm->mm_mt, start, start); -return do_mas_munmap (&mas, mm, start, len, uf, downgrade); -} -/* * do_mas_munmap() - munmap a given range. * @mas: The maple state
@@ -2598,7 +2591,8 @@ int do_mas_munmap(struct ma_state *mas, struct mm_struct *mm,int do_munmap (struct mm_struct *mm,unsigned long start,size_t len,struct list_head *uf) { -return __do_munmap (mm, start, len, uf, false); +MA_STATE (mas, &mm->mm_mt, start, start); +return do_mas_munmap (&mas, mm, start, len, uf, false); } unsigned long mmap_region(struct file *file, unsigned long addr,
@@ -2846,11 +2840,12 @@ static int __vm_munmap(unsigned long start, size_t len, bool downgrade)int ret;struct mm_struct *mm = current->mm;LIST_HEAD (uf); +MA_STATE (mas, &mm->mm_mt, start, start);if (mmap_write_lock_killable (mm))return -EINTR ; - ret =__do_munmap (mm, start, len, &uf, downgrade); + ret =do_mas_munmap (&mas, mm, start, len, &uf, downgrade);/* * Returning 1 indicates mmap_lock is downgraded. * But 1 is not legal return value of vm_munmap() and munmap(), reset
@@ -3002,9 +2997,6 @@ static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma,arch_unmap (mm, newbrk, oldbrk);if (likely (vma->vm_start >= newbrk)) {// remove entire mapping(s) -mas_set (mas, newbrk); -if (vma->vm_start != newbrk) -mas_reset (mas);// cause a re-walk for the first overlap. ret =do_mas_munmap (mas, mm, newbrk, oldbrk-newbrk, uf, true); goto munmap_full_vma; }
mm/mremap.c +4 -3
@@ -723,14 +723,15 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,/* * Always allow a shrinking remap: that just unmaps * the unnecessary pages.. -* __do_munmap does all the needed commit accounting, and +* do_mas_munmap does all the needed commit accounting, and * downgrades mmap_lock to read if so directed. */ if (old_len >= new_len) {int retval; +MA_STATE (mas, &mm->mm_mt, addr + new_len, addr + new_len); - retval =__do_munmap (mm, addr+new_len, old_len - new_len, - &uf_unmap, true); + retval =do_mas_munmap (&mas, mm, addr + new_len, + old_len - new_len, &uf_unmap, true);if (retval <0 && old_len != new_len) { ret = retval; goto out;
Signature · 1 line
2.28.0
PATCH 26/28 mm/mmap: Change do_brk_munmap() to use do_mas_align_munmap()
In reply to Liam R. Howlettdo_brk_munmap() already has aligned addresses.
--- mm/mmap.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
mm/mmap.c +3 -2
@@ -2992,12 +2992,13 @@ static int do_brk_munmap(struct ma_state *mas, struct vm_area_struct *vma,struct mm_struct *mm = vma->vm_mm;struct vm_area_struct unmap;unsigned long unmap_pages; -int ret =1 ; +int ret;arch_unmap (mm, newbrk, oldbrk);if (likely (vma->vm_start >= newbrk)) {// remove entire mapping(s) - ret =do_mas_munmap (mas, mm, newbrk, oldbrk-newbrk, uf, true); + ret =do_mas_align_munmap (mas, vma, mm, newbrk, oldbrk, uf, + true); goto munmap_full_vma; }
Signature · 1 line
2.28.0
PATCH 27/28 mmap: Update count_vma_pages_range() to only use one ma_state
In reply to Liam R. Howlett--- mm/mmap.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
mm/mmap.c +1 -3
@@ -483,17 +483,15 @@ static unsigned long count_vma_pages_range(struct mm_struct *mm,MA_STATE (mas, &mm->mm_mt, addr, addr);/* Find first overlaping mapping */ - vma =find_vma_intersection (mm, addr, end); + vma =mas_find (&mas, end -1 );if (!vma)return 0 ; vm_start = vma->vm_start; vm_end = vma->vm_end; - nr_pages = (min (end, vm_end) -max (addr, vm_start)) >>PAGE_SHIFT ;/* Iterate over the rest of the overlaps */ - mas.index = mas.last = vm_end;mas_for_each (&mas, vma, end) { vm_start = vma->vm_start; vm_end = vma->vm_end;
Signature · 1 line
2.28.0
PATCH 28/28 mmap: make remove_vma_list() inline
In reply to Liam R. Howlett--- mm/mmap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/mmap.c +2 -1
@@ -2280,7 +2280,8 @@ EXPORT_SYMBOL_GPL(find_extend_vma);* * Called with the mm semaphore held. */ -static void remove_vma_list (struct mm_struct *mm,struct vm_area_struct *vma) +static inline void remove_vma_list (struct mm_struct *mm, +struct vm_area_struct *vma) {unsigned long nr_accounted =0 ;
Signature · 1 line
2.28.0