wakelock.c 5.7 KB

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