wakelock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * kernel/power/wakelock.c
  4. *
  5. * User space wakeup sources support.
  6. *
  7. * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
  8. *
  9. * This code is based on the analogous interface allowing user space to
  10. * manipulate wakelocks on Android.
  11. */
  12. #include <linux/capability.h>
  13. #include <linux/ctype.h>
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/list.h>
  18. #include <linux/rbtree.h>
  19. #include <linux/slab.h>
  20. #include <linux/workqueue.h>
  21. #include "power.h"
  22. static DEFINE_MUTEX(wakelocks_lock);
  23. struct wakelock {
  24. char *name;
  25. struct rb_node node;
  26. struct wakeup_source *ws;
  27. #ifdef CONFIG_PM_WAKELOCKS_GC
  28. struct list_head lru;
  29. #endif
  30. };
  31. static struct rb_root wakelocks_tree = RB_ROOT;
  32. ssize_t pm_show_wakelocks(char *buf, bool show_active)
  33. {
  34. struct rb_node *node;
  35. struct wakelock *wl;
  36. int len = 0;
  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. len += sysfs_emit_at(buf, len, "%s ", wl->name);
  42. }
  43. len += sysfs_emit_at(buf, len, "\n");
  44. mutex_unlock(&wakelocks_lock);
  45. return len;
  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 void __wakelocks_gc(struct work_struct *work);
  70. static LIST_HEAD(wakelocks_lru_list);
  71. static DECLARE_WORK(wakelock_work, __wakelocks_gc);
  72. static unsigned int wakelocks_gc_count;
  73. static inline void wakelocks_lru_add(struct wakelock *wl)
  74. {
  75. list_add(&wl->lru, &wakelocks_lru_list);
  76. }
  77. static inline void wakelocks_lru_most_recent(struct wakelock *wl)
  78. {
  79. list_move(&wl->lru, &wakelocks_lru_list);
  80. }
  81. static void __wakelocks_gc(struct work_struct *work)
  82. {
  83. struct wakelock *wl, *aux;
  84. ktime_t now;
  85. mutex_lock(&wakelocks_lock);
  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_unregister(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. mutex_unlock(&wakelocks_lock);
  107. }
  108. static void wakelocks_gc(void)
  109. {
  110. if (++wakelocks_gc_count <= WL_GC_COUNT_MAX)
  111. return;
  112. schedule_work(&wakelock_work);
  113. }
  114. #else /* !CONFIG_PM_WAKELOCKS_GC */
  115. static inline void wakelocks_lru_add(struct wakelock *wl) {}
  116. static inline void wakelocks_lru_most_recent(struct wakelock *wl) {}
  117. static inline void wakelocks_gc(void) {}
  118. #endif /* !CONFIG_PM_WAKELOCKS_GC */
  119. static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
  120. bool add_if_not_found)
  121. {
  122. struct rb_node **node = &wakelocks_tree.rb_node;
  123. struct rb_node *parent = *node;
  124. struct wakelock *wl;
  125. while (*node) {
  126. int diff;
  127. parent = *node;
  128. wl = rb_entry(*node, struct wakelock, node);
  129. diff = strncmp(name, wl->name, len);
  130. if (diff == 0) {
  131. if (wl->name[len])
  132. diff = -1;
  133. else
  134. return wl;
  135. }
  136. if (diff < 0)
  137. node = &(*node)->rb_left;
  138. else
  139. node = &(*node)->rb_right;
  140. }
  141. if (!add_if_not_found)
  142. return ERR_PTR(-EINVAL);
  143. if (wakelocks_limit_exceeded())
  144. return ERR_PTR(-ENOSPC);
  145. /* Not found, we have to add a new one. */
  146. wl = kzalloc(sizeof(*wl), GFP_KERNEL);
  147. if (!wl)
  148. return ERR_PTR(-ENOMEM);
  149. wl->name = kstrndup(name, len, GFP_KERNEL);
  150. if (!wl->name) {
  151. kfree(wl);
  152. return ERR_PTR(-ENOMEM);
  153. }
  154. wl->ws = wakeup_source_register(NULL, wl->name);
  155. if (!wl->ws) {
  156. kfree(wl->name);
  157. kfree(wl);
  158. return ERR_PTR(-ENOMEM);
  159. }
  160. wl->ws->last_time = ktime_get();
  161. rb_link_node(&wl->node, parent, node);
  162. rb_insert_color(&wl->node, &wakelocks_tree);
  163. wakelocks_lru_add(wl);
  164. increment_wakelocks_number();
  165. return wl;
  166. }
  167. int pm_wake_lock(const char *buf)
  168. {
  169. const char *str = buf;
  170. struct wakelock *wl;
  171. u64 timeout_ns = 0;
  172. size_t len;
  173. int ret = 0;
  174. if (!capable(CAP_BLOCK_SUSPEND))
  175. return -EPERM;
  176. while (*str && !isspace(*str))
  177. str++;
  178. len = str - buf;
  179. if (!len)
  180. return -EINVAL;
  181. if (*str && *str != '\n') {
  182. /* Find out if there's a valid timeout string appended. */
  183. ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
  184. if (ret)
  185. return -EINVAL;
  186. }
  187. mutex_lock(&wakelocks_lock);
  188. wl = wakelock_lookup_add(buf, len, true);
  189. if (IS_ERR(wl)) {
  190. ret = PTR_ERR(wl);
  191. goto out;
  192. }
  193. if (timeout_ns) {
  194. u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
  195. do_div(timeout_ms, NSEC_PER_MSEC);
  196. __pm_wakeup_event(wl->ws, timeout_ms);
  197. } else {
  198. __pm_stay_awake(wl->ws);
  199. }
  200. wakelocks_lru_most_recent(wl);
  201. out:
  202. mutex_unlock(&wakelocks_lock);
  203. return ret;
  204. }
  205. int pm_wake_unlock(const char *buf)
  206. {
  207. struct wakelock *wl;
  208. size_t len;
  209. int ret = 0;
  210. if (!capable(CAP_BLOCK_SUSPEND))
  211. return -EPERM;
  212. len = strlen(buf);
  213. if (!len)
  214. return -EINVAL;
  215. if (buf[len-1] == '\n')
  216. len--;
  217. if (!len)
  218. return -EINVAL;
  219. mutex_lock(&wakelocks_lock);
  220. wl = wakelock_lookup_add(buf, len, false);
  221. if (IS_ERR(wl)) {
  222. ret = PTR_ERR(wl);
  223. goto out;
  224. }
  225. __pm_relax(wl->ws);
  226. wakelocks_lru_most_recent(wl);
  227. wakelocks_gc();
  228. out:
  229. mutex_unlock(&wakelocks_lock);
  230. return ret;
  231. }