Skip to content

Commit 65b51a0

Browse files
committed
btrfs_search_slot: reduce lock contention by cowing in two stages
A btree block cow has two parts, the first is to allocate a destination block and the second is to copy the old bock over. The first part needs locks in the extent allocation tree, and may need to do IO. This changeset splits that into a separate function that can be called without any tree locks held. btrfs_search_slot is changed to drop its path and start over if it has to COW a contended block. This often means that many writers will pre-alloc a new destination for a the same contended block, but they cache their prealloc for later use on lower levels in the tree. Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 18e35e0 commit 65b51a0

6 files changed

Lines changed: 173 additions & 48 deletions

File tree

‎fs/btrfs/ctree.c‎

Lines changed: 120 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ int __btrfs_cow_block(struct btrfs_trans_handle *trans,
181181
struct extent_buffer *buf,
182182
struct extent_buffer *parent, int parent_slot,
183183
struct extent_buffer **cow_ret,
184-
u64 search_start, u64 empty_size)
184+
u64 search_start, u64 empty_size,
185+
u64 prealloc_dest)
185186
{
186187
u64 root_gen;
187188
struct extent_buffer *cow;
@@ -216,10 +217,27 @@ int __btrfs_cow_block(struct btrfs_trans_handle *trans,
216217
} else {
217218
first_key.objectid = 0;
218219
}
219-
cow = btrfs_alloc_free_block(trans, root, buf->len,
220-
root->root_key.objectid,
221-
root_gen, first_key.objectid, level,
222-
search_start, empty_size);
220+
if (prealloc_dest) {
221+
struct btrfs_key ins;
222+
223+
ins.objectid = prealloc_dest;
224+
ins.offset = buf->len;
225+
ins.type = BTRFS_EXTENT_ITEM_KEY;
226+
227+
ret = btrfs_alloc_reserved_extent(trans, root,
228+
root->root_key.objectid,
229+
root_gen, level,
230+
first_key.objectid,
231+
&ins);
232+
BUG_ON(ret);
233+
cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
234+
buf->len);
235+
} else {
236+
cow = btrfs_alloc_free_block(trans, root, buf->len,
237+
root->root_key.objectid,
238+
root_gen, first_key.objectid,
239+
level, search_start, empty_size);
240+
}
223241
if (IS_ERR(cow))
224242
return PTR_ERR(cow);
225243

@@ -279,7 +297,7 @@ int __btrfs_cow_block(struct btrfs_trans_handle *trans,
279297
int btrfs_cow_block(struct btrfs_trans_handle *trans,
280298
struct btrfs_root *root, struct extent_buffer *buf,
281299
struct extent_buffer *parent, int parent_slot,
282-
struct extent_buffer **cow_ret)
300+
struct extent_buffer **cow_ret, u64 prealloc_dest)
283301
{
284302
u64 search_start;
285303
u64 header_trans;
@@ -302,12 +320,14 @@ int btrfs_cow_block(struct btrfs_trans_handle *trans,
302320
!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
303321
*cow_ret = buf;
304322
spin_unlock(&root->fs_info->hash_lock);
323+
WARN_ON(prealloc_dest);
305324
return 0;
306325
}
307326
spin_unlock(&root->fs_info->hash_lock);
308327
search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
309328
ret = __btrfs_cow_block(trans, root, buf, parent,
310-
parent_slot, cow_ret, search_start, 0);
329+
parent_slot, cow_ret, search_start, 0,
330+
prealloc_dest);
311331
return ret;
312332
}
313333

