Received: by 10.223.176.46 with SMTP id f43csp946196wra; Fri, 19 Jan 2018 04:40:35 -0800 (PST) X-Google-Smtp-Source: ACJfBouSrOPOgbFNE2RJdcL72BkKzdAGlNotv6IQ0y2mu0uFGMGjIQlovlc6d5eYrMYNfCT8NSAZ X-Received: by 10.98.147.154 with SMTP id r26mr18482504pfk.207.1516365635436; Fri, 19 Jan 2018 04:40:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516365635; cv=none; d=google.com; s=arc-20160816; b=mCOQF7LNiYyS4z615bOwpbF9c3mU2jMQGGYNX6SM/xWEQCT0Wjkp4spq8Q4Xwl62oV a53qDU4rs91YyCytAV+BPIPpYiDiPMZRxUGdy2OCI4C0Bzsz/QM6uSPN9uEEMRDcgdC2 a3UqhDggx6OKeEkM7F/bmICDDI4II7tpj46zyEyRwdX1j1E6zeuRB0LF760obyyTE6l1 pGjluFCP1lnApNwy6SZ4IimFObOGtwwgA3St+I6SK46gO+oI9ntWtNUd+Ydkacb1hg8Q 42PK6evEfOqbPugbGQmtbamnOXfMVISQIP6KeZkRbeUfMPXeJYfjOvKeBP9ANWEUwRdy IkDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :arc-authentication-results; bh=yHi7Qt3anLm9dbcmiR025ptY1jbKGStWTWPfx5V4Bd0=; b=DeKbkYLex8zhisv+YFFgTOWZF0Sx6NZo9RM5P4WmopxLPLp3ZNHxhdQH506bOM3aoG m9NstPBMJTSWJZbpjtN6I6eLNKNyUlNnLcC8BDpiDt1uNcIz0oA4t/y7mFDzRF6Lwax1 rziz+6D7V0P1ci6iI/H2M9iK39W4b2RtuL66fx7PpFaWX0wBSzKCYC98PFNy28FJyYL+ OnbtJT2iECvDcrjtnxl80aHispLoXy2/TWABh38Lf664P0fGC9K/QsHrTXT5pp4Sk2U1 wmor2AnUBKk0fCxyBId8X8XLe+IABQgOogk1OCQmOWNoK7UZm7HRfrhqsDo1VUgQCjm+ dGlw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y64si8063130pgy.247.2018.01.19.04.40.20; Fri, 19 Jan 2018 04:40:35 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755382AbeASMjn (ORCPT + 99 others); Fri, 19 Jan 2018 07:39:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:13811 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754699AbeASMjf (ORCPT ); Fri, 19 Jan 2018 07:39:35 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 651C77855A; Fri, 19 Jan 2018 12:39:35 +0000 (UTC) Received: from redhat.com (ovpn-127-36.rdu2.redhat.com [10.10.127.36]) by smtp.corp.redhat.com (Postfix) with SMTP id A183E614F2; Fri, 19 Jan 2018 12:39:27 +0000 (UTC) Date: Fri, 19 Jan 2018 14:39:27 +0200 From: "Michael S. Tsirkin" To: Wei Wang Cc: virtio-dev@lists.oasis-open.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, linux-mm@kvack.org, mhocko@kernel.org, akpm@linux-foundation.org, pbonzini@redhat.com, liliang.opensource@gmail.com, yang.zhang.wz@gmail.com, quan.xu0@gmail.com, nilal@redhat.com, riel@redhat.com Subject: Re: [PATCH v22 2/3] virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_VQ Message-ID: <20180119143517-mutt-send-email-mst@kernel.org> References: <1516165812-3995-1-git-send-email-wei.w.wang@intel.com> <1516165812-3995-3-git-send-email-wei.w.wang@intel.com> <20180117180337-mutt-send-email-mst@kernel.org> <5A616995.4050702@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5A616995.4050702@intel.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 19 Jan 2018 12:39:35 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 19, 2018 at 11:44:21AM +0800, Wei Wang wrote: > On 01/18/2018 12:44 AM, Michael S. Tsirkin wrote: > > On Wed, Jan 17, 2018 at 01:10:11PM +0800, Wei Wang wrote: > > > > > > > > +{ > > > + struct scatterlist sg; > > > + unsigned int unused; > > > + int err; > > > + > > > + sg_init_one(&sg, addr, sizeof(uint32_t)); > > This passes a guest-endian value to host. This is a problem: > > should always pass LE values. > > I think the endianness is handled when virtqueue_add_outbuf(): > > desc[i].addr = cpu_to_virtio64(_vq->vdev, addr); > > right? No - that handles the address, not the value you pass in. > > > > > + > > > + /* > > > + * This handles the cornercase that the vq happens to be full when > > > + * adding a cmd id. Rarely happen in practice. > > > + */ > > > + while (!vq->num_free) > > > + virtqueue_get_buf(vq, &unused); > > I dislike this busy-waiting. It's a hint after all - > > why not just retry later - hopefully after getting an > > interrupt? > > > > Alternatively, stop adding more entries when we have a single > > ring entry left, making sure we have space for the command. > > I think the second one looks good. Thanks. > > > > + queue_work(system_freezable_wq, > > > + &vb->update_balloon_size_work); > > > + spin_unlock_irqrestore(&vb->stop_update_lock, flags); > > > + } > > > + > > > + virtio_cread(vb->vdev, struct virtio_balloon_config, > > > + free_page_report_cmd_id, &cmd_id); > > You want virtio_cread_feature, don't access the new field > > if the feature has not been negotiated. > > Right. We probably need to put all the following cmd id related things under > the feature check, > > How about > > if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_VQ)) { > virtio_cread(..); > if (cmd_id == VIRTIO_BALLOON_FREE_PAGE_REPORT_STOP_ID) { > .... > } > that's ok too. > > > > > > > + if (cmd_id == VIRTIO_BALLOON_FREE_PAGE_REPORT_STOP_ID) { > > > + WRITE_ONCE(vb->report_free_page, false); > > > + } else if (cmd_id != vb->start_cmd_id) { > > > + /* > > > + * Host requests to start the reporting by sending a new cmd > > > + * id. > > > + */ > > > + WRITE_ONCE(vb->report_free_page, true); > > I don't know why we bother with WRITE_ONCE here. The point of > > report_free_page being used lockless is that that it's not a big deal if > > it's wrong occasionally, right? > > Actually the main reason is that "vb->report_free_page" is a value shared by > two threads: > Written by the config_change here, and read by the worker thread that > reports the free pages. Right but what's wrong if it's read or written twice and not once? > Alternatively, we could let the two sides access to the shared variable with > "volatile" pointers. > > > > > > > > > > > + vb->start_cmd_id = cmd_id; > > > + queue_work(vb->balloon_wq, &vb->report_free_page_work); > > It seems that if a command was already queued (with a different id), > > this will result in new command id being sent to host twice, which will > > likely confuse the host. > > I think that case won't happen, because > - the host sends a cmd id to the guest via the config, while the guest acks > back the received cmd id via the virtqueue; > - the guest ack back a cmd id only when a new cmd id is received from the > host, that is the above check: > > if (cmd_id != vb->start_cmd_id) { --> the driver only queues the > reporting work only when a new cmd id is received > /* > * Host requests to start the reporting by sending a > * new cmd id. > */ > WRITE_ONCE(vb->report_free_page, true); > vb->start_cmd_id = cmd_id; > queue_work(vb->balloon_wq, > &vb->report_free_page_work); > } > > So the same cmd id wouldn't queue the reporting work twice. > Like this: vb->start_cmd_id = cmd_id; queue_work(vb->balloon_wq, &vb->report_free_page_work); command id changes vb->start_cmd_id = cmd_id; work executes queue_work(vb->balloon_wq, &vb->report_free_page_work); work executes again > > > > > > > > > + } > > > +} > > > + > > > static void update_balloon_size(struct virtio_balloon *vb) > > > { > > > u32 actual = vb->num_pages; > > > @@ -417,40 +513,113 @@ static void update_balloon_size_func(struct work_struct *work) > > > static int init_vqs(struct virtio_balloon *vb) > > > { > > > - struct virtqueue *vqs[3]; > > > - vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request }; > > > - static const char * const names[] = { "inflate", "deflate", "stats" }; > > > - int err, nvqs; > > > + struct virtqueue **vqs; > > > + vq_callback_t **callbacks; > > > + const char **names; > > > + struct scatterlist sg; > > > + int i, nvqs, err = -ENOMEM; > > > + > > > + /* Inflateq and deflateq are used unconditionally */ > > > + nvqs = 2; > > > + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) > > > + nvqs++; > > > + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_VQ)) > > > + nvqs++; > > > + > > > + /* Allocate space for find_vqs parameters */ > > > + vqs = kcalloc(nvqs, sizeof(*vqs), GFP_KERNEL); > > > + if (!vqs) > > > + goto err_vq; > > > + callbacks = kmalloc_array(nvqs, sizeof(*callbacks), GFP_KERNEL); > > > + if (!callbacks) > > > + goto err_callback; > > > + names = kmalloc_array(nvqs, sizeof(*names), GFP_KERNEL); > > > + if (!names) > > > + goto err_names; > > Why not just keep these 3 arrays on stack? they aren't large. > > Sounds good. Here is the new implementation: > > static int init_vqs(struct virtio_balloon *vb) > { > struct virtqueue *vqs[4]; > vq_callback_t *callbacks[4]; > const char *names[4]; > struct scatterlist sg; > int ret; > > > /* > * Inflateq and deflateq are used unconditionally. stats_vq and > * free_page_vq uses names[2] and names[3], respectively. The > names[] > * will be NULL if the related feature is not enabled, which will > * cause no allocation for the corresponding virtqueue in find_vqs. > */ > callbacks[0] = balloon_ack; > names[0] = "inflate"; > callbacks[1] = balloon_ack; > names[1] = "deflate"; > names[2] = NULL; > names[3] = NULL; > > if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { > names[2] = "stats"; > callbacks[2] = stats_request; > } > if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_VQ)) { > names[3] = "free_page_vq"; > callbacks[3] = NULL; > } > > ret = vb->vdev->config->find_vqs(vb->vdev, 4, vqs, callbacks, names, > NULL, NULL); > if (ret) > return ret; > > vb->inflate_vq = vqs[0]; > vb->deflate_vq = vqs[1]; > > if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { > vb->stats_vq = vqs[2]; > /* > * Prime this virtqueue with one buffer so the hypervisor > can > * use it to signal us later (it can't be broken yet!). > */ > sg_init_one(&sg, vb->stats, sizeof(vb->stats)); > ret = virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb, > GFP_KERNEL); > if (ret) { > dev_warn(&vb->vdev->dev, "%s: add stat_vq failed\n", > __func__); > return ret; > } > virtqueue_kick(vb->stats_vq); > } > > if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_VQ)) > vb->free_page_vq = vqs[3]; > > return 0; > } > > > Btw, the QEMU side doesn't have an option to disable STATS_VQ currently, we > may need to add that later. > > Best, > Wei why not