dma-debug.c 44 KB

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