2014-03-04 06:58:05 -07:00
|
|
|
/* periodic_work.c
|
|
|
|
*
|
2015-07-16 10:40:48 -06:00
|
|
|
* Copyright (C) 2010 - 2015 UNISYS CORPORATION
|
2014-03-04 06:58:05 -07:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2015-07-16 10:40:48 -06:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
2014-03-04 06:58:05 -07:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
|
|
|
|
* NON INFRINGEMENT. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper functions to schedule periodic work in Linux kernel mode.
|
|
|
|
*/
|
2015-05-05 16:37:04 -06:00
|
|
|
#include <linux/sched.h>
|
2014-03-04 06:58:05 -07:00
|
|
|
|
|
|
|
#include "periodic_work.h"
|
|
|
|
|
|
|
|
#define MYDRVNAME "periodic_work"
|
|
|
|
|
2014-09-30 10:07:46 -06:00
|
|
|
struct periodic_work {
|
2014-03-04 06:58:05 -07:00
|
|
|
rwlock_t lock;
|
|
|
|
struct delayed_work work;
|
|
|
|
void (*workfunc)(void *);
|
|
|
|
void *workfuncarg;
|
2015-05-05 16:37:01 -06:00
|
|
|
bool is_scheduled;
|
|
|
|
bool want_to_stop;
|
2014-03-04 06:58:05 -07:00
|
|
|
ulong jiffy_interval;
|
|
|
|
struct workqueue_struct *workqueue;
|
|
|
|
const char *devnam;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void periodic_work_func(struct work_struct *work)
|
|
|
|
{
|
2014-09-30 10:07:46 -06:00
|
|
|
struct periodic_work *pw;
|
|
|
|
|
|
|
|
pw = container_of(work, struct periodic_work, work.work);
|
|
|
|
(*pw->workfunc)(pw->workfuncarg);
|
2014-03-04 06:58:05 -07:00
|
|
|
}
|
|
|
|
|
2015-11-17 11:34:58 -07:00
|
|
|
struct periodic_work
|
|
|
|
*visor_periodic_work_create(ulong jiffy_interval,
|
|
|
|
struct workqueue_struct *workqueue,
|
|
|
|
void (*workfunc)(void *),
|
|
|
|
void *workfuncarg,
|
|
|
|
const char *devnam)
|
2014-03-04 06:58:05 -07:00
|
|
|
{
|
2014-09-30 10:07:46 -06:00
|
|
|
struct periodic_work *pw;
|
|
|
|
|
|
|
|
pw = kzalloc(sizeof(*pw), GFP_KERNEL | __GFP_NORETRY);
|
|
|
|
if (!pw)
|
2014-03-04 06:58:05 -07:00
|
|
|
return NULL;
|
2014-09-30 10:07:46 -06:00
|
|
|
|
|
|
|
rwlock_init(&pw->lock);
|
|
|
|
pw->jiffy_interval = jiffy_interval;
|
|
|
|
pw->workqueue = workqueue;
|
|
|
|
pw->workfunc = workfunc;
|
|
|
|
pw->workfuncarg = workfuncarg;
|
|
|
|
pw->devnam = devnam;
|
|
|
|
return pw;
|
2014-03-04 06:58:05 -07:00
|
|
|
}
|
2014-03-05 13:52:25 -07:00
|
|
|
EXPORT_SYMBOL_GPL(visor_periodic_work_create);
|
2014-03-04 06:58:05 -07:00
|
|
|
|
2014-09-30 10:07:46 -06:00
|
|
|
void visor_periodic_work_destroy(struct periodic_work *pw)
|
2014-03-04 06:58:05 -07:00
|
|
|
{
|
2014-09-30 10:07:46 -06:00
|
|
|
kfree(pw);
|
2014-03-04 06:58:05 -07:00
|
|
|
}
|
2014-03-05 13:52:25 -07:00
|
|
|
EXPORT_SYMBOL_GPL(visor_periodic_work_destroy);
|
2014-03-04 06:58:05 -07:00
|
|
|
|
|
|
|
/** Call this from your periodic work worker function to schedule the next
|
|
|
|
* call.
|
2015-05-05 16:37:01 -06:00
|
|
|
* If this function returns false, there was a failure and the
|
2014-03-04 06:58:05 -07:00
|
|
|
* periodic work is no longer scheduled
|
|
|
|
*/
|
2015-05-05 16:37:01 -06:00
|
|
|
bool visor_periodic_work_nextperiod(struct periodic_work *pw)
|
2014-03-04 06:58:05 -07:00
|
|
|
{
|
2015-05-05 16:37:01 -06:00
|
|
|
bool rc = false;
|
2014-05-17 14:32:53 -06:00
|
|
|
|
2014-09-30 10:07:46 -06:00
|
|
|
write_lock(&pw->lock);
|
|
|
|
if (pw->want_to_stop) {
|
2015-05-05 16:37:01 -06:00
|
|
|
pw->is_scheduled = false;
|
|
|
|
pw->want_to_stop = false;
|
|
|
|
rc = true; /* yes, true; see visor_periodic_work_stop() */
|
2014-09-30 10:07:46 -06:00
|
|
|
goto unlock;
|
2015-10-01 09:52:30 -06:00
|
|
|
} else if (!queue_delayed_work(pw->workqueue, &pw->work,
|
|
|
|
pw->jiffy_interval)) {
|
2015-05-05 16:37:01 -06:00
|
|
|
pw->is_scheduled = false;
|
|
|
|
rc = false;
|
2014-09-30 10:07:46 -06:00
|
|
|
goto unlock;
|
2014-03-04 06:58:05 -07:00
|
|
|
}
|
2015-05-05 16:37:01 -06:00
|
|
|
rc = true;
|
2014-09-30 10:07:46 -06:00
|
|
|
unlock:
|
|
|
|
write_unlock(&pw->lock);
|
2014-03-04 06:58:05 -07:00
|
|
|
return rc;
|
|
|
|
}
|
2014-03-05 13:52:25 -07:00
|
|
|
EXPORT_SYMBOL_GPL(visor_periodic_work_nextperiod);
|
2014-03-04 06:58:05 -07:00
|
|
|
|
2015-05-05 16:37:01 -06:00
|
|
|
/** This function returns true iff new periodic work was actually started.
|
|
|
|
* If this function returns false, then no work was started
|
2014-03-04 06:58:05 -07:00
|
|
|
* (either because it was already started, or because of a failure).
|
|
|
|
*/
|
2015-05-05 16:37:01 -06:00
|
|
|
bool visor_periodic_work_start(struct periodic_work *pw)
|
2014-03-04 06:58:05 -07:00
|
|
|
{
|
2015-05-05 16:37:01 -06:00
|
|
|
bool rc = false;
|
2014-03-04 06:58:05 -07:00
|
|
|
|
2014-09-30 10:07:46 -06:00
|
|
|
write_lock(&pw->lock);
|
|
|
|
if (pw->is_scheduled) {
|
2015-05-05 16:37:01 -06:00
|
|
|
rc = false;
|
2014-09-30 10:07:46 -06:00
|
|
|
goto unlock;
|
2014-03-19 12:06:21 -06:00
|
|
|
}
|
2014-09-30 10:07:46 -06:00
|
|
|
if (pw->want_to_stop) {
|
2015-05-05 16:37:01 -06:00
|
|
|
rc = false;
|
2014-09-30 10:07:46 -06:00
|
|
|
goto unlock;
|
2014-03-04 06:58:05 -07:00
|
|
|
}
|
2014-09-30 10:07:46 -06:00
|
|
|
INIT_DELAYED_WORK(&pw->work, &periodic_work_func);
|
2015-10-01 09:52:30 -06:00
|
|
|
if (!queue_delayed_work(pw->workqueue, &pw->work,
|
|
|
|
pw->jiffy_interval)) {
|
2015-05-05 16:37:01 -06:00
|
|
|
rc = false;
|
2014-09-30 10:07:46 -06:00
|
|
|
goto unlock;
|
2014-03-04 06:58:05 -07:00
|
|
|
}
|
2015-05-05 16:37:01 -06:00
|
|
|
pw->is_scheduled = true;
|
|
|
|
rc = true;
|
2014-09-30 10:07:46 -06:00
|
|
|
unlock:
|
|
|
|
write_unlock(&pw->lock);
|
2014-03-04 06:58:05 -07:00
|
|
|
return rc;
|
|
|
|
}
|
2014-03-05 13:52:25 -07:00
|
|
|
EXPORT_SYMBOL_GPL(visor_periodic_work_start);
|
2014-03-04 06:58:05 -07:00
|
|
|
|
2015-05-05 16:37:01 -06:00
|
|
|
/** This function returns true iff your call actually stopped the periodic
|
2014-03-04 06:58:05 -07:00
|
|
|
* work.
|
|
|
|
*
|
|
|
|
* -- PAY ATTENTION... this is important --
|
|
|
|
*
|
|
|
|
* NO NO #1
|
|
|
|
*
|
|
|
|
* Do NOT call this function from some function that is running on the
|
|
|
|
* same workqueue as the work you are trying to stop might be running
|
2014-03-05 13:52:25 -07:00
|
|
|
* on! If you violate this rule, visor_periodic_work_stop() MIGHT work,
|
|
|
|
* but it also MIGHT get hung up in an infinite loop saying
|
2014-03-04 06:58:05 -07:00
|
|
|
* "waiting for delayed work...". This will happen if the delayed work
|
|
|
|
* you are trying to cancel has been put in the workqueue list, but can't
|
|
|
|
* run yet because we are running that same workqueue thread right now.
|
|
|
|
*
|
2014-03-05 13:52:25 -07:00
|
|
|
* Bottom line: If you need to call visor_periodic_work_stop() from a
|
|
|
|
* workitem, be sure the workitem is on a DIFFERENT workqueue than the
|
|
|
|
* workitem that you are trying to cancel.
|
2014-03-04 06:58:05 -07:00
|
|
|
*
|
|
|
|
* If I could figure out some way to check for this "no no" condition in
|
|
|
|
* the code, I would. It would have saved me the trouble of writing this
|
|
|
|
* long comment. And also, don't think this is some "theoretical" race
|
|
|
|
* condition. It is REAL, as I have spent the day chasing it.
|
|
|
|
*
|
|
|
|
* NO NO #2
|
|
|
|
*
|
|
|
|
* Take close note of the locks that you own when you call this function.
|
|
|
|
* You must NOT own any locks that are needed by the periodic work
|
|
|
|
* function that is currently installed. If you DO, a deadlock may result,
|
|
|
|
* because stopping the periodic work often involves waiting for the last
|
|
|
|
* iteration of the periodic work function to complete. Again, if you hit
|
|
|
|
* this deadlock, you will get hung up in an infinite loop saying
|
|
|
|
* "waiting for delayed work...".
|
|
|
|
*/
|
2015-05-05 16:37:01 -06:00
|
|
|
bool visor_periodic_work_stop(struct periodic_work *pw)
|
2014-03-04 06:58:05 -07:00
|
|
|
{
|
2015-05-05 16:37:01 -06:00
|
|
|
bool stopped_something = false;
|
2014-03-04 06:58:05 -07:00
|
|
|
|
2014-09-30 10:07:46 -06:00
|
|
|
write_lock(&pw->lock);
|
|
|
|
stopped_something = pw->is_scheduled && (!pw->want_to_stop);
|
|
|
|
while (pw->is_scheduled) {
|
2015-05-05 16:37:01 -06:00
|
|
|
pw->want_to_stop = true;
|
2014-09-30 10:07:46 -06:00
|
|
|
if (cancel_delayed_work(&pw->work)) {
|
2014-03-04 06:58:05 -07:00
|
|
|
/* We get here if the delayed work was pending as
|
|
|
|
* delayed work, but was NOT run.
|
|
|
|
*/
|
2015-03-04 10:14:23 -07:00
|
|
|
WARN_ON(!pw->is_scheduled);
|
2015-05-05 16:37:01 -06:00
|
|
|
pw->is_scheduled = false;
|
2014-03-04 06:58:05 -07:00
|
|
|
} else {
|
|
|
|
/* If we get here, either the delayed work:
|
|
|
|
* - was run, OR,
|
|
|
|
* - is running RIGHT NOW on another processor, OR,
|
|
|
|
* - wasn't even scheduled (there is a miniscule
|
|
|
|
* timing window where this could be the case)
|
|
|
|
* flush_workqueue() would make sure it is finished
|
|
|
|
* executing, but that still isn't very useful, which
|
|
|
|
* explains the loop...
|
|
|
|
*/
|
|
|
|
}
|
2014-09-30 10:07:46 -06:00
|
|
|
if (pw->is_scheduled) {
|
|
|
|
write_unlock(&pw->lock);
|
2015-05-29 09:31:16 -06:00
|
|
|
schedule_timeout_interruptible(msecs_to_jiffies(10));
|
2014-09-30 10:07:46 -06:00
|
|
|
write_lock(&pw->lock);
|
2014-11-04 09:25:22 -07:00
|
|
|
} else {
|
2015-05-05 16:37:01 -06:00
|
|
|
pw->want_to_stop = false;
|
2014-11-04 09:25:22 -07:00
|
|
|
}
|
2014-03-04 06:58:05 -07:00
|
|
|
}
|
2014-09-30 10:07:46 -06:00
|
|
|
write_unlock(&pw->lock);
|
2014-03-04 06:58:05 -07:00
|
|
|
return stopped_something;
|
|
|
|
}
|
2014-03-05 13:52:25 -07:00
|
|
|
EXPORT_SYMBOL_GPL(visor_periodic_work_stop);
|