wakelock.c 5.6 KB

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