[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[UNIKRAFT/BALLOON v2 4/7] plat/kvm/balloon: KVM balloon driver calls implementation



From: Cezar Craciunoiu <cezar.craciunoiu@xxxxxxxxx>

The functions prepare the parameters for the calls to the inflate/deflate
functions.
The balloon_set function helps the functions in bbuddy jump over the
inflate calls, as these are not needed when allocating memory with KVM.

As you can see in the Config.uk file, the KVM balloon driver depends
on the mutex implementation. Locks were added with multi-threading in mind.
For a single thread, the balloon works without the locks (and the dependecy
on uklock is removed).

Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@xxxxxxxxx>
---
 plat/kvm/Config.uk   |  10 ++++
 plat/kvm/Makefile.uk |  11 +++++
 plat/kvm/balloon.c   | 107 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 plat/kvm/balloon.c

diff --git a/plat/kvm/Config.uk b/plat/kvm/Config.uk
index 3372b6c..929a172 100644
--- a/plat/kvm/Config.uk
+++ b/plat/kvm/Config.uk
@@ -84,6 +84,16 @@ config VIRTIO_BUS
                Virtio bus driver for probing and operating virtio device and
                transport layer.
 
+config BALLOON
+       bool "Balloon driver"
+       default y if LIBUKALLOCBBUDDY
+       default n
+       depends on VIRTIO_PCI
+       depends on LIBUKLOCK_MUTEX
+       help
+              Virtio Memory ballooning device driver for returning memory
+              to the host.
+
 menu "Virtio"
 config VIRTIO_PCI
        bool "Virtio PCI device support"
diff --git a/plat/kvm/Makefile.uk b/plat/kvm/Makefile.uk
index 94321e0..35216e4 100644
--- a/plat/kvm/Makefile.uk
+++ b/plat/kvm/Makefile.uk
@@ -14,6 +14,7 @@ $(eval $(call 
addplatlib_s,kvm,libkvmvirtioblk,$(CONFIG_VIRTIO_BLK)))
 $(eval $(call addplatlib_s,kvm,libkvmvirtio9p,$(CONFIG_VIRTIO_9P)))
 $(eval $(call addplatlib_s,kvm,libkvmofw,$(CONFIG_LIBOFW)))
 $(eval $(call addplatlib_s,kvm,libkvmgicv2,$(CONFIG_LIBGICV2)))
+$(eval $(call addplatlib_s,kvm,libkvmballoon,$(CONFIG_BALLOON)))
 
 ##
 ## Platform library definitions
@@ -182,3 +183,13 @@ LIBKVMGICV2_CINCLUDES-y         += 
-I$(UK_PLAT_COMMON_BASE)/include
 LIBKVMGICV2_CINCLUDES-y         += -I$(UK_PLAT_DRIVERS_BASE)/include
 
 LIBKVMGICV2_SRCS-y += $(UK_PLAT_DRIVERS_BASE)/gic/gic-v2.c
+
+##
+## BALLOON library definitions
+##
+LIBKVMBALLOON_CINCLUDES-y += -I$(LIBKVMPLAT_BASE)/include
+LIBKVMBALLOON_CINCLUDES-y += -I$(UK_PLAT_DRIVERS_BASE)/include
+LIBKVMBALLOON_CINCLUDES-y += -I$(UK_PLAT_COMMON_BASE)/include
+
+LIBKVMBALLOON_SRCS-y += $(UK_PLAT_DRIVERS_BASE)/balloon/balloon_drv.c
+LIBKVMBALLOON_SRCS-y += $(LIBKVMPLAT_BASE)/balloon.c
diff --git a/plat/kvm/balloon.c b/plat/kvm/balloon.c
new file mode 100644
index 0000000..d3c5fd2
--- /dev/null
+++ b/plat/kvm/balloon.c
@@ -0,0 +1,107 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Cason Schindler & Jack Raney <cason.j.schindler@xxxxxxxxx>
+ *          Cezar Craciunoiu <cezar.craciunoiu@xxxxxxxxx>
+ *
+ * Copyright (c) 2019, The University of Texas at Austin. All rights reserved.
+ *               2020, University Politehnica of Bucharest. All rights 
reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
+ */
+#include <inttypes.h>
+#include <errno.h>
+#include <stddef.h>
+
+#include <balloon/balloon.h>
+#include <uk/plat/balloon.h>
+#include <uk/asm/limits.h>
+
+static inline int get_num_pages(int order)
+{
+       int num_pages = 1;
+       int i;
+
+       for (i = 0; i < order; i++)
+               num_pages *= 2;
+       return num_pages;
+}
+
+/**
+ * Fills addresses for page range starting at first_page.
+ */
+static inline void fill_page_array(uintptr_t *pages_array,
+                       void *first_page, int num_pages)
+{
+       uint64_t current_pg = (uint64_t) first_page;
+       int i;
+
+       for (i = 0; i < num_pages; i++) {
+               pages_array[i] = current_pg;
+               current_pg += __PAGE_SIZE;
+       }
+}
+
+/**
+ * Calls driver inflate_balloon
+ * returns number of pages actually put into balloon or < 0 on error
+ */
+int ukplat_inflate(void *page, int order)
+{
+       int num_pages = get_num_pages(order);
+       uintptr_t pages_to_host[num_pages];
+
+       if (page == NULL)
+               return -EINVAL;
+
+       fill_page_array(pages_to_host, page, num_pages);
+       return inflate_balloon(pages_to_host, num_pages);
+}
+
+/**
+ * Call driver deflate_balloon
+ * returns number of pages actually taken from balloon or < 0 on error
+ */
+int ukplat_deflate(void *page, int order)
+{
+       int num_pages = get_num_pages(order);
+       uintptr_t pages_to_guest[num_pages];
+
+       if (page == NULL)
+               return -EINVAL;
+
+       fill_page_array(pages_to_guest, page, num_pages);
+       return deflate_balloon(pages_to_guest, num_pages);
+}
+
+/**
+ * Sets behaviour for KVM.
+ */
+void ukplat_balloon_set(char *balloon_type)
+{
+       *balloon_type = 2;
+}
-- 
2.20.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.