diff options
author | Michael S. Tsirkin <mst@redhat.com> | 2015-04-14 20:47:43 -0400 |
---|---|---|
committer | Rusty Russell <rusty@rustcorp.com.au> | 2015-04-14 23:11:09 -0400 |
commit | df81b29c7b81b9d70ee29b7a263dd5009daa0ce4 (patch) | |
tree | a73a9ac6aa72aa32bd7e25a25148044025caa31f | |
parent | 9e1a27ea42691429e31f158cce6fc61bc79bb2e9 (diff) |
virtio_balloon: transitional interface
Virtio 1.0 doesn't include a modern balloon device.
But it's not a big change to support a transitional
balloon device: this has the advantage of supporting
existing drivers, transparently.
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Cornelia Huck <cornelia.huck@de.ibm.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
-rw-r--r-- | drivers/virtio/virtio_balloon.c | 21 | ||||
-rw-r--r-- | include/uapi/linux/virtio_balloon.h | 32 |
2 files changed, 43 insertions, 10 deletions
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index 6a356e344f82..82e80e034f25 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c | |||
@@ -214,8 +214,8 @@ static inline void update_stat(struct virtio_balloon *vb, int idx, | |||
214 | u16 tag, u64 val) | 214 | u16 tag, u64 val) |
215 | { | 215 | { |
216 | BUG_ON(idx >= VIRTIO_BALLOON_S_NR); | 216 | BUG_ON(idx >= VIRTIO_BALLOON_S_NR); |
217 | vb->stats[idx].tag = tag; | 217 | vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag); |
218 | vb->stats[idx].val = val; | 218 | vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val); |
219 | } | 219 | } |
220 | 220 | ||
221 | #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT) | 221 | #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT) |
@@ -283,18 +283,27 @@ static void virtballoon_changed(struct virtio_device *vdev) | |||
283 | 283 | ||
284 | static inline s64 towards_target(struct virtio_balloon *vb) | 284 | static inline s64 towards_target(struct virtio_balloon *vb) |
285 | { | 285 | { |
286 | __le32 v; | ||
287 | s64 target; | 286 | s64 target; |
287 | u32 num_pages; | ||
288 | 288 | ||
289 | virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, &v); | 289 | virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, |
290 | &num_pages); | ||
290 | 291 | ||
291 | target = le32_to_cpu(v); | 292 | /* Legacy balloon config space is LE, unlike all other devices. */ |
293 | if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1)) | ||
294 | num_pages = le32_to_cpu((__force __le32)num_pages); | ||
295 | |||
296 | target = num_pages; | ||
292 | return target - vb->num_pages; | 297 | return target - vb->num_pages; |
293 | } | 298 | } |
294 | 299 | ||
295 | static void update_balloon_size(struct virtio_balloon *vb) | 300 | static void update_balloon_size(struct virtio_balloon *vb) |
296 | { | 301 | { |
297 | __le32 actual = cpu_to_le32(vb->num_pages); | 302 | u32 actual = vb->num_pages; |
303 | |||
304 | /* Legacy balloon config space is LE, unlike all other devices. */ | ||
305 | if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1)) | ||
306 | actual = (__force u32)cpu_to_le32(actual); | ||
298 | 307 | ||
299 | virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual, | 308 | virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual, |
300 | &actual); | 309 | &actual); |
diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h index 4b0488f20b2e..984169a819ee 100644 --- a/include/uapi/linux/virtio_balloon.h +++ b/include/uapi/linux/virtio_balloon.h | |||
@@ -25,6 +25,7 @@ | |||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 | * SUCH DAMAGE. */ | 27 | * SUCH DAMAGE. */ |
28 | #include <linux/types.h> | ||
28 | #include <linux/virtio_ids.h> | 29 | #include <linux/virtio_ids.h> |
29 | #include <linux/virtio_config.h> | 30 | #include <linux/virtio_config.h> |
30 | 31 | ||
@@ -38,9 +39,9 @@ | |||
38 | 39 | ||
39 | struct virtio_balloon_config { | 40 | struct virtio_balloon_config { |
40 | /* Number of pages host wants Guest to give up. */ | 41 | /* Number of pages host wants Guest to give up. */ |
41 | __le32 num_pages; | 42 | __u32 num_pages; |
42 | /* Number of pages we've actually got in balloon. */ | 43 | /* Number of pages we've actually got in balloon. */ |
43 | __le32 actual; | 44 | __u32 actual; |
44 | }; | 45 | }; |
45 | 46 | ||
46 | #define VIRTIO_BALLOON_S_SWAP_IN 0 /* Amount of memory swapped in */ | 47 | #define VIRTIO_BALLOON_S_SWAP_IN 0 /* Amount of memory swapped in */ |
@@ -51,9 +52,32 @@ struct virtio_balloon_config { | |||
51 | #define VIRTIO_BALLOON_S_MEMTOT 5 /* Total amount of memory */ | 52 | #define VIRTIO_BALLOON_S_MEMTOT 5 /* Total amount of memory */ |
52 | #define VIRTIO_BALLOON_S_NR 6 | 53 | #define VIRTIO_BALLOON_S_NR 6 |
53 | 54 | ||
55 | /* | ||
56 | * Memory statistics structure. | ||
57 | * Driver fills an array of these structures and passes to device. | ||
58 | * | ||
59 | * NOTE: fields are laid out in a way that would make compiler add padding | ||
60 | * between and after fields, so we have to use compiler-specific attributes to | ||
61 | * pack it, to disable this padding. This also often causes compiler to | ||
62 | * generate suboptimal code. | ||
63 | * | ||
64 | * We maintain this statistics structure format for backwards compatibility, | ||
65 | * but don't follow this example. | ||
66 | * | ||
67 | * If implementing a similar structure, do something like the below instead: | ||
68 | * struct virtio_balloon_stat { | ||
69 | * __virtio16 tag; | ||
70 | * __u8 reserved[6]; | ||
71 | * __virtio64 val; | ||
72 | * }; | ||
73 | * | ||
74 | * In other words, add explicit reserved fields to align field and | ||
75 | * structure boundaries at field size, avoiding compiler padding | ||
76 | * without the packed attribute. | ||
77 | */ | ||
54 | struct virtio_balloon_stat { | 78 | struct virtio_balloon_stat { |
55 | __u16 tag; | 79 | __virtio16 tag; |
56 | __u64 val; | 80 | __virtio64 val; |
57 | } __attribute__((packed)); | 81 | } __attribute__((packed)); |
58 | 82 | ||
59 | #endif /* _LINUX_VIRTIO_BALLOON_H */ | 83 | #endif /* _LINUX_VIRTIO_BALLOON_H */ |