2010-12-29 17:20:19

by Daniel Kiper

[permalink] [raw]
Subject: [PATCH R2 5/7] xen/balloon: Protect before CPU exhaust by event/x process

Protect before CPU exhaust by event/x process during
errors by adding some delays in scheduling next event.

Signed-off-by: Daniel Kiper <[email protected]>
---
drivers/xen/balloon.c | 100 ++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 82 insertions(+), 18 deletions(-)

diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c
index 11143af..a62427d 100644
--- a/drivers/xen/balloon.c
+++ b/drivers/xen/balloon.c
@@ -65,6 +65,20 @@

#define BALLOON_CLASS_NAME "xen_memory"

+/*
+ * balloon_process() state:
+ *
+ * BP_ERROR: error, go to sleep,
+ * BP_DONE: done or nothing to do,
+ * BP_HUNGRY: hungry.
+ */
+
+enum bp_state {
+ BP_ERROR,
+ BP_DONE,
+ BP_HUNGRY
+};
+
struct balloon_stats {
/* We aim for 'current allocation' == 'target allocation'. */
unsigned long current_pages;
@@ -72,6 +86,8 @@ struct balloon_stats {
/* Number of pages in high- and low-memory balloons. */
unsigned long balloon_low;
unsigned long balloon_high;
+ unsigned long schedule_delay;
+ unsigned long max_schedule_delay;
};

static DEFINE_MUTEX(balloon_mutex);
@@ -166,6 +182,25 @@ static struct page *balloon_next_page(struct page *page)
return list_entry(next, struct page, lru);
}

+static void update_schedule_delay(enum bp_state state)
+{
+ unsigned long new_schedule_delay;
+
+ if (state != BP_ERROR) {
+ balloon_stats.schedule_delay = 1;
+ return;
+ }
+
+ new_schedule_delay = balloon_stats.schedule_delay << 1;
+
+ if (new_schedule_delay > balloon_stats.max_schedule_delay) {
+ balloon_stats.schedule_delay = balloon_stats.max_schedule_delay;
+ return;
+ }
+
+ balloon_stats.schedule_delay = new_schedule_delay;
+}
+
static unsigned long current_target(void)
{
unsigned long target = balloon_stats.target_pages;
@@ -178,11 +213,12 @@ static unsigned long current_target(void)
return target;
}

