Skip to content

Commit b2348fe

Browse files
author
Kent Overstreet
committed
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: 247abee ("bcachefs: btree_trans_subbuf") Signed-off-by: Kent Overstreet <[email protected]>
1 parent 4346359 commit b2348fe

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

fs/bcachefs/btree_update.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,20 +549,26 @@ void *__bch2_trans_subbuf_alloc(struct btree_trans *trans,
549549
unsigned u64s)
550550
{
551551
unsigned new_top = buf->u64s + u64s;
552-
unsigned old_size = buf->size;
552+
unsigned new_size = buf->size;
553553

554-
if (new_top > buf->size)
555-
buf->size = roundup_pow_of_two(new_top);
554+
BUG_ON(roundup_pow_of_two(new_top) > U16_MAX);
556555

557-
void *n = bch2_trans_kmalloc_nomemzero(trans, buf->size * sizeof(u64));
556+
if (new_top > new_size)
557+
new_size = roundup_pow_of_two(new_top);
558+
559+
void *n = bch2_trans_kmalloc_nomemzero(trans, new_size * sizeof(u64));
558560
if (IS_ERR(n))
559561
return n;
560562

563+
unsigned offset = (u64 *) n - (u64 *) trans->mem;
564+
BUG_ON(offset > U16_MAX);
565+
561566
if (buf->u64s)
562567
memcpy(n,
563568
btree_trans_subbuf_base(trans, buf),
564-
old_size * sizeof(u64));
569+
buf->size * sizeof(u64));
565570
buf->base = (u64 *) n - (u64 *) trans->mem;
571+
buf->size = new_size;
566572

567573
void *p = btree_trans_subbuf_top(trans, buf);
568574
buf->u64s = new_top;

0 commit comments

Comments
 (0)