From patchwork Tue Jul 12 20:13:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sergey.fedorov@linaro.org X-Patchwork-Id: 9226185 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E5FD7604DB for ; Tue, 12 Jul 2016 20:14:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D640E271FD for ; Tue, 12 Jul 2016 20:14:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C920227D85; Tue, 12 Jul 2016 20:14:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1F180271FD for ; Tue, 12 Jul 2016 20:14:57 +0000 (UTC) Received: from localhost ([::1]:43181 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bN452-0001hD-3a for patchwork-qemu-devel@patchwork.kernel.org; Tue, 12 Jul 2016 16:14:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42890) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bN44T-0001ff-A7 for qemu-devel@nongnu.org; Tue, 12 Jul 2016 16:14:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bN44Q-0008By-4E for qemu-devel@nongnu.org; Tue, 12 Jul 2016 16:14:20 -0400 Received: from mail-lf0-x233.google.com ([2a00:1450:4010:c07::233]:35739) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bN44P-0008Bp-OY for qemu-devel@nongnu.org; Tue, 12 Jul 2016 16:14:18 -0400 Received: by mail-lf0-x233.google.com with SMTP id f93so22864579lfi.2 for ; Tue, 12 Jul 2016 13:14:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=c4QiJh5EB0E5gPWsCTU8b/A7JXctLw8Wg8G7nqLbl2I=; b=acmgB+7Yxn9glt+LS5IX0FN4rE28cURAlGb1afV5Tg/CAsuibO06iZBqJtzkSibpof EzbiTcuS4EYLTe5cVf9qbc+ppwTtlho3ENE6/ifkZqug7nZG3jbFJAiItaMlBso7VaEj /0TmCX4Sgejf98pcmLeViY1YwrXGoZTeWYczM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=c4QiJh5EB0E5gPWsCTU8b/A7JXctLw8Wg8G7nqLbl2I=; b=F3+E9zsOaZxRzcKCfTkiF/s/dh5HimTw5u3o2ZyuEEXIK09qdIBVxCDur+Jh/+rRLo ANlw2PyBcAfUyTA0RihXUd2jQErNdgnzi0S2mRiiRi6PD1EbQdX8RbUveKabM0NoUKBj KP64thyu6P71gMveXbE69GyReV2wdkLnnho1zV/FjEm/kgZKxcSS0IoGCfqVaDNSR3RS T/boo9sVW8SbePkOFDiYwOmizNGzL7391u0IOmtWgfa3+cYsGv0CzjBrJZGtulMO237Y i+diWLH6jmG54YQhcsV3F2dZ5TsJZHc9q71qs7HcZ4ywPIBapq7Kzk65uN9E9tA1PFdu RkMg== X-Gm-Message-State: ALyK8tIscmf52n3yuAlaTL0y30fGTVRus6DhuabBhXg1InVtCsQXUiRuWiv/HlQZwXBVbxxu X-Received: by 10.46.32.131 with SMTP id g3mr2263108lji.46.1468354457069; Tue, 12 Jul 2016 13:14:17 -0700 (PDT) Received: from sergey-laptop.Dlink (broadband-46-188-120-37.2com.net. [46.188.120.37]) by smtp.gmail.com with ESMTPSA id g40sm2474029ljg.22.2016.07.12.13.14.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 12 Jul 2016 13:14:16 -0700 (PDT) From: Sergey Fedorov To: qemu-devel@nongnu.org, mttcg@listserver.greensocs.com, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, serge.fdrv@gmail.com, cota@braap.org, bobby.prani@gmail.com, rth@twiddle.net Date: Tue, 12 Jul 2016 23:13:36 +0300 Message-Id: <1468354426-837-2-git-send-email-sergey.fedorov@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1468354426-837-1-git-send-email-sergey.fedorov@linaro.org> References: <1468354426-837-1-git-send-email-sergey.fedorov@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:4010:c07::233 Subject: [Qemu-devel] [PATCH v3 01/11] util/qht: Document memory ordering assumptions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, Sergey Fedorov , patches@linaro.org, claudio.fontana@huawei.com, mark.burton@greensocs.com, jan.kiszka@siemens.com, pbonzini@redhat.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Sergey Fedorov It is naturally expected that some memory ordering should be provided around qht_insert(), qht_remove(), 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: Sergey Fedorov Signed-off-by: Sergey Fedorov Signed-off-by: Paolo Bonzini --- include/qemu/qht.h | 9 +++++++++ util/qht.c | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/include/qemu/qht.h b/include/qemu/qht.h index 70bfc68b8d67..5f633e5d8100 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. */ @@ -86,6 +89,9 @@ bool qht_insert(struct qht *ht, void *p, uint32_t hash); * The user-provided @func compares pointers in QHT against @userp. * If the function returns true, a match has been found. * + * smp_rmb() is implied before and after the pointer is looked up and retrieved + * from the hash table. + * * Returns the corresponding pointer when a match is found. * Returns NULL otherwise. */ @@ -105,6 +111,9 @@ void *qht_lookup(struct qht *ht, qht_lookup_func_t func, const void *userp, * This guarantees that concurrent lookups will always compare against valid * data. * + * In case of successful operation, smp_wmb() is implied after the pointer is + * removed from the hash table. + * * 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 40d6e218f759..0469d068b4de 100644 --- a/util/qht.c +++ b/util/qht.c @@ -466,8 +466,10 @@ void *qht_lookup__slowpath(struct qht_bucket *b, qht_lookup_func_t func, void *ret; do { + /* seqlock_read_begin() also serves as the gaurantee of smp_rmb() */ version = seqlock_read_begin(&b->sequence); ret = qht_do_lookup(b, func, userp, hash); + /* seqlock_read_retry() also serves as the gaurantee of smp_rmb() */ } while (seqlock_read_retry(&b->sequence, version)); return ret; } @@ -483,8 +485,10 @@ void *qht_lookup(struct qht *ht, qht_lookup_func_t func, const void *userp, map = atomic_rcu_read(&ht->map); b = qht_map_to_bucket(map, hash); + /* seqlock_read_begin() also serves as the gaurantee of smp_rmb() */ version = seqlock_read_begin(&b->sequence); ret = qht_do_lookup(b, func, userp, hash); + /* seqlock_read_retry() also serves as the gaurantee of smp_rmb() */ if (likely(!seqlock_read_retry(&b->sequence, version))) { return ret; } @@ -530,6 +534,7 @@ static bool qht_insert__locked(struct qht *ht, struct qht_map *map, found: /* found an empty key: acquire the seqlock and write */ + /* seqlock_write_begin() also serves as the guarantee of smp_wmb() */ seqlock_write_begin(&head->sequence); if (new) { atomic_rcu_set(&prev->next, b); @@ -661,6 +666,9 @@ bool qht_remove__locked(struct qht_map *map, struct qht_bucket *head, qht_debug_assert(b->hashes[i] == hash); seqlock_write_begin(&head->sequence); qht_bucket_remove_entry(b, i); + /* seqlock_write_end() also serves as the guarantee of + * smp_wmb() + */ seqlock_write_end(&head->sequence); return true; }