Skip to content

Commit f4a584f

Browse files
author
Kent Overstreet
committed
bcachefs: bch2_disk_accounting_mod2()
We're hitting some issues with uninitialized struct padding, flagged by kmsan. They appear to be falso positives, otherwise bch2_accounting_validate() would have flagged them as "junk at end". But for now, we'll need to initialize disk_accounting_pos with memset(). This adds a new helper, bch2_disk_accounting_mod2(), that initializes a disk_accounting_pos and does the accounting mod all at once - so overall things actually get slightly more ergonomic. BCH_DISK_ACCOUNTING_replicas keys are left for now; KMSAN isn't warning about them and they're a bit special. Signed-off-by: Kent Overstreet <[email protected]>
1 parent 5ae6f33 commit f4a584f

File tree

6 files changed

+57
-65
lines changed

6 files changed

+57
-65
lines changed

fs/bcachefs/alloc_background.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -777,14 +777,12 @@ static inline int bch2_dev_data_type_accounting_mod(struct btree_trans *trans, s
777777
s64 delta_sectors,
778778
s64 delta_fragmented, unsigned flags)
779779
{
780-
struct disk_accounting_pos acc = {
781-
.type = BCH_DISK_ACCOUNTING_dev_data_type,
782-
.dev_data_type.dev = ca->dev_idx,
783-
.dev_data_type.data_type = data_type,
784-
};
785780
s64 d[3] = { delta_buckets, delta_sectors, delta_fragmented };
786781

787-
return bch2_disk_accounting_mod(trans, &acc, d, 3, flags & BTREE_TRIGGER_gc);
782+
return bch2_disk_accounting_mod2(trans, flags & BTREE_TRIGGER_gc,
783+
d, dev_data_type,
784+
.dev = ca->dev_idx,
785+
.data_type = data_type);
788786
}
789787

