All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: famz@redhat.com, stefanha@redhat.com, qemu-block@nongnu.org
Subject: [Qemu-devel] [PATCH 10/18] util: add stats64 module
Date: Thu, 11 May 2017 16:42:00 +0200	[thread overview]
Message-ID: <20170511144208.24075-11-pbonzini@redhat.com> (raw)
In-Reply-To: <20170511144208.24075-1-pbonzini@redhat.com>

This module provides fast paths for 64-bit atomic operations on machines
that only have 32-bit atomic access.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
        v1->v2: use CONFIG_ATOMIC64 [Paolo]
                fix compilation on 32-bit machines [patchew]
                simplify "fast" version of stat64_min/stat64_max [Paolo]
                fix typo [Roman]
                use cpu_relax [Fam]

 include/qemu/stats64.h | 193 +++++++++++++++++++++++++++++++++++++++++++++++++
 util/Makefile.objs     |   1 +
 util/stats64.c         | 136 ++++++++++++++++++++++++++++++++++
 3 files changed, 330 insertions(+)
 create mode 100644 include/qemu/stats64.h
 create mode 100644 util/stats64.c

diff --git a/include/qemu/stats64.h b/include/qemu/stats64.h
new file mode 100644
index 0000000000..f9baf9b159
--- /dev/null
+++ b/include/qemu/stats64.h
@@ -0,0 +1,193 @@
+/*
+ * Atomic operations on 64-bit quantities.
+ *
+ * Copyright (C) 2017 Red Hat, Inc.
+ *
+ * Author: Paolo Bonzini <pbonzini@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_STATS64_H
+#define QEMU_STATS64_H 1
+
+#include "qemu/atomic.h"
+
+/* This provides atomic operations on 64-bit type, using a reader-writer
+ * spinlock on architectures that do not have 64-bit accesses.  However
+ * it tries hard not to take the lock.
+ */
+
+typedef struct Stat64 {
+#ifdef CONFIG_ATOMIC64
+    uint64_t value;
+#else
+    uint32_t low, high;
+    uint32_t lock;
+#endif
+} Stat64;
+
+#ifdef CONFIG_ATOMIC64
+static inline void stat64_init(Stat64 *s, uint64_t value)
+{
+    /* This is not guaranteed to be atomic! */
+    *s = (Stat64) { value };
+}
+
+static inline uint64_t stat64_get(const Stat64 *s)
+{
+    return atomic_read(&s->value);
+}
+
+static inline void stat64_add(Stat64 *s, uint64_t value)
+{
+    atomic_add(&s->value, value);
+}
+
+static inline void stat64_min(Stat64 *s, uint64_t value)
+{
+    uint64_t orig = atomic_read(&s->value);
+    while (orig > value) {
+        orig = atomic_cmpxchg(&s->value, orig, value);
+    }
+}
+
+static inline void stat64_max(Stat64 *s, uint64_t value)
+{
+    uint64_t orig = atomic_read(&s->value);
+    while (orig < value) {
+        orig = atomic_cmpxchg(&s->value, orig, value);
+    }
+}
+#else
+uint64_t stat64_get(const Stat64 *s);
+bool stat64_min_slow(Stat64 *s, uint64_t value);
+bool stat64_max_slow(Stat64 *s, uint64_t value);
+bool stat64_add32_carry(Stat64 *s, uint32_t low, uint32_t high);
+
+static inline void stat64_init(Stat64 *s, uint64_t value)
+{
+    /* This is not guaranteed to be atomic! */
+    *s = (Stat64) { .low = value, .high = value >> 32, .lock = 0 };
+}
+
+static inline void stat64_add(Stat64 *s, uint64_t value)
+{
+    uint32_t low, high;
+    high = value >> 32;
+    low = (uint32_t) value;
+    if (!low) {
+        if (high) {
+            atomic_add(&s->high, high);
+        }
+        return;
+    }
+
+    for (;;) {
+        uint32_t orig = s->low;
+        uint32_t result = orig + low;
+        uint32_t old;
+
+        if (result < low || high) {
+            /* If the high part is affected, take the lock.  */
+            if (stat64_add32_carry(s, low, high)) {
+                return;
+            }
+            continue;
+        }
+
+        /* No carry, try with a 32-bit cmpxchg.  The result is independent of
+         * the high 32 bits, so it can race just fine with stat64_add32_carry
+         * and even stat64_get!
+         */
+        old = atomic_cmpxchg(&s->low, orig, result);
+        if (orig == old) {
+            return;
+        }
+    }
+}
+
+static inline void stat64_min(Stat64 *s, uint64_t value)
+{
+    uint32_t low, high;
+    uint32_t orig_low, orig_high;
+
+    high = value >> 32;
+    low = (uint32_t) value;
+    do {
+        orig_high = atomic_read(&s->high);
+        if (orig_high < high) {
+            return;
+        }
+
+        if (orig_high == high) {
+            /* High 32 bits are equal.  Read low after high, otherwise we
+             * can get a false positive (e.g. 0x1235,0x0000 changes to
+             * 0x1234,0x8000 and we read it as 0x1234,0x0000). Pairs with
+             * the write barrier in stat64_min_slow.
+             */
+            smp_rmb();
+            orig_low = atomic_read(&s->low);
+            if (orig_low <= low) {
+                return;
+            }
+
+            /* See if we were lucky and a writer raced against us.  The
+             * barrier is theoretically unnecessary, but if we remove it
+             * we may miss being lucky.
+             */
+            smp_rmb();
+            orig_high = atomic_read(&s->high);
+            if (orig_high < high) {
+                return;
+            }
+        }
+
+        /* If the value changes in any way, we have to take the lock.  */
+    } while (!stat64_min_slow(s, value));
+}
+
+static inline void stat64_max(Stat64 *s, uint64_t value)
+{
+    uint32_t low, high;
+    uint32_t orig_low, orig_high;
+
+    high = value >> 32;
+    low = (uint32_t) value;
+    do {
+        orig_high = atomic_read(&s->high);
+        if (orig_high > high) {
+            return;
+        }
+
+        if (orig_high == high) {
+            /* High 32 bits are equal.  Read low after high, otherwise we
+             * can get a false positive (e.g. 0x1234,0x8000 changes to
+             * 0x1235,0x0000 and we read it as 0x1235,0x8000). Pairs with
+             * the write barrier in stat64_max_slow.
+             */
+            smp_rmb();
+            orig_low = atomic_read(&s->low);
+            if (orig_low >= low) {
+                return;
+            }
+
+            /* See if we were lucky and a writer raced against us.  The
+             * barrier is theoretically unnecessary, but if we remove it
+             * we may miss being lucky.
+             */
+            smp_rmb();
+            orig_high = atomic_read(&s->high);
+            if (orig_high > high) {
+                return;
+            }
+        }
+
+        /* If the value changes in any way, we have to take the lock.  */
+    } while (!stat64_max_slow(s, value));
+}
+
+#endif
+
+#endif
diff --git a/util/Makefile.objs b/util/Makefile.objs
index c6205ebf86..8a333d3dd7 100644
--- a/util/Makefile.objs
+++ b/util/Makefile.objs
@@ -42,4 +42,5 @@ util-obj-y += log.o
 util-obj-y += qdist.o
 util-obj-y += qht.o
 util-obj-y += range.o
