From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48580) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO7OF-0002Ti-Ic for qemu-devel@nongnu.org; Fri, 15 Jul 2016 13:59:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bO7O9-0006xo-A5 for qemu-devel@nongnu.org; Fri, 15 Jul 2016 13:59:06 -0400 Received: from mail-lf0-x22c.google.com ([2a00:1450:4010:c07::22c]:36045) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO7O9-0006wo-26 for qemu-devel@nongnu.org; Fri, 15 Jul 2016 13:59:01 -0400 Received: by mail-lf0-x22c.google.com with SMTP id y184so4007175lfd.3 for ; Fri, 15 Jul 2016 10:59:00 -0700 (PDT) From: Sergey Fedorov Date: Fri, 15 Jul 2016 20:58:41 +0300 Message-Id: <20160715175852.30749-2-sergey.fedorov@linaro.org> In-Reply-To: <20160715175852.30749-1-sergey.fedorov@linaro.org> References: <20160715175852.30749-1-sergey.fedorov@linaro.org> Subject: [Qemu-devel] [PATCH v4 01/12] util/qht: Document memory ordering assumptions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: patches@linaro.org, Sergey Fedorov , mttcg@listserver.greensocs.com, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, cota@braap.org, bobby.prani@gmail.com, rth@twiddle.net, mark.burton@greensocs.com, pbonzini@redhat.com, jan.kiszka@siemens.com, peter.maydell@linaro.org, claudio.fontana@huawei.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= From: Paolo Bonzini It is naturally expected that some memory ordering should be provided around qht_insert() and qht_lookup(). Document these assumptions in the header file and put some comments in the source to denote how that memory ordering requirements are fulfilled. Signed-off-by: Paolo Bonzini [Sergey Fedorov: commit title and message provided; comment on qht_remove() elided] Signed-off-by: Sergey Fedorov --- Changes in v4: - Modified version of Paolo's patch is used --- include/qemu/qht.h | 5 +++++ util/qht.c | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/qemu/qht.h b/include/qemu/qht.h index 70bfc68b8d67..311139b85a9a 100644 --- a/include/qemu/qht.h +++ b/include/qemu/qht.h @@ -69,6 +69,9 @@ void qht_destroy(struct qht *ht); * Attempting to insert a NULL @p is a bug. * Inserting the same pointer @p with different @hash values is a bug. * + * In case of successful operation, smp_wmb() is implied before the pointer is + * inserted into the hash table. + * * Returns true on sucess. * Returns false if the @p-@hash pair already exists in the hash table. */ @@ -83,6 +86,8 @@ bool qht_insert(struct qht *ht, void *p, uint32_t hash); * * Needs to be called under an RCU read-critical section. * + * smp_read_barrier_depends() is implied before the call to @func. + * * The user-provided @func compares pointers in QHT against @userp. * If the function returns true, a match has been found. * diff --git a/util/qht.c b/util/qht.c index 40d6e218f759..28ce289245a7 100644 --- a/util/qht.c +++ b/util/qht.c @@ -445,7 +445,11 @@ void *qht_do_lookup(struct qht_bucket *head, qht_lookup_func_t func, do { for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { if (b->hashes[i] == hash) { - void *p = atomic_read(&b->pointers[i]); + /* The pointer is dereferenced before seqlock_read_retry, + * so (unlike qht_insert__locked) we need to use + * atomic_rcu_read here. + */ + void *p = atomic_rcu_read(&b->pointers[i]); if (likely(p) && likely(func(p, userp))) { return p; @@ -535,6 +539,7 @@ static bool qht_insert__locked(struct qht *ht, struct qht_map *map, atomic_rcu_set(&prev->next, b); } b->hashes[i] = hash; + /* smp_wmb() implicit in seqlock_write_begin. */ atomic_set(&b->pointers[i], p); seqlock_write_end(&head->sequence); return true; -- 2.9.1