Commit e3728b50 authored by Justin Husted's avatar Justin Husted Committed by Kent Overstreet

bcachefs: Initialize padding space after alloc bkey

Packed bkeys are padded up to 64 bit alignment, but the alloc bkey type
was not clearing the pad bytes after the last data byte. This left the
key possibly containing some random garbage at the end.

This problem was found using valgrind.

This patch also changes a path with the inode bkey to clear in the same
way.
Signed-off-by: default avatarJustin Husted <sigstop@gmail.com>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent e2199655
...@@ -152,6 +152,7 @@ void bch2_alloc_pack(struct bkey_i_alloc *dst, ...@@ -152,6 +152,7 @@ void bch2_alloc_pack(struct bkey_i_alloc *dst,
{ {
unsigned idx = 0; unsigned idx = 0;
void *d = dst->v.data; void *d = dst->v.data;
unsigned bytes;
dst->v.fields = 0; dst->v.fields = 0;
dst->v.gen = src.gen; dst->v.gen = src.gen;
...@@ -160,7 +161,9 @@ void bch2_alloc_pack(struct bkey_i_alloc *dst, ...@@ -160,7 +161,9 @@ void bch2_alloc_pack(struct bkey_i_alloc *dst,
BCH_ALLOC_FIELDS() BCH_ALLOC_FIELDS()
#undef x #undef x
set_bkey_val_bytes(&dst->k, (void *) d - (void *) &dst->v); bytes = (void *) d - (void *) &dst->v;
set_bkey_val_bytes(&dst->k, bytes);
memset_u64s_tail(&dst->v, 0, bytes);
} }
static unsigned bch_alloc_val_u64s(const struct bch_alloc *a) static unsigned bch_alloc_val_u64s(const struct bch_alloc *a)
......
...@@ -95,6 +95,7 @@ void bch2_inode_pack(struct bkey_inode_buf *packed, ...@@ -95,6 +95,7 @@ void bch2_inode_pack(struct bkey_inode_buf *packed,
u8 *end = (void *) &packed[1]; u8 *end = (void *) &packed[1];
u8 *last_nonzero_field = out; u8 *last_nonzero_field = out;
unsigned nr_fields = 0, last_nonzero_fieldnr = 0; unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
unsigned bytes;
bkey_inode_init(&packed->inode.k_i); bkey_inode_init(&packed->inode.k_i);
packed->inode.k.p.inode = inode->bi_inum; packed->inode.k.p.inode = inode->bi_inum;
...@@ -117,10 +118,9 @@ void bch2_inode_pack(struct bkey_inode_buf *packed, ...@@ -117,10 +118,9 @@ void bch2_inode_pack(struct bkey_inode_buf *packed,
out = last_nonzero_field; out = last_nonzero_field;
nr_fields = last_nonzero_fieldnr; nr_fields = last_nonzero_fieldnr;
set_bkey_val_bytes(&packed->inode.k, out - (u8 *) &packed->inode.v); bytes = out - (u8 *) &packed->inode.v;
memset(out, 0, set_bkey_val_bytes(&packed->inode.k, bytes);
(u8 *) &packed->inode.v + memset_u64s_tail(&packed->inode.v, 0, bytes);
bkey_val_bytes(&packed->inode.k) - out);
SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields); SET_INODE_NR_FIELDS(&packed->inode.v, nr_fields);
......
...@@ -648,6 +648,14 @@ static inline void memmove_u64s(void *dst, const void *src, ...@@ -648,6 +648,14 @@ static inline void memmove_u64s(void *dst, const void *src,
__memmove_u64s_up(dst, src, u64s); __memmove_u64s_up(dst, src, u64s);
} }
/* Set the last few bytes up to a u64 boundary given an offset into a buffer. */
static inline void memset_u64s_tail(void *s, int c, unsigned bytes)
{
unsigned rem = round_up(bytes, sizeof(u64)) - bytes;
memset(s + bytes, c, rem);
}
void sort_cmp_size(void *base, size_t num, size_t size, void sort_cmp_size(void *base, size_t num, size_t size,
int (*cmp_func)(const void *, const void *, size_t), int (*cmp_func)(const void *, const void *, size_t),
void (*swap_func)(void *, void *, size_t)); void (*swap_func)(void *, void *, size_t));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment