af_packet: prevent information leak
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / pm_qos_params.c
bloba9582efcba428bf21ad66ab061f6d019ad50bbcb
1 /*
2 * This module exposes the interface to kernel space for specifying
3 * QoS dependencies. It provides infrastructure for registration of:
5 * Dependents on a QoS value : register requests
6 * Watchers of QoS value : get notified when target QoS value changes
8 * This QoS design is best effort based. Dependents register their QoS needs.
9 * Watchers register to keep track of the current QoS needs of the system.
11 * There are 3 basic classes of QoS parameter: latency, timeout, throughput
12 * each have defined units:
13 * latency: usec
14 * timeout: usec <-- currently not used.
15 * throughput: kbs (kilo byte / sec)
17 * There are lists of pm_qos_objects each one wrapping requests, notifiers
19 * User mode requests on a QOS parameter register themselves to the
20 * subsystem by opening the device node /dev/... and writing there request to
21 * the node. As long as the process holds a file handle open to the node the
22 * client continues to be accounted for. Upon file release the usermode
23 * request is removed and a new qos target is computed. This way when the
24 * request that the application has is cleaned up when closes the file
25 * pointer or exits the pm_qos_object will get an opportunity to clean up.
27 * Mark Gross <mgross@linux.intel.com>
30 /*#define DEBUG*/
32 #include <linux/pm_qos_params.h>
33 #include <linux/sched.h>
34 #include <linux/spinlock.h>
35 #include <linux/slab.h>
36 #include <linux/time.h>
37 #include <linux/fs.h>
38 #include <linux/device.h>
39 #include <linux/miscdevice.h>
40 #include <linux/string.h>
41 #include <linux/platform_device.h>
42 #include <linux/init.h>
44 #include <linux/uaccess.h>
47 * locking rule: all changes to requests or notifiers lists
48 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
49 * held, taken with _irqsave. One lock to rule them all
51 enum pm_qos_type {
52 PM_QOS_MAX, /* return the largest value */
53 PM_QOS_MIN /* return the smallest value */
57 * Note: The lockless read path depends on the CPU accessing
58 * target_value atomically. Atomic access is only guaranteed on all CPU
59 * types linux supports for 32 bit quantites
61 struct pm_qos_object {
62 struct plist_head requests;
63 struct blocking_notifier_head *notifiers;
64 struct miscdevice pm_qos_power_miscdev;
65 char *name;
66 s32 target_value; /* Do not change to 64 bit */
67 s32 default_value;
68 enum pm_qos_type type;
71 static DEFINE_SPINLOCK(pm_qos_lock);
73 static struct pm_qos_object null_pm_qos;
74 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
75 static struct pm_qos_object cpu_dma_pm_qos = {
76 .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
77 .notifiers = &cpu_dma_lat_notifier,
78 .name = "cpu_dma_latency",
79 .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
80 .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
81 .type = PM_QOS_MIN,
84 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
85 static struct pm_qos_object network_lat_pm_qos = {
86 .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
87 .notifiers = &network_lat_notifier,
88 .name = "network_latency",
89 .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
90 .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
91 .type = PM_QOS_MIN
95 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
96 static struct pm_qos_object network_throughput_pm_qos = {
97 .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
98 .notifiers = &network_throughput_notifier,
99 .name = "network_throughput",
100 .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
101 .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
102 .type = PM_QOS_MAX,
106 static struct pm_qos_object *pm_qos_array[] = {
107 &null_pm_qos,
108 &cpu_dma_pm_qos,
109 &network_lat_pm_qos,
110 &network_throughput_pm_qos
113 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
114 size_t count, loff_t *f_pos);
115 static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
116 size_t count, loff_t *f_pos);
117 static int pm_qos_power_open(struct inode *inode, struct file *filp);
118 static int pm_qos_power_release(struct inode *inode, struct file *filp);
120 static const struct file_operations pm_qos_power_fops = {
121 .write = pm_qos_power_write,
122 .read = pm_qos_power_read,
123 .open = pm_qos_power_open,
124 .release = pm_qos_power_release,
125 .llseek = noop_llseek,
128 /* unlocked internal variant */
129 static inline int pm_qos_get_value(struct pm_qos_object *o)
131 if (plist_head_empty(&o->requests))
132 return o->default_value;
134 switch (o->type) {
135 case PM_QOS_MIN:
136 return plist_first(&o->requests)->prio;
138 case PM_QOS_MAX:
139 return plist_last(&o->requests)->prio;
141 default:
142 /* runtime check for not using enum */
143 BUG();
147 static inline s32 pm_qos_read_value(struct pm_qos_object *o)
149 return o->target_value;
152 static inline void pm_qos_set_value(struct pm_qos_object *o, s32 value)
154 o->target_value = value;
157 static void update_target(struct pm_qos_object *o, struct plist_node *node,
158 int del, int value)
160 unsigned long flags;
161 int prev_value, curr_value;
163 spin_lock_irqsave(&pm_qos_lock, flags);
164 prev_value = pm_qos_get_value(o);
165 /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
166 if (value != PM_QOS_DEFAULT_VALUE) {
168 * to change the list, we atomically remove, reinit
169 * with new value and add, then see if the extremal
170 * changed
172 plist_del(node, &o->requests);
173 plist_node_init(node, value);
174 plist_add(node, &o->requests);
175 } else if (del) {
176 plist_del(node, &o->requests);
177 } else {
178 plist_add(node, &o->requests);
180 curr_value = pm_qos_get_value(o);
181 pm_qos_set_value(o, curr_value);
182 spin_unlock_irqrestore(&pm_qos_lock, flags);
184 if (prev_value != curr_value)
185 blocking_notifier_call_chain(o->notifiers,
186 (unsigned long)curr_value,
187 NULL);
190 static int register_pm_qos_misc(struct pm_qos_object *qos)
192 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
193 qos->pm_qos_power_miscdev.name = qos->name;
194 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
196 return misc_register(&qos->pm_qos_power_miscdev);
199 static int find_pm_qos_object_by_minor(int minor)
201 int pm_qos_class;
203 for (pm_qos_class = 0;
204 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
205 if (minor ==
206 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
207 return pm_qos_class;
209 return -1;
213 * pm_qos_request - returns current system wide qos expectation
214 * @pm_qos_class: identification of which qos value is requested
216 * This function returns the current target value.
218 int pm_qos_request(int pm_qos_class)
220 return pm_qos_read_value(pm_qos_array[pm_qos_class]);
222 EXPORT_SYMBOL_GPL(pm_qos_request);
224 int pm_qos_request_active(struct pm_qos_request_list *req)
226 return req->pm_qos_class != 0;
228 EXPORT_SYMBOL_GPL(pm_qos_request_active);
231 * pm_qos_add_request - inserts new qos request into the list
232 * @dep: pointer to a preallocated handle
233 * @pm_qos_class: identifies which list of qos request to use
234 * @value: defines the qos request
236 * This function inserts a new entry in the pm_qos_class list of requested qos
237 * performance characteristics. It recomputes the aggregate QoS expectations
238 * for the pm_qos_class of parameters and initializes the pm_qos_request_list
239 * handle. Caller needs to save this handle for later use in updates and
240 * removal.
243 void pm_qos_add_request(struct pm_qos_request_list *dep,
244 int pm_qos_class, s32 value)
246 struct pm_qos_object *o = pm_qos_array[pm_qos_class];
247 int new_value;
249 if (pm_qos_request_active(dep)) {
250 WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
251 return;
253 if (value == PM_QOS_DEFAULT_VALUE)
254 new_value = o->default_value;
255 else
256 new_value = value;
257 plist_node_init(&dep->list, new_value);
258 dep->pm_qos_class = pm_qos_class;
259 update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
261 EXPORT_SYMBOL_GPL(pm_qos_add_request);
264 * pm_qos_update_request - modifies an existing qos request
265 * @pm_qos_req : handle to list element holding a pm_qos request to use
266 * @value: defines the qos request
268 * Updates an existing qos request for the pm_qos_class of parameters along
269 * with updating the target pm_qos_class value.
271 * Attempts are made to make this code callable on hot code paths.
273 void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
274 s32 new_value)
276 s32 temp;
277 struct pm_qos_object *o;
279 if (!pm_qos_req) /*guard against callers passing in null */
280 return;
282 if (!pm_qos_request_active(pm_qos_req)) {
283 WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
284 return;
287 o = pm_qos_array[pm_qos_req->pm_qos_class];
289 if (new_value == PM_QOS_DEFAULT_VALUE)
290 temp = o->default_value;
291 else
292 temp = new_value;
294 if (temp != pm_qos_req->list.prio)
295 update_target(o, &pm_qos_req->list, 0, temp);
297 EXPORT_SYMBOL_GPL(pm_qos_update_request);
300 * pm_qos_remove_request - modifies an existing qos request
301 * @pm_qos_req: handle to request list element
303 * Will remove pm qos request from the list of requests and
304 * recompute the current target value for the pm_qos_class. Call this
305 * on slow code paths.
307 void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
309 struct pm_qos_object *o;
311 if (pm_qos_req == NULL)
312 return;
313 /* silent return to keep pcm code cleaner */
315 if (!pm_qos_request_active(pm_qos_req)) {
316 WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
317 return;
320 o = pm_qos_array[pm_qos_req->pm_qos_class];
321 update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
322 memset(pm_qos_req, 0, sizeof(*pm_qos_req));
324 EXPORT_SYMBOL_GPL(pm_qos_remove_request);
327 * pm_qos_add_notifier - sets notification entry for changes to target value
328 * @pm_qos_class: identifies which qos target changes should be notified.
329 * @notifier: notifier block managed by caller.
331 * will register the notifier into a notification chain that gets called
332 * upon changes to the pm_qos_class target value.
334 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
336 int retval;
338 retval = blocking_notifier_chain_register(
339 pm_qos_array[pm_qos_class]->notifiers, notifier);
341 return retval;
343 EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
346 * pm_qos_remove_notifier - deletes notification entry from chain.
347 * @pm_qos_class: identifies which qos target changes are notified.
348 * @notifier: notifier block to be removed.
350 * will remove the notifier from the notification chain that gets called
351 * upon changes to the pm_qos_class target value.
353 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
355 int retval;
357 retval = blocking_notifier_chain_unregister(
358 pm_qos_array[pm_qos_class]->notifiers, notifier);
360 return retval;
362 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
364 static int pm_qos_power_open(struct inode *inode, struct file *filp)
366 long pm_qos_class;
368 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
369 if (pm_qos_class >= 0) {
370 struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL);
371 if (!req)
372 return -ENOMEM;
374 pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
375 filp->private_data = req;
377 if (filp->private_data)
378 return 0;
380 return -EPERM;
383 static int pm_qos_power_release(struct inode *inode, struct file *filp)
385 struct pm_qos_request_list *req;
387 req = filp->private_data;
388 pm_qos_remove_request(req);
389 kfree(req);
391 return 0;
395 static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
396 size_t count, loff_t *f_pos)
398 s32 value;
399 unsigned long flags;
400 struct pm_qos_object *o;
401 struct pm_qos_request_list *pm_qos_req = filp->private_data;;
403 if (!pm_qos_req)
404 return -EINVAL;
405 if (!pm_qos_request_active(pm_qos_req))
406 return -EINVAL;
408 o = pm_qos_array[pm_qos_req->pm_qos_class];
409 spin_lock_irqsave(&pm_qos_lock, flags);
410 value = pm_qos_get_value(o);
411 spin_unlock_irqrestore(&pm_qos_lock, flags);
413 return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
416 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
417 size_t count, loff_t *f_pos)
419 s32 value;
420 int x;
421 char ascii_value[11];
422 struct pm_qos_request_list *pm_qos_req;
424 if (count == sizeof(s32)) {
425 if (copy_from_user(&value, buf, sizeof(s32)))
426 return -EFAULT;
427 } else if (count == 11) { /* len('0x12345678/0') */
428 if (copy_from_user(ascii_value, buf, 11))
429 return -EFAULT;
430 if (strlen(ascii_value) != 10)
431 return -EINVAL;
432 x = sscanf(ascii_value, "%x", &value);
433 if (x != 1)
434 return -EINVAL;
435 pr_debug("%s, %d, 0x%x\n", ascii_value, x, value);
436 } else
437 return -EINVAL;
439 pm_qos_req = filp->private_data;
440 pm_qos_update_request(pm_qos_req, value);
442 return count;
446 static int __init pm_qos_power_init(void)
448 int ret = 0;
450 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
451 if (ret < 0) {
452 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
453 return ret;
455 ret = register_pm_qos_misc(&network_lat_pm_qos);
456 if (ret < 0) {
457 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
458 return ret;
460 ret = register_pm_qos_misc(&network_throughput_pm_qos);
461 if (ret < 0)
462 printk(KERN_ERR
463 "pm_qos_param: network_throughput setup failed\n");
465 return ret;
468 late_initcall(pm_qos_power_init);