lib: Make radix_tree_node_alloc() work correctly within interrupt
Subsystems (6)
Jan Kara
maintainerPATCH v2 1/1 lib: Make radix_tree_node_alloc() work correctly within interrupt
With users of radix_tree_preload() run from interrupt (block/blk-ioc.c is one such possible user), the following race can happen:
radix_tree_preload()
...radix_tree_insert()
radix_tree_node_alloc()
if (rtp->nr) {
ret = rtp->nodes[rtp->nr - 1];<interrupt>
...radix_tree_preload()
...radix_tree_insert()
radix_tree_node_alloc()
if (rtp->nr) {
ret = rtp->nodes[rtp->nr - 1];And we give out one radix tree node twice. That clearly results in radix tree corruption with different results (usually OOPS) depending on which two users of radix tree race.
We fix the problem by making radix_tree_node_alloc() always allocate fresh radix tree nodes when in interrupt. Using preloading when in interrupt doesn't make sence since all the allocations have to be atomic anyway and we cannot steal nodes from process-context users because some users rely on radix_tree_insert() succeeding after radix_tree_preload(). in_interrupt() check is somewhat ugly but we cannot simply key off passed gfp_mask as that is acquired from root_gfp_mask() and thus the same for all preload users.
Another part of the fix is to avoid node preallocation in radix_tree_preload() when passed gfp_mask doesn't allow waiting. Again, preallocation in such case doesn't make sence and when preallocation would happen in interrupt we could possibly leak some allocated nodes. However, some users of radix_tree_preload() require following radix_tree_insert() to succeed. To avoid unexpected effects for these users, radix_tree_preload() only warns if passed gfp mask doesn't allow waiting and we provide a new function radix_tree_maybe_preload() for those users which get different gfp mask from different call sites and which are prepared to handle radix_tree_insert() failure.
--- block/blk-ioc.c | 2 +- fs/fscache/page.c | 2 +- include/linux/radix-tree.h | 1 + lib/radix-tree.c | 41 +++++++++++++++++++++++++++++++++++++++-- mm/filemap.c | 2 +- mm/shmem.c | 2 +- mm/swap_state.c | 4 ++-- 7 files changed, 46 insertions(+), 8 deletions(-)
So after some discussions over the first version here comes v2 which should handle issues we found.
block/blk-ioc.c +1 -1
@@ -367,7 +367,7 @@ struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,if (!icq)return NULL ; -if (radix_tree_preload (gfp_mask) <0 ) { +if (radix_tree_maybe_preload (gfp_mask) <0 ) {kmem_cache_free (et->icq_cache, icq);return NULL ; }
fs/fscache/page.c +1 -1
@@ -890,7 +890,7 @@ int __fscache_write_page(struct fscache_cookie *cookie, (1 <<FSCACHE_OP_WAITING ) | (1 <<FSCACHE_OP_UNUSE_COOKIE ); - ret =radix_tree_preload (gfp & ~__GFP_HIGHMEM); + ret =radix_tree_maybe_preload (gfp & ~__GFP_HIGHMEM);if (ret <0 ) goto nomem_free;
include/linux/radix-tree.h +1 -0
@@ -231,6 +231,7 @@ unsigned long radix_tree_next_hole(struct radix_tree_root *root,unsigned long radix_tree_prev_hole (struct radix_tree_root *root,unsigned long index,unsigned long max_scan);int radix_tree_preload (gfp_t gfp_mask); +int radix_tree_maybe_preload (gfp_t gfp_mask);void radix_tree_init (void );void *radix_tree_tag_set (struct radix_tree_root *root,unsigned long index,unsigned int tag);
lib/radix-tree.c +39 -2
68 lines not on this page · raw message
mm/filemap.c +1 -1
@@ -469,7 +469,7 @@ int add_to_page_cache_locked(struct page *page, struct address_space *mapping,if (error) goto out; - error =radix_tree_preload (gfp_mask & ~__GFP_HIGHMEM); + error =radix_tree_maybe_preload (gfp_mask & ~__GFP_HIGHMEM);if (error ==0 ) {page_cache_get (page); page->mapping = mapping;
mm/shmem.c +1 -1
@@ -1205,7 +1205,7 @@ repeat: gfp & GFP_RECLAIM_MASK);if (error) goto decused; - error =radix_tree_preload (gfp &GFP_RECLAIM_MASK ); + error =radix_tree_maybe_preload (gfp &GFP_RECLAIM_MASK );if (!error) { error =shmem_add_to_page_cache (page, mapping, index, gfp,NULL );
mm/swap_state.c +2 -2
@@ -122,7 +122,7 @@ int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp_mask) {int error; - error =radix_tree_preload (gfp_mask); + error =radix_tree_maybe_preload (gfp_mask);if (!error) { error =__add_to_swap_cache (page, entry);radix_tree_preload_end ();
@@ -328,7 +328,7 @@ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,/* * call radix_tree_preload() while we can wait. */ - err =radix_tree_preload (gfp_mask &GFP_KERNEL ); + err =radix_tree_maybe_preload (gfp_mask &GFP_KERNEL );if (err)break ;
Signature · 7 lines
1.8.1.4 -- 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>
On 07/23/13 15:04, Jan Kara wrote:
Hi,
s/sence/sense/ please.
Quoted 14 lines
lib/radix-tree.c +1
-0@@ -32,6 +32,7 @@#include <linux/string.h> #include <linux/bitops.h> #include <linux/rcupdate.h> +#include <linux/hardirq.h> /* in_interrupt() */ #ifdef __KERNEL__
lib/radix-tree.c radix_tree_node_alloc(struct radix_tree_root *root) · line 212
Quoted 6 lines
lib/radix-tree.c +2
-1@@ -207,7 +208,12 @@ radix_tree_node_alloc(struct radix_tree_root *root)struct radix_tree_node *ret =NULL ;gfp_t gfp_mask =root_gfp_mask (root); -if (!(gfp_mask & __GFP_WAIT)) { +/* +* Preload code isn't irq safe and it doesn't make sence to use
sense
Quoted 45 lines
lib/radix-tree.c +27
-1+* preloading in the interrupt anyway as all the allocations have to +* be atomic. So just do normal allocation when in interrupt. +*/ +if (!(gfp_mask & __GFP_WAIT) && !in_interrupt ()) {struct radix_tree_preload *rtp;/* @@ -264,7 +270,7 @@ radix_tree_node_free(struct radix_tree_node *node)* To make use of this facility, the radix tree must be initialised without * __GFP_WAIT being passed to INIT_RADIX_TREE(). */ -int radix_tree_preload (gfp_t gfp_mask) +static int __radix_tree_preload (gfp_t gfp_mask) {struct radix_tree_preload *rtp;struct radix_tree_node *node;@@ -288,9 +294,40 @@ int radix_tree_preload(gfp_t gfp_mask) out:return ret; } + +/* +* Load up this CPU's radix_tree_node buffer with sufficient objects to +* ensure that the addition of a single element in the tree cannot fail. On +* success, return zero, with preemption disabled. On error, return -ENOMEM +* with preemption not disabled. +* +* To make use of this facility, the radix tree must be initialised without +* __GFP_WAIT being passed to INIT_RADIX_TREE(). +*/ +int radix_tree_preload (gfp_t gfp_mask) +{ +/* Warn on non-sensical use... */ +WARN_ON_ONCE (!(gfp_mask & __GFP_WAIT)); +return __radix_tree_preload (gfp_mask); +}EXPORT_SYMBOL (radix_tree_preload);/* +* The same as above function, except we don't guarantee preloading happens. +* We do it, if we decide it helps. On success, return zero with preemption +* disabled. On error, return -ENOMEM with preemption not disabled. +*/ +int radix_tree_maybe_preload (gfp_t gfp_mask) +{
lib/radix-tree.c int radix_tree_preload(gfp_t gfp_mask) · line 331
Quoted 10 lines
lib/radix-tree.c +9
-0+if (gfp_mask & __GFP_WAIT) +return __radix_tree_preload (gfp_mask); +/* Preloading doesn't help anything with this gfp mask, skip it */ +preempt_disable (); +return 0 ; +} +EXPORT_SYMBOL (radix_tree_maybe_preload); + +/* * Return the maximum key which can be store into a
stored
Quoted 2 lines
lib/radix-tree.c +0
-0* radix tree with height HEIGHT. */
Signature · 7 lines
~Randy -- 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>
Jan Kara
maintainerQuoted 2 lines · Randy Dunlap
On 07/23/13 15:04, Jan Kara wrote:
s/sence/sense/ please.
Thanks. I keep getting this wrong... I've updated the patch locally but I
won't resend it just for this spellcheck fix (yet).
Honza
Quoted 85 lines
Quoted 13 lines
lib/radix-tree.c +1
-0@@ -32,6 +32,7 @@#include <linux/string.h> #include <linux/bitops.h> #include <linux/rcupdate.h> +#include <linux/hardirq.h> /* in_interrupt() */ #ifdef __KERNEL__lib/radix-tree.c radix_tree_node_alloc(struct radix_tree_root *root) · line 212
Quoted 6 lines
lib/radix-tree.c +2
-1@@ -207,7 +208,12 @@ radix_tree_node_alloc(struct radix_tree_root *root)struct radix_tree_node *ret =NULL ;gfp_t gfp_mask =root_gfp_mask (root); -if (!(gfp_mask & __GFP_WAIT)) { +/* +* Preload code isn't irq safe and it doesn't make sence to use sense
Quoted 45 lines
lib/radix-tree.c +27
-1+* preloading in the interrupt anyway as all the allocations have to +* be atomic. So just do normal allocation when in interrupt. +*/ +if (!(gfp_mask & __GFP_WAIT) && !in_interrupt ()) {struct radix_tree_preload *rtp;/* @@ -264,7 +270,7 @@ radix_tree_node_free(struct radix_tree_node *node)* To make use of this facility, the radix tree must be initialised without * __GFP_WAIT being passed to INIT_RADIX_TREE(). */ -int radix_tree_preload (gfp_t gfp_mask) +static int __radix_tree_preload (gfp_t gfp_mask) {struct radix_tree_preload *rtp;struct radix_tree_node *node;@@ -288,9 +294,40 @@ int radix_tree_preload(gfp_t gfp_mask) out:return ret; } + +/* +* Load up this CPU's radix_tree_node buffer with sufficient objects to +* ensure that the addition of a single element in the tree cannot fail. On +* success, return zero, with preemption disabled. On error, return -ENOMEM +* with preemption not disabled. +* +* To make use of this facility, the radix tree must be initialised without +* __GFP_WAIT being passed to INIT_RADIX_TREE(). +*/ +int radix_tree_preload (gfp_t gfp_mask) +{ +/* Warn on non-sensical use... */ +WARN_ON_ONCE (!(gfp_mask & __GFP_WAIT)); +return __radix_tree_preload (gfp_mask); +}EXPORT_SYMBOL (radix_tree_preload);/* +* The same as above function, except we don't guarantee preloading happens. +* We do it, if we decide it helps. On success, return zero with preemption +* disabled. On error, return -ENOMEM with preemption not disabled. +*/ +int radix_tree_maybe_preload (gfp_t gfp_mask) +{lib/radix-tree.c int radix_tree_preload(gfp_t gfp_mask) · line 331
Quoted 10 lines
lib/radix-tree.c +9
-0+if (gfp_mask & __GFP_WAIT) +return __radix_tree_preload (gfp_mask); +/* Preloading doesn't help anything with this gfp mask, skip it */ +preempt_disable (); +return 0 ; +} +EXPORT_SYMBOL (radix_tree_maybe_preload); + +/* * Return the maximum key which can be store into a stored
Quoted 2 lines
lib/radix-tree.c +0
-0* radix tree with height HEIGHT. */ Signature · 1 line
~Randy
Signature · 8 lines
Jan Kara <[email protected]> SUSE Labs, CR -- 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>