dma-debug.c 44 KB

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