wakelock.c 5.8 KB

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