hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
kernel/sound/core/pcm_memory.c
....@@ -31,20 +31,51 @@
3131 module_param(max_alloc_per_card, ulong, 0644);
3232 MODULE_PARM_DESC(max_alloc_per_card, "Max total allocation bytes per card.");
3333
34
+static void __update_allocated_size(struct snd_card *card, ssize_t bytes)
35
+{
36
+ card->total_pcm_alloc_bytes += bytes;
37
+}
38
+
39
+static void update_allocated_size(struct snd_card *card, ssize_t bytes)
40
+{
41
+ mutex_lock(&card->memory_mutex);
42
+ __update_allocated_size(card, bytes);
43
+ mutex_unlock(&card->memory_mutex);
44
+}
45
+
46
+static void decrease_allocated_size(struct snd_card *card, size_t bytes)
47
+{
48
+ mutex_lock(&card->memory_mutex);
49
+ WARN_ON(card->total_pcm_alloc_bytes < bytes);
50
+ __update_allocated_size(card, -(ssize_t)bytes);
51
+ mutex_unlock(&card->memory_mutex);
52
+}
53
+
3454 static int do_alloc_pages(struct snd_card *card, int type, struct device *dev,
3555 size_t size, struct snd_dma_buffer *dmab)
3656 {
3757 int err;
3858
59
+ /* check and reserve the requested size */
60
+ mutex_lock(&card->memory_mutex);
3961 if (max_alloc_per_card &&
40
- card->total_pcm_alloc_bytes + size > max_alloc_per_card)
62
+ card->total_pcm_alloc_bytes + size > max_alloc_per_card) {
63
+ mutex_unlock(&card->memory_mutex);
4164 return -ENOMEM;
65
+ }
66
+ __update_allocated_size(card, size);
67
+ mutex_unlock(&card->memory_mutex);
4268
4369 err = snd_dma_alloc_pages(type, dev, size, dmab);
4470 if (!err) {
45
- mutex_lock(&card->memory_mutex);
46
- card->total_pcm_alloc_bytes += dmab->bytes;
47
- mutex_unlock(&card->memory_mutex);
71
+ /* the actual allocation size might be bigger than requested,
72
+ * and we need to correct the account
73
+ */
74
+ if (dmab->bytes != size)
75
+ update_allocated_size(card, dmab->bytes - size);
76
+ } else {
77
+ /* take back on allocation failure */
78
+ decrease_allocated_size(card, size);
4879 }
4980 return err;
5081 }
....@@ -53,10 +84,7 @@
5384 {
5485 if (!dmab->area)
5586 return;
56
- mutex_lock(&card->memory_mutex);
57
- WARN_ON(card->total_pcm_alloc_bytes < dmab->bytes);
58
- card->total_pcm_alloc_bytes -= dmab->bytes;
59
- mutex_unlock(&card->memory_mutex);
87
+ decrease_allocated_size(card, dmab->bytes);
6088 snd_dma_free_pages(dmab);
6189 dmab->area = NULL;
6290 }