Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * kernel/power/wakelock.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * User space wakeup sources support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * This code is based on the analogous interface allowing user space to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * manipulate wakelocks on Android.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "power.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static DEFINE_MUTEX(wakelocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct wakelock {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	char			*name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct rb_node		node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct wakeup_source	*ws;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #ifdef CONFIG_PM_WAKELOCKS_GC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct list_head	lru;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static struct rb_root wakelocks_tree = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) ssize_t pm_show_wakelocks(char *buf, bool show_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct wakelock *wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	mutex_lock(&wakelocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		wl = rb_entry(node, struct wakelock, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		if (wl->ws->active == show_active)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			len += sysfs_emit_at(buf, len, "%s ", wl->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	len += sysfs_emit_at(buf, len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	mutex_unlock(&wakelocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #if CONFIG_PM_WAKELOCKS_LIMIT > 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static unsigned int number_of_wakelocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static inline bool wakelocks_limit_exceeded(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static inline void increment_wakelocks_number(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	number_of_wakelocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static inline void decrement_wakelocks_number(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	number_of_wakelocks--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static inline bool wakelocks_limit_exceeded(void) { return false; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static inline void increment_wakelocks_number(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static inline void decrement_wakelocks_number(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #endif /* CONFIG_PM_WAKELOCKS_LIMIT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #ifdef CONFIG_PM_WAKELOCKS_GC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #define WL_GC_COUNT_MAX	100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #define WL_GC_TIME_SEC	300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static void __wakelocks_gc(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static LIST_HEAD(wakelocks_lru_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static DECLARE_WORK(wakelock_work, __wakelocks_gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static unsigned int wakelocks_gc_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static inline void wakelocks_lru_add(struct wakelock *wl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	list_add(&wl->lru, &wakelocks_lru_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static inline void wakelocks_lru_most_recent(struct wakelock *wl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	list_move(&wl->lru, &wakelocks_lru_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void __wakelocks_gc(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct wakelock *wl, *aux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ktime_t now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	mutex_lock(&wakelocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	now = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		u64 idle_time_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		bool active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		spin_lock_irq(&wl->ws->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws->last_time));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		active = wl->ws->active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		spin_unlock_irq(&wl->ws->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		if (!active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			wakeup_source_unregister(wl->ws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			rb_erase(&wl->node, &wakelocks_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			list_del(&wl->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			kfree(wl->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			kfree(wl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			decrement_wakelocks_number();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	wakelocks_gc_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	mutex_unlock(&wakelocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static void wakelocks_gc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (++wakelocks_gc_count <= WL_GC_COUNT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	schedule_work(&wakelock_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #else /* !CONFIG_PM_WAKELOCKS_GC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static inline void wakelocks_lru_add(struct wakelock *wl) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static inline void wakelocks_lru_most_recent(struct wakelock *wl) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static inline void wakelocks_gc(void) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #endif /* !CONFIG_PM_WAKELOCKS_GC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 					    bool add_if_not_found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct rb_node **node = &wakelocks_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct rb_node *parent = *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct wakelock *wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	while (*node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		int diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		parent = *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		wl = rb_entry(*node, struct wakelock, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		diff = strncmp(name, wl->name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		if (diff == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			if (wl->name[len])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				diff = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				return wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		if (diff < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			node = &(*node)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			node = &(*node)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (!add_if_not_found)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (wakelocks_limit_exceeded())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return ERR_PTR(-ENOSPC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	/* Not found, we have to add a new one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	wl = kzalloc(sizeof(*wl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (!wl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	wl->name = kstrndup(name, len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (!wl->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		kfree(wl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	wl->ws = wakeup_source_register(NULL, wl->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (!wl->ws) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		kfree(wl->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		kfree(wl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	wl->ws->last_time = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	rb_link_node(&wl->node, parent, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	rb_insert_color(&wl->node, &wakelocks_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	wakelocks_lru_add(wl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	increment_wakelocks_number();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int pm_wake_lock(const char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	const char *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct wakelock *wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	u64 timeout_ns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (!capable(CAP_BLOCK_SUSPEND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	while (*str && !isspace(*str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		str++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	len = str - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (*str && *str != '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		/* Find out if there's a valid timeout string appended. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	mutex_lock(&wakelocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	wl = wakelock_lookup_add(buf, len, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (IS_ERR(wl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		ret = PTR_ERR(wl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (timeout_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		do_div(timeout_ms, NSEC_PER_MSEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		__pm_wakeup_event(wl->ws, timeout_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		__pm_stay_awake(wl->ws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	wakelocks_lru_most_recent(wl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	mutex_unlock(&wakelocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int pm_wake_unlock(const char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct wakelock *wl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (!capable(CAP_BLOCK_SUSPEND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	len = strlen(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (buf[len-1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	mutex_lock(&wakelocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	wl = wakelock_lookup_add(buf, len, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (IS_ERR(wl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		ret = PTR_ERR(wl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	__pm_relax(wl->ws);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	wakelocks_lru_most_recent(wl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	wakelocks_gc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	mutex_unlock(&wakelocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }