dma-debug.c 43 KB

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