aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media
diff options
context:
space:
mode:
authorMike Isely <isely@pobox.com>2008-04-07 01:22:04 -0400
committerMauro Carvalho Chehab <mchehab@infradead.org>2008-04-24 13:09:48 -0400
commite5be15c63804e05b5a94197524023702a259e308 (patch)
tree4223cab8016088a06d0423e1e41eacb646867c46 /drivers/media
parentd913d6303072ca194919d851e6743ad8c3a7563d (diff)
V4L/DVB (7711): pvrusb2: Fix race on module unload
The pvrusb2 driver - for basically forever - was not enforcing a proper module tear-down. Kernel threads are used inside the driver and all must be gone before the module can be safely removed. This changeset reimplements a chunk of pvrusb2-context.c to enforce this correctly. Unfortunately this is not a simple fix. The new implementation also cuts back on kernel thread usage; instead of there being 1 control thread per instance now it's just 1 control thread shared by all instances. (By dropping to a single thread then the module exit function can block on its shutdown and the thread itself can monitor and cleanly shut down all of the other instances first.) Signed-off-by: Mike Isely <isely@pobox.com> Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Diffstat (limited to 'drivers/media')
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-context.c204
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-context.h9
-rw-r--r--drivers/media/video/pvrusb2/pvrusb2-main.c12
3 files changed, 174 insertions, 51 deletions
diff --git a/drivers/media/video/pvrusb2/pvrusb2-context.c b/drivers/media/video/pvrusb2/pvrusb2-context.c
index 22bd8302c4aa..e7a2ed58bde2 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-context.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-context.c
@@ -23,100 +23,206 @@
23#include "pvrusb2-ioread.h" 23#include "pvrusb2-ioread.h"
24#include "pvrusb2-hdw.h" 24#include "pvrusb2-hdw.h"
25#include "pvrusb2-debug.h" 25#include "pvrusb2-debug.h"
26#include <linux/wait.h>
26#include <linux/kthread.h> 27#include <linux/kthread.h>
27#include <linux/errno.h> 28#include <linux/errno.h>
28#include <linux/string.h> 29#include <linux/string.h>
29#include <linux/slab.h> 30#include <linux/slab.h>
30 31
32static struct pvr2_context *pvr2_context_exist_first;
33static struct pvr2_context *pvr2_context_exist_last;
34static struct pvr2_context *pvr2_context_notify_first;
35static struct pvr2_context *pvr2_context_notify_last;
36static DEFINE_MUTEX(pvr2_context_mutex);
37static DECLARE_WAIT_QUEUE_HEAD(pvr2_context_sync_data);
38static struct task_struct *pvr2_context_thread_ptr;
39
40
41static void pvr2_context_set_notify(struct pvr2_context *mp, int fl)
42{
43 int signal_flag = 0;
44 mutex_lock(&pvr2_context_mutex);
45 if (fl) {
46 if (!mp->notify_flag) {
47 signal_flag = (pvr2_context_notify_first == NULL);
48 mp->notify_prev = pvr2_context_notify_last;
49 mp->notify_next = NULL;
50 pvr2_context_notify_last = mp;
51 if (mp->notify_prev) {
52 mp->notify_prev->notify_next = mp;
53 } else {
54 pvr2_context_notify_first = mp;
55 }
56 mp->notify_flag = !0;
57 }
58 } else {
59 if (mp->notify_flag) {
60 mp->notify_flag = 0;
61 if (mp->notify_next) {
62 mp->notify_next->notify_prev = mp->notify_prev;
63 } else {
64 pvr2_context_notify_last = mp->notify_prev;
65 }
66 if (mp->notify_prev) {
67 mp->notify_prev->notify_next = mp->notify_next;
68 } else {
69 pvr2_context_notify_first = mp->notify_next;
70 }
71 }
72 }
73 mutex_unlock(&pvr2_context_mutex);
74 if (signal_flag) wake_up(&pvr2_context_sync_data);
75}
76
31 77
32static void pvr2_context_destroy(struct pvr2_context *mp) 78static void pvr2_context_destroy(struct pvr2_context *mp)
33{ 79{
34 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (destroy)",mp); 80 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (destroy)",mp);
35 if (mp->hdw) pvr2_hdw_destroy(mp->hdw); 81 if (mp->hdw) pvr2_hdw_destroy(mp->hdw);
82 pvr2_context_set_notify(mp, 0);
83 mutex_lock(&pvr2_context_mutex);
84 if (mp->exist_next) {
85 mp->exist_next->exist_prev = mp->exist_prev;
86 } else {
87 pvr2_context_exist_last = mp->exist_prev;
88 }
89 if (mp->exist_prev) {
90 mp->exist_prev->exist_next = mp->exist_next;
91 } else {
92 pvr2_context_exist_first = mp->exist_next;
93 }
94 if (!pvr2_context_exist_first) {
95 /* Trigger wakeup on control thread in case it is waiting
96 for an exit condition. */
97 wake_up(&pvr2_context_sync_data);
98 }
99 mutex_unlock(&pvr2_context_mutex);
36 kfree(mp); 100 kfree(mp);
37} 101}
38 102
39 103
40static void pvr2_context_notify(struct pvr2_context *mp) 104static void pvr2_context_notify(struct pvr2_context *mp)
41{ 105{
42 mp->notify_flag = !0; 106 pvr2_context_set_notify(mp,!0);
43 wake_up(&mp->wait_data);
44} 107}
45 108
46 109
47static int pvr2_context_thread(void *_mp) 110static void pvr2_context_check(struct pvr2_context *mp)
48{ 111{
49 struct pvr2_channel *ch1,*ch2; 112 struct pvr2_channel *ch1, *ch2;
50 struct pvr2_context *mp = _mp; 113 pvr2_trace(PVR2_TRACE_CTXT,
51 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (thread start)",mp); 114 "pvr2_context %p (notify)", mp);
52 115 if (!mp->initialized_flag && !mp->disconnect_flag) {
53 /* Finish hardware initialization */ 116 mp->initialized_flag = !0;
54 if (pvr2_hdw_initialize(mp->hdw,
55 (void (*)(void *))pvr2_context_notify,mp)) {
56 mp->video_stream.stream =
57 pvr2_hdw_get_video_stream(mp->hdw);
58 /* Trigger interface initialization. By doing this here
59 initialization runs in our own safe and cozy thread
60 context. */
61 if (mp->setup_func) mp->setup_func(mp);
62 } else {
63 pvr2_trace(PVR2_TRACE_CTXT, 117 pvr2_trace(PVR2_TRACE_CTXT,
64 "pvr2_context %p (thread skipping setup)",mp); 118 "pvr2_context %p (initialize)", mp);
65 /* Even though initialization did not succeed, we're still 119 /* Finish hardware initialization */
66 going to enter the wait loop anyway. We need to do this 120 if (pvr2_hdw_initialize(mp->hdw,
67 in order to await the expected disconnect (which we will 121 (void (*)(void *))pvr2_context_notify,
68 detect in the normal course of operation). */ 122 mp)) {
69 } 123 mp->video_stream.stream =
70 124 pvr2_hdw_get_video_stream(mp->hdw);
71 /* Now just issue callbacks whenever hardware state changes or if 125 /* Trigger interface initialization. By doing this
72 there is a disconnect. If there is a disconnect and there are 126 here initialization runs in our own safe and
73 no channels left, then there's no reason to stick around anymore 127 cozy thread context. */
74 so we'll self-destruct - tearing down the rest of this driver 128 if (mp->setup_func) mp->setup_func(mp);
75 instance along the way. */ 129 } else {
76 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (thread enter loop)",mp);
77 while (!mp->disconnect_flag || mp->mc_first) {
78 if (mp->notify_flag) {
79 mp->notify_flag = 0;
80 pvr2_trace(PVR2_TRACE_CTXT, 130 pvr2_trace(PVR2_TRACE_CTXT,
81 "pvr2_context %p (thread notify)",mp); 131 "pvr2_context %p (thread skipping setup)",
82 for (ch1 = mp->mc_first; ch1; ch1 = ch2) { 132 mp);
83 ch2 = ch1->mc_next; 133 /* Even though initialization did not succeed,
84 if (ch1->check_func) ch1->check_func(ch1); 134 we're still going to continue anyway. We need
85 } 135 to do this in order to await the expected
136 disconnect (which we will detect in the normal
137 course of operation). */
86 } 138 }
87 wait_event_interruptible(mp->wait_data, mp->notify_flag);
88 } 139 }
89 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (thread end)",mp); 140
90 pvr2_context_destroy(mp); 141 for (ch1 = mp->mc_first; ch1; ch1 = ch2) {
142 ch2 = ch1->mc_next;
143 if (ch1->check_func) ch1->check_func(ch1);
144 }
145
146 if (mp->disconnect_flag && !mp->mc_first) {
147 /* Go away... */
148 pvr2_context_destroy(mp);
149 return;
150 }
151}
152
153
154static int pvr2_context_shutok(void)
155{
156 return kthread_should_stop() && (pvr2_context_exist_first == NULL);
157}
158
159
160static int pvr2_context_thread_func(void *foo)
161{
162 struct pvr2_context *mp;
163
164 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread start");
165
166 do {
167 while ((mp = pvr2_context_notify_first) != NULL) {
168 pvr2_context_set_notify(mp, 0);
169 pvr2_context_check(mp);
170 }
171 wait_event_interruptible(
172 pvr2_context_sync_data,
173 ((pvr2_context_notify_first != NULL) ||
174 pvr2_context_shutok()));
175 } while (!pvr2_context_shutok());
176
177 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context thread end");
178
91 return 0; 179 return 0;
92} 180}
93 181
94 182
183int pvr2_context_global_init(void)
184{
185 pvr2_context_thread_ptr = kthread_run(pvr2_context_thread_func,
186 0,
187 "pvrusb2-context");
188 return (pvr2_context_thread_ptr ? 0 : -ENOMEM);
189}
190
191
192void pvr2_context_global_done(void)
193{
194 kthread_stop(pvr2_context_thread_ptr);
195}
196
197
95struct pvr2_context *pvr2_context_create( 198struct pvr2_context *pvr2_context_create(
96 struct usb_interface *intf, 199 struct usb_interface *intf,
97 const struct usb_device_id *devid, 200 const struct usb_device_id *devid,
98 void (*setup_func)(struct pvr2_context *)) 201 void (*setup_func)(struct pvr2_context *))
99{ 202{
100 struct task_struct *thread;
101 struct pvr2_context *mp = NULL; 203 struct pvr2_context *mp = NULL;
102 mp = kzalloc(sizeof(*mp),GFP_KERNEL); 204 mp = kzalloc(sizeof(*mp),GFP_KERNEL);
103 if (!mp) goto done; 205 if (!mp) goto done;
104 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (create)",mp); 206 pvr2_trace(PVR2_TRACE_CTXT,"pvr2_context %p (create)",mp);
105 init_waitqueue_head(&mp->wait_data);
106 mp->setup_func = setup_func; 207 mp->setup_func = setup_func;
107 mutex_init(&mp->mutex); 208 mutex_init(&mp->mutex);
209 mutex_lock(&pvr2_context_mutex);
210 mp->exist_prev = pvr2_context_exist_last;
211 mp->exist_next = NULL;
212 pvr2_context_exist_last = mp;
213 if (mp->exist_prev) {
214 mp->exist_prev->exist_next = mp;
215 } else {
216 pvr2_context_exist_first = mp;
217 }
218 mutex_unlock(&pvr2_context_mutex);
108 mp->hdw = pvr2_hdw_create(intf,devid); 219 mp->hdw = pvr2_hdw_create(intf,devid);
109 if (!mp->hdw) { 220 if (!mp->hdw) {
110 pvr2_context_destroy(mp); 221 pvr2_context_destroy(mp);
111 mp = NULL; 222 mp = NULL;
112 goto done; 223 goto done;
113 } 224 }
114 thread = kthread_run(pvr2_context_thread, mp, "pvrusb2-context"); 225 pvr2_context_set_notify(mp, !0);
115 if (!thread) {
116 pvr2_context_destroy(mp);
117 mp = NULL;
118 goto done;
119 }
120 done: 226 done:
121 return mp; 227 return mp;
122} 228}
diff --git a/drivers/media/video/pvrusb2/pvrusb2-context.h b/drivers/media/video/pvrusb2/pvrusb2-context.h
index 127ec53e0913..6fb6ab022851 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-context.h
+++ b/drivers/media/video/pvrusb2/pvrusb2-context.h
@@ -40,14 +40,17 @@ struct pvr2_context_stream {
40struct pvr2_context { 40struct pvr2_context {
41 struct pvr2_channel *mc_first; 41 struct pvr2_channel *mc_first;
42 struct pvr2_channel *mc_last; 42 struct pvr2_channel *mc_last;
43 struct pvr2_context *exist_next;
44 struct pvr2_context *exist_prev;
45 struct pvr2_context *notify_next;
46 struct pvr2_context *notify_prev;
43 struct pvr2_hdw *hdw; 47 struct pvr2_hdw *hdw;
44 struct pvr2_context_stream video_stream; 48 struct pvr2_context_stream video_stream;
45 struct mutex mutex; 49 struct mutex mutex;
46 int notify_flag; 50 int notify_flag;
51 int initialized_flag;
47 int disconnect_flag; 52 int disconnect_flag;
48 53
49 wait_queue_head_t wait_data;
50
51 /* Called after pvr2_context initialization is complete */ 54 /* Called after pvr2_context initialization is complete */
52 void (*setup_func)(struct pvr2_context *); 55 void (*setup_func)(struct pvr2_context *);
53 56
@@ -74,6 +77,8 @@ int pvr2_channel_claim_stream(struct pvr2_channel *,
74struct pvr2_ioread *pvr2_channel_create_mpeg_stream( 77struct pvr2_ioread *pvr2_channel_create_mpeg_stream(
75 struct pvr2_context_stream *); 78 struct pvr2_context_stream *);
76 79
80int pvr2_context_global_init(void);
81void pvr2_context_global_done(void);
77 82
78#endif /* __PVRUSB2_CONTEXT_H */ 83#endif /* __PVRUSB2_CONTEXT_H */
79/* 84/*
diff --git a/drivers/media/video/pvrusb2/pvrusb2-main.c b/drivers/media/video/pvrusb2/pvrusb2-main.c
index 54d9f168d7ad..332aced8a5a1 100644
--- a/drivers/media/video/pvrusb2/pvrusb2-main.c
+++ b/drivers/media/video/pvrusb2/pvrusb2-main.c
@@ -125,6 +125,12 @@ static int __init pvr_init(void)
125 125
126 pvr2_trace(PVR2_TRACE_INIT,"pvr_init"); 126 pvr2_trace(PVR2_TRACE_INIT,"pvr_init");
127 127
128 ret = pvr2_context_global_init();
129 if (ret != 0) {
130 pvr2_trace(PVR2_TRACE_INIT,"pvr_init failure code=%d",ret);
131 return ret;
132 }
133
128#ifdef CONFIG_VIDEO_PVRUSB2_SYSFS 134#ifdef CONFIG_VIDEO_PVRUSB2_SYSFS
129 class_ptr = pvr2_sysfs_class_create(); 135 class_ptr = pvr2_sysfs_class_create();
130#endif /* CONFIG_VIDEO_PVRUSB2_SYSFS */ 136#endif /* CONFIG_VIDEO_PVRUSB2_SYSFS */
@@ -136,6 +142,8 @@ static int __init pvr_init(void)
136 if (pvrusb2_debug) info("Debug mask is %d (0x%x)", 142 if (pvrusb2_debug) info("Debug mask is %d (0x%x)",
137 pvrusb2_debug,pvrusb2_debug); 143 pvrusb2_debug,pvrusb2_debug);
138 144
145 pvr2_trace(PVR2_TRACE_INIT,"pvr_init complete");
146
139 return ret; 147 return ret;
140} 148}
141 149
@@ -148,6 +156,10 @@ static void __exit pvr_exit(void)
148#ifdef CONFIG_VIDEO_PVRUSB2_SYSFS 156#ifdef CONFIG_VIDEO_PVRUSB2_SYSFS
149 pvr2_sysfs_class_destroy(class_ptr); 157 pvr2_sysfs_class_destroy(class_ptr);
150#endif /* CONFIG_VIDEO_PVRUSB2_SYSFS */ 158#endif /* CONFIG_VIDEO_PVRUSB2_SYSFS */
159
160 pvr2_context_global_done();
161
162 pvr2_trace(PVR2_TRACE_INIT,"pvr_exit complete");
151} 163}
152 164
153module_init(pvr_init); 165module_init(pvr_init);