debug.c 42 KB

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