dma-debug.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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. /* little merge helper - remove it after the merge window */
  100. #ifndef BUS_NOTIFY_UNBOUND_DRIVER
  101. #define BUS_NOTIFY_UNBOUND_DRIVER 0x0005
  102. #endif
  103. /*
  104. * The access to some variables in this macro is racy. We can't use atomic_t
  105. * here because all these variables are exported to debugfs. Some of them even
  106. * writeable. This is also the reason why a lock won't help much. But anyway,
  107. * the races are no big deal. Here is why:
  108. *
  109. * error_count: the addition is racy, but the worst thing that can happen is
  110. * that we don't count some errors
  111. * show_num_errors: the subtraction is racy. Also no big deal because in
  112. * worst case this will result in one warning more in the
  113. * system log than the user configured. This variable is
  114. * writeable via debugfs.
  115. */
  116. static inline void dump_entry_trace(struct dma_debug_entry *entry)
  117. {
  118. #ifdef CONFIG_STACKTRACE
  119. if (entry) {
  120. pr_warning("Mapped at:\n");
  121. print_stack_trace(&entry->stacktrace, 0);
  122. }
  123. #endif
  124. }
  125. static bool driver_filter(struct device *dev)
  126. {
  127. struct device_driver *drv;
  128. unsigned long flags;
  129. bool ret;
  130. /* driver filter off */
  131. if (likely(!current_driver_name[0]))
  132. return true;
  133. /* driver filter on and initialized */
  134. if (current_driver && dev->driver == current_driver)
  135. return true;
  136. if (current_driver || !current_driver_name[0])
  137. return false;
  138. /* driver filter on but not yet initialized */
  139. drv = get_driver(dev->driver);
  140. if (!drv)
  141. return false;
  142. /* lock to protect against change of current_driver_name */
  143. read_lock_irqsave(&driver_name_lock, flags);
  144. ret = false;
  145. if (drv->name &&
  146. strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
  147. current_driver = drv;
  148. ret = true;
  149. }
  150. read_unlock_irqrestore(&driver_name_lock, flags);
  151. put_driver(drv);
  152. return ret;
  153. }
  154. #define err_printk(dev, entry, format, arg...) do { \
  155. error_count += 1; \
  156. if (driver_filter(dev) && \
  157. (show_all_errors || show_num_errors > 0)) { \
  158. WARN(1, "%s %s: " format, \
  159. dev_driver_string(dev), \
  160. dev_name(dev) , ## arg); \
  161. dump_entry_trace(entry); \
  162. } \
  163. if (!show_all_errors && show_num_errors > 0) \
  164. show_num_errors -= 1; \
  165. } while (0);
  166. /*
  167. * Hash related functions
  168. *
  169. * Every DMA-API request is saved into a struct dma_debug_entry. To
  170. * have quick access to these structs they are stored into a hash.
  171. */
  172. static int hash_fn(struct dma_debug_entry *entry)
  173. {
  174. /*
  175. * Hash function is based on the dma address.
  176. * We use bits 20-27 here as the index into the hash
  177. */
  178. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  179. }
  180. /*
  181. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  182. */
  183. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  184. unsigned long *flags)
  185. {
  186. int idx = hash_fn(entry);
  187. unsigned long __flags;
  188. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  189. *flags = __flags;
  190. return &dma_entry_hash[idx];
  191. }
  192. /*
  193. * Give up exclusive access to the hash bucket
  194. */
  195. static void put_hash_bucket(struct hash_bucket *bucket,
  196. unsigned long *flags)
  197. {
  198. unsigned long __flags = *flags;
  199. spin_unlock_irqrestore(&bucket->lock, __flags);
  200. }
  201. /*
  202. * Search a given entry in the hash bucket list
  203. */
  204. static struct dma_debug_entry *hash_bucket_find(struct hash_bucket *bucket,
  205. struct dma_debug_entry *ref)
  206. {
  207. struct dma_debug_entry *entry, *ret = NULL;
  208. int matches = 0, match_lvl, last_lvl = 0;
  209. list_for_each_entry(entry, &bucket->list, list) {
  210. if ((entry->dev_addr != ref->dev_addr) ||
  211. (entry->dev != ref->dev))
  212. continue;
  213. /*
  214. * Some drivers map the same physical address multiple
  215. * times. Without a hardware IOMMU this results in the
  216. * same device addresses being put into the dma-debug
  217. * hash multiple times too. This can result in false
  218. * positives being reported. Therfore we implement a
  219. * best-fit algorithm here which returns the entry from
  220. * the hash which fits best to the reference value
  221. * instead of the first-fit.
  222. */
  223. matches += 1;
  224. match_lvl = 0;
  225. entry->size == ref->size ? ++match_lvl : match_lvl;
  226. entry->type == ref->type ? ++match_lvl : match_lvl;
  227. entry->direction == ref->direction ? ++match_lvl : match_lvl;
  228. if (match_lvl == 3) {
  229. /* perfect-fit - return the result */
  230. return entry;
  231. } else if (match_lvl > last_lvl) {
  232. /*
  233. * We found an entry that fits better then the
  234. * previous one
  235. */
  236. last_lvl = match_lvl;
  237. ret = entry;
  238. }
  239. }
  240. /*
  241. * If we have multiple matches but no perfect-fit, just return
  242. * NULL.
  243. */
  244. ret = (matches == 1) ? ret : NULL;
  245. return ret;
  246. }
  247. /*
  248. * Add an entry to a hash bucket
  249. */
  250. static void hash_bucket_add(struct hash_bucket *bucket,
  251. struct dma_debug_entry *entry)
  252. {
  253. list_add_tail(&entry->list, &bucket->list);
  254. }
  255. /*
  256. * Remove entry from a hash bucket list
  257. */
  258. static void hash_bucket_del(struct dma_debug_entry *entry)
  259. {
  260. list_del(&entry->list);
  261. }
  262. /*
  263. * Dump mapping entries for debugging purposes
  264. */
  265. void debug_dma_dump_mappings(struct device *dev)
  266. {
  267. int idx;
  268. for (idx = 0; idx < HASH_SIZE; idx++) {
  269. struct hash_bucket *bucket = &dma_entry_hash[idx];
  270. struct dma_debug_entry *entry;
  271. unsigned long flags;
  272. spin_lock_irqsave(&bucket->lock, flags);
  273. list_for_each_entry(entry, &bucket->list, list) {
  274. if (!dev || dev == entry->dev) {
  275. dev_info(entry->dev,
  276. "%s idx %d P=%Lx D=%Lx L=%Lx %s\n",
  277. type2name[entry->type], idx,
  278. (unsigned long long)entry->paddr,
  279. entry->dev_addr, entry->size,
  280. dir2name[entry->direction]);
  281. }
  282. }
  283. spin_unlock_irqrestore(&bucket->lock, flags);
  284. }
  285. }
  286. EXPORT_SYMBOL(debug_dma_dump_mappings);
  287. /*
  288. * Wrapper function for adding an entry to the hash.
  289. * This function takes care of locking itself.
  290. */
  291. static void add_dma_entry(struct dma_debug_entry *entry)
  292. {
  293. struct hash_bucket *bucket;
  294. unsigned long flags;
  295. bucket = get_hash_bucket(entry, &flags);
  296. hash_bucket_add(bucket, entry);
  297. put_hash_bucket(bucket, &flags);
  298. }
  299. static struct dma_debug_entry *__dma_entry_alloc(void)
  300. {
  301. struct dma_debug_entry *entry;
  302. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  303. list_del(&entry->list);
  304. memset(entry, 0, sizeof(*entry));
  305. num_free_entries -= 1;
  306. if (num_free_entries < min_free_entries)
  307. min_free_entries = num_free_entries;
  308. return entry;
  309. }
  310. /* struct dma_entry allocator
  311. *
  312. * The next two functions implement the allocator for
  313. * struct dma_debug_entries.
  314. */
  315. static struct dma_debug_entry *dma_entry_alloc(void)
  316. {
  317. struct dma_debug_entry *entry = NULL;
  318. unsigned long flags;
  319. spin_lock_irqsave(&free_entries_lock, flags);
  320. if (list_empty(&free_entries)) {
  321. pr_err("DMA-API: debugging out of memory - disabling\n");
  322. global_disable = true;
  323. goto out;
  324. }
  325. entry = __dma_entry_alloc();
  326. #ifdef CONFIG_STACKTRACE
  327. entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
  328. entry->stacktrace.entries = entry->st_entries;
  329. entry->stacktrace.skip = 2;
  330. save_stack_trace(&entry->stacktrace);
  331. #endif
  332. out:
  333. spin_unlock_irqrestore(&free_entries_lock, flags);
  334. return entry;
  335. }
  336. static void dma_entry_free(struct dma_debug_entry *entry)
  337. {
  338. unsigned long flags;
  339. /*
  340. * add to beginning of the list - this way the entries are
  341. * more likely cache hot when they are reallocated.
  342. */
  343. spin_lock_irqsave(&free_entries_lock, flags);
  344. list_add(&entry->list, &free_entries);
  345. num_free_entries += 1;
  346. spin_unlock_irqrestore(&free_entries_lock, flags);
  347. }
  348. int dma_debug_resize_entries(u32 num_entries)
  349. {
  350. int i, delta, ret = 0;
  351. unsigned long flags;
  352. struct dma_debug_entry *entry;
  353. LIST_HEAD(tmp);
  354. spin_lock_irqsave(&free_entries_lock, flags);
  355. if (nr_total_entries < num_entries) {
  356. delta = num_entries - nr_total_entries;
  357. spin_unlock_irqrestore(&free_entries_lock, flags);
  358. for (i = 0; i < delta; i++) {
  359. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  360. if (!entry)
  361. break;
  362. list_add_tail(&entry->list, &tmp);
  363. }
  364. spin_lock_irqsave(&free_entries_lock, flags);
  365. list_splice(&tmp, &free_entries);
  366. nr_total_entries += i;
  367. num_free_entries += i;
  368. } else {
  369. delta = nr_total_entries - num_entries;
  370. for (i = 0; i < delta && !list_empty(&free_entries); i++) {
  371. entry = __dma_entry_alloc();
  372. kfree(entry);
  373. }
  374. nr_total_entries -= i;
  375. }
  376. if (nr_total_entries != num_entries)
  377. ret = 1;
  378. spin_unlock_irqrestore(&free_entries_lock, flags);
  379. return ret;
  380. }
  381. EXPORT_SYMBOL(dma_debug_resize_entries);
  382. /*
  383. * DMA-API debugging init code
  384. *
  385. * The init code does two things:
  386. * 1. Initialize core data structures
  387. * 2. Preallocate a given number of dma_debug_entry structs
  388. */
  389. static int prealloc_memory(u32 num_entries)
  390. {
  391. struct dma_debug_entry *entry, *next_entry;
  392. int i;
  393. for (i = 0; i < num_entries; ++i) {
  394. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  395. if (!entry)
  396. goto out_err;
  397. list_add_tail(&entry->list, &free_entries);
  398. }
  399. num_free_entries = num_entries;
  400. min_free_entries = num_entries;
  401. pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
  402. return 0;
  403. out_err:
  404. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  405. list_del(&entry->list);
  406. kfree(entry);
  407. }
  408. return -ENOMEM;
  409. }
  410. static ssize_t filter_read(struct file *file, char __user *user_buf,
  411. size_t count, loff_t *ppos)
  412. {
  413. char buf[NAME_MAX_LEN + 1];
  414. unsigned long flags;
  415. int len;
  416. if (!current_driver_name[0])
  417. return 0;
  418. /*
  419. * We can't copy to userspace directly because current_driver_name can
  420. * only be read under the driver_name_lock with irqs disabled. So
  421. * create a temporary copy first.
  422. */
  423. read_lock_irqsave(&driver_name_lock, flags);
  424. len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
  425. read_unlock_irqrestore(&driver_name_lock, flags);
  426. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  427. }
  428. static ssize_t filter_write(struct file *file, const char __user *userbuf,
  429. size_t count, loff_t *ppos)
  430. {
  431. char buf[NAME_MAX_LEN];
  432. unsigned long flags;
  433. size_t len;
  434. int i;
  435. /*
  436. * We can't copy from userspace directly. Access to
  437. * current_driver_name is protected with a write_lock with irqs
  438. * disabled. Since copy_from_user can fault and may sleep we
  439. * need to copy to temporary buffer first
  440. */
  441. len = min(count, (size_t)(NAME_MAX_LEN - 1));
  442. if (copy_from_user(buf, userbuf, len))
  443. return -EFAULT;
  444. buf[len] = 0;
  445. write_lock_irqsave(&driver_name_lock, flags);
  446. /*
  447. * Now handle the string we got from userspace very carefully.
  448. * The rules are:
  449. * - only use the first token we got
  450. * - token delimiter is everything looking like a space
  451. * character (' ', '\n', '\t' ...)
  452. *
  453. */
  454. if (!isalnum(buf[0])) {
  455. /*
  456. * If the first character userspace gave us is not
  457. * alphanumerical then assume the filter should be
  458. * switched off.
  459. */
  460. if (current_driver_name[0])
  461. pr_info("DMA-API: switching off dma-debug driver filter\n");
  462. current_driver_name[0] = 0;
  463. current_driver = NULL;
  464. goto out_unlock;
  465. }
  466. /*
  467. * Now parse out the first token and use it as the name for the
  468. * driver to filter for.
  469. */
  470. for (i = 0; i < NAME_MAX_LEN; ++i) {
  471. current_driver_name[i] = buf[i];
  472. if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
  473. break;
  474. }
  475. current_driver_name[i] = 0;
  476. current_driver = NULL;
  477. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  478. current_driver_name);
  479. out_unlock:
  480. write_unlock_irqrestore(&driver_name_lock, flags);
  481. return count;
  482. }
  483. const struct file_operations filter_fops = {
  484. .read = filter_read,
  485. .write = filter_write,
  486. };
  487. static int dma_debug_fs_init(void)
  488. {
  489. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  490. if (!dma_debug_dent) {
  491. pr_err("DMA-API: can not create debugfs directory\n");
  492. return -ENOMEM;
  493. }
  494. global_disable_dent = debugfs_create_bool("disabled", 0444,
  495. dma_debug_dent,
  496. (u32 *)&global_disable);
  497. if (!global_disable_dent)
  498. goto out_err;
  499. error_count_dent = debugfs_create_u32("error_count", 0444,
  500. dma_debug_dent, &error_count);
  501. if (!error_count_dent)
  502. goto out_err;
  503. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  504. dma_debug_dent,
  505. &show_all_errors);
  506. if (!show_all_errors_dent)
  507. goto out_err;
  508. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  509. dma_debug_dent,
  510. &show_num_errors);
  511. if (!show_num_errors_dent)
  512. goto out_err;
  513. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  514. dma_debug_dent,
  515. &num_free_entries);
  516. if (!num_free_entries_dent)
  517. goto out_err;
  518. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  519. dma_debug_dent,
  520. &min_free_entries);
  521. if (!min_free_entries_dent)
  522. goto out_err;
  523. filter_dent = debugfs_create_file("driver_filter", 0644,
  524. dma_debug_dent, NULL, &filter_fops);
  525. if (!filter_dent)
  526. goto out_err;
  527. return 0;
  528. out_err:
  529. debugfs_remove_recursive(dma_debug_dent);
  530. return -ENOMEM;
  531. }
  532. static int device_dma_allocations(struct device *dev)
  533. {
  534. struct dma_debug_entry *entry;
  535. unsigned long flags;
  536. int count = 0, i;
  537. local_irq_save(flags);
  538. for (i = 0; i < HASH_SIZE; ++i) {
  539. spin_lock(&dma_entry_hash[i].lock);
  540. list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
  541. if (entry->dev == dev)
  542. count += 1;
  543. }
  544. spin_unlock(&dma_entry_hash[i].lock);
  545. }
  546. local_irq_restore(flags);
  547. return count;
  548. }
  549. static int dma_debug_device_change(struct notifier_block *nb,
  550. unsigned long action, void *data)
  551. {
  552. struct device *dev = data;
  553. int count;
  554. switch (action) {
  555. case BUS_NOTIFY_UNBOUND_DRIVER:
  556. count = device_dma_allocations(dev);
  557. if (count == 0)
  558. break;
  559. err_printk(dev, NULL, "DMA-API: device driver has pending "
  560. "DMA allocations while released from device "
  561. "[count=%d]\n", count);
  562. break;
  563. default:
  564. break;
  565. }
  566. return 0;
  567. }
  568. void dma_debug_add_bus(struct bus_type *bus)
  569. {
  570. struct notifier_block *nb;
  571. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  572. if (nb == NULL) {
  573. pr_err("dma_debug_add_bus: out of memory\n");
  574. return;
  575. }
  576. nb->notifier_call = dma_debug_device_change;
  577. bus_register_notifier(bus, nb);
  578. }
  579. /*
  580. * Let the architectures decide how many entries should be preallocated.
  581. */
  582. void dma_debug_init(u32 num_entries)
  583. {
  584. int i;
  585. if (global_disable)
  586. return;
  587. for (i = 0; i < HASH_SIZE; ++i) {
  588. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  589. dma_entry_hash[i].lock = SPIN_LOCK_UNLOCKED;
  590. }
  591. if (dma_debug_fs_init() != 0) {
  592. pr_err("DMA-API: error creating debugfs entries - disabling\n");
  593. global_disable = true;
  594. return;
  595. }
  596. if (req_entries)
  597. num_entries = req_entries;
  598. if (prealloc_memory(num_entries) != 0) {
  599. pr_err("DMA-API: debugging out of memory error - disabled\n");
  600. global_disable = true;
  601. return;
  602. }
  603. nr_total_entries = num_free_entries;
  604. pr_info("DMA-API: debugging enabled by kernel config\n");
  605. }
  606. static __init int dma_debug_cmdline(char *str)
  607. {
  608. if (!str)
  609. return -EINVAL;
  610. if (strncmp(str, "off", 3) == 0) {
  611. pr_info("DMA-API: debugging disabled on kernel command line\n");
  612. global_disable = true;
  613. }
  614. return 0;
  615. }
  616. static __init int dma_debug_entries_cmdline(char *str)
  617. {
  618. int res;
  619. if (!str)
  620. return -EINVAL;
  621. res = get_option(&str, &req_entries);
  622. if (!res)
  623. req_entries = 0;
  624. return 0;
  625. }
  626. __setup("dma_debug=", dma_debug_cmdline);
  627. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  628. static void check_unmap(struct dma_debug_entry *ref)
  629. {
  630. struct dma_debug_entry *entry;
  631. struct hash_bucket *bucket;
  632. unsigned long flags;
  633. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  634. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  635. "to free an invalid DMA memory address\n");
  636. return;
  637. }
  638. bucket = get_hash_bucket(ref, &flags);
  639. entry = hash_bucket_find(bucket, ref);
  640. if (!entry) {
  641. err_printk(ref->dev, NULL, "DMA-API: device driver tries "
  642. "to free DMA memory it has not allocated "
  643. "[device address=0x%016llx] [size=%llu bytes]\n",
  644. ref->dev_addr, ref->size);
  645. goto out;
  646. }
  647. if (ref->size != entry->size) {
  648. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  649. "DMA memory with different size "
  650. "[device address=0x%016llx] [map size=%llu bytes] "
  651. "[unmap size=%llu bytes]\n",
  652. ref->dev_addr, entry->size, ref->size);
  653. }
  654. if (ref->type != entry->type) {
  655. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  656. "DMA memory with wrong function "
  657. "[device address=0x%016llx] [size=%llu bytes] "
  658. "[mapped as %s] [unmapped as %s]\n",
  659. ref->dev_addr, ref->size,
  660. type2name[entry->type], type2name[ref->type]);
  661. } else if ((entry->type == dma_debug_coherent) &&
  662. (ref->paddr != entry->paddr)) {
  663. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  664. "DMA memory with different CPU address "
  665. "[device address=0x%016llx] [size=%llu bytes] "
  666. "[cpu alloc address=%p] [cpu free address=%p]",
  667. ref->dev_addr, ref->size,
  668. (void *)entry->paddr, (void *)ref->paddr);
  669. }
  670. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  671. ref->sg_call_ents != entry->sg_call_ents) {
  672. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  673. "DMA sg list with different entry count "
  674. "[map count=%d] [unmap count=%d]\n",
  675. entry->sg_call_ents, ref->sg_call_ents);
  676. }
  677. /*
  678. * This may be no bug in reality - but most implementations of the
  679. * DMA API don't handle this properly, so check for it here
  680. */
  681. if (ref->direction != entry->direction) {
  682. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  683. "DMA memory with different direction "
  684. "[device address=0x%016llx] [size=%llu bytes] "
  685. "[mapped with %s] [unmapped with %s]\n",
  686. ref->dev_addr, ref->size,
  687. dir2name[entry->direction],
  688. dir2name[ref->direction]);
  689. }
  690. hash_bucket_del(entry);
  691. dma_entry_free(entry);
  692. out:
  693. put_hash_bucket(bucket, &flags);
  694. }
  695. static void check_for_stack(struct device *dev, void *addr)
  696. {
  697. if (object_is_on_stack(addr))
  698. err_printk(dev, NULL, "DMA-API: device driver maps memory from"
  699. "stack [addr=%p]\n", addr);
  700. }
  701. static inline bool overlap(void *addr, u64 size, void *start, void *end)
  702. {
  703. void *addr2 = (char *)addr + size;
  704. return ((addr >= start && addr < end) ||
  705. (addr2 >= start && addr2 < end) ||
  706. ((addr < start) && (addr2 >= end)));
  707. }
  708. static void check_for_illegal_area(struct device *dev, void *addr, u64 size)
  709. {
  710. if (overlap(addr, size, _text, _etext) ||
  711. overlap(addr, size, __start_rodata, __end_rodata))
  712. err_printk(dev, NULL, "DMA-API: device driver maps "
  713. "memory from kernel text or rodata "
  714. "[addr=%p] [size=%llu]\n", addr, size);
  715. }
  716. static void check_sync(struct device *dev, dma_addr_t addr,
  717. u64 size, u64 offset, int direction, bool to_cpu)
  718. {
  719. struct dma_debug_entry ref = {
  720. .dev = dev,
  721. .dev_addr = addr,
  722. .size = size,
  723. .direction = direction,
  724. };
  725. struct dma_debug_entry *entry;
  726. struct hash_bucket *bucket;
  727. unsigned long flags;
  728. bucket = get_hash_bucket(&ref, &flags);
  729. entry = hash_bucket_find(bucket, &ref);
  730. if (!entry) {
  731. err_printk(dev, NULL, "DMA-API: device driver tries "
  732. "to sync DMA memory it has not allocated "
  733. "[device address=0x%016llx] [size=%llu bytes]\n",
  734. (unsigned long long)addr, size);
  735. goto out;
  736. }
  737. if ((offset + size) > entry->size) {
  738. err_printk(dev, entry, "DMA-API: device driver syncs"
  739. " DMA memory outside allocated range "
  740. "[device address=0x%016llx] "
  741. "[allocation size=%llu bytes] [sync offset=%llu] "
  742. "[sync size=%llu]\n", entry->dev_addr, entry->size,
  743. offset, size);
  744. }
  745. if (direction != entry->direction) {
  746. err_printk(dev, entry, "DMA-API: device driver syncs "
  747. "DMA memory with different direction "
  748. "[device address=0x%016llx] [size=%llu bytes] "
  749. "[mapped with %s] [synced with %s]\n",
  750. (unsigned long long)addr, entry->size,
  751. dir2name[entry->direction],
  752. dir2name[direction]);
  753. }
  754. if (entry->direction == DMA_BIDIRECTIONAL)
  755. goto out;
  756. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  757. !(direction == DMA_TO_DEVICE))
  758. err_printk(dev, entry, "DMA-API: device driver syncs "
  759. "device read-only DMA memory for cpu "
  760. "[device address=0x%016llx] [size=%llu bytes] "
  761. "[mapped with %s] [synced with %s]\n",
  762. (unsigned long long)addr, entry->size,
  763. dir2name[entry->direction],
  764. dir2name[direction]);
  765. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  766. !(direction == DMA_FROM_DEVICE))
  767. err_printk(dev, entry, "DMA-API: device driver syncs "
  768. "device write-only DMA memory to device "
  769. "[device address=0x%016llx] [size=%llu bytes] "
  770. "[mapped with %s] [synced with %s]\n",
  771. (unsigned long long)addr, entry->size,
  772. dir2name[entry->direction],
  773. dir2name[direction]);
  774. out:
  775. put_hash_bucket(bucket, &flags);
  776. }
  777. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  778. size_t size, int direction, dma_addr_t dma_addr,
  779. bool map_single)
  780. {
  781. struct dma_debug_entry *entry;
  782. if (unlikely(global_disable))
  783. return;
  784. if (unlikely(dma_mapping_error(dev, dma_addr)))
  785. return;
  786. entry = dma_entry_alloc();
  787. if (!entry)
  788. return;
  789. entry->dev = dev;
  790. entry->type = dma_debug_page;
  791. entry->paddr = page_to_phys(page) + offset;
  792. entry->dev_addr = dma_addr;
  793. entry->size = size;
  794. entry->direction = direction;
  795. if (map_single)
  796. entry->type = dma_debug_single;
  797. if (!PageHighMem(page)) {
  798. void *addr = ((char *)page_address(page)) + offset;
  799. check_for_stack(dev, addr);
  800. check_for_illegal_area(dev, addr, size);
  801. }
  802. add_dma_entry(entry);
  803. }
  804. EXPORT_SYMBOL(debug_dma_map_page);
  805. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  806. size_t size, int direction, bool map_single)
  807. {
  808. struct dma_debug_entry ref = {
  809. .type = dma_debug_page,
  810. .dev = dev,
  811. .dev_addr = addr,
  812. .size = size,
  813. .direction = direction,
  814. };
  815. if (unlikely(global_disable))
  816. return;
  817. if (map_single)
  818. ref.type = dma_debug_single;
  819. check_unmap(&ref);
  820. }
  821. EXPORT_SYMBOL(debug_dma_unmap_page);
  822. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  823. int nents, int mapped_ents, int direction)
  824. {
  825. struct dma_debug_entry *entry;
  826. struct scatterlist *s;
  827. int i;
  828. if (unlikely(global_disable))
  829. return;
  830. for_each_sg(sg, s, mapped_ents, i) {
  831. entry = dma_entry_alloc();
  832. if (!entry)
  833. return;
  834. entry->type = dma_debug_sg;
  835. entry->dev = dev;
  836. entry->paddr = sg_phys(s);
  837. entry->size = sg_dma_len(s);
  838. entry->dev_addr = sg_dma_address(s);
  839. entry->direction = direction;
  840. entry->sg_call_ents = nents;
  841. entry->sg_mapped_ents = mapped_ents;
  842. if (!PageHighMem(sg_page(s))) {
  843. check_for_stack(dev, sg_virt(s));
  844. check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
  845. }
  846. add_dma_entry(entry);
  847. }
  848. }
  849. EXPORT_SYMBOL(debug_dma_map_sg);
  850. static int get_nr_mapped_entries(struct device *dev, struct scatterlist *s)
  851. {
  852. struct dma_debug_entry *entry, ref;
  853. struct hash_bucket *bucket;
  854. unsigned long flags;
  855. int mapped_ents;
  856. ref.dev = dev;
  857. ref.dev_addr = sg_dma_address(s);
  858. ref.size = sg_dma_len(s),
  859. bucket = get_hash_bucket(&ref, &flags);
  860. entry = hash_bucket_find(bucket, &ref);
  861. mapped_ents = 0;
  862. if (entry)
  863. mapped_ents = entry->sg_mapped_ents;
  864. put_hash_bucket(bucket, &flags);
  865. return mapped_ents;
  866. }
  867. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  868. int nelems, int dir)
  869. {
  870. struct scatterlist *s;
  871. int mapped_ents = 0, i;
  872. if (unlikely(global_disable))
  873. return;
  874. for_each_sg(sglist, s, nelems, i) {
  875. struct dma_debug_entry ref = {
  876. .type = dma_debug_sg,
  877. .dev = dev,
  878. .paddr = sg_phys(s),
  879. .dev_addr = sg_dma_address(s),
  880. .size = sg_dma_len(s),
  881. .direction = dir,
  882. .sg_call_ents = 0,
  883. };
  884. if (mapped_ents && i >= mapped_ents)
  885. break;
  886. if (!i) {
  887. ref.sg_call_ents = nelems;
  888. mapped_ents = get_nr_mapped_entries(dev, s);
  889. }
  890. check_unmap(&ref);
  891. }
  892. }
  893. EXPORT_SYMBOL(debug_dma_unmap_sg);
  894. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  895. dma_addr_t dma_addr, void *virt)
  896. {
  897. struct dma_debug_entry *entry;
  898. if (unlikely(global_disable))
  899. return;
  900. if (unlikely(virt == NULL))
  901. return;
  902. entry = dma_entry_alloc();
  903. if (!entry)
  904. return;
  905. entry->type = dma_debug_coherent;
  906. entry->dev = dev;
  907. entry->paddr = virt_to_phys(virt);
  908. entry->size = size;
  909. entry->dev_addr = dma_addr;
  910. entry->direction = DMA_BIDIRECTIONAL;
  911. add_dma_entry(entry);
  912. }
  913. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  914. void debug_dma_free_coherent(struct device *dev, size_t size,
  915. void *virt, dma_addr_t addr)
  916. {
  917. struct dma_debug_entry ref = {
  918. .type = dma_debug_coherent,
  919. .dev = dev,
  920. .paddr = virt_to_phys(virt),
  921. .dev_addr = addr,
  922. .size = size,
  923. .direction = DMA_BIDIRECTIONAL,
  924. };
  925. if (unlikely(global_disable))
  926. return;
  927. check_unmap(&ref);
  928. }
  929. EXPORT_SYMBOL(debug_dma_free_coherent);
  930. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  931. size_t size, int direction)
  932. {
  933. if (unlikely(global_disable))
  934. return;
  935. check_sync(dev, dma_handle, size, 0, direction, true);
  936. }
  937. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  938. void debug_dma_sync_single_for_device(struct device *dev,
  939. dma_addr_t dma_handle, size_t size,
  940. int direction)
  941. {
  942. if (unlikely(global_disable))
  943. return;
  944. check_sync(dev, dma_handle, size, 0, direction, false);
  945. }
  946. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  947. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  948. dma_addr_t dma_handle,
  949. unsigned long offset, size_t size,
  950. int direction)
  951. {
  952. if (unlikely(global_disable))
  953. return;
  954. check_sync(dev, dma_handle, size, offset, direction, true);
  955. }
  956. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  957. void debug_dma_sync_single_range_for_device(struct device *dev,
  958. dma_addr_t dma_handle,
  959. unsigned long offset,
  960. size_t size, int direction)
  961. {
  962. if (unlikely(global_disable))
  963. return;
  964. check_sync(dev, dma_handle, size, offset, direction, false);
  965. }
  966. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  967. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  968. int nelems, int direction)
  969. {
  970. struct scatterlist *s;
  971. int mapped_ents = 0, i;
  972. if (unlikely(global_disable))
  973. return;
  974. for_each_sg(sg, s, nelems, i) {
  975. if (!i)
  976. mapped_ents = get_nr_mapped_entries(dev, s);
  977. if (i >= mapped_ents)
  978. break;
  979. check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
  980. direction, true);
  981. }
  982. }
  983. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  984. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  985. int nelems, int direction)
  986. {
  987. struct scatterlist *s;
  988. int mapped_ents = 0, i;
  989. if (unlikely(global_disable))
  990. return;
  991. for_each_sg(sg, s, nelems, i) {
  992. if (!i)
  993. mapped_ents = get_nr_mapped_entries(dev, s);
  994. if (i >= mapped_ents)
  995. break;
  996. check_sync(dev, sg_dma_address(s), sg_dma_len(s), 0,
  997. direction, false);
  998. }
  999. }
  1000. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
  1001. static int __init dma_debug_driver_setup(char *str)
  1002. {
  1003. int i;
  1004. for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
  1005. current_driver_name[i] = *str;
  1006. if (*str == 0)
  1007. break;
  1008. }
  1009. if (current_driver_name[0])
  1010. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  1011. current_driver_name);
  1012. return 1;
  1013. }
  1014. __setup("dma_debug_driver=", dma_debug_driver_setup);