Skip to content

Commit d385ca5

Browse files
author
Kent Overstreet
committed
bcachefs: Reduce stack usage in data_update_index_update()
Separate tracepoint message generation and other slowpath code into non-inline functions, and use bch2_trans_log_str() instead of using a printbuf for our journal message. Signed-off-by: Kent Overstreet <[email protected]>
1 parent 7d886a8 commit d385ca5

File tree

1 file changed

+87
-67
lines changed

1 file changed

+87
-67
lines changed

fs/bcachefs/data_update.c

Lines changed: 87 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ static bool bkey_nocow_lock(struct bch_fs *c, struct moving_context *ctxt, struc
100100
return true;
101101
}
102102

103-
static noinline void trace_io_move_finish2(struct data_update *u,
104-
struct bkey_i *new,
105-
struct bkey_i *insert)
103+
noinline_for_stack
104+
static void trace_io_move_finish2(struct data_update *u,
105+
struct bkey_i *new,
106+
struct bkey_i *insert)
106107
{
107108
struct bch_fs *c = u->op.c;
108109
struct printbuf buf = PRINTBUF;
@@ -124,6 +125,7 @@ static noinline void trace_io_move_finish2(struct data_update *u,
124125
printbuf_exit(&buf);
125126
}
126127

128+
noinline_for_stack
127129
static void trace_io_move_fail2(struct data_update *m,
128130
struct bkey_s_c new,
129131
struct bkey_s_c wrote,
@@ -179,19 +181,84 @@ static void trace_io_move_fail2(struct data_update *m,
179181
printbuf_exit(&buf);
180182
}
181183

184+
noinline_for_stack
185+
static void trace_data_update2(struct data_update *m,
186+
struct bkey_s_c old, struct bkey_s_c k,
187+
struct bkey_i *insert)
188+
{
189+
struct bch_fs *c = m->op.c;
190+
struct printbuf buf = PRINTBUF;
191+
192+
prt_str(&buf, "\nold: ");
193+
bch2_bkey_val_to_text(&buf, c, old);
194+
prt_str(&buf, "\nk: ");
195+
bch2_bkey_val_to_text(&buf, c, k);
196+
prt_str(&buf, "\nnew: ");
197+
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
198+
199+
trace_data_update(c, buf.buf);
200+
printbuf_exit(&buf);
201+
}
202+
203+
noinline_for_stack
204+
static void trace_io_move_created_rebalance2(struct data_update *m,
205+
struct bkey_s_c old, struct bkey_s_c k,
206+
struct bkey_i *insert)
207+
{
208+
struct bch_fs *c = m->op.c;
209+
struct printbuf buf = PRINTBUF;
210+
211+
bch2_data_update_opts_to_text(&buf, c, &m->op.opts, &m->data_opts);
212+
213+
prt_str(&buf, "\nold: ");
214+
bch2_bkey_val_to_text(&buf, c, old);
215+
prt_str(&buf, "\nk: ");
216+
bch2_bkey_val_to_text(&buf, c, k);
217+
prt_str(&buf, "\nnew: ");
218+
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
219+
220+
trace_io_move_created_rebalance(c, buf.buf);
221+
printbuf_exit(&buf);
222+
223+
this_cpu_inc(c->counters[BCH_COUNTER_io_move_created_rebalance]);
224+
}
225+
226+
noinline_for_stack
227+
static int data_update_invalid_bkey(struct data_update *m,
228+
struct bkey_s_c old, struct bkey_s_c k,
229+
struct bkey_i *insert)
230+
{
231+
struct bch_fs *c = m->op.c;
232+
struct printbuf buf = PRINTBUF;
233+
bch2_log_msg_start(c, &buf);
234+
235+
prt_str(&buf, "about to insert invalid key in data update path");
236+
prt_printf(&buf, "\nop.nonce: %u", m->op.nonce);
237+
prt_str(&buf, "\nold: ");
238+
bch2_bkey_val_to_text(&buf, c, old);
239+
prt_str(&buf, "\nk: ");
240+
bch2_bkey_val_to_text(&buf, c, k);
241+
prt_str(&buf, "\nnew: ");
242+
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
243+
244+
bch2_fs_emergency_read_only2(c, &buf);
245+
246+
bch2_print_str(c, KERN_ERR, buf.buf);
247+
printbuf_exit(&buf);
248+
249+
return -BCH_ERR_invalid_bkey;
250+
}
251+
182252
static int __bch2_data_update_index_update(struct btree_trans *trans,
183253
struct bch_write_op *op)
184254
{
185255
struct bch_fs *c = op->c;
186256
struct btree_iter iter;
187-
struct data_update *m =
188-
container_of(op, struct data_update, op);
189-
struct keylist *keys = &op->insert_keys;
190-
struct printbuf journal_msg = PRINTBUF;
257+
struct data_update *m = container_of(op, struct data_update, op);
191258
int ret = 0;
192259

193260
bch2_trans_iter_init(trans, &iter, m->btree_id,
194-
bkey_start_pos(&bch2_keylist_front(keys)->k),
261+
bkey_start_pos(&bch2_keylist_front(&op->insert_keys)->k),
195262
BTREE_ITER_slots|BTREE_ITER_intent);
196263

197264
while (1) {
@@ -216,11 +283,11 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
216283
if (ret)
217284
goto err;
218285

219-
new = bkey_i_to_extent(bch2_keylist_front(keys));
286+
new = bkey_i_to_extent(bch2_keylist_front(&op->insert_keys));
220287

221288
if (!bch2_extents_match(k, old)) {
222289
trace_io_move_fail2(m, k, bkey_i_to_s_c(&new->k_i),
223-
NULL, "no match:");
290+
NULL, "no match:");
224291
goto nowork;
225292
}
226293

@@ -239,7 +306,7 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
239306
if (ret)
240307
goto err;
241308

242-
bkey_copy(&new->k_i, bch2_keylist_front(keys));
309+
bkey_copy(&new->k_i, bch2_keylist_front(&op->insert_keys));
243310
bch2_cut_front(iter.pos, &new->k_i);
244311

245312
bch2_cut_front(iter.pos, insert);
@@ -353,31 +420,11 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
353420
.flags = BCH_VALIDATE_commit,
354421
});
355422
if (unlikely(invalid)) {
356-
struct printbuf buf = PRINTBUF;
357-
bch2_log_msg_start(c, &buf);
358-
359-
prt_str(&buf, "about to insert invalid key in data update path");
360-
prt_printf(&buf, "\nop.nonce: %u", m->op.nonce);
361-
prt_str(&buf, "\nold: ");
362-
bch2_bkey_val_to_text(&buf, c, old);
363-
prt_str(&buf, "\nk: ");
364-
bch2_bkey_val_to_text(&buf, c, k);
365-
prt_str(&buf, "\nnew: ");
366-
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
367-
368-
bch2_fs_emergency_read_only2(c, &buf);
369-
370-
bch2_print_str(c, KERN_ERR, buf.buf);
371-
printbuf_exit(&buf);
372-
373-
ret = -BCH_ERR_invalid_bkey;
423+
ret = data_update_invalid_bkey(m, old, k, insert);
374424
goto out;
375425
}
376426

377-
printbuf_reset(&journal_msg);
378-
prt_str(&journal_msg, bch2_data_update_type_strs[m->type]);
379-
380-
ret = bch2_trans_log_msg(trans, &journal_msg) ?:
427+
ret = bch2_trans_log_str(trans, bch2_data_update_type_strs[m->type]) ?:
381428
bch2_trans_log_bkey(trans, m->btree_id, 0, m->k.k) ?:
382429
bch2_insert_snapshot_whiteouts(trans, m->btree_id,
383430
k.k->p, bkey_start_pos(&insert->k)) ?:
@@ -389,38 +436,12 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
389436
if (ret)
390437
goto err;
391438

392-
if (trace_data_update_enabled()) {
393-
struct printbuf buf = PRINTBUF;
394-
395-
prt_str(&buf, "\nold: ");
396-
bch2_bkey_val_to_text(&buf, c, old);
397-
prt_str(&buf, "\nk: ");
398-
bch2_bkey_val_to_text(&buf, c, k);
399-
prt_str(&buf, "\nnew: ");
400-
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
401-
402-
trace_data_update(c, buf.buf);
403-
printbuf_exit(&buf);
404-
}
439+
if (trace_data_update_enabled())
440+
trace_data_update2(m, old, k, insert);
405441

406442
if (bch2_bkey_sectors_need_rebalance(c, bkey_i_to_s_c(insert)) * k.k->size >
407-
bch2_bkey_sectors_need_rebalance(c, k) * insert->k.size) {
408-
struct printbuf buf = PRINTBUF;
409-
410-
bch2_data_update_opts_to_text(&buf, c, &m->op.opts, &m->data_opts);
411-
412-
prt_str(&buf, "\nold: ");
413-
bch2_bkey_val_to_text(&buf, c, old);
414-
prt_str(&buf, "\nk: ");
415-
bch2_bkey_val_to_text(&buf, c, k);
416-
prt_str(&buf, "\nnew: ");
417-
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(insert));
418-
419-
trace_io_move_created_rebalance(c, buf.buf);
420-
printbuf_exit(&buf);
421-
422-
this_cpu_inc(c->counters[BCH_COUNTER_io_move_created_rebalance]);
423-
}
443+
bch2_bkey_sectors_need_rebalance(c, k) * insert->k.size)
444+
trace_io_move_created_rebalance2(m, old, k, insert);
424445

425446
ret = bch2_trans_commit(trans, &op->res,
426447
NULL,
@@ -441,9 +462,9 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
441462
if (ret)
442463
break;
443464
next:
444-
while (bkey_ge(iter.pos, bch2_keylist_front(keys)->k.p)) {
445-
bch2_keylist_pop_front(keys);
446-
if (bch2_keylist_empty(keys))
465+
while (bkey_ge(iter.pos, bch2_keylist_front(&op->insert_keys)->k.p)) {
466+
bch2_keylist_pop_front(&op->insert_keys);
467+
if (bch2_keylist_empty(&op->insert_keys))
447468
goto out;
448469
}
449470
continue;
@@ -461,7 +482,6 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
461482
goto next;
462483
}
463484
out:
464-
printbuf_exit(&journal_msg);
465485
bch2_trans_iter_exit(trans, &iter);
466486
BUG_ON(bch2_err_matches(ret, BCH_ERR_transaction_restart));
467487
return ret;

0 commit comments

Comments
 (0)