wakelock.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #include <linux/capability.h>
  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. #include "power.h"
  20. static DEFINE_MUTEX(wakelocks_lock);
  21. struct wakelock {
  22. char *name;
  23. struct rb_node node;
  24. struct wakeup_source ws;
  25. #ifdef CONFIG_PM_WAKELOCKS_GC
  26. struct list_head lru;
  27. #endif
  28. };
  29. static struct rb_root wakelocks_tree = RB_ROOT;
  30. ssize_t pm_show_wakelocks(char *buf, bool show_active)
  31. {
  32. struct rb_node *node;
  33. struct wakelock *wl;
  34. char *str = buf;
  35. char *end = buf + PAGE_SIZE;
  36. mutex_lock(&wakelocks_lock);
  37. for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
  38. wl = rb_entry(node, struct wakelock, node);
  39. if (wl->ws.active == show_active)
  40. str += scnprintf(str, end - str, "%s ", wl->name);
  41. }
  42. if (str > buf)
  43. str--;
  44. str += scnprintf(str, end - str, "\n");
  45. mutex_unlock(&wakelocks_lock);
  46. return (str - buf);
  47. }
  48. #if CONFIG_PM_WAKELOCKS_LIMIT > 0
  49. static unsigned int number_of_wakelocks;
  50. static inline bool wakelocks_limit_exceeded(void)
  51. {
  52. return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT;
  53. }
  54. static inline void increment_wakelocks_number(void)
  55. {
  56. number_of_wakelocks++;
  57. }
  58. static inline void decrement_wakelocks_number(void)
  59. {
  60. number_of_wakelocks--;
  61. }
  62. #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
  63. static inline bool wakelocks_limit_exceeded(void) { return false; }
  64. static inline void increment_wakelocks_number(void) {}
  65. static inline void decrement_wakelocks_number(void) {}
  66. #endif /* CONFIG_PM_WAKELOCKS_LIMIT */
  67. #ifdef CONFIG_PM_WAKELOCKS_GC
  68. #define WL_GC_COUNT_MAX 100
  69. #define WL_GC_TIME_SEC 300
  70. static LIST_HEAD(wakelocks_lru_list);
  71. static unsigned int wakelocks_gc_count;
  72. static inline void wakelocks_lru_add(struct wakelock *wl)
  73. {
  74. list_add(&wl->lru, &wakelocks_lru_list);
  75. }
  76. static inline void wakelocks_lru_most_recent(struct wakelock *wl)
  77. {
  78. list_move(&wl->lru, &wakelocks_lru_list);
  79. }
  80. static void wakelocks_gc(void)
  81. {
  82. struct wakelock *wl, *aux;
  83. ktime_t now;
  84. if (++wakelocks_gc_count <= WL_GC_COUNT_MAX)
  85. return;
  86. now = ktime_get();
  87. list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) {
  88. u64 idle_time_ns;
  89. bool active;
  90. spin_lock_irq(&wl->ws.lock);
  91. idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws.last_time));
  92. active = wl->ws.active;
  93. spin_unlock_irq(&wl->ws.lock);
  94. if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC))
  95. break;
  96. if (!active) {
  97. wakeup_source_remove(&wl->ws);
  98. rb_erase(&wl->node, &wakelocks_tree);
  99. list_del(&wl->lru);
  100. kfree(wl->name);
  101. kfree(wl);
  102. decrement_wakelocks_number();
  103. }
  104. }
  105. wakelocks_gc_count = 0;
  106. }
  107. #else /* !CONFIG_PM_WAKELOCKS_GC */
  108. static inline void wakelocks_lru_add(struct wakelock *wl) {}
  109. static inline void wakelocks_lru_most_recent(struct wakelock *wl) {}
  110. static inline void wakelocks_gc(void) {}
  111. #endif /* !CONFIG_PM_WAKELOCKS_GC */
  112. static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
  113. bool add_if_not_found)
  114. {
  115. struct rb_node **node = &wakelocks_tree.rb_node;
  116. struct rb_node *parent = *node;
  117. struct wakelock *wl;
  118. while (*node) {
  119. int diff;
  120. parent = *node;
  121. wl = rb_entry(*node, struct wakelock, node);
  122. diff = strncmp(name, wl->name, len);
  123. if (diff == 0) {
  124. if (wl->name[len])
  125. diff = -1;
  126. else
  127. return wl;
  128. }
  129. if (diff < 0)
  130. node = &(*node)->rb_left;
  131. else
  132. node = &(*node)->rb_right;
  133. }
  134. if (!add_if_not_found)
  135. return ERR_PTR(-EINVAL);
  136. if (wakelocks_limit_exceeded())
  137. return ERR_PTR(-ENOSPC);
  138. /* Not found, we have to add a new one. */
  139. wl = kzalloc(sizeof(*wl), GFP_KERNEL);
  140. if (!wl)
  141. return ERR_PTR(-ENOMEM);
  142. wl->name = kstrndup(name, len, GFP_KERNEL);
  143. if (!wl->name) {
  144. kfree(wl);
  145. return ERR_PTR(-ENOMEM);
  146. }
  147. wl->ws.name = wl->name;
  148. wakeup_source_add(&wl->ws);
  149. rb_link_node(&wl->node, parent, node);
  150. rb_insert_color(&wl->node, &wakelocks_tree);
  151. wakelocks_lru_add(wl);
  152. increment_wakelocks_number();
  153. return wl;
  154. }
  155. int pm_wake_lock(const char *buf)
  156. {
  157. const char *str = buf;
  158. struct wakelock *wl;
  159. u64 timeout_ns = 0;
  160. size_t len;
  161. int ret = 0;
  162. if (!capable(CAP_BLOCK_SUSPEND))
  163. return -EPERM;
  164. while (*str && !isspace(*str))
  165. str++;
  166. len = str - buf;
  167. if (!len)
  168. return -EINVAL;
  169. if (*str && *str != '\n') {
  170. /* Find out if there's a valid timeout string appended. */
  171. ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
  172. if (ret)
  173. return -EINVAL;
  174. }
  175. mutex_lock(&wakelocks_lock);
  176. wl = wakelock_lookup_add(buf, len, true);
  177. if (IS_ERR(wl)) {
  178. ret = PTR_ERR(wl);
  179. goto out;
  180. }
  181. if (timeout_ns) {
  182. u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
  183. do_div(timeout_ms, NSEC_PER_MSEC);
  184. __pm_wakeup_event(&wl->ws, timeout_ms);
  185. } else {
  186. __pm_stay_awake(&wl->ws);
  187. }
  188. wakelocks_lru_most_recent(wl);
  189. out:
  190. mutex_unlock(&wakelocks_lock);
  191. return ret;
  192. }
  193. int pm_wake_unlock(const char *buf)
  194. {
  195. struct wakelock *wl;
  196. size_t len;
  197. int ret = 0;
  198. if (!capable(CAP_BLOCK_SUSPEND))
  199. return -EPERM;
  200. len = strlen(buf);
  201. if (!len)
  202. return -EINVAL;
  203. if (buf[len-1] == '\n')
  204. len--;
  205. if (!len)
  206. return -EINVAL;
  207. mutex_lock(&wakelocks_lock);
  208. wl = wakelock_lookup_add(buf, len, false);
  209. if (IS_ERR(wl)) {
  210. ret = PTR_ERR(wl);
  211. goto out;
  212. }
  213. __pm_relax(&wl->ws);
  214. wakelocks_lru_most_recent(wl);
  215. wakelocks_gc();
  216. out:
  217. mutex_unlock(&wakelocks_lock);
  218. return ret;
  219. }