Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelv...
[cascardo/linux.git] / drivers / staging / unisys / visorutil / periodic_work.c
1 /* 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
30 struct 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
44 static 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
53 PERIODIC_WORK *visor_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 = kzalloc(sizeof(PERIODIC_WORK),
60                                                GFP_KERNEL | __GFP_NORETRY);
61         if (periodic_work == NULL) {
62                 ERRDRV("periodic_work allocation failed ");
63                 return NULL;
64         }
65         rwlock_init(&periodic_work->lock);
66         periodic_work->jiffy_interval = jiffy_interval;
67         periodic_work->workqueue = workqueue;
68         periodic_work->workfunc = workfunc;
69         periodic_work->workfuncarg = workfuncarg;
70         periodic_work->devnam = devnam;
71         return periodic_work;
72 }
73 EXPORT_SYMBOL_GPL(visor_periodic_work_create);
74
75
76
77 void visor_periodic_work_destroy(PERIODIC_WORK *periodic_work)
78 {
79         if (periodic_work == NULL)
80                 return;
81         kfree(periodic_work);
82 }
83 EXPORT_SYMBOL_GPL(visor_periodic_work_destroy);
84
85
86
87 /** Call this from your periodic work worker function to schedule the next
88  *  call.
89  *  If this function returns FALSE, there was a failure and the
90  *  periodic work is no longer scheduled
91  */
92 BOOL visor_periodic_work_nextperiod(PERIODIC_WORK *periodic_work)
93 {
94         BOOL rc = FALSE;
95         write_lock(&periodic_work->lock);
96         if (periodic_work->want_to_stop) {
97                 periodic_work->is_scheduled = FALSE;
98                 periodic_work->want_to_stop = FALSE;
99                 rc = TRUE;  /* yes, TRUE; see visor_periodic_work_stop() */
100                 goto Away;
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                 rc = FALSE;
107                 goto Away;
108         }
109         rc = TRUE;
110 Away:
111         write_unlock(&periodic_work->lock);
112         return rc;
113 }
114 EXPORT_SYMBOL_GPL(visor_periodic_work_nextperiod);
115
116
117
118 /** This function returns TRUE iff new periodic work was actually started.
119  *  If this function returns FALSE, then no work was started
120  *  (either because it was already started, or because of a failure).
121  */
122 BOOL visor_periodic_work_start(PERIODIC_WORK *periodic_work)
123 {
124         BOOL rc = FALSE;
125
126         write_lock(&periodic_work->lock);
127         if (periodic_work->is_scheduled) {
128                 rc = FALSE;
129                 goto Away;
130         }
131         if (periodic_work->want_to_stop) {
132                 ERRDEV(periodic_work->devnam,
133                        "dev_start_periodic_work failed!");
134                 rc = FALSE;
135                 goto Away;
136         }
137         INIT_DELAYED_WORK(&periodic_work->work, &periodic_work_func);
138         if (queue_delayed_work(periodic_work->workqueue,
139                                &periodic_work->work,
140                                periodic_work->jiffy_interval) < 0) {
141                 ERRDEV(periodic_work->devnam,
142                        "%s queue_delayed_work failed!", __func__);
143                 rc = FALSE;
144                 goto Away;
145         }
146         periodic_work->is_scheduled = TRUE;
147         rc = TRUE;
148 Away:
149         write_unlock(&periodic_work->lock);
150         return rc;
151
152 }
153 EXPORT_SYMBOL_GPL(visor_periodic_work_start);
154
155
156
157
158 /** This function returns TRUE iff your call actually stopped the periodic
159  *  work.
160  *
161  *  -- PAY ATTENTION... this is important --
162  *
163  *  NO NO #1
164  *
165  *     Do NOT call this function from some function that is running on the
166  *     same workqueue as the work you are trying to stop might be running
167  *     on!  If you violate this rule, visor_periodic_work_stop() MIGHT work,
168  *     but it also MIGHT get hung up in an infinite loop saying
169  *     "waiting for delayed work...".  This will happen if the delayed work
170  *     you are trying to cancel has been put in the workqueue list, but can't
171  *     run yet because we are running that same workqueue thread right now.
172  *
173  *     Bottom line: If you need to call visor_periodic_work_stop() from a
174  *     workitem, be sure the workitem is on a DIFFERENT workqueue than the
175  *     workitem that you are trying to cancel.
176  *
177  *     If I could figure out some way to check for this "no no" condition in
178  *     the code, I would.  It would have saved me the trouble of writing this
179  *     long comment.  And also, don't think this is some "theoretical" race
180  *     condition.  It is REAL, as I have spent the day chasing it.
181  *
182  *  NO NO #2
183  *
184  *     Take close note of the locks that you own when you call this function.
185  *     You must NOT own any locks that are needed by the periodic work
186  *     function that is currently installed.  If you DO, a deadlock may result,
187  *     because stopping the periodic work often involves waiting for the last
188  *     iteration of the periodic work function to complete.  Again, if you hit
189  *     this deadlock, you will get hung up in an infinite loop saying
190  *     "waiting for delayed work...".
191  */
192 BOOL visor_periodic_work_stop(PERIODIC_WORK *periodic_work)
193 {
194         BOOL stopped_something = FALSE;
195
196         write_lock(&periodic_work->lock);
197         stopped_something = periodic_work->is_scheduled &&
198                 (!periodic_work->want_to_stop);
199         while (periodic_work->is_scheduled) {
200                 periodic_work->want_to_stop = TRUE;
201                 if (cancel_delayed_work(&periodic_work->work)) {
202                         /* We get here if the delayed work was pending as
203                          * delayed work, but was NOT run.
204                          */
205                         ASSERT(periodic_work->is_scheduled);
206                         periodic_work->is_scheduled = FALSE;
207                 } else {
208                         /* If we get here, either the delayed work:
209                          * - was run, OR,
210                          * - is running RIGHT NOW on another processor, OR,
211                          * - wasn't even scheduled (there is a miniscule
212                          *   timing window where this could be the case)
213                          * flush_workqueue() would make sure it is finished
214                          * executing, but that still isn't very useful, which
215                          * explains the loop...
216                          */
217                 }
218                 if (periodic_work->is_scheduled) {
219                         write_unlock(&periodic_work->lock);
220                         WARNDEV(periodic_work->devnam,
221                                 "waiting for delayed work...");
222                         /* We rely on the delayed work function running here,
223                          * and eventually calling
224                          * visor_periodic_work_nextperiod(),
225                          * which will see that want_to_stop is set, and
226                          * subsequently clear is_scheduled.
227                          */
228                         SLEEPJIFFIES(10);
229                         write_lock(&periodic_work->lock);
230                 } else
231                         periodic_work->want_to_stop = FALSE;
232         }
233         write_unlock(&periodic_work->lock);
234         return stopped_something;
235 }
236 EXPORT_SYMBOL_GPL(visor_periodic_work_stop);