rbtree based interval tree as a prio_tree replacement
Subsystems (18)
cover rbtree based interval tree as a prio_tree replacement
This patchset goes over the rbtree changes that have been already integrated into Andrew's -mm tree, as well as the augmented rbtree proposal which is currently pending.
Patch 1 implements support for interval trees, on top of the augmented rbtree API. It also adds synthetic tests to compare the performance of interval trees vs prio trees. Short answers is that interval trees are slightly faster (~25%) on insert/erase, and much faster (~2.4 - 3x) on search. It is debatable how realistic the synthetic test is, and I have not made such measurements yet, but my impression is that interval trees would still come out faster.
Patch 2 uses a preprocessor template to make the interval tree generic, and uses it as a replacement for the vma prio_tree.
Patch 3 takes the other prio_tree user, kmemleak, and converts it to use a basic rbtree. We don't actually need the augmented rbtree support here because the intervals are always non-overlapping.
Patch 4 removes the now-unused prio tree library.
Patch 5 proposes an additional optimization to rb_erase_augmented, now providing it as an inline function so that the augmented callbacks can be inlined in. This provides an additional 5-10% performance improvement for the interval tree insert/erase benchmark. There is a maintainance cost as it exposes augmented rbtree users to some of the rbtree library internals; however I think this cost shouldn't be too high as I expect the augmented rbtree will always have much less users than the base rbtree.
I should probably add a quick summary of why I think it makes sense to replace prio trees with augmented rbtree based interval trees now. One of the drivers is that we need augmented rbtrees for Rik's vma gap finding code, and once you have them, it just makes sense to use them for interval trees as well, as this is the simpler and more well known algorithm. prio trees, in comparison, seem *too* clever: they impose an additional 'heap' constraint on the tree, which they use to guarantee a faster worst-case complexity of O(k+log N) for stabbing queries in a well-balanced prio tree, vs O(k*log N) for interval trees (where k=number of matches, N=number of intervals). Now this sounds great, but in practice prio trees don't realize this theorical benefit. First, the additional constraint makes them harder to update, so that the kernel implementation has to simplify things by balancing them like a radix tree, which is not always ideal. Second, the fact that there are both index and heap properties makes both tree manipulation and search more complex, which results in a higher multiplicative time constant. As it turns out, the simple interval tree algorithm ends up running faster than the more clever prio tree.
Michel Lespinasse (5):
rbtree: add prio tree and interval tree tests
mm: replace vma prio_tree with an interval tree
kmemleak: use rbtree instead of prio tree
prio_tree: remove
rbtree: move augmented rbtree functionality to rbtree_augmented.h
Documentation/00-INDEX | 2 - Documentation/prio_tree.txt | 107 -------- Documentation/rbtree.txt | 13 + arch/arm/mm/fault-armv.c | 3 +- arch/arm/mm/flush.c | 3 +- arch/parisc/kernel/cache.c | 3 +- arch/x86/mm/hugetlbpage.c | 3 +- arch/x86/mm/pat_rbtree.c | 2 +- fs/hugetlbfs/inode.c | 9 +- fs/inode.c | 2 +- include/linux/fs.h | 6 +- include/linux/interval_tree.h | 27 ++ include/linux/interval_tree_tmpl.h | 219 +++++++++++++++++ include/linux/mm.h | 30 ++- include/linux/mm_types.h | 14 +- include/linux/prio_tree.h | 120 --------- include/linux/rbtree.h | 48 ---- include/linux/rbtree_augmented.h | 223 +++++++++++++++++ init/main.c | 2 - kernel/events/uprobes.c | 3 +- kernel/fork.c | 2 +- lib/Kconfig.debug | 6 + lib/Makefile | 5 +- lib/interval_tree.c | 13 + lib/interval_tree_test_main.c | 105 ++++++++ lib/prio_tree.c | 466 ------------------------------------ lib/rbtree.c | 162 +------------ lib/rbtree_test.c | 2 +- mm/Makefile | 4 +- mm/filemap_xip.c | 3 +- mm/fremap.c | 2 +- mm/hugetlb.c | 3 +- mm/interval_tree.c | 61 +++++ mm/kmemleak.c | 98 ++++---- mm/memory-failure.c | 3 +- mm/memory.c | 9 +- mm/mmap.c | 22 +- mm/nommu.c | 12 +- mm/prio_tree.c | 208 ---------------- mm/rmap.c | 18 +- 40 files changed, 803 insertions(+), 1240 deletions(-) delete mode 100644 Documentation/prio_tree.txt create mode 100644 include/linux/interval_tree.h create mode 100644 include/linux/interval_tree_tmpl.h delete mode 100644 include/linux/prio_tree.h create mode 100644 include/linux/rbtree_augmented.h create mode 100644 lib/interval_tree.c create mode 100644 lib/interval_tree_test_main.c delete mode 100644 lib/prio_tree.c create mode 100644 mm/interval_tree.c delete mode 100644 mm/prio_tree.c
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/5 rbtree: add prio tree and interval tree tests
This adds two test modules:
- prio_tree_test measures the performance of lib/prio_tree.c, both for insertion/removal and for stabbing searches
- interval_tree_test measures the performance of a library of equivalent functionality, built using the augmented rbtree support.
In order to support the second test module, lib/interval_tree.c is introduced. It is kept separate from the interval_tree_test main file for two reasons: first we don't want to provide an unfair advantage over prio_tree_test by having everything in a single compilation unit, and second there is the possibility that the interval tree functionality could get some non-test users in kernel over time.
--- include/linux/interval_tree.h | 27 +++++++ lib/Kconfig.debug | 12 +++ lib/Makefile | 4 + lib/interval_tree.c | 159 +++++++++++++++++++++++++++++++++++++++++ lib/interval_tree_test_main.c | 105 +++++++++++++++++++++++++++ lib/prio_tree.c | 4 + lib/prio_tree_test.c | 106 +++++++++++++++++++++++++++ 7 files changed, 417 insertions(+), 0 deletions(-) create mode 100644 include/linux/interval_tree.h create mode 100644 lib/interval_tree.c create mode 100644 lib/interval_tree_test_main.c create mode 100644 lib/prio_tree_test.c
include/linux/interval_tree.h new file +27 -0
@@ -0,0 +1,27 @@ +#ifndef _LINUX_INTERVAL_TREE_H +#define _LINUX_INTERVAL_TREE_H + +#include <linux/rbtree.h> + +struct interval_tree_node { +struct rb_node rb; +unsigned long start;/* Start of interval */ +unsigned long last;/* Last location _in_ interval */ +unsigned long __subtree_last; +}; + +extern void +interval_tree_insert (struct interval_tree_node *node,struct rb_root *root); + +extern void +interval_tree_remove (struct interval_tree_node *node,struct rb_root *root); + +extern struct interval_tree_node * +interval_tree_iter_first (struct rb_root *root, +unsigned long start,unsigned long last); + +extern struct interval_tree_node * +interval_tree_iter_next (struct interval_tree_node *node, +unsigned long start,unsigned long last); + +#endif /* _LINUX_INTERVAL_TREE_H */
lib/Kconfig.debug +12 -0
@@ -1185,6 +1185,18 @@ config RBTREE_TEST A benchmark measuring the performance of the rbtree library. Also includes rbtree invariant checks. +config PRIO_TREE_TEST + tristate "Prio tree test" + depends on m && DEBUG_KERNEL + help + A benchmark measuring the performance of the prio tree library + +config INTERVAL_TREE_TEST + tristate "Interval tree test" + depends on m && DEBUG_KERNEL + help + A benchmark measuring the performance of the interval tree library + config PROVIDE_OHCI1394_DMA_INIT bool "Remote debugging over FireWire early on boot" depends on PCI && X86
lib/Makefile +4 -0
@@ -131,6 +131,10 @@ obj-$(CONFIG_GENERIC_STRNLEN_USER) += strnlen_user.o obj-$(CONFIG_STMP_DEVICE) += stmp_device.o obj-$(CONFIG_RBTREE_TEST) += rbtree_test.o +obj-$(CONFIG_PRIO_TREE_TEST) += prio_tree_test.o +obj-$(CONFIG_INTERVAL_TREE_TEST) += interval_tree_test.o + +interval_tree_test-objs := interval_tree_test_main.o interval_tree.o hostprogs-y := gen_crc32table clean-files := crc32table.h
lib/interval_tree.c new file +159 -0
159 lines not on this page · raw message
lib/interval_tree_test_main.c new file +105 -0
105 lines not on this page · raw message
lib/prio_tree.c +4 -0
@@ -14,6 +14,7 @@#include <linux/init.h> #include <linux/mm.h> #include <linux/prio_tree.h> +#include <linux/export.h> /* * A clever mix of heap and radix trees forms a radix priority search tree (PST)
@@ -241,6 +242,7 @@ struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,BUG ();return NULL ; } +EXPORT_SYMBOL (prio_tree_insert);/* * Remove a prio_tree_node @node from a radix priority search tree @root. The
@@ -290,6 +292,7 @@ void prio_tree_remove(struct prio_tree_root *root, struct prio_tree_node *node)while (cur != node) cur =prio_tree_replace (root, cur->parent, cur); } +EXPORT_SYMBOL (prio_tree_remove);static void iter_walk_down (struct prio_tree_iter *iter) {
@@ -464,3 +467,4 @@ repeat: goto repeat; } +EXPORT_SYMBOL (prio_tree_next);
lib/prio_tree_test.c new file +106 -0
106 lines not on this page · raw message
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 2/5 mm: replace vma prio_tree with an interval tree
In reply to Michel LespinasseImplement an interval tree as a replacement for the VMA prio_tree. The algorithms are similar to lib/interval_tree.c; however that code can't be directly reused as the interval endpoints are not explicitly stored in the VMA. So instead, the common algorithm is moved into a template and the details (node type, how to get interval endpoints from the node, etc) are filled in using the C preprocessor.
Once the interval tree functions are available, using them as a replacement to the VMA prio tree is a relatively simple, mechanical job.
--- arch/arm/mm/fault-armv.c | 3 +- arch/arm/mm/flush.c | 3 +- arch/parisc/kernel/cache.c | 3 +- arch/x86/mm/hugetlbpage.c | 3 +- fs/hugetlbfs/inode.c | 9 +- fs/inode.c | 2 +- include/linux/fs.h | 6 +- include/linux/interval_tree_tmpl.h | 215 ++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 30 +++-- include/linux/mm_types.h | 14 +-- kernel/events/uprobes.c | 3 +- kernel/fork.c | 2 +- lib/interval_tree.c | 166 ++-------------------------- lib/prio_tree.c | 19 +--- mm/Makefile | 4 +- mm/filemap_xip.c | 3 +- mm/fremap.c | 2 +- mm/hugetlb.c | 3 +- mm/interval_tree.c | 61 ++++++++++ mm/memory-failure.c | 3 +- mm/memory.c | 9 +- mm/mmap.c | 22 ++-- mm/nommu.c | 12 +- mm/prio_tree.c | 208 ---------------------------------- mm/rmap.c | 18 +-- 25 files changed, 357 insertions(+), 466 deletions(-) create mode 100644 include/linux/interval_tree_tmpl.h create mode 100644 mm/interval_tree.c delete mode 100644 mm/prio_tree.c
arch/arm/mm/fault-armv.c +1 -2
@@ -134,7 +134,6 @@ make_coherent(struct address_space *mapping, struct vm_area_struct *vma, {struct mm_struct *mm = vma->vm_mm;struct vm_area_struct *mpnt; -struct prio_tree_iter iter;unsigned long offset;pgoff_t pgoff;int aliases =0 ;
@@ -147,7 +146,7 @@ make_coherent(struct address_space *mapping, struct vm_area_struct *vma,* cache coherency. */ flush_dcache_mmap_lock (mapping); - vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) {/* * If this VMA is not in our MM, we can ignore it. * Note that we intentionally mask out the VMA
arch/arm/mm/flush.c +1 -2
@@ -196,7 +196,6 @@ static void __flush_dcache_aliases(struct address_space *mapping, struct page *p {struct mm_struct *mm = current->active_mm;struct vm_area_struct *mpnt; -struct prio_tree_iter iter;pgoff_t pgoff;/*
@@ -208,7 +207,7 @@ static void __flush_dcache_aliases(struct address_space *mapping, struct page *p pgoff = page->index << (PAGE_CACHE_SHIFT -PAGE_SHIFT );flush_dcache_mmap_lock (mapping); - vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) {unsigned long offset;/*
arch/parisc/kernel/cache.c +1 -2
@@ -276,7 +276,6 @@ void flush_dcache_page(struct page *page) {struct address_space *mapping =page_mapping (page);struct vm_area_struct *mpnt; -struct prio_tree_iter iter;unsigned long offset;unsigned long addr, old_addr =0 ;pgoff_t pgoff;
@@ -299,7 +298,7 @@ void flush_dcache_page(struct page *page)* to flush one address here for them all to become coherent */ flush_dcache_mmap_lock (mapping); - vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) { offset = (pgoff - mpnt->vm_pgoff) <<PAGE_SHIFT ; addr = mpnt->vm_start + offset;
arch/x86/mm/hugetlbpage.c +1 -2
@@ -64,7 +64,6 @@ static void huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)struct address_space *mapping = vma->vm_file->f_mapping;pgoff_t idx = ((addr - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; -struct prio_tree_iter iter;struct vm_area_struct *svma;unsigned long saddr;pte_t *spte =NULL ;
@@ -73,7 +72,7 @@ static void huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)return ;mutex_lock (&mapping->i_mmap_mutex); - vma_prio_tree_foreach(svma, &iter, &mapping->i_mmap, idx, idx) { + vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {if (svma == vma)continue ;
fs/hugetlbfs/inode.c +4 -5
@@ -397,17 +397,16 @@ static void hugetlbfs_evict_inode(struct inode *inode) }static inline void -hugetlb_vmtruncate_list (struct prio_tree_root *root,pgoff_t pgoff) +hugetlb_vmtruncate_list (struct rb_root *root,pgoff_t pgoff) {struct vm_area_struct *vma; -struct prio_tree_iter iter; - vma_prio_tree_foreach(vma, &iter, root, pgoff, ULONG_MAX) { + vma_interval_tree_foreach(vma, root, pgoff, ULONG_MAX) {unsigned long v_offset;/* * Can the expression below overflow on 32-bit arches? -* No, because the prio_tree returns us only those vmas +* No, because the interval tree returns us only those vmas * which overlap the truncated area starting at pgoff, * and no vma on a 32-bit arch can span beyond the 4GB. */
@@ -432,7 +431,7 @@ static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)i_size_write (inode, offset);mutex_lock (&mapping->i_mmap_mutex); -if (!prio_tree_empty (&mapping->i_mmap)) +if (!RB_EMPTY_ROOT (&mapping->i_mmap))hugetlb_vmtruncate_list (&mapping->i_mmap, pgoff);mutex_unlock (&mapping->i_mmap_mutex);truncate_hugepages (inode, offset);
fs/inode.c +1 -1
@@ -348,7 +348,7 @@ void address_space_init_once(struct address_space *mapping)mutex_init (&mapping->i_mmap_mutex);INIT_LIST_HEAD (&mapping->private_list);spin_lock_init (&mapping->private_lock); -INIT_RAW_PRIO_TREE_ROOT (&mapping->i_mmap); + mapping->i_mmap =RB_ROOT ;INIT_LIST_HEAD (&mapping->i_mmap_nonlinear); }EXPORT_SYMBOL (address_space_init_once);
include/linux/fs.h +3 -3
@@ -399,7 +399,7 @@ struct inodes_stat_t {#include <linux/cache.h> #include <linux/list.h> #include <linux/radix-tree.h> -#include <linux/prio_tree.h> +#include <linux/rbtree.h> #include <linux/init.h> #include <linux/pid.h> #include <linux/bug.h>
@@ -658,7 +658,7 @@ struct address_space {struct radix_tree_root page_tree;/* radix tree of all pages */ spinlock_t tree_lock;/* and lock protecting it */ unsigned int i_mmap_writable;/* count VM_SHARED mappings */ -struct prio_tree_root i_mmap;/* tree of private and shared mappings */ +struct rb_root i_mmap;/* tree of private and shared mappings */ struct list_head i_mmap_nonlinear;/*list VM_NONLINEAR mappings */ struct mutex i_mmap_mutex;/* protect tree, count, list */ /* Protected by tree_lock together with the radix tree */
@@ -730,7 +730,7 @@ int mapping_tagged(struct address_space *mapping, int tag);*/ static inline int mapping_mapped (struct address_space *mapping) { -return !prio_tree_empty (&mapping->i_mmap) || +return !RB_EMPTY_ROOT (&mapping->i_mmap) || !list_empty (&mapping->i_mmap_nonlinear); }
include/linux/interval_tree_tmpl.h new file +215 -0
215 lines not on this page · raw message
include/linux/mm.h +17 -13
@@ -10,7 +10,6 @@#include <linux/list.h> #include <linux/mmzone.h> #include <linux/rbtree.h> -#include <linux/prio_tree.h> #include <linux/atomic.h> #include <linux/debug_locks.h> #include <linux/mm_types.h>
@@ -1336,22 +1335,27 @@ extern void zone_pcp_update(struct zone *zone);extern atomic_long_t mmap_pages_allocated;extern int nommu_shrink_inode_mappings (struct inode *,size_t ,size_t ); -/* prio_tree.c */ -void vma_prio_tree_add (struct vm_area_struct *,struct vm_area_struct *old); -void vma_prio_tree_insert (struct vm_area_struct *,struct prio_tree_root *); -void vma_prio_tree_remove (struct vm_area_struct *,struct prio_tree_root *); -struct vm_area_struct *vma_prio_tree_next (struct vm_area_struct *vma, -struct prio_tree_iter *iter); - -#define vma_prio_tree_foreach (vma, iter, root, begin, end) \ - for (prio_tree_iter_init(iter, root, begin, end), vma = NULL; \ - (vma = vma_prio_tree_next(vma, iter)); ) +/* interval_tree.c */ +void vma_interval_tree_add (struct vm_area_struct *vma, +struct vm_area_struct *old, +struct address_space *mapping); +void vma_interval_tree_insert (struct vm_area_struct *node, +struct rb_root *root); +void vma_interval_tree_remove (struct vm_area_struct *node, +struct rb_root *root); +struct vm_area_struct *vma_interval_tree_iter_first (struct rb_root *root, +unsigned long start,unsigned long last); +struct vm_area_struct *vma_interval_tree_iter_next (struct vm_area_struct *node, +unsigned long start,unsigned long last); + +#define vma_interval_tree_foreach (vma, root, start, last) \ + for (vma = vma_interval_tree_iter_first(root, start, last); \ + vma; vma = vma_interval_tree_iter_next(vma, start, last))static inline void vma_nonlinear_insert (struct vm_area_struct *vma,struct list_head *list) { - vma->shared.vm_set.parent =NULL ; -list_add_tail (&vma->shared.vm_set.list, list); +list_add_tail (&vma->shared.nonlinear, list); }/* mmap.c */
include/linux/mm_types.h +5 -9
@@ -6,7 +6,6 @@#include <linux/threads.h> #include <linux/list.h> #include <linux/spinlock.h> -#include <linux/prio_tree.h> #include <linux/rbtree.h> #include <linux/rwsem.h> #include <linux/completion.h>
@@ -224,18 +223,15 @@ struct vm_area_struct {/* * For areas with an address space and backing store, -* linkage into the address_space->i_mmap prio tree, or -* linkage to the list of like vmas hanging off its node, or +* linkage into the address_space->i_mmap interval tree, or * linkage of vma in the address_space->i_mmap_nonlinear list. */ union {struct { -struct list_head list; -void *parent;/* aligns with prio_tree_node parent */ -struct vm_area_struct *head; - } vm_set; - -struct raw_prio_tree_node prio_tree_node; +struct rb_node rb; +unsigned long rb_subtree_last; + } linear; +struct list_head nonlinear; } shared;/*
kernel/events/uprobes.c +1 -2
@@ -754,7 +754,6 @@ static struct vma_info * __find_next_vma_info(struct address_space *mapping, struct list_head *head, struct vma_info *vi, loff_t offset, bool is_register) { - struct prio_tree_iter iter; struct vm_area_struct *vma;struct vma_info *tmpvi;unsigned long pgoff;
@@ -763,7 +762,7 @@ __find_next_vma_info(struct address_space *mapping, struct list_head *head, pgoff = offset >>PAGE_SHIFT ; - vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {if (!valid_vma (vma, is_register))continue ;
kernel/fork.c +1 -1
@@ -425,7 +425,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) mapping->i_mmap_writable++;flush_dcache_mmap_lock (mapping);/* insert tmp into the share list, just after mpnt */ -vma_prio_tree_add (tmp, mpnt); +vma_interval_tree_add (tmp, mpnt, mapping);flush_dcache_mmap_unlock (mapping);mutex_unlock (&mapping->i_mmap_mutex); }
lib/interval_tree.c +10 -156
169 lines not on this page · raw message
lib/prio_tree.c +2 -17
@@ -44,27 +44,12 @@* The following macros are used for implementing prio_tree for i_mmap */ -#define RADIX_INDEX (vma) ((vma)->vm_pgoff) -#define VMA_SIZE (vma) (((vma)->vm_end - (vma)->vm_start) >> PAGE_SHIFT) -/* avoid overflow */ -#define HEAP_INDEX (vma) ((vma)->vm_pgoff + (VMA_SIZE(vma) - 1)) - -static void get_index (const struct prio_tree_root *root,const struct prio_tree_node *node,unsigned long *radix,unsigned long *heap) { -if (root->raw) { -struct vm_area_struct *vma =prio_tree_entry ( - node, struct vm_area_struct, shared.prio_tree_node); - - *radix =RADIX_INDEX (vma); - *heap =HEAP_INDEX (vma); - } -else { - *radix = node->start; - *heap = node->last; - } + *radix = node->start; + *heap = node->last; }static unsigned long index_bits_to_maxindex[BITS_PER_LONG ];
mm/Makefile +2 -2
@@ -14,9 +14,9 @@ endif obj-y := filemap.o mempool.o oom_kill.o fadvise.o \ maccess.o page_alloc.o page-writeback.o \ readahead.o swap.o truncate.o vmscan.o shmem.o \ - prio_tree.o util.o mmzone.o vmstat.o backing-dev.o \ + util.o mmzone.o vmstat.o backing-dev.o \ page_isolation.o mm_init.o mmu_context.o percpu.o \ - compaction.o $(mmu-y) + compaction.o interval_tree.o $(mmu-y) obj-y += init-mm.o ifdef CONFIG_NO_BOOTMEM
mm/filemap_xip.c +1 -2
@@ -167,7 +167,6 @@ __xip_unmap (struct address_space * mapping, {struct vm_area_struct *vma;struct mm_struct *mm; -struct prio_tree_iter iter;unsigned long address;pte_t *pte;pte_t pteval;
@@ -184,7 +183,7 @@ __xip_unmap (struct address_space * mapping, retry:mutex_lock (&mapping->i_mmap_mutex); - vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { mm = vma->vm_mm; address = vma->vm_start + ((pgoff - vma->vm_pgoff) <<PAGE_SHIFT );
mm/fremap.c +1 -1
@@ -213,7 +213,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,mutex_lock (&mapping->i_mmap_mutex);flush_dcache_mmap_lock (mapping); vma->vm_flags |=VM_NONLINEAR ; -vma_prio_tree_remove (vma, &mapping->i_mmap); +vma_interval_tree_remove (vma, &mapping->i_mmap);vma_nonlinear_insert (vma, &mapping->i_mmap_nonlinear);flush_dcache_mmap_unlock (mapping);mutex_unlock (&mapping->i_mmap_mutex);
mm/hugetlb.c +1 -2
@@ -2408,7 +2408,6 @@ static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,struct hstate *h =hstate_vma (vma);struct vm_area_struct *iter_vma;struct address_space *mapping; -struct prio_tree_iter iter;pgoff_t pgoff;/*
@@ -2425,7 +2424,7 @@ static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,* __unmap_hugepage_range() is called as the lock is already held */ mutex_lock (&mapping->i_mmap_mutex); - vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {/* Do not unmap the current VMA */ if (iter_vma == vma)continue ;
mm/interval_tree.c new file +61 -0
61 lines not on this page · raw message
mm/memory-failure.c +1 -2
@@ -431,7 +431,6 @@ static void collect_procs_file(struct page *page, struct list_head *to_kill, {struct vm_area_struct *vma;struct task_struct *tsk; -struct prio_tree_iter iter;struct address_space *mapping = page->mapping;mutex_lock (&mapping->i_mmap_mutex);
@@ -442,7 +441,7 @@ static void collect_procs_file(struct page *page, struct list_head *to_kill,if (!task_early_kill (tsk))continue ; -vma_prio_tree_foreach (vma, &iter, &mapping->i_mmap, pgoff, +vma_interval_tree_foreach (vma, &mapping->i_mmap, pgoff, pgoff) {/* * Send early kill signal to tasks where a vma covers
mm/memory.c +4 -5
@@ -2790,14 +2790,13 @@ static void unmap_mapping_range_vma(struct vm_area_struct *vma,zap_page_range_single (vma, start_addr, end_addr - start_addr, details); } -static inline void unmap_mapping_range_tree (struct prio_tree_root *root, +static inline void unmap_mapping_range_tree (struct rb_root *root,struct zap_details *details) {struct vm_area_struct *vma; -struct prio_tree_iter iter;pgoff_t vba, vea, zba, zea; -vma_prio_tree_foreach (vma, &iter, root, +vma_interval_tree_foreach (vma, root, details->first_index, details->last_index) { vba = vma->vm_pgoff;
@@ -2828,7 +2827,7 @@ static inline void unmap_mapping_range_list(struct list_head *head,* across *all* the pages in each nonlinear VMA, not just the pages * whose virtual address lies outside the file truncation point. */ -list_for_each_entry (vma, head, shared.vm_set.list) { +list_for_each_entry (vma, head, shared.nonlinear) { details->nonlinear_vma = vma;unmap_mapping_range_vma (vma, vma->vm_start, vma->vm_end, details); }
@@ -2872,7 +2871,7 @@ void unmap_mapping_range(struct address_space *mapping,mutex_lock (&mapping->i_mmap_mutex); -if (unlikely (!prio_tree_empty (&mapping->i_mmap))) +if (unlikely (!RB_EMPTY_ROOT (&mapping->i_mmap)))unmap_mapping_range_tree (&mapping->i_mmap, &details);if (unlikely (!list_empty (&mapping->i_mmap_nonlinear)))unmap_mapping_range_list (&mapping->i_mmap_nonlinear, &details);
mm/mmap.c +11 -11
@@ -199,14 +199,14 @@ static void __remove_shared_vm_struct(struct vm_area_struct *vma,flush_dcache_mmap_lock (mapping);if (unlikely (vma->vm_flags &VM_NONLINEAR )) -list_del_init (&vma->shared.vm_set.list); +list_del_init (&vma->shared.nonlinear);else -vma_prio_tree_remove (vma, &mapping->i_mmap); +vma_interval_tree_remove (vma, &mapping->i_mmap);flush_dcache_mmap_unlock (mapping); }/* -* Unlink a file-based vm structure from its prio_tree, to hide +* Unlink a file-based vm structure from its interval tree, to hide * vma from rmap and vmtruncate before freeing its page tables. */ void unlink_file_vma (struct vm_area_struct *vma)
@@ -417,7 +417,7 @@ static void __vma_link_file(struct vm_area_struct *vma)if (unlikely (vma->vm_flags &VM_NONLINEAR ))vma_nonlinear_insert (vma, &mapping->i_mmap_nonlinear);else -vma_prio_tree_insert (vma, &mapping->i_mmap); +vma_interval_tree_insert (vma, &mapping->i_mmap);flush_dcache_mmap_unlock (mapping); } }
@@ -455,7 +455,7 @@ static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,/* * Helper for vma_adjust() in the split_vma insert case: insert a vma into the -* mm's list and rbtree. It has already been inserted into the prio_tree. +* mm's list and rbtree. It has already been inserted into the interval tree. */ static void __insert_vm_struct (struct mm_struct *mm,struct vm_area_struct *vma) {
@@ -496,7 +496,7 @@ int vma_adjust(struct vm_area_struct *vma, unsigned long start,struct vm_area_struct *next = vma->vm_next;struct vm_area_struct *importer =NULL ;struct address_space *mapping =NULL ; -struct prio_tree_root *root =NULL ; +struct rb_root *root =NULL ;struct anon_vma *anon_vma =NULL ;struct file *file = vma->vm_file;long adjust_next =0 ;
@@ -559,7 +559,7 @@ again: remove_next = 1 + (end > next->vm_end);mutex_lock (&mapping->i_mmap_mutex);if (insert) {/* -* Put into prio_tree now, so instantiated pages +* Put into interval tree now, so instantiated pages * are visible to arm/parisc __flush_dcache_page * throughout; but we cannot insert into address * space until vma start or end is updated.
@@ -583,9 +583,9 @@ again: remove_next = 1 + (end > next->vm_end);if (root) {flush_dcache_mmap_lock (mapping); -vma_prio_tree_remove (vma, root); +vma_interval_tree_remove (vma, root);if (adjust_next) -vma_prio_tree_remove (next, root); +vma_interval_tree_remove (next, root); } vma->vm_start = start;
@@ -598,8 +598,8 @@ again: remove_next = 1 + (end > next->vm_end);if (root) {if (adjust_next) -vma_prio_tree_insert (next, root); -vma_prio_tree_insert (vma, root); +vma_interval_tree_insert (next, root); +vma_interval_tree_insert (vma, root);flush_dcache_mmap_unlock (mapping); }
mm/nommu.c +5 -7
@@ -698,7 +698,7 @@ static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)mutex_lock (&mapping->i_mmap_mutex);flush_dcache_mmap_lock (mapping); -vma_prio_tree_insert (vma, &mapping->i_mmap); +vma_interval_tree_insert (vma, &mapping->i_mmap);flush_dcache_mmap_unlock (mapping);mutex_unlock (&mapping->i_mmap_mutex); }
@@ -764,7 +764,7 @@ static void delete_vma_from_mm(struct vm_area_struct *vma)mutex_lock (&mapping->i_mmap_mutex);flush_dcache_mmap_lock (mapping); -vma_prio_tree_remove (vma, &mapping->i_mmap); +vma_interval_tree_remove (vma, &mapping->i_mmap);flush_dcache_mmap_unlock (mapping);mutex_unlock (&mapping->i_mmap_mutex); }
@@ -2047,7 +2047,6 @@ int nommu_shrink_inode_mappings(struct inode *inode, size_t size, size_t newsize) {struct vm_area_struct *vma; -struct prio_tree_iter iter;struct vm_region *region;pgoff_t low, high;size_t r_size, r_top;
@@ -2059,8 +2058,7 @@ int nommu_shrink_inode_mappings(struct inode *inode, size_t size,mutex_lock (&inode->i_mapping->i_mmap_mutex);/* search for VMAs that fall within the dead zone */ -vma_prio_tree_foreach (vma, &iter, &inode->i_mapping->i_mmap, - low, high) { + vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {/* found one - only interested if it's shared out of the page * cache */ if (vma->vm_flags &VM_SHARED ) {
@@ -2076,8 +2074,8 @@ int nommu_shrink_inode_mappings(struct inode *inode, size_t size,* we don't check for any regions that start beyond the EOF as there * shouldn't be any */ -vma_prio_tree_foreach (vma, &iter, &inode->i_mapping->i_mmap, - 0, ULONG_MAX) { +vma_interval_tree_foreach (vma, &inode->i_mapping->i_mmap, + 0, ULONG_MAX) {if (!(vma->vm_flags &VM_SHARED ))continue ;
mm/prio_tree.c deleted +0 -208
208 lines not on this page · raw message
mm/rmap.c +7 -11
@@ -820,7 +820,6 @@ static int page_referenced_file(struct page *page,struct address_space *mapping = page->mapping;pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT -PAGE_SHIFT );struct vm_area_struct *vma; -struct prio_tree_iter iter;int referenced =0 ;/*
@@ -846,7 +845,7 @@ static int page_referenced_file(struct page *page,*/ mapcount =page_mapcount (page); - vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {unsigned long address =vma_address (page, vma);if (address == -EFAULT )continue ;
@@ -945,13 +944,12 @@ static int page_mkclean_file(struct address_space *mapping, struct page *page) {pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT -PAGE_SHIFT );struct vm_area_struct *vma; -struct prio_tree_iter iter;int ret =0 ;BUG_ON (PageAnon (page));mutex_lock (&mapping->i_mmap_mutex); - vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {if (vma->vm_flags &VM_SHARED ) {unsigned long address =vma_address (page, vma);if (address == -EFAULT )
@@ -1547,7 +1545,6 @@ static int try_to_unmap_file(struct page *page, enum ttu_flags flags)struct address_space *mapping = page->mapping;pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT -PAGE_SHIFT );struct vm_area_struct *vma; -struct prio_tree_iter iter;int ret =SWAP_AGAIN ;unsigned long cursor;unsigned long max_nl_cursor =0 ;
@@ -1555,7 +1552,7 @@ static int try_to_unmap_file(struct page *page, enum ttu_flags flags)unsigned int mapcount;mutex_lock (&mapping->i_mmap_mutex); - vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {unsigned long address =vma_address (page, vma);if (address == -EFAULT )continue ;
@@ -1576,7 +1573,7 @@ static int try_to_unmap_file(struct page *page, enum ttu_flags flags) goto out;list_for_each_entry (vma, &mapping->i_mmap_nonlinear, - shared.vm_set.list) { + shared.nonlinear) { cursor = (unsigned long ) vma->vm_private_data;if (cursor > max_nl_cursor) max_nl_cursor = cursor;
@@ -1608,7 +1605,7 @@ static int try_to_unmap_file(struct page *page, enum ttu_flags flags) do { list_for_each_entry(vma, &mapping->i_mmap_nonlinear, - shared.vm_set.list) { + shared.nonlinear) { cursor = (unsigned long) vma->vm_private_data; while ( cursor < max_nl_cursor && cursor < vma->vm_end - vma->vm_start) {
@@ -1631,7 +1628,7 @@ static int try_to_unmap_file(struct page *page, enum ttu_flags flags)* in locked vmas). Reset cursor on all unreserved nonlinear * vmas, now forgetting on which ones it had fallen behind. */ -list_for_each_entry (vma, &mapping->i_mmap_nonlinear, shared.vm_set.list) +list_for_each_entry (vma, &mapping->i_mmap_nonlinear, shared.nonlinear) vma->vm_private_data =NULL ; out:mutex_unlock (&mapping->i_mmap_mutex);
@@ -1748,13 +1745,12 @@ static int rmap_walk_file(struct page *page, int (*rmap_one)(struct page *,struct address_space *mapping = page->mapping;pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT -PAGE_SHIFT );struct vm_area_struct *vma; -struct prio_tree_iter iter;int ret =SWAP_AGAIN ;if (!mapping)return ret;mutex_lock (&mapping->i_mmap_mutex); - vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {unsigned long address =vma_address (page, vma);if (address == -EFAULT )continue ;
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 2/5] mm: replace vma prio_tree with an interval tree
Quoted 4 lines · Michel Lespinasse
mm/interval_tree.c new file +4
-0+#define ITSTRUCT struct vm_area_struct +#define ITSTART (n) ((n)->vm_pgoff) +#define ITLAST (n) ((n)->vm_pgoff + \ + (((n)->vm_end - (n)->vm_start) >> PAGE_SHIFT) - 1)
[...]
Quoted 9 lines
mm/rmap.c +0
-1@@ -1547,7 +1545,6 @@ static int try_to_unmap_file(struct page *page, enum ttu_flags flags)struct address_space *mapping = page->mapping;pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT -PAGE_SHIFT );struct vm_area_struct *vma; -struct prio_tree_iter iter;int ret =SWAP_AGAIN ;unsigned long cursor;unsigned long max_nl_cursor =0 ;
mm/rmap.c static int try_to_unmap_file(struct page *page, enum ttu_flags flags) · line 1555
Quoted 5 lines
mm/rmap.c +1
-1@@ -1555,7 +1552,7 @@ static int try_to_unmap_file(struct page *page, enum ttu_flags flags)unsigned int mapcount;mutex_lock (&mapping->i_mmap_mutex); - vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { + vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
Given the above defines for ITSTART and ITLAST, page index perhaps could not be used directly in scanning interval tree for vma when ttum hugetlb page?
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 3/5 kmemleak: use rbtree instead of prio tree
In reply to Michel Lespinassekmemleak uses a tree where each node represents an allocated memory object in order to quickly find out what object a given address is part of. However, the objects don't overlap, so rbtrees are a better choice than prio tree for this use. They are both faster and have lower memory overhead.
Tested by booting a kernel with kmemleak enabled, loading the kmemleak_test module, and looking for the expected messages.
--- mm/kmemleak.c | 98 +++++++++++++++++++++++++++++---------------------------- 1 files changed, 50 insertions(+), 48 deletions(-)
mm/kmemleak.c +50 -48
@@ -29,7 +29,7 @@* - kmemleak_lock (rwlock): protects the object_list modifications and * accesses to the object_tree_root. The object_list is the main list * holding the metadata (struct kmemleak_object) for the allocated memory -* blocks. The object_tree_root is a priority search tree used to look-up +* blocks. The object_tree_root is a red black tree used to look-up * metadata based on a pointer to the corresponding memory block. The * kmemleak_object structures are added to the object_list and * object_tree_root in the create_object() function called from the
@@ -71,7 +71,7 @@#include <linux/delay.h> #include <linux/export.h> #include <linux/kthread.h> -#include <linux/prio_tree.h> +#include <linux/rbtree.h> #include <linux/fs.h> #include <linux/debugfs.h> #include <linux/seq_file.h>
@@ -132,7 +132,7 @@ struct kmemleak_scan_area {* Structure holding the metadata for each allocated memory block. * Modifications to such objects should be made while holding the * object->lock. Insertions or deletions from object_list, gray_list or -* tree_node are already protected by the corresponding locks or mutex (see +* rb_node are already protected by the corresponding locks or mutex (see * the notes on locking above). These objects are reference-counted * (use_count) and freed using the RCU mechanism. */
@@ -141,7 +141,7 @@ struct kmemleak_object {unsigned long flags;/* object status flags */ struct list_head object_list;struct list_head gray_list; -struct prio_tree_node tree_node; +struct rb_node rb_node;struct rcu_head rcu;/* object_list lockless traversal */ /* object usage count; object freed when use_count == 0 */ atomic_t use_count;
@@ -182,9 +182,9 @@ struct kmemleak_object {static LIST_HEAD (object_list);/* the list of gray-colored objects (see color_gray comment below) */ static LIST_HEAD (gray_list); -/* prio search tree for object boundaries */ -static struct prio_tree_root object_tree_root; -/* rw_lock protecting the access to object_list and prio_tree_root */ +/* search tree for object boundaries */ +static struct rb_root object_tree_root =RB_ROOT ; +/* rw_lock protecting the access to object_list and object_tree_root */ static DEFINE_RWLOCK (kmemleak_lock);/* allocation caches for kmemleak internal data */
@@ -380,7 +380,7 @@ static void dump_object_info(struct kmemleak_object *object) trace.entries = object->trace;pr_notice ("Object 0x%08lx (size %zu):\n" , - object->tree_node.start, object->size); + object->pointer, object->size);pr_notice (" comm \"%s\", pid %d, jiffies %lu\n" , object->comm, object->pid, object->jiffies);pr_notice (" min_count = %d\n" , object->min_count);
@@ -392,32 +392,32 @@ static void dump_object_info(struct kmemleak_object *object) }/* -* Look-up a memory block metadata (kmemleak_object) in the priority search +* Look-up a memory block metadata (kmemleak_object) in the object search * tree based on a pointer value. If alias is 0, only values pointing to the * beginning of the memory block are allowed. The kmemleak_lock must be held * when calling this function. */ static struct kmemleak_object *lookup_object (unsigned long ptr,int alias) { -struct prio_tree_node *node; -struct prio_tree_iter iter; -struct kmemleak_object *object; - -prio_tree_iter_init (&iter, &object_tree_root, ptr, ptr); - node =prio_tree_next (&iter); -if (node) { - object = prio_tree_entry(node, struct kmemleak_object, - tree_node); -if (!alias && object->pointer != ptr) { +struct rb_node *rb = object_tree_root.rb_node; + +while (rb) { +struct kmemleak_object *object = + rb_entry(rb, struct kmemleak_object, rb_node); +if (ptr < object->pointer) + rb = object->rb_node.rb_left; +else if (object->pointer + object->size <= ptr) + rb = object->rb_node.rb_right; +else if (object->pointer == ptr || alias) +return object; +else {kmemleak_warn ("Found object by alias at 0x%08lx\n" , ptr);dump_object_info (object); - object =NULL ; +break ; } - }else - object =NULL ; - -return object; + } +return NULL ; }/*
@@ -471,7 +471,7 @@ static void put_object(struct kmemleak_object *object) }/* -* Look up an object in the prio search tree and increase its use_count. +* Look up an object in the object search tree and increase its use_count. */ static struct kmemleak_object *find_and_get_object (unsigned long ptr,int alias) {
@@ -516,8 +516,8 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, int min_count, gfp_t gfp) {unsigned long flags; -struct kmemleak_object *object; -struct prio_tree_node *node; +struct kmemleak_object *object, *parent; +struct rb_node **link, *rb_parent; object =kmem_cache_alloc (object_cache,gfp_kmemleak_mask (gfp));if (!object) {
@@ -560,31 +560,34 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,/* kernel backtrace */ object->trace_len =__save_stack_trace (object->trace); -INIT_PRIO_TREE_NODE (&object->tree_node); - object->tree_node.start = ptr; - object->tree_node.last = ptr + size -1 ; -write_lock_irqsave (&kmemleak_lock, flags); min_addr =min (min_addr, ptr); max_addr =max (max_addr, ptr + size); - node =prio_tree_insert (&object_tree_root, &object->tree_node); -/* -* The code calling the kernel does not yet have the pointer to the -* memory block to be able to free it. However, we still hold the -* kmemleak_lock here in case parts of the kernel started freeing -* random memory blocks. -*/ -if (node != &object->tree_node) { -kmemleak_stop ("Cannot insert 0x%lx into the object search tree " -"(already existing)\n" , ptr); - object =lookup_object (ptr,1 ); -spin_lock (&object->lock); -dump_object_info (object); -spin_unlock (&object->lock); - goto out; + link = &object_tree_root.rb_node; + rb_parent =NULL ; +while (*link) { + rb_parent = *link; + parent = rb_entry(rb_parent, struct kmemleak_object, rb_node); +if (ptr + size <= parent->pointer) + link = &parent->rb_node.rb_left; +else if (parent->pointer + parent->size <= ptr) + link = &parent->rb_node.rb_right; +else { +kmemleak_stop ("Cannot insert 0x%lx into the object " +"search tree (overlaps existing)\n" , + ptr); + object = parent; +spin_lock (&object->lock); +dump_object_info (object); +spin_unlock (&object->lock); + goto out; + } } +rb_link_node (&object->rb_node, rb_parent, link); +rb_insert_color (&object->rb_node, &object_tree_root); +list_add_tail_rcu (&object->object_list, &object_list); out:write_unlock_irqrestore (&kmemleak_lock, flags);
@@ -600,7 +603,7 @@ static void __delete_object(struct kmemleak_object *object)unsigned long flags;write_lock_irqsave (&kmemleak_lock, flags); -prio_tree_remove (&object_tree_root, &object->tree_node); +rb_erase (&object->rb_node, &object_tree_root);list_del_rcu (&object->object_list);write_unlock_irqrestore (&kmemleak_lock, flags);
@@ -1768,7 +1771,6 @@ void __init kmemleak_init(void) object_cache =KMEM_CACHE (kmemleak_object,SLAB_NOLEAKTRACE ); scan_area_cache =KMEM_CACHE (kmemleak_scan_area,SLAB_NOLEAKTRACE ); -INIT_PRIO_TREE_ROOT (&object_tree_root);if (crt_early_log >=ARRAY_SIZE (early_log)) pr_warning("Early log buffer exceeded (%d), please increase "
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/5] kmemleak: use rbtree instead of prio tree
Forgot to add Catalin on this review...
---------- Forwarded message ----------
From: Michel Lespinasse <[email protected]>
Date: Tue, Aug 7, 2012 at 12:25 AM
Subject: [PATCH 3/5] kmemleak: use rbtree instead of prio tree
To: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Cc: [email protected], [email protected], [email protected]
kmemleak uses a tree where each node represents an allocated memory object in order to quickly find out what object a given address is part of. However, the objects don't overlap, so rbtrees are a better choice than prio tree for this use. They are both faster and have lower memory overhead.
Tested by booting a kernel with kmemleak enabled, loading the kmemleak_test module, and looking for the expected messages.
--- mm/kmemleak.c | 98 +++++++++++++++++++++++++++++---------------------------- 1 files changed, 50 insertions(+), 48 deletions(-)
mm/kmemleak.c +8 -8
@@ -29,7 +29,7 @@* - kmemleak_lock (rwlock): protects the object_list modifications and * accesses to the object_tree_root. The object_list is the main list * holding the metadata (struct kmemleak_object) for the allocated memory -* blocks. The object_tree_root is a priority search tree used to look-up +* blocks. The object_tree_root is a red black tree used to look-up * metadata based on a pointer to the corresponding memory block. The * kmemleak_object structures are added to the object_list and * object_tree_root in the create_object() function called from the
@@ -71,7 +71,7 @@#include <linux/delay.h> #include <linux/export.h> #include <linux/kthread.h> -#include <linux/prio_tree.h> +#include <linux/rbtree.h> #include <linux/fs.h> #include <linux/debugfs.h> #include <linux/seq_file.h>
@@ -132,7 +132,7 @@ struct kmemleak_scan_area {* Structure holding the metadata for each allocated memory block. * Modifications to such objects should be made while holding the * object->lock. Insertions or deletions from object_list, gray_list or -* tree_node are already protected by the corresponding locks or mutex (see +* rb_node are already protected by the corresponding locks or mutex (see * the notes on locking above). These objects are reference-counted * (use_count) and freed using the RCU mechanism. */
@@ -141,7 +141,7 @@ struct kmemleak_object {unsigned long flags;/* object status flags */ struct list_head object_list;struct list_head gray_list; -struct prio_tree_node tree_node; +struct rb_node rb_node;struct rcu_head rcu;/* object_list lockless traversal */ /* object usage count; object freed when use_count == 0 */ atomic_t use_count;
@@ -182,9 +182,9 @@ struct kmemleak_object {static LIST_HEAD (object_list);/* the list of gray-colored objects (see color_gray comment below) */ static LIST_HEAD (gray_list); -/* prio search tree for object boundaries */ -static struct prio_tree_root object_tree_root; -/* rw_lock protecting the access to object_list and prio_tree_root */ +/* search tree for object boundaries */ +static struct rb_root object_tree_root =RB_ROOT ; +/* rw_lock protecting the access to object_list and object_tree_root */ static DEFINE_RWLOCK (kmemleak_lock);/* allocation caches for kmemleak internal data */
@@ -380,7 +380,7 @@ static void dump_object_info(struct kmemleak_object *object) trace.entries = object->trace;pr_notice ("Object 0x%08lx (size %zu):\n" , - object->tree_node.start, object->size); + object->pointer, object->size);pr_notice (" comm \"%s\", pid %d, jiffies %lu\n" , object->comm, object->pid, object->jiffies);pr_notice (" min_count = %d\n" , object->min_count);
@@ -392,32 +392,32 @@ static void dump_object_info(struct
kmemleak_object *object)
}
/*
- * Look-up a memory block metadata (kmemleak_object) in the priority search
+ * Look-up a memory block metadata (kmemleak_object) in the object search
* tree based on a pointer value. If alias is 0, only values pointing to the
* beginning of the memory block are allowed. The kmemleak_lock must be held
* when calling this function.
*/
static struct kmemleak_object *lookup_object(unsigned long ptr, int alias) {
- struct prio_tree_node *node;
- struct prio_tree_iter iter;
- struct kmemleak_object *object;
-
- prio_tree_iter_init(&iter, &object_tree_root, ptr, ptr);
- node = prio_tree_next(&iter);
- if (node) {- object = prio_tree_entry(node, struct kmemleak_object,
- tree_node);
- if (!alias && object->pointer != ptr) {
+ struct rb_node *rb = object_tree_root.rb_node;
+
+ while (rb) {
+ struct kmemleak_object *object =
+ rb_entry(rb, struct kmemleak_object, rb_node);
+ if (ptr < object->pointer)
+ rb = object->rb_node.rb_left;
+ else if (object->pointer + object->size <= ptr)
+ rb = object->rb_node.rb_right;
+ else if (object->pointer == ptr || alias)
+ return object;
+ else {
kmemleak_warn("Found object by alias at 0x%08lx\n",
ptr);
dump_object_info(object);
- object = NULL;
+ break;
}
- } else
- object = NULL;
-
- return object;
+ }
+ return NULL;}
/*
@@ -471,7 +471,7 @@ static void put_object(struct kmemleak_object *object)
}
/*
- * Look up an object in the prio search tree and increase its use_count.
+ * Look up an object in the object search tree and increase its use_count. */
static struct kmemleak_object *find_and_get_object(unsigned long ptr,
int alias)
{
@@ -516,8 +516,8 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
int min_count, gfp_t gfp)
{
unsigned long flags;
- struct kmemleak_object *object;
- struct prio_tree_node *node;
+ struct kmemleak_object *object, *parent;
+ struct rb_node **link, *rb_parent; object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
if (!object) {
@@ -560,31 +560,34 @@ static struct kmemleak_object
*create_object(unsigned long ptr, size_t size,
/* kernel backtrace */
object->trace_len = __save_stack_trace(object->trace);- INIT_PRIO_TREE_NODE(&object->tree_node);
- object->tree_node.start = ptr;
- object->tree_node.last = ptr + size - 1;
-
write_lock_irqsave(&kmemleak_lock, flags);
min_addr = min(min_addr, ptr);
max_addr = max(max_addr, ptr + size);
- node = prio_tree_insert(&object_tree_root, &object->tree_node);
- /*
- * The code calling the kernel does not yet have the pointer to the
- * memory block to be able to free it. However, we still hold the
- * kmemleak_lock here in case parts of the kernel started freeing
- * random memory blocks.
- */
- if (node != &object->tree_node) {
- kmemleak_stop("Cannot insert 0x%lx into the object search tree "
- "(already existing)\n", ptr);
- object = lookup_object(ptr, 1);
- spin_lock(&object->lock);
- dump_object_info(object);
- spin_unlock(&object->lock);- goto out;
+ link = &object_tree_root.rb_node;
+ rb_parent = NULL;
+ while (*link) {
+ rb_parent = *link;
+ parent = rb_entry(rb_parent, struct kmemleak_object, rb_node);
+ if (ptr + size <= parent->pointer)+ link = &parent->rb_node.rb_left;
+ else if (parent->pointer + parent->size <= ptr)
+ link = &parent->rb_node.rb_right;
+ else {
+ kmemleak_stop("Cannot insert 0x%lx into the object "
+ "search tree (overlaps existing)\n",
+ ptr);
+ object = parent;
+ spin_lock(&object->lock);
+ dump_object_info(object);
+ spin_unlock(&object->lock);
+ goto out;
+ }
}
+ rb_link_node(&object->rb_node, rb_parent, link);
+ rb_insert_color(&object->rb_node, &object_tree_root);
+
list_add_tail_rcu(&object->object_list, &object_list);
out:
write_unlock_irqrestore(&kmemleak_lock, flags);
@@ -600,7 +603,7 @@ static void __delete_object(struct kmemleak_object *object)
unsigned long flags;
write_lock_irqsave(&kmemleak_lock, flags);
- prio_tree_remove(&object_tree_root, &object->tree_node);
+ rb_erase(&object->rb_node, &object_tree_root);
list_del_rcu(&object->object_list);
write_unlock_irqrestore(&kmemleak_lock, flags);@@ -1768,7 +1771,6 @@ void __init kmemleak_init(void)
object_cache = KMEM_CACHE(kmemleak_object, SLAB_NOLEAKTRACE);
scan_area_cache = KMEM_CACHE(kmemleak_scan_area, SLAB_NOLEAKTRACE);- INIT_PRIO_TREE_ROOT(&object_tree_root);
if (crt_early_log >= ARRAY_SIZE(early_log))
pr_warning("Early log buffer exceeded (%d), please increase "
--
1.7.7.3
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Catalin Marinas
maintainerRe: [PATCH 3/5] kmemleak: use rbtree instead of prio tree
Quoted 9 lines · Michel Lespinasse
kmemleak uses a tree where each node represents an allocated memory object in order to quickly find out what object a given address is part of. However, the objects don't overlap, so rbtrees are a better choice than prio tree for this use. They are both faster and have lower memory overhead.
Tested by booting a kernel with kmemleak enabled, loading the
kmemleak_testmodule, and looking for the expected messages.Signed-off-by Michel Lespinasse <[email protected]>
The patch looks fine to me but I'll give it a test later today and let you know.
Signature · 7 lines
Catalin -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Catalin Marinas
maintainerRe: [PATCH 3/5] kmemleak: use rbtree instead of prio tree
Quoted 6 lines · Catalin Marinas
Quoted 5 lines · Michel Lespinasse
kmemleak uses a tree where each node represents an allocated memory object in order to quickly find out what object a given address is part of. However, the objects don't overlap, so rbtrees are a better choice than prio tree for this use. They are both faster and have lower memory overhead.
Quoted 7 lines
Quoted 4 lines
Tested by booting a kernel with kmemleak enabled, loading the
kmemleak_testmodule, and looking for the expected messages.Signed-off-by Michel Lespinasse <[email protected]>The patch looks fine to me but I'll give it a test later today and let you know.
Couldn't test it because the patch got messed up somewhere on the email path (tabs replaced with spaces). Is there a Git tree I can grab it from (or you could just send it to me separately as attachment)?
Thanks,
Catalin
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/5] kmemleak: use rbtree instead of prio tree
Quoted 3 lines · Catalin Marinas
Couldn't test it because the patch got messed up somewhere on the email path (tabs replaced with spaces). Is there a Git tree I can grab it from (or you could just send it to me separately as attachment)?
Sorry about that. The original patch I sent to lkml & linux-mm wasn't corrupted, but the forward I sent you after I realized I had forgotten to include you was.
https://lkml.org/lkml/2012/8/7/52 has the original patch, and "get diff 1" in the left column can be used to retrieve it.
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 3/5] kmemleak: use rbtree instead of prio tree
Quoted 6 lines · Michel Lespinasse
Quoted 3 lines · Catalin Marinas
Couldn't test it because the patch got messed up somewhere on the email path (tabs replaced with spaces). Is there a Git tree I can grab it from (or you could just send it to me separately as attachment)?
Quoted 6 lines
Sorry about that. The original patch I sent to lkml & linux-mm wasn't corrupted, but the forward I sent you after I realized I had forgotten to include you was.
https://lkml.org/lkml/2012/8/7/52 has the original patch, and "get diff 1" in the left column can be used to retrieve it.
Thanks. It works fine in my tests and the scanning time seems to have got down from 22s to 19s on my board.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 4/5 prio_tree: remove
In reply to Michel LespinasseAfter both prio_tree users have been converted to use red-black trees, there is no need to keep around the prio tree library anymore.
--- Documentation/00-INDEX | 2 - Documentation/prio_tree.txt | 107 ---------- include/linux/prio_tree.h | 120 ------------ init/main.c | 2 - lib/Kconfig.debug | 6 - lib/Makefile | 3 +- lib/prio_tree.c | 455 ------------------------------------------- lib/prio_tree_test.c | 106 ---------- 8 files changed, 1 insertions(+), 800 deletions(-) delete mode 100644 Documentation/prio_tree.txt delete mode 100644 include/linux/prio_tree.h delete mode 100644 lib/prio_tree.c delete mode 100644 lib/prio_tree_test.c
Documentation/00-INDEX +0 -2
@@ -270,8 +270,6 @@ preempt-locking.txt - info on locking under a preemptive kernel. printk-formats.txt - how to get printk format specifiers right -prio_tree.txt - - info on radix-priority-search-tree use for indexing vmas. ramoops.txt - documentation of the ramoops oops/panic logging module. rbtree.txt
Documentation/prio_tree.txt deleted +0 -107
107 lines not on this page · raw message
include/linux/prio_tree.h deleted +0 -120
120 lines not on this page · raw message
init/main.c +0 -2
@@ -85,7 +85,6 @@ extern void init_IRQ(void);extern void fork_init (unsigned long );extern void mca_init (void );extern void sbus_init (void ); -extern void prio_tree_init (void );extern void radix_tree_init (void );#ifndef CONFIG_DEBUG_RODATA static inline void mark_rodata_ro (void ) { }
@@ -546,7 +545,6 @@ asmlinkage void __init start_kernel(void)/* init some links before init_ISA_irqs() */ early_irq_init ();init_IRQ (); -prio_tree_init ();init_timers ();hrtimers_init ();softirq_init ();
lib/Kconfig.debug +0 -6
@@ -1185,12 +1185,6 @@ config RBTREE_TEST A benchmark measuring the performance of the rbtree library. Also includes rbtree invariant checks. -config PRIO_TREE_TEST - tristate "Prio tree test" - depends on m && DEBUG_KERNEL - help - A benchmark measuring the performance of the prio tree library - config INTERVAL_TREE_TEST tristate "Interval tree test" depends on m && DEBUG_KERNEL
lib/Makefile +1 -2
@@ -9,7 +9,7 @@ endif lib-y := ctype.o string.o vsprintf.o cmdline.o \ rbtree.o radix-tree.o dump_stack.o timerqueue.o\ - idr.o int_sqrt.o extable.o prio_tree.o \ + idr.o int_sqrt.o extable.o \ sha1.o md5.o irq_regs.o reciprocal_div.o argv_split.o \ proportions.o prio_heap.o ratelimit.o show_mem.o \ is_single_threaded.o plist.o decompress.o
@@ -131,7 +131,6 @@ obj-$(CONFIG_GENERIC_STRNLEN_USER) += strnlen_user.o obj-$(CONFIG_STMP_DEVICE) += stmp_device.o obj-$(CONFIG_RBTREE_TEST) += rbtree_test.o -obj-$(CONFIG_PRIO_TREE_TEST) += prio_tree_test.o obj-$(CONFIG_INTERVAL_TREE_TEST) += interval_tree_test.o interval_tree_test-objs := interval_tree_test_main.o interval_tree.o
lib/prio_tree.c deleted +0 -455
455 lines not on this page · raw message
lib/prio_tree_test.c deleted +0 -106
106 lines not on this page · raw message
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 5/5 rbtree: move augmented rbtree functionality to rbtree_augmented.h
In reply to Michel LespinasseProvide rb_insert_augmented() and rb_erase_augmented through a new rbtree_augmented.h include file. rb_erase_augmented() is defined there as an __always_inline function, in order to allow inlining of augmented rbtree callbacks into it. Since this generates a relatively large function, each augmented rbtree users should make sure to have a single call site.
--- Documentation/rbtree.txt | 13 ++ arch/x86/mm/pat_rbtree.c | 2 +- include/linux/interval_tree_tmpl.h | 8 +- include/linux/rbtree.h | 48 -------- include/linux/rbtree_augmented.h | 223 ++++++++++++++++++++++++++++++++++++ lib/rbtree.c | 162 ++------------------------ lib/rbtree_test.c | 2 +- 7 files changed, 255 insertions(+), 203 deletions(-) create mode 100644 include/linux/rbtree_augmented.h
Documentation/rbtree.txt +13 -0
@@ -202,6 +202,14 @@ An rbtree user who wants this feature will have to call the augmentation functions with the user provided augmentation callback when inserting and erasing nodes. +C files implementing augmented rbtree manipulation must include +<linux/rbtree_augmented.h> instead of <linus/rbtree.h>. Note that +linux/rbtree_augmented.h exposes some rbtree implementations details +you are not expected to rely on; please stick to the documented APIs +there and do not include <linux/rbtree_augmented.h> from header files +either so as to minimize chances of your users accidentally relying on +such implementation details. + On insertion, the user must update the augmented information on the path leading to the inserted node, then call rb_link_node() as usual and rb_augment_inserted() instead of the usual rb_insert_color() call.
@@ -227,6 +235,11 @@ In both cases, the callbacks are provided through struct rb_augment_callbacks. subtree to a newly assigned subtree root AND recomputes the augmented information for the former subtree root. +The compiled code for rb_erase_augmented() may inline the propagation and +copy callbacks, which results in a large function, so each augmented rbtree +user should have a single rb_erase_augmented() call site in order to limit +compiled code size. + Sample usage:
arch/x86/mm/pat_rbtree.c +1 -1
@@ -12,7 +12,7 @@#include <linux/debugfs.h> #include <linux/kernel.h> #include <linux/module.h> -#include <linux/rbtree.h> +#include <linux/rbtree_augmented.h> #include <linux/sched.h> #include <linux/gfp.h>
include/linux/interval_tree_tmpl.h +6 -2
@@ -19,6 +19,8 @@include/linux/interval_tree_tmpl.h */ +#include <linux/rbtree_augmented.h> +/* * Template for implementing interval trees *
@@ -57,7 +59,8 @@ static inline ITTYPE IT(compute_subtree_last)(ITSTRUCT *node)return max; } -static void IT (augment_propagate )(struct rb_node *rb,struct rb_node *stop) +static inline void +IT (augment_propagate )(struct rb_node *rb,struct rb_node *stop) {while (rb != stop) {ITSTRUCT *node =rb_entry (rb,ITSTRUCT ,ITRB );
@@ -69,7 +72,8 @@ static void IT(augment_propagate)(struct rb_node *rb, struct rb_node *stop) } } -static void IT (augment_copy )(struct rb_node *rb_old,struct rb_node *rb_new) +static inline void +IT (augment_copy )(struct rb_node *rb_old,struct rb_node *rb_new) {ITSTRUCT *old =rb_entry (rb_old,ITSTRUCT ,ITRB );ITSTRUCT *new =rb_entry (rb_new,ITSTRUCT ,ITRB );
include/linux/rbtree.h +0 -48
54 lines not on this page · raw message
include/linux/rbtree_augmented.h new file +223 -0
223 lines not on this page · raw message
lib/rbtree.c +11 -151
243 lines not on this page · raw message
lib/rbtree_test.c +1 -1
@@ -1,5 +1,5 @@#include <linux/module.h> -#include <linux/rbtree.h> +#include <linux/rbtree_augmented.h> #include <linux/random.h> #include <asm/timex.h>
Signature · 7 lines
1.7.7.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 5/5] rbtree: move augmented rbtree functionality to rbtree_augmented.h
Quoted 6 lines · Michel Lespinasse
Provide
rb_insert_augmented()andrb_erase_augmentedthrough a newrbtree_augmented.hinclude file.rb_erase_augmented()is defined there as an__always_inlinefunction, in order to allow inlining of augmented rbtree callbacks into it. Since this generates a relatively large function, each augmented rbtree users should make sure to have a single call site.
I should probably add this to show how the code generation works out in practice (with a gcc 4.6 based compiler)
Before this change:
text data bss dec hex filename
3426 0 0 3426 d62 lib/rbtree.o
0000000000000af0 g F .text 000000000000001e rb_last
0000000000000b80 g F .text 0000000000000047 rb_next
0000000000000000 g F .text 0000000000000165 rb_insert_color
0000000000000bd0 g F .text 0000000000000047 rb_prev
00000000000004e0 g F .text 00000000000001cd __rb_insert_augmented
0000000000000170 g F .text 000000000000036f rb_erase
00000000000006b0 g F .text 0000000000000416 rb_erase_augmented
0000000000000ad0 g F .text 000000000000001e rb_first
0000000000000b10 g F .text 000000000000006e rb_replace_node
text data bss dec hex filename
540 0 0 540 21c lib/interval_tree.o
0000000000000000 l F .text 0000000000000054
interval_tree_augment_propagate
0000000000000060 l F .text 000000000000000e interval_tree_augment_copy
0000000000000070 l F .text 000000000000003e
interval_tree_augment_rotate
00000000000000b0 g F .text 0000000000000065 interval_tree_insert
0000000000000120 g F .text 0000000000000012 interval_tree_remove
0000000000000140 g F .text 000000000000004c interval_tree_iter_first
0000000000000190 g F .text 0000000000000074 interval_tree_iter_nextAfter this change:
text data bss dec hex filename
2976 0 0 2976 ba0 lib/rbtree.o
0000000000000000 g F .text 000000000000025c __rb_erase_color
0000000000000930 g F .text 000000000000001e rb_last
00000000000009c0 g F .text 0000000000000047 rb_next
0000000000000260 g F .text 0000000000000165 rb_insert_color
0000000000000a10 g F .text 0000000000000047 rb_prev
0000000000000740 g F .text 00000000000001cd __rb_insert_augmented
00000000000003d0 g F .text 000000000000036f rb_erase
0000000000000910 g F .text 000000000000001e rb_first
0000000000000950 g F .text 000000000000006e rb_replace_node
text data bss dec hex filename
900 0 0 900 384 lib/interval_tree.o
0000000000000000 l F .text 000000000000003e
interval_tree_augment_rotate
0000000000000040 g F .text 0000000000000065 interval_tree_insert
00000000000000b0 g F .text 000000000000020b interval_tree_remove
00000000000002c0 g F .text 000000000000004c interval_tree_iter_first
0000000000000310 g F .text 0000000000000074 interval_tree_iter_nextSo the code size effect is that the library code for augmented erase shrinks by 450 bytes, and each augmented rb_erase user grows by (well, this will very between users, but I think interval tree is representative of a typical user) ~360 bytes. The rotate callback is generated as a static function so it can be passed by pointer to the library rebalancing routines; the copy and propagate functions are inlined into interval_tree_remove (and the compiler is able to determine that there are no remaining non-inlined calls, so it doesn't generate an additional static definition).
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Peter Zijlstra
maintainerQuoted 7 lines · Michel Lespinasse
a faster worst-case complexity of O(k+log N) for stabbing queries in a well-balanced prio tree, vs
O(k*log N)for interval trees (where k=number of matches, N=number of intervals). Now this sounds great, but in practice prio trees don't realize this theorical benefit. First, the additional constraint makes them harder to update, so that the kernel implementation has to simplify things by balancing them like a radix tree, which is not always ideal.
Not something spending a great deal of time on, but do you have any idea what the radix like balancing does the the worst case stabbing complexity?
Anyway, I like the thing, that prio-tree code always made my head hurt.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Michel Lespinasse
a faster worst-case complexity of O(k+log N) for stabbing queries in a well-balanced prio tree, vs
O(k*log N)for interval trees (where k=number of matches, N=number of intervals). Now this sounds great, but in practice prio trees don't realize this theorical benefit. First, the additional constraint makes them harder to update, so that the kernel implementation has to simplify things by balancing them like a radix tree, which is not always ideal.
Quoted 3 lines
Not something spending a great deal of time on, but do you have any idea what the radix like balancing does the the worst case stabbing complexity?
Well, it's not terribly bad as the height of the radix tree is still bounded by (IIRC) log(ULONG_MAX) + log(max pgoff_t in the file). So the complexity ends up being O(k + some constant) where the constant is in practice always larger than log(N).
One can still construct cases where a prio tree stabbing query would read less nodes (so touch less cache lines) than with augmented rbtree. I think the best way to explain this is to look at what augmented rbtree does - for a stabbing query returning k matches, it visits all nodes between the root and the k matching nodes (plus one extra path at the end to conclude that there are no further matches). So the worst case there would be if the matching nodes are all close to leaf level in the tree, and if the paths leading to them branch up close to the root level. It's easy to construct such a case by making all N nodes in the tree start to the left of the stabbing query, and having only an arbitrary k among them end to the right of the query. Prio tree achieves a better worst case complexity by placing these k nodes near the top of the tree (due to the heap constraint). But this worst case situation isn't very likely to begin with, and in the typical case the k paths leading to the matching nodes in an augmented rbtree don't branch up so close to the root so typically prio tree doesn't get an advantage there.
To sum it up, I would say that both algorithms are really pretty good, with prio tree getting a slightly better theorical worst-case and augmented rbtree getting (IMO) a practical advantage due to lower constant factors and a still very acceptable worst case.
Quoted 1 line
Anyway, I like the thing, that prio-tree code always made my head hurt.
Black magic I tell you ;-)
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Andrew Morton
maintainerQuoted 3 lines · On Tue, 7 Aug 2012 00:25:38 -0700 Michel Lespinasse <[email protected]> wrote:
This patchset goes over the rbtree changes that have been already integrated into Andrew's -mm tree, as well as the augmented rbtree proposal which is currently pending.
hm. Well I grabbed these for a bit of testing.
It's a large change in MM and it depends on code which hasn't yet been merged in mainline. It's probably prudent to do all this in two steps
- we'll see.
It would good to have solid acknowledgement from Rik that this approach does indeed suit his pending vma changes.
The templates-with-CPP thing is not terribly appealing. It's not obvious that it really needed to be done this way - we've avoided it in plenty of other places. It would be nice to see that alternatives have been thoroughly explored, and why they were rejected.
AFAICT the code will work OK when expanding macros which reference their arguments multiple times. For example, interval_tree.c has
#define ITLAST(n) ((n)->vm_pgoff + \ (((n)->vm_end - (n)->vm_start) >> PAGE_SHIFT) - 1)
which will explode if passed "foo++". Things like that.
The code uses the lame-and-useless "inline" absolutely all over the place. I do think that for new code it would be better to get down and actually make proper engineering decisions about which functions should be inlined and mark them __always_inline.
Hillf has made a review suggestion which AFAICT remains unresponded to.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 2 lines · Andrew Morton
It would good to have solid acknowledgement from Rik that this approach does indeed suit his pending vma changes.
It does. Michel's rbtree rework is exactly what I need.
I do not need the interval tree bits, but the faster augmented rbtree is required for my vma changes to no longer have the performance regression Johannes measured with a kernel build.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 7 lines · Andrew Morton
Quoted 3 lines · On Tue, 7 Aug 2012 00:25:38 -0700 Michel Lespinasse <[email protected]> wrote:
This patchset goes over the rbtree changes that have been already integrated into Andrew's -mm tree, as well as the augmented rbtree proposal which is currently pending.
Quoted 5 lines
hm. Well I grabbed these for a bit of testing.
It's a large change in MM and it depends on code which hasn't yet been merged in mainline. It's probably prudent to do all this in two steps
- we'll see.
Makes sense to me. If we want to split the series as they get sent upstream, I would suggest sending all the rbtree and augmented rbtree infrastructure first, and then the rbtree usages (prio tree replacement, anon rmap interval tree which I'm going to send next, and rik's augmented rbtree based vma gap finder) in the next kernel.
Quoted 4 lines
The templates-with-CPP thing is not terribly appealing. It's not obvious that it really needed to be done this way - we've avoided it in plenty of other places. It would be nice to see that alternatives have been thoroughly explored, and why they were rejected.
I am actually wondering if the interval_tree_tmpl.h include file shouldn't be done as one large preprocessor #define instead. The ITSTRUCT, ITRB, etc... definitions would then become arguments to that large definition. It would also be possible to break up that #define into smaller ones - most likely, one for insertion, one for removal, and one for the subtree_search / iter_first / iter_next functions. Do you think this might help ?
I don't really see other workable alternatives that don't involve code replication.
Quoted 4 lines
The code uses the lame-and-useless "inline" absolutely all over the place. I do think that for new code it would be better to get down and actually make proper engineering decisions about which functions should be inlined and mark them
__always_inline.
You mentionned this before, but I'm not convinced that __always_inline would be better. The kernel is full of 2-line functions that we really want inlined, and I don't see what the value would be in converting these all to __always_inline. I am tempted to stick with the current usage, which I understand as being:
- use inline when the programmer believes a function should be inlined (this includes the static inline functions in header files - if the programmer didn't believe this should be inlined, he wouldn't put the function in a header file)
- use __always_inline if the function absolutely needs to be inlined for correct operation (I believe scheduler has some of these, which need to be included in the parent in order to end up in the correct section), OR in rare cases if the compiler is known to generate bad code with a mere inline and the programmer wants to force the issue.
I would also note that replacing inline with __always_inline is not a no-op change, even when the compiler was already inlining the original (marked inline) function. Sometimes the generated code ends up being different with __always_inline and I would rather not apply these changes blindly.
Quoted 1 line
Hillf has made a review suggestion which AFAICT remains unresponded to.
To be honest, I wasn't quite sure what he was suggesting ?
Signature · 8 lines
Michel "Walken" Lespinasse A program is never fully debugged until the last user dies. -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>