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