Skip to content

Commit cdd5983

Browse files
committed
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
Pull virtio fixes from Michael S. Tsirkin: "Here are some virtio fixes for 3.4: a test build fix, a patch by Ren fixing naming for systems with a massive number of virtio blk devices, and balloon fixes for powerpc by David Gibson. There was some discussion about Ren's patch for virtio disc naming: some people wanted to move the legacy name mangling function to the block core. But there's no concensus on that yet, and we can always deduplicate later. Added comments in the hope that this will stop people from copying this legacy naming scheme into future drivers." * tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost: virtio_balloon: fix handling of PAGE_SIZE != 4k virtio_balloon: Fix endian bug virtio_blk: helper function to format disk names tools/virtio: fix up vhost/test module build
2 parents a6cb9ee + 3ccc937 commit cdd5983

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

drivers/block/virtio_blk.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,34 @@ static int init_vq(struct virtio_blk *vblk)
375375
return err;
376376
}
377377

378+
/*
379+
* Legacy naming scheme used for virtio devices. We are stuck with it for
380+
* virtio blk but don't ever use it for any new driver.
381+
*/
382+
static int virtblk_name_format(char *prefix, int index, char *buf, int buflen)
383+
{
384+
const int base = 'z' - 'a' + 1;
385+
char *begin = buf + strlen(prefix);
386+
char *end = buf + buflen;
387+
char *p;
388+
int unit;
389+
390+
p = end - 1;
391+
*p = '\0';
392+
unit = base;
393+
do {
394+
if (p == begin)
395+
return -EINVAL;
396+
*--p = 'a' + (index % unit);
397+
index = (index / unit) - 1;
398+
} while (index >= 0);
399+
400+
memmove(begin, p, end - p);
401+
memcpy(buf, prefix, strlen(prefix));
402+
403+
return 0;
404+
}
405+
378406
static int __devinit virtblk_probe(struct virtio_device *vdev)
379407
{
380408
struct virtio_blk *vblk;
@@ -443,18 +471,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
443471

444472
q->queuedata = vblk;
445473

446-
if (index < 26) {
447-
sprintf(vblk->disk->disk_name, "vd%c", 'a' + index % 26);
448-
} else if (index < (26 + 1) * 26) {
449-
sprintf(vblk->disk->disk_name, "vd%c%c",
450-
'a' + index / 26 - 1, 'a' + index % 26);
451-
} else {
452-
const unsigned int m1 = (index / 26 - 1) / 26 - 1;
453-
const unsigned int m2 = (index / 26 - 1) % 26;
454-
const unsigned int m3 = index % 26;
455-
sprintf(vblk->disk->disk_name, "vd%c%c%c",
456-
'a' + m1, 'a' + m2, 'a' + m3);
457-
}
474+
virtblk_name_format("vd", index, vblk->disk->disk_name, DISK_NAME_LEN);
458475

459476
vblk->disk->major = major;
460477
vblk->disk->first_minor = index_to_minor(index);

drivers/vhost/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static int vhost_test_release(struct inode *inode, struct file *f)
155155

156156
vhost_test_stop(n, &private);
157157
vhost_test_flush(n);
158-
vhost_dev_cleanup(&n->dev);
158+
vhost_dev_cleanup(&n->dev, false);
159159
/* We do an extra flush before freeing memory,
160160
* since jobs can re-queue themselves. */
161161
vhost_test_flush(n);

drivers/virtio/virtio_balloon.c

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
#include <linux/slab.h>
2929
#include <linux/module.h>
3030

31+
/*
32+
* Balloon device works in 4K page units. So each page is pointed to by
33+
* multiple balloon pages. All memory counters in this driver are in balloon
34+
* page units.
35+
*/
36+
#define VIRTIO_BALLOON_PAGES_PER_PAGE (PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT)
37+
3138
struct virtio_balloon
3239
{
3340
struct virtio_device *vdev;
@@ -42,8 +49,13 @@ struct virtio_balloon
4249
/* Waiting for host to ack the pages we released. */
4350
struct completion acked;
4451

45-
/* The pages we've told the Host we're not using. */
52+
/* Number of balloon pages we've told the Host we're not using. */
4653
unsigned int num_pages;
54+
/*
55+
* The pages we've told the Host we're not using.
56+
* Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE
57+
* to num_pages above.
58+
*/
4759
struct list_head pages;
4860

4961
/* The array of pfns we tell the Host about. */
@@ -66,7 +78,13 @@ static u32 page_to_balloon_pfn(struct page *page)
6678

6779
BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT);
6880
/* Convert pfn from Linux page size to balloon page size. */
69-
return pfn >> (PAGE_SHIFT - VIRTIO_BALLOON_PFN_SHIFT);
81+
return pfn * VIRTIO_BALLOON_PAGES_PER_PAGE;
82+
}
83+
84+
static struct page *balloon_pfn_to_page(u32 pfn)
85+
{
86+
BUG_ON(pfn % VIRTIO_BALLOON_PAGES_PER_PAGE);
87+
return pfn_to_page(pfn / VIRTIO_BALLOON_PAGES_PER_PAGE);
7088
}
7189

