Fix radix_tree_lookup_slot()
Subsystems (6)
cover Fix radix_tree_lookup_slot()
From: Matthew Wilcox <[email protected]>
Hi Linus,
Please apply for 4.8. The same bug is also present in 4.7, but is probably latent.
Matthew Wilcox (2):
radix tree test suite: Test radix_tree_replace_slot() for multiorder
entries
radix-tree: Fix optimisation problem
lib/radix-tree.c | 3 ++- tools/testing/radix-tree/Makefile | 2 +- tools/testing/radix-tree/multiorder.c | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-)
Signature · 7 lines
2.9.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/2 radix-tree: Fix optimisation problem
From: Matthew Wilcox <[email protected]>
When compiling the radix tree with -O2, GCC thinks it can optimise:
void *entry = parent->slots[offset]; int siboff = entry - parent->slots; void *slot = parent->slots + siboff;
into
void *slot = entry;
Unfortunately, 'entry' is a tagged pointer, so this optimisation leads to getting an unaligned pointer back from radix_tree_lookup_slot(). The test suite wasn't being compiled with optimisation, so we hadn't spotted it before now. Change the test suite to compile with -O2, and fix the optimisation problem by passing 'entry' through entry_to_node() so gcc knows this isn't a plain pointer.
--- lib/radix-tree.c | 3 ++- tools/testing/radix-tree/Makefile | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-)
lib/radix-tree.c +2 -1
@@ -105,7 +105,8 @@ static unsigned int radix_tree_descend(struct radix_tree_node *parent,#ifdef CONFIG_RADIX_TREE_MULTIORDER if (radix_tree_is_internal_node (entry)) { -unsigned long siboff =get_slot_offset (parent, entry); +unsigned long siboff =get_slot_offset (parent, + (void **)entry_to_node (entry));if (siboff <RADIX_TREE_MAP_SIZE ) { offset = siboff; entry =rcu_dereference_raw (parent->slots[offset]);
tools/testing/radix-tree/Makefile +1 -1
@@ -1,5 +1,5 @@ -CFLAGS += -I. -g -Wall -D_LGPL_SOURCE +CFLAGS += -I. -g -O2 -Wall -D_LGPL_SOURCE LDFLAGS += -lpthread -lurcu TARGETS = main OFILES = main.o radix-tree.o linux.o test.o tag_check.o find_next_bit.o \
Signature · 7 lines
2.9.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/2 radix tree test suite: Test radix_tree_replace_slot() for multiorder entries
In reply to Matthew WilcoxFrom: Matthew Wilcox <[email protected]>
When we replace a multiorder entry, check that all indices reflect the new value.
--- tools/testing/radix-tree/multiorder.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
tools/testing/radix-tree/multiorder.c +12 -4
@@ -124,6 +124,8 @@ static void multiorder_check(unsigned long index, int order)unsigned long i;unsigned long min = index & ~((1UL << order) -1 );unsigned long max = min + (1UL << order); +void **slot; +struct item *item2 =item_create (min);RADIX_TREE (tree,GFP_KERNEL );printf ("Multiorder index %ld, order %d\n" , index, order);
@@ -139,13 +141,19 @@ static void multiorder_check(unsigned long index, int order)item_check_absent (&tree, i);for (i = max; i <2 *max; i++)item_check_absent (&tree, i); +for (i = min; i < max; i++) +assert (radix_tree_insert (&tree, i, item2) == -EEXIST ); + + slot =radix_tree_lookup_slot (&tree, index); +free (*slot); +radix_tree_replace_slot (slot, item2);for (i = min; i < max; i++) { -static void *entry = (void *) - (0xA0 |RADIX_TREE_EXCEPTIONAL_ENTRY ); -assert (radix_tree_insert (&tree, i, entry) == -EEXIST ); +struct item *item =item_lookup (&tree, i); +assert (item !=0 ); +assert (item->index == min); } -assert (item_delete (&tree, index) !=0 ); +assert (item_delete (&tree, min) !=0 );for (i =0 ; i <2 *max; i++)item_check_absent (&tree, i);
Signature · 7 lines
2.9.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/2] radix-tree: Fix optimisation problem
In reply to Matthew WilcoxQuoted 4 lines · Matthew Wilcox
Change the test suite to compile with -O2, and
fix the optimisation problem by passing 'entry' throughentry_to_node()so gcc knows this isn't a plain pointer.
Ugh. I really don't like this patch very much.
Wouldn't it be cleaner to just fix "get_slot_offset()" instead? As it is, looking at the code, I suspect that it's really hard to convince people that there isn't some other place this might happen. Because the "pointer subtraction followed by pointer addition" pattern is all hidden in these inline functions.
Or at least add a big comment about why this is the only such case.
Because without that, the code now looks very bad.
Linus
--
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/2] radix-tree: Fix optimisation problem
From: [email protected] [mailto:[email protected]] On Behalf Of Linus Torvalds
Quoted 15 lines
Quoted 4 lines · Matthew Wilcox
Change the test suite to compile with -O2, and
fix the optimisation problem by passing 'entry' throughentry_to_node()so gcc knows this isn't a plain pointer.Ugh. I really don't like this patch very much.
Wouldn't it be cleaner to just fix "
get_slot_offset()" instead? As it is, looking at the code, I suspect that it's really hard to convince people that there isn't some other place this might happen. Because the "pointer subtraction followed by pointer addition" pattern is all hidden in these inline functions.
Quoted 3 lines
Or at least add a big comment about why this is the only such case.
Because without that, the code now looks very bad.
That's fair. I looked at all the other callers of get_slot_offset, and all the others are using a real slot pointer. radix_tree_descend() really is the outlier here. I think the real problem is that the types in the tree are wrong; instead of storing void *, we should be storing uintptr_t. But fixing that is a little beyond the scope of -rc8. Here's a slightly better version which asserts that the passed pointer really is a pointer.
(attached as well, I have no idea whether this patch will get mangled)
lib/radix-tree.c +10 -3
@@ -91,9 +91,15 @@ static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node) }#endif +/* +* The slot pointer must be a real pointer as GCC will optimise +* through inlined functions and may deduce that +* parent->slots + get_slot_offset(parent, slot) == slot +*/ static inline unsigned long get_slot_offset (struct radix_tree_node *parent,void **slot) { +BUG_ON (radix_tree_exception (slot));return slot - parent->slots; }
@@ -101,11 +107,12 @@ static unsigned int radix_tree_descend(struct radix_tree_node *parent, struct radix_tree_node **nodep, unsigned long index) {unsigned int offset = (index >> parent->shift) &RADIX_TREE_MAP_MASK ; -void **entry =rcu_dereference_raw (parent->slots[offset]); +void *entry =rcu_dereference_raw (parent->slots[offset]);#ifdef CONFIG_RADIX_TREE_MULTIORDER if (radix_tree_is_internal_node (entry)) { -unsigned long siboff =get_slot_offset (parent, entry); +unsigned long siboff =get_slot_offset (parent, + (void **)entry_to_node (entry));if (siboff <RADIX_TREE_MAP_SIZE ) { offset = siboff; entry =rcu_dereference_raw (parent->slots[offset]);
@@ -113,7 +120,7 @@ static unsigned int radix_tree_descend(struct radix_tree_node *parent, }#endif - *nodep = (void *)entry; + *nodep = entry;return offset; }
tools/testing/radix-tree/Makefile +1 -1
@@ -1,5 +1,5 @@ -CFLAGS += -I. -g -Wall -D_LGPL_SOURCE +CFLAGS += -I. -g -O2 -Wall -D_LGPL_SOURCE LDFLAGS += -lpthread -lurcu TARGETS = main OFILES = main.o radix-tree.o linux.o test.o tag_check.o find_next_bit.o \
lib/radix-tree.c +10 -3
@@ -91,9 +91,15 @@ static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node) }#endif +/* +* The slot pointer must be a real pointer as GCC will optimise +* through inlined functions and may deduce that +* parent->slots + get_slot_offset(parent, slot) == slot +*/ static inline unsigned long get_slot_offset (struct radix_tree_node *parent,void **slot) { +BUG_ON (radix_tree_exception (slot));return slot - parent->slots; }
@@ -101,11 +107,12 @@ static unsigned int radix_tree_descend(struct radix_tree_node *parent, struct radix_tree_node **nodep, unsigned long index) {unsigned int offset = (index >> parent->shift) &RADIX_TREE_MAP_MASK ; -void **entry =rcu_dereference_raw (parent->slots[offset]); +void *entry =rcu_dereference_raw (parent->slots[offset]);#ifdef CONFIG_RADIX_TREE_MULTIORDER if (radix_tree_is_internal_node (entry)) { -unsigned long siboff =get_slot_offset (parent, entry); +unsigned long siboff =get_slot_offset (parent, + (void **)entry_to_node (entry));if (siboff <RADIX_TREE_MAP_SIZE ) { offset = siboff; entry =rcu_dereference_raw (parent->slots[offset]);
@@ -113,7 +120,7 @@ static unsigned int radix_tree_descend(struct radix_tree_node *parent, }#endif - *nodep = (void *)entry; + *nodep = entry;return offset; }
tools/testing/radix-tree/Makefile +1 -1
@@ -1,5 +1,5 @@ -CFLAGS += -I. -g -Wall -D_LGPL_SOURCE +CFLAGS += -I. -g -O2 -Wall -D_LGPL_SOURCE LDFLAGS += -lpthread -lurcu TARGETS = main OFILES = main.o radix-tree.o linux.o test.o tag_check.o find_next_bit.o \
Re: [PATCH 2/2] radix-tree: Fix optimisation problem
In reply to Matthew WilcoxQuoted 28 lines · Matthew Wilcox
From: Matthew Wilcox <[email protected]>
When compiling the radix tree with -O2, GCC thinks it can optimise:
void *entry = parent->slots[offset]; int siboff = entry - parent->slots; void *slot = parent->slots + siboff;into
void *slot = entry;Unfortunately, 'entry' is a tagged pointer, so this optimisation leads to getting an unaligned pointer back from
radix_tree_lookup_slot(). The test suite wasn't being compiled with optimisation, so we hadn't spotted it before now. Change the test suite to compile with -O2, and fix the optimisation problem by passing 'entry' throughentry_to_node()so gcc knows this isn't a plain pointer.--- lib/radix-tree.c | 3 ++- tools/testing/radix-tree/Makefile | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-)lib/radix-tree.c +0
-0
lib/radix-tree.c static unsigned int radix_tree_descend(struct radix_tree_node *parent, · line 109
Quoted 6 lines
lib/radix-tree.c +2
-1@@ -105,7 +105,8 @@ static unsigned int radix_tree_descend(struct radix_tree_node *parent,#ifdef CONFIG_RADIX_TREE_MULTIORDER if (radix_tree_is_internal_node (entry)) { -unsigned long siboff =get_slot_offset (parent, entry); +unsigned long siboff =get_slot_offset (parent, + (void **)entry_to_node (entry));
As I see this is the only place where get_slot_offset used for unaligned pointer.
Nobody uses "multiorder entries" so this never happens. And I have plan to kill this code.
Quoted 15 lines
lib/radix-tree.c +0
-0if (siboff <RADIX_TREE_MAP_SIZE ) { offset = siboff; entry =rcu_dereference_raw (parent->slots[offset]);tools/testing/radix-tree/Makefile +1
-2@@ -1,5 +1,5 @@ -CFLAGS += -I. -g -Wall -D_LGPL_SOURCE +CFLAGS += -I. -g -O2 -Wall -D_LGPL_SOURCE LDFLAGS += -lpthread -lurcu TARGETS = main OFILES = main.o radix-tree.o linux.o test.o tag_check.o find_next_bit.o \ --
Quoted 2 lines
2.9.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/2] radix-tree: Fix optimisation problem
In reply to Matthew Wilcoxlib/radix-tree.c static unsigned int radix_tree_descend(struct radix_tree_node *parent, · line 115
Quoted 6 lines · Matthew Wilcox
#ifdef CONFIG_RADIX_TREE_MULTIORDER if (radix_tree_is_internal_node(entry)) {lib/radix-tree.c +2
-1-unsigned long siboff =get_slot_offset (parent, entry); +unsigned long siboff =get_slot_offset (parent, + (void **)entry_to_node (entry));
I feel that it is *this* part that I think needs a huge honking comment.
If you are going to make get_slot_offset() different, then you could just rewrite get_slot_offset() to do
unsigned long diff = (unsigned long) slot - (unsigned
long)parent->slots;
return diff / sizeof(void *);and add a comment to say "don't do this as a pointer diff, because 'slot' may not be an aligned pointer". No BUG_ON() necessary, because it "just works".
At that point, gcc should just generate the right code, because it doesn't see it as a pointer subtraction followed by a pointer addition.
And yes, that crazy " (void **)entry_to_node(entry)" fixes it *too*, but it needs a *comment*.
Why is that special, when all the other uses of get_slot_offset() don't have that? *That* is what should be explained. Not some internal detail.
That said, if this code isn't even used, as Konstantin says (THP selects it - doesn't THP use it?), then the fix really should be to just remove the odd code instead of adding to it.
Looking around for uses that set "order" to anything but zero, I really don't see it. So maybe we should just do *that* trivial thing instead, and remove CONFIG_RADIX_TREE_MULTIORDER, since it's appears to be buggy and always has been.
Linus
--
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/2] radix-tree: Fix optimisation problem
Quoted 9 lines · Linus Torvalds
That said, if this code isn't even used, as Konstantin says (THP selects it - doesn't THP use it?), then the fix really should be to just remove the odd code instead of adding to it.
Looking around for uses that set "order" to anything but zero, I really don't see it. So maybe we should just do *that* trivial thing instead, and remove
CONFIG_RADIX_TREE_MULTIORDER, since it's appears to be buggy and always has been.
IOW, a patch something like this?
NOTE! This is entirely untested. Things still seem to compile with it, at least with some configurations. That's all I can say.
I do like this part:
11 files changed, 29 insertions(+), 518 deletions(-)
although admittedly 2/3rds of the deletions were for the multiorder tests. But even if you ignore the test side, it's just fairly clean removal of code that is apparently not used, and that was buggy.
Linus
include/linux/radix-tree.h | 48 +--- lib/Kconfig | 3 - lib/radix-tree.c | 100 +------- mm/Kconfig | 1 - mm/filemap.c | 2 +- tools/testing/radix-tree/Makefile | 2 +- tools/testing/radix-tree/generated/autoconf.h | 1 - tools/testing/radix-tree/main.c | 34 +-- tools/testing/radix-tree/multiorder.c | 337 -------------------------- tools/testing/radix-tree/test.c | 14 +- tools/testing/radix-tree/test.h | 5 - 11 files changed, 29 insertions(+), 518 deletions(-)
include/linux/radix-tree.h +4 -44
@@ -263,15 +263,9 @@ static inline void radix_tree_replace_slot(void **pslot, void *item) }int __radix_tree_create (struct radix_tree_root *root,unsigned long index, -unsigned order,struct radix_tree_node **nodep, +struct radix_tree_node **nodep,void ***slotp); -int __radix_tree_insert (struct radix_tree_root *,unsigned long index, -unsigned order,void *); -static inline int radix_tree_insert (struct radix_tree_root *root, -unsigned long index,void *entry) -{ -return __radix_tree_insert (root, index,0 , entry); -} +int radix_tree_insert (struct radix_tree_root *,unsigned long index,void *);void *__radix_tree_lookup (struct radix_tree_root *root,unsigned long index,struct radix_tree_node **nodep,void ***slotp);void *radix_tree_lookup (struct radix_tree_root *,unsigned long );
@@ -338,20 +332,8 @@ struct radix_tree_iter {unsigned long index;unsigned long next_index;unsigned long tags; -#ifdef CONFIG_RADIX_TREE_MULTIORDER -unsigned int shift; -#endif }; -static inline unsigned int iter_shift (struct radix_tree_iter *iter) -{ -#ifdef CONFIG_RADIX_TREE_MULTIORDER -return iter->shift; -#else -return 0 ; -#endif -} -#define RADIX_TREE_ITER_TAG_MASK 0x00FF/* tag index in lower byte */ #define RADIX_TREE_ITER_TAGGED 0x0100/* lookup tagged slots */ #define RADIX_TREE_ITER_CONTIG 0x0200/* stop at first hole */
@@ -415,7 +397,7 @@ void **radix_tree_iter_retry(struct radix_tree_iter *iter)static inline unsigned long __radix_tree_iter_add (struct radix_tree_iter *iter,unsigned long slots) { -return iter->index + (slots <<iter_shift (iter)); +return iter->index + slots; }/**
@@ -443,7 +425,7 @@ void **radix_tree_iter_next(struct radix_tree_iter *iter)static __always_inline long radix_tree_chunk_size (struct radix_tree_iter *iter) { -return (iter->next_index - iter->index) >>iter_shift (iter); +return iter->next_index - iter->index; }static inline struct radix_tree_node *entry_to_node (void *ptr)
@@ -466,22 +448,9 @@ static __always_inline void ** radix_tree_next_slot(void **slot, struct radix_tree_iter *iter, unsigned flags) {if (flags &RADIX_TREE_ITER_TAGGED ) { -void *canon = slot; - iter->tags >>=1 ;if (unlikely (!iter->tags))return NULL ; -while (IS_ENABLED (CONFIG_RADIX_TREE_MULTIORDER ) && -radix_tree_is_internal_node (slot[1 ])) { -if (entry_to_node (slot[1 ]) == canon) { - iter->tags >>=1 ; - iter->index =__radix_tree_iter_add (iter,1 ); - slot++; -continue ; - } - iter->next_index =__radix_tree_iter_add (iter,1 ); -return NULL ; - }if (likely (iter->tags &1ul )) { iter->index =__radix_tree_iter_add (iter,1 );return slot +1 ;
@@ -495,20 +464,11 @@ radix_tree_next_slot(void **slot, struct radix_tree_iter *iter, unsigned flags) } }else {long count =radix_tree_chunk_size (iter); -void *canon = slot;while (--count >0 ) { slot++; iter->index =__radix_tree_iter_add (iter,1 ); -if (IS_ENABLED (CONFIG_RADIX_TREE_MULTIORDER ) && -radix_tree_is_internal_node (*slot)) { -if (entry_to_node (*slot) == canon) -continue ; - iter->next_index = iter->index; -break ; - } -if (likely (*slot))return slot;if (flags &RADIX_TREE_ITER_CONTIG ) {
lib/Kconfig +0 -3
@@ -362,9 +362,6 @@ config INTERVAL_TREE for more information. -config RADIX_TREE_MULTIORDER - bool - config ASSOCIATIVE_ARRAY bool help
lib/radix-tree.c +9 -91
213 lines not on this page · raw message
mm/Kconfig +0 -1
@@ -412,7 +412,6 @@ config TRANSPARENT_HUGEPAGE bool "Transparent Hugepage Support" depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE select COMPACTION - select RADIX_TREE_MULTIORDER help Transparent Hugepages allows the kernel to use huge pages and huge tlb transparently to the applications whenever possible.
mm/filemap.c +1 -1
@@ -591,7 +591,7 @@ static int page_cache_tree_insert(struct address_space *mapping,void **slot;int error; - error =__radix_tree_create (&mapping->page_tree, page->index,0 , + error =__radix_tree_create (&mapping->page_tree, page->index, &node, &slot);if (error)return error;
tools/testing/radix-tree/Makefile +1 -1
@@ -3,7 +3,7 @@ CFLAGS += -I. -g -Wall -D_LGPL_SOURCE LDFLAGS += -lpthread -lurcu TARGETS = main OFILES = main.o radix-tree.o linux.o test.o tag_check.o find_next_bit.o \ - regression1.o regression2.o regression3.o multiorder.o + regression1.o regression2.o regression3.o targets: $(TARGETS)
tools/testing/radix-tree/generated/autoconf.h +0 -1
@@ -1,3 +1,2 @@ -#define CONFIG_RADIX_TREE_MULTIORDER 1#define CONFIG_SHMEM 1#define CONFIG_SWAP 1
tools/testing/radix-tree/main.c +13 -21
@@ -232,18 +232,17 @@ void copy_tag_check(void)item_kill_tree (&tree); } -static void __locate_check (struct radix_tree_root *tree,unsigned long index, -unsigned order) +static void __locate_check (struct radix_tree_root *tree,unsigned long index) {struct item *item;unsigned long index2; -item_insert_order (tree, index, order); +item_insert (tree, index); item =item_lookup (tree, index); index2 =radix_tree_locate_item (tree, item);if (index != index2) { -printf ("index %ld order %d inserted; found %ld\n" , - index, order, index2); +printf ("index %ld inserted; found %ld\n" , + index, index2);abort (); } }
@@ -254,7 +253,7 @@ static void __order_0_locate_check(void)int i;for (i =0 ; i <50 ; i++) -__locate_check (&tree,rand () %INT_MAX ,0 ); +__locate_check (&tree,rand () %INT_MAX );item_kill_tree (&tree); }
@@ -262,28 +261,23 @@ static void __order_0_locate_check(void)static void locate_check (void ) {RADIX_TREE (tree,GFP_KERNEL ); -unsigned order;unsigned long offset, index;__order_0_locate_check (); -for (order =0 ; order <20 ; order++) { -for (offset =0 ; offset < (1 << (order +3 )); - offset += (1UL << order)) { -for (index =0 ; index < (1UL << (order +5 )); - index += (1UL << order)) { -__locate_check (&tree, index + offset, order); - } -if (radix_tree_locate_item (&tree, &tree) !=-1 ) -abort (); - -item_kill_tree (&tree); +for (offset =0 ; offset < (1 <<3 ); offset++) { +for (index =0 ; index < (1UL <<5 ); index++) { +__locate_check (&tree, index + offset); } +if (radix_tree_locate_item (&tree, &tree) !=-1 ) +abort (); + +item_kill_tree (&tree); }if (radix_tree_locate_item (&tree, &tree) !=-1 )abort (); -__locate_check (&tree,-1 ,0 ); +__locate_check (&tree,-1 );if (radix_tree_locate_item (&tree, &tree) !=-1 )abort ();item_kill_tree (&tree);
@@ -294,8 +288,6 @@ static void single_thread_tests(bool long_run)int i;printf ("starting single_thread_tests: %d allocated\n" , nr_allocated); -multiorder_checks (); -printf ("after multiorder_check: %d allocated\n" , nr_allocated);locate_check ();printf ("after locate_check: %d allocated\n" , nr_allocated);tag_check ();
tools/testing/radix-tree/multiorder.c deleted +0 -337
337 lines not on this page · raw message
tools/testing/radix-tree/test.c +1 -13
@@ -24,21 +24,9 @@ int item_tag_get(struct radix_tree_root *root, unsigned long index, int tag)return radix_tree_tag_get (root, index, tag); } -int __item_insert (struct radix_tree_root *root,struct item *item, -unsigned order) -{ -return __radix_tree_insert (root, item->index, order, item); -} -int item_insert (struct radix_tree_root *root,unsigned long index) { -return __item_insert (root,item_create (index),0 ); -} - -int item_insert_order (struct radix_tree_root *root,unsigned long index, -unsigned order) -{ -return __item_insert (root,item_create (index), order); +return radix_tree_insert (root, index,item_create (index)); }int item_delete (struct radix_tree_root *root,unsigned long index)
tools/testing/radix-tree/test.h +0 -5
@@ -8,11 +8,7 @@ struct item { };struct item *item_create (unsigned long index); -int __item_insert (struct radix_tree_root *root,struct item *item, -unsigned order);int item_insert (struct radix_tree_root *root,unsigned long index); -int item_insert_order (struct radix_tree_root *root,unsigned long index, -unsigned order);int item_delete (struct radix_tree_root *root,unsigned long index);struct item *item_lookup (struct radix_tree_root *root,unsigned long index);
@@ -26,7 +22,6 @@ void item_full_scan(struct radix_tree_root *root, unsigned long start,void item_kill_tree (struct radix_tree_root *root);void tag_check (void ); -void multiorder_checks (void );struct item *item_tag_set (struct radix_tree_root *root,unsigned long index,int tag);
Re: [PATCH 2/2] radix-tree: Fix optimisation problem
In reply to Linus TorvaldsQuoted 32 lines · Linus Torvalds
lib/radix-tree.c static unsigned int radix_tree_descend(struct radix_tree_node *parent, · line 115
Quoted 6 lines · Matthew Wilcox
#ifdef CONFIG_RADIX_TREE_MULTIORDER if (radix_tree_is_internal_node(entry)) {lib/radix-tree.c +2
-1-unsigned long siboff =get_slot_offset (parent, entry); +unsigned long siboff =get_slot_offset (parent, + (void **)entry_to_node (entry));I feel that it is *this* part that I think needs a huge honking comment.
If you are going to make
get_slot_offset()different, then you could just rewriteget_slot_offset()to dounsigned long diff = (unsigned long) slot - (unsigned
long)parent->slots; return diff / sizeof(void *);and add a comment to say "don't do this as a pointer diff, because 'slot' may not be an aligned pointer". No
BUG_ON()necessary, because it "just works".At that point, gcc should just generate the right code, because it doesn't see it as a pointer subtraction followed by a pointer addition.
And yes, that crazy " (void **)entry_to_node(entry)" fixes it *too*, but it needs a *comment*.
Why is that special, when all the other uses of
get_slot_offset()don't have that? *That* is what should be explained. Not some internal detail.
Quoted 8 lines
That said, if this code isn't even used, as Konstantin says (THP selects it - doesn't THP use it?), then the fix really should be to just remove the odd code instead of adding to it.
Looking around for uses that set "order" to anything but zero, I really don't see it. So maybe we should just do *that* trivial thing instead, and remove
CONFIG_RADIX_TREE_MULTIORDER, since it's appears to be buggy and always has been.
Well, my ext4-with-huge-pages patchset[1] uses multi-order entries. It also converts shmem-with-huge-pages and hugetlb to them.
I'm okay with converting it to other mechanism, but I need something. (I looked into Konstantin's RFC patchset[2]. It looks okay, but I don't feel myself qualified to review it as I don't know much about radix-tree internals.)
[1] http://lkml.kernel.org/r/[email protected]
[2] http://lkml.kernel.org/r/147230727479.9957.1087787722571077339.stgit@zurg
Signature · 7 lines
Kirill A. Shutemov -- 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/2] radix-tree: Fix optimisation problem
Quoted 3 lines · Kirill A. Shutemov
Well, my ext4-with-huge-pages patchset[1] uses multi-order entries. It also converts shmem-with-huge-pages and hugetlb to them.
Ok, so that code actually has a chance of being used. I guess we'll not remove it. But I *would* like this subtle issue to have a comment around that odd cast/and/mask thing.
Linus
--
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/2] radix-tree: Fix optimisation problem
In reply to Matthew WilcoxQuoted 6 lines · Matthew Wilcox
From: Matthew Wilcox <[email protected]>
When compiling the radix tree with -O2, GCC thinks it can optimise:
void *entry = parent->slots[offset]; int siboff = entry - parent->slots;
If entry is a pointer to void, how can you do pointer arithmetic with it? Also, if you use pointer distances, the use of int is not valid, it should then be ptrdiff_t siboff.
lint(1) would bite your arse off in both cases.
Sadly only UNIX (Solaris, AIX, ...) use lint(1) as mandatory part of the build process and make warnings and errors of lint(1) fatal...
Ced
Signature · 9 lines
Cedric Blancher <[email protected]> [https://plus.google.com/u/0/+CedricBlancher/] Institute Pasteur -- 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/2] radix-tree: Fix optimisation problem
Quoted 5 lines · Cedric Blancher
Quoted 3 lines
void *entry = parent->slots[offset]; int siboff = entry - parent->slots;If entry is a pointer to void, how can you do pointer arithmetic with it?
It's actually void **.
(That said, gcc has an extension that considers "void *" to be a byte pointer, so you can actually do arithmetic on them, and it acts like "char *")
Quoted 2 lines
Also, if you use pointer distances, the use of int is not valid, it should then be
ptrdiff_tsiboff.
The use of "int" is perfectly valid, since it's limited by RADIX_TREE_MAP_SIZE, so it's going to be a small integer.
Linus
--
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/2] radix-tree: Fix optimisation problem
Quoted 14 lines · Linus Torvalds
Quoted 5 lines · Cedric Blancher
Quoted 3 lines
void *entry = parent->slots[offset]; int siboff = entry - parent->slots;If entry is a pointer to void, how can you do pointer arithmetic with it?
It's actually void **.
(That said, gcc has an extension that considers "void *" to be a byte pointer, so you can actually do arithmetic on them, and it acts like "char *")
Quoted 5 lines
Quoted 2 lines
Also, if you use pointer distances, the use of int is not valid, it should then be
ptrdiff_tsiboff.The use of "int" is perfectly valid, since it's limited by
RADIX_TREE_MAP_SIZE, so it's going to be a small integer.
A specific data type would be wise (aka radtr_mapsz_t) to prevent a disaster as SystemV had early during development. It took AT&T TWO fucking months to figure out that their avl tree implementation had a small type problem with int vs long.
Since I'd expect no one cares I'm going to print this email so I can send the scan as PDF each time you hit that problem in the future with "told you so"
Ced
Signature · 9 lines
Cedric Blancher <[email protected]> [https://plus.google.com/u/0/+CedricBlancher/] Institute Pasteur -- 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/2] radix-tree: Fix optimisation problem
Quoted 6 lines · Cedric Blancher
Quoted 3 lines
The use of "int" is perfectly valid, since it's limited by
RADIX_TREE_MAP_SIZE, so it's going to be a small integer.A specific data type would be wise (aka
radtr_mapsz_t) to prevent a disaster as SystemV had early during development.
Actually, you're right that the code is shit and shouldn't use an "int" there.
The value range is indeed just up to RADIX_TREE_MAP_SIZE, but since the code actually can get entries that are *not* sibling entries, it could overflow
The more I look at that particular piece of code, the less I like it. It's buggy shit. It needs to be rewritten entirely too actually check for sibling entries, not that ad-hoc arithmetic crap.
Linus
Re: [PATCH 2/2] radix-tree: Fix optimisation problem
Quoted 4 lines · Linus Torvalds
The more I look at that particular piece of code, the less I like it. It's buggy shit. It needs to be rewritten entirely too actually check for sibling entries, not that ad-hoc arithmetic crap.
Here's my attempt at cleaning the mess up.
I'm not claiming it's perfect, but I think it's better. It gets rid of the ad-hoc arithmetic in radix_tree_descend(), and just makes all that be inside the is_sibling_entry() logic instead. Which got renamed and made to actually return the main sibling. So now there is at least only *one* piece of code that does that range comparison, and I don't think there is any huge need to explain what's going on, because the "magic" is unconditional.
Willy?
Linus
lib/radix-tree.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
lib/radix-tree.c +12 -10
@@ -78,18 +78,20 @@ static inline void *node_to_entry(void *ptr)#ifdef CONFIG_RADIX_TREE_MULTIORDER /* Sibling slots point directly to another slot in the same node */ -static inline bool is_sibling_entry (struct radix_tree_node *parent,void *node) +static inline void **get_sibling_entry (struct radix_tree_node *parent,void *node) { -void **ptr = node; -return (parent->slots <= ptr) && - (ptr < parent->slots +RADIX_TREE_MAP_SIZE ); +void **ptr = (void **)entry_to_node (node); +if ((parent->slots <= ptr) && (ptr < parent->slots +RADIX_TREE_MAP_SIZE )) +return ptr; +return NULL ; }#else -static inline bool is_sibling_entry (struct radix_tree_node *parent,void *node) +static inline void **get_sibling_entry (struct radix_tree_node *parent,void *node) { -return false; +return NULL ; }#endif +#define is_sibling_entry (parent, node) (get_sibling_entry(parent,node) != NULL)static inline unsigned long get_slot_offset (struct radix_tree_node *parent,void **slot)
@@ -105,10 +107,10 @@ static unsigned int radix_tree_descend(struct radix_tree_node *parent,#ifdef CONFIG_RADIX_TREE_MULTIORDER if (radix_tree_is_internal_node (entry)) { -unsigned long siboff =get_slot_offset (parent, entry); -if (siboff <RADIX_TREE_MAP_SIZE ) { - offset = siboff; - entry =rcu_dereference_raw (parent->slots[offset]); +void **sibentry =get_sibling_entry (parent, entry); +if (sibentry) { + offset =get_slot_offset (parent, sibentry); + entry =rcu_dereference_raw (*sibentry); } }#endif
Re: [PATCH 2/2] radix-tree: Fix optimisation problem
LGTM, except that #define is_sibling_entry should be IS_SIBLING_ENTRY
Ced
Quoted 20 lines · Linus Torvalds
Quoted 4 lines · Linus Torvalds
The more I look at that particular piece of code, the less I like it. It's buggy shit. It needs to be rewritten entirely too actually check for sibling entries, not that ad-hoc arithmetic crap.
Here's my attempt at cleaning the mess up.
I'm not claiming it's perfect, but I think it's better. It gets rid of the ad-hoc arithmetic in
radix_tree_descend(), and just makes all that be inside theis_sibling_entry()logic instead. Which got renamed and made to actually return the main sibling. So now there is at least only *one* piece of code that does that range comparison, and I don't think there is any huge need to explain what's going on, because the "magic" is unconditional.Willy?
Linus
Signature · 9 lines
Cedric Blancher <[email protected]> [https://plus.google.com/u/0/+CedricBlancher/] Institute Pasteur -- 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/2] radix-tree: Fix optimisation problem
In reply to Linus TorvaldsQuoted 4 lines · Linus Torvalds
It gets rid of
the ad-hoc arithmetic inradix_tree_descend(), and just makes all that be inside theis_sibling_entry()logic instead. Which got renamed and made to actually return the main sibling.
Sadly, it looks like gcc generates bad code for this approach. Looks like it ends up testing the resulting sibling pointer twice (because we explicitly disable -fno-delete-null-pointer-checks in the kernel, and we have no way to say "look, I know this pointer I'm returning is non-null").
So a smaller patch that keeps the old boolean "is_sibling_entry()" but then actually *uses* that inside radix_tree_descend() and then tries to make the nasty cast to "void **" more legible by making it use a temporary variable seems to be a reasonable balance.
At least I feel like I can still read the code, but admittedly by now that may be because I've stared at those few lines so much that I feel like I know what's going on. So maybe the code isn't actually any more legible after all.
.. and unlike my previous patch, it actually generates better code than the original (while still passing the fixed test-suite, of course). The reason seems to be exactly that temporary variable, allowing us to just do
entry = rcu_dereference_raw(*sibentry);
rather than doing
entry = rcu_dereference_raw(parent->slots[offset]);
with the re-computed offset.
So I think I'll commit this unless somebody screams.
Linus
lib/radix-tree.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
lib/radix-tree.c +4 -4
@@ -105,10 +105,10 @@ static unsigned int radix_tree_descend(struct radix_tree_node *parent,#ifdef CONFIG_RADIX_TREE_MULTIORDER if (radix_tree_is_internal_node (entry)) { -unsigned long siboff =get_slot_offset (parent, entry); -if (siboff <RADIX_TREE_MAP_SIZE ) { - offset = siboff; - entry =rcu_dereference_raw (parent->slots[offset]); +if (is_sibling_entry (parent, entry)) { +void **sibentry = (void **)entry_to_node (entry); + offset =get_slot_offset (parent, sibentry); + entry =rcu_dereference_raw (*sibentry); } }#endif
Re: [PATCH 2/2] radix-tree: Fix optimisation problem
In reply to Linus TorvaldsQuoted 12 lines · Linus Torvalds
Quoted 3 lines · On Sat, Sep 24, 2016 at 2:04 PM, Kirill A. Shutemov…
Well, my ext4-with-huge-pages patchset[1] uses multi-order entries. It also converts shmem-with-huge-pages and hugetlb to them.
Ok, so that code actually has a chance of being used. I guess we'll not remove it. But I *would* like this subtle issue to have a comment around that odd cast/and/mask thing.
Linus
My DAX PMD patches will use multi-order entries as well.
Matthew Wilcox
Acked-byRE: [PATCH 2/2] radix-tree: Fix optimisation problem
In reply to Linus TorvaldsFrom: [email protected] [mailto:[email protected]] On Behalf Of Linus Torvalds
Quoted 30 lines
Quoted 4 lines · Linus Torvalds
It gets rid of
the ad-hoc arithmetic inradix_tree_descend(), and just makes all that be inside theis_sibling_entry()logic instead. Which got renamed and made to actually return the main sibling.Sadly, it looks like gcc generates bad code for this approach. Looks like it ends up testing the resulting sibling pointer twice (because we explicitly disable -fno-delete-null-pointer-checks in the kernel, and we have no way to say "look, I know this pointer I'm returning is non-null").
So a smaller patch that keeps the old boolean "
is_sibling_entry()" but then actually *uses* that insideradix_tree_descend()and then tries to make the nasty cast to "void **" more legible by making it use a temporary variable seems to be a reasonable balance.At least I feel like I can still read the code, but admittedly by now that may be because I've stared at those few lines so much that I feel like I know what's going on. So maybe the code isn't actually any more legible after all.
.. and unlike my previous patch, it actually generates better code than the original (while still passing the fixed test-suite, of course). The reason seems to be exactly that temporary variable, allowing us to just do
entry = rcu_dereference_raw(*sibentry);
Quoted 7 lines
rather than doing
entry = rcu_dereference_raw(parent->slots[offset]);with the re-computed offset.
So I think I'll commit this unless somebody screams.
I don't love it. But I think it's a reasonable fix for this point in the release cycle, and I have an idea for changing the representation of sibling slots that will make this moot.
(Basically adopting Konstantin's idea for using the *last* entry instead of the *first*, and then using entries of the form (offset << 2 | RADIX_TREE_INTERNAL_NODE), so we can identify sibling entries without knowing the parent pointer, and we can go straight from sibling entry to slot offset as a shift rather than as a pointer subtraction).
Re: [PATCH 2/2] radix-tree: Fix optimisation problem
You might also try to use valid, plain ISO C99 instead of perverted gcc extensions which only cause a lot of trouble in the long run.
Ced
Quoted 44 lines · Matthew Wilcox
From: [email protected] [mailto:[email protected]] On Behalf Of Linus Torvalds
Quoted 30 lines
Quoted 4 lines · Linus Torvalds
It gets rid of
the ad-hoc arithmetic inradix_tree_descend(), and just makes all that be inside theis_sibling_entry()logic instead. Which got renamed and made to actually return the main sibling.Sadly, it looks like gcc generates bad code for this approach. Looks like it ends up testing the resulting sibling pointer twice (because we explicitly disable -fno-delete-null-pointer-checks in the kernel, and we have no way to say "look, I know this pointer I'm returning is non-null").
So a smaller patch that keeps the old boolean "
is_sibling_entry()" but then actually *uses* that insideradix_tree_descend()and then tries to make the nasty cast to "void **" more legible by making it use a temporary variable seems to be a reasonable balance.At least I feel like I can still read the code, but admittedly by now that may be because I've stared at those few lines so much that I feel like I know what's going on. So maybe the code isn't actually any more legible after all.
.. and unlike my previous patch, it actually generates better code than the original (while still passing the fixed test-suite, of course). The reason seems to be exactly that temporary variable, allowing us to just do
entry = rcu_dereference_raw(*sibentry);Quoted 7 lines
rather than doing
entry = rcu_dereference_raw(parent->slots[offset]);with the re-computed offset.
So I think I'll commit this unless somebody screams.
Acked-by Matthew Wilcox <[email protected]>I don't love it. But I think it's a reasonable fix for this point in the release cycle, and I have an idea for changing the representation of sibling slots that will make this moot.
(Basically adopting Konstantin's idea for using the *last* entry instead of the *first*, and then using entries of the form (offset << 2 |
RADIX_TREE_INTERNAL_NODE), so we can identify sibling entries without knowing the parent pointer, and we can go straight from sibling entry to slot offset as a shift rather than as a pointer subtraction).
Signature · 9 lines
Cedric Blancher <[email protected]> [https://plus.google.com/u/0/+CedricBlancher/] Institute Pasteur -- 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>