When alloc percpu counters in bch2_fs_counters_init, we do not
initialize it. When nr < BCH_COUNTER_NR, the variables after
counters[nr - 1] are not initialized. Considering __GFP_ZERO flags
aren't guaranteed to be all zeros on different archs for percpu
allocations, so here initialize this variable explicitly.
Fixes: 104c69745fdf ("bcachefs: Add persistent counters")
Signed-off-by: Hongbo Li <[email protected]>
---
fs/bcachefs/sb-counters.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/fs/bcachefs/sb-counters.c b/fs/bcachefs/sb-counters.c
index 6992e7469112..92cb3745d1c2 100644
--- a/fs/bcachefs/sb-counters.c
+++ b/fs/bcachefs/sb-counters.c
@@ -50,7 +50,7 @@ int bch2_sb_counters_to_cpu(struct bch_fs *c)
for (i = 0; i < min_t(unsigned int, nr, BCH_COUNTER_NR); i++) {
val = le64_to_cpu(ctrs->d[i]);
- percpu_u64_set(&c->counters[i], val);
+ this_cpu_write(c->counters[i], val);
c->counters_on_mount[i] = val;
}
return 0;
@@ -86,10 +86,15 @@ void bch2_fs_counters_exit(struct bch_fs *c)
int bch2_fs_counters_init(struct bch_fs *c)
{
+ unsigned int i;
+
c->counters = __alloc_percpu(sizeof(u64) * BCH_COUNTER_NR, sizeof(u64));
if (!c->counters)
return -BCH_ERR_ENOMEM_fs_counters_init;
+ for (i = 0; i < BCH_COUNTER_NR; i++)
+ percpu_u64_set(&c->counters[i], 0);
+
return bch2_sb_counters_to_cpu(c);
}
--
2.34.1