Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp4584251rwb; Tue, 8 Nov 2022 20:19:01 -0800 (PST) X-Google-Smtp-Source: AMsMyM6REw1FPDg6eG7PZBD/kPc9C6IYXaoClVDX72OfcrginTQTrqD80JQfZuoRe7IT1pKKyTCB X-Received: by 2002:a63:125f:0:b0:46e:f23a:e9bd with SMTP id 31-20020a63125f000000b0046ef23ae9bdmr49377830pgs.21.1667967541084; Tue, 08 Nov 2022 20:19:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667967541; cv=none; d=google.com; s=arc-20160816; b=mgx42pv64FaKLsJAkdpnxxYEK7KtHH16ZQLt34Jb3RBQdUxJvl3pysggXvncEasyI4 tlLNiruQg2WHdslFkxl0xjg687AOM5iLqqiQKQMgDZ2GQ58KdG4rYcHsjrJzOedVgG1z uTMv7twRID+bQkWZBhLWIfjx9rs+phADdzKDSq/zbTcjSYyBWivlTfuHgkJf1WFWk+tu Pd2gS4Qiv9vwvr6DrenEZx2jkDush2f/IxHV/dpLD0BmcqmFpMppj04gDDCH2Cj81fx0 47J/0xIgYLjn/gCgVdjTAgcdkJBMUdRdBoUhaz0YlbGVHquoB+O2+qsbu74Ym58gxE8G P/Dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=1SzpVeGFSvJuoKb2g3I84q9t3Q2PU0091JEzT9e1Qh0=; b=j9KR7PiJOUgD0QTE4vZIT2R37SIZJWZwnAXTwXHfepMSiSBCdjcu7sq6Kp3RBVpGzZ niX9rkZ+uWG6D2+PZneNwKE/3brN1sd0wwtU+bU5f7NF0bCBxEhRnj4ArHsn2m8kAxPq e5U+jp7OvLC1Y1zU0nbPRj5fxbdpYIxqNMcdraXbkOVx8FSKLkcyqyBrnr8fbCyiTT2E c4o1FzIVXMsX3q+ovSnV6B9OlvD6cFTsKZl4oPLCnQeUCvrL92KM/7+CWsYSllL8O26E Fym7aVsyDAqvrFGtRzQ11Wq/4l4h9n5cgVWhryU7HmPbR42f8jGQgAzpb8KdlgEdrzoM asUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="O/cJ9AJ8"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y1-20020a636401000000b00461f10556b9si16852709pgb.319.2022.11.08.20.18.48; Tue, 08 Nov 2022 20:19:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="O/cJ9AJ8"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229675AbiKIDpQ (ORCPT + 94 others); Tue, 8 Nov 2022 22:45:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229540AbiKIDpO (ORCPT ); Tue, 8 Nov 2022 22:45:14 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 515A35FD8 for ; Tue, 8 Nov 2022 19:44:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667965457; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=1SzpVeGFSvJuoKb2g3I84q9t3Q2PU0091JEzT9e1Qh0=; b=O/cJ9AJ8YRmr61PfwAIUGfqM8Jq2jek/yQ9L6jtceCFPeF+mVU0wLQHwN90N3Z2gZoybsw wCELnpJVgJZaLk84uwFuV1UqTJbIqrzuI/Jvt4Y3cPxCNi+0hkFghboU0Pgz6YbpxWmTOf d4kBk74KDvXzyqSlKLGGopjUQglRrWQ= Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-614-2IcuGbmSPFakY3919nGpgg-1; Tue, 08 Nov 2022 22:44:16 -0500 X-MC-Unique: 2IcuGbmSPFakY3919nGpgg-1 Received: by mail-oo1-f72.google.com with SMTP id b25-20020a4a9bd9000000b0047679132f18so4534265ook.21 for ; Tue, 08 Nov 2022 19:44:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1SzpVeGFSvJuoKb2g3I84q9t3Q2PU0091JEzT9e1Qh0=; b=absk0NG38zPNCt4Bona10H7eV4aAuvA7g33YdyJH0gLM1dd8EHJpj9Ct/hQYDyGPNr N2fQZM4Dgcdwwieg0vxkeAjYGNnvnc5+3p8Sz7wdW73BG2Q8Y/Y9ohIkpETKtcaTkmBt wOnLDFVXuBiUTWl3+GuiZMJ15PgU3/D+rozFJggL2e+cLAh/XW/m5Sl+MQadj4VV59JN EtereM1WaFNizTnjxfQvgx07buj53eGs/YKtpmebi0JRBdeewwdhnPrjQRLeu39N3y84 HIg7Tk1/hSCc9fP6owgm8mh5O6BMPsV9m1mL9W8tBFLqwCKLv6e3KfhvPU9QnLEw33v2 9yWQ== X-Gm-Message-State: ACrzQf1e14yl+GmkyJd17ulUCBMO2FCRO5asUWdUa0LrKKMXwWin2pYx Z1ap072ni2sKz93Cw4mXRD4bgvBNpFYFbyiKzpSOPGdmym2p2N73GZ/FqPyfvYDJSjsrLUgHTmF tdsBZ6UDXm/9vRATUzzJI+iODhTNfqkhC6VkFDlGE X-Received: by 2002:a05:6808:181e:b0:35a:5959:5909 with SMTP id bh30-20020a056808181e00b0035a59595909mr14803967oib.35.1667965455533; Tue, 08 Nov 2022 19:44:15 -0800 (PST) X-Received: by 2002:a05:6808:181e:b0:35a:5959:5909 with SMTP id bh30-20020a056808181e00b0035a59595909mr14803953oib.35.1667965455277; Tue, 08 Nov 2022 19:44:15 -0800 (PST) MIME-Version: 1.0 References: <20221107203431.368306-1-eric.auger@redhat.com> <20221107153924-mutt-send-email-mst@kernel.org> <20221107180022-mutt-send-email-mst@kernel.org> <20221108035142-mutt-send-email-mst@kernel.org> <20221108041820-mutt-send-email-mst@kernel.org> <7105abc8-85d1-63a4-7f77-a2b3e0177b6f@redhat.com> In-Reply-To: <7105abc8-85d1-63a4-7f77-a2b3e0177b6f@redhat.com> From: Jason Wang Date: Wed, 9 Nov 2022 11:44:03 +0800 Message-ID: Subject: Re: [RFC] vhost: Clear the pending messages on vhost_init_device_iotlb() To: eric.auger@redhat.com Cc: "Michael S. Tsirkin" , eric.auger.pro@gmail.com, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, peterx@redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 8, 2022 at 6:17 PM Eric Auger wrote: > > Hi Michael, Jason, > > On 11/8/22 10:31, Michael S. Tsirkin wrote: > > On Tue, Nov 08, 2022 at 05:13:50PM +0800, Jason Wang wrote: > >> On Tue, Nov 8, 2022 at 4:56 PM Michael S. Tsirkin wrote: > >>> On Tue, Nov 08, 2022 at 11:09:36AM +0800, Jason Wang wrote: > >>>> On Tue, Nov 8, 2022 at 7:06 AM Michael S. Tsirkin wrote: > >>>>> On Mon, Nov 07, 2022 at 10:10:06PM +0100, Eric Auger wrote: > >>>>>> Hi Michael, > >>>>>> On 11/7/22 21:42, Michael S. Tsirkin wrote: > >>>>>>> On Mon, Nov 07, 2022 at 09:34:31PM +0100, Eric Auger wrote: > >>>>>>>> When the vhost iotlb is used along with a guest virtual iommu > >>>>>>>> and the guest gets rebooted, some MISS messages may have been > >>>>>>>> recorded just before the reboot and spuriously executed by > >>>>>>>> the virtual iommu after the reboot. Despite the device iotlb gets > >>>>>>>> re-initialized, the messages are not cleared. Fix that by calling > >>>>>>>> vhost_clear_msg() at the end of vhost_init_device_iotlb(). > >>>>>>>> > >>>>>>>> Signed-off-by: Eric Auger > >>>>>>>> --- > >>>>>>>> drivers/vhost/vhost.c | 1 + > >>>>>>>> 1 file changed, 1 insertion(+) > >>>>>>>> > >>>>>>>> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c > >>>>>>>> index 40097826cff0..422a1fdee0ca 100644 > >>>>>>>> --- a/drivers/vhost/vhost.c > >>>>>>>> +++ b/drivers/vhost/vhost.c > >>>>>>>> @@ -1751,6 +1751,7 @@ int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled) > >>>>>>>> } > >>>>>>>> > >>>>>>>> vhost_iotlb_free(oiotlb); > >>>>>>>> + vhost_clear_msg(d); > >>>>>>>> > >>>>>>>> return 0; > >>>>>>>> } > >>>>>>> Hmm. Can't messages meanwhile get processes and affect the > >>>>>>> new iotlb? > >>>>>> Isn't the msg processing stopped at the moment this function is called > >>>>>> (VHOST_SET_FEATURES)? > >>>>>> > >>>>>> Thanks > >>>>>> > >>>>>> Eric > >>>>> It's pretty late here I'm not sure. You tell me what prevents it. > >>>> So the proposed code assumes that Qemu doesn't process device IOTLB > >>>> before VHOST_SET_FEAETURES. Consider there's no reset in the general > >>>> vhost uAPI, I wonder if it's better to move the clear to device code > >>>> like VHOST_NET_SET_BACKEND. So we can clear it per vq? > >>> Hmm this makes no sense to me. iommu sits between backend > >>> and frontend. Tying one to another is going to backfire. > >> I think we need to emulate what real devices are doing. Device should > >> clear the page fault message during reset, so the driver won't read > >> anything after reset. But we don't have a per device stop or reset > >> message for vhost-net. That's why the VHOST_NET_SET_BACKEND came into > >> my mind. > > That's not a reset message. Userspace can switch backends at will. > > I guess we could check when backend is set to -1. > > It's a hack but might work. > > > >>> I'm thinking more along the lines of doing everything > >>> under iotlb_lock. > >> I think the problem is we need to find a proper place to clear the > >> message. So I don't get how iotlb_lock can help: the message could be > >> still read from user space after the backend is set to NULL. > >> > >> Thanks > > Well I think the real problem is this. > > > > vhost_net_set_features does: > > > > if ((features & (1ULL << VIRTIO_F_ACCESS_PLATFORM))) { > > if (vhost_init_device_iotlb(&n->dev, true)) > > goto out_unlock; > > } > > > > > > so we get a new iotlb each time features are set. > > > > But features can be changes while device is running. > > E.g. > > VHOST_F_LOG_ALL > > > > > > Let's just say this hack of reusing feature bits for backend > > was not my brightest idea :( > > > > Isn't vhost_init_device_iotlb() racy then, as d->iotlb is first updated with niotlb and later d->vqs[i]->iotlb is updated with niotlb. What does garantee this is done atomically? > > Shouldn't we hold the dev->mutex to make all the sequence atomic and > include vhost_clear_msg()? Can't the vhost_clear_msg() take the dev lock? It depends on where we want to place the vhost_clear_msg(), e.g in most of the device ioctl, the dev->mutex has been held. Thanks > > Thanks > > Eric > > > > > > > > >>> > >>> > >>>>> BTW vhost_init_device_iotlb gets enabled parameter but ignores > >>>>> it, we really should drop that. > >>>> Yes. > >>>> > >>>>> Also, it looks like if features are set with VIRTIO_F_ACCESS_PLATFORM > >>>>> and then cleared, iotlb is not properly cleared - bug? > >>>> Not sure, old IOTLB may still work. But for safety, we need to disable > >>>> device IOTLB in this case. > >>>> > >>>> Thanks > >>>> > >>>>> > >>>>>>> > >>>>>>>> -- > >>>>>>>> 2.37.3 >