Received: from eggs.gnu.org ([2001:4830:134:3::10]:49128) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bgvbx-000304-4e for qemu-devel@nongnu.org; Mon, 05 Sep 2016 11:15:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bgvbo-0003Ud-Uj for qemu-devel@nongnu.org; Mon, 05 Sep 2016 11:15:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53494) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bgvbo-0003UH-NX for qemu-devel@nongnu.org; Mon, 05 Sep 2016 11:14:52 -0400 References: <1472725227-10374-1-git-send-email-pbonzini@redhat.com> <1472725227-10374-11-git-send-email-pbonzini@redhat.com> <87pooiz8nh.fsf@linaro.org> From: Paolo Bonzini Message-ID: Date: Mon, 5 Sep 2016 17:14:48 +0200 MIME-Version: 1.0 In-Reply-To: <87pooiz8nh.fsf@linaro.org> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 10/12] cpus-common: Introduce async_safe_run_on_cpu() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?UTF-8?Q?Alex_Benn=c3=a9e?= Cc: qemu-devel@nongnu.org, Sergey Fedorov , Sergey Fedorov , "Emilio G. Cota" On 05/09/2016 17:08, Alex Benn=C3=A9e wrote: >=20 > Paolo Bonzini writes: >=20 >> 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_f= unc 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 =3D 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 =3D g_new(SafeWorkItem, 1); >=20 > OK so I appreciate this approach is a neat way to embed safe work in th= e > existing queue but does it really offer that much more for yet another > dynamic allocation vs an extra flag to the WorkItem? Actually I had it that way in the "first version" (the one that I promised last Monday). The questions are: 1) is it so different to have 1 vs. 2 mini allocations 2) is this a fast path but it's no big deal, I can certainly change back. Paolo > In previous iterations I can DoS QEMU with a guest that does heavy > cross-CPU TLB flushing which led to a storm of mini allocations (for th= e > list and associated structures). This caused the massive memory usage a= s > the queue backed up. >=20 > I appreciate it was a fairly special test case and I introduced other > mitigations in the base patches cputlb code to get around it however it > was the driver for me experimenting with the pre-allocated array for > holding work items. >=20 >> + >> + w->func =3D func; >> + w->data =3D 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 fu= nc, 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 asynch= ronously, >> + * 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 ta= ken 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. >> * >=20 >=20 > -- > Alex Benn=C3=A9e >=20