bcachefs: percpu utility code
authorKent Overstreet <kent.overstreet@gmail.com>
Wed, 6 Feb 2019 16:42:13 +0000 (11:42 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:08:15 +0000 (17:08 -0400)
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/buckets.c
fs/bcachefs/replicas.c
fs/bcachefs/sysfs.c
fs/bcachefs/util.h

index d40aa69532cc7af78074ac2382eb5ad9560f9b4d..d919c1cacee5ed862f4d614753dc9d06c5fa4b16 100644 (file)
@@ -1005,10 +1005,7 @@ void bch2_mark_update(struct btree_insert *trans,
 
 static u64 bch2_recalc_sectors_available(struct bch_fs *c)
 {
-       int cpu;
-
-       for_each_possible_cpu(cpu)
-               per_cpu_ptr(c->pcpu, cpu)->sectors_available = 0;
+       percpu_u64_set(&c->pcpu->sectors_available, 0);
 
        return avail_factor(bch2_fs_sectors_free(c));
 }
index 34a5475cfabac8ea751b3e6687c742688badbcda..5663441fd7ced43bfd82382b70273f006ba6fb47 100644 (file)
@@ -426,14 +426,12 @@ int bch2_replicas_gc_end(struct bch_fs *c, int ret)
                struct bch_replicas_entry *e =
                        cpu_replicas_entry(&c->replicas, i);
                struct bch_replicas_cpu n;
-               u64 v = 0;
-               int cpu;
+               u64 v;
 
                if (__replicas_has_entry(&c->replicas_gc, e))
                        continue;
 
-               for_each_possible_cpu(cpu)
-                       v += *per_cpu_ptr(&c->usage[0]->data[i], cpu);
+               v = percpu_u64_get(&c->usage[0]->data[i]);
                if (!v)
                        continue;
 
index f33a533ee6b8cb5ac3b53351f70dba51d06afa65..8ad7b6026d1b6e3272236009fba7a51b5df45249 100644 (file)
@@ -893,20 +893,15 @@ static const char * const bch2_rw[] = {
 static ssize_t show_dev_iodone(struct bch_dev *ca, char *buf)
 {
        struct printbuf out = _PBUF(buf, PAGE_SIZE);
-       int rw, i, cpu;
+       int rw, i;
 
        for (rw = 0; rw < 2; rw++) {
                pr_buf(&out, "%s:\n", bch2_rw[rw]);
 
-               for (i = 1; i < BCH_DATA_NR; i++) {
-                       u64 n = 0;
-
-                       for_each_possible_cpu(cpu)
-                               n += per_cpu_ptr(ca->io_done, cpu)->sectors[rw][i];
-
+               for (i = 1; i < BCH_DATA_NR; i++)
                        pr_buf(&out, "%-12s:%12llu\n",
-                              bch2_data_types[i], n << 9);
-               }
+                              bch2_data_types[i],
+                              percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
        }
 
        return out.pos - buf;
index fa1a3adc87df299a3479d6cb50505c8dd3ab6f6f..dc40a52ac8c72b11632889d0c4a3aea18c4f68f2 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/llist.h>
 #include <linux/log2.h>
 #include <linux/percpu.h>
+#include <linux/preempt.h>
 #include <linux/ratelimit.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
@@ -701,6 +702,28 @@ do {                                                                       \
        }                                                               \
 } while (0)
 
+static inline u64 percpu_u64_get(u64 __percpu *src)
+{
+       u64 ret = 0;
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               ret += *per_cpu_ptr(src, cpu);
+       return ret;
+}
+
+static inline void percpu_u64_set(u64 __percpu *dst, u64 src)
+{
+       int cpu;
+
+       for_each_possible_cpu(cpu)
+               *per_cpu_ptr(dst, cpu) = 0;
+
+       preempt_disable();
+       *this_cpu_ptr(dst) = src;
+       preempt_enable();
+}
+
 static inline void acc_u64s(u64 *acc, const u64 *src, unsigned nr)
 {
        unsigned i;