dma-debug.c 33 KB

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