xref: /linux-6.15/sound/core/seq/seq_memory.c (revision e3cd33ab)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  ALSA sequencer Memory Manager
41da177e4SLinus Torvalds  *  Copyright (c) 1998 by Frank van de Pol <[email protected]>
5c1017a4cSJaroslav Kysela  *                        Jaroslav Kysela <[email protected]>
61da177e4SLinus Torvalds  *                2000 by Takashi Iwai <[email protected]>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/init.h>
10d81a6d71SPaul Gortmaker #include <linux/export.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
12174cd4b1SIngo Molnar #include <linux/sched/signal.h>
13fd7ae83dSTakashi Iwai #include <linux/mm.h>
141da177e4SLinus Torvalds #include <sound/core.h>
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <sound/seq_kernel.h>
171da177e4SLinus Torvalds #include "seq_memory.h"
181da177e4SLinus Torvalds #include "seq_queue.h"
191da177e4SLinus Torvalds #include "seq_info.h"
201da177e4SLinus Torvalds #include "seq_lock.h"
211da177e4SLinus Torvalds 
snd_seq_pool_available(struct snd_seq_pool * pool)22c7e0b5bfSTakashi Iwai static inline int snd_seq_pool_available(struct snd_seq_pool *pool)
231da177e4SLinus Torvalds {
241da177e4SLinus Torvalds 	return pool->total_elements - atomic_read(&pool->counter);
251da177e4SLinus Torvalds }
261da177e4SLinus Torvalds 
snd_seq_output_ok(struct snd_seq_pool * pool)27c7e0b5bfSTakashi Iwai static inline int snd_seq_output_ok(struct snd_seq_pool *pool)
281da177e4SLinus Torvalds {
291da177e4SLinus Torvalds 	return snd_seq_pool_available(pool) >= pool->room;
301da177e4SLinus Torvalds }
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /*
331da177e4SLinus Torvalds  * Variable length event:
341da177e4SLinus Torvalds  * The event like sysex uses variable length type.
351da177e4SLinus Torvalds  * The external data may be stored in three different formats.
361da177e4SLinus Torvalds  * 1) kernel space
371da177e4SLinus Torvalds  *    This is the normal case.
381da177e4SLinus Torvalds  *      ext.data.len = length
391da177e4SLinus Torvalds  *      ext.data.ptr = buffer pointer
401da177e4SLinus Torvalds  * 2) user space
411da177e4SLinus Torvalds  *    When an event is generated via read(), the external data is
421da177e4SLinus Torvalds  *    kept in user space until expanded.
431da177e4SLinus Torvalds  *      ext.data.len = length | SNDRV_SEQ_EXT_USRPTR
441da177e4SLinus Torvalds  *      ext.data.ptr = userspace pointer
451da177e4SLinus Torvalds  * 3) chained cells
461da177e4SLinus Torvalds  *    When the variable length event is enqueued (in prioq or fifo),
471da177e4SLinus Torvalds  *    the external data is decomposed to several cells.
481da177e4SLinus Torvalds  *      ext.data.len = length | SNDRV_SEQ_EXT_CHAINED
491da177e4SLinus Torvalds  *      ext.data.ptr = the additiona cell head
501da177e4SLinus Torvalds  *         -> cell.next -> cell.next -> ..
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * exported:
551da177e4SLinus Torvalds  * call dump function to expand external data.
561da177e4SLinus Torvalds  */
571da177e4SLinus Torvalds 
get_var_len(const struct snd_seq_event * event)58c7e0b5bfSTakashi Iwai static int get_var_len(const struct snd_seq_event *event)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	if ((event->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
611da177e4SLinus Torvalds 		return -EINVAL;
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	return event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
dump_var_event(const struct snd_seq_event * event,snd_seq_dump_func_t func,void * private_data,int offset,int maxlen)66ea46f797STakashi Iwai static int dump_var_event(const struct snd_seq_event *event,
67ea46f797STakashi Iwai 			  snd_seq_dump_func_t func, void *private_data,
68ea46f797STakashi Iwai 			  int offset, int maxlen)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	int len, err;
71c7e0b5bfSTakashi Iwai 	struct snd_seq_event_cell *cell;
721da177e4SLinus Torvalds 
73f9a6bb84STakashi Iwai 	len = get_var_len(event);
74f9a6bb84STakashi Iwai 	if (len <= 0)
751da177e4SLinus Torvalds 		return len;
76ea46f797STakashi Iwai 	if (len <= offset)
77ea46f797STakashi Iwai 		return 0;
78ea46f797STakashi Iwai 	if (maxlen && len > offset + maxlen)
79ea46f797STakashi Iwai 		len = offset + maxlen;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
821da177e4SLinus Torvalds 		char buf[32];
83fea952e5SClemens Ladisch 		char __user *curptr = (char __force __user *)event->data.ext.ptr;
84ea46f797STakashi Iwai 		curptr += offset;
85ea46f797STakashi Iwai 		len -= offset;
861da177e4SLinus Torvalds 		while (len > 0) {
871da177e4SLinus Torvalds 			int size = sizeof(buf);
881da177e4SLinus Torvalds 			if (len < size)
891da177e4SLinus Torvalds 				size = len;
901da177e4SLinus Torvalds 			if (copy_from_user(buf, curptr, size))
911da177e4SLinus Torvalds 				return -EFAULT;
921da177e4SLinus Torvalds 			err = func(private_data, buf, size);
931da177e4SLinus Torvalds 			if (err < 0)
941da177e4SLinus Torvalds 				return err;
951da177e4SLinus Torvalds 			curptr += size;
961da177e4SLinus Torvalds 			len -= size;
971da177e4SLinus Torvalds 		}
981da177e4SLinus Torvalds 		return 0;
991da177e4SLinus Torvalds 	}
100b245a822SRasmus Villemoes 	if (!(event->data.ext.len & SNDRV_SEQ_EXT_CHAINED))
101ea46f797STakashi Iwai 		return func(private_data, event->data.ext.ptr + offset,
102ea46f797STakashi Iwai 			    len - offset);
1031da177e4SLinus Torvalds 
104c7e0b5bfSTakashi Iwai 	cell = (struct snd_seq_event_cell *)event->data.ext.ptr;
1051da177e4SLinus Torvalds 	for (; len > 0 && cell; cell = cell->next) {
106c7e0b5bfSTakashi Iwai 		int size = sizeof(struct snd_seq_event);
107ea46f797STakashi Iwai 		char *curptr = (char *)&cell->event;
108ea46f797STakashi Iwai 
109ea46f797STakashi Iwai 		if (offset >= size) {
110ea46f797STakashi Iwai 			offset -= size;
111ea46f797STakashi Iwai 			len -= size;
112ea46f797STakashi Iwai 			continue;
113ea46f797STakashi Iwai 		}
1141da177e4SLinus Torvalds 		if (len < size)
1151da177e4SLinus Torvalds 			size = len;
116ea46f797STakashi Iwai 		err = func(private_data, curptr + offset, size - offset);
1171da177e4SLinus Torvalds 		if (err < 0)
1181da177e4SLinus Torvalds 			return err;
119ea46f797STakashi Iwai 		offset = 0;
1201da177e4SLinus Torvalds 		len -= size;
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds 	return 0;
1231da177e4SLinus Torvalds }
124ea46f797STakashi Iwai 
snd_seq_dump_var_event(const struct snd_seq_event * event,snd_seq_dump_func_t func,void * private_data)125ea46f797STakashi Iwai int snd_seq_dump_var_event(const struct snd_seq_event *event,
126ea46f797STakashi Iwai 			   snd_seq_dump_func_t func, void *private_data)
127ea46f797STakashi Iwai {
128ea46f797STakashi Iwai 	return dump_var_event(event, func, private_data, 0, 0);
129ea46f797STakashi Iwai }
13091715ed9STakashi Iwai EXPORT_SYMBOL(snd_seq_dump_var_event);
13191715ed9STakashi Iwai 
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /*
1341da177e4SLinus Torvalds  * exported:
1351da177e4SLinus Torvalds  * expand the variable length event to linear buffer space.
1361da177e4SLinus Torvalds  */
1371da177e4SLinus Torvalds 
seq_copy_in_kernel(void * ptr,void * src,int size)13805530ef7SKees Cook static int seq_copy_in_kernel(void *ptr, void *src, int size)
1391da177e4SLinus Torvalds {
14005530ef7SKees Cook 	char **bufptr = ptr;
14105530ef7SKees Cook 
1421da177e4SLinus Torvalds 	memcpy(*bufptr, src, size);
1431da177e4SLinus Torvalds 	*bufptr += size;
1441da177e4SLinus Torvalds 	return 0;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
seq_copy_in_user(void * ptr,void * src,int size)14705530ef7SKees Cook static int seq_copy_in_user(void *ptr, void *src, int size)
1481da177e4SLinus Torvalds {
14905530ef7SKees Cook 	char __user **bufptr = ptr;
15005530ef7SKees Cook 
1511da177e4SLinus Torvalds 	if (copy_to_user(*bufptr, src, size))
1521da177e4SLinus Torvalds 		return -EFAULT;
1531da177e4SLinus Torvalds 	*bufptr += size;
1541da177e4SLinus Torvalds 	return 0;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
expand_var_event(const struct snd_seq_event * event,int offset,int size,char * buf,bool in_kernel)157ea46f797STakashi Iwai static int expand_var_event(const struct snd_seq_event *event,
158ea46f797STakashi Iwai 			    int offset, int size, char *buf, bool in_kernel)
159ea46f797STakashi Iwai {
160ea46f797STakashi Iwai 	if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
161ea46f797STakashi Iwai 		if (! in_kernel)
162ea46f797STakashi Iwai 			return -EINVAL;
163ea46f797STakashi Iwai 		if (copy_from_user(buf,
164ea46f797STakashi Iwai 				   (char __force __user *)event->data.ext.ptr + offset,
165ea46f797STakashi Iwai 				   size))
166ea46f797STakashi Iwai 			return -EFAULT;
167ea46f797STakashi Iwai 		return 0;
168ea46f797STakashi Iwai 	}
169ea46f797STakashi Iwai 	return dump_var_event(event,
170ea46f797STakashi Iwai 			     in_kernel ? seq_copy_in_kernel : seq_copy_in_user,
171ea46f797STakashi Iwai 			     &buf, offset, size);
172ea46f797STakashi Iwai }
173ea46f797STakashi Iwai 
snd_seq_expand_var_event(const struct snd_seq_event * event,int count,char * buf,int in_kernel,int size_aligned)174c7e0b5bfSTakashi Iwai int snd_seq_expand_var_event(const struct snd_seq_event *event, int count, char *buf,
175c7e0b5bfSTakashi Iwai 			     int in_kernel, int size_aligned)
1761da177e4SLinus Torvalds {
177ea46f797STakashi Iwai 	int len, newlen, err;
1781da177e4SLinus Torvalds 
179f9a6bb84STakashi Iwai 	len = get_var_len(event);
180f9a6bb84STakashi Iwai 	if (len < 0)
1811da177e4SLinus Torvalds 		return len;
1821da177e4SLinus Torvalds 	newlen = len;
1831da177e4SLinus Torvalds 	if (size_aligned > 0)
184201efe37SClemens Ladisch 		newlen = roundup(len, size_aligned);
1851da177e4SLinus Torvalds 	if (count < newlen)
1861da177e4SLinus Torvalds 		return -EAGAIN;
187ea46f797STakashi Iwai 	err = expand_var_event(event, 0, len, buf, in_kernel);
188f80e6d60STakashi Iwai 	if (err < 0)
189f80e6d60STakashi Iwai 		return err;
19086496fd4STakashi Iwai 	if (len != newlen) {
19186496fd4STakashi Iwai 		if (in_kernel)
192f80e6d60STakashi Iwai 			memset(buf + len, 0, newlen - len);
19386496fd4STakashi Iwai 		else if (clear_user((__force void __user *)buf + len,
19486496fd4STakashi Iwai 				    newlen - len))
19586496fd4STakashi Iwai 			return -EFAULT;
19686496fd4STakashi Iwai 	}
197f80e6d60STakashi Iwai 	return newlen;
1981da177e4SLinus Torvalds }
19991715ed9STakashi Iwai EXPORT_SYMBOL(snd_seq_expand_var_event);
2001da177e4SLinus Torvalds 
snd_seq_expand_var_event_at(const struct snd_seq_event * event,int count,char * buf,int offset)201ea46f797STakashi Iwai int snd_seq_expand_var_event_at(const struct snd_seq_event *event, int count,
202ea46f797STakashi Iwai 				char *buf, int offset)
203ea46f797STakashi Iwai {
204ea46f797STakashi Iwai 	int len, err;
205ea46f797STakashi Iwai 
206ea46f797STakashi Iwai 	len = get_var_len(event);
207ea46f797STakashi Iwai 	if (len < 0)
208ea46f797STakashi Iwai 		return len;
209ea46f797STakashi Iwai 	if (len <= offset)
210ea46f797STakashi Iwai 		return 0;
211ea46f797STakashi Iwai 	len -= offset;
212ea46f797STakashi Iwai 	if (len > count)
213ea46f797STakashi Iwai 		len = count;
214ea46f797STakashi Iwai 	err = expand_var_event(event, offset, count, buf, true);
215ea46f797STakashi Iwai 	if (err < 0)
216ea46f797STakashi Iwai 		return err;
217ea46f797STakashi Iwai 	return len;
218ea46f797STakashi Iwai }
219ea46f797STakashi Iwai EXPORT_SYMBOL_GPL(snd_seq_expand_var_event_at);
220ea46f797STakashi Iwai 
2211da177e4SLinus Torvalds /*
2221da177e4SLinus Torvalds  * release this cell, free extended data if available
2231da177e4SLinus Torvalds  */
2241da177e4SLinus Torvalds 
free_cell(struct snd_seq_pool * pool,struct snd_seq_event_cell * cell)225c7e0b5bfSTakashi Iwai static inline void free_cell(struct snd_seq_pool *pool,
226c7e0b5bfSTakashi Iwai 			     struct snd_seq_event_cell *cell)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	cell->next = pool->free;
2291da177e4SLinus Torvalds 	pool->free = cell;
2301da177e4SLinus Torvalds 	atomic_dec(&pool->counter);
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
snd_seq_cell_free(struct snd_seq_event_cell * cell)233c7e0b5bfSTakashi Iwai void snd_seq_cell_free(struct snd_seq_event_cell * cell)
2341da177e4SLinus Torvalds {
235c7e0b5bfSTakashi Iwai 	struct snd_seq_pool *pool;
2361da177e4SLinus Torvalds 
2377eaa943cSTakashi Iwai 	if (snd_BUG_ON(!cell))
2387eaa943cSTakashi Iwai 		return;
2391da177e4SLinus Torvalds 	pool = cell->pool;
2407eaa943cSTakashi Iwai 	if (snd_BUG_ON(!pool))
2417eaa943cSTakashi Iwai 		return;
2421da177e4SLinus Torvalds 
2436768bd10STakashi Iwai 	guard(spinlock_irqsave)(&pool->lock);
2441da177e4SLinus Torvalds 	free_cell(pool, cell);
2451da177e4SLinus Torvalds 	if (snd_seq_ev_is_variable(&cell->event)) {
2461da177e4SLinus Torvalds 		if (cell->event.data.ext.len & SNDRV_SEQ_EXT_CHAINED) {
247c7e0b5bfSTakashi Iwai 			struct snd_seq_event_cell *curp, *nextptr;
2481da177e4SLinus Torvalds 			curp = cell->event.data.ext.ptr;
2491da177e4SLinus Torvalds 			for (; curp; curp = nextptr) {
2501da177e4SLinus Torvalds 				nextptr = curp->next;
2511da177e4SLinus Torvalds 				curp->next = pool->free;
2521da177e4SLinus Torvalds 				free_cell(pool, curp);
2531da177e4SLinus Torvalds 			}
2541da177e4SLinus Torvalds 		}
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds 	if (waitqueue_active(&pool->output_sleep)) {
2571da177e4SLinus Torvalds 		/* has enough space now? */
2581da177e4SLinus Torvalds 		if (snd_seq_output_ok(pool))
2591da177e4SLinus Torvalds 			wake_up(&pool->output_sleep);
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds /*
2651da177e4SLinus Torvalds  * allocate an event cell.
2661da177e4SLinus Torvalds  */
snd_seq_cell_alloc(struct snd_seq_pool * pool,struct snd_seq_event_cell ** cellp,int nonblock,struct file * file,struct mutex * mutexp)267c7e0b5bfSTakashi Iwai static int snd_seq_cell_alloc(struct snd_seq_pool *pool,
268c7e0b5bfSTakashi Iwai 			      struct snd_seq_event_cell **cellp,
2697bd80091STakashi Iwai 			      int nonblock, struct file *file,
2707bd80091STakashi Iwai 			      struct mutex *mutexp)
2711da177e4SLinus Torvalds {
272c7e0b5bfSTakashi Iwai 	struct snd_seq_event_cell *cell;
2731da177e4SLinus Torvalds 	unsigned long flags;
2741da177e4SLinus Torvalds 	int err = -EAGAIN;
275ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	if (pool == NULL)
2781da177e4SLinus Torvalds 		return -EINVAL;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	*cellp = NULL;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	init_waitqueue_entry(&wait, current);
2831da177e4SLinus Torvalds 	spin_lock_irqsave(&pool->lock, flags);
2841da177e4SLinus Torvalds 	if (pool->ptr == NULL) {	/* not initialized */
28504cc79a0STakashi Iwai 		pr_debug("ALSA: seq: pool is not initialized\n");
2861da177e4SLinus Torvalds 		err = -EINVAL;
2871da177e4SLinus Torvalds 		goto __error;
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 	while (pool->free == NULL && ! nonblock && ! pool->closing) {
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 		set_current_state(TASK_INTERRUPTIBLE);
2921da177e4SLinus Torvalds 		add_wait_queue(&pool->output_sleep, &wait);
2934b24b960STakashi Iwai 		spin_unlock_irqrestore(&pool->lock, flags);
2947bd80091STakashi Iwai 		if (mutexp)
2957bd80091STakashi Iwai 			mutex_unlock(mutexp);
2961da177e4SLinus Torvalds 		schedule();
2977bd80091STakashi Iwai 		if (mutexp)
2987bd80091STakashi Iwai 			mutex_lock(mutexp);
2994b24b960STakashi Iwai 		spin_lock_irqsave(&pool->lock, flags);
3001da177e4SLinus Torvalds 		remove_wait_queue(&pool->output_sleep, &wait);
3011da177e4SLinus Torvalds 		/* interrupted? */
3021da177e4SLinus Torvalds 		if (signal_pending(current)) {
3031da177e4SLinus Torvalds 			err = -ERESTARTSYS;
3041da177e4SLinus Torvalds 			goto __error;
3051da177e4SLinus Torvalds 		}
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 	if (pool->closing) { /* closing.. */
3081da177e4SLinus Torvalds 		err = -ENOMEM;
3091da177e4SLinus Torvalds 		goto __error;
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	cell = pool->free;
3131da177e4SLinus Torvalds 	if (cell) {
3141da177e4SLinus Torvalds 		int used;
3151da177e4SLinus Torvalds 		pool->free = cell->next;
3161da177e4SLinus Torvalds 		atomic_inc(&pool->counter);
3171da177e4SLinus Torvalds 		used = atomic_read(&pool->counter);
3181da177e4SLinus Torvalds 		if (pool->max_used < used)
3191da177e4SLinus Torvalds 			pool->max_used = used;
3201da177e4SLinus Torvalds 		pool->event_alloc_success++;
3211da177e4SLinus Torvalds 		/* clear cell pointers */
3221da177e4SLinus Torvalds 		cell->next = NULL;
3231da177e4SLinus Torvalds 		err = 0;
3241da177e4SLinus Torvalds 	} else
3251da177e4SLinus Torvalds 		pool->event_alloc_failures++;
3261da177e4SLinus Torvalds 	*cellp = cell;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds __error:
3291da177e4SLinus Torvalds 	spin_unlock_irqrestore(&pool->lock, flags);
3301da177e4SLinus Torvalds 	return err;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds /*
3351da177e4SLinus Torvalds  * duplicate the event to a cell.
3361da177e4SLinus Torvalds  * if the event has external data, the data is decomposed to additional
3371da177e4SLinus Torvalds  * cells.
3381da177e4SLinus Torvalds  */
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)339c7e0b5bfSTakashi Iwai int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event,
340c7e0b5bfSTakashi Iwai 		      struct snd_seq_event_cell **cellp, int nonblock,
3417bd80091STakashi Iwai 		      struct file *file, struct mutex *mutexp)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	int ncells, err;
3441da177e4SLinus Torvalds 	unsigned int extlen;
345c7e0b5bfSTakashi Iwai 	struct snd_seq_event_cell *cell;
34646397622STakashi Iwai 	int size;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	*cellp = NULL;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	ncells = 0;
3511da177e4SLinus Torvalds 	extlen = 0;
3521da177e4SLinus Torvalds 	if (snd_seq_ev_is_variable(event)) {
3531da177e4SLinus Torvalds 		extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
354afcfbcb3SLars-Peter Clausen 		ncells = DIV_ROUND_UP(extlen, sizeof(struct snd_seq_event));
3551da177e4SLinus Torvalds 	}
3561da177e4SLinus Torvalds 	if (ncells >= pool->total_elements)
3571da177e4SLinus Torvalds 		return -ENOMEM;
3581da177e4SLinus Torvalds 
3597bd80091STakashi Iwai 	err = snd_seq_cell_alloc(pool, &cell, nonblock, file, mutexp);
3601da177e4SLinus Torvalds 	if (err < 0)
3611da177e4SLinus Torvalds 		return err;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	/* copy the event */
36446397622STakashi Iwai 	size = snd_seq_event_packet_size(event);
36546397622STakashi Iwai 	memcpy(&cell->ump, event, size);
36646397622STakashi Iwai #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
36746397622STakashi Iwai 	if (size < sizeof(cell->event))
36846397622STakashi Iwai 		cell->ump.raw.extra = 0;
36946397622STakashi Iwai #endif
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	/* decompose */
3721da177e4SLinus Torvalds 	if (snd_seq_ev_is_variable(event)) {
3731da177e4SLinus Torvalds 		int len = extlen;
3741da177e4SLinus Torvalds 		int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED;
3751da177e4SLinus Torvalds 		int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR;
376c7e0b5bfSTakashi Iwai 		struct snd_seq_event_cell *src, *tmp, *tail;
3771da177e4SLinus Torvalds 		char *buf;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 		cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED;
3801da177e4SLinus Torvalds 		cell->event.data.ext.ptr = NULL;
3811da177e4SLinus Torvalds 
382c7e0b5bfSTakashi Iwai 		src = (struct snd_seq_event_cell *)event->data.ext.ptr;
3831da177e4SLinus Torvalds 		buf = (char *)event->data.ext.ptr;
3841da177e4SLinus Torvalds 		tail = NULL;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 		while (ncells-- > 0) {
38746397622STakashi Iwai 			size = sizeof(struct snd_seq_event);
3881da177e4SLinus Torvalds 			if (len < size)
3891da177e4SLinus Torvalds 				size = len;
3907bd80091STakashi Iwai 			err = snd_seq_cell_alloc(pool, &tmp, nonblock, file,
3917bd80091STakashi Iwai 						 mutexp);
3921da177e4SLinus Torvalds 			if (err < 0)
3931da177e4SLinus Torvalds 				goto __error;
3941da177e4SLinus Torvalds 			if (cell->event.data.ext.ptr == NULL)
3951da177e4SLinus Torvalds 				cell->event.data.ext.ptr = tmp;
3961da177e4SLinus Torvalds 			if (tail)
3971da177e4SLinus Torvalds 				tail->next = tmp;
3981da177e4SLinus Torvalds 			tail = tmp;
3991da177e4SLinus Torvalds 			/* copy chunk */
4001da177e4SLinus Torvalds 			if (is_chained && src) {
4011da177e4SLinus Torvalds 				tmp->event = src->event;
4021da177e4SLinus Torvalds 				src = src->next;
4031da177e4SLinus Torvalds 			} else if (is_usrptr) {
404fea952e5SClemens Ladisch 				if (copy_from_user(&tmp->event, (char __force __user *)buf, size)) {
4051da177e4SLinus Torvalds 					err = -EFAULT;
4061da177e4SLinus Torvalds 					goto __error;
4071da177e4SLinus Torvalds 				}
4081da177e4SLinus Torvalds 			} else {
4091da177e4SLinus Torvalds 				memcpy(&tmp->event, buf, size);
4101da177e4SLinus Torvalds 			}
4111da177e4SLinus Torvalds 			buf += size;
4121da177e4SLinus Torvalds 			len -= size;
4131da177e4SLinus Torvalds 		}
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	*cellp = cell;
4171da177e4SLinus Torvalds 	return 0;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds __error:
4201da177e4SLinus Torvalds 	snd_seq_cell_free(cell);
4211da177e4SLinus Torvalds 	return err;
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds /* poll wait */
snd_seq_pool_poll_wait(struct snd_seq_pool * pool,struct file * file,poll_table * wait)426c7e0b5bfSTakashi Iwai int snd_seq_pool_poll_wait(struct snd_seq_pool *pool, struct file *file,
427c7e0b5bfSTakashi Iwai 			   poll_table *wait)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	poll_wait(file, &pool->output_sleep, wait);
430*e3cd33abSTakashi Iwai 	guard(spinlock_irq)(&pool->lock);
4311da177e4SLinus Torvalds 	return snd_seq_output_ok(pool);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds /* allocate room specified number of events */
snd_seq_pool_init(struct snd_seq_pool * pool)436c7e0b5bfSTakashi Iwai int snd_seq_pool_init(struct snd_seq_pool *pool)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	int cell;
439c7e0b5bfSTakashi Iwai 	struct snd_seq_event_cell *cellptr;
4401da177e4SLinus Torvalds 
4417eaa943cSTakashi Iwai 	if (snd_BUG_ON(!pool))
4427eaa943cSTakashi Iwai 		return -EINVAL;
4431da177e4SLinus Torvalds 
444126c18a4SDmitry Antipov 	cellptr = kvmalloc_array(pool->size,
445126c18a4SDmitry Antipov 				 sizeof(struct snd_seq_event_cell),
446fd7ae83dSTakashi Iwai 				 GFP_KERNEL);
447d99a36f4STakashi Iwai 	if (!cellptr)
4481da177e4SLinus Torvalds 		return -ENOMEM;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	/* add new cells to the free cell list */
4516768bd10STakashi Iwai 	guard(spinlock_irq)(&pool->lock);
452d99a36f4STakashi Iwai 	if (pool->ptr) {
453fd7ae83dSTakashi Iwai 		kvfree(cellptr);
454d99a36f4STakashi Iwai 		return 0;
455d99a36f4STakashi Iwai 	}
456d99a36f4STakashi Iwai 
457d99a36f4STakashi Iwai 	pool->ptr = cellptr;
4581da177e4SLinus Torvalds 	pool->free = NULL;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	for (cell = 0; cell < pool->size; cell++) {
4611da177e4SLinus Torvalds 		cellptr = pool->ptr + cell;
4621da177e4SLinus Torvalds 		cellptr->pool = pool;
4631da177e4SLinus Torvalds 		cellptr->next = pool->free;
4641da177e4SLinus Torvalds 		pool->free = cellptr;
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 	pool->room = (pool->size + 1) / 2;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	/* init statistics */
4691da177e4SLinus Torvalds 	pool->max_used = 0;
4701da177e4SLinus Torvalds 	pool->total_elements = pool->size;
4711da177e4SLinus Torvalds 	return 0;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
474c520ff3dSTakashi Iwai /* refuse the further insertion to the pool */
snd_seq_pool_mark_closing(struct snd_seq_pool * pool)475c520ff3dSTakashi Iwai void snd_seq_pool_mark_closing(struct snd_seq_pool *pool)
476c520ff3dSTakashi Iwai {
477c520ff3dSTakashi Iwai 	if (snd_BUG_ON(!pool))
478c520ff3dSTakashi Iwai 		return;
4796768bd10STakashi Iwai 	guard(spinlock_irqsave)(&pool->lock);
480c520ff3dSTakashi Iwai 	pool->closing = 1;
481c520ff3dSTakashi Iwai }
482c520ff3dSTakashi Iwai 
4831da177e4SLinus Torvalds /* remove events */
snd_seq_pool_done(struct snd_seq_pool * pool)484c7e0b5bfSTakashi Iwai int snd_seq_pool_done(struct snd_seq_pool *pool)
4851da177e4SLinus Torvalds {
486c7e0b5bfSTakashi Iwai 	struct snd_seq_event_cell *ptr;
4871da177e4SLinus Torvalds 
4887eaa943cSTakashi Iwai 	if (snd_BUG_ON(!pool))
4897eaa943cSTakashi Iwai 		return -EINVAL;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	/* wait for closing all threads */
4921da177e4SLinus Torvalds 	if (waitqueue_active(&pool->output_sleep))
4931da177e4SLinus Torvalds 		wake_up(&pool->output_sleep);
4941da177e4SLinus Torvalds 
49537a7ea4aSTakashi Iwai 	while (atomic_read(&pool->counter) > 0)
4968433a509SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/* release all resources */
4996768bd10STakashi Iwai 	scoped_guard(spinlock_irq, &pool->lock) {
5001da177e4SLinus Torvalds 		ptr = pool->ptr;
5011da177e4SLinus Torvalds 		pool->ptr = NULL;
5021da177e4SLinus Torvalds 		pool->free = NULL;
5031da177e4SLinus Torvalds 		pool->total_elements = 0;
5046768bd10STakashi Iwai 	}
5051da177e4SLinus Torvalds 
506fd7ae83dSTakashi Iwai 	kvfree(ptr);
5071da177e4SLinus Torvalds 
5086768bd10STakashi Iwai 	guard(spinlock_irq)(&pool->lock);
5091da177e4SLinus Torvalds 	pool->closing = 0;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	return 0;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds /* init new memory pool */
snd_seq_pool_new(int poolsize)516c7e0b5bfSTakashi Iwai struct snd_seq_pool *snd_seq_pool_new(int poolsize)
5171da177e4SLinus Torvalds {
518c7e0b5bfSTakashi Iwai 	struct snd_seq_pool *pool;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	/* create pool block */
521ecca82b4STakashi Iwai 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
52224db8bbaSTakashi Iwai 	if (!pool)
5231da177e4SLinus Torvalds 		return NULL;
5241da177e4SLinus Torvalds 	spin_lock_init(&pool->lock);
5251da177e4SLinus Torvalds 	pool->ptr = NULL;
5261da177e4SLinus Torvalds 	pool->free = NULL;
5271da177e4SLinus Torvalds 	pool->total_elements = 0;
5281da177e4SLinus Torvalds 	atomic_set(&pool->counter, 0);
5291da177e4SLinus Torvalds 	pool->closing = 0;
5301da177e4SLinus Torvalds 	init_waitqueue_head(&pool->output_sleep);
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	pool->size = poolsize;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	/* init statistics */
5351da177e4SLinus Torvalds 	pool->max_used = 0;
5361da177e4SLinus Torvalds 	return pool;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds /* remove memory pool */
snd_seq_pool_delete(struct snd_seq_pool ** ppool)540c7e0b5bfSTakashi Iwai int snd_seq_pool_delete(struct snd_seq_pool **ppool)
5411da177e4SLinus Torvalds {
542c7e0b5bfSTakashi Iwai 	struct snd_seq_pool *pool = *ppool;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	*ppool = NULL;
5451da177e4SLinus Torvalds 	if (pool == NULL)
5461da177e4SLinus Torvalds 		return 0;
547c520ff3dSTakashi Iwai 	snd_seq_pool_mark_closing(pool);
5481da177e4SLinus Torvalds 	snd_seq_pool_done(pool);
5491da177e4SLinus Torvalds 	kfree(pool);
5501da177e4SLinus Torvalds 	return 0;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds /* exported to seq_clientmgr.c */
snd_seq_info_pool(struct snd_info_buffer * buffer,struct snd_seq_pool * pool,char * space)554c7e0b5bfSTakashi Iwai void snd_seq_info_pool(struct snd_info_buffer *buffer,
555c7e0b5bfSTakashi Iwai 		       struct snd_seq_pool *pool, char *space)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	if (pool == NULL)
5581da177e4SLinus Torvalds 		return;
5591da177e4SLinus Torvalds 	snd_iprintf(buffer, "%sPool size          : %d\n", space, pool->total_elements);
5601da177e4SLinus Torvalds 	snd_iprintf(buffer, "%sCells in use       : %d\n", space, atomic_read(&pool->counter));
5611da177e4SLinus Torvalds 	snd_iprintf(buffer, "%sPeak cells in use  : %d\n", space, pool->max_used);
5621da177e4SLinus Torvalds 	snd_iprintf(buffer, "%sAlloc success      : %d\n", space, pool->event_alloc_success);
5631da177e4SLinus Torvalds 	snd_iprintf(buffer, "%sAlloc failures     : %d\n", space, pool->event_alloc_failures);
5641da177e4SLinus Torvalds }
565