]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.c
Update bcachefs sources to 0d63ed13ea3d closures: Fix race in closure_sync()
[bcachefs-tools-debian] / libbcachefs / util.c
index 9939bf2a8f6caf06a3874e5690871a7b9728b298..adeec805dd0cdb3dce3caecc0bef9a59b7ded7b8 100644 (file)
@@ -112,10 +112,10 @@ got_unit:
 
 #define parse_or_ret(cp, _f)                   \
 do {                                           \
-       int ret = _f;                           \
-       if (ret < 0)                            \
-               return ret;                     \
-       cp += ret;                              \
+       int _ret = _f;                          \
+       if (_ret < 0)                           \
+               return _ret;                    \
+       cp += _ret;                             \
 } while (0)
 
 static int __bch2_strtou64_h(const char *cp, u64 *res)
@@ -216,6 +216,7 @@ u64 bch2_read_flag_list(char *opt, const char * const list[])
 
        while ((p = strsep(&s, ","))) {
                int flag = match_string(list, -1, p);
+
                if (flag < 0) {
                        ret = -1;
                        break;
@@ -240,36 +241,6 @@ bool bch2_is_zero(const void *_p, size_t n)
        return true;
 }
 
-static void bch2_quantiles_update(struct bch2_quantiles *q, u64 v)
-{
-       unsigned i = 0;
-
-       while (i < ARRAY_SIZE(q->entries)) {
-               struct bch2_quantile_entry *e = q->entries + i;
-
-               if (unlikely(!e->step)) {
-                       e->m = v;
-                       e->step = max_t(unsigned, v / 2, 1024);
-               } else if (e->m > v) {
-                       e->m = e->m >= e->step
-                               ? e->m - e->step
-                               : 0;
-               } else if (e->m < v) {
-                       e->m = e->m + e->step > e->m
-                               ? e->m + e->step
-                               : U32_MAX;
-               }
-
-               if ((e->m > v ? e->m - v : v - e->m) < e->step)
-                       e->step = max_t(unsigned, e->step / 2, 1);
-
-               if (v >= e->m)
-                       break;
-
-               i = eytzinger0_child(i, v > e->m);
-       }
-}
-
 void bch2_prt_u64_binary(struct printbuf *out, u64 v, unsigned nr_bits)
 {
        while (nr_bits)
@@ -296,27 +267,87 @@ void bch2_print_string_as_lines(const char *prefix, const char *lines)
        console_unlock();
 }
 
-int bch2_prt_backtrace(struct printbuf *out, struct task_struct *task)
+int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *task)
 {
-       unsigned long entries[32];
-       unsigned i, nr_entries;
+#ifdef CONFIG_STACKTRACE
+       unsigned nr_entries = 0;
+       int ret = 0;
+
+       stack->nr = 0;
+       ret = darray_make_room(stack, 32);
+       if (ret)
+               return ret;
 
        if (!down_read_trylock(&task->signal->exec_update_lock))
-               return 0;
+               return -1;
 
-       nr_entries = stack_trace_save_tsk(task, entries, ARRAY_SIZE(entries), 0);
-       for (i = 0; i < nr_entries; i++) {
-               prt_printf(out, "[<0>] %pB", (void *)entries[i]);
-               prt_newline(out);
-       }
+       do {
+               nr_entries = stack_trace_save_tsk(task, stack->data, stack->size, 0);
+       } while (nr_entries == stack->size &&
+                !(ret = darray_make_room(stack, stack->size * 2)));
 
+       stack->nr = nr_entries;
        up_read(&task->signal->exec_update_lock);
+
+       return ret;
+#else
        return 0;
+#endif
+}
+
+void bch2_prt_backtrace(struct printbuf *out, bch_stacktrace *stack)
+{
+       unsigned long *i;
+
+       darray_for_each(*stack, i) {
+               prt_printf(out, "[<0>] %pB", (void *) *i);
+               prt_newline(out);
+       }
+}
+
+int bch2_prt_task_backtrace(struct printbuf *out, struct task_struct *task)
+{
+       bch_stacktrace stack = { 0 };
+       int ret = bch2_save_backtrace(&stack, task);
+
+       bch2_prt_backtrace(out, &stack);
+       darray_exit(&stack);
+       return ret;
 }
 
 /* time stats: */
 
 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
