From 34e41766d2e2716b0708694fe7877c54cdf09f74 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Tue, 20 Dec 2011 17:08:54 +0800 Subject: [PATCH] kvm tools: carefully send and handle balloon ipc Remove struct balloon_cmd and use kvm_ipc__send_msg(). Signed-off-by: Lai Jiangshan Signed-off-by: Pekka Enberg --- tools/kvm/builtin-balloon.c | 18 +++++------------- tools/kvm/virtio/balloon.c | 6 +++++- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/tools/kvm/builtin-balloon.c b/tools/kvm/builtin-balloon.c index 3f3056e55513..5bd2291648a8 100644 --- a/tools/kvm/builtin-balloon.c +++ b/tools/kvm/builtin-balloon.c @@ -13,12 +13,6 @@ static const char *instance_name; static u64 inflate; static u64 deflate; -struct balloon_cmd { - u32 type; - u32 len; - int amount; -}; - static const char * const balloon_usage[] = { "lkvm balloon [-n name] [-p pid] [-i amount] [-d amount]", NULL @@ -50,9 +44,9 @@ static void parse_balloon_options(int argc, const char **argv) int kvm_cmd_balloon(int argc, const char **argv, const char *prefix) { - struct balloon_cmd cmd; int instance; int r; + int amount; parse_balloon_options(argc, argv); @@ -67,17 +61,15 @@ int kvm_cmd_balloon(int argc, const char **argv, const char *prefix) if (instance <= 0) die("Failed locating instance"); - cmd.type = KVM_IPC_BALLOON; - cmd.len = sizeof(cmd.amount); - if (inflate) - cmd.amount = inflate; + amount = inflate; else if (deflate) - cmd.amount = -deflate; + amount = -deflate; else kvm_balloon_help(); - r = write(instance, &cmd, sizeof(cmd)); + r = kvm_ipc__send_msg(instance, KVM_IPC_BALLOON, + sizeof(amount), (u8 *)&amount); close(instance); diff --git a/tools/kvm/virtio/balloon.c b/tools/kvm/virtio/balloon.c index 556f0ec5d302..bd984b721dab 100644 --- a/tools/kvm/virtio/balloon.c +++ b/tools/kvm/virtio/balloon.c @@ -154,8 +154,12 @@ static void virtio_bln__print_stats(int fd, u32 type, u32 len, u8 *msg) static void handle_mem(int fd, u32 type, u32 len, u8 *msg) { - int mem = *(int *)msg; + int mem; + if (WARN_ON(type != KVM_IPC_BALLOON || len != sizeof(int))) + return; + + mem = *(int *)msg; if (mem > 0) { bdev.config.num_pages += 256 * mem; } else if (mem < 0) { -- 2.39.5