PM: Do not use dynamically allocated objects in pm_wakeup_event()

Originally, pm_wakeup_event() uses struct delayed_work objects,
allocated with GFP_ATOMIC, to schedule the execution of pm_relax()
in future.  However, as noted by Alan Stern, it is not necessary to
do that, because all pm_wakeup_event() calls can use one static timer
that will always be set to expire at the latest time passed to
pm_wakeup_event().

The modifications are based on the example code posted by Alan.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
This commit is contained in:
Rafael J. Wysocki 2010-07-07 23:43:51 +02:00
parent ce4410116c
commit 4eb241e569

View File

@ -28,6 +28,11 @@ static unsigned long events_in_progress;
static DEFINE_SPINLOCK(events_lock); static DEFINE_SPINLOCK(events_lock);
static void pm_wakeup_timer_fn(unsigned long data);
static DEFINE_TIMER(events_timer, pm_wakeup_timer_fn, 0, 0);
static unsigned long events_timer_expires;
/* /*
* The functions below use the observation that each wakeup event starts a * The functions below use the observation that each wakeup event starts a
* period in which the system should not be suspended. The moment this period * period in which the system should not be suspended. The moment this period
@ -103,17 +108,22 @@ void pm_relax(void)
} }
/** /**
* pm_wakeup_work_fn - Deferred closing of a wakeup event. * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
* *
* Execute pm_relax() for a wakeup event detected in the past and free the * Decrease the counter of wakeup events being processed after it was increased
* work item object used for queuing up the work. * by pm_wakeup_event().
*/ */
static void pm_wakeup_work_fn(struct work_struct *work) static void pm_wakeup_timer_fn(unsigned long data)
{ {
struct delayed_work *dwork = to_delayed_work(work); unsigned long flags;
pm_relax(); spin_lock_irqsave(&events_lock, flags);
kfree(dwork); if (events_timer_expires
&& time_before_eq(events_timer_expires, jiffies)) {
events_in_progress--;
events_timer_expires = 0;
}
spin_unlock_irqrestore(&events_lock, flags);
} }
/** /**
@ -123,30 +133,38 @@ static void pm_wakeup_work_fn(struct work_struct *work)
* *
* Notify the PM core of a wakeup event (signaled by @dev) that will take * Notify the PM core of a wakeup event (signaled by @dev) that will take
* approximately @msec milliseconds to be processed by the kernel. Increment * approximately @msec milliseconds to be processed by the kernel. Increment
* the counter of wakeup events being processed and queue up a work item * the counter of registered wakeup events and (if @msec is nonzero) set up
* that will execute pm_relax() for the event after @msec milliseconds. If @dev * the wakeup events timer to execute pm_wakeup_timer_fn() in future (if the
* is not NULL, the counter of wakeup events related to @dev is incremented too. * timer has not been set up already, increment the counter of wakeup events
* being processed). If @dev is not NULL, the counter of wakeup events related
* to @dev is incremented too.
* *
* It is safe to call this function from interrupt context. * It is safe to call this function from interrupt context.
*/ */
void pm_wakeup_event(struct device *dev, unsigned int msec) void pm_wakeup_event(struct device *dev, unsigned int msec)
{ {
unsigned long flags; unsigned long flags;
struct delayed_work *dwork;
dwork = msec ? kzalloc(sizeof(*dwork), GFP_ATOMIC) : NULL;
spin_lock_irqsave(&events_lock, flags); spin_lock_irqsave(&events_lock, flags);
event_count++;
if (dev) if (dev)
dev->power.wakeup_count++; dev->power.wakeup_count++;
if (dwork) { if (msec) {
INIT_DELAYED_WORK(dwork, pm_wakeup_work_fn); unsigned long expires;
schedule_delayed_work(dwork, msecs_to_jiffies(msec));
expires = jiffies + msecs_to_jiffies(msec);
if (!expires)
expires = 1;
if (!events_timer_expires
|| time_after(expires, events_timer_expires)) {
if (!events_timer_expires)
events_in_progress++; events_in_progress++;
} else {
event_count++; mod_timer(&events_timer, expires);
events_timer_expires = expires;
}
} }
spin_unlock_irqrestore(&events_lock, flags); spin_unlock_irqrestore(&events_lock, flags);
} }