dma-debug.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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. enum map_err_types {
  44. MAP_ERR_CHECK_NOT_APPLICABLE,
  45. MAP_ERR_NOT_CHECKED,
  46. MAP_ERR_CHECKED,
  47. };
  48. #define DMA_DEBUG_STACKTRACE_ENTRIES 5
  49. /**
  50. * struct dma_debug_entry - track a dma_map* or dma_alloc_coherent mapping
  51. * @list: node on pre-allocated free_entries list
  52. * @dev: 'dev' argument to dma_map_{page|single|sg} or dma_alloc_coherent
  53. * @type: single, page, sg, coherent
  54. * @pfn: page frame of the start address
  55. * @offset: offset of mapping relative to pfn
  56. * @size: length of the mapping
  57. * @direction: enum dma_data_direction
  58. * @sg_call_ents: 'nents' from dma_map_sg
  59. * @sg_mapped_ents: 'mapped_ents' from dma_map_sg
  60. * @map_err_type: track whether dma_mapping_error() was checked
  61. * @stacktrace: support backtraces when a violation is detected
  62. */
  63. struct dma_debug_entry {
  64. struct list_head list;
  65. struct device *dev;
  66. int type;
  67. unsigned long pfn;
  68. size_t offset;
  69. u64 dev_addr;
  70. u64 size;
  71. int direction;
  72. int sg_call_ents;
  73. int sg_mapped_ents;
  74. enum map_err_types map_err_type;
  75. #ifdef CONFIG_STACKTRACE
  76. struct stack_trace stacktrace;
  77. unsigned long st_entries[DMA_DEBUG_STACKTRACE_ENTRIES];
  78. #endif
  79. };
  80. typedef bool (*match_fn)(struct dma_debug_entry *, struct dma_debug_entry *);
  81. struct hash_bucket {
  82. struct list_head list;
  83. spinlock_t lock;
  84. } ____cacheline_aligned_in_smp;
  85. /* Hash list to save the allocated dma addresses */
  86. static struct hash_bucket dma_entry_hash[HASH_SIZE];
  87. /* List of pre-allocated dma_debug_entry's */
  88. static LIST_HEAD(free_entries);
  89. /* Lock for the list above */
  90. static DEFINE_SPINLOCK(free_entries_lock);
  91. /* Global disable flag - will be set in case of an error */
  92. static u32 global_disable __read_mostly;
  93. /* Early initialization disable flag, set at the end of dma_debug_init */
  94. static bool dma_debug_initialized __read_mostly;
  95. static inline bool dma_debug_disabled(void)
  96. {
  97. return global_disable || !dma_debug_initialized;
  98. }
  99. /* Global error count */
  100. static u32 error_count;
  101. /* Global error show enable*/
  102. static u32 show_all_errors __read_mostly;
  103. /* Number of errors to show */
  104. static u32 show_num_errors = 1;
  105. static u32 num_free_entries;
  106. static u32 min_free_entries;
  107. static u32 nr_total_entries;
  108. /* number of preallocated entries requested by kernel cmdline */
  109. static u32 req_entries;
  110. /* debugfs dentry's for the stuff above */
  111. static struct dentry *dma_debug_dent __read_mostly;
  112. static struct dentry *global_disable_dent __read_mostly;
  113. static struct dentry *error_count_dent __read_mostly;
  114. static struct dentry *show_all_errors_dent __read_mostly;
  115. static struct dentry *show_num_errors_dent __read_mostly;
  116. static struct dentry *num_free_entries_dent __read_mostly;
  117. static struct dentry *min_free_entries_dent __read_mostly;
  118. static struct dentry *filter_dent __read_mostly;
  119. /* per-driver filter related state */
  120. #define NAME_MAX_LEN 64
  121. static char current_driver_name[NAME_MAX_LEN] __read_mostly;
  122. static struct device_driver *current_driver __read_mostly;
  123. static DEFINE_RWLOCK(driver_name_lock);
  124. static const char *const maperr2str[] = {
  125. [MAP_ERR_CHECK_NOT_APPLICABLE] = "dma map error check not applicable",
  126. [MAP_ERR_NOT_CHECKED] = "dma map error not checked",
  127. [MAP_ERR_CHECKED] = "dma map error checked",
  128. };
  129. static const char *type2name[4] = { "single", "page",
  130. "scather-gather", "coherent" };
  131. static const char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
  132. "DMA_FROM_DEVICE", "DMA_NONE" };
  133. /*
  134. * The access to some variables in this macro is racy. We can't use atomic_t
  135. * here because all these variables are exported to debugfs. Some of them even
  136. * writeable. This is also the reason why a lock won't help much. But anyway,
  137. * the races are no big deal. Here is why:
  138. *
  139. * error_count: the addition is racy, but the worst thing that can happen is
  140. * that we don't count some errors
  141. * show_num_errors: the subtraction is racy. Also no big deal because in
  142. * worst case this will result in one warning more in the
  143. * system log than the user configured. This variable is
  144. * writeable via debugfs.
  145. */
  146. static inline void dump_entry_trace(struct dma_debug_entry *entry)
  147. {
  148. #ifdef CONFIG_STACKTRACE
  149. if (entry) {
  150. pr_warning("Mapped at:\n");
  151. print_stack_trace(&entry->stacktrace, 0);
  152. }
  153. #endif
  154. }
  155. static bool driver_filter(struct device *dev)
  156. {
  157. struct device_driver *drv;
  158. unsigned long flags;
  159. bool ret;
  160. /* driver filter off */
  161. if (likely(!current_driver_name[0]))
  162. return true;
  163. /* driver filter on and initialized */
  164. if (current_driver && dev && dev->driver == current_driver)
  165. return true;
  166. /* driver filter on, but we can't filter on a NULL device... */
  167. if (!dev)
  168. return false;
  169. if (current_driver || !current_driver_name[0])
  170. return false;
  171. /* driver filter on but not yet initialized */
  172. drv = dev->driver;
  173. if (!drv)
  174. return false;
  175. /* lock to protect against change of current_driver_name */
  176. read_lock_irqsave(&driver_name_lock, flags);
  177. ret = false;
  178. if (drv->name &&
  179. strncmp(current_driver_name, drv->name, NAME_MAX_LEN - 1) == 0) {
  180. current_driver = drv;
  181. ret = true;
  182. }
  183. read_unlock_irqrestore(&driver_name_lock, flags);
  184. return ret;
  185. }
  186. #define err_printk(dev, entry, format, arg...) do { \
  187. error_count += 1; \
  188. if (driver_filter(dev) && \
  189. (show_all_errors || show_num_errors > 0)) { \
  190. WARN(1, "%s %s: " format, \
  191. dev ? dev_driver_string(dev) : "NULL", \
  192. dev ? dev_name(dev) : "NULL", ## arg); \
  193. dump_entry_trace(entry); \
  194. } \
  195. if (!show_all_errors && show_num_errors > 0) \
  196. show_num_errors -= 1; \
  197. } while (0);
  198. /*
  199. * Hash related functions
  200. *
  201. * Every DMA-API request is saved into a struct dma_debug_entry. To
  202. * have quick access to these structs they are stored into a hash.
  203. */
  204. static int hash_fn(struct dma_debug_entry *entry)
  205. {
  206. /*
  207. * Hash function is based on the dma address.
  208. * We use bits 20-27 here as the index into the hash
  209. */
  210. return (entry->dev_addr >> HASH_FN_SHIFT) & HASH_FN_MASK;
  211. }
  212. /*
  213. * Request exclusive access to a hash bucket for a given dma_debug_entry.
  214. */
  215. static struct hash_bucket *get_hash_bucket(struct dma_debug_entry *entry,
  216. unsigned long *flags)
  217. {
  218. int idx = hash_fn(entry);
  219. unsigned long __flags;
  220. spin_lock_irqsave(&dma_entry_hash[idx].lock, __flags);
  221. *flags = __flags;
  222. return &dma_entry_hash[idx];
  223. }
  224. /*
  225. * Give up exclusive access to the hash bucket
  226. */
  227. static void put_hash_bucket(struct hash_bucket *bucket,
  228. unsigned long *flags)
  229. {
  230. unsigned long __flags = *flags;
  231. spin_unlock_irqrestore(&bucket->lock, __flags);
  232. }
  233. static bool exact_match(struct dma_debug_entry *a, struct dma_debug_entry *b)
  234. {
  235. return ((a->dev_addr == b->dev_addr) &&
  236. (a->dev == b->dev)) ? true : false;
  237. }
  238. static bool containing_match(struct dma_debug_entry *a,
  239. struct dma_debug_entry *b)
  240. {
  241. if (a->dev != b->dev)
  242. return false;
  243. if ((b->dev_addr <= a->dev_addr) &&
  244. ((b->dev_addr + b->size) >= (a->dev_addr + a->size)))
  245. return true;
  246. return false;
  247. }
  248. /*
  249. * Search a given entry in the hash bucket list
  250. */
  251. static struct dma_debug_entry *__hash_bucket_find(struct hash_bucket *bucket,
  252. struct dma_debug_entry *ref,
  253. match_fn match)
  254. {
  255. struct dma_debug_entry *entry, *ret = NULL;
  256. int matches = 0, match_lvl, last_lvl = -1;
  257. list_for_each_entry(entry, &bucket->list, list) {
  258. if (!match(ref, entry))
  259. continue;
  260. /*
  261. * Some drivers map the same physical address multiple
  262. * times. Without a hardware IOMMU this results in the
  263. * same device addresses being put into the dma-debug
  264. * hash multiple times too. This can result in false
  265. * positives being reported. Therefore we implement a
  266. * best-fit algorithm here which returns the entry from
  267. * the hash which fits best to the reference value
  268. * instead of the first-fit.
  269. */
  270. matches += 1;
  271. match_lvl = 0;
  272. entry->size == ref->size ? ++match_lvl : 0;
  273. entry->type == ref->type ? ++match_lvl : 0;
  274. entry->direction == ref->direction ? ++match_lvl : 0;
  275. entry->sg_call_ents == ref->sg_call_ents ? ++match_lvl : 0;
  276. if (match_lvl == 4) {
  277. /* perfect-fit - return the result */
  278. return entry;
  279. } else if (match_lvl > last_lvl) {
  280. /*
  281. * We found an entry that fits better then the
  282. * previous one or it is the 1st match.
  283. */
  284. last_lvl = match_lvl;
  285. ret = entry;
  286. }
  287. }
  288. /*
  289. * If we have multiple matches but no perfect-fit, just return
  290. * NULL.
  291. */
  292. ret = (matches == 1) ? ret : NULL;
  293. return ret;
  294. }
  295. static struct dma_debug_entry *bucket_find_exact(struct hash_bucket *bucket,
  296. struct dma_debug_entry *ref)
  297. {
  298. return __hash_bucket_find(bucket, ref, exact_match);
  299. }
  300. static struct dma_debug_entry *bucket_find_contain(struct hash_bucket **bucket,
  301. struct dma_debug_entry *ref,
  302. unsigned long *flags)
  303. {
  304. unsigned int max_range = dma_get_max_seg_size(ref->dev);
  305. struct dma_debug_entry *entry, index = *ref;
  306. unsigned int range = 0;
  307. while (range <= max_range) {
  308. entry = __hash_bucket_find(*bucket, ref, containing_match);
  309. if (entry)
  310. return entry;
  311. /*
  312. * Nothing found, go back a hash bucket
  313. */
  314. put_hash_bucket(*bucket, flags);
  315. range += (1 << HASH_FN_SHIFT);
  316. index.dev_addr -= (1 << HASH_FN_SHIFT);
  317. *bucket = get_hash_bucket(&index, flags);
  318. }
  319. return NULL;
  320. }
  321. /*
  322. * Add an entry to a hash bucket
  323. */
  324. static void hash_bucket_add(struct hash_bucket *bucket,
  325. struct dma_debug_entry *entry)
  326. {
  327. list_add_tail(&entry->list, &bucket->list);
  328. }
  329. /*
  330. * Remove entry from a hash bucket list
  331. */
  332. static void hash_bucket_del(struct dma_debug_entry *entry)
  333. {
  334. list_del(&entry->list);
  335. }
  336. static unsigned long long phys_addr(struct dma_debug_entry *entry)
  337. {
  338. return page_to_phys(pfn_to_page(entry->pfn)) + entry->offset;
  339. }
  340. /*
  341. * Dump mapping entries for debugging purposes
  342. */
  343. void debug_dma_dump_mappings(struct device *dev)
  344. {
  345. int idx;
  346. for (idx = 0; idx < HASH_SIZE; idx++) {
  347. struct hash_bucket *bucket = &dma_entry_hash[idx];
  348. struct dma_debug_entry *entry;
  349. unsigned long flags;
  350. spin_lock_irqsave(&bucket->lock, flags);
  351. list_for_each_entry(entry, &bucket->list, list) {
  352. if (!dev || dev == entry->dev) {
  353. dev_info(entry->dev,
  354. "%s idx %d P=%Lx N=%lx D=%Lx L=%Lx %s %s\n",
  355. type2name[entry->type], idx,
  356. phys_addr(entry), entry->pfn,
  357. entry->dev_addr, entry->size,
  358. dir2name[entry->direction],
  359. maperr2str[entry->map_err_type]);
  360. }
  361. }
  362. spin_unlock_irqrestore(&bucket->lock, flags);
  363. }
  364. }
  365. EXPORT_SYMBOL(debug_dma_dump_mappings);
  366. /*
  367. * For each mapping (initial cacheline in the case of
  368. * dma_alloc_coherent/dma_map_page, initial cacheline in each page of a
  369. * scatterlist, or the cacheline specified in dma_map_single) insert
  370. * into this tree using the cacheline as the key. At
  371. * dma_unmap_{single|sg|page} or dma_free_coherent delete the entry. If
  372. * the entry already exists at insertion time add a tag as a reference
  373. * count for the overlapping mappings. For now, the overlap tracking
  374. * just ensures that 'unmaps' balance 'maps' before marking the
  375. * cacheline idle, but we should also be flagging overlaps as an API
  376. * violation.
  377. *
  378. * Memory usage is mostly constrained by the maximum number of available
  379. * dma-debug entries in that we need a free dma_debug_entry before
  380. * inserting into the tree. In the case of dma_map_page and
  381. * dma_alloc_coherent there is only one dma_debug_entry and one
  382. * dma_active_cacheline entry to track per event. dma_map_sg(), on the
  383. * other hand, consumes a single dma_debug_entry, but inserts 'nents'
  384. * entries into the tree.
  385. *
  386. * At any time debug_dma_assert_idle() can be called to trigger a
  387. * warning if any cachelines in the given page are in the active set.
  388. */
  389. static RADIX_TREE(dma_active_cacheline, GFP_NOWAIT);
  390. static DEFINE_SPINLOCK(radix_lock);
  391. #define ACTIVE_CACHELINE_MAX_OVERLAP ((1 << RADIX_TREE_MAX_TAGS) - 1)
  392. #define CACHELINE_PER_PAGE_SHIFT (PAGE_SHIFT - L1_CACHE_SHIFT)
  393. #define CACHELINES_PER_PAGE (1 << CACHELINE_PER_PAGE_SHIFT)
  394. static phys_addr_t to_cacheline_number(struct dma_debug_entry *entry)
  395. {
  396. return (entry->pfn << CACHELINE_PER_PAGE_SHIFT) +
  397. (entry->offset >> L1_CACHE_SHIFT);
  398. }
  399. static int active_cacheline_read_overlap(phys_addr_t cln)
  400. {
  401. int overlap = 0, i;
  402. for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
  403. if (radix_tree_tag_get(&dma_active_cacheline, cln, i))
  404. overlap |= 1 << i;
  405. return overlap;
  406. }
  407. static int active_cacheline_set_overlap(phys_addr_t cln, int overlap)
  408. {
  409. int i;
  410. if (overlap > ACTIVE_CACHELINE_MAX_OVERLAP || overlap < 0)
  411. return overlap;
  412. for (i = RADIX_TREE_MAX_TAGS - 1; i >= 0; i--)
  413. if (overlap & 1 << i)
  414. radix_tree_tag_set(&dma_active_cacheline, cln, i);
  415. else
  416. radix_tree_tag_clear(&dma_active_cacheline, cln, i);
  417. return overlap;
  418. }
  419. static void active_cacheline_inc_overlap(phys_addr_t cln)
  420. {
  421. int overlap = active_cacheline_read_overlap(cln);
  422. overlap = active_cacheline_set_overlap(cln, ++overlap);
  423. /* If we overflowed the overlap counter then we're potentially
  424. * leaking dma-mappings. Otherwise, if maps and unmaps are
  425. * balanced then this overflow may cause false negatives in
  426. * debug_dma_assert_idle() as the cacheline may be marked idle
  427. * prematurely.
  428. */
  429. WARN_ONCE(overlap > ACTIVE_CACHELINE_MAX_OVERLAP,
  430. "DMA-API: exceeded %d overlapping mappings of cacheline %pa\n",
  431. ACTIVE_CACHELINE_MAX_OVERLAP, &cln);
  432. }
  433. static int active_cacheline_dec_overlap(phys_addr_t cln)
  434. {
  435. int overlap = active_cacheline_read_overlap(cln);
  436. return active_cacheline_set_overlap(cln, --overlap);
  437. }
  438. static int active_cacheline_insert(struct dma_debug_entry *entry)
  439. {
  440. phys_addr_t cln = to_cacheline_number(entry);
  441. unsigned long flags;
  442. int rc;
  443. /* If the device is not writing memory then we don't have any
  444. * concerns about the cpu consuming stale data. This mitigates
  445. * legitimate usages of overlapping mappings.
  446. */
  447. if (entry->direction == DMA_TO_DEVICE)
  448. return 0;
  449. spin_lock_irqsave(&radix_lock, flags);
  450. rc = radix_tree_insert(&dma_active_cacheline, cln, entry);
  451. if (rc == -EEXIST)
  452. active_cacheline_inc_overlap(cln);
  453. spin_unlock_irqrestore(&radix_lock, flags);
  454. return rc;
  455. }
  456. static void active_cacheline_remove(struct dma_debug_entry *entry)
  457. {
  458. phys_addr_t cln = to_cacheline_number(entry);
  459. unsigned long flags;
  460. /* ...mirror the insert case */
  461. if (entry->direction == DMA_TO_DEVICE)
  462. return;
  463. spin_lock_irqsave(&radix_lock, flags);
  464. /* since we are counting overlaps the final put of the
  465. * cacheline will occur when the overlap count is 0.
  466. * active_cacheline_dec_overlap() returns -1 in that case
  467. */
  468. if (active_cacheline_dec_overlap(cln) < 0)
  469. radix_tree_delete(&dma_active_cacheline, cln);
  470. spin_unlock_irqrestore(&radix_lock, flags);
  471. }
  472. /**
  473. * debug_dma_assert_idle() - assert that a page is not undergoing dma
  474. * @page: page to lookup in the dma_active_cacheline tree
  475. *
  476. * Place a call to this routine in cases where the cpu touching the page
  477. * before the dma completes (page is dma_unmapped) will lead to data
  478. * corruption.
  479. */
  480. void debug_dma_assert_idle(struct page *page)
  481. {
  482. static struct dma_debug_entry *ents[CACHELINES_PER_PAGE];
  483. struct dma_debug_entry *entry = NULL;
  484. void **results = (void **) &ents;
  485. unsigned int nents, i;
  486. unsigned long flags;
  487. phys_addr_t cln;
  488. if (!page)
  489. return;
  490. cln = (phys_addr_t) page_to_pfn(page) << CACHELINE_PER_PAGE_SHIFT;
  491. spin_lock_irqsave(&radix_lock, flags);
  492. nents = radix_tree_gang_lookup(&dma_active_cacheline, results, cln,
  493. CACHELINES_PER_PAGE);
  494. for (i = 0; i < nents; i++) {
  495. phys_addr_t ent_cln = to_cacheline_number(ents[i]);
  496. if (ent_cln == cln) {
  497. entry = ents[i];
  498. break;
  499. } else if (ent_cln >= cln + CACHELINES_PER_PAGE)
  500. break;
  501. }
  502. spin_unlock_irqrestore(&radix_lock, flags);
  503. if (!entry)
  504. return;
  505. cln = to_cacheline_number(entry);
  506. err_printk(entry->dev, entry,
  507. "DMA-API: cpu touching an active dma mapped cacheline [cln=%pa]\n",
  508. &cln);
  509. }
  510. /*
  511. * Wrapper function for adding an entry to the hash.
  512. * This function takes care of locking itself.
  513. */
  514. static void add_dma_entry(struct dma_debug_entry *entry)
  515. {
  516. struct hash_bucket *bucket;
  517. unsigned long flags;
  518. int rc;
  519. bucket = get_hash_bucket(entry, &flags);
  520. hash_bucket_add(bucket, entry);
  521. put_hash_bucket(bucket, &flags);
  522. rc = active_cacheline_insert(entry);
  523. if (rc == -ENOMEM) {
  524. pr_err("DMA-API: cacheline tracking ENOMEM, dma-debug disabled\n");
  525. global_disable = true;
  526. }
  527. /* TODO: report -EEXIST errors here as overlapping mappings are
  528. * not supported by the DMA API
  529. */
  530. }
  531. static struct dma_debug_entry *__dma_entry_alloc(void)
  532. {
  533. struct dma_debug_entry *entry;
  534. entry = list_entry(free_entries.next, struct dma_debug_entry, list);
  535. list_del(&entry->list);
  536. memset(entry, 0, sizeof(*entry));
  537. num_free_entries -= 1;
  538. if (num_free_entries < min_free_entries)
  539. min_free_entries = num_free_entries;
  540. return entry;
  541. }
  542. /* struct dma_entry allocator
  543. *
  544. * The next two functions implement the allocator for
  545. * struct dma_debug_entries.
  546. */
  547. static struct dma_debug_entry *dma_entry_alloc(void)
  548. {
  549. struct dma_debug_entry *entry;
  550. unsigned long flags;
  551. spin_lock_irqsave(&free_entries_lock, flags);
  552. if (list_empty(&free_entries)) {
  553. pr_err("DMA-API: debugging out of memory - disabling\n");
  554. global_disable = true;
  555. spin_unlock_irqrestore(&free_entries_lock, flags);
  556. return NULL;
  557. }
  558. entry = __dma_entry_alloc();
  559. spin_unlock_irqrestore(&free_entries_lock, flags);
  560. #ifdef CONFIG_STACKTRACE
  561. entry->stacktrace.max_entries = DMA_DEBUG_STACKTRACE_ENTRIES;
  562. entry->stacktrace.entries = entry->st_entries;
  563. entry->stacktrace.skip = 2;
  564. save_stack_trace(&entry->stacktrace);
  565. #endif
  566. return entry;
  567. }
  568. static void dma_entry_free(struct dma_debug_entry *entry)
  569. {
  570. unsigned long flags;
  571. active_cacheline_remove(entry);
  572. /*
  573. * add to beginning of the list - this way the entries are
  574. * more likely cache hot when they are reallocated.
  575. */
  576. spin_lock_irqsave(&free_entries_lock, flags);
  577. list_add(&entry->list, &free_entries);
  578. num_free_entries += 1;
  579. spin_unlock_irqrestore(&free_entries_lock, flags);
  580. }
  581. int dma_debug_resize_entries(u32 num_entries)
  582. {
  583. int i, delta, ret = 0;
  584. unsigned long flags;
  585. struct dma_debug_entry *entry;
  586. LIST_HEAD(tmp);
  587. spin_lock_irqsave(&free_entries_lock, flags);
  588. if (nr_total_entries < num_entries) {
  589. delta = num_entries - nr_total_entries;
  590. spin_unlock_irqrestore(&free_entries_lock, flags);
  591. for (i = 0; i < delta; i++) {
  592. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  593. if (!entry)
  594. break;
  595. list_add_tail(&entry->list, &tmp);
  596. }
  597. spin_lock_irqsave(&free_entries_lock, flags);
  598. list_splice(&tmp, &free_entries);
  599. nr_total_entries += i;
  600. num_free_entries += i;
  601. } else {
  602. delta = nr_total_entries - num_entries;
  603. for (i = 0; i < delta && !list_empty(&free_entries); i++) {
  604. entry = __dma_entry_alloc();
  605. kfree(entry);
  606. }
  607. nr_total_entries -= i;
  608. }
  609. if (nr_total_entries != num_entries)
  610. ret = 1;
  611. spin_unlock_irqrestore(&free_entries_lock, flags);
  612. return ret;
  613. }
  614. EXPORT_SYMBOL(dma_debug_resize_entries);
  615. /*
  616. * DMA-API debugging init code
  617. *
  618. * The init code does two things:
  619. * 1. Initialize core data structures
  620. * 2. Preallocate a given number of dma_debug_entry structs
  621. */
  622. static int prealloc_memory(u32 num_entries)
  623. {
  624. struct dma_debug_entry *entry, *next_entry;
  625. int i;
  626. for (i = 0; i < num_entries; ++i) {
  627. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  628. if (!entry)
  629. goto out_err;
  630. list_add_tail(&entry->list, &free_entries);
  631. }
  632. num_free_entries = num_entries;
  633. min_free_entries = num_entries;
  634. pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
  635. return 0;
  636. out_err:
  637. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  638. list_del(&entry->list);
  639. kfree(entry);
  640. }
  641. return -ENOMEM;
  642. }
  643. static ssize_t filter_read(struct file *file, char __user *user_buf,
  644. size_t count, loff_t *ppos)
  645. {
  646. char buf[NAME_MAX_LEN + 1];
  647. unsigned long flags;
  648. int len;
  649. if (!current_driver_name[0])
  650. return 0;
  651. /*
  652. * We can't copy to userspace directly because current_driver_name can
  653. * only be read under the driver_name_lock with irqs disabled. So
  654. * create a temporary copy first.
  655. */
  656. read_lock_irqsave(&driver_name_lock, flags);
  657. len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
  658. read_unlock_irqrestore(&driver_name_lock, flags);
  659. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  660. }
  661. static ssize_t filter_write(struct file *file, const char __user *userbuf,
  662. size_t count, loff_t *ppos)
  663. {
  664. char buf[NAME_MAX_LEN];
  665. unsigned long flags;
  666. size_t len;
  667. int i;
  668. /*
  669. * We can't copy from userspace directly. Access to
  670. * current_driver_name is protected with a write_lock with irqs
  671. * disabled. Since copy_from_user can fault and may sleep we
  672. * need to copy to temporary buffer first
  673. */
  674. len = min(count, (size_t)(NAME_MAX_LEN - 1));
  675. if (copy_from_user(buf, userbuf, len))
  676. return -EFAULT;
  677. buf[len] = 0;
  678. write_lock_irqsave(&driver_name_lock, flags);
  679. /*
  680. * Now handle the string we got from userspace very carefully.
  681. * The rules are:
  682. * - only use the first token we got
  683. * - token delimiter is everything looking like a space
  684. * character (' ', '\n', '\t' ...)
  685. *
  686. */
  687. if (!isalnum(buf[0])) {
  688. /*
  689. * If the first character userspace gave us is not
  690. * alphanumerical then assume the filter should be
  691. * switched off.
  692. */
  693. if (current_driver_name[0])
  694. pr_info("DMA-API: switching off dma-debug driver filter\n");
  695. current_driver_name[0] = 0;
  696. current_driver = NULL;
  697. goto out_unlock;
  698. }
  699. /*
  700. * Now parse out the first token and use it as the name for the
  701. * driver to filter for.
  702. */
  703. for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
  704. current_driver_name[i] = buf[i];
  705. if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
  706. break;
  707. }
  708. current_driver_name[i] = 0;
  709. current_driver = NULL;
  710. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  711. current_driver_name);
  712. out_unlock:
  713. write_unlock_irqrestore(&driver_name_lock, flags);
  714. return count;
  715. }
  716. static const struct file_operations filter_fops = {
  717. .read = filter_read,
  718. .write = filter_write,
  719. .llseek = default_llseek,
  720. };
  721. static int dma_debug_fs_init(void)
  722. {
  723. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  724. if (!dma_debug_dent) {
  725. pr_err("DMA-API: can not create debugfs directory\n");
  726. return -ENOMEM;
  727. }
  728. global_disable_dent = debugfs_create_bool("disabled", 0444,
  729. dma_debug_dent,
  730. &global_disable);
  731. if (!global_disable_dent)
  732. goto out_err;
  733. error_count_dent = debugfs_create_u32("error_count", 0444,
  734. dma_debug_dent, &error_count);
  735. if (!error_count_dent)
  736. goto out_err;
  737. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  738. dma_debug_dent,
  739. &show_all_errors);
  740. if (!show_all_errors_dent)
  741. goto out_err;
  742. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  743. dma_debug_dent,
  744. &show_num_errors);
  745. if (!show_num_errors_dent)
  746. goto out_err;
  747. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  748. dma_debug_dent,
  749. &num_free_entries);
  750. if (!num_free_entries_dent)
  751. goto out_err;
  752. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  753. dma_debug_dent,
  754. &min_free_entries);
  755. if (!min_free_entries_dent)
  756. goto out_err;
  757. filter_dent = debugfs_create_file("driver_filter", 0644,
  758. dma_debug_dent, NULL, &filter_fops);
  759. if (!filter_dent)
  760. goto out_err;
  761. return 0;
  762. out_err:
  763. debugfs_remove_recursive(dma_debug_dent);
  764. return -ENOMEM;
  765. }
  766. static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
  767. {
  768. struct dma_debug_entry *entry;
  769. unsigned long flags;
  770. int count = 0, i;
  771. local_irq_save(flags);
  772. for (i = 0; i < HASH_SIZE; ++i) {
  773. spin_lock(&dma_entry_hash[i].lock);
  774. list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
  775. if (entry->dev == dev) {
  776. count += 1;
  777. *out_entry = entry;
  778. }
  779. }
  780. spin_unlock(&dma_entry_hash[i].lock);
  781. }
  782. local_irq_restore(flags);
  783. return count;
  784. }
  785. static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
  786. {
  787. struct device *dev = data;
  788. struct dma_debug_entry *uninitialized_var(entry);
  789. int count;
  790. if (dma_debug_disabled())
  791. return 0;
  792. switch (action) {
  793. case BUS_NOTIFY_UNBOUND_DRIVER:
  794. count = device_dma_allocations(dev, &entry);
  795. if (count == 0)
  796. break;
  797. err_printk(dev, entry, "DMA-API: device driver has pending "
  798. "DMA allocations while released from device "
  799. "[count=%d]\n"
  800. "One of leaked entries details: "
  801. "[device address=0x%016llx] [size=%llu bytes] "
  802. "[mapped with %s] [mapped as %s]\n",
  803. count, entry->dev_addr, entry->size,
  804. dir2name[entry->direction], type2name[entry->type]);
  805. break;
  806. default:
  807. break;
  808. }
  809. return 0;
  810. }
  811. void dma_debug_add_bus(struct bus_type *bus)
  812. {
  813. struct notifier_block *nb;
  814. if (dma_debug_disabled())
  815. return;
  816. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  817. if (nb == NULL) {
  818. pr_err("dma_debug_add_bus: out of memory\n");
  819. return;
  820. }
  821. nb->notifier_call = dma_debug_device_change;
  822. bus_register_notifier(bus, nb);
  823. }
  824. /*
  825. * Let the architectures decide how many entries should be preallocated.
  826. */
  827. void dma_debug_init(u32 num_entries)
  828. {
  829. int i;
  830. /* Do not use dma_debug_initialized here, since we really want to be
  831. * called to set dma_debug_initialized
  832. */
  833. if (global_disable)
  834. return;
  835. for (i = 0; i < HASH_SIZE; ++i) {
  836. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  837. spin_lock_init(&dma_entry_hash[i].lock);
  838. }
  839. if (dma_debug_fs_init() != 0) {
  840. pr_err("DMA-API: error creating debugfs entries - disabling\n");
  841. global_disable = true;
  842. return;
  843. }
  844. if (req_entries)
  845. num_entries = req_entries;
  846. if (prealloc_memory(num_entries) != 0) {
  847. pr_err("DMA-API: debugging out of memory error - disabled\n");
  848. global_disable = true;
  849. return;
  850. }
  851. nr_total_entries = num_free_entries;
  852. dma_debug_initialized = true;
  853. pr_info("DMA-API: debugging enabled by kernel config\n");
  854. }
  855. static __init int dma_debug_cmdline(char *str)
  856. {
  857. if (!str)
  858. return -EINVAL;
  859. if (strncmp(str, "off", 3) == 0) {
  860. pr_info("DMA-API: debugging disabled on kernel command line\n");
  861. global_disable = true;
  862. }
  863. return 0;
  864. }
  865. static __init int dma_debug_entries_cmdline(char *str)
  866. {
  867. int res;
  868. if (!str)
  869. return -EINVAL;
  870. res = get_option(&str, &req_entries);
  871. if (!res)
  872. req_entries = 0;
  873. return 0;
  874. }
  875. __setup("dma_debug=", dma_debug_cmdline);
  876. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  877. static void check_unmap(struct dma_debug_entry *ref)
  878. {
  879. struct dma_debug_entry *entry;
  880. struct hash_bucket *bucket;
  881. unsigned long flags;
  882. bucket = get_hash_bucket(ref, &flags);
  883. entry = bucket_find_exact(bucket, ref);
  884. if (!entry) {
  885. /* must drop lock before calling dma_mapping_error */
  886. put_hash_bucket(bucket, &flags);
  887. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  888. err_printk(ref->dev, NULL,
  889. "DMA-API: device driver tries to free an "
  890. "invalid DMA memory address\n");
  891. } else {
  892. err_printk(ref->dev, NULL,
  893. "DMA-API: device driver tries to free DMA "
  894. "memory it has not allocated [device "
  895. "address=0x%016llx] [size=%llu bytes]\n",
  896. ref->dev_addr, ref->size);
  897. }
  898. return;
  899. }
  900. if (ref->size != entry->size) {
  901. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  902. "DMA memory with different size "
  903. "[device address=0x%016llx] [map size=%llu bytes] "
  904. "[unmap size=%llu bytes]\n",
  905. ref->dev_addr, entry->size, ref->size);
  906. }
  907. if (ref->type != entry->type) {
  908. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  909. "DMA memory with wrong function "
  910. "[device address=0x%016llx] [size=%llu bytes] "
  911. "[mapped as %s] [unmapped as %s]\n",
  912. ref->dev_addr, ref->size,
  913. type2name[entry->type], type2name[ref->type]);
  914. } else if ((entry->type == dma_debug_coherent) &&
  915. (phys_addr(ref) != phys_addr(entry))) {
  916. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  917. "DMA memory with different CPU address "
  918. "[device address=0x%016llx] [size=%llu bytes] "
  919. "[cpu alloc address=0x%016llx] "
  920. "[cpu free address=0x%016llx]",
  921. ref->dev_addr, ref->size,
  922. phys_addr(entry),
  923. phys_addr(ref));
  924. }
  925. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  926. ref->sg_call_ents != entry->sg_call_ents) {
  927. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  928. "DMA sg list with different entry count "
  929. "[map count=%d] [unmap count=%d]\n",
  930. entry->sg_call_ents, ref->sg_call_ents);
  931. }
  932. /*
  933. * This may be no bug in reality - but most implementations of the
  934. * DMA API don't handle this properly, so check for it here
  935. */
  936. if (ref->direction != entry->direction) {
  937. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  938. "DMA memory with different direction "
  939. "[device address=0x%016llx] [size=%llu bytes] "
  940. "[mapped with %s] [unmapped with %s]\n",
  941. ref->dev_addr, ref->size,
  942. dir2name[entry->direction],
  943. dir2name[ref->direction]);
  944. }
  945. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  946. err_printk(ref->dev, entry,
  947. "DMA-API: device driver failed to check map error"
  948. "[device address=0x%016llx] [size=%llu bytes] "
  949. "[mapped as %s]",
  950. ref->dev_addr, ref->size,
  951. type2name[entry->type]);
  952. }
  953. hash_bucket_del(entry);
  954. dma_entry_free(entry);
  955. put_hash_bucket(bucket, &flags);
  956. }
  957. static void check_for_stack(struct device *dev, void *addr)
  958. {
  959. if (object_is_on_stack(addr))
  960. err_printk(dev, NULL, "DMA-API: device driver maps memory from "
  961. "stack [addr=%p]\n", addr);
  962. }
  963. static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
  964. {
  965. unsigned long a1 = (unsigned long)addr;
  966. unsigned long b1 = a1 + len;
  967. unsigned long a2 = (unsigned long)start;
  968. unsigned long b2 = (unsigned long)end;
  969. return !(b1 <= a2 || a1 >= b2);
  970. }
  971. static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
  972. {
  973. if (overlap(addr, len, _text, _etext) ||
  974. overlap(addr, len, __start_rodata, __end_rodata))
  975. err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
  976. }
  977. static void check_sync(struct device *dev,
  978. struct dma_debug_entry *ref,
  979. bool to_cpu)
  980. {
  981. struct dma_debug_entry *entry;
  982. struct hash_bucket *bucket;
  983. unsigned long flags;
  984. bucket = get_hash_bucket(ref, &flags);
  985. entry = bucket_find_contain(&bucket, ref, &flags);
  986. if (!entry) {
  987. err_printk(dev, NULL, "DMA-API: device driver tries "
  988. "to sync DMA memory it has not allocated "
  989. "[device address=0x%016llx] [size=%llu bytes]\n",
  990. (unsigned long long)ref->dev_addr, ref->size);
  991. goto out;
  992. }
  993. if (ref->size > entry->size) {
  994. err_printk(dev, entry, "DMA-API: device driver syncs"
  995. " DMA memory outside allocated range "
  996. "[device address=0x%016llx] "
  997. "[allocation size=%llu bytes] "
  998. "[sync offset+size=%llu]\n",
  999. entry->dev_addr, entry->size,
  1000. ref->size);
  1001. }
  1002. if (entry->direction == DMA_BIDIRECTIONAL)
  1003. goto out;
  1004. if (ref->direction != entry->direction) {
  1005. err_printk(dev, entry, "DMA-API: device driver syncs "
  1006. "DMA memory with different direction "
  1007. "[device address=0x%016llx] [size=%llu bytes] "
  1008. "[mapped with %s] [synced with %s]\n",
  1009. (unsigned long long)ref->dev_addr, entry->size,
  1010. dir2name[entry->direction],
  1011. dir2name[ref->direction]);
  1012. }
  1013. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  1014. !(ref->direction == DMA_TO_DEVICE))
  1015. err_printk(dev, entry, "DMA-API: device driver syncs "
  1016. "device read-only DMA memory for cpu "
  1017. "[device address=0x%016llx] [size=%llu bytes] "
  1018. "[mapped with %s] [synced with %s]\n",
  1019. (unsigned long long)ref->dev_addr, entry->size,
  1020. dir2name[entry->direction],
  1021. dir2name[ref->direction]);
  1022. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  1023. !(ref->direction == DMA_FROM_DEVICE))
  1024. err_printk(dev, entry, "DMA-API: device driver syncs "
  1025. "device write-only DMA memory to device "
  1026. "[device address=0x%016llx] [size=%llu bytes] "
  1027. "[mapped with %s] [synced with %s]\n",
  1028. (unsigned long long)ref->dev_addr, entry->size,
  1029. dir2name[entry->direction],
  1030. dir2name[ref->direction]);
  1031. out:
  1032. put_hash_bucket(bucket, &flags);
  1033. }
  1034. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  1035. size_t size, int direction, dma_addr_t dma_addr,
  1036. bool map_single)
  1037. {
  1038. struct dma_debug_entry *entry;
  1039. if (unlikely(dma_debug_disabled()))
  1040. return;
  1041. if (dma_mapping_error(dev, dma_addr))
  1042. return;
  1043. entry = dma_entry_alloc();
  1044. if (!entry)
  1045. return;
  1046. entry->dev = dev;
  1047. entry->type = dma_debug_page;
  1048. entry->pfn = page_to_pfn(page);
  1049. entry->offset = offset,
  1050. entry->dev_addr = dma_addr;
  1051. entry->size = size;
  1052. entry->direction = direction;
  1053. entry->map_err_type = MAP_ERR_NOT_CHECKED;
  1054. if (map_single)
  1055. entry->type = dma_debug_single;
  1056. if (!PageHighMem(page)) {
  1057. void *addr = page_address(page) + offset;
  1058. check_for_stack(dev, addr);
  1059. check_for_illegal_area(dev, addr, size);
  1060. }
  1061. add_dma_entry(entry);
  1062. }
  1063. EXPORT_SYMBOL(debug_dma_map_page);
  1064. void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  1065. {
  1066. struct dma_debug_entry ref;
  1067. struct dma_debug_entry *entry;
  1068. struct hash_bucket *bucket;
  1069. unsigned long flags;
  1070. if (unlikely(dma_debug_disabled()))
  1071. return;
  1072. ref.dev = dev;
  1073. ref.dev_addr = dma_addr;
  1074. bucket = get_hash_bucket(&ref, &flags);
  1075. list_for_each_entry(entry, &bucket->list, list) {
  1076. if (!exact_match(&ref, entry))
  1077. continue;
  1078. /*
  1079. * The same physical address can be mapped multiple
  1080. * times. Without a hardware IOMMU this results in the
  1081. * same device addresses being put into the dma-debug
  1082. * hash multiple times too. This can result in false
  1083. * positives being reported. Therefore we implement a
  1084. * best-fit algorithm here which updates the first entry
  1085. * from the hash which fits the reference value and is
  1086. * not currently listed as being checked.
  1087. */
  1088. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  1089. entry->map_err_type = MAP_ERR_CHECKED;
  1090. break;
  1091. }
  1092. }
  1093. put_hash_bucket(bucket, &flags);
  1094. }
  1095. EXPORT_SYMBOL(debug_dma_mapping_error);
  1096. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  1097. size_t size, int direction, bool map_single)
  1098. {
  1099. struct dma_debug_entry ref = {
  1100. .type = dma_debug_page,
  1101. .dev = dev,
  1102. .dev_addr = addr,
  1103. .size = size,
  1104. .direction = direction,
  1105. };
  1106. if (unlikely(dma_debug_disabled()))
  1107. return;
  1108. if (map_single)
  1109. ref.type = dma_debug_single;
  1110. check_unmap(&ref);
  1111. }
  1112. EXPORT_SYMBOL(debug_dma_unmap_page);
  1113. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  1114. int nents, int mapped_ents, int direction)
  1115. {
  1116. struct dma_debug_entry *entry;
  1117. struct scatterlist *s;
  1118. int i;
  1119. if (unlikely(dma_debug_disabled()))
  1120. return;
  1121. for_each_sg(sg, s, mapped_ents, i) {
  1122. entry = dma_entry_alloc();
  1123. if (!entry)
  1124. return;
  1125. entry->type = dma_debug_sg;
  1126. entry->dev = dev;
  1127. entry->pfn = page_to_pfn(sg_page(s));
  1128. entry->offset = s->offset,
  1129. entry->size = sg_dma_len(s);
  1130. entry->dev_addr = sg_dma_address(s);
  1131. entry->direction = direction;
  1132. entry->sg_call_ents = nents;
  1133. entry->sg_mapped_ents = mapped_ents;
  1134. if (!PageHighMem(sg_page(s))) {
  1135. check_for_stack(dev, sg_virt(s));
  1136. check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
  1137. }
  1138. add_dma_entry(entry);
  1139. }
  1140. }
  1141. EXPORT_SYMBOL(debug_dma_map_sg);
  1142. static int get_nr_mapped_entries(struct device *dev,
  1143. struct dma_debug_entry *ref)
  1144. {
  1145. struct dma_debug_entry *entry;
  1146. struct hash_bucket *bucket;
  1147. unsigned long flags;
  1148. int mapped_ents;
  1149. bucket = get_hash_bucket(ref, &flags);
  1150. entry = bucket_find_exact(bucket, ref);
  1151. mapped_ents = 0;
  1152. if (entry)
  1153. mapped_ents = entry->sg_mapped_ents;
  1154. put_hash_bucket(bucket, &flags);
  1155. return mapped_ents;
  1156. }
  1157. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  1158. int nelems, int dir)
  1159. {
  1160. struct scatterlist *s;
  1161. int mapped_ents = 0, i;
  1162. if (unlikely(dma_debug_disabled()))
  1163. return;
  1164. for_each_sg(sglist, s, nelems, i) {
  1165. struct dma_debug_entry ref = {
  1166. .type = dma_debug_sg,
  1167. .dev = dev,
  1168. .pfn = page_to_pfn(sg_page(s)),
  1169. .offset = s->offset,
  1170. .dev_addr = sg_dma_address(s),
  1171. .size = sg_dma_len(s),
  1172. .direction = dir,
  1173. .sg_call_ents = nelems,
  1174. };
  1175. if (mapped_ents && i >= mapped_ents)
  1176. break;
  1177. if (!i)
  1178. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1179. check_unmap(&ref);
  1180. }
  1181. }
  1182. EXPORT_SYMBOL(debug_dma_unmap_sg);
  1183. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  1184. dma_addr_t dma_addr, void *virt)
  1185. {
  1186. struct dma_debug_entry *entry;
  1187. if (unlikely(dma_debug_disabled()))
  1188. return;
  1189. if (unlikely(virt == NULL))
  1190. return;
  1191. entry = dma_entry_alloc();
  1192. if (!entry)
  1193. return;
  1194. entry->type = dma_debug_coherent;
  1195. entry->dev = dev;
  1196. entry->pfn = page_to_pfn(virt_to_page(virt));
  1197. entry->offset = (size_t) virt & PAGE_MASK;
  1198. entry->size = size;
  1199. entry->dev_addr = dma_addr;
  1200. entry->direction = DMA_BIDIRECTIONAL;
  1201. add_dma_entry(entry);
  1202. }
  1203. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  1204. void debug_dma_free_coherent(struct device *dev, size_t size,
  1205. void *virt, dma_addr_t addr)
  1206. {
  1207. struct dma_debug_entry ref = {
  1208. .type = dma_debug_coherent,
  1209. .dev = dev,
  1210. .pfn = page_to_pfn(virt_to_page(virt)),
  1211. .offset = (size_t) virt & PAGE_MASK,
  1212. .dev_addr = addr,
  1213. .size = size,
  1214. .direction = DMA_BIDIRECTIONAL,
  1215. };
  1216. if (unlikely(dma_debug_disabled()))
  1217. return;
  1218. check_unmap(&ref);
  1219. }
  1220. EXPORT_SYMBOL(debug_dma_free_coherent);
  1221. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  1222. size_t size, int direction)
  1223. {
  1224. struct dma_debug_entry ref;
  1225. if (unlikely(dma_debug_disabled()))
  1226. return;
  1227. ref.type = dma_debug_single;
  1228. ref.dev = dev;
  1229. ref.dev_addr = dma_handle;
  1230. ref.size = size;
  1231. ref.direction = direction;
  1232. ref.sg_call_ents = 0;
  1233. check_sync(dev, &ref, true);
  1234. }
  1235. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  1236. void debug_dma_sync_single_for_device(struct device *dev,
  1237. dma_addr_t dma_handle, size_t size,
  1238. int direction)
  1239. {
  1240. struct dma_debug_entry ref;
  1241. if (unlikely(dma_debug_disabled()))
  1242. return;
  1243. ref.type = dma_debug_single;
  1244. ref.dev = dev;
  1245. ref.dev_addr = dma_handle;
  1246. ref.size = size;
  1247. ref.direction = direction;
  1248. ref.sg_call_ents = 0;
  1249. check_sync(dev, &ref, false);
  1250. }
  1251. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  1252. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  1253. dma_addr_t dma_handle,
  1254. unsigned long offset, size_t size,
  1255. int direction)
  1256. {
  1257. struct dma_debug_entry ref;
  1258. if (unlikely(dma_debug_disabled()))
  1259. return;
  1260. ref.type = dma_debug_single;
  1261. ref.dev = dev;
  1262. ref.dev_addr = dma_handle;
  1263. ref.size = offset + size;
  1264. ref.direction = direction;
  1265. ref.sg_call_ents = 0;
  1266. check_sync(dev, &ref, true);
  1267. }
  1268. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  1269. void debug_dma_sync_single_range_for_device(struct device *dev,
  1270. dma_addr_t dma_handle,
  1271. unsigned long offset,
  1272. size_t size, int direction)
  1273. {
  1274. struct dma_debug_entry ref;
  1275. if (unlikely(dma_debug_disabled()))
  1276. return;
  1277. ref.type = dma_debug_single;
  1278. ref.dev = dev;
  1279. ref.dev_addr = dma_handle;
  1280. ref.size = offset + size;
  1281. ref.direction = direction;
  1282. ref.sg_call_ents = 0;
  1283. check_sync(dev, &ref, false);
  1284. }
  1285. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  1286. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1287. int nelems, int direction)
  1288. {
  1289. struct scatterlist *s;
  1290. int mapped_ents = 0, i;
  1291. if (unlikely(dma_debug_disabled()))
  1292. return;
  1293. for_each_sg(sg, s, nelems, i) {
  1294. struct dma_debug_entry ref = {
  1295. .type = dma_debug_sg,
  1296. .dev = dev,
  1297. .pfn = page_to_pfn(sg_page(s)),
  1298. .offset = s->offset,
  1299. .dev_addr = sg_dma_address(s),
  1300. .size = sg_dma_len(s),
  1301. .direction = direction,
  1302. .sg_call_ents = nelems,
  1303. };
  1304. if (!i)
  1305. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1306. if (i >= mapped_ents)
  1307. break;
  1308. check_sync(dev, &ref, true);
  1309. }
  1310. }
  1311. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  1312. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1313. int nelems, int direction)
  1314. {
  1315. struct scatterlist *s;
  1316. int mapped_ents = 0, i;
  1317. if (unlikely(dma_debug_disabled()))
  1318. return;
  1319. for_each_sg(sg, s, nelems, i) {
  1320. struct dma_debug_entry ref = {
  1321. .type = dma_debug_sg,
  1322. .dev = dev,
  1323. .pfn = page_to_pfn(sg_page(s)),
  1324. .offset = s->offset,
  1325. .dev_addr = sg_dma_address(s),
  1326. .size = sg_dma_len(s),
  1327. .direction = direction,
  1328. .sg_call_ents = nelems,
  1329. };
  1330. if (!i)
  1331. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1332. if (i >= mapped_ents)
  1333. break;
  1334. check_sync(dev, &ref, false);
  1335. }
  1336. }
  1337. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
  1338. static int __init dma_debug_driver_setup(char *str)
  1339. {
  1340. int i;
  1341. for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
  1342. current_driver_name[i] = *str;
  1343. if (*str == 0)
  1344. break;
  1345. }
  1346. if (current_driver_name[0])
  1347. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  1348. current_driver_name);
  1349. return 1;
  1350. }
  1351. __setup("dma_debug_driver=", dma_debug_driver_setup);