From patchwork Fri Jul 15 17:58:41 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: 9232399 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 9EDA860865 for ; Fri, 15 Jul 2016 18:04:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 81D1E27D9B for ; Fri, 15 Jul 2016 18:04:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7696027F94; Fri, 15 Jul 2016 18:04:18 +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 1C95F27D9B for ; Fri, 15 Jul 2016 18:04:18 +0000 (UTC) Received: from localhost ([::1]:34149 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bO7TF-0007gB-Ad for patchwork-qemu-devel@patchwork.kernel.org; Fri, 15 Jul 2016 14:04:17 -0400 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) 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=PPvPlOv8JK1aApgCkkH1fMFvmGZPbJcxSIQTGDRR5+0=; b=UchAT0w+hWWY/nkTp5RzH2dSYyRCsjzqJt/irrrRb8ht6mu3ttK6QmEXLHKxM04aKH 02vLwpLei5XfCMQIhrsol1ROXN0lRclR8I1KtyEX/2lJAHaKOL3LtKhGJqWrrXeKLvIa 6oLgja3aJbmc7pAkPstdeLtWG9gYvLDp/ymWA= 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=PPvPlOv8JK1aApgCkkH1fMFvmGZPbJcxSIQTGDRR5+0=; b=HR4WHOgJwTpuid38BRpIXFnqyAuIxfEHItUheZWCnvmCfIHaCvZ8Mvg5NqVBEaoZPy 47Yv2IF8T3f1s8SQQJEHHu0mA2y0lyq7N2xRgRoE9nwND3l0LLl68MGFnVPWYNLwwdNx /uLhwf8RigZe8Sj4Emgz/9OMrP5c4qE7I2ONzbqq3HHeM4hNmVTWjnFEojywYeHvpF6N Lmfh8IlwctRWC4AyJp/z+QRQ9k9yCakeatVUMyqkv9KtWsF/ZBtG9rnW3GRR3Nwn+Fk4 /yzrayiWBUprXl9+63aYxW6HW51sHYo4obv0/qyBBApSDP667cLaa8vN5WD1FwLrZur3 rgkw== X-Gm-Message-State: ALyK8tJXDWu49XzaHd0KYLAPR4wG6L32F5j5R62L2JtSkTGOIehjNE5hYRSUQwS7I7Xhkipg X-Received: by 10.25.76.137 with SMTP id z131mr9957316lfa.49.1468605540170; Fri, 15 Jul 2016 10:59:00 -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 g193sm2113268lfb.14.2016.07.15.10.58.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Jul 2016 10:58:59 -0700 (PDT) From: Sergey Fedorov To: qemu-devel@nongnu.org Date: Fri, 15 Jul 2016 20:58:41 +0300 Message-Id: <20160715175852.30749-2-sergey.fedorov@linaro.org> X-Mailer: git-send-email 2.9.1 In-Reply-To: <20160715175852.30749-1-sergey.fedorov@linaro.org> References: <20160715175852.30749-1-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::22c Subject: [Qemu-devel] [PATCH v4 01/12] 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: mttcg@listserver.greensocs.com, peter.maydell@linaro.org, claudio.fontana@huawei.com, patches@linaro.org, jan.kiszka@siemens.com, mark.burton@greensocs.com, a.rigo@virtualopensystems.com, cota@braap.org, pbonzini@redhat.com, Sergey Fedorov , bobby.prani@gmail.com, rth@twiddle.net, =?UTF-8?q?Alex=20Benn=C3=A9e?= , fred.konrad@greensocs.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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;