Received: from eggs.gnu.org ([2001:4830:134:3::10]:51344) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO2b4-00023y-0J for qemu-devel@nongnu.org; Fri, 15 Jul 2016 08:52:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bO2az-0000Y0-PI for qemu-devel@nongnu.org; Fri, 15 Jul 2016 08:52:00 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34274) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO2az-0000Xv-Gg for qemu-devel@nongnu.org; Fri, 15 Jul 2016 08:51:57 -0400 References: <1468354426-837-1-git-send-email-sergey.fedorov@linaro.org> <1468354426-837-2-git-send-email-sergey.fedorov@linaro.org> <1bfb56fa-55dd-455a-e4ec-c34ec7996baa@redhat.com> <5788D8FE.90607@gmail.com> From: Paolo Bonzini Message-ID: Date: Fri, 15 Jul 2016 14:51:50 +0200 MIME-Version: 1.0 In-Reply-To: <5788D8FE.90607@gmail.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v3 01/11] util/qht: Document memory ordering assumptions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Sergey Fedorov , Sergey Fedorov , qemu-devel@nongnu.org, mttcg@greensocs.com, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, cota@braap.org, bobby.prani@gmail.com, rth@twiddle.net Cc: peter.maydell@linaro.org, patches@linaro.org, claudio.fontana@huawei.com, mark.burton@greensocs.com, jan.kiszka@siemens.com, =?UTF-8?Q?Alex_Benn=c3=a9e?= On 15/07/2016 14:37, Sergey Fedorov wrote: > On 13/07/16 14:13, Paolo Bonzini wrote: >> diff --git a/include/qemu/qht.h b/include/qemu/qht.h >> index 70bfc68..f4f1d55 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 p= ointer is >> + * inserted into the hash table. >> + * >> * Returns true on sucess. >> * Returns false if the @p-@hash pair already exists in the hash tabl= e. >> */ >> @@ -83,6 +86,8 @@ bool qht_insert(struct qht *ht, void *p, uint32_t ha= sh); >> * >> * 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. >> * >> @@ -105,6 +110,10 @@ void *qht_lookup(struct qht *ht, qht_lookup_func_= t func, const void *userp, >> * This guarantees that concurrent lookups will always compare agains= t valid >> * data. >> * >> + * In case of successful operation, a smp_wmb() barrier is implied be= fore and >> + * after the pointer is removed from the hash table. In other words, >> + * a successful qht_remove acts as a bidirectional write barrier. >> + * >=20 > I understand why an implied wmb can be expected after the entry is > removed: so that the caller can trash the contents of the object > removed. However that would require double-check on lookup side to make > sure the entry hadn't been removed after the first lookup (something > like seqlock read side). But I have no idea why we might like to promis= e > wmb before the removal. Could you please share your point regarding thi= s? The reasoning was to make qht_remove() "look to be ordered" with respect to writes. So anything after remove wouldn't bleed into it, nor would anything before. In the case of this series, it would let you remove the smp_wmb() after tb_mark_invalid(). However, it's also okay to only handle qht_insert() and qht_lookup(), and keep the memory barrier after the TB is marked invalid (though it is unnecessary). Paolo > As of qht_insert() and qht_lookup(), I agree and this is enough > guarantees for the series. >=20 > Thanks, > Sergey >=20 >> * Returns true on success. >> * Returns false if the @p-@hash pair was not found. >> */ >> diff --git a/util/qht.c b/util/qht.c >> index 40d6e21..d38948e 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 =3D 0; i < QHT_BUCKET_ENTRIES; i++) { >> if (b->hashes[i] =3D=3D hash) { >> - void *p =3D atomic_read(&b->pointers[i]); >> + /* The pointer is dereferenced before seqlock_read_re= try, >> + * so (unlike qht_insert__locked) we need to use >> + * atomic_rcu_read here. >> + */ >> + void *p =3D atomic_rcu_read(&b->pointers[i]); >> =20 >> if (likely(p) && likely(func(p, userp))) { >> return p; >> @@ -535,6 +539,7 @@ static bool qht_insert__locked(struct qht *ht, str= uct qht_map *map, >> atomic_rcu_set(&prev->next, b); >> } >> b->hashes[i] =3D hash; >> + /* smp_wmb() implicit in seqlock_write_begin. */ >> atomic_set(&b->pointers[i], p); >> seqlock_write_end(&head->sequence); >> return true; >> @@ -659,6 +664,9 @@ bool qht_remove__locked(struct qht_map *map, struc= t qht_bucket *head, >> } >> if (q =3D=3D p) { >> qht_debug_assert(b->hashes[i] =3D=3D hash); >> + /* seqlock_write_begin and seqlock_write_end provide = write >> + * memory barrier semantics to callers of qht_remove. >> + */ >> seqlock_write_begin(&head->sequence); >> qht_bucket_remove_entry(b, i); >> seqlock_write_end(&head->sequence); >=20