-static int increase_reservation(unsigned long nr_pages)
+static enum bp_state increase_reservation(unsigned long nr_pages)
{
+ enum bp_state state = BP_DONE;
+ int rc;
unsigned long pfn, i, flags;
struct page *page;
- long rc;
struct xen_memory_reservation reservation = {
.address_bits = 0,
.extent_order = 0,
@@ -195,8 +231,17 @@ static int increase_reservation(unsigned long nr_pages)
spin_lock_irqsave(&xen_reservation_lock, flags);

page = balloon_first_page();
+
+ if (!page) {
+ state = BP_ERROR;
+ goto out;
+ }
+
for (i = 0; i < nr_pages; i++) {
- BUG_ON(page == NULL);
+ if (!page) {
+ nr_pages = i;
+ break;
+ }
frame_list[i] = page_to_pfn(page);
page = balloon_next_page(page);
}
@@ -204,8 +249,11 @@ static int increase_reservation(unsigned long nr_pages)
set_xen_guest_handle(reservation.extent_start, frame_list);
reservation.nr_extents = nr_pages;
rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
- if (rc < 0)
- goto out;
+ if (rc < nr_pages) {
+ state = (rc <= 0) ? BP_ERROR : BP_HUNGRY;
+ if (rc <= 0)
+ goto out;
+ }

for (i = 0; i < rc; i++) {
page = balloon_retrieve();
@@ -238,14 +286,14 @@ static int increase_reservation(unsigned long nr_pages)
out:
spin_unlock_irqrestore(&xen_reservation_lock, flags);

- return rc < 0 ? rc : rc != nr_pages;
+ return state;
}

-static int decrease_reservation(unsigned long nr_pages)
+static enum bp_state decrease_reservation(unsigned long nr_pages)
{
+ enum bp_state state = BP_DONE;
unsigned long pfn, i, flags;
struct page *page;
- int need_sleep = 0;
int ret;
struct xen_memory_reservation reservation = {
.address_bits = 0,
@@ -259,7 +307,7 @@ static int decrease_reservation(unsigned long nr_pages)
for (i = 0; i < nr_pages; i++) {
if ((page = alloc_page(GFP_BALLOON)) == NULL) {
nr_pages = i;
- need_sleep = 1;
+ state = BP_ERROR;
break;
}

@@ -299,7 +347,7 @@ static int decrease_reservation(unsigned long nr_pages)

spin_unlock_irqrestore(&xen_reservation_lock, flags);

- return need_sleep;
+ return state;
}

/*
@@ -310,27 +358,35 @@ static int decrease_reservation(unsigned long nr_pages)
*/
static void balloon_process(struct work_struct *work)
{
- int need_sleep = 0;
+ enum bp_state rc, state = BP_DONE;
long credit;

mutex_lock(&balloon_mutex);

do {
credit = current_target() - balloon_stats.current_pages;
- if (credit > 0)
- need_sleep = (increase_reservation(credit) != 0);
- if (credit < 0)
- need_sleep = (decrease_reservation(-credit) != 0);
+
+ if (credit > 0) {
+ rc = increase_reservation(credit);
+ state = (rc == BP_ERROR) ? BP_ERROR : state;
+ }
+
+ if (credit < 0) {
+ rc = decrease_reservation(-credit);
+ state = (rc == BP_ERROR) ? BP_ERROR : state;
+ }
+
+ update_schedule_delay(state);

#ifndef CONFIG_PREEMPT
if (need_resched())
schedule();
#endif
- } while ((credit != 0) && !need_sleep);
+ } while (credit && state != BP_ERROR);

/* Schedule more work if there is some still to be done. */
- if (current_target() != balloon_stats.current_pages)
- schedule_delayed_work(&balloon_worker, HZ);
+ if (state == BP_ERROR)
+ schedule_delayed_work(&balloon_worker, balloon_stats.schedule_delay * HZ);

mutex_unlock(&balloon_mutex);
}
@@ -399,6 +455,9 @@ static int __init balloon_init(void)
balloon_stats.balloon_low = 0;
balloon_stats.balloon_high = 0;

+ balloon_stats.schedule_delay = 1;
+ balloon_stats.max_schedule_delay = 32;
+
register_balloon(&balloon_sysdev);

/* Initialise the balloon with excess memory space. */
@@ -439,6 +498,9 @@ BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));

+static SYSDEV_ULONG_ATTR(schedule_delay, 0644, balloon_stats.schedule_delay);
+static SYSDEV_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
+
static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr,
char *buf)
{
@@ -500,6 +562,8 @@ static SYSDEV_ATTR(target, S_IRUGO | S_IWUSR,
static struct sysdev_attribute *balloon_attrs[] = {
&attr_target_kb,
&attr_target,
+ &attr_schedule_delay.attr,
+ &attr_max_schedule_delay.attr
};

static struct attribute *balloon_info_attrs[] = {
--
1.4.4.4


2010-12-30 16:27:55

by Konrad Rzeszutek Wilk

[permalink] [raw]
Subject: Re: [PATCH R2 5/7] xen/balloon: Protect before CPU exhaust by event/x process

> -static int increase_reservation(unsigned long nr_pages)
> +static enum bp_state increase_reservation(unsigned long nr_pages)
> {
> + enum bp_state state = BP_DONE;
> + int rc;
> unsigned long pfn, i, flags;
> struct page *page;
> - long rc;

How come? Is it just a cleanup?

2010-12-30 22:05:55

by Daniel Kiper

[permalink] [raw]
Subject: Re: [PATCH R2 5/7] xen/balloon: Protect before CPU exhaust by event/x process

Hi,

On Thu, Dec 30, 2010 at 11:26:11AM -0500, Konrad Rzeszutek Wilk wrote:
> > -static int increase_reservation(unsigned long nr_pages)
> > +static enum bp_state increase_reservation(unsigned long nr_pages)
> > {
> > + enum bp_state state = BP_DONE;
> > + int rc;
> > unsigned long pfn, i, flags;
> > struct page *page;
> > - long rc;
>
> How come? Is it just a cleanup?

I forgot to move it to separate patch. When I was working
on protection before CPU exhaust I discovered that
HYPERVISOR_memory_op() returns int and rc could be
declared as int not as long.

Daniel