All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Emilio G. Cota" <cota@braap.org>
To: Igor Mammedov <imammedo@redhat.com>, Richard Henderson <rth@twiddle.net>
Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org,
	Paolo Bonzini <pbonzini@redhat.com>,
	Peter Crosthwaite <crosthwaite.peter@gmail.com>,
	MTTCG Devel <mttcg@greensocs.com>
Subject: [Qemu-devel] [PATCH] qht: support resetting an uninitialized qht
Date: Wed, 10 Aug 2016 15:25:49 -0400	[thread overview]
Message-ID: <1470857149-32003-1-git-send-email-cota@braap.org> (raw)
In-Reply-To: <20160810153659.4b0172bd@nial.brq.redhat.com>

tb_flush() is called when debugging the guest (under both KVM
and TCG accelerators) with gdb. tb_flush() resets TCG's qht, which
segfaults if we're using KVM due to the qht not being initialized.

Fix this adding a magic number field to struct qht to track whether a qht
has been initialized with qht_init(). Then, explicitly allow
passing uninitialized qht's to qht_reset() and qht_reset_size(),
just like we do with qht_statistics_init().

Reported-by: Brent Baccala <cosine@freesoft.org>
Reported-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Emilio G. Cota <cota@braap.org>
---
 include/qemu/qht.h |  7 +++++++
 tests/test-qht.c   |  3 +++
 util/qht.c         | 20 +++++++++++++++++---
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/include/qemu/qht.h b/include/qemu/qht.h
index 311139b..39dd5e8 100644
--- a/include/qemu/qht.h
+++ b/include/qemu/qht.h
@@ -15,6 +15,7 @@ struct qht {
     struct qht_map *map;
     QemuMutex lock; /* serializes setters of ht->map */
     unsigned int mode;
+    unsigned int magic;
 };
 
 /**
@@ -124,6 +125,8 @@ bool qht_remove(struct qht *ht, const void *p, uint32_t hash);
  * If concurrent readers may exist, the objects pointed to by the hash table
  * must remain valid for the existing RCU grace period -- see qht_remove().
  * See also: qht_reset_size()
+ *
+ * Note: it is OK to pass an uninitialized @ht.
  */
 void qht_reset(struct qht *ht);
 
@@ -138,6 +141,8 @@ void qht_reset(struct qht *ht);
  * If concurrent readers may exist, the objects pointed to by the hash table
  * must remain valid for the existing RCU grace period -- see qht_remove().
  * See also: qht_reset(), qht_resize().
+ *
+ * Note: it is OK to pass an uninitialized @ht.
  */
 bool qht_reset_size(struct qht *ht, size_t n_elems);
 
@@ -173,6 +178,8 @@ void qht_iter(struct qht *ht, qht_iter_func_t func, void *userp);
  *
  * When done with @stats, pass the struct to qht_statistics_destroy().
  * Failing to do this will leak memory.
+ *
+ * Note: it is OK to pass an uninitialized @ht.
  */
 void qht_statistics_init(struct qht *ht, struct qht_stats *stats);
 
