memory.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  3. *
  4. * Memory allocation helpers.
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <sound/driver.h>
  23. #include <asm/io.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/time.h>
  28. #include <linux/pci.h>
  29. #include <sound/core.h>
  30. #include <sound/info.h>
  31. /*
  32. * memory allocation helpers and debug routines
  33. */
  34. #ifdef CONFIG_SND_DEBUG_MEMORY
  35. struct snd_alloc_track {
  36. unsigned long magic;
  37. void *caller;
  38. size_t size;
  39. struct list_head list;
  40. long data[0];
  41. };
  42. #define snd_alloc_track_entry(obj) (struct snd_alloc_track *)((char*)obj - (unsigned long)((struct snd_alloc_track *)0)->data)
  43. static long snd_alloc_kmalloc;
  44. static long snd_alloc_vmalloc;
  45. static LIST_HEAD(snd_alloc_kmalloc_list);
  46. static LIST_HEAD(snd_alloc_vmalloc_list);
  47. static DEFINE_SPINLOCK(snd_alloc_kmalloc_lock);
  48. static DEFINE_SPINLOCK(snd_alloc_vmalloc_lock);
  49. #define KMALLOC_MAGIC 0x87654321
  50. #define VMALLOC_MAGIC 0x87654320
  51. static snd_info_entry_t *snd_memory_info_entry;
  52. void __init snd_memory_init(void)
  53. {
  54. snd_alloc_kmalloc = 0;
  55. snd_alloc_vmalloc = 0;
  56. }
  57. void snd_memory_done(void)
  58. {
  59. struct list_head *head;
  60. struct snd_alloc_track *t;
  61. if (snd_alloc_kmalloc > 0)
  62. snd_printk(KERN_ERR "Not freed snd_alloc_kmalloc = %li\n", snd_alloc_kmalloc);
  63. if (snd_alloc_vmalloc > 0)
  64. snd_printk(KERN_ERR "Not freed snd_alloc_vmalloc = %li\n", snd_alloc_vmalloc);
  65. list_for_each_prev(head, &snd_alloc_kmalloc_list) {
  66. t = list_entry(head, struct snd_alloc_track, list);
  67. if (t->magic != KMALLOC_MAGIC) {
  68. snd_printk(KERN_ERR "Corrupted kmalloc\n");
  69. break;
  70. }
  71. snd_printk(KERN_ERR "kmalloc(%ld) from %p not freed\n", (long) t->size, t->caller);
  72. }
  73. list_for_each_prev(head, &snd_alloc_vmalloc_list) {
  74. t = list_entry(head, struct snd_alloc_track, list);
  75. if (t->magic != VMALLOC_MAGIC) {
  76. snd_printk(KERN_ERR "Corrupted vmalloc\n");
  77. break;
  78. }
  79. snd_printk(KERN_ERR "vmalloc(%ld) from %p not freed\n", (long) t->size, t->caller);
  80. }
  81. }
  82. static void *__snd_kmalloc(size_t size, unsigned int __nocast flags, void *caller)
  83. {
  84. unsigned long cpu_flags;
  85. struct snd_alloc_track *t;
  86. void *ptr;
  87. ptr = snd_wrapper_kmalloc(size + sizeof(struct snd_alloc_track), flags);
  88. if (ptr != NULL) {
  89. t = (struct snd_alloc_track *)ptr;
  90. t->magic = KMALLOC_MAGIC;
  91. t->caller = caller;
  92. spin_lock_irqsave(&snd_alloc_kmalloc_lock, cpu_flags);
  93. list_add_tail(&t->list, &snd_alloc_kmalloc_list);
  94. spin_unlock_irqrestore(&snd_alloc_kmalloc_lock, cpu_flags);
  95. t->size = size;
  96. snd_alloc_kmalloc += size;
  97. ptr = t->data;
  98. }
  99. return ptr;
  100. }
  101. #define _snd_kmalloc(size, flags) __snd_kmalloc((size), (flags), __builtin_return_address(0));
  102. void *snd_hidden_kmalloc(size_t size, unsigned int __nocast flags)
  103. {
  104. return _snd_kmalloc(size, flags);
  105. }
  106. void *snd_hidden_kzalloc(size_t size, unsigned int __nocast flags)
  107. {
  108. void *ret = _snd_kmalloc(size, flags);
  109. if (ret)
  110. memset(ret, 0, size);
  111. return ret;
  112. }
  113. EXPORT_SYMBOL(snd_hidden_kzalloc);
  114. void *snd_hidden_kcalloc(size_t n, size_t size, unsigned int __nocast flags)
  115. {
  116. void *ret = NULL;
  117. if (n != 0 && size > INT_MAX / n)
  118. return ret;
  119. return snd_hidden_kzalloc(n * size, flags);
  120. }
  121. void snd_hidden_kfree(const void *obj)
  122. {
  123. unsigned long flags;
  124. struct snd_alloc_track *t;
  125. if (obj == NULL)
  126. return;
  127. t = snd_alloc_track_entry(obj);
  128. if (t->magic != KMALLOC_MAGIC) {
  129. snd_printk(KERN_WARNING "bad kfree (called from %p)\n", __builtin_return_address(0));
  130. return;
  131. }
  132. spin_lock_irqsave(&snd_alloc_kmalloc_lock, flags);
  133. list_del(&t->list);
  134. spin_unlock_irqrestore(&snd_alloc_kmalloc_lock, flags);
  135. t->magic = 0;
  136. snd_alloc_kmalloc -= t->size;
  137. obj = t;
  138. snd_wrapper_kfree(obj);
  139. }
  140. void *snd_hidden_vmalloc(unsigned long size)
  141. {
  142. void *ptr;
  143. ptr = snd_wrapper_vmalloc(size + sizeof(struct snd_alloc_track));
  144. if (ptr) {
  145. struct snd_alloc_track *t = (struct snd_alloc_track *)ptr;
  146. t->magic = VMALLOC_MAGIC;
  147. t->caller = __builtin_return_address(0);
  148. spin_lock(&snd_alloc_vmalloc_lock);
  149. list_add_tail(&t->list, &snd_alloc_vmalloc_list);
  150. spin_unlock(&snd_alloc_vmalloc_lock);
  151. t->size = size;
  152. snd_alloc_vmalloc += size;
  153. ptr = t->data;
  154. }
  155. return ptr;
  156. }
  157. void snd_hidden_vfree(void *obj)
  158. {
  159. struct snd_alloc_track *t;
  160. if (obj == NULL)
  161. return;
  162. t = snd_alloc_track_entry(obj);
  163. if (t->magic != VMALLOC_MAGIC) {
  164. snd_printk(KERN_ERR "bad vfree (called from %p)\n", __builtin_return_address(0));
  165. return;
  166. }
  167. spin_lock(&snd_alloc_vmalloc_lock);
  168. list_del(&t->list);
  169. spin_unlock(&snd_alloc_vmalloc_lock);
  170. t->magic = 0;
  171. snd_alloc_vmalloc -= t->size;
  172. obj = t;
  173. snd_wrapper_vfree(obj);
  174. }
  175. char *snd_hidden_kstrdup(const char *s, unsigned int __nocast flags)
  176. {
  177. int len;
  178. char *buf;
  179. if (!s) return NULL;
  180. len = strlen(s) + 1;
  181. buf = _snd_kmalloc(len, flags);
  182. if (buf)
  183. memcpy(buf, s, len);
  184. return buf;
  185. }
  186. static void snd_memory_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
  187. {
  188. snd_iprintf(buffer, "kmalloc: %li bytes\n", snd_alloc_kmalloc);
  189. snd_iprintf(buffer, "vmalloc: %li bytes\n", snd_alloc_vmalloc);
  190. }
  191. int __init snd_memory_info_init(void)
  192. {
  193. snd_info_entry_t *entry;
  194. entry = snd_info_create_module_entry(THIS_MODULE, "meminfo", NULL);
  195. if (entry) {
  196. entry->c.text.read_size = 256;
  197. entry->c.text.read = snd_memory_info_read;
  198. if (snd_info_register(entry) < 0) {
  199. snd_info_free_entry(entry);
  200. entry = NULL;
  201. }
  202. }
  203. snd_memory_info_entry = entry;
  204. return 0;
  205. }
  206. int __exit snd_memory_info_done(void)
  207. {
  208. if (snd_memory_info_entry)
  209. snd_info_unregister(snd_memory_info_entry);
  210. return 0;
  211. }
  212. #endif /* CONFIG_SND_DEBUG_MEMORY */
  213. /**
  214. * copy_to_user_fromio - copy data from mmio-space to user-space
  215. * @dst: the destination pointer on user-space
  216. * @src: the source pointer on mmio
  217. * @count: the data size to copy in bytes
  218. *
  219. * Copies the data from mmio-space to user-space.
  220. *
  221. * Returns zero if successful, or non-zero on failure.
  222. */
  223. int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
  224. {
  225. #if defined(__i386__) || defined(CONFIG_SPARC32)
  226. return copy_to_user(dst, (const void*)src, count) ? -EFAULT : 0;
  227. #else
  228. char buf[256];
  229. while (count) {
  230. size_t c = count;
  231. if (c > sizeof(buf))
  232. c = sizeof(buf);
  233. memcpy_fromio(buf, (void __iomem *)src, c);
  234. if (copy_to_user(dst, buf, c))
  235. return -EFAULT;
  236. count -= c;
  237. dst += c;
  238. src += c;
  239. }
  240. return 0;
  241. #endif
  242. }
  243. /**
  244. * copy_from_user_toio - copy data from user-space to mmio-space
  245. * @dst: the destination pointer on mmio-space
  246. * @src: the source pointer on user-space
  247. * @count: the data size to copy in bytes
  248. *
  249. * Copies the data from user-space to mmio-space.
  250. *
  251. * Returns zero if successful, or non-zero on failure.
  252. */
  253. int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
  254. {
  255. #if defined(__i386__) || defined(CONFIG_SPARC32)
  256. return copy_from_user((void*)dst, src, count) ? -EFAULT : 0;
  257. #else
  258. char buf[256];
  259. while (count) {
  260. size_t c = count;
  261. if (c > sizeof(buf))
  262. c = sizeof(buf);
  263. if (copy_from_user(buf, src, c))
  264. return -EFAULT;
  265. memcpy_toio(dst, buf, c);
  266. count -= c;
  267. dst += c;
  268. src += c;
  269. }
  270. return 0;
  271. #endif
  272. }