Skip to content

Commit 27d033d

Browse files
author
Kent Overstreet
committed
bcachefs: Convert clock code to u64s
Eliminate possible integer truncation bugs on 32 bit Signed-off-by: Kent Overstreet <[email protected]>
1 parent ec8bf49 commit 27d033d

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

fs/bcachefs/clock.c

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@ static inline long io_timer_cmp(io_timer_heap *h,
1515

1616
void bch2_io_timer_add(struct io_clock *clock, struct io_timer *timer)
1717
{
18-
size_t i;
19-
2018
spin_lock(&clock->timer_lock);
2119

22-
if (time_after_eq((unsigned long) atomic64_read(&clock->now),
23-
timer->expire)) {
20+
if (time_after_eq64((u64) atomic64_read(&clock->now), timer->expire)) {
2421
spin_unlock(&clock->timer_lock);
2522
timer->fn(timer);
2623
return;
2724
}
2825

29-
for (i = 0; i < clock->timers.used; i++)
26+
for (size_t i = 0; i < clock->timers.used; i++)
3027
if (clock->timers.data[i] == timer)
3128
goto out;
3229

@@ -37,11 +34,9 @@ void bch2_io_timer_add(struct io_clock *clock, struct io_timer *timer)
3734

3835
void bch2_io_timer_del(struct io_clock *clock, struct io_timer *timer)
3936
{
40-
size_t i;
41-
4237
spin_lock(&clock->timer_lock);
4338

44-
for (i = 0; i < clock->timers.used; i++)
39+
for (size_t i = 0; i < clock->timers.used; i++)
4540
if (clock->timers.data[i] == timer) {
4641
heap_del(&clock->timers, i, io_timer_cmp, NULL);
4742
break;
@@ -75,33 +70,31 @@ static void io_clock_cpu_timeout(struct timer_list *timer)
7570
wake_up_process(wait->task);
7671
}
7772

78-
void bch2_io_clock_schedule_timeout(struct io_clock *clock, unsigned long until)
73+
void bch2_io_clock_schedule_timeout(struct io_clock *clock, u64 until)
7974
{
80-
struct io_clock_wait wait;
75+
struct io_clock_wait wait = {
76+
.io_timer.expire = until,
77+
.io_timer.fn = io_clock_wait_fn,
78+
.io_timer.fn2 = (void *) _RET_IP_,
79+
.task = current,
80+
};
8181

82-
/* XXX: calculate sleep time rigorously */
83-
wait.io_timer.expire = until;
84-
wait.io_timer.fn = io_clock_wait_fn;
85-
wait.task = current;
86-
wait.expired = 0;
8782
bch2_io_timer_add(clock, &wait.io_timer);
88-
8983
schedule();
90-
9184
bch2_io_timer_del(clock, &wait.io_timer);
9285
}
9386

9487
void bch2_kthread_io_clock_wait(struct io_clock *clock,
95-
unsigned long io_until,
96-
unsigned long cpu_timeout)
88+
u64 io_until, unsigned long cpu_timeout)
9789
{
9890
bool kthread = (current->flags & PF_KTHREAD) != 0;
99-
struct io_clock_wait wait;
91+
struct io_clock_wait wait = {
92+
.io_timer.expire = io_until,
93+
.io_timer.fn = io_clock_wait_fn,
94+
.io_timer.fn2 = (void *) _RET_IP_,
95+
.task = current,
96+
};
10097

101-
wait.io_timer.expire = io_until;
102-
wait.io_timer.fn = io_clock_wait_fn;
103-
wait.task = current;
104-
wait.expired = 0;
10598
bch2_io_timer_add(clock, &wait.io_timer);
10699

107100
timer_setup_on_stack(&wait.cpu_timer, io_clock_cpu_timeout, 0);
@@ -127,21 +120,20 @@ void bch2_kthread_io_clock_wait(struct io_clock *clock,
127120
bch2_io_timer_del(clock, &wait.io_timer);
128121
}
129122

130-
static struct io_timer *get_expired_timer(struct io_clock *clock,
131-
unsigned long now)
123+
static struct io_timer *get_expired_timer(struct io_clock *clock, u64 now)
132124
{
133125
struct io_timer *ret = NULL;
134126

135127
if (clock->timers.used &&
136-
time_after_eq(now, clock->timers.data[0]->expire))
128+
time_after_eq64(now, clock->timers.data[0]->expire))
137129
heap_pop(&clock->timers, ret, io_timer_cmp, NULL);
138130
return ret;
139131
}
140132

141-
void __bch2_increment_clock(struct io_clock *clock, unsigned sectors)
133+
void __bch2_increment_clock(struct io_clock *clock, u64 sectors)
142134
{
143135
struct io_timer *timer;
144-
unsigned long now = atomic64_add_return(sectors, &clock->now);
136+
u64 now = atomic64_add_return(sectors, &clock->now);
145137

146138
spin_lock(&clock->timer_lock);
147139
while ((timer = get_expired_timer(clock, now)))
@@ -151,17 +143,18 @@ void __bch2_increment_clock(struct io_clock *clock, unsigned sectors)
151143

152144
void bch2_io_timers_to_text(struct printbuf *out, struct io_clock *clock)
153145
{
154-
unsigned long now;
155-
unsigned i;
156-
157146
out->atomic++;
158147
spin_lock(&clock->timer_lock);
159-
now = atomic64_read(&clock->now);
148+
u64 now = atomic64_read(&clock->now);
149+
150+
printbuf_tabstop_push(out, 40);
151+
prt_printf(out, "current time:\t%llu\n", now);
160152

161-
for (i = 0; i < clock->timers.used; i++)
162-
prt_printf(out, "%ps:\t%li\n",
153+
for (unsigned i = 0; i < clock->timers.used; i++)
154+
prt_printf(out, "%ps %ps:\t%llu\n",
163155
clock->timers.data[i]->fn,
164-
clock->timers.data[i]->expire - now);
156+
clock->timers.data[i]->fn2,
157+
clock->timers.data[i]->expire);
165158
spin_unlock(&clock->timer_lock);
166159
--out->atomic;
167160
}

fs/bcachefs/clock.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
void bch2_io_timer_add(struct io_clock *, struct io_timer *);
66
void bch2_io_timer_del(struct io_clock *, struct io_timer *);
7-
void bch2_kthread_io_clock_wait(struct io_clock *, unsigned long,
8-
unsigned long);
7+
void bch2_kthread_io_clock_wait(struct io_clock *, u64, unsigned long);
98

10-
void __bch2_increment_clock(struct io_clock *, unsigned);
9+
void __bch2_increment_clock(struct io_clock *, u64);
1110

12-
static inline void bch2_increment_clock(struct bch_fs *c, unsigned sectors,
11+
static inline void bch2_increment_clock(struct bch_fs *c, u64 sectors,
1312
int rw)
1413
{
1514
struct io_clock *clock = &c->io_clock[rw];
@@ -19,7 +18,7 @@ static inline void bch2_increment_clock(struct bch_fs *c, unsigned sectors,
1918
__bch2_increment_clock(clock, this_cpu_xchg(*clock->pcpu_buf, 0));
2019
}
2120

22-
void bch2_io_clock_schedule_timeout(struct io_clock *, unsigned long);
21+
void bch2_io_clock_schedule_timeout(struct io_clock *, u64);
2322

2423
#define bch2_kthread_wait_event_ioclock_timeout(condition, clock, timeout)\
2524
({ \

fs/bcachefs/clock_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ typedef void (*io_timer_fn)(struct io_timer *);
1717

1818
struct io_timer {
1919
io_timer_fn fn;
20-
unsigned long expire;
20+
void *fn2;
21+
u64 expire;
2122
};
2223

2324
/* Amount to buffer up on a percpu counter */

0 commit comments

Comments
 (0)