Skip to content

Commit 691f2cb

Browse files
author
Kent Overstreet
committed
bcachefs: btree cache counters should be size_t
32 bits won't overflow any time soon, but size_t is the correct type for counting objects in memory. Signed-off-by: Kent Overstreet <[email protected]>
1 parent ad5dbe3 commit 691f2cb

File tree

6 files changed

+37
-36
lines changed

6 files changed

+37
-36
lines changed

fs/bcachefs/btree_cache.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ const char * const bch2_btree_node_flags[] = {
3232

3333
void bch2_recalc_btree_reserve(struct bch_fs *c)
3434
{
35-
unsigned i, reserve = 16;
35+
unsigned reserve = 16;
3636

3737
if (!c->btree_roots_known[0].b)
3838
reserve += 8;
3939

40-
for (i = 0; i < btree_id_nr_alive(c); i++) {
40+
for (unsigned i = 0; i < btree_id_nr_alive(c); i++) {
4141
struct btree_root *r = bch2_btree_id_root(c, i);
4242

4343
if (r->b)
4444
reserve += min_t(unsigned, 1, r->b->c.level) * 8;
4545
}
4646

47-
c->btree_cache.reserve = reserve;
47+
c->btree_cache.nr_reserve = reserve;
4848
}
4949

50-
static inline unsigned btree_cache_can_free(struct btree_cache *bc)
50+
static inline size_t btree_cache_can_free(struct btree_cache *bc)
5151
{
52-
return max_t(int, 0, bc->used - bc->reserve);
52+
return max_t(int, 0, bc->nr_used - bc->nr_reserve);
5353
}
5454

5555
static void btree_node_to_freedlist(struct btree_cache *bc, struct btree *b)
@@ -87,7 +87,7 @@ static void btree_node_data_free(struct bch_fs *c, struct btree *b)
8787
#endif
8888
b->aux_data = NULL;
8989

90-
bc->used--;
90+
bc->nr_used--;
9191

9292
btree_node_to_freedlist(bc, b);
9393
}
@@ -167,7 +167,7 @@ struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *c)
167167

168168
bch2_btree_lock_init(&b->c, 0);
169169

170-
bc->used++;
170+
bc->nr_used++;
171171
list_add(&b->list, &bc->freeable);
172172
return b;
173173
}
@@ -194,7 +194,7 @@ void bch2_btree_node_hash_remove(struct btree_cache *bc, struct btree *b)
194194
b->hash_val = 0;
195195

196196
if (b->c.btree_id < BTREE_ID_NR)
197-
--bc->used_by_btree[b->c.btree_id];
197+
--bc->nr_by_btree[b->c.btree_id];
198198
}
199199

200200
int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
@@ -205,7 +205,7 @@ int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
205205
int ret = rhashtable_lookup_insert_fast(&bc->table, &b->hash,
206206
bch_btree_cache_params);
207207
if (!ret && b->c.btree_id < BTREE_ID_NR)
208-
bc->used_by_btree[b->c.btree_id]++;
208+
bc->nr_by_btree[b->c.btree_id]++;
209209
return ret;
210210
}
211211

@@ -401,8 +401,8 @@ static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
401401
unsigned long touched = 0;
402402
unsigned i, flags;
403403
unsigned long ret = SHRINK_STOP;
404-
bool trigger_writes = atomic_read(&bc->dirty) + nr >=
405-
bc->used * 3 / 4;
404+
bool trigger_writes = atomic_long_read(&bc->nr_dirty) + nr >=
405+
bc->nr_used * 3 / 4;
406406

407407
if (bch2_btree_shrinker_disabled)
408408
return SHRINK_STOP;
@@ -439,7 +439,7 @@ static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
439439
six_unlock_write(&b->c.lock);
440440
six_unlock_intent(&b->c.lock);
441441
freed++;
442-
bc->freed++;
442+
bc->nr_freed++;
443443
}
444444
}
445445
restart:
@@ -453,7 +453,7 @@ static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
453453
} else if (!btree_node_reclaim(c, b, true)) {
454454
freed++;
455455
btree_node_data_free(c, b);
456-
bc->freed++;
456+
bc->nr_freed++;
457457

458458
bch2_btree_node_hash_remove(bc, b);
459459
six_unlock_write(&b->c.lock);
@@ -539,7 +539,7 @@ void bch2_fs_btree_cache_exit(struct bch_fs *c)
539539
}
540540

