3 * Copyright (C) 2010 - 2015 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
18 * Helper functions to schedule periodic work in Linux kernel mode.
20 #include <linux/sched.h>
22 #include "periodic_work.h"
24 #define MYDRVNAME "periodic_work"
26 struct periodic_work {
28 struct delayed_work work;
29 void (*workfunc)(void *);
34 struct workqueue_struct *workqueue;
38 static void periodic_work_func(struct work_struct *work)
40 struct periodic_work *pw;
42 pw = container_of(work, struct periodic_work, work.work);
43 (*pw->workfunc)(pw->workfuncarg);
46 struct periodic_work *visor_periodic_work_create(ulong jiffy_interval,
47 struct workqueue_struct *workqueue,
48 void (*workfunc)(void *),
52 struct periodic_work *pw;
54 pw = kzalloc(sizeof(*pw), GFP_KERNEL | __GFP_NORETRY);
58 rwlock_init(&pw->lock);
59 pw->jiffy_interval = jiffy_interval;
60 pw->workqueue = workqueue;
61 pw->workfunc = workfunc;
62 pw->workfuncarg = workfuncarg;
66 EXPORT_SYMBOL_GPL(visor_periodic_work_create);
68 void visor_periodic_work_destroy(struct periodic_work *pw)
72 EXPORT_SYMBOL_GPL(visor_periodic_work_destroy);
74 /** Call this from your periodic work worker function to schedule the next
76 * If this function returns false, there was a failure and the
77 * periodic work is no longer scheduled
79 bool visor_periodic_work_nextperiod(struct periodic_work *pw)
83 write_lock(&pw->lock);
84 if (pw->want_to_stop) {
85 pw->is_scheduled = false;
86 pw->want_to_stop = false;
87 rc = true; /* yes, true; see visor_periodic_work_stop() */
89 } else if (!queue_delayed_work(pw->workqueue, &pw->work,
90 pw->jiffy_interval)) {
91 pw->is_scheduled = false;
97 write_unlock(&pw->lock);
100 EXPORT_SYMBOL_GPL(visor_periodic_work_nextperiod);
102 /** This function returns true iff new periodic work was actually started.
103 * If this function returns false, then no work was started
104 * (either because it was already started, or because of a failure).
106 bool visor_periodic_work_start(struct periodic_work *pw)
110 write_lock(&pw->lock);
111 if (pw->is_scheduled) {
115 if (pw->want_to_stop) {
119 INIT_DELAYED_WORK(&pw->work, &periodic_work_func);
120 if (!queue_delayed_work(pw->workqueue, &pw->work,
121 pw->jiffy_interval)) {
125 pw->is_scheduled = true;
128 write_unlock(&pw->lock);
131 EXPORT_SYMBOL_GPL(visor_periodic_work_start);
133 /** This function returns true iff your call actually stopped the periodic
136 * -- PAY ATTENTION... this is important --
140 * Do NOT call this function from some function that is running on the
141 * same workqueue as the work you are trying to stop might be running
142 * on! If you violate this rule, visor_periodic_work_stop() MIGHT work,
143 * but it also MIGHT get hung up in an infinite loop saying
144 * "waiting for delayed work...". This will happen if the delayed work
145 * you are trying to cancel has been put in the workqueue list, but can't
146 * run yet because we are running that same workqueue thread right now.
148 * Bottom line: If you need to call visor_periodic_work_stop() from a
149 * workitem, be sure the workitem is on a DIFFERENT workqueue than the
150 * workitem that you are trying to cancel.
152 * If I could figure out some way to check for this "no no" condition in
153 * the code, I would. It would have saved me the trouble of writing this
154 * long comment. And also, don't think this is some "theoretical" race
155 * condition. It is REAL, as I have spent the day chasing it.
159 * Take close note of the locks that you own when you call this function.
160 * You must NOT own any locks that are needed by the periodic work
161 * function that is currently installed. If you DO, a deadlock may result,
162 * because stopping the periodic work often involves waiting for the last
163 * iteration of the periodic work function to complete. Again, if you hit
164 * this deadlock, you will get hung up in an infinite loop saying
165 * "waiting for delayed work...".
167 bool visor_periodic_work_stop(struct periodic_work *pw)
169 bool stopped_something = false;
171 write_lock(&pw->lock);
172 stopped_something = pw->is_scheduled && (!pw->want_to_stop);
173 while (pw->is_scheduled) {
174 pw->want_to_stop = true;
175 if (cancel_delayed_work(&pw->work)) {
176 /* We get here if the delayed work was pending as
177 * delayed work, but was NOT run.
179 WARN_ON(!pw->is_scheduled);
180 pw->is_scheduled = false;
182 /* If we get here, either the delayed work:
184 * - is running RIGHT NOW on another processor, OR,
185 * - wasn't even scheduled (there is a miniscule
186 * timing window where this could be the case)
187 * flush_workqueue() would make sure it is finished
188 * executing, but that still isn't very useful, which
189 * explains the loop...
192 if (pw->is_scheduled) {
193 write_unlock(&pw->lock);
194 schedule_timeout_interruptible(msecs_to_jiffies(10));
195 write_lock(&pw->lock);
197 pw->want_to_stop = false;
200 write_unlock(&pw->lock);
201 return stopped_something;
203 EXPORT_SYMBOL_GPL(visor_periodic_work_stop);