dma-debug.c 42 KB

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