dma-debug.c 45 KB

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