dma-debug.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * Copyright (C) 2008 Advanced Micro Devices, Inc.
  3. *
  4. * Author: Joerg Roedel <joerg.roedel@amd.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/scatterlist.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/stacktrace.h>
  22. #include <linux/dma-debug.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/device.h>
  27. #include <linux/types.h>
  28. #include <linux/sched.h>
  29. #include <linux/ctype.h>
  30. #include <linux/list.h>
  31. #include <linux/slab.h>
  32. #include <asm/sections.h>
  33. #define HASH_SIZE 1024ULL
  34. #define HASH_FN_SHIFT 13
  35. #define HASH_FN_MASK (HASH_SIZE - 1)
  36. enum {
  37. dma_debug_single,
  38. dma_debug_page,
  39. dma_debug_sg,
  40. dma_debug_coherent,
  41. };
  42. #define DMA_DEBUG_STACKTRACE_ENTRIES 5
  43. struct dma_debug_entry {
  44. struct list_head list;
  45. struct device *dev;
  46. int type;
  47. phys_addr_t paddr;
  48. u64 dev_addr;
  49. u64 size;
  50. int direction;
  51. int sg_call_ents;
  52. int sg_mapped_ents;
  53. #ifdef CONFIG_STACKTRACE
  54. struct stack_trace stacktrace;
  55. unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
  56. #endif
  57. };
  58. struct hash_bucket {
  59. struct list_head list;
  60. spinlock_t lock;
  61. } ____cacheline_aligned_in_smp;
  62. /* Hash list to save the allocated dma addresses */
  63. static struct hash_bucket dma_entry_hash[HASH_SIZE];
  64. /* List of pre-allocated dma_debug_entry's */
  65. static LIST_HEAD(free_entries);
  66. /* Lock for the list above */
  67. static DEFINE_SPINLOCK(free_entries_lock);
  68. /* Global disable flag - will be set in case of an error */
  69. static bool global_disable __read_mostly;
  70. /* Global error count */
  71. static u32 error_count;
  72. /* Global error show enable*/
  73. static u32 show_all_errors __read_mostly;
  74. /* Number of errors to show */
  75. static u32 show_num_errors = 1;
  76. static u32 num_free_entries;
  77. static u32 min_free_entries;
  78. static u32 nr_total_entries;
  79. /* number of preallocated entries requested by kernel cmdline */
  80. static u32 req_entries;
  81. /* debugfs dentry's for the stuff above */
  82. static struct dentry *dma_debug_dent __read_mostly;
  83. static struct dentry *global_disable_dent __read_mostly;
  84. static struct dentry *error_count_dent __read_mostly;
  85. static struct dentry *show_all_errors_dent __read_mostly;
  86. static struct dentry *show_num_errors_dent __read_mostly;
  87. static struct dentry *num_free_entries_dent __read_mostly;
  88. static struct dentry *min_free_entries_dent __read_mostly;
  89. static struct dentry *filter_dent __read_mostly;
  90. /* per-driver filter related state */
  91. #define NAME_MAX_LEN 64
  92. static char current_driver_name[NAME_MAX_LEN] __read_mostly;
  93. static struct device_driver *current_driver __read_mostly;
  94. static DEFINE_RWLOCK(driver_name_lock);
  95. static const char *type2name[4] = { "single", "page",
  96. "scather-gather", "coherent" };
  97. static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
  98. "DMA_FROM_DEVICE", "DMA_NONE" };
  99. /*
  100. * The access to some variables in this macro is racy. We can't use atomic_t
  101. * here because all these variables are exported to debugfs. Some of them even
  102. * writeable. This is also the reason why a lock won't help much. But anyway,
  103. * the races are no big deal. Here is why:
  104. *
  105. * error_count: the addition is racy, but the worst thing that can happen is
  106. * that we don't count some errors
  107. * show_num_errors: the subtraction is racy. Also no big deal because in
  108. * worst case this will result in one warning more in the
  109. * system log than the user configured. This variable is
  110. * writeable via debugfs.
  111. */
  112. static inline void dump_entry_trace(struct dma_debug_entry *entry)
  113. {
  114. #ifdef CONFIG_STACKTRACE
  115. if (entry) {
  116. printk(KERN_WARNING "Mapped at:\n");
  117. print_stack_trace(&entry->stacktrace, 0);
  118. }
  119. #endif
  120. }
  121. static bool driver_filter(struct device *dev)
  122. {
  123. /* driver filter off */
  124. if (likely(!current_driver_name[0]))
  125. return true;
  126. /* driver filter on and initialized */
  127. if (current_driver && dev->driver == current_driver)
  128. return true;
  129. /* driver filter on but not yet initialized */
  130. if (!current_driver && current_driver_name[0]) {
  131. struct device_driver *drv = get_driver(dev->driver);
  132. unsigned long flags;
  133. bool ret = false;
  134. if (!drv)
  135. return false;
  136. /* lock to protect against change of current_driver_name */
  137. read_lock_irqsave(&driver_name_lock, flags);
  138. if (drv->name &&
  139. strncmp(current_driver_name, drv->name,
  140. NAME_MAX_LEN-1) == 0) {
  141. current_driver = drv;
  142. ret = true;
  143. }
  144. read_unlock_irqrestore(&driver_name_lock, flags);
  145. put_driver(drv);
  146. return ret;
  147. }
  148. return false;
  149. }
  150. #define err_printk(dev, entry, format, arg...) do { \
  151. error_count += 1; \
  152. if (driver_filter(dev) && \
  153. (show_all_errors || show_num_errors > 0)) { \
  154. WARN(1, "%s %s: " format, \
  155. dev_driver_string(dev), \
  156. dev_name(dev) , ## arg); \
  157. dump_entry_trace(entry); \
  158. } \
  159. if (!show_all_errors && show_num_errors > 0) \
  160. show_num_errors -= 1; \
  161. } while (0);
  162. /*
  163. * Hash related functions
  164. *
  165. * Every DMA-API request is saved into a struct dma_debug_entry. To
  166. * have quick access to these structs they are stored into a hash.
  167. */
  168. static int hash_fn(struct dma_debug_entry *entry)
  169. {
  170. /*
  171. * Hash function is based on the dma address.
  172. * We use bits 20-27 here as the index into the hash
  173. */
  174. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  175. }
  176. /*
  177. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  178. */
  179. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  180. unsigned long *flags)
  181. {
  182. int idx = hash_fn(entry);
  183. unsigned long __flags;
  184. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  185. *flags = __flags;
  186. return &dma_entry_hash[idx];
  187. }
  188. /*
  189. * Give up exclusive access to the hash bucket
  190. */
  191. static void put_hash_bucket(struct hash_bucket *bucket,
  192. unsigned long *flags)
  193. {
  194. unsigned long __flags = *flags;
  195. spin_unlock_irqrestore(&bucket->lock, __flags);
  196. }
  197. /*
  198. * Search a given entry in the hash bucket list
  199. */
  200. static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
  201. struct dma_debug_entry *ref)
  202. {
  203. struct dma_debug_entry *entry;
  204. list_for_each_entry(entry, &bucket->list, list) {
  205. if ((entry->dev_addr == ref->dev_addr) &&
  206. (entry->dev == ref->dev))
  207. return entry;
  208. }
  209. return NULL;
  210. }
  211. /*
  212. * Add an entry to a hash bucket
  213. */
  214. static void hash_bucket_add(struct hash_bucket *bucket,
  215. struct dma_debug_entry *entry)
  216. {
  217. list_add_tail(&entry->list, &bucket->list);
  218. }
  219. /*
  220. * Remove entry from a hash bucket list
  221. */
  222. static void hash_bucket_del(struct dma_debug_entry *entry)
  223. {
  224. list_del(&entry->list);
  225. }
  226. /*
  227. * Dump mapping entries for debugging purposes
  228. */
  229. void debug_dma_dump_mappings(struct device *dev)
  230. {
  231. int idx;
  232. for (idx = 0; idx < HASH_SIZE; idx++) {
  233. struct hash_bucket *bucket = &dma_entry_hash[idx];
  234. struct dma_debug_entry *entry;
  235. unsigned long flags;
  236. spin_lock_irqsave(&bucket->lock, flags);
  237. list_for_each_entry(entry, &bucket->list, list) {
  238. if (!dev || dev == entry->dev) {
  239. dev_info(entry->dev,
  240. "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
  241. type2name[entry->type], idx,
  242. (unsigned long long)entry->paddr,
  243. entry->dev_addr, entry->size,
  244. dir2name[entry->direction]);
  245. }
  246. }
  247. spin_unlock_irqrestore(&bucket->lock, flags);
  248. }
  249. }
  250. EXPORT_SYMBOL(debug_dma_dump_mappings);
  251. /*
  252. * Wrapper function for adding an entry to the hash.
  253. * This function takes care of locking itself.
  254. */
  255. static void add_dma_entry(struct dma_debug_entry *entry)
  256. {
  257. struct hash_bucket *bucket;
  258. unsigned long flags;
  259. bucket = get_hash_bucket(entry, &flags);
  260. hash_bucket_add(bucket, entry);
  261. put_hash_bucket(bucket, &flags);
  262. }
  263. static struct dma_debug_entry *__dma_entry_alloc(void)
  264. {
  265. struct dma_debug_entry *entry;
  266. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  267. list_del(&entry->list);
  268. memset(entry, 0, sizeof(*entry));
  269. num_free_entries -= 1;
  270. if (num_free_entries < min_free_entries)
  271. min_free_entries = num_free_entries;
  272. return entry;
  273. }
  274. /* struct dma_entry allocator
  275. *
  276. * The next two functions implement the allocator for
  277. * struct dma_debug_entries.
  278. */
  279. static struct dma_debug_entry *dma_entry_alloc(void)
  280. {
  281. struct dma_debug_entry *entry = NULL;
  282. unsigned long flags;
  283. spin_lock_irqsave(&free_entries_lock, flags);
  284. if (list_empty(&free_entries)) {
  285. printk(KERN_ERR "DMA-API: debugging out of memory "
  286. "- disabling\n");
  287. global_disable = true;
  288. goto out;
  289. }
  290. entry = __dma_entry_alloc();
  291. #ifdef CONFIG_STACKTRACE
  292. entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
  293. entry->stacktrace.entries = entry->st_entries;
  294. entry->stacktrace.skip = 2;
  295. save_stack_trace(&entry->stacktrace);
  296. #endif
  297. out:
  298. spin_unlock_irqrestore(&free_entries_lock, flags);
  299. return entry;
  300. }
  301. static void dma_entry_free(struct dma_debug_entry *entry)
  302. {
  303. unsigned long flags;
  304. /*
  305. * add to beginning of the list - this way the entries are
  306. * more likely cache hot when they are reallocated.
  307. */
  308. spin_lock_irqsave(&free_entries_lock, flags);
  309. list_add(&entry->list, &free_entries);
  310. num_free_entries += 1;
  311. spin_unlock_irqrestore(&free_entries_lock, flags);
  312. }
  313. int dma_debug_resize_entries(u32 num_entries)
  314. {
  315. int i, delta, ret = 0;
  316. unsigned long flags;
  317. struct dma_debug_entry *entry;
  318. LIST_HEAD(tmp);
  319. spin_lock_irqsave(&free_entries_lock, flags);
  320. if (nr_total_entries < num_entries) {
  321. delta = num_entries - nr_total_entries;
  322. spin_unlock_irqrestore(&free_entries_lock, flags);
  323. for (i = 0; i < delta; i++) {
  324. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  325. if (!entry)
  326. break;
  327. list_add_tail(&entry->list, &tmp);
  328. }
  329. spin_lock_irqsave(&free_entries_lock, flags);
  330. list_splice(&tmp, &free_entries);
  331. nr_total_entries += i;
  332. num_free_entries += i;
  333. } else {
  334. delta = nr_total_entries - num_entries;
  335. for (i = 0; i < delta && !list_empty(&free_entries); i++) {
  336. entry = __dma_entry_alloc();
  337. kfree(entry);
  338. }
  339. nr_total_entries -= i;
  340. }
  341. if (nr_total_entries != num_entries)
  342. ret = 1;
  343. spin_unlock_irqrestore(&free_entries_lock, flags);
  344. return ret;
  345. }
  346. EXPORT_SYMBOL(dma_debug_resize_entries);
  347. /*
  348. * DMA-API debugging init code
  349. *
  350. * The init code does two things:
  351. * 1. Initialize core data structures
  352. * 2. Preallocate a given number of dma_debug_entry structs
  353. */
  354. static int prealloc_memory(u32 num_entries)
  355. {
  356. struct dma_debug_entry *entry, *next_entry;
  357. int i;
  358. for (i = 0; i < num_entries; ++i) {
  359. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  360. if (!entry)
  361. goto out_err;
  362. list_add_tail(&entry->list, &free_entries);
  363. }
  364. num_free_entries = num_entries;
  365. min_free_entries = num_entries;
  366. printk(KERN_INFO "DMA-API: preallocated %d debug entries\n",
  367. num_entries);
  368. return 0;
  369. out_err:
  370. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  371. list_del(&entry->list);
  372. kfree(entry);
  373. }
  374. return -ENOMEM;
  375. }
  376. static ssize_t filter_read(struct file *file, char __user *user_buf,
  377. size_t count, loff_t *ppos)
  378. {
  379. unsigned long flags;
  380. char buf[NAME_MAX_LEN + 1];
  381. int len;
  382. if (!current_driver_name[0])
  383. return 0;
  384. /*
  385. * We can't copy to userspace directly because current_driver_name can
  386. * only be read under the driver_name_lock with irqs disabled. So
  387. * create a temporary copy first.
  388. */
  389. read_lock_irqsave(&driver_name_lock, flags);
  390. len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
  391. read_unlock_irqrestore(&driver_name_lock, flags);
  392. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  393. }
  394. static ssize_t filter_write(struct file *file, const char __user *userbuf,
  395. size_t count, loff_t *ppos)
  396. {
  397. unsigned long flags;
  398. char buf[NAME_MAX_LEN];
  399. size_t len = NAME_MAX_LEN - 1;
  400. int i;
  401. /*
  402. * We can't copy from userspace directly. Access to
  403. * current_driver_name is protected with a write_lock with irqs
  404. * disabled. Since copy_from_user can fault and may sleep we
  405. * need to copy to temporary buffer first
  406. */
  407. len = min(count, len);
  408. if (copy_from_user(buf, userbuf, len))
  409. return -EFAULT;
  410. buf[len] = 0;
  411. write_lock_irqsave(&driver_name_lock, flags);
  412. /* Now handle the string we got from userspace very carefully.
  413. * The rules are:
  414. * - only use the first token we got
  415. * - token delimiter is everything looking like a space
  416. * character (' ', '\n', '\t' ...)
  417. *
  418. */
  419. if (!isalnum(buf[0])) {
  420. /*
  421. If the first character userspace gave us is not
  422. * alphanumerical then assume the filter should be
  423. * switched off.
  424. */
  425. if (current_driver_name[0])
  426. printk(KERN_INFO "DMA-API: switching off dma-debug "
  427. "driver filter\n");
  428. current_driver_name[0] = 0;
  429. current_driver = NULL;
  430. goto out_unlock;
  431. }
  432. /*
  433. * Now parse out the first token and use it as the name for the
  434. * driver to filter for.
  435. */
  436. for (i = 0; i < NAME_MAX_LEN; ++i) {
  437. current_driver_name[i] = buf[i];
  438. if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
  439. break;
  440. }
  441. current_driver_name[i] = 0;
  442. current_driver = NULL;
  443. printk(KERN_INFO "DMA-API: enable driver filter for driver [%s]\n",
  444. current_driver_name);
  445. out_unlock:
  446. write_unlock_irqrestore(&driver_name_lock, flags);
  447. return count;
  448. }
  449. const struct file_operations filter_fops = {
  450. .read = filter_read,
  451. .write = filter_write,
  452. };
  453. static int dma_debug_fs_init(void)
  454. {
  455. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  456. if (!dma_debug_dent) {
  457. printk(KERN_ERR "DMA-API: can not create debugfs directory\n");
  458. return -ENOMEM;
  459. }
  460. global_disable_dent = debugfs_create_bool("disabled", 0444,
  461. dma_debug_dent,
  462. (u32 *)&global_disable);
  463. if (!global_disable_dent)
  464. goto out_err;
  465. error_count_dent = debugfs_create_u32("error_count", 0444,
  466. dma_debug_dent, &error_count);
  467. if (!error_count_dent)
  468. goto out_err;
  469. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  470. dma_debug_dent,
  471. &show_all_errors);
  472. if (!show_all_errors_dent)
  473. goto out_err;
  474. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  475. dma_debug_dent,
  476. &show_num_errors);
  477. if (!show_num_errors_dent)
  478. goto out_err;
  479. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  480. dma_debug_dent,
  481. &num_free_entries);
  482. if (!num_free_entries_dent)
  483. goto out_err;
  484. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  485. dma_debug_dent,
  486. &min_free_entries);
  487. if (!min_free_entries_dent)
  488. goto out_err;
  489. filter_dent = debugfs_create_file("driver_filter", 0644,
  490. dma_debug_dent, NULL, &filter_fops);
  491. if (!filter_dent)
  492. goto out_err;
  493. return 0;
  494. out_err:
  495. debugfs_remove_recursive(dma_debug_dent);
  496. return -ENOMEM;
  497. }
  498. void dma_debug_add_bus(struct bus_type *bus)
  499. {
  500. /* FIXME: register notifier */
  501. }
  502. /*
  503. * Let the architectures decide how many entries should be preallocated.
  504. */
  505. void dma_debug_init(u32 num_entries)
  506. {
  507. int i;
  508. if (global_disable)
  509. return;
  510. for (i = 0; i < HASH_SIZE; ++i) {
  511. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  512. dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
  513. }
  514. if (dma_debug_fs_init() != 0) {
  515. printk(KERN_ERR "DMA-API: error creating debugfs entries "
  516. "- disabling\n");
  517. global_disable = true;
  518. return;
  519. }
  520. if (req_entries)
  521. num_entries = req_entries;
  522. if (prealloc_memory(num_entries) != 0) {
  523. printk(KERN_ERR "DMA-API: debugging out of memory error "
  524. "- disabled\n");
  525. global_disable = true;
  526. return;
  527. }
  528. nr_total_entries = num_free_entries;
  529. printk(KERN_INFO "DMA-API: debugging enabled by kernel config\n");
  530. }
  531. static __init int dma_debug_cmdline(char *str)
  532. {
  533. if (!str)
  534. return -EINVAL;
  535. if (strncmp(str, "off", 3) == 0) {
  536. printk(KERN_INFO "DMA-API: debugging disabled on kernel "
  537. "command line\n");
  538. global_disable = true;
  539. }
  540. return 0;
  541. }
  542. static __init int dma_debug_entries_cmdline(char *str)
  543. {
  544. int res;
  545. if (!str)
  546. return -EINVAL;
  547. res = get_option(&str, &req_entries);
  548. if (!res)
  549. req_entries = 0;
  550. return 0;
  551. }
  552. __setup("dma_debug=", dma_debug_cmdline);
  553. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  554. static void check_unmap(struct dma_debug_entry *ref)
  555. {
  556. struct dma_debug_entry *entry;
  557. struct hash_bucket *bucket;
  558. unsigned long flags;
  559. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  560. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  561. "to free an invalid DMA memory address\n");
  562. return;
  563. }
  564. bucket = get_hash_bucket(ref, &flags);
  565. entry = hash_bucket_find(bucket, ref);
  566. if (!entry) {
  567. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  568. "to free DMA memory it has not allocated "
  569. "[device address=0x%016llx] [size=%llu bytes]\n",
  570. ref->dev_addr, ref->size);
  571. goto out;
  572. }
  573. if (ref->size != entry->size) {
  574. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  575. "DMA memory with different size "
  576. "[device address=0x%016llx] [map size=%llu bytes] "
  577. "[unmap size=%llu bytes]\n",
  578. ref->dev_addr, entry->size, ref->size);
  579. }
  580. if (ref->type != entry->type) {
  581. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  582. "DMA memory with wrong function "
  583. "[device address=0x%016llx] [size=%llu bytes] "
  584. "[mapped as %s] [unmapped as %s]\n",
  585. ref->dev_addr, ref->size,
  586. type2name[entry->type], type2name[ref->type]);
  587. } else if ((entry->type == dma_debug_coherent) &&
  588. (ref->paddr != entry->paddr)) {
  589. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  590. "DMA memory with different CPU address "
  591. "[device address=0x%016llx] [size=%llu bytes] "
  592. "[cpu alloc address=%p] [cpu free address=%p]",
  593. ref->dev_addr, ref->size,
  594. (void *)entry->paddr, (void *)ref->paddr);
  595. }
  596. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  597. ref->sg_call_ents != entry->sg_call_ents) {
  598. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  599. "DMA sg list with different entry count "
  600. "[map count=%d] [unmap count=%d]\n",
  601. entry->sg_call_ents, ref->sg_call_ents);
  602. }
  603. /*
  604. * This may be no bug in reality - but most implementations of the
  605. * DMA API don't handle this properly, so check for it here
  606. */
  607. if (ref->direction != entry->direction) {
  608. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  609. "DMA memory with different direction "
  610. "[device address=0x%016llx] [size=%llu bytes] "
  611. "[mapped with %s] [unmapped with %s]\n",
  612. ref->dev_addr, ref->size,
  613. dir2name[entry->direction],
  614. dir2name[ref->direction]);
  615. }
  616. hash_bucket_del(entry);
  617. dma_entry_free(entry);
  618. out:
  619. put_hash_bucket(bucket, &flags);
  620. }
  621. static void check_for_stack(struct device *dev, void *addr)
  622. {
  623. if (object_is_on_stack(addr))
  624. err_printk(dev, NULL, "DMA-API: device driver maps memory from"
  625. "stack [addr=%p]\n", addr);
  626. }
  627. static inline bool overlap(void *addr, u64 size, void *start, void *end)
  628. {
  629. void *addr2 = (char *)addr + size;
  630. return ((addr >= start && addr < end) ||
  631. (addr2 >= start && addr2 < end) ||
  632. ((addr < start) && (addr2 >= end)));
  633. }
  634. static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
  635. {
  636. if (overlap(addr, size, _text, _etext) ||
  637. overlap(addr, size, __start_rodata, __end_rodata))
  638. err_printk(dev, NULL, "DMA-API: device driver maps "
  639. "memory from kernel text or rodata "
  640. "[addr=%p] [size=%llu]\n", addr, size);
  641. }
  642. static void check_sync(struct device *dev, dma_addr_t addr,
  643. u64 size, u64 offset, int direction, bool to_cpu)
  644. {
  645. struct dma_debug_entry ref = {
  646. .dev = dev,
  647. .dev_addr = addr,
  648. .size = size,
  649. .direction = direction,
  650. };
  651. struct dma_debug_entry *entry;
  652. struct hash_bucket *bucket;
  653. unsigned long flags;
  654. bucket = get_hash_bucket(&ref, &flags);
  655. entry = hash_bucket_find(bucket, &ref);
  656. if (!entry) {
  657. err_printk(dev, NULL, "DMA-API: device driver tries "
  658. "to sync DMA memory it has not allocated "
  659. "[device address=0x%016llx] [size=%llu bytes]\n",
  660. (unsigned long long)addr, size);
  661. goto out;
  662. }
  663. if ((offset + size) > entry->size) {
  664. err_printk(dev, entry, "DMA-API: device driver syncs"
  665. " DMA memory outside allocated range "
  666. "[device address=0x%016llx] "
  667. "[allocation size=%llu bytes] [sync offset=%llu] "
  668. "[sync size=%llu]\n", entry->dev_addr, entry->size,
  669. offset, size);
  670. }
  671. if (direction != entry->direction) {
  672. err_printk(dev, entry, "DMA-API: device driver syncs "
  673. "DMA memory with different direction "
  674. "[device address=0x%016llx] [size=%llu bytes] "
  675. "[mapped with %s] [synced with %s]\n",
  676. (unsigned long long)addr, entry->size,
  677. dir2name[entry->direction],
  678. dir2name[direction]);
  679. }
  680. if (entry->direction == DMA_BIDIRECTIONAL)
  681. goto out;
  682. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  683. !(direction == DMA_TO_DEVICE))
  684. err_printk(dev, entry, "DMA-API: device driver syncs "
  685. "device read-only DMA memory for cpu "
  686. "[device address=0x%016llx] [size=%llu bytes] "
  687. "[mapped with %s] [synced with %s]\n",
  688. (unsigned long long)addr, entry->size,
  689. dir2name[entry->direction],
  690. dir2name[direction]);
  691. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  692. !(direction == DMA_FROM_DEVICE))
  693. err_printk(dev, entry, "DMA-API: device driver syncs "
  694. "device write-only DMA memory to device "
  695. "[device address=0x%016llx] [size=%llu bytes] "
  696. "[mapped with %s] [synced with %s]\n",
  697. (unsigned long long)addr, entry->size,
  698. dir2name[entry->direction],
  699. dir2name[direction]);
  700. out:
  701. put_hash_bucket(bucket, &flags);
  702. }
  703. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  704. size_t size, int direction, dma_addr_t dma_addr,
  705. bool map_single)
  706. {
  707. struct dma_debug_entry *entry;
  708. if (unlikely(global_disable))
  709. return;
  710. if (unlikely(dma_mapping_error(dev, dma_addr)))
  711. return;
  712. entry = dma_entry_alloc();
  713. if (!entry)
  714. return;
  715. entry->dev = dev;
  716. entry->type = dma_debug_page;
  717. entry->paddr = page_to_phys(page) + offset;
  718. entry->dev_addr = dma_addr;
  719. entry->size = size;
  720. entry->direction = direction;
  721. if (map_single)
  722. entry->type = dma_debug_single;
  723. if (!PageHighMem(page)) {
  724. void *addr = ((char *)page_address(page)) + offset;
  725. check_for_stack(dev, addr);
  726. check_for_illegal_area(dev, addr, size);
  727. }
  728. add_dma_entry(entry);
  729. }
  730. EXPORT_SYMBOL(debug_dma_map_page);
  731. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  732. size_t size, int direction, bool map_single)
  733. {
  734. struct dma_debug_entry ref = {
  735. .type = dma_debug_page,
  736. .dev = dev,
  737. .dev_addr = addr,
  738. .size = size,
  739. .direction = direction,
  740. };
  741. if (unlikely(global_disable))
  742. return;
  743. if (map_single)
  744. ref.type = dma_debug_single;
  745. check_unmap(&ref);
  746. }
  747. EXPORT_SYMBOL(debug_dma_unmap_page);
  748. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  749. int nents, int mapped_ents, int direction)
  750. {
  751. struct dma_debug_entry *entry;
  752. struct scatterlist *s;
  753. int i;
  754. if (unlikely(global_disable))
  755. return;
  756. for_each_sg(sg, s, mapped_ents, i) {
  757. entry = dma_entry_alloc();
  758. if (!entry)
  759. return;
  760. entry->type = dma_debug_sg;
  761. entry->dev = dev;
  762. entry->paddr = sg_phys(s);
  763. entry->size = s->length;
  764. entry->dev_addr = s->dma_address;
  765. entry->direction = direction;
  766. entry->sg_call_ents = nents;
  767. entry->sg_mapped_ents = mapped_ents;
  768. if (!PageHighMem(sg_page(s))) {
  769. check_for_stack(dev, sg_virt(s));
  770. check_for_illegal_area(dev, sg_virt(s), s->length);
  771. }
  772. add_dma_entry(entry);
  773. }
  774. }
  775. EXPORT_SYMBOL(debug_dma_map_sg);
  776. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  777. int nelems, int dir)
  778. {
  779. struct dma_debug_entry *entry;
  780. struct scatterlist *s;
  781. int mapped_ents = 0, i;
  782. unsigned long flags;
  783. if (unlikely(global_disable))
  784. return;
  785. for_each_sg(sglist, s, nelems, i) {
  786. struct dma_debug_entry ref = {
  787. .type = dma_debug_sg,
  788. .dev = dev,
  789. .paddr = sg_phys(s),
  790. .dev_addr = s->dma_address,
  791. .size = s->length,
  792. .direction = dir,
  793. .sg_call_ents = 0,
  794. };
  795. if (mapped_ents && i >= mapped_ents)
  796. break;
  797. if (mapped_ents == 0) {
  798. struct hash_bucket *bucket;
  799. ref.sg_call_ents = nelems;
  800. bucket = get_hash_bucket(&ref, &flags);
  801. entry = hash_bucket_find(bucket, &ref);
  802. if (entry)
  803. mapped_ents = entry->sg_mapped_ents;
  804. put_hash_bucket(bucket, &flags);
  805. }
  806. check_unmap(&ref);
  807. }
  808. }
  809. EXPORT_SYMBOL(debug_dma_unmap_sg);
  810. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  811. dma_addr_t dma_addr, void *virt)
  812. {
  813. struct dma_debug_entry *entry;
  814. if (unlikely(global_disable))
  815. return;
  816. if (unlikely(virt == NULL))
  817. return;
  818. entry = dma_entry_alloc();
  819. if (!entry)
  820. return;
  821. entry->type = dma_debug_coherent;
  822. entry->dev = dev;
  823. entry->paddr = virt_to_phys(virt);
  824. entry->size = size;
  825. entry->dev_addr = dma_addr;
  826. entry->direction = DMA_BIDIRECTIONAL;
  827. add_dma_entry(entry);
  828. }
  829. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  830. void debug_dma_free_coherent(struct device *dev, size_t size,
  831. void *virt, dma_addr_t addr)
  832. {
  833. struct dma_debug_entry ref = {
  834. .type = dma_debug_coherent,
  835. .dev = dev,
  836. .paddr = virt_to_phys(virt),
  837. .dev_addr = addr,
  838. .size = size,
  839. .direction = DMA_BIDIRECTIONAL,
  840. };
  841. if (unlikely(global_disable))
  842. return;
  843. check_unmap(&ref);
  844. }
  845. EXPORT_SYMBOL(debug_dma_free_coherent);
  846. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  847. size_t size, int direction)
  848. {
  849. if (unlikely(global_disable))
  850. return;
  851. check_sync(dev, dma_handle, size, 0, direction, true);
  852. }
  853. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  854. void debug_dma_sync_single_for_device(struct device *dev,
  855. dma_addr_t dma_handle, size_t size,
  856. int direction)
  857. {
  858. if (unlikely(global_disable))
  859. return;
  860. check_sync(dev, dma_handle, size, 0, direction, false);
  861. }
  862. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  863. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  864. dma_addr_t dma_handle,
  865. unsigned long offset, size_t size,
  866. int direction)
  867. {
  868. if (unlikely(global_disable))
  869. return;
  870. check_sync(dev, dma_handle, size, offset, direction, true);
  871. }
  872. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  873. void debug_dma_sync_single_range_for_device(struct device *dev,
  874. dma_addr_t dma_handle,
  875. unsigned long offset,
  876. size_t size, int direction)
  877. {
  878. if (unlikely(global_disable))
  879. return;
  880. check_sync(dev, dma_handle, size, offset, direction, false);
  881. }
  882. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  883. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  884. int nelems, int direction)
  885. {
  886. struct scatterlist *s;
  887. int i;
  888. if (unlikely(global_disable))
  889. return;
  890. for_each_sg(sg, s, nelems, i) {
  891. check_sync(dev, s->dma_address, s->dma_length, 0,
  892. direction, true);
  893. }
  894. }
  895. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  896. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  897. int nelems, int direction)
  898. {
  899. struct scatterlist *s;
  900. int i;
  901. if (unlikely(global_disable))
  902. return;
  903. for_each_sg(sg, s, nelems, i) {
  904. check_sync(dev, s->dma_address, s->dma_length, 0,
  905. direction, false);
  906. }
  907. }
  908. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);