bcachefs: Don't underflow c->sectors_available
authorKent Overstreet <kent.overstreet@gmail.com>
Fri, 11 Jun 2021 03:33:27 +0000 (23:33 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:06 +0000 (17:09 -0400)
This rarely used error path should've been checking for underflow -
oops.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
fs/bcachefs/buckets.c

index c0e4cec21b744158a4b0eea2ced816ca80748360..da24c4038fc9841ec6543148ea580edc12c9ee4f 100644 (file)
@@ -1437,7 +1437,14 @@ void bch2_trans_fs_usage_apply(struct btree_trans *trans,
         */
        should_not_have_added = added - (s64) disk_res_sectors;
        if (unlikely(should_not_have_added > 0)) {
-               atomic64_sub(should_not_have_added, &c->sectors_available);
+               u64 old, new, v = atomic64_read(&c->sectors_available);
+
+               do {
+                       old = v;
+                       new = max_t(s64, 0, old - should_not_have_added);
+               } while ((v = atomic64_cmpxchg(&c->sectors_available,
+                                              old, new)) != old);
+
                added -= should_not_have_added;
                warn = true;
        }