1717#include <linux/rbtree.h>
1818#include <linux/slab.h>
1919
20- #define WL_GC_COUNT_MAX 100
21- #define WL_GC_TIME_SEC 300
22-
2320static DEFINE_MUTEX (wakelocks_lock );
2421
2522struct wakelock {
2623 char * name ;
2724 struct rb_node node ;
2825 struct wakeup_source ws ;
26+ #ifdef CONFIG_PM_WAKELOCKS_GC
2927 struct list_head lru ;
28+ #endif
3029};
3130
3231static struct rb_root wakelocks_tree = RB_ROOT ;
33- static LIST_HEAD (wakelocks_lru_list );
34- static unsigned int wakelocks_gc_count ;
3532
3633ssize_t pm_show_wakelocks (char * buf , bool show_active )
3734{
@@ -79,6 +76,61 @@ static inline void increment_wakelocks_number(void) {}
7976static inline void decrement_wakelocks_number (void ) {}
8077#endif /* CONFIG_PM_WAKELOCKS_LIMIT */
8178
79+ #ifdef CONFIG_PM_WAKELOCKS_GC
80+ #define WL_GC_COUNT_MAX 100
81+ #define WL_GC_TIME_SEC 300
82+
83+ static LIST_HEAD (wakelocks_lru_list );
84+ static unsigned int wakelocks_gc_count ;
85+
86+ static inline void wakelocks_lru_add (struct wakelock * wl )
87+ {
88+ list_add (& wl -> lru , & wakelocks_lru_list );
89+ }
90+
91+ static inline void wakelocks_lru_most_recent (struct wakelock * wl )
92+ {
93+ list_move (& wl -> lru , & wakelocks_lru_list );
94+ }
95+
96+ static void wakelocks_gc (void )
97+ {
98+ struct wakelock * wl , * aux ;
99+ ktime_t now ;
100+
101+ if (++ wakelocks_gc_count <= WL_GC_COUNT_MAX )
102+ return ;
103+
104+ now = ktime_get ();
105+ list_for_each_entry_safe_reverse (wl , aux , & wakelocks_lru_list , lru ) {
106+ u64 idle_time_ns ;
107+ bool active ;
108+
109+ spin_lock_irq (& wl -> ws .lock );
110+ idle_time_ns = ktime_to_ns (ktime_sub (now , wl -> ws .last_time ));
111+ active = wl -> ws .active ;
112+ spin_unlock_irq (& wl -> ws .lock );
113+
114+ if (idle_time_ns < ((u64 )WL_GC_TIME_SEC * NSEC_PER_SEC ))
115+ break ;
116+
117+ if (!active ) {
118+ wakeup_source_remove (& wl -> ws );
119+ rb_erase (& wl -> node , & wakelocks_tree );
120+ list_del (& wl -> lru );
121+ kfree (wl -> name );
122+ kfree (wl );
123+ decrement_wakelocks_number ();
124+ }
125+ }
126+ wakelocks_gc_count = 0 ;
127+ }
128+ #else /* !CONFIG_PM_WAKELOCKS_GC */
129+ static inline void wakelocks_lru_add (struct wakelock * wl ) {}
130+ static inline void wakelocks_lru_most_recent (struct wakelock * wl ) {}
131+ static inline void wakelocks_gc (void ) {}
132+ #endif /* !CONFIG_PM_WAKELOCKS_GC */
133+
82134static struct wakelock * wakelock_lookup_add (const char * name , size_t len ,
83135 bool add_if_not_found )
84136{
@@ -123,7 +175,7 @@ static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
123175 wakeup_source_add (& wl -> ws );
124176 rb_link_node (& wl -> node , parent , node );
125177 rb_insert_color (& wl -> node , & wakelocks_tree );
126- list_add ( & wl -> lru , & wakelocks_lru_list );
178+ wakelocks_lru_add ( wl );
127179 increment_wakelocks_number ();
128180 return wl ;
129181}
@@ -166,42 +218,13 @@ int pm_wake_lock(const char *buf)
166218 __pm_stay_awake (& wl -> ws );
167219 }
168220
169- list_move ( & wl -> lru , & wakelocks_lru_list );
221+ wakelocks_lru_most_recent ( wl );
170222
171223 out :
172224 mutex_unlock (& wakelocks_lock );
173225 return ret ;
174226}
175227
176- static void wakelocks_gc (void )
177- {
178- struct wakelock * wl , * aux ;
179- ktime_t now = ktime_get ();
180-
181- list_for_each_entry_safe_reverse (wl , aux , & wakelocks_lru_list , lru ) {
182- u64 idle_time_ns ;
183- bool active ;
184-
185- spin_lock_irq (& wl -> ws .lock );
186- idle_time_ns = ktime_to_ns (ktime_sub (now , wl -> ws .last_time ));
187- active = wl -> ws .active ;
188- spin_unlock_irq (& wl -> ws .lock );
189-
190- if (idle_time_ns < ((u64 )WL_GC_TIME_SEC * NSEC_PER_SEC ))
191- break ;
192-
193- if (!active ) {
194- wakeup_source_remove (& wl -> ws );
195- rb_erase (& wl -> node , & wakelocks_tree );
196- list_del (& wl -> lru );
197- kfree (wl -> name );
198- kfree (wl );
199- decrement_wakelocks_number ();
200- }
201- }
202- wakelocks_gc_count = 0 ;
203- }
204-
205228int pm_wake_unlock (const char * buf )
206229{
207230 struct wakelock * wl ;
@@ -226,9 +249,9 @@ int pm_wake_unlock(const char *buf)
226249 goto out ;
227250 }
228251 __pm_relax (& wl -> ws );
229- list_move ( & wl -> lru , & wakelocks_lru_list );
230- if ( ++ wakelocks_gc_count > WL_GC_COUNT_MAX )
231- wakelocks_gc ();
252+
253+ wakelocks_lru_most_recent ( wl );
254+ wakelocks_gc ();
232255
233256 out :
234257 mutex_unlock (& wakelocks_lock );
0 commit comments