+static void bch2_quantiles_update(struct bch2_quantiles *q, u64 v)
+{
+       unsigned i = 0;
+
+       while (i < ARRAY_SIZE(q->entries)) {
+               struct bch2_quantile_entry *e = q->entries + i;
+
+               if (unlikely(!e->step)) {
+                       e->m = v;
+                       e->step = max_t(unsigned, v / 2, 1024);
+               } else if (e->m > v) {
+                       e->m = e->m >= e->step
+                               ? e->m - e->step
+                               : 0;
+               } else if (e->m < v) {
+                       e->m = e->m + e->step > e->m
+                               ? e->m + e->step
+                               : U32_MAX;
+               }
+
+               if ((e->m > v ? e->m - v : v - e->m) < e->step)
+                       e->step = max_t(unsigned, e->step / 2, 1);
+
+               if (v >= e->m)
+                       break;
+
+               i = eytzinger0_child(i, v > e->m);
+       }
+}
+
 static inline void bch2_time_stats_update_one(struct bch2_time_stats *stats,
                                              u64 start, u64 end)
 {
@@ -324,11 +355,8 @@ static inline void bch2_time_stats_update_one(struct bch2_time_stats *stats,
 
        if (time_after64(end, start)) {
                duration = end - start;
-               stats->duration_stats = mean_and_variance_update_inlined(stats->duration_stats,
-                                                                duration);
-               stats->duration_stats_weighted = mean_and_variance_weighted_update(
-                       stats->duration_stats_weighted,
-                       duration);
+               mean_and_variance_update(&stats->duration_stats, duration);
+               mean_and_variance_weighted_update(&stats->duration_stats_weighted, duration);
                stats->max_duration = max(stats->max_duration, duration);
                stats->min_duration = min(stats->min_duration, duration);
                bch2_quantiles_update(&stats->quantiles, duration);
@@ -336,10 +364,8 @@ static inline void bch2_time_stats_update_one(struct bch2_time_stats *stats,
 
        if (time_after64(end, stats->last_event)) {
                freq = end - stats->last_event;
-               stats->freq_stats = mean_and_variance_update_inlined(stats->freq_stats, freq);
-               stats->freq_stats_weighted = mean_and_variance_weighted_update(
-                       stats->freq_stats_weighted,
-                       freq);
+               mean_and_variance_update(&stats->freq_stats, freq);
+               mean_and_variance_weighted_update(&stats->freq_stats_weighted, freq);
                stats->max_freq = max(stats->max_freq, freq);
                stats->min_freq = min(stats->min_freq, freq);
                stats->last_event = end;
@@ -407,8 +433,8 @@ static const struct time_unit {
        { "us",         NSEC_PER_USEC    },
        { "ms",         NSEC_PER_MSEC    },
        { "s",          NSEC_PER_SEC     },
-       { "m",          NSEC_PER_SEC * 60},
-       { "h",          NSEC_PER_SEC * 3600},
+       { "m",          (u64) NSEC_PER_SEC * 60},
+       { "h",          (u64) NSEC_PER_SEC * 3600},
        { "eon",        U64_MAX          },
 };
 
@@ -568,8 +594,8 @@ void bch2_time_stats_exit(struct bch2_time_stats *stats)
 void bch2_time_stats_init(struct bch2_time_stats *stats)
 {
        memset(stats, 0, sizeof(*stats));
-       stats->duration_stats_weighted.w = 8;
-       stats->freq_stats_weighted.w = 8;
+       stats->duration_stats_weighted.weight = 8;
+       stats->freq_stats_weighted.weight = 8;
        stats->min_duration = U64_MAX;
        stats->min_freq = U64_MAX;
        spin_lock_init(&stats->lock);
@@ -579,11 +605,9 @@ void bch2_time_stats_init(struct bch2_time_stats *stats)
 
 /**
  * bch2_ratelimit_delay() - return how long to delay until the next time to do
- * some work
- *
- * @d - the struct bch_ratelimit to update
- *
- * Returns the amount of time to delay by, in jiffies
+ *             some work
+ * @d:         the struct bch_ratelimit to update
+ * Returns:    the amount of time to delay by, in jiffies
  */
 u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
 {
@@ -596,9 +620,8 @@ u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
 
 /**
  * bch2_ratelimit_increment() - increment @d by the amount of work done
- *
- * @d - the struct bch_ratelimit to update
- * @done - the amount of work done, in arbitrary units
+ * @d:         the struct bch_ratelimit to update
+ * @done:      the amount of work done, in arbitrary units
  */
 void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
 {
@@ -738,7 +761,7 @@ void bch2_bio_map(struct bio *bio, void *base, size_t size)
 int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
 {
        while (size) {
-               struct page *page = alloc_page(gfp_mask);
+               struct page *page = alloc_pages(gfp_mask, 0);
                unsigned len = min_t(size_t, PAGE_SIZE, size);
 
                if (!page)
@@ -776,9 +799,10 @@ void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src)
        struct bvec_iter iter;
 
        __bio_for_each_segment(bv, dst, iter, dst_iter) {
-               void *dstp = kmap_atomic(bv.bv_page);
+               void *dstp = kmap_local_page(bv.bv_page);
+
                memcpy(dstp + bv.bv_offset, src, bv.bv_len);
-               kunmap_atomic(dstp);
+               kunmap_local(dstp);
 
                src += bv.bv_len;
        }
@@ -790,9 +814,10 @@ void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
        struct bvec_iter iter;
 
        __bio_for_each_segment(bv, src, iter, src_iter) {
-               void *srcp = kmap_atomic(bv.bv_page);
+               void *srcp = kmap_local_page(bv.bv_page);
+
                memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
-               kunmap_atomic(srcp);
+               kunmap_local(srcp);
 
                dst += bv.bv_len;
        }