@@ -451,7 +471,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
451471
err = __btrfs_cow_block(trans, root, cur, parent, i,
452472
&cur, search_start,
453473
min(16 * blocksize,
454-
(end_slot - i) * blocksize));
474+
(end_slot - i) * blocksize), 0);
455475
if (err) {
456476
btrfs_tree_unlock(cur);
457477
free_extent_buffer(cur);
@@ -803,7 +823,7 @@ static int balance_level(struct btrfs_trans_handle *trans,
803823
child = read_node_slot(root, mid, 0);
804824
btrfs_tree_lock(child);
805825
BUG_ON(!child);
806-
ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
826+
ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
807827
BUG_ON(ret);
808828

809829
spin_lock(&root->node_lock);
@@ -836,7 +856,7 @@ static int balance_level(struct btrfs_trans_handle *trans,
836856
if (left) {
837857
btrfs_tree_lock(left);
838858
wret = btrfs_cow_block(trans, root, left,
839-
parent, pslot - 1, &left);
859+
parent, pslot - 1, &left, 0);
840860
if (wret) {
841861
ret = wret;
842862
goto enospc;
@@ -846,7 +866,7 @@ static int balance_level(struct btrfs_trans_handle *trans,
846866
if (right) {
847867
btrfs_tree_lock(right);
848868
wret = btrfs_cow_block(trans, root, right,
849-
parent, pslot + 1, &right);
869+
parent, pslot + 1, &right, 0);
850870
if (wret) {
851871
ret = wret;
852872
goto enospc;
@@ -1021,7 +1041,7 @@ static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
10211041
wret = 1;
10221042
} else {
10231043
ret = btrfs_cow_block(trans, root, left, parent,
1024-
pslot - 1, &left);
1044+
pslot - 1, &left, 0);
10251045
if (ret)
10261046
wret = 1;
10271047
else {
@@ -1069,7 +1089,7 @@ static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
10691089
} else {
10701090
ret = btrfs_cow_block(trans, root, right,
10711091
parent, pslot + 1,
1072-
&right);
1092+
&right, 0);
10731093
if (ret)
10741094
wret = 1;
10751095
else {
@@ -1245,6 +1265,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
12451265
u8 lowest_level = 0;
12461266
u64 blocknr;
12471267
u64 gen;
1268+
struct btrfs_key prealloc_block;
12481269

12491270
lowest_level = p->lowest_level;
12501271
WARN_ON(lowest_level && ins_len);
@@ -1253,6 +1274,9 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
12531274
!mutex_is_locked(&root->fs_info->alloc_mutex));
12541275
if (ins_len < 0)
12551276
lowest_unlock = 2;
1277+
1278+
prealloc_block.objectid = 0;
1279+
12561280
again:
12571281
if (p->skip_locking)
12581282
b = btrfs_root_node(root);
@@ -1261,27 +1285,82 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
12611285

12621286
while (b) {
12631287
level = btrfs_header_level(b);
1288+
1289+
/*
1290+
* setup the path here so we can release it under lock
1291+
* contention with the cow code
1292+
*/
1293+
p->nodes[level] = b;
1294+
if (!p->skip_locking)
1295+
p->locks[level] = 1;
1296+
12641297
if (cow) {
12651298
int wret;
1299+
1300+
/* is a cow on this block not required */
1301+
spin_lock(&root->fs_info->hash_lock);
1302+
if (btrfs_header_generation(b) == trans->transid &&
1303+
!btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1304+
spin_unlock(&root->fs_info->hash_lock);
1305+
goto cow_done;
1306+
}
1307+
spin_unlock(&root->fs_info->hash_lock);
1308+
1309+
/* ok, we have to cow, is our old prealloc the right
1310+
* size?
1311+
*/
1312+
if (prealloc_block.objectid &&
1313+
prealloc_block.offset != b->len) {
1314+
btrfs_free_reserved_extent(root,
1315+
prealloc_block.objectid,
1316+
prealloc_block.offset);
1317+
prealloc_block.objectid = 0;
1318+
}
1319+
1320+
/*
1321+
* for higher level blocks, try not to allocate blocks
1322+
* with the block and the parent locks held.
1323+
*/
1324+
if (level > 1 && !prealloc_block.objectid &&
1325+
btrfs_path_lock_waiting(p, level)) {
1326+
u32 size = b->len;
1327+
u64 hint = b->start;
1328+
1329+
btrfs_release_path(root, p);
1330+
ret = btrfs_reserve_extent(trans, root,
1331+
size, size, 0,
1332+
hint, (u64)-1,
1333+
&prealloc_block, 0);
1334+
BUG_ON(ret);
1335+
goto again;
1336+
}
1337+
12661338
wret = btrfs_cow_block(trans, root, b,
12671339
p->nodes[level + 1],
12681340
p->slots[level + 1],
1269-
&b);
1341+
&b, prealloc_block.objectid);
1342+
prealloc_block.objectid = 0;
12701343
if (wret) {
12711344
free_extent_buffer(b);
1272-
return wret;
1345+
ret = wret;
1346+
goto done;
12731347
}
12741348
}
1349+
cow_done:
12751350
BUG_ON(!cow && ins_len);
12761351
if (level != btrfs_header_level(b))
12771352
WARN_ON(1);
12781353
level = btrfs_header_level(b);
1354+
12791355
p->nodes[level] = b;
12801356
if (!p->skip_locking)
12811357
p->locks[level] = 1;
1358+
12821359
ret = check_block(root, p, level);
1283-
if (ret)
1284-
return -1;
1360+
if (ret) {
1361+
ret = -1;
1362+
goto done;
1363+
}
12851364

12861365
ret = bin_search(b, key, level, &slot);
12871366
if (level != 0) {
@@ -1292,15 +1371,19 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
12921371
BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
12931372
int sret = split_node(trans, root, p, level);
12941373
BUG_ON(sret > 0);
1295-
if (sret)
1296-
return sret;
1374+
if (sret) {
1375+
ret = sret;
1376+
goto done;
1377+
}
12971378
b = p->nodes[level];
12981379
slot = p->slots[level];
12991380
} else if (ins_len < 0) {
13001381
int sret = balance_level(trans, root, p,
13011382
level);
1302-
if (sret)
1303-
return sret;
1383+
if (sret) {
1384+
ret = sret;
1385+
goto done;
1386+
}
13041387
b = p->nodes[level];
13051388
if (!b) {
13061389
btrfs_release_path(NULL, p);
@@ -1362,14 +1445,24 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
13621445
int sret = split_leaf(trans, root, key,
13631446
p, ins_len, ret == 0);
13641447
BUG_ON(sret > 0);
1365-
if (sret)
1366-
return sret;
1448+
if (sret) {
1449+
ret = sret;
1450+
goto done;
1451+
}
13671452
}
13681453
unlock_up(p, level, lowest_unlock);
1369-
return ret;
1454+
goto done;
13701455
}
13711456
}
1372-
return 1;
1457+
ret = 1;
1458+
done:
1459+
if (prealloc_block.objectid) {
1460+
btrfs_free_reserved_extent(root,
1461+
prealloc_block.objectid,
1462+
prealloc_block.offset);
1463+
}
1464+
1465+
return ret;
13731466
}
13741467

13751468
/*
@@ -1840,7 +1933,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
18401933

18411934
/* cow and double check */
18421935
ret = btrfs_cow_block(trans, root, right, upper,
1843-
slot + 1, &right);
1936+
slot + 1, &right, 0);
18441937
if (ret)
18451938
goto out_unlock;
18461939

@@ -2021,7 +2114,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
20212114

20222115
/* cow and double check */
20232116
ret = btrfs_cow_block(trans, root, left,
2024-
path->nodes[1], slot - 1, &left);
2117+
path->nodes[1], slot - 1, &left, 0);
20252118
if (ret) {
20262119
/* we hit -ENOSPC, but it isn't fatal here */
20272120
ret = 1;

‎fs/btrfs/ctree.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,9 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
14211421
int level,
14221422
u64 hint,
14231423
u64 empty_size);
1424+
struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
1425+
struct btrfs_root *root,
1426+
u64 bytenr, u32 blocksize);
14241427
int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size);
14251428
int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
14261429
struct btrfs_root *root,
@@ -1451,6 +1454,7 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
14511454
*root, u64 bytenr, u64 num_bytes,
14521455
u64 root_objectid, u64 ref_generation,
14531456
u64 owner_objectid, u64 owner_offset, int pin);
1457+
int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len);
14541458
int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
14551459
struct btrfs_root *root,
14561460
struct extent_io_tree *unpin);
@@ -1484,7 +1488,7 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
14841488
int btrfs_cow_block(struct btrfs_trans_handle *trans,
14851489
struct btrfs_root *root, struct extent_buffer *buf,
14861490
struct extent_buffer *parent, int parent_slot,
1487-
struct extent_buffer **cow_ret);
1491+
struct extent_buffer **cow_ret, u64 prealloc_dest);
14881492
int btrfs_copy_root(struct btrfs_trans_handle *trans,
14891493
struct btrfs_root *root,
14901494
struct extent_buffer *buf,

‎fs/btrfs/extent-tree.c‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,15 @@ static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
21182118
return 0;
21192119
}
21202120

