drivers/cpufreq/cpufreq_userspace.c
Source file repositories/reference/linux-study-clean/drivers/cpufreq/cpufreq_userspace.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/cpufreq/cpufreq_userspace.c- Extension
.c- Size
- 4055 bytes
- Lines
- 156
- Domain
- Driver Families
- Bucket
- drivers/cpufreq
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- 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/cpufreq.hlinux/init.hlinux/module.hlinux/mutex.hlinux/slab.h
Detected Declarations
struct userspace_policyfunction cpufreq_setfunction show_speedfunction cpufreq_userspace_policy_initfunction cpufreq_userspace_policy_exitfunction cpufreq_userspace_policy_startfunction cpufreq_userspace_policy_stopfunction cpufreq_userspace_policy_limits
Annotated Snippet
struct userspace_policy {
unsigned int is_managed;
unsigned int setspeed;
struct mutex mutex;
};
/**
* cpufreq_set - set the CPU frequency
* @policy: pointer to policy struct where freq is being set
* @freq: target frequency in kHz
*
* Sets the CPU frequency to freq.
*/
static int cpufreq_set(struct cpufreq_policy *policy, unsigned int freq)
{
int ret = -EINVAL;
struct userspace_policy *userspace = policy->governor_data;
pr_debug("cpufreq_set for cpu %u, freq %u kHz\n", policy->cpu, freq);
mutex_lock(&userspace->mutex);
if (!userspace->is_managed)
goto err;
userspace->setspeed = freq;
ret = __cpufreq_driver_target(policy, freq, CPUFREQ_RELATION_L);
err:
mutex_unlock(&userspace->mutex);
return ret;
}
static ssize_t show_speed(struct cpufreq_policy *policy, char *buf)
{
struct userspace_policy *userspace = policy->governor_data;
return sprintf(buf, "%u\n", userspace->setspeed);
}
static int cpufreq_userspace_policy_init(struct cpufreq_policy *policy)
{
struct userspace_policy *userspace;
userspace = kzalloc_obj(*userspace);
if (!userspace)
return -ENOMEM;
mutex_init(&userspace->mutex);
policy->governor_data = userspace;
return 0;
}
/*
* Any routine that writes to the policy struct will hold the "rwsem" of
* policy struct that means it is free to free "governor_data" here.
*/
static void cpufreq_userspace_policy_exit(struct cpufreq_policy *policy)
{
kfree(policy->governor_data);
policy->governor_data = NULL;
}
static int cpufreq_userspace_policy_start(struct cpufreq_policy *policy)
{
struct userspace_policy *userspace = policy->governor_data;
BUG_ON(!policy->cur);
pr_debug("started managing cpu %u\n", policy->cpu);
mutex_lock(&userspace->mutex);
userspace->is_managed = 1;
userspace->setspeed = policy->cur;
mutex_unlock(&userspace->mutex);
return 0;
}
static void cpufreq_userspace_policy_stop(struct cpufreq_policy *policy)
{
struct userspace_policy *userspace = policy->governor_data;
pr_debug("managing cpu %u stopped\n", policy->cpu);
mutex_lock(&userspace->mutex);
userspace->is_managed = 0;
userspace->setspeed = 0;
mutex_unlock(&userspace->mutex);
}
static void cpufreq_userspace_policy_limits(struct cpufreq_policy *policy)
Annotation
- Immediate include surface: `linux/cpufreq.h`, `linux/init.h`, `linux/module.h`, `linux/mutex.h`, `linux/slab.h`.
- Detected declarations: `struct userspace_policy`, `function cpufreq_set`, `function show_speed`, `function cpufreq_userspace_policy_init`, `function cpufreq_userspace_policy_exit`, `function cpufreq_userspace_policy_start`, `function cpufreq_userspace_policy_stop`, `function cpufreq_userspace_policy_limits`.
- Atlas domain: Driver Families / drivers/cpufreq.
- 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.