debug.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  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 nr_prealloc_entries = PREALLOC_DMA_DEBUG_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. cond_resched();
  377. }
  378. }
  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. /*
  630. * DMA-API debugging init code
  631. *
  632. * The init code does two things:
  633. * 1. Initialize core data structures
  634. * 2. Preallocate a given number of dma_debug_entry structs
  635. */
  636. static int prealloc_memory(u32 num_entries)
  637. {
  638. struct dma_debug_entry *entry, *next_entry;
  639. int i;
  640. for (i = 0; i < num_entries; ++i) {
  641. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  642. if (!entry)
  643. goto out_err;
  644. list_add_tail(&entry->list, &free_entries);
  645. }
  646. num_free_entries = num_entries;
  647. min_free_entries = num_entries;
  648. pr_info("DMA-API: preallocated %d debug entries\n", num_entries);
  649. return 0;
  650. out_err:
  651. list_for_each_entry_safe(entry, next_entry, &free_entries, list) {
  652. list_del(&entry->list);
  653. kfree(entry);
  654. }
  655. return -ENOMEM;
  656. }
  657. static ssize_t filter_read(struct file *file, char __user *user_buf,
  658. size_t count, loff_t *ppos)
  659. {
  660. char buf[NAME_MAX_LEN + 1];
  661. unsigned long flags;
  662. int len;
  663. if (!current_driver_name[0])
  664. return 0;
  665. /*
  666. * We can't copy to userspace directly because current_driver_name can
  667. * only be read under the driver_name_lock with irqs disabled. So
  668. * create a temporary copy first.
  669. */
  670. read_lock_irqsave(&driver_name_lock, flags);
  671. len = scnprintf(buf, NAME_MAX_LEN + 1, "%s\n", current_driver_name);
  672. read_unlock_irqrestore(&driver_name_lock, flags);
  673. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  674. }
  675. static ssize_t filter_write(struct file *file, const char __user *userbuf,
  676. size_t count, loff_t *ppos)
  677. {
  678. char buf[NAME_MAX_LEN];
  679. unsigned long flags;
  680. size_t len;
  681. int i;
  682. /*
  683. * We can't copy from userspace directly. Access to
  684. * current_driver_name is protected with a write_lock with irqs
  685. * disabled. Since copy_from_user can fault and may sleep we
  686. * need to copy to temporary buffer first
  687. */
  688. len = min(count, (size_t)(NAME_MAX_LEN - 1));
  689. if (copy_from_user(buf, userbuf, len))
  690. return -EFAULT;
  691. buf[len] = 0;
  692. write_lock_irqsave(&driver_name_lock, flags);
  693. /*
  694. * Now handle the string we got from userspace very carefully.
  695. * The rules are:
  696. * - only use the first token we got
  697. * - token delimiter is everything looking like a space
  698. * character (' ', '\n', '\t' ...)
  699. *
  700. */
  701. if (!isalnum(buf[0])) {
  702. /*
  703. * If the first character userspace gave us is not
  704. * alphanumerical then assume the filter should be
  705. * switched off.
  706. */
  707. if (current_driver_name[0])
  708. pr_info("DMA-API: switching off dma-debug driver filter\n");
  709. current_driver_name[0] = 0;
  710. current_driver = NULL;
  711. goto out_unlock;
  712. }
  713. /*
  714. * Now parse out the first token and use it as the name for the
  715. * driver to filter for.
  716. */
  717. for (i = 0; i < NAME_MAX_LEN - 1; ++i) {
  718. current_driver_name[i] = buf[i];
  719. if (isspace(buf[i]) || buf[i] == ' ' || buf[i] == 0)
  720. break;
  721. }
  722. current_driver_name[i] = 0;
  723. current_driver = NULL;
  724. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  725. current_driver_name);
  726. out_unlock:
  727. write_unlock_irqrestore(&driver_name_lock, flags);
  728. return count;
  729. }
  730. static const struct file_operations filter_fops = {
  731. .read = filter_read,
  732. .write = filter_write,
  733. .llseek = default_llseek,
  734. };
  735. static int dma_debug_fs_init(void)
  736. {
  737. dma_debug_dent = debugfs_create_dir("dma-api", NULL);
  738. if (!dma_debug_dent) {
  739. pr_err("DMA-API: can not create debugfs directory\n");
  740. return -ENOMEM;
  741. }
  742. global_disable_dent = debugfs_create_bool("disabled", 0444,
  743. dma_debug_dent,
  744. &global_disable);
  745. if (!global_disable_dent)
  746. goto out_err;
  747. error_count_dent = debugfs_create_u32("error_count", 0444,
  748. dma_debug_dent, &error_count);
  749. if (!error_count_dent)
  750. goto out_err;
  751. show_all_errors_dent = debugfs_create_u32("all_errors", 0644,
  752. dma_debug_dent,
  753. &show_all_errors);
  754. if (!show_all_errors_dent)
  755. goto out_err;
  756. show_num_errors_dent = debugfs_create_u32("num_errors", 0644,
  757. dma_debug_dent,
  758. &show_num_errors);
  759. if (!show_num_errors_dent)
  760. goto out_err;
  761. num_free_entries_dent = debugfs_create_u32("num_free_entries", 0444,
  762. dma_debug_dent,
  763. &num_free_entries);
  764. if (!num_free_entries_dent)
  765. goto out_err;
  766. min_free_entries_dent = debugfs_create_u32("min_free_entries", 0444,
  767. dma_debug_dent,
  768. &min_free_entries);
  769. if (!min_free_entries_dent)
  770. goto out_err;
  771. filter_dent = debugfs_create_file("driver_filter", 0644,
  772. dma_debug_dent, NULL, &filter_fops);
  773. if (!filter_dent)
  774. goto out_err;
  775. return 0;
  776. out_err:
  777. debugfs_remove_recursive(dma_debug_dent);
  778. return -ENOMEM;
  779. }
  780. static int device_dma_allocations(struct device *dev, struct dma_debug_entry **out_entry)
  781. {
  782. struct dma_debug_entry *entry;
  783. unsigned long flags;
  784. int count = 0, i;
  785. for (i = 0; i < HASH_SIZE; ++i) {
  786. spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
  787. list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
  788. if (entry->dev == dev) {
  789. count += 1;
  790. *out_entry = entry;
  791. }
  792. }
  793. spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
  794. }
  795. return count;
  796. }
  797. static int dma_debug_device_change(struct notifier_block *nb, unsigned long action, void *data)
  798. {
  799. struct device *dev = data;
  800. struct dma_debug_entry *uninitialized_var(entry);
  801. int count;
  802. if (dma_debug_disabled())
  803. return 0;
  804. switch (action) {
  805. case BUS_NOTIFY_UNBOUND_DRIVER:
  806. count = device_dma_allocations(dev, &entry);
  807. if (count == 0)
  808. break;
  809. err_printk(dev, entry, "DMA-API: device driver has pending "
  810. "DMA allocations while released from device "
  811. "[count=%d]\n"
  812. "One of leaked entries details: "
  813. "[device address=0x%016llx] [size=%llu bytes] "
  814. "[mapped with %s] [mapped as %s]\n",
  815. count, entry->dev_addr, entry->size,
  816. dir2name[entry->direction], type2name[entry->type]);
  817. break;
  818. default:
  819. break;
  820. }
  821. return 0;
  822. }
  823. void dma_debug_add_bus(struct bus_type *bus)
  824. {
  825. struct notifier_block *nb;
  826. if (dma_debug_disabled())
  827. return;
  828. nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
  829. if (nb == NULL) {
  830. pr_err("dma_debug_add_bus: out of memory\n");
  831. return;
  832. }
  833. nb->notifier_call = dma_debug_device_change;
  834. bus_register_notifier(bus, nb);
  835. }
  836. static int dma_debug_init(void)
  837. {
  838. int i;
  839. /* Do not use dma_debug_initialized here, since we really want to be
  840. * called to set dma_debug_initialized
  841. */
  842. if (global_disable)
  843. return 0;
  844. for (i = 0; i < HASH_SIZE; ++i) {
  845. INIT_LIST_HEAD(&dma_entry_hash[i].list);
  846. spin_lock_init(&dma_entry_hash[i].lock);
  847. }
  848. if (dma_debug_fs_init() != 0) {
  849. pr_err("DMA-API: error creating debugfs entries - disabling\n");
  850. global_disable = true;
  851. return 0;
  852. }
  853. if (prealloc_memory(nr_prealloc_entries) != 0) {
  854. pr_err("DMA-API: debugging out of memory error - disabled\n");
  855. global_disable = true;
  856. return 0;
  857. }
  858. nr_total_entries = num_free_entries;
  859. dma_debug_initialized = true;
  860. pr_info("DMA-API: debugging enabled by kernel config\n");
  861. return 0;
  862. }
  863. core_initcall(dma_debug_init);
  864. static __init int dma_debug_cmdline(char *str)
  865. {
  866. if (!str)
  867. return -EINVAL;
  868. if (strncmp(str, "off", 3) == 0) {
  869. pr_info("DMA-API: debugging disabled on kernel command line\n");
  870. global_disable = true;
  871. }
  872. return 0;
  873. }
  874. static __init int dma_debug_entries_cmdline(char *str)
  875. {
  876. if (!str)
  877. return -EINVAL;
  878. if (!get_option(&str, &nr_prealloc_entries))
  879. nr_prealloc_entries = PREALLOC_DMA_DEBUG_ENTRIES;
  880. return 0;
  881. }
  882. __setup("dma_debug=", dma_debug_cmdline);
  883. __setup("dma_debug_entries=", dma_debug_entries_cmdline);
  884. static void check_unmap(struct dma_debug_entry *ref)
  885. {
  886. struct dma_debug_entry *entry;
  887. struct hash_bucket *bucket;
  888. unsigned long flags;
  889. bucket = get_hash_bucket(ref, &flags);
  890. entry = bucket_find_exact(bucket, ref);
  891. if (!entry) {
  892. /* must drop lock before calling dma_mapping_error */
  893. put_hash_bucket(bucket, &flags);
  894. if (dma_mapping_error(ref->dev, ref->dev_addr)) {
  895. err_printk(ref->dev, NULL,
  896. "DMA-API: device driver tries to free an "
  897. "invalid DMA memory address\n");
  898. } else {
  899. err_printk(ref->dev, NULL,
  900. "DMA-API: device driver tries to free DMA "
  901. "memory it has not allocated [device "
  902. "address=0x%016llx] [size=%llu bytes]\n",
  903. ref->dev_addr, ref->size);
  904. }
  905. return;
  906. }
  907. if (ref->size != entry->size) {
  908. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  909. "DMA memory with different size "
  910. "[device address=0x%016llx] [map size=%llu bytes] "
  911. "[unmap size=%llu bytes]\n",
  912. ref->dev_addr, entry->size, ref->size);
  913. }
  914. if (ref->type != entry->type) {
  915. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  916. "DMA memory with wrong function "
  917. "[device address=0x%016llx] [size=%llu bytes] "
  918. "[mapped as %s] [unmapped as %s]\n",
  919. ref->dev_addr, ref->size,
  920. type2name[entry->type], type2name[ref->type]);
  921. } else if ((entry->type == dma_debug_coherent) &&
  922. (phys_addr(ref) != phys_addr(entry))) {
  923. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  924. "DMA memory with different CPU address "
  925. "[device address=0x%016llx] [size=%llu bytes] "
  926. "[cpu alloc address=0x%016llx] "
  927. "[cpu free address=0x%016llx]",
  928. ref->dev_addr, ref->size,
  929. phys_addr(entry),
  930. phys_addr(ref));
  931. }
  932. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  933. ref->sg_call_ents != entry->sg_call_ents) {
  934. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  935. "DMA sg list with different entry count "
  936. "[map count=%d] [unmap count=%d]\n",
  937. entry->sg_call_ents, ref->sg_call_ents);
  938. }
  939. /*
  940. * This may be no bug in reality - but most implementations of the
  941. * DMA API don't handle this properly, so check for it here
  942. */
  943. if (ref->direction != entry->direction) {
  944. err_printk(ref->dev, entry, "DMA-API: device driver frees "
  945. "DMA memory with different direction "
  946. "[device address=0x%016llx] [size=%llu bytes] "
  947. "[mapped with %s] [unmapped with %s]\n",
  948. ref->dev_addr, ref->size,
  949. dir2name[entry->direction],
  950. dir2name[ref->direction]);
  951. }
  952. /*
  953. * Drivers should use dma_mapping_error() to check the returned
  954. * addresses of dma_map_single() and dma_map_page().
  955. * If not, print this warning message. See Documentation/DMA-API.txt.
  956. */
  957. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  958. err_printk(ref->dev, entry,
  959. "DMA-API: device driver failed to check map error"
  960. "[device address=0x%016llx] [size=%llu bytes] "
  961. "[mapped as %s]",
  962. ref->dev_addr, ref->size,
  963. type2name[entry->type]);
  964. }
  965. hash_bucket_del(entry);
  966. dma_entry_free(entry);
  967. put_hash_bucket(bucket, &flags);
  968. }
  969. static void check_for_stack(struct device *dev,
  970. struct page *page, size_t offset)
  971. {
  972. void *addr;
  973. struct vm_struct *stack_vm_area = task_stack_vm_area(current);
  974. if (!stack_vm_area) {
  975. /* Stack is direct-mapped. */
  976. if (PageHighMem(page))
  977. return;
  978. addr = page_address(page) + offset;
  979. if (object_is_on_stack(addr))
  980. err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [addr=%p]\n", addr);
  981. } else {
  982. /* Stack is vmalloced. */
  983. int i;
  984. for (i = 0; i < stack_vm_area->nr_pages; i++) {
  985. if (page != stack_vm_area->pages[i])
  986. continue;
  987. addr = (u8 *)current->stack + i * PAGE_SIZE + offset;
  988. err_printk(dev, NULL, "DMA-API: device driver maps memory from stack [probable addr=%p]\n", addr);
  989. break;
  990. }
  991. }
  992. }
  993. static inline bool overlap(void *addr, unsigned long len, void *start, void *end)
  994. {
  995. unsigned long a1 = (unsigned long)addr;
  996. unsigned long b1 = a1 + len;
  997. unsigned long a2 = (unsigned long)start;
  998. unsigned long b2 = (unsigned long)end;
  999. return !(b1 <= a2 || a1 >= b2);
  1000. }
  1001. static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
  1002. {
  1003. if (overlap(addr, len, _stext, _etext) ||
  1004. overlap(addr, len, __start_rodata, __end_rodata))
  1005. err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
  1006. }
  1007. static void check_sync(struct device *dev,
  1008. struct dma_debug_entry *ref,
  1009. bool to_cpu)
  1010. {
  1011. struct dma_debug_entry *entry;
  1012. struct hash_bucket *bucket;
  1013. unsigned long flags;
  1014. bucket = get_hash_bucket(ref, &flags);
  1015. entry = bucket_find_contain(&bucket, ref, &flags);
  1016. if (!entry) {
  1017. err_printk(dev, NULL, "DMA-API: device driver tries "
  1018. "to sync DMA memory it has not allocated "
  1019. "[device address=0x%016llx] [size=%llu bytes]\n",
  1020. (unsigned long long)ref->dev_addr, ref->size);
  1021. goto out;
  1022. }
  1023. if (ref->size > entry->size) {
  1024. err_printk(dev, entry, "DMA-API: device driver syncs"
  1025. " DMA memory outside allocated range "
  1026. "[device address=0x%016llx] "
  1027. "[allocation size=%llu bytes] "
  1028. "[sync offset+size=%llu]\n",
  1029. entry->dev_addr, entry->size,
  1030. ref->size);
  1031. }
  1032. if (entry->direction == DMA_BIDIRECTIONAL)
  1033. goto out;
  1034. if (ref->direction != entry->direction) {
  1035. err_printk(dev, entry, "DMA-API: device driver syncs "
  1036. "DMA memory with different direction "
  1037. "[device address=0x%016llx] [size=%llu bytes] "
  1038. "[mapped with %s] [synced with %s]\n",
  1039. (unsigned long long)ref->dev_addr, entry->size,
  1040. dir2name[entry->direction],
  1041. dir2name[ref->direction]);
  1042. }
  1043. if (to_cpu && !(entry->direction == DMA_FROM_DEVICE) &&
  1044. !(ref->direction == DMA_TO_DEVICE))
  1045. err_printk(dev, entry, "DMA-API: device driver syncs "
  1046. "device read-only DMA memory for cpu "
  1047. "[device address=0x%016llx] [size=%llu bytes] "
  1048. "[mapped with %s] [synced with %s]\n",
  1049. (unsigned long long)ref->dev_addr, entry->size,
  1050. dir2name[entry->direction],
  1051. dir2name[ref->direction]);
  1052. if (!to_cpu && !(entry->direction == DMA_TO_DEVICE) &&
  1053. !(ref->direction == DMA_FROM_DEVICE))
  1054. err_printk(dev, entry, "DMA-API: device driver syncs "
  1055. "device write-only DMA memory to device "
  1056. "[device address=0x%016llx] [size=%llu bytes] "
  1057. "[mapped with %s] [synced with %s]\n",
  1058. (unsigned long long)ref->dev_addr, entry->size,
  1059. dir2name[entry->direction],
  1060. dir2name[ref->direction]);
  1061. if (ref->sg_call_ents && ref->type == dma_debug_sg &&
  1062. ref->sg_call_ents != entry->sg_call_ents) {
  1063. err_printk(ref->dev, entry, "DMA-API: device driver syncs "
  1064. "DMA sg list with different entry count "
  1065. "[map count=%d] [sync count=%d]\n",
  1066. entry->sg_call_ents, ref->sg_call_ents);
  1067. }
  1068. out:
  1069. put_hash_bucket(bucket, &flags);
  1070. }
  1071. static void check_sg_segment(struct device *dev, struct scatterlist *sg)
  1072. {
  1073. #ifdef CONFIG_DMA_API_DEBUG_SG
  1074. unsigned int max_seg = dma_get_max_seg_size(dev);
  1075. u64 start, end, boundary = dma_get_seg_boundary(dev);
  1076. /*
  1077. * Either the driver forgot to set dma_parms appropriately, or
  1078. * whoever generated the list forgot to check them.
  1079. */
  1080. if (sg->length > max_seg)
  1081. err_printk(dev, NULL, "DMA-API: mapping sg segment longer than device claims to support [len=%u] [max=%u]\n",
  1082. sg->length, max_seg);
  1083. /*
  1084. * In some cases this could potentially be the DMA API
  1085. * implementation's fault, but it would usually imply that
  1086. * the scatterlist was built inappropriately to begin with.
  1087. */
  1088. start = sg_dma_address(sg);
  1089. end = start + sg_dma_len(sg) - 1;
  1090. if ((start ^ end) & ~boundary)
  1091. err_printk(dev, NULL, "DMA-API: mapping sg segment across boundary [start=0x%016llx] [end=0x%016llx] [boundary=0x%016llx]\n",
  1092. start, end, boundary);
  1093. #endif
  1094. }
  1095. void debug_dma_map_page(struct device *dev, struct page *page, size_t offset,
  1096. size_t size, int direction, dma_addr_t dma_addr,
  1097. bool map_single)
  1098. {
  1099. struct dma_debug_entry *entry;
  1100. if (unlikely(dma_debug_disabled()))
  1101. return;
  1102. if (dma_mapping_error(dev, dma_addr))
  1103. return;
  1104. entry = dma_entry_alloc();
  1105. if (!entry)
  1106. return;
  1107. entry->dev = dev;
  1108. entry->type = dma_debug_page;
  1109. entry->pfn = page_to_pfn(page);
  1110. entry->offset = offset,
  1111. entry->dev_addr = dma_addr;
  1112. entry->size = size;
  1113. entry->direction = direction;
  1114. entry->map_err_type = MAP_ERR_NOT_CHECKED;
  1115. if (map_single)
  1116. entry->type = dma_debug_single;
  1117. check_for_stack(dev, page, offset);
  1118. if (!PageHighMem(page)) {
  1119. void *addr = page_address(page) + offset;
  1120. check_for_illegal_area(dev, addr, size);
  1121. }
  1122. add_dma_entry(entry);
  1123. }
  1124. EXPORT_SYMBOL(debug_dma_map_page);
  1125. void debug_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  1126. {
  1127. struct dma_debug_entry ref;
  1128. struct dma_debug_entry *entry;
  1129. struct hash_bucket *bucket;
  1130. unsigned long flags;
  1131. if (unlikely(dma_debug_disabled()))
  1132. return;
  1133. ref.dev = dev;
  1134. ref.dev_addr = dma_addr;
  1135. bucket = get_hash_bucket(&ref, &flags);
  1136. list_for_each_entry(entry, &bucket->list, list) {
  1137. if (!exact_match(&ref, entry))
  1138. continue;
  1139. /*
  1140. * The same physical address can be mapped multiple
  1141. * times. Without a hardware IOMMU this results in the
  1142. * same device addresses being put into the dma-debug
  1143. * hash multiple times too. This can result in false
  1144. * positives being reported. Therefore we implement a
  1145. * best-fit algorithm here which updates the first entry
  1146. * from the hash which fits the reference value and is
  1147. * not currently listed as being checked.
  1148. */
  1149. if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
  1150. entry->map_err_type = MAP_ERR_CHECKED;
  1151. break;
  1152. }
  1153. }
  1154. put_hash_bucket(bucket, &flags);
  1155. }
  1156. EXPORT_SYMBOL(debug_dma_mapping_error);
  1157. void debug_dma_unmap_page(struct device *dev, dma_addr_t addr,
  1158. size_t size, int direction, bool map_single)
  1159. {
  1160. struct dma_debug_entry ref = {
  1161. .type = dma_debug_page,
  1162. .dev = dev,
  1163. .dev_addr = addr,
  1164. .size = size,
  1165. .direction = direction,
  1166. };
  1167. if (unlikely(dma_debug_disabled()))
  1168. return;
  1169. if (map_single)
  1170. ref.type = dma_debug_single;
  1171. check_unmap(&ref);
  1172. }
  1173. EXPORT_SYMBOL(debug_dma_unmap_page);
  1174. void debug_dma_map_sg(struct device *dev, struct scatterlist *sg,
  1175. int nents, int mapped_ents, int direction)
  1176. {
  1177. struct dma_debug_entry *entry;
  1178. struct scatterlist *s;
  1179. int i;
  1180. if (unlikely(dma_debug_disabled()))
  1181. return;
  1182. for_each_sg(sg, s, mapped_ents, i) {
  1183. entry = dma_entry_alloc();
  1184. if (!entry)
  1185. return;
  1186. entry->type = dma_debug_sg;
  1187. entry->dev = dev;
  1188. entry->pfn = page_to_pfn(sg_page(s));
  1189. entry->offset = s->offset,
  1190. entry->size = sg_dma_len(s);
  1191. entry->dev_addr = sg_dma_address(s);
  1192. entry->direction = direction;
  1193. entry->sg_call_ents = nents;
  1194. entry->sg_mapped_ents = mapped_ents;
  1195. check_for_stack(dev, sg_page(s), s->offset);
  1196. if (!PageHighMem(sg_page(s))) {
  1197. check_for_illegal_area(dev, sg_virt(s), sg_dma_len(s));
  1198. }
  1199. check_sg_segment(dev, s);
  1200. add_dma_entry(entry);
  1201. }
  1202. }
  1203. EXPORT_SYMBOL(debug_dma_map_sg);
  1204. static int get_nr_mapped_entries(struct device *dev,
  1205. struct dma_debug_entry *ref)
  1206. {
  1207. struct dma_debug_entry *entry;
  1208. struct hash_bucket *bucket;
  1209. unsigned long flags;
  1210. int mapped_ents;
  1211. bucket = get_hash_bucket(ref, &flags);
  1212. entry = bucket_find_exact(bucket, ref);
  1213. mapped_ents = 0;
  1214. if (entry)
  1215. mapped_ents = entry->sg_mapped_ents;
  1216. put_hash_bucket(bucket, &flags);
  1217. return mapped_ents;
  1218. }
  1219. void debug_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  1220. int nelems, int dir)
  1221. {
  1222. struct scatterlist *s;
  1223. int mapped_ents = 0, i;
  1224. if (unlikely(dma_debug_disabled()))
  1225. return;
  1226. for_each_sg(sglist, s, nelems, i) {
  1227. struct dma_debug_entry ref = {
  1228. .type = dma_debug_sg,
  1229. .dev = dev,
  1230. .pfn = page_to_pfn(sg_page(s)),
  1231. .offset = s->offset,
  1232. .dev_addr = sg_dma_address(s),
  1233. .size = sg_dma_len(s),
  1234. .direction = dir,
  1235. .sg_call_ents = nelems,
  1236. };
  1237. if (mapped_ents && i >= mapped_ents)
  1238. break;
  1239. if (!i)
  1240. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1241. check_unmap(&ref);
  1242. }
  1243. }
  1244. EXPORT_SYMBOL(debug_dma_unmap_sg);
  1245. void debug_dma_alloc_coherent(struct device *dev, size_t size,
  1246. dma_addr_t dma_addr, void *virt)
  1247. {
  1248. struct dma_debug_entry *entry;
  1249. if (unlikely(dma_debug_disabled()))
  1250. return;
  1251. if (unlikely(virt == NULL))
  1252. return;
  1253. /* handle vmalloc and linear addresses */
  1254. if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
  1255. return;
  1256. entry = dma_entry_alloc();
  1257. if (!entry)
  1258. return;
  1259. entry->type = dma_debug_coherent;
  1260. entry->dev = dev;
  1261. entry->offset = offset_in_page(virt);
  1262. entry->size = size;
  1263. entry->dev_addr = dma_addr;
  1264. entry->direction = DMA_BIDIRECTIONAL;
  1265. if (is_vmalloc_addr(virt))
  1266. entry->pfn = vmalloc_to_pfn(virt);
  1267. else
  1268. entry->pfn = page_to_pfn(virt_to_page(virt));
  1269. add_dma_entry(entry);
  1270. }
  1271. EXPORT_SYMBOL(debug_dma_alloc_coherent);
  1272. void debug_dma_free_coherent(struct device *dev, size_t size,
  1273. void *virt, dma_addr_t addr)
  1274. {
  1275. struct dma_debug_entry ref = {
  1276. .type = dma_debug_coherent,
  1277. .dev = dev,
  1278. .offset = offset_in_page(virt),
  1279. .dev_addr = addr,
  1280. .size = size,
  1281. .direction = DMA_BIDIRECTIONAL,
  1282. };
  1283. /* handle vmalloc and linear addresses */
  1284. if (!is_vmalloc_addr(virt) && !virt_addr_valid(virt))
  1285. return;
  1286. if (is_vmalloc_addr(virt))
  1287. ref.pfn = vmalloc_to_pfn(virt);
  1288. else
  1289. ref.pfn = page_to_pfn(virt_to_page(virt));
  1290. if (unlikely(dma_debug_disabled()))
  1291. return;
  1292. check_unmap(&ref);
  1293. }
  1294. EXPORT_SYMBOL(debug_dma_free_coherent);
  1295. void debug_dma_map_resource(struct device *dev, phys_addr_t addr, size_t size,
  1296. int direction, dma_addr_t dma_addr)
  1297. {
  1298. struct dma_debug_entry *entry;
  1299. if (unlikely(dma_debug_disabled()))
  1300. return;
  1301. entry = dma_entry_alloc();
  1302. if (!entry)
  1303. return;
  1304. entry->type = dma_debug_resource;
  1305. entry->dev = dev;
  1306. entry->pfn = PHYS_PFN(addr);
  1307. entry->offset = offset_in_page(addr);
  1308. entry->size = size;
  1309. entry->dev_addr = dma_addr;
  1310. entry->direction = direction;
  1311. entry->map_err_type = MAP_ERR_NOT_CHECKED;
  1312. add_dma_entry(entry);
  1313. }
  1314. EXPORT_SYMBOL(debug_dma_map_resource);
  1315. void debug_dma_unmap_resource(struct device *dev, dma_addr_t dma_addr,
  1316. size_t size, int direction)
  1317. {
  1318. struct dma_debug_entry ref = {
  1319. .type = dma_debug_resource,
  1320. .dev = dev,
  1321. .dev_addr = dma_addr,
  1322. .size = size,
  1323. .direction = direction,
  1324. };
  1325. if (unlikely(dma_debug_disabled()))
  1326. return;
  1327. check_unmap(&ref);
  1328. }
  1329. EXPORT_SYMBOL(debug_dma_unmap_resource);
  1330. void debug_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  1331. size_t size, int direction)
  1332. {
  1333. struct dma_debug_entry ref;
  1334. if (unlikely(dma_debug_disabled()))
  1335. return;
  1336. ref.type = dma_debug_single;
  1337. ref.dev = dev;
  1338. ref.dev_addr = dma_handle;
  1339. ref.size = size;
  1340. ref.direction = direction;
  1341. ref.sg_call_ents = 0;
  1342. check_sync(dev, &ref, true);
  1343. }
  1344. EXPORT_SYMBOL(debug_dma_sync_single_for_cpu);
  1345. void debug_dma_sync_single_for_device(struct device *dev,
  1346. dma_addr_t dma_handle, 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 = size;
  1356. ref.direction = direction;
  1357. ref.sg_call_ents = 0;
  1358. check_sync(dev, &ref, false);
  1359. }
  1360. EXPORT_SYMBOL(debug_dma_sync_single_for_device);
  1361. void debug_dma_sync_single_range_for_cpu(struct device *dev,
  1362. dma_addr_t dma_handle,
  1363. unsigned long offset, size_t size,
  1364. 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, true);
  1376. }
  1377. EXPORT_SYMBOL(debug_dma_sync_single_range_for_cpu);
  1378. void debug_dma_sync_single_range_for_device(struct device *dev,
  1379. dma_addr_t dma_handle,
  1380. unsigned long offset,
  1381. size_t size, int direction)
  1382. {
  1383. struct dma_debug_entry ref;
  1384. if (unlikely(dma_debug_disabled()))
  1385. return;
  1386. ref.type = dma_debug_single;
  1387. ref.dev = dev;
  1388. ref.dev_addr = dma_handle;
  1389. ref.size = offset + size;
  1390. ref.direction = direction;
  1391. ref.sg_call_ents = 0;
  1392. check_sync(dev, &ref, false);
  1393. }
  1394. EXPORT_SYMBOL(debug_dma_sync_single_range_for_device);
  1395. void debug_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  1396. int nelems, int direction)
  1397. {
  1398. struct scatterlist *s;
  1399. int mapped_ents = 0, i;
  1400. if (unlikely(dma_debug_disabled()))
  1401. return;
  1402. for_each_sg(sg, s, nelems, i) {
  1403. struct dma_debug_entry ref = {
  1404. .type = dma_debug_sg,
  1405. .dev = dev,
  1406. .pfn = page_to_pfn(sg_page(s)),
  1407. .offset = s->offset,
  1408. .dev_addr = sg_dma_address(s),
  1409. .size = sg_dma_len(s),
  1410. .direction = direction,
  1411. .sg_call_ents = nelems,
  1412. };
  1413. if (!i)
  1414. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1415. if (i >= mapped_ents)
  1416. break;
  1417. check_sync(dev, &ref, true);
  1418. }
  1419. }
  1420. EXPORT_SYMBOL(debug_dma_sync_sg_for_cpu);
  1421. void debug_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  1422. int nelems, int direction)
  1423. {
  1424. struct scatterlist *s;
  1425. int mapped_ents = 0, i;
  1426. if (unlikely(dma_debug_disabled()))
  1427. return;
  1428. for_each_sg(sg, s, nelems, i) {
  1429. struct dma_debug_entry ref = {
  1430. .type = dma_debug_sg,
  1431. .dev = dev,
  1432. .pfn = page_to_pfn(sg_page(s)),
  1433. .offset = s->offset,
  1434. .dev_addr = sg_dma_address(s),
  1435. .size = sg_dma_len(s),
  1436. .direction = direction,
  1437. .sg_call_ents = nelems,
  1438. };
  1439. if (!i)
  1440. mapped_ents = get_nr_mapped_entries(dev, &ref);
  1441. if (i >= mapped_ents)
  1442. break;
  1443. check_sync(dev, &ref, false);
  1444. }
  1445. }
  1446. EXPORT_SYMBOL(debug_dma_sync_sg_for_device);
  1447. static int __init dma_debug_driver_setup(char *str)
  1448. {
  1449. int i;
  1450. for (i = 0; i < NAME_MAX_LEN - 1; ++i, ++str) {
  1451. current_driver_name[i] = *str;
  1452. if (*str == 0)
  1453. break;
  1454. }
  1455. if (current_driver_name[0])
  1456. pr_info("DMA-API: enable driver filter for driver [%s]\n",
  1457. current_driver_name);
  1458. return 1;
  1459. }
  1460. __setup("dma_debug_driver=", dma_debug_driver_setup);