diff --git a/tests/test-qht.c b/tests/test-qht.c
index 46a64b6..a923b2e 100644
--- a/tests/test-qht.c
+++ b/tests/test-qht.c
@@ -97,6 +97,9 @@ static void qht_do_test(unsigned int mode, size_t init_entries)
 {
     /* under KVM we might fetch stats from an uninitialized qht */
     check_n(0);
+    /* resetting an uninitialized qht can happen as well, e.g. KVM + gdb */
+    qht_reset(&ht);
+    qht_reset_size(&ht, 0);
 
     qht_init(&ht, 0, mode);
 
diff --git a/util/qht.c b/util/qht.c
index 16a8d79..e4c90d6 100644
--- a/util/qht.c
+++ b/util/qht.c
@@ -89,6 +89,8 @@
 #define QHT_BUCKET_ENTRIES 4
 #endif
 
+#define QHT_MAGIC 0xbebec4fe
+
 /*
  * Note: reading partially-updated pointers in @pointers could lead to
  * segfaults. We thus access them with atomic_read/set; this guarantees
@@ -182,6 +184,11 @@ static inline void qht_map_debug__all_locked(struct qht_map *map)
 { }
 #endif /* QHT_DEBUG */
 
+static inline bool qht_inited(const struct qht *ht)
+{
+    return ht->magic == QHT_MAGIC;
+}
+
 static inline size_t qht_elems_to_buckets(size_t n_elems)
 {
     return pow2ceil(n_elems / QHT_BUCKET_ENTRIES);
@@ -356,6 +363,7 @@ void qht_init(struct qht *ht, size_t n_elems, unsigned int mode)
     size_t n_buckets = qht_elems_to_buckets(n_elems);
 
     ht->mode = mode;
+    ht->magic = QHT_MAGIC;
     qemu_mutex_init(&ht->lock);
     map = qht_map_create(n_buckets);
     atomic_rcu_set(&ht->map, map);
@@ -403,6 +411,10 @@ void qht_reset(struct qht *ht)
 {
     struct qht_map *map;
 
+    if (unlikely(!qht_inited(ht))) {
+        return;
+    }
+
     qht_map_lock_buckets__no_stale(ht, &map);
     qht_map_reset__all_locked(map);
     qht_map_unlock_buckets(map);
@@ -415,6 +427,9 @@ bool qht_reset_size(struct qht *ht, size_t n_elems)
     size_t n_buckets;
     bool resize = false;
 
+    if (unlikely(!qht_inited(ht))) {
+        return false;
+    }
     n_buckets = qht_elems_to_buckets(n_elems);
 
     qemu_mutex_lock(&ht->lock);
@@ -787,17 +802,16 @@ void qht_statistics_init(struct qht *ht, struct qht_stats *stats)
     struct qht_map *map;
     int i;
 
-    map = atomic_rcu_read(&ht->map);
-
     stats->used_head_buckets = 0;
     stats->entries = 0;
     qdist_init(&stats->chain);
     qdist_init(&stats->occupancy);
     /* bail out if the qht has not yet been initialized */
-    if (unlikely(map == NULL)) {
+    if (unlikely(!qht_inited(ht))) {
         stats->head_buckets = 0;
         return;
     }
+    map = atomic_rcu_read(&ht->map);
     stats->head_buckets = map->n_buckets;
 
     for (i = 0; i < map->n_buckets; i++) {
-- 
2.5.0

  reply	other threads:[~2016-08-10 19:26 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-10 14:26 [Qemu-devel] [PULL 00/15] tb hash improvements Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 01/15] compiler.h: add QEMU_ALIGNED() to enforce struct alignment Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 02/15] seqlock: remove optional mutex Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 03/15] seqlock: rename write_lock/unlock to write_begin/end Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 04/15] include/processor.h: define cpu_relax() Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 05/15] qemu-thread: add simple test-and-set spinlock Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 06/15] exec: add tb_hash_func5, derived from xxhash Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 07/15] tb hash: hash phys_pc, pc, and flags with xxhash Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 08/15] qdist: add module to represent frequency distributions of data Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 09/15] qdist: add test program Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 10/15] qht: QEMU's fast, resizable and scalable Hash Table Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 11/15] qht: add test program Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 12/15] qht: add qht-bench, a performance benchmark Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 13/15] qht: add test-qht-par to invoke qht-bench from 'check' target Richard Henderson
2016-06-10 14:26 ` [Qemu-devel] [PULL 14/15] tb hash: track translated blocks with qht Richard Henderson
2016-08-10 13:36   ` Igor Mammedov
2016-08-10 19:25     ` Emilio G. Cota [this message]
2016-08-11  8:43       ` [Qemu-devel] [PATCH] qht: support resetting an uninitialized qht Igor Mammedov
2016-06-10 14:26 ` [Qemu-devel] [PULL 15/15] translate-all: add tb hash bucket info to 'info jit' dump Richard Henderson
2016-07-22  9:04   ` Changlong Xie
2016-07-22 16:36     ` [Qemu-devel] [PATCH] qht: do not segfault when gathering stats from an uninitialized qht Emilio G. Cota
2016-07-23  7:45       ` Paolo Bonzini
2016-07-23 10:01       ` Peter Maydell
2016-07-23 10:54         ` Paolo Bonzini
2016-07-23 23:09           ` Emilio G. Cota
2016-06-10 15:33 ` [Qemu-devel] [PULL 00/15] tb hash improvements Peter Maydell
2016-06-10 15:57   ` Peter Maydell
2016-06-10 16:34   ` Emilio G. Cota
2016-06-10 16:41     ` Peter Maydell
2016-06-10 19:24       ` Emilio G. Cota
2016-06-11 23:09       ` Richard Henderson

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=1470857149-32003-1-git-send-email-cota@braap.org \
    --to=cota@braap.org \
    --cc=crosthwaite.peter@gmail.com \
    --cc=imammedo@redhat.com \
    --cc=mttcg@greensocs.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /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.