1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ALSA sequencer Memory Manager
4 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
5 * Jaroslav Kysela <perex@perex.cz>
6 * 2000 by Takashi Iwai <tiwai@suse.de>
7 */
8
9 #include <linux/init.h>
10 #include <linux/export.h>
11 #include <linux/slab.h>
12 #include <linux/sched/signal.h>
13 #include <linux/mm.h>
14 #include <sound/core.h>
15
16 #include <sound/seq_kernel.h>
17 #include "seq_memory.h"
18 #include "seq_queue.h"
19 #include "seq_info.h"
20 #include "seq_lock.h"
21
snd_seq_pool_available(struct snd_seq_pool * pool)22 static inline int snd_seq_pool_available(struct snd_seq_pool *pool)
23 {
24 return pool->total_elements - atomic_read(&pool->counter);
25 }
26
snd_seq_output_ok(struct snd_seq_pool * pool)27 static inline int snd_seq_output_ok(struct snd_seq_pool *pool)
28 {
29 return snd_seq_pool_available(pool) >= pool->room;
30 }
31
32 /*
33 * Variable length event:
34 * The event like sysex uses variable length type.
35 * The external data may be stored in three different formats.
36 * 1) kernel space
37 * This is the normal case.
38 * ext.data.len = length
39 * ext.data.ptr = buffer pointer
40 * 2) user space
41 * When an event is generated via read(), the external data is
42 * kept in user space until expanded.
43 * ext.data.len = length | SNDRV_SEQ_EXT_USRPTR
44 * ext.data.ptr = userspace pointer
45 * 3) chained cells
46 * When the variable length event is enqueued (in prioq or fifo),
47 * the external data is decomposed to several cells.
48 * ext.data.len = length | SNDRV_SEQ_EXT_CHAINED
49 * ext.data.ptr = the additiona cell head
50 * -> cell.next -> cell.next -> ..
51 */
52
53 /*
54 * exported:
55 * call dump function to expand external data.
56 */
57
get_var_len(const struct snd_seq_event * event)58 static int get_var_len(const struct snd_seq_event *event)
59 {
60 if ((event->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
61 return -EINVAL;
62
63 return event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
64 }
65
snd_seq_dump_var_event(const struct snd_seq_event * event,snd_seq_dump_func_t func,void * private_data)66 int snd_seq_dump_var_event(const struct snd_seq_event *event,
67 snd_seq_dump_func_t func, void *private_data)
68 {
69 int len, err;
70 struct snd_seq_event_cell *cell;
71
72 if ((len = get_var_len(event)) <= 0)
73 return len;
74
75 if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
76 char buf[32];
77 char __user *curptr = (char __force __user *)event->data.ext.ptr;
78 while (len > 0) {
79 int size = sizeof(buf);
80 if (len < size)
81 size = len;
82 if (copy_from_user(buf, curptr, size))
83 return -EFAULT;
84 err = func(private_data, buf, size);
85 if (err < 0)
86 return err;
87 curptr += size;
88 len -= size;
89 }
90 return 0;
91 }
92 if (!(event->data.ext.len & SNDRV_SEQ_EXT_CHAINED))
93 return func(private_data, event->data.ext.ptr, len);
94
95 cell = (struct snd_seq_event_cell *)event->data.ext.ptr;
96 for (; len > 0 && cell; cell = cell->next) {
97 int size = sizeof(struct snd_seq_event);
98 if (len < size)
99 size = len;
100 err = func(private_data, &cell->event, size);
101 if (err < 0)
102 return err;
103 len -= size;
104 }
105 return 0;
106 }
107 EXPORT_SYMBOL(snd_seq_dump_var_event);
108
109
110 /*
111 * exported:
112 * expand the variable length event to linear buffer space.
113 */
114
seq_copy_in_kernel(void * ptr,void * src,int size)115 static int seq_copy_in_kernel(void *ptr, void *src, int size)
116 {
117 char **bufptr = ptr;
118
119 memcpy(*bufptr, src, size);
120 *bufptr += size;
121 return 0;
122 }
123
seq_copy_in_user(void * ptr,void * src,int size)124 static int seq_copy_in_user(void *ptr, void *src, int size)
125 {
126 char __user **bufptr = ptr;
127
128 if (copy_to_user(*bufptr, src, size))
129 return -EFAULT;
130 *bufptr += size;
131 return 0;
132 }
133
snd_seq_expand_var_event(const struct snd_seq_event * event,int count,char * buf,int in_kernel,int size_aligned)134 int snd_seq_expand_var_event(const struct snd_seq_event *event, int count, char *buf,
135 int in_kernel, int size_aligned)
136 {
137 int len, newlen;
138 int err;
139
140 if ((len = get_var_len(event)) < 0)
141 return len;
142 newlen = len;
143 if (size_aligned > 0)
144 newlen = roundup(len, size_aligned);
145 if (count < newlen)
146 return -EAGAIN;
147
148 if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
149 if (! in_kernel)
150 return -EINVAL;
151 if (copy_from_user(buf, (void __force __user *)event->data.ext.ptr, len))
152 return -EFAULT;
153 return newlen;
154 }
155 err = snd_seq_dump_var_event(event,
156 in_kernel ? seq_copy_in_kernel : seq_copy_in_user,
157 &buf);
158 return err < 0 ? err : newlen;
159 }
160 EXPORT_SYMBOL(snd_seq_expand_var_event);
161
162 /*
163 * release this cell, free extended data if available
164 */
165
free_cell(struct snd_seq_pool * pool,struct snd_seq_event_cell * cell)166 static inline void free_cell(struct snd_seq_pool *pool,
167 struct snd_seq_event_cell *cell)
168 {
169 cell->next = pool->free;
170 pool->free = cell;
171 atomic_dec(&pool->counter);
172 }
173
snd_seq_cell_free(struct snd_seq_event_cell * cell)174 void snd_seq_cell_free(struct snd_seq_event_cell * cell)
175 {
176 unsigned long flags;
177 struct snd_seq_pool *pool;
178
179 if (snd_BUG_ON(!cell))
180 return;
181 pool = cell->pool;
182 if (snd_BUG_ON(!pool))
183 return;
184
185 spin_lock_irqsave(&pool->lock, flags);
186 free_cell(pool, cell);
187 if (snd_seq_ev_is_variable(&cell->event)) {
188 if (cell->event.data.ext.len & SNDRV_SEQ_EXT_CHAINED) {
189 struct snd_seq_event_cell *curp, *nextptr;
190 curp = cell->event.data.ext.ptr;
191 for (; curp; curp = nextptr) {
192 nextptr = curp->next;
193 curp->next = pool->free;
194 free_cell(pool, curp);
195 }
196 }
197 }
198 if (waitqueue_active(&pool->output_sleep)) {
199 /* has enough space now? */
200 if (snd_seq_output_ok(pool))
201 wake_up(&pool->output_sleep);
202 }
203 spin_unlock_irqrestore(&pool->lock, flags);
204 }
205
206
207 /*
208 * allocate an event cell.
209 */
snd_seq_cell_alloc(struct snd_seq_pool * pool,struct snd_seq_event_cell ** cellp,int nonblock,struct file * file,struct mutex * mutexp)210 static int snd_seq_cell_alloc(struct snd_seq_pool *pool,
211 struct snd_seq_event_cell **cellp,
212 int nonblock, struct file *file,
213 struct mutex *mutexp)
214 {
215 struct snd_seq_event_cell *cell;
216 unsigned long flags;
217 int err = -EAGAIN;
218 wait_queue_entry_t wait;
219
220 if (pool == NULL)
221 return -EINVAL;
222
223 *cellp = NULL;
224
225 init_waitqueue_entry(&wait, current);
226 spin_lock_irqsave(&pool->lock, flags);
227 if (pool->ptr == NULL) { /* not initialized */
228 pr_debug("ALSA: seq: pool is not initialized\n");
229 err = -EINVAL;
230 goto __error;
231 }
232 while (pool->free == NULL && ! nonblock && ! pool->closing) {
233
234 set_current_state(TASK_INTERRUPTIBLE);
235 add_wait_queue(&pool->output_sleep, &wait);
236 spin_unlock_irqrestore(&pool->lock, flags);
237 if (mutexp)
238 mutex_unlock(mutexp);
239 schedule();
240 if (mutexp)
241 mutex_lock(mutexp);
242 spin_lock_irqsave(&pool->lock, flags);
243 remove_wait_queue(&pool->output_sleep, &wait);
244 /* interrupted? */
245 if (signal_pending(current)) {
246 err = -ERESTARTSYS;
247 goto __error;
248 }
249 }
250 if (pool->closing) { /* closing.. */
251 err = -ENOMEM;
252 goto __error;
253 }
254
255 cell = pool->free;
256 if (cell) {
257 int used;
258 pool->free = cell->next;
259 atomic_inc(&pool->counter);
260 used = atomic_read(&pool->counter);
261 if (pool->max_used < used)
262 pool->max_used = used;
263 pool->event_alloc_success++;
264 /* clear cell pointers */
265 cell->next = NULL;
266 err = 0;
267 } else
268 pool->event_alloc_failures++;
269 *cellp = cell;
270
271 __error:
272 spin_unlock_irqrestore(&pool->lock, flags);
273 return err;
274 }
275
276
277 /*
278 * duplicate the event to a cell.
279 * if the event has external data, the data is decomposed to additional
280 * cells.
281 */
snd_seq_event_dup(struct snd_seq_pool * pool,struct snd_seq_event * event,struct snd_seq_event_cell ** cellp,int nonblock,struct file * file,struct mutex * mutexp)282 int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event,
283 struct snd_seq_event_cell **cellp, int nonblock,
284 struct file *file, struct mutex *mutexp)
285 {
286 int ncells, err;
287 unsigned int extlen;
288 struct snd_seq_event_cell *cell;
289
290 *cellp = NULL;
291
292 ncells = 0;
293 extlen = 0;
294 if (snd_seq_ev_is_variable(event)) {
295 extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
296 ncells = (extlen + sizeof(struct snd_seq_event) - 1) / sizeof(struct snd_seq_event);
297 }
298 if (ncells >= pool->total_elements)
299 return -ENOMEM;
300
301 err = snd_seq_cell_alloc(pool, &cell, nonblock, file, mutexp);
302 if (err < 0)
303 return err;
304
305 /* copy the event */
306 cell->event = *event;
307
308 /* decompose */
309 if (snd_seq_ev_is_variable(event)) {
310 int len = extlen;
311 int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED;
312 int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR;
313 struct snd_seq_event_cell *src, *tmp, *tail;
314 char *buf;
315
316 cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED;
317 cell->event.data.ext.ptr = NULL;
318
319 src = (struct snd_seq_event_cell *)event->data.ext.ptr;
320 buf = (char *)event->data.ext.ptr;
321 tail = NULL;
322
323 while (ncells-- > 0) {
324 int size = sizeof(struct snd_seq_event);
325 if (len < size)
326 size = len;
327 err = snd_seq_cell_alloc(pool, &tmp, nonblock, file,
328 mutexp);
329 if (err < 0)
330 goto __error;
331 if (cell->event.data.ext.ptr == NULL)
332 cell->event.data.ext.ptr = tmp;
333 if (tail)
334 tail->next = tmp;
335 tail = tmp;
336 /* copy chunk */
337 if (is_chained && src) {
338 tmp->event = src->event;
339 src = src->next;
340 } else if (is_usrptr) {
341 if (copy_from_user(&tmp->event, (char __force __user *)buf, size)) {
342 err = -EFAULT;
343 goto __error;
344 }
345 } else {
346 memcpy(&tmp->event, buf, size);
347 }
348 buf += size;
349 len -= size;
350 }
351 }
352
353 *cellp = cell;
354 return 0;
355
356 __error:
357 snd_seq_cell_free(cell);
358 return err;
359 }
360
361
362 /* poll wait */
snd_seq_pool_poll_wait(struct snd_seq_pool * pool,struct file * file,poll_table * wait)363 int snd_seq_pool_poll_wait(struct snd_seq_pool *pool, struct file *file,
364 poll_table *wait)
365 {
366 poll_wait(file, &pool->output_sleep, wait);
367 return snd_seq_output_ok(pool);
368 }
369
370
371 /* allocate room specified number of events */
snd_seq_pool_init(struct snd_seq_pool * pool)372 int snd_seq_pool_init(struct snd_seq_pool *pool)
373 {
374 int cell;
375 struct snd_seq_event_cell *cellptr;
376
377 if (snd_BUG_ON(!pool))
378 return -EINVAL;
379
380 cellptr = kvmalloc_array(sizeof(struct snd_seq_event_cell), pool->size,
381 GFP_KERNEL);
382 if (!cellptr)
383 return -ENOMEM;
384
385 /* add new cells to the free cell list */
386 spin_lock_irq(&pool->lock);
387 if (pool->ptr) {
388 spin_unlock_irq(&pool->lock);
389 kvfree(cellptr);
390 return 0;
391 }
392
393 pool->ptr = cellptr;
394 pool->free = NULL;
395
396 for (cell = 0; cell < pool->size; cell++) {
397 cellptr = pool->ptr + cell;
398 cellptr->pool = pool;
399 cellptr->next = pool->free;
400 pool->free = cellptr;
401 }
402 pool->room = (pool->size + 1) / 2;
403
404 /* init statistics */
405 pool->max_used = 0;
406 pool->total_elements = pool->size;
407 spin_unlock_irq(&pool->lock);
408 return 0;
409 }
410
411 /* refuse the further insertion to the pool */
snd_seq_pool_mark_closing(struct snd_seq_pool * pool)412 void snd_seq_pool_mark_closing(struct snd_seq_pool *pool)
413 {
414 unsigned long flags;
415
416 if (snd_BUG_ON(!pool))
417 return;
418 spin_lock_irqsave(&pool->lock, flags);
419 pool->closing = 1;
420 spin_unlock_irqrestore(&pool->lock, flags);
421 }
422
423 /* remove events */
snd_seq_pool_done(struct snd_seq_pool * pool)424 int snd_seq_pool_done(struct snd_seq_pool *pool)
425 {
426 struct snd_seq_event_cell *ptr;
427
428 if (snd_BUG_ON(!pool))
429 return -EINVAL;
430
431 /* wait for closing all threads */
432 if (waitqueue_active(&pool->output_sleep))
433 wake_up(&pool->output_sleep);
434
435 while (atomic_read(&pool->counter) > 0)
436 schedule_timeout_uninterruptible(1);
437
438 /* release all resources */
439 spin_lock_irq(&pool->lock);
440 ptr = pool->ptr;
441 pool->ptr = NULL;
442 pool->free = NULL;
443 pool->total_elements = 0;
444 spin_unlock_irq(&pool->lock);
445
446 kvfree(ptr);
447
448 spin_lock_irq(&pool->lock);
449 pool->closing = 0;
450 spin_unlock_irq(&pool->lock);
451
452 return 0;
453 }
454
455
456 /* init new memory pool */
snd_seq_pool_new(int poolsize)457 struct snd_seq_pool *snd_seq_pool_new(int poolsize)
458 {
459 struct snd_seq_pool *pool;
460
461 /* create pool block */
462 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
463 if (!pool)
464 return NULL;
465 spin_lock_init(&pool->lock);
466 pool->ptr = NULL;
467 pool->free = NULL;
468 pool->total_elements = 0;
469 atomic_set(&pool->counter, 0);
470 pool->closing = 0;
471 init_waitqueue_head(&pool->output_sleep);
472
473 pool->size = poolsize;
474
475 /* init statistics */
476 pool->max_used = 0;
477 return pool;
478 }
479
480 /* remove memory pool */
snd_seq_pool_delete(struct snd_seq_pool ** ppool)481 int snd_seq_pool_delete(struct snd_seq_pool **ppool)
482 {
483 struct snd_seq_pool *pool = *ppool;
484
485 *ppool = NULL;
486 if (pool == NULL)
487 return 0;
488 snd_seq_pool_mark_closing(pool);
489 snd_seq_pool_done(pool);
490 kfree(pool);
491 return 0;
492 }
493
494 /* exported to seq_clientmgr.c */
snd_seq_info_pool(struct snd_info_buffer * buffer,struct snd_seq_pool * pool,char * space)495 void snd_seq_info_pool(struct snd_info_buffer *buffer,
496 struct snd_seq_pool *pool, char *space)
497 {
498 if (pool == NULL)
499 return;
500 snd_iprintf(buffer, "%sPool size : %d\n", space, pool->total_elements);
501 snd_iprintf(buffer, "%sCells in use : %d\n", space, atomic_read(&pool->counter));
502 snd_iprintf(buffer, "%sPeak cells in use : %d\n", space, pool->max_used);
503 snd_iprintf(buffer, "%sAlloc success : %d\n", space, pool->event_alloc_success);
504 snd_iprintf(buffer, "%sAlloc failures : %d\n", space, pool->event_alloc_failures);
505 }
506