Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7977C433DF for ; Wed, 5 Aug 2020 13:31:19 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 766962245C for ; Wed, 5 Aug 2020 13:31:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=xen.org header.i=@xen.org header.b="AYF33p9g" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 766962245C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k3JVK-0006us-Hw; Wed, 05 Aug 2020 13:30:50 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k3JVJ-0006uh-3d for xen-devel@lists.xenproject.org; Wed, 05 Aug 2020 13:30:49 +0000 X-Inumbo-ID: 1fdd7be4-0537-4558-8ba0-31fef9e1cde0 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 1fdd7be4-0537-4558-8ba0-31fef9e1cde0; Wed, 05 Aug 2020 13:30:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type:In-Reply-To: MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=NhU77mw8lxmVWmWAz3GWPDyUog0FnaFTIwxGxqeKRsw=; b=AYF33p9gSoEp7Ud7goSZByRiWG 8aYQbLkjhxYn94eiyvwEYATNvjE+9kKZkGitcU4F3aSkvSxFeHHHgRuM2OX/6Dh6zNA8pqQTP0ORR J9G2yLAX9pohSOqwjbEo3IpY4p3ZiV++KurJITy9dpjF/BHv9AyAW/9mm+ZZCCM204uQ=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1k3JV6-0005cr-S5; Wed, 05 Aug 2020 13:30:36 +0000 Received: from 54-240-197-228.amazon.com ([54.240.197.228] helo=a483e7b01a66.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k3JV6-0000U7-HJ; Wed, 05 Aug 2020 13:30:36 +0000 Subject: Re: [RFC PATCH V1 01/12] hvm/ioreq: Make x86's IOREQ feature common To: Oleksandr Tyshchenko , xen-devel@lists.xenproject.org References: <1596478888-23030-1-git-send-email-olekstysh@gmail.com> <1596478888-23030-2-git-send-email-olekstysh@gmail.com> From: Julien Grall Message-ID: <6bfc3920-8f29-188c-cff4-2b99dabe166f@xen.org> Date: Wed, 5 Aug 2020 14:30:33 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 MIME-Version: 1.0 In-Reply-To: <1596478888-23030-2-git-send-email-olekstysh@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Kevin Tian , Stefano Stabellini , Jan Beulich , Wei Liu , Paul Durrant , Andrew Cooper , Ian Jackson , George Dunlap , Tim Deegan , Oleksandr Tyshchenko , Julien Grall , Jun Nakajima , =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Hi, On 03/08/2020 19:21, Oleksandr Tyshchenko wrote: > From: Oleksandr Tyshchenko > > As a lot of x86 code can be re-used on Arm later on, this patch > splits IOREQ support into common and arch specific parts. > > This support is going to be used on Arm to be able run device > emulator outside of Xen hypervisor. > > Please note, this is a split/cleanup of Julien's PoC: > "Add support for Guest IO forwarding to a device emulator" > > Signed-off-by: Julien Grall > Signed-off-by: Oleksandr Tyshchenko > --- > xen/arch/x86/Kconfig | 1 + > xen/arch/x86/hvm/dm.c | 2 +- > xen/arch/x86/hvm/emulate.c | 2 +- > xen/arch/x86/hvm/hvm.c | 2 +- > xen/arch/x86/hvm/io.c | 2 +- > xen/arch/x86/hvm/ioreq.c | 1431 +-------------------------------------- > xen/arch/x86/hvm/stdvga.c | 2 +- > xen/arch/x86/hvm/vmx/realmode.c | 1 + > xen/arch/x86/hvm/vmx/vvmx.c | 2 +- > xen/arch/x86/mm.c | 2 +- > xen/arch/x86/mm/shadow/common.c | 2 +- > xen/common/Kconfig | 3 + > xen/common/Makefile | 1 + > xen/common/hvm/Makefile | 1 + > xen/common/hvm/ioreq.c | 1430 ++++++++++++++++++++++++++++++++++++++ > xen/include/asm-x86/hvm/ioreq.h | 45 +- > xen/include/asm-x86/hvm/vcpu.h | 7 - > xen/include/xen/hvm/ioreq.h | 89 +++ > 18 files changed, 1575 insertions(+), 1450 deletions(-) That's quite a lot of code moved in a single patch. How can we check the code moved is still correct? Is it a verbatim copy? > create mode 100644 xen/common/hvm/Makefile > create mode 100644 xen/common/hvm/ioreq.c > create mode 100644 xen/include/xen/hvm/ioreq.h [...] > +static bool hvm_wait_for_io(struct hvm_ioreq_vcpu *sv, ioreq_t *p) > +{ > + unsigned int prev_state = STATE_IOREQ_NONE; > + > + while ( sv->pending ) > + { > + unsigned int state = p->state; > + > + smp_rmb(); > + > + recheck: > + if ( unlikely(state == STATE_IOREQ_NONE) ) > + { > + /* > + * The only reason we should see this case is when an > + * emulator is dying and it races with an I/O being > + * requested. > + */ > + hvm_io_assist(sv, ~0ul); > + break; > + } > + > + if ( unlikely(state < prev_state) ) > + { > + gdprintk(XENLOG_ERR, "Weird HVM ioreq state transition %u -> %u\n", > + prev_state, state); > + sv->pending = false; > + domain_crash(sv->vcpu->domain); > + return false; /* bail */ > + } > + > + switch ( prev_state = state ) > + { > + case STATE_IORESP_READY: /* IORESP_READY -> NONE */ > + p->state = STATE_IOREQ_NONE; > + hvm_io_assist(sv, p->data); > + break; > + case STATE_IOREQ_READY: /* IOREQ_{READY,INPROCESS} -> IORESP_READY */ > + case STATE_IOREQ_INPROCESS: > + wait_on_xen_event_channel(sv->ioreq_evtchn, > + ({ state = p->state; > + smp_rmb(); > + state != prev_state; })); > + goto recheck; I recall some discussion on security@ about this specific code. An IOREQ server can be destroyed at any time. When destroying IOREQ server, the all the vCPUs will be paused to avoid race. On x86, this was considered to be safe because wait_on_xen_event_channel() will never return if the vCPU is re-scheduled. However, on Arm, this function will return even after rescheduling. In this case, sv and p may point to invalid memory. IIRC, the suggestion was to harden hvm_wait_for_io(). I guess we could fetch the sv and p after wait_on_xen_event_channel. Any opinions? Cheers, -- Julien Grall