blob: 00b152764f84c8de01c77d311899fcb82b9c5822 [file] [log] [blame]
Ken Cox9d9baad2014-03-04 07:58:05 -06001/* periodic_work.c
2 *
Benjamin Romer6f14cc12015-07-16 12:40:48 -04003 * Copyright (C) 2010 - 2015 UNISYS CORPORATION
Ken Cox9d9baad2014-03-04 07:58:05 -06004 * All rights reserved.
5 *
Benjamin Romer6f14cc12015-07-16 12:40:48 -04006 * 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.
Ken Cox9d9baad2014-03-04 07:58:05 -06009 *
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 */
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -040020#include <linux/sched.h>
Ken Cox9d9baad2014-03-04 07:58:05 -060021
Ken Cox9d9baad2014-03-04 07:58:05 -060022#include "periodic_work.h"
23
24#define MYDRVNAME "periodic_work"
25
Benjamin Romer2c5653b2014-09-30 12:07:46 -040026struct periodic_work {
Ken Cox9d9baad2014-03-04 07:58:05 -060027 rwlock_t lock;
28 struct delayed_work work;
29 void (*workfunc)(void *);
30 void *workfuncarg;
Prarit Bhargava779d0752015-05-05 18:37:01 -040031 bool is_scheduled;
32 bool want_to_stop;
Ken Cox9d9baad2014-03-04 07:58:05 -060033 ulong jiffy_interval;
34 struct workqueue_struct *workqueue;
35 const char *devnam;
36};
37
Ken Cox9d9baad2014-03-04 07:58:05 -060038static void periodic_work_func(struct work_struct *work)
39{
Benjamin Romer2c5653b2014-09-30 12:07:46 -040040 struct periodic_work *pw;
41
42 pw = container_of(work, struct periodic_work, work.work);
43 (*pw->workfunc)(pw->workfuncarg);
Ken Cox9d9baad2014-03-04 07:58:05 -060044}
45
Erik Arfvidson3d057342015-11-17 13:34:58 -050046struct periodic_work
47*visor_periodic_work_create(ulong jiffy_interval,
48 struct workqueue_struct *workqueue,
49 void (*workfunc)(void *),
50 void *workfuncarg,
51 const char *devnam)
Ken Cox9d9baad2014-03-04 07:58:05 -060052{
Benjamin Romer2c5653b2014-09-30 12:07:46 -040053 struct periodic_work *pw;
54
55 pw = kzalloc(sizeof(*pw), GFP_KERNEL | __GFP_NORETRY);
56 if (!pw)
Ken Cox9d9baad2014-03-04 07:58:05 -060057 return NULL;
Benjamin Romer2c5653b2014-09-30 12:07:46 -040058
59 rwlock_init(&pw->lock);
60 pw->jiffy_interval = jiffy_interval;
61 pw->workqueue = workqueue;
62 pw->workfunc = workfunc;
63 pw->workfuncarg = workfuncarg;
64 pw->devnam = devnam;
65 return pw;
Ken Cox9d9baad2014-03-04 07:58:05 -060066}
Ken Cox927c7922014-03-05 14:52:25 -060067EXPORT_SYMBOL_GPL(visor_periodic_work_create);
Ken Cox9d9baad2014-03-04 07:58:05 -060068
Benjamin Romer2c5653b2014-09-30 12:07:46 -040069void visor_periodic_work_destroy(struct periodic_work *pw)
Ken Cox9d9baad2014-03-04 07:58:05 -060070{
Benjamin Romer2c5653b2014-09-30 12:07:46 -040071 kfree(pw);
Ken Cox9d9baad2014-03-04 07:58:05 -060072}
Ken Cox927c7922014-03-05 14:52:25 -060073EXPORT_SYMBOL_GPL(visor_periodic_work_destroy);
Ken Cox9d9baad2014-03-04 07:58:05 -060074
Ken Cox9d9baad2014-03-04 07:58:05 -060075/** Call this from your periodic work worker function to schedule the next
76 * call.
Prarit Bhargava779d0752015-05-05 18:37:01 -040077 * If this function returns false, there was a failure and the
Ken Cox9d9baad2014-03-04 07:58:05 -060078 * periodic work is no longer scheduled
79 */
Prarit Bhargava779d0752015-05-05 18:37:01 -040080bool visor_periodic_work_nextperiod(struct periodic_work *pw)
Ken Cox9d9baad2014-03-04 07:58:05 -060081{
Prarit Bhargava779d0752015-05-05 18:37:01 -040082 bool rc = false;
Masaru Nomurae03e1e32014-05-17 21:32:53 +010083
Benjamin Romer2c5653b2014-09-30 12:07:46 -040084 write_lock(&pw->lock);
85 if (pw->want_to_stop) {
Prarit Bhargava779d0752015-05-05 18:37:01 -040086 pw->is_scheduled = false;
87 pw->want_to_stop = false;
88 rc = true; /* yes, true; see visor_periodic_work_stop() */
Benjamin Romer2c5653b2014-09-30 12:07:46 -040089 goto unlock;
Benjamin Romerf84bd622015-10-01 11:52:30 -040090 } else if (!queue_delayed_work(pw->workqueue, &pw->work,
91 pw->jiffy_interval)) {
Prarit Bhargava779d0752015-05-05 18:37:01 -040092 pw->is_scheduled = false;
93 rc = false;
Benjamin Romer2c5653b2014-09-30 12:07:46 -040094 goto unlock;
Ken Cox9d9baad2014-03-04 07:58:05 -060095 }
Prarit Bhargava779d0752015-05-05 18:37:01 -040096 rc = true;
Benjamin Romer2c5653b2014-09-30 12:07:46 -040097unlock:
98 write_unlock(&pw->lock);
Ken Cox9d9baad2014-03-04 07:58:05 -060099 return rc;
100}
Ken Cox927c7922014-03-05 14:52:25 -0600101EXPORT_SYMBOL_GPL(visor_periodic_work_nextperiod);
Ken Cox9d9baad2014-03-04 07:58:05 -0600102
Prarit Bhargava779d0752015-05-05 18:37:01 -0400103/** This function returns true iff new periodic work was actually started.
104 * If this function returns false, then no work was started
Ken Cox9d9baad2014-03-04 07:58:05 -0600105 * (either because it was already started, or because of a failure).
106 */
Prarit Bhargava779d0752015-05-05 18:37:01 -0400107bool visor_periodic_work_start(struct periodic_work *pw)
Ken Cox9d9baad2014-03-04 07:58:05 -0600108{
Prarit Bhargava779d0752015-05-05 18:37:01 -0400109 bool rc = false;
Ken Cox9d9baad2014-03-04 07:58:05 -0600110
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400111 write_lock(&pw->lock);
112 if (pw->is_scheduled) {
Prarit Bhargava779d0752015-05-05 18:37:01 -0400113 rc = false;
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400114 goto unlock;
Ken Cox61e03b42014-03-19 13:06:21 -0500115 }
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400116 if (pw->want_to_stop) {
Prarit Bhargava779d0752015-05-05 18:37:01 -0400117 rc = false;
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400118 goto unlock;
Ken Cox9d9baad2014-03-04 07:58:05 -0600119 }
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400120 INIT_DELAYED_WORK(&pw->work, &periodic_work_func);
Benjamin Romerf84bd622015-10-01 11:52:30 -0400121 if (!queue_delayed_work(pw->workqueue, &pw->work,
122 pw->jiffy_interval)) {
Prarit Bhargava779d0752015-05-05 18:37:01 -0400123 rc = false;
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400124 goto unlock;
Ken Cox9d9baad2014-03-04 07:58:05 -0600125 }
Prarit Bhargava779d0752015-05-05 18:37:01 -0400126 pw->is_scheduled = true;
127 rc = true;
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400128unlock:
129 write_unlock(&pw->lock);
Ken Cox9d9baad2014-03-04 07:58:05 -0600130 return rc;
Ken Cox9d9baad2014-03-04 07:58:05 -0600131}
Ken Cox927c7922014-03-05 14:52:25 -0600132EXPORT_SYMBOL_GPL(visor_periodic_work_start);
Ken Cox9d9baad2014-03-04 07:58:05 -0600133
Prarit Bhargava779d0752015-05-05 18:37:01 -0400134/** This function returns true iff your call actually stopped the periodic
Ken Cox9d9baad2014-03-04 07:58:05 -0600135 * work.
136 *
137 * -- PAY ATTENTION... this is important --
138 *
139 * NO NO #1
140 *
141 * Do NOT call this function from some function that is running on the
142 * same workqueue as the work you are trying to stop might be running
Ken Cox927c7922014-03-05 14:52:25 -0600143 * on! If you violate this rule, visor_periodic_work_stop() MIGHT work,
144 * but it also MIGHT get hung up in an infinite loop saying
Ken Cox9d9baad2014-03-04 07:58:05 -0600145 * "waiting for delayed work...". This will happen if the delayed work
146 * you are trying to cancel has been put in the workqueue list, but can't
147 * run yet because we are running that same workqueue thread right now.
148 *
Ken Cox927c7922014-03-05 14:52:25 -0600149 * Bottom line: If you need to call visor_periodic_work_stop() from a
150 * workitem, be sure the workitem is on a DIFFERENT workqueue than the
151 * workitem that you are trying to cancel.
Ken Cox9d9baad2014-03-04 07:58:05 -0600152 *
153 * If I could figure out some way to check for this "no no" condition in
154 * the code, I would. It would have saved me the trouble of writing this
155 * long comment. And also, don't think this is some "theoretical" race
156 * condition. It is REAL, as I have spent the day chasing it.
157 *
158 * NO NO #2
159 *
160 * Take close note of the locks that you own when you call this function.
161 * You must NOT own any locks that are needed by the periodic work
162 * function that is currently installed. If you DO, a deadlock may result,
163 * because stopping the periodic work often involves waiting for the last
164 * iteration of the periodic work function to complete. Again, if you hit
165 * this deadlock, you will get hung up in an infinite loop saying
166 * "waiting for delayed work...".
167 */
Prarit Bhargava779d0752015-05-05 18:37:01 -0400168bool visor_periodic_work_stop(struct periodic_work *pw)
Ken Cox9d9baad2014-03-04 07:58:05 -0600169{
Prarit Bhargava779d0752015-05-05 18:37:01 -0400170 bool stopped_something = false;
Ken Cox9d9baad2014-03-04 07:58:05 -0600171
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400172 write_lock(&pw->lock);
173 stopped_something = pw->is_scheduled && (!pw->want_to_stop);
174 while (pw->is_scheduled) {
Prarit Bhargava779d0752015-05-05 18:37:01 -0400175 pw->want_to_stop = true;
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400176 if (cancel_delayed_work(&pw->work)) {
Ken Cox9d9baad2014-03-04 07:58:05 -0600177 /* We get here if the delayed work was pending as
178 * delayed work, but was NOT run.
179 */
Benjamin Romer1a84fec2015-03-04 12:14:23 -0500180 WARN_ON(!pw->is_scheduled);
Prarit Bhargava779d0752015-05-05 18:37:01 -0400181 pw->is_scheduled = false;
Ken Cox9d9baad2014-03-04 07:58:05 -0600182 } else {
183 /* If we get here, either the delayed work:
184 * - was run, OR,
185 * - is running RIGHT NOW on another processor, OR,
186 * - wasn't even scheduled (there is a miniscule
187 * timing window where this could be the case)
188 * flush_workqueue() would make sure it is finished
189 * executing, but that still isn't very useful, which
190 * explains the loop...
191 */
192 }
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400193 if (pw->is_scheduled) {
194 write_unlock(&pw->lock);
Nicholas Mc Guire08486112015-05-29 17:31:16 +0200195 schedule_timeout_interruptible(msecs_to_jiffies(10));
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400196 write_lock(&pw->lock);
Benjamin Romerf0b5c6d2014-11-04 11:25:22 -0500197 } else {
Prarit Bhargava779d0752015-05-05 18:37:01 -0400198 pw->want_to_stop = false;
Benjamin Romerf0b5c6d2014-11-04 11:25:22 -0500199 }
Ken Cox9d9baad2014-03-04 07:58:05 -0600200 }
Benjamin Romer2c5653b2014-09-30 12:07:46 -0400201 write_unlock(&pw->lock);
Ken Cox9d9baad2014-03-04 07:58:05 -0600202 return stopped_something;
203}
Ken Cox927c7922014-03-05 14:52:25 -0600204EXPORT_SYMBOL_GPL(visor_periodic_work_stop);