Skip to contentSkip to conversation

bcachefs: Plumb trans_kmalloc ip to trans_log_msg

PATCH In review Kent Overstreet 2 participants 5 messages Jun 19, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 bcachefs: Plumb trans_kmalloc ip to trans_log_msg

---
 fs/bcachefs/btree_update.c | 15 ++++++++-------
 fs/bcachefs/btree_update.h | 26 ++++++++++++++++++++------
 2 files changed, 28 insertions(+), 13 deletions(-)
fs/bcachefs/btree_update.c +8 -7
index e97e78c10f49..f7949dbe8f70 100644
@@ -546,7 +546,7 @@ int bch2_btree_insert_clone_trans(struct btree_trans *trans,
 
 void *__bch2_trans_subbuf_alloc(struct btree_trans *trans,
 				struct btree_trans_subbuf *buf,
-				unsigned u64s)
+				unsigned u64s, ulong ip)
 {
 	unsigned new_top = buf->u64s + u64s;
 	unsigned old_size = buf->size;
@@ -554,7 +554,7 @@ void *__bch2_trans_subbuf_alloc(struct btree_trans *trans,
 	if (new_top > buf->size)
 		buf->size = roundup_pow_of_two(new_top);
 
-	void *n = bch2_trans_kmalloc_nomemzero(trans, buf->size * sizeof(u64));
+	void *n = bch2_trans_kmalloc_nomemzero_ip(trans, buf->size * sizeof(u64), ip);
 	if (IS_ERR(n))
 		return n;
 
@@ -807,11 +807,11 @@ int bch2_btree_bit_mod_buffered(struct btree_trans *trans, enum btree_id btree,
 	return bch2_trans_update_buffered(trans, btree, &k);
 }
 
-static int __bch2_trans_log_str(struct btree_trans *trans, const char *str, unsigned len)
+static int __bch2_trans_log_str(struct btree_trans *trans, const char *str, unsigned len, ulong ip)
 {
 	unsigned u64s = DIV_ROUND_UP(len, sizeof(u64));
 
-	struct jset_entry *e = bch2_trans_jset_entry_alloc(trans, jset_u64s(u64s));
+	struct jset_entry *e = bch2_trans_jset_entry_alloc_ip(trans, jset_u64s(u64s), ip);
 	int ret = PTR_ERR_OR_ZERO(e);
 	if (ret)
 		return ret;
@@ -824,7 +824,7 @@ static int __bch2_trans_log_str(struct btree_trans *trans, const char *str, unsi
 
 int bch2_trans_log_str(struct btree_trans *trans, const char *str)
 {
-	return __bch2_trans_log_str(trans, str, strlen(str));
+	return __bch2_trans_log_str(trans, str, strlen(str), _RET_IP_);
 }
 
 int bch2_trans_log_msg(struct btree_trans *trans, struct printbuf *buf)
@@ -833,13 +833,14 @@ int bch2_trans_log_msg(struct btree_trans *trans, struct printbuf *buf)
 	if (ret)
 		return ret;
 
-	return __bch2_trans_log_str(trans, buf->buf, buf->pos);
+	return __bch2_trans_log_str(trans, buf->buf, buf->pos, _RET_IP_);
 }
 
 int bch2_trans_log_bkey(struct btree_trans *trans, enum btree_id btree,
 			unsigned level, struct bkey_i *k)
 {
-	struct jset_entry *e = bch2_trans_jset_entry_alloc(trans, jset_u64s(k->k.u64s));
+	struct jset_entry *e = bch2_trans_jset_entry_alloc_ip(trans,
+						jset_u64s(k->k.u64s), _RET_IP_);
 	int ret = PTR_ERR_OR_ZERO(e);
 	if (ret)
 		return ret;
fs/bcachefs/btree_update.h +20 -6
index 9feef1dc4de5..e4b6e7d51a2e 100644
@@ -137,21 +137,29 @@ static inline void *btree_trans_subbuf_top(struct btree_trans *trans,
 
 void *__bch2_trans_subbuf_alloc(struct btree_trans *,
 				struct btree_trans_subbuf *,
-				unsigned);
+				unsigned, ulong);
 
 static inline void *
-bch2_trans_subbuf_alloc(struct btree_trans *trans,
-			struct btree_trans_subbuf *buf,
-			unsigned u64s)
+bch2_trans_subbuf_alloc_ip(struct btree_trans *trans,
+			   struct btree_trans_subbuf *buf,
+			   unsigned u64s, ulong ip)
 {
 	if (buf->u64s + u64s > buf->size)
-		return __bch2_trans_subbuf_alloc(trans, buf, u64s);
+		return __bch2_trans_subbuf_alloc(trans, buf, u64s, ip);
 
 	void *p = btree_trans_subbuf_top(trans, buf);
 	buf->u64s += u64s;
 	return p;
 }
 
+static inline void *
+bch2_trans_subbuf_alloc(struct btree_trans *trans,
+			struct btree_trans_subbuf *buf,
+			unsigned u64s)
+{
+	return bch2_trans_subbuf_alloc_ip(trans, buf, u64s, _THIS_IP_);
+}
+
 static inline struct jset_entry *btree_trans_journal_entries_start(struct btree_trans *trans)
 {
 	return btree_trans_subbuf_base(trans, &trans->journal_entries);
@@ -162,10 +170,16 @@ static inline struct jset_entry *btree_trans_journal_entries_top(struct btree_tr
 	return btree_trans_subbuf_top(trans, &trans->journal_entries);
 }
 
+static inline struct jset_entry *
+bch2_trans_jset_entry_alloc_ip(struct btree_trans *trans, unsigned u64s, ulong ip)
+{
+	return bch2_trans_subbuf_alloc_ip(trans, &trans->journal_entries, u64s, ip);
+}
+
 static inline struct jset_entry *
 bch2_trans_jset_entry_alloc(struct btree_trans *trans, unsigned u64s)
 {
-	return bch2_trans_subbuf_alloc(trans, &trans->journal_entries, u64s);
+	return bch2_trans_jset_entry_alloc_ip(trans, u64s, _THIS_IP_);
 }
 
 int bch2_btree_insert_clone_trans(struct btree_trans *, enum btree_id, struct bkey_i *);
Signature · 1 line
2.50.0
#1 lore raw

PATCH 2/3 bcachefs: Fix *__bch2_trans_subbuf_alloc() error path

Don't change buf->size on error - this would usually be a transaction restart, but it could also be -ENOMEM - when we've exceeded the bump allocator max).

Fixes 247abee6ae6d ("bcachefs: btree_trans_subbuf")
---
 fs/bcachefs/btree_update.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
fs/bcachefs/btree_update.c +10 -3
index f7949dbe8f70..668d4feb2879 100644
@@ -550,19 +550,26 @@ void *__bch2_trans_subbuf_alloc(struct btree_trans *trans,
 {
 	unsigned new_top = buf->u64s + u64s;
 	unsigned old_size = buf->size;
+	unsigned new_size = buf->size;
 
-	if (new_top > buf->size)
-		buf->size = roundup_pow_of_two(new_top);
+	BUG_ON(roundup_pow_of_two(new_top) > U16_MAX);
 
-	void *n = bch2_trans_kmalloc_nomemzero_ip(trans, buf->size * sizeof(u64), ip);
+	if (new_top > new_size)
+		new_size = roundup_pow_of_two(new_top);
+
+	void *n = bch2_trans_kmalloc_nomemzero_ip(trans, new_size * sizeof(u64), ip);
 	if (IS_ERR(n))
 		return n;
 
+	unsigned offset = (u64 *) n - (u64 *) trans->mem;
+	BUG_ON(offset > U16_MAX);
+
 	if (buf->u64s)
 		memcpy(n,
 		       btree_trans_subbuf_base(trans, buf),
 		       old_size * sizeof(u64));
 	buf->base = (u64 *) n - (u64 *) trans->mem;
+	buf->size = new_size;
 
 	void *p = btree_trans_subbuf_top(trans, buf);
 	buf->u64s = new_top;
Signature · 1 line
2.50.0
#2 lore raw

Re: [PATCH 2/3] bcachefs: Fix *__bch2_trans_subbuf_alloc() error path

Quoted 25 lines · Kent Overstreet

Don't change buf->size on error - this would usually be a transaction restart, but it could also be -ENOMEM - when we've exceeded the bump allocator max).

Fixes 247abee6ae6d ("bcachefs: btree_trans_subbuf")

---
fs/bcachefs/btree_update.c | 13 ++++++++++---

1 file changed, 10 insertions(+), 3 deletions(-)
fs/bcachefs/btree_update.c +2 -2
index f7949dbe8f70..668d4feb2879 100644
@@ -550,19 +550,26 @@ void *__bch2_trans_subbuf_alloc(struct btree_trans *trans,
 {
 unsigned new_top = buf->u64s + u64s;
 unsigned old_size = buf->size;
+ unsigned new_size = buf->size;
 
- if (new_top > buf->size)
- buf->size = roundup_pow_of_two(new_top);
+ BUG_ON(roundup_pow_of_two(new_top) > U16_MAX);
 

fs/bcachefs/btree_update.c void *__bch2_trans_subbuf_alloc(struct btree_trans *trans, · line 565

Quoted 10 lines
fs/bcachefs/btree_update.c +6 -1
- void *n = bch2_trans_kmalloc_nomemzero_ip(trans, buf->size * sizeof(u64), ip);
+ if (new_top > new_size)
+ new_size = roundup_pow_of_two(new_top);
+
+ void *n = bch2_trans_kmalloc_nomemzero_ip(trans, new_size * sizeof(u64), ip);
 if (IS_ERR(n))
 return n;
 
+ unsigned offset = (u64 *) n - (u64 *) trans->mem;
+ BUG_ON(offset > U16_MAX);

offset * sizeof(u64) ?

fs/bcachefs/btree_update.c void *__bch2_trans_subbuf_alloc(struct btree_trans *trans, · line 570

Quoted 5 lines
fs/bcachefs/btree_update.c +1 -0
+
 if (buf->u64s)
 memcpy(n,
      btree_trans_subbuf_base(trans, buf),
      old_size * sizeof(u64));

copy buf->u64s is enough.

Quoted 8 lines
fs/bcachefs/btree_update.c +1 -0
 buf->base = (u64 *) n - (u64 *) trans->mem;
+ buf->size = new_size;
 
 void *p = btree_trans_subbuf_top(trans, buf);
 buf->u64s = new_top;
Signature · 1 line
2.50.0
#3 lore raw

Re: [PATCH 2/3] bcachefs: Fix *__bch2_trans_subbuf_alloc() error path

Quoted 35 lines · Alan Huang
Quoted 33 lines · Kent Overstreet

Don't change buf->size on error - this would usually be a transaction restart, but it could also be -ENOMEM - when we've exceeded the bump allocator max).

Fixes 247abee6ae6d ("bcachefs: btree_trans_subbuf")

---
fs/bcachefs/btree_update.c | 13 ++++++++++---

1 file changed, 10 insertions(+), 3 deletions(-)
fs/bcachefs/btree_update.c +6 -3
index f7949dbe8f70..668d4feb2879 100644
@@ -550,19 +550,26 @@ void *__bch2_trans_subbuf_alloc(struct btree_trans *trans,
 {
 unsigned new_top = buf->u64s + u64s;
 unsigned old_size = buf->size;
+ unsigned new_size = buf->size;
 
- if (new_top > buf->size)
- buf->size = roundup_pow_of_two(new_top);
+ BUG_ON(roundup_pow_of_two(new_top) > U16_MAX);
 
- void *n = bch2_trans_kmalloc_nomemzero_ip(trans, buf->size * sizeof(u64), ip);
+ if (new_top > new_size)
+ new_size = roundup_pow_of_two(new_top);
+
+ void *n = bch2_trans_kmalloc_nomemzero_ip(trans, new_size * sizeof(u64), ip);
 if (IS_ERR(n))
 return n;
 
Quoted 4 lines

fs/bcachefs/btree_update.c void *__bch2_trans_subbuf_alloc(struct btree_trans *trans, · line 565

Quoted 2 lines
fs/bcachefs/btree_update.c +2 -0
+ unsigned offset = (u64 *) n - (u64 *) trans->mem;
+ BUG_ON(offset > U16_MAX);

offset * sizeof(u64) ?

btree_trans_subbuf fields are u16s, we're just concerned about integer overflows here

Quoted 8 lines

fs/bcachefs/btree_update.c void *__bch2_trans_subbuf_alloc(struct btree_trans *trans, · line 570

Quoted 6 lines
fs/bcachefs/btree_update.c +1 -0
+
 if (buf->u64s)
 memcpy(n,
      btree_trans_subbuf_base(trans, buf),
      old_size * sizeof(u64));

copy buf->u64s is enough.

yup, already spotted that

Quoted 10 lines
Quoted 9 lines
fs/bcachefs/btree_update.c +1 -0
 buf->base = (u64 *) n - (u64 *) trans->mem;
+ buf->size = new_size;
 
 void *p = btree_trans_subbuf_top(trans, buf);
 buf->u64s = new_top;
Signature · 1 line
2.50.0
#4 lore raw

PATCH 3/3 bcachefs: WARN() in bch2_trans_put() if we have pending updates

In reply to Kent Overstreet

If we exit from a btree transaction with uncommitted updates, that's probably not what we intended to do.

---
 fs/bcachefs/btree_iter.c         | 2 ++
 fs/bcachefs/btree_trans_commit.c | 4 +---
 fs/bcachefs/btree_update.h       | 7 +++++++
 3 files changed, 10 insertions(+), 3 deletions(-)
fs/bcachefs/btree_iter.c +2 -0
index bc3f38f7044c..7ce2ed4ac509 100644
@@ -3551,6 +3551,8 @@ void bch2_trans_put(struct btree_trans *trans)
 {
 	struct bch_fs *c = trans->c;
 
+	WARN_ON(bch2_trans_has_updates(trans));
+
 	if (trans->restarted)
 		bch2_trans_in_restart_error(trans);
 
fs/bcachefs/btree_trans_commit.c +1 -3
index 639ef75b3dbd..454b4c5c1808 100644
@@ -1015,9 +1015,7 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
 	if (unlikely(ret))
 		goto out_reset;
 
-	if (!trans->nr_updates &&
-	    !trans->journal_entries.u64s &&
-	    !trans->accounting.u64s)
+	if (!bch2_trans_has_updates(trans))
 		goto out_reset;
 
 	ret = bch2_trans_commit_run_triggers(trans);
fs/bcachefs/btree_update.h +7 -0
index e4b6e7d51a2e..54c71e3bad63 100644
@@ -272,6 +272,13 @@ static inline int bch2_trans_commit(struct btree_trans *trans,
 	     (_i) < (_trans)->updates + (_trans)->nr_updates;		\
 	     (_i)++)
 
+static inline bool bch2_trans_has_updates(struct btree_trans *trans)
+{
+	return trans->nr_updates ||
+		trans->journal_entries.u64s ||
+		trans->accounting.u64s;
+}
+
 static inline void bch2_trans_reset_updates(struct btree_trans *trans)
 {
 	trans_for_each_update(trans, i)
Signature · 1 line
2.50.0