790788
int bch2_alloc_key_to_dev_counters(struct btree_trans *trans, struct bch_dev *ca,

fs/bcachefs/buckets.c

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,7 @@ static int __trigger_extent(struct btree_trans *trans,
724724
.replicas.nr_required = 1,
725725
};
726726

727-
struct disk_accounting_pos acct_compression_key = {
728-
.type = BCH_DISK_ACCOUNTING_compression,
729-
};
727+
unsigned cur_compression_type = 0;
730728
u64 compression_acct[3] = { 1, 0, 0 };
731729

732730
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
@@ -760,13 +758,13 @@ static int __trigger_extent(struct btree_trans *trans,
760758
acc_replicas_key.replicas.nr_required = 0;
761759
}
762760

763-
if (acct_compression_key.compression.type &&
764-
acct_compression_key.compression.type != p.crc.compression_type) {
761+
if (cur_compression_type &&
762+
cur_compression_type != p.crc.compression_type) {
765763
if (flags & BTREE_TRIGGER_overwrite)
766764
bch2_u64s_neg(compression_acct, ARRAY_SIZE(compression_acct));
767765

768-
ret = bch2_disk_accounting_mod(trans, &acct_compression_key, compression_acct,
769-
ARRAY_SIZE(compression_acct), gc);
766+
ret = bch2_disk_accounting_mod2(trans, gc, compression_acct,
767+
compression, cur_compression_type);
770768
if (ret)
771769
return ret;
772770

@@ -775,7 +773,7 @@ static int __trigger_extent(struct btree_trans *trans,
775773
compression_acct[2] = 0;
776774
}
777775

778-
acct_compression_key.compression.type = p.crc.compression_type;
776+
cur_compression_type = p.crc.compression_type;
779777
if (p.crc.compression_type) {
780778
compression_acct[1] += p.crc.uncompressed_size;
781779
compression_acct[2] += p.crc.compressed_size;
@@ -789,45 +787,34 @@ static int __trigger_extent(struct btree_trans *trans,
789787
}
790788

791789
if (acc_replicas_key.replicas.nr_devs && !level && k.k->p.snapshot) {
792-
struct disk_accounting_pos acc_snapshot_key = {
793-
.type = BCH_DISK_ACCOUNTING_snapshot,
794-
.snapshot.id = k.k->p.snapshot,
795-
};
796-
ret = bch2_disk_accounting_mod(trans, &acc_snapshot_key, replicas_sectors, 1, gc);
790+
ret = bch2_disk_accounting_mod2_nr(trans, gc, replicas_sectors, 1, snapshot, k.k->p.snapshot);
797791
if (ret)
798792
return ret;
799793
}
800794

801-
if (acct_compression_key.compression.type) {
795+
if (cur_compression_type) {
802796
if (flags & BTREE_TRIGGER_overwrite)
803797
bch2_u64s_neg(compression_acct, ARRAY_SIZE(compression_acct));
804798

805-
ret = bch2_disk_accounting_mod(trans, &acct_compression_key, compression_acct,
806-
ARRAY_SIZE(compression_acct), gc);
799+
ret = bch2_disk_accounting_mod2(trans, gc, compression_acct,
800+
compression, cur_compression_type);
807801
if (ret)
808802
return ret;
809803
}
810804

811805
if (level) {
812-
struct disk_accounting_pos acc_btree_key = {
813-
.type = BCH_DISK_ACCOUNTING_btree,
814-
.btree.id = btree_id,
815-
};
816-
ret = bch2_disk_accounting_mod(trans, &acc_btree_key, replicas_sectors, 1, gc);
806+
ret = bch2_disk_accounting_mod2_nr(trans, gc, replicas_sectors, 1, btree, btree_id);
817807
if (ret)
818808
return ret;
819809
} else {
820810
bool insert = !(flags & BTREE_TRIGGER_overwrite);
821-
struct disk_accounting_pos acc_inum_key = {
822-
.type = BCH_DISK_ACCOUNTING_inum,
823-
.inum.inum = k.k->p.inode,
824-
};
811+
825812
s64 v[3] = {
826813
insert ? 1 : -1,
827814
insert ? k.k->size : -((s64) k.k->size),
828815
*replicas_sectors,
829816
};
830-
ret = bch2_disk_accounting_mod(trans, &acc_inum_key, v, ARRAY_SIZE(v), gc);
817+
ret = bch2_disk_accounting_mod2(trans, gc, v, inum, k.k->p.inode);
831818
if (ret)
832819
return ret;
833820
}
@@ -876,15 +863,15 @@ int bch2_trigger_extent(struct btree_trans *trans,
876863
}
877864

878865
int need_rebalance_delta = 0;
879-
s64 need_rebalance_sectors_delta = 0;
866+
s64 need_rebalance_sectors_delta[1] = { 0 };
880867

881868
s64 s = bch2_bkey_sectors_need_rebalance(c, old);
882869
need_rebalance_delta -= s != 0;
883-
need_rebalance_sectors_delta -= s;
870+
need_rebalance_sectors_delta[0] -= s;
884871

885872
s = bch2_bkey_sectors_need_rebalance(c, new.s_c);
886873
need_rebalance_delta += s != 0;
887-
need_rebalance_sectors_delta += s;
874+
need_rebalance_sectors_delta[0] += s;
888875

889876
if ((flags & BTREE_TRIGGER_transactional) && need_rebalance_delta) {
890877
int ret = bch2_btree_bit_mod_buffered(trans, BTREE_ID_rebalance_work,
@@ -893,12 +880,9 @@ int bch2_trigger_extent(struct btree_trans *trans,
893880
return ret;
894881
}
895882

896-
if (need_rebalance_sectors_delta) {
897-
struct disk_accounting_pos acc = {
898-
.type = BCH_DISK_ACCOUNTING_rebalance_work,
899-
};
900-
int ret = bch2_disk_accounting_mod(trans, &acc, &need_rebalance_sectors_delta, 1,
901-
flags & BTREE_TRIGGER_gc);
883+
if (need_rebalance_sectors_delta[0]) {
884+
int ret = bch2_disk_accounting_mod2(trans, flags & BTREE_TRIGGER_gc,
885+
need_rebalance_sectors_delta, rebalance_work);
902886
if (ret)
903887
return ret;
904888
}
@@ -914,17 +898,13 @@ static int __trigger_reservation(struct btree_trans *trans,
914898
enum btree_iter_update_trigger_flags flags)
915899
{
916900
if (flags & (BTREE_TRIGGER_transactional|BTREE_TRIGGER_gc)) {
917-
s64 sectors = k.k->size;
901+
s64 sectors[1] = { k.k->size };
918902

919903
if (flags & BTREE_TRIGGER_overwrite)
920-
sectors = -sectors;
921-
922-
struct disk_accounting_pos acc = {
923-
.type = BCH_DISK_ACCOUNTING_persistent_reserved,
924-
.persistent_reserved.nr_replicas = bkey_s_c_to_reservation(k).v->nr_replicas,
925-
};
904+
sectors[0] = -sectors[0];
926905

927-
return bch2_disk_accounting_mod(trans, &acc, &sectors, 1, flags & BTREE_TRIGGER_gc);
906+
return bch2_disk_accounting_mod2(trans, flags & BTREE_TRIGGER_gc, sectors,
907+
persistent_reserved, bkey_s_c_to_reservation(k).v->nr_replicas);
928908
}
929909

930910
return 0;

fs/bcachefs/disk_accounting.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ static inline struct bpos disk_accounting_pos_to_bpos(struct disk_accounting_pos
8585

8686
int bch2_disk_accounting_mod(struct btree_trans *, struct disk_accounting_pos *,
8787
s64 *, unsigned, bool);
88+
89+
#define disk_accounting_key_init(_k, _type, ...) \
90+
do { \
91+
memset(&(_k), 0, sizeof(_k)); \
92+
(_k).type = BCH_DISK_ACCOUNTING_##_type; \
93+
(_k)._type = (struct bch_acct_##_type) { __VA_ARGS__ }; \
94+
} while (0)
95+
96+
#define bch2_disk_accounting_mod2_nr(_trans, _gc, _v, _nr, ...) \
97+
({ \
98+
struct disk_accounting_pos pos; \
99+
disk_accounting_key_init(pos, __VA_ARGS__); \
100+
bch2_disk_accounting_mod(trans, &pos, _v, _nr, _gc); \
101+
})
102+
103+
#define bch2_disk_accounting_mod2(_trans, _gc, _v, ...) \
104+
bch2_disk_accounting_mod2_nr(_trans, _gc, _v, ARRAY_SIZE(_v), __VA_ARGS__)
105+
88106
int bch2_mod_dev_cached_sectors(struct btree_trans *, unsigned, s64, bool);
89107

90108
int bch2_accounting_validate(struct bch_fs *, struct bkey_s_c,

fs/bcachefs/disk_accounting_format.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ enum disk_accounting_type {
113113
BCH_DISK_ACCOUNTING_TYPE_NR,
114114
};
115115

116-
struct bch_nr_inodes {
116+
struct bch_acct_nr_inodes {
117117
};
118118

119-
struct bch_persistent_reserved {
119+
struct bch_acct_persistent_reserved {
120120
__u8 nr_replicas;
121121
};
122122

123-
struct bch_dev_data_type {
123+
struct bch_acct_dev_data_type {
124124
__u8 dev;
125125
__u8 data_type;
126126
};
@@ -149,10 +149,10 @@ struct disk_accounting_pos {
149149
struct {
150150
__u8 type;
151151
union {
152-
struct bch_nr_inodes nr_inodes;
153-
struct bch_persistent_reserved persistent_reserved;
152+
struct bch_acct_nr_inodes nr_inodes;
153+
struct bch_acct_persistent_reserved persistent_reserved;
154154
struct bch_replicas_entry_v1 replicas;
155-
struct bch_dev_data_type dev_data_type;
155+
struct bch_acct_dev_data_type dev_data_type;
156156
struct bch_acct_compression compression;
157157
struct bch_acct_snapshot snapshot;
158158
struct bch_acct_btree btree;

fs/bcachefs/inode.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,9 @@ int bch2_trigger_inode(struct btree_trans *trans,
731731
bkey_s_to_inode_v3(new).v->bi_journal_seq = cpu_to_le64(trans->journal_res.seq);
732732
}
733733

734-
s64 nr = bkey_is_inode(new.k) - bkey_is_inode(old.k);
735-
if ((flags & (BTREE_TRIGGER_transactional|BTREE_TRIGGER_gc)) && nr) {
736-
struct disk_accounting_pos acc = { .type = BCH_DISK_ACCOUNTING_nr_inodes };
737-
int ret = bch2_disk_accounting_mod(trans, &acc, &nr, 1, flags & BTREE_TRIGGER_gc);
734+
s64 nr[1] = { bkey_is_inode(new.k) - bkey_is_inode(old.k) };
735+
if ((flags & (BTREE_TRIGGER_transactional|BTREE_TRIGGER_gc)) && nr[0]) {
736+
int ret = bch2_disk_accounting_mod2(trans, flags & BTREE_TRIGGER_gc, nr, nr_inodes);
738737
if (ret)
739738
return ret;
740739
}

fs/bcachefs/super.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,15 +1990,12 @@ int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
19901990
mutex_unlock(&c->sb_lock);
19911991

19921992
if (ca->mi.freespace_initialized) {
1993-
struct disk_accounting_pos acc = {
1994-
.type = BCH_DISK_ACCOUNTING_dev_data_type,
1995-
.dev_data_type.dev = ca->dev_idx,
1996-
.dev_data_type.data_type = BCH_DATA_free,
1997-
};
19981993
u64 v[3] = { nbuckets - old_nbuckets, 0, 0 };
19991994

20001995
ret = bch2_trans_commit_do(ca->fs, NULL, NULL, 0,
2001-
bch2_disk_accounting_mod(trans, &acc, v, ARRAY_SIZE(v), false)) ?:
1996+
bch2_disk_accounting_mod2(trans, false, v, dev_data_type,
1997+
.dev = ca->dev_idx,
1998+
.data_type = BCH_DATA_free)) ?:
20021999
bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets);
20032000
if (ret)
20042001
goto err;

0 commit comments

Comments
 (0)