blob: 1350b8ef59a30f2338d61d6eaa422f8d6649b55c [file] [log] [blame]
Ken Cox9d9baad2014-03-04 07:58:05 -06001/* periodic_work.c
2 *
3 * Copyright © 2010 - 2013 UNISYS CORPORATION
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
15 * details.
16 */
17
18/*
19 * Helper functions to schedule periodic work in Linux kernel mode.
20 */
21
22#include "uniklog.h"
23#include "timskmod.h"
24#include "periodic_work.h"
25
26#define MYDRVNAME "periodic_work"
27
28
29
30struct PERIODIC_WORK_Tag {
31 rwlock_t lock;
32 struct delayed_work work;
33 void (*workfunc)(void *);
34 void *workfuncarg;
35 BOOL is_scheduled;
36 BOOL want_to_stop;
37 ulong jiffy_interval;
38 struct workqueue_struct *workqueue;
39 const char *devnam;
40};
41
42
43
44static void periodic_work_func(struct work_struct *work)
45{
46 PERIODIC_WORK *periodic_work =
47 container_of(work, struct PERIODIC_WORK_Tag, work.work);
48 (*periodic_work->workfunc)(periodic_work->workfuncarg);
49}
50
51
52
53PERIODIC_WORK *periodic_work_create(ulong jiffy_interval,
54 struct workqueue_struct *workqueue,
55 void (*workfunc)(void *),
56 void *workfuncarg,
57 const char *devnam)
58{
59 PERIODIC_WORK *periodic_work = kmalloc(sizeof(PERIODIC_WORK),
60 GFP_KERNEL|__GFP_NORETRY);
61 if (periodic_work == NULL) {
62 ERRDRV("periodic_work allocation failed ");
63 return NULL;
64 }
65 memset(periodic_work, '\0', sizeof(PERIODIC_WORK));
66 rwlock_init(&periodic_work->lock);
67 periodic_work->jiffy_interval = jiffy_interval;
68 periodic_work->workqueue = workqueue;
69 periodic_work->workfunc = workfunc;
70 periodic_work->workfuncarg = workfuncarg;
71 periodic_work->devnam = devnam;
72 return periodic_work;
73}
74EXPORT_SYMBOL_GPL(periodic_work_create);
75
76
77
78void periodic_work_destroy(PERIODIC_WORK *periodic_work)
79{
80 if (periodic_work == NULL)
81 return;
82 kfree(periodic_work);
83}
84EXPORT_SYMBOL_GPL(periodic_work_destroy);
85
86
87
88/** Call this from your periodic work worker function to schedule the next
89 * call.
90 * If this function returns FALSE, there was a failure and the
91 * periodic work is no longer scheduled
92 */
93BOOL periodic_work_nextperiod(PERIODIC_WORK *periodic_work)
94{
95 BOOL rc = FALSE;
96 write_lock(&periodic_work->lock);
97 if (periodic_work->want_to_stop) {
98 periodic_work->is_scheduled = FALSE;
99 periodic_work->want_to_stop = FALSE;
100 RETBOOL(TRUE); /* yes, TRUE; see periodic_work_stop() */
101 } else if (queue_delayed_work(periodic_work->workqueue,
102 &periodic_work->work,
103 periodic_work->jiffy_interval) < 0) {
104 ERRDEV(periodic_work->devnam, "queue_delayed_work failed!");
105 periodic_work->is_scheduled = FALSE;
106 RETBOOL(FALSE);
107 }
108 RETBOOL(TRUE);
109Away:
110 write_unlock(&periodic_work->lock);
111 return rc;
112}
113EXPORT_SYMBOL_GPL(periodic_work_nextperiod);
114
115
116
117/** This function returns TRUE iff new periodic work was actually started.
118 * If this function returns FALSE, then no work was started
119 * (either because it was already started, or because of a failure).
120 */
121BOOL periodic_work_start(PERIODIC_WORK *periodic_work)
122{
123 BOOL rc = FALSE;
124
125 write_lock(&periodic_work->lock);
126 if (periodic_work->is_scheduled)
127 RETBOOL(FALSE);
128 if (periodic_work->want_to_stop) {
129 ERRDEV(periodic_work->devnam,
130 "dev_start_periodic_work failed!");
131 RETBOOL(FALSE);
132 }
133 INIT_DELAYED_WORK(&periodic_work->work, &periodic_work_func);
134 if (queue_delayed_work(periodic_work->workqueue,
135 &periodic_work->work,
136 periodic_work->jiffy_interval) < 0) {
137 ERRDEV(periodic_work->devnam,
138 "%s queue_delayed_work failed!", __func__);
139 RETBOOL(FALSE);
140 }
141 periodic_work->is_scheduled = TRUE;
142 RETBOOL(TRUE);
143Away:
144 write_unlock(&periodic_work->lock);
145 return rc;
146
147}
148EXPORT_SYMBOL_GPL(periodic_work_start);
149
150
151
152
153/** This function returns TRUE iff your call actually stopped the periodic
154 * work.
155 *
156 * -- PAY ATTENTION... this is important --
157 *
158 * NO NO #1
159 *
160 * Do NOT call this function from some function that is running on the
161 * same workqueue as the work you are trying to stop might be running
162 * on! If you violate this rule, periodic_work_stop() MIGHT work, but it
163 * also MIGHT get hung up in an infinite loop saying
164 * "waiting for delayed work...". This will happen if the delayed work
165 * you are trying to cancel has been put in the workqueue list, but can't
166 * run yet because we are running that same workqueue thread right now.
167 *
168 * Bottom line: If you need to call periodic_work_stop() from a workitem,
169 * be sure the workitem is on a DIFFERENT workqueue than the workitem that
170 * you are trying to cancel.
171 *
172 * If I could figure out some way to check for this "no no" condition in
173 * the code, I would. It would have saved me the trouble of writing this
174 * long comment. And also, don't think this is some "theoretical" race
175 * condition. It is REAL, as I have spent the day chasing it.
176 *
177 * NO NO #2
178 *
179 * Take close note of the locks that you own when you call this function.
180 * You must NOT own any locks that are needed by the periodic work
181 * function that is currently installed. If you DO, a deadlock may result,
182 * because stopping the periodic work often involves waiting for the last
183 * iteration of the periodic work function to complete. Again, if you hit
184 * this deadlock, you will get hung up in an infinite loop saying
185 * "waiting for delayed work...".
186 */
187BOOL periodic_work_stop(PERIODIC_WORK *periodic_work)
188{
189 BOOL stopped_something = FALSE;
190
191 write_lock(&periodic_work->lock);
192 stopped_something = periodic_work->is_scheduled &&
193 (!periodic_work->want_to_stop);
194 while (periodic_work->is_scheduled) {
195 periodic_work->want_to_stop = TRUE;
196 if (cancel_delayed_work(&periodic_work->work)) {
197 /* We get here if the delayed work was pending as
198 * delayed work, but was NOT run.
199 */
200 ASSERT(periodic_work->is_scheduled);
201 periodic_work->is_scheduled = FALSE;
202 } else {
203 /* If we get here, either the delayed work:
204 * - was run, OR,
205 * - is running RIGHT NOW on another processor, OR,
206 * - wasn't even scheduled (there is a miniscule
207 * timing window where this could be the case)
208 * flush_workqueue() would make sure it is finished
209 * executing, but that still isn't very useful, which
210 * explains the loop...
211 */
212 }
213 if (periodic_work->is_scheduled) {
214 write_unlock(&periodic_work->lock);
215 WARNDEV(periodic_work->devnam,
216 "waiting for delayed work...");
217 /* We rely on the delayed work function running here,
218 * and eventually calling periodic_work_nextperiod(),
219 * which will see that want_to_stop is set, and
220 * subsequently clear is_scheduled.
221 */
222 SLEEPJIFFIES(10);
223 write_lock(&periodic_work->lock);
224 } else
225 periodic_work->want_to_stop = FALSE;
226 }
227 write_unlock(&periodic_work->lock);
228 return stopped_something;
229}
230EXPORT_SYMBOL_GPL(periodic_work_stop);