wakelock.c 4.8 KB

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