From cc4b145dbf7cd58bf5100cc18f6362cb3d61a2d7 Mon Sep 17 00:00:00 2001
From: Roger Pau Monne <roger.pau@citrix.com>
Date: Wed, 19 Mar 2014 11:02:10 +0100
Subject: [PATCH 23/31] xen: allow users to request ballooned out pages.

Add two new public functions to the balloon driver that are used to
request and return ballooned out pages from the balloon driver. This
pages will be used by the privcmd driver to map memory from foreign
domains.

Approved by: xxx
Sponsored by: Citrix Systems R&D

dev/xen/balloon/balloon.c:
 - Add two new functions to request and return ballooned out pages.

dev/xen/balloon/balloon.h:
 - Add public prototypes for the newly added functions.
---
 sys/dev/xen/balloon/balloon.c |   65 +++++++++++++++++++++++++++++++++++++++++
 sys/dev/xen/balloon/balloon.h |   35 ++++++++++++++++++++++
 2 files changed, 100 insertions(+), 0 deletions(-)
 create mode 100644 sys/dev/xen/balloon/balloon.h

diff --git a/sys/dev/xen/balloon/balloon.c b/sys/dev/xen/balloon/balloon.c
index 934ff59..16d366a 100644
--- a/sys/dev/xen/balloon/balloon.c
+++ b/sys/dev/xen/balloon/balloon.c
@@ -51,6 +51,8 @@ __FBSDID("$FreeBSD$");
 
 #include <machine/xen/xenvar.h>
 
+#include <dev/xen/balloon/balloon.h>
+
 static MALLOC_DEFINE(M_BALLOON, "Balloon", "Xen Balloon Driver");
 
 /* Convert from KB (as fetched from xenstore) to number of PAGES */
@@ -500,3 +502,66 @@ devclass_t xenballoon_devclass;
 
 DRIVER_MODULE(xenballoon, xenstore, xenballoon_driver, xenballoon_devclass,
     NULL, NULL);
+
+/*--------------------------- Public interface -------------------------------*/
+
+/**
+ * alloc_xenballooned_pages - get pages that have been ballooned out
+ * @nr_pages: Number of pages to get
+ * @pages: pages returned
+ * @return 0 on success, error otherwise
+ */
+int
+alloc_xenballooned_pages(int nr_pages, vm_page_t *pages)
+{
+	int pgno = 0;
+	vm_page_t page;
+
+	mtx_lock(&balloon_mutex);
+	while (pgno < nr_pages) {
+		page = balloon_retrieve();
+		if (page != NULL) {
+			pages[pgno++] = page;
+		} else {
+			int ret;
+
+			ret = decrease_reservation(nr_pages - pgno);
+			if (ret != 0)
+				goto out_undo;
+		}
+	}
+	mtx_unlock(&balloon_mutex);
+
+	return (0);
+
+ out_undo:
+	while (pgno > 0)
+		balloon_append(pages[--pgno]);
+	/* Free the memory back to the kernel soon */
+	wakeup(balloon_process);
+	mtx_unlock(&balloon_mutex);
+	return (ENOMEM);
+}
+
+/**
+ * free_xenballooned_pages - return pages retrieved with get_ballooned_pages
+ * @nr_pages: Number of pages
+ * @pages: pages to return
+ */
+void
+free_xenballooned_pages(int nr_pages, vm_page_t *pages)
+{
+	int i;
+
+	mtx_lock(&balloon_mutex);
+
+	for (i = 0; i < nr_pages; i++) {
+		if (pages[i])
+			balloon_append(pages[i]);
+	}
+
+	/* The balloon may be too large now. Shrink it if needed. */
+	wakeup(balloon_process);
+
+	mtx_unlock(&balloon_mutex);
+}
diff --git a/sys/dev/xen/balloon/balloon.h b/sys/dev/xen/balloon/balloon.h
new file mode 100644
index 0000000..98ebc10
--- /dev/null
+++ b/sys/dev/xen/balloon/balloon.h
@@ -0,0 +1,35 @@
+/*-
+ * Copyright (c) 2014 Roger Pau Monné <roger.pau@citrix.com>
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef __XEN_BALLOON_H__
+#define __XEN_BALLOON_H__
+
+int alloc_xenballooned_pages(int, vm_page_t *);
+void free_xenballooned_pages(int, vm_page_t *);
+
+#endif /* !__XEN_BALLOON_H__ */
\ No newline at end of file
-- 
1.7.7.5 (Apple Git-26)

