bcachefs: Kill bkey_buf usage in data_update_index_update()
PATCH 1/3 bcachefs: Kill bkey_buf usage in data_update_index_update()
Reduce stack usage - bkey_buf has a 96 byte buffer on the stack, but the btree_trans bump allocator works just fine here.
Signed-off-by Kent Overstreet <[email protected]>
--- fs/bcachefs/data_update.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-)
fs/bcachefs/data_update.c +15 -11
@@ -187,14 +187,9 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,struct data_update *m = container_of(op, struct data_update, op);struct keylist *keys = &op->insert_keys; -struct bkey_buf _new, _insert;struct printbuf journal_msg =PRINTBUF ;int ret =0 ; -bch2_bkey_buf_init (&_new); -bch2_bkey_buf_init (&_insert); -bch2_bkey_buf_realloc (&_insert, c,U8_MAX ); -bch2_trans_iter_init (trans, &iter, m->btree_id,bkey_start_pos (&bch2_keylist_front (keys)->k), BTREE_ITER_slots|BTREE_ITER_intent);
@@ -229,11 +224,22 @@ static int __bch2_data_update_index_update(struct btree_trans *trans, goto nowork; } -bkey_reassemble (_insert.k, k); - insert = _insert.k; + insert =bch2_trans_kmalloc (trans, +bkey_bytes (k.k) + +bkey_val_bytes (&new->k) + +sizeof (struct bch_extent_rebalance )); + ret =PTR_ERR_OR_ZERO (insert); +if (ret) + goto err; + +bkey_reassemble (insert, k); + + new =bch2_trans_kmalloc (trans,bkey_bytes (&new->k)); + ret =PTR_ERR_OR_ZERO (new); +if (ret) + goto err; -bch2_bkey_buf_copy (&_new, c,bch2_keylist_front (keys)); - new =bkey_i_to_extent (_new.k); +bkey_copy (&new->k_i,bch2_keylist_front (keys));bch2_cut_front (iter.pos, &new->k_i);bch2_cut_front (iter.pos, insert);
@@ -457,8 +463,6 @@ static int __bch2_data_update_index_update(struct btree_trans *trans, out:printbuf_exit (&journal_msg);bch2_trans_iter_exit (trans, &iter); -bch2_bkey_buf_exit (&_insert, c); -bch2_bkey_buf_exit (&_new, c);BUG_ON (bch2_err_matches (ret, BCH_ERR_transaction_restart));return ret; }
Signature · 1 line
2.49.0
PATCH 2/3 bcachefs: bch2_trans_log_str()
The data update path doesn't need a printbuf for its log message - this will help reduce stack usage.
Signed-off-by Kent Overstreet <[email protected]>
--- fs/bcachefs/btree_update.c | 26 ++++++++++++++++++-------- fs/bcachefs/btree_update.h | 1 + 2 files changed, 19 insertions(+), 8 deletions(-)
fs/bcachefs/btree_update.c +18 -8
@@ -829,25 +829,35 @@ int bch2_btree_bit_mod_buffered(struct btree_trans *trans, enum btree_id btree,return bch2_trans_update_buffered (trans, btree, &k); } -int bch2_trans_log_msg (struct btree_trans *trans,struct printbuf *buf) +static int __bch2_trans_log_str (struct btree_trans *trans,const char *str,unsigned len) { -unsigned u64s =DIV_ROUND_UP (buf->pos,sizeof (u64)); - -int ret = buf->allocation_failure ? -BCH_ERR_ENOMEM_trans_log_msg :0 ; -if (ret) -return ret; +unsigned u64s =DIV_ROUND_UP (len,sizeof (u64));struct jset_entry *e =bch2_trans_jset_entry_alloc (trans,jset_u64s (u64s)); - ret =PTR_ERR_OR_ZERO (e); +int ret =PTR_ERR_OR_ZERO (e);if (ret)return ret; struct jset_entry_log *l = container_of(e, struct jset_entry_log, entry);journal_entry_init (e, BCH_JSET_ENTRY_log,0 ,1 , u64s); -memcpy_and_pad (l->d, u64s *sizeof (u64), buf->buf, buf->pos,0 ); +memcpy_and_pad (l->d, u64s *sizeof (u64), str, len,0 );return 0 ; } +int bch2_trans_log_str (struct btree_trans *trans,const char *str) +{ +return __bch2_trans_log_str (trans, str,strlen (str)); +} + +int bch2_trans_log_msg (struct btree_trans *trans,struct printbuf *buf) +{ +int ret = buf->allocation_failure ? -BCH_ERR_ENOMEM_trans_log_msg :0 ; +if (ret) +return ret; + +return __bch2_trans_log_str (trans, buf->buf, buf->pos); +} +int bch2_trans_log_bkey (struct btree_trans *trans,enum btree_id btree,unsigned level,struct bkey_i *k) {
fs/bcachefs/btree_update.h +1 -0
@@ -205,6 +205,7 @@ void bch2_trans_commit_hook(struct btree_trans *, struct btree_trans_commit_hook *);int __bch2_trans_commit (struct btree_trans *,unsigned ); +int bch2_trans_log_str (struct btree_trans *,const char *);int bch2_trans_log_msg (struct btree_trans *,struct printbuf *);int bch2_trans_log_bkey (struct btree_trans *,enum btree_id ,unsigned ,struct bkey_i *);
Signature · 1 line
2.49.0
PATCH 3/3 bcachefs: Reduce stack usage in data_update_index_update()
In reply to Kent OverstreetSeparate 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]>
--- fs/bcachefs/data_update.c | 154 +++++++++++++++++++++----------------- 1 file changed, 87 insertions(+), 67 deletions(-)
fs/bcachefs/data_update.c +87 -67
@@ -100,9 +100,10 @@ static bool bkey_nocow_lock(struct bch_fs *c, struct moving_context *ctxt, strucreturn true; } -static noinline void trace_io_move_finish2(struct data_update *u, -struct bkey_i *new, -struct bkey_i *insert) +noinline_for_stack +static void trace_io_move_finish2(struct data_update *u, +struct bkey_i *new, +struct bkey_i *insert) {struct bch_fs *c = u->op.c;struct printbuf buf =PRINTBUF ;
@@ -124,6 +125,7 @@ static noinline void trace_io_move_finish2(struct data_update *u,printbuf_exit (&buf); } +noinline_for_stack static void trace_io_move_fail2(struct data_update *m, struct bkey_s_c new, struct bkey_s_c wrote,
@@ -179,19 +181,84 @@ static void trace_io_move_fail2(struct data_update *m,printbuf_exit (&buf); } +noinline_for_stack +static void trace_data_update2(struct data_update *m, +struct bkey_s_c old,struct bkey_s_c k, +struct bkey_i *insert) +{ +struct bch_fs *c = m->op.c; +struct printbuf buf =PRINTBUF ; + +prt_str (&buf,"\nold: " ); +bch2_bkey_val_to_text (&buf, c, old); +prt_str (&buf,"\nk: " ); +bch2_bkey_val_to_text (&buf, c, k); +prt_str (&buf,"\nnew: " ); +bch2_bkey_val_to_text (&buf, c,bkey_i_to_s_c (insert)); + +trace_data_update (c, buf.buf); +printbuf_exit (&buf); +} + +noinline_for_stack +static void trace_io_move_created_rebalance2(struct data_update *m, +struct bkey_s_c old,struct bkey_s_c k, +struct bkey_i *insert) +{ +struct bch_fs *c = m->op.c; +struct printbuf buf =PRINTBUF ; + +bch2_data_update_opts_to_text (&buf, c, &m->op.opts, &m->data_opts); + +prt_str (&buf,"\nold: " ); +bch2_bkey_val_to_text (&buf, c, old); +prt_str (&buf,"\nk: " ); +bch2_bkey_val_to_text (&buf, c, k); +prt_str (&buf,"\nnew: " ); +bch2_bkey_val_to_text (&buf, c,bkey_i_to_s_c (insert)); + +trace_io_move_created_rebalance (c, buf.buf); +printbuf_exit (&buf); + +this_cpu_inc (c->counters[BCH_COUNTER_io_move_created_rebalance]); +} + +noinline_for_stack +static int data_update_invalid_bkey(struct data_update *m, +struct bkey_s_c old,struct bkey_s_c k, +struct bkey_i *insert) +{ +struct bch_fs *c = m->op.c; +struct printbuf buf =PRINTBUF ; +bch2_log_msg_start (c, &buf); + +prt_str (&buf,"about to insert invalid key in data update path" ); +prt_printf (&buf,"\nop.nonce: %u" , m->op.nonce); +prt_str (&buf,"\nold: " ); +bch2_bkey_val_to_text (&buf, c, old); +prt_str (&buf,"\nk: " ); +bch2_bkey_val_to_text (&buf, c, k); +prt_str (&buf,"\nnew: " ); +bch2_bkey_val_to_text (&buf, c,bkey_i_to_s_c (insert)); + +bch2_fs_emergency_read_only2 (c, &buf); + +bch2_print_str (c,KERN_ERR , buf.buf); +printbuf_exit (&buf); + +return -BCH_ERR_invalid_bkey; +} +static int __bch2_data_update_index_update (struct btree_trans *trans,struct bch_write_op *op) {struct bch_fs *c = op->c;struct btree_iter iter; -struct data_update *m = - container_of(op, struct data_update, op); -struct keylist *keys = &op->insert_keys; -struct printbuf journal_msg =PRINTBUF ; + struct data_update *m = container_of(op, struct data_update, op);int ret =0 ;bch2_trans_iter_init (trans, &iter, m->btree_id, -bkey_start_pos (&bch2_keylist_front (keys)->k), +bkey_start_pos (&bch2_keylist_front (&op->insert_keys)->k), BTREE_ITER_slots|BTREE_ITER_intent);while (1 ) {
@@ -216,11 +283,11 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,if (ret) goto err; - new =bkey_i_to_extent (bch2_keylist_front (keys)); + new =bkey_i_to_extent (bch2_keylist_front (&op->insert_keys));if (!bch2_extents_match (k, old)) {trace_io_move_fail2 (m, k,bkey_i_to_s_c (&new->k_i), -NULL ,"no match:" ); +NULL ,"no match:" ); goto nowork; }
@@ -239,7 +306,7 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,if (ret) goto err; -bkey_copy (&new->k_i,bch2_keylist_front (keys)); +bkey_copy (&new->k_i,bch2_keylist_front (&op->insert_keys));bch2_cut_front (iter.pos, &new->k_i);bch2_cut_front (iter.pos, insert);
@@ -353,31 +420,11 @@ static int __bch2_data_update_index_update(struct btree_trans *trans, .flags = BCH_VALIDATE_commit, });if (unlikely (invalid)) { -struct printbuf buf =PRINTBUF ; -bch2_log_msg_start (c, &buf); - -prt_str (&buf,"about to insert invalid key in data update path" ); -prt_printf (&buf,"\nop.nonce: %u" , m->op.nonce); -prt_str (&buf,"\nold: " ); -bch2_bkey_val_to_text (&buf, c, old); -prt_str (&buf,"\nk: " ); -bch2_bkey_val_to_text (&buf, c, k); -prt_str (&buf,"\nnew: " ); -bch2_bkey_val_to_text (&buf, c,bkey_i_to_s_c (insert)); - -bch2_fs_emergency_read_only2 (c, &buf); - -bch2_print_str (c,KERN_ERR , buf.buf); -printbuf_exit (&buf); - - ret = -BCH_ERR_invalid_bkey; + ret =data_update_invalid_bkey (m, old, k, insert); goto out; } -printbuf_reset (&journal_msg); -prt_str (&journal_msg, bch2_data_update_type_strs[m->type]); - - ret =bch2_trans_log_msg (trans, &journal_msg) ?: + ret =bch2_trans_log_str (trans, bch2_data_update_type_strs[m->type]) ?:bch2_trans_log_bkey (trans, m->btree_id,0 , m->k.k) ?:bch2_insert_snapshot_whiteouts (trans, m->btree_id, k.k->p, bkey_start_pos(&insert->k)) ?:
@@ -389,38 +436,12 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,if (ret) goto err; -if (trace_data_update_enabled ()) { -struct printbuf buf =PRINTBUF ; - -prt_str (&buf,"\nold: " ); -bch2_bkey_val_to_text (&buf, c, old); -prt_str (&buf,"\nk: " ); -bch2_bkey_val_to_text (&buf, c, k); -prt_str (&buf,"\nnew: " ); -bch2_bkey_val_to_text (&buf, c,bkey_i_to_s_c (insert)); - -trace_data_update (c, buf.buf); -printbuf_exit (&buf); - } +if (trace_data_update_enabled ()) +trace_data_update2 (m, old, k, insert);if (bch2_bkey_sectors_need_rebalance (c,bkey_i_to_s_c (insert)) * k.k->size > -bch2_bkey_sectors_need_rebalance (c, k) * insert->k.size) { -struct printbuf buf =PRINTBUF ; - -bch2_data_update_opts_to_text (&buf, c, &m->op.opts, &m->data_opts); - -prt_str (&buf,"\nold: " ); -bch2_bkey_val_to_text (&buf, c, old); -prt_str (&buf,"\nk: " ); -bch2_bkey_val_to_text (&buf, c, k); -prt_str (&buf,"\nnew: " ); -bch2_bkey_val_to_text (&buf, c,bkey_i_to_s_c (insert)); - -trace_io_move_created_rebalance (c, buf.buf); -printbuf_exit (&buf); - -this_cpu_inc (c->counters[BCH_COUNTER_io_move_created_rebalance]); - } +bch2_bkey_sectors_need_rebalance (c, k) * insert->k.size) +trace_io_move_created_rebalance2 (m, old, k, insert); ret = bch2_trans_commit(trans, &op->res, NULL,
@@ -441,9 +462,9 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,if (ret)break ; next: -while (bkey_ge (iter.pos,bch2_keylist_front (keys)->k.p)) { -bch2_keylist_pop_front (keys); -if (bch2_keylist_empty (keys)) +while (bkey_ge (iter.pos,bch2_keylist_front (&op->insert_keys)->k.p)) { +bch2_keylist_pop_front (&op->insert_keys); +if (bch2_keylist_empty (&op->insert_keys)) goto out; }continue ;
@@ -461,7 +482,6 @@ static int __bch2_data_update_index_update(struct btree_trans *trans, goto next; } out: -printbuf_exit (&journal_msg);bch2_trans_iter_exit (trans, &iter);BUG_ON (bch2_err_matches (ret, BCH_ERR_transaction_restart));return ret;
Signature · 1 line
2.49.0