541541
BUG_ON(!bch2_journal_error(&c->journal) &&
542-
atomic_read(&c->btree_cache.dirty));
542+
atomic_long_read(&c->btree_cache.nr_dirty));
543543

544544
list_splice(&bc->freed_pcpu, &bc->freed_nonpcpu);
545545

@@ -572,7 +572,7 @@ int bch2_fs_btree_cache_init(struct bch_fs *c)
572572

573573
bch2_recalc_btree_reserve(c);
574574

575-
for (i = 0; i < bc->reserve; i++)
575+
for (i = 0; i < bc->nr_reserve; i++)
576576
if (!__bch2_btree_node_mem_alloc(c))
577577
goto err;
578578

@@ -739,7 +739,7 @@ struct btree *bch2_btree_node_mem_alloc(struct btree_trans *trans, bool pcpu_rea
739739
}
740740

741741
mutex_lock(&bc->lock);
742-
bc->used++;
742+
bc->nr_used++;
743743
got_mem:
744744
mutex_unlock(&bc->lock);
745745

@@ -1353,11 +1353,11 @@ void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c, const struc
13531353
}
13541354

13551355
static void prt_btree_cache_line(struct printbuf *out, const struct bch_fs *c,
1356-
const char *label, unsigned nr)
1356+
const char *label, size_t nr)
13571357
{
13581358
prt_printf(out, "%s\t", label);
13591359
prt_human_readable_u64(out, nr * c->opts.btree_node_size);
1360-
prt_printf(out, " (%u)\n", nr);
1360+
prt_printf(out, " (%zu)\n", nr);
13611361
}
13621362

