dma-debug.c 32 KB

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