From: Jeff Mahoney <jeffm@suse.com>
To: Btrfs List <linux-btrfs@vger.kernel.org>
Subject: [patch 07/99] btrfs: Use mempools for extent_state structures
Date: Wed, 23 Nov 2011 19:35:40 -0500 [thread overview]
Message-ID: <20111124004220.628076319@suse.com> (raw)
In-Reply-To: 20111124003533.395674389@suse.com
The extent_state structure is used at the core of the extent i/o code
for managing flags, locking, etc. It requires allocations deep in the
write code and if failures occur they are difficult to recover from.
We avoid most of the failures by using a mempool, which can sleep when
required, to honor the allocations. This allows future patches to convert
most of the {set,clear,convert}_extent_bit and derivatives to return
void.
Signed-off-by: Jeff Mahoney <jeffm@suse.com>
---
fs/btrfs/extent_io.c | 71 ++++++++++++++++++++++++++++++++++++---------------
1 file changed, 51 insertions(+), 20 deletions(-)
Index: source/fs/btrfs/extent_io.c
===================================================================
--- source.orig/fs/btrfs/extent_io.c 2011-11-21 14:13:55.000000000 -0500
+++ source/fs/btrfs/extent_io.c 2011-11-21 14:38:23.000000000 -0500
@@ -12,6 +12,7 @@
#include <linux/pagevec.h>
#include <linux/prefetch.h>
#include <linux/cleancache.h>
+#include <linux/mempool.h>
#include "extent_io.h"
#include "extent_map.h"
#include "compat.h"
@@ -21,6 +22,8 @@
static struct kmem_cache *extent_state_cache;
static struct kmem_cache *extent_buffer_cache;
+static mempool_t *extent_state_pool;
+#define EXTENT_STATE_POOL_SIZE (64*1024)
static LIST_HEAD(buffers);
static LIST_HEAD(states);
@@ -61,18 +64,28 @@ tree_fs_info(struct extent_io_tree *tree
int __init extent_io_init(void)
{
extent_state_cache = kmem_cache_create("extent_state",
- sizeof(struct extent_state), 0,
- SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
+ sizeof(struct extent_state), 0,
+ SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
+ NULL);
if (!extent_state_cache)
return -ENOMEM;
+ extent_state_pool = mempool_create_slab_pool(
+ EXTENT_STATE_POOL_SIZE /
+ sizeof(struct extent_state),
+ extent_state_cache);
+ if (!extent_state_pool)
+ goto free_state_cache;
+
extent_buffer_cache = kmem_cache_create("extent_buffers",
sizeof(struct extent_buffer), 0,
SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
if (!extent_buffer_cache)
- goto free_state_cache;
+ goto free_state_mempool;
return 0;
+free_state_mempool:
+ mempool_destroy(extent_state_pool);
free_state_cache:
kmem_cache_destroy(extent_state_cache);
return -ENOMEM;
@@ -103,6 +116,8 @@ void extent_io_exit(void)
list_del(&eb->leak_list);
kmem_cache_free(extent_buffer_cache, eb);
}
+ if (extent_state_pool)
+ mempool_destroy(extent_state_pool);
if (extent_state_cache)
kmem_cache_destroy(extent_state_cache);
if (extent_buffer_cache)
@@ -128,7 +143,7 @@ static struct extent_state *alloc_extent
unsigned long flags;
#endif
- state = kmem_cache_alloc(extent_state_cache, mask);
+ state = mempool_alloc(extent_state_pool, mask);
if (!state)
return state;
state->state = 0;
@@ -145,6 +160,12 @@ static struct extent_state *alloc_extent
return state;
}
+static struct extent_state *alloc_extent_state_nofail(gfp_t mask)
+{
+ BUG_ON(!(mask & __GFP_WAIT));
+ return alloc_extent_state(mask);
+}
+
void free_extent_state(struct extent_state *state)
{
if (!state)
@@ -160,7 +181,7 @@ void free_extent_state(struct extent_sta
spin_unlock_irqrestore(&leak_lock, flags);
#endif
trace_free_extent_state(state, _RET_IP_);
- kmem_cache_free(extent_state_cache, state);
+ mempool_free(state, extent_state_pool);
}
}
@@ -437,6 +458,12 @@ static int clear_state_bit(struct extent
return ret;
}
+static void
+assert_atomic_alloc(struct extent_state *prealloc, gfp_t mask)
+{
+ WARN_ON(!prealloc && (mask & __GFP_WAIT));
+}
+
static struct extent_state *
alloc_extent_state_atomic(struct extent_state *prealloc)
{
@@ -464,6 +491,7 @@ NORET_TYPE void extent_io_tree_panic(str
* the range [start, end] is inclusive.
*
* This takes the tree lock, and returns 0 on success and < 0 on error.
+ * If (mask & __GFP_WAIT) == 0, there are no error conditions.
*/
int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
int bits, int wake, int delete,
@@ -486,11 +514,8 @@ int clear_extent_bit(struct extent_io_tr
if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
clear = 1;
again:
- if (!prealloc && (mask & __GFP_WAIT)) {
- prealloc = alloc_extent_state(mask);
- if (!prealloc)
- return -ENOMEM;
- }
+ if (!prealloc && (mask & __GFP_WAIT))
+ prealloc = alloc_extent_state_nofail(mask);
spin_lock(&tree->lock);
if (cached_state) {
@@ -542,6 +567,7 @@ hit_next:
*/
if (state->start < start) {
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
err = split_state(tree, state, prealloc, start);
@@ -566,6 +592,7 @@ hit_next:
* on the first half
*/
if (state->start <= end && state->end > end) {
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
err = split_state(tree, state, prealloc, end + 1);
@@ -726,15 +753,14 @@ int set_extent_bit(struct extent_io_tree
struct extent_state *prealloc = NULL;
struct rb_node *node;
int err = 0;
+ int wait = mask & __GFP_WAIT;
u64 last_start;
u64 last_end;
bits |= EXTENT_FIRST_DELALLOC;
again:
- if (!prealloc && (mask & __GFP_WAIT)) {
- prealloc = alloc_extent_state(mask);
- BUG_ON(!prealloc);
- }
+ if (!prealloc && wait)
+ prealloc = alloc_extent_state_nofail(mask);
spin_lock(&tree->lock);
if (cached_state && *cached_state) {
@@ -751,6 +777,7 @@ again:
*/
node = tree_search(tree, start);
if (!node) {
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
err = insert_state(tree, prealloc, start, end, &bits);
@@ -820,6 +847,7 @@ hit_next:
goto out;
}
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
err = split_state(tree, state, prealloc, start);
@@ -853,6 +881,7 @@ hit_next:
else
this_end = last_start - 1;
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
@@ -883,6 +912,7 @@ hit_next:
goto out;
}
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
BUG_ON(!prealloc);
err = split_state(tree, state, prealloc, end + 1);
@@ -909,7 +939,7 @@ search_again:
if (start > end)
goto out;
spin_unlock(&tree->lock);
- if (mask & __GFP_WAIT)
+ if (wait)
cond_resched();
goto again;
}
@@ -940,11 +970,8 @@ int convert_extent_bit(struct extent_io_
u64 last_end;
again:
- if (!prealloc && (mask & __GFP_WAIT)) {
- prealloc = alloc_extent_state(mask);
- if (!prealloc)
- return -ENOMEM;
- }
+ if (!prealloc && (mask & __GFP_WAIT))
+ prealloc = alloc_extent_state_nofail(mask);
spin_lock(&tree->lock);
/*
@@ -953,6 +980,7 @@ again:
*/
node = tree_search(tree, start);
if (!node) {
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
if (!prealloc)
return -ENOMEM;
@@ -1010,6 +1038,7 @@ hit_next:
* desired bit on it.
*/
if (state->start < start) {
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
if (!prealloc)
return -ENOMEM;
@@ -1042,6 +1071,7 @@ hit_next:
else
this_end = last_start - 1;
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
if (!prealloc)
return -ENOMEM;
@@ -1069,6 +1099,7 @@ hit_next:
* on the first half
*/
if (state->start <= end && state->end > end) {
+ assert_atomic_alloc(prealloc, mask);
prealloc = alloc_extent_state_atomic(prealloc);
if (!prealloc)
return -ENOMEM;
next prev parent reply other threads:[~2011-11-24 0:35 UTC|newest]
Thread overview: 115+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-24 0:35 [patch 00/99] Error handling patchset v6 Jeff Mahoney
2011-11-24 0:35 ` [patch 01/99] btrfs: Add btrfs_panic() Jeff Mahoney
2011-11-24 2:05 ` David Brown
2011-11-24 2:22 ` Jeff Mahoney
2011-11-24 6:37 ` David Brown
2011-11-25 2:36 ` Jeff Mahoney
2011-11-25 5:42 ` David Brown
2011-11-24 0:35 ` [patch 02/99] btrfs: Catch locking failures in {set,clear,convert}_extent_bit Jeff Mahoney
2011-11-24 0:35 ` [patch 03/99] btrfs: Panic on bad rbtree operations Jeff Mahoney
2011-11-24 23:41 ` David Sterba
2011-11-25 2:13 ` Jeff Mahoney
2011-11-24 0:35 ` [patch 04/99] btrfs: Simplify btrfs_insert_root Jeff Mahoney
2011-11-24 0:35 ` [patch 05/99] btrfs: Remove set bits return from clear_extent_bit Jeff Mahoney
2011-11-24 0:35 ` [patch 06/99] btrfs: Add extent_state alloc/free tracing Jeff Mahoney
2011-11-24 0:35 ` Jeff Mahoney [this message]
2011-11-28 23:53 ` [patch 07/99] btrfs: Use mempools for extent_state structures Andi Kleen
2011-11-29 0:04 ` Jeff Mahoney
2011-12-01 19:55 ` Jeff Mahoney
2011-12-03 4:53 ` Jeff Mahoney
2011-11-24 0:35 ` [patch 08/99] btrfs: clear_extent_bit should return void with __GFP_WAIT set Jeff Mahoney
2011-11-24 0:35 ` [patch 09/99] btrfs: unlock_extent can return void Jeff Mahoney
2011-11-24 0:35 ` [patch 10/99] btrfs: Split unlock_extent_cached into sleeping and atomic versions Jeff Mahoney
2011-11-24 0:35 ` [patch 11/99] btrfs: unlock_extent can drop gfp_t argument Jeff Mahoney
2011-11-24 0:35 ` [patch 12/99] btrfs: clear_extent_dirty " Jeff Mahoney
2011-11-24 0:35 ` [patch 13/99] btrfs: clear_extent_uptodate can drop gfp_t argumetn Jeff Mahoney
2011-11-24 23:57 ` David Sterba
2011-11-25 2:14 ` Jeff Mahoney
2011-11-24 0:35 ` [patch 14/99] btrfs: clear_extent_bits " Jeff Mahoney
2011-11-24 0:35 ` [patch 15/99] btrfs: try_lock_extent " Jeff Mahoney
2011-11-24 0:35 ` [patch 16/99] btrfs: clear_extent_bit can drop gfp_t argument Jeff Mahoney
2011-11-24 0:35 ` [patch 17/99] btrfs: set_extent_bit: split exclusive mode out Jeff Mahoney
2011-11-24 0:35 ` [patch 18/99] btrfs: set_extent_bit should return void with __GFP_WAIT set Jeff Mahoney
2011-11-24 0:35 ` [patch 19/99] btrfs: lock_extent can drop gfp_t argument Jeff Mahoney
2011-11-24 0:35 ` [patch 20/99] btrfs: set_extent_dirty " Jeff Mahoney
2011-11-24 0:35 ` [patch 21/99] btrfs: set_extent_bits " Jeff Mahoney
2011-11-24 0:35 ` [patch 22/99] btrfs: set_extent_delalloc " Jeff Mahoney
2011-11-24 0:35 ` [patch 23/99] btrfs: set_extent_new " Jeff Mahoney
2011-11-24 0:35 ` [patch 24/99] btrfs: set_extent_uptodate " Jeff Mahoney
2011-11-24 0:35 ` [patch 25/99] btrfs: set_extent_bit " Jeff Mahoney
2011-11-24 0:35 ` [patch 26/99] btrfs: set_extent_buffer_uptodate should return void Jeff Mahoney
2011-11-24 0:36 ` [patch 27/99] btrfs: set_extent_bit should return -ENOMEM on GFP_ATOMIC failures Jeff Mahoney
2011-11-24 0:36 ` [patch 28/99] btrfs: clear_extent_bit error push-up Jeff Mahoney
2011-11-24 0:36 ` [patch 29/99] btrfs: convert_extent_bit should return void with __GFP_WAIT set Jeff Mahoney
2011-11-24 0:36 ` [patch 30/99] btrfs: pin_down_extent should return void Jeff Mahoney
2011-11-24 0:36 ` [patch 31/99] btrfs: btrfs_pin_extent error push-up Jeff Mahoney
2011-11-24 0:36 ` [patch 32/99] btrfs: btrfs_drop_snapshot should return int Jeff Mahoney
2011-11-24 0:36 ` [patch 33/99] btrfs: btrfs_start_transaction non-looped error push-up Jeff Mahoney
2011-11-24 0:36 ` [patch 34/99] btrfs: find_and_setup_root " Jeff Mahoney
2011-11-24 0:36 ` [patch 35/99] btrfs: btrfs_update_root " Jeff Mahoney
2011-11-24 0:36 ` [patch 36/99] btrfs: set_range_writeback should return void Jeff Mahoney
2011-11-24 0:36 ` [patch 37/99] btrfs: wait_on_state " Jeff Mahoney
2011-11-24 0:36 ` [patch 38/99] btrfs: wait_extent_bit " Jeff Mahoney
2011-11-24 0:36 ` [patch 39/99] btrfs: __unlock_for_delalloc " Jeff Mahoney
2011-11-24 0:36 ` [patch 40/99] btrfs: check_page_uptodate " Jeff Mahoney
2011-11-24 0:36 ` [patch 41/99] btrfs: check_page_locked " Jeff Mahoney
2011-11-24 0:36 ` [patch 42/99] btrfs: check_page_writeback " Jeff Mahoney
2011-11-24 0:36 ` [patch 43/99] btrfs: clear_extent_buffer_dirty " Jeff Mahoney
2011-11-24 0:36 ` [patch 44/99] btrfs: btrfs_cleanup_fs_uuids " Jeff Mahoney
2011-11-24 0:36 ` [patch 45/99] btrfs: run_scheduled_bios " Jeff Mahoney
2011-11-24 0:36 ` [patch 46/99] btrfs: btrfs_close_extra_devices " Jeff Mahoney
2011-11-24 0:36 ` [patch 47/99] btrfs: schedule_bio " Jeff Mahoney
2011-11-24 0:36 ` [patch 48/99] btrfs: fill_device_from_item " Jeff Mahoney
2011-11-24 0:36 ` [patch 49/99] btrfs: btrfs_queue_worker " Jeff Mahoney
2011-11-24 0:36 ` [patch 50/99] btrfs: run_ordered_completions " Jeff Mahoney
2011-11-24 0:36 ` [patch 51/99] btrfs: btrfs_stop_workers " Jeff Mahoney
2011-11-24 0:36 ` [patch 52/99] btrfs: btrfs_requeue_work " Jeff Mahoney
2011-11-24 0:36 ` [patch 53/99] btrfs: btrfs_end_log_trans " Jeff Mahoney
2011-11-24 0:36 ` [patch 54/99] btrfs: wait_for_writer " Jeff Mahoney
2011-11-24 0:36 ` [patch 55/99] btrfs: btrfs_init_compress " Jeff Mahoney
2011-11-24 0:36 ` [patch 56/99] btrfs: btrfs_invalidate_inodes " Jeff Mahoney
2011-11-24 0:36 ` [patch 57/99] btrfs: __setup_root " Jeff Mahoney
2011-11-24 0:36 ` [patch 58/99] btrfs: btrfs_destroy_delalloc_inodes " Jeff Mahoney
2011-11-24 0:36 ` [patch 59/99] btrfs: btrfs_prepare_extent_commit " Jeff Mahoney
2011-11-24 0:36 ` [patch 60/99] btrfs: btrfs_set_block_group_rw " Jeff Mahoney
2011-11-24 0:36 ` [patch 61/99] btrfs: setup_inline_extent_backref " Jeff Mahoney
2011-11-24 0:36 ` [patch 62/99] btrfs: btrfs_run_defrag_inodes " Jeff Mahoney
2011-11-24 0:36 ` [patch 63/99] btrfs: Simplify btrfs_submit_bio_hook Jeff Mahoney
2011-11-24 0:36 ` [patch 64/99] btrfs: Factor out tree->ops->merge_bio_hook call Jeff Mahoney
2011-11-24 0:36 ` [patch 65/99] btrfs: ->submit_bio_hook error push-up Jeff Mahoney
2011-11-25 0:46 ` David Sterba
2011-11-25 2:17 ` Jeff Mahoney
2011-11-24 0:36 ` [patch 66/99] btrfs: __add_reloc_root " Jeff Mahoney
2011-11-24 0:36 ` [patch 67/99] btrfs: fixup_low_keys should return void Jeff Mahoney
2011-11-24 0:36 ` [patch 68/99] btrfs: setup_items_for_insert " Jeff Mahoney
2011-11-24 0:36 ` [patch 69/99] btrfs: del_ptr " Jeff Mahoney
2011-11-24 0:36 ` [patch 70/99] btrfs: insert_ptr " Jeff Mahoney
2011-11-24 0:36 ` [patch 71/99] btrfs: add_delayed_ref_head " Jeff Mahoney
2011-11-24 0:36 ` [patch 72/99] btrfs: add_delayed_tree_ref " Jeff Mahoney
2011-11-24 0:36 ` [patch 73/99] btrfs: add_delayed_data_ref " Jeff Mahoney
2011-11-24 0:36 ` [patch 74/99] btrfs: Fix kfree of member instead of structure Jeff Mahoney
2011-11-24 0:36 ` [patch 75/99] btrfs: Use mempools for delayed refs Jeff Mahoney
2011-11-24 0:36 ` [patch 76/99] btrfs: Delayed ref mempool functions should return void Jeff Mahoney
2011-11-24 0:36 ` [patch 77/99] btrfs: btrfs_inc_extent_ref void return prep Jeff Mahoney
2011-11-24 0:36 ` [patch 78/99] btrfs: btrfs_free_extent " Jeff Mahoney
2011-11-24 0:36 ` [patch 79/99] btrfs: __btrfs_mod_refs process_func should return void Jeff Mahoney
2011-11-24 0:36 ` [patch 80/99] btrfs: __btrfs_mod_ref " Jeff Mahoney
2011-11-24 0:36 ` [patch 81/99] btrfs: clean_tree_block " Jeff Mahoney
2011-11-24 0:36 ` [patch 82/99] btrfs: btrfs_truncate_item " Jeff Mahoney
2011-11-24 0:36 ` [patch 83/99] btrfs: btrfs_extend_item " Jeff Mahoney
2011-11-24 0:36 ` [patch 84/99] btrfs: end_compressed_writeback " Jeff Mahoney
2011-11-24 0:36 ` [patch 85/99] btrfs: copy_for_split " Jeff Mahoney
2011-11-24 0:36 ` [patch 86/99] btrfs: update_inline_extent_backref " Jeff Mahoney
2011-11-24 0:37 ` [patch 87/99] btrfs: btrfs_put_ordered_extent " Jeff Mahoney
2011-11-24 0:37 ` [patch 88/99] btrfs: __btrfs_remove_ordered_extent " Jeff Mahoney
2011-11-24 0:37 ` [patch 89/99] btrfs: btrfs_wait_ordered_extents " Jeff Mahoney
2011-11-24 0:37 ` [patch 90/99] btrfs: btrfs_wait_ordered_range " Jeff Mahoney
2011-11-24 0:37 ` [patch 91/99] btrfs: btrfs_run_ordered_operations " Jeff Mahoney
2011-11-24 0:37 ` [patch 92/99] btrfs: btrfs_add_ordered_operation " Jeff Mahoney
2011-11-24 0:37 ` [patch 93/99] btrfs: btrfs_add_ordered_sum " Jeff Mahoney
2011-11-24 0:37 ` [patch 94/99] btrfs: btrfs_free_fs_root " Jeff Mahoney
2011-11-24 0:37 ` [patch 95/99] btrfs: del_fs_roots " Jeff Mahoney
2011-11-24 0:37 ` [patch 96/99] btrfs: btrfs_destroy_ordered_operations " Jeff Mahoney
2011-11-24 0:37 ` [patch 97/99] btrfs: btrfs_destroy_ordered_extents " Jeff Mahoney
2011-11-24 0:37 ` [patch 98/99] btrfs: btrfs_destroy_pending_snapshots " Jeff Mahoney
2011-11-24 0:37 ` [patch 99/99] btrfs: add_excluded_extent " Jeff Mahoney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20111124004220.628076319@suse.com \
--to=jeffm@suse.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).