Received: from eggs.gnu.org ([2001:4830:134:3::10]:60360) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1boQHk-0004uj-70 for qemu-devel@nongnu.org; Mon, 26 Sep 2016 03:25:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1boQHg-0006V5-0C for qemu-devel@nongnu.org; Mon, 26 Sep 2016 03:25:07 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:36152) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1boQHf-0006UP-Q7 for qemu-devel@nongnu.org; Mon, 26 Sep 2016 03:25:03 -0400 Received: by mail-wm0-f68.google.com with SMTP id b184so12640590wma.3 for ; Mon, 26 Sep 2016 00:25:03 -0700 (PDT) Sender: Paolo Bonzini References: <1474615909-17069-1-git-send-email-pbonzini@redhat.com> <1474615909-17069-16-git-send-email-pbonzini@redhat.com> <1562781019.2636285.1474717908026.JavaMail.zimbra@redhat.com> <1237a144-5a83-8fe9-fddd-6891c3055005@twiddle.net> From: Paolo Bonzini Message-ID: <17a8bf21-51c7-7861-c0b3-8fb2e3dcf08d@redhat.com> Date: Mon, 26 Sep 2016 09:24:01 +0200 MIME-Version: 1.0 In-Reply-To: <1237a144-5a83-8fe9-fddd-6891c3055005@twiddle.net> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH 15/16] tcg: Make tb_flush() thread safe List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Richard Henderson Cc: serge fdrv , cota@braap.org, alex bennee , qemu-devel@nongnu.org, sergey fedorov On 24/09/2016 22:44, Richard Henderson wrote: > On 09/24/2016 04:51 AM, Paolo Bonzini wrote: >> >> >> ----- Original Message ----- >>> From: "Richard Henderson" >>> To: "Paolo Bonzini" , qemu-devel@nongnu.org >>> Cc: "serge fdrv" , cota@braap.org, "alex >>> bennee" , "sergey fedorov" >>> >>> Sent: Friday, September 23, 2016 8:06:09 PM >>> Subject: Re: [Qemu-devel] [PATCH 15/16] tcg: Make tb_flush() thread safe >>> >>> On 09/23/2016 12:31 AM, Paolo Bonzini wrote: >>>> + unsigned tb_flush_req = (unsigned) (uintptr_t) data; >>> >>> Extra cast? >>> >>>> - tcg_ctx.tb_ctx.tb_flush_count++; >>>> + atomic_inc(&tcg_ctx.tb_ctx.tb_flush_count); >>> >>> Since this is the only place this value is incremented, and we're >>> under a >>> lock, >>> it should be cheaper to use >>> >>> atomic_mb_set(&tcg_ctx.tb_ctx.tb_flush_count, tb_flush_req + 1); >> >> atomic_set will do even. Though it's not really a fast path, which is >> why I went for atomic_inc. > > Don't we need the flush to be complete before the new count is seen? > That's why I was suggesting the mb_set. You're right in that the mb_set is more correct, or even better would be a store-release. Actually even atomic_set works, though in a fairly surprising manner. This is because the final check is done by do_tb_flush under the mutex, so the final check does wait for the flush to be complete. If tb_flush_count is exposed too early to tb_flush, all that can happen is that do_tb_flush sees a tb_flush_req that's more recent than it should. do_tb_flush then incorrectly redoes the flush, but that's not a disaster. But I'm changing it to mb_set as you suggested. Paolo