wakelock.c 5.3 KB

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