xen: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Cc: Stable Kernel <stable@kernel.org>

authored by

Ian Campbell and committed by
Jeremy Fitzhardinge
bc2c0303 3d65c948

+5 -26
+5 -26
drivers/xen/balloon.c
··· 66 /* We aim for 'current allocation' == 'target allocation'. */ 67 unsigned long current_pages; 68 unsigned long target_pages; 69 - /* We may hit the hard limit in Xen. If we do then we remember it. */ 70 - unsigned long hard_limit; 71 /* 72 * Drivers may alter the memory reservation independently, but they 73 * must inform the balloon driver so we avoid hitting the hard limit. ··· 183 184 static unsigned long current_target(void) 185 { 186 - unsigned long target = min(balloon_stats.target_pages, balloon_stats.hard_limit); 187 188 target = min(target, 189 balloon_stats.current_pages + ··· 219 set_xen_guest_handle(reservation.extent_start, frame_list); 220 reservation.nr_extents = nr_pages; 221 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation); 222 - if (rc < nr_pages) { 223 - if (rc > 0) { 224 - int ret; 225 - 226 - /* We hit the Xen hard limit: reprobe. */ 227 - reservation.nr_extents = rc; 228 - ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation, 229 - &reservation); 230 - BUG_ON(ret != rc); 231 - } 232 - if (rc >= 0) 233 - balloon_stats.hard_limit = (balloon_stats.current_pages + rc - 234 - balloon_stats.driver_pages); 235 goto out; 236 - } 237 238 - for (i = 0; i < nr_pages; i++) { 239 page = balloon_retrieve(); 240 BUG_ON(page == NULL); 241 ··· 248 __free_page(page); 249 } 250 251 - balloon_stats.current_pages += nr_pages; 252 253 out: 254 spin_unlock_irqrestore(&balloon_lock, flags); 255 256 - return 0; 257 } 258 259 static int decrease_reservation(unsigned long nr_pages) ··· 354 static void balloon_set_new_target(unsigned long target) 355 { 356 /* No need for lock. Not read-modify-write updates. */ 357 - balloon_stats.hard_limit = ~0UL; 358 balloon_stats.target_pages = target; 359 schedule_work(&balloon_worker); 360 } ··· 412 balloon_stats.balloon_low = 0; 413 balloon_stats.balloon_high = 0; 414 balloon_stats.driver_pages = 0UL; 415 - balloon_stats.hard_limit = ~0UL; 416 417 init_timer(&balloon_timer); 418 balloon_timer.data = 0; ··· 456 BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages)); 457 BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low)); 458 BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high)); 459 - BALLOON_SHOW(hard_limit_kb, 460 - (balloon_stats.hard_limit!=~0UL) ? "%lu\n" : "???\n", 461 - (balloon_stats.hard_limit!=~0UL) ? PAGES2KB(balloon_stats.hard_limit) : 0); 462 BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages)); 463 464 static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr, ··· 525 &attr_current_kb.attr, 526 &attr_low_kb.attr, 527 &attr_high_kb.attr, 528 - &attr_hard_limit_kb.attr, 529 &attr_driver_kb.attr, 530 NULL 531 };
··· 66 /* We aim for 'current allocation' == 'target allocation'. */ 67 unsigned long current_pages; 68 unsigned long target_pages; 69 /* 70 * Drivers may alter the memory reservation independently, but they 71 * must inform the balloon driver so we avoid hitting the hard limit. ··· 185 186 static unsigned long current_target(void) 187 { 188 + unsigned long target = balloon_stats.target_pages; 189 190 target = min(target, 191 balloon_stats.current_pages + ··· 221 set_xen_guest_handle(reservation.extent_start, frame_list); 222 reservation.nr_extents = nr_pages; 223 rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation); 224 + if (rc < 0) 225 goto out; 226 227 + for (i = 0; i < rc; i++) { 228 page = balloon_retrieve(); 229 BUG_ON(page == NULL); 230 ··· 263 __free_page(page); 264 } 265 266 + balloon_stats.current_pages += rc; 267 268 out: 269 spin_unlock_irqrestore(&balloon_lock, flags); 270 271 + return rc < 0 ? rc : rc != nr_pages; 272 } 273 274 static int decrease_reservation(unsigned long nr_pages) ··· 369 static void balloon_set_new_target(unsigned long target) 370 { 371 /* No need for lock. Not read-modify-write updates. */ 372 balloon_stats.target_pages = target; 373 schedule_work(&balloon_worker); 374 } ··· 428 balloon_stats.balloon_low = 0; 429 balloon_stats.balloon_high = 0; 430 balloon_stats.driver_pages = 0UL; 431 432 init_timer(&balloon_timer); 433 balloon_timer.data = 0; ··· 473 BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages)); 474 BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low)); 475 BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high)); 476 BALLOON_SHOW(driver_kb, "%lu\n", PAGES2KB(balloon_stats.driver_pages)); 477 478 static ssize_t show_target_kb(struct sys_device *dev, struct sysdev_attribute *attr, ··· 545 &attr_current_kb.attr, 546 &attr_low_kb.attr, 547 &attr_high_kb.attr, 548 &attr_driver_kb.attr, 549 NULL 550 };