+util-obj-y += stats64.o
 util-obj-y += systemd.o
diff --git a/util/stats64.c b/util/stats64.c
new file mode 100644
index 0000000000..ac236e3840
--- /dev/null
+++ b/util/stats64.c
@@ -0,0 +1,136 @@
+/*
+ * Atomic operations on 64-bit quantities.
+ *
+ * Copyright (C) 2017 Red Hat, Inc.
+ *
+ * Author: Paolo Bonzini <pbonzini@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/atomic.h"
+#include "qemu/stats64.h"
+
+#ifndef CONFIG_ATOMIC64
+static inline void stat64_rdlock(Stat64 *s)
+{
+    /* Keep out incoming writers to avoid them starving us. */
+    atomic_add(&s->lock, 2);
+
+    /* If there is a concurrent writer, wait for it.  */
+    while (atomic_read(&s->lock) & 1) {
+        cpu_relax();
+    }
+}
+
+static inline void stat64_rdunlock(Stat64 *s)
+{
+    atomic_sub(&s->lock, 2);
+}
+
+static inline bool stat64_wrtrylock(Stat64 *s)
+{
+    return atomic_cmpxchg(&s->lock, 0, 1) == 0;
+}
+
+static inline void stat64_wrunlock(Stat64 *s)
+{
+    atomic_dec(&s->lock);
+}
+
+uint64_t stat64_get(const Stat64 *s)
+{
+    uint32_t high, low;
+
+    stat64_rdlock((Stat64 *)s);
+
+    /* 64-bit writes always take the lock, so we can read in
+     * any order.
+     */
+    high = atomic_read(&s->high);
+    low = atomic_read(&s->low);
+    stat64_rdunlock((Stat64 *)s);
+
+    return ((uint64_t)high << 32) | low;
+}
+
+bool stat64_add32_carry(Stat64 *s, uint32_t low, uint32_t high)
+{
+    uint32_t old;
+
+    if (!stat64_wrtrylock(s)) {
+        cpu_relax();
+        return false;
+    }
+
+    /* 64-bit reads always take the lock, so they don't care about the
+     * order of our update.  By updating s->low first, we can check
+     * whether we have to carry into s->high.
+     */
+    old = atomic_fetch_add(&s->low, low);
+    high += (old + low) < old;
+    atomic_add(&s->high, high);
+    stat64_wrunlock(s);
+    return true;
+}
+
+bool stat64_min_slow(Stat64 *s, uint64_t value)
+{
+    uint32_t high, low;
+    uint64_t orig;
+
+    if (!stat64_wrtrylock(s)) {
+        cpu_relax();
+        return false;
+    }
+
+    high = atomic_read(&s->high);
+    low = atomic_read(&s->low);
+
+    orig = ((uint64_t)high << 32) | low;
+    if (orig < value) {
+        /* We have to set low before high, just like stat64_min reads
+         * high before low.  The value may become higher temporarily, but
+         * stat64_get does not notice (it takes the lock) and the only ill
+         * effect on stat64_min is that the slow path may be triggered
+         * unnecessarily.
+         */
+        atomic_set(&s->low, (uint32_t)value);
+        smp_wmb();
+        atomic_set(&s->high, value >> 32);
+    }
+    stat64_wrunlock(s);
+    return true;
+}
+
+bool stat64_max_slow(Stat64 *s, uint64_t value)
+{
+    uint32_t high, low;
+    uint64_t orig;
+
+    if (!stat64_wrtrylock(s)) {
+        cpu_relax();
+        return false;
+    }
+
+    high = atomic_read(&s->high);
+    low = atomic_read(&s->low);
+
+    orig = ((uint64_t)high << 32) | low;
+    if (orig > value) {
+        /* We have to set low before high, just like stat64_max reads
+         * high before low.  The value may become lower temporarily, but
+         * stat64_get does not notice (it takes the lock) and the only ill
+         * effect on stat64_max is that the slow path may be triggered
+         * unnecessarily.
+         */
+        atomic_set(&s->low, (uint32_t)value);
+        smp_wmb();
+        atomic_set(&s->high, value >> 32);
+    }
+    stat64_wrunlock(s);
+    return true;
+}
+#endif
-- 
2.12.2

  parent reply	other threads:[~2017-05-11 14:42 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-11 14:41 [Qemu-devel] [PATCH v2 00/18] Block layer thread safety, part 1 Paolo Bonzini
2017-05-11 14:41 ` [Qemu-devel] [PATCH 01/18] block: access copy_on_read with atomic ops Paolo Bonzini
2017-05-16 13:34   ` Stefan Hajnoczi
2017-05-11 14:41 ` [Qemu-devel] [PATCH 02/18] block: access quiesce_counter " Paolo Bonzini
2017-05-12  7:56   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2017-05-11 14:41 ` [Qemu-devel] [PATCH 03/18] block: access io_limits_disabled " Paolo Bonzini
2017-05-18 12:25   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2017-05-11 14:41 ` [Qemu-devel] [PATCH 04/18] block: access serialising_in_flight " Paolo Bonzini
2017-05-11 14:41 ` [Qemu-devel] [PATCH 05/18] block: access wakeup " Paolo Bonzini
2017-05-16 13:48   ` Stefan Hajnoczi
2017-05-11 14:41 ` [Qemu-devel] [PATCH 06/18] block: access io_plugged " Paolo Bonzini
2017-05-11 16:10   ` Eric Blake
2017-05-11 14:41 ` [Qemu-devel] [PATCH 07/18] throttle-groups: only start one coroutine from drained_begin Paolo Bonzini
2017-05-16 13:54   ` Stefan Hajnoczi
2017-05-17 21:50   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2017-05-11 14:41 ` [Qemu-devel] [PATCH 08/18] throttle-groups: do not use qemu_co_enter_next Paolo Bonzini
2017-05-16 14:44   ` Stefan Hajnoczi
2017-05-18 11:56   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2017-05-11 14:41 ` [Qemu-devel] [PATCH 09/18] throttle-groups: protect throttled requests with a CoMutex Paolo Bonzini
2017-05-16 14:47   ` Stefan Hajnoczi
2017-05-18 12:06   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2017-05-18 12:08     ` Paolo Bonzini
2017-05-18 12:19       ` Alberto Garcia
2017-05-11 14:42 ` Paolo Bonzini [this message]
2017-05-16 14:47   ` [Qemu-devel] [PATCH 10/18] util: add stats64 module Stefan Hajnoczi
2017-05-11 14:42 ` [Qemu-devel] [PATCH 11/18] block: use Stat64 for wr_highest_offset Paolo Bonzini
2017-05-11 14:42 ` [Qemu-devel] [PATCH 12/18] block: access write_gen with atomics Paolo Bonzini
2017-05-11 14:42 ` [Qemu-devel] [PATCH 13/18] block: protect tracked_requests and flush_queue with reqs_lock Paolo Bonzini
2017-05-16 14:50   ` Stefan Hajnoczi
2017-05-11 14:42 ` [Qemu-devel] [PATCH 14/18] block: introduce dirty_bitmap_mutex Paolo Bonzini
2017-05-16 14:55   ` Stefan Hajnoczi
2017-05-11 14:42 ` [Qemu-devel] [PATCH 15/18] migration/block: reset dirty bitmap before reading Paolo Bonzini
2017-05-16 15:03   ` Stefan Hajnoczi
2017-05-11 14:42 ` [Qemu-devel] [PATCH 16/18] block: protect modification of dirty bitmaps with a mutex Paolo Bonzini
2017-05-16 15:05   ` Stefan Hajnoczi
2017-05-11 14:42 ` [Qemu-devel] [PATCH 17/18] block: introduce block_account_one_io Paolo Bonzini
2017-05-16 15:07   ` Stefan Hajnoczi
2017-05-18 12:09   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2017-05-25  7:28   ` [Qemu-devel] " Fam Zheng
2017-05-11 14:42 ` [Qemu-devel] [PATCH 18/18] block: make accounting thread-safe Paolo Bonzini
2017-05-16 15:08   ` Stefan Hajnoczi
2017-05-18 12:16   ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2017-05-11 18:21 ` [Qemu-devel] [PATCH v2 00/18] Block layer thread safety, part 1 no-reply
2017-05-11 19:24 ` no-reply
2017-05-16 15:08 ` Stefan Hajnoczi
2017-05-24  8:32 ` Paolo Bonzini
2017-05-25  7:40   ` Fam Zheng
2017-05-25  9:14     ` [Qemu-devel] [Qemu-block] " Paolo Bonzini
2017-05-25 16:17     ` [Qemu-devel] " Paolo Bonzini
2017-05-25 16:32 [Qemu-devel] [PATCH v3 " Paolo Bonzini
2017-05-25 16:32 ` [Qemu-devel] [PATCH 10/18] util: add stats64 module Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170511144208.24075-11-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=famz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.