memory.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 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, int 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, int flags)
  103. {
  104. return _snd_kmalloc(size, flags);
  105. }
  106. void *snd_hidden_kcalloc(size_t n, size_t size, int flags)
  107. {
  108. void *ret = NULL;
  109. if (n != 0 && size > INT_MAX / n)
  110. return ret;
  111. ret = _snd_kmalloc(n * size, flags);
  112. if (ret)
  113. memset(ret, 0, n * size);
  114. return ret;
  115. }
  116. void snd_hidden_kfree(const void *obj)
  117. {
  118. unsigned long flags;
  119. struct snd_alloc_track *t;
  120. if (obj == NULL)
  121. return;
  122. t = snd_alloc_track_entry(obj);
  123. if (t->magic != KMALLOC_MAGIC) {
  124. snd_printk(KERN_WARNING "bad kfree (called from %p)\n", __builtin_return_address(0));
  125. return;
  126. }
  127. spin_lock_irqsave(&snd_alloc_kmalloc_lock, flags);
  128. list_del(&t->list);
  129. spin_unlock_irqrestore(&snd_alloc_kmalloc_lock, flags);
  130. t->magic = 0;
  131. snd_alloc_kmalloc -= t->size;
  132. obj = t;
  133. snd_wrapper_kfree(obj);
  134. }
  135. void *snd_hidden_vmalloc(unsigned long size)
  136. {
  137. void *ptr;
  138. ptr = snd_wrapper_vmalloc(size + sizeof(struct snd_alloc_track));
  139. if (ptr) {
  140. struct snd_alloc_track *t = (struct snd_alloc_track *)ptr;
  141. t->magic = VMALLOC_MAGIC;
  142. t->caller = __builtin_return_address(0);
  143. spin_lock(&snd_alloc_vmalloc_lock);
  144. list_add_tail(&t->list, &snd_alloc_vmalloc_list);
  145. spin_unlock(&snd_alloc_vmalloc_lock);
  146. t->size = size;
  147. snd_alloc_vmalloc += size;
  148. ptr = t->data;
  149. }
  150. return ptr;
  151. }
  152. void snd_hidden_vfree(void *obj)
  153. {
  154. struct snd_alloc_track *t;
  155. if (obj == NULL)
  156. return;
  157. t = snd_alloc_track_entry(obj);
  158. if (t->magic != VMALLOC_MAGIC) {
  159. snd_printk(KERN_ERR "bad vfree (called from %p)\n", __builtin_return_address(0));
  160. return;
  161. }
  162. spin_lock(&snd_alloc_vmalloc_lock);
  163. list_del(&t->list);
  164. spin_unlock(&snd_alloc_vmalloc_lock);
  165. t->magic = 0;
  166. snd_alloc_vmalloc -= t->size;
  167. obj = t;
  168. snd_wrapper_vfree(obj);
  169. }
  170. static void snd_memory_info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
  171. {
  172. snd_iprintf(buffer, "kmalloc: %li bytes\n", snd_alloc_kmalloc);
  173. snd_iprintf(buffer, "vmalloc: %li bytes\n", snd_alloc_vmalloc);
  174. }
  175. int __init snd_memory_info_init(void)
  176. {
  177. snd_info_entry_t *entry;
  178. entry = snd_info_create_module_entry(THIS_MODULE, "meminfo", NULL);
  179. if (entry) {
  180. entry->c.text.read_size = 256;
  181. entry->c.text.read = snd_memory_info_read;
  182. if (snd_info_register(entry) < 0) {
  183. snd_info_free_entry(entry);
  184. entry = NULL;
  185. }
  186. }
  187. snd_memory_info_entry = entry;
  188. return 0;
  189. }
  190. int __exit snd_memory_info_done(void)
  191. {
  192. if (snd_memory_info_entry)
  193. snd_info_unregister(snd_memory_info_entry);
  194. return 0;
  195. }
  196. #else
  197. #define _snd_kmalloc kmalloc
  198. #endif /* CONFIG_SND_DEBUG_MEMORY */
  199. /**
  200. * snd_kmalloc_strdup - copy the string
  201. * @string: the original string
  202. * @flags: allocation conditions, GFP_XXX
  203. *
  204. * Allocates a memory chunk via kmalloc() and copies the string to it.
  205. *
  206. * Returns the pointer, or NULL if no enoguh memory.
  207. */
  208. char *snd_kmalloc_strdup(const char *string, int flags)
  209. {
  210. size_t len;
  211. char *ptr;
  212. if (!string)
  213. return NULL;
  214. len = strlen(string) + 1;
  215. ptr = _snd_kmalloc(len, flags);
  216. if (ptr)
  217. memcpy(ptr, string, len);
  218. return ptr;
  219. }
  220. /**
  221. * copy_to_user_fromio - copy data from mmio-space to user-space
  222. * @dst: the destination pointer on user-space
  223. * @src: the source pointer on mmio
  224. * @count: the data size to copy in bytes
  225. *
  226. * Copies the data from mmio-space to user-space.
  227. *
  228. * Returns zero if successful, or non-zero on failure.
  229. */
  230. int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count)
  231. {
  232. #if defined(__i386__) || defined(CONFIG_SPARC32)
  233. return copy_to_user(dst, (const void*)src, count) ? -EFAULT : 0;
  234. #else
  235. char buf[256];
  236. while (count) {
  237. size_t c = count;
  238. if (c > sizeof(buf))
  239. c = sizeof(buf);
  240. memcpy_fromio(buf, (void __iomem *)src, c);
  241. if (copy_to_user(dst, buf, c))
  242. return -EFAULT;
  243. count -= c;
  244. dst += c;
  245. src += c;
  246. }
  247. return 0;
  248. #endif
  249. }
  250. /**
  251. * copy_from_user_toio - copy data from user-space to mmio-space
  252. * @dst: the destination pointer on mmio-space
  253. * @src: the source pointer on user-space
  254. * @count: the data size to copy in bytes
  255. *
  256. * Copies the data from user-space to mmio-space.
  257. *
  258. * Returns zero if successful, or non-zero on failure.
  259. */
  260. int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count)
  261. {
  262. #if defined(__i386__) || defined(CONFIG_SPARC32)
  263. return copy_from_user((void*)dst, src, count) ? -EFAULT : 0;
  264. #else
  265. char buf[256];
  266. while (count) {
  267. size_t c = count;
  268. if (c > sizeof(buf))
  269. c = sizeof(buf);
  270. if (copy_from_user(buf, src, c))
  271. return -EFAULT;
  272. memcpy_toio(dst, buf, c);
  273. count -= c;
  274. dst += c;
  275. src += c;
  276. }
  277. return 0;
  278. #endif
  279. }