dma-debug.c 25 KB

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