debug.c 43 KB

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