Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'pm-sleep'

* pm-sleep:
PM / Sleep: User space wakeup sources garbage collector Kconfig option
PM / Sleep: Make the limit of user space wakeup sources configurable
PM / Documentation: suspend-and-cpuhotplug.txt: Fix typo
PM / Sleep: Fix a mistake in a conditional in autosleep_store()
epoll: Add a flag, EPOLLWAKEUP, to prevent suspend while epoll events are ready
PM / Sleep: Add user space interface for manipulating wakeup sources, v3
PM / Sleep: Add "prevent autosleep time" statistics to wakeup sources
PM / Sleep: Implement opportunistic sleep, v2
PM / Sleep: Add wakeup_source_activate and wakeup_source_deactivate tracepoints
PM / Sleep: Change wakeup source statistics to follow Android
PM / Sleep: Use wait queue to signal "no wakeup events in progress"
PM / Sleep: Look for wakeup events in later stages of device suspend
PM / Hibernate: Hibernate/thaw fixes/improvements

+1050 -118
+28 -7
Documentation/ABI/testing/sysfs-devices-power
··· 96 96 is read-only. If the device is not enabled to wake up the 97 97 system from sleep states, this attribute is not present. 98 98 99 - What: /sys/devices/.../power/wakeup_hit_count 100 - Date: September 2010 99 + What: /sys/devices/.../power/wakeup_abort_count 100 + Date: February 2012 101 101 Contact: Rafael J. Wysocki <rjw@sisk.pl> 102 102 Description: 103 - The /sys/devices/.../wakeup_hit_count attribute contains the 103 + The /sys/devices/.../wakeup_abort_count attribute contains the 104 104 number of times the processing of a wakeup event associated with 105 - the device might prevent the system from entering a sleep state. 106 - This attribute is read-only. If the device is not enabled to 107 - wake up the system from sleep states, this attribute is not 108 - present. 105 + the device might have aborted system transition into a sleep 106 + state in progress. This attribute is read-only. If the device 107 + is not enabled to wake up the system from sleep states, this 108 + attribute is not present. 109 + 110 + What: /sys/devices/.../power/wakeup_expire_count 111 + Date: February 2012 112 + Contact: Rafael J. Wysocki <rjw@sisk.pl> 113 + Description: 114 + The /sys/devices/.../wakeup_expire_count attribute contains the 115 + number of times a wakeup event associated with the device has 116 + been reported with a timeout that expired. This attribute is 117 + read-only. If the device is not enabled to wake up the system 118 + from sleep states, this attribute is not present. 109 119 110 120 What: /sys/devices/.../power/wakeup_active 111 121 Date: September 2010 ··· 157 147 milliseconds. This attribute is read-only. If the device is 158 148 not enabled to wake up the system from sleep states, this 159 149 attribute is not present. 150 + 151 + What: /sys/devices/.../power/wakeup_prevent_sleep_time_ms 152 + Date: February 2012 153 + Contact: Rafael J. Wysocki <rjw@sisk.pl> 154 + Description: 155 + The /sys/devices/.../wakeup_prevent_sleep_time_ms attribute 156 + contains the total time the device has been preventing 157 + opportunistic transitions to sleep states from occuring. 158 + This attribute is read-only. If the device is not enabled to 159 + wake up the system from sleep states, this attribute is not 160 + present. 160 161 161 162 What: /sys/devices/.../power/autosuspend_delay_ms 162 163 Date: September 2010
+59
Documentation/ABI/testing/sysfs-power
··· 172 172 173 173 Reading from this file will display the current value, which is 174 174 set to 1 MB by default. 175 + 176 + What: /sys/power/autosleep 177 + Date: April 2012 178 + Contact: Rafael J. Wysocki <rjw@sisk.pl> 179 + Description: 180 + The /sys/power/autosleep file can be written one of the strings 181 + returned by reads from /sys/power/state. If that happens, a 182 + work item attempting to trigger a transition of the system to 183 + the sleep state represented by that string is queued up. This 184 + attempt will only succeed if there are no active wakeup sources 185 + in the system at that time. After every execution, regardless 186 + of whether or not the attempt to put the system to sleep has 187 + succeeded, the work item requeues itself until user space 188 + writes "off" to /sys/power/autosleep. 189 + 190 + Reading from this file causes the last string successfully 191 + written to it to be returned. 192 + 193 + What: /sys/power/wake_lock 194 + Date: February 2012 195 + Contact: Rafael J. Wysocki <rjw@sisk.pl> 196 + Description: 197 + The /sys/power/wake_lock file allows user space to create 198 + wakeup source objects and activate them on demand (if one of 199 + those wakeup sources is active, reads from the 200 + /sys/power/wakeup_count file block or return false). When a 201 + string without white space is written to /sys/power/wake_lock, 202 + it will be assumed to represent a wakeup source name. If there 203 + is a wakeup source object with that name, it will be activated 204 + (unless active already). Otherwise, a new wakeup source object 205 + will be registered, assigned the given name and activated. 206 + If a string written to /sys/power/wake_lock contains white 207 + space, the part of the string preceding the white space will be 208 + regarded as a wakeup source name and handled as descrived above. 209 + The other part of the string will be regarded as a timeout (in 210 + nanoseconds) such that the wakeup source will be automatically 211 + deactivated after it has expired. The timeout, if present, is 212 + set regardless of the current state of the wakeup source object 213 + in question. 214 + 215 + Reads from this file return a string consisting of the names of 216 + wakeup sources created with the help of it that are active at 217 + the moment, separated with spaces. 218 + 219 + 220 + What: /sys/power/wake_unlock 221 + Date: February 2012 222 + Contact: Rafael J. Wysocki <rjw@sisk.pl> 223 + Description: 224 + The /sys/power/wake_unlock file allows user space to deactivate 225 + wakeup sources created with the help of /sys/power/wake_lock. 226 + When a string is written to /sys/power/wake_unlock, it will be 227 + assumed to represent the name of a wakeup source to deactivate. 228 + If a wakeup source object of that name exists and is active at 229 + the moment, it will be deactivated. 230 + 231 + Reads from this file return a string consisting of the names of 232 + wakeup sources created with the help of /sys/power/wake_lock 233 + that are inactive at the moment, separated with spaces.
+1 -1
Documentation/power/suspend-and-cpuhotplug.txt
··· 29 29 30 30 Write 'mem' to 31 31 /sys/power/state 32 - syfs file 32 + sysfs file 33 33 | 34 34 v 35 35 Acquire pm_mutex lock
+10
drivers/base/power/main.c
··· 889 889 if (!list_empty(&dev->power.entry)) 890 890 list_move(&dev->power.entry, &dpm_noirq_list); 891 891 put_device(dev); 892 + 893 + if (pm_wakeup_pending()) { 894 + error = -EBUSY; 895 + break; 896 + } 892 897 } 893 898 mutex_unlock(&dpm_list_mtx); 894 899 if (error) ··· 967 962 if (!list_empty(&dev->power.entry)) 968 963 list_move(&dev->power.entry, &dpm_late_early_list); 969 964 put_device(dev); 965 + 966 + if (pm_wakeup_pending()) { 967 + error = -EBUSY; 968 + break; 969 + } 970 970 } 971 971 mutex_unlock(&dpm_list_mtx); 972 972 if (error)
+49 -5
drivers/base/power/sysfs.c
··· 314 314 315 315 static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL); 316 316 317 - static ssize_t wakeup_hit_count_show(struct device *dev, 318 - struct device_attribute *attr, char *buf) 317 + static ssize_t wakeup_abort_count_show(struct device *dev, 318 + struct device_attribute *attr, 319 + char *buf) 319 320 { 320 321 unsigned long count = 0; 321 322 bool enabled = false; 322 323 323 324 spin_lock_irq(&dev->power.lock); 324 325 if (dev->power.wakeup) { 325 - count = dev->power.wakeup->hit_count; 326 + count = dev->power.wakeup->wakeup_count; 326 327 enabled = true; 327 328 } 328 329 spin_unlock_irq(&dev->power.lock); 329 330 return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n"); 330 331 } 331 332 332 - static DEVICE_ATTR(wakeup_hit_count, 0444, wakeup_hit_count_show, NULL); 333 + static DEVICE_ATTR(wakeup_abort_count, 0444, wakeup_abort_count_show, NULL); 334 + 335 + static ssize_t wakeup_expire_count_show(struct device *dev, 336 + struct device_attribute *attr, 337 + char *buf) 338 + { 339 + unsigned long count = 0; 340 + bool enabled = false; 341 + 342 + spin_lock_irq(&dev->power.lock); 343 + if (dev->power.wakeup) { 344 + count = dev->power.wakeup->expire_count; 345 + enabled = true; 346 + } 347 + spin_unlock_irq(&dev->power.lock); 348 + return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n"); 349 + } 350 + 351 + static DEVICE_ATTR(wakeup_expire_count, 0444, wakeup_expire_count_show, NULL); 333 352 334 353 static ssize_t wakeup_active_show(struct device *dev, 335 354 struct device_attribute *attr, char *buf) ··· 417 398 } 418 399 419 400 static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL); 401 + 402 + #ifdef CONFIG_PM_AUTOSLEEP 403 + static ssize_t wakeup_prevent_sleep_time_show(struct device *dev, 404 + struct device_attribute *attr, 405 + char *buf) 406 + { 407 + s64 msec = 0; 408 + bool enabled = false; 409 + 410 + spin_lock_irq(&dev->power.lock); 411 + if (dev->power.wakeup) { 412 + msec = ktime_to_ms(dev->power.wakeup->prevent_sleep_time); 413 + enabled = true; 414 + } 415 + spin_unlock_irq(&dev->power.lock); 416 + return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n"); 417 + } 418 + 419 + static DEVICE_ATTR(wakeup_prevent_sleep_time_ms, 0444, 420 + wakeup_prevent_sleep_time_show, NULL); 421 + #endif /* CONFIG_PM_AUTOSLEEP */ 420 422 #endif /* CONFIG_PM_SLEEP */ 421 423 422 424 #ifdef CONFIG_PM_ADVANCED_DEBUG ··· 526 486 &dev_attr_wakeup.attr, 527 487 &dev_attr_wakeup_count.attr, 528 488 &dev_attr_wakeup_active_count.attr, 529 - &dev_attr_wakeup_hit_count.attr, 489 + &dev_attr_wakeup_abort_count.attr, 490 + &dev_attr_wakeup_expire_count.attr, 530 491 &dev_attr_wakeup_active.attr, 531 492 &dev_attr_wakeup_total_time_ms.attr, 532 493 &dev_attr_wakeup_max_time_ms.attr, 533 494 &dev_attr_wakeup_last_time_ms.attr, 495 + #ifdef CONFIG_PM_AUTOSLEEP 496 + &dev_attr_wakeup_prevent_sleep_time_ms.attr, 497 + #endif 534 498 #endif 535 499 NULL, 536 500 };
+120 -54
drivers/base/power/wakeup.c
··· 14 14 #include <linux/suspend.h> 15 15 #include <linux/seq_file.h> 16 16 #include <linux/debugfs.h> 17 + #include <trace/events/power.h> 17 18 18 19 #include "power.h" 19 - 20 - #define TIMEOUT 100 21 20 22 21 /* 23 22 * If set, the suspend/hibernate code will abort transitions to a sleep state 24 23 * if wakeup events are registered during or immediately before the transition. 25 24 */ 26 - bool events_check_enabled; 25 + bool events_check_enabled __read_mostly; 27 26 28 27 /* 29 28 * Combined counters of registered wakeup events and wakeup events in progress. ··· 50 51 static void pm_wakeup_timer_fn(unsigned long data); 51 52 52 53 static LIST_HEAD(wakeup_sources); 54 + 55 + static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue); 53 56 54 57 /** 55 58 * wakeup_source_prepare - Prepare a new wakeup source for initialization. ··· 133 132 spin_lock_init(&ws->lock); 134 133 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws); 135 134 ws->active = false; 135 + ws->last_time = ktime_get(); 136 136 137 137 spin_lock_irq(&events_lock); 138 138 list_add_rcu(&ws->entry, &wakeup_sources); ··· 376 374 */ 377 375 static void wakeup_source_activate(struct wakeup_source *ws) 378 376 { 377 + unsigned int cec; 378 + 379 379 ws->active = true; 380 380 ws->active_count++; 381 381 ws->last_time = ktime_get(); 382 + if (ws->autosleep_enabled) 383 + ws->start_prevent_time = ws->last_time; 382 384 383 385 /* Increment the counter of events in progress. */ 384 - atomic_inc(&combined_event_count); 386 + cec = atomic_inc_return(&combined_event_count); 387 + 388 + trace_wakeup_source_activate(ws->name, cec); 389 + } 390 + 391 + /** 392 + * wakeup_source_report_event - Report wakeup event using the given source. 393 + * @ws: Wakeup source to report the event for. 394 + */ 395 + static void wakeup_source_report_event(struct wakeup_source *ws) 396 + { 397 + ws->event_count++; 398 + /* This is racy, but the counter is approximate anyway. */ 399 + if (events_check_enabled) 400 + ws->wakeup_count++; 401 + 402 + if (!ws->active) 403 + wakeup_source_activate(ws); 385 404 } 386 405 387 406 /** ··· 420 397 421 398 spin_lock_irqsave(&ws->lock, flags); 422 399 423 - ws->event_count++; 424 - if (!ws->active) 425 - wakeup_source_activate(ws); 426 - 400 + wakeup_source_report_event(ws); 427 401 del_timer(&ws->timer); 428 402 ws->timer_expires = 0; 429 403 ··· 452 432 } 453 433 EXPORT_SYMBOL_GPL(pm_stay_awake); 454 434 435 + #ifdef CONFIG_PM_AUTOSLEEP 436 + static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now) 437 + { 438 + ktime_t delta = ktime_sub(now, ws->start_prevent_time); 439 + ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta); 440 + } 441 + #else 442 + static inline void update_prevent_sleep_time(struct wakeup_source *ws, 443 + ktime_t now) {} 444 + #endif 445 + 455 446 /** 456 447 * wakup_source_deactivate - Mark given wakeup source as inactive. 457 448 * @ws: Wakeup source to handle. ··· 473 442 */ 474 443 static void wakeup_source_deactivate(struct wakeup_source *ws) 475 444 { 445 + unsigned int cnt, inpr, cec; 476 446 ktime_t duration; 477 447 ktime_t now; 478 448 ··· 500 468 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time)) 501 469 ws->max_time = duration; 502 470 471 + ws->last_time = now; 503 472 del_timer(&ws->timer); 504 473 ws->timer_expires = 0; 474 + 475 + if (ws->autosleep_enabled) 476 + update_prevent_sleep_time(ws, now); 505 477 506 478 /* 507 479 * Increment the counter of registered wakeup events and decrement the 508 480 * couter of wakeup events in progress simultaneously. 509 481 */ 510 - atomic_add(MAX_IN_PROGRESS, &combined_event_count); 482 + cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count); 483 + trace_wakeup_source_deactivate(ws->name, cec); 484 + 485 + split_counters(&cnt, &inpr); 486 + if (!inpr && waitqueue_active(&wakeup_count_wait_queue)) 487 + wake_up(&wakeup_count_wait_queue); 511 488 } 512 489 513 490 /** ··· 577 536 spin_lock_irqsave(&ws->lock, flags); 578 537 579 538 if (ws->active && ws->timer_expires 580 - && time_after_eq(jiffies, ws->timer_expires)) 539 + && time_after_eq(jiffies, ws->timer_expires)) { 581 540 wakeup_source_deactivate(ws); 541 + ws->expire_count++; 542 + } 582 543 583 544 spin_unlock_irqrestore(&ws->lock, flags); 584 545 } ··· 607 564 608 565 spin_lock_irqsave(&ws->lock, flags); 609 566 610 - ws->event_count++; 611 - if (!ws->active) 612 - wakeup_source_activate(ws); 567 + wakeup_source_report_event(ws); 613 568 614 569 if (!msec) { 615 570 wakeup_source_deactivate(ws); ··· 650 609 EXPORT_SYMBOL_GPL(pm_wakeup_event); 651 610 652 611 /** 653 - * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources. 654 - */ 655 - static void pm_wakeup_update_hit_counts(void) 656 - { 657 - unsigned long flags; 658 - struct wakeup_source *ws; 659 - 660 - rcu_read_lock(); 661 - list_for_each_entry_rcu(ws, &wakeup_sources, entry) { 662 - spin_lock_irqsave(&ws->lock, flags); 663 - if (ws->active) 664 - ws->hit_count++; 665 - spin_unlock_irqrestore(&ws->lock, flags); 666 - } 667 - rcu_read_unlock(); 668 - } 669 - 670 - /** 671 612 * pm_wakeup_pending - Check if power transition in progress should be aborted. 672 613 * 673 614 * Compare the current number of registered wakeup events with its preserved ··· 671 648 events_check_enabled = !ret; 672 649 } 673 650 spin_unlock_irqrestore(&events_lock, flags); 674 - if (ret) 675 - pm_wakeup_update_hit_counts(); 676 651 return ret; 677 652 } 678 653 679 654 /** 680 655 * pm_get_wakeup_count - Read the number of registered wakeup events. 681 656 * @count: Address to store the value at. 657 + * @block: Whether or not to block. 682 658 * 683 - * Store the number of registered wakeup events at the address in @count. Block 684 - * if the current number of wakeup events being processed is nonzero. 659 + * Store the number of registered wakeup events at the address in @count. If 660 + * @block is set, block until the current number of wakeup events being 661 + * processed is zero. 685 662 * 686 - * Return 'false' if the wait for the number of wakeup events being processed to 687 - * drop down to zero has been interrupted by a signal (and the current number 688 - * of wakeup events being processed is still nonzero). Otherwise return 'true'. 663 + * Return 'false' if the current number of wakeup events being processed is 664 + * nonzero. Otherwise return 'true'. 689 665 */ 690 - bool pm_get_wakeup_count(unsigned int *count) 666 + bool pm_get_wakeup_count(unsigned int *count, bool block) 691 667 { 692 668 unsigned int cnt, inpr; 693 669 694 - for (;;) { 695 - split_counters(&cnt, &inpr); 696 - if (inpr == 0 || signal_pending(current)) 697 - break; 698 - pm_wakeup_update_hit_counts(); 699 - schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT)); 670 + if (block) { 671 + DEFINE_WAIT(wait); 672 + 673 + for (;;) { 674 + prepare_to_wait(&wakeup_count_wait_queue, &wait, 675 + TASK_INTERRUPTIBLE); 676 + split_counters(&cnt, &inpr); 677 + if (inpr == 0 || signal_pending(current)) 678 + break; 679 + 680 + schedule(); 681 + } 682 + finish_wait(&wakeup_count_wait_queue, &wait); 700 683 } 701 684 702 685 split_counters(&cnt, &inpr); ··· 732 703 events_check_enabled = true; 733 704 } 734 705 spin_unlock_irq(&events_lock); 735 - if (!events_check_enabled) 736 - pm_wakeup_update_hit_counts(); 737 706 return events_check_enabled; 738 707 } 708 + 709 + #ifdef CONFIG_PM_AUTOSLEEP 710 + /** 711 + * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources. 712 + * @enabled: Whether to set or to clear the autosleep_enabled flags. 713 + */ 714 + void pm_wakep_autosleep_enabled(bool set) 715 + { 716 + struct wakeup_source *ws; 717 + ktime_t now = ktime_get(); 718 + 719 + rcu_read_lock(); 720 + list_for_each_entry_rcu(ws, &wakeup_sources, entry) { 721 + spin_lock_irq(&ws->lock); 722 + if (ws->autosleep_enabled != set) { 723 + ws->autosleep_enabled = set; 724 + if (ws->active) { 725 + if (set) 726 + ws->start_prevent_time = now; 727 + else 728 + update_prevent_sleep_time(ws, now); 729 + } 730 + } 731 + spin_unlock_irq(&ws->lock); 732 + } 733 + rcu_read_unlock(); 734 + } 735 + #endif /* CONFIG_PM_AUTOSLEEP */ 739 736 740 737 static struct dentry *wakeup_sources_stats_dentry; 741 738 ··· 778 723 ktime_t max_time; 779 724 unsigned long active_count; 780 725 ktime_t active_time; 726 + ktime_t prevent_sleep_time; 781 727 int ret; 782 728 783 729 spin_lock_irqsave(&ws->lock, flags); 784 730 785 731 total_time = ws->total_time; 786 732 max_time = ws->max_time; 733 + prevent_sleep_time = ws->prevent_sleep_time; 787 734 active_count = ws->active_count; 788 735 if (ws->active) { 789 - active_time = ktime_sub(ktime_get(), ws->last_time); 736 + ktime_t now = ktime_get(); 737 + 738 + active_time = ktime_sub(now, ws->last_time); 790 739 total_time = ktime_add(total_time, active_time); 791 740 if (active_time.tv64 > max_time.tv64) 792 741 max_time = active_time; 742 + 743 + if (ws->autosleep_enabled) 744 + prevent_sleep_time = ktime_add(prevent_sleep_time, 745 + ktime_sub(now, ws->start_prevent_time)); 793 746 } else { 794 747 active_time = ktime_set(0, 0); 795 748 } 796 749 797 - ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t" 798 - "%lld\t\t%lld\t\t%lld\t\t%lld\n", 799 - ws->name, active_count, ws->event_count, ws->hit_count, 750 + ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t" 751 + "%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n", 752 + ws->name, active_count, ws->event_count, 753 + ws->wakeup_count, ws->expire_count, 800 754 ktime_to_ms(active_time), ktime_to_ms(total_time), 801 - ktime_to_ms(max_time), ktime_to_ms(ws->last_time)); 755 + ktime_to_ms(max_time), ktime_to_ms(ws->last_time), 756 + ktime_to_ms(prevent_sleep_time)); 802 757 803 758 spin_unlock_irqrestore(&ws->lock, flags); 804 759 ··· 823 758 { 824 759 struct wakeup_source *ws; 825 760 826 - seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t" 827 - "active_since\ttotal_time\tmax_time\tlast_change\n"); 761 + seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t" 762 + "expire_count\tactive_since\ttotal_time\tmax_time\t" 763 + "last_change\tprevent_suspend_time\n"); 828 764 829 765 rcu_read_lock(); 830 766 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
+87 -3
fs/eventpoll.c
··· 33 33 #include <linux/bitops.h> 34 34 #include <linux/mutex.h> 35 35 #include <linux/anon_inodes.h> 36 + #include <linux/device.h> 36 37 #include <asm/uaccess.h> 37 38 #include <asm/io.h> 38 39 #include <asm/mman.h> ··· 88 87 */ 89 88 90 89 /* Epoll private bits inside the event mask */ 91 - #define EP_PRIVATE_BITS (EPOLLONESHOT | EPOLLET) 90 + #define EP_PRIVATE_BITS (EPOLLWAKEUP | EPOLLONESHOT | EPOLLET) 92 91 93 92 /* Maximum number of nesting allowed inside epoll sets */ 94 93 #define EP_MAX_NESTS 4 ··· 155 154 /* List header used to link this item to the "struct file" items list */ 156 155 struct list_head fllink; 157 156 157 + /* wakeup_source used when EPOLLWAKEUP is set */ 158 + struct wakeup_source *ws; 159 + 158 160 /* The structure that describe the interested events and the source fd */ 159 161 struct epoll_event event; 160 162 }; ··· 197 193 * holding ->lock. 198 194 */ 199 195 struct epitem *ovflist; 196 + 197 + /* wakeup_source used when ep_scan_ready_list is running */ 198 + struct wakeup_source *ws; 200 199 201 200 /* The user that created the eventpoll descriptor */ 202 201 struct user_struct *user; ··· 595 588 * queued into ->ovflist but the "txlist" might already 596 589 * contain them, and the list_splice() below takes care of them. 597 590 */ 598 - if (!ep_is_linked(&epi->rdllink)) 591 + if (!ep_is_linked(&epi->rdllink)) { 599 592 list_add_tail(&epi->rdllink, &ep->rdllist); 593 + __pm_stay_awake(epi->ws); 594 + } 600 595 } 601 596 /* 602 597 * We need to set back ep->ovflist to EP_UNACTIVE_PTR, so that after ··· 611 602 * Quickly re-inject items left on "txlist". 612 603 */ 613 604 list_splice(&txlist, &ep->rdllist); 605 + __pm_relax(ep->ws); 614 606 615 607 if (!list_empty(&ep->rdllist)) { 616 608 /* ··· 666 656 list_del_init(&epi->rdllink); 667 657 spin_unlock_irqrestore(&ep->lock, flags); 668 658 659 + wakeup_source_unregister(epi->ws); 660 + 669 661 /* At this point it is safe to free the eventpoll item */ 670 662 kmem_cache_free(epi_cache, epi); 671 663 ··· 718 706 mutex_unlock(&epmutex); 719 707 mutex_destroy(&ep->mtx); 720 708 free_uid(ep->user); 709 + wakeup_source_unregister(ep->ws); 721 710 kfree(ep); 722 711 } 723 712 ··· 750 737 * callback, but it's not actually ready, as far as 751 738 * caller requested events goes. We can remove it here. 752 739 */ 740 + __pm_relax(epi->ws); 753 741 list_del_init(&epi->rdllink); 754 742 } 755 743 } ··· 941 927 if (epi->next == EP_UNACTIVE_PTR) { 942 928 epi->next = ep->ovflist; 943 929 ep->ovflist = epi; 930 + if (epi->ws) { 931 + /* 932 + * Activate ep->ws since epi->ws may get 933 + * deactivated at any time. 934 + */ 935 + __pm_stay_awake(ep->ws); 936 + } 937 + 944 938 } 945 939 goto out_unlock; 946 940 } 947 941 948 942 /* If this file is already in the ready list we exit soon */ 949 - if (!ep_is_linked(&epi->rdllink)) 943 + if (!ep_is_linked(&epi->rdllink)) { 950 944 list_add_tail(&epi->rdllink, &ep->rdllist); 945 + __pm_stay_awake(epi->ws); 946 + } 951 947 952 948 /* 953 949 * Wake up ( if active ) both the eventpoll wait list and the ->poll() ··· 1115 1091 return error; 1116 1092 } 1117 1093 1094 + static int ep_create_wakeup_source(struct epitem *epi) 1095 + { 1096 + const char *name; 1097 + 1098 + if (!epi->ep->ws) { 1099 + epi->ep->ws = wakeup_source_register("eventpoll"); 1100 + if (!epi->ep->ws) 1101 + return -ENOMEM; 1102 + } 1103 + 1104 + name = epi->ffd.file->f_path.dentry->d_name.name; 1105 + epi->ws = wakeup_source_register(name); 1106 + if (!epi->ws) 1107 + return -ENOMEM; 1108 + 1109 + return 0; 1110 + } 1111 + 1112 + static void ep_destroy_wakeup_source(struct epitem *epi) 1113 + { 1114 + wakeup_source_unregister(epi->ws); 1115 + epi->ws = NULL; 1116 + } 1117 + 1118 1118 /* 1119 1119 * Must be called with "mtx" held. 1120 1120 */ ··· 1166 1118 epi->event = *event; 1167 1119 epi->nwait = 0; 1168 1120 epi->next = EP_UNACTIVE_PTR; 1121 + if (epi->event.events & EPOLLWAKEUP) { 1122 + error = ep_create_wakeup_source(epi); 1123 + if (error) 1124 + goto error_create_wakeup_source; 1125 + } else { 1126 + epi->ws = NULL; 1127 + } 1169 1128 1170 1129 /* Initialize the poll table using the queue callback */ 1171 1130 epq.epi = epi; ··· 1219 1164 /* If the file is already "ready" we drop it inside the ready list */ 1220 1165 if ((revents & event->events) && !ep_is_linked(&epi->rdllink)) { 1221 1166 list_add_tail(&epi->rdllink, &ep->rdllist); 1167 + __pm_stay_awake(epi->ws); 1222 1168 1223 1169 /* Notify waiting tasks that events are available */ 1224 1170 if (waitqueue_active(&ep->wq)) ··· 1260 1204 list_del_init(&epi->rdllink); 1261 1205 spin_unlock_irqrestore(&ep->lock, flags); 1262 1206 1207 + wakeup_source_unregister(epi->ws); 1208 + 1209 + error_create_wakeup_source: 1263 1210 kmem_cache_free(epi_cache, epi); 1264 1211 1265 1212 return error; ··· 1288 1229 epi->event.events = event->events; 1289 1230 pt._key = event->events; 1290 1231 epi->event.data = event->data; /* protected by mtx */ 1232 + if (epi->event.events & EPOLLWAKEUP) { 1233 + if (!epi->ws) 1234 + ep_create_wakeup_source(epi); 1235 + } else if (epi->ws) { 1236 + ep_destroy_wakeup_source(epi); 1237 + } 1291 1238 1292 1239 /* 1293 1240 * Get current event bits. We can safely use the file* here because ··· 1309 1244 spin_lock_irq(&ep->lock); 1310 1245 if (!ep_is_linked(&epi->rdllink)) { 1311 1246 list_add_tail(&epi->rdllink, &ep->rdllist); 1247 + __pm_stay_awake(epi->ws); 1312 1248 1313 1249 /* Notify waiting tasks that events are available */ 1314 1250 if (waitqueue_active(&ep->wq)) ··· 1348 1282 !list_empty(head) && eventcnt < esed->maxevents;) { 1349 1283 epi = list_first_entry(head, struct epitem, rdllink); 1350 1284 1285 + /* 1286 + * Activate ep->ws before deactivating epi->ws to prevent 1287 + * triggering auto-suspend here (in case we reactive epi->ws 1288 + * below). 1289 + * 1290 + * This could be rearranged to delay the deactivation of epi->ws 1291 + * instead, but then epi->ws would temporarily be out of sync 1292 + * with ep_is_linked(). 1293 + */ 1294 + if (epi->ws && epi->ws->active) 1295 + __pm_stay_awake(ep->ws); 1296 + __pm_relax(epi->ws); 1351 1297 list_del_init(&epi->rdllink); 1352 1298 1353 1299 pt._key = epi->event.events; ··· 1376 1298 if (__put_user(revents, &uevent->events) || 1377 1299 __put_user(epi->event.data, &uevent->data)) { 1378 1300 list_add(&epi->rdllink, head); 1301 + __pm_stay_awake(epi->ws); 1379 1302 return eventcnt ? eventcnt : -EFAULT; 1380 1303 } 1381 1304 eventcnt++; ··· 1396 1317 * poll callback will queue them in ep->ovflist. 1397 1318 */ 1398 1319 list_add_tail(&epi->rdllink, &ep->rdllist); 1320 + __pm_stay_awake(epi->ws); 1399 1321 } 1400 1322 } 1401 1323 } ··· 1707 1627 /* The target file descriptor must support poll */ 1708 1628 error = -EPERM; 1709 1629 if (!tfile->f_op || !tfile->f_op->poll) 1630 + goto error_tgt_fput; 1631 + 1632 + /* Check if EPOLLWAKEUP is allowed */ 1633 + if ((epds.events & EPOLLWAKEUP) && !capable(CAP_EPOLLWAKEUP)) 1710 1634 goto error_tgt_fput; 1711 1635 1712 1636 /*
+4 -1
include/linux/capability.h
··· 360 360 361 361 #define CAP_WAKE_ALARM 35 362 362 363 + /* Allow preventing system suspends while epoll events are pending */ 363 364 364 - #define CAP_LAST_CAP CAP_WAKE_ALARM 365 + #define CAP_EPOLLWAKEUP 36 366 + 367 + #define CAP_LAST_CAP CAP_EPOLLWAKEUP 365 368 366 369 #define cap_valid(x) ((x) >= 0 && (x) <= CAP_LAST_CAP) 367 370
+12
include/linux/eventpoll.h
··· 26 26 #define EPOLL_CTL_DEL 2 27 27 #define EPOLL_CTL_MOD 3 28 28 29 + /* 30 + * Request the handling of system wakeup events so as to prevent system suspends 31 + * from happening while those events are being processed. 32 + * 33 + * Assuming neither EPOLLET nor EPOLLONESHOT is set, system suspends will not be 34 + * re-allowed until epoll_wait is called again after consuming the wakeup 35 + * event(s). 36 + * 37 + * Requires CAP_EPOLLWAKEUP 38 + */ 39 + #define EPOLLWAKEUP (1 << 29) 40 + 29 41 /* Set the One Shot behaviour for the target file descriptor */ 30 42 #define EPOLLONESHOT (1 << 30) 31 43
+11 -4
include/linux/pm_wakeup.h
··· 33 33 * 34 34 * @total_time: Total time this wakeup source has been active. 35 35 * @max_time: Maximum time this wakeup source has been continuously active. 36 - * @last_time: Monotonic clock when the wakeup source's was activated last time. 36 + * @last_time: Monotonic clock when the wakeup source's was touched last time. 37 + * @prevent_sleep_time: Total time this source has been preventing autosleep. 37 38 * @event_count: Number of signaled wakeup events. 38 39 * @active_count: Number of times the wakeup sorce was activated. 39 40 * @relax_count: Number of times the wakeup sorce was deactivated. 40 - * @hit_count: Number of times the wakeup sorce might abort system suspend. 41 + * @expire_count: Number of times the wakeup source's timeout has expired. 42 + * @wakeup_count: Number of times the wakeup source might abort suspend. 41 43 * @active: Status of the wakeup source. 44 + * @has_timeout: The wakeup source has been activated with a timeout. 42 45 */ 43 46 struct wakeup_source { 44 47 const char *name; ··· 52 49 ktime_t total_time; 53 50 ktime_t max_time; 54 51 ktime_t last_time; 52 + ktime_t start_prevent_time; 53 + ktime_t prevent_sleep_time; 55 54 unsigned long event_count; 56 55 unsigned long active_count; 57 56 unsigned long relax_count; 58 - unsigned long hit_count; 59 - unsigned int active:1; 57 + unsigned long expire_count; 58 + unsigned long wakeup_count; 59 + bool active:1; 60 + bool autosleep_enabled:1; 60 61 }; 61 62 62 63 #ifdef CONFIG_PM_SLEEP
+13 -1
include/linux/suspend.h
··· 356 356 extern bool events_check_enabled; 357 357 358 358 extern bool pm_wakeup_pending(void); 359 - extern bool pm_get_wakeup_count(unsigned int *count); 359 + extern bool pm_get_wakeup_count(unsigned int *count, bool block); 360 360 extern bool pm_save_wakeup_count(unsigned int count); 361 + extern void pm_wakep_autosleep_enabled(bool set); 361 362 362 363 static inline void lock_system_sleep(void) 363 364 { ··· 407 406 static inline void unlock_system_sleep(void) {} 408 407 409 408 #endif /* !CONFIG_PM_SLEEP */ 409 + 410 + #ifdef CONFIG_PM_AUTOSLEEP 411 + 412 + /* kernel/power/autosleep.c */ 413 + void queue_up_suspend_work(void); 414 + 415 + #else /* !CONFIG_PM_AUTOSLEEP */ 416 + 417 + static inline void queue_up_suspend_work(void) {} 418 + 419 + #endif /* !CONFIG_PM_AUTOSLEEP */ 410 420 411 421 #ifdef CONFIG_ARCH_SAVE_PAGE_KEYS 412 422 /*
+34
include/trace/events/power.h
··· 65 65 TP_printk("state=%lu", (unsigned long)__entry->state) 66 66 ); 67 67 68 + DECLARE_EVENT_CLASS(wakeup_source, 69 + 70 + TP_PROTO(const char *name, unsigned int state), 71 + 72 + TP_ARGS(name, state), 73 + 74 + TP_STRUCT__entry( 75 + __string( name, name ) 76 + __field( u64, state ) 77 + ), 78 + 79 + TP_fast_assign( 80 + __assign_str(name, name); 81 + __entry->state = state; 82 + ), 83 + 84 + TP_printk("%s state=0x%lx", __get_str(name), 85 + (unsigned long)__entry->state) 86 + ); 87 + 88 + DEFINE_EVENT(wakeup_source, wakeup_source_activate, 89 + 90 + TP_PROTO(const char *name, unsigned int state), 91 + 92 + TP_ARGS(name, state) 93 + ); 94 + 95 + DEFINE_EVENT(wakeup_source, wakeup_source_deactivate, 96 + 97 + TP_PROTO(const char *name, unsigned int state), 98 + 99 + TP_ARGS(name, state) 100 + ); 101 + 68 102 #ifdef CONFIG_EVENT_POWER_TRACING_DEPRECATED 69 103 70 104 /*
+27
kernel/power/Kconfig
··· 103 103 select HOTPLUG 104 104 select HOTPLUG_CPU 105 105 106 + config PM_AUTOSLEEP 107 + bool "Opportunistic sleep" 108 + depends on PM_SLEEP 109 + default n 110 + ---help--- 111 + Allow the kernel to trigger a system transition into a global sleep 112 + state automatically whenever there are no active wakeup sources. 113 + 114 + config PM_WAKELOCKS 115 + bool "User space wakeup sources interface" 116 + depends on PM_SLEEP 117 + default n 118 + ---help--- 119 + Allow user space to create, activate and deactivate wakeup source 120 + objects with the help of a sysfs-based interface. 121 + 122 + config PM_WAKELOCKS_LIMIT 123 + int "Maximum number of user space wakeup sources (0 = no limit)" 124 + range 0 100000 125 + default 100 126 + depends on PM_WAKELOCKS 127 + 128 + config PM_WAKELOCKS_GC 129 + bool "Garbage collector for user space wakeup sources" 130 + depends on PM_WAKELOCKS 131 + default y 132 + 106 133 config PM_RUNTIME 107 134 bool "Run-time PM core functionality" 108 135 depends on !IA64_HP_SIM
+2
kernel/power/Makefile
··· 9 9 obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o 10 10 obj-$(CONFIG_HIBERNATION) += hibernate.o snapshot.o swap.o user.o \ 11 11 block_io.o 12 + obj-$(CONFIG_PM_AUTOSLEEP) += autosleep.o 13 + obj-$(CONFIG_PM_WAKELOCKS) += wakelock.o 12 14 13 15 obj-$(CONFIG_MAGIC_SYSRQ) += poweroff.o
+127
kernel/power/autosleep.c
··· 1 + /* 2 + * kernel/power/autosleep.c 3 + * 4 + * Opportunistic sleep support. 5 + * 6 + * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl> 7 + */ 8 + 9 + #include <linux/device.h> 10 + #include <linux/mutex.h> 11 + #include <linux/pm_wakeup.h> 12 + 13 + #include "power.h" 14 + 15 + static suspend_state_t autosleep_state; 16 + static struct workqueue_struct *autosleep_wq; 17 + /* 18 + * Note: it is only safe to mutex_lock(&autosleep_lock) if a wakeup_source 19 + * is active, otherwise a deadlock with try_to_suspend() is possible. 20 + * Alternatively mutex_lock_interruptible() can be used. This will then fail 21 + * if an auto_sleep cycle tries to freeze processes. 22 + */ 23 + static DEFINE_MUTEX(autosleep_lock); 24 + static struct wakeup_source *autosleep_ws; 25 + 26 + static void try_to_suspend(struct work_struct *work) 27 + { 28 + unsigned int initial_count, final_count; 29 + 30 + if (!pm_get_wakeup_count(&initial_count, true)) 31 + goto out; 32 + 33 + mutex_lock(&autosleep_lock); 34 + 35 + if (!pm_save_wakeup_count(initial_count)) { 36 + mutex_unlock(&autosleep_lock); 37 + goto out; 38 + } 39 + 40 + if (autosleep_state == PM_SUSPEND_ON) { 41 + mutex_unlock(&autosleep_lock); 42 + return; 43 + } 44 + if (autosleep_state >= PM_SUSPEND_MAX) 45 + hibernate(); 46 + else 47 + pm_suspend(autosleep_state); 48 + 49 + mutex_unlock(&autosleep_lock); 50 + 51 + if (!pm_get_wakeup_count(&final_count, false)) 52 + goto out; 53 + 54 + /* 55 + * If the wakeup occured for an unknown reason, wait to prevent the 56 + * system from trying to suspend and waking up in a tight loop. 57 + */ 58 + if (final_count == initial_count) 59 + schedule_timeout_uninterruptible(HZ / 2); 60 + 61 + out: 62 + queue_up_suspend_work(); 63 + } 64 + 65 + static DECLARE_WORK(suspend_work, try_to_suspend); 66 + 67 + void queue_up_suspend_work(void) 68 + { 69 + if (!work_pending(&suspend_work) && autosleep_state > PM_SUSPEND_ON) 70 + queue_work(autosleep_wq, &suspend_work); 71 + } 72 + 73 + suspend_state_t pm_autosleep_state(void) 74 + { 75 + return autosleep_state; 76 + } 77 + 78 + int pm_autosleep_lock(void) 79 + { 80 + return mutex_lock_interruptible(&autosleep_lock); 81 + } 82 + 83 + void pm_autosleep_unlock(void) 84 + { 85 + mutex_unlock(&autosleep_lock); 86 + } 87 + 88 + int pm_autosleep_set_state(suspend_state_t state) 89 + { 90 + 91 + #ifndef CONFIG_HIBERNATION 92 + if (state >= PM_SUSPEND_MAX) 93 + return -EINVAL; 94 + #endif 95 + 96 + __pm_stay_awake(autosleep_ws); 97 + 98 + mutex_lock(&autosleep_lock); 99 + 100 + autosleep_state = state; 101 + 102 + __pm_relax(autosleep_ws); 103 + 104 + if (state > PM_SUSPEND_ON) { 105 + pm_wakep_autosleep_enabled(true); 106 + queue_up_suspend_work(); 107 + } else { 108 + pm_wakep_autosleep_enabled(false); 109 + } 110 + 111 + mutex_unlock(&autosleep_lock); 112 + return 0; 113 + } 114 + 115 + int __init pm_autosleep_init(void) 116 + { 117 + autosleep_ws = wakeup_source_register("autosleep"); 118 + if (!autosleep_ws) 119 + return -ENOMEM; 120 + 121 + autosleep_wq = alloc_ordered_workqueue("autosleep", 0); 122 + if (autosleep_wq) 123 + return 0; 124 + 125 + wakeup_source_unregister(autosleep_ws); 126 + return -ENOMEM; 127 + }
+141 -19
kernel/power/main.c
··· 269 269 return (s - buf); 270 270 } 271 271 272 - static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, 273 - const char *buf, size_t n) 272 + static suspend_state_t decode_state(const char *buf, size_t n) 274 273 { 275 274 #ifdef CONFIG_SUSPEND 276 275 suspend_state_t state = PM_SUSPEND_STANDBY; ··· 277 278 #endif 278 279 char *p; 279 280 int len; 280 - int error = -EINVAL; 281 281 282 282 p = memchr(buf, '\n', n); 283 283 len = p ? p - buf : n; 284 284 285 - /* First, check if we are requested to hibernate */ 286 - if (len == 4 && !strncmp(buf, "disk", len)) { 287 - error = hibernate(); 288 - goto Exit; 289 - } 285 + /* Check hibernation first. */ 286 + if (len == 4 && !strncmp(buf, "disk", len)) 287 + return PM_SUSPEND_MAX; 290 288 291 289 #ifdef CONFIG_SUSPEND 292 - for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { 293 - if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { 294 - error = pm_suspend(state); 295 - break; 296 - } 297 - } 290 + for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) 291 + if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) 292 + return state; 298 293 #endif 299 294 300 - Exit: 295 + return PM_SUSPEND_ON; 296 + } 297 + 298 + static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, 299 + const char *buf, size_t n) 300 + { 301 + suspend_state_t state; 302 + int error; 303 + 304 + error = pm_autosleep_lock(); 305 + if (error) 306 + return error; 307 + 308 + if (pm_autosleep_state() > PM_SUSPEND_ON) { 309 + error = -EBUSY; 310 + goto out; 311 + } 312 + 313 + state = decode_state(buf, n); 314 + if (state < PM_SUSPEND_MAX) 315 + error = pm_suspend(state); 316 + else if (state == PM_SUSPEND_MAX) 317 + error = hibernate(); 318 + else 319 + error = -EINVAL; 320 + 321 + out: 322 + pm_autosleep_unlock(); 301 323 return error ? error : n; 302 324 } 303 325 ··· 359 339 { 360 340 unsigned int val; 361 341 362 - return pm_get_wakeup_count(&val) ? sprintf(buf, "%u\n", val) : -EINTR; 342 + return pm_get_wakeup_count(&val, true) ? 343 + sprintf(buf, "%u\n", val) : -EINTR; 363 344 } 364 345 365 346 static ssize_t wakeup_count_store(struct kobject *kobj, ··· 368 347 const char *buf, size_t n) 369 348 { 370 349 unsigned int val; 350 + int error; 371 351 352 + error = pm_autosleep_lock(); 353 + if (error) 354 + return error; 355 + 356 + if (pm_autosleep_state() > PM_SUSPEND_ON) { 357 + error = -EBUSY; 358 + goto out; 359 + } 360 + 361 + error = -EINVAL; 372 362 if (sscanf(buf, "%u", &val) == 1) { 373 363 if (pm_save_wakeup_count(val)) 374 - return n; 364 + error = n; 375 365 } 376 - return -EINVAL; 366 + 367 + out: 368 + pm_autosleep_unlock(); 369 + return error; 377 370 } 378 371 379 372 power_attr(wakeup_count); 373 + 374 + #ifdef CONFIG_PM_AUTOSLEEP 375 + static ssize_t autosleep_show(struct kobject *kobj, 376 + struct kobj_attribute *attr, 377 + char *buf) 378 + { 379 + suspend_state_t state = pm_autosleep_state(); 380 + 381 + if (state == PM_SUSPEND_ON) 382 + return sprintf(buf, "off\n"); 383 + 384 + #ifdef CONFIG_SUSPEND 385 + if (state < PM_SUSPEND_MAX) 386 + return sprintf(buf, "%s\n", valid_state(state) ? 387 + pm_states[state] : "error"); 388 + #endif 389 + #ifdef CONFIG_HIBERNATION 390 + return sprintf(buf, "disk\n"); 391 + #else 392 + return sprintf(buf, "error"); 393 + #endif 394 + } 395 + 396 + static ssize_t autosleep_store(struct kobject *kobj, 397 + struct kobj_attribute *attr, 398 + const char *buf, size_t n) 399 + { 400 + suspend_state_t state = decode_state(buf, n); 401 + int error; 402 + 403 + if (state == PM_SUSPEND_ON 404 + && strcmp(buf, "off") && strcmp(buf, "off\n")) 405 + return -EINVAL; 406 + 407 + error = pm_autosleep_set_state(state); 408 + return error ? error : n; 409 + } 410 + 411 + power_attr(autosleep); 412 + #endif /* CONFIG_PM_AUTOSLEEP */ 413 + 414 + #ifdef CONFIG_PM_WAKELOCKS 415 + static ssize_t wake_lock_show(struct kobject *kobj, 416 + struct kobj_attribute *attr, 417 + char *buf) 418 + { 419 + return pm_show_wakelocks(buf, true); 420 + } 421 + 422 + static ssize_t wake_lock_store(struct kobject *kobj, 423 + struct kobj_attribute *attr, 424 + const char *buf, size_t n) 425 + { 426 + int error = pm_wake_lock(buf); 427 + return error ? error : n; 428 + } 429 + 430 + power_attr(wake_lock); 431 + 432 + static ssize_t wake_unlock_show(struct kobject *kobj, 433 + struct kobj_attribute *attr, 434 + char *buf) 435 + { 436 + return pm_show_wakelocks(buf, false); 437 + } 438 + 439 + static ssize_t wake_unlock_store(struct kobject *kobj, 440 + struct kobj_attribute *attr, 441 + const char *buf, size_t n) 442 + { 443 + int error = pm_wake_unlock(buf); 444 + return error ? error : n; 445 + } 446 + 447 + power_attr(wake_unlock); 448 + 449 + #endif /* CONFIG_PM_WAKELOCKS */ 380 450 #endif /* CONFIG_PM_SLEEP */ 381 451 382 452 #ifdef CONFIG_PM_TRACE ··· 521 409 #ifdef CONFIG_PM_SLEEP 522 410 &pm_async_attr.attr, 523 411 &wakeup_count_attr.attr, 412 + #ifdef CONFIG_PM_AUTOSLEEP 413 + &autosleep_attr.attr, 414 + #endif 415 + #ifdef CONFIG_PM_WAKELOCKS 416 + &wake_lock_attr.attr, 417 + &wake_unlock_attr.attr, 418 + #endif 524 419 #ifdef CONFIG_PM_DEBUG 525 420 &pm_test_attr.attr, 526 421 #endif ··· 563 444 power_kobj = kobject_create_and_add("power", NULL); 564 445 if (!power_kobj) 565 446 return -ENOMEM; 566 - return sysfs_create_group(power_kobj, &attr_group); 447 + error = sysfs_create_group(power_kobj, &attr_group); 448 + if (error) 449 + return error; 450 + return pm_autosleep_init(); 567 451 } 568 452 569 453 core_initcall(pm_init);
+27
kernel/power/power.h
··· 264 264 { 265 265 } 266 266 #endif 267 + 268 + #ifdef CONFIG_PM_AUTOSLEEP 269 + 270 + /* kernel/power/autosleep.c */ 271 + extern int pm_autosleep_init(void); 272 + extern int pm_autosleep_lock(void); 273 + extern void pm_autosleep_unlock(void); 274 + extern suspend_state_t pm_autosleep_state(void); 275 + extern int pm_autosleep_set_state(suspend_state_t state); 276 + 277 + #else /* !CONFIG_PM_AUTOSLEEP */ 278 + 279 + static inline int pm_autosleep_init(void) { return 0; } 280 + static inline int pm_autosleep_lock(void) { return 0; } 281 + static inline void pm_autosleep_unlock(void) {} 282 + static inline suspend_state_t pm_autosleep_state(void) { return PM_SUSPEND_ON; } 283 + 284 + #endif /* !CONFIG_PM_AUTOSLEEP */ 285 + 286 + #ifdef CONFIG_PM_WAKELOCKS 287 + 288 + /* kernel/power/wakelock.c */ 289 + extern ssize_t pm_show_wakelocks(char *buf, bool show_active); 290 + extern int pm_wake_lock(const char *buf); 291 + extern int pm_wake_unlock(const char *buf); 292 + 293 + #endif /* !CONFIG_PM_WAKELOCKS */
+39 -23
kernel/power/swap.c
··· 6 6 * 7 7 * Copyright (C) 1998,2001-2005 Pavel Machek <pavel@ucw.cz> 8 8 * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl> 9 - * Copyright (C) 2010 Bojan Smojver <bojan@rexursive.com> 9 + * Copyright (C) 2010-2012 Bojan Smojver <bojan@rexursive.com> 10 10 * 11 11 * This file is released under the GPLv2. 12 12 * ··· 282 282 return -ENOSPC; 283 283 284 284 if (bio_chain) { 285 - src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH); 285 + src = (void *)__get_free_page(__GFP_WAIT | __GFP_NOWARN | 286 + __GFP_NORETRY); 286 287 if (src) { 287 288 copy_page(src, buf); 288 289 } else { 289 290 ret = hib_wait_on_bio_chain(bio_chain); /* Free pages */ 290 291 if (ret) 291 292 return ret; 292 - src = (void *)__get_free_page(__GFP_WAIT | __GFP_HIGH); 293 + src = (void *)__get_free_page(__GFP_WAIT | 294 + __GFP_NOWARN | 295 + __GFP_NORETRY); 293 296 if (src) { 294 297 copy_page(src, buf); 295 298 } else { ··· 370 367 clear_page(handle->cur); 371 368 handle->cur_swap = offset; 372 369 handle->k = 0; 373 - } 374 - if (bio_chain && low_free_pages() <= handle->reqd_free_pages) { 375 - error = hib_wait_on_bio_chain(bio_chain); 376 - if (error) 377 - goto out; 378 - handle->reqd_free_pages = reqd_free_pages(); 370 + 371 + if (bio_chain && low_free_pages() <= handle->reqd_free_pages) { 372 + error = hib_wait_on_bio_chain(bio_chain); 373 + if (error) 374 + goto out; 375 + /* 376 + * Recalculate the number of required free pages, to 377 + * make sure we never take more than half. 378 + */ 379 + handle->reqd_free_pages = reqd_free_pages(); 380 + } 379 381 } 380 382 out: 381 383 return error; ··· 427 419 /* Maximum number of threads for compression/decompression. */ 428 420 #define LZO_THREADS 3 429 421 430 - /* Maximum number of pages for read buffering. */ 431 - #define LZO_READ_PAGES (MAP_PAGE_ENTRIES * 8) 422 + /* Minimum/maximum number of pages for read buffering. */ 423 + #define LZO_MIN_RD_PAGES 1024 424 + #define LZO_MAX_RD_PAGES 8192 432 425 433 426 434 427 /** ··· 640 631 } 641 632 642 633 /* 643 - * Adjust number of free pages after all allocations have been done. 644 - * We don't want to run out of pages when writing. 645 - */ 646 - handle->reqd_free_pages = reqd_free_pages(); 647 - 648 - /* 649 634 * Start the CRC32 thread. 650 635 */ 651 636 init_waitqueue_head(&crc->go); ··· 659 656 ret = -ENOMEM; 660 657 goto out_clean; 661 658 } 659 + 660 + /* 661 + * Adjust the number of required free pages after all allocations have 662 + * been done. We don't want to run out of pages when writing. 663 + */ 664 + handle->reqd_free_pages = reqd_free_pages(); 662 665 663 666 printk(KERN_INFO 664 667 "PM: Using %u thread(s) for compression.\n" ··· 1076 1067 unsigned i, thr, run_threads, nr_threads; 1077 1068 unsigned ring = 0, pg = 0, ring_size = 0, 1078 1069 have = 0, want, need, asked = 0; 1079 - unsigned long read_pages; 1070 + unsigned long read_pages = 0; 1080 1071 unsigned char **page = NULL; 1081 1072 struct dec_data *data = NULL; 1082 1073 struct crc_data *crc = NULL; ··· 1088 1079 nr_threads = num_online_cpus() - 1; 1089 1080 nr_threads = clamp_val(nr_threads, 1, LZO_THREADS); 1090 1081 1091 - page = vmalloc(sizeof(*page) * LZO_READ_PAGES); 1082 + page = vmalloc(sizeof(*page) * LZO_MAX_RD_PAGES); 1092 1083 if (!page) { 1093 1084 printk(KERN_ERR "PM: Failed to allocate LZO page\n"); 1094 1085 ret = -ENOMEM; ··· 1153 1144 } 1154 1145 1155 1146 /* 1156 - * Adjust number of pages for read buffering, in case we are short. 1147 + * Set the number of pages for read buffering. 1148 + * This is complete guesswork, because we'll only know the real 1149 + * picture once prepare_image() is called, which is much later on 1150 + * during the image load phase. We'll assume the worst case and 1151 + * say that none of the image pages are from high memory. 1157 1152 */ 1158 - read_pages = (nr_free_pages() - snapshot_get_image_size()) >> 1; 1159 - read_pages = clamp_val(read_pages, LZO_CMP_PAGES, LZO_READ_PAGES); 1153 + if (low_free_pages() > snapshot_get_image_size()) 1154 + read_pages = (low_free_pages() - snapshot_get_image_size()) / 2; 1155 + read_pages = clamp_val(read_pages, LZO_MIN_RD_PAGES, LZO_MAX_RD_PAGES); 1160 1156 1161 1157 for (i = 0; i < read_pages; i++) { 1162 1158 page[i] = (void *)__get_free_page(i < LZO_CMP_PAGES ? 1163 1159 __GFP_WAIT | __GFP_HIGH : 1164 - __GFP_WAIT); 1160 + __GFP_WAIT | __GFP_NOWARN | 1161 + __GFP_NORETRY); 1162 + 1165 1163 if (!page[i]) { 1166 1164 if (i < LZO_CMP_PAGES) { 1167 1165 ring_size = i;
+259
kernel/power/wakelock.c
··· 1 + /* 2 + * kernel/power/wakelock.c 3 + * 4 + * User space wakeup sources support. 5 + * 6 + * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl> 7 + * 8 + * This code is based on the analogous interface allowing user space to 9 + * manipulate wakelocks on Android. 10 + */ 11 + 12 + #include <linux/ctype.h> 13 + #include <linux/device.h> 14 + #include <linux/err.h> 15 + #include <linux/hrtimer.h> 16 + #include <linux/list.h> 17 + #include <linux/rbtree.h> 18 + #include <linux/slab.h> 19 + 20 + static DEFINE_MUTEX(wakelocks_lock); 21 + 22 + struct wakelock { 23 + char *name; 24 + struct rb_node node; 25 + struct wakeup_source ws; 26 + #ifdef CONFIG_PM_WAKELOCKS_GC 27 + struct list_head lru; 28 + #endif 29 + }; 30 + 31 + static struct rb_root wakelocks_tree = RB_ROOT; 32 + 33 + ssize_t pm_show_wakelocks(char *buf, bool show_active) 34 + { 35 + struct rb_node *node; 36 + struct wakelock *wl; 37 + char *str = buf; 38 + char *end = buf + PAGE_SIZE; 39 + 40 + mutex_lock(&wakelocks_lock); 41 + 42 + for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) { 43 + wl = rb_entry(node, struct wakelock, node); 44 + if (wl->ws.active == show_active) 45 + str += scnprintf(str, end - str, "%s ", wl->name); 46 + } 47 + if (str > buf) 48 + str--; 49 + 50 + str += scnprintf(str, end - str, "\n"); 51 + 52 + mutex_unlock(&wakelocks_lock); 53 + return (str - buf); 54 + } 55 + 56 + #if CONFIG_PM_WAKELOCKS_LIMIT > 0 57 + static unsigned int number_of_wakelocks; 58 + 59 + static inline bool wakelocks_limit_exceeded(void) 60 + { 61 + return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT; 62 + } 63 + 64 + static inline void increment_wakelocks_number(void) 65 + { 66 + number_of_wakelocks++; 67 + } 68 + 69 + static inline void decrement_wakelocks_number(void) 70 + { 71 + number_of_wakelocks--; 72 + } 73 + #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */ 74 + static inline bool wakelocks_limit_exceeded(void) { return false; } 75 + static inline void increment_wakelocks_number(void) {} 76 + static inline void decrement_wakelocks_number(void) {} 77 + #endif /* CONFIG_PM_WAKELOCKS_LIMIT */ 78 + 79 + #ifdef CONFIG_PM_WAKELOCKS_GC 80 + #define WL_GC_COUNT_MAX 100 81 + #define WL_GC_TIME_SEC 300 82 + 83 + static LIST_HEAD(wakelocks_lru_list); 84 + static unsigned int wakelocks_gc_count; 85 + 86 + static inline void wakelocks_lru_add(struct wakelock *wl) 87 + { 88 + list_add(&wl->lru, &wakelocks_lru_list); 89 + } 90 + 91 + static inline void wakelocks_lru_most_recent(struct wakelock *wl) 92 + { 93 + list_move(&wl->lru, &wakelocks_lru_list); 94 + } 95 + 96 + static void wakelocks_gc(void) 97 + { 98 + struct wakelock *wl, *aux; 99 + ktime_t now; 100 + 101 + if (++wakelocks_gc_count <= WL_GC_COUNT_MAX) 102 + return; 103 + 104 + now = ktime_get(); 105 + list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) { 106 + u64 idle_time_ns; 107 + bool active; 108 + 109 + spin_lock_irq(&wl->ws.lock); 110 + idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws.last_time)); 111 + active = wl->ws.active; 112 + spin_unlock_irq(&wl->ws.lock); 113 + 114 + if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC)) 115 + break; 116 + 117 + if (!active) { 118 + wakeup_source_remove(&wl->ws); 119 + rb_erase(&wl->node, &wakelocks_tree); 120 + list_del(&wl->lru); 121 + kfree(wl->name); 122 + kfree(wl); 123 + decrement_wakelocks_number(); 124 + } 125 + } 126 + wakelocks_gc_count = 0; 127 + } 128 + #else /* !CONFIG_PM_WAKELOCKS_GC */ 129 + static inline void wakelocks_lru_add(struct wakelock *wl) {} 130 + static inline void wakelocks_lru_most_recent(struct wakelock *wl) {} 131 + static inline void wakelocks_gc(void) {} 132 + #endif /* !CONFIG_PM_WAKELOCKS_GC */ 133 + 134 + static struct wakelock *wakelock_lookup_add(const char *name, size_t len, 135 + bool add_if_not_found) 136 + { 137 + struct rb_node **node = &wakelocks_tree.rb_node; 138 + struct rb_node *parent = *node; 139 + struct wakelock *wl; 140 + 141 + while (*node) { 142 + int diff; 143 + 144 + parent = *node; 145 + wl = rb_entry(*node, struct wakelock, node); 146 + diff = strncmp(name, wl->name, len); 147 + if (diff == 0) { 148 + if (wl->name[len]) 149 + diff = -1; 150 + else 151 + return wl; 152 + } 153 + if (diff < 0) 154 + node = &(*node)->rb_left; 155 + else 156 + node = &(*node)->rb_right; 157 + } 158 + if (!add_if_not_found) 159 + return ERR_PTR(-EINVAL); 160 + 161 + if (wakelocks_limit_exceeded()) 162 + return ERR_PTR(-ENOSPC); 163 + 164 + /* Not found, we have to add a new one. */ 165 + wl = kzalloc(sizeof(*wl), GFP_KERNEL); 166 + if (!wl) 167 + return ERR_PTR(-ENOMEM); 168 + 169 + wl->name = kstrndup(name, len, GFP_KERNEL); 170 + if (!wl->name) { 171 + kfree(wl); 172 + return ERR_PTR(-ENOMEM); 173 + } 174 + wl->ws.name = wl->name; 175 + wakeup_source_add(&wl->ws); 176 + rb_link_node(&wl->node, parent, node); 177 + rb_insert_color(&wl->node, &wakelocks_tree); 178 + wakelocks_lru_add(wl); 179 + increment_wakelocks_number(); 180 + return wl; 181 + } 182 + 183 + int pm_wake_lock(const char *buf) 184 + { 185 + const char *str = buf; 186 + struct wakelock *wl; 187 + u64 timeout_ns = 0; 188 + size_t len; 189 + int ret = 0; 190 + 191 + while (*str && !isspace(*str)) 192 + str++; 193 + 194 + len = str - buf; 195 + if (!len) 196 + return -EINVAL; 197 + 198 + if (*str && *str != '\n') { 199 + /* Find out if there's a valid timeout string appended. */ 200 + ret = kstrtou64(skip_spaces(str), 10, &timeout_ns); 201 + if (ret) 202 + return -EINVAL; 203 + } 204 + 205 + mutex_lock(&wakelocks_lock); 206 + 207 + wl = wakelock_lookup_add(buf, len, true); 208 + if (IS_ERR(wl)) { 209 + ret = PTR_ERR(wl); 210 + goto out; 211 + } 212 + if (timeout_ns) { 213 + u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1; 214 + 215 + do_div(timeout_ms, NSEC_PER_MSEC); 216 + __pm_wakeup_event(&wl->ws, timeout_ms); 217 + } else { 218 + __pm_stay_awake(&wl->ws); 219 + } 220 + 221 + wakelocks_lru_most_recent(wl); 222 + 223 + out: 224 + mutex_unlock(&wakelocks_lock); 225 + return ret; 226 + } 227 + 228 + int pm_wake_unlock(const char *buf) 229 + { 230 + struct wakelock *wl; 231 + size_t len; 232 + int ret = 0; 233 + 234 + len = strlen(buf); 235 + if (!len) 236 + return -EINVAL; 237 + 238 + if (buf[len-1] == '\n') 239 + len--; 240 + 241 + if (!len) 242 + return -EINVAL; 243 + 244 + mutex_lock(&wakelocks_lock); 245 + 246 + wl = wakelock_lookup_add(buf, len, false); 247 + if (IS_ERR(wl)) { 248 + ret = PTR_ERR(wl); 249 + goto out; 250 + } 251 + __pm_relax(&wl->ws); 252 + 253 + wakelocks_lru_most_recent(wl); 254 + wakelocks_gc(); 255 + 256 + out: 257 + mutex_unlock(&wakelocks_lock); 258 + return ret; 259 + }