kernel/power/wakelock.c
Source file repositories/reference/linux-study-clean/kernel/power/wakelock.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/power/wakelock.c- Extension
.c- Size
- 5972 bytes
- Lines
- 289
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/capability.hlinux/ctype.hlinux/device.hlinux/err.hlinux/hrtimer.hlinux/list.hlinux/rbtree.hlinux/slab.hlinux/workqueue.hpower.h
Detected Declarations
struct wakelockfunction pm_show_wakelocksfunction wakelocks_limit_exceededfunction increment_wakelocks_numberfunction decrement_wakelocks_numberfunction wakelocks_limit_exceededfunction increment_wakelocks_numberfunction wakelocks_lru_addfunction wakelocks_lru_most_recentfunction __wakelocks_gcfunction wakelocks_gcfunction wakelocks_lru_addfunction pm_wake_lockfunction pm_wake_unlock
Annotated Snippet
struct wakelock {
char *name;
struct rb_node node;
struct wakeup_source *ws;
#ifdef CONFIG_PM_WAKELOCKS_GC
struct list_head lru;
#endif
};
static struct rb_root wakelocks_tree = RB_ROOT;
ssize_t pm_show_wakelocks(char *buf, bool show_active)
{
struct rb_node *node;
struct wakelock *wl;
int len = 0;
mutex_lock(&wakelocks_lock);
for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
wl = rb_entry(node, struct wakelock, node);
if (wl->ws->active == show_active)
len += sysfs_emit_at(buf, len, "%s ", wl->name);
}
if (len > 0)
--len;
len += sysfs_emit_at(buf, len, "\n");
mutex_unlock(&wakelocks_lock);
return len;
}
#if CONFIG_PM_WAKELOCKS_LIMIT > 0
static unsigned int number_of_wakelocks;
static inline bool wakelocks_limit_exceeded(void)
{
return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT;
}
static inline void increment_wakelocks_number(void)
{
number_of_wakelocks++;
}
static inline void decrement_wakelocks_number(void)
{
number_of_wakelocks--;
}
#else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
static inline bool wakelocks_limit_exceeded(void) { return false; }
static inline void increment_wakelocks_number(void) {}
static inline void decrement_wakelocks_number(void) {}
#endif /* CONFIG_PM_WAKELOCKS_LIMIT */
#ifdef CONFIG_PM_WAKELOCKS_GC
#define WL_GC_COUNT_MAX 100
#define WL_GC_TIME_SEC 300
static void __wakelocks_gc(struct work_struct *work);
static LIST_HEAD(wakelocks_lru_list);
static DECLARE_WORK(wakelock_work, __wakelocks_gc);
static unsigned int wakelocks_gc_count;
static inline void wakelocks_lru_add(struct wakelock *wl)
{
list_add(&wl->lru, &wakelocks_lru_list);
}
static inline void wakelocks_lru_most_recent(struct wakelock *wl)
{
list_move(&wl->lru, &wakelocks_lru_list);
}
static void __wakelocks_gc(struct work_struct *work)
{
struct wakelock *wl, *aux;
ktime_t now;
mutex_lock(&wakelocks_lock);
now = ktime_get();
list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) {
u64 idle_time_ns;
bool active;
spin_lock_irq(&wl->ws->lock);
idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws->last_time));
Annotation
- Immediate include surface: `linux/capability.h`, `linux/ctype.h`, `linux/device.h`, `linux/err.h`, `linux/hrtimer.h`, `linux/list.h`, `linux/rbtree.h`, `linux/slab.h`.
- Detected declarations: `struct wakelock`, `function pm_show_wakelocks`, `function wakelocks_limit_exceeded`, `function increment_wakelocks_number`, `function decrement_wakelocks_number`, `function wakelocks_limit_exceeded`, `function increment_wakelocks_number`, `function wakelocks_lru_add`, `function wakelocks_lru_most_recent`, `function __wakelocks_gc`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.