hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/sound/core/misc.c
....@@ -1,22 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Misc and compatibility things
34 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4
- *
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
9
- * (at your option) any later version.
10
- *
11
- * This program is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; if not, write to the Free Software
18
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
- *
205 */
216
227 #include <linux/init.h>
....@@ -25,6 +10,7 @@
2510 #include <linux/time.h>
2611 #include <linux/slab.h>
2712 #include <linux/ioport.h>
13
+#include <linux/fs.h>
2814 #include <sound/core.h>
2915
3016 #ifdef CONFIG_SND_DEBUG
....@@ -160,3 +146,96 @@
160146 }
161147 EXPORT_SYMBOL(snd_pci_quirk_lookup);
162148 #endif
149
+
150
+/*
151
+ * Deferred async signal helpers
152
+ *
153
+ * Below are a few helper functions to wrap the async signal handling
154
+ * in the deferred work. The main purpose is to avoid the messy deadlock
155
+ * around tasklist_lock and co at the kill_fasync() invocation.
156
+ * fasync_helper() and kill_fasync() are replaced with snd_fasync_helper()
157
+ * and snd_kill_fasync(), respectively. In addition, snd_fasync_free() has
158
+ * to be called at releasing the relevant file object.
159
+ */
160
+struct snd_fasync {
161
+ struct fasync_struct *fasync;
162
+ int signal;
163
+ int poll;
164
+ int on;
165
+ struct list_head list;
166
+};
167
+
168
+static DEFINE_SPINLOCK(snd_fasync_lock);
169
+static LIST_HEAD(snd_fasync_list);
170
+
171
+static void snd_fasync_work_fn(struct work_struct *work)
172
+{
173
+ struct snd_fasync *fasync;
174
+
175
+ spin_lock_irq(&snd_fasync_lock);
176
+ while (!list_empty(&snd_fasync_list)) {
177
+ fasync = list_first_entry(&snd_fasync_list, struct snd_fasync, list);
178
+ list_del_init(&fasync->list);
179
+ spin_unlock_irq(&snd_fasync_lock);
180
+ if (fasync->on)
181
+ kill_fasync(&fasync->fasync, fasync->signal, fasync->poll);
182
+ spin_lock_irq(&snd_fasync_lock);
183
+ }
184
+ spin_unlock_irq(&snd_fasync_lock);
185
+}
186
+
187
+static DECLARE_WORK(snd_fasync_work, snd_fasync_work_fn);
188
+
189
+int snd_fasync_helper(int fd, struct file *file, int on,
190
+ struct snd_fasync **fasyncp)
191
+{
192
+ struct snd_fasync *fasync = NULL;
193
+
194
+ if (on) {
195
+ fasync = kzalloc(sizeof(*fasync), GFP_KERNEL);
196
+ if (!fasync)
197
+ return -ENOMEM;
198
+ INIT_LIST_HEAD(&fasync->list);
199
+ }
200
+
201
+ spin_lock_irq(&snd_fasync_lock);
202
+ if (*fasyncp) {
203
+ kfree(fasync);
204
+ fasync = *fasyncp;
205
+ } else {
206
+ if (!fasync) {
207
+ spin_unlock_irq(&snd_fasync_lock);
208
+ return 0;
209
+ }
210
+ *fasyncp = fasync;
211
+ }
212
+ fasync->on = on;
213
+ spin_unlock_irq(&snd_fasync_lock);
214
+ return fasync_helper(fd, file, on, &fasync->fasync);
215
+}
216
+EXPORT_SYMBOL_GPL(snd_fasync_helper);
217
+
218
+void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll)
219
+{
220
+ unsigned long flags;
221
+
222
+ if (!fasync || !fasync->on)
223
+ return;
224
+ spin_lock_irqsave(&snd_fasync_lock, flags);
225
+ fasync->signal = signal;
226
+ fasync->poll = poll;
227
+ list_move(&fasync->list, &snd_fasync_list);
228
+ schedule_work(&snd_fasync_work);
229
+ spin_unlock_irqrestore(&snd_fasync_lock, flags);
230
+}
231
+EXPORT_SYMBOL_GPL(snd_kill_fasync);
232
+
233
+void snd_fasync_free(struct snd_fasync *fasync)
234
+{
235
+ if (!fasync)
236
+ return;
237
+ fasync->on = 0;
238
+ flush_work(&snd_fasync_work);
239
+ kfree(fasync);
240
+}
241
+EXPORT_SYMBOL_GPL(snd_fasync_free);