13631363
static const char * const bch2_btree_cache_not_freed_reasons_strs[] = {
@@ -1374,16 +1374,16 @@ void bch2_btree_cache_to_text(struct printbuf *out, const struct btree_cache *bc
13741374
if (!out->nr_tabstops)
13751375
printbuf_tabstop_push(out, 32);
13761376

1377-
prt_btree_cache_line(out, c, "total:", bc->used);
1378-
prt_btree_cache_line(out, c, "nr dirty:", atomic_read(&bc->dirty));
1377+
prt_btree_cache_line(out, c, "total:", bc->nr_used);
1378+
prt_btree_cache_line(out, c, "nr dirty:", atomic_long_read(&bc->nr_dirty));
13791379
prt_printf(out, "cannibalize lock:\t%p\n", bc->alloc_lock);
13801380
prt_newline(out);
13811381

1382-
for (unsigned i = 0; i < ARRAY_SIZE(bc->used_by_btree); i++)
1383-
prt_btree_cache_line(out, c, bch2_btree_id_str(i), bc->used_by_btree[i]);
1382+
for (unsigned i = 0; i < ARRAY_SIZE(bc->nr_by_btree); i++)
1383+
prt_btree_cache_line(out, c, bch2_btree_id_str(i), bc->nr_by_btree[i]);
13841384

13851385
prt_newline(out);
1386-
prt_printf(out, "freed:\t%u\n", bc->freed);
1386+
prt_printf(out, "freed:\t%zu\n", bc->nr_freed);
13871387
prt_printf(out, "not freed:\n");
13881388

13891389
for (unsigned i = 0; i < ARRAY_SIZE(bc->not_freed); i++)

fs/bcachefs/btree_io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ void __bch2_btree_node_write(struct bch_fs *c, struct btree *b, unsigned flags)
20312031
do_write:
20322032
BUG_ON((type == BTREE_WRITE_initial) != (b->written == 0));
20332033

2034-
atomic_dec(&c->btree_cache.dirty);
2034+
atomic_long_dec(&c->btree_cache.nr_dirty);
20352035

20362036
BUG_ON(btree_node_fake(b));
20372037
BUG_ON((b->will_make_reachable != 0) != !b->written);

fs/bcachefs/btree_io.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ struct btree_node_read_all;
1818
static inline void set_btree_node_dirty_acct(struct bch_fs *c, struct btree *b)
1919
{
2020
if (!test_and_set_bit(BTREE_NODE_dirty, &b->flags))
21-
atomic_inc(&c->btree_cache.dirty);
21+
atomic_long_inc(&c->btree_cache.nr_dirty);
2222
}
2323

2424
static inline void clear_btree_node_dirty_acct(struct bch_fs *c, struct btree *b)
2525
{
2626
if (test_and_clear_bit(BTREE_NODE_dirty, &b->flags))
27-
atomic_dec(&c->btree_cache.dirty);
27+
atomic_long_dec(&c->btree_cache.nr_dirty);
2828
}
2929

3030
static inline unsigned btree_ptr_sectors_written(struct bkey_s_c k)

fs/bcachefs/btree_types.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,16 @@ struct btree_cache {
180180
struct list_head freed_nonpcpu;
181181

182182
/* Number of elements in live + freeable lists */
183-
unsigned used;
184-
unsigned reserve;
185-
unsigned freed;
186-
atomic_t dirty;
183+
size_t nr_used;
184+
size_t nr_reserve;
185+
size_t nr_by_btree[BTREE_ID_NR];
186+
atomic_long_t nr_dirty;
187+
188+
/* shrinker stats */
189+
size_t nr_freed;
187190
u64 not_freed[BCH_BTREE_CACHE_NOT_FREED_REASONS_NR];
188191
struct shrinker *shrink;
189192

190-
unsigned used_by_btree[BTREE_ID_NR];
191-
192193
/*
193194
* If we need to allocate memory for a new btree node and that
194195
* allocation fails, we can cannibalize another node in the btree cache

fs/bcachefs/journal_reclaim.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,16 +681,16 @@ static int __bch2_journal_reclaim(struct journal *j, bool direct, bool kicked)
681681
if (j->watermark != BCH_WATERMARK_stripe)
682682
min_nr = 1;
683683

684-
if (atomic_read(&c->btree_cache.dirty) * 2 > c->btree_cache.used)
684+
if (atomic_long_read(&c->btree_cache.nr_dirty) * 2 > c->btree_cache.nr_used)
685685
min_nr = 1;
686686

687687
min_key_cache = min(bch2_nr_btree_keys_need_flush(c), (size_t) 128);
688688

689689
trace_and_count(c, journal_reclaim_start, c,
690690
direct, kicked,
691691
min_nr, min_key_cache,
692-
atomic_read(&c->btree_cache.dirty),
693-
c->btree_cache.used,
692+
atomic_long_read(&c->btree_cache.nr_dirty),
693+
c->btree_cache.nr_used,
694694
atomic_long_read(&c->btree_key_cache.nr_dirty),
695695
atomic_long_read(&c->btree_key_cache.nr_keys));
696696

fs/bcachefs/super.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ void bch2_fs_read_only(struct bch_fs *c)
370370
test_bit(BCH_FS_clean_shutdown, &c->flags) &&
371371
c->recovery_pass_done >= BCH_RECOVERY_PASS_journal_replay) {
372372
BUG_ON(c->journal.last_empty_seq != journal_cur_seq(&c->journal));
373-
BUG_ON(atomic_read(&c->btree_cache.dirty));
373+
BUG_ON(atomic_long_read(&c->btree_cache.nr_dirty));
374374
BUG_ON(atomic_long_read(&c->btree_key_cache.nr_dirty));
375375
BUG_ON(c->btree_write_buffer.inc.keys.nr);
376376
BUG_ON(c->btree_write_buffer.flushing.keys.nr);

0 commit comments

Comments
 (0)