ia64/linux-2.6.18-xen.hg

annotate drivers/pci/pci-driver.c @ 897:329ea0ccb344

balloon: 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>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jun 05 14:01:20 2009 +0100 (2009-06-05)
parents 831230e53067
children
rev   line source
ian@0 1 /*
ian@0 2 * drivers/pci/pci-driver.c
ian@0 3 *
ian@0 4 */
ian@0 5
ian@0 6 #include <linux/pci.h>
ian@0 7 #include <linux/module.h>
ian@0 8 #include <linux/init.h>
ian@0 9 #include <linux/device.h>
ian@0 10 #include <linux/mempolicy.h>
ian@0 11 #include <linux/string.h>
ian@0 12 #include <linux/slab.h>
ian@0 13 #include <linux/sched.h>
ian@0 14 #include "pci.h"
ian@0 15
ian@0 16 /*
ian@0 17 * Registration of PCI drivers and handling of hot-pluggable devices.
ian@0 18 */
ian@0 19
ian@0 20 /*
ian@0 21 * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
ian@0 22 */
ian@0 23
ian@0 24 struct pci_dynid {
ian@0 25 struct list_head node;
ian@0 26 struct pci_device_id id;
ian@0 27 };
ian@0 28
ian@0 29 #ifdef CONFIG_HOTPLUG
ian@0 30
ian@0 31 /**
ian@0 32 * store_new_id - add a new PCI device ID to this driver and re-probe devices
ian@0 33 * @driver: target device driver
ian@0 34 * @buf: buffer for scanning device ID data
ian@0 35 * @count: input size
ian@0 36 *
ian@0 37 * Adds a new dynamic pci device ID to this driver,
ian@0 38 * and causes the driver to probe for all devices again.
ian@0 39 */
ian@0 40 static ssize_t
ian@0 41 store_new_id(struct device_driver *driver, const char *buf, size_t count)
ian@0 42 {
ian@0 43 struct pci_dynid *dynid;
ian@0 44 struct pci_driver *pdrv = to_pci_driver(driver);
ian@0 45 __u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
ian@0 46 subdevice=PCI_ANY_ID, class=0, class_mask=0;
ian@0 47 unsigned long driver_data=0;
ian@0 48 int fields=0;
ian@0 49
ian@0 50 fields = sscanf(buf, "%x %x %x %x %x %x %lux",
ian@0 51 &vendor, &device, &subvendor, &subdevice,
ian@0 52 &class, &class_mask, &driver_data);
ian@0 53 if (fields < 0)
ian@0 54 return -EINVAL;
ian@0 55
ian@0 56 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
ian@0 57 if (!dynid)
ian@0 58 return -ENOMEM;
ian@0 59
ian@0 60 INIT_LIST_HEAD(&dynid->node);
ian@0 61 dynid->id.vendor = vendor;
ian@0 62 dynid->id.device = device;
ian@0 63 dynid->id.subvendor = subvendor;
ian@0 64 dynid->id.subdevice = subdevice;
ian@0 65 dynid->id.class = class;
ian@0 66 dynid->id.class_mask = class_mask;
ian@0 67 dynid->id.driver_data = pdrv->dynids.use_driver_data ?
ian@0 68 driver_data : 0UL;
ian@0 69
ian@0 70 spin_lock(&pdrv->dynids.lock);
ian@0 71 list_add_tail(&pdrv->dynids.list, &dynid->node);
ian@0 72 spin_unlock(&pdrv->dynids.lock);
ian@0 73
ian@0 74 if (get_driver(&pdrv->driver)) {
ian@0 75 driver_attach(&pdrv->driver);
ian@0 76 put_driver(&pdrv->driver);
ian@0 77 }
ian@0 78
ian@0 79 return count;
ian@0 80 }
ian@0 81 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
ian@0 82
ian@0 83 static void
ian@0 84 pci_free_dynids(struct pci_driver *drv)
ian@0 85 {
ian@0 86 struct pci_dynid *dynid, *n;
ian@0 87
ian@0 88 spin_lock(&drv->dynids.lock);
ian@0 89 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
ian@0 90 list_del(&dynid->node);
ian@0 91 kfree(dynid);
ian@0 92 }
ian@0 93 spin_unlock(&drv->dynids.lock);
ian@0 94 }
ian@0 95
ian@0 96 static int
ian@0 97 pci_create_newid_file(struct pci_driver *drv)
ian@0 98 {
ian@0 99 int error = 0;
ian@0 100 if (drv->probe != NULL)
ian@0 101 error = sysfs_create_file(&drv->driver.kobj,
ian@0 102 &driver_attr_new_id.attr);
ian@0 103 return error;
ian@0 104 }
ian@0 105
ian@0 106 #else /* !CONFIG_HOTPLUG */
ian@0 107 static inline void pci_free_dynids(struct pci_driver *drv) {}
ian@0 108 static inline int pci_create_newid_file(struct pci_driver *drv)
ian@0 109 {
ian@0 110 return 0;
ian@0 111 }
ian@0 112 #endif
ian@0 113
ian@0 114 /**
ian@0 115 * pci_match_id - See if a pci device matches a given pci_id table
ian@0 116 * @ids: array of PCI device id structures to search in
ian@0 117 * @dev: the PCI device structure to match against.
ian@0 118 *
ian@0 119 * Used by a driver to check whether a PCI device present in the
ian@0 120 * system is in its list of supported devices. Returns the matching
ian@0 121 * pci_device_id structure or %NULL if there is no match.
ian@0 122 *
ian@0 123 * Depreciated, don't use this as it will not catch any dynamic ids
ian@0 124 * that a driver might want to check for.
ian@0 125 */
ian@0 126 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
ian@0 127 struct pci_dev *dev)
ian@0 128 {
ian@0 129 if (ids) {
ian@0 130 while (ids->vendor || ids->subvendor || ids->class_mask) {
ian@0 131 if (pci_match_one_device(ids, dev))
ian@0 132 return ids;
ian@0 133 ids++;
ian@0 134 }
ian@0 135 }
ian@0 136 return NULL;
ian@0 137 }
ian@0 138
ian@0 139 /**
ian@0 140 * pci_match_device - Tell if a PCI device structure has a matching
ian@0 141 * PCI device id structure
ian@0 142 * @drv: the PCI driver to match against
ian@0 143 * @dev: the PCI device structure to match against
ian@0 144 *
ian@0 145 * Used by a driver to check whether a PCI device present in the
ian@0 146 * system is in its list of supported devices. Returns the matching
ian@0 147 * pci_device_id structure or %NULL if there is no match.
ian@0 148 */
ian@0 149 const struct pci_device_id *pci_match_device(struct pci_driver *drv,
ian@0 150 struct pci_dev *dev)
ian@0 151 {
ian@0 152 const struct pci_device_id *id;
ian@0 153 struct pci_dynid *dynid;
ian@0 154
ian@0 155 id = pci_match_id(drv->id_table, dev);
ian@0 156 if (id)
ian@0 157 return id;
ian@0 158
ian@0 159 /* static ids didn't match, lets look at the dynamic ones */
ian@0 160 spin_lock(&drv->dynids.lock);
ian@0 161 list_for_each_entry(dynid, &drv->dynids.list, node) {
ian@0 162 if (pci_match_one_device(&dynid->id, dev)) {
ian@0 163 spin_unlock(&drv->dynids.lock);
ian@0 164 return &dynid->id;
ian@0 165 }
ian@0 166 }
ian@0 167 spin_unlock(&drv->dynids.lock);
ian@0 168 return NULL;
ian@0 169 }
ian@0 170
ian@0 171 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
ian@0 172 const struct pci_device_id *id)
ian@0 173 {
ian@0 174 int error;
ian@0 175 #ifdef CONFIG_NUMA
ian@0 176 /* Execute driver initialization on node where the
ian@0 177 device's bus is attached to. This way the driver likely
ian@0 178 allocates its local memory on the right node without
ian@0 179 any need to change it. */
ian@0 180 struct mempolicy *oldpol;
ian@0 181 cpumask_t oldmask = current->cpus_allowed;
ian@0 182 int node = pcibus_to_node(dev->bus);
ian@0 183 if (node >= 0 && node_online(node))
ian@0 184 set_cpus_allowed(current, node_to_cpumask(node));
ian@0 185 /* And set default memory allocation policy */
ian@0 186 oldpol = current->mempolicy;
ian@0 187 current->mempolicy = &default_policy;
ian@0 188 mpol_get(current->mempolicy);
ian@0 189 #endif
ian@0 190 error = drv->probe(dev, id);
ian@0 191 #ifdef CONFIG_NUMA
ian@0 192 set_cpus_allowed(current, oldmask);
ian@0 193 mpol_free(current->mempolicy);
ian@0 194 current->mempolicy = oldpol;
ian@0 195 #endif
ian@0 196 return error;
ian@0 197 }
ian@0 198
ian@0 199 /**
ian@0 200 * __pci_device_probe()
ian@0 201 * @drv: driver to call to check if it wants the PCI device
ian@0 202 * @pci_dev: PCI device being probed
ian@0 203 *
ian@0 204 * returns 0 on success, else error.
ian@0 205 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
ian@0 206 */
ian@0 207 static int
ian@0 208 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
ian@0 209 {
ian@0 210 const struct pci_device_id *id;
ian@0 211 int error = 0;
ian@0 212
ian@0 213 if (!pci_dev->driver && drv->probe) {
ian@0 214 error = -ENODEV;
ian@0 215
ian@0 216 id = pci_match_device(drv, pci_dev);
ian@0 217 if (id)
ian@0 218 error = pci_call_probe(drv, pci_dev, id);
ian@0 219 if (error >= 0) {
ian@0 220 pci_dev->driver = drv;
ian@0 221 error = 0;
ian@0 222 }
ian@0 223 }
ian@0 224 return error;
ian@0 225 }
ian@0 226
ian@0 227 static int pci_device_probe(struct device * dev)
ian@0 228 {
ian@0 229 int error = 0;
ian@0 230 struct pci_driver *drv;
ian@0 231 struct pci_dev *pci_dev;
ian@0 232
ian@0 233 drv = to_pci_driver(dev->driver);
ian@0 234 pci_dev = to_pci_dev(dev);
ian@0 235 pci_dev_get(pci_dev);
ian@0 236 error = __pci_device_probe(drv, pci_dev);
ian@0 237 if (error)
ian@0 238 pci_dev_put(pci_dev);
ian@0 239
ian@0 240 return error;
ian@0 241 }
ian@0 242
ian@0 243 static int pci_device_remove(struct device * dev)
ian@0 244 {
ian@0 245 struct pci_dev * pci_dev = to_pci_dev(dev);
ian@0 246 struct pci_driver * drv = pci_dev->driver;
ian@0 247
ian@0 248 if (drv) {
ian@0 249 if (drv->remove)
ian@0 250 drv->remove(pci_dev);
ian@0 251 pci_dev->driver = NULL;
ian@0 252 }
ian@0 253
ian@0 254 /*
ian@0 255 * We would love to complain here if pci_dev->is_enabled is set, that
ian@0 256 * the driver should have called pci_disable_device(), but the
ian@0 257 * unfortunate fact is there are too many odd BIOS and bridge setups
ian@0 258 * that don't like drivers doing that all of the time.
ian@0 259 * Oh well, we can dream of sane hardware when we sleep, no matter how
ian@0 260 * horrible the crap we have to deal with is when we are awake...
ian@0 261 */
ian@0 262
ian@0 263 pci_dev_put(pci_dev);
ian@0 264 return 0;
ian@0 265 }
ian@0 266
ian@0 267 static int pci_device_suspend(struct device * dev, pm_message_t state)
ian@0 268 {
ian@0 269 struct pci_dev * pci_dev = to_pci_dev(dev);
ian@0 270 struct pci_driver * drv = pci_dev->driver;
ian@0 271 int i = 0;
ian@0 272
ian@0 273 if (drv && drv->suspend) {
ian@0 274 i = drv->suspend(pci_dev, state);
ian@0 275 suspend_report_result(drv->suspend, i);
ian@0 276 } else {
ian@0 277 pci_save_state(pci_dev);
ian@0 278 }
ian@0 279 return i;
ian@0 280 }
ian@0 281
ian@0 282
ian@0 283 /*
ian@0 284 * Default resume method for devices that have no driver provided resume,
ian@0 285 * or not even a driver at all.
ian@0 286 */
ian@0 287 static int pci_default_resume(struct pci_dev *pci_dev)
ian@0 288 {
ian@0 289 int retval = 0;
ian@0 290
ian@0 291 /* restore the PCI config space */
ian@0 292 pci_restore_state(pci_dev);
ian@0 293 /* if the device was enabled before suspend, reenable */
ian@0 294 if (pci_dev->is_enabled)
ian@0 295 retval = pci_enable_device(pci_dev);
ian@0 296 /* if the device was busmaster before the suspend, make it busmaster again */
ian@0 297 if (pci_dev->is_busmaster)
ian@0 298 pci_set_master(pci_dev);
ian@0 299
ian@0 300 return retval;
ian@0 301 }
ian@0 302
ian@0 303 static int pci_device_resume(struct device * dev)
ian@0 304 {
ian@0 305 int error;
ian@0 306 struct pci_dev * pci_dev = to_pci_dev(dev);
ian@0 307 struct pci_driver * drv = pci_dev->driver;
ian@0 308
ian@0 309 if (drv && drv->resume)
ian@0 310 error = drv->resume(pci_dev);
ian@0 311 else
ian@0 312 error = pci_default_resume(pci_dev);
ian@0 313 return error;
ian@0 314 }
ian@0 315
ian@0 316 static void pci_device_shutdown(struct device *dev)
ian@0 317 {
ian@0 318 struct pci_dev *pci_dev = to_pci_dev(dev);
ian@0 319 struct pci_driver *drv = pci_dev->driver;
ian@0 320
ian@0 321 if (drv && drv->shutdown)
ian@0 322 drv->shutdown(pci_dev);
ian@0 323 }
ian@0 324
ian@0 325 #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
ian@0 326 #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
ian@0 327
ian@0 328 static ssize_t
ian@0 329 pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
ian@0 330 {
ian@0 331 struct device_driver *driver = kobj_to_pci_driver(kobj);
ian@0 332 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
ian@0 333 ssize_t ret;
ian@0 334
ian@0 335 if (!get_driver(driver))
ian@0 336 return -ENODEV;
ian@0 337
ian@0 338 ret = dattr->show ? dattr->show(driver, buf) : -EIO;
ian@0 339
ian@0 340 put_driver(driver);
ian@0 341 return ret;
ian@0 342 }
ian@0 343
ian@0 344 static ssize_t
ian@0 345 pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
ian@0 346 const char *buf, size_t count)
ian@0 347 {
ian@0 348 struct device_driver *driver = kobj_to_pci_driver(kobj);
ian@0 349 struct driver_attribute *dattr = attr_to_driver_attribute(attr);
ian@0 350 ssize_t ret;
ian@0 351
ian@0 352 if (!get_driver(driver))
ian@0 353 return -ENODEV;
ian@0 354
ian@0 355 ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
ian@0 356
ian@0 357 put_driver(driver);
ian@0 358 return ret;
ian@0 359 }
ian@0 360
ian@0 361 static struct sysfs_ops pci_driver_sysfs_ops = {
ian@0 362 .show = pci_driver_attr_show,
ian@0 363 .store = pci_driver_attr_store,
ian@0 364 };
ian@0 365 static struct kobj_type pci_driver_kobj_type = {
ian@0 366 .sysfs_ops = &pci_driver_sysfs_ops,
ian@0 367 };
ian@0 368
ian@0 369 /**
ian@0 370 * __pci_register_driver - register a new pci driver
ian@0 371 * @drv: the driver structure to register
ian@0 372 * @owner: owner module of drv
ian@0 373 *
ian@0 374 * Adds the driver structure to the list of registered drivers.
ian@0 375 * Returns a negative value on error, otherwise 0.
ian@0 376 * If no error occurred, the driver remains registered even if
ian@0 377 * no device was claimed during registration.
ian@0 378 */
ian@0 379 int __pci_register_driver(struct pci_driver *drv, struct module *owner)
ian@0 380 {
ian@0 381 int error;
ian@0 382
ian@0 383 /* initialize common driver fields */
ian@0 384 drv->driver.name = drv->name;
ian@0 385 drv->driver.bus = &pci_bus_type;
ian@0 386 drv->driver.owner = owner;
ian@0 387 drv->driver.kobj.ktype = &pci_driver_kobj_type;
ian@0 388
ian@0 389 spin_lock_init(&drv->dynids.lock);
ian@0 390 INIT_LIST_HEAD(&drv->dynids.list);
ian@0 391
ian@0 392 /* register with core */
ian@0 393 error = driver_register(&drv->driver);
ian@0 394
ian@0 395 if (!error)
ian@0 396 error = pci_create_newid_file(drv);
ian@0 397
ian@0 398 return error;
ian@0 399 }
ian@0 400
ian@0 401 /**
ian@0 402 * pci_unregister_driver - unregister a pci driver
ian@0 403 * @drv: the driver structure to unregister
ian@0 404 *
ian@0 405 * Deletes the driver structure from the list of registered PCI drivers,
ian@0 406 * gives it a chance to clean up by calling its remove() function for
ian@0 407 * each device it was responsible for, and marks those devices as
ian@0 408 * driverless.
ian@0 409 */
ian@0 410
ian@0 411 void
ian@0 412 pci_unregister_driver(struct pci_driver *drv)
ian@0 413 {
ian@0 414 driver_unregister(&drv->driver);
ian@0 415 pci_free_dynids(drv);
ian@0 416 }
ian@0 417
ian@0 418 static struct pci_driver pci_compat_driver = {
ian@0 419 .name = "compat"
ian@0 420 };
ian@0 421
ian@0 422 /**
ian@0 423 * pci_dev_driver - get the pci_driver of a device
ian@0 424 * @dev: the device to query
ian@0 425 *
ian@0 426 * Returns the appropriate pci_driver structure or %NULL if there is no
ian@0 427 * registered driver for the device.
ian@0 428 */
ian@0 429 struct pci_driver *
ian@0 430 pci_dev_driver(const struct pci_dev *dev)
ian@0 431 {
ian@0 432 if (dev->driver)
ian@0 433 return dev->driver;
ian@0 434 else {
ian@0 435 int i;
ian@0 436 for(i=0; i<=PCI_ROM_RESOURCE; i++)
ian@0 437 if (dev->resource[i].flags & IORESOURCE_BUSY)
ian@0 438 return &pci_compat_driver;
ian@0 439 }
ian@0 440 return NULL;
ian@0 441 }
ian@0 442
ian@0 443 /**
ian@0 444 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
ian@0 445 * @dev: the PCI device structure to match against
ian@0 446 * @drv: the device driver to search for matching PCI device id structures
ian@0 447 *
ian@0 448 * Used by a driver to check whether a PCI device present in the
ian@0 449 * system is in its list of supported devices. Returns the matching
ian@0 450 * pci_device_id structure or %NULL if there is no match.
ian@0 451 */
ian@0 452 static int pci_bus_match(struct device *dev, struct device_driver *drv)
ian@0 453 {
ian@0 454 struct pci_dev *pci_dev = to_pci_dev(dev);
ian@0 455 struct pci_driver *pci_drv = to_pci_driver(drv);
ian@0 456 const struct pci_device_id *found_id;
ian@0 457
ian@0 458 found_id = pci_match_device(pci_drv, pci_dev);
ian@0 459 if (found_id)
ian@0 460 return 1;
ian@0 461
ian@0 462 return 0;
ian@0 463 }
ian@0 464
ian@0 465 /**
ian@0 466 * pci_dev_get - increments the reference count of the pci device structure
ian@0 467 * @dev: the device being referenced
ian@0 468 *
ian@0 469 * Each live reference to a device should be refcounted.
ian@0 470 *
ian@0 471 * Drivers for PCI devices should normally record such references in
ian@0 472 * their probe() methods, when they bind to a device, and release
ian@0 473 * them by calling pci_dev_put(), in their disconnect() methods.
ian@0 474 *
ian@0 475 * A pointer to the device with the incremented reference counter is returned.
ian@0 476 */
ian@0 477 struct pci_dev *pci_dev_get(struct pci_dev *dev)
ian@0 478 {
ian@0 479 if (dev)
ian@0 480 get_device(&dev->dev);
ian@0 481 return dev;
ian@0 482 }
ian@0 483
ian@0 484 /**
ian@0 485 * pci_dev_put - release a use of the pci device structure
ian@0 486 * @dev: device that's been disconnected
ian@0 487 *
ian@0 488 * Must be called when a user of a device is finished with it. When the last
ian@0 489 * user of the device calls this function, the memory of the device is freed.
ian@0 490 */
ian@0 491 void pci_dev_put(struct pci_dev *dev)
ian@0 492 {
ian@0 493 if (dev)
ian@0 494 put_device(&dev->dev);
ian@0 495 }
ian@0 496
ian@0 497 #ifndef CONFIG_HOTPLUG
ian@0 498 int pci_uevent(struct device *dev, char **envp, int num_envp,
ian@0 499 char *buffer, int buffer_size)
ian@0 500 {
ian@0 501 return -ENODEV;
ian@0 502 }
ian@0 503 #endif
ian@0 504
ian@0 505 struct bus_type pci_bus_type = {
ian@0 506 .name = "pci",
ian@0 507 .match = pci_bus_match,
ian@0 508 .uevent = pci_uevent,
ian@0 509 .probe = pci_device_probe,
ian@0 510 .remove = pci_device_remove,
ian@0 511 .suspend = pci_device_suspend,
ian@0 512 .shutdown = pci_device_shutdown,
ian@0 513 .resume = pci_device_resume,
ian@0 514 .dev_attrs = pci_dev_attrs,
ian@0 515 };
ian@0 516
ian@0 517 static int __init pci_driver_init(void)
ian@0 518 {
ian@0 519 return bus_register(&pci_bus_type);
ian@0 520 }
ian@0 521
ian@0 522 postcore_initcall(pci_driver_init);
ian@0 523
ian@0 524 EXPORT_SYMBOL(pci_match_id);
ian@0 525 EXPORT_SYMBOL(pci_match_device);
ian@0 526 EXPORT_SYMBOL(__pci_register_driver);
ian@0 527 EXPORT_SYMBOL(pci_unregister_driver);
ian@0 528 EXPORT_SYMBOL(pci_dev_driver);
ian@0 529 EXPORT_SYMBOL(pci_bus_type);
ian@0 530 EXPORT_SYMBOL(pci_dev_get);
ian@0 531 EXPORT_SYMBOL(pci_dev_put);