dma-debug.c 44 KB

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