2121+
int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2122+
{
2123+
maybe_lock_mutex(root);
2124+
set_extent_dirty(&root->fs_info->free_space_cache,
2125+
start, start + len - 1, GFP_NOFS);
2126+
maybe_unlock_mutex(root);
2127+
return 0;
2128+
}
2129+
21212130
int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
21222131
struct btrfs_root *root,
21232132
u64 num_bytes, u64 min_alloc_size,
@@ -2267,6 +2276,26 @@ int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
22672276
maybe_unlock_mutex(root);
22682277
return ret;
22692278
}
2279+
2280+
struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2281+
struct btrfs_root *root,
2282+
u64 bytenr, u32 blocksize)
2283+
{
2284+
struct extent_buffer *buf;
2285+
2286+
buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2287+
if (!buf)
2288+
return ERR_PTR(-ENOMEM);
2289+
btrfs_set_header_generation(buf, trans->transid);
2290+
btrfs_tree_lock(buf);
2291+
clean_tree_block(trans, root, buf);
2292+
btrfs_set_buffer_uptodate(buf);
2293+
set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2294+
buf->start + buf->len - 1, GFP_NOFS);
2295+
trans->blocks_used++;
2296+
return buf;
2297+
}
2298+
22702299
/*
22712300
* helper function to allocate a block for a given tree
22722301
* returns the tree buffer or NULL.
@@ -2293,26 +2322,8 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
22932322
BUG_ON(ret > 0);
22942323
return ERR_PTR(ret);
22952324
}
2296-
buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2297-
if (!buf) {
2298-
btrfs_free_extent(trans, root, ins.objectid, blocksize,
2299-
root->root_key.objectid, ref_generation,
2300-
0, 0, 0);
2301-
return ERR_PTR(-ENOMEM);
2302-
}
2303-
btrfs_set_header_generation(buf, trans->transid);
2304-
btrfs_tree_lock(buf);
2305-
clean_tree_block(trans, root, buf);
2306-
btrfs_set_buffer_uptodate(buf);
2307-
2308-
if (PageDirty(buf->first_page)) {
2309-
printk("page %lu dirty\n", buf->first_page->index);
2310-
WARN_ON(1);
2311-
}
23122325

2313-
set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2314-
buf->start + buf->len - 1, GFP_NOFS);
2315-
trans->blocks_used++;
2326+
buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
23162327
return buf;
23172328
}
23182329

0 commit comments

Comments
 (0)