From patchwork Tue Jun 9 20:07:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Robert Foley X-Patchwork-Id: 187711 Delivered-To: patch@linaro.org Received: by 2002:a92:cf06:0:0:0:0:0 with SMTP id c6csp861892ilo; Tue, 9 Jun 2020 13:12:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDJYBEHehYfJsBvVOiDJugEfOsg//o/90URGSa1qGthq7FU4UHGL9PU3AJgOt0T2pSqyOx X-Received: by 2002:a25:6403:: with SMTP id y3mr9520738ybb.229.1591733522580; Tue, 09 Jun 2020 13:12:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591733522; cv=none; d=google.com; s=arc-20160816; b=tHDFudFSESw1Zt4LPCP0ujscuFbj2+bl4TSekMU2h3Yl9YUAgy4s/zfQJHwsbQzMcS mBYyAfksvppLOLV8Sg5ypl3QV8EbonZJCF1UoOuwuQ44F5yGmNnme2VXN7cfYNgB7QLr 5PEzZhfAiE3d8fsME2l7TJly3YkSvS19NTw1FnBsdYSjzAwLm6CQZyeBJux+1zAZLgSC 6iUdhIiyT1zDBr7sbPNLoMp7DPtwpe6Ca70Bm1iIboqQHQf1qpOH/omufVqJBbb/j/uQ Mkk9URucT85QA2ZosNd4YQYTRtF9lPSBWoQjPSAa6S4WBu2hNjQ+cqC7Dzm8wLJAC2RD GFdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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:to:from :dkim-signature; bh=pD3JWxT76a4NROnBN3hpZMTAPaW7sxqBc70GvHSGYPw=; b=BzgCF+HGOdYHvCbDi9gC5bw3Rwu/0/EC8XlH1SWnjdeuK0gkaAZoVXvv8fjMieXIYk G+P5zhaGAfnkT3tnsNnnAYIvtTifDFcY77lBl1VrrkUoPQF3OMgqyAD/9+Gcp1F9qA9Y PSjtcvXhMhUUOMP0J79rLV7uVYJR9N3egNawsGZNiqwbqUyFMV08e1A2lKgV3ohOhbWH WrQtXo/wSwsJhTk1GJ7XySN5HNWqw+BOaHnvvVesWXRzR6luIUTOklqJsEKakMF86p2i I7cIy5/5tCDtTCbTdLCNW9Q3oEGyhyhhOhUzxzbfac7mXKSu/P4TVcyoDY1KMhgRMeFl +CCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=wCQnDTZK; 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=fail (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 n8si3279760ybk.265.2020.06.09.13.12.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 09 Jun 2020 13:12:02 -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=fail header.i=@linaro.org header.s=google header.b=wCQnDTZK; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:35866 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jikbJ-00027p-Vg for patch@linaro.org; Tue, 09 Jun 2020 16:12:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59274) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jikZ3-0007Os-67 for qemu-devel@nongnu.org; Tue, 09 Jun 2020 16:09:41 -0400 Received: from mail-pj1-x1043.google.com ([2607:f8b0:4864:20::1043]:36666) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jikZ2-0005ZH-7E for qemu-devel@nongnu.org; Tue, 09 Jun 2020 16:09:40 -0400 Received: by mail-pj1-x1043.google.com with SMTP id q24so1885049pjd.1 for ; Tue, 09 Jun 2020 13:09:39 -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 :mime-version:content-transfer-encoding; bh=pD3JWxT76a4NROnBN3hpZMTAPaW7sxqBc70GvHSGYPw=; b=wCQnDTZKeoZgWMDzK6ZzWdAIyphAjAWuDsiT1OCXPbI2zLvafiPEIs3hsKVdrm0LQv 4c7ABOz46g0c/CtX9kuWYATO/3bTb9ToHouMW3cjNrgLYRsbubnYCDEETXG5EH6ISEtc 0dPqkXzdw8t2NhpxWntekoroRYJalibvDEKKraXQKwq1PGsY77Ky8dtZ0A5d7iuedN/Z FF2OZu/YKr8jyl6yH2n8TFMrOaRmCh/6p3464DttMRgKevX8w18Kn0HQQrIO1JVwIdrh 4gGBEz4xB5Jv0/A7CNs6NPSD3DhzLb+wLdMSQiLWAMsVlsLCGEY3DzHGDKfJQK8K5P9F mJKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pD3JWxT76a4NROnBN3hpZMTAPaW7sxqBc70GvHSGYPw=; b=qO8o+YNZqE6DA3pX2s9WB8i885AZTkmAmxuH6LRRL4PhSvPKfpa2x62QXIPBopVxOL JHEvhi5BhULb0elgZiWVq99jPHEdnAtIEEhcXGvJbXbWwvc6SvoLVM2RAOOoktZ79kiS u7LKU2x1RfAtK7hYnq5v/S/8nmUAbv/Yr0C5kULTAOnv8cwzdVA7bRDZP6DjeoiiPWFZ pRVaBm3cCoCsJLBl4gIBiwHMfUjBSlRNqdhECngK3oPpF7cyX25WgOJj4E7Xhmj0U6k+ BM+MSy53MWuYCTVyAI81zLdpbFGqcq728NaUTeIZkFjmfiDwbbdEIEf6haRwcOtCP3Eo JxiA== X-Gm-Message-State: AOAM532QSaRyNMTuara+MUnPn2yNtPq8p4UaW84QVOBEydm9WCgaaE6s MJNwIj12p9Cp4uEX/Vj9ZZKAqEFHKVrUMA== X-Received: by 2002:a17:90a:240c:: with SMTP id h12mr6670763pje.42.1591733378558; Tue, 09 Jun 2020 13:09:38 -0700 (PDT) Received: from Rfoley-MA01.hsd1.ma.comcast.net ([2601:199:4480:60c0:1d09:cef2:3b1f:abce]) by smtp.gmail.com with ESMTPSA id n19sm10523374pfu.194.2020.06.09.13.09.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jun 2020 13:09:37 -0700 (PDT) From: Robert Foley To: qemu-devel@nongnu.org Subject: [PATCH v3 02/13] cpu: convert queued work to a QSIMPLEQ Date: Tue, 9 Jun 2020 16:07:27 -0400 Message-Id: <20200609200738.445-3-robert.foley@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200609200738.445-1-robert.foley@linaro.org> References: <20200609200738.445-1-robert.foley@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1043; envelope-from=robert.foley@linaro.org; helo=mail-pj1-x1043.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: robert.foley@linaro.org, Eduardo Habkost , cota@braap.org, Paolo Bonzini , peter.puhov@linaro.org, alex.bennee@linaro.org, Richard Henderson Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: "Emilio G. Cota" We convert queued work to a QSIMPLEQ, instead of open-coding it. While at it, make sure that all accesses to the list are performed while holding the list's lock. Reviewed-by: Richard Henderson Reviewed-by: Alex Bennée Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Emilio G. Cota Signed-off-by: Robert Foley --- cpus-common.c | 25 ++++++++----------------- cpus.c | 14 ++++++++++++-- hw/core/cpu.c | 1 + include/hw/core/cpu.h | 6 +++--- 4 files changed, 24 insertions(+), 22 deletions(-) -- 2.17.1 diff --git a/cpus-common.c b/cpus-common.c index 70a9d12981..8f5512b3d7 100644 --- a/cpus-common.c +++ b/cpus-common.c @@ -97,7 +97,7 @@ void cpu_list_remove(CPUState *cpu) } struct qemu_work_item { - struct qemu_work_item *next; + QSIMPLEQ_ENTRY(qemu_work_item) node; run_on_cpu_func func; run_on_cpu_data data; bool free, exclusive, done; @@ -106,13 +106,7 @@ struct qemu_work_item { static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi) { qemu_mutex_lock(&cpu->work_mutex); - if (cpu->queued_work_first == NULL) { - cpu->queued_work_first = wi; - } else { - cpu->queued_work_last->next = wi; - } - cpu->queued_work_last = wi; - wi->next = NULL; + QSIMPLEQ_INSERT_TAIL(&cpu->work_list, wi, node); wi->done = false; qemu_mutex_unlock(&cpu->work_mutex); @@ -306,17 +300,14 @@ void process_queued_cpu_work(CPUState *cpu) { struct qemu_work_item *wi; - if (cpu->queued_work_first == NULL) { + qemu_mutex_lock(&cpu->work_mutex); + if (QSIMPLEQ_EMPTY(&cpu->work_list)) { + qemu_mutex_unlock(&cpu->work_mutex); return; } - - qemu_mutex_lock(&cpu->work_mutex); - while (cpu->queued_work_first != NULL) { - wi = cpu->queued_work_first; - cpu->queued_work_first = wi->next; - if (!cpu->queued_work_first) { - cpu->queued_work_last = NULL; - } + while (!QSIMPLEQ_EMPTY(&cpu->work_list)) { + wi = QSIMPLEQ_FIRST(&cpu->work_list); + QSIMPLEQ_REMOVE_HEAD(&cpu->work_list, node); qemu_mutex_unlock(&cpu->work_mutex); if (wi->exclusive) { /* Running work items outside the BQL avoids the following deadlock: diff --git a/cpus.c b/cpus.c index 5670c96bcf..af44027549 100644 --- a/cpus.c +++ b/cpus.c @@ -97,9 +97,19 @@ bool cpu_is_stopped(CPUState *cpu) return cpu->stopped || !runstate_is_running(); } +static inline bool cpu_work_list_empty(CPUState *cpu) +{ + bool ret; + + qemu_mutex_lock(&cpu->work_mutex); + ret = QSIMPLEQ_EMPTY(&cpu->work_list); + qemu_mutex_unlock(&cpu->work_mutex); + return ret; +} + static bool cpu_thread_is_idle(CPUState *cpu) { - if (cpu->stop || cpu->queued_work_first) { + if (cpu->stop || !cpu_work_list_empty(cpu)) { return false; } if (cpu_is_stopped(cpu)) { @@ -1498,7 +1508,7 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg) cpu = first_cpu; } - while (cpu && !cpu->queued_work_first && !cpu->exit_request) { + while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) { atomic_mb_set(&tcg_current_rr_cpu, cpu); current_cpu = cpu; diff --git a/hw/core/cpu.c b/hw/core/cpu.c index f31ec48ee6..80d51c24dd 100644 --- a/hw/core/cpu.c +++ b/hw/core/cpu.c @@ -370,6 +370,7 @@ static void cpu_common_initfn(Object *obj) cpu->nr_threads = 1; qemu_mutex_init(&cpu->work_mutex); + QSIMPLEQ_INIT(&cpu->work_list); QTAILQ_INIT(&cpu->breakpoints); QTAILQ_INIT(&cpu->watchpoints); diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 497600c49e..b3f4b79318 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -331,8 +331,8 @@ struct qemu_work_item; * @opaque: User data. * @mem_io_pc: Host Program Counter at which the memory was accessed. * @kvm_fd: vCPU file descriptor for KVM. - * @work_mutex: Lock to prevent multiple access to queued_work_*. - * @queued_work_first: First asynchronous work pending. + * @work_mutex: Lock to prevent multiple access to @work_list. + * @work_list: List of pending asynchronous work. * @trace_dstate_delayed: Delayed changes to trace_dstate (includes all changes * to @trace_dstate). * @trace_dstate: Dynamic tracing state of events for this vCPU (bitmask). @@ -376,7 +376,7 @@ struct CPUState { sigjmp_buf jmp_env; QemuMutex work_mutex; - struct qemu_work_item *queued_work_first, *queued_work_last; + QSIMPLEQ_HEAD(, qemu_work_item) work_list; CPUAddressSpace *cpu_ases; int num_ases;