From patchwork Sat Aug 26 23:24:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 717232 Delivered-To: patch@linaro.org Received: by 2002:a5d:688a:0:b0:317:ecd7:513f with SMTP id h10csp547109wru; Sat, 26 Aug 2023 16:25:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEDtJjQytHiNHWQkYKoP2ykmud+SjCfn5XRrYWuj6zEluTSx8w5KzaOU+T6Y+PGBQJsIFoi X-Received: by 2002:a05:622a:1992:b0:410:ab6d:54da with SMTP id u18-20020a05622a199200b00410ab6d54damr17670944qtc.16.1693092319606; Sat, 26 Aug 2023 16:25:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693092319; cv=none; d=google.com; s=arc-20160816; b=Z59xyj1KdTZaJCm3FFMu6NmQ2UwHwuy77kpIwNU+0VLTjyUErl5LxQrxbBq+/szznK eaMi9MwFKxsAl0nyJJf2JxK9zY2xD0lXe+A+IzgE21usZP7nhYH6CgZYjmJ83e4XlkMa Y2LdywIkfQJKILwCiD3YZiQg6SalFr/UG/cbGrsC1FpXMkjGXtcGqwfhqbe75IRDt/oY ap7qdWpttuBuZDlvVdzwljKxSwY6ySZ0B3mtfOg5nXl5PptsD762u/7H84+oBfzHv+AC ecrP8w/HKVybJPHGRmU4LzATcKz73zG9p6Yf9cLxvBC4ItfxTRMRBz+IMOuxd/yhe4BE ZP6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=m4J6eq/v5V1H1fkQEu9wQ8wHYQUH4m1you9F+PZm5SU=; fh=fuBYewipBenZDUITvBadSj9IJzxQGV99ZSsGoAYZmvE=; b=xVXEmTNJ0wJE0HgQk/0QiF4vLno3AQqvmZsUXsodNsNgaiHd4WgftWujAakYjYsJ0Q 4flM3VY9KbemAJrERwhyzF6jk5o58iDpVWFE8ZbRUm0T9buhU9K8VyudjbmIFPrze052 ORXMKPhcD/xF/NWalI3+jJpUupmF+VZxRtV0Y1Lnjz3c1uiGPNg4+oy7VYRxMJJWWNOr VoCT9sALqlfZXwhK3o1BYaXT1kUWscDVrtl2OZk5Rf8RUfrFKcLTK62qtoFEJUlvP+JB zIEdpApi0osbdV1QUBGHX0cIiAMQ4WCdRV4y93gmSKOuIP9b4T5fYse/1YIAKU2Ml2hr pPDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=vCPnKSVb; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id y15-20020a05622a164f00b00403b0f943easi2970256qtj.728.2023.08.26.16.25.19 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 26 Aug 2023 16:25:19 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=vCPnKSVb; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qa2dy-0000T0-1O; Sat, 26 Aug 2023 19:24:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qa2dn-0000Rb-3L for qemu-devel@nongnu.org; Sat, 26 Aug 2023 19:24:28 -0400 Received: from mail-oi1-x22b.google.com ([2607:f8b0:4864:20::22b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qa2dh-00066R-Pf for qemu-devel@nongnu.org; Sat, 26 Aug 2023 19:24:25 -0400 Received: by mail-oi1-x22b.google.com with SMTP id 5614622812f47-3a7f4f7a8easo1096713b6e.2 for ; Sat, 26 Aug 2023 16:24:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693092260; x=1693697060; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=m4J6eq/v5V1H1fkQEu9wQ8wHYQUH4m1you9F+PZm5SU=; b=vCPnKSVbESkMAxpwhkHwJlUwtYIa1IJnx6FSggjfoNMgvBDrHFZ0TNmKfPpur7o5uc NsOrTk0OwVzDaQ1d3WBdllcIyhO5/j4fjSuFeU/vy8zrKYj3FIONpBkkZNVg5LkY2cAI fdMLj/Y58Hy0cEvJ8uFjWH0WmwNdxVVzositPEP9g7fgaujBTqmDwykU0gFIDyK8cOD4 PThh/IyCD4hRtfvHgJfA6WGa1OM9Loiq8CAYs2hAp4zczr7+z8uPaxQZKbafrO7ivDup xiQM9Octg6rcYpA8flFa2ynnAPYTQUDhwSa3K4Up3uYCMG77jRDxNDEv8kD+SbbftSOB XhGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693092260; x=1693697060; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=m4J6eq/v5V1H1fkQEu9wQ8wHYQUH4m1you9F+PZm5SU=; b=cD9j4Ram9ncrIfCR77p8qWCBCnk/c75ry030iE9mOLBXHxUG2edo+lEbBARqiyiROP dHf51IPhURHPS0heL3414tq7B8ru9LJe+w8QmyYqGGQVG2GFYz00nMppQHzXjA533xWP yE4k35TUoaDMYAtSCe9Kuu8h6tRPRSASnOa8+weVxW+lBDzulvP8uunpv6GdTzx4oe59 OZ/av3yznCzwI6i6b/MvndfpEwYjquszRpz0Hx0zvghca422LyaYFf7RedRq0fIlp7V+ 0QHejAUdHrBAsofK3gNyeIk94OiPVrnVW2jlLGhsuVEzHxfgehm5byGPOmyQ4j2wLXQM GQgw== X-Gm-Message-State: AOJu0Yx5fY+GAGCfuBDz8pSDazCK3hwtiQDrQDo9MXmS8D6L9fpdB9N1 4iU8qkpub9ZaTBSrpq4dPRz2J64tMYt02Z+hcFE= X-Received: by 2002:a54:441a:0:b0:3a8:f3e7:d691 with SMTP id k26-20020a54441a000000b003a8f3e7d691mr6534937oiw.37.1693092260577; Sat, 26 Aug 2023 16:24:20 -0700 (PDT) Received: from stoup.. ([71.212.131.115]) by smtp.gmail.com with ESMTPSA id u9-20020a63a909000000b005633311c70dsm4226472pge.32.2023.08.26.16.24.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Aug 2023 16:24:19 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, pbonzini@redhat.com, alex.bennee@linaro.org Subject: [PATCH 2/3] softmmu: Use async_run_on_cpu in tcg_commit Date: Sat, 26 Aug 2023 16:24:14 -0700 Message-Id: <20230826232415.80233-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230826232415.80233-1-richard.henderson@linaro.org> References: <20230826232415.80233-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::22b; envelope-from=richard.henderson@linaro.org; helo=mail-oi1-x22b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org After system startup, run the update to memory_dispatch and the tlb_flush on the cpu. This eliminates a race, wherein a running cpu sees the memory_dispatch change but has not yet seen the tlb_flush. Since the update now happens on the cpu, we need not use qatomic_rcu_read to protect the read of memory_dispatch. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1826 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1834 Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1846 Signed-off-by: Richard Henderson Tested-by: Alex Bennée Reviewed-by: Alex Bennée Tested-by: Jonathan Cameron --- softmmu/physmem.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 7597dc1c39..18277ddd67 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -680,8 +680,7 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr orig_addr, IOMMUTLBEntry iotlb; int iommu_idx; hwaddr addr = orig_addr; - AddressSpaceDispatch *d = - qatomic_rcu_read(&cpu->cpu_ases[asidx].memory_dispatch); + AddressSpaceDispatch *d = cpu->cpu_ases[asidx].memory_dispatch; for (;;) { section = address_space_translate_internal(d, addr, &addr, plen, false); @@ -2412,7 +2411,7 @@ MemoryRegionSection *iotlb_to_section(CPUState *cpu, { int asidx = cpu_asidx_from_attrs(cpu, attrs); CPUAddressSpace *cpuas = &cpu->cpu_ases[asidx]; - AddressSpaceDispatch *d = qatomic_rcu_read(&cpuas->memory_dispatch); + AddressSpaceDispatch *d = cpuas->memory_dispatch; int section_index = index & ~TARGET_PAGE_MASK; MemoryRegionSection *ret; @@ -2487,23 +2486,42 @@ static void tcg_log_global_after_sync(MemoryListener *listener) } } +static void tcg_commit_cpu(CPUState *cpu, run_on_cpu_data data) +{ + CPUAddressSpace *cpuas = data.host_ptr; + + cpuas->memory_dispatch = address_space_to_dispatch(cpuas->as); + tlb_flush(cpu); +} + static void tcg_commit(MemoryListener *listener) { CPUAddressSpace *cpuas; - AddressSpaceDispatch *d; + CPUState *cpu; assert(tcg_enabled()); /* since each CPU stores ram addresses in its TLB cache, we must reset the modified entries */ cpuas = container_of(listener, CPUAddressSpace, tcg_as_listener); - cpu_reloading_memory_map(); - /* The CPU and TLB are protected by the iothread lock. - * We reload the dispatch pointer now because cpu_reloading_memory_map() - * may have split the RCU critical section. + cpu = cpuas->cpu; + + /* + * Defer changes to as->memory_dispatch until the cpu is quiescent. + * Otherwise we race between (1) other cpu threads and (2) ongoing + * i/o for the current cpu thread, with data cached by mmu_lookup(). + * + * In addition, queueing the work function will kick the cpu back to + * the main loop, which will end the RCU critical section and reclaim + * the memory data structures. + * + * That said, the listener is also called during realize, before + * all of the tcg machinery for run-on is initialized: thus halt_cond. */ - d = address_space_to_dispatch(cpuas->as); - qatomic_rcu_set(&cpuas->memory_dispatch, d); - tlb_flush(cpuas->cpu); + if (cpu->halt_cond) { + async_run_on_cpu(cpu, tcg_commit_cpu, RUN_ON_CPU_HOST_PTR(cpuas)); + } else { + tcg_commit_cpu(cpu, RUN_ON_CPU_HOST_PTR(cpuas)); + } } static void memory_map_init(void)