1 /* periodic_work.c
2  *
3  * Copyright (C) 2010 - 2015 UNISYS CORPORATION
4  * All rights reserved.
5  *
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.
9  *
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
14  * details.
15  */
16 
17 /*
18  *  Helper functions to schedule periodic work in Linux kernel mode.
19  */
20 #include <linux/sched.h>
21 
22 #include "periodic_work.h"
23 
24 #define MYDRVNAME "periodic_work"
25 
26 struct periodic_work {
27 	rwlock_t lock;
28 	struct delayed_work work;
29 	void (*workfunc)(void *);
30 	void *workfuncarg;
31 	bool is_scheduled;
32 	bool want_to_stop;
33 	ulong jiffy_interval;
34 	struct workqueue_struct *workqueue;
35 	const char *devnam;
36 };
37 
periodic_work_func(struct work_struct * work)38 static void periodic_work_func(struct work_struct *work)
39 {
40 	struct periodic_work *pw;
41 
42 	pw = container_of(work, struct periodic_work, work.work);
43 	(*pw->workfunc)(pw->workfuncarg);
44 }
45 
visor_periodic_work_create(ulong jiffy_interval,struct workqueue_struct * workqueue,void (* workfunc)(void *),void * workfuncarg,const char * devnam)46 struct periodic_work *visor_periodic_work_create(ulong jiffy_interval,
47 					struct workqueue_struct *workqueue,
48 					void (*workfunc)(void *),
49 					void *workfuncarg,
50 					const char *devnam)
51 {
52 	struct periodic_work *pw;
53 
54 	pw = kzalloc(sizeof(*pw), GFP_KERNEL | __GFP_NORETRY);
55 	if (!pw)
56 		return NULL;
57 
58 	rwlock_init(&pw->lock);
59 	pw->jiffy_interval = jiffy_interval;
60 	pw->workqueue = workqueue;
61 	pw->workfunc = workfunc;
62 	pw->workfuncarg = workfuncarg;
63 	pw->devnam = devnam;
64 	return pw;
65 }
66 EXPORT_SYMBOL_GPL(visor_periodic_work_create);
67 
visor_periodic_work_destroy(struct periodic_work * pw)68 void visor_periodic_work_destroy(struct periodic_work *pw)
69 {
70 	kfree(pw);
71 }
72 EXPORT_SYMBOL_GPL(visor_periodic_work_destroy);
73 
74 /** Call this from your periodic work worker function to schedule the next
75  *  call.
76  *  If this function returns false, there was a failure and the
77  *  periodic work is no longer scheduled
78  */
visor_periodic_work_nextperiod(struct periodic_work * pw)79 bool visor_periodic_work_nextperiod(struct periodic_work *pw)
80 {
81 	bool rc = false;
82 
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() */
88 		goto unlock;
89 	} else if (!queue_delayed_work(pw->workqueue, &pw->work,
90 				       pw->jiffy_interval)) {
91 		pw->is_scheduled = false;
92 		rc = false;
93 		goto unlock;
94 	}
95 	rc = true;
96 unlock:
97 	write_unlock(&pw->lock);
98 	return rc;
99 }
100 EXPORT_SYMBOL_GPL(visor_periodic_work_nextperiod);
101 
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).
105  */
visor_periodic_work_start(struct periodic_work * pw)106 bool visor_periodic_work_start(struct periodic_work *pw)
107 {
108 	bool rc = false;
109 
110 	write_lock(&pw->lock);
111 	if (pw->is_scheduled) {
112 		rc = false;
113 		goto unlock;
114 	}
115 	if (pw->want_to_stop) {
116 		rc = false;
117 		goto unlock;
118 	}
119 	INIT_DELAYED_WORK(&pw->work, &periodic_work_func);
120 	if (!queue_delayed_work(pw->workqueue, &pw->work,
121 				pw->jiffy_interval)) {
122 		rc = false;
123 		goto unlock;
124 	}
125 	pw->is_scheduled = true;
126 	rc = true;
127 unlock:
128 	write_unlock(&pw->lock);
129 	return rc;
130 }
131 EXPORT_SYMBOL_GPL(visor_periodic_work_start);
132 
133 /** This function returns true iff your call actually stopped the periodic
134  *  work.
135  *
136  *  -- PAY ATTENTION... this is important --
137  *
138  *  NO NO #1
139  *
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.
147  *
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.
151  *
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.
156  *
157  *  NO NO #2
158  *
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...".
166  */
visor_periodic_work_stop(struct periodic_work * pw)167 bool visor_periodic_work_stop(struct periodic_work *pw)
168 {
169 	bool stopped_something = false;
170 
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.
178 			 */
179 			WARN_ON(!pw->is_scheduled);
180 			pw->is_scheduled = false;
181 		} else {
182 			/* If we get here, either the delayed work:
183 			 * - was run, OR,
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...
190 			 */
191 		}
192 		if (pw->is_scheduled) {
193 			write_unlock(&pw->lock);
194 			schedule_timeout_interruptible(msecs_to_jiffies(10));
195 			write_lock(&pw->lock);
196 		} else {
197 			pw->want_to_stop = false;
198 		}
199 	}
200 	write_unlock(&pw->lock);
201 	return stopped_something;
202 }
203 EXPORT_SYMBOL_GPL(visor_periodic_work_stop);
204