From patchwork Mon Sep 12 18:25:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pranith Kumar X-Patchwork-Id: 668925 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 ozlabs.org (Postfix) with ESMTPS id 3sXxBV6LRTz9s9c for ; Tue, 13 Sep 2016 04:28:02 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=asP9prIp; dkim-atps=neutral Received: from localhost ([::1]:44547 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjVxY-0002Wm-EM for incoming@patchwork.ozlabs.org; Mon, 12 Sep 2016 14:28:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41053) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjVwY-0001qZ-U3 for qemu-devel@nongnu.org; Mon, 12 Sep 2016 14:27:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bjVwU-0006Nh-Ky for qemu-devel@nongnu.org; Mon, 12 Sep 2016 14:26:57 -0400 Received: from mail-yw0-f193.google.com ([209.85.161.193]:35680) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjVwU-00067f-FJ for qemu-devel@nongnu.org; Mon, 12 Sep 2016 14:26:54 -0400 Received: by mail-yw0-f193.google.com with SMTP id u82so3610355ywc.2 for ; Mon, 12 Sep 2016 11:26:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=references:from:to:cc:subject:in-reply-to:date:message-id :mime-version; bh=NgWlAzEfK5CuTn1mie/HvMbfDyWDaa5ZyN2yjiSLKhE=; b=asP9prIp3T4UFqFFSCIpkU6Z/9pGGcdFuLhqQgUwNfESqCgqcLSFTlWl7mwj4JnCog t4MRebY0H7ucLsRgKSArwP+GgLpUwDFrK/trcRT36aDj7e8/JtX3cYIY8Znorawk+/8s TP40y2qgjMJzSJErj2nk8eTPBJyRWvFLQAx2VPfujVlT8xlhKTLmuKERYXkAnG8kabIC fl6GJq/QM/b3GX43OBeCLHofCQg64WVVCKW/68zZwcoOtm0pkxg4WYcha4jEh27KRrsX tgVfY1gWox1GSu/uX4SK/VsoH0gErgP/l88LR5S8V5n/UXrfauazBd2+P+J4UCj1eSmV negQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:references:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=NgWlAzEfK5CuTn1mie/HvMbfDyWDaa5ZyN2yjiSLKhE=; b=kfENRp4J54X7Emsvzz48D+6FIJl7DPuLKjHWfQQ+V6Hsna+cZaKXGSto6qmNjhA8ya /nWR4XbVnPTZZ2cVlI/awHi5wybRcsEeWsmdYLUaIN5ZDI8YJsqTIKcdL6Zexel6W/ZL XE7bBEP1AXfqH4pUd0FcXbQ0ef0ApJ4KPneDUXNolKWk1JHZ9rsbg//TnROvJSBfpCaD C7Sp++3weEasf/Sp2UHjsEFxgPSc+WZMj4xS9V/fhq6cS0IcBAcVhbj5/FlGEZP67Spw Vr5SYq+5Dp9fGWdaCW8nAWhuyl+1wFjgVKoEykaY8O/PmIZLk/7yL8cLf12Pt1xtyz4w JfIA== X-Gm-Message-State: AE9vXwMbiLVOeYDABfpjNrk/sdYAgbHF3MhtT6cbF0VRTxRf5MbOVN7fUJes44L/kkS0Hw== X-Received: by 10.129.78.15 with SMTP id c15mr17110319ywb.163.1473704732759; Mon, 12 Sep 2016 11:25:32 -0700 (PDT) Received: from evgadesktop (108-232-152-155.lightspeed.tukrga.sbcglobal.net. [108.232.152.155]) by smtp.gmail.com with ESMTPSA id f203sm7466562ywb.52.2016.09.12.11.25.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Sep 2016 11:25:31 -0700 (PDT) References: <1472725227-10374-1-git-send-email-pbonzini@redhat.com> <1472725227-10374-11-git-send-email-pbonzini@redhat.com> From: Pranith Kumar To: Paolo Bonzini In-reply-to: <1472725227-10374-11-git-send-email-pbonzini@redhat.com> Date: Mon, 12 Sep 2016 14:25:31 -0400 Message-ID: <87poo96kmc.fsf@gmail.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.85.161.193 Subject: Re: [Qemu-devel] [PATCH 10/12] cpus-common: Introduce async_safe_run_on_cpu() 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: Sergey Fedorov , "Emilio G. Cota" , Alex =?utf-8?Q?Benn=C3=A9e?= , qemu-devel@nongnu.org, Sergey Fedorov Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Paolo Bonzini writes: > Signed-off-by: Paolo Bonzini > --- > cpus-common.c | 25 +++++++++++++++++++++++++ > include/qom/cpu.h | 12 ++++++++++++ > 2 files changed, 37 insertions(+) > > diff --git a/cpus-common.c b/cpus-common.c > index 59c8dc8..88cf5ec 100644 > --- a/cpus-common.c > +++ b/cpus-common.c > @@ -144,6 +144,11 @@ void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data) > queue_work_on_cpu(cpu, wi); > } > > +typedef struct SafeWorkItem { > + run_on_cpu_func func; > + void *data; > +} SafeWorkItem; > + > /* Wait for pending exclusive operations to complete. The exclusive lock > must be held. */ > static inline void exclusive_idle(void) > @@ -208,6 +213,26 @@ void cpu_exec_end(CPUState *cpu) > qemu_mutex_unlock(&qemu_cpu_list_mutex); > } > > +static void async_safe_run_on_cpu_fn(CPUState *cpu, void *data) > +{ > + SafeWorkItem *w = data; > + > + start_exclusive(); > + w->func(cpu, w->data); > + end_exclusive(); > + g_free(w); > +} > + > +void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data) > +{ > + SafeWorkItem *w = g_new(SafeWorkItem, 1); > + > + w->func = func; > + w->data = data; > + > + async_run_on_cpu(cpu, async_safe_run_on_cpu_fn, w); > +} > + > void process_queued_cpu_work(CPUState *cpu) > { > struct qemu_work_item *wi; > diff --git a/include/qom/cpu.h b/include/qom/cpu.h > index 0e04e8f..54a875e 100644 > --- a/include/qom/cpu.h > +++ b/include/qom/cpu.h > @@ -663,6 +663,18 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data); > void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data); > > /** > + * async_safe_run_on_cpu: > + * @cpu: The vCPU to run on. > + * @func: The function to be executed. > + * @data: Data to pass to the function. > + * > + * Schedules the function @func for execution on the vCPU @cpu asynchronously, > + * while all other vCPUs are sleeping. @func is called with the CPU list lock > + * taken (and for system emulation the BQL); any other lock can be taken safely. > + */ > +void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data); > + > +/** > * qemu_get_cpu: > * @index: The CPUState@cpu_index value of the CPU to obtain. > * I used the following diff on top(as suggested by bonzini) to fix a deadlock I was seeing while testing. Fix deadlock caused by holding BQL for safe work Suggested-by: Paolo Bonzini Signed-off-by: Pranith Kumar --- cpus-common.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpus-common.c b/cpus-common.c index 443617a..579917f 100644 --- a/cpus-common.c +++ b/cpus-common.c @@ -18,6 +18,7 @@ */ #include "qemu/osdep.h" +#include "qemu/main-loop.h" #include "cpu.h" #include "sysemu/cpus.h" #include "exec/memory-internal.h" @@ -269,9 +270,11 @@ static void async_safe_run_on_cpu_fn(CPUState *cpu, void *data) { SafeWorkItem *w = data; + qemu_mutex_unlock_iothread(); start_exclusive(); w->func(cpu, w->data); end_exclusive(); + qemu_mutex_lock_iothread(); g_free(w); }