From: Rusty Russell <rusty@rustcorp.com.au>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Tejun Heo <tj@kernel.org>, Jiri Kosina <jkosina@suse.cz>,
linux-kernel@vger.kernel.org, Petr Mladek <pmladek@suse.cz>,
virtualization@lists.linux-foundation.org
Subject: Re: [PATCH] virtio_balloon: Convert "vballon" kthread into a workqueue
Date: Wed, 15 Oct 2014 15:40:41 +1030 [thread overview]
Message-ID: <87lhohc3zi.fsf@rustcorp.com.au> (raw)
In-Reply-To: <1411662041-10986-1-git-send-email-pmladek@suse.cz>
Petr Mladek <pmladek@suse.cz> writes:
> Workqueues have clean and rich API for all basic operations. The code is usually
> easier and better readable. It can be easily tuned for the given purpose.
OK, sure.
> -static void fill_balloon(struct virtio_balloon *vb, size_t num)
> +static void fill_balloon(struct virtio_balloon *vb, size_t diff)
> {
> struct balloon_dev_info *vb_dev_info = vb->vb_dev_info;
> + size_t num;
> + bool done;
>
> /* We can only do one array worth at a time. */
> - num = min(num, ARRAY_SIZE(vb->pfns));
> + num = min(diff, ARRAY_SIZE(vb->pfns));
> + done = (num == diff) ? true : false;
>
> mutex_lock(&vb->balloon_lock);
> for (vb->num_pfns = 0; vb->num_pfns < num;
> @@ -143,6 +143,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
> VIRTIO_BALLOON_PAGES_PER_PAGE);
> /* Sleep for at least 1/5 of a second before retry. */
> msleep(200);
> + done = false;
> break;
> }
> set_page_pfns(vb->pfns + vb->num_pfns, page);
> @@ -154,6 +155,9 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
> if (vb->num_pfns != 0)
> tell_host(vb, vb->inflate_vq);
> mutex_unlock(&vb->balloon_lock);
> +
> + if (!done)
> + queue_work(vb->wq, &vb->wq_work);
> }
Hmm, this is a bit convoluted. I would spell it out by keeping a
num_done counter:
if (num_done < diff)
queue_work(vb->wq, &vb->wq_work);
> static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
> @@ -168,20 +172,25 @@ static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
> }
> }
>
> -static void leak_balloon(struct virtio_balloon *vb, size_t num)
> +static void leak_balloon(struct virtio_balloon *vb, size_t diff)
> {
> struct page *page;
> struct balloon_dev_info *vb_dev_info = vb->vb_dev_info;
> + size_t num;
> + bool done;
>
> /* We can only do one array worth at a time. */
> - num = min(num, ARRAY_SIZE(vb->pfns));
> + num = min(diff, ARRAY_SIZE(vb->pfns));
> + done = (num == diff) ? true : false;
>
> mutex_lock(&vb->balloon_lock);
> for (vb->num_pfns = 0; vb->num_pfns < num;
> vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
> page = balloon_page_dequeue(vb_dev_info);
> - if (!page)
> + if (!page) {
> + done = false;
> break;
> + }
> set_page_pfns(vb->pfns + vb->num_pfns, page);
> vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
> }
> @@ -195,6 +204,9 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)
> tell_host(vb, vb->deflate_vq);
> mutex_unlock(&vb->balloon_lock);
> release_pages_by_pfn(vb->pfns, vb->num_pfns);
> +
> + if (!done)
> + queue_work(vb->wq, &vb->wq_work);
> }
Here too.
> @@ -471,11 +469,13 @@ static int virtballoon_probe(struct virtio_device *vdev)
> if (err)
> goto out_free_vb_mapping;
>
> - vb->thread = kthread_run(balloon, vb, "vballoon");
> - if (IS_ERR(vb->thread)) {
> - err = PTR_ERR(vb->thread);
> + vb->wq = alloc_workqueue("vballoon_wq",
> + WQ_FREEZABLE | WQ_MEM_RECLAIM, 0);
> + if (!vb->wq) {
> + err = -ENOMEM;
> goto out_del_vqs;
> }
> + INIT_WORK(&vb->wq_work, balloon);
That looks racy to me; shouldn't we init vq-work before allocating wq?
Otherwise, looks good!
Thanks,
Rusty.
WARNING: multiple messages have this Message-ID (diff)
From: Rusty Russell <rusty@rustcorp.com.au>
To: Petr Mladek <pmladek@suse.cz>, "Michael S. Tsirkin" <mst@redhat.com>
Cc: Tejun Heo <tj@kernel.org>, Jiri Kosina <jkosina@suse.cz>,
virtualization@lists.linux-foundation.org,
linux-kernel@vger.kernel.org, Petr Mladek <pmladek@suse.cz>
Subject: Re: [PATCH] virtio_balloon: Convert "vballon" kthread into a workqueue
Date: Wed, 15 Oct 2014 15:40:41 +1030 [thread overview]
Message-ID: <87lhohc3zi.fsf@rustcorp.com.au> (raw)
In-Reply-To: <1411662041-10986-1-git-send-email-pmladek@suse.cz>
Petr Mladek <pmladek@suse.cz> writes:
> Workqueues have clean and rich API for all basic operations. The code is usually
> easier and better readable. It can be easily tuned for the given purpose.
OK, sure.
> -static void fill_balloon(struct virtio_balloon *vb, size_t num)
> +static void fill_balloon(struct virtio_balloon *vb, size_t diff)
> {
> struct balloon_dev_info *vb_dev_info = vb->vb_dev_info;
> + size_t num;
> + bool done;
>
> /* We can only do one array worth at a time. */
> - num = min(num, ARRAY_SIZE(vb->pfns));
> + num = min(diff, ARRAY_SIZE(vb->pfns));
> + done = (num == diff) ? true : false;
>
> mutex_lock(&vb->balloon_lock);
> for (vb->num_pfns = 0; vb->num_pfns < num;
> @@ -143,6 +143,7 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
> VIRTIO_BALLOON_PAGES_PER_PAGE);
> /* Sleep for at least 1/5 of a second before retry. */
> msleep(200);
> + done = false;
> break;
> }
> set_page_pfns(vb->pfns + vb->num_pfns, page);
> @@ -154,6 +155,9 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
> if (vb->num_pfns != 0)
> tell_host(vb, vb->inflate_vq);
> mutex_unlock(&vb->balloon_lock);
> +
> + if (!done)
> + queue_work(vb->wq, &vb->wq_work);
> }
Hmm, this is a bit convoluted. I would spell it out by keeping a
num_done counter:
if (num_done < diff)
queue_work(vb->wq, &vb->wq_work);
> static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
> @@ -168,20 +172,25 @@ static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
> }
> }
>
> -static void leak_balloon(struct virtio_balloon *vb, size_t num)
> +static void leak_balloon(struct virtio_balloon *vb, size_t diff)
> {
> struct page *page;
> struct balloon_dev_info *vb_dev_info = vb->vb_dev_info;
> + size_t num;
> + bool done;
>
> /* We can only do one array worth at a time. */
> - num = min(num, ARRAY_SIZE(vb->pfns));
> + num = min(diff, ARRAY_SIZE(vb->pfns));
> + done = (num == diff) ? true : false;
>
> mutex_lock(&vb->balloon_lock);
> for (vb->num_pfns = 0; vb->num_pfns < num;
> vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
> page = balloon_page_dequeue(vb_dev_info);
> - if (!page)
> + if (!page) {
> + done = false;
> break;
> + }
> set_page_pfns(vb->pfns + vb->num_pfns, page);
> vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
> }
> @@ -195,6 +204,9 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)
> tell_host(vb, vb->deflate_vq);
> mutex_unlock(&vb->balloon_lock);
> release_pages_by_pfn(vb->pfns, vb->num_pfns);
> +
> + if (!done)
> + queue_work(vb->wq, &vb->wq_work);
> }
Here too.
> @@ -471,11 +469,13 @@ static int virtballoon_probe(struct virtio_device *vdev)
> if (err)
> goto out_free_vb_mapping;
>
> - vb->thread = kthread_run(balloon, vb, "vballoon");
> - if (IS_ERR(vb->thread)) {
> - err = PTR_ERR(vb->thread);
> + vb->wq = alloc_workqueue("vballoon_wq",
> + WQ_FREEZABLE | WQ_MEM_RECLAIM, 0);
> + if (!vb->wq) {
> + err = -ENOMEM;
> goto out_del_vqs;
> }
> + INIT_WORK(&vb->wq_work, balloon);
That looks racy to me; shouldn't we init vq-work before allocating wq?
Otherwise, looks good!
Thanks,
Rusty.
next prev parent reply other threads:[~2014-10-15 5:10 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-25 16:20 [PATCH] virtio_balloon: Convert "vballon" kthread into a workqueue Petr Mladek
2014-09-28 12:43 ` Tejun Heo
2014-09-28 12:43 ` Tejun Heo
2014-10-15 5:10 ` Rusty Russell [this message]
2014-10-15 5:10 ` Rusty Russell
-- strict thread matches above, loose matches on Subject: below --
2014-09-25 16:20 Petr Mladek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87lhohc3zi.fsf@rustcorp.com.au \
--to=rusty@rustcorp.com.au \
--cc=jkosina@suse.cz \
--cc=linux-kernel@vger.kernel.org \
--cc=mst@redhat.com \
--cc=pmladek@suse.cz \
--cc=tj@kernel.org \
--cc=virtualization@lists.linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.