7290
static void balloon_ack(struct virtqueue *vq)
@@ -96,12 +114,23 @@ static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
96114
wait_for_completion(&vb->acked);
97115
}
98116

117+
static void set_page_pfns(u32 pfns[], struct page *page)
118+
{
119+
unsigned int i;
120+
121+
/* Set balloon pfns pointing at this page.
122+
* Note that the first pfn points at start of the page. */
123+
for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++)
124+
pfns[i] = page_to_balloon_pfn(page) + i;
125+
}
126+
99127
static void fill_balloon(struct virtio_balloon *vb, size_t num)
100128
{
101129
/* We can only do one array worth at a time. */
102130
num = min(num, ARRAY_SIZE(vb->pfns));
103131

104-
for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
132+
for (vb->num_pfns = 0; vb->num_pfns < num;
133+
vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
105134
struct page *page = alloc_page(GFP_HIGHUSER | __GFP_NORETRY |
106135
__GFP_NOMEMALLOC | __GFP_NOWARN);
107136
if (!page) {
@@ -113,9 +142,9 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
113142
msleep(200);
114143
break;
115144
}
116-
vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
145+
set_page_pfns(vb->pfns + vb->num_pfns, page);
146+
vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
117147
totalram_pages--;
118-
vb->num_pages++;
119148
list_add(&page->lru, &vb->pages);
120149
}
121150

@@ -130,8 +159,9 @@ static void release_pages_by_pfn(const u32 pfns[], unsigned int num)
130159
{
131160
unsigned int i;
132161

133-
for (i = 0; i < num; i++) {
134-
__free_page(pfn_to_page(pfns[i]));
162+
/* Find pfns pointing at start of each page, get pages and free them. */
163+
for (i = 0; i < num; i += VIRTIO_BALLOON_PAGES_PER_PAGE) {
164+
__free_page(balloon_pfn_to_page(pfns[i]));
135165
totalram_pages++;
136166
}
137167
}
@@ -143,11 +173,12 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num)
143173
/* We can only do one array worth at a time. */
144174
num = min(num, ARRAY_SIZE(vb->pfns));
145175

146-
for (vb->num_pfns = 0; vb->num_pfns < num; vb->num_pfns++) {
176+
for (vb->num_pfns = 0; vb->num_pfns < num;
177+
vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
147178
page = list_first_entry(&vb->pages, struct page, lru);
148179
list_del(&page->lru);
149-
vb->pfns[vb->num_pfns] = page_to_balloon_pfn(page);
150-
vb->num_pages--;
180+
set_page_pfns(vb->pfns + vb->num_pfns, page);
181+
vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
151182
}
152183

153184
/*
@@ -234,11 +265,14 @@ static void virtballoon_changed(struct virtio_device *vdev)
234265

235266
static inline s64 towards_target(struct virtio_balloon *vb)
236267
{
237-
u32 v;
268+
__le32 v;
269+
s64 target;
270+
238271
vb->vdev->config->get(vb->vdev,
239272
offsetof(struct virtio_balloon_config, num_pages),
240273
&v, sizeof(v));
241-
return (s64)v - vb->num_pages;
274+
target = le32_to_cpu(v);
275+
return target - vb->num_pages;
242276
}
243277

244278
static void update_balloon_size(struct virtio_balloon *vb)

0 commit comments

Comments
 (0)