Received: from eggs.gnu.org ([2001:4830:134:3::10]:56833) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bKCP7-0002Aq-CQ for qemu-devel@nongnu.org; Mon, 04 Jul 2016 18:31:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bKCP2-00062B-6z for qemu-devel@nongnu.org; Mon, 04 Jul 2016 18:31:48 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:34995) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bKCOz-00061j-Tq for qemu-devel@nongnu.org; Mon, 04 Jul 2016 18:31:44 -0400 Date: Mon, 4 Jul 2016 18:31:32 -0400 From: "Emilio G. Cota" Message-ID: <20160704223132.GB29624@flamenco> References: <1467389770-9738-1-git-send-email-alex.bennee@linaro.org> <1467389770-9738-2-git-send-email-alex.bennee@linaro.org> <20160702001736.GA2295@flamenco> <87y45kiki8.fsf@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <87y45kiki8.fsf@linaro.org> Subject: Re: [Qemu-devel] [PATCH 1/2] tcg: Ensure safe tb_jmp_cache lookup out of 'tb_lock' List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alex =?iso-8859-1?Q?Benn=E9e?= Cc: mttcg@listserver.greensocs.com, qemu-devel@nongnu.org, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, serge.fdrv@gmail.com, 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, Sergey Fedorov , Peter Crosthwaite On Sat, Jul 02, 2016 at 08:09:35 +0100, Alex Bennée wrote: > > Emilio G. Cota writes: > > > On Fri, Jul 01, 2016 at 17:16:09 +0100, Alex Bennée wrote: > >> From: Sergey Fedorov > > (snip) > >> @@ -333,7 +338,7 @@ static inline TranslationBlock *tb_find_fast(CPUState *cpu, > >> is executed. */ > >> cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags); > >> tb_lock(); > >> - tb = cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)]; > >> + tb = atomic_read(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)]); > >> if (unlikely(!tb || tb->pc != pc || tb->cs_base != cs_base || > >> tb->flags != flags)) { > >> tb = tb_find_slow(cpu, pc, cs_base, flags); > >> diff --git a/translate-all.c b/translate-all.c > >> index eaa95e4..1fcfe79 100644 > >> --- a/translate-all.c > >> +++ b/translate-all.c > >> @@ -1004,11 +1004,16 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) > >> invalidate_page_bitmap(p); > >> } > >> > >> + /* Ensure that we won't find the TB in the shared hash table > >> + * if we con't see it in CPU's local cache. > > > > s/con't/can't/ > > > >> + * Pairs with smp_rmb() in tb_find_slow(). */ > >> + smp_wmb(); > > > > This fence is already embedded in qht_remove, since it internally > > calls seqlock_write_end() on a successful removal, so we could get > > away with a comment instead of emitting a redundant fence. > > However, if qht ever changed its implementation this would have > > to be taken into account. So I'd be OK with emitting the > > fence here too. > > > >> + > >> /* remove the TB from the hash list */ > >> h = tb_jmp_cache_hash_func(tb->pc); > >> CPU_FOREACH(cpu) { > >> if (cpu->tb_jmp_cache[h] == tb) { > > > > Missing atomic_read here: if (atomic_read(cpu->tb_jmp_cache[...])) { > > Oops, good catch. My mistake. An atomic_read here isn't needed: as the commit message points out, we only need atomic_read when tb_lock isn't held. In this case tb_lock is held, so we only use atomic accesses for writing to the array. E.