Commit 5f279845 authored by James Bottomley's avatar James Bottomley Committed by Rafael J. Wysocki

pm_qos: Reimplement using plists

A lot of the pm_qos extremal value handling is really duplicating what a
priority ordered list does, just in a less efficient fashion.  Simply
redoing the implementation in terms of a plist gets rid of a lot of this
junk (although there are several other strange things that could do with
tidying up, like pm_qos_request_list has to carry the pm_qos_class with
every node, simply because it doesn't get passed in to
pm_qos_update_request even though every caller knows full well what
parameter it's updating).

I think this redo is a win independent of android, so we should do
something like this now.
Signed-off-by: default avatarJames Bottomley <James.Bottomley@suse.de>
Signed-off-by: default avatarmark gross <markgross@thegnar.org>
Signed-off-by: default avatarRafael J. Wysocki <rjw@sisk.pl>
parent 12e4d0cc
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
/*#define DEBUG*/ /*#define DEBUG*/
#include <linux/pm_qos_params.h> #include <linux/pm_qos_params.h>
#include <linux/plist.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/slab.h> #include <linux/slab.h>
...@@ -49,58 +50,53 @@ ...@@ -49,58 +50,53 @@
* held, taken with _irqsave. One lock to rule them all * held, taken with _irqsave. One lock to rule them all
*/ */
struct pm_qos_request_list { struct pm_qos_request_list {
struct list_head list; struct plist_node list;
union {
s32 value;
s32 usec;
s32 kbps;
};
int pm_qos_class; int pm_qos_class;
}; };
static s32 max_compare(s32 v1, s32 v2); enum pm_qos_type {
static s32 min_compare(s32 v1, s32 v2); PM_QOS_MAX, /* return the largest value */
PM_QOS_MIN /* return the smallest value */
};
struct pm_qos_object { struct pm_qos_object {
struct pm_qos_request_list requests; struct plist_head requests;
struct blocking_notifier_head *notifiers; struct blocking_notifier_head *notifiers;
struct miscdevice pm_qos_power_miscdev; struct miscdevice pm_qos_power_miscdev;
char *name; char *name;
s32 default_value; s32 default_value;
atomic_t target_value; enum pm_qos_type type;
s32 (*comparitor)(s32, s32);
}; };
static DEFINE_SPINLOCK(pm_qos_lock);
static struct pm_qos_object null_pm_qos; static struct pm_qos_object null_pm_qos;
static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier); static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
static struct pm_qos_object cpu_dma_pm_qos = { static struct pm_qos_object cpu_dma_pm_qos = {
.requests = {LIST_HEAD_INIT(cpu_dma_pm_qos.requests.list)}, .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
.notifiers = &cpu_dma_lat_notifier, .notifiers = &cpu_dma_lat_notifier,
.name = "cpu_dma_latency", .name = "cpu_dma_latency",
.default_value = 2000 * USEC_PER_SEC, .default_value = 2000 * USEC_PER_SEC,
.target_value = ATOMIC_INIT(2000 * USEC_PER_SEC), .type = PM_QOS_MIN,
.comparitor = min_compare
}; };
static BLOCKING_NOTIFIER_HEAD(network_lat_notifier); static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
static struct pm_qos_object network_lat_pm_qos = { static struct pm_qos_object network_lat_pm_qos = {
.requests = {LIST_HEAD_INIT(network_lat_pm_qos.requests.list)}, .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
.notifiers = &network_lat_notifier, .notifiers = &network_lat_notifier,
.name = "network_latency", .name = "network_latency",
.default_value = 2000 * USEC_PER_SEC, .default_value = 2000 * USEC_PER_SEC,
.target_value = ATOMIC_INIT(2000 * USEC_PER_SEC), .type = PM_QOS_MIN
.comparitor = min_compare
}; };
static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier); static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
static struct pm_qos_object network_throughput_pm_qos = { static struct pm_qos_object network_throughput_pm_qos = {
.requests = {LIST_HEAD_INIT(network_throughput_pm_qos.requests.list)}, .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
.notifiers = &network_throughput_notifier, .notifiers = &network_throughput_notifier,
.name = "network_throughput", .name = "network_throughput",
.default_value = 0, .default_value = 0,
.target_value = ATOMIC_INIT(0), .type = PM_QOS_MAX,
.comparitor = max_compare
}; };
...@@ -111,8 +107,6 @@ static struct pm_qos_object *pm_qos_array[] = { ...@@ -111,8 +107,6 @@ static struct pm_qos_object *pm_qos_array[] = {
&network_throughput_pm_qos &network_throughput_pm_qos
}; };
static DEFINE_SPINLOCK(pm_qos_lock);
static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
size_t count, loff_t *f_pos); size_t count, loff_t *f_pos);
static int pm_qos_power_open(struct inode *inode, struct file *filp); static int pm_qos_power_open(struct inode *inode, struct file *filp);
...@@ -124,46 +118,55 @@ static const struct file_operations pm_qos_power_fops = { ...@@ -124,46 +118,55 @@ static const struct file_operations pm_qos_power_fops = {
.release = pm_qos_power_release, .release = pm_qos_power_release,
}; };
/* static helper functions */ /* unlocked internal variant */
static s32 max_compare(s32 v1, s32 v2) static inline int pm_qos_get_value(struct pm_qos_object *o)
{ {
return max(v1, v2); if (plist_head_empty(&o->requests))
} return o->default_value;
static s32 min_compare(s32 v1, s32 v2) switch (o->type) {
{ case PM_QOS_MIN:
return min(v1, v2); return plist_last(&o->requests)->prio;
}
case PM_QOS_MAX:
return plist_first(&o->requests)->prio;
static void update_target(int pm_qos_class) default:
/* runtime check for not using enum */
BUG();
}
}
static void update_target(struct pm_qos_object *o, struct plist_node *node,
int del, int value)
{ {
s32 extreme_value;
struct pm_qos_request_list *node;
unsigned long flags; unsigned long flags;
int call_notifier = 0; int prev_value, curr_value;
spin_lock_irqsave(&pm_qos_lock, flags); spin_lock_irqsave(&pm_qos_lock, flags);
extreme_value = pm_qos_array[pm_qos_class]->default_value; prev_value = pm_qos_get_value(o);
list_for_each_entry(node, /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
&pm_qos_array[pm_qos_class]->requests.list, list) { if (value != PM_QOS_DEFAULT_VALUE) {
extreme_value = pm_qos_array[pm_qos_class]->comparitor( /*
extreme_value, node->value); * to change the list, we atomically remove, reinit
} * with new value and add, then see if the extremal
if (atomic_read(&pm_qos_array[pm_qos_class]->target_value) != * changed
extreme_value) { */
call_notifier = 1; plist_del(node, &o->requests);
atomic_set(&pm_qos_array[pm_qos_class]->target_value, plist_node_init(node, value);
extreme_value); plist_add(node, &o->requests);
pr_debug(KERN_ERR "new target for qos %d is %d\n", pm_qos_class, } else if (del) {
atomic_read(&pm_qos_array[pm_qos_class]->target_value)); plist_del(node, &o->requests);
} else {
plist_add(node, &o->requests);
} }
curr_value = pm_qos_get_value(o);
spin_unlock_irqrestore(&pm_qos_lock, flags); spin_unlock_irqrestore(&pm_qos_lock, flags);
if (call_notifier) if (prev_value != curr_value)
blocking_notifier_call_chain( blocking_notifier_call_chain(o->notifiers,
pm_qos_array[pm_qos_class]->notifiers, (unsigned long)curr_value,
(unsigned long) extreme_value, NULL); NULL);
} }
static int register_pm_qos_misc(struct pm_qos_object *qos) static int register_pm_qos_misc(struct pm_qos_object *qos)
...@@ -196,7 +199,14 @@ static int find_pm_qos_object_by_minor(int minor) ...@@ -196,7 +199,14 @@ static int find_pm_qos_object_by_minor(int minor)
*/ */
int pm_qos_request(int pm_qos_class) int pm_qos_request(int pm_qos_class)
{ {
return atomic_read(&pm_qos_array[pm_qos_class]->target_value); unsigned long flags;
int value;
spin_lock_irqsave(&pm_qos_lock, flags);
value = pm_qos_get_value(pm_qos_array[pm_qos_class]);
spin_unlock_irqrestore(&pm_qos_lock, flags);
return value;
} }
EXPORT_SYMBOL_GPL(pm_qos_request); EXPORT_SYMBOL_GPL(pm_qos_request);
...@@ -214,21 +224,19 @@ EXPORT_SYMBOL_GPL(pm_qos_request); ...@@ -214,21 +224,19 @@ EXPORT_SYMBOL_GPL(pm_qos_request);
struct pm_qos_request_list *pm_qos_add_request(int pm_qos_class, s32 value) struct pm_qos_request_list *pm_qos_add_request(int pm_qos_class, s32 value)
{ {
struct pm_qos_request_list *dep; struct pm_qos_request_list *dep;
unsigned long flags;
dep = kzalloc(sizeof(struct pm_qos_request_list), GFP_KERNEL); dep = kzalloc(sizeof(struct pm_qos_request_list), GFP_KERNEL);
if (dep) { if (dep) {
struct pm_qos_object *o = pm_qos_array[pm_qos_class];
int new_value;
if (value == PM_QOS_DEFAULT_VALUE) if (value == PM_QOS_DEFAULT_VALUE)
dep->value = pm_qos_array[pm_qos_class]->default_value; new_value = o->default_value;
else else
dep->value = value; new_value = value;
plist_node_init(&dep->list, new_value);
dep->pm_qos_class = pm_qos_class; dep->pm_qos_class = pm_qos_class;
update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
spin_lock_irqsave(&pm_qos_lock, flags);
list_add(&dep->list,
&pm_qos_array[pm_qos_class]->requests.list);
spin_unlock_irqrestore(&pm_qos_lock, flags);
update_target(pm_qos_class);
} }
return dep; return dep;
...@@ -248,25 +256,21 @@ EXPORT_SYMBOL_GPL(pm_qos_add_request); ...@@ -248,25 +256,21 @@ EXPORT_SYMBOL_GPL(pm_qos_add_request);
void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req, void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
s32 new_value) s32 new_value)
{ {
unsigned long flags;
int pending_update = 0;
s32 temp; s32 temp;
struct pm_qos_object *o;
if (!pm_qos_req) /*guard against callers passing in null */
return;
o = pm_qos_array[pm_qos_req->pm_qos_class];
if (pm_qos_req) { /*guard against callers passing in null */
spin_lock_irqsave(&pm_qos_lock, flags);
if (new_value == PM_QOS_DEFAULT_VALUE) if (new_value == PM_QOS_DEFAULT_VALUE)
temp = pm_qos_array[pm_qos_req->pm_qos_class]->default_value; temp = o->default_value;
else else
temp = new_value; temp = new_value;
if (temp != pm_qos_req->value) { if (temp != pm_qos_req->list.prio)
pending_update = 1; update_target(o, &pm_qos_req->list, 0, temp);
pm_qos_req->value = temp;
}
spin_unlock_irqrestore(&pm_qos_lock, flags);
if (pending_update)
update_target(pm_qos_req->pm_qos_class);
}
} }
EXPORT_SYMBOL_GPL(pm_qos_update_request); EXPORT_SYMBOL_GPL(pm_qos_update_request);
...@@ -280,19 +284,15 @@ EXPORT_SYMBOL_GPL(pm_qos_update_request); ...@@ -280,19 +284,15 @@ EXPORT_SYMBOL_GPL(pm_qos_update_request);
*/ */
void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req) void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
{ {
unsigned long flags; struct pm_qos_object *o;
int qos_class;
if (pm_qos_req == NULL) if (pm_qos_req == NULL)
return; return;
/* silent return to keep pcm code cleaner */ /* silent return to keep pcm code cleaner */
qos_class = pm_qos_req->pm_qos_class; o = pm_qos_array[pm_qos_req->pm_qos_class];
spin_lock_irqsave(&pm_qos_lock, flags); update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
list_del(&pm_qos_req->list);
kfree(pm_qos_req); kfree(pm_qos_req);
spin_unlock_irqrestore(&pm_qos_lock, flags);
update_target(qos_class);
} }
EXPORT_SYMBOL_GPL(pm_qos_remove_request); EXPORT_SYMBOL_GPL(pm_